1 #ifndef __LINUX_COMPILER_H
2 #define __LINUX_COMPILER_H
3
4 #ifdef __clang__
5 # define __user
6 # define __force
7 #else
8
9 #ifndef __ASSEMBLY__
10
11 #ifdef __CHECKER__
12 # define __user __attribute__((noderef, address_space(1)))
13 # define __kernel __attribute__((address_space(0)))
14 # define __safe __attribute__((safe))
15 # define __force __attribute__((force))
16 # define __nocast __attribute__((nocast))
17 # define __iomem __attribute__((noderef, address_space(2)))
18 # define __must_hold(x) __attribute__((context(x,1,1)))
19 # define __acquires(x) __attribute__((context(x,0,1)))
20 # define __releases(x) __attribute__((context(x,1,0)))
21 # define __acquire(x) __context__(x,1)
22 # define __release(x) __context__(x,-1)
23 # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
24 # define __percpu __attribute__((noderef, address_space(3)))
25 # define __pmem __attribute__((noderef, address_space(5)))
26 #ifdef CONFIG_SPARSE_RCU_POINTER
27 # define __rcu __attribute__((noderef, address_space(4)))
28 #else
29 # define __rcu
30 #endif
31 extern void __chk_user_ptr(const volatile void __user *);
32 extern void __chk_io_ptr(const volatile void __iomem *);
33 #else
34 # define __user
35 # define __kernel
36 # define __safe
37 # define __force
38 # define __nocast
39 # define __iomem
40 # define __chk_user_ptr(x) (void)0
41 # define __chk_io_ptr(x) (void)0
42 # define __builtin_warning(x, y...) (1)
43 # define __must_hold(x)
44 # define __acquires(x)
45 # define __releases(x)
46 # define __acquire(x) (void)0
47 # define __release(x) (void)0
48 # define __cond_lock(x,c) (c)
49 # define __percpu
50 # define __rcu
51 # define __pmem
52 #endif
53
54 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
55 #define ___PASTE(a,b) a##b
56 #define __PASTE(a,b) ___PASTE(a,b)
57
58 #ifdef __KERNEL__
59
60 /*
61 * Minimal backport of compiler_attributes.h to add support for __copy
62 * to v4.9.y so that we can use it in init/exit_module to avoid
63 * -Werror=missing-attributes errors on GCC 9.
64 */
65 #ifndef __has_attribute
66 # define __has_attribute(x) __GCC4_has_attribute_##x
67 # define __GCC4_has_attribute___copy__ 0
68 #endif
69
70 #if __has_attribute(__copy__)
71 # define __copy(symbol) __attribute__((__copy__(symbol)))
72 #else
73 # define __copy(symbol)
74 #endif
75
76 #ifdef __GNUC__
77 #include <linux/compiler-gcc.h>
78 #endif
79
80 #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
81 #define notrace __attribute__((hotpatch(0,0)))
82 #else
83 #define notrace __attribute__((no_instrument_function))
84 #endif
85
86 /* Intel compiler defines __GNUC__. So we will overwrite implementations
87 * coming from above header files here
88 */
89 #ifdef __INTEL_COMPILER
90 # include <linux/compiler-intel.h>
91 #endif
92
93 /* Clang compiler defines __GNUC__. So we will overwrite implementations
94 * coming from above header files here
95 */
96 #ifdef __clang__
97 #include <linux/compiler-clang.h>
98 #endif
99
100 /*
101 * Generic compiler-dependent macros required for kernel
102 * build go below this comment. Actual compiler/compiler version
103 * specific implementations come from the above header files
104 */
105
106 struct ftrace_branch_data {
107 const char *func;
108 const char *file;
109 unsigned line;
110 union {
111 struct {
112 unsigned long correct;
113 unsigned long incorrect;
114 };
115 struct {
116 unsigned long miss;
117 unsigned long hit;
118 };
119 unsigned long miss_hit[2];
120 };
121 };
122
123 /*
124 * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
125 * to disable branch tracing on a per file basis.
126 */
127 #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
128 && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
129 void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
130
131 #define likely_notrace(x) __builtin_expect(!!(x), 1)
132 #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
133
134 #define __branch_check__(x, expect) ({ \
135 long ______r; \
136 static struct ftrace_branch_data \
137 __attribute__((__aligned__(4))) \
138 __attribute__((section("_ftrace_annotated_branch"))) \
139 ______f = { \
140 .func = __func__, \
141 .file = __FILE__, \
142 .line = __LINE__, \
143 }; \
144 ______r = likely_notrace(x); \
145 ftrace_likely_update(&______f, ______r, expect); \
146 ______r; \
147 })
148
149 /*
150 * Using __builtin_constant_p(x) to ignore cases where the return
151 * value is always the same. This idea is taken from a similar patch
152 * written by Daniel Walker.
153 */
154 # ifndef likely
155 # define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
156 # endif
157 # ifndef unlikely
158 # define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
159 # endif
160
161 #ifdef CONFIG_PROFILE_ALL_BRANCHES
162 /*
163 * "Define 'is'", Bill Clinton
164 * "Define 'if'", Steven Rostedt
165 */
166 #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
167 #define __trace_if(cond) \
168 if (__builtin_constant_p(!!(cond)) ? !!(cond) : \
169 ({ \
170 int ______r; \
171 static struct ftrace_branch_data \
172 __attribute__((__aligned__(4))) \
173 __attribute__((section("_ftrace_branch"))) \
174 ______f = { \
175 .func = __func__, \
176 .file = __FILE__, \
177 .line = __LINE__, \
178 }; \
179 ______r = !!(cond); \
180 ______f.miss_hit[______r]++; \
181 ______r; \
182 }))
183 #endif /* CONFIG_PROFILE_ALL_BRANCHES */
184
185 #else
186 # define likely(x) __builtin_expect(!!(x), 1)
187 # define unlikely(x) __builtin_expect(!!(x), 0)
188 #endif
189
190 /* Optimization barrier */
191 #ifndef barrier
192 # define barrier() __memory_barrier()
193 #endif
194
195 #ifndef barrier_data
196 # define barrier_data(ptr) barrier()
197 #endif
198
199 /* workaround for GCC PR82365 if needed */
200 #ifndef barrier_before_unreachable
201 # define barrier_before_unreachable() do { } while (0)
202 #endif
203
204 /* Unreachable code */
205 #ifndef unreachable
206 # define unreachable() do { } while (1)
207 #endif
208
209 #ifndef RELOC_HIDE
210 # define RELOC_HIDE(ptr, off) \
211 ({ unsigned long __ptr; \
212 __ptr = (unsigned long) (ptr); \
213 (typeof(ptr)) (__ptr + (off)); })
214 #endif
215
216 #ifndef OPTIMIZER_HIDE_VAR
217 #define OPTIMIZER_HIDE_VAR(var) barrier()
218 #endif
219
220 /* Not-quite-unique ID. */
221 #ifndef __UNIQUE_ID
222 # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
223 #endif
224
225 #include <uapi/linux/types.h>
226
227 #define __READ_ONCE_SIZE \
228 ({ \
229 switch (size) { \
230 case 1: *(__u8 *)res = *(volatile __u8 *)p; break; \
231 case 2: *(__u16 *)res = *(volatile __u16 *)p; break; \
232 case 4: *(__u32 *)res = *(volatile __u32 *)p; break; \
233 case 8: *(__u64 *)res = *(volatile __u64 *)p; break; \
234 default: \
235 barrier(); \
236 __builtin_memcpy((void *)res, (const void *)p, size); \
237 barrier(); \
238 } \
239 })
240
241 static __always_inline
__read_once_size(const volatile void * p,void * res,int size)242 void __read_once_size(const volatile void *p, void *res, int size)
243 {
244 __READ_ONCE_SIZE;
245 }
246
247 #ifdef CONFIG_KASAN
248 /*
249 * This function is not 'inline' because __no_sanitize_address confilcts
250 * with inlining. Attempt to inline it may cause a build failure.
251 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
252 * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
253 */
254 static __no_sanitize_address __maybe_unused
__read_once_size_nocheck(const volatile void * p,void * res,int size)255 void __read_once_size_nocheck(const volatile void *p, void *res, int size)
256 {
257 __READ_ONCE_SIZE;
258 }
259 #else
260 static __always_inline
__read_once_size_nocheck(const volatile void * p,void * res,int size)261 void __read_once_size_nocheck(const volatile void *p, void *res, int size)
262 {
263 __READ_ONCE_SIZE;
264 }
265 #endif
266
__write_once_size(volatile void * p,void * res,int size)267 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
268 {
269 switch (size) {
270 case 1:
271 *(volatile __u8 *)p = *(__u8 *)res;
272 break;
273 case 2:
274 *(volatile __u16 *)p = *(__u16 *)res;
275 break;
276 case 4:
277 *(volatile __u32 *)p = *(__u32 *)res;
278 break;
279 case 8:
280 *(volatile __u64 *)p = *(__u64 *)res;
281 break;
282 default:
283 barrier();
284 __builtin_memcpy((void *)p, (const void *)res, size);
285 barrier();
286 }
287 }
288
289 /*
290 * Prevent the compiler from merging or refetching reads or writes. The
291 * compiler is also forbidden from reordering successive instances of
292 * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
293 * compiler is aware of some particular ordering. One way to make the
294 * compiler aware of ordering is to put the two invocations of READ_ONCE,
295 * WRITE_ONCE or ACCESS_ONCE() in different C statements.
296 *
297 * In contrast to ACCESS_ONCE these two macros will also work on aggregate
298 * data types like structs or unions. If the size of the accessed data
299 * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
300 * READ_ONCE() and WRITE_ONCE() will fall back to memcpy and print a
301 * compile-time warning.
302 *
303 * Their two major use cases are: (1) Mediating communication between
304 * process-level code and irq/NMI handlers, all running on the same CPU,
305 * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
306 * mutilate accesses that either do not require ordering or that interact
307 * with an explicit memory barrier or atomic instruction that provides the
308 * required ordering.
309 */
310
311 #define __READ_ONCE(x, check) \
312 ({ \
313 union { typeof(x) __val; char __c[1]; } __u; \
314 if (check) \
315 __read_once_size(&(x), __u.__c, sizeof(x)); \
316 else \
317 __read_once_size_nocheck(&(x), __u.__c, sizeof(x)); \
318 __u.__val; \
319 })
320 #define READ_ONCE(x) __READ_ONCE(x, 1)
321
322 /*
323 * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
324 * to hide memory access from KASAN.
325 */
326 #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
327
328 #define WRITE_ONCE(x, val) \
329 ({ \
330 union { typeof(x) __val; char __c[1]; } __u = \
331 { .__val = (__force typeof(x)) (val) }; \
332 __write_once_size(&(x), __u.__c, sizeof(x)); \
333 __u.__val; \
334 })
335
336 #endif /* __KERNEL__ */
337
338 #endif /* __ASSEMBLY__ */
339
340 #ifdef __KERNEL__
341 /*
342 * Allow us to mark functions as 'deprecated' and have gcc emit a nice
343 * warning for each use, in hopes of speeding the functions removal.
344 * Usage is:
345 * int __deprecated foo(void)
346 */
347 #ifndef __deprecated
348 # define __deprecated /* unimplemented */
349 #endif
350
351 #ifdef MODULE
352 #define __deprecated_for_modules __deprecated
353 #else
354 #define __deprecated_for_modules
355 #endif
356
357 #ifndef __must_check
358 #define __must_check
359 #endif
360
361 #ifndef CONFIG_ENABLE_MUST_CHECK
362 #undef __must_check
363 #define __must_check
364 #endif
365 #ifndef CONFIG_ENABLE_WARN_DEPRECATED
366 #undef __deprecated
367 #undef __deprecated_for_modules
368 #define __deprecated
369 #define __deprecated_for_modules
370 #endif
371
372 /*
373 * Allow us to avoid 'defined but not used' warnings on functions and data,
374 * as well as force them to be emitted to the assembly file.
375 *
376 * As of gcc 3.4, static functions that are not marked with attribute((used))
377 * may be elided from the assembly file. As of gcc 3.4, static data not so
378 * marked will not be elided, but this may change in a future gcc version.
379 *
380 * NOTE: Because distributions shipped with a backported unit-at-a-time
381 * compiler in gcc 3.3, we must define __used to be __attribute__((used))
382 * for gcc >=3.3 instead of 3.4.
383 *
384 * In prior versions of gcc, such functions and data would be emitted, but
385 * would be warned about except with attribute((unused)).
386 *
387 * Mark functions that are referenced only in inline assembly as __used so
388 * the code is emitted even though it appears to be unreferenced.
389 */
390 #ifndef __used
391 # define __used /* unimplemented */
392 #endif
393
394 #ifndef __maybe_unused
395 # define __maybe_unused /* unimplemented */
396 #endif
397
398 #ifndef __always_unused
399 # define __always_unused /* unimplemented */
400 #endif
401
402 #ifndef noinline
403 #define noinline
404 #endif
405
406 /*
407 * Rather then using noinline to prevent stack consumption, use
408 * noinline_for_stack instead. For documentation reasons.
409 */
410 #define noinline_for_stack noinline
411
412 #ifndef __always_inline
413 #define __always_inline inline
414 #endif
415
416 #endif /* __KERNEL__ */
417
418 /*
419 * From the GCC manual:
420 *
421 * Many functions do not examine any values except their arguments,
422 * and have no effects except the return value. Basically this is
423 * just slightly more strict class than the `pure' attribute above,
424 * since function is not allowed to read global memory.
425 *
426 * Note that a function that has pointer arguments and examines the
427 * data pointed to must _not_ be declared `const'. Likewise, a
428 * function that calls a non-`const' function usually must not be
429 * `const'. It does not make sense for a `const' function to return
430 * `void'.
431 */
432 #ifndef __attribute_const__
433 # define __attribute_const__ /* unimplemented */
434 #endif
435
436 /*
437 * Tell gcc if a function is cold. The compiler will assume any path
438 * directly leading to the call is unlikely.
439 */
440
441 #ifndef __cold
442 #define __cold
443 #endif
444
445 /* Simple shorthand for a section definition */
446 #ifndef __section
447 # define __section(S) __attribute__ ((__section__(#S)))
448 #endif
449
450 #ifndef __visible
451 #define __visible
452 #endif
453
454 /*
455 * Assume alignment of return value.
456 */
457 #ifndef __assume_aligned
458 #define __assume_aligned(a, ...)
459 #endif
460
461
462 /* Are two types/vars the same type (ignoring qualifiers)? */
463 #ifndef __same_type
464 # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
465 #endif
466
467 /* Is this type a native word size -- useful for atomic operations */
468 #ifndef __native_word
469 # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
470 #endif
471
472 /* Compile time object size, -1 for unknown */
473 #ifndef __compiletime_object_size
474 # define __compiletime_object_size(obj) -1
475 #endif
476 #ifndef __compiletime_warning
477 # define __compiletime_warning(message)
478 #endif
479 #ifndef __compiletime_error
480 # define __compiletime_error(message)
481 /*
482 * Sparse complains of variable sized arrays due to the temporary variable in
483 * __compiletime_assert. Unfortunately we can't just expand it out to make
484 * sparse see a constant array size without breaking compiletime_assert on old
485 * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
486 */
487 # ifndef __CHECKER__
488 # define __compiletime_error_fallback(condition) \
489 do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
490 # endif
491 #endif
492 #ifndef __compiletime_error_fallback
493 # define __compiletime_error_fallback(condition) do { } while (0)
494 #endif
495
496 #define __compiletime_assert(condition, msg, prefix, suffix) \
497 do { \
498 bool __cond = !(condition); \
499 extern void prefix ## suffix(void) __compiletime_error(msg); \
500 if (__cond) \
501 prefix ## suffix(); \
502 __compiletime_error_fallback(__cond); \
503 } while (0)
504
505 #define _compiletime_assert(condition, msg, prefix, suffix) \
506 __compiletime_assert(condition, msg, prefix, suffix)
507
508 /**
509 * compiletime_assert - break build and emit msg if condition is false
510 * @condition: a compile-time constant condition to check
511 * @msg: a message to emit if condition is false
512 *
513 * In tradition of POSIX assert, this macro will break the build if the
514 * supplied condition is *false*, emitting the supplied error message if the
515 * compiler has support to do so.
516 */
517 #define compiletime_assert(condition, msg) \
518 _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
519
520 #define compiletime_assert_atomic_type(t) \
521 compiletime_assert(__native_word(t), \
522 "Need native word sized stores/loads for atomicity.")
523
524 /*
525 * Prevent the compiler from merging or refetching accesses. The compiler
526 * is also forbidden from reordering successive instances of ACCESS_ONCE(),
527 * but only when the compiler is aware of some particular ordering. One way
528 * to make the compiler aware of ordering is to put the two invocations of
529 * ACCESS_ONCE() in different C statements.
530 *
531 * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
532 * on a union member will work as long as the size of the member matches the
533 * size of the union and the size is smaller than word size.
534 *
535 * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
536 * between process-level code and irq/NMI handlers, all running on the same CPU,
537 * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
538 * mutilate accesses that either do not require ordering or that interact
539 * with an explicit memory barrier or atomic instruction that provides the
540 * required ordering.
541 *
542 * If possible use READ_ONCE()/WRITE_ONCE() instead.
543 */
544 #define __ACCESS_ONCE(x) ({ \
545 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
546 (volatile typeof(x) *)&(x); })
547 #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
548
549 /**
550 * lockless_dereference() - safely load a pointer for later dereference
551 * @p: The pointer to load
552 *
553 * Similar to rcu_dereference(), but for situations where the pointed-to
554 * object's lifetime is managed by something other than RCU. That
555 * "something other" might be reference counting or simple immortality.
556 */
557 #define lockless_dereference(p) \
558 ({ \
559 typeof(p) _________p1 = READ_ONCE(p); \
560 smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
561 (_________p1); \
562 })
563
564 /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
565 #ifdef CONFIG_KPROBES
566 # define __kprobes __attribute__((__section__(".kprobes.text")))
567 # define nokprobe_inline __always_inline
568 #else
569 # define __kprobes
570 # define nokprobe_inline inline
571 #endif
572
573 #endif /* __clang__ */
574 #endif /* __LINUX_COMPILER_H */
575