xref: /linux/include/linux/compiler.h (revision 230fa253df6352af12ad0a16128760b5cb3f92df)
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 
5728614889SSteven Rostedt #define notrace __attribute__((no_instrument_function))
5828614889SSteven Rostedt 
591da177e4SLinus Torvalds /* Intel compiler defines __GNUC__. So we will overwrite implementations
601da177e4SLinus Torvalds  * coming from above header files here
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds #ifdef __INTEL_COMPILER
631da177e4SLinus Torvalds # include <linux/compiler-intel.h>
641da177e4SLinus Torvalds #endif
651da177e4SLinus Torvalds 
66565cbdc2SMark Charlebois /* Clang compiler defines __GNUC__. So we will overwrite implementations
67565cbdc2SMark Charlebois  * coming from above header files here
68565cbdc2SMark Charlebois  */
69565cbdc2SMark Charlebois #ifdef __clang__
70565cbdc2SMark Charlebois #include <linux/compiler-clang.h>
71565cbdc2SMark Charlebois #endif
72565cbdc2SMark Charlebois 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  * Generic compiler-dependent macros required for kernel
751da177e4SLinus Torvalds  * build go below this comment. Actual compiler/compiler version
761da177e4SLinus Torvalds  * specific implementations come from the above header files
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds 
792ed84eebSSteven Rostedt struct ftrace_branch_data {
801f0d69a9SSteven Rostedt 	const char *func;
811f0d69a9SSteven Rostedt 	const char *file;
821f0d69a9SSteven Rostedt 	unsigned line;
832bcd521aSSteven Rostedt 	union {
842bcd521aSSteven Rostedt 		struct {
851f0d69a9SSteven Rostedt 			unsigned long correct;
861f0d69a9SSteven Rostedt 			unsigned long incorrect;
871f0d69a9SSteven Rostedt 		};
882bcd521aSSteven Rostedt 		struct {
892bcd521aSSteven Rostedt 			unsigned long miss;
902bcd521aSSteven Rostedt 			unsigned long hit;
912bcd521aSSteven Rostedt 		};
9297e7e4f3SWitold Baryluk 		unsigned long miss_hit[2];
932bcd521aSSteven Rostedt 	};
942bcd521aSSteven Rostedt };
952ed84eebSSteven Rostedt 
962ed84eebSSteven Rostedt /*
972ed84eebSSteven Rostedt  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
982ed84eebSSteven Rostedt  * to disable branch tracing on a per file basis.
992ed84eebSSteven Rostedt  */
100d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
101d9ad8bc0SBart Van Assche     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
1022ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect);
1031f0d69a9SSteven Rostedt 
1041f0d69a9SSteven Rostedt #define likely_notrace(x)	__builtin_expect(!!(x), 1)
1051f0d69a9SSteven Rostedt #define unlikely_notrace(x)	__builtin_expect(!!(x), 0)
1061f0d69a9SSteven Rostedt 
10745b79749SSteven Rostedt #define __branch_check__(x, expect) ({					\
1081f0d69a9SSteven Rostedt 			int ______r;					\
1092ed84eebSSteven Rostedt 			static struct ftrace_branch_data		\
1101f0d69a9SSteven Rostedt 				__attribute__((__aligned__(4)))		\
11145b79749SSteven Rostedt 				__attribute__((section("_ftrace_annotated_branch"))) \
1121f0d69a9SSteven Rostedt 				______f = {				\
1131f0d69a9SSteven Rostedt 				.func = __func__,			\
1141f0d69a9SSteven Rostedt 				.file = __FILE__,			\
1151f0d69a9SSteven Rostedt 				.line = __LINE__,			\
1161f0d69a9SSteven Rostedt 			};						\
1171f0d69a9SSteven Rostedt 			______r = likely_notrace(x);			\
11845b79749SSteven Rostedt 			ftrace_likely_update(&______f, ______r, expect); \
1191f0d69a9SSteven Rostedt 			______r;					\
1201f0d69a9SSteven Rostedt 		})
1211f0d69a9SSteven Rostedt 
1221f0d69a9SSteven Rostedt /*
1231f0d69a9SSteven Rostedt  * Using __builtin_constant_p(x) to ignore cases where the return
1241f0d69a9SSteven Rostedt  * value is always the same.  This idea is taken from a similar patch
1251f0d69a9SSteven Rostedt  * written by Daniel Walker.
1261f0d69a9SSteven Rostedt  */
1271f0d69a9SSteven Rostedt # ifndef likely
12845b79749SSteven Rostedt #  define likely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))
1291f0d69a9SSteven Rostedt # endif
1301f0d69a9SSteven Rostedt # ifndef unlikely
13145b79749SSteven Rostedt #  define unlikely(x)	(__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))
1321f0d69a9SSteven Rostedt # endif
1332bcd521aSSteven Rostedt 
1342bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES
1352bcd521aSSteven Rostedt /*
1362bcd521aSSteven Rostedt  * "Define 'is'", Bill Clinton
1372bcd521aSSteven Rostedt  * "Define 'if'", Steven Rostedt
1382bcd521aSSteven Rostedt  */
139ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) )
140ab3c9c68SLinus Torvalds #define __trace_if(cond) \
141ab3c9c68SLinus Torvalds 	if (__builtin_constant_p((cond)) ? !!(cond) :			\
1422bcd521aSSteven Rostedt 	({								\
1432bcd521aSSteven Rostedt 		int ______r;						\
1442bcd521aSSteven Rostedt 		static struct ftrace_branch_data			\
1452bcd521aSSteven Rostedt 			__attribute__((__aligned__(4)))			\
1462bcd521aSSteven Rostedt 			__attribute__((section("_ftrace_branch")))	\
1472bcd521aSSteven Rostedt 			______f = {					\
1482bcd521aSSteven Rostedt 				.func = __func__,			\
1492bcd521aSSteven Rostedt 				.file = __FILE__,			\
1502bcd521aSSteven Rostedt 				.line = __LINE__,			\
1512bcd521aSSteven Rostedt 			};						\
1522bcd521aSSteven Rostedt 		______r = !!(cond);					\
15397e7e4f3SWitold Baryluk 		______f.miss_hit[______r]++;					\
1542bcd521aSSteven Rostedt 		______r;						\
1552bcd521aSSteven Rostedt 	}))
1562bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */
1572bcd521aSSteven Rostedt 
1581f0d69a9SSteven Rostedt #else
1591da177e4SLinus Torvalds # define likely(x)	__builtin_expect(!!(x), 1)
1601da177e4SLinus Torvalds # define unlikely(x)	__builtin_expect(!!(x), 0)
1611f0d69a9SSteven Rostedt #endif
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /* Optimization barrier */
1641da177e4SLinus Torvalds #ifndef barrier
1651da177e4SLinus Torvalds # define barrier() __memory_barrier()
1661da177e4SLinus Torvalds #endif
1671da177e4SLinus Torvalds 
16838938c87SDavid Daney /* Unreachable code */
16938938c87SDavid Daney #ifndef unreachable
17038938c87SDavid Daney # define unreachable() do { } while (1)
17138938c87SDavid Daney #endif
17238938c87SDavid Daney 
1731da177e4SLinus Torvalds #ifndef RELOC_HIDE
1741da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off)					\
1751da177e4SLinus Torvalds   ({ unsigned long __ptr;					\
1761da177e4SLinus Torvalds      __ptr = (unsigned long) (ptr);				\
1771da177e4SLinus Torvalds     (typeof(ptr)) (__ptr + (off)); })
1781da177e4SLinus Torvalds #endif
1791da177e4SLinus Torvalds 
180fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR
181fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier()
182fe8c8a12SCesar Eduardo Barros #endif
183fe8c8a12SCesar Eduardo Barros 
1846f33d587SRusty Russell /* Not-quite-unique ID. */
1856f33d587SRusty Russell #ifndef __UNIQUE_ID
1866f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
1876f33d587SRusty Russell #endif
1886f33d587SRusty Russell 
189*230fa253SChristian Borntraeger #include <uapi/linux/types.h>
190*230fa253SChristian Borntraeger 
191*230fa253SChristian Borntraeger static __always_inline void data_access_exceeds_word_size(void)
192*230fa253SChristian Borntraeger #ifdef __compiletime_warning
193*230fa253SChristian Borntraeger __compiletime_warning("data access exceeds word size and won't be atomic")
194*230fa253SChristian Borntraeger #endif
195*230fa253SChristian Borntraeger ;
196*230fa253SChristian Borntraeger 
197*230fa253SChristian Borntraeger static __always_inline void data_access_exceeds_word_size(void)
198*230fa253SChristian Borntraeger {
199*230fa253SChristian Borntraeger }
200*230fa253SChristian Borntraeger 
201*230fa253SChristian Borntraeger static __always_inline void __read_once_size(volatile void *p, void *res, int size)
202*230fa253SChristian Borntraeger {
203*230fa253SChristian Borntraeger 	switch (size) {
204*230fa253SChristian Borntraeger 	case 1: *(__u8 *)res = *(volatile __u8 *)p; break;
205*230fa253SChristian Borntraeger 	case 2: *(__u16 *)res = *(volatile __u16 *)p; break;
206*230fa253SChristian Borntraeger 	case 4: *(__u32 *)res = *(volatile __u32 *)p; break;
207*230fa253SChristian Borntraeger #ifdef CONFIG_64BIT
208*230fa253SChristian Borntraeger 	case 8: *(__u64 *)res = *(volatile __u64 *)p; break;
209*230fa253SChristian Borntraeger #endif
210*230fa253SChristian Borntraeger 	default:
211*230fa253SChristian Borntraeger 		barrier();
212*230fa253SChristian Borntraeger 		__builtin_memcpy((void *)res, (const void *)p, size);
213*230fa253SChristian Borntraeger 		data_access_exceeds_word_size();
214*230fa253SChristian Borntraeger 		barrier();
215*230fa253SChristian Borntraeger 	}
216*230fa253SChristian Borntraeger }
217*230fa253SChristian Borntraeger 
218*230fa253SChristian Borntraeger static __always_inline void __assign_once_size(volatile void *p, void *res, int size)
219*230fa253SChristian Borntraeger {
220*230fa253SChristian Borntraeger 	switch (size) {
221*230fa253SChristian Borntraeger 	case 1: *(volatile __u8 *)p = *(__u8 *)res; break;
222*230fa253SChristian Borntraeger 	case 2: *(volatile __u16 *)p = *(__u16 *)res; break;
223*230fa253SChristian Borntraeger 	case 4: *(volatile __u32 *)p = *(__u32 *)res; break;
224*230fa253SChristian Borntraeger #ifdef CONFIG_64BIT
225*230fa253SChristian Borntraeger 	case 8: *(volatile __u64 *)p = *(__u64 *)res; break;
226*230fa253SChristian Borntraeger #endif
227*230fa253SChristian Borntraeger 	default:
228*230fa253SChristian Borntraeger 		barrier();
229*230fa253SChristian Borntraeger 		__builtin_memcpy((void *)p, (const void *)res, size);
230*230fa253SChristian Borntraeger 		data_access_exceeds_word_size();
231*230fa253SChristian Borntraeger 		barrier();
232*230fa253SChristian Borntraeger 	}
233*230fa253SChristian Borntraeger }
234*230fa253SChristian Borntraeger 
235*230fa253SChristian Borntraeger /*
236*230fa253SChristian Borntraeger  * Prevent the compiler from merging or refetching reads or writes. The
237*230fa253SChristian Borntraeger  * compiler is also forbidden from reordering successive instances of
238*230fa253SChristian Borntraeger  * READ_ONCE, ASSIGN_ONCE and ACCESS_ONCE (see below), but only when the
239*230fa253SChristian Borntraeger  * compiler is aware of some particular ordering.  One way to make the
240*230fa253SChristian Borntraeger  * compiler aware of ordering is to put the two invocations of READ_ONCE,
241*230fa253SChristian Borntraeger  * ASSIGN_ONCE or ACCESS_ONCE() in different C statements.
242*230fa253SChristian Borntraeger  *
243*230fa253SChristian Borntraeger  * In contrast to ACCESS_ONCE these two macros will also work on aggregate
244*230fa253SChristian Borntraeger  * data types like structs or unions. If the size of the accessed data
245*230fa253SChristian Borntraeger  * type exceeds the word size of the machine (e.g., 32 bits or 64 bits)
246*230fa253SChristian Borntraeger  * READ_ONCE() and ASSIGN_ONCE()  will fall back to memcpy and print a
247*230fa253SChristian Borntraeger  * compile-time warning.
248*230fa253SChristian Borntraeger  *
249*230fa253SChristian Borntraeger  * Their two major use cases are: (1) Mediating communication between
250*230fa253SChristian Borntraeger  * process-level code and irq/NMI handlers, all running on the same CPU,
251*230fa253SChristian Borntraeger  * and (2) Ensuring that the compiler does not  fold, spindle, or otherwise
252*230fa253SChristian Borntraeger  * mutilate accesses that either do not require ordering or that interact
253*230fa253SChristian Borntraeger  * with an explicit memory barrier or atomic instruction that provides the
254*230fa253SChristian Borntraeger  * required ordering.
255*230fa253SChristian Borntraeger  */
256*230fa253SChristian Borntraeger 
257*230fa253SChristian Borntraeger #define READ_ONCE(x) \
258*230fa253SChristian Borntraeger 	({ typeof(x) __val; __read_once_size(&x, &__val, sizeof(__val)); __val; })
259*230fa253SChristian Borntraeger 
260*230fa253SChristian Borntraeger #define ASSIGN_ONCE(val, x) \
261*230fa253SChristian Borntraeger 	({ typeof(x) __val; __val = val; __assign_once_size(&x, &__val, sizeof(__val)); __val; })
262*230fa253SChristian Borntraeger 
2631da177e4SLinus Torvalds #endif /* __KERNEL__ */
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */
2661da177e4SLinus Torvalds 
2674f79c3ffSDavid Woodhouse #ifdef __KERNEL__
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  * Allow us to mark functions as 'deprecated' and have gcc emit a nice
2701da177e4SLinus Torvalds  * warning for each use, in hopes of speeding the functions removal.
2711da177e4SLinus Torvalds  * Usage is:
2721da177e4SLinus Torvalds  * 		int __deprecated foo(void)
2731da177e4SLinus Torvalds  */
2741da177e4SLinus Torvalds #ifndef __deprecated
2751da177e4SLinus Torvalds # define __deprecated		/* unimplemented */
2761da177e4SLinus Torvalds #endif
2771da177e4SLinus Torvalds 
278512345beSPaul E. McKenney #ifdef MODULE
279512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated
280512345beSPaul E. McKenney #else
281512345beSPaul E. McKenney #define __deprecated_for_modules
282512345beSPaul E. McKenney #endif
283512345beSPaul E. McKenney 
2841da177e4SLinus Torvalds #ifndef __must_check
2851da177e4SLinus Torvalds #define __must_check
2861da177e4SLinus Torvalds #endif
2871da177e4SLinus Torvalds 
288cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK
289cebc04baSAndrew Morton #undef __must_check
290cebc04baSAndrew Morton #define __must_check
291cebc04baSAndrew Morton #endif
292de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED
293de488443SJeff Garzik #undef __deprecated
294de488443SJeff Garzik #undef __deprecated_for_modules
295de488443SJeff Garzik #define __deprecated
296de488443SJeff Garzik #define __deprecated_for_modules
297de488443SJeff Garzik #endif
298cebc04baSAndrew Morton 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * Allow us to avoid 'defined but not used' warnings on functions and data,
3011da177e4SLinus Torvalds  * as well as force them to be emitted to the assembly file.
3021da177e4SLinus Torvalds  *
3030d7ebbbcSDavid Rientjes  * As of gcc 3.4, static functions that are not marked with attribute((used))
3040d7ebbbcSDavid Rientjes  * may be elided from the assembly file.  As of gcc 3.4, static data not so
3051da177e4SLinus Torvalds  * marked will not be elided, but this may change in a future gcc version.
3061da177e4SLinus Torvalds  *
3070d7ebbbcSDavid Rientjes  * NOTE: Because distributions shipped with a backported unit-at-a-time
3080d7ebbbcSDavid Rientjes  * compiler in gcc 3.3, we must define __used to be __attribute__((used))
3090d7ebbbcSDavid Rientjes  * for gcc >=3.3 instead of 3.4.
3100d7ebbbcSDavid Rientjes  *
3111da177e4SLinus Torvalds  * In prior versions of gcc, such functions and data would be emitted, but
3121da177e4SLinus Torvalds  * would be warned about except with attribute((unused)).
3130d7ebbbcSDavid Rientjes  *
3140d7ebbbcSDavid Rientjes  * Mark functions that are referenced only in inline assembly as __used so
3150d7ebbbcSDavid Rientjes  * the code is emitted even though it appears to be unreferenced.
3161da177e4SLinus Torvalds  */
3170d7ebbbcSDavid Rientjes #ifndef __used
3180d7ebbbcSDavid Rientjes # define __used			/* unimplemented */
3190d7ebbbcSDavid Rientjes #endif
3200d7ebbbcSDavid Rientjes 
3210d7ebbbcSDavid Rientjes #ifndef __maybe_unused
3220d7ebbbcSDavid Rientjes # define __maybe_unused		/* unimplemented */
3231da177e4SLinus Torvalds #endif
3241da177e4SLinus Torvalds 
3257b2a3513SLi Zefan #ifndef __always_unused
3267b2a3513SLi Zefan # define __always_unused	/* unimplemented */
3277b2a3513SLi Zefan #endif
3287b2a3513SLi Zefan 
329423bc7b2SDavid Woodhouse #ifndef noinline
330423bc7b2SDavid Woodhouse #define noinline
331423bc7b2SDavid Woodhouse #endif
332423bc7b2SDavid Woodhouse 
333735c4fb9SAndrew Morton /*
334735c4fb9SAndrew Morton  * Rather then using noinline to prevent stack consumption, use
335e6be0c9eSAlexander Stein  * noinline_for_stack instead.  For documentation reasons.
336735c4fb9SAndrew Morton  */
337735c4fb9SAndrew Morton #define noinline_for_stack noinline
338735c4fb9SAndrew Morton 
339423bc7b2SDavid Woodhouse #ifndef __always_inline
340423bc7b2SDavid Woodhouse #define __always_inline inline
341423bc7b2SDavid Woodhouse #endif
342423bc7b2SDavid Woodhouse 
343423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */
344423bc7b2SDavid Woodhouse 
3451da177e4SLinus Torvalds /*
3461da177e4SLinus Torvalds  * From the GCC manual:
3471da177e4SLinus Torvalds  *
3481da177e4SLinus Torvalds  * Many functions do not examine any values except their arguments,
3491da177e4SLinus Torvalds  * and have no effects except the return value.  Basically this is
3501da177e4SLinus Torvalds  * just slightly more strict class than the `pure' attribute above,
3511da177e4SLinus Torvalds  * since function is not allowed to read global memory.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * Note that a function that has pointer arguments and examines the
3541da177e4SLinus Torvalds  * data pointed to must _not_ be declared `const'.  Likewise, a
3551da177e4SLinus Torvalds  * function that calls a non-`const' function usually must not be
3561da177e4SLinus Torvalds  * `const'.  It does not make sense for a `const' function to return
3571da177e4SLinus Torvalds  * `void'.
3581da177e4SLinus Torvalds  */
3591da177e4SLinus Torvalds #ifndef __attribute_const__
3601da177e4SLinus Torvalds # define __attribute_const__	/* unimplemented */
3611da177e4SLinus Torvalds #endif
3621da177e4SLinus Torvalds 
363a586df06SAndi Kleen /*
364a586df06SAndi Kleen  * Tell gcc if a function is cold. The compiler will assume any path
365a586df06SAndi Kleen  * directly leading to the call is unlikely.
366a586df06SAndi Kleen  */
367a586df06SAndi Kleen 
368a586df06SAndi Kleen #ifndef __cold
369a586df06SAndi Kleen #define __cold
370a586df06SAndi Kleen #endif
371a586df06SAndi Kleen 
372f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */
373f3fe866dSSam Ravnborg #ifndef __section
374f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S)))
375f3fe866dSSam Ravnborg #endif
376f3fe866dSSam Ravnborg 
3779a858dc7SAndi Kleen #ifndef __visible
3789a858dc7SAndi Kleen #define __visible
3799a858dc7SAndi Kleen #endif
3809a858dc7SAndi Kleen 
381d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */
382d2c123c2SRusty Russell #ifndef __same_type
383d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
384d2c123c2SRusty Russell #endif
385d2c123c2SRusty Russell 
38647933ad4SPeter Zijlstra /* Is this type a native word size -- useful for atomic operations */
38747933ad4SPeter Zijlstra #ifndef __native_word
38847933ad4SPeter Zijlstra # define __native_word(t) (sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
38947933ad4SPeter Zijlstra #endif
39047933ad4SPeter Zijlstra 
3919f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */
3929f0cf4adSArjan van de Ven #ifndef __compiletime_object_size
3939f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1
3949f0cf4adSArjan van de Ven #endif
3954a312769SArjan van de Ven #ifndef __compiletime_warning
3964a312769SArjan van de Ven # define __compiletime_warning(message)
3974a312769SArjan van de Ven #endif
39863312b6aSArjan van de Ven #ifndef __compiletime_error
39963312b6aSArjan van de Ven # define __compiletime_error(message)
4002c0d259eSJames Hogan /*
4012c0d259eSJames Hogan  * Sparse complains of variable sized arrays due to the temporary variable in
4022c0d259eSJames Hogan  * __compiletime_assert. Unfortunately we can't just expand it out to make
4032c0d259eSJames Hogan  * sparse see a constant array size without breaking compiletime_assert on old
4042c0d259eSJames Hogan  * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether.
4052c0d259eSJames Hogan  */
4062c0d259eSJames Hogan # ifndef __CHECKER__
407c361d3e5SDaniel Santos #  define __compiletime_error_fallback(condition) \
4089a8ab1c3SDaniel Santos 	do { ((void)sizeof(char[1 - 2 * condition])); } while (0)
4092c0d259eSJames Hogan # endif
4102c0d259eSJames Hogan #endif
4112c0d259eSJames Hogan #ifndef __compiletime_error_fallback
412c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0)
41363312b6aSArjan van de Ven #endif
414c361d3e5SDaniel Santos 
4159a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix)		\
4169a8ab1c3SDaniel Santos 	do {								\
4179a8ab1c3SDaniel Santos 		bool __cond = !(condition);				\
4189a8ab1c3SDaniel Santos 		extern void prefix ## suffix(void) __compiletime_error(msg); \
4199a8ab1c3SDaniel Santos 		if (__cond)						\
4209a8ab1c3SDaniel Santos 			prefix ## suffix();				\
4219a8ab1c3SDaniel Santos 		__compiletime_error_fallback(__cond);			\
4229a8ab1c3SDaniel Santos 	} while (0)
4239a8ab1c3SDaniel Santos 
4249a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \
4259a8ab1c3SDaniel Santos 	__compiletime_assert(condition, msg, prefix, suffix)
4269a8ab1c3SDaniel Santos 
4279a8ab1c3SDaniel Santos /**
4289a8ab1c3SDaniel Santos  * compiletime_assert - break build and emit msg if condition is false
4299a8ab1c3SDaniel Santos  * @condition: a compile-time constant condition to check
4309a8ab1c3SDaniel Santos  * @msg:       a message to emit if condition is false
4319a8ab1c3SDaniel Santos  *
4329a8ab1c3SDaniel Santos  * In tradition of POSIX assert, this macro will break the build if the
4339a8ab1c3SDaniel Santos  * supplied condition is *false*, emitting the supplied error message if the
4349a8ab1c3SDaniel Santos  * compiler has support to do so.
4359a8ab1c3SDaniel Santos  */
4369a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \
4379a8ab1c3SDaniel Santos 	_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
4389a8ab1c3SDaniel Santos 
43947933ad4SPeter Zijlstra #define compiletime_assert_atomic_type(t)				\
44047933ad4SPeter Zijlstra 	compiletime_assert(__native_word(t),				\
44147933ad4SPeter Zijlstra 		"Need native word sized stores/loads for atomicity.")
44247933ad4SPeter Zijlstra 
4439c3cdc1fSLinus Torvalds /*
4449c3cdc1fSLinus Torvalds  * Prevent the compiler from merging or refetching accesses.  The compiler
4459c3cdc1fSLinus Torvalds  * is also forbidden from reordering successive instances of ACCESS_ONCE(),
4469c3cdc1fSLinus Torvalds  * but only when the compiler is aware of some particular ordering.  One way
4479c3cdc1fSLinus Torvalds  * to make the compiler aware of ordering is to put the two invocations of
4489c3cdc1fSLinus Torvalds  * ACCESS_ONCE() in different C statements.
4499c3cdc1fSLinus Torvalds  *
4509c3cdc1fSLinus Torvalds  * This macro does absolutely -nothing- to prevent the CPU from reordering,
451ded00a56SPaul E. McKenney  * merging, or refetching absolutely anything at any time.  Its main intended
452ded00a56SPaul E. McKenney  * use is to mediate communication between process-level code and irq/NMI
453ded00a56SPaul E. McKenney  * handlers, all running on the same CPU.
4549c3cdc1fSLinus Torvalds  */
4559c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
4569c3cdc1fSLinus Torvalds 
457324670b6SMasami Hiramatsu /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */
458324670b6SMasami Hiramatsu #ifdef CONFIG_KPROBES
459324670b6SMasami Hiramatsu # define __kprobes	__attribute__((__section__(".kprobes.text")))
460376e2424SMasami Hiramatsu # define nokprobe_inline	__always_inline
461324670b6SMasami Hiramatsu #else
462324670b6SMasami Hiramatsu # define __kprobes
463376e2424SMasami Hiramatsu # define nokprobe_inline	inline
464324670b6SMasami Hiramatsu #endif
4651da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */
466