aboutsummaryrefslogtreecommitdiff
path: root/include/qemu/compiler.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/qemu/compiler.h')
-rw-r--r--include/qemu/compiler.h171
1 files changed, 164 insertions, 7 deletions
diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h
index c797f0d..65b8995 100644
--- a/include/qemu/compiler.h
+++ b/include/qemu/compiler.h
@@ -22,12 +22,7 @@
#define QEMU_EXTERN_C extern
#endif
-#if defined(_WIN32) && (defined(__x86_64__) || defined(__i386__))
-# define QEMU_PACKED __attribute__((gcc_struct, packed))
-#else
-# define QEMU_PACKED __attribute__((packed))
-#endif
-
+#define QEMU_PACKED __attribute__((packed))
#define QEMU_ALIGNED(X) __attribute__((aligned(X)))
#ifndef glue
@@ -38,7 +33,7 @@
#endif
/* Expands into an identifier stemN, where N is another number each time */
-#define MAKE_IDENTFIER(stem) glue(stem, __COUNTER__)
+#define MAKE_IDENTIFIER(stem) glue(stem, __COUNTER__)
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
@@ -213,6 +208,122 @@
#endif
/*
+ * Disable -ftrivial-auto-var-init on a local variable.
+ *
+ * Use this in cases where there a method in the device I/O path (or other
+ * important hot paths), that has large variables on the stack. A rule of
+ * thumb is that "large" means a method with 4kb data in the local stack
+ * frame. Any variables which are KB in size, should be annotated with this
+ * attribute, to pre-emptively eliminate any potential overhead from the
+ * compiler's implicit zero'ing of memory.
+ *
+ * Given that this turns off a security hardening feature, when using this
+ * to flag variables, it is important that the code is double-checked to
+ * ensure there is no possible use of uninitialized data in the method.
+ */
+#if __has_attribute(uninitialized)
+# define QEMU_UNINITIALIZED __attribute__((uninitialized))
+#else
+# define QEMU_UNINITIALIZED
+#endif
+
+/*
+ * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
+ *
+ * TSA is available since clang 3.6-ish.
+ */
+#ifdef __clang__
+# define TSA(x) __attribute__((x))
+#else
+# define TSA(x) /* No TSA, make TSA attributes no-ops. */
+#endif
+
+/*
+ * TSA_CAPABILITY() is used to annotate typedefs:
+ *
+ * typedef pthread_mutex_t TSA_CAPABILITY("mutex") tsa_mutex;
+ */
+#define TSA_CAPABILITY(x) TSA(capability(x))
+
+/*
+ * TSA_GUARDED_BY() is used to annotate global variables,
+ * the data is guarded:
+ *
+ * Foo foo TSA_GUARDED_BY(mutex);
+ */
+#define TSA_GUARDED_BY(x) TSA(guarded_by(x))
+
+/*
+ * TSA_PT_GUARDED_BY() is used to annotate global pointers, the data
+ * behind the pointer is guarded.
+ *
+ * Foo* ptr TSA_PT_GUARDED_BY(mutex);
+ */
+#define TSA_PT_GUARDED_BY(x) TSA(pt_guarded_by(x))
+
+/*
+ * The TSA_REQUIRES() is used to annotate functions: the caller of the
+ * function MUST hold the resource, the function will NOT release it.
+ *
+ * More than one mutex may be specified, comma-separated.
+ *
+ * void Foo(void) TSA_REQUIRES(mutex);
+ */
+#define TSA_REQUIRES(...) TSA(requires_capability(__VA_ARGS__))
+#define TSA_REQUIRES_SHARED(...) TSA(requires_shared_capability(__VA_ARGS__))
+
+/*
+ * TSA_EXCLUDES() is used to annotate functions: the caller of the
+ * function MUST NOT hold resource, the function first acquires the
+ * resource, and then releases it.
+ *
+ * More than one mutex may be specified, comma-separated.
+ *
+ * void Foo(void) TSA_EXCLUDES(mutex);
+ */
+#define TSA_EXCLUDES(...) TSA(locks_excluded(__VA_ARGS__))
+
+/*
+ * TSA_ACQUIRE() is used to annotate functions: the caller of the
+ * function MUST NOT hold the resource, the function will acquire the
+ * resource, but NOT release it.
+ *
+ * More than one mutex may be specified, comma-separated.
+ *
+ * void Foo(void) TSA_ACQUIRE(mutex);
+ */
+#define TSA_ACQUIRE(...) TSA(acquire_capability(__VA_ARGS__))
+#define TSA_ACQUIRE_SHARED(...) TSA(acquire_shared_capability(__VA_ARGS__))
+
+/*
+ * TSA_RELEASE() is used to annotate functions: the caller of the
+ * function MUST hold the resource, but the function will then release it.
+ *
+ * More than one mutex may be specified, comma-separated.
+ *
+ * void Foo(void) TSA_RELEASE(mutex);
+ */
+#define TSA_RELEASE(...) TSA(release_capability(__VA_ARGS__))
+#define TSA_RELEASE_SHARED(...) TSA(release_shared_capability(__VA_ARGS__))
+
+/*
+ * TSA_NO_TSA is used to annotate functions. Use only when you need to.
+ *
+ * void Foo(void) TSA_NO_TSA;
+ */
+#define TSA_NO_TSA TSA(no_thread_safety_analysis)
+
+/*
+ * TSA_ASSERT() is used to annotate functions: This function will assert that
+ * the lock is held. When it returns, the caller of the function is assumed to
+ * already hold the resource.
+ *
+ * More than one mutex may be specified, comma-separated.
+ */
+#define TSA_ASSERT(...) TSA(assert_capability(__VA_ARGS__))
+#define TSA_ASSERT_SHARED(...) TSA(assert_shared_capability(__VA_ARGS__))
+
+/*
* Ugly CPP trick that is like "defined FOO", but also works in C
* code. Useful to replace #ifdef with "if" statements; assumes
* the symbol was defined with Meson's "config.set()", so it is empty
@@ -227,4 +338,50 @@
#define SECOND_ARG(first, second, ...) second
#define IS_EMPTY_(junk_maybecomma) SECOND_ARG(junk_maybecomma 1, 0)
+#ifndef __cplusplus
+/*
+ * Useful in macros that need to declare temporary variables. For example,
+ * the variable that receives the old value of an atomically-accessed
+ * variable must be non-qualified, because atomic builtins return values
+ * through a pointer-type argument as in __atomic_load(&var, &old, MODEL).
+ *
+ * This macro has to handle types smaller than int manually, because of
+ * implicit promotion. int and larger types, as well as pointers, can be
+ * converted to a non-qualified type just by applying a binary operator.
+ */
+#define typeof_strip_qual(expr) \
+ typeof( \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(expr), bool) || \
+ __builtin_types_compatible_p(typeof(expr), const bool) || \
+ __builtin_types_compatible_p(typeof(expr), volatile bool) || \
+ __builtin_types_compatible_p(typeof(expr), const volatile bool), \
+ (bool)1, \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(expr), signed char) || \
+ __builtin_types_compatible_p(typeof(expr), const signed char) || \
+ __builtin_types_compatible_p(typeof(expr), volatile signed char) || \
+ __builtin_types_compatible_p(typeof(expr), const volatile signed char), \
+ (signed char)1, \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(expr), unsigned char) || \
+ __builtin_types_compatible_p(typeof(expr), const unsigned char) || \
+ __builtin_types_compatible_p(typeof(expr), volatile unsigned char) || \
+ __builtin_types_compatible_p(typeof(expr), const volatile unsigned char), \
+ (unsigned char)1, \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(expr), signed short) || \
+ __builtin_types_compatible_p(typeof(expr), const signed short) || \
+ __builtin_types_compatible_p(typeof(expr), volatile signed short) || \
+ __builtin_types_compatible_p(typeof(expr), const volatile signed short), \
+ (signed short)1, \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(typeof(expr), unsigned short) || \
+ __builtin_types_compatible_p(typeof(expr), const unsigned short) || \
+ __builtin_types_compatible_p(typeof(expr), volatile unsigned short) || \
+ __builtin_types_compatible_p(typeof(expr), const volatile unsigned short), \
+ (unsigned short)1, \
+ (expr)+0))))))
+#endif
+
#endif /* COMPILER_H */