xref: /openbmc/u-boot/include/linux/compiler.h (revision 002e9108)
195ffaba3SGraeme Russ #ifndef __LINUX_COMPILER_H
295ffaba3SGraeme Russ #define __LINUX_COMPILER_H
395ffaba3SGraeme Russ 
495ffaba3SGraeme Russ #ifndef __ASSEMBLY__
595ffaba3SGraeme Russ 
695ffaba3SGraeme Russ #ifdef __CHECKER__
795ffaba3SGraeme Russ # define __user		__attribute__((noderef, address_space(1)))
8fb8ffd7cSMasahiro Yamada # define __kernel	__attribute__((address_space(0)))
995ffaba3SGraeme Russ # define __safe		__attribute__((safe))
1095ffaba3SGraeme Russ # define __force	__attribute__((force))
1195ffaba3SGraeme Russ # define __nocast	__attribute__((nocast))
1295ffaba3SGraeme Russ # define __iomem	__attribute__((noderef, address_space(2)))
13fb8ffd7cSMasahiro Yamada # define __must_hold(x)	__attribute__((context(x,1,1)))
1495ffaba3SGraeme Russ # define __acquires(x)	__attribute__((context(x,0,1)))
1595ffaba3SGraeme Russ # define __releases(x)	__attribute__((context(x,1,0)))
1695ffaba3SGraeme Russ # define __acquire(x)	__context__(x,1)
1795ffaba3SGraeme Russ # define __release(x)	__context__(x,-1)
1895ffaba3SGraeme Russ # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
19fb8ffd7cSMasahiro Yamada # define __percpu	__attribute__((noderef, address_space(3)))
209b2c282bSTom Rini # define __pmem		__attribute__((noderef, address_space(5)))
21fb8ffd7cSMasahiro Yamada #ifdef CONFIG_SPARSE_RCU_POINTER
22fb8ffd7cSMasahiro Yamada # define __rcu		__attribute__((noderef, address_space(4)))
23fb8ffd7cSMasahiro Yamada #else
24fb8ffd7cSMasahiro Yamada # define __rcu
25fb8ffd7cSMasahiro Yamada #endif
2695ffaba3SGraeme Russ extern void __chk_user_ptr(const volatile void __user *);
2795ffaba3SGraeme Russ extern void __chk_io_ptr(const volatile void __iomem *);
2895ffaba3SGraeme Russ #else
2995ffaba3SGraeme Russ # define __user
3095ffaba3SGraeme Russ # define __kernel
3195ffaba3SGraeme Russ # define __safe
3295ffaba3SGraeme Russ # define __force
3395ffaba3SGraeme Russ # define __nocast
3495ffaba3SGraeme Russ # define __iomem
3595ffaba3SGraeme Russ # define __chk_user_ptr(x) (void)0
3695ffaba3SGraeme Russ # define __chk_io_ptr(x) (void)0
3795ffaba3SGraeme Russ # define __builtin_warning(x, y...) (1)
38fb8ffd7cSMasahiro Yamada # define __must_hold(x)
3995ffaba3SGraeme Russ # define __acquires(x)
4095ffaba3SGraeme Russ # define __releases(x)
4195ffaba3SGraeme Russ # define __acquire(x) (void)0
4295ffaba3SGraeme Russ # define __release(x) (void)0
4395ffaba3SGraeme Russ # define __cond_lock(x,c) (c)
44fb8ffd7cSMasahiro Yamada # define __percpu
45fb8ffd7cSMasahiro Yamada # define __rcu
469b2c282bSTom Rini # define __pmem
4795ffaba3SGraeme Russ #endif
4895ffaba3SGraeme Russ 
49fb8ffd7cSMasahiro Yamada /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
50fb8ffd7cSMasahiro Yamada #define ___PASTE(a,b) a##b
51fb8ffd7cSMasahiro Yamada #define __PASTE(a,b) ___PASTE(a,b)
52fb8ffd7cSMasahiro Yamada 
5395ffaba3SGraeme Russ #ifdef __KERNEL__
5495ffaba3SGraeme Russ 
5595ffaba3SGraeme Russ #ifdef __GNUC__
5695ffaba3SGraeme Russ #include <linux/compiler-gcc.h>
5795ffaba3SGraeme Russ #endif
5895ffaba3SGraeme Russ 
599b2c282bSTom Rini #if defined(CC_USING_HOTPATCH) && !defined(__CHECKER__)
609b2c282bSTom Rini #define notrace __attribute__((hotpatch(0,0)))
619b2c282bSTom Rini #else
6295ffaba3SGraeme Russ #define notrace __attribute__((no_instrument_function))
639b2c282bSTom Rini #endif
6495ffaba3SGraeme Russ 
6595ffaba3SGraeme Russ /* Intel compiler defines __GNUC__. So we will overwrite implementations
6695ffaba3SGraeme Russ  * coming from above header files here
6795ffaba3SGraeme Russ  */
6895ffaba3SGraeme Russ #ifdef __INTEL_COMPILER
6995ffaba3SGraeme Russ # include <linux/compiler-intel.h>
7095ffaba3SGraeme Russ #endif
7195ffaba3SGraeme Russ 
72fb8ffd7cSMasahiro Yamada /* Clang compiler defines __GNUC__. So we will overwrite implementations
73fb8ffd7cSMasahiro Yamada  * coming from above header files here
74fb8ffd7cSMasahiro Yamada  */
75fb8ffd7cSMasahiro Yamada #ifdef __clang__
76fb8ffd7cSMasahiro Yamada #include <linux/compiler-clang.h>
77fb8ffd7cSMasahiro Yamada #endif
78fb8ffd7cSMasahiro Yamada 
7995ffaba3SGraeme Russ /*
8095ffaba3SGraeme Russ  * Generic compiler-dependent macros required for kernel
8195ffaba3SGraeme Russ  * build go below this comment. Actual compiler/compiler version
8295ffaba3SGraeme Russ  * specific implementations come from the above header files
8395ffaba3SGraeme Russ  */
8495ffaba3SGraeme Russ 
8595ffaba3SGraeme Russ struct ftrace_branch_data {
8695ffaba3SGraeme Russ 	const char *func;
8795ffaba3SGraeme Russ 	const char *file;
8895ffaba3SGraeme Russ 	unsigned line;
8995ffaba3SGraeme Russ 	union {
9095ffaba3SGraeme Russ 		struct {
9195ffaba3SGraeme Russ 			unsigned long correct;
9295ffaba3SGraeme Russ 			unsigned long incorrect;
9395ffaba3SGraeme Russ 		};
9495ffaba3SGraeme Russ 		struct {
9595ffaba3SGraeme Russ 			unsigned long miss;
9695ffaba3SGraeme Russ 			unsigned long hit;
9795ffaba3SGraeme Russ 		};
9895ffaba3SGraeme Russ 		unsigned long miss_hit[2];
9995ffaba3SGraeme Russ 	};
10095ffaba3SGraeme Russ };
10195ffaba3SGraeme Russ 
10295ffaba3SGraeme Russ /*
10395ffaba3SGraeme Russ  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
10495ffaba3SGraeme Russ  * to disable branch tracing on a per file basis.
10595ffaba3SGraeme Russ  */
10695ffaba3SGraeme Russ #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
10795ffaba3SGraeme Russ     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
10895ffaba3SGraeme Russ void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
10995ffaba3SGraeme Russ 
11095ffaba3SGraeme Russ #define likely_notrace(x)	__builtin_expect(!!(x), 1)
11195ffaba3SGraeme Russ #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
11295ffaba3SGraeme Russ 
11395ffaba3SGraeme Russ #define __branch_check__(x, expect) ({					\
11495ffaba3SGraeme Russ 			int ______r;					\
11595ffaba3SGraeme Russ 			static struct ftrace_branch_data		\
11695ffaba3SGraeme Russ 				__attribute__((__aligned__(4)))		\
11795ffaba3SGraeme Russ 				__attribute__((section("_ftrace_annotated_branch"))) \
11895ffaba3SGraeme Russ 				______f = {				\
11995ffaba3SGraeme Russ 				.func = __func__,			\
12095ffaba3SGraeme Russ 				.file = __FILE__,			\
12195ffaba3SGraeme Russ 				.line = __LINE__,			\
12295ffaba3SGraeme Russ 			};						\
12395ffaba3SGraeme Russ 			______r = likely_notrace(x);			\
12495ffaba3SGraeme Russ 			ftrace_likely_update(&______f, ______r, expect); \
12595ffaba3SGraeme Russ 			______r;					\
12695ffaba3SGraeme Russ 		})
12795ffaba3SGraeme Russ 
12895ffaba3SGraeme Russ /*
12995ffaba3SGraeme Russ  * Using __builtin_constant_p(x) to ignore cases where the return
13095ffaba3SGraeme Russ  * value is always the same.  This idea is taken from a similar patch
13195ffaba3SGraeme Russ  * written by Daniel Walker.
13295ffaba3SGraeme Russ  */
13395ffaba3SGraeme Russ # ifndef likely
13495ffaba3SGraeme Russ #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
13595ffaba3SGraeme Russ # endif
13695ffaba3SGraeme Russ # ifndef unlikely
13795ffaba3SGraeme Russ #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
13895ffaba3SGraeme Russ # endif
13995ffaba3SGraeme Russ 
14095ffaba3SGraeme Russ #ifdef CONFIG_PROFILE_ALL_BRANCHES
14195ffaba3SGraeme Russ /*
14295ffaba3SGraeme Russ  * "Define 'is'", Bill Clinton
14395ffaba3SGraeme Russ  * "Define 'if'", Steven Rostedt
14495ffaba3SGraeme Russ  */
14595ffaba3SGraeme Russ #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
14695ffaba3SGraeme Russ #define __trace_if(cond) \
1479b2c282bSTom Rini 	if (__builtin_constant_p(!!(cond)) ? !!(cond) :			\
14895ffaba3SGraeme Russ 	({								\
14995ffaba3SGraeme Russ 		int ______r;						\
15095ffaba3SGraeme Russ 		static struct ftrace_branch_data			\
15195ffaba3SGraeme Russ 			__attribute__((__aligned__(4)))			\
15295ffaba3SGraeme Russ 			__attribute__((section("_ftrace_branch")))	\
15395ffaba3SGraeme Russ 			______f = {					\
15495ffaba3SGraeme Russ 				.func = __func__,			\
15595ffaba3SGraeme Russ 				.file = __FILE__,			\
15695ffaba3SGraeme Russ 				.line = __LINE__,			\
15795ffaba3SGraeme Russ 			};						\
15895ffaba3SGraeme Russ 		______r = !!(cond);					\
15995ffaba3SGraeme Russ 		______f.miss_hit[______r]++;					\
16095ffaba3SGraeme Russ 		______r;						\
16195ffaba3SGraeme Russ 	}))
16295ffaba3SGraeme Russ #endif /* CONFIG_PROFILE_ALL_BRANCHES */
16395ffaba3SGraeme Russ 
16495ffaba3SGraeme Russ #else
16595ffaba3SGraeme Russ # define likely(x)	__builtin_expect(!!(x), 1)
16695ffaba3SGraeme Russ # define unlikely(x)	__builtin_expect(!!(x), 0)
16795ffaba3SGraeme Russ #endif
16895ffaba3SGraeme Russ 
16995ffaba3SGraeme Russ /* Optimization barrier */
17095ffaba3SGraeme Russ #ifndef barrier
17195ffaba3SGraeme Russ # define barrier() __memory_barrier()
17295ffaba3SGraeme Russ #endif
17395ffaba3SGraeme Russ 
1749b2c282bSTom Rini #ifndef barrier_data
1759b2c282bSTom Rini # define barrier_data(ptr) barrier()
1769b2c282bSTom Rini #endif
1779b2c282bSTom Rini 
17895ffaba3SGraeme Russ /* Unreachable code */
17995ffaba3SGraeme Russ #ifndef unreachable
18095ffaba3SGraeme Russ # define unreachable() do { } while (1)
18195ffaba3SGraeme Russ #endif
18295ffaba3SGraeme Russ 
18395ffaba3SGraeme Russ #ifndef RELOC_HIDE
18495ffaba3SGraeme Russ # define RELOC_HIDE(ptr, off)					\
18595ffaba3SGraeme Russ   ({ unsigned long __ptr;					\
18695ffaba3SGraeme Russ      __ptr = (unsigned long) (ptr);				\
18795ffaba3SGraeme Russ     (typeof(ptr)) (__ptr + (off)); })
18895ffaba3SGraeme Russ #endif
18995ffaba3SGraeme Russ 
190fb8ffd7cSMasahiro Yamada #ifndef OPTIMIZER_HIDE_VAR
191fb8ffd7cSMasahiro Yamada #define OPTIMIZER_HIDE_VAR(var) barrier()
192fb8ffd7cSMasahiro Yamada #endif
193fb8ffd7cSMasahiro Yamada 
194fb8ffd7cSMasahiro Yamada /* Not-quite-unique ID. */
195fb8ffd7cSMasahiro Yamada #ifndef __UNIQUE_ID
196fb8ffd7cSMasahiro Yamada # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
197fb8ffd7cSMasahiro Yamada #endif
198fb8ffd7cSMasahiro Yamada 
1999b2c282bSTom Rini #include <linux/types.h>
2009b2c282bSTom Rini 
2019b2c282bSTom Rini #define __READ_ONCE_SIZE						\
2029b2c282bSTom Rini ({									\
2039b2c282bSTom Rini 	switch (size) {							\
2049b2c282bSTom Rini 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;		\
2059b2c282bSTom Rini 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;		\
2069b2c282bSTom Rini 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;		\
2079b2c282bSTom Rini 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;		\
2089b2c282bSTom Rini 	default:							\
2099b2c282bSTom Rini 		barrier();						\
2109b2c282bSTom Rini 		__builtin_memcpy((void *)res, (const void *)p, size);	\
2119b2c282bSTom Rini 		barrier();						\
2129b2c282bSTom Rini 	}								\
2139b2c282bSTom Rini })
2149b2c282bSTom Rini 
2159b2c282bSTom Rini static __always_inline
__read_once_size(const volatile void * p,void * res,int size)2169b2c282bSTom Rini void __read_once_size(const volatile void *p, void *res, int size)
2179b2c282bSTom Rini {
2189b2c282bSTom Rini 	__READ_ONCE_SIZE;
2199b2c282bSTom Rini }
2209b2c282bSTom Rini 
2219b2c282bSTom Rini #ifdef CONFIG_KASAN
2229b2c282bSTom Rini /*
2239b2c282bSTom Rini  * This function is not 'inline' because __no_sanitize_address confilcts
2249b2c282bSTom Rini  * with inlining. Attempt to inline it may cause a build failure.
2259b2c282bSTom Rini  * 	https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
2269b2c282bSTom Rini  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
2279b2c282bSTom Rini  */
2289b2c282bSTom Rini static __no_sanitize_address __maybe_unused
__read_once_size_nocheck(const volatile void * p,void * res,int size)2299b2c282bSTom Rini void __read_once_size_nocheck(const volatile void *p, void *res, int size)
2309b2c282bSTom Rini {
2319b2c282bSTom Rini 	__READ_ONCE_SIZE;
2329b2c282bSTom Rini }
2339b2c282bSTom Rini #else
2349b2c282bSTom Rini static __always_inline
__read_once_size_nocheck(const volatile void * p,void * res,int size)2359b2c282bSTom Rini void __read_once_size_nocheck(const volatile void *p, void *res, int size)
2369b2c282bSTom Rini {
2379b2c282bSTom Rini 	__READ_ONCE_SIZE;
2389b2c282bSTom Rini }
2399b2c282bSTom Rini #endif
2409b2c282bSTom Rini 
__write_once_size(volatile void * p,void * res,int size)2419b2c282bSTom Rini static __always_inline void __write_once_size(volatile void *p, void *res, int size)
2429b2c282bSTom Rini {
2439b2c282bSTom Rini 	switch (size) {
2449b2c282bSTom Rini 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
2459b2c282bSTom Rini 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
2469b2c282bSTom Rini 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
2479b2c282bSTom Rini 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
2489b2c282bSTom Rini 	default:
2499b2c282bSTom Rini 		barrier();
2509b2c282bSTom Rini 		__builtin_memcpy((void *)p, (const void *)res, size);
2519b2c282bSTom Rini 		barrier();
2529b2c282bSTom Rini 	}
2539b2c282bSTom Rini }
2549b2c282bSTom Rini 
2559b2c282bSTom Rini /*
2569b2c282bSTom Rini  * Prevent the compiler from merging or refetching reads or writes. The
2579b2c282bSTom Rini  * compiler is also forbidden from reordering successive instances of
2589b2c282bSTom Rini  * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
2599b2c282bSTom Rini  * compiler is aware of some particular ordering.  One way to make the
2609b2c282bSTom Rini  * compiler aware of ordering is to put the two invocations of READ_ONCE,
2619b2c282bSTom Rini  * WRITE_ONCE or ACCESS_ONCE() in different C statements.
2629b2c282bSTom Rini  *
2639b2c282bSTom Rini  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
2649b2c282bSTom Rini  * data types like structs or unions. If the size of the accessed data
2659b2c282bSTom Rini  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
2669b2c282bSTom Rini  * READ_ONCE() and WRITE_ONCE()  will fall back to memcpy and print a
2679b2c282bSTom Rini  * compile-time warning.
2689b2c282bSTom Rini  *
2699b2c282bSTom Rini  * Their two major use cases are: (1) Mediating communication between
2709b2c282bSTom Rini  * process-level code and irq/NMI handlers, all running on the same CPU,
2719b2c282bSTom Rini  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
2729b2c282bSTom Rini  * mutilate accesses that either do not require ordering or that interact
2739b2c282bSTom Rini  * with an explicit memory barrier or atomic instruction that provides the
2749b2c282bSTom Rini  * required ordering.
2759b2c282bSTom Rini  */
2769b2c282bSTom Rini 
2779b2c282bSTom Rini #define __READ_ONCE(x, check)						\
2789b2c282bSTom Rini ({									\
2799b2c282bSTom Rini 	union { typeof(x) __val; char __c[1]; } __u;			\
2809b2c282bSTom Rini 	if (check)							\
2819b2c282bSTom Rini 		__read_once_size(&(x), __u.__c, sizeof(x));		\
2829b2c282bSTom Rini 	else								\
2839b2c282bSTom Rini 		__read_once_size_nocheck(&(x), __u.__c, sizeof(x));	\
2849b2c282bSTom Rini 	__u.__val;							\
2859b2c282bSTom Rini })
2869b2c282bSTom Rini #define READ_ONCE(x) __READ_ONCE(x, 1)
2879b2c282bSTom Rini 
2889b2c282bSTom Rini /*
2899b2c282bSTom Rini  * Use READ_ONCE_NOCHECK() instead of READ_ONCE() if you need
2909b2c282bSTom Rini  * to hide memory access from KASAN.
2919b2c282bSTom Rini  */
2929b2c282bSTom Rini #define READ_ONCE_NOCHECK(x) __READ_ONCE(x, 0)
2939b2c282bSTom Rini 
2949b2c282bSTom Rini #define WRITE_ONCE(x, val) \
2959b2c282bSTom Rini ({							\
2969b2c282bSTom Rini 	union { typeof(x) __val; char __c[1]; } __u =	\
2979b2c282bSTom Rini 		{ .__val = (__force typeof(x)) (val) }; \
2989b2c282bSTom Rini 	__write_once_size(&(x), __u.__c, sizeof(x));	\
2999b2c282bSTom Rini 	__u.__val;					\
3009b2c282bSTom Rini })
3019b2c282bSTom Rini 
3029b2c282bSTom Rini /**
3039b2c282bSTom Rini  * smp_cond_acquire() - Spin wait for cond with ACQUIRE ordering
3049b2c282bSTom Rini  * @cond: boolean expression to wait for
3059b2c282bSTom Rini  *
3069b2c282bSTom Rini  * Equivalent to using smp_load_acquire() on the condition variable but employs
3079b2c282bSTom Rini  * the control dependency of the wait to reduce the barrier on many platforms.
3089b2c282bSTom Rini  *
3099b2c282bSTom Rini  * The control dependency provides a LOAD->STORE order, the additional RMB
3109b2c282bSTom Rini  * provides LOAD->LOAD order, together they provide LOAD->{LOAD,STORE} order,
3119b2c282bSTom Rini  * aka. ACQUIRE.
3129b2c282bSTom Rini  */
3139b2c282bSTom Rini #define smp_cond_acquire(cond)	do {		\
3149b2c282bSTom Rini 	while (!(cond))				\
3159b2c282bSTom Rini 		cpu_relax();			\
3169b2c282bSTom Rini 	smp_rmb(); /* ctrl + rmb := acquire */	\
3179b2c282bSTom Rini } while (0)
3189b2c282bSTom Rini 
31995ffaba3SGraeme Russ #endif /* __KERNEL__ */
32095ffaba3SGraeme Russ 
32195ffaba3SGraeme Russ #endif /* __ASSEMBLY__ */
32295ffaba3SGraeme Russ 
32395ffaba3SGraeme Russ #ifdef __KERNEL__
32495ffaba3SGraeme Russ /*
32595ffaba3SGraeme Russ  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
32695ffaba3SGraeme Russ  * warning for each use, in hopes of speeding the functions removal.
32795ffaba3SGraeme Russ  * Usage is:
32895ffaba3SGraeme Russ  * 		int __deprecated foo(void)
32995ffaba3SGraeme Russ  */
33095ffaba3SGraeme Russ #ifndef __deprecated
33195ffaba3SGraeme Russ # define __deprecated		/* unimplemented */
33295ffaba3SGraeme Russ #endif
33395ffaba3SGraeme Russ 
33495ffaba3SGraeme Russ #ifdef MODULE
33595ffaba3SGraeme Russ #define __deprecated_for_modules __deprecated
33695ffaba3SGraeme Russ #else
33795ffaba3SGraeme Russ #define __deprecated_for_modules
33895ffaba3SGraeme Russ #endif
33995ffaba3SGraeme Russ 
34095ffaba3SGraeme Russ #ifndef __must_check
34195ffaba3SGraeme Russ #define __must_check
34295ffaba3SGraeme Russ #endif
34395ffaba3SGraeme Russ 
34495ffaba3SGraeme Russ #ifndef CONFIG_ENABLE_MUST_CHECK
34595ffaba3SGraeme Russ #undef __must_check
34695ffaba3SGraeme Russ #define __must_check
34795ffaba3SGraeme Russ #endif
34895ffaba3SGraeme Russ #ifndef CONFIG_ENABLE_WARN_DEPRECATED
34995ffaba3SGraeme Russ #undef __deprecated
35095ffaba3SGraeme Russ #undef __deprecated_for_modules
35195ffaba3SGraeme Russ #define __deprecated
35295ffaba3SGraeme Russ #define __deprecated_for_modules
35395ffaba3SGraeme Russ #endif
35495ffaba3SGraeme Russ 
35595ffaba3SGraeme Russ /*
35695ffaba3SGraeme Russ  * Allow us to avoid 'defined but not used' warnings on functions and data,
35795ffaba3SGraeme Russ  * as well as force them to be emitted to the assembly file.
35895ffaba3SGraeme Russ  *
35995ffaba3SGraeme Russ  * As of gcc 3.4, static functions that are not marked with attribute((used))
36095ffaba3SGraeme Russ  * may be elided from the assembly file.  As of gcc 3.4, static data not so
36195ffaba3SGraeme Russ  * marked will not be elided, but this may change in a future gcc version.
36295ffaba3SGraeme Russ  *
36395ffaba3SGraeme Russ  * NOTE: Because distributions shipped with a backported unit-at-a-time
36495ffaba3SGraeme Russ  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
36595ffaba3SGraeme Russ  * for gcc >=3.3 instead of 3.4.
36695ffaba3SGraeme Russ  *
36795ffaba3SGraeme Russ  * In prior versions of gcc, such functions and data would be emitted, but
36895ffaba3SGraeme Russ  * would be warned about except with attribute((unused)).
36995ffaba3SGraeme Russ  *
37095ffaba3SGraeme Russ  * Mark functions that are referenced only in inline assembly as __used so
37195ffaba3SGraeme Russ  * the code is emitted even though it appears to be unreferenced.
37295ffaba3SGraeme Russ  */
37395ffaba3SGraeme Russ #ifndef __used
37495ffaba3SGraeme Russ # define __used			/* unimplemented */
37595ffaba3SGraeme Russ #endif
37695ffaba3SGraeme Russ 
37795ffaba3SGraeme Russ #ifndef __maybe_unused
37895ffaba3SGraeme Russ # define __maybe_unused		/* unimplemented */
37995ffaba3SGraeme Russ #endif
38095ffaba3SGraeme Russ 
38195ffaba3SGraeme Russ #ifndef __always_unused
38295ffaba3SGraeme Russ # define __always_unused	/* unimplemented */
38395ffaba3SGraeme Russ #endif
38495ffaba3SGraeme Russ 
38595ffaba3SGraeme Russ #ifndef noinline
38695ffaba3SGraeme Russ #define noinline
38795ffaba3SGraeme Russ #endif
38895ffaba3SGraeme Russ 
38995ffaba3SGraeme Russ /*
39095ffaba3SGraeme Russ  * Rather then using noinline to prevent stack consumption, use
391fb8ffd7cSMasahiro Yamada  * noinline_for_stack instead.  For documentation reasons.
39295ffaba3SGraeme Russ  */
39395ffaba3SGraeme Russ #define noinline_for_stack noinline
39495ffaba3SGraeme Russ 
39595ffaba3SGraeme Russ #ifndef __always_inline
39695ffaba3SGraeme Russ #define __always_inline inline
39795ffaba3SGraeme Russ #endif
39895ffaba3SGraeme Russ 
39995ffaba3SGraeme Russ #endif /* __KERNEL__ */
40095ffaba3SGraeme Russ 
40195ffaba3SGraeme Russ /*
40295ffaba3SGraeme Russ  * From the GCC manual:
40395ffaba3SGraeme Russ  *
40495ffaba3SGraeme Russ  * Many functions do not examine any values except their arguments,
40595ffaba3SGraeme Russ  * and have no effects except the return value.  Basically this is
40695ffaba3SGraeme Russ  * just slightly more strict class than the `pure' attribute above,
40795ffaba3SGraeme Russ  * since function is not allowed to read global memory.
40895ffaba3SGraeme Russ  *
40995ffaba3SGraeme Russ  * Note that a function that has pointer arguments and examines the
41095ffaba3SGraeme Russ  * data pointed to must _not_ be declared `const'.  Likewise, a
41195ffaba3SGraeme Russ  * function that calls a non-`const' function usually must not be
41295ffaba3SGraeme Russ  * `const'.  It does not make sense for a `const' function to return
41395ffaba3SGraeme Russ  * `void'.
41495ffaba3SGraeme Russ  */
41595ffaba3SGraeme Russ #ifndef __attribute_const__
41695ffaba3SGraeme Russ # define __attribute_const__	/* unimplemented */
41795ffaba3SGraeme Russ #endif
41895ffaba3SGraeme Russ 
41995ffaba3SGraeme Russ /*
42095ffaba3SGraeme Russ  * Tell gcc if a function is cold. The compiler will assume any path
42195ffaba3SGraeme Russ  * directly leading to the call is unlikely.
42295ffaba3SGraeme Russ  */
42395ffaba3SGraeme Russ 
42495ffaba3SGraeme Russ #ifndef __cold
42595ffaba3SGraeme Russ #define __cold
42695ffaba3SGraeme Russ #endif
42795ffaba3SGraeme Russ 
42895ffaba3SGraeme Russ /* Simple shorthand for a section definition */
42995ffaba3SGraeme Russ #ifndef __section
43095ffaba3SGraeme Russ # define __section(S) __attribute__ ((__section__(#S)))
43195ffaba3SGraeme Russ #endif
43295ffaba3SGraeme Russ 
433fb8ffd7cSMasahiro Yamada #ifndef __visible
434fb8ffd7cSMasahiro Yamada #define __visible
435fb8ffd7cSMasahiro Yamada #endif
436fb8ffd7cSMasahiro Yamada 
4379b2c282bSTom Rini /*
4389b2c282bSTom Rini  * Assume alignment of return value.
4399b2c282bSTom Rini  */
4409b2c282bSTom Rini #ifndef __assume_aligned
4419b2c282bSTom Rini #define __assume_aligned(a, ...)
4429b2c282bSTom Rini #endif
4439b2c282bSTom Rini 
4449b2c282bSTom Rini 
44595ffaba3SGraeme Russ /* Are two types/vars the same type (ignoring qualifiers)? */
44695ffaba3SGraeme Russ #ifndef __same_type
44795ffaba3SGraeme Russ # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
44895ffaba3SGraeme Russ #endif
44995ffaba3SGraeme Russ 
450fb8ffd7cSMasahiro Yamada /* Is this type a native word size -- useful for atomic operations */
451fb8ffd7cSMasahiro Yamada #ifndef __native_word
4529b2c282bSTom Rini # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
453fb8ffd7cSMasahiro Yamada #endif
454fb8ffd7cSMasahiro Yamada 
45595ffaba3SGraeme Russ /* Compile time object size, -1 for unknown */
45695ffaba3SGraeme Russ #ifndef __compiletime_object_size
45795ffaba3SGraeme Russ # define __compiletime_object_size(obj) -1
45895ffaba3SGraeme Russ #endif
45995ffaba3SGraeme Russ #ifndef __compiletime_warning
46095ffaba3SGraeme Russ # define __compiletime_warning(message)
46195ffaba3SGraeme Russ #endif
46295ffaba3SGraeme Russ #ifndef __compiletime_error
46395ffaba3SGraeme Russ # define __compiletime_error(message)
464fb8ffd7cSMasahiro Yamada /*
465fb8ffd7cSMasahiro Yamada  * Sparse complains of variable sized arrays due to the temporary variable in
466fb8ffd7cSMasahiro Yamada  * __compiletime_assert. Unfortunately we can't just expand it out to make
467fb8ffd7cSMasahiro Yamada  * sparse see a constant array size without breaking compiletime_assert on old
468fb8ffd7cSMasahiro Yamada  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
469fb8ffd7cSMasahiro Yamada  */
470fb8ffd7cSMasahiro Yamada # ifndef __CHECKER__
471fb8ffd7cSMasahiro Yamada #  define __compiletime_error_fallback(condition) \
472fb8ffd7cSMasahiro Yamada 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
47395ffaba3SGraeme Russ # endif
474fb8ffd7cSMasahiro Yamada #endif
475fb8ffd7cSMasahiro Yamada #ifndef __compiletime_error_fallback
476fb8ffd7cSMasahiro Yamada # define __compiletime_error_fallback(condition) do { } while (0)
477fb8ffd7cSMasahiro Yamada #endif
478fb8ffd7cSMasahiro Yamada 
479*059a4809SMasahiro Yamada #ifdef __OPTIMIZE__
480fb8ffd7cSMasahiro Yamada # define __compiletime_assert(condition, msg, prefix, suffix)		\
481fb8ffd7cSMasahiro Yamada 	do {								\
482fb8ffd7cSMasahiro Yamada 		bool __cond = !(condition);				\
483fb8ffd7cSMasahiro Yamada 		extern void prefix ## suffix(void) __compiletime_error(msg); \
484fb8ffd7cSMasahiro Yamada 		if (__cond)						\
485fb8ffd7cSMasahiro Yamada 			prefix ## suffix();				\
486fb8ffd7cSMasahiro Yamada 		__compiletime_error_fallback(__cond);			\
487fb8ffd7cSMasahiro Yamada 	} while (0)
488*059a4809SMasahiro Yamada #else
489*059a4809SMasahiro Yamada # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
490*059a4809SMasahiro Yamada #endif
491fb8ffd7cSMasahiro Yamada 
492fb8ffd7cSMasahiro Yamada #define _compiletime_assert(condition, msg, prefix, suffix) \
493fb8ffd7cSMasahiro Yamada 	__compiletime_assert(condition, msg, prefix, suffix)
494fb8ffd7cSMasahiro Yamada 
495fb8ffd7cSMasahiro Yamada /**
496fb8ffd7cSMasahiro Yamada  * compiletime_assert - break build and emit msg if condition is false
497fb8ffd7cSMasahiro Yamada  * @condition: a compile-time constant condition to check
498fb8ffd7cSMasahiro Yamada  * @msg:       a message to emit if condition is false
499fb8ffd7cSMasahiro Yamada  *
500fb8ffd7cSMasahiro Yamada  * In tradition of POSIX assert, this macro will break the build if the
501fb8ffd7cSMasahiro Yamada  * supplied condition is *false*, emitting the supplied error message if the
502fb8ffd7cSMasahiro Yamada  * compiler has support to do so.
503fb8ffd7cSMasahiro Yamada  */
504fb8ffd7cSMasahiro Yamada #define compiletime_assert(condition, msg) \
505fb8ffd7cSMasahiro Yamada 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
506fb8ffd7cSMasahiro Yamada 
507fb8ffd7cSMasahiro Yamada #define compiletime_assert_atomic_type(t)				\
508fb8ffd7cSMasahiro Yamada 	compiletime_assert(__native_word(t),				\
509fb8ffd7cSMasahiro Yamada 		"Need native word sized stores/loads for atomicity.")
51095ffaba3SGraeme Russ 
51195ffaba3SGraeme Russ /*
51295ffaba3SGraeme Russ  * Prevent the compiler from merging or refetching accesses.  The compiler
51395ffaba3SGraeme Russ  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
51495ffaba3SGraeme Russ  * but only when the compiler is aware of some particular ordering.  One way
51595ffaba3SGraeme Russ  * to make the compiler aware of ordering is to put the two invocations of
51695ffaba3SGraeme Russ  * ACCESS_ONCE() in different C statements.
51795ffaba3SGraeme Russ  *
5189b2c282bSTom Rini  * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
5199b2c282bSTom Rini  * on a union member will work as long as the size of the member matches the
5209b2c282bSTom Rini  * size of the union and the size is smaller than word size.
5219b2c282bSTom Rini  *
5229b2c282bSTom Rini  * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
5239b2c282bSTom Rini  * between process-level code and irq/NMI handlers, all running on the same CPU,
5249b2c282bSTom Rini  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
5259b2c282bSTom Rini  * mutilate accesses that either do not require ordering or that interact
5269b2c282bSTom Rini  * with an explicit memory barrier or atomic instruction that provides the
5279b2c282bSTom Rini  * required ordering.
5289b2c282bSTom Rini  *
5299b2c282bSTom Rini  * If possible use READ_ONCE()/WRITE_ONCE() instead.
53095ffaba3SGraeme Russ  */
5319b2c282bSTom Rini #define __ACCESS_ONCE(x) ({ \
5329b2c282bSTom Rini 	 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
5339b2c282bSTom Rini 	(volatile typeof(x) *)&(x); })
5349b2c282bSTom Rini #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
5359b2c282bSTom Rini 
5369b2c282bSTom Rini /**
5379b2c282bSTom Rini  * lockless_dereference() - safely load a pointer for later dereference
5389b2c282bSTom Rini  * @p: The pointer to load
5399b2c282bSTom Rini  *
5409b2c282bSTom Rini  * Similar to rcu_dereference(), but for situations where the pointed-to
5419b2c282bSTom Rini  * object's lifetime is managed by something other than RCU.  That
5429b2c282bSTom Rini  * "something other" might be reference counting or simple immortality.
5439b2c282bSTom Rini  */
5449b2c282bSTom Rini #define lockless_dereference(p) \
5459b2c282bSTom Rini ({ \
5469b2c282bSTom Rini 	typeof(p) _________p1 = READ_ONCE(p); \
5479b2c282bSTom Rini 	smp_read_barrier_depends(); /* Dependency order vs. p above. */ \
5489b2c282bSTom Rini 	(_________p1); \
5499b2c282bSTom Rini })
55095ffaba3SGraeme Russ 
551fb8ffd7cSMasahiro Yamada /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
552fb8ffd7cSMasahiro Yamada #ifdef CONFIG_KPROBES
553fb8ffd7cSMasahiro Yamada # define __kprobes	__attribute__((__section__(".kprobes.text")))
554fb8ffd7cSMasahiro Yamada # define nokprobe_inline	__always_inline
555fb8ffd7cSMasahiro Yamada #else
556fb8ffd7cSMasahiro Yamada # define __kprobes
557fb8ffd7cSMasahiro Yamada # define nokprobe_inline	inline
558fb8ffd7cSMasahiro Yamada #endif
55995ffaba3SGraeme Russ #endif /* __LINUX_COMPILER_H */
560