Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_COMPILER_TYPES_H
0003 #define __LINUX_COMPILER_TYPES_H
0004 
0005 #ifndef __ASSEMBLY__
0006 
0007 #if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) && \
0008     __has_attribute(btf_type_tag)
0009 # define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value)))
0010 #else
0011 # define BTF_TYPE_TAG(value) /* nothing */
0012 #endif
0013 
0014 /* sparse defines __CHECKER__; see Documentation/dev-tools/sparse.rst */
0015 #ifdef __CHECKER__
0016 /* address spaces */
0017 # define __kernel   __attribute__((address_space(0)))
0018 # define __user     __attribute__((noderef, address_space(__user)))
0019 # define __iomem    __attribute__((noderef, address_space(__iomem)))
0020 # define __percpu   __attribute__((noderef, address_space(__percpu)))
0021 # define __rcu      __attribute__((noderef, address_space(__rcu)))
0022 static inline void __chk_user_ptr(const volatile void __user *ptr) { }
0023 static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
0024 /* context/locking */
0025 # define __must_hold(x) __attribute__((context(x,1,1)))
0026 # define __acquires(x)  __attribute__((context(x,0,1)))
0027 # define __cond_acquires(x) __attribute__((context(x,0,-1)))
0028 # define __releases(x)  __attribute__((context(x,1,0)))
0029 # define __acquire(x)   __context__(x,1)
0030 # define __release(x)   __context__(x,-1)
0031 # define __cond_lock(x,c)   ((c) ? ({ __acquire(x); 1; }) : 0)
0032 /* other */
0033 # define __force    __attribute__((force))
0034 # define __nocast   __attribute__((nocast))
0035 # define __safe     __attribute__((safe))
0036 # define __private  __attribute__((noderef))
0037 # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member))
0038 #else /* __CHECKER__ */
0039 /* address spaces */
0040 # define __kernel
0041 # ifdef STRUCTLEAK_PLUGIN
0042 #  define __user    __attribute__((user))
0043 # else
0044 #  define __user    BTF_TYPE_TAG(user)
0045 # endif
0046 # define __iomem
0047 # define __percpu   BTF_TYPE_TAG(percpu)
0048 # define __rcu
0049 # define __chk_user_ptr(x)  (void)0
0050 # define __chk_io_ptr(x)    (void)0
0051 /* context/locking */
0052 # define __must_hold(x)
0053 # define __acquires(x)
0054 # define __cond_acquires(x)
0055 # define __releases(x)
0056 # define __acquire(x)   (void)0
0057 # define __release(x)   (void)0
0058 # define __cond_lock(x,c) (c)
0059 /* other */
0060 # define __force
0061 # define __nocast
0062 # define __safe
0063 # define __private
0064 # define ACCESS_PRIVATE(p, member) ((p)->member)
0065 # define __builtin_warning(x, y...) (1)
0066 #endif /* __CHECKER__ */
0067 
0068 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
0069 #define ___PASTE(a,b) a##b
0070 #define __PASTE(a,b) ___PASTE(a,b)
0071 
0072 #ifdef __KERNEL__
0073 
0074 /* Attributes */
0075 #include <linux/compiler_attributes.h>
0076 
0077 /* Builtins */
0078 
0079 /*
0080  * __has_builtin is supported on gcc >= 10, clang >= 3 and icc >= 21.
0081  * In the meantime, to support gcc < 10, we implement __has_builtin
0082  * by hand.
0083  */
0084 #ifndef __has_builtin
0085 #define __has_builtin(x) (0)
0086 #endif
0087 
0088 /* Compiler specific macros. */
0089 #ifdef __clang__
0090 #include <linux/compiler-clang.h>
0091 #elif defined(__INTEL_COMPILER)
0092 #include <linux/compiler-intel.h>
0093 #elif defined(__GNUC__)
0094 /* The above compilers also define __GNUC__, so order is important here. */
0095 #include <linux/compiler-gcc.h>
0096 #else
0097 #error "Unknown compiler"
0098 #endif
0099 
0100 /*
0101  * Some architectures need to provide custom definitions of macros provided
0102  * by linux/compiler-*.h, and can do so using asm/compiler.h. We include that
0103  * conditionally rather than using an asm-generic wrapper in order to avoid
0104  * build failures if any C compilation, which will include this file via an
0105  * -include argument in c_flags, occurs prior to the asm-generic wrappers being
0106  * generated.
0107  */
0108 #ifdef CONFIG_HAVE_ARCH_COMPILER_H
0109 #include <asm/compiler.h>
0110 #endif
0111 
0112 struct ftrace_branch_data {
0113     const char *func;
0114     const char *file;
0115     unsigned line;
0116     union {
0117         struct {
0118             unsigned long correct;
0119             unsigned long incorrect;
0120         };
0121         struct {
0122             unsigned long miss;
0123             unsigned long hit;
0124         };
0125         unsigned long miss_hit[2];
0126     };
0127 };
0128 
0129 struct ftrace_likely_data {
0130     struct ftrace_branch_data   data;
0131     unsigned long           constant;
0132 };
0133 
0134 #if defined(CC_USING_HOTPATCH)
0135 #define notrace         __attribute__((hotpatch(0, 0)))
0136 #elif defined(CC_USING_PATCHABLE_FUNCTION_ENTRY)
0137 #define notrace         __attribute__((patchable_function_entry(0, 0)))
0138 #else
0139 #define notrace         __attribute__((__no_instrument_function__))
0140 #endif
0141 
0142 /*
0143  * it doesn't make sense on ARM (currently the only user of __naked)
0144  * to trace naked functions because then mcount is called without
0145  * stack and frame pointer being set up and there is no chance to
0146  * restore the lr register to the value before mcount was called.
0147  */
0148 #define __naked         __attribute__((__naked__)) notrace
0149 
0150 /*
0151  * Prefer gnu_inline, so that extern inline functions do not emit an
0152  * externally visible function. This makes extern inline behave as per gnu89
0153  * semantics rather than c99. This prevents multiple symbol definition errors
0154  * of extern inline functions at link time.
0155  * A lot of inline functions can cause havoc with function tracing.
0156  */
0157 #define inline inline __gnu_inline __inline_maybe_unused notrace
0158 
0159 /*
0160  * gcc provides both __inline__ and __inline as alternate spellings of
0161  * the inline keyword, though the latter is undocumented. New kernel
0162  * code should only use the inline spelling, but some existing code
0163  * uses __inline__. Since we #define inline above, to ensure
0164  * __inline__ has the same semantics, we need this #define.
0165  *
0166  * However, the spelling __inline is strictly reserved for referring
0167  * to the bare keyword.
0168  */
0169 #define __inline__ inline
0170 
0171 /*
0172  * GCC does not warn about unused static inline functions for -Wunused-function.
0173  * Suppress the warning in clang as well by using __maybe_unused, but enable it
0174  * for W=1 build. This will allow clang to find unused functions. Remove the
0175  * __inline_maybe_unused entirely after fixing most of -Wunused-function warnings.
0176  */
0177 #ifdef KBUILD_EXTRA_WARN1
0178 #define __inline_maybe_unused
0179 #else
0180 #define __inline_maybe_unused __maybe_unused
0181 #endif
0182 
0183 /*
0184  * Rather then using noinline to prevent stack consumption, use
0185  * noinline_for_stack instead.  For documentation reasons.
0186  */
0187 #define noinline_for_stack noinline
0188 
0189 /*
0190  * Sanitizer helper attributes: Because using __always_inline and
0191  * __no_sanitize_* conflict, provide helper attributes that will either expand
0192  * to __no_sanitize_* in compilation units where instrumentation is enabled
0193  * (__SANITIZE_*__), or __always_inline in compilation units without
0194  * instrumentation (__SANITIZE_*__ undefined).
0195  */
0196 #ifdef __SANITIZE_ADDRESS__
0197 /*
0198  * We can't declare function 'inline' because __no_sanitize_address conflicts
0199  * with inlining. Attempt to inline it may cause a build failure.
0200  *     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67368
0201  * '__maybe_unused' allows us to avoid defined-but-not-used warnings.
0202  */
0203 # define __no_kasan_or_inline __no_sanitize_address notrace __maybe_unused
0204 # define __no_sanitize_or_inline __no_kasan_or_inline
0205 #else
0206 # define __no_kasan_or_inline __always_inline
0207 #endif
0208 
0209 #ifdef __SANITIZE_THREAD__
0210 /*
0211  * Clang still emits instrumentation for __tsan_func_{entry,exit}() and builtin
0212  * atomics even with __no_sanitize_thread (to avoid false positives in userspace
0213  * ThreadSanitizer). The kernel's requirements are stricter and we really do not
0214  * want any instrumentation with __no_kcsan.
0215  *
0216  * Therefore we add __disable_sanitizer_instrumentation where available to
0217  * disable all instrumentation. See Kconfig.kcsan where this is mandatory.
0218  */
0219 # define __no_kcsan __no_sanitize_thread __disable_sanitizer_instrumentation
0220 # define __no_sanitize_or_inline __no_kcsan notrace __maybe_unused
0221 #else
0222 # define __no_kcsan
0223 #endif
0224 
0225 #ifndef __no_sanitize_or_inline
0226 #define __no_sanitize_or_inline __always_inline
0227 #endif
0228 
0229 /* Section for code which can't be instrumented at all */
0230 #define noinstr                             \
0231     noinline notrace __attribute((__section__(".noinstr.text")))    \
0232     __no_kcsan __no_sanitize_address __no_profile __no_sanitize_coverage
0233 
0234 #endif /* __KERNEL__ */
0235 
0236 #endif /* __ASSEMBLY__ */
0237 
0238 /*
0239  * The below symbols may be defined for one or more, but not ALL, of the above
0240  * compilers. We don't consider that to be an error, so set them to nothing.
0241  * For example, some of them are for compiler specific plugins.
0242  */
0243 #ifndef __latent_entropy
0244 # define __latent_entropy
0245 #endif
0246 
0247 #if defined(RANDSTRUCT) && !defined(__CHECKER__)
0248 # define __randomize_layout __designated_init __attribute__((randomize_layout))
0249 # define __no_randomize_layout __attribute__((no_randomize_layout))
0250 /* This anon struct can add padding, so only enable it under randstruct. */
0251 # define randomized_struct_fields_start struct {
0252 # define randomized_struct_fields_end   } __randomize_layout;
0253 #else
0254 # define __randomize_layout __designated_init
0255 # define __no_randomize_layout
0256 # define randomized_struct_fields_start
0257 # define randomized_struct_fields_end
0258 #endif
0259 
0260 #ifndef __noscs
0261 # define __noscs
0262 #endif
0263 
0264 #ifndef __nocfi
0265 # define __nocfi
0266 #endif
0267 
0268 #ifndef __cficanonical
0269 # define __cficanonical
0270 #endif
0271 
0272 /*
0273  * Any place that could be marked with the "alloc_size" attribute is also
0274  * a place to be marked with the "malloc" attribute. Do this as part of the
0275  * __alloc_size macro to avoid redundant attributes and to avoid missing a
0276  * __malloc marking.
0277  */
0278 #ifdef __alloc_size__
0279 # define __alloc_size(x, ...)   __alloc_size__(x, ## __VA_ARGS__) __malloc
0280 #else
0281 # define __alloc_size(x, ...)   __malloc
0282 #endif
0283 
0284 #ifndef asm_volatile_goto
0285 #define asm_volatile_goto(x...) asm goto(x)
0286 #endif
0287 
0288 #ifdef CONFIG_CC_HAS_ASM_INLINE
0289 #define asm_inline asm __inline
0290 #else
0291 #define asm_inline asm
0292 #endif
0293 
0294 /* Are two types/vars the same type (ignoring qualifiers)? */
0295 #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
0296 
0297 /*
0298  * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
0299  *                 non-scalar types unchanged.
0300  */
0301 /*
0302  * Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
0303  * is not type-compatible with 'signed char', and we define a separate case.
0304  */
0305 #define __scalar_type_to_expr_cases(type)               \
0306         unsigned type:  (unsigned type)0,           \
0307         signed type:    (signed type)0
0308 
0309 #define __unqual_scalar_typeof(x) typeof(               \
0310         _Generic((x),                       \
0311              char:  (char)0,                \
0312              __scalar_type_to_expr_cases(char),     \
0313              __scalar_type_to_expr_cases(short),        \
0314              __scalar_type_to_expr_cases(int),      \
0315              __scalar_type_to_expr_cases(long),     \
0316              __scalar_type_to_expr_cases(long long),    \
0317              default: (x)))
0318 
0319 /* Is this type a native word size -- useful for atomic operations */
0320 #define __native_word(t) \
0321     (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
0322      sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
0323 
0324 #ifdef __OPTIMIZE__
0325 # define __compiletime_assert(condition, msg, prefix, suffix)       \
0326     do {                                \
0327         /*                          \
0328          * __noreturn is needed to give the compiler enough \
0329          * information to avoid certain possibly-uninitialized  \
0330          * warnings (regardless of the build failing).      \
0331          */                         \
0332         __noreturn extern void prefix ## suffix(void)       \
0333             __compiletime_error(msg);           \
0334         if (!(condition))                   \
0335             prefix ## suffix();             \
0336     } while (0)
0337 #else
0338 # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
0339 #endif
0340 
0341 #define _compiletime_assert(condition, msg, prefix, suffix) \
0342     __compiletime_assert(condition, msg, prefix, suffix)
0343 
0344 /**
0345  * compiletime_assert - break build and emit msg if condition is false
0346  * @condition: a compile-time constant condition to check
0347  * @msg:       a message to emit if condition is false
0348  *
0349  * In tradition of POSIX assert, this macro will break the build if the
0350  * supplied condition is *false*, emitting the supplied error message if the
0351  * compiler has support to do so.
0352  */
0353 #define compiletime_assert(condition, msg) \
0354     _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
0355 
0356 #define compiletime_assert_atomic_type(t)               \
0357     compiletime_assert(__native_word(t),                \
0358         "Need native word sized stores/loads for atomicity.")
0359 
0360 /* Helpers for emitting diagnostics in pragmas. */
0361 #ifndef __diag
0362 #define __diag(string)
0363 #endif
0364 
0365 #ifndef __diag_GCC
0366 #define __diag_GCC(version, severity, string)
0367 #endif
0368 
0369 #define __diag_push()   __diag(push)
0370 #define __diag_pop()    __diag(pop)
0371 
0372 #define __diag_ignore(compiler, version, option, comment) \
0373     __diag_ ## compiler(version, ignore, option)
0374 #define __diag_warn(compiler, version, option, comment) \
0375     __diag_ ## compiler(version, warn, option)
0376 #define __diag_error(compiler, version, option, comment) \
0377     __diag_ ## compiler(version, error, option)
0378 
0379 #ifndef __diag_ignore_all
0380 #define __diag_ignore_all(option, comment)
0381 #endif
0382 
0383 #endif /* __LINUX_COMPILER_TYPES_H */