0001
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
0013
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
0041
0042
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
0054
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
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
0084 #ifndef barrier
0085
0086 # define barrier() __asm__ __volatile__("": : :"memory")
0087 #endif
0088
0089 #ifndef barrier_data
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 # define barrier_data(ptr) __asm__ __volatile__("": :"r"(ptr) :"memory")
0104 #endif
0105
0106
0107 #ifndef barrier_before_unreachable
0108 # define barrier_before_unreachable() do { } while (0)
0109 #endif
0110
0111
0112 #ifdef CONFIG_OBJTOOL
0113
0114
0115
0116
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
0129 #define __annotate_jump_table __section(".rodata..c_jump_table")
0130
0131 #else
0132 #define annotate_unreachable()
0133 #define __annotate_jump_table
0134 #endif
0135
0136 #ifndef unreachable
0137 # define unreachable() do { \
0138 annotate_unreachable(); \
0139 __builtin_unreachable(); \
0140 } while (0)
0141 #endif
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
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
0177 #define OPTIMIZER_HIDE_VAR(var) \
0178 __asm__ ("" : "=r" (var) : "0" (var))
0179 #endif
0180
0181
0182 #ifndef __UNIQUE_ID
0183 # define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __LINE__)
0184 #endif
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
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
0208
0209
0210
0211
0212 #ifndef function_nocfi
0213 #define function_nocfi(x) (x)
0214 #endif
0215
0216 #endif
0217
0218
0219
0220
0221
0222
0223
0224 #define __ADDRESSABLE(sym) \
0225 static void * __section(".discard.addressable") __used \
0226 __UNIQUE_ID(__PASTE(__addressable_,sym)) = (void *)&sym;
0227
0228
0229
0230
0231
0232 static inline void *offset_to_ptr(const int *off)
0233 {
0234 return (void *)((unsigned long)off + *off);
0235 }
0236
0237 #endif
0238
0239
0240 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
0241
0242
0243
0244
0245
0246 #define is_signed_type(type) (((type)(-1)) < (__force type)1)
0247
0248
0249
0250
0251
0252 #define prevent_tail_call_optimization() mb()
0253
0254 #include <asm/rwonce.h>
0255
0256 #endif