Skip to contentSkip to conversation

hw/core: Make machine-qmp-cmds.c target independent

PATCH New Thomas Huth 1 participant 4 messages Apr 24, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 hw/core: Use a callback for target specific query-cpus-fast information

In reply to Thomas Huth

For being able to create a universal QEMU binary one day, core files like machine-qmp-cmds.c must not contain any "#ifdef TARGET_..." parts. Thus let's provide the target specific function via a function pointer in CPUClass instead, as a first step towards making this file target independent.

---
 include/hw/core/cpu.h      |  4 ++++
 include/qemu/typedefs.h    |  1 +
 hw/core/machine-qmp-cmds.c | 16 ++--------------
 target/s390x/cpu.c         |  8 ++++++++
 4 files changed, 15 insertions(+), 14 deletions(-)
include/hw/core/cpu.h +4 -0
index 397fd3ac68..5a019a27bc 100644
@@ -106,6 +106,9 @@ struct SysemuCPUOps;
  * @has_work: Callback for checking if there is work to do.
  * @memory_rw_debug: Callback for GDB memory access.
  * @dump_state: Callback for dumping state.
+ * @query_cpu_fast:
+ *       Fill in target specific information for the "query-cpus-fast"
+ *       QAPI call.
  * @get_arch_id: Callback for getting architecture-dependent CPU ID.
  * @set_pc: Callback for setting the Program Counter register. This
  *       should have the semantics used by the target architecture when
