compiler.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. # define __rcu __attribute__((noderef, address_space(4)))
  19. # define __private __attribute__((noderef))
  20. extern void __chk_user_ptr(const volatile void __user *);
  21. extern void __chk_io_ptr(const volatile void __iomem *);
  22. # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
  23. #else /* __CHECKER__ */
  24. # ifdef STRUCTLEAK_PLUGIN
  25. # define __user __attribute__((user))
  26. # else
  27. # define __user
  28. # endif
  29. # define __kernel
  30. # define __safe
  31. # define __force
  32. # define __nocast
  33. # define __iomem
  34. # define __chk_user_ptr(x) (void)0
  35. # define __chk_io_ptr(x) (void)0
  36. # define __builtin_warning(x, y...) (1)
  37. # define __must_hold(x)
  38. # define __acquires(x)
  39. # define __releases(x)
  40. # define __acquire(x) (void)0
  41. # define __release(x) (void)0
  42. # define __cond_lock(x,c) (c)
  43. # define __percpu
  44. # define __rcu
  45. # define __private
  46. # define ACCESS_PRIVATE(p, member) ((p)->member)
  47. #endif /* __CHECKER__ */
  48. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  49. #define ___PASTE(a,b) a##b
  50. #define __PASTE(a,b) ___PASTE(a,b)
  51. #ifdef __KERNEL__
  52. #ifdef __GNUC__
  53. #include <linux/compiler-gcc.h>
  54. #endif
  55. #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
  56. #define notrace __attribute__((hotpatch(0,0)))
  57. #else
  58. #define notrace __attribute__((no_instrument_function))
  59. #endif
  60. /* Intel compiler defines __GNUC__. So we will overwrite implementations
  61. * coming from above header files here
  62. */
  63. #ifdef __INTEL_COMPILER
  64. # include <linux/compiler-intel.h>
  65. #endif
  66. /* Clang compiler defines __GNUC__. So we will overwrite implementations
  67. * coming from above header files here
  68. */
  69. #ifdef __clang__
  70. #include <linux/compiler-clang.h>
  71. #endif
  72. /*
  73. * Generic compiler-dependent macros required for kernel
  74. * build go below this comment. Actual compiler/compiler version
  75. * specific implementations come from the above header files
  76. */
  77. struct ftrace_branch_data {
  78. const char *func;
  79. const char *file;
  80. unsigned line;
  81. union {
  82. struct {
  83. unsigned long correct;
  84. unsigned long incorrect;
  85. };
  86. struct {
  87. unsigned long miss;
  88. unsigned long hit;
  89. };
  90. unsigned long miss_hit[2];
  91. };
  92. };
  93. struct ftrace_likely_data {
  94. struct ftrace_branch_data data;
  95. unsigned long constant;
  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_likely_data *f, int val,
  104. int expect, int is_constant);
  105. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  106. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  107. #define __branch_check__(x, expect, is_constant) ({ \
  108. int ______r; \
  109. static struct ftrace_likely_data \
  110. __attribute__((__aligned__(4))) \
  111. __attribute__((section("_ftrace_annotated_branch"))) \
  112. ______f = { \
  113. .data.func = __func__, \
  114. .data.file = __FILE__, \
  115. .data.line = __LINE__, \
  116. }; \
  117. ______r = __builtin_expect(!!(x), expect); \
  118. ftrace_likely_update(&______f, ______r, \
  119. expect, is_constant); \
  120. ______r; \
  121. })
  122. /*
  123. * Using __builtin_constant_p(x) to ignore cases where the return
  124. * value is always the same. This idea is taken from a similar patch
  125. * written by Daniel Walker.
  126. */
  127. # ifndef likely
  128. # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
  129. # endif
  130. # ifndef unlikely
  131. # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
  132. # endif
  133. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  134. /*
  135. * "Define 'is'", Bill Clinton
  136. * "Define 'if'", Steven Rostedt
  137. */
  138. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  139. #define __trace_if(cond) \
  140. if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
  141. ({ \
  142. int ______r; \
  143. static struct ftrace_branch_data \
  144. __attribute__((__aligned__(4))) \
  145. __attribute__((section("_ftrace_branch"))) \
  146. ______f = { \
  147. .func = __func__, \
  148. .file = __FILE__, \
  149. .line = __LINE__, \
  150. }; \
  151. ______r = !!(cond); \
  152. ______f.miss_hit[______r]++; \
  153. ______r; \
  154. }))
  155. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  156. #else
  157. # define likely(x) __builtin_expect(!!(x), 1)
  158. # define unlikely(x) __builtin_expect(!!(x), 0)
  159. #endif
  160. /* Optimization barrier */
  161. #ifndef barrier
  162. # define barrier() __memory_barrier()
  163. #endif
  164. #ifndef barrier_data
  165. # define barrier_data(ptr) barrier()
  166. #endif
  167. /* Unreachable code */
  168. #ifndef unreachable
  169. # define unreachable() do { } while (1)
  170. #endif
  171. /*
  172. * KENTRY - kernel entry point
  173. * This can be used to annotate symbols (functions or data) that are used
  174. * without their linker symbol being referenced explicitly. For example,
  175. * interrupt vector handlers, or functions in the kernel image that are found
  176. * programatically.
  177. *
  178. * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
  179. * are handled in their own way (with KEEP() in linker scripts).
  180. *
  181. * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
  182. * linker script. For example an architecture could KEEP() its entire
  183. * boot/exception vector code rather than annotate each function and data.
  184. */
  185. #ifndef KENTRY
  186. # define KENTRY(sym) \
  187. extern typeof(sym) sym; \
  188. static const unsigned long __kentry_##sym \
  189. __used \
  190. __attribute__((section("___kentry" "+" #sym ), used)) \
  191. = (unsigned long)&sym;
  192. #endif
  193. #ifndef RELOC_HIDE
  194. # define RELOC_HIDE(ptr, off) \
  195. ({ unsigned long __ptr; \
  196. __ptr = (unsigned long) (ptr); \
  197. (typeof(ptr)) (__ptr + (off)); })
  198. #endif
  199. #ifndef OPTIMIZER_HIDE_VAR
  200. #define OPTIMIZER_HIDE_VAR(var) barrier()
  201. #endif
  202. /* Not-quite-unique ID. */
  203. #ifndef __UNIQUE_ID
  204. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  205. #endif
  206. #include <uapi/linux/types.h>
  207. #define __READ_ONCE_SIZE \
  208. ({ \
  209. switch (size) { \
  210. case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
  211. case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
  212. case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
  213. case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
  214. default: \
  215. barrier(); \
  216. __builtin_memcpy((void *)res, (const void *)p, size); \
  217. barrier(); \
  218. } \
  219. })
  220. static __always_inline
  221. void __read_once_size(const volatile void *p, void *res, int size)
  222. {
  223. __READ_ONCE_SIZE;
  224. }
  225. #ifdef CONFIG_KASAN
  226. /*
  227. * This function is not 'inline' because __no_sanitize_address confilcts
  228. * with inlining. Attempt to inline it may cause a build failure.
  229. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
  230. * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
  231. */
  232. static __no_sanitize_address __maybe_unused
  233. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  234. {
  235. __READ_ONCE_SIZE;
  236. }
  237. #else
  238. static __always_inline
  239. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  240. {
  241. __READ_ONCE_SIZE;
  242. }
  243. #endif
  244. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  245. {
  246. switch (size) {
  247. case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
  248. case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
  249. case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
  250. case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
  251. default:
  252. barrier();
  253. __builtin_memcpy((void *)p, (const void *)res, size);
  254. barrier();
  255. }
  256. }
  257. /*
  258. * Prevent the compiler from merging or refetching reads or writes. The
  259. * compiler is also forbidden from reordering successive instances of
  260. * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
  261. * compiler is aware of some particular ordering. One way to make the
  262. * compiler aware of ordering is to put the two invocations of READ_ONCE,
  263. * WRITE_ONCE or ACCESS_ONCE() in different C statements.
  264. *
  265. * In contrast to ACCESS_ONCE these two macros will also work on aggregate
  266. * data types like structs or unions. If the size of the accessed data
  267. * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
  268. * READ_ONCE() and WRITE_ONCE() will fall back to memcpy(). There's at
  269. * least two memcpy()s: one for the __builtin_memcpy() and then one for
  270. * the macro doing the copy of variable - '__u' allocated on the stack.
  271. *
  272. * Their two major use cases are: (1) Mediating communication between
  273. * process-level code and irq/NMI handlers, all running on the same CPU,
  274. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  275. * mutilate accesses that either do not require ordering or that interact
  276. * with an explicit memory barrier or atomic instruction that provides the
  277. * required ordering.
  278. */
  279. #define __READ_ONCE(x, check) \
  280. ({ \
  281. union { typeof(x) __val; char __c[1]; } __u; \
  282. if (check) \
  283. __read_once_size(&(x), __u.__c, sizeof(x)); \
  284. else \
  285. __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
  286. __u.__val; \
  287. })
  288. #define READ_ONCE(x) __READ_ONCE(x, 1)
  289. /*
  290. * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
  291. * to hide memory access from KASAN.
  292. */
  293. #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
  294. #define WRITE_ONCE(x, val) \
  295. ({ \
  296. union { typeof(x) __val; char __c[1]; } __u = \
  297. { .__val = (__force typeof(x)) (val) }; \
  298. __write_once_size(&(x), __u.__c, sizeof(x)); \
  299. __u.__val; \
  300. })
  301. #endif /* __KERNEL__ */
  302. #endif /* __ASSEMBLY__ */
  303. #ifdef __KERNEL__
  304. /*
  305. * Allow us to mark functions as 'deprecated' and have gcc emit a nice
  306. * warning for each use, in hopes of speeding the functions removal.
  307. * Usage is:
  308. * int __deprecated foo(void)
  309. */
  310. #ifndef __deprecated
  311. # define __deprecated /* unimplemented */
  312. #endif
  313. #ifdef MODULE
  314. #define __deprecated_for_modules __deprecated
  315. #else
  316. #define __deprecated_for_modules
  317. #endif
  318. #ifndef __must_check
  319. #define __must_check
  320. #endif
  321. #ifndef CONFIG_ENABLE_MUST_CHECK
  322. #undef __must_check
  323. #define __must_check
  324. #endif
  325. #ifndef CONFIG_ENABLE_WARN_DEPRECATED
  326. #undef __deprecated
  327. #undef __deprecated_for_modules
  328. #define __deprecated
  329. #define __deprecated_for_modules
  330. #endif
  331. #ifndef __malloc
  332. #define __malloc
  333. #endif
  334. /*
  335. * Allow us to avoid 'defined but not used' warnings on functions and data,
  336. * as well as force them to be emitted to the assembly file.
  337. *
  338. * As of gcc 3.4, static functions that are not marked with attribute((used))
  339. * may be elided from the assembly file. As of gcc 3.4, static data not so
  340. * marked will not be elided, but this may change in a future gcc version.
  341. *
  342. * NOTE: Because distributions shipped with a backported unit-at-a-time
  343. * compiler in gcc 3.3, we must define __used to be __attribute__((used))
  344. * for gcc >=3.3 instead of 3.4.
  345. *
  346. * In prior versions of gcc, such functions and data would be emitted, but
  347. * would be warned about except with attribute((unused)).
  348. *
  349. * Mark functions that are referenced only in inline assembly as __used so
  350. * the code is emitted even though it appears to be unreferenced.
  351. */
  352. #ifndef __used
  353. # define __used /* unimplemented */
  354. #endif
  355. #ifndef __maybe_unused
  356. # define __maybe_unused /* unimplemented */
  357. #endif
  358. #ifndef __always_unused
  359. # define __always_unused /* unimplemented */
  360. #endif
  361. #ifndef noinline
  362. #define noinline
  363. #endif
  364. /*
  365. * Rather then using noinline to prevent stack consumption, use
  366. * noinline_for_stack instead. For documentation reasons.
  367. */
  368. #define noinline_for_stack noinline
  369. #ifndef __always_inline
  370. #define __always_inline inline
  371. #endif
  372. #endif /* __KERNEL__ */
  373. /*
  374. * From the GCC manual:
  375. *
  376. * Many functions do not examine any values except their arguments,
  377. * and have no effects except the return value. Basically this is
  378. * just slightly more strict class than the `pure' attribute above,
  379. * since function is not allowed to read global memory.
  380. *
  381. * Note that a function that has pointer arguments and examines the
  382. * data pointed to must _not_ be declared `const'. Likewise, a
  383. * function that calls a non-`const' function usually must not be
  384. * `const'. It does not make sense for a `const' function to return
  385. * `void'.
  386. */
  387. #ifndef __attribute_const__
  388. # define __attribute_const__ /* unimplemented */
  389. #endif
  390. #ifndef __latent_entropy
  391. # define __latent_entropy
  392. #endif
  393. /*
  394. * Tell gcc if a function is cold. The compiler will assume any path
  395. * directly leading to the call is unlikely.
  396. */
  397. #ifndef __cold
  398. #define __cold
  399. #endif
  400. /* Simple shorthand for a section definition */
  401. #ifndef __section
  402. # define __section(S) __attribute__ ((__section__(#S)))
  403. #endif
  404. #ifndef __visible
  405. #define __visible
  406. #endif
  407. /*
  408. * Assume alignment of return value.
  409. */
  410. #ifndef __assume_aligned
  411. #define __assume_aligned(a, ...)
  412. #endif
  413. /* Are two types/vars the same type (ignoring qualifiers)? */
  414. #ifndef __same_type
  415. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  416. #endif
  417. /* Is this type a native word size -- useful for atomic operations */
  418. #ifndef __native_word
  419. # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
  420. #endif
  421. /* Compile time object size, -1 for unknown */
  422. #ifndef __compiletime_object_size
  423. # define __compiletime_object_size(obj) -1
  424. #endif
  425. #ifndef __compiletime_warning
  426. # define __compiletime_warning(message)
  427. #endif
  428. #ifndef __compiletime_error
  429. # define __compiletime_error(message)
  430. /*
  431. * Sparse complains of variable sized arrays due to the temporary variable in
  432. * __compiletime_assert. Unfortunately we can't just expand it out to make
  433. * sparse see a constant array size without breaking compiletime_assert on old
  434. * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
  435. */
  436. # ifndef __CHECKER__
  437. # define __compiletime_error_fallback(condition) \
  438. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  439. # endif
  440. #endif
  441. #ifndef __compiletime_error_fallback
  442. # define __compiletime_error_fallback(condition) do { } while (0)
  443. #endif
  444. #define __compiletime_assert(condition, msg, prefix, suffix) \
  445. do { \
  446. bool __cond = !(condition); \
  447. extern void prefix ## suffix(void) __compiletime_error(msg); \
  448. if (__cond) \
  449. prefix ## suffix(); \
  450. __compiletime_error_fallback(__cond); \
  451. } while (0)
  452. #define _compiletime_assert(condition, msg, prefix, suffix) \
  453. __compiletime_assert(condition, msg, prefix, suffix)
  454. /**
  455. * compiletime_assert - break build and emit msg if condition is false
  456. * @condition: a compile-time constant condition to check
  457. * @msg: a message to emit if condition is false
  458. *
  459. * In tradition of POSIX assert, this macro will break the build if the
  460. * supplied condition is *false*, emitting the supplied error message if the
  461. * compiler has support to do so.
  462. */
  463. #define compiletime_assert(condition, msg) \
  464. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  465. #define compiletime_assert_atomic_type(t) \
  466. compiletime_assert(__native_word(t), \
  467. "Need native word sized stores/loads for atomicity.")
  468. /*
  469. * Prevent the compiler from merging or refetching accesses. The compiler
  470. * is also forbidden from reordering successive instances of ACCESS_ONCE(),
  471. * but only when the compiler is aware of some particular ordering. One way
  472. * to make the compiler aware of ordering is to put the two invocations of
  473. * ACCESS_ONCE() in different C statements.
  474. *
  475. * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
  476. * on a union member will work as long as the size of the member matches the
  477. * size of the union and the size is smaller than word size.
  478. *
  479. * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
  480. * between process-level code and irq/NMI handlers, all running on the same CPU,
  481. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  482. * mutilate accesses that either do not require ordering or that interact
  483. * with an explicit memory barrier or atomic instruction that provides the
  484. * required ordering.
  485. *
  486. * If possible use READ_ONCE()/WRITE_ONCE() instead.
  487. */
  488. #define __ACCESS_ONCE(x) ({ \
  489. __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
  490. (volatile typeof(x) *)&(x); })
  491. #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
  492. /**
  493. * lockless_dereference() - safely load a pointer for later dereference
  494. * @p: The pointer to load
  495. *
  496. * Similar to rcu_dereference(), but for situations where the pointed-to
  497. * object's lifetime is managed by something other than RCU. That
  498. * "something other" might be reference counting or simple immortality.
  499. *
  500. * The seemingly unused variable ___typecheck_p validates that @p is
  501. * indeed a pointer type by using a pointer to typeof(*p) as the type.
  502. * Taking a pointer to typeof(*p) again is needed in case p is void *.
  503. */
  504. #define lockless_dereference(p) \
  505. ({ \
  506. typeof(p) _________p1 = READ_ONCE(p); \
  507. typeof(*(p)) *___typecheck_p __maybe_unused; \
  508. smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
  509. (_________p1); \
  510. })
  511. #endif /* __LINUX_COMPILER_H */