Skip to contentSkip to conversation

qom: pass TargetInfo to is_available

PATCH In review Yonggang Luo 2 participants 4 messages Sep 3 – Sep 4 Arm/Aspeed machine support

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 694481ae3ec..592c04f1e79 100644
@@ -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
index cf42aabbc8b..61404baa4b5 100644
@@ -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
index b7024c9faef..2a19918ae08 100644
@@ -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
index 7ecd0f210fb..49956388098 100644
@@ -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
index 60a695b7ca3..36d5c6ded4f 100644
@@ -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
index b1834a57cc9..c5a1bd74a38 100644
@@ -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
index 6965e4d1169..aba1f954496 100644
@@ -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
#2 lore raw

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
index 73362b60d2a..8a7c6a116ac 100644
@@ -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
index 460c9ff0d2e..02a8cff1d9d 100644
@@ -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
index fadbacb2534..22b06c6ca22 100644
@@ -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
index 7d3eed96304..b47081200e1 100644
@@ -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
index b23e5bc5865..5d8cd3439a3 100644
@@ -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
index 49956388098..08d47c632a0 100644
@@ -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 TypeInfo
     void (*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
index c5a1bd74a38..5d959743169 100644
@@ -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
index 012b384036d..84effb50ca9 100644
@@ -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
#3 lore raw
Quoted 53 lines · 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(-)
include/qom/object.h +5 -3
index 49956388098..08d47c632a0 100644
@@ -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 TypeInfo
      void (*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
index c5a1bd74a38..5d959743169 100644

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 -0
          return NULL;
      }