@@ -151,6 +154,7 @@ struct CPUClass {
     int (*memory_rw_debug)(CPUState *cpu, vaddr addr,
                            uint8_t *buf, int len, bool is_write);
     void (*dump_state)(CPUState *cpu, FILE *, int flags);
+    void (*query_cpu_fast)(CPUState *cpu, CpuInfoFast *value);
     int64_t (*get_arch_id)(CPUState *cpu);
     void (*set_pc)(CPUState *cpu, vaddr value);
     vaddr (*get_pc)(CPUState *cpu);
include/qemu/typedefs.h +1 -0
index df4b55ac65..8e9ef252f5 100644
@@ -41,6 +41,7 @@ typedef struct CompatProperty CompatProperty;
 typedef struct ConfidentialGuestSupport ConfidentialGuestSupport;
 typedef struct CPUAddressSpace CPUAddressSpace;
 typedef struct CPUArchState CPUArchState;
+typedef struct CpuInfoFast CpuInfoFast;
 typedef struct CPUJumpCache CPUJumpCache;
 typedef struct CPUState CPUState;
 typedef struct CPUTLBEntryFull CPUTLBEntryFull;
hw/core/machine-qmp-cmds.c +2 -14
index b98ff15089..c158c02aa3 100644
@@ -28,18 +28,6 @@
 #include "sysemu/runstate.h"
 #include "sysemu/sysemu.h"
 
-static void cpustate_to_cpuinfo_s390(CpuInfoS390 *info, const CPUState *cpu)
-{
-#ifdef TARGET_S390X
-    S390CPU *s390_cpu = S390_CPU(cpu);
-    CPUS390XState *env = &s390_cpu->env;
-
-    info->cpu_state = env->cpu_state;
-#else
-    abort();
-#endif
-}
-
 /*
  * fast means: we NEVER interrupt vCPU threads to retrieve
  * information from KVM.
@@ -68,8 +56,8 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
         }
 
         value->target = target;
-        if (target == SYS_EMU_TARGET_S390X) {
-            cpustate_to_cpuinfo_s390(&value->u.s390x, cpu);
+        if (cpu->cc->query_cpu_fast) {
+            cpu->cc->query_cpu_fast(cpu, value);
         }
 
         QAPI_LIST_APPEND(tail, value);
target/s390x/cpu.c +8 -0
index 40fdeaa905..df167493c3 100644
@@ -140,6 +140,13 @@ static bool s390_cpu_has_work(CPUState *cs)
     return s390_cpu_has_int(cpu);
 }
 
+static void s390_query_cpu_fast(CPUState *cpu, CpuInfoFast *value)
+{
+    S390CPU *s390_cpu = S390_CPU(cpu);
+
+    value->u.s390x.cpu_state = s390_cpu->env.cpu_state;
+}
+
 /* S390CPUClass::reset() */
 static void s390_cpu_reset(CPUState *s, cpu_reset_type type)
 {
@@ -332,6 +339,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
     cc->class_by_name = s390_cpu_class_by_name,
     cc->has_work = s390_cpu_has_work;
     cc->dump_state = s390_cpu_dump_state;
+    cc->query_cpu_fast = s390_query_cpu_fast;
     cc->set_pc = s390_cpu_set_pc;
     cc->get_pc = s390_cpu_get_pc;
     cc->gdb_read_register = s390_cpu_gdb_read_register;
Signature · 1 line
2.31.1
#3 lore raw

PATCH 3/3 hw/core: Move machine-qmp-cmds.c into the target independent source set

In reply to Thomas Huth

The only target specific code that is left in here are two spots that use TARGET_NAME. Change them to use the new target_name() wrapper function instead, so we can move the file into the common softmmu_ss source set. That way we only have to compile this file once, and not for each target anymore.

---
 hw/core/machine-qmp-cmds.c | 4 ++--
 hw/core/meson.build        | 5 +----
 2 files changed, 3 insertions(+), 6 deletions(-)
hw/core/machine-qmp-cmds.c +2 -2
index c158c02aa3..3860a50c3b 100644
@@ -37,7 +37,7 @@ CpuInfoFastList *qmp_query_cpus_fast(Error **errp)
     MachineState *ms = MACHINE(qdev_get_machine());
     MachineClass *mc = MACHINE_GET_CLASS(ms);
     CpuInfoFastList *head = NULL, **tail = &head;
-    SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME,
+    SysEmuTarget target = qapi_enum_parse(&SysEmuTarget_lookup, target_name(),
                                           -1, &error_abort);
     CPUState *cpu;
 
@@ -117,7 +117,7 @@ TargetInfo *qmp_query_target(Error **errp)
 {
     TargetInfo *info = g_malloc0(sizeof(*info));
 
-    info->arch = qapi_enum_parse(&SysEmuTarget_lookup, TARGET_NAME, -1,
+    info->arch = qapi_enum_parse(&SysEmuTarget_lookup, target_name(), -1,
                                  &error_abort);
 
     return info;
hw/core/meson.build +1 -4
index ae977c9396..959bc924d4 100644
@@ -41,6 +41,7 @@ softmmu_ss.add(files(
   'gpio.c',
   'loader.c',
   'machine-hmp-cmds.c',
+  'machine-qmp-cmds.c',
   'machine.c',
   'nmi.c',
   'null-machine.c',
@@ -51,7 +52,3 @@ softmmu_ss.add(files(
   'vm-change-state-handler.c',
   'clock-vmstate.c',
 ))
-
-specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: files(
-  'machine-qmp-cmds.c',
-))
Signature · 1 line
2.31.1
#0 lore raw

cover hw/core: Make machine-qmp-cmds.c target independent

For being able to create a universal QEMU binary one day, core files like machine-qmp-cmds.c must not contain any target specifc macros. This series reworks the related spots in this file, so we can move it to the common softmmu_ss source set. This has also the advantage that we only have to compile this file once, and not multiple times (one time for each target) anymore.

Thomas Huth (3):
hw/core: Use a callback for target specific query-cpus-fast
information
cpu: Introduce a wrapper for being able to use TARGET_NAME in common
code
hw/core: Move machine-qmp-cmds.c into the target independent source
set

 include/hw/core/cpu.h      |  6 ++++++
 include/qemu/typedefs.h    |  1 +
 cpu.c                      |  5 +++++
 hw/core/machine-qmp-cmds.c | 20 ++++----------------
 target/s390x/cpu.c         |  8 ++++++++
 hw/core/meson.build        |  5 +----
 6 files changed, 25 insertions(+), 20 deletions(-)
Signature · 1 line
2.31.1
#2 lore raw

PATCH 2/3 cpu: Introduce a wrapper for being able to use TARGET_NAME in common code

In some spots, it would be helpful to be able to use TARGET_NAME in common (target independent) code, too. Thus introduce a wrapper that can be called from common code, too, just like we already have one for target_words_bigendian().

---
 include/hw/core/cpu.h | 2 ++
 cpu.c                 | 5 +++++
 2 files changed, 7 insertions(+)
include/hw/core/cpu.h +2 -0
index 5a019a27bc..39150cf8f8 100644
@@ -1013,6 +1013,8 @@ void cpu_exec_unrealizefn(CPUState *cpu);
  */
 bool target_words_bigendian(void);
 
+const char *target_name(void);
+
 void page_size_init(void);
 
 #ifdef NEED_CPU_H
cpu.c +5 -0
index 9105c85404..65ebaf8159 100644
@@ -427,6 +427,11 @@ bool target_words_bigendian(void)
 #endif
 }
 
+const char *target_name(void)
+{
+    return TARGET_NAME;
+}
+
 void page_size_init(void)
 {
     /* NOTE: we can always suppose that qemu_host_page_size >=
Signature · 1 line
2.31.1