qom: pass TargetInfo to is_available
cover qom: pass TargetInfo to is_available
TypeInfo.is_available currently has no TargetInfo argument, so QOM cannot ask a type whether it is available for the selected target without a wrapper around target_aarch64() or similar. This series moves target selection into QOM and passes TargetInfo into is_available.
- Move target_info() from target-info-qom.c into qom/object.c so QOM can read the current TargetInfo without a target-info-qom cycle. Add target_info_select() to install the TargetInfo once. target_info() is NULL until that call. Keep TargetInfo as an incomplete type in object.h. User-mode binaries select the sole TargetInfo from MODULE_INIT_TARGET_INFO, which now runs before MODULE_INIT_QOM.
- Change TypeInfo.is_available to take const TargetInfo *. Add target_is_riscv64() and target_is_aarch64() for TypeInfo callbacks. Keep target_riscv64() and target_aarch64() as wrappers around the current TargetInfo. Point existing ARM machine is_available callbacks at target_is_aarch64().
Yonggang Luo (2):
target-info: move selection into QOM
qom: pass TargetInfo to is_available
bsd-user/main.c | 1 + hw/arm/aspeed_ast27x0-fc.c | 2 +- hw/arm/aspeed_ast27x0_evb.c | 4 ++-- hw/arm/raspi.c | 4 ++-- hw/arm/raspi4b.c | 2 +- include/qemu/target-info-impl.h | 13 ++++--------- include/qemu/target-info-init.h | 18 ++++++++++-------- include/qemu/target-info.h | 18 ++++++++++++++++++ include/qom/object.h | 26 +++++++++++++++++++++++--- linux-user/main.c | 1 + qom/object.c | 15 ++++++++++++++- target-info-qom.c | 9 +-------- target-info.c | 14 ++++++++++++-- 13 files changed, 90 insertions(+), 37 deletions(-)
Signature · 1 line
2.52.0.windows.1
PATCH 1/2 target-info: move selection into QOM
- Move target_info() from target-info-qom.c into qom/object.c so QOM can read the current TargetInfo without a target-info-qom cycle.
- Add target_info_select() to install the TargetInfo once. Return NULL from target_info() until it is called.
- Keep TargetInfo as an incomplete type in object.h; define the struct only in target-info-impl.h.
- Select the sole user-mode TargetInfo from MODULE_INIT_TARGET_INFO instead of defining target_info() in that binary.
- Have target_info_qom_set_target() call target_info_select().
- Call MODULE_INIT_TARGET_INFO before MODULE_INIT_QOM in linux-user and bsd-user so target_info() is set before QOM class_init.
--- bsd-user/main.c | 1 + include/qemu/target-info-impl.h | 13 ++++--------- include/qemu/target-info-init.h | 18 ++++++++++-------- include/qom/object.h | 18 ++++++++++++++++++ linux-user/main.c | 1 + qom/object.c | 13 +++++++++++++ target-info-qom.c | 9 +-------- 7 files changed, 48 insertions(+), 25 deletions(-)
bsd-user/main.c +1 -0
@@ -285,6 +285,7 @@ int main(int argc, char **argv)error_init (argv[0 ]);module_call_init (MODULE_INIT_TRACE );qemu_init_cpu_list (); +module_call_init (MODULE_INIT_TARGET_INFO );module_call_init (MODULE_INIT_QOM ); envlist =envlist_create ();
include/qemu/target-info-impl.h +4 -9
@@ -9,10 +9,12 @@ #ifndef QEMU_TARGET_INFO_IMPL_H #define QEMU_TARGET_INFO_IMPL_H +#include "qemu/target-info.h" +#include "qom/object.h" #include "qapi/qapi-types-common.h" #include "qapi/qapi-types-machine.h" -typedef struct TargetInfo { +struct TargetInfo { /* runtime equivalent of TARGET_NAME definition */ const char *target_name; /* related to TARGET_ARCH definition */
@@ -30,13 +32,6 @@ typedef struct TargetInfo {unsigned page_bits_init;/* runtime equivalent of TARGET_PAGE_BITS_VARY definition */ bool page_bits_vary; -} TargetInfo; - -/** -* target_info: -* -* Returns: The TargetInfo structure definition for this target binary. -*/ -const TargetInfo *target_info (void ); +};#endif
include/qemu/target-info-init.h +10 -8
@@ -12,6 +12,8 @@#ifndef QEMU_TARGET_INFO_INIT_H #define QEMU_TARGET_INFO_INIT_H +#include "qom/object.h" +#define DEFINE_TARGET_INFO_TYPE (info) \ static void do_qemu_init_target_info(void) \ { \
@@ -23,19 +25,19 @@ module_init(do_qemu_init_target_info, MODULE_INIT_TARGET_INFO)#ifdef CONFIG_USER_ONLY /* -* User mode does not support multiple targets in the same binary, so just -* define target_info(). +* User mode does not support multiple targets in the same binary. +* Install the sole TargetInfo before MODULE_INIT_QOM. */ -#define target_info_init (ti_var) \ -const TargetInfo *target_info(void) \ -{ \ - return &ti_var; \ -} +#define target_info_init (ti_var) \ +static void do_qemu_init_target_info(void) \ +{ \ + target_info_select(&ti_var); \ +} \ +module_init(do_qemu_init_target_info, MODULE_INIT_TARGET_INFO)#else /* CONFIG_USER_ONLY */ #include "qemu/target-info-qom.h" -#include "qom/object.h" #define target_info_init (ti_var) \ static const TypeInfo target_info_qom_target_type_info = { \
include/qom/object.h +18 -0
@@ -117,6 +117,24 @@ typedef void (ObjectUnparent)(Object *obj);*/ typedef void (ObjectFree )(void *obj); +typedef struct TargetInfo TargetInfo ; + +/** +* target_info: +* +* Returns: The current TargetInfo, or NULL before target_info_select(). +*/ +const TargetInfo *target_info (void ); + +/** +* target_info_select: +* @ti: TargetInfo to select +* +* Sets the TargetInfo returned by target_info(). Must be called only +* once. @ti must not be NULL. +*/ +void target_info_select (const TargetInfo *ti); +#define OBJECT_CLASS_CAST_CACHE 4/**
linux-user/main.c +1 -0
@@ -700,6 +700,7 @@ int main(int argc, char **argv, char **envp)error_init (argv[0 ]);module_call_init (MODULE_INIT_TRACE );qemu_init_cpu_list (); +module_call_init (MODULE_INIT_TARGET_INFO );module_call_init (MODULE_INIT_QOM ); envlist =envlist_create ();
qom/object.c +13 -0
@@ -15,6 +15,7 @@#include "qom/compat-properties.h" #include "qom/object.h" #include "qom/object_interfaces.h" +#include "qemu/target-info-impl.h" #include "qemu/cutils.h" #include "qemu/memalign.h" #include "qapi/visitor.h"
@@ -83,6 +84,18 @@ static GHashTable *type_table;static bool enumerating_types; +static const TargetInfo *target_info_ptr; + +const TargetInfo *target_info (void ) +{ +return target_info_ptr; +} + +void target_info_select (const TargetInfo *ti) +{ + target_info_ptr = ti; +} +static void type_table_add (TypeImpl *ti) {assert (!enumerating_types);
target-info-qom.c +1 -8
@@ -33,13 +33,6 @@ static const TypeInfo target_info_parent_type = {DEFINE_TARGET_INFO_TYPE (target_info_parent_type) -static const TargetInfo *target_info_ptr; - -const TargetInfo *target_info (void ) -{ -return target_info_ptr; -} - void target_info_qom_set_target(void) {g_autoptr (GSList) targets =object_class_get_list (TYPE_TARGET_INFO , false);
@@ -51,5 +44,5 @@ void target_info_qom_set_target(void) "more than one target-info is available"); } - target_info_ptr =TARGET_INFO_CLASS (targets->data)->target_info; +target_info_select (TARGET_INFO_CLASS (targets->data)->target_info); }
Signature · 1 line
2.52.0.windows.1
PATCH 2/2 qom: pass TargetInfo to is_available
In reply to Yonggang Luo- Change TypeInfo.is_available to take const TargetInfo *.
- Pass target_info() from type_register_internal().
- Add target_is_riscv64() and target_is_aarch64() so TypeInfo can use them as is_available callbacks without wrappers.
- Keep target_riscv64() and target_aarch64() as wrappers around the current TargetInfo.
- Point existing ARM machine is_available callbacks at target_is_aarch64().
--- hw/arm/aspeed_ast27x0-fc.c | 2 +- hw/arm/aspeed_ast27x0_evb.c | 4 ++-- hw/arm/raspi.c | 4 ++-- hw/arm/raspi4b.c | 2 +- include/qemu/target-info.h | 18 ++++++++++++++++++ include/qom/object.h | 8 +++++--- qom/object.c | 2 +- target-info.c | 14 ++++++++++++-- 8 files changed, 42 insertions(+), 12 deletions(-)
hw/arm/aspeed_ast27x0-fc.c +1 -1
@@ -234,7 +234,7 @@ static const TypeInfo ast2700fc_types[] = { .parent = TYPE_MACHINE, .class_init = ast2700fc_class_init, .instance_size = sizeof(Ast2700FCState), - .is_available = target_aarch64, + .is_available = target_is_aarch64, }, };
hw/arm/aspeed_ast27x0_evb.c +2 -2
@@ -75,13 +75,13 @@ static const TypeInfo aspeed_ast27x0_evb_types[] = { .name = MACHINE_TYPE_NAME("ast2700a1-evb"), .parent = TYPE_ASPEED_MACHINE, .class_init = aspeed_machine_ast2700a1_evb_class_init, - .is_available = target_aarch64, + .is_available = target_is_aarch64, }, { .name = MACHINE_TYPE_NAME("ast2700a2-evb"), .parent = TYPE_ASPEED_MACHINE, .class_init = aspeed_machine_ast2700a2_evb_class_init, - .is_available = target_aarch64, + .is_available = target_is_aarch64, } };
hw/arm/raspi.c +2 -2
@@ -403,12 +403,12 @@ static const TypeInfo raspi_machine_types[] = { .name = MACHINE_TYPE_NAME("raspi3ap"), .parent = TYPE_RASPI_MACHINE, .class_init = raspi3ap_machine_class_init, - .is_available = target_aarch64, + .is_available = target_is_aarch64, }, { .name = MACHINE_TYPE_NAME("raspi3b"), .parent = TYPE_RASPI_MACHINE, .class_init = raspi3b_machine_class_init, - .is_available = target_aarch64, + .is_available = target_is_aarch64, }, { .name = TYPE_RASPI_MACHINE, .parent = TYPE_RASPI_BASE_MACHINE,
hw/arm/raspi4b.c +1 -1
@@ -125,7 +125,7 @@ static const TypeInfo raspi4b_machine_type = { .parent = TYPE_RASPI_BASE_MACHINE, .instance_size = sizeof(Raspi4bMachineState), .class_init = raspi4b_machine_class_init, - .is_available = target_aarch64, + .is_available = target_is_aarch64, };static void raspi4b_machine_register_type (void )
include/qemu/target-info.h +18 -0
@@ -9,6 +9,8 @@#ifndef QEMU_TARGET_INFO_H #define QEMU_TARGET_INFO_H +typedef struct TargetInfo TargetInfo ; +/** * target_name: *
@@ -56,6 +58,14 @@ bool target_base_arm(void);*/ bool target_arm (void ); +/** +* target_is_aarch64: +* @ti: TargetInfo to inspect +* +* Returns whether @ti is Aarch64. +*/ +bool target_is_aarch64 (const TargetInfo *ti); +/** * target_aarch64: *
@@ -91,6 +101,14 @@ bool target_ppc64(void);*/ bool target_s390x (void ); +/** +* target_is_riscv64: +* @ti: TargetInfo to inspect +* +* Returns whether @ti is riscv64. +*/ +bool target_is_riscv64 (const TargetInfo *ti); +/** * target_riscv64: *
include/qom/object.h +5 -3
@@ -487,8 +487,10 @@ struct Object* @class_data: Data to pass to the @class_init, * @class_base_init. This can be useful when building dynamic * classes. -* @is_available: callback invoked at registration time, to dynamically check if -* this type should be available or not. +* @is_available: Optional callback. After target_info_select(), QOM +* calls it at registration with target_info() and skips the type if +* it returns false. Callers may also invoke it with a chosen +* TargetInfo. * @interfaces: The list of interfaces associated with this type. This * should point to a static array that's terminated with a zero filled * element.
@@ -511,7 +513,7 @@ struct TypeInfovoid (*class_base_init)(ObjectClass *klass,const void *data);const void *class_data; -bool (*is_available)(void ); +bool (*is_available)(const TargetInfo *ti);const InterfaceInfo *interfaces; };
qom/object.c +1 -1
@@ -176,7 +176,7 @@ static TypeImpl *type_register_internal(const TypeInfo *info)abort (); } -if (info->is_available && !info->is_available ()) { +if (info->is_available && !info->is_available (target_info ())) {return NULL ; }
target-info.c +12 -2
@@ -58,9 +58,14 @@ bool target_arm(void)return target_arch () ==SYS_EMU_TARGET_ARM ; } +bool target_is_aarch64 (const TargetInfo *ti) +{ +return ti->target_arch ==SYS_EMU_TARGET_AARCH64 ; +} +bool target_aarch64 (void ) { -return target_arch () ==SYS_EMU_TARGET_AARCH64 ; +return target_is_aarch64 (target_info ()); }bool target_base_ppc (void )
@@ -89,7 +94,12 @@ bool target_s390x(void)return target_arch () ==SYS_EMU_TARGET_S390X ; } +bool target_is_riscv64 (const TargetInfo *ti) +{ +return ti->target_arch ==SYS_EMU_TARGET_RISCV64 ; +} +bool target_riscv64 (void ) { -return target_arch () ==SYS_EMU_TARGET_RISCV64 ; +return target_is_riscv64 (target_info ()); }
Signature · 1 line
2.52.0.windows.1
Quoted 53 lines · Yonggang Luo
- Change TypeInfo.is_available to take const TargetInfo *.
- Passtarget_info()fromtype_register_internal().
- Addtarget_is_riscv64()andtarget_is_aarch64()so TypeInfo can use them asis_availablecallbacks without wrappers.
- Keeptarget_riscv64()andtarget_aarch64()as wrappers around the current TargetInfo.
- Point existing ARM machineis_availablecallbacks attarget_is_aarch64().Signed-off-by Yonggang Luo <[email protected]>--- hw/arm/aspeed_ast27x0-fc.c | 2 +- hw/arm/aspeed_ast27x0_evb.c | 4 ++-- hw/arm/raspi.c | 4 ++-- hw/arm/raspi4b.c | 2 +- include/qemu/target-info.h | 18 ++++++++++++++++++ include/qom/object.h | 8 +++++--- qom/object.c | 2 +- target-info.c | 14 ++++++++++++-- 8 files changed, 42 insertions(+), 12 deletions(-)include/qom/object.h +5
-3@@ -487,8 +487,10 @@ struct Object* @class_data: Data to pass to the @class_init, * @class_base_init. This can be useful when building dynamic * classes. -* @is_available: callback invoked at registration time, to dynamically check if -* this type should be available or not. +* @is_available: Optional callback. After target_info_select(), QOM +* calls it at registration with target_info() and skips the type if +* it returns false. Callers may also invoke it with a chosen +* TargetInfo. * @interfaces: The list of interfaces associated with this type. This * should point to a static array that's terminated with a zero filled * element. @@ -511,7 +513,7 @@ struct TypeInfovoid (*class_base_init)(ObjectClass *klass,const void *data);const void *class_data; -bool (*is_available)(void ); +bool (*is_available)(const TargetInfo *ti);const InterfaceInfo *interfaces; };qom/object.c +0
-0
qom/object.c static TypeImpl *type_register_internal(const TypeInfo *info) · line 179
Quoted 5 lines
qom/object.c +1
-1@@ -176,7 +176,7 @@ static TypeImpl *type_register_internal(const TypeInfo *info)abort (); } -if (info->is_available && !info->is_available ()) { +if (info->is_available && !info->is_available (target_info ())) {
While this might work for the single binary, this doesn't scale to heterogeneous emulation, which is why we started the single binary milestone, and where we are heading. Let's think about the API we want to have it right.
Quoted 2 lines
qom/object.c +0
-0return NULL ; }