Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef __LINUX_COMPILER_H |
3 | #define __LINUX_COMPILER_H | |
4 | ||
d1515582 | 5 | #include <linux/compiler_types.h> |
1da177e4 | 6 | |
d1515582 | 7 | #ifndef __ASSEMBLY__ |
6f33d587 | 8 | |
1da177e4 LT |
9 | #ifdef __KERNEL__ |
10 | ||
2ed84eeb SR |
11 | /* |
12 | * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code | |
13 | * to disable branch tracing on a per file basis. | |
14 | */ | |
134e6a03 | 15 | void ftrace_likely_update(struct ftrace_likely_data *f, int val, |
d45ae1f7 | 16 | int expect, int is_constant); |
a18ef64f AB |
17 | #if defined(CONFIG_TRACE_BRANCH_PROFILING) \ |
18 | && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__) | |
1f0d69a9 SR |
19 | #define likely_notrace(x) __builtin_expect(!!(x), 1) |
20 | #define unlikely_notrace(x) __builtin_expect(!!(x), 0) | |
21 | ||
d45ae1f7 | 22 | #define __branch_check__(x, expect, is_constant) ({ \ |
2026d357 | 23 | long ______r; \ |
134e6a03 | 24 | static struct ftrace_likely_data \ |
e04462fb | 25 | __aligned(4) \ |
33def849 | 26 | __section("_ftrace_annotated_branch") \ |
1f0d69a9 | 27 | ______f = { \ |
134e6a03 SRV |
28 | .data.func = __func__, \ |
29 | .data.file = __FILE__, \ | |
30 | .data.line = __LINE__, \ | |
1f0d69a9 | 31 | }; \ |
d45ae1f7 SRV |
32 | ______r = __builtin_expect(!!(x), expect); \ |
33 | ftrace_likely_update(&______f, ______r, \ | |
34 | expect, is_constant); \ | |
1f0d69a9 SR |
35 | ______r; \ |
36 | }) | |
37 | ||
38 | /* | |
39 | * Using __builtin_constant_p(x) to ignore cases where the return | |
40 | * value is always the same. This idea is taken from a similar patch | |
41 | * written by Daniel Walker. | |
42 | */ | |
43 | # ifndef likely | |
d45ae1f7 | 44 | # define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x))) |
1f0d69a9 SR |
45 | # endif |
46 | # ifndef unlikely | |
d45ae1f7 | 47 | # define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x))) |
1f0d69a9 | 48 | # endif |
2bcd521a SR |
49 | |
50 | #ifdef CONFIG_PROFILE_ALL_BRANCHES | |
51 | /* | |
52 | * "Define 'is'", Bill Clinton | |
53 | * "Define 'if'", Steven Rostedt | |
54 | */ | |
a15fd609 LT |
55 | #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) ) |
56 | ||
57 | #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond)) | |
58 | ||
59 | #define __trace_if_value(cond) ({ \ | |
60 | static struct ftrace_branch_data \ | |
61 | __aligned(4) \ | |
33def849 | 62 | __section("_ftrace_branch") \ |
a15fd609 LT |
63 | __if_trace = { \ |
64 | .func = __func__, \ | |
65 | .file = __FILE__, \ | |
66 | .line = __LINE__, \ | |
67 | }; \ | |
68 | (cond) ? \ | |
69 | (__if_trace.miss_hit[1]++,1) : \ | |
70 | (__if_trace.miss_hit[0]++,0); \ | |
71 | }) | |
72 | ||
2bcd521a SR |
73 | #endif /* CONFIG_PROFILE_ALL_BRANCHES */ |
74 | ||
1f0d69a9 SR |
75 | #else |
76 | # define likely(x) __builtin_expect(!!(x), 1) | |
77 | # define unlikely(x) __builtin_expect(!!(x), 0) | |
2f0df49c SRV |
78 | # define likely_notrace(x) likely(x) |
79 | # define unlikely_notrace(x) unlikely(x) | |
1f0d69a9 | 80 | #endif |
1da177e4 LT |
81 | |
82 | /* Optimization barrier */ | |
83 | #ifndef barrier | |
3347acc6 AS |
84 | /* The "volatile" is due to gcc bugs */ |
85 | # define barrier() __asm__ __volatile__("": : :"memory") | |
1da177e4 LT |
86 | #endif |
87 | ||
7829fb09 | 88 | #ifndef barrier_data |
3347acc6 AS |
89 | /* |
90 | * This version is i.e. to prevent dead stores elimination on @ptr | |
91 | * where gcc and llvm may behave differently when otherwise using | |
92 | * normal barrier(): while gcc behavior gets along with a normal | |
93 | * barrier(), llvm needs an explicit input variable to be assumed | |
94 | * clobbered. The issue is as follows: while the inline asm might | |
95 | * access any memory it wants, the compiler could have fit all of | |
96 | * @ptr into memory registers instead, and since @ptr never escaped | |
97 | * from that, it proved that the inline asm wasn't touching any of | |
98 | * it. This version works well with both compilers, i.e. we're telling | |
99 | * the compiler that the inline asm absolutely may see the contents | |
100 | * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495 | |
101 | */ | |
102 | # define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory") | |
7829fb09 DB |
103 | #endif |
104 | ||
173a3efd AB |
105 | /* workaround for GCC PR82365 if needed */ |
106 | #ifndef barrier_before_unreachable | |
107 | # define barrier_before_unreachable() do { } while (0) | |
108 | #endif | |
109 | ||
38938c87 | 110 | /* Unreachable code */ |
03f16cd0 | 111 | #ifdef CONFIG_OBJTOOL |
d0c2e691 JP |
112 | /* |
113 | * These macros help objtool understand GCC code flow for unreachable code. | |
114 | * The __COUNTER__ based labels are a hack to make each instance of the macros | |
115 | * unique, to convince GCC not to merge duplicate inline asm statements. | |
116 | */ | |
f1069a87 VG |
117 | #define __stringify_label(n) #n |
118 | ||
f1069a87 VG |
119 | #define __annotate_unreachable(c) ({ \ |
120 | asm volatile(__stringify_label(c) ":\n\t" \ | |
96af6cd0 | 121 | ".pushsection .discard.unreachable\n\t" \ |
f1069a87 | 122 | ".long " __stringify_label(c) "b - .\n\t" \ |
dcce50e6 | 123 | ".popsection\n\t" : : "i" (c)); \ |
649ea4d5 | 124 | }) |
f1069a87 VG |
125 | #define annotate_unreachable() __annotate_unreachable(__COUNTER__) |
126 | ||
87b512de | 127 | /* Annotate a C jump table to allow objtool to follow the code flow */ |
33def849 | 128 | #define __annotate_jump_table __section(".rodata..c_jump_table") |
87b512de | 129 | |
03f16cd0 | 130 | #else /* !CONFIG_OBJTOOL */ |
649ea4d5 | 131 | #define annotate_unreachable() |
87b512de | 132 | #define __annotate_jump_table |
03f16cd0 | 133 | #endif /* CONFIG_OBJTOOL */ |
649ea4d5 | 134 | |
38938c87 | 135 | #ifndef unreachable |
fe0640eb | 136 | # define unreachable() do { \ |
137 | annotate_unreachable(); \ | |
138 | __builtin_unreachable(); \ | |
139 | } while (0) | |
38938c87 DD |
140 | #endif |
141 | ||
b67067f1 NP |
142 | /* |
143 | * KENTRY - kernel entry point | |
144 | * This can be used to annotate symbols (functions or data) that are used | |
145 | * without their linker symbol being referenced explicitly. For example, | |
146 | * interrupt vector handlers, or functions in the kernel image that are found | |
147 | * programatically. | |
148 | * | |
149 | * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those | |
150 | * are handled in their own way (with KEEP() in linker scripts). | |
151 | * | |
152 | * KENTRY can be avoided if the symbols in question are marked as KEEP() in the | |
153 | * linker script. For example an architecture could KEEP() its entire | |
154 | * boot/exception vector code rather than annotate each function and data. | |
155 | */ | |
156 | #ifndef KENTRY | |
157 | # define KENTRY(sym) \ | |
158 | extern typeof(sym) sym; \ | |
159 | static const unsigned long __kentry_##sym \ | |
160 | __used \ | |
a25c13b3 | 161 | __attribute__((__section__("___kentry+" #sym))) \ |
b67067f1 NP |
162 | = (unsigned long)&sym; |
163 | #endif | |
164 | ||
1da177e4 LT |
165 | #ifndef RELOC_HIDE |
166 | # define RELOC_HIDE(ptr, off) \ | |
167 | ({ unsigned long __ptr; \ | |
168 | __ptr = (unsigned long) (ptr); \ | |
169 | (typeof(ptr)) (__ptr + (off)); }) | |
170 | #endif | |
171 | ||
f6b5f1a5 GR |
172 | #define absolute_pointer(val) RELOC_HIDE((void *)(val), 0) |
173 | ||
fe8c8a12 | 174 | #ifndef OPTIMIZER_HIDE_VAR |
3e2ffd65 MT |
175 | /* Make the optimizer believe the variable can be manipulated arbitrarily. */ |
176 | #define OPTIMIZER_HIDE_VAR(var) \ | |
177 | __asm__ ("" : "=r" (var) : "0" (var)) | |
fe8c8a12 CEB |
178 | #endif |
179 | ||
6f33d587 RR |
180 | /* Not-quite-unique ID. */ |
181 | #ifndef __UNIQUE_ID | |
182 | # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__) | |
183 | #endif | |
184 | ||
37d1a04b TG |
185 | /** |
186 | * data_race - mark an expression as containing intentional data races | |
187 | * | |
188 | * This data_race() macro is useful for situations in which data races | |
189 | * should be forgiven. One example is diagnostic code that accesses | |
190 | * shared variables but is not a part of the core synchronization design. | |
191 | * | |
192 | * This macro *does not* affect normal code generation, but is a hint | |
193 | * to tooling that data races here are to be ignored. | |
194 | */ | |
195 | #define data_race(expr) \ | |
d976441f | 196 | ({ \ |
95c094fc ME |
197 | __unqual_scalar_typeof(({ expr; })) __v = ({ \ |
198 | __kcsan_disable_current(); \ | |
199 | expr; \ | |
37d1a04b | 200 | }); \ |
95c094fc ME |
201 | __kcsan_enable_current(); \ |
202 | __v; \ | |
d976441f | 203 | }) |
230fa253 | 204 | |
1da177e4 LT |
205 | #endif /* __KERNEL__ */ |
206 | ||
7290d580 AB |
207 | /* |
208 | * Force the compiler to emit 'sym' as a symbol, so that we can reference | |
209 | * it from inline assembler. Necessary in case 'sym' could be inlined | |
210 | * otherwise, or eliminated entirely due to lack of references that are | |
211 | * visible to the compiler. | |
212 | */ | |
92efda8e ST |
213 | #define ___ADDRESSABLE(sym, __attrs) \ |
214 | static void * __used __attrs \ | |
563a02b0 | 215 | __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym; |
92efda8e ST |
216 | #define __ADDRESSABLE(sym) \ |
217 | ___ADDRESSABLE(sym, __section(".discard.addressable")) | |
7290d580 AB |
218 | |
219 | /** | |
220 | * offset_to_ptr - convert a relative memory offset to an absolute pointer | |
221 | * @off: the address of the 32-bit offset value | |
222 | */ | |
223 | static inline void *offset_to_ptr(const int *off) | |
224 | { | |
225 | return (void *)((unsigned long)off + *off); | |
226 | } | |
227 | ||
1da177e4 LT |
228 | #endif /* __ASSEMBLY__ */ |
229 | ||
ec0bbef6 MO |
230 | /* &a[0] degrades to a pointer: a different type from an array */ |
231 | #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0])) | |
ec0bbef6 | 232 | |
dcf8e563 BVA |
233 | /* |
234 | * Whether 'type' is a signed type or an unsigned type. Supports scalar types, | |
235 | * bool and also pointer types. | |
236 | */ | |
237 | #define is_signed_type(type) (((type)(-1)) < (__force type)1) | |
4b21d25b | 238 | #define is_unsigned_type(type) (!is_signed_type(type)) |
dcf8e563 | 239 | |
a9a3ed1e BP |
240 | /* |
241 | * This is needed in functions which generate the stack canary, see | |
242 | * arch/x86/kernel/smpboot.c::start_secondary() for an example. | |
243 | */ | |
244 | #define prevent_tail_call_optimization() mb() | |
245 | ||
e506ea45 WD |
246 | #include <asm/rwonce.h> |
247 | ||
1da177e4 | 248 | #endif /* __LINUX_COMPILER_H */ |