Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_COMPILER_H
0003 #define __LINUX_COMPILER_H
0004 
0005 #include <linux/compiler_types.h>
0006 
0007 #ifndef __ASSEMBLY__
0008 
0009 #ifdef __KERNEL__
0010 
0011 /*
0012  * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code
0013  * to disable branch tracing on a per file basis.
0014  */
0015 #if defined(CONFIG_TRACE_BRANCH_PROFILING) \
0016     && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)
0017 void ftrace_likely_update(struct ftrace_likely_data *f, int val,
0018               int expect, int is_constant);
0019 
0020 #define likely_notrace(x)   __builtin_expect(!!(x), 1)
0021 #define unlikely_notrace(x) __builtin_expect(!!(x), 0)
0022 
0023 #define __branch_check__(x, expect, is_constant) ({         \
0024             long ______r;                   \
0025             static struct ftrace_likely_data        \
0026                 __aligned(4)                \
0027                 __section("_ftrace_annotated_branch")   \
0028                 ______f = {             \
0029                 .data.func = __func__,          \
0030                 .data.file = __FILE__,          \
0031                 .data.line = __LINE__,          \
0032             };                      \
0033             ______r = __builtin_expect(!!(x), expect);  \
0034             ftrace_likely_update(&______f, ______r,     \
0035                          expect, is_constant);  \
0036             ______r;                    \
0037         })
0038 
0039 /*
0040  * Using __builtin_constant_p(x) to ignore cases where the return
0041  * value is always the same.  This idea is taken from a similar patch
0042  * written by Daniel Walker.
0043  */
0044 # ifndef likely
0045 #  define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))
0046 # endif
0047 # ifndef unlikely
0048 #  define unlikely(x)   (__branch_check__(x, 0, __builtin_constant_p(x)))
0049 # endif
0050 
0051 #ifdef CONFIG_PROFILE_ALL_BRANCHES
0052 /*
0053  * "Define 'is'", Bill Clinton
0054  * "Define 'if'", Steven Rostedt
0055  */
0056 #define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
0057 
0058 #define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
0059 
0060 #define __trace_if_value(cond) ({           \
0061     static struct ftrace_branch_data        \
0062         __aligned(4)                \
0063         __section("_ftrace_branch")     \
0064         __if_trace = {              \
0065             .func = __func__,       \
0066             .file = __FILE__,       \
0067             .line = __LINE__,       \
0068         };                  \
0069     (cond) ?                    \
0070         (__if_trace.miss_hit[1]++,1) :      \
0071         (__if_trace.miss_hit[0]++,0);       \
0072 })
0073 
0074 #endif /* CONFIG_PROFILE_ALL_BRANCHES */
0075 
0076 #else
0077 # define likely(x)  __builtin_expect(!!(x), 1)
0078 # define unlikely(x)    __builtin_expect(!!(x), 0)
0079 # define likely_notrace(x)  likely(x)
0080 # define unlikely_notrace(x)    unlikely(x)
0081 #endif
0082 
0083 /* Optimization barrier */
0084 #ifndef barrier
0085 /* The "volatile" is due to gcc bugs */
0086 # define barrier() __asm__ __volatile__("": : :"memory")
0087 #endif
0088 
0089 #ifndef barrier_data
0090 /*
0091  * This version is i.e. to prevent dead stores elimination on @ptr
0092  * where gcc and llvm may behave differently when otherwise using
0093  * normal barrier(): while gcc behavior gets along with a normal
0094  * barrier(), llvm needs an explicit input variable to be assumed
0095  * clobbered. The issue is as follows: while the inline asm might
0096  * access any memory it wants, the compiler could have fit all of
0097  * @ptr into memory registers instead, and since @ptr never escaped
0098  * from that, it proved that the inline asm wasn't touching any of
0099  * it. This version works well with both compilers, i.e. we're telling
0100  * the compiler that the inline asm absolutely may see the contents
0101  * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495
0102  */
0103 # define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
0104 #endif
0105 
0106 /* workaround for GCC PR82365 if needed */
0107 #ifndef barrier_before_unreachable
0108 # define barrier_before_unreachable() do { } while (0)
0109 #endif
0110 
0111 /* Unreachable code */
0112 #ifdef CONFIG_OBJTOOL
0113 /*
0114  * These macros help objtool understand GCC code flow for unreachable code.
0115  * The __COUNTER__ based labels are a hack to make each instance of the macros
0116  * unique, to convince GCC not to merge duplicate inline asm statements.
0117  */
0118 #define __stringify_label(n) #n
0119 
0120 #define __annotate_unreachable(c) ({                    \
0121     asm volatile(__stringify_label(c) ":\n\t"           \
0122              ".pushsection .discard.unreachable\n\t"        \
0123              ".long " __stringify_label(c) "b - .\n\t"      \
0124              ".popsection\n\t" : : "i" (c));            \
0125 })
0126 #define annotate_unreachable() __annotate_unreachable(__COUNTER__)
0127 
0128 /* Annotate a C jump table to allow objtool to follow the code flow */
0129 #define __annotate_jump_table __section(".rodata..c_jump_table")
0130 
0131 #else /* !CONFIG_OBJTOOL */
0132 #define annotate_unreachable()
0133 #define __annotate_jump_table
0134 #endif /* CONFIG_OBJTOOL */
0135 
0136 #ifndef unreachable
0137 # define unreachable() do {     \
0138     annotate_unreachable();     \
0139     __builtin_unreachable();    \
0140 } while (0)
0141 #endif
0142 
0143 /*
0144  * KENTRY - kernel entry point
0145  * This can be used to annotate symbols (functions or data) that are used
0146  * without their linker symbol being referenced explicitly. For example,
0147  * interrupt vector handlers, or functions in the kernel image that are found
0148  * programatically.
0149  *
0150  * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those
0151  * are handled in their own way (with KEEP() in linker scripts).
0152  *
0153  * KENTRY can be avoided if the symbols in question are marked as KEEP() in the
0154  * linker script. For example an architecture could KEEP() its entire
0155  * boot/exception vector code rather than annotate each function and data.
0156  */
0157 #ifndef KENTRY
0158 # define KENTRY(sym)                        \
0159     extern typeof(sym) sym;                 \
0160     static const unsigned long __kentry_##sym       \
0161     __used                          \
0162     __attribute__((__section__("___kentry+" #sym)))     \
0163     = (unsigned long)&sym;
0164 #endif
0165 
0166 #ifndef RELOC_HIDE
0167 # define RELOC_HIDE(ptr, off)                   \
0168   ({ unsigned long __ptr;                   \
0169      __ptr = (unsigned long) (ptr);             \
0170     (typeof(ptr)) (__ptr + (off)); })
0171 #endif
0172 
0173 #define absolute_pointer(val)   RELOC_HIDE((void *)(val), 0)
0174 
0175 #ifndef OPTIMIZER_HIDE_VAR
0176 /* Make the optimizer believe the variable can be manipulated arbitrarily. */
0177 #define OPTIMIZER_HIDE_VAR(var)                     \
0178     __asm__ ("" : "=r" (var) : "0" (var))
0179 #endif
0180 
0181 /* Not-quite-unique ID. */
0182 #ifndef __UNIQUE_ID
0183 # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
0184 #endif
0185 
0186 /**
0187  * data_race - mark an expression as containing intentional data races
0188  *
0189  * This data_race() macro is useful for situations in which data races
0190  * should be forgiven.  One example is diagnostic code that accesses
0191  * shared variables but is not a part of the core synchronization design.
0192  *
0193  * This macro *does not* affect normal code generation, but is a hint
0194  * to tooling that data races here are to be ignored.
0195  */
0196 #define data_race(expr)                         \
0197 ({                                  \
0198     __unqual_scalar_typeof(({ expr; })) __v = ({            \
0199         __kcsan_disable_current();              \
0200         expr;                           \
0201     });                             \
0202     __kcsan_enable_current();                   \
0203     __v;                                \
0204 })
0205 
0206 /*
0207  * With CONFIG_CFI_CLANG, the compiler replaces function addresses in
0208  * instrumented C code with jump table addresses. Architectures that
0209  * support CFI can define this macro to return the actual function address
0210  * when needed.
0211  */
0212 #ifndef function_nocfi
0213 #define function_nocfi(x) (x)
0214 #endif
0215 
0216 #endif /* __KERNEL__ */
0217 
0218 /*
0219  * Force the compiler to emit 'sym' as a symbol, so that we can reference
0220  * it from inline assembler. Necessary in case 'sym' could be inlined
0221  * otherwise, or eliminated entirely due to lack of references that are
0222  * visible to the compiler.
0223  */
0224 #define __ADDRESSABLE(sym) \
0225     static void * __section(".discard.addressable") __used \
0226         __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym;
0227 
0228 /**
0229  * offset_to_ptr - convert a relative memory offset to an absolute pointer
0230  * @off:    the address of the 32-bit offset value
0231  */
0232 static inline void *offset_to_ptr(const int *off)
0233 {
0234     return (void *)((unsigned long)off + *off);
0235 }
0236 
0237 #endif /* __ASSEMBLY__ */
0238 
0239 /* &a[0] degrades to a pointer: a different type from an array */
0240 #define __must_be_array(a)  BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
0241 
0242 /*
0243  * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
0244  * bool and also pointer types.
0245  */
0246 #define is_signed_type(type) (((type)(-1)) < (__force type)1)
0247 
0248 /*
0249  * This is needed in functions which generate the stack canary, see
0250  * arch/x86/kernel/smpboot.c::start_secondary() for an example.
0251  */
0252 #define prevent_tail_call_optimization()    mb()
0253 
0254 #include <asm/rwonce.h>
0255 
0256 #endif /* __LINUX_COMPILER_H */