166dbeef9SMiguel Ojeda /* SPDX-License-Identifier: GPL-2.0 */
2d1515582SWill Deacon #ifndef __LINUX_COMPILER_TYPES_H
3d1515582SWill Deacon #define __LINUX_COMPILER_TYPES_H
4d1515582SWill Deacon
5d1515582SWill Deacon #ifndef __ASSEMBLY__
6d1515582SWill Deacon
72f7ab126SMiguel Ojeda /*
82f7ab126SMiguel Ojeda * Skipped when running bindgen due to a libclang issue;
92f7ab126SMiguel Ojeda * see https://github.com/rust-lang/rust-bindgen/issues/2244.
102f7ab126SMiguel Ojeda */
116789ab96SHao Luo #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
122f7ab126SMiguel Ojeda __has_attribute(btf_type_tag) && !defined(__BINDGEN__)
136789ab96SHao Luo # define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
146789ab96SHao Luo #else
156789ab96SHao Luo # define BTF_TYPE_TAG(value) /* nothing */
166789ab96SHao Luo #endif
176789ab96SHao Luo
18179fd6baSBjorn Helgaas /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
19d1515582SWill Deacon #ifdef __CHECKER__
2025fd529cSLuc Van Oostenryck /* address spaces */
21d1515582SWill Deacon # define __kernel __attribute__((address_space(0)))
22670d0a4bSLuc Van Oostenryck # define __user __attribute__((noderef, address_space(__user)))
23670d0a4bSLuc Van Oostenryck # define __iomem __attribute__((noderef, address_space(__iomem)))
2425fd529cSLuc Van Oostenryck # define __percpu __attribute__((noderef, address_space(__percpu)))
2525fd529cSLuc Van Oostenryck # define __rcu __attribute__((noderef, address_space(__rcu)))
__chk_user_ptr(const volatile void __user * ptr)26e5fc436fSLuc Van Oostenryck static inline void __chk_user_ptr(const volatile void __user *ptr) { }
__chk_io_ptr(const volatile void __iomem * ptr)27e5fc436fSLuc Van Oostenryck static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
2825fd529cSLuc Van Oostenryck /* context/locking */
29d1515582SWill Deacon # define __must_hold(x) __attribute__((context(x,1,1)))
30d1515582SWill Deacon # define __acquires(x) __attribute__((context(x,0,1)))
314a557a5dSLinus Torvalds # define __cond_acquires(x) __attribute__((context(x,0,-1)))
32d1515582SWill Deacon # define __releases(x) __attribute__((context(x,1,0)))
33d1515582SWill Deacon # define __acquire(x) __context__(x,1)
34d1515582SWill Deacon # define __release(x) __context__(x,-1)
35d1515582SWill Deacon # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0)
3625fd529cSLuc Van Oostenryck /* other */
3725fd529cSLuc Van Oostenryck # define __force __attribute__((force))
3825fd529cSLuc Van Oostenryck # define __nocast __attribute__((nocast))
3925fd529cSLuc Van Oostenryck # define __safe __attribute__((safe))
40d1515582SWill Deacon # define __private __attribute__((noderef))
41d1515582SWill Deacon # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
42d1515582SWill Deacon #else /* __CHECKER__ */
4325fd529cSLuc Van Oostenryck /* address spaces */
4425fd529cSLuc Van Oostenryck # define __kernel
45d1515582SWill Deacon # ifdef STRUCTLEAK_PLUGIN
46d1515582SWill Deacon # define __user __attribute__((user))
47d1515582SWill Deacon # else
486789ab96SHao Luo # define __user BTF_TYPE_TAG(user)
49d1515582SWill Deacon # endif
50d1515582SWill Deacon # define __iomem
516789ab96SHao Luo # define __percpu BTF_TYPE_TAG(percpu)
525a0f663fSYonghong Song # define __rcu BTF_TYPE_TAG(rcu)
535a0f663fSYonghong Song
54d1515582SWill Deacon # define __chk_user_ptr(x) (void)0
55d1515582SWill Deacon # define __chk_io_ptr(x) (void)0
5625fd529cSLuc Van Oostenryck /* context/locking */
57d1515582SWill Deacon # define __must_hold(x)
58d1515582SWill Deacon # define __acquires(x)
594a557a5dSLinus Torvalds # define __cond_acquires(x)
60d1515582SWill Deacon # define __releases(x)
61d1515582SWill Deacon # define __acquire(x) (void)0
62d1515582SWill Deacon # define __release(x) (void)0
63d1515582SWill Deacon # define __cond_lock(x,c) (c)
6425fd529cSLuc Van Oostenryck /* other */
6525fd529cSLuc Van Oostenryck # define __force
6625fd529cSLuc Van Oostenryck # define __nocast
6725fd529cSLuc Van Oostenryck # define __safe
68d1515582SWill Deacon # define __private
69d1515582SWill Deacon # define ACCESS_PRIVATE(p, member) ((p)->member)
7025fd529cSLuc Van Oostenryck # define __builtin_warning(x, y...) (1)
71d1515582SWill Deacon #endif /* __CHECKER__ */
72d1515582SWill Deacon
73d1515582SWill Deacon /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
74d1515582SWill Deacon #define ___PASTE(a,b) a##b
75d1515582SWill Deacon #define __PASTE(a,b) ___PASTE(a,b)
76d1515582SWill Deacon
77d1515582SWill Deacon #ifdef __KERNEL__
78d1515582SWill Deacon
79a3f8a30fSMiguel Ojeda /* Attributes */
80a3f8a30fSMiguel Ojeda #include <linux/compiler_attributes.h>
81a3f8a30fSMiguel Ojeda
82c27cd083SMark Rutland #if CONFIG_FUNCTION_ALIGNMENT > 0
83c27cd083SMark Rutland #define __function_aligned __aligned(CONFIG_FUNCTION_ALIGNMENT)
84c27cd083SMark Rutland #else
85c27cd083SMark Rutland #define __function_aligned
86c27cd083SMark Rutland #endif
87c27cd083SMark Rutland
88c27cd083SMark Rutland /*
89c27cd083SMark Rutland * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-cold-function-attribute
90c27cd083SMark Rutland * gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
91c27cd083SMark Rutland *
92c27cd083SMark Rutland * When -falign-functions=N is in use, we must avoid the cold attribute as
93c27cd083SMark Rutland * contemporary versions of GCC drop the alignment for cold functions. Worse,
94c27cd083SMark Rutland * GCC can implicitly mark callees of cold functions as cold themselves, so
95c27cd083SMark Rutland * it's not sufficient to add __function_aligned here as that will not ensure
96c27cd083SMark Rutland * that callees are correctly aligned.
97c27cd083SMark Rutland *
98c27cd083SMark Rutland * See:
99c27cd083SMark Rutland *
100c27cd083SMark Rutland * https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
101c27cd083SMark Rutland * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
102c27cd083SMark Rutland */
103c27cd083SMark Rutland #if !defined(CONFIG_CC_IS_GCC) || (CONFIG_FUNCTION_ALIGNMENT == 0)
104c27cd083SMark Rutland #define __cold __attribute__((__cold__))
105c27cd083SMark Rutland #else
106c27cd083SMark Rutland #define __cold
107c27cd083SMark Rutland #endif
108c27cd083SMark Rutland
1097a0fd5e1SMarco Elver /*
1107a0fd5e1SMarco Elver * On x86-64 and arm64 targets, __preserve_most changes the calling convention
1117a0fd5e1SMarco Elver * of a function to make the code in the caller as unintrusive as possible. This
1127a0fd5e1SMarco Elver * convention behaves identically to the C calling convention on how arguments
1137a0fd5e1SMarco Elver * and return values are passed, but uses a different set of caller- and callee-
1147a0fd5e1SMarco Elver * saved registers.
1157a0fd5e1SMarco Elver *
1167a0fd5e1SMarco Elver * The purpose is to alleviates the burden of saving and recovering a large
1177a0fd5e1SMarco Elver * register set before and after the call in the caller. This is beneficial for
1187a0fd5e1SMarco Elver * rarely taken slow paths, such as error-reporting functions that may be called
1197a0fd5e1SMarco Elver * from hot paths.
1207a0fd5e1SMarco Elver *
1217a0fd5e1SMarco Elver * Note: This may conflict with instrumentation inserted on function entry which
1227a0fd5e1SMarco Elver * does not use __preserve_most or equivalent convention (if in assembly). Since
1237a0fd5e1SMarco Elver * function tracing assumes the normal C calling convention, where the attribute
1247a0fd5e1SMarco Elver * is supported, __preserve_most implies notrace. It is recommended to restrict
1257a0fd5e1SMarco Elver * use of the attribute to functions that should or already disable tracing.
1267a0fd5e1SMarco Elver *
1277a0fd5e1SMarco Elver * Optional: not supported by gcc.
1287a0fd5e1SMarco Elver *
1297a0fd5e1SMarco Elver * clang: https://clang.llvm.org/docs/AttributeReference.html#preserve-most
1307a0fd5e1SMarco Elver */
1317a0fd5e1SMarco Elver #if __has_attribute(__preserve_most__) && (defined(CONFIG_X86_64) || defined(CONFIG_ARM64))
1327a0fd5e1SMarco Elver # define __preserve_most notrace __attribute__((__preserve_most__))
1337a0fd5e1SMarco Elver #else
1347a0fd5e1SMarco Elver # define __preserve_most
1357a0fd5e1SMarco Elver #endif
1367a0fd5e1SMarco Elver
137caabdd0fSArnd Bergmann /* Builtins */
138caabdd0fSArnd Bergmann
139caabdd0fSArnd Bergmann /*
140caabdd0fSArnd Bergmann * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21.
141caabdd0fSArnd Bergmann * In the meantime, to support gcc < 10, we implement __has_builtin
142caabdd0fSArnd Bergmann * by hand.
143caabdd0fSArnd Bergmann */
144caabdd0fSArnd Bergmann #ifndef __has_builtin
145caabdd0fSArnd Bergmann #define __has_builtin(x) (0)
146caabdd0fSArnd Bergmann #endif
147caabdd0fSArnd Bergmann
148815f0ddbSNick Desaulniers /* Compiler specific macros. */
149d1515582SWill Deacon #ifdef __clang__
150d1515582SWill Deacon #include <linux/compiler-clang.h>
151815f0ddbSNick Desaulniers #elif defined(__GNUC__)
152815f0ddbSNick Desaulniers /* The above compilers also define __GNUC__, so order is important here. */
153815f0ddbSNick Desaulniers #include <linux/compiler-gcc.h>
154815f0ddbSNick Desaulniers #else
155815f0ddbSNick Desaulniers #error "Unknown compiler"
156d1515582SWill Deacon #endif
157d1515582SWill Deacon
158d1515582SWill Deacon /*
15904f264d3SPaul Burton * Some architectures need to provide custom definitions of macros provided
16004f264d3SPaul Burton * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
16104f264d3SPaul Burton * conditionally rather than using an asm-generic wrapper in order to avoid
16204f264d3SPaul Burton * build failures if any C compilation, which will include this file via an
16304f264d3SPaul Burton * -include argument in c_flags, occurs prior to the asm-generic wrappers being
16404f264d3SPaul Burton * generated.
16504f264d3SPaul Burton */
16604f264d3SPaul Burton #ifdef CONFIG_HAVE_ARCH_COMPILER_H
16704f264d3SPaul Burton #include <asm/compiler.h>
16804f264d3SPaul Burton #endif
16904f264d3SPaul Burton
170d1515582SWill Deacon struct ftrace_branch_data {
171d1515582SWill Deacon const char *func;
172d1515582SWill Deacon const char *file;
173d1515582SWill Deacon unsigned line;
174d1515582SWill Deacon union {
175d1515582SWill Deacon struct {
176d1515582SWill Deacon unsigned long correct;
177d1515582SWill Deacon unsigned long incorrect;
178d1515582SWill Deacon };
179d1515582SWill Deacon struct {
180d1515582SWill Deacon unsigned long miss;
181d1515582SWill Deacon unsigned long hit;
182d1515582SWill Deacon };
183d1515582SWill Deacon unsigned long miss_hit[2];
184d1515582SWill Deacon };
185d1515582SWill Deacon };
186d1515582SWill Deacon
187d1515582SWill Deacon struct ftrace_likely_data {
188d1515582SWill Deacon struct ftrace_branch_data data;
189d1515582SWill Deacon unsigned long constant;
190d1515582SWill Deacon };
191d1515582SWill Deacon
19271391bddSXiaozhou Liu #if defined(CC_USING_HOTPATCH)
19371391bddSXiaozhou Liu #define notrace __attribute__((hotpatch(0, 0)))
1942809b392SSven Schnelle #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
1952809b392SSven Schnelle #define notrace __attribute__((patchable_function_entry(0, 0)))
19671391bddSXiaozhou Liu #else
19771391bddSXiaozhou Liu #define notrace __attribute__((__no_instrument_function__))
19871391bddSXiaozhou Liu #endif
19971391bddSXiaozhou Liu
20071391bddSXiaozhou Liu /*
20171391bddSXiaozhou Liu * it doesn't make sense on ARM (currently the only user of __naked)
20271391bddSXiaozhou Liu * to trace naked functions because then mcount is called without
20371391bddSXiaozhou Liu * stack and frame pointer being set up and there is no chance to
20471391bddSXiaozhou Liu * restore the lr register to the value before mcount was called.
20571391bddSXiaozhou Liu */
20671391bddSXiaozhou Liu #define __naked __attribute__((__naked__)) notrace
20771391bddSXiaozhou Liu
20871391bddSXiaozhou Liu /*
20971391bddSXiaozhou Liu * Prefer gnu_inline, so that extern inline functions do not emit an
21071391bddSXiaozhou Liu * externally visible function. This makes extern inline behave as per gnu89
21171391bddSXiaozhou Liu * semantics rather than c99. This prevents multiple symbol definition errors
21271391bddSXiaozhou Liu * of extern inline functions at link time.
21371391bddSXiaozhou Liu * A lot of inline functions can cause havoc with function tracing.
21471391bddSXiaozhou Liu */
215889b3c12SMasahiro Yamada #define inline inline __gnu_inline __inline_maybe_unused notrace
21671391bddSXiaozhou Liu
217c30724e9SRasmus Villemoes /*
218c30724e9SRasmus Villemoes * gcc provides both __inline__ and __inline as alternate spellings of
219c30724e9SRasmus Villemoes * the inline keyword, though the latter is undocumented. New kernel
220c30724e9SRasmus Villemoes * code should only use the inline spelling, but some existing code
221c30724e9SRasmus Villemoes * uses __inline__. Since we #define inline above, to ensure
222c30724e9SRasmus Villemoes * __inline__ has the same semantics, we need this #define.
223c30724e9SRasmus Villemoes *
224c30724e9SRasmus Villemoes * However, the spelling __inline is strictly reserved for referring
225c30724e9SRasmus Villemoes * to the bare keyword.
226c30724e9SRasmus Villemoes */
22771391bddSXiaozhou Liu #define __inline__ inline
22871391bddSXiaozhou Liu
22971391bddSXiaozhou Liu /*
2306863f564SMasahiro Yamada * GCC does not warn about unused static inline functions for -Wunused-function.
2316863f564SMasahiro Yamada * Suppress the warning in clang as well by using __maybe_unused, but enable it
2326863f564SMasahiro Yamada * for W=1 build. This will allow clang to find unused functions. Remove the
2336863f564SMasahiro Yamada * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
2346863f564SMasahiro Yamada */
2356863f564SMasahiro Yamada #ifdef KBUILD_EXTRA_WARN1
2366863f564SMasahiro Yamada #define __inline_maybe_unused
2376863f564SMasahiro Yamada #else
2386863f564SMasahiro Yamada #define __inline_maybe_unused __maybe_unused
2396863f564SMasahiro Yamada #endif
2406863f564SMasahiro Yamada
2416863f564SMasahiro Yamada /*
24271391bddSXiaozhou Liu * Rather then using noinline to prevent stack consumption, use
24371391bddSXiaozhou Liu * noinline_for_stack instead. For documentation reasons.
24471391bddSXiaozhou Liu */
24571391bddSXiaozhou Liu #define noinline_for_stack noinline
24671391bddSXiaozhou Liu
2471f44328eSMarco Elver /*
2481f44328eSMarco Elver * Sanitizer helper attributes: Because using __always_inline and
2491f44328eSMarco Elver * __no_sanitize_* conflict, provide helper attributes that will either expand
2501f44328eSMarco Elver * to __no_sanitize_* in compilation units where instrumentation is enabled
2511f44328eSMarco Elver * (__SANITIZE_*__), or __always_inline in compilation units without
2521f44328eSMarco Elver * instrumentation (__SANITIZE_*__ undefined).
2531f44328eSMarco Elver */
2541f44328eSMarco Elver #ifdef __SANITIZE_ADDRESS__
255eb73876cSMarco Elver /*
256eb73876cSMarco Elver * We can't declare function 'inline' because __no_sanitize_address conflicts
257eb73876cSMarco Elver * with inlining. Attempt to inline it may cause a build failure.
258eb73876cSMarco Elver * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
259eb73876cSMarco Elver * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
260eb73876cSMarco Elver */
261eb73876cSMarco Elver # define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused
262eb73876cSMarco Elver # define __no_sanitize_or_inline __no_kasan_or_inline
263eb73876cSMarco Elver #else
264eb73876cSMarco Elver # define __no_kasan_or_inline __always_inline
265eb73876cSMarco Elver #endif
266eb73876cSMarco Elver
267eb73876cSMarco Elver #ifdef __SANITIZE_THREAD__
268bd3d5bd1SMarco Elver /*
269bd3d5bd1SMarco Elver * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
270bd3d5bd1SMarco Elver * atomics even with __no_sanitize_thread (to avoid false positives in userspace
271bd3d5bd1SMarco Elver * ThreadSanitizer). The kernel's requirements are stricter and we really do not
272bd3d5bd1SMarco Elver * want any instrumentation with __no_kcsan.
273bd3d5bd1SMarco Elver *
274bd3d5bd1SMarco Elver * Therefore we add __disable_sanitizer_instrumentation where available to
275bd3d5bd1SMarco Elver * disable all instrumentation. See Kconfig.kcsan where this is mandatory.
276bd3d5bd1SMarco Elver */
277bd3d5bd1SMarco Elver # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation
278e79302aeSPeter Zijlstra # define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
279bd3d5bd1SMarco Elver #else
280bd3d5bd1SMarco Elver # define __no_kcsan
281eb73876cSMarco Elver #endif
282eb73876cSMarco Elver
2835d6ad8f8SAlexander Potapenko #ifdef __SANITIZE_MEMORY__
2845d6ad8f8SAlexander Potapenko /*
2855d6ad8f8SAlexander Potapenko * Similarly to KASAN and KCSAN, KMSAN loses function attributes of inlined
2865d6ad8f8SAlexander Potapenko * functions, therefore disabling KMSAN checks also requires disabling inlining.
2875d6ad8f8SAlexander Potapenko *
2885d6ad8f8SAlexander Potapenko * __no_sanitize_or_inline effectively prevents KMSAN from reporting errors
2895d6ad8f8SAlexander Potapenko * within the function and marks all its outputs as initialized.
2905d6ad8f8SAlexander Potapenko */
2915d6ad8f8SAlexander Potapenko # define __no_sanitize_or_inline __no_kmsan_checks notrace __maybe_unused
2925d6ad8f8SAlexander Potapenko #endif
2935d6ad8f8SAlexander Potapenko
294eb73876cSMarco Elver #ifndef __no_sanitize_or_inline
295eb73876cSMarco Elver #define __no_sanitize_or_inline __always_inline
296eb73876cSMarco Elver #endif
297eb73876cSMarco Elver
2985ddbc408SPeter Zijlstra /* Section for code which can't be instrumented at all */
2992b5a0e42SPeter Zijlstra #define __noinstr_section(section) \
3002b5a0e42SPeter Zijlstra noinline notrace __attribute((__section__(section))) \
3015de0ce85SAlexander Potapenko __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage \
3025de0ce85SAlexander Potapenko __no_sanitize_memory
3035ddbc408SPeter Zijlstra
3042b5a0e42SPeter Zijlstra #define noinstr __noinstr_section(".noinstr.text")
3052b5a0e42SPeter Zijlstra
3060e985e9dSPeter Zijlstra /*
3070e985e9dSPeter Zijlstra * The __cpuidle section is used twofold:
3080e985e9dSPeter Zijlstra *
3090e985e9dSPeter Zijlstra * 1) the original use -- identifying if a CPU is 'stuck' in idle state based
3100e985e9dSPeter Zijlstra * on it's instruction pointer. See cpu_in_idle().
3110e985e9dSPeter Zijlstra *
3120e985e9dSPeter Zijlstra * 2) supressing instrumentation around where cpuidle disables RCU; where the
3130e985e9dSPeter Zijlstra * function isn't strictly required for #1, this is interchangeable with
3140e985e9dSPeter Zijlstra * noinstr.
3150e985e9dSPeter Zijlstra */
3162b5a0e42SPeter Zijlstra #define __cpuidle __noinstr_section(".cpuidle.text")
3172b5a0e42SPeter Zijlstra
318d1515582SWill Deacon #endif /* __KERNEL__ */
319d1515582SWill Deacon
320815f0ddbSNick Desaulniers #endif /* __ASSEMBLY__ */
321d1515582SWill Deacon
322815f0ddbSNick Desaulniers /*
323815f0ddbSNick Desaulniers * The below symbols may be defined for one or more, but not ALL, of the above
324815f0ddbSNick Desaulniers * compilers. We don't consider that to be an error, so set them to nothing.
325815f0ddbSNick Desaulniers * For example, some of them are for compiler specific plugins.
326815f0ddbSNick Desaulniers */
327d1515582SWill Deacon #ifndef __latent_entropy
328d1515582SWill Deacon # define __latent_entropy
329d1515582SWill Deacon #endif
330d1515582SWill Deacon
331595b893eSKees Cook #if defined(RANDSTRUCT) && !defined(__CHECKER__)
332595b893eSKees Cook # define __randomize_layout __designated_init __attribute__((randomize_layout))
333595b893eSKees Cook # define __no_randomize_layout __attribute__((no_randomize_layout))
334595b893eSKees Cook /* This anon struct can add padding, so only enable it under randstruct. */
335595b893eSKees Cook # define randomized_struct_fields_start struct {
336595b893eSKees Cook # define randomized_struct_fields_end } __randomize_layout;
337595b893eSKees Cook #else
338d1515582SWill Deacon # define __randomize_layout __designated_init
339d1515582SWill Deacon # define __no_randomize_layout
340d1515582SWill Deacon # define randomized_struct_fields_start
341d1515582SWill Deacon # define randomized_struct_fields_end
342d1515582SWill Deacon #endif
343d1515582SWill Deacon
344d08b9f0cSSami Tolvanen #ifndef __noscs
345d08b9f0cSSami Tolvanen # define __noscs
346d08b9f0cSSami Tolvanen #endif
347d08b9f0cSSami Tolvanen
348cf68fffbSSami Tolvanen #ifndef __nocfi
349cf68fffbSSami Tolvanen # define __nocfi
350cf68fffbSSami Tolvanen #endif
351cf68fffbSSami Tolvanen
35286cffecdSKees Cook /*
35386cffecdSKees Cook * Any place that could be marked with the "alloc_size" attribute is also
3549ed9cac1SKees Cook * a place to be marked with the "malloc" attribute, except those that may
3559ed9cac1SKees Cook * be performing a _reallocation_, as that may alias the existing pointer.
3569ed9cac1SKees Cook * For these, use __realloc_size().
35786cffecdSKees Cook */
35886cffecdSKees Cook #ifdef __alloc_size__
35986cffecdSKees Cook # define __alloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc
3609ed9cac1SKees Cook # define __realloc_size(x, ...) __alloc_size__(x, ## __VA_ARGS__)
36186cffecdSKees Cook #else
36286cffecdSKees Cook # define __alloc_size(x, ...) __malloc
3639ed9cac1SKees Cook # define __realloc_size(x, ...)
36486cffecdSKees Cook #endif
36586cffecdSKees Cook
366f328d96dSLinus Torvalds /*
367f328d96dSLinus Torvalds * Some versions of gcc do not mark 'asm goto' volatile:
368f328d96dSLinus Torvalds *
369f328d96dSLinus Torvalds * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103979
370f328d96dSLinus Torvalds *
371f328d96dSLinus Torvalds * We do it here by hand, because it doesn't hurt.
372f328d96dSLinus Torvalds */
373aaff74d8SLinus Torvalds #ifndef asm_goto_output
374f328d96dSLinus Torvalds #define asm_goto_output(x...) asm volatile goto(x)
3758bd66d14Sndesaulniers@google.com #endif
3768bd66d14Sndesaulniers@google.com
377eb111869SRasmus Villemoes #ifdef CONFIG_CC_HAS_ASM_INLINE
378eb111869SRasmus Villemoes #define asm_inline asm __inline
379eb111869SRasmus Villemoes #else
380eb111869SRasmus Villemoes #define asm_inline asm
381eb111869SRasmus Villemoes #endif
382eb111869SRasmus Villemoes
383d1515582SWill Deacon /* Are two types/vars the same type (ignoring qualifiers)? */
384d1515582SWill Deacon #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
385d1515582SWill Deacon
386dee081bfSWill Deacon /*
387dee081bfSWill Deacon * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
388dee081bfSWill Deacon * non-scalar types unchanged.
3891fd76043SMarco Elver */
3901fd76043SMarco Elver /*
3916ec4476aSLinus Torvalds * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
3921fd76043SMarco Elver * is not type-compatible with 'signed char', and we define a separate case.
3931fd76043SMarco Elver */
3941fd76043SMarco Elver #define __scalar_type_to_expr_cases(type) \
3951fd76043SMarco Elver unsigned type: (unsigned type)0, \
3961fd76043SMarco Elver signed type: (signed type)0
3971fd76043SMarco Elver
3981fd76043SMarco Elver #define __unqual_scalar_typeof(x) typeof( \
3991fd76043SMarco Elver _Generic((x), \
4001fd76043SMarco Elver char: (char)0, \
4011fd76043SMarco Elver __scalar_type_to_expr_cases(char), \
4021fd76043SMarco Elver __scalar_type_to_expr_cases(short), \
4031fd76043SMarco Elver __scalar_type_to_expr_cases(int), \
4041fd76043SMarco Elver __scalar_type_to_expr_cases(long), \
4051fd76043SMarco Elver __scalar_type_to_expr_cases(long long), \
4061fd76043SMarco Elver default: (x)))
407dee081bfSWill Deacon
408d1515582SWill Deacon /* Is this type a native word size -- useful for atomic operations */
409815f0ddbSNick Desaulniers #define __native_word(t) \
410815f0ddbSNick Desaulniers (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
411815f0ddbSNick Desaulniers sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
412815f0ddbSNick Desaulniers
413eb5c2d4bSWill Deacon #ifdef __OPTIMIZE__
414eb5c2d4bSWill Deacon # define __compiletime_assert(condition, msg, prefix, suffix) \
415eb5c2d4bSWill Deacon do { \
4167c00621dSMiguel Ojeda /* \
4177c00621dSMiguel Ojeda * __noreturn is needed to give the compiler enough \
4187c00621dSMiguel Ojeda * information to avoid certain possibly-uninitialized \
4197c00621dSMiguel Ojeda * warnings (regardless of the build failing). \
4207c00621dSMiguel Ojeda */ \
4217c00621dSMiguel Ojeda __noreturn extern void prefix ## suffix(void) \
4227c00621dSMiguel Ojeda __compiletime_error(msg); \
423eb5c2d4bSWill Deacon if (!(condition)) \
424eb5c2d4bSWill Deacon prefix ## suffix(); \
425eb5c2d4bSWill Deacon } while (0)
426eb5c2d4bSWill Deacon #else
427eb5c2d4bSWill Deacon # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
428eb5c2d4bSWill Deacon #endif
429eb5c2d4bSWill Deacon
430eb5c2d4bSWill Deacon #define _compiletime_assert(condition, msg, prefix, suffix) \
431eb5c2d4bSWill Deacon __compiletime_assert(condition, msg, prefix, suffix)
432eb5c2d4bSWill Deacon
433eb5c2d4bSWill Deacon /**
434eb5c2d4bSWill Deacon * compiletime_assert - break build and emit msg if condition is false
435eb5c2d4bSWill Deacon * @condition: a compile-time constant condition to check
436eb5c2d4bSWill Deacon * @msg: a message to emit if condition is false
437eb5c2d4bSWill Deacon *
438eb5c2d4bSWill Deacon * In tradition of POSIX assert, this macro will break the build if the
439eb5c2d4bSWill Deacon * supplied condition is *false*, emitting the supplied error message if the
440eb5c2d4bSWill Deacon * compiler has support to do so.
441eb5c2d4bSWill Deacon */
442eb5c2d4bSWill Deacon #define compiletime_assert(condition, msg) \
443eb5c2d4bSWill Deacon _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
444eb5c2d4bSWill Deacon
445eb5c2d4bSWill Deacon #define compiletime_assert_atomic_type(t) \
446eb5c2d4bSWill Deacon compiletime_assert(__native_word(t), \
447eb5c2d4bSWill Deacon "Need native word sized stores/loads for atomicity.")
448eb5c2d4bSWill Deacon
449815f0ddbSNick Desaulniers /* Helpers for emitting diagnostics in pragmas. */
4508793bb7fSArnd Bergmann #ifndef __diag
4518793bb7fSArnd Bergmann #define __diag(string)
4528793bb7fSArnd Bergmann #endif
4538793bb7fSArnd Bergmann
4548793bb7fSArnd Bergmann #ifndef __diag_GCC
4558793bb7fSArnd Bergmann #define __diag_GCC(version, severity, string)
4568793bb7fSArnd Bergmann #endif
4578793bb7fSArnd Bergmann
4588793bb7fSArnd Bergmann #define __diag_push() __diag(push)
4598793bb7fSArnd Bergmann #define __diag_pop() __diag(pop)
4608793bb7fSArnd Bergmann
4618793bb7fSArnd Bergmann #define __diag_ignore(compiler, version, option, comment) \
4628793bb7fSArnd Bergmann __diag_ ## compiler(version, ignore, option)
4638793bb7fSArnd Bergmann #define __diag_warn(compiler, version, option, comment) \
4648793bb7fSArnd Bergmann __diag_ ## compiler(version, warn, option)
4658793bb7fSArnd Bergmann #define __diag_error(compiler, version, option, comment) \
4668793bb7fSArnd Bergmann __diag_ ## compiler(version, error, option)
4678793bb7fSArnd Bergmann
4684d1ea705SKumar Kartikeya Dwivedi #ifndef __diag_ignore_all
4694d1ea705SKumar Kartikeya Dwivedi #define __diag_ignore_all(option, comment)
4704d1ea705SKumar Kartikeya Dwivedi #endif
4714d1ea705SKumar Kartikeya Dwivedi
472d1515582SWill Deacon #endif /* __LINUX_COMPILER_TYPES_H */
473