Register accel-$cpu_type for all targets
Subsystems (2)
v3 cover Register accel-$cpu_type for all targets
A multi-target system binary links more than one target, so each one needs an accel-$cpu_type parent. accel-cpu-target.c registers that parent from a static type_init. The name is "accel-" plus CPU_RESOLVING_TYPE. System targets for arm, riscv, sh4, and xtensa share one CPU_RESOLVING_TYPE, so those bases build the file once per TARGET_BASE_ARCH and link that object into every system target of the base. User-mode and every other system target compile it once per target. The constructor is static, so more than one copy can be linked.
test-multi-target checks that each target's accel parent exists after QOM init.
This is an alternative to gating accelerator CPU types with is_available. The RISC-V TCG and KVM gates are reverted here as redundant.
v2: https://lore.kernel.org/qemu-devel/[email protected]/
Changes v2 -> v3:
- Drop accel_register_cpu_type() and the bool array keyed by SysEmuTarget. Register the parent from a static type_init in accel-cpu-target.c
- Build that file once per TARGET_BASE_ARCH for system targets that share CPU_RESOLVING_TYPE (arm, riscv, sh4, and xtensa), and link that one object into every system target of the base. v2 compiled accel-target.c once per target and skipped a second register
- Compile it once per target for user-mode and for every other system target. The static constructor lets a multi-target binary link those copies when the type names differ
- Check the accel parent from test_qom_types_are_unique(). v2 used a separate accel-cpu-parent-per-target test
Yonggang Luo (3):
accel: register CPU parent once per target base arch Revert "target/riscv/kvm: restrict kvm-accel-riscv-cpu to riscv
targets"
Revert "target/riscv/tcg: restrict tcg-accel-riscv-cpu to riscv
targets"
accel/accel-common.c | 15 --------------- accel/accel-cpu-target.c | 27 +++++++++++++++++++++++++++ meson.build | 32 ++++++++++++++++++++++++++++++++ target/riscv/kvm/kvm-cpu.c | 2 -- target/riscv/tcg/tcg-cpu.c | 1 - tests/unit/test-multi-target.c | 10 ++++++++++ 6 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 accel/accel-cpu-target.c
Signature · 1 line
2.55.0.windows.3
PATCH v3 1/3 accel: register CPU parent once per target base arch
Move accel-$cpu_type registration into accel-cpu-target.c. System targets whose variants share CPU_RESOLVING_TYPE (arm, riscv, sh4, and xtensa) build that file once per TARGET_BASE_ARCH. User-mode and every other system target compile it once per target.
test-multi-target checks that each target's accel parent exists after QOM init.
--- accel/accel-common.c | 15 --------------- accel/accel-cpu-target.c | 27 +++++++++++++++++++++++++++ meson.build | 32 ++++++++++++++++++++++++++++++++ tests/unit/test-multi-target.c | 10 ++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 accel/accel-cpu-target.c
accel/accel-common.c +0 -15
@@ -129,18 +129,3 @@ static const TypeInfo accel_types[] = { };DEFINE_TYPES (accel_types) - -static void register_accel_target_type(void) -{ - g_autofree char *name = g_strconcat("accel-", target_cpu_type(), NULL); -const TypeInfo accel_cpu_type = { - .name = name, - .parent =TYPE_OBJECT , - .abstract = true, - .class_size =sizeof (AccelCPUClass), - }; - -type_register_static (&accel_cpu_type); -} - -type_init (register_accel_target_type);
accel/accel-cpu-target.c new file +27 -0
@@ -0,0 +1,27 @@ +/* +* Accelerator CPU parent registration. +* +* Compiled once per target. System targets for arm, riscv, sh4, and +* xtensa share one CPU_RESOLVING_TYPE, so those bases compile it once. +* The constructor is static, so more than one copy can be linked. +* +* SPDX-License-Identifier: MIT +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "accel/accel-cpu-target.h" + +static void register_accel_target_type (void ) +{ +const TypeInfo accel_cpu_type = { + .name =TYPE_ACCEL_CPU , + .parent =TYPE_OBJECT , + .abstract = true, + .class_size =sizeof (AccelCPUClass), + }; + +type_register_static (&accel_cpu_type); +} + +type_init (register_accel_target_type);
meson.build +32 -0
@@ -3243,6 +3243,11 @@ config_devices_h = {} config_target_h = {} config_target_mak = {} config_base_arch_mak = {} +# System targets of these bases share one CPU_RESOLVING_TYPE. +# i386/x86_64, ppc/ppc64, mips/mips64, and sparc/sparc64 do not. +# Once they share one name too, drop this workaround. +config_base_arch_cpu_type = ['arm', 'riscv', 'sh4', 'xtensa'] +config_base_arch_target = {} config_target_info = {} disassemblers = {
@@ -3443,6 +3448,10 @@ foreach target : target_dirs target_base_arch = config_target['TARGET_BASE_ARCH'] config_base_arch = config_base_arch_mak.get(target_base_arch, {}) + config_target config_base_arch_mak += {target_base_arch: config_base_arch} + if (target.endswith('-softmmu') and + target_base_arch in config_base_arch_cpu_type) + config_base_arch_target += {target_base_arch: target} + endif endforeach target_dirs = actual_target_dirs
@@ -4150,6 +4159,7 @@ common_all = static_library('common', target_common_arch_libs = {} target_common_system_arch_libs = {} target_stubs_arch_libs = {} +target_cpu_libs = {} foreach target_base_arch, config_base_arch : config_base_arch_mak target_inc = [include_directories('target' / target_base_arch)] inc = [common_user_inc + target_inc]
@@ -4219,6 +4229,23 @@ foreach target_base_arch, config_base_arch : config_base_arch_mak c_args: target_system_c_args) target_stubs_arch_libs += {target_base_arch: lib} endif + + if target_base_arch in config_base_arch_target + accel_target = config_base_arch_target[target_base_arch] + target_cpu_libs += { + target_base_arch: static_library( + 'accel-cpu-type-' + target_base_arch, + files('accel/accel-cpu-target.c'), + config_target_h[accel_target], + genh, + build_by_default: false, + include_directories: inc, + c_args: ['-DCOMPILING_PER_TARGET', + '-DCONFIG_TARGET="@[email protected]"'.format(accel_target), + '-DTARGET_ARCH_PREFIX=target_' + target_base_arch.to_lower()], + dependencies: common_deps + system_deps) + } + endif endforeach if have_rust
@@ -4390,6 +4417,11 @@ foreach target : target_dirs target_common = common_ss.apply(config_target, strict: false) objects = [common_all.extract_objects(target_common.sources())] arch_deps += target_common.dependencies() + if target_type == 'system' and target_base_arch in target_cpu_libs + objects += target_cpu_libs[target_base_arch].extract_all_objects(recursive: false) + else + arch_srcs += files('accel/accel-cpu-target.c') + endif if target_base_arch in target_common_arch_libs src = target_common_arch[target_base_arch].apply(config_target, strict: false) lib = target_common_arch_libs[target_base_arch]
tests/unit/test-multi-target.c +10 -0
@@ -30,6 +30,13 @@/* stub for system/main.c */ int (*qemu_main)(void ); +static void test_accel_cpu_parent (const TargetInfo *ti) +{ + g_autofree char *name = g_strconcat("accel-", ti->cpu_type, NULL); + +g_assert_nonnull (object_class_by_name (name)); +} +static void test_qom_types_are_unique (const void *data) {const TargetInfo *ti = data;
@@ -49,6 +56,9 @@ static void test_qom_types_are_unique(const void *data)module_call_init (MODULE_INIT_QOM );/* trigger type_initialize for all types */ object_class_get_list ("" , false); + +/* Accel parent lookup depends on the QOM types above being unique. */ +test_accel_cpu_parent (ti); }int main (int argc,char **argv)
Signature · 1 line
2.55.0.windows.3
Re: [PATCH v3 1/3] accel: register CPU parent once per target base arch
Quoted 78 lines · Yonggang Luo
Move accel-$cpu_type registration into
accel-cpu-target.c. System targets whose variants shareCPU_RESOLVING_TYPE(arm, riscv, sh4, and xtensa) build that file once perTARGET_BASE_ARCH. User-mode and every other system target compile it once per target.test-multi-target checks that each target's accel parent exists after QOM init.
Signed-off-by Yonggang Luo <[email protected]>--- accel/accel-common.c | 15 --------------- accel/accel-cpu-target.c | 27 +++++++++++++++++++++++++++ meson.build | 32 ++++++++++++++++++++++++++++++++ tests/unit/test-multi-target.c | 10 ++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-)create mode 100644
accel/accel-cpu-target.caccel/accel-common.c +0
-15@@ -129,18 +129,3 @@ static const TypeInfo accel_types[] = { };DEFINE_TYPES (accel_types) - -static void register_accel_target_type(void) -{ - g_autofree char *name = g_strconcat("accel-", target_cpu_type(), NULL); -const TypeInfo accel_cpu_type = { - .name = name, - .parent =TYPE_OBJECT , - .abstract = true, - .class_size =sizeof (AccelCPUClass), - }; - -type_register_static (&accel_cpu_type); -} - -type_init (register_accel_target_type);accel/accel-cpu-target.c new file +27
-0@@ -0,0 +1,27 @@ +/* +* Accelerator CPU parent registration. +* +* Compiled once per target. System targets for arm, riscv, sh4, and +* xtensa share one CPU_RESOLVING_TYPE, so those bases compile it once. +* The constructor is static, so more than one copy can be linked. +* +* SPDX-License-Identifier: MIT +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "accel/accel-cpu-target.h" + +static void register_accel_target_type (void ) +{ +const TypeInfo accel_cpu_type = { + .name =TYPE_ACCEL_CPU , + .parent =TYPE_OBJECT , + .abstract = true, + .class_size =sizeof (AccelCPUClass), + }; + +type_register_static (&accel_cpu_type); +} + +type_init (register_accel_target_type);meson.build +0
-0
meson.build config_devices_h = {} · line 3252
Quoted 10 lines
meson.build +5
-0@@ -3243,6 +3243,11 @@ config_devices_h = {} config_target_h = {} config_target_mak = {} config_base_arch_mak = {} +# System targets of these bases share one CPU_RESOLVING_TYPE. +# i386/x86_64, ppc/ppc64, mips/mips64, and sparc/sparc64 do not. +# Once they share one name too, drop this workaround. +config_base_arch_cpu_type = ['arm', 'riscv', 'sh4', 'xtensa'] +config_base_arch_target = {} config_target_info = {}
It's basically the same implementation that v2, except the loop is in build system.
Could we express that for each architecture instead of a loop like this at the top?
Like having a .c file for each target, reusing a macro (or a .c.inc) with a parameter for cpu type, being either CPU_RESOLVING_TYPE, or some specific ones for the arch mentioned above. We have the common_* libs for target that share the same type among variants.
Quoted 84 lines
meson.build +27
-0disassemblers = {@@ -3443,6 +3448,10 @@ foreach target : target_dirs target_base_arch = config_target['TARGET_BASE_ARCH'] config_base_arch = config_base_arch_mak.get(target_base_arch, {}) + config_target config_base_arch_mak += {target_base_arch: config_base_arch} + if (target.endswith('-softmmu') and + target_base_arch in config_base_arch_cpu_type) + config_base_arch_target += {target_base_arch: target} + endif endforeach target_dirs = actual_target_dirs@@ -4150,6 +4159,7 @@ common_all = static_library('common', target_common_arch_libs = {} target_common_system_arch_libs = {} target_stubs_arch_libs = {} +target_cpu_libs = {} foreach target_base_arch, config_base_arch : config_base_arch_mak target_inc = [include_directories('target' / target_base_arch)] inc = [common_user_inc + target_inc]@@ -4219,6 +4229,23 @@ foreach target_base_arch, config_base_arch : config_base_arch_mak c_args: target_system_c_args) target_stubs_arch_libs += {target_base_arch: lib} endif + + if target_base_arch in config_base_arch_target + accel_target = config_base_arch_target[target_base_arch] + target_cpu_libs += { + target_base_arch: static_library( + 'accel-cpu-type-' + target_base_arch, + files('accel/accel-cpu-target.c'), + config_target_h[accel_target], + genh, + build_by_default: false, + include_directories: inc, + c_args: ['-DCOMPILING_PER_TARGET', + '-DCONFIG_TARGET="@[email protected]"'.format(accel_target), + '-DTARGET_ARCH_PREFIX=target_' + target_base_arch.to_lower()], + dependencies: common_deps + system_deps) + } + endif endforeach if have_rust@@ -4390,6 +4417,11 @@ foreach target : target_dirs target_common = common_ss.apply(config_target, strict: false) objects = [common_all.extract_objects(target_common.sources())] arch_deps += target_common.dependencies() + if target_type == 'system' and target_base_arch in target_cpu_libs + objects += target_cpu_libs[target_base_arch].extract_all_objects(recursive: false) + else + arch_srcs += files('accel/accel-cpu-target.c') + endif if target_base_arch in target_common_arch_libs src = target_common_arch[target_base_arch].apply(config_target, strict: false) lib = target_common_arch_libs[target_base_arch]tests/unit/test-multi-target.c +10
-0@@ -30,6 +30,13 @@/* stub for system/main.c */ int (*qemu_main)(void ); +static void test_accel_cpu_parent (const TargetInfo *ti) +{ + g_autofree char *name = g_strconcat("accel-", ti->cpu_type, NULL); + +g_assert_nonnull (object_class_by_name (name)); +} +static void test_qom_types_are_unique (const void *data) {const TargetInfo *ti = data;@@ -49,6 +56,9 @@ static void test_qom_types_are_unique(const void *data)module_call_init (MODULE_INIT_QOM );/* trigger type_initialize for all types */ object_class_get_list ("" , false); + +/* Accel parent lookup depends on the QOM types above being unique. */ +test_accel_cpu_parent (ti); }int main (int argc,char **argv)
PATCH v3 2/3 Revert "target/riscv/kvm: restrict kvm-accel-riscv-cpu to riscv targets"
In reply to Yonggang LuoThis is redundant now as all accel-$cpu_type parent is already registered.
This reverts commit 2d3e6b4003d5380c245ce8ffac4b8866f6d10a33.
--- target/riscv/kvm/kvm-cpu.c | 2 -- 1 file changed, 2 deletions(-)
target/riscv/kvm/kvm-cpu.c +0 -2
@@ -26,7 +26,6 @@#include "qapi/error.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" -#include "qemu/target-info.h" #include "qapi/visitor.h" #include "system/system.h" #include "system/kvm.h"
@@ -2236,7 +2235,6 @@ static const TypeInfo kvm_cpu_accel_type_info = { .parent = TYPE_ACCEL_CPU, .class_init = kvm_cpu_accel_class_init, - .is_available = target_base_riscv, .abstract = true, };static void kvm_cpu_accel_register_types (void )
Signature · 1 line
2.55.0.windows.3
PATCH v3 3/3 Revert "target/riscv/tcg: restrict tcg-accel-riscv-cpu to riscv targets"
In reply to Yonggang LuoThis is redundant now as all accel-$cpu_type parent is already registered.
This reverts commit ec65e16fd2ba012d57b1bbfcfcd04c31c4111924.
--- target/riscv/tcg/tcg-cpu.c | 1 - 1 file changed, 1 deletion(-)
target/riscv/tcg/tcg-cpu.c +0 -1
@@ -1754,7 +1754,6 @@ static const TypeInfo riscv_tcg_cpu_accel_type_info = { .parent = TYPE_ACCEL_CPU, .class_init = riscv_tcg_cpu_accel_class_init, - .is_available = target_base_riscv, .abstract = true, };
Signature · 1 line
2.55.0.windows.3