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