1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 6 #ifndef COMPILER_H 7 #define COMPILER_H 8 9 /* 10 * Macros that should be used instead of using __attribute__ directly to 11 * ease portability and make the code easier to read. 12 * 13 * Some of the defines below is known to sometimes cause conflicts when 14 * this file is included from xtest in normal world. It is assumed that 15 * the conflicting defines has the same meaning in that environment. 16 * Surrounding the troublesome defines with #ifndef should be enough. 17 */ 18 #define __deprecated __attribute__((deprecated)) 19 #ifndef __packed 20 #define __packed __attribute__((packed)) 21 #endif 22 #define __weak __attribute__((weak)) 23 #define __alias(x) __attribute__((alias(x))) 24 #ifndef __noreturn 25 #define __noreturn __attribute__((__noreturn__)) 26 #endif 27 #define __pure __attribute__((pure)) 28 #define __aligned(x) __attribute__((aligned(x))) 29 #define __printf(a, b) __attribute__((format(printf, a, b))) 30 #define __noinline __attribute__((noinline)) 31 #define __attr_const __attribute__((__const__)) 32 #ifndef __unused 33 #define __unused __attribute__((unused)) 34 #endif 35 #define __maybe_unused __attribute__((unused)) 36 #ifndef __used 37 #define __used __attribute__((__used__)) 38 #endif 39 #define __must_check __attribute__((warn_unused_result)) 40 #define __cold __attribute__((__cold__)) 41 #define __section(x) __attribute__((section(x))) 42 #define __data __section(".data") 43 #define __bss __section(".bss") 44 #ifdef __clang__ 45 #define __SECTION_FLAGS_RODATA 46 #else 47 /* 48 * Override sections flags/type generated by the C compiler to make sure they 49 * are: "a",%progbits (thus creating an allocatable, non-writeable, non- 50 * executable data section). 51 * The trailing COMMENT_CHAR comments out the flags generated by the compiler. 52 * This avoids a harmless warning with GCC. 53 */ 54 #if defined(__aarch64__) || defined(__arm__) 55 #define COMMENT_CHAR "//" 56 #else 57 #define COMMENT_CHAR "#" 58 #endif 59 #define __SECTION_FLAGS_RODATA ",\"a\",%progbits " COMMENT_CHAR 60 #endif 61 #define __rodata __section(".rodata" __SECTION_FLAGS_RODATA) 62 #define __rodata_dummy __section(".rodata.dummy" __SECTION_FLAGS_RODATA) 63 #define __rodata_unpaged(x) \ 64 __section(".rodata.__unpaged." x __SECTION_FLAGS_RODATA) 65 #ifdef CFG_CORE_ASLR 66 #define __relrodata_unpaged(x) __section(".data.rel.ro.__unpaged." x) 67 #else 68 #define __relrodata_unpaged(x) __rodata_unpaged(x) 69 #endif 70 #ifdef CFG_VIRTUALIZATION 71 #define __nex_bss __section(".nex_bss") 72 #define __nex_data __section(".nex_data") 73 #else /* CFG_VIRTUALIZATION */ 74 #define __nex_bss 75 #define __nex_data 76 #endif /* CFG_VIRTUALIZATION */ 77 #define __noprof __attribute__((no_instrument_function)) 78 #define __nostackcheck __attribute__((no_instrument_function)) 79 80 #define __compiler_bswap64(x) __builtin_bswap64((x)) 81 #define __compiler_bswap32(x) __builtin_bswap32((x)) 82 #define __compiler_bswap16(x) __builtin_bswap16((x)) 83 84 #define __GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \ 85 __GNUC_PATCHLEVEL__) 86 87 #if __GCC_VERSION >= 50100 && !defined(__CHECKER__) 88 #define __HAVE_BUILTIN_OVERFLOW 1 89 #endif 90 91 #if __GCC_VERSION >= 90100 && !defined(__CHECKER__) 92 #define __HAVE_SINGLE_ARGUMENT_STATIC_ASSERT 1 93 #endif 94 95 #ifdef __HAVE_BUILTIN_OVERFLOW 96 #define __compiler_add_overflow(a, b, res) \ 97 __builtin_add_overflow((a), (b), (res)) 98 99 #define __compiler_sub_overflow(a, b, res) \ 100 __builtin_sub_overflow((a), (b), (res)) 101 102 #define __compiler_mul_overflow(a, b, res) \ 103 __builtin_mul_overflow((a), (b), (res)) 104 #else /*!__HAVE_BUILTIN_OVERFLOW*/ 105 106 /* 107 * Copied/inspired from https://www.fefe.de/intof.html 108 */ 109 110 #define __INTOF_ASSIGN(dest, src) (__extension__({ \ 111 typeof(src) __intof_x = (src); \ 112 typeof(dest) __intof_y = __intof_x; \ 113 (((uintmax_t)__intof_x == (uintmax_t)__intof_y) && \ 114 ((__intof_x < 1) == (__intof_y < 1)) ? \ 115 (void)((dest) = __intof_y) , 0 : 1); \ 116 })) 117 118 #define __INTOF_ADD(c, a, b) (__extension__({ \ 119 typeof(a) __intofa_a = (a); \ 120 typeof(b) __intofa_b = (b); \ 121 intmax_t __intofa_a_signed = __intofa_a; \ 122 uintmax_t __intofa_a_unsigned = __intofa_a; \ 123 intmax_t __intofa_b_signed = __intofa_b; \ 124 uintmax_t __intofa_b_unsigned = __intofa_b; \ 125 \ 126 __intofa_b < 1 ? \ 127 __intofa_a < 1 ? \ 128 ((INTMAX_MIN - __intofa_b_signed <= \ 129 __intofa_a_signed)) ? \ 130 __INTOF_ASSIGN((c), __intofa_a_signed + \ 131 __intofa_b_signed) : 1 \ 132 : \ 133 ((__intofa_a_unsigned >= (uintmax_t)-__intofa_b) ? \ 134 __INTOF_ASSIGN((c), __intofa_a_unsigned + \ 135 __intofa_b_signed) \ 136 : \ 137 __INTOF_ASSIGN((c), \ 138 (intmax_t)(__intofa_a_unsigned + \ 139 __intofa_b_signed))) \ 140 : \ 141 __intofa_a < 1 ? \ 142 ((__intofa_b_unsigned >= (uintmax_t)-__intofa_a) ? \ 143 __INTOF_ASSIGN((c), __intofa_a_signed + \ 144 __intofa_b_unsigned) \ 145 : \ 146 __INTOF_ASSIGN((c), \ 147 (intmax_t)(__intofa_a_signed + \ 148 __intofa_b_unsigned))) \ 149 : \ 150 ((UINTMAX_MAX - __intofa_b_unsigned >= \ 151 __intofa_a_unsigned) ? \ 152 __INTOF_ASSIGN((c), __intofa_a_unsigned + \ 153 __intofa_b_unsigned) : 1); \ 154 })) 155 156 #define __INTOF_SUB(c, a, b) (__extension__({ \ 157 typeof(a) __intofs_a = a; \ 158 typeof(b) __intofs_b = b; \ 159 intmax_t __intofs_a_signed = __intofs_a; \ 160 uintmax_t __intofs_a_unsigned = __intofs_a; \ 161 intmax_t __intofs_b_signed = __intofs_b; \ 162 uintmax_t __intofs_b_unsigned = __intofs_b; \ 163 \ 164 __intofs_b < 1 ? \ 165 __intofs_a < 1 ? \ 166 ((INTMAX_MAX + __intofs_b_signed >= \ 167 __intofs_a_signed) ? \ 168 __INTOF_ASSIGN((c), __intofs_a_signed - \ 169 __intofs_b_signed) : 1) \ 170 : \ 171 (((uintmax_t)(UINTMAX_MAX + __intofs_b_signed) >= \ 172 __intofs_a_unsigned) ? \ 173 __INTOF_ASSIGN((c), __intofs_a - \ 174 __intofs_b) : 1) \ 175 : \ 176 __intofs_a < 1 ? \ 177 (((intmax_t)(INTMAX_MIN + __intofs_b) <= \ 178 __intofs_a_signed) ? \ 179 __INTOF_ASSIGN((c), \ 180 (intmax_t)(__intofs_a_signed - \ 181 __intofs_b_unsigned)) : 1) \ 182 : \ 183 ((__intofs_b_unsigned <= __intofs_a_unsigned) ? \ 184 __INTOF_ASSIGN((c), __intofs_a_unsigned - \ 185 __intofs_b_unsigned) \ 186 : \ 187 __INTOF_ASSIGN((c), \ 188 (intmax_t)(__intofs_a_unsigned - \ 189 __intofs_b_unsigned))); \ 190 })) 191 192 /* 193 * Dealing with detecting overflow in multiplication of integers. 194 * 195 * First step is to remove two corner cases with the minum signed integer 196 * which can't be represented as a positive integer + sign. 197 * Multiply with 0 or 1 can't overflow, no checking needed of the operation, 198 * only if it can be assigned to the result. 199 * 200 * After the corner cases are eliminated we convert the two factors to 201 * positive unsigned values, keeping track of the original in another 202 * variable which is used at the end to determine the sign of the product. 203 * 204 * The two terms (a and b) are divided into upper and lower half (x1 upper 205 * and x0 lower), so the product is: 206 * ((a1 << hshift) + a0) * ((b1 << hshift) + b0) 207 * which also is: 208 * ((a1 * b1) << (hshift * 2)) + (T1) 209 * ((a1 * b0 + a0 * b1) << hshift) + (T2) 210 * (a0 * b0) (T3) 211 * 212 * From this we can tell and (a1 * b1) has to be 0 or we'll overflow, that 213 * is, at least one of a1 or b1 has to be 0. Once this has been checked the 214 * addition: ((a1 * b0) << hshift) + ((a0 * b1) << hshift) 215 * isn't an addition as one of the terms will be 0. 216 * 217 * Since each factor in: (a0 * b0) 218 * only uses half the capicity of the underlaying type it can't overflow 219 * 220 * The addition of T2 and T3 can overflow so we use __INTOF_ADD() to 221 * perform that addition. If the addition succeeds without overflow the 222 * result is assigned the required sign and checked for overflow again. 223 */ 224 225 #define __intof_mul_negate ((__intof_oa < 1) != (__intof_ob < 1)) 226 #define __intof_mul_hshift (sizeof(uintmax_t) * 8 / 2) 227 #define __intof_mul_hmask (UINTMAX_MAX >> __intof_mul_hshift) 228 #define __intof_mul_a0 ((uintmax_t)(__intof_a) >> __intof_mul_hshift) 229 #define __intof_mul_b0 ((uintmax_t)(__intof_b) >> __intof_mul_hshift) 230 #define __intof_mul_a1 ((uintmax_t)(__intof_a) & __intof_mul_hmask) 231 #define __intof_mul_b1 ((uintmax_t)(__intof_b) & __intof_mul_hmask) 232 #define __intof_mul_t (__intof_mul_a1 * __intof_mul_b0 + \ 233 __intof_mul_a0 * __intof_mul_b1) 234 235 #define __INTOF_MUL(c, a, b) (__extension__({ \ 236 typeof(a) __intof_oa = (a); \ 237 typeof(a) __intof_a = __intof_oa < 1 ? -__intof_oa : __intof_oa; \ 238 typeof(b) __intof_ob = (b); \ 239 typeof(b) __intof_b = __intof_ob < 1 ? -__intof_ob : __intof_ob; \ 240 typeof(c) __intof_c; \ 241 \ 242 __intof_oa == 0 || __intof_ob == 0 || \ 243 __intof_oa == 1 || __intof_ob == 1 ? \ 244 __INTOF_ASSIGN((c), __intof_oa * __intof_ob) : \ 245 (__intof_mul_a0 && __intof_mul_b0) || \ 246 __intof_mul_t > __intof_mul_hmask ? 1 : \ 247 __INTOF_ADD((__intof_c), __intof_mul_t << __intof_mul_hshift, \ 248 __intof_mul_a1 * __intof_mul_b1) ? 1 : \ 249 __intof_mul_negate ? __INTOF_ASSIGN((c), -__intof_c) : \ 250 __INTOF_ASSIGN((c), __intof_c); \ 251 })) 252 253 #define __compiler_add_overflow(a, b, res) __INTOF_ADD(*(res), (a), (b)) 254 #define __compiler_sub_overflow(a, b, res) __INTOF_SUB(*(res), (a), (b)) 255 #define __compiler_mul_overflow(a, b, res) __INTOF_MUL(*(res), (a), (b)) 256 257 #endif /*!__HAVE_BUILTIN_OVERFLOW*/ 258 259 #define __compiler_compare_and_swap(p, oval, nval) \ 260 __atomic_compare_exchange_n((p), (oval), (nval), true, \ 261 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) \ 262 263 #define __compiler_atomic_load(p) __atomic_load_n((p), __ATOMIC_RELAXED) 264 #define __compiler_atomic_store(p, val) \ 265 __atomic_store_n((p), (val), __ATOMIC_RELAXED) 266 267 #define barrier() asm volatile ("" : : : "memory") 268 269 #ifndef __has_attribute 270 #define __has_attribute(x) 0 271 #endif 272 273 #if __has_attribute(__fallthrough__) 274 #define fallthrough __attribute__((__fallthrough__)) 275 #else 276 #define fallthrough do {} while (0) /* fallthrough */ 277 #endif 278 279 #endif /*COMPILER_H*/ 280