Add a write-once ABI byte-order setter for runtime-endian Linux-user targets.
Expose const data to readers and inline runtime endian checks, keeping its writable initializer outside LTO. Retain immutable data and compile-time byte swapping for fixed-endian targets. Restrict TARGET_RUNTIME_ENDIAN to Linux-user configurations. Inspired-by: Richard Henderson <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]> --- meson.build | 26 ++++++++++++++++++++++++-- include/exec/tswap.h | 11 +++++++---- include/qemu/target-info-def.h | 14 ++++++++++++++ include/qemu/target-info.h | 20 ++++++++++++++++++++ target-info-def.c | 19 +++++++++++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index d7bcb581c181..04cf5d77f188 100644 --- a/meson.build +++ b/meson.build @@ -3342,6 +3342,11 @@ foreach target : target_dirs config_target += keyval.load('configs/targets' / target + '.mak') + if 'TARGET_RUNTIME_ENDIAN' in config_target + assert(target.endswith('linux-user'), + 'Runtime target endianness is only supported for Linux-user') + endif + target_kconfig = [] foreach sym: accelerators if sym == 'CONFIG_TCG' or target in accelerator_targets.get(sym, []) @@ -4400,8 +4405,6 @@ foreach target : target_dirs endif endif - arch_srcs += files('target-info-def.c') - if target_base_arch in target_arch t = target_arch[target_base_arch].apply(config_target, strict: false) arch_srcs += t.sources() @@ -4470,6 +4473,25 @@ foreach target : target_dirs lib_deps += dep.partial_dependency(compile_args: true, includes: true) endforeach + # Keep the writable runtime-endian definition opaque to const readers, + # as for page-vary-common.c above. + target_info = files('target-info-def.c') + if get_option('b_lto') and 'TARGET_RUNTIME_ENDIAN' in config_target + target_info_flags = ['-fno-lto'] + if get_option('cfi') + target_info_flags += '-fno-sanitize=cfi-icall' + endif + target_info = static_library('target-info-' + target, + sources: target_info + genh + config_target_h[target], + dependencies: lib_deps, + include_directories: target_inc, + c_args: c_args + target_info_flags, + build_by_default: false) + objects += target_info.extract_all_objects(recursive: false) + else + arch_srcs += target_info + endif + lib = static_library('qemu-' + target, sources: arch_srcs + genh, dependencies: lib_deps, diff --git a/include/exec/tswap.h b/include/exec/tswap.h index 17ac5444544d..869619bac314 100644 --- a/include/exec/tswap.h +++ b/include/exec/tswap.h @@ -13,14 +13,17 @@ #include "hw/core/cpu.h" /* - * If we're in target-specific code, we can hard-code the swapping - * condition, otherwise we have to do (slower) run-time checks. + * For fixed-endian target-specific code, hard-code the swapping condition. + * Otherwise use run-time checks. */ #ifdef COMPILING_PER_TARGET +#ifndef TARGET_RUNTIME_ENDIAN #define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) -#else +#endif +#endif +#ifndef target_needs_bswap #define target_needs_bswap() (HOST_BIG_ENDIAN != target_big_endian()) -#endif /* COMPILING_PER_TARGET */ +#endif #if defined(CONFIG_USER_ONLY) \ || !defined(TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API) diff --git a/include/qemu/target-info-def.h b/include/qemu/target-info-def.h index 8019d2c7196e..34058955edec 100644 --- a/include/qemu/target-info-def.h +++ b/include/qemu/target-info-def.h @@ -32,6 +32,20 @@ typedef struct TargetInfo { bool page_bits_vary; } TargetInfo; +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +/* + * Linux-user selects the ELF byte order before creating CPUs or guest threads. + * The data is then immutable; its writable definition must stay outside LTO. + */ +#ifdef IN_TARGET_INFO_DEF +extern TargetInfo target_info_stub; +#else +extern const TargetInfo target_info_stub; +#endif +#endif +#endif + /** * target_info: * diff --git a/include/qemu/target-info.h b/include/qemu/target-info.h index 2cd4cc5671b5..6ed4cfda8d7b 100644 --- a/include/qemu/target-info.h +++ b/include/qemu/target-info.h @@ -9,6 +9,12 @@ #ifndef QEMU_TARGET_INFO_H #define QEMU_TARGET_INFO_H +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +#include "qemu/target-info-def.h" +#endif +#endif + /** * target_name: * @@ -40,7 +46,21 @@ const char *target_cpu_type(void); * the target, so please do *not* use this function unless you know very * well what you are doing! */ +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +static inline bool target_big_endian(void) +{ + return target_info_stub.endianness == ENDIAN_MODE_BIG; +} + +/* Select the Linux-user ABI byte order once, before CPUs or guest threads. */ +void target_set_big_endian(bool big_endian); +#else bool target_big_endian(void); +#endif +#else +bool target_big_endian(void); +#endif /** * target_heterogeneous: diff --git a/target-info-def.c b/target-info-def.c index bcf9cfe1e437..7580993e0bb3 100644 --- a/target-info-def.c +++ b/target-info-def.c @@ -6,6 +6,8 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ +#define IN_TARGET_INFO_DEF 1 + #include "qemu/osdep.h" #include "qemu/target-info.h" #include "qemu/target-info-def.h" @@ -24,7 +26,12 @@ QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState)); QEMU_BUILD_BUG_ON(TARGET_PAGE_BITS < TARGET_PAGE_BITS_MIN); #endif +#ifdef TARGET_RUNTIME_ENDIAN +/* This writable definition must not be compiled with -flto. */ +TargetInfo target_info_stub = { +#else static const TargetInfo target_info_stub = { +#endif .target_name = TARGET_NAME, .target_arch = glue(SYS_EMU_TARGET_, TARGET_ARCH_VARIANT), .long_bits = TARGET_LONG_BITS, @@ -42,3 +49,15 @@ static const TargetInfo target_info_stub = { }; target_info_init(target_info_stub) + +#ifdef TARGET_RUNTIME_ENDIAN +void target_set_big_endian(bool big_endian) +{ + static bool endian_decided; + + assert(!endian_decided); + endian_decided = true; + target_info_stub.endianness = big_endian ? ENDIAN_MODE_BIG + : ENDIAN_MODE_LITTLE; +} +#endif -- 2.55.0
