compiler.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. __aligned(4) \
  21. __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, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
  49. #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
  50. #define __trace_if_value(cond) ({ \
  51. static struct ftrace_branch_data \
  52. __aligned(4) \
  53. __section("_ftrace_branch") \
  54. __if_trace = { \
  55. .func = __func__, \
  56. .file = __FILE__, \
  57. .line = __LINE__, \
  58. }; \
  59. (cond) ? \
  60. (__if_trace.miss_hit[1]++,1) : \
  61. (__if_trace.miss_hit[0]++,0); \
  62. })
  63. #endif /* CONFIG_PROFILE_ALL_BRANCHES */
  64. #else
  65. # define likely(x) __builtin_expect(!!(x), 1)
  66. # define unlikely(x) __builtin_expect(!!(x), 0)
  67. # define likely_notrace(x) likely(x)
  68. # define unlikely_notrace(x) unlikely(x)
  69. #endif
  70. /* Optimization barrier */
  71. #ifndef barrier
  72. /* The "volatile" is due to gcc bugs */
  73. # define barrier() __asm__ __volatile__("": : :"memory")
  74. #endif
  75. #ifndef barrier_data
  76. /*
  77. * This version is i.e. to prevent dead stores elimination on @ptr
  78. * where gcc and llvm may behave differently when otherwise using
  79. * normal barrier(): while gcc behavior gets along with a normal
  80. * barrier(), llvm needs an explicit input variable to be assumed
  81. * clobbered. The issue is as follows: while the inline asm might
  82. * access any memory it wants, the compiler could have fit all of
  83. * @ptr into memory registers instead, and since @ptr never escaped
  84. * from that, it proved that the inline asm wasn't touching any of
  85. * it. This version works well with both compilers, i.e. we're telling
  86. * the compiler that the inline asm absolutely may see the contents
  87. * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
  88. */
  89. # define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
  90. #endif
  91. /* workaround for GCC PR82365 if needed */
  92. #ifndef barrier_before_unreachable
  93. # define barrier_before_unreachable() do { } while (0)
  94. #endif
  95. /* Unreachable code */
  96. #ifdef CONFIG_OBJTOOL
  97. /*
  98. * These macros help objtool understand GCC code flow for unreachable code.
  99. * The __COUNTER__ based labels are a hack to make each instance of the macros
  100. * unique, to convince GCC not to merge duplicate inline asm statements.
  101. */
  102. #define __stringify_label(n) #n
  103. #define __annotate_unreachable(c) ({ \
  104. asm volatile(__stringify_label(c) ":\n\t" \
  105. ".pushsection .discard.unreachable\n\t" \
  106. ".long " __stringify_label(c) "b - .\n\t" \
  107. ".popsection\n\t" : : "i" (c)); \
  108. })
  109. #define annotate_unreachable() __annotate_unreachable(__COUNTER__)
  110. /* Annotate a C jump table to allow objtool to follow the code flow */
  111. #define __annotate_jump_table __section(".rodata..c_jump_table")
  112. #else /* !CONFIG_OBJTOOL */
  113. #define annotate_unreachable()
  114. #define __annotate_jump_table
  115. #endif /* CONFIG_OBJTOOL */
  116. #ifndef unreachable
  117. # define unreachable() do { \
  118. annotate_unreachable(); \
  119. __builtin_unreachable(); \
  120. } while (0)
  121. #endif
  122. /*
  123. * KENTRY - kernel entry point
  124. * This can be used to annotate symbols (functions or data) that are used
  125. * without their linker symbol being referenced explicitly. For example,
  126. * interrupt vector handlers, or functions in the kernel image that are found
  127. * programatically.
  128. *
  129. * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
  130. * are handled in their own way (with KEEP() in linker scripts).
  131. *
  132. * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
  133. * linker script. For example an architecture could KEEP() its entire
  134. * boot/exception vector code rather than annotate each function and data.
  135. */
  136. #ifndef KENTRY
  137. # define KENTRY(sym) \
  138. extern typeof(sym) sym; \
  139. static const unsigned long __kentry_##sym \
  140. __used \
  141. __attribute__((__section__("___kentry+" #sym))) \
  142. = (unsigned long)&sym;
  143. #endif
  144. #ifndef RELOC_HIDE
  145. # define RELOC_HIDE(ptr, off) \
  146. ({ unsigned long __ptr; \
  147. __ptr = (unsigned long) (ptr); \
  148. (typeof(ptr)) (__ptr + (off)); })
  149. #endif
  150. #define absolute_pointer(val) RELOC_HIDE((void *)(val), 0)
  151. #ifndef OPTIMIZER_HIDE_VAR
  152. /* Make the optimizer believe the variable can be manipulated arbitrarily. */
  153. #define OPTIMIZER_HIDE_VAR(var) \
  154. __asm__ ("" : "=r" (var) : "0" (var))
  155. #endif
  156. /* Not-quite-unique ID. */
  157. #ifndef __UNIQUE_ID
  158. # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
  159. #endif
  160. /**
  161. * data_race - mark an expression as containing intentional data races
  162. *
  163. * This data_race() macro is useful for situations in which data races
  164. * should be forgiven. One example is diagnostic code that accesses
  165. * shared variables but is not a part of the core synchronization design.
  166. *
  167. * This macro *does not* affect normal code generation, but is a hint
  168. * to tooling that data races here are to be ignored.
  169. */
  170. #define data_race(expr) \
  171. ({ \
  172. __unqual_scalar_typeof(({ expr; })) __v = ({ \
  173. __kcsan_disable_current(); \
  174. expr; \
  175. }); \
  176. __kcsan_enable_current(); \
  177. __v; \
  178. })
  179. #endif /* __KERNEL__ */
  180. /*
  181. * Force the compiler to emit 'sym' as a symbol, so that we can reference
  182. * it from inline assembler. Necessary in case 'sym' could be inlined
  183. * otherwise, or eliminated entirely due to lack of references that are
  184. * visible to the compiler.
  185. */
  186. #define ___ADDRESSABLE(sym, __attrs) \
  187. static void * __used __attrs \
  188. __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym;
  189. #define __ADDRESSABLE(sym) \
  190. ___ADDRESSABLE(sym, __section(".discard.addressable"))
  191. /**
  192. * offset_to_ptr - convert a relative memory offset to an absolute pointer
  193. * @off: the address of the 32-bit offset value
  194. */
  195. static inline void *offset_to_ptr(const int *off)
  196. {
  197. return (void *)((unsigned long)off + *off);
  198. }
  199. #endif /* __ASSEMBLY__ */
  200. /* &a[0] degrades to a pointer: a different type from an array */
  201. #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
  202. /*
  203. * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
  204. * bool and also pointer types.
  205. */
  206. #define is_signed_type(type) (((type)(-1)) < (__force type)1)
  207. #define is_unsigned_type(type) (!is_signed_type(type))
  208. /*
  209. * This is needed in functions which generate the stack canary, see
  210. * arch/x86/kernel/smpboot.c::start_secondary() for an example.
  211. */
  212. #define prevent_tail_call_optimization() mb()
  213. #include <asm/rwonce.h>
  214. #endif /* __LINUX_COMPILER_H */