1 /* compiler.h: macros to abstract away compiler specifics 2 * 3 * This work is licensed under the terms of the GNU GPL, version 2 or later. 4 * See the COPYING file in the top-level directory. 5 */ 6 7 #ifndef COMPILER_H 8 #define COMPILER_H 9 10 #define HOST_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) 11 12 /* HOST_LONG_BITS is the size of a native pointer in bits. */ 13 #define HOST_LONG_BITS (__SIZEOF_POINTER__ * 8) 14 15 #if defined __clang_analyzer__ || defined __COVERITY__ 16 #define QEMU_STATIC_ANALYSIS 1 17 #endif 18 19 #ifdef __cplusplus 20 #define QEMU_EXTERN_C extern "C" 21 #else 22 #define QEMU_EXTERN_C extern 23 #endif 24 25 #define QEMU_PACKED __attribute__((packed)) 26 #define QEMU_ALIGNED(X) __attribute__((aligned(X))) 27 28 #ifndef glue 29 #define xglue(x, y) x ## y 30 #define glue(x, y) xglue(x, y) 31 #define stringify(s) tostring(s) 32 #define tostring(s) #s 33 #endif 34 35 /* Expands into an identifier stemN, where N is another number each time */ 36 #define MAKE_IDENTIFIER(stem) glue(stem, __COUNTER__) 37 38 #ifndef likely 39 #define likely(x) __builtin_expect(!!(x), 1) 40 #define unlikely(x) __builtin_expect(!!(x), 0) 41 #endif 42 43 #ifndef container_of 44 #define container_of(ptr, type, member) ({ \ 45 const typeof(((type *) 0)->member) *__mptr = (ptr); \ 46 (type *) ((char *) __mptr - offsetof(type, member));}) 47 #endif 48 49 #define sizeof_field(type, field) sizeof(((type *)0)->field) 50 51 /* 52 * Calculate the number of bytes up to and including the given 'field' of 53 * 'container'. 54 */ 55 #define endof(container, field) \ 56 (offsetof(container, field) + sizeof_field(container, field)) 57 58 /* Convert from a base type to a parent type, with compile time checking. */ 59 #define DO_UPCAST(type, field, dev) ( __extension__ ( { \ 60 char __attribute__((unused)) offset_must_be_zero[ \ 61 -offsetof(type, field)]; \ 62 container_of(dev, type, field);})) 63 64 #define typeof_field(type, field) typeof(((type *)0)->field) 65 #define type_check(t1,t2) ((t1*)0 - (t2*)0) 66 67 #define QEMU_BUILD_BUG_ON_STRUCT(x) \ 68 struct { \ 69 int:(x) ? -1 : 1; \ 70 } 71 72 #define QEMU_BUILD_BUG_MSG(x, msg) _Static_assert(!(x), msg) 73 74 #define QEMU_BUILD_BUG_ON(x) QEMU_BUILD_BUG_MSG(x, "not expecting: " #x) 75 76 #define QEMU_BUILD_BUG_ON_ZERO(x) (sizeof(QEMU_BUILD_BUG_ON_STRUCT(x)) - \ 77 sizeof(QEMU_BUILD_BUG_ON_STRUCT(x))) 78 79 #if !defined(__clang__) && defined(_WIN32) 80 /* 81 * Map __printf__ to __gnu_printf__ because we want standard format strings even 82 * when MinGW or GLib include files use __printf__. 83 */ 84 # define __printf__ __gnu_printf__ 85 #endif 86 87 #ifndef __has_warning 88 #define __has_warning(x) 0 /* compatibility with non-clang compilers */ 89 #endif 90 91 #ifndef __has_feature 92 #define __has_feature(x) 0 /* compatibility with non-clang compilers */ 93 #endif 94 95 #ifndef __has_builtin 96 #define __has_builtin(x) 0 /* compatibility with non-clang compilers */ 97 #endif 98 99 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__) 100 #define HAS_ASSUME_ALIGNED 101 #endif 102 103 #ifndef __has_attribute 104 #define __has_attribute(x) 0 /* compatibility with older GCC */ 105 #endif 106 107 #if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) 108 # define QEMU_SANITIZE_ADDRESS 1 109 #endif 110 111 #if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer) 112 # define QEMU_SANITIZE_THREAD 1 113 #endif 114 115 /* 116 * GCC doesn't provide __has_attribute() until GCC 5, but we know all the GCC 117 * versions we support have the "flatten" attribute. Clang may not have the 118 * "flatten" attribute but always has __has_attribute() to check for it. 119 */ 120 #if __has_attribute(flatten) || !defined(__clang__) 121 # define QEMU_FLATTEN __attribute__((flatten)) 122 #else 123 # define QEMU_FLATTEN 124 #endif 125 126 /* 127 * If __attribute__((error)) is present, use it to produce an error at 128 * compile time. Otherwise, one must wait for the linker to diagnose 129 * the missing symbol. 130 */ 131 #if __has_attribute(error) 132 # define QEMU_ERROR(X) __attribute__((error(X))) 133 #else 134 # define QEMU_ERROR(X) 135 #endif 136 137 /* 138 * The nonstring variable attribute specifies that an object or member 139 * declaration with type array of char or pointer to char is intended 140 * to store character arrays that do not necessarily contain a terminating 141 * NUL character. This is useful in detecting uses of such arrays or pointers 142 * with functions that expect NUL-terminated strings, and to avoid warnings 143 * when such an array or pointer is used as an argument to a bounded string 144 * manipulation function such as strncpy. 145 */ 146 #if __has_attribute(nonstring) 147 # define QEMU_NONSTRING __attribute__((nonstring)) 148 #else 149 # define QEMU_NONSTRING 150 #endif 151 152 /* 153 * Forced inlining may be desired to encourage constant propagation 154 * of function parameters. However, it can also make debugging harder, 155 * so disable it for a non-optimizing build. 156 */ 157 #if defined(__OPTIMIZE__) 158 #define QEMU_ALWAYS_INLINE __attribute__((always_inline)) 159 #else 160 #define QEMU_ALWAYS_INLINE 161 #endif 162 163 /** 164 * In most cases, normal "fallthrough" comments are good enough for 165 * switch-case statements, but sometimes the compiler has problems 166 * with those. In that case you can use QEMU_FALLTHROUGH instead. 167 */ 168 #if __has_attribute(fallthrough) 169 # define QEMU_FALLTHROUGH __attribute__((fallthrough)) 170 #else 171 # define QEMU_FALLTHROUGH do {} while (0) /* fallthrough */ 172 #endif 173 174 #ifdef CONFIG_CFI 175 /* 176 * If CFI is enabled, use an attribute to disable cfi-icall on the following 177 * function 178 */ 179 #define QEMU_DISABLE_CFI __attribute__((no_sanitize("cfi-icall"))) 180 #else 181 /* If CFI is not enabled, use an empty define to not change the behavior */ 182 #define QEMU_DISABLE_CFI 183 #endif 184 185 /* 186 * Apple clang version 14 has a bug in its __builtin_subcll(); define 187 * BUILTIN_SUBCLL_BROKEN for the offending versions so we can avoid it. 188 * When a version of Apple clang which has this bug fixed is released 189 * we can add an upper bound to this check. 190 * See https://gitlab.com/qemu-project/qemu/-/issues/1631 191 * and https://gitlab.com/qemu-project/qemu/-/issues/1659 for details. 192 * The bug never made it into any upstream LLVM releases, only Apple ones. 193 */ 194 #if defined(__apple_build_version__) && __clang_major__ >= 14 195 #define BUILTIN_SUBCLL_BROKEN 196 #endif 197 198 #if __has_attribute(annotate) 199 #define QEMU_ANNOTATE(x) __attribute__((annotate(x))) 200 #else 201 #define QEMU_ANNOTATE(x) 202 #endif 203 204 #if __has_attribute(used) 205 # define QEMU_USED __attribute__((used)) 206 #else 207 # define QEMU_USED 208 #endif 209 210 /* 211 * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html 212 * 213 * TSA is available since clang 3.6-ish. 214 */ 215 #ifdef __clang__ 216 # define TSA(x) __attribute__((x)) 217 #else 218 # define TSA(x) /* No TSA, make TSA attributes no-ops. */ 219 #endif 220 221 /* 222 * TSA_CAPABILITY() is used to annotate typedefs: 223 * 224 * typedef pthread_mutex_t TSA_CAPABILITY("mutex") tsa_mutex; 225 */ 226 #define TSA_CAPABILITY(x) TSA(capability(x)) 227 228 /* 229 * TSA_GUARDED_BY() is used to annotate global variables, 230 * the data is guarded: 231 * 232 * Foo foo TSA_GUARDED_BY(mutex); 233 */ 234 #define TSA_GUARDED_BY(x) TSA(guarded_by(x)) 235 236 /* 237 * TSA_PT_GUARDED_BY() is used to annotate global pointers, the data 238 * behind the pointer is guarded. 239 * 240 * Foo* ptr TSA_PT_GUARDED_BY(mutex); 241 */ 242 #define TSA_PT_GUARDED_BY(x) TSA(pt_guarded_by(x)) 243 244 /* 245 * The TSA_REQUIRES() is used to annotate functions: the caller of the 246 * function MUST hold the resource, the function will NOT release it. 247 * 248 * More than one mutex may be specified, comma-separated. 249 * 250 * void Foo(void) TSA_REQUIRES(mutex); 251 */ 252 #define TSA_REQUIRES(...) TSA(requires_capability(__VA_ARGS__)) 253 #define TSA_REQUIRES_SHARED(...) TSA(requires_shared_capability(__VA_ARGS__)) 254 255 /* 256 * TSA_EXCLUDES() is used to annotate functions: the caller of the 257 * function MUST NOT hold resource, the function first acquires the 258 * resource, and then releases it. 259 * 260 * More than one mutex may be specified, comma-separated. 261 * 262 * void Foo(void) TSA_EXCLUDES(mutex); 263 */ 264 #define TSA_EXCLUDES(...) TSA(locks_excluded(__VA_ARGS__)) 265 266 /* 267 * TSA_ACQUIRE() is used to annotate functions: the caller of the 268 * function MUST NOT hold the resource, the function will acquire the 269 * resource, but NOT release it. 270 * 271 * More than one mutex may be specified, comma-separated. 272 * 273 * void Foo(void) TSA_ACQUIRE(mutex); 274 */ 275 #define TSA_ACQUIRE(...) TSA(acquire_capability(__VA_ARGS__)) 276 #define TSA_ACQUIRE_SHARED(...) TSA(acquire_shared_capability(__VA_ARGS__)) 277 278 /* 279 * TSA_RELEASE() is used to annotate functions: the caller of the 280 * function MUST hold the resource, but the function will then release it. 281 * 282 * More than one mutex may be specified, comma-separated. 283 * 284 * void Foo(void) TSA_RELEASE(mutex); 285 */ 286 #define TSA_RELEASE(...) TSA(release_capability(__VA_ARGS__)) 287 #define TSA_RELEASE_SHARED(...) TSA(release_shared_capability(__VA_ARGS__)) 288 289 /* 290 * TSA_NO_TSA is used to annotate functions. Use only when you need to. 291 * 292 * void Foo(void) TSA_NO_TSA; 293 */ 294 #define TSA_NO_TSA TSA(no_thread_safety_analysis) 295 296 /* 297 * TSA_ASSERT() is used to annotate functions: This function will assert that 298 * the lock is held. When it returns, the caller of the function is assumed to 299 * already hold the resource. 300 * 301 * More than one mutex may be specified, comma-separated. 302 */ 303 #define TSA_ASSERT(...) TSA(assert_capability(__VA_ARGS__)) 304 #define TSA_ASSERT_SHARED(...) TSA(assert_shared_capability(__VA_ARGS__)) 305 306 /* 307 * Ugly CPP trick that is like "defined FOO", but also works in C 308 * code. Useful to replace #ifdef with "if" statements; assumes 309 * the symbol was defined with Meson's "config.set()", so it is empty 310 * if defined. 311 */ 312 #define IS_ENABLED(x) IS_EMPTY(x) 313 314 #define IS_EMPTY_JUNK_ junk, 315 #define IS_EMPTY(value) IS_EMPTY_(IS_EMPTY_JUNK_##value) 316 317 /* Expands to either SECOND_ARG(junk, 1, 0) or SECOND_ARG(IS_EMPTY_JUNK_CONFIG_FOO 1, 0) */ 318 #define SECOND_ARG(first, second, ...) second 319 #define IS_EMPTY_(junk_maybecomma) SECOND_ARG(junk_maybecomma 1, 0) 320 321 #ifndef __cplusplus 322 /* 323 * Useful in macros that need to declare temporary variables. For example, 324 * the variable that receives the old value of an atomically-accessed 325 * variable must be non-qualified, because atomic builtins return values 326 * through a pointer-type argument as in __atomic_load(&var, &old, MODEL). 327 * 328 * This macro has to handle types smaller than int manually, because of 329 * implicit promotion. int and larger types, as well as pointers, can be 330 * converted to a non-qualified type just by applying a binary operator. 331 */ 332 #define typeof_strip_qual(expr) \ 333 typeof( \ 334 __builtin_choose_expr( \ 335 __builtin_types_compatible_p(typeof(expr), bool) || \ 336 __builtin_types_compatible_p(typeof(expr), const bool) || \ 337 __builtin_types_compatible_p(typeof(expr), volatile bool) || \ 338 __builtin_types_compatible_p(typeof(expr), const volatile bool), \ 339 (bool)1, \ 340 __builtin_choose_expr( \ 341 __builtin_types_compatible_p(typeof(expr), signed char) || \ 342 __builtin_types_compatible_p(typeof(expr), const signed char) || \ 343 __builtin_types_compatible_p(typeof(expr), volatile signed char) || \ 344 __builtin_types_compatible_p(typeof(expr), const volatile signed char), \ 345 (signed char)1, \ 346 __builtin_choose_expr( \ 347 __builtin_types_compatible_p(typeof(expr), unsigned char) || \ 348 __builtin_types_compatible_p(typeof(expr), const unsigned char) || \ 349 __builtin_types_compatible_p(typeof(expr), volatile unsigned char) || \ 350 __builtin_types_compatible_p(typeof(expr), const volatile unsigned char), \ 351 (unsigned char)1, \ 352 __builtin_choose_expr( \ 353 __builtin_types_compatible_p(typeof(expr), signed short) || \ 354 __builtin_types_compatible_p(typeof(expr), const signed short) || \ 355 __builtin_types_compatible_p(typeof(expr), volatile signed short) || \ 356 __builtin_types_compatible_p(typeof(expr), const volatile signed short), \ 357 (signed short)1, \ 358 __builtin_choose_expr( \ 359 __builtin_types_compatible_p(typeof(expr), unsigned short) || \ 360 __builtin_types_compatible_p(typeof(expr), const unsigned short) || \ 361 __builtin_types_compatible_p(typeof(expr), volatile unsigned short) || \ 362 __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \ 363 (unsigned short)1, \ 364 (expr)+0)))))) 365 #endif 366 367 #endif /* COMPILER_H */ 368