1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LINUX_COMPILER_TYPES_H
3 #define __LINUX_COMPILER_TYPES_H
4
5 #ifndef __ASSEMBLY__
6
7 #include "compiler_attributes.h"
8
9 /*
10 * Skipped when running bindgen due to a libclang issue;
11 * see https://github.com/rust-lang/rust-bindgen/issues/2244.
12 */
13 #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
14 __has_attribute(btf_type_tag) && !defined(__BINDGEN__)
15 #define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
16 #else
17 #define BTF_TYPE_TAG(value) /* nothing */
18 #endif
19
20 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
21 #define ___PASTE(a, b) a##b
22 #define __PASTE(a, b) ___PASTE(a, b)
23
24 /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
25 #ifdef __CHECKER__
26 /* address spaces */
27 #define __kernel __attribute__((address_space(0)))
28 #define __user __attribute__((noderef, address_space(__user)))
29 #define __iomem __attribute__((noderef, address_space(__iomem)))
30 #define __percpu __attribute__((noderef, address_space(__percpu)))
31 #define __rcu __attribute__((noderef, address_space(__rcu)))
__chk_user_ptr(const volatile void __user * ptr)32 static inline void __chk_user_ptr(const volatile void __user *ptr)
33 {
34 }
__chk_io_ptr(const volatile void __iomem * ptr)35 static inline void __chk_io_ptr(const volatile void __iomem *ptr)
36 {
37 }
38 /* context/locking */
39 #define __must_hold(x) __attribute__((context(x, 1, 1)))
40 #define __acquires(x) __attribute__((context(x, 0, 1)))
41 #define __cond_acquires(x) __attribute__((context(x, 0, -1)))
42 #define __releases(x) __attribute__((context(x, 1, 0)))
43 #define __acquire(x) __context__(x, 1)
44 #define __release(x) __context__(x, -1)
45 #define __cond_lock(x, c) \
46 ((c) ? ({ \
47 __acquire(x); \
48 1; \
49 }) : \
50 0)
51 /* other */
52 #define __force __attribute__((force))
53 #define __nocast __attribute__((nocast))
54 #define __safe __attribute__((safe))
55 #define __private __attribute__((noderef))
56 #define ACCESS_PRIVATE(p, member) \
57 (*((typeof((p)->member) __force *)&(p)->member))
58 #else /* __CHECKER__ */
59 /* address spaces */
60 #define __kernel
61 #ifdef STRUCTLEAK_PLUGIN
62 #define __user __attribute__((user))
63 #else
64 #define __user BTF_TYPE_TAG(user)
65 #endif
66 #define __iomem
67 #define __percpu BTF_TYPE_TAG(percpu)
68 #define __rcu BTF_TYPE_TAG(rcu)
69
70 #define __chk_user_ptr(x) (void)0
71 #define __chk_io_ptr(x) (void)0
72 /* context/locking */
73 #define __must_hold(x)
74 #define __acquires(x)
75 #define __cond_acquires(x)
76 #define __releases(x)
77 #define __acquire(x) (void)0
78 #define __release(x) (void)0
79 #define __cond_lock(x, c) (c)
80 /* other */
81 #define __force
82 #define __nocast
83 #define __safe
84 #define __private
85 #define ACCESS_PRIVATE(p, member) ((p)->member)
86 #define __builtin_warning(x, y...) (1)
87 #endif /* __CHECKER__ */
88
89 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
90 #define ___PASTE(a, b) a##b
91 #define __PASTE(a, b) ___PASTE(a, b)
92
93 #endif /* __ASSEMBLY__ */
94
95 /*
96 * The below symbols may be defined for one or more, but not ALL, of the above
97 * compilers. We don't consider that to be an error, so set them to nothing.
98 * For example, some of them are for compiler specific plugins.
99 */
100 #ifndef __latent_entropy
101 #define __latent_entropy
102 #endif
103
104 #if defined(RANDSTRUCT) && !defined(__CHECKER__)
105 #define __randomize_layout __designated_init __attribute__((randomize_layout))
106 #define __no_randomize_layout __attribute__((no_randomize_layout))
107 /* This anon struct can add padding, so only enable it under randstruct. */
108 #define randomized_struct_fields_start struct {
109 #define randomized_struct_fields_end \
110 } \
111 __randomize_layout;
112 #else
113 #define __randomize_layout __designated_init
114 #define __no_randomize_layout
115 #define randomized_struct_fields_start
116 #define randomized_struct_fields_end
117 #endif
118
119 #ifndef __noscs
120 #define __noscs
121 #endif
122
123 #ifndef __nocfi
124 #define __nocfi
125 #endif
126
127 #if defined(CC_USING_HOTPATCH)
128 #define notrace __attribute__((hotpatch(0, 0)))
129 #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
130 #define notrace __attribute__((patchable_function_entry(0, 0)))
131 #else
132 #define notrace __attribute__((__no_instrument_function__))
133 #endif
134
135
136
137 /*
138 * gcc provides both __inline__ and __inline as alternate spellings of
139 * the inline keyword, though the latter is undocumented. New kernel
140 * code should only use the inline spelling, but some existing code
141 * uses __inline__. Since we #define inline above, to ensure
142 * __inline__ has the same semantics, we need this #define.
143 *
144 * However, the spelling __inline is strictly reserved for referring
145 * to the bare keyword.
146 */
147 #define __inline__ inline
148
149 /*
150 * GCC does not warn about unused static inline functions for -Wunused-function.
151 * Suppress the warning in clang as well by using __maybe_unused, but enable it
152 * for W=1 build. This will allow clang to find unused functions. Remove the
153 * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
154 */
155 #ifdef KBUILD_EXTRA_WARN1
156 #define __inline_maybe_unused
157 #else
158 #define __inline_maybe_unused __maybe_unused
159 #endif
160
161 /*
162 * Rather then using noinline to prevent stack consumption, use
163 * noinline_for_stack instead. For documentation reasons.
164 */
165 #define noinline_for_stack noinline
166
167 /*
168 * Prefer gnu_inline, so that extern inline functions do not emit an
169 * externally visible function. This makes extern inline behave as per gnu89
170 * semantics rather than c99. This prevents multiple symbol definition errors
171 * of extern inline functions at link time.
172 * A lot of inline functions can cause havoc with function tracing.
173 */
174 #define inline inline __gnu_inline __inline_maybe_unused notrace
175
176 /*
177 * Any place that could be marked with the "alloc_size" attribute is also
178 * a place to be marked with the "malloc" attribute, except those that may
179 * be performing a _reallocation_, as that may alias the existing pointer.
180 * For these, use __realloc_size().
181 */
182 #ifdef __alloc_size__
183 #define __alloc_size(x, ...) __alloc_size__(x, ##__VA_ARGS__) __malloc
184 #define __realloc_size(x, ...) __alloc_size__(x, ##__VA_ARGS__)
185 #else
186 #define __alloc_size(x, ...) __malloc
187 #define __realloc_size(x, ...)
188 #endif
189
190 #ifndef asm_volatile_goto
191 #define asm_volatile_goto(x...) asm goto(x)
192 #endif
193
194 #ifdef CONFIG_CC_HAS_ASM_INLINE
195 #define asm_inline asm __inline
196 #else
197 #define asm_inline asm
198 #endif
199
200 /* Are two types/vars the same type (ignoring qualifiers)? */
201 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
202
203 /*
204 * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
205 * non-scalar types unchanged.
206 */
207 /*
208 * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
209 * is not type-compatible with 'signed char', and we define a separate case.
210 */
211 #define __scalar_type_to_expr_cases(type) \
212 unsigned type : (unsigned type)0, signed type : (signed type)0
213
214 #define __unqual_scalar_typeof(x) \
215 typeof(_Generic((x), char \
216 : (char)0, __scalar_type_to_expr_cases(char), \
217 __scalar_type_to_expr_cases(short), \
218 __scalar_type_to_expr_cases(int), \
219 __scalar_type_to_expr_cases(long), \
220 __scalar_type_to_expr_cases(long long), default \
221 : (x)))
222
223 /* Is this type a native word size -- useful for atomic operations */
224 #define __native_word(t) \
225 (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
226 sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
227
228 #ifdef __OPTIMIZE__
229 #define __compiletime_assert(condition, msg, prefix, suffix) \
230 do { \
231 /* \
232 * __noreturn is needed to give the compiler enough \
233 * information to avoid certain possibly-uninitialized \
234 * warnings (regardless of the build failing). \
235 */ \
236 __noreturn extern void prefix##suffix(void) \
237 __compiletime_error(msg); \
238 if (!(condition)) \
239 prefix##suffix(); \
240 } while (0)
241 #else
242 #define __compiletime_assert(condition, msg, prefix, suffix) \
243 do { \
244 } while (0)
245 #endif
246
247 #define _compiletime_assert(condition, msg, prefix, suffix) \
248 __compiletime_assert(condition, msg, prefix, suffix)
249
250 /**
251 * compiletime_assert - break build and emit msg if condition is false
252 * @condition: a compile-time constant condition to check
253 * @msg: a message to emit if condition is false
254 *
255 * In tradition of POSIX assert, this macro will break the build if the
256 * supplied condition is *false*, emitting the supplied error message if the
257 * compiler has support to do so.
258 */
259 #define compiletime_assert(condition, msg) \
260 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
261
262 #define compiletime_assert_atomic_type(t) \
263 compiletime_assert( \
264 __native_word(t), \
265 "Need native word sized stores/loads for atomicity.")
266
267 /* Helpers for emitting diagnostics in pragmas. */
268 #ifndef __diag
269 #define __diag(string)
270 #endif
271
272 #ifndef __diag_GCC
273 #define __diag_GCC(version, severity, string)
274 #endif
275
276 #define __diag_push() __diag(push)
277 #define __diag_pop() __diag(pop)
278
279 #define __diag_ignore(compiler, version, option, comment) \
280 __diag_##compiler(version, ignore, option)
281 #define __diag_warn(compiler, version, option, comment) \
282 __diag_##compiler(version, warn, option)
283 #define __diag_error(compiler, version, option, comment) \
284 __diag_##compiler(version, error, option)
285
286 #ifndef __diag_ignore_all
287 #define __diag_ignore_all(option, comment)
288 #endif
289
290 #endif /* __LINUX_COMPILER_TYPES_H */
291