compiler.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_LINUX_COMPILER_H_
  3. #define _TOOLS_LINUX_COMPILER_H_
  4. #ifndef __ASSEMBLY__
  5. #include <linux/compiler_types.h>
  6. #ifndef __compiletime_error
  7. # define __compiletime_error(message)
  8. #endif
  9. #ifdef __OPTIMIZE__
  10. # define __compiletime_assert(condition, msg, prefix, suffix) \
  11. do { \
  12. extern void prefix ## suffix(void) __compiletime_error(msg); \
  13. if (!(condition)) \
  14. prefix ## suffix(); \
  15. } while (0)
  16. #else
  17. # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
  18. #endif
  19. #define _compiletime_assert(condition, msg, prefix, suffix) \
  20. __compiletime_assert(condition, msg, prefix, suffix)
  21. /**
  22. * compiletime_assert - break build and emit msg if condition is false
  23. * @condition: a compile-time constant condition to check
  24. * @msg: a message to emit if condition is false
  25. *
  26. * In tradition of POSIX assert, this macro will break the build if the
  27. * supplied condition is *false*, emitting the supplied error message if the
  28. * compiler has support to do so.
  29. */
  30. #define compiletime_assert(condition, msg) \
  31. _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
  32. /* Optimization barrier */
  33. /* The "volatile" is due to gcc bugs */
  34. #define barrier() __asm__ __volatile__("": : :"memory")
  35. #ifndef __always_inline
  36. # define __always_inline inline __attribute__((always_inline))
  37. #endif
  38. #ifndef __always_unused
  39. #define __always_unused __attribute__((__unused__))
  40. #endif
  41. #ifndef __noreturn
  42. #define __noreturn __attribute__((__noreturn__))
  43. #endif
  44. #ifndef unreachable
  45. #define unreachable() __builtin_unreachable()
  46. #endif
  47. #ifndef noinline
  48. #define noinline
  49. #endif
  50. #ifndef __nocf_check
  51. #define __nocf_check __attribute__((nocf_check))
  52. #endif
  53. #ifndef __naked
  54. #define __naked __attribute__((__naked__))
  55. #endif
  56. /* Are two types/vars the same type (ignoring qualifiers)? */
  57. #ifndef __same_type
  58. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  59. #endif
  60. /*
  61. * This returns a constant expression while determining if an argument is
  62. * a constant expression, most importantly without evaluating the argument.
  63. * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
  64. */
  65. #define __is_constexpr(x) \
  66. (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
  67. /*
  68. * Similar to statically_true() but produces a constant expression
  69. *
  70. * To be used in conjunction with macros, such as BUILD_BUG_ON_ZERO(),
  71. * which require their input to be a constant expression and for which
  72. * statically_true() would otherwise fail.
  73. *
  74. * This is a trade-off: const_true() requires all its operands to be
  75. * compile time constants. Else, it would always returns false even on
  76. * the most trivial cases like:
  77. *
  78. * true || non_const_var
  79. *
  80. * On the opposite, statically_true() is able to fold more complex
  81. * tautologies and will return true on expressions such as:
  82. *
  83. * !(non_const_var * 8 % 4)
  84. *
  85. * For the general case, statically_true() is better.
  86. */
  87. #define const_true(x) __builtin_choose_expr(__is_constexpr(x), x, false)
  88. #ifdef __ANDROID__
  89. /*
  90. * FIXME: Big hammer to get rid of tons of:
  91. * "warning: always_inline function might not be inlinable"
  92. *
  93. * At least on android-ndk-r12/platforms/android-24/arch-arm
  94. */
  95. #undef __always_inline
  96. #define __always_inline inline
  97. #endif
  98. #define __user
  99. #define __rcu
  100. #define __read_mostly
  101. #ifndef __attribute_const__
  102. # define __attribute_const__
  103. #endif
  104. #ifndef __maybe_unused
  105. # define __maybe_unused __attribute__((unused))
  106. #endif
  107. #ifndef __used
  108. # define __used __attribute__((__unused__))
  109. #endif
  110. #ifndef __packed
  111. # define __packed __attribute__((__packed__))
  112. #endif
  113. #ifndef __force
  114. # define __force
  115. #endif
  116. #ifndef __iomem
  117. # define __iomem
  118. #endif
  119. #ifndef __weak
  120. # define __weak __attribute__((weak))
  121. #endif
  122. #ifndef likely
  123. # define likely(x) __builtin_expect(!!(x), 1)
  124. #endif
  125. #ifndef unlikely
  126. # define unlikely(x) __builtin_expect(!!(x), 0)
  127. #endif
  128. #include <linux/types.h>
  129. /*
  130. * Following functions are taken from kernel sources and
  131. * break aliasing rules in their original form.
  132. *
  133. * While kernel is compiled with -fno-strict-aliasing,
  134. * perf uses -Wstrict-aliasing=3 which makes build fail
  135. * under gcc 4.4.
  136. *
  137. * Using extra __may_alias__ type to allow aliasing
  138. * in this case.
  139. */
  140. typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
  141. typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
  142. typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
  143. typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
  144. static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
  145. {
  146. switch (size) {
  147. case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
  148. case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
  149. case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
  150. case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
  151. default:
  152. barrier();
  153. __builtin_memcpy((void *)res, (const void *)p, size);
  154. barrier();
  155. }
  156. }
  157. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  158. {
  159. switch (size) {
  160. case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
  161. case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
  162. case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
  163. case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
  164. default:
  165. barrier();
  166. __builtin_memcpy((void *)p, (const void *)res, size);
  167. barrier();
  168. }
  169. }
  170. /*
  171. * Prevent the compiler from merging or refetching reads or writes. The
  172. * compiler is also forbidden from reordering successive instances of
  173. * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
  174. * particular ordering. One way to make the compiler aware of ordering is to
  175. * put the two invocations of READ_ONCE or WRITE_ONCE in different C
  176. * statements.
  177. *
  178. * These two macros will also work on aggregate data types like structs or
  179. * unions. If the size of the accessed data type exceeds the word size of
  180. * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
  181. * fall back to memcpy and print a compile-time warning.
  182. *
  183. * Their two major use cases are: (1) Mediating communication between
  184. * process-level code and irq/NMI handlers, all running on the same CPU,
  185. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  186. * mutilate accesses that either do not require ordering or that interact
  187. * with an explicit memory barrier or atomic instruction that provides the
  188. * required ordering.
  189. */
  190. #define READ_ONCE(x) \
  191. ({ \
  192. union { typeof(x) __val; char __c[1]; } __u = \
  193. { .__c = { 0 } }; \
  194. __read_once_size(&(x), __u.__c, sizeof(x)); \
  195. __u.__val; \
  196. })
  197. #define WRITE_ONCE(x, val) \
  198. ({ \
  199. union { typeof(x) __val; char __c[1]; } __u = \
  200. { .__val = (val) }; \
  201. __write_once_size(&(x), __u.__c, sizeof(x)); \
  202. __u.__val; \
  203. })
  204. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  205. #define ___PASTE(a, b) a##b
  206. #define __PASTE(a, b) ___PASTE(a, b)
  207. #ifndef OPTIMIZER_HIDE_VAR
  208. /* Make the optimizer believe the variable can be manipulated arbitrarily. */
  209. #define OPTIMIZER_HIDE_VAR(var) \
  210. __asm__ ("" : "=r" (var) : "0" (var))
  211. #endif
  212. #ifndef __BUILD_BUG_ON_ZERO_MSG
  213. #if defined(__clang__)
  214. #define __BUILD_BUG_ON_ZERO_MSG(e, msg, ...) ((int)(sizeof(struct { int:(-!!(e)); })))
  215. #else
  216. #define __BUILD_BUG_ON_ZERO_MSG(e, msg, ...) ((int)sizeof(struct {_Static_assert(!(e), msg);}))
  217. #endif
  218. #endif
  219. #endif /* __ASSEMBLY__ */
  220. #endif /* _TOOLS_LINUX_COMPILER_H */