@samitouri / QOSamiQemu / commits / 0340d819d0

target-info: introduce TargetInfo in QOM

For the single-binary, we want to be able to retrieve at runtime the current target among the different ones available. A consequence is that we can't rely on existing target_info() definition since it will create a conflict once more than one target is available. To solve this, we add TargetInfo in QOM, with this hierarchy. We define one class "target-info-X" per target, that inherits from abstract class "target-info". Using concrete vs abstract class ensure we can easily filter "target-info-X" from all QOM types. Associated TargetInfo is directly set through class initialization, without relying on any instance. For user mode, we simply define target_info() like it was done previously. In this patch, we keep the same definition for system-mode also, and it will be replaced in next commits. We will introduce detection of target from QOM, so we need to make sure those types are registered early. Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Link: https://lore.kernel.org/qemu-devel/20260514172303.1484273-4-pierrick.bouvier@oss.qualcomm.com Signed-off-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>

Pierrick Bouvier committed May 14, 2026 at 10:23 UTC 0340d819d08dcb6694543aaa02af9478c51de1a2
9 files changed +120 -12
configs/targets/aarch64-softmmu.c
+2 -4
@@ -8,6 +8,7 @@
8
9 #include "qemu/osdep.h"
10 #include "qemu/target-info-impl.h"
11 +#include "qemu/target-info-init.h"
12 #include "hw/arm/machines-qom.h"
13 #include "target/arm/cpu-qom.h"
14 #include "target/arm/cpu-param.h"
@@ -23,7 +24,4 @@ static const TargetInfo target_info_aarch64_system = {
24 .page_bits_init = TARGET_PAGE_BITS_LEGACY,
25 };
26
26 -const TargetInfo *target_info(void)
27 -{
28 - return &target_info_aarch64_system;
29 -}
27 +target_info_init(target_info_aarch64_system)
configs/targets/arm-softmmu.c
+2 -4
@@ -8,6 +8,7 @@
8
9 #include "qemu/osdep.h"
10 #include "qemu/target-info-impl.h"
11 +#include "qemu/target-info-init.h"
12 #include "hw/arm/machines-qom.h"
13 #include "target/arm/cpu-qom.h"
14 #include "target/arm/cpu-param.h"
@@ -23,7 +24,4 @@ static const TargetInfo target_info_arm_system = {
24 .page_bits_init = TARGET_PAGE_BITS_LEGACY,
25 };
26
26 -const TargetInfo *target_info(void)
27 -{
28 - return &target_info_arm_system;
29 -}
27 +target_info_init(target_info_arm_system)
include/qemu/module.h
+1
@@ -43,6 +43,7 @@ typedef enum {
43 MODULE_INIT_MIGRATION,
44 MODULE_INIT_BLOCK,
45 MODULE_INIT_OPTS,
46 + MODULE_INIT_TARGET_INFO,
47 MODULE_INIT_QOM,
48 MODULE_INIT_TRACE,
49 MODULE_INIT_XEN_BACKEND,
include/qemu/target-info-init.h new
+58
@@ -0,0 +1,58 @@
1 +/*
2 + * QEMU target info initialization
3 + *
4 + * Copyright (c) Qualcomm
5 + *
6 + * SPDX-License-Identifier: GPL-2.0-or-later
7 + *
8 + * This file is included by each file defining a TargetInfo structure and is
9 + * responsible for registering it.
10 + */
11 +
12 +#ifndef QEMU_TARGET_INFO_INIT_H
13 +#define QEMU_TARGET_INFO_INIT_H
14 +
15 +#define DEFINE_TARGET_INFO_TYPE(info) \
16 +static void do_qemu_init_target_info(void) \
17 +{ \
18 + type_register_static(&info); \
19 +} \
20 +module_init(do_qemu_init_target_info, MODULE_INIT_TARGET_INFO)
21 +
22 +#ifdef COMPILING_PER_TARGET
23 +#ifdef CONFIG_USER_ONLY
24 +
25 +/*
26 + * User mode does not support multiple targets in the same binary, so just
27 + * define target_info().
28 + */
29 +#define target_info_init(ti_var) \
30 +const TargetInfo *target_info(void) \
31 +{ \
32 + return &ti_var; \
33 +}
34 +
35 +#else /* CONFIG_USER_ONLY */
36 +
37 +#include "qemu/target-info-qom.h"
38 +#include "qom/object.h"
39 +
40 +#define target_info_init(ti_var) \
41 +const TargetInfo *target_info(void) \
42 +{ \
43 + return &ti_var; \
44 +} \
45 + \
46 +static const TypeInfo target_info_qom_target_type_info = { \
47 + .name = TYPE_TARGET_INFO"-"TARGET_NAME, \
48 + .parent = TYPE_TARGET_INFO, \
49 + .instance_size = sizeof(TargetInfoQom), \
50 + .class_size = sizeof(TargetInfoQomClass), \
51 + .class_data = &ti_var, \
52 +}; \
53 +DEFINE_TARGET_INFO_TYPE(target_info_qom_target_type_info)
54 +
55 +#endif /* CONFIG_USER_ONLY */
56 +#endif /* COMPILING_PER_TARGET */
57 +
58 +#endif /* QEMU_TARGET_INFO_INIT_H */
include/qemu/target-info-qom.h new
+28
@@ -0,0 +1,28 @@
1 +/*
2 + * QEMU target info QOM types
3 + *
4 + * Copyright (c) Qualcomm
5 + *
6 + * SPDX-License-Identifier: GPL-2.0-or-later
7 + */
8 +
9 +#ifndef QEMU_TARGET_INFO_QOM_H
10 +#define QEMU_TARGET_INFO_QOM_H
11 +
12 +#include "qemu/target-info-impl.h"
13 +#include "qom/object.h"
14 +
15 +#define TYPE_TARGET_INFO "target-info"
16 +
17 +typedef struct TargetInfoQom {
18 + Object parent_obj;
19 +} TargetInfoQom;
20 +
21 +typedef struct TargetInfoQomClass {
22 + ObjectClass parent_class;
23 + const TargetInfo *target_info;
24 +} TargetInfoQomClass;
25 +
26 +OBJECT_DECLARE_TYPE(TargetInfoQom, TargetInfoQomClass, TARGET_INFO)
27 +
28 +#endif /* QEMU_TARGET_INFO_QOM_H */
system/vl.c
+2
@@ -2889,6 +2889,8 @@ void qemu_init(int argc, char **argv)
2889
2890 os_setup_limits();
2891
2892 + module_call_init(MODULE_INIT_TARGET_INFO);
2893 +
2894 module_init_info(qemu_modinfo);
2895 module_allow_arch(target_name());
2896
target-info-qom.c
+24
@@ -7,7 +7,11 @@
7 */
8
9 #include "qemu/osdep.h"
10 +#include "qapi/error.h"
11 #include "qom/object.h"
12 +#include "qemu/target-info-impl.h"
13 +#include "qemu/target-info-init.h"
14 +#include "qemu/target-info-qom.h"
15 #include "hw/arm/machines-qom.h"
16
17 static const TypeInfo target_info_types[] = {
@@ -22,3 +26,23 @@ static const TypeInfo target_info_types[] = {
26 };
27
28 DEFINE_TYPES(target_info_types)
29 +
30 +static void target_info_qom_class_init(ObjectClass *oc, const void * data)
31 +{
32 + TargetInfoQomClass *klass = TARGET_INFO_CLASS(oc);
33 + klass->target_info = data;
34 +}
35 +
36 +static const TypeInfo target_info_parent_type = {
37 + .name = TYPE_TARGET_INFO,
38 + .parent = TYPE_OBJECT,
39 + .instance_size = sizeof(TargetInfoQom),
40 + .class_size = sizeof(TargetInfoQomClass),
41 + /* use class_base_init so children classes can set class_data accordingly */
42 + .class_base_init = target_info_qom_class_init,
43 + /* children classes will be concrete, which allows to easily query them
44 + * without listing this parent class also */
45 + .abstract = true,
46 +};
47 +
48 +DEFINE_TARGET_INFO_TYPE(target_info_parent_type)
target-info-stub.c
+2 -4
@@ -9,6 +9,7 @@
9 #include "qemu/osdep.h"
10 #include "qemu/target-info.h"
11 #include "qemu/target-info-impl.h"
12 +#include "qemu/target-info-init.h"
13 #include "hw/core/boards.h"
14 #include "cpu.h"
15 #include "exec/cpu-defs.h"
@@ -41,7 +42,4 @@ static const TargetInfo target_info_stub = {
42 #endif
43 };
44
44 -const TargetInfo *target_info(void)
45 -{
46 - return &target_info_stub;
47 -}
45 +target_info_init(target_info_stub)
tests/qtest/fuzz/fuzz.c
+1
@@ -172,6 +172,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv, char ***envp)
172
173 /* Initialize qgraph and modules */
174 qos_graph_init();
175 + module_call_init(MODULE_INIT_TARGET_INFO);
176 module_call_init(MODULE_INIT_FUZZ_TARGET);
177 module_call_init(MODULE_INIT_QOM);
178 module_call_init(MODULE_INIT_LIBQOS);