compiler.h 8.3 KB

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