Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _TOOLS_LINUX_COMPILER_H_
0003 #define _TOOLS_LINUX_COMPILER_H_
0004 
0005 #include <linux/compiler_types.h>
0006 
0007 #ifndef __compiletime_error
0008 # define __compiletime_error(message)
0009 #endif
0010 
0011 #ifdef __OPTIMIZE__
0012 # define __compiletime_assert(condition, msg, prefix, suffix)       \
0013     do {                                \
0014         extern void prefix ## suffix(void) __compiletime_error(msg); \
0015         if (!(condition))                   \
0016             prefix ## suffix();             \
0017     } while (0)
0018 #else
0019 # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
0020 #endif
0021 
0022 #define _compiletime_assert(condition, msg, prefix, suffix) \
0023     __compiletime_assert(condition, msg, prefix, suffix)
0024 
0025 /**
0026  * compiletime_assert - break build and emit msg if condition is false
0027  * @condition: a compile-time constant condition to check
0028  * @msg:       a message to emit if condition is false
0029  *
0030  * In tradition of POSIX assert, this macro will break the build if the
0031  * supplied condition is *false*, emitting the supplied error message if the
0032  * compiler has support to do so.
0033  */
0034 #define compiletime_assert(condition, msg) \
0035     _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
0036 
0037 /* Optimization barrier */
0038 /* The "volatile" is due to gcc bugs */
0039 #define barrier() __asm__ __volatile__("": : :"memory")
0040 
0041 #ifndef __always_inline
0042 # define __always_inline    inline __attribute__((always_inline))
0043 #endif
0044 
0045 #ifndef noinline
0046 #define noinline
0047 #endif
0048 
0049 /* Are two types/vars the same type (ignoring qualifiers)? */
0050 #ifndef __same_type
0051 # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
0052 #endif
0053 
0054 #ifdef __ANDROID__
0055 /*
0056  * FIXME: Big hammer to get rid of tons of:
0057  *   "warning: always_inline function might not be inlinable"
0058  *
0059  * At least on android-ndk-r12/platforms/android-24/arch-arm
0060  */
0061 #undef __always_inline
0062 #define __always_inline inline
0063 #endif
0064 
0065 #define __user
0066 #define __rcu
0067 #define __read_mostly
0068 
0069 #ifndef __attribute_const__
0070 # define __attribute_const__
0071 #endif
0072 
0073 #ifndef __maybe_unused
0074 # define __maybe_unused     __attribute__((unused))
0075 #endif
0076 
0077 #ifndef __used
0078 # define __used     __attribute__((__unused__))
0079 #endif
0080 
0081 #ifndef __packed
0082 # define __packed       __attribute__((__packed__))
0083 #endif
0084 
0085 #ifndef __force
0086 # define __force
0087 #endif
0088 
0089 #ifndef __weak
0090 # define __weak         __attribute__((weak))
0091 #endif
0092 
0093 #ifndef likely
0094 # define likely(x)      __builtin_expect(!!(x), 1)
0095 #endif
0096 
0097 #ifndef unlikely
0098 # define unlikely(x)        __builtin_expect(!!(x), 0)
0099 #endif
0100 
0101 #ifndef __init
0102 # define __init
0103 #endif
0104 
0105 #include <linux/types.h>
0106 
0107 /*
0108  * Following functions are taken from kernel sources and
0109  * break aliasing rules in their original form.
0110  *
0111  * While kernel is compiled with -fno-strict-aliasing,
0112  * perf uses -Wstrict-aliasing=3 which makes build fail
0113  * under gcc 4.4.
0114  *
0115  * Using extra __may_alias__ type to allow aliasing
0116  * in this case.
0117  */
0118 typedef __u8  __attribute__((__may_alias__))  __u8_alias_t;
0119 typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
0120 typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
0121 typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
0122 
0123 static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
0124 {
0125     switch (size) {
0126     case 1: *(__u8_alias_t  *) res = *(volatile __u8_alias_t  *) p; break;
0127     case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
0128     case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
0129     case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
0130     default:
0131         barrier();
0132         __builtin_memcpy((void *)res, (const void *)p, size);
0133         barrier();
0134     }
0135 }
0136 
0137 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
0138 {
0139     switch (size) {
0140     case 1: *(volatile  __u8_alias_t *) p = *(__u8_alias_t  *) res; break;
0141     case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
0142     case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
0143     case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
0144     default:
0145         barrier();
0146         __builtin_memcpy((void *)p, (const void *)res, size);
0147         barrier();
0148     }
0149 }
0150 
0151 /*
0152  * Prevent the compiler from merging or refetching reads or writes. The
0153  * compiler is also forbidden from reordering successive instances of
0154  * READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
0155  * particular ordering. One way to make the compiler aware of ordering is to
0156  * put the two invocations of READ_ONCE or WRITE_ONCE in different C
0157  * statements.
0158  *
0159  * These two macros will also work on aggregate data types like structs or
0160  * unions. If the size of the accessed data type exceeds the word size of
0161  * the machine (e.g., 32 bits or 64 bits) READ_ONCE() and WRITE_ONCE() will
0162  * fall back to memcpy and print a compile-time warning.
0163  *
0164  * Their two major use cases are: (1) Mediating communication between
0165  * process-level code and irq/NMI handlers, all running on the same CPU,
0166  * and (2) Ensuring that the compiler does not fold, spindle, or otherwise
0167  * mutilate accesses that either do not require ordering or that interact
0168  * with an explicit memory barrier or atomic instruction that provides the
0169  * required ordering.
0170  */
0171 
0172 #define READ_ONCE(x)                    \
0173 ({                          \
0174     union { typeof(x) __val; char __c[1]; } __u =   \
0175         { .__c = { 0 } };           \
0176     __read_once_size(&(x), __u.__c, sizeof(x)); \
0177     __u.__val;                  \
0178 })
0179 
0180 #define WRITE_ONCE(x, val)              \
0181 ({                          \
0182     union { typeof(x) __val; char __c[1]; } __u =   \
0183         { .__val = (val) };             \
0184     __write_once_size(&(x), __u.__c, sizeof(x));    \
0185     __u.__val;                  \
0186 })
0187 
0188 
0189 #ifndef __fallthrough
0190 # define __fallthrough
0191 #endif
0192 
0193 /* Indirect macros required for expanded argument pasting, eg. __LINE__. */
0194 #define ___PASTE(a, b) a##b
0195 #define __PASTE(a, b) ___PASTE(a, b)
0196 
0197 #endif /* _TOOLS_LINUX_COMPILER_H */