1#ifndef _TOOLS_LINUX_COMPILER_H_
2#define _TOOLS_LINUX_COMPILER_H_
3
4
5
6#define barrier() __asm__ __volatile__("": : :"memory")
7
8#ifndef __always_inline
9# define __always_inline inline __attribute__((always_inline))
10#endif
11
12#define __user
13
14#ifndef __attribute_const__
15# define __attribute_const__
16#endif
17
18#ifndef __maybe_unused
19# define __maybe_unused __attribute__((unused))
20#endif
21
22#ifndef __packed
23# define __packed __attribute__((__packed__))
24#endif
25
26#ifndef __force
27# define __force
28#endif
29
30#ifndef __weak
31# define __weak __attribute__((weak))
32#endif
33
34#ifndef likely
35# define likely(x) __builtin_expect(!!(x), 1)
36#endif
37
38#ifndef unlikely
39# define unlikely(x) __builtin_expect(!!(x), 0)
40#endif
41
42#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
43
44#include <linux/types.h>
45
46
47
48
49
50
51
52
53
54
55
56
57typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
58typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
59typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
60typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
61
62static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
63{
64 switch (size) {
65 case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
66 case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
67 case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
68 case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
69 default:
70 barrier();
71 __builtin_memcpy((void *)res, (const void *)p, size);
72 barrier();
73 }
74}
75
76static __always_inline void __write_once_size(volatile void *p, void *res, int size)
77{
78 switch (size) {
79 case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
80 case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
81 case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
82 case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
83 default:
84 barrier();
85 __builtin_memcpy((void *)p, (const void *)res, size);
86 barrier();
87 }
88}
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112#define READ_ONCE(x) \
113 ({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
114
115#define WRITE_ONCE(x, val) \
116 ({ union { typeof(x) __val; char __c[1]; } __u = { .__val = (val) }; __write_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
117
118#endif
119