compiler.h 9.6 KB

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