compiler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_COMPILER_H
  3. #define __LINUX_COMPILER_H
  4. #include <linux/compiler_types.h>
  5. #ifndef __ASSEMBLY__
  6. #ifdef __KERNEL__
  7. /*
  8. * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
  9. * to disable branch tracing on a per file basis.
  10. */
  11. #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
  12. && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
  13. void ftrace_likely_update(struct ftrace_likely_data *f, int val,
  14. int expect, int is_constant);
  15. #define likely_notrace(x) __builtin_expect(!!(x), 1)
  16. #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
  17. #define __branch_check__(x, expect, is_constant) ({ \
  18. long ______r; \
  19. static struct ftrace_likely_data \
  20. __attribute__((__aligned__(4))) \
  21. __attribute__((section("_ftrace_annotated_branch"))) \
  22. ______f = { \
  23. .data.func = __func__, \
  24. .data.file = __FILE__, \
  25. .data.line = __LINE__, \
  26. }; \
  27. ______r = __builtin_expect(!!(x), expect); \
  28. ftrace_likely_update(&______f, ______r, \
  29. expect, is_constant); \
  30. ______r; \
  31. })
  32. /*
  33. * Using __builtin_constant_p(x) to ignore cases where the return
  34. * value is always the same. This idea is taken from a similar patch
  35. * written by Daniel Walker.
  36. */
  37. # ifndef likely
  38. # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
  39. # endif
  40. # ifndef unlikely
  41. # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))
  42. # endif
  43. #ifdef CONFIG_PROFILE_ALL_BRANCHES
  44. /*
  45. * "Define 'is'", Bill Clinton
  46. * "Define 'if'", Steven Rostedt
  47. */
  48. #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
  49. #define __trace_if(cond) \
  50. if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
  51. ({ \
  52. int ______r; \
  53. static struct ftrace_branch_data \
  54. __attribute__((__aligned__(4))) \
  55. __attribute__((section("_ftrace_branch"))) \
  56. ______f = { \
  57. .func = __func__, \
  58. .file = __FILE__, \
  59. .line = __LINE__, \
  60. }; \
  61. ______r = !!(cond); \
  62. ______f.miss_hit[______r]++; \
  63. ______r; \
  64. }))
  65. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  66. #else
  67. # define likely(x) __builtin_expect(!!(x), 1)
  68. # define unlikely(x) __builtin_expect(!!(x), 0)
  69. #endif
  70. /* Optimization barrier */
  71. #ifndef barrier
  72. # define barrier() __memory_barrier()
  73. #endif
  74. #ifndef barrier_data
  75. # define barrier_data(ptr) barrier()
  76. #endif
  77. /* workaround for GCC PR82365 if needed */
  78. #ifndef barrier_before_unreachable
  79. # define barrier_before_unreachable() do { } while (0)
  80. #endif
  81. /* Unreachable code */
  82. #ifdef CONFIG_STACK_VALIDATION
  83. /*
  84. * These macros help objtool understand GCC code flow for unreachable code.
  85. * The __COUNTER__ based labels are a hack to make each instance of the macros
  86. * unique, to convince GCC not to merge duplicate inline asm statements.
  87. */
  88. #define annotate_reachable() ({ \
  89. asm volatile("ANNOTATE_REACHABLE counter=%c0" \
  90. : : "i" (__COUNTER__)); \
  91. })
  92. #define annotate_unreachable() ({ \
  93. asm volatile("ANNOTATE_UNREACHABLE counter=%c0" \
  94. : : "i" (__COUNTER__)); \
  95. })
  96. #else
  97. #define annotate_reachable()
  98. #define annotate_unreachable()
  99. #endif
  100. #ifndef ASM_UNREACHABLE
  101. # define ASM_UNREACHABLE
  102. #endif
  103. #ifndef unreachable
  104. # define unreachable() do { annotate_reachable(); do { } while (1); } while (0)
  105. #endif
  106. /*
  107. * KENTRY - kernel entry point
  108. * This can be used to annotate symbols (functions or data) that are used
  109. * without their linker symbol being referenced explicitly. For example,
  110. * interrupt vector handlers, or functions in the kernel image that are found
  111. * programatically.
  112. *
  113. * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
  114. * are handled in their own way (with KEEP() in linker scripts).
  115. *
  116. * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
  117. * linker script. For example an architecture could KEEP() its entire
  118. * boot/exception vector code rather than annotate each function and data.
  119. */
  120. #ifndef KENTRY
  121. # define KENTRY(sym) \
  122. extern typeof(sym) sym; \
  123. static const unsigned long __kentry_##sym \
  124. __used \
  125. __attribute__((section("___kentry" "+" #sym ), used)) \
  126. = (unsigned long)&sym;
  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. #ifndef OPTIMIZER_HIDE_VAR
  135. #define OPTIMIZER_HIDE_VAR(var) barrier()
  136. #endif
  137. /* Not-quite-unique ID. */
  138. #ifndef __UNIQUE_ID
  139. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  140. #endif
  141. #include <uapi/linux/types.h>
  142. #define __READ_ONCE_SIZE \
  143. ({ \
  144. switch (size) { \
  145. case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
  146. case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
  147. case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
  148. case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
  149. default: \
  150. barrier(); \
  151. __builtin_memcpy((void *)res, (const void *)p, size); \
  152. barrier(); \
  153. } \
  154. })
  155. static __always_inline
  156. void __read_once_size(const volatile void *p, void *res, int size)
  157. {
  158. __READ_ONCE_SIZE;
  159. }
  160. #ifdef CONFIG_KASAN
  161. /*
  162. * We can't declare function 'inline' because __no_sanitize_address confilcts
  163. * with inlining. Attempt to inline it may cause a build failure.
  164. * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
  165. * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
  166. */
  167. # define __no_kasan_or_inline __no_sanitize_address __maybe_unused
  168. #else
  169. # define __no_kasan_or_inline __always_inline
  170. #endif
  171. static __no_kasan_or_inline
  172. void __read_once_size_nocheck(const volatile void *p, void *res, int size)
  173. {
  174. __READ_ONCE_SIZE;
  175. }
  176. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  177. {
  178. switch (size) {
  179. case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
  180. case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
  181. case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
  182. case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
  183. default:
  184. barrier();
  185. __builtin_memcpy((void *)p, (const void *)res, size);
  186. barrier();
  187. }
  188. }
  189. /*
  190. * Prevent the compiler from merging or refetching reads or writes. The
  191. * compiler is also forbidden from reordering successive instances of
  192. * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
  193. * particular ordering. One way to make the compiler aware of ordering is to
  194. * put the two invocations of READ_ONCE or WRITE_ONCE in different C
  195. * statements.
  196. *
  197. * These two macros will also work on aggregate data types like structs or
  198. * unions. If the size of the accessed data type exceeds the word size of
  199. * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
  200. * fall back to memcpy(). There's at least two memcpy()s: one for the
  201. * __builtin_memcpy() and then one for the macro doing the copy of variable
  202. * - '__u' allocated on the stack.
  203. *
  204. * Their two major use cases are: (1) Mediating communication between
  205. * process-level code and irq/NMI handlers, all running on the same CPU,
  206. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  207. * mutilate accesses that either do not require ordering or that interact
  208. * with an explicit memory barrier or atomic instruction that provides the
  209. * required ordering.
  210. */
  211. #include <asm/barrier.h>
  212. #include <linux/kasan-checks.h>
  213. #define __READ_ONCE(x, check) \
  214. ({ \
  215. union { typeof(x) __val; char __c[1]; } __u; \
  216. if (check) \
  217. __read_once_size(&(x), __u.__c, sizeof(x)); \
  218. else \
  219. __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
  220. smp_read_barrier_depends(); /* Enforce dependency ordering from x */ \
  221. __u.__val; \
  222. })
  223. #define READ_ONCE(x) __READ_ONCE(x, 1)
  224. /*
  225. * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
  226. * to hide memory access from KASAN.
  227. */
  228. #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
  229. static __no_kasan_or_inline
  230. unsigned long read_word_at_a_time(const void *addr)
  231. {
  232. kasan_check_read(addr, 1);
  233. return *(unsigned long *)addr;
  234. }
  235. #define WRITE_ONCE(x, val) \
  236. ({ \
  237. union { typeof(x) __val; char __c[1]; } __u = \
  238. { .__val = (__force typeof(x)) (val) }; \
  239. __write_once_size(&(x), __u.__c, sizeof(x)); \
  240. __u.__val; \
  241. })
  242. #endif /* __KERNEL__ */
  243. /*
  244. * Force the compiler to emit 'sym' as a symbol, so that we can reference
  245. * it from inline assembler. Necessary in case 'sym' could be inlined
  246. * otherwise, or eliminated entirely due to lack of references that are
  247. * visible to the compiler.
  248. */
  249. #define __ADDRESSABLE(sym) \
  250. static void * __attribute__((section(".discard.addressable"), used)) \
  251. __PASTE(__addressable_##sym, __LINE__) = (void *)&sym;
  252. /**
  253. * offset_to_ptr - convert a relative memory offset to an absolute pointer
  254. * @off: the address of the 32-bit offset value
  255. */
  256. static inline void *offset_to_ptr(const int *off)
  257. {
  258. return (void *)((unsigned long)off + *off);
  259. }
  260. #else /* __ASSEMBLY__ */
  261. #ifdef __KERNEL__
  262. #ifndef LINKER_SCRIPT
  263. #ifdef CONFIG_STACK_VALIDATION
  264. .macro ANNOTATE_UNREACHABLE counter:req
  265. \counter:
  266. .pushsection .discard.unreachable
  267. .long \counter\()b -.
  268. .popsection
  269. .endm
  270. .macro ANNOTATE_REACHABLE counter:req
  271. \counter:
  272. .pushsection .discard.reachable
  273. .long \counter\()b -.
  274. .popsection
  275. .endm
  276. .macro ASM_UNREACHABLE
  277. 999:
  278. .pushsection .discard.unreachable
  279. .long 999b - .
  280. .popsection
  281. .endm
  282. #else /* CONFIG_STACK_VALIDATION */
  283. .macro ANNOTATE_UNREACHABLE counter:req
  284. .endm
  285. .macro ANNOTATE_REACHABLE counter:req
  286. .endm
  287. .macro ASM_UNREACHABLE
  288. .endm
  289. #endif /* CONFIG_STACK_VALIDATION */
  290. #endif /* LINKER_SCRIPT */
  291. #endif /* __KERNEL__ */
  292. #endif /* __ASSEMBLY__ */
  293. #ifndef __optimize
  294. # define __optimize(level)
  295. #endif
  296. /* Compile time object size, -1 for unknown */
  297. #ifndef __compiletime_object_size
  298. # define __compiletime_object_size(obj) -1
  299. #endif
  300. #ifndef __compiletime_warning
  301. # define __compiletime_warning(message)
  302. #endif
  303. #ifndef __compiletime_error
  304. # define __compiletime_error(message)
  305. /*
  306. * Sparse complains of variable sized arrays due to the temporary variable in
  307. * __compiletime_assert. Unfortunately we can't just expand it out to make
  308. * sparse see a constant array size without breaking compiletime_assert on old
  309. * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
  310. */
  311. # ifndef __CHECKER__
  312. # define __compiletime_error_fallback(condition) \
  313. do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
  314. # endif
  315. #endif
  316. #ifndef __compiletime_error_fallback
  317. # define __compiletime_error_fallback(condition) do { } while (0)
  318. #endif
  319. #ifdef __OPTIMIZE__
  320. # define __compiletime_assert(condition, msg, prefix, suffix) \
  321. do { \
  322. int __cond = !(condition); \
  323. extern void prefix ## suffix(void) __compiletime_error(msg); \
  324. if (__cond) \
  325. prefix ## suffix(); \
  326. __compiletime_error_fallback(__cond); \
  327. } while (0)
  328. #else
  329. # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
  330. #endif
  331. #define _compiletime_assert(condition, msg, prefix, suffix) \
  332. __compiletime_assert(condition, msg, prefix, suffix)
  333. /**
  334. * compiletime_assert - break build and emit msg if condition is false
  335. * @condition: a compile-time constant condition to check
  336. * @msg: a message to emit if condition is false
  337. *
  338. * In tradition of POSIX assert, this macro will break the build if the
  339. * supplied condition is *false*, emitting the supplied error message if the
  340. * compiler has support to do so.
  341. */
  342. #define compiletime_assert(condition, msg) \
  343. _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
  344. #define compiletime_assert_atomic_type(t) \
  345. compiletime_assert(__native_word(t), \
  346. "Need native word sized stores/loads for atomicity.")
  347. #endif /* __LINUX_COMPILER_H */