xref: /aosp_15_r20/external/libbpf/include/linux/compiler.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker 
3*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LINUX_COMPILER_H
4*f7c14bbaSAndroid Build Coastguard Worker #define __LINUX_COMPILER_H
5*f7c14bbaSAndroid Build Coastguard Worker 
6*f7c14bbaSAndroid Build Coastguard Worker #define likely(x)		__builtin_expect(!!(x), 1)
7*f7c14bbaSAndroid Build Coastguard Worker #define unlikely(x)		__builtin_expect(!!(x), 0)
8*f7c14bbaSAndroid Build Coastguard Worker 
9*f7c14bbaSAndroid Build Coastguard Worker #define READ_ONCE(x)		(*(volatile typeof(x) *)&x)
10*f7c14bbaSAndroid Build Coastguard Worker #define WRITE_ONCE(x, v)	(*(volatile typeof(x) *)&x) = (v)
11*f7c14bbaSAndroid Build Coastguard Worker 
12*f7c14bbaSAndroid Build Coastguard Worker #define barrier()		asm volatile("" ::: "memory")
13*f7c14bbaSAndroid Build Coastguard Worker 
14*f7c14bbaSAndroid Build Coastguard Worker #if defined(__x86_64__)
15*f7c14bbaSAndroid Build Coastguard Worker 
16*f7c14bbaSAndroid Build Coastguard Worker # define smp_rmb()		barrier()
17*f7c14bbaSAndroid Build Coastguard Worker # define smp_wmb()		barrier()
18*f7c14bbaSAndroid Build Coastguard Worker # define smp_mb()		asm volatile("lock; addl $0,-132(%%rsp)" ::: "memory", "cc")
19*f7c14bbaSAndroid Build Coastguard Worker 
20*f7c14bbaSAndroid Build Coastguard Worker # define smp_store_release(p, v)		\
21*f7c14bbaSAndroid Build Coastguard Worker do {						\
22*f7c14bbaSAndroid Build Coastguard Worker 	barrier();				\
23*f7c14bbaSAndroid Build Coastguard Worker 	WRITE_ONCE(*p, v);			\
24*f7c14bbaSAndroid Build Coastguard Worker } while (0)
25*f7c14bbaSAndroid Build Coastguard Worker 
26*f7c14bbaSAndroid Build Coastguard Worker # define smp_load_acquire(p)			\
27*f7c14bbaSAndroid Build Coastguard Worker ({						\
28*f7c14bbaSAndroid Build Coastguard Worker 	typeof(*p) ___p = READ_ONCE(*p);	\
29*f7c14bbaSAndroid Build Coastguard Worker 	barrier();				\
30*f7c14bbaSAndroid Build Coastguard Worker 	___p;					\
31*f7c14bbaSAndroid Build Coastguard Worker })
32*f7c14bbaSAndroid Build Coastguard Worker 
33*f7c14bbaSAndroid Build Coastguard Worker #elif defined(__aarch64__)
34*f7c14bbaSAndroid Build Coastguard Worker 
35*f7c14bbaSAndroid Build Coastguard Worker # define smp_rmb()		asm volatile("dmb ishld" ::: "memory")
36*f7c14bbaSAndroid Build Coastguard Worker # define smp_wmb()		asm volatile("dmb ishst" ::: "memory")
37*f7c14bbaSAndroid Build Coastguard Worker # define smp_mb()		asm volatile("dmb ish" ::: "memory")
38*f7c14bbaSAndroid Build Coastguard Worker 
39*f7c14bbaSAndroid Build Coastguard Worker #endif
40*f7c14bbaSAndroid Build Coastguard Worker 
41*f7c14bbaSAndroid Build Coastguard Worker #ifndef smp_mb
42*f7c14bbaSAndroid Build Coastguard Worker # define smp_mb()		__sync_synchronize()
43*f7c14bbaSAndroid Build Coastguard Worker #endif
44*f7c14bbaSAndroid Build Coastguard Worker 
45*f7c14bbaSAndroid Build Coastguard Worker #ifndef smp_rmb
46*f7c14bbaSAndroid Build Coastguard Worker # define smp_rmb()		smp_mb()
47*f7c14bbaSAndroid Build Coastguard Worker #endif
48*f7c14bbaSAndroid Build Coastguard Worker 
49*f7c14bbaSAndroid Build Coastguard Worker #ifndef smp_wmb
50*f7c14bbaSAndroid Build Coastguard Worker # define smp_wmb()		smp_mb()
51*f7c14bbaSAndroid Build Coastguard Worker #endif
52*f7c14bbaSAndroid Build Coastguard Worker 
53*f7c14bbaSAndroid Build Coastguard Worker #ifndef smp_store_release
54*f7c14bbaSAndroid Build Coastguard Worker # define smp_store_release(p, v)		\
55*f7c14bbaSAndroid Build Coastguard Worker do {						\
56*f7c14bbaSAndroid Build Coastguard Worker 	smp_mb();				\
57*f7c14bbaSAndroid Build Coastguard Worker 	WRITE_ONCE(*p, v);			\
58*f7c14bbaSAndroid Build Coastguard Worker } while (0)
59*f7c14bbaSAndroid Build Coastguard Worker #endif
60*f7c14bbaSAndroid Build Coastguard Worker 
61*f7c14bbaSAndroid Build Coastguard Worker #ifndef smp_load_acquire
62*f7c14bbaSAndroid Build Coastguard Worker # define smp_load_acquire(p)			\
63*f7c14bbaSAndroid Build Coastguard Worker ({						\
64*f7c14bbaSAndroid Build Coastguard Worker 	typeof(*p) ___p = READ_ONCE(*p);	\
65*f7c14bbaSAndroid Build Coastguard Worker 	smp_mb();				\
66*f7c14bbaSAndroid Build Coastguard Worker 	___p;					\
67*f7c14bbaSAndroid Build Coastguard Worker })
68*f7c14bbaSAndroid Build Coastguard Worker #endif
69*f7c14bbaSAndroid Build Coastguard Worker 
70*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LINUX_COMPILER_H */
71