xref: /linux/include/linux/compiler.h (revision 7829fb09a2b4268b30dd9bc782fa5ebee278b137)
11da177e4SLinus Torvalds #ifndef __LINUX_COMPILER_H
21da177e4SLinus Torvalds #define __LINUX_COMPILER_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds #ifndef __ASSEMBLY__
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #ifdef __CHECKER__
71da177e4SLinus Torvalds # define __user		__attribute__((noderef, address_space(1)))
8e0fdb0e0SRusty Russell # define __kernel	__attribute__((address_space(0)))
91da177e4SLinus Torvalds # define __safe		__attribute__((safe))
101da177e4SLinus Torvalds # define __force	__attribute__((force))
111da177e4SLinus Torvalds # define __nocast	__attribute__((nocast))
121da177e4SLinus Torvalds # define __iomem	__attribute__((noderef, address_space(2)))
138529091eSJosh Triplett # define __must_hold(x)	__attribute__((context(x,1,1)))
14c902e0a0SJosh Triplett # define __acquires(x)	__attribute__((context(x,0,1)))
15c902e0a0SJosh Triplett # define __releases(x)	__attribute__((context(x,1,0)))
16c902e0a0SJosh Triplett # define __acquire(x)	__context__(x,1)
17c902e0a0SJosh Triplett # define __release(x)	__context__(x,-1)
18dcc8e559SJosh Triplett # define __cond_lock(x,c)	((c) ? ({ __acquire(x); 1; }) : 0)
19e0fdb0e0SRusty Russell # define __percpu	__attribute__((noderef, address_space(3)))
20ca5ecddfSPaul E. McKenney #ifdef CONFIG_SPARSE_RCU_POINTER
21ca5ecddfSPaul E. McKenney # define __rcu		__attribute__((noderef, address_space(4)))
22ca5ecddfSPaul E. McKenney #else
2371d1d5c7SPaul E. McKenney # define __rcu
24ca5ecddfSPaul E. McKenney #endif
25c47ffe3dSAl Viro extern void __chk_user_ptr(const volatile void __user *);
26c47ffe3dSAl Viro extern void __chk_io_ptr(const volatile void __iomem *);
271da177e4SLinus Torvalds #else
281da177e4SLinus Torvalds # define __user
291da177e4SLinus Torvalds # define __kernel
301da177e4SLinus Torvalds # define __safe
311da177e4SLinus Torvalds # define __force
321da177e4SLinus Torvalds # define __nocast
331da177e4SLinus Torvalds # define __iomem
341da177e4SLinus Torvalds # define __chk_user_ptr(x) (void)0
351da177e4SLinus Torvalds # define __chk_io_ptr(x) (void)0
361da177e4SLinus Torvalds # define __builtin_warning(x, y...) (1)
378529091eSJosh Triplett # define __must_hold(x)
381da177e4SLinus Torvalds # define __acquires(x)
391da177e4SLinus Torvalds # define __releases(x)
401da177e4SLinus Torvalds # define __acquire(x) (void)0
411da177e4SLinus Torvalds # define __release(x) (void)0
42dcc8e559SJosh Triplett # define __cond_lock(x,c) (c)
43e0fdb0e0SRusty Russell # define __percpu
4471d1d5c7SPaul E. McKenney # define __rcu
451da177e4SLinus Torvalds #endif
461da177e4SLinus Torvalds 
476f33d587SRusty Russell /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
486f33d587SRusty Russell #define ___PASTE(a,b) a##b
496f33d587SRusty Russell #define __PASTE(a,b) ___PASTE(a,b)
506f33d587SRusty Russell 
511da177e4SLinus Torvalds #ifdef __KERNEL__
521da177e4SLinus Torvalds 
53f153b821SLinus Torvalds #ifdef __GNUC__
54f153b821SLinus Torvalds #include <linux/compiler-gcc.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds 
5761f55214SHeiko Carstens #ifdef CC_USING_HOTPATCH
5861f55214SHeiko Carstens #define notrace __attribute__((hotpatch(0,0)))
5961f55214SHeiko Carstens #else
6028614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
6161f55214SHeiko Carstens #endif
6228614889SSteven Rostedt 
631da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
641da177e4SLinus Torvalds  * coming from above header files here
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
671da177e4SLinus Torvalds # include <linux/compiler-intel.h>
681da177e4SLinus Torvalds #endif
691da177e4SLinus Torvalds 
70565cbdc2SMark Charlebois /* Clang compiler defines __GNUC__. So we will overwrite implementations
71565cbdc2SMark Charlebois  * coming from above header files here
72565cbdc2SMark Charlebois  */
73565cbdc2SMark Charlebois #ifdef __clang__
74565cbdc2SMark Charlebois #include <linux/compiler-clang.h>
75565cbdc2SMark Charlebois #endif
76565cbdc2SMark Charlebois 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
791da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
801da177e4SLinus Torvalds  * specific implementations come from the above header files
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
832ed84eebSSteven Rostedt struct ftrace_branch_data {
841f0d69a9SSteven Rostedt 	const char *func;
851f0d69a9SSteven Rostedt 	const char *file;
861f0d69a9SSteven Rostedt 	unsigned line;
872bcd521aSSteven Rostedt 	union {
882bcd521aSSteven Rostedt 		struct {
891f0d69a9SSteven Rostedt 			unsigned long correct;
901f0d69a9SSteven Rostedt 			unsigned long incorrect;
911f0d69a9SSteven Rostedt 		};
922bcd521aSSteven Rostedt 		struct {
932bcd521aSSteven Rostedt 			unsigned long miss;
942bcd521aSSteven Rostedt 			unsigned long hit;
952bcd521aSSteven Rostedt 		};
9697e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
972bcd521aSSteven Rostedt 	};
982bcd521aSSteven Rostedt };
992ed84eebSSteven Rostedt 
1002ed84eebSSteven Rostedt /*
1012ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
1022ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
1032ed84eebSSteven Rostedt  */
104d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
105d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
1062ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
1071f0d69a9SSteven Rostedt 
1081f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
1091f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
1101f0d69a9SSteven Rostedt 
11145b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
1121f0d69a9SSteven Rostedt 			int ______r;					\
1132ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
1141f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
11545b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1161f0d69a9SSteven Rostedt 				______f = {				\
1171f0d69a9SSteven Rostedt 				.func = __func__,			\
1181f0d69a9SSteven Rostedt 				.file = __FILE__,			\
1191f0d69a9SSteven Rostedt 				.line = __LINE__,			\
1201f0d69a9SSteven Rostedt 			};						\
1211f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
12245b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
1231f0d69a9SSteven Rostedt 			______r;					\
1241f0d69a9SSteven Rostedt 		})
1251f0d69a9SSteven Rostedt 
1261f0d69a9SSteven Rostedt /*
1271f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1281f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1291f0d69a9SSteven Rostedt  * written by Daniel Walker.
1301f0d69a9SSteven Rostedt  */
1311f0d69a9SSteven Rostedt # ifndef likely
13245b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1331f0d69a9SSteven Rostedt # endif
1341f0d69a9SSteven Rostedt # ifndef unlikely
13545b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1361f0d69a9SSteven Rostedt # endif
1372bcd521aSSteven Rostedt 
1382bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1392bcd521aSSteven Rostedt /*
1402bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1412bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1422bcd521aSSteven Rostedt  */
143ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
144ab3c9c68SLinus Torvalds #define __trace_if(cond) \
145ab3c9c68SLinus Torvalds 	if (__builtin_constant_p((cond)) ? !!(cond) :			\
1462bcd521aSSteven Rostedt 	({								\
1472bcd521aSSteven Rostedt 		int ______r;						\
1482bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1492bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1502bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1512bcd521aSSteven Rostedt 			______f = {					\
1522bcd521aSSteven Rostedt 				.func = __func__,			\
1532bcd521aSSteven Rostedt 				.file = __FILE__,			\
1542bcd521aSSteven Rostedt 				.line = __LINE__,			\
1552bcd521aSSteven Rostedt 			};						\
1562bcd521aSSteven Rostedt 		______r = !!(cond);					\
15797e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1582bcd521aSSteven Rostedt 		______r;						\
1592bcd521aSSteven Rostedt 	}))
1602bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1612bcd521aSSteven Rostedt 
1621f0d69a9SSteven Rostedt #else
1631da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1641da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1651f0d69a9SSteven Rostedt #endif
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* Optimization barrier */
1681da177e4SLinus Torvalds #ifndef barrier
1691da177e4SLinus Torvalds # define barrier() __memory_barrier()
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
172*7829fb09SDaniel Borkmann #ifndef barrier_data
173*7829fb09SDaniel Borkmann # define barrier_data(ptr) barrier()
174*7829fb09SDaniel Borkmann #endif
175*7829fb09SDaniel Borkmann 
17638938c87SDavid Daney /* Unreachable code */
17738938c87SDavid Daney #ifndef unreachable
17838938c87SDavid Daney # define unreachable() do { } while (1)
17938938c87SDavid Daney #endif
18038938c87SDavid Daney 
1811da177e4SLinus Torvalds #ifndef RELOC_HIDE
1821da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1831da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1841da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1851da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1861da177e4SLinus Torvalds #endif
1871da177e4SLinus Torvalds 
188fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
189fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier()
190fe8c8a12SCesar Eduardo Barros #endif
191fe8c8a12SCesar Eduardo Barros 
1926f33d587SRusty Russell /* Not-quite-unique ID. */
1936f33d587SRusty Russell #ifndef __UNIQUE_ID
1946f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
1956f33d587SRusty Russell #endif
1966f33d587SRusty Russell 
197230fa253SChristian Borntraeger #include <uapi/linux/types.h>
198230fa253SChristian Borntraeger 
199dd369297SLinus Torvalds static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
200230fa253SChristian Borntraeger {
201230fa253SChristian Borntraeger 	switch (size) {
202230fa253SChristian Borntraeger 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
203230fa253SChristian Borntraeger 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
204230fa253SChristian Borntraeger 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
205230fa253SChristian Borntraeger 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
206230fa253SChristian Borntraeger 	default:
207230fa253SChristian Borntraeger 		barrier();
208230fa253SChristian Borntraeger 		__builtin_memcpy((void *)res, (const void *)p, size);
209230fa253SChristian Borntraeger 		barrier();
210230fa253SChristian Borntraeger 	}
211230fa253SChristian Borntraeger }
212230fa253SChristian Borntraeger 
21343239cbeSChristian Borntraeger static __always_inline void __write_once_size(volatile void *p, void *res, int size)
214230fa253SChristian Borntraeger {
215230fa253SChristian Borntraeger 	switch (size) {
216230fa253SChristian Borntraeger 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
217230fa253SChristian Borntraeger 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
218230fa253SChristian Borntraeger 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
219230fa253SChristian Borntraeger 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
220230fa253SChristian Borntraeger 	default:
221230fa253SChristian Borntraeger 		barrier();
222230fa253SChristian Borntraeger 		__builtin_memcpy((void *)p, (const void *)res, size);
223230fa253SChristian Borntraeger 		barrier();
224230fa253SChristian Borntraeger 	}
225230fa253SChristian Borntraeger }
226230fa253SChristian Borntraeger 
227230fa253SChristian Borntraeger /*
228230fa253SChristian Borntraeger  * Prevent the compiler from merging or refetching reads or writes. The
229230fa253SChristian Borntraeger  * compiler is also forbidden from reordering successive instances of
23043239cbeSChristian Borntraeger  * READ_ONCE, WRITE_ONCE and ACCESS_ONCE (see below), but only when the
231230fa253SChristian Borntraeger  * compiler is aware of some particular ordering.  One way to make the
232230fa253SChristian Borntraeger  * compiler aware of ordering is to put the two invocations of READ_ONCE,
23343239cbeSChristian Borntraeger  * WRITE_ONCE or ACCESS_ONCE() in different C statements.
234230fa253SChristian Borntraeger  *
235230fa253SChristian Borntraeger  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
236230fa253SChristian Borntraeger  * data types like structs or unions. If the size of the accessed data
237230fa253SChristian Borntraeger  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
23843239cbeSChristian Borntraeger  * READ_ONCE() and WRITE_ONCE()  will fall back to memcpy and print a
239230fa253SChristian Borntraeger  * compile-time warning.
240230fa253SChristian Borntraeger  *
241230fa253SChristian Borntraeger  * Their two major use cases are: (1) Mediating communication between
242230fa253SChristian Borntraeger  * process-level code and irq/NMI handlers, all running on the same CPU,
243230fa253SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
244230fa253SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
245230fa253SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
246230fa253SChristian Borntraeger  * required ordering.
247230fa253SChristian Borntraeger  */
248230fa253SChristian Borntraeger 
249230fa253SChristian Borntraeger #define READ_ONCE(x) \
250dd369297SLinus Torvalds 	({ union { typeof(x) __val; char __c[1]; } __u; __read_once_size(&(x), __u.__c, sizeof(x)); __u.__val; })
251230fa253SChristian Borntraeger 
25243239cbeSChristian Borntraeger #define WRITE_ONCE(x, val) \
253dd369297SLinus Torvalds 	({ typeof(x) __val = (val); __write_once_size(&(x), &__val, sizeof(__val)); __val; })
254230fa253SChristian Borntraeger 
2551da177e4SLinus Torvalds #endif /* __KERNEL__ */
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
2581da177e4SLinus Torvalds 
2594f79c3ffSDavid Woodhouse #ifdef __KERNEL__
2601da177e4SLinus Torvalds /*
2611da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
2621da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
2631da177e4SLinus Torvalds  * Usage is:
2641da177e4SLinus Torvalds  * 		int __deprecated foo(void)
2651da177e4SLinus Torvalds  */
2661da177e4SLinus Torvalds #ifndef __deprecated
2671da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
2681da177e4SLinus Torvalds #endif
2691da177e4SLinus Torvalds 
270512345beSPaul E. McKenney #ifdef MODULE
271512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
272512345beSPaul E. McKenney #else
273512345beSPaul E. McKenney #define __deprecated_for_modules
274512345beSPaul E. McKenney #endif
275512345beSPaul E. McKenney 
2761da177e4SLinus Torvalds #ifndef __must_check
2771da177e4SLinus Torvalds #define __must_check
2781da177e4SLinus Torvalds #endif
2791da177e4SLinus Torvalds 
280cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
281cebc04baSAndrew Morton #undef __must_check
282cebc04baSAndrew Morton #define __must_check
283cebc04baSAndrew Morton #endif
284de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
285de488443SJeff Garzik #undef __deprecated
286de488443SJeff Garzik #undef __deprecated_for_modules
287de488443SJeff Garzik #define __deprecated
288de488443SJeff Garzik #define __deprecated_for_modules
289de488443SJeff Garzik #endif
290cebc04baSAndrew Morton 
2911da177e4SLinus Torvalds /*
2921da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
2931da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
2941da177e4SLinus Torvalds  *
2950d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
2960d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
2971da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
2981da177e4SLinus Torvalds  *
2990d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
3000d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
3010d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
3020d7ebbbcSDavid Rientjes  *
3031da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
3041da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
3050d7ebbbcSDavid Rientjes  *
3060d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
3070d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
3081da177e4SLinus Torvalds  */
3090d7ebbbcSDavid Rientjes #ifndef __used
3100d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
3110d7ebbbcSDavid Rientjes #endif
3120d7ebbbcSDavid Rientjes 
3130d7ebbbcSDavid Rientjes #ifndef __maybe_unused
3140d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
3151da177e4SLinus Torvalds #endif
3161da177e4SLinus Torvalds 
3177b2a3513SLi Zefan #ifndef __always_unused
3187b2a3513SLi Zefan # define __always_unused	/* unimplemented */
3197b2a3513SLi Zefan #endif
3207b2a3513SLi Zefan 
321423bc7b2SDavid Woodhouse #ifndef noinline
322423bc7b2SDavid Woodhouse #define noinline
323423bc7b2SDavid Woodhouse #endif
324423bc7b2SDavid Woodhouse 
325735c4fb9SAndrew Morton /*
326735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
327e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
328735c4fb9SAndrew Morton  */
329735c4fb9SAndrew Morton #define noinline_for_stack noinline
330735c4fb9SAndrew Morton 
331423bc7b2SDavid Woodhouse #ifndef __always_inline
332423bc7b2SDavid Woodhouse #define __always_inline inline
333423bc7b2SDavid Woodhouse #endif
334423bc7b2SDavid Woodhouse 
335423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
336423bc7b2SDavid Woodhouse 
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds  * From the GCC manual:
3391da177e4SLinus Torvalds  *
3401da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
3411da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
3421da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
3431da177e4SLinus Torvalds  * since function is not allowed to read global memory.
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
3461da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
3471da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
3481da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
3491da177e4SLinus Torvalds  * `void'.
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds #ifndef __attribute_const__
3521da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
3531da177e4SLinus Torvalds #endif
3541da177e4SLinus Torvalds 
355a586df06SAndi Kleen /*
356a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
357a586df06SAndi Kleen  * directly leading to the call is unlikely.
358a586df06SAndi Kleen  */
359a586df06SAndi Kleen 
360a586df06SAndi Kleen #ifndef __cold
361a586df06SAndi Kleen #define __cold
362a586df06SAndi Kleen #endif
363a586df06SAndi Kleen 
364f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
365f3fe866dSSam Ravnborg #ifndef __section
366f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
367f3fe866dSSam Ravnborg #endif
368f3fe866dSSam Ravnborg 
3699a858dc7SAndi Kleen #ifndef __visible
3709a858dc7SAndi Kleen #define __visible
3719a858dc7SAndi Kleen #endif
3729a858dc7SAndi Kleen 
373d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
374d2c123c2SRusty Russell #ifndef __same_type
375d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
376d2c123c2SRusty Russell #endif
377d2c123c2SRusty Russell 
37847933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */
37947933ad4SPeter Zijlstra #ifndef __native_word
380536fa402SPaul E. McKenney # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
38147933ad4SPeter Zijlstra #endif
38247933ad4SPeter Zijlstra 
3839f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
3849f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
3859f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
3869f0cf4adSArjan van de Ven #endif
3874a312769SArjan van de Ven #ifndef __compiletime_warning
3884a312769SArjan van de Ven # define __compiletime_warning(message)
3894a312769SArjan van de Ven #endif
39063312b6aSArjan van de Ven #ifndef __compiletime_error
39163312b6aSArjan van de Ven # define __compiletime_error(message)
3922c0d259eSJames Hogan /*
3932c0d259eSJames Hogan  * Sparse complains of variable sized arrays due to the temporary variable in
3942c0d259eSJames Hogan  * __compiletime_assert. Unfortunately we can't just expand it out to make
3952c0d259eSJames Hogan  * sparse see a constant array size without breaking compiletime_assert on old
3962c0d259eSJames Hogan  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
3972c0d259eSJames Hogan  */
3982c0d259eSJames Hogan # ifndef __CHECKER__
399c361d3e5SDaniel Santos #  define __compiletime_error_fallback(condition) \
4009a8ab1c3SDaniel Santos 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
4012c0d259eSJames Hogan # endif
4022c0d259eSJames Hogan #endif
4032c0d259eSJames Hogan #ifndef __compiletime_error_fallback
404c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0)
40563312b6aSArjan van de Ven #endif
406c361d3e5SDaniel Santos 
4079a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix)		\
4089a8ab1c3SDaniel Santos 	do {								\
4099a8ab1c3SDaniel Santos 		bool __cond = !(condition);				\
4109a8ab1c3SDaniel Santos 		extern void prefix ## suffix(void) __compiletime_error(msg); \
4119a8ab1c3SDaniel Santos 		if (__cond)						\
4129a8ab1c3SDaniel Santos 			prefix ## suffix();				\
4139a8ab1c3SDaniel Santos 		__compiletime_error_fallback(__cond);			\
4149a8ab1c3SDaniel Santos 	} while (0)
4159a8ab1c3SDaniel Santos 
4169a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \
4179a8ab1c3SDaniel Santos 	__compiletime_assert(condition, msg, prefix, suffix)
4189a8ab1c3SDaniel Santos 
4199a8ab1c3SDaniel Santos /**
4209a8ab1c3SDaniel Santos  * compiletime_assert - break build and emit msg if condition is false
4219a8ab1c3SDaniel Santos  * @condition: a compile-time constant condition to check
4229a8ab1c3SDaniel Santos  * @msg:       a message to emit if condition is false
4239a8ab1c3SDaniel Santos  *
4249a8ab1c3SDaniel Santos  * In tradition of POSIX assert, this macro will break the build if the
4259a8ab1c3SDaniel Santos  * supplied condition is *false*, emitting the supplied error message if the
4269a8ab1c3SDaniel Santos  * compiler has support to do so.
4279a8ab1c3SDaniel Santos  */
4289a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \
4299a8ab1c3SDaniel Santos 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
4309a8ab1c3SDaniel Santos 
43147933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t)				\
43247933ad4SPeter Zijlstra 	compiletime_assert(__native_word(t),				\
43347933ad4SPeter Zijlstra 		"Need native word sized stores/loads for atomicity.")
43447933ad4SPeter Zijlstra 
4359c3cdc1fSLinus Torvalds /*
4369c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
4379c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
4389c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
4399c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
4409c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
4419c3cdc1fSLinus Torvalds  *
442927609d6SChristian Borntraeger  * ACCESS_ONCE will only work on scalar types. For union types, ACCESS_ONCE
443927609d6SChristian Borntraeger  * on a union member will work as long as the size of the member matches the
444927609d6SChristian Borntraeger  * size of the union and the size is smaller than word size.
445927609d6SChristian Borntraeger  *
446927609d6SChristian Borntraeger  * The major use cases of ACCESS_ONCE used to be (1) Mediating communication
447927609d6SChristian Borntraeger  * between process-level code and irq/NMI handlers, all running on the same CPU,
448927609d6SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
449927609d6SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
450927609d6SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
451927609d6SChristian Borntraeger  * required ordering.
452927609d6SChristian Borntraeger  *
453927609d6SChristian Borntraeger  * If possible use READ_ONCE/ASSIGN_ONCE instead.
4549c3cdc1fSLinus Torvalds  */
455927609d6SChristian Borntraeger #define __ACCESS_ONCE(x) ({ \
456c5b19946SChristian Borntraeger 	 __maybe_unused typeof(x) __var = (__force typeof(x)) 0; \
457927609d6SChristian Borntraeger 	(volatile typeof(x) *)&(x); })
458927609d6SChristian Borntraeger #define ACCESS_ONCE(x) (*__ACCESS_ONCE(x))
4599c3cdc1fSLinus Torvalds 
460324670b6SMasami Hiramatsu /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
461324670b6SMasami Hiramatsu #ifdef CONFIG_KPROBES
462324670b6SMasami Hiramatsu # define __kprobes	__attribute__((__section__(".kprobes.text")))
463376e2424SMasami Hiramatsu # define nokprobe_inline	__always_inline
464324670b6SMasami Hiramatsu #else
465324670b6SMasami Hiramatsu # define __kprobes
466376e2424SMasami Hiramatsu # define nokprobe_inline	inline
467324670b6SMasami Hiramatsu #endif
4681da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
469