compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #ifndef __LINUX_COMPILER_H
  2. #define __LINUX_COMPILER_H
  3. #ifndef __ASSEMBLY__
  4. #ifdef __CHECKER__
  5. # define __user __attribute__((noderef, address_space(1)))
  6. # define __kernel __attribute__((address_space(0)))
  7. # define __safe __attribute__((safe))
  8. # define __force __attribute__((force))
  9. # define __nocast __attribute__((nocast))
  10. # define __iomem __attribute__((noderef, address_space(2)))
  11. # define __must_hold(x) __attribute__((context(x,1,1)))
  12. # define __acquires(x) __attribute__((context(x,0,1)))
  13. # define __releases(x) __attribute__((context(x,1,0)))
  14. # define __acquire(x) __context__(x,1)
  15. # define __release(x) __context__(x,-1)
  16. # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
  17. # define __percpu __attribute__((noderef, address_space(3)))
  18. #ifdef CONFIG_SPARSE_RCU_POINTER
  19. # define __rcu __attribute__((noderef, address_space(4)))
  20. #else
  21. # define __rcu
  22. #endif
  23. extern void __chk_user_ptr(const volatile void __user *);
  24. extern void __chk_io_ptr(const volatile void __iomem *);
  25. #else
  26. # define __user
  27. # define __kernel
  28. # define __safe
  29. # define __force
  30. # define __nocast
  31. # define __iomem
  32. # define __chk_user_ptr(x) (void)0
  33. # define __chk_io_ptr(x) (void)0
  34. # define __builtin_warning(x, y...) (1)
  35. # define __must_hold(x)
  36. # define __acquires(x)
  37. # define __releases(x)
  38. # define __acquire(x) (void)0
  39. # define __release(x) (void)0
  40. # define __cond_lock(x,c) (c)
  41. # define __percpu
  42. # define __rcu
  43. #endif
  44. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  45. #define ___PASTE(a,b) a##b
  46. #define __PASTE(a,b) ___PASTE(a,b)
  47. #ifdef __KERNEL__
  48. #ifdef __GNUC__
  49. #include <linux/compiler-gcc.h>
  50. #endif
  51. #define notrace __attribute__((no_instrument_function))
  52. /* Intel compiler defines __GNUC__. So we will overwrite implementations
  53. * coming from above header files here
  54. */
  55. #ifdef __INTEL_COMPILER
  56. # include <linux/compiler-intel.h>
  57. #endif
  58. /*
  59. * Generic compiler-dependent macros required for kernel
  60. * build go below this comment. Actual compiler/compiler version
  61. * specific implementations come from the above header files
  62. */
  63. struct ftrace_branch_data {
  64. const char *func;
  65. const char *file;
  66. unsigned line;
  67. union {
  68. struct {
  69. unsigned long correct;
  70. unsigned long incorrect;
  71. };
  72. struct {
  73. unsigned long miss;
  74. unsigned long hit;
  75. };
  76. unsigned long miss_hit[2];
  77. };
  78. };
  79. /*
  80. * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
  81. * to disable branch tracing on a per file basis.
  82. */
  83. #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
  84. && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
  85. void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
  86. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  87. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  88. #define __branch_check__(x, expect) ({ \
  89. int ______r; \
  90. static struct ftrace_branch_data \
  91. __attribute__((__aligned__(4))) \
  92. __attribute__((section("_ftrace_annotated_branch"))) \
  93. ______f = { \
  94. .func = __func__, \
  95. .file = __FILE__, \
  96. .line = __LINE__, \
  97. }; \
  98. ______r = likely_notrace(x); \
  99. ftrace_likely_update(&______f, ______r, expect); \
  100. ______r; \
  101. })
  102. /*
  103. * Using __builtin_constant_p(x) to ignore cases where the return
  104. * value is always the same. This idea is taken from a similar patch
  105. * written by Daniel Walker.
  106. */
  107. # ifndef likely
  108. # define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
  109. # endif
  110. # ifndef unlikely
  111. # define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
  112. # endif
  113. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  114. /*
  115. * "Define 'is'", Bill Clinton
  116. * "Define 'if'", Steven Rostedt
  117. */
  118. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  119. #define __trace_if(cond) \
  120. if (__builtin_constant_p((cond)) ? !!(cond) : \
  121. ({ \
  122. int ______r; \
  123. static struct ftrace_branch_data \
  124. __attribute__((__aligned__(4))) \
  125. __attribute__((section("_ftrace_branch"))) \
  126. ______f = { \
  127. .func = __func__, \
  128. .file = __FILE__, \
  129. .line = __LINE__, \
  130. }; \
  131. ______r = !!(cond); \
  132. ______f.miss_hit[______r]++; \
  133. ______r; \
  134. }))
  135. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  136. #else
  137. # define likely(x) __builtin_expect(!!(x), 1)
  138. # define unlikely(x) __builtin_expect(!!(x), 0)
  139. #endif
  140. /* Optimization barrier */
  141. #ifndef barrier
  142. # define barrier() __memory_barrier()
  143. #endif
  144. /* Unreachable code */
  145. #ifndef unreachable
  146. # define unreachable() do { } while (1)
  147. #endif
  148. #ifndef RELOC_HIDE
  149. # define RELOC_HIDE(ptr, off) \
  150. ({ unsigned long __ptr; \
  151. __ptr = (unsigned long) (ptr); \
  152. (typeof(ptr)) (__ptr + (off)); })
  153. #endif
  154. /* Not-quite-unique ID. */
  155. #ifndef __UNIQUE_ID
  156. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  157. #endif
  158. #endif /* __KERNEL__ */
  159. #endif /* __ASSEMBLY__ */
  160. #ifdef __KERNEL__
  161. /*
  162. * Allow us to mark functions as 'deprecated' and have gcc emit a nice
  163. * warning for each use, in hopes of speeding the functions removal.
  164. * Usage is:
  165. * int __deprecated foo(void)
  166. */
  167. #ifndef __deprecated
  168. # define __deprecated /* unimplemented */
  169. #endif
  170. #ifdef MODULE
  171. #define __deprecated_for_modules __deprecated
  172. #else
  173. #define __deprecated_for_modules
  174. #endif
  175. #ifndef __must_check
  176. #define __must_check
  177. #endif
  178. #ifndef CONFIG_ENABLE_MUST_CHECK
  179. #undef __must_check
  180. #define __must_check
  181. #endif
  182. #ifndef CONFIG_ENABLE_WARN_DEPRECATED
  183. #undef __deprecated
  184. #undef __deprecated_for_modules
  185. #define __deprecated
  186. #define __deprecated_for_modules
  187. #endif
  188. /*
  189. * Allow us to avoid 'defined but not used' warnings on functions and data,
  190. * as well as force them to be emitted to the assembly file.
  191. *
  192. * As of gcc 3.4, static functions that are not marked with attribute((used))
  193. * may be elided from the assembly file. As of gcc 3.4, static data not so
  194. * marked will not be elided, but this may change in a future gcc version.
  195. *
  196. * NOTE: Because distributions shipped with a backported unit-at-a-time
  197. * compiler in gcc 3.3, we must define __used to be __attribute__((used))
  198. * for gcc >=3.3 instead of 3.4.
  199. *
  200. * In prior versions of gcc, such functions and data would be emitted, but
  201. * would be warned about except with attribute((unused)).
  202. *
  203. * Mark functions that are referenced only in inline assembly as __used so
  204. * the code is emitted even though it appears to be unreferenced.
  205. */
  206. #ifndef __used
  207. # define __used /* unimplemented */
  208. #endif
  209. #ifndef __maybe_unused
  210. # define __maybe_unused /* unimplemented */
  211. #endif
  212. #ifndef __always_unused
  213. # define __always_unused /* unimplemented */
  214. #endif
  215. #ifndef noinline
  216. #define noinline
  217. #endif
  218. /*
  219. * Rather then using noinline to prevent stack consumption, use
  220. * noinline_for_stack instead. For documentation reasons.
  221. */
  222. #define noinline_for_stack noinline
  223. #ifndef __always_inline
  224. #define __always_inline inline
  225. #endif
  226. #endif /* __KERNEL__ */
  227. /*
  228. * From the GCC manual:
  229. *
  230. * Many functions do not examine any values except their arguments,
  231. * and have no effects except the return value. Basically this is
  232. * just slightly more strict class than the `pure' attribute above,
  233. * since function is not allowed to read global memory.
  234. *
  235. * Note that a function that has pointer arguments and examines the
  236. * data pointed to must _not_ be declared `const'. Likewise, a
  237. * function that calls a non-`const' function usually must not be
  238. * `const'. It does not make sense for a `const' function to return
  239. * `void'.
  240. */
  241. #ifndef __attribute_const__
  242. # define __attribute_const__ /* unimplemented */
  243. #endif
  244. /*
  245. * Tell gcc if a function is cold. The compiler will assume any path
  246. * directly leading to the call is unlikely.
  247. */
  248. #ifndef __cold
  249. #define __cold
  250. #endif
  251. /* Simple shorthand for a section definition */
  252. #ifndef __section
  253. # define __section(S) __attribute__ ((__section__(#S)))
  254. #endif
  255. #ifndef __visible
  256. #define __visible
  257. #endif
  258. /* Are two types/vars the same type (ignoring qualifiers)? */
  259. #ifndef __same_type
  260. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  261. #endif
  262. /* Compile time object size, -1 for unknown */
  263. #ifndef __compiletime_object_size
  264. # define __compiletime_object_size(obj) -1
  265. #endif
  266. #ifndef __compiletime_warning
  267. # define __compiletime_warning(message)
  268. #endif
  269. #ifndef __compiletime_error
  270. # define __compiletime_error(message)
  271. # define __compiletime_error_fallback(condition) \
  272. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  273. #else
  274. # define __compiletime_error_fallback(condition) do { } while (0)
  275. #endif
  276. #define __compiletime_assert(condition, msg, prefix, suffix) \
  277. do { \
  278. bool __cond = !(condition); \
  279. extern void prefix ## suffix(void) __compiletime_error(msg); \
  280. if (__cond) \
  281. prefix ## suffix(); \
  282. __compiletime_error_fallback(__cond); \
  283. } while (0)
  284. #define _compiletime_assert(condition, msg, prefix, suffix) \
  285. __compiletime_assert(condition, msg, prefix, suffix)
  286. /**
  287. * compiletime_assert - break build and emit msg if condition is false
  288. * @condition: a compile-time constant condition to check
  289. * @msg: a message to emit if condition is false
  290. *
  291. * In tradition of POSIX assert, this macro will break the build if the
  292. * supplied condition is *false*, emitting the supplied error message if the
  293. * compiler has support to do so.
  294. */
  295. #define compiletime_assert(condition, msg) \
  296. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  297. /*
  298. * Prevent the compiler from merging or refetching accesses. The compiler
  299. * is also forbidden from reordering successive instances of ACCESS_ONCE(),
  300. * but only when the compiler is aware of some particular ordering. One way
  301. * to make the compiler aware of ordering is to put the two invocations of
  302. * ACCESS_ONCE() in different C statements.
  303. *
  304. * This macro does absolutely -nothing- to prevent the CPU from reordering,
  305. * merging, or refetching absolutely anything at any time. Its main intended
  306. * use is to mediate communication between process-level code and irq/NMI
  307. * handlers, all running on the same CPU.
  308. */
  309. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  310. /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
  311. #ifdef CONFIG_KPROBES
  312. # define __kprobes __attribute__((__section__(".kprobes.text")))
  313. #else
  314. # define __kprobes
  315. #endif
  316. #endif /* __LINUX_COMPILER_H */