compiler.h 7.5 KB

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