compiler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 __must_hold(x) __attribute__((context(x,1,1)))
  12. # define __acquires(x) __attribute__((context(x,0,1)))
  13. # define __releases(x) __attribute__((context(x,1,0)))
  14. # define __acquire(x) __context__(x,1)
  15. # define __release(x) __context__(x,-1)
  16. # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
  17. # define __percpu __attribute__((noderef, address_space(3)))
  18. #ifdef CONFIG_SPARSE_RCU_POINTER
  19. # define __rcu __attribute__((noderef, address_space(4)))
  20. #else
  21. # define __rcu
  22. #endif
  23. extern void __chk_user_ptr(const volatile void __user *);
  24. extern void __chk_io_ptr(const volatile void __iomem *);
  25. #else
  26. # define __user
  27. # define __kernel
  28. # define __safe
  29. # define __force
  30. # define __nocast
  31. # define __iomem
  32. # define __chk_user_ptr(x) (void)0
  33. # define __chk_io_ptr(x) (void)0
  34. # define __builtin_warning(x, y...) (1)
  35. # define __must_hold(x)
  36. # define __acquires(x)
  37. # define __releases(x)
  38. # define __acquire(x) (void)0
  39. # define __release(x) (void)0
  40. # define __cond_lock(x,c) (c)
  41. # define __percpu
  42. # define __rcu
  43. #endif
  44. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  45. #define ___PASTE(a,b) a##b
  46. #define __PASTE(a,b) ___PASTE(a,b)
  47. #ifdef __KERNEL__
  48. #ifdef __GNUC__
  49. #include <linux/compiler-gcc.h>
  50. #endif
  51. #ifdef CC_USING_HOTPATCH
  52. #define notrace __attribute__((hotpatch(0,0)))
  53. #else
  54. #define notrace __attribute__((no_instrument_function))
  55. #endif
  56. /* Intel compiler defines __GNUC__. So we will overwrite implementations
  57. * coming from above header files here
  58. */
  59. #ifdef __INTEL_COMPILER
  60. # include <linux/compiler-intel.h>
  61. #endif
  62. /* Clang compiler defines __GNUC__. So we will overwrite implementations
  63. * coming from above header files here
  64. */
  65. #ifdef __clang__
  66. #include <linux/compiler-clang.h>
  67. #endif
  68. /*
  69. * Generic compiler-dependent macros required for kernel
  70. * build go below this comment. Actual compiler/compiler version
  71. * specific implementations come from the above header files
  72. */
  73. struct ftrace_branch_data {
  74. const char *func;
  75. const char *file;
  76. unsigned line;
  77. union {
  78. struct {
  79. unsigned long correct;
  80. unsigned long incorrect;
  81. };
  82. struct {
  83. unsigned long miss;
  84. unsigned long hit;
  85. };
  86. unsigned long miss_hit[2];
  87. };
  88. };
  89. /*
  90. * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
  91. * to disable branch tracing on a per file basis.
  92. */
  93. #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
  94. && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
  95. void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
  96. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  97. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  98. #define __branch_check__(x, expect) ({ \
  99. int ______r; \
  100. static struct ftrace_branch_data \
  101. __attribute__((__aligned__(4))) \
  102. __attribute__((section("_ftrace_annotated_branch"))) \
  103. ______f = { \
  104. .func = __func__, \
  105. .file = __FILE__, \
  106. .line = __LINE__, \
  107. }; \
  108. ______r = likely_notrace(x); \
  109. ftrace_likely_update(&______f, ______r, expect); \
  110. ______r; \
  111. })
  112. /*
  113. * Using __builtin_constant_p(x) to ignore cases where the return
  114. * value is always the same. This idea is taken from a similar patch
  115. * written by Daniel Walker.
  116. */
  117. # ifndef likely
  118. # define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
  119. # endif
  120. # ifndef unlikely
  121. # define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
  122. # endif
  123. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  124. /*
  125. * "Define 'is'", Bill Clinton
  126. * "Define 'if'", Steven Rostedt
  127. */
  128. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  129. #define __trace_if(cond) \
  130. if (__builtin_constant_p((cond)) ? !!(cond) : \
  131. ({ \
  132. int ______r; \
  133. static struct ftrace_branch_data \
  134. __attribute__((__aligned__(4))) \
  135. __attribute__((section("_ftrace_branch"))) \
  136. ______f = { \
  137. .func = __func__, \
  138. .file = __FILE__, \
  139. .line = __LINE__, \
  140. }; \
  141. ______r = !!(cond); \
  142. ______f.miss_hit[______r]++; \
  143. ______r; \
  144. }))
  145. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  146. #else
  147. # define likely(x) __builtin_expect(!!(x), 1)
  148. # define unlikely(x) __builtin_expect(!!(x), 0)
  149. #endif
  150. /* Optimization barrier */
  151. #ifndef barrier
  152. # define barrier() __memory_barrier()
  153. #endif
  154. /* Unreachable code */
  155. #ifndef unreachable
  156. # define unreachable() do { } while (1)
  157. #endif
  158. #ifndef RELOC_HIDE
  159. # define RELOC_HIDE(ptr, off) \
  160. ({ unsigned long __ptr; \
  161. __ptr = (unsigned long) (ptr); \
  162. (typeof(ptr)) (__ptr + (off)); })
  163. #endif
  164. #ifndef OPTIMIZER_HIDE_VAR
  165. #define OPTIMIZER_HIDE_VAR(var) barrier()
  166. #endif
  167. /* Not-quite-unique ID. */
  168. #ifndef __UNIQUE_ID
  169. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  170. #endif
  171. #include <uapi/linux/types.h>
  172. static __always_inline void data_access_exceeds_word_size(void)
  173. #ifdef __compiletime_warning
  174. __compiletime_warning("data access exceeds word size and won't be atomic")
  175. #endif
  176. ;
  177. static __always_inline void data_access_exceeds_word_size(void)
  178. {
  179. }
  180. static __always_inline void __read_once_size(volatile void *p, void *res, int size)
  181. {
  182. switch (size) {
  183. case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
  184. case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
  185. case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
  186. #ifdef CONFIG_64BIT
  187. case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
  188. #endif
  189. default:
  190. barrier();
  191. __builtin_memcpy((void *)res, (const void *)p, size);
  192. data_access_exceeds_word_size();
  193. barrier();
  194. }
  195. }
  196. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  197. {
  198. switch (size) {
  199. case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
  200. case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
  201. case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
  202. #ifdef CONFIG_64BIT
  203. case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
  204. #endif
  205. default:
  206. barrier();
  207. __builtin_memcpy((void *)p, (const void *)res, size);
  208. data_access_exceeds_word_size();
  209. barrier();
  210. }
  211. }
  212. /*
  213. * Prevent the compiler from merging or refetching reads or writes. The
  214. * compiler is also forbidden from reordering successive instances of
  215. * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
  216. * compiler is aware of some particular ordering. One way to make the
  217. * compiler aware of ordering is to put the two invocations of READ_ONCE,
  218. * WRITE_ONCE or ACCESS_ONCE() in different C statements.
  219. *
  220. * In contrast to ACCESS_ONCE these two macros will also work on aggregate
  221. * data types like structs or unions. If the size of the accessed data
  222. * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
  223. * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a
  224. * compile-time warning.
  225. *
  226. * Their two major use cases are: (1) Mediating communication between
  227. * process-level code and irq/NMI handlers, all running on the same CPU,
  228. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  229. * mutilate accesses that either do not require ordering or that interact
  230. * with an explicit memory barrier or atomic instruction that provides the
  231. * required ordering.
  232. */
  233. #define READ_ONCE(x) \
  234. ({ typeof(x) __val; __read_once_size(&x, &__val, sizeof(__val)); __val; })
  235. #define WRITE_ONCE(x, val) \
  236. ({ typeof(x) __val; __val = val; __write_once_size(&x, &__val, sizeof(__val)); __val; })
  237. #endif /* __KERNEL__ */
  238. #endif /* __ASSEMBLY__ */
  239. #ifdef __KERNEL__
  240. /*
  241. * Allow us to mark functions as 'deprecated' and have gcc emit a nice
  242. * warning for each use, in hopes of speeding the functions removal.
  243. * Usage is:
  244. * int __deprecated foo(void)
  245. */
  246. #ifndef __deprecated
  247. # define __deprecated /* unimplemented */
  248. #endif
  249. #ifdef MODULE
  250. #define __deprecated_for_modules __deprecated
  251. #else
  252. #define __deprecated_for_modules
  253. #endif
  254. #ifndef __must_check
  255. #define __must_check
  256. #endif
  257. #ifndef CONFIG_ENABLE_MUST_CHECK
  258. #undef __must_check
  259. #define __must_check
  260. #endif
  261. #ifndef CONFIG_ENABLE_WARN_DEPRECATED
  262. #undef __deprecated
  263. #undef __deprecated_for_modules
  264. #define __deprecated
  265. #define __deprecated_for_modules
  266. #endif
  267. /*
  268. * Allow us to avoid 'defined but not used' warnings on functions and data,
  269. * as well as force them to be emitted to the assembly file.
  270. *
  271. * As of gcc 3.4, static functions that are not marked with attribute((used))
  272. * may be elided from the assembly file. As of gcc 3.4, static data not so
  273. * marked will not be elided, but this may change in a future gcc version.
  274. *
  275. * NOTE: Because distributions shipped with a backported unit-at-a-time
  276. * compiler in gcc 3.3, we must define __used to be __attribute__((used))
  277. * for gcc >=3.3 instead of 3.4.
  278. *
  279. * In prior versions of gcc, such functions and data would be emitted, but
  280. * would be warned about except with attribute((unused)).
  281. *
  282. * Mark functions that are referenced only in inline assembly as __used so
  283. * the code is emitted even though it appears to be unreferenced.
  284. */
  285. #ifndef __used
  286. # define __used /* unimplemented */
  287. #endif
  288. #ifndef __maybe_unused
  289. # define __maybe_unused /* unimplemented */
  290. #endif
  291. #ifndef __always_unused
  292. # define __always_unused /* unimplemented */
  293. #endif
  294. #ifndef noinline
  295. #define noinline
  296. #endif
  297. /*
  298. * Rather then using noinline to prevent stack consumption, use
  299. * noinline_for_stack instead. For documentation reasons.
  300. */
  301. #define noinline_for_stack noinline
  302. #ifndef __always_inline
  303. #define __always_inline inline
  304. #endif
  305. #endif /* __KERNEL__ */
  306. /*
  307. * From the GCC manual:
  308. *
  309. * Many functions do not examine any values except their arguments,
  310. * and have no effects except the return value. Basically this is
  311. * just slightly more strict class than the `pure' attribute above,
  312. * since function is not allowed to read global memory.
  313. *
  314. * Note that a function that has pointer arguments and examines the
  315. * data pointed to must _not_ be declared `const'. Likewise, a
  316. * function that calls a non-`const' function usually must not be
  317. * `const'. It does not make sense for a `const' function to return
  318. * `void'.
  319. */
  320. #ifndef __attribute_const__
  321. # define __attribute_const__ /* unimplemented */
  322. #endif
  323. /*
  324. * Tell gcc if a function is cold. The compiler will assume any path
  325. * directly leading to the call is unlikely.
  326. */
  327. #ifndef __cold
  328. #define __cold
  329. #endif
  330. /* Simple shorthand for a section definition */
  331. #ifndef __section
  332. # define __section(S) __attribute__ ((__section__(#S)))
  333. #endif
  334. #ifndef __visible
  335. #define __visible
  336. #endif
  337. /* Are two types/vars the same type (ignoring qualifiers)? */
  338. #ifndef __same_type
  339. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  340. #endif
  341. /* Is this type a native word size -- useful for atomic operations */
  342. #ifndef __native_word
  343. # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
  344. #endif
  345. /* Compile time object size, -1 for unknown */
  346. #ifndef __compiletime_object_size
  347. # define __compiletime_object_size(obj) -1
  348. #endif
  349. #ifndef __compiletime_warning
  350. # define __compiletime_warning(message)
  351. #endif
  352. #ifndef __compiletime_error
  353. # define __compiletime_error(message)
  354. /*
  355. * Sparse complains of variable sized arrays due to the temporary variable in
  356. * __compiletime_assert. Unfortunately we can't just expand it out to make
  357. * sparse see a constant array size without breaking compiletime_assert on old
  358. * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
  359. */
  360. # ifndef __CHECKER__
  361. # define __compiletime_error_fallback(condition) \
  362. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  363. # endif
  364. #endif
  365. #ifndef __compiletime_error_fallback
  366. # define __compiletime_error_fallback(condition) do { } while (0)
  367. #endif
  368. #define __compiletime_assert(condition, msg, prefix, suffix) \
  369. do { \
  370. bool __cond = !(condition); \
  371. extern void prefix ## suffix(void) __compiletime_error(msg); \
  372. if (__cond) \
  373. prefix ## suffix(); \
  374. __compiletime_error_fallback(__cond); \
  375. } while (0)
  376. #define _compiletime_assert(condition, msg, prefix, suffix) \
  377. __compiletime_assert(condition, msg, prefix, suffix)
  378. /**
  379. * compiletime_assert - break build and emit msg if condition is false
  380. * @condition: a compile-time constant condition to check
  381. * @msg: a message to emit if condition is false
  382. *
  383. * In tradition of POSIX assert, this macro will break the build if the
  384. * supplied condition is *false*, emitting the supplied error message if the
  385. * compiler has support to do so.
  386. */
  387. #define compiletime_assert(condition, msg) \
  388. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  389. #define compiletime_assert_atomic_type(t) \
  390. compiletime_assert(__native_word(t), \
  391. "Need native word sized stores/loads for atomicity.")
  392. /*
  393. * Prevent the compiler from merging or refetching accesses. The compiler
  394. * is also forbidden from reordering successive instances of ACCESS_ONCE(),
  395. * but only when the compiler is aware of some particular ordering. One way
  396. * to make the compiler aware of ordering is to put the two invocations of
  397. * ACCESS_ONCE() in different C statements.
  398. *
  399. * This macro does absolutely -nothing- to prevent the CPU from reordering,
  400. * merging, or refetching absolutely anything at any time. Its main intended
  401. * use is to mediate communication between process-level code and irq/NMI
  402. * handlers, all running on the same CPU.
  403. */
  404. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  405. /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
  406. #ifdef CONFIG_KPROBES
  407. # define __kprobes __attribute__((__section__(".kprobes.text")))
  408. # define nokprobe_inline __always_inline
  409. #else
  410. # define __kprobes
  411. # define nokprobe_inline inline
  412. #endif
  413. #endif /* __LINUX_COMPILER_H */