compiler.h 8.3 KB

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