* Rename __attribute_pure__ to just __pure before it gains users. * Introduce __constructor which is going to be used in lib/, and is unconditionally cf_check. * Identify the areas of xen/bitops.h which are a mess. * Introduce xen/boot-check.h as helpers for compile and boot time testing. This provides a statement of the ABI, and a confirmation that arch-specific implementations behave as expected.
Sadly Clang 7 and older isn't happy with the compile time checks. Skip them, and just rely on the runtime checks. Signed-off-by: Andrew Cooper <andrew.coop...@citrix.com> --- CC: Jan Beulich <jbeul...@suse.com> CC: Roger Pau Monné <roger....@citrix.com> CC: Wei Liu <w...@xen.org> CC: Stefano Stabellini <sstabell...@kernel.org> CC: Julien Grall <jul...@xen.org> CC: Volodymyr Babchuk <volodymyr_babc...@epam.com> CC: Bertrand Marquis <bertrand.marq...@arm.com> CC: Michal Orzel <michal.or...@amd.com> CC: Oleksii Kurochko <oleksii.kuroc...@gmail.com> CC: Shawn Anastasio <sanasta...@raptorengineering.com> CC: consult...@bugseng.com <consult...@bugseng.com> CC: Simone Ballarin <simone.balla...@bugseng.com> CC: Federico Serafini <federico.seraf...@bugseng.com> CC: Nicola Vetrini <nicola.vetr...@bugseng.com> v2: * Break macros out into a header as they're going to be used elsewhere too * Use panic() rather than BUG_ON() to be more helpful when something fails * Brackets in HIDE() * Alignment adjustments * Skip COMPILE_CHECK() for Clang < 8 --- xen/include/xen/bitops.h | 13 ++++++-- xen/include/xen/boot-check.h | 60 ++++++++++++++++++++++++++++++++++++ xen/include/xen/compiler.h | 3 +- 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 xen/include/xen/boot-check.h diff --git a/xen/include/xen/bitops.h b/xen/include/xen/bitops.h index e3c5a4ccf321..9b40f20381a2 100644 --- a/xen/include/xen/bitops.h +++ b/xen/include/xen/bitops.h @@ -1,5 +1,7 @@ -#ifndef _LINUX_BITOPS_H -#define _LINUX_BITOPS_H +#ifndef XEN_BITOPS_H +#define XEN_BITOPS_H + +#include <xen/compiler.h> #include <xen/types.h> /* @@ -103,8 +105,13 @@ static inline int generic_flsl(unsigned long x) * Include this here because some architectures need generic_ffs/fls in * scope */ + +/* --------------------- Please tidy above here --------------------- */ + #include <asm/bitops.h> +/* --------------------- Please tidy below here --------------------- */ + #ifndef find_next_bit /** * find_next_bit - find the next set bit in a memory region @@ -294,4 +301,4 @@ static inline __u32 ror32(__u32 word, unsigned int shift) #define BIT_WORD(nr) ((nr) / BITS_PER_LONG) -#endif +#endif /* XEN_BITOPS_H */ diff --git a/xen/include/xen/boot-check.h b/xen/include/xen/boot-check.h new file mode 100644 index 000000000000..250f9a40d3b0 --- /dev/null +++ b/xen/include/xen/boot-check.h @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* + * Helpers for boot-time checks of basic logic, including confirming that + * examples which should be calculated by the compiler are. + */ +#ifndef XEN_BOOT_CHECK_H +#define XEN_BOOT_CHECK_H + +#include <xen/lib.h> + +/* Hide a value from the optimiser. */ +#define HIDE(x) \ + ({ typeof(x) _x = (x); asm volatile ( "" : "+r" (_x) ); _x; }) + +/* + * Check that fn(val) can be calcuated by the compiler, and that it gives the + * expected answer. + * + * Clang < 8 can't fold constants through static inlines, causing this to + * fail. Simply skip it for incredibly old compilers. + */ +#if !CONFIG_CC_IS_CLANG || CONFIG_CLANG_VERSION >= 80000 +#define COMPILE_CHECK(fn, val, res) \ + do { \ + typeof(fn(val)) real = fn(val); \ + \ + if ( !__builtin_constant_p(real) ) \ + asm ( ".error \"'" STR(fn(val)) "' not compile-time constant\"" ); \ + else if ( real != res ) \ + asm ( ".error \"Compile time check '" STR(fn(val) == res) "' failed\"" ); \ + } while ( 0 ) +#else +#define COMPILE_CHECK(fn, val, res) +#endif + +/* + * Check that Xen's runtime logic for fn(val) gives the expected answer. This + * requires using HIDE() to prevent the optimiser from collapsing the logic + * into a constant. + */ +#define RUNTIME_CHECK(fn, val, res) \ + do { \ + typeof(fn(val)) real = fn(HIDE(val)); \ + \ + if ( real != res ) \ + panic("%s: %s(%s) expected %u, got %u\n", \ + __func__, #fn, #val, real, res); \ + } while ( 0 ) + +/* + * Perform compiletime and runtime checks for fn(val) == res. + */ +#define CHECK(fn, val, res) \ + do { \ + COMPILE_CHECK(fn, val, res); \ + RUNTIME_CHECK(fn, val, res); \ + } while ( 0 ) + +#endif /* XEN_BOOT_CHECK_H */ diff --git a/xen/include/xen/compiler.h b/xen/include/xen/compiler.h index 179ff23e62c5..444bf80142c7 100644 --- a/xen/include/xen/compiler.h +++ b/xen/include/xen/compiler.h @@ -86,7 +86,8 @@ #define inline inline __init #endif -#define __attribute_pure__ __attribute__((__pure__)) +#define __constructor __attribute__((__constructor__)) cf_check +#define __pure __attribute__((__pure__)) #define __attribute_const__ __attribute__((__const__)) #define __transparent__ __attribute__((__transparent_union__)) -- 2.30.2