compiler.h 7.3 KB

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