xref: /kvm-unit-tests/lib/linux/compiler.h (revision d5b60621b0e6863fd62e4657b7a2ec70bf7eee34)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Taken from Linux commit 219d54332a09 ("Linux 5.4"), from the file
4  * tools/include/linux/compiler.h, with minor changes.
5  */
6 #ifndef __LINUX_COMPILER_H
7 #define __LINUX_COMPILER_H
8 
9 #ifndef __ASSEMBLY__
10 
11 #include <stdint.h>
12 
13 #define barrier()	asm volatile("" : : : "memory")
14 
15 #define __always_inline	inline __attribute__((always_inline))
16 
17 static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
18 {
19 	switch (size) {
20 	case 1: *(uint8_t *)res = *(volatile uint8_t *)p; break;
21 	case 2: *(uint16_t *)res = *(volatile uint16_t *)p; break;
22 	case 4: *(uint32_t *)res = *(volatile uint32_t *)p; break;
23 	case 8: *(uint64_t *)res = *(volatile uint64_t *)p; break;
24 	default:
25 		barrier();
26 		__builtin_memcpy((void *)res, (const void *)p, size);
27 		barrier();
28 	}
29 }
30 
31 /*
32  * Prevent the compiler from merging or refetching reads or writes. The
33  * compiler is also forbidden from reordering successive instances of
34  * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
35  * particular ordering. One way to make the compiler aware of ordering is to
36  * put the two invocations of READ_ONCE or WRITE_ONCE in different C
37  * statements.
38  *
39  * These two macros will also work on aggregate data types like structs or
40  * unions. If the size of the accessed data type exceeds the word size of
41  * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
42  * fall back to memcpy and print a compile-time warning.
43  *
44  * Their two major use cases are: (1) Mediating communication between
45  * process-level code and irq/NMI handlers, all running on the same CPU,
46  * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
47  * mutilate accesses that either do not require ordering or that interact
48  * with an explicit memory barrier or atomic instruction that provides the
49  * required ordering.
50  */
51 
52 #define READ_ONCE(x)					\
53 ({							\
54 	union { typeof(x) __val; char __c[1]; } __u =	\
55 		{ .__c = { 0 } };			\
56 	__read_once_size(&(x), __u.__c, sizeof(x));	\
57 	__u.__val;					\
58 })
59 
60 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
61 {
62 	switch (size) {
63 	case 1: *(volatile uint8_t *) p = *(uint8_t  *) res; break;
64 	case 2: *(volatile uint16_t *) p = *(uint16_t *) res; break;
65 	case 4: *(volatile uint32_t *) p = *(uint32_t *) res; break;
66 	case 8: *(volatile uint64_t *) p = *(uint64_t *) res; break;
67 	default:
68 		barrier();
69 		__builtin_memcpy((void *)p, (const void *)res, size);
70 		barrier();
71 	}
72 }
73 
74 #define WRITE_ONCE(x, val)				\
75 ({							\
76 	union { typeof(x) __val; char __c[1]; } __u =	\
77 		{ .__val = (val) }; 			\
78 	__write_once_size(&(x), __u.__c, sizeof(x));	\
79 	__u.__val;					\
80 })
81 
82 #endif /* !__ASSEMBLY__ */
83 #endif /* !__LINUX_COMPILER_H */
84