compiler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_LINUX_COMPILER_H_
  3. #define _TOOLS_LINUX_COMPILER_H_
  4. #ifdef __GNUC__
  5. #include <linux/compiler-gcc.h>
  6. #endif
  7. #ifndef __compiletime_error
  8. # define __compiletime_error(message)
  9. #endif
  10. #ifdef __OPTIMIZE__
  11. # define __compiletime_assert(condition, msg, prefix, suffix) \
  12. do { \
  13. extern void prefix ## suffix(void) __compiletime_error(msg); \
  14. if (!(condition)) \
  15. prefix ## suffix(); \
  16. } while (0)
  17. #else
  18. # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
  19. #endif
  20. #define _compiletime_assert(condition, msg, prefix, suffix) \
  21. __compiletime_assert(condition, msg, prefix, suffix)
  22. /**
  23. * compiletime_assert - break build and emit msg if condition is false
  24. * @condition: a compile-time constant condition to check
  25. * @msg: a message to emit if condition is false
  26. *
  27. * In tradition of POSIX assert, this macro will break the build if the
  28. * supplied condition is *false*, emitting the supplied error message if the
  29. * compiler has support to do so.
  30. */
  31. #define compiletime_assert(condition, msg) \
  32. _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
  33. /* Optimization barrier */
  34. /* The "volatile" is due to gcc bugs */
  35. #define barrier() __asm__ __volatile__("": : :"memory")
  36. #ifndef __always_inline
  37. # define __always_inline inline __attribute__((always_inline))
  38. #endif
  39. #ifndef noinline
  40. #define noinline
  41. #endif
  42. /* Are two types/vars the same type (ignoring qualifiers)? */
  43. #ifndef __same_type
  44. # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
  45. #endif
  46. #ifdef __ANDROID__
  47. /*
  48. * FIXME: Big hammer to get rid of tons of:
  49. * "warning: always_inline function might not be inlinable"
  50. *
  51. * At least on android-ndk-r12/platforms/android-24/arch-arm
  52. */
  53. #undef __always_inline
  54. #define __always_inline inline
  55. #endif
  56. #define __user
  57. #define __rcu
  58. #define __read_mostly
  59. #ifndef __attribute_const__
  60. # define __attribute_const__
  61. #endif
  62. #ifndef __maybe_unused
  63. # define __maybe_unused __attribute__((unused))
  64. #endif
  65. #ifndef __used
  66. # define __used __attribute__((__unused__))
  67. #endif
  68. #ifndef __packed
  69. # define __packed __attribute__((__packed__))
  70. #endif
  71. #ifndef __force
  72. # define __force
  73. #endif
  74. #ifndef __weak
  75. # define __weak __attribute__((weak))
  76. #endif
  77. #ifndef likely
  78. # define likely(x) __builtin_expect(!!(x), 1)
  79. #endif
  80. #ifndef unlikely
  81. # define unlikely(x) __builtin_expect(!!(x), 0)
  82. #endif
  83. #ifndef __init
  84. # define __init
  85. #endif
  86. #ifndef noinline
  87. # define noinline
  88. #endif
  89. #include <linux/types.h>
  90. /*
  91. * Following functions are taken from kernel sources and
  92. * break aliasing rules in their original form.
  93. *
  94. * While kernel is compiled with -fno-strict-aliasing,
  95. * perf uses -Wstrict-aliasing=3 which makes build fail
  96. * under gcc 4.4.
  97. *
  98. * Using extra __may_alias__ type to allow aliasing
  99. * in this case.
  100. */
  101. typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
  102. typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
  103. typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
  104. typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
  105. static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
  106. {
  107. switch (size) {
  108. case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
  109. case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
  110. case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
  111. case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
  112. default:
  113. barrier();
  114. __builtin_memcpy((void *)res, (const void *)p, size);
  115. barrier();
  116. }
  117. }
  118. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  119. {
  120. switch (size) {
  121. case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
  122. case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
  123. case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
  124. case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
  125. default:
  126. barrier();
  127. __builtin_memcpy((void *)p, (const void *)res, size);
  128. barrier();
  129. }
  130. }
  131. /*
  132. * Prevent the compiler from merging or refetching reads or writes. The
  133. * compiler is also forbidden from reordering successive instances of
  134. * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
  135. * particular ordering. One way to make the compiler aware of ordering is to
  136. * put the two invocations of READ_ONCE or WRITE_ONCE in different C
  137. * statements.
  138. *
  139. * These two macros will also work on aggregate data types like structs or
  140. * unions. If the size of the accessed data type exceeds the word size of
  141. * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
  142. * fall back to memcpy and print a compile-time warning.
  143. *
  144. * Their two major use cases are: (1) Mediating communication between
  145. * process-level code and irq/NMI handlers, all running on the same CPU,
  146. * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
  147. * mutilate accesses that either do not require ordering or that interact
  148. * with an explicit memory barrier or atomic instruction that provides the
  149. * required ordering.
  150. */
  151. #define READ_ONCE(x) \
  152. ({ \
  153. union { typeof(x) __val; char __c[1]; } __u = \
  154. { .__c = { 0 } }; \
  155. __read_once_size(&(x), __u.__c, sizeof(x)); \
  156. __u.__val; \
  157. })
  158. #define WRITE_ONCE(x, val) \
  159. ({ \
  160. union { typeof(x) __val; char __c[1]; } __u = \
  161. { .__val = (val) }; \
  162. __write_once_size(&(x), __u.__c, sizeof(x)); \
  163. __u.__val; \
  164. })
  165. #ifndef __fallthrough
  166. # define __fallthrough
  167. #endif
  168. /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
  169. #define ___PASTE(a, b) a##b
  170. #define __PASTE(a, b) ___PASTE(a, b)
  171. #endif /* _TOOLS_LINUX_COMPILER_H */