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