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 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Generic compiler-dependent macros required for kernel 681da177e4SLinus Torvalds * build go below this comment. Actual compiler/compiler version 691da177e4SLinus Torvalds * specific implementations come from the above header files 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds 722ed84eebSSteven Rostedt struct ftrace_branch_data { 731f0d69a9SSteven Rostedt const char *func; 741f0d69a9SSteven Rostedt const char *file; 751f0d69a9SSteven Rostedt unsigned line; 762bcd521aSSteven Rostedt union { 772bcd521aSSteven Rostedt struct { 781f0d69a9SSteven Rostedt unsigned long correct; 791f0d69a9SSteven Rostedt unsigned long incorrect; 801f0d69a9SSteven Rostedt }; 812bcd521aSSteven Rostedt struct { 822bcd521aSSteven Rostedt unsigned long miss; 832bcd521aSSteven Rostedt unsigned long hit; 842bcd521aSSteven Rostedt }; 8597e7e4f3SWitold Baryluk unsigned long miss_hit[2]; 862bcd521aSSteven Rostedt }; 872bcd521aSSteven Rostedt }; 882ed84eebSSteven Rostedt 892ed84eebSSteven Rostedt /* 902ed84eebSSteven Rostedt * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code 912ed84eebSSteven Rostedt * to disable branch tracing on a per file basis. 922ed84eebSSteven Rostedt */ 93d9ad8bc0SBart Van Assche #if defined(CONFIG_TRACE_BRANCH_PROFILING) \ 94d9ad8bc0SBart Van Assche && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__) 952ed84eebSSteven Rostedt void ftrace_likely_update(struct ftrace_branch_data *f, int val, int expect); 961f0d69a9SSteven Rostedt 971f0d69a9SSteven Rostedt #define likely_notrace(x) __builtin_expect(!!(x), 1) 981f0d69a9SSteven Rostedt #define unlikely_notrace(x) __builtin_expect(!!(x), 0) 991f0d69a9SSteven Rostedt 10045b79749SSteven Rostedt #define __branch_check__(x, expect) ({ \ 1011f0d69a9SSteven Rostedt int ______r; \ 1022ed84eebSSteven Rostedt static struct ftrace_branch_data \ 1031f0d69a9SSteven Rostedt __attribute__((__aligned__(4))) \ 10445b79749SSteven Rostedt __attribute__((section("_ftrace_annotated_branch"))) \ 1051f0d69a9SSteven Rostedt ______f = { \ 1061f0d69a9SSteven Rostedt .func = __func__, \ 1071f0d69a9SSteven Rostedt .file = __FILE__, \ 1081f0d69a9SSteven Rostedt .line = __LINE__, \ 1091f0d69a9SSteven Rostedt }; \ 1101f0d69a9SSteven Rostedt ______r = likely_notrace(x); \ 11145b79749SSteven Rostedt ftrace_likely_update(&______f, ______r, expect); \ 1121f0d69a9SSteven Rostedt ______r; \ 1131f0d69a9SSteven Rostedt }) 1141f0d69a9SSteven Rostedt 1151f0d69a9SSteven Rostedt /* 1161f0d69a9SSteven Rostedt * Using __builtin_constant_p(x) to ignore cases where the return 1171f0d69a9SSteven Rostedt * value is always the same. This idea is taken from a similar patch 1181f0d69a9SSteven Rostedt * written by Daniel Walker. 1191f0d69a9SSteven Rostedt */ 1201f0d69a9SSteven Rostedt # ifndef likely 12145b79749SSteven Rostedt # define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1)) 1221f0d69a9SSteven Rostedt # endif 1231f0d69a9SSteven Rostedt # ifndef unlikely 12445b79749SSteven Rostedt # define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0)) 1251f0d69a9SSteven Rostedt # endif 1262bcd521aSSteven Rostedt 1272bcd521aSSteven Rostedt #ifdef CONFIG_PROFILE_ALL_BRANCHES 1282bcd521aSSteven Rostedt /* 1292bcd521aSSteven Rostedt * "Define 'is'", Bill Clinton 1302bcd521aSSteven Rostedt * "Define 'if'", Steven Rostedt 1312bcd521aSSteven Rostedt */ 132ab3c9c68SLinus Torvalds #define if(cond, ...) __trace_if( (cond , ## __VA_ARGS__) ) 133ab3c9c68SLinus Torvalds #define __trace_if(cond) \ 134ab3c9c68SLinus Torvalds if (__builtin_constant_p((cond)) ? !!(cond) : \ 1352bcd521aSSteven Rostedt ({ \ 1362bcd521aSSteven Rostedt int ______r; \ 1372bcd521aSSteven Rostedt static struct ftrace_branch_data \ 1382bcd521aSSteven Rostedt __attribute__((__aligned__(4))) \ 1392bcd521aSSteven Rostedt __attribute__((section("_ftrace_branch"))) \ 1402bcd521aSSteven Rostedt ______f = { \ 1412bcd521aSSteven Rostedt .func = __func__, \ 1422bcd521aSSteven Rostedt .file = __FILE__, \ 1432bcd521aSSteven Rostedt .line = __LINE__, \ 1442bcd521aSSteven Rostedt }; \ 1452bcd521aSSteven Rostedt ______r = !!(cond); \ 14697e7e4f3SWitold Baryluk ______f.miss_hit[______r]++; \ 1472bcd521aSSteven Rostedt ______r; \ 1482bcd521aSSteven Rostedt })) 1492bcd521aSSteven Rostedt #endif /* CONFIG_PROFILE_ALL_BRANCHES */ 1502bcd521aSSteven Rostedt 1511f0d69a9SSteven Rostedt #else 1521da177e4SLinus Torvalds # define likely(x) __builtin_expect(!!(x), 1) 1531da177e4SLinus Torvalds # define unlikely(x) __builtin_expect(!!(x), 0) 1541f0d69a9SSteven Rostedt #endif 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /* Optimization barrier */ 1571da177e4SLinus Torvalds #ifndef barrier 1581da177e4SLinus Torvalds # define barrier() __memory_barrier() 1591da177e4SLinus Torvalds #endif 1601da177e4SLinus Torvalds 16138938c87SDavid Daney /* Unreachable code */ 16238938c87SDavid Daney #ifndef unreachable 16338938c87SDavid Daney # define unreachable() do { } while (1) 16438938c87SDavid Daney #endif 16538938c87SDavid Daney 1661da177e4SLinus Torvalds #ifndef RELOC_HIDE 1671da177e4SLinus Torvalds # define RELOC_HIDE(ptr, off) \ 1681da177e4SLinus Torvalds ({ unsigned long __ptr; \ 1691da177e4SLinus Torvalds __ptr = (unsigned long) (ptr); \ 1701da177e4SLinus Torvalds (typeof(ptr)) (__ptr + (off)); }) 1711da177e4SLinus Torvalds #endif 1721da177e4SLinus Torvalds 173*fe8c8a12SCesar Eduardo Barros #ifndef OPTIMIZER_HIDE_VAR 174*fe8c8a12SCesar Eduardo Barros #define OPTIMIZER_HIDE_VAR(var) barrier() 175*fe8c8a12SCesar Eduardo Barros #endif 176*fe8c8a12SCesar Eduardo Barros 1776f33d587SRusty Russell /* Not-quite-unique ID. */ 1786f33d587SRusty Russell #ifndef __UNIQUE_ID 1796f33d587SRusty Russell # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__) 1806f33d587SRusty Russell #endif 1816f33d587SRusty Russell 1821da177e4SLinus Torvalds #endif /* __KERNEL__ */ 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds #endif /* __ASSEMBLY__ */ 1851da177e4SLinus Torvalds 1864f79c3ffSDavid Woodhouse #ifdef __KERNEL__ 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * Allow us to mark functions as 'deprecated' and have gcc emit a nice 1891da177e4SLinus Torvalds * warning for each use, in hopes of speeding the functions removal. 1901da177e4SLinus Torvalds * Usage is: 1911da177e4SLinus Torvalds * int __deprecated foo(void) 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds #ifndef __deprecated 1941da177e4SLinus Torvalds # define __deprecated /* unimplemented */ 1951da177e4SLinus Torvalds #endif 1961da177e4SLinus Torvalds 197512345beSPaul E. McKenney #ifdef MODULE 198512345beSPaul E. McKenney #define __deprecated_for_modules __deprecated 199512345beSPaul E. McKenney #else 200512345beSPaul E. McKenney #define __deprecated_for_modules 201512345beSPaul E. McKenney #endif 202512345beSPaul E. McKenney 2031da177e4SLinus Torvalds #ifndef __must_check 2041da177e4SLinus Torvalds #define __must_check 2051da177e4SLinus Torvalds #endif 2061da177e4SLinus Torvalds 207cebc04baSAndrew Morton #ifndef CONFIG_ENABLE_MUST_CHECK 208cebc04baSAndrew Morton #undef __must_check 209cebc04baSAndrew Morton #define __must_check 210cebc04baSAndrew Morton #endif 211de488443SJeff Garzik #ifndef CONFIG_ENABLE_WARN_DEPRECATED 212de488443SJeff Garzik #undef __deprecated 213de488443SJeff Garzik #undef __deprecated_for_modules 214de488443SJeff Garzik #define __deprecated 215de488443SJeff Garzik #define __deprecated_for_modules 216de488443SJeff Garzik #endif 217cebc04baSAndrew Morton 2181da177e4SLinus Torvalds /* 2191da177e4SLinus Torvalds * Allow us to avoid 'defined but not used' warnings on functions and data, 2201da177e4SLinus Torvalds * as well as force them to be emitted to the assembly file. 2211da177e4SLinus Torvalds * 2220d7ebbbcSDavid Rientjes * As of gcc 3.4, static functions that are not marked with attribute((used)) 2230d7ebbbcSDavid Rientjes * may be elided from the assembly file. As of gcc 3.4, static data not so 2241da177e4SLinus Torvalds * marked will not be elided, but this may change in a future gcc version. 2251da177e4SLinus Torvalds * 2260d7ebbbcSDavid Rientjes * NOTE: Because distributions shipped with a backported unit-at-a-time 2270d7ebbbcSDavid Rientjes * compiler in gcc 3.3, we must define __used to be __attribute__((used)) 2280d7ebbbcSDavid Rientjes * for gcc >=3.3 instead of 3.4. 2290d7ebbbcSDavid Rientjes * 2301da177e4SLinus Torvalds * In prior versions of gcc, such functions and data would be emitted, but 2311da177e4SLinus Torvalds * would be warned about except with attribute((unused)). 2320d7ebbbcSDavid Rientjes * 2330d7ebbbcSDavid Rientjes * Mark functions that are referenced only in inline assembly as __used so 2340d7ebbbcSDavid Rientjes * the code is emitted even though it appears to be unreferenced. 2351da177e4SLinus Torvalds */ 2360d7ebbbcSDavid Rientjes #ifndef __used 2370d7ebbbcSDavid Rientjes # define __used /* unimplemented */ 2380d7ebbbcSDavid Rientjes #endif 2390d7ebbbcSDavid Rientjes 2400d7ebbbcSDavid Rientjes #ifndef __maybe_unused 2410d7ebbbcSDavid Rientjes # define __maybe_unused /* unimplemented */ 2421da177e4SLinus Torvalds #endif 2431da177e4SLinus Torvalds 2447b2a3513SLi Zefan #ifndef __always_unused 2457b2a3513SLi Zefan # define __always_unused /* unimplemented */ 2467b2a3513SLi Zefan #endif 2477b2a3513SLi Zefan 248423bc7b2SDavid Woodhouse #ifndef noinline 249423bc7b2SDavid Woodhouse #define noinline 250423bc7b2SDavid Woodhouse #endif 251423bc7b2SDavid Woodhouse 252735c4fb9SAndrew Morton /* 253735c4fb9SAndrew Morton * Rather then using noinline to prevent stack consumption, use 254e6be0c9eSAlexander Stein * noinline_for_stack instead. For documentation reasons. 255735c4fb9SAndrew Morton */ 256735c4fb9SAndrew Morton #define noinline_for_stack noinline 257735c4fb9SAndrew Morton 258423bc7b2SDavid Woodhouse #ifndef __always_inline 259423bc7b2SDavid Woodhouse #define __always_inline inline 260423bc7b2SDavid Woodhouse #endif 261423bc7b2SDavid Woodhouse 262423bc7b2SDavid Woodhouse #endif /* __KERNEL__ */ 263423bc7b2SDavid Woodhouse 2641da177e4SLinus Torvalds /* 2651da177e4SLinus Torvalds * From the GCC manual: 2661da177e4SLinus Torvalds * 2671da177e4SLinus Torvalds * Many functions do not examine any values except their arguments, 2681da177e4SLinus Torvalds * and have no effects except the return value. Basically this is 2691da177e4SLinus Torvalds * just slightly more strict class than the `pure' attribute above, 2701da177e4SLinus Torvalds * since function is not allowed to read global memory. 2711da177e4SLinus Torvalds * 2721da177e4SLinus Torvalds * Note that a function that has pointer arguments and examines the 2731da177e4SLinus Torvalds * data pointed to must _not_ be declared `const'. Likewise, a 2741da177e4SLinus Torvalds * function that calls a non-`const' function usually must not be 2751da177e4SLinus Torvalds * `const'. It does not make sense for a `const' function to return 2761da177e4SLinus Torvalds * `void'. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds #ifndef __attribute_const__ 2791da177e4SLinus Torvalds # define __attribute_const__ /* unimplemented */ 2801da177e4SLinus Torvalds #endif 2811da177e4SLinus Torvalds 282a586df06SAndi Kleen /* 283a586df06SAndi Kleen * Tell gcc if a function is cold. The compiler will assume any path 284a586df06SAndi Kleen * directly leading to the call is unlikely. 285a586df06SAndi Kleen */ 286a586df06SAndi Kleen 287a586df06SAndi Kleen #ifndef __cold 288a586df06SAndi Kleen #define __cold 289a586df06SAndi Kleen #endif 290a586df06SAndi Kleen 291f3fe866dSSam Ravnborg /* Simple shorthand for a section definition */ 292f3fe866dSSam Ravnborg #ifndef __section 293f3fe866dSSam Ravnborg # define __section(S) __attribute__ ((__section__(#S))) 294f3fe866dSSam Ravnborg #endif 295f3fe866dSSam Ravnborg 2969a858dc7SAndi Kleen #ifndef __visible 2979a858dc7SAndi Kleen #define __visible 2989a858dc7SAndi Kleen #endif 2999a858dc7SAndi Kleen 300d2c123c2SRusty Russell /* Are two types/vars the same type (ignoring qualifiers)? */ 301d2c123c2SRusty Russell #ifndef __same_type 302d2c123c2SRusty Russell # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) 303d2c123c2SRusty Russell #endif 304d2c123c2SRusty Russell 3059f0cf4adSArjan van de Ven /* Compile time object size, -1 for unknown */ 3069f0cf4adSArjan van de Ven #ifndef __compiletime_object_size 3079f0cf4adSArjan van de Ven # define __compiletime_object_size(obj) -1 3089f0cf4adSArjan van de Ven #endif 3094a312769SArjan van de Ven #ifndef __compiletime_warning 3104a312769SArjan van de Ven # define __compiletime_warning(message) 3114a312769SArjan van de Ven #endif 31263312b6aSArjan van de Ven #ifndef __compiletime_error 31363312b6aSArjan van de Ven # define __compiletime_error(message) 314c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) \ 3159a8ab1c3SDaniel Santos do { ((void)sizeof(char[1 - 2 * condition])); } while (0) 316c361d3e5SDaniel Santos #else 317c361d3e5SDaniel Santos # define __compiletime_error_fallback(condition) do { } while (0) 31863312b6aSArjan van de Ven #endif 319c361d3e5SDaniel Santos 3209a8ab1c3SDaniel Santos #define __compiletime_assert(condition, msg, prefix, suffix) \ 3219a8ab1c3SDaniel Santos do { \ 3229a8ab1c3SDaniel Santos bool __cond = !(condition); \ 3239a8ab1c3SDaniel Santos extern void prefix ## suffix(void) __compiletime_error(msg); \ 3249a8ab1c3SDaniel Santos if (__cond) \ 3259a8ab1c3SDaniel Santos prefix ## suffix(); \ 3269a8ab1c3SDaniel Santos __compiletime_error_fallback(__cond); \ 3279a8ab1c3SDaniel Santos } while (0) 3289a8ab1c3SDaniel Santos 3299a8ab1c3SDaniel Santos #define _compiletime_assert(condition, msg, prefix, suffix) \ 3309a8ab1c3SDaniel Santos __compiletime_assert(condition, msg, prefix, suffix) 3319a8ab1c3SDaniel Santos 3329a8ab1c3SDaniel Santos /** 3339a8ab1c3SDaniel Santos * compiletime_assert - break build and emit msg if condition is false 3349a8ab1c3SDaniel Santos * @condition: a compile-time constant condition to check 3359a8ab1c3SDaniel Santos * @msg: a message to emit if condition is false 3369a8ab1c3SDaniel Santos * 3379a8ab1c3SDaniel Santos * In tradition of POSIX assert, this macro will break the build if the 3389a8ab1c3SDaniel Santos * supplied condition is *false*, emitting the supplied error message if the 3399a8ab1c3SDaniel Santos * compiler has support to do so. 3409a8ab1c3SDaniel Santos */ 3419a8ab1c3SDaniel Santos #define compiletime_assert(condition, msg) \ 3429a8ab1c3SDaniel Santos _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__) 3439a8ab1c3SDaniel Santos 3449c3cdc1fSLinus Torvalds /* 3459c3cdc1fSLinus Torvalds * Prevent the compiler from merging or refetching accesses. The compiler 3469c3cdc1fSLinus Torvalds * is also forbidden from reordering successive instances of ACCESS_ONCE(), 3479c3cdc1fSLinus Torvalds * but only when the compiler is aware of some particular ordering. One way 3489c3cdc1fSLinus Torvalds * to make the compiler aware of ordering is to put the two invocations of 3499c3cdc1fSLinus Torvalds * ACCESS_ONCE() in different C statements. 3509c3cdc1fSLinus Torvalds * 3519c3cdc1fSLinus Torvalds * This macro does absolutely -nothing- to prevent the CPU from reordering, 352ded00a56SPaul E. McKenney * merging, or refetching absolutely anything at any time. Its main intended 353ded00a56SPaul E. McKenney * use is to mediate communication between process-level code and irq/NMI 354ded00a56SPaul E. McKenney * handlers, all running on the same CPU. 3559c3cdc1fSLinus Torvalds */ 3569c3cdc1fSLinus Torvalds #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) 3579c3cdc1fSLinus Torvalds 358324670b6SMasami Hiramatsu /* Ignore/forbid kprobes attach on very low level functions marked by this attribute: */ 359324670b6SMasami Hiramatsu #ifdef CONFIG_KPROBES 360324670b6SMasami Hiramatsu # define __kprobes __attribute__((__section__(".kprobes.text"))) 361324670b6SMasami Hiramatsu #else 362324670b6SMasami Hiramatsu # define __kprobes 363324670b6SMasami Hiramatsu #endif 3641da177e4SLinus Torvalds #endif /* __LINUX_COMPILER_H */ 365