320 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef __LINUX_COMPILER_H3#define __LINUX_COMPILER_H4 5#include <linux/compiler_types.h>6 7#ifndef __ASSEMBLY__8 9#ifdef __KERNEL__10 11/*12 * Note: DISABLE_BRANCH_PROFILING can be used by special lowlevel code13 * to disable branch tracing on a per file basis.14 */15void ftrace_likely_update(struct ftrace_likely_data *f, int val,16 int expect, int is_constant);17#if defined(CONFIG_TRACE_BRANCH_PROFILING) \18 && !defined(DISABLE_BRANCH_PROFILING) && !defined(__CHECKER__)19#define likely_notrace(x) __builtin_expect(!!(x), 1)20#define unlikely_notrace(x) __builtin_expect(!!(x), 0)21 22#define __branch_check__(x, expect, is_constant) ({ \23 long ______r; \24 static struct ftrace_likely_data \25 __aligned(4) \26 __section("_ftrace_annotated_branch") \27 ______f = { \28 .data.func = __func__, \29 .data.file = __FILE__, \30 .data.line = __LINE__, \31 }; \32 ______r = __builtin_expect(!!(x), expect); \33 ftrace_likely_update(&______f, ______r, \34 expect, is_constant); \35 ______r; \36 })37 38/*39 * Using __builtin_constant_p(x) to ignore cases where the return40 * value is always the same. This idea is taken from a similar patch41 * written by Daniel Walker.42 */43# ifndef likely44# define likely(x) (__branch_check__(x, 1, __builtin_constant_p(x)))45# endif46# ifndef unlikely47# define unlikely(x) (__branch_check__(x, 0, __builtin_constant_p(x)))48# endif49 50#ifdef CONFIG_PROFILE_ALL_BRANCHES51/*52 * "Define 'is'", Bill Clinton53 * "Define 'if'", Steven Rostedt54 */55#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )56 57#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))58 59#define __trace_if_value(cond) ({ \60 static struct ftrace_branch_data \61 __aligned(4) \62 __section("_ftrace_branch") \63 __if_trace = { \64 .func = __func__, \65 .file = __FILE__, \66 .line = __LINE__, \67 }; \68 (cond) ? \69 (__if_trace.miss_hit[1]++,1) : \70 (__if_trace.miss_hit[0]++,0); \71})72 73#endif /* CONFIG_PROFILE_ALL_BRANCHES */74 75#else76# define likely(x) __builtin_expect(!!(x), 1)77# define unlikely(x) __builtin_expect(!!(x), 0)78# define likely_notrace(x) likely(x)79# define unlikely_notrace(x) unlikely(x)80#endif81 82/* Optimization barrier */83#ifndef barrier84/* The "volatile" is due to gcc bugs */85# define barrier() __asm__ __volatile__("": : :"memory")86#endif87 88#ifndef barrier_data89/*90 * This version is i.e. to prevent dead stores elimination on @ptr91 * where gcc and llvm may behave differently when otherwise using92 * normal barrier(): while gcc behavior gets along with a normal93 * barrier(), llvm needs an explicit input variable to be assumed94 * clobbered. The issue is as follows: while the inline asm might95 * access any memory it wants, the compiler could have fit all of96 * @ptr into memory registers instead, and since @ptr never escaped97 * from that, it proved that the inline asm wasn't touching any of98 * it. This version works well with both compilers, i.e. we're telling99 * the compiler that the inline asm absolutely may see the contents100 * of @ptr. See also: https://llvm.org/bugs/show_bug.cgi?id=15495101 */102# define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")103#endif104 105/* workaround for GCC PR82365 if needed */106#ifndef barrier_before_unreachable107# define barrier_before_unreachable() do { } while (0)108#endif109 110/* Unreachable code */111#ifdef CONFIG_OBJTOOL112/*113 * These macros help objtool understand GCC code flow for unreachable code.114 * The __COUNTER__ based labels are a hack to make each instance of the macros115 * unique, to convince GCC not to merge duplicate inline asm statements.116 */117#define __stringify_label(n) #n118 119#define __annotate_reachable(c) ({ \120 asm volatile(__stringify_label(c) ":\n\t" \121 ".pushsection .discard.reachable\n\t" \122 ".long " __stringify_label(c) "b - .\n\t" \123 ".popsection\n\t"); \124})125#define annotate_reachable() __annotate_reachable(__COUNTER__)126 127#define __annotate_unreachable(c) ({ \128 asm volatile(__stringify_label(c) ":\n\t" \129 ".pushsection .discard.unreachable\n\t" \130 ".long " __stringify_label(c) "b - .\n\t" \131 ".popsection\n\t" : : "i" (c)); \132})133#define annotate_unreachable() __annotate_unreachable(__COUNTER__)134 135/* Annotate a C jump table to allow objtool to follow the code flow */136#define __annotate_jump_table __section(".rodata..c_jump_table,\"a\",@progbits #")137 138#else /* !CONFIG_OBJTOOL */139#define annotate_reachable()140#define annotate_unreachable()141#define __annotate_jump_table142#endif /* CONFIG_OBJTOOL */143 144#ifndef unreachable145# define unreachable() do { \146 annotate_unreachable(); \147 __builtin_unreachable(); \148} while (0)149#endif150 151/*152 * KENTRY - kernel entry point153 * This can be used to annotate symbols (functions or data) that are used154 * without their linker symbol being referenced explicitly. For example,155 * interrupt vector handlers, or functions in the kernel image that are found156 * programatically.157 *158 * Not required for symbols exported with EXPORT_SYMBOL, or initcalls. Those159 * are handled in their own way (with KEEP() in linker scripts).160 *161 * KENTRY can be avoided if the symbols in question are marked as KEEP() in the162 * linker script. For example an architecture could KEEP() its entire163 * boot/exception vector code rather than annotate each function and data.164 */165#ifndef KENTRY166# define KENTRY(sym) \167 extern typeof(sym) sym; \168 static const unsigned long __kentry_##sym \169 __used \170 __attribute__((__section__("___kentry+" #sym))) \171 = (unsigned long)&sym;172#endif173 174#ifndef RELOC_HIDE175# define RELOC_HIDE(ptr, off) \176 ({ unsigned long __ptr; \177 __ptr = (unsigned long) (ptr); \178 (typeof(ptr)) (__ptr + (off)); })179#endif180 181#define absolute_pointer(val) RELOC_HIDE((void *)(val), 0)182 183#ifndef OPTIMIZER_HIDE_VAR184/* Make the optimizer believe the variable can be manipulated arbitrarily. */185#define OPTIMIZER_HIDE_VAR(var) \186 __asm__ ("" : "=r" (var) : "0" (var))187#endif188 189#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)190 191/**192 * data_race - mark an expression as containing intentional data races193 *194 * This data_race() macro is useful for situations in which data races195 * should be forgiven. One example is diagnostic code that accesses196 * shared variables but is not a part of the core synchronization design.197 * For example, if accesses to a given variable are protected by a lock,198 * except for diagnostic code, then the accesses under the lock should199 * be plain C-language accesses and those in the diagnostic code should200 * use data_race(). This way, KCSAN will complain if buggy lockless201 * accesses to that variable are introduced, even if the buggy accesses202 * are protected by READ_ONCE() or WRITE_ONCE().203 *204 * This macro *does not* affect normal code generation, but is a hint205 * to tooling that data races here are to be ignored. If the access must206 * be atomic *and* KCSAN should ignore the access, use both data_race()207 * and READ_ONCE(), for example, data_race(READ_ONCE(x)).208 */209#define data_race(expr) \210({ \211 __kcsan_disable_current(); \212 __auto_type __v = (expr); \213 __kcsan_enable_current(); \214 __v; \215})216 217#endif /* __KERNEL__ */218 219/*220 * Force the compiler to emit 'sym' as a symbol, so that we can reference221 * it from inline assembler. Necessary in case 'sym' could be inlined222 * otherwise, or eliminated entirely due to lack of references that are223 * visible to the compiler.224 */225#define ___ADDRESSABLE(sym, __attrs) \226 static void * __used __attrs \227 __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)(uintptr_t)&sym;228#define __ADDRESSABLE(sym) \229 ___ADDRESSABLE(sym, __section(".discard.addressable"))230 231/**232 * offset_to_ptr - convert a relative memory offset to an absolute pointer233 * @off: the address of the 32-bit offset value234 */235static inline void *offset_to_ptr(const int *off)236{237 return (void *)((unsigned long)off + *off);238}239 240#endif /* __ASSEMBLY__ */241 242/* &a[0] degrades to a pointer: a different type from an array */243#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))244 245/* Require C Strings (i.e. NUL-terminated) lack the "nonstring" attribute. */246#define __must_be_cstr(p) BUILD_BUG_ON_ZERO(__annotated(p, nonstring))247 248/*249 * This returns a constant expression while determining if an argument is250 * a constant expression, most importantly without evaluating the argument.251 * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>252 *253 * Details:254 * - sizeof() return an integer constant expression, and does not evaluate255 * the value of its operand; it only examines the type of its operand.256 * - The results of comparing two integer constant expressions is also257 * an integer constant expression.258 * - The first literal "8" isn't important. It could be any literal value.259 * - The second literal "8" is to avoid warnings about unaligned pointers;260 * this could otherwise just be "1".261 * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit262 * architectures.263 * - The C Standard defines "null pointer constant", "(void *)0", as264 * distinct from other void pointers.265 * - If (x) is an integer constant expression, then the "* 0l" resolves266 * it into an integer constant expression of value 0. Since it is cast to267 * "void *", this makes the second operand a null pointer constant.268 * - If (x) is not an integer constant expression, then the second operand269 * resolves to a void pointer (but not a null pointer constant: the value270 * is not an integer constant 0).271 * - The conditional operator's third operand, "(int *)8", is an object272 * pointer (to type "int").273 * - The behavior (including the return type) of the conditional operator274 * ("operand1 ? operand2 : operand3") depends on the kind of expressions275 * given for the second and third operands. This is the central mechanism276 * of the macro:277 * - When one operand is a null pointer constant (i.e. when x is an integer278 * constant expression) and the other is an object pointer (i.e. our279 * third operand), the conditional operator returns the type of the280 * object pointer operand (i.e. "int *"). Here, within the sizeof(), we281 * would then get:282 * sizeof(*((int *)(...)) == sizeof(int) == 4283 * - When one operand is a void pointer (i.e. when x is not an integer284 * constant expression) and the other is an object pointer (i.e. our285 * third operand), the conditional operator returns a "void *" type.286 * Here, within the sizeof(), we would then get:287 * sizeof(*((void *)(...)) == sizeof(void) == 1288 * - The equality comparison to "sizeof(int)" therefore depends on (x):289 * sizeof(int) == sizeof(int) (x) was a constant expression290 * sizeof(int) != sizeof(void) (x) was not a constant expression291 */292#define __is_constexpr(x) \293 (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))294 295/*296 * Whether 'type' is a signed type or an unsigned type. Supports scalar types,297 * bool and also pointer types.298 */299#define is_signed_type(type) (((type)(-1)) < (__force type)1)300#define is_unsigned_type(type) (!is_signed_type(type))301 302/*303 * Useful shorthand for "is this condition known at compile-time?"304 *305 * Note that the condition may involve non-constant values,306 * but the compiler may know enough about the details of the307 * values to determine that the condition is statically true.308 */309#define statically_true(x) (__builtin_constant_p(x) && (x))310 311/*312 * This is needed in functions which generate the stack canary, see313 * arch/x86/kernel/smpboot.c::start_secondary() for an example.314 */315#define prevent_tail_call_optimization() mb()316 317#include <asm/rwonce.h>318 319#endif /* __LINUX_COMPILER_H */320