Skip to contentSkip to conversation

cpus: Convert cpu_list definition to CPUClass:list_cpus callback

PATCH Superseded Philippe Mathieu-Daudé 3 participants 14 messages Mar 24, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover cpus: Convert cpu_list definition to CPUClass:list_cpus callback

Since v1 (Thomas review comments)
- Move s390_set_qemu_cpu_model/s390_cpu_list to "cpu_models.h"
- Correct 'target/s390x: Register CPUClass:list_cpus' subject

'cpu_list' might be defined per target, and force code to be built per-target. We can avoid that by introducing a CPUClass callback.

This series combined with another which converts CPU_RESOLVING_TYPE to a runtime helper, allows to move most of cpu-target to common.

Based-on: <[email protected]>

Philippe Mathieu-Daudé (7):
cpus: Introduce CPUClass::list_cpus() callback target/i386: Register CPUClass:list_cpus
target/ppc: Register CPUClass:list_cpus
target/sparc: Register CPUClass:list_cpus
target/s390x: Declare s390_set_qemu_cpu_model/cpu_list in cpu_models.h target/s390x: Register CPUClass:list_cpus
cpus: Remove #ifdef check on cpu_list definition

 include/hw/core/cpu.h      |  2 ++
 target/i386/cpu.h          |  3 ---
 target/ppc/cpu.h           |  4 ----
 target/s390x/cpu.h         |  4 ----
 target/s390x/cpu_models.h  |  3 +++
 target/sparc/cpu.h         |  3 ---
 cpu-target.c               | 25 ++++++++++++-------------
 hw/s390x/s390-virtio-ccw.c |  2 +-
 target/i386/cpu.c          |  3 ++-
 target/ppc/cpu_init.c      |  3 ++-
 target/s390x/cpu.c         |  1 +
 target/sparc/cpu.c         |  3 ++-
 12 files changed, 25 insertions(+), 31 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH v2 1/7 cpus: Introduce CPUClass::list_cpus() callback

Some targets define cpu_list to a method listing their CPUs on stdout. In order to make list_cpus() generic, introduce the CPUClass::list_cpus() callback. When no callback is registered, list_cpus() defaults to the cpu_list definition.

---
 include/hw/core/cpu.h | 2 ++
 cpu-target.c          | 8 +++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
include/hw/core/cpu.h +2 -0
index 54570d21aea..b633766ee8f 100644
@@ -102,6 +102,7 @@ struct SysemuCPUOps;
  * CPUClass:
  * @class_by_name: Callback to map -cpu command line model name to an
  *                 instantiatable CPU type.
+ * @list_cpus: list available CPU models and flags.
  * @parse_features: Callback to parse command line arguments.
  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  * @mmu_index: Callback for choosing softmmu mmu index;
@@ -150,6 +151,7 @@ struct CPUClass {
     /*< public >*/
 
     ObjectClass *(*class_by_name)(const char *cpu_model);
+    void (*list_cpus)(void);
     void (*parse_features)(const char *typename, char *str, Error **errp);
 
     int (*mmu_index)(CPUState *cpu, bool ifetch);
cpu-target.c +7 -1
index cae77374b38..5947ca31a0a 100644
@@ -98,7 +98,13 @@ static void cpu_list(void)
 
 void list_cpus(void)
 {
-    cpu_list();
+    CPUClass *cc = CPU_CLASS(object_class_by_name(CPU_RESOLVING_TYPE));
+
+    if (cc->list_cpus) {
+        cc->list_cpus();
+    } else {
+        cpu_list();
+    }
 }
 
 /* enable or disable single step mode. EXCP_DEBUG is returned by the
Signature · 1 line
2.47.1
#2 lore raw

PATCH v2 2/7 target/i386: Register CPUClass:list_cpus

In reply to Philippe Mathieu-Daudé

Register x86_cpu_list() as CPUClass:list_cpus callback. Reduce its scope and remove the cpu_list definition.

---
 target/i386/cpu.h | 3 ---
 target/i386/cpu.c | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)
target/i386/cpu.h +0 -3
index 76f24446a55..28011eff0a8 100644
@@ -2367,7 +2367,6 @@ int x86_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
 int x86_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
 void x86_cpu_gdb_init(CPUState *cs);
 
-void x86_cpu_list(void);
 int cpu_x86_support_mca_broadcast(CPUX86State *env);
 
 #ifndef CONFIG_USER_ONLY
@@ -2561,8 +2560,6 @@ uint64_t cpu_get_tsc(CPUX86State *env);
 #define TARGET_DEFAULT_CPU_TYPE X86_CPU_TYPE_NAME("qemu32")
 #endif
 
-#define cpu_list x86_cpu_list
-
 /* MMU modes definitions */
 #define MMU_KSMAP64_IDX    0
 #define MMU_KSMAP32_IDX    1
target/i386/cpu.c +2 -1
index 1b64ceaaba4..1f502587c96 100644
@@ -6305,7 +6305,7 @@ static void x86_cpu_list_entry(gpointer data, gpointer user_data)
 }
 
 /* list available CPU models and flags */
-void x86_cpu_list(void)
+static void x86_cpu_list(void)
 {
     int i, j;
     GSList *list;
@@ -8924,6 +8924,7 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
     cc->reset_dump_flags = CPU_DUMP_FPU | CPU_DUMP_CCOP;
 
     cc->class_by_name = x86_cpu_class_by_name;
+    cc->list_cpus = x86_cpu_list;
     cc->parse_features = x86_cpu_parse_featurestr;
     cc->mmu_index = x86_cpu_mmu_index;
     cc->dump_state = x86_cpu_dump_state;
Signature · 1 line
2.47.1
#3 lore raw

PATCH v2 3/7 target/ppc: Register CPUClass:list_cpus

In reply to Philippe Mathieu-Daudé

Register ppc_cpu_list() as CPUClass:list_cpus callback. Reduce its scope and remove the cpu_list definition.

---
 target/ppc/cpu.h      | 4 ----
 target/ppc/cpu_init.c | 3 ++-
 2 files changed, 2 insertions(+), 5 deletions(-)
target/ppc/cpu.h +0 -4
index efab54a0683..0062579ef3e 100644
@@ -1597,8 +1597,6 @@ void ppc_store_dawrx1(CPUPPCState *env, uint32_t value);
 #endif /* !defined(CONFIG_USER_ONLY) */
 void ppc_store_msr(CPUPPCState *env, target_ulong value);
 
-void ppc_cpu_list(void);
-
 /* Time-base and decrementer management */
 uint64_t cpu_ppc_load_tbl(CPUPPCState *env);
 uint32_t cpu_ppc_load_tbu(CPUPPCState *env);
@@ -1660,8 +1658,6 @@ static inline uint64_t ppc_dump_gpr(CPUPPCState *env, int gprn)
 int ppc_dcr_read(ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp);
 int ppc_dcr_write(ppc_dcr_t *dcr_env, int dcrn, uint32_t val);
 
-#define cpu_list ppc_cpu_list
-
 /* MMU modes definitions */
 #define MMU_USER_IDX 0
 static inline int ppc_env_mmu_index(CPUPPCState *env, bool ifetch)
target/ppc/cpu_init.c +2 -1
index ed79cc1a6b7..4d7c0619739 100644
@@ -7184,7 +7184,7 @@ static void ppc_cpu_list_entry(gpointer data, gpointer user_data)
     g_free(name);
 }
 
-void ppc_cpu_list(void)
+static void ppc_cpu_list(void)
 {
     GSList *list;
 
@@ -7528,6 +7528,7 @@ static void ppc_cpu_class_init(ObjectClass *oc, void *data)
                                        &pcc->parent_phases);
 
     cc->class_by_name = ppc_cpu_class_by_name;
+    cc->list_cpus = ppc_cpu_list;
     cc->mmu_index = ppc_cpu_mmu_index;
     cc->dump_state = ppc_cpu_dump_state;
     cc->set_pc = ppc_cpu_set_pc;
Signature · 1 line
2.47.1
#4 lore raw

PATCH v2 4/7 target/sparc: Register CPUClass:list_cpus

In reply to Philippe Mathieu-Daudé

Register sparc_cpu_list() as CPUClass:list_cpus callback. Reduce its scope and remove the cpu_list definition.

---
 target/sparc/cpu.h | 3 ---
 target/sparc/cpu.c | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)
target/sparc/cpu.h +0 -3
index 462bcb6c0e6..7c6296ae70e 100644
@@ -594,7 +594,6 @@ G_NORETURN void cpu_raise_exception_ra(CPUSPARCState *, int, uintptr_t);
 
 /* cpu_init.c */
 void cpu_sparc_set_id(CPUSPARCState *env, unsigned int cpu);
-void sparc_cpu_list(void);
 /* mmu_helper.c */
 bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                         MMUAccessType access_type, int mmu_idx,
@@ -665,8 +664,6 @@ hwaddr cpu_get_phys_page_nofault(CPUSPARCState *env, target_ulong addr,
 
 #define CPU_RESOLVING_TYPE TYPE_SPARC_CPU
 
-#define cpu_list sparc_cpu_list
-
 /* MMU modes definitions */
 #if defined (TARGET_SPARC64)
 #define MMU_USER_IDX   0
target/sparc/cpu.c +2 -1
index 2ae7173c0bc..6b2288c727d 100644
@@ -579,7 +579,7 @@ static void print_features(uint32_t features, const char *prefix)
     }
 }
 
-void sparc_cpu_list(void)
+static void sparc_cpu_list(void)
 {
     unsigned int i;
 
@@ -1055,6 +1055,7 @@ static void sparc_cpu_class_init(ObjectClass *oc, void *data)
                                        &scc->parent_phases);
 
     cc->class_by_name = sparc_cpu_class_by_name;
+    cc->list_cpus = sparc_cpu_list,
     cc->parse_features = sparc_cpu_parse_features;
     cc->mmu_index = sparc_cpu_mmu_index;
     cc->dump_state = sparc_cpu_dump_state;
Signature · 1 line
2.47.1
#5 lore raw

PATCH v2 5/7 target/s390x: Declare s390_set_qemu_cpu_model/cpu_list in cpu_models.h

In reply to Philippe Mathieu-Daudé

Both s390_cpu_list() and s390_set_qemu_cpu_model() are defined in cpu_models.c, move their declarations in the related "cpu_models.h" header. Use full path to header in s390-virtio-ccw.c file.

---
 target/s390x/cpu.h         | 4 ----
 target/s390x/cpu_models.h  | 3 +++
 hw/s390x/s390-virtio-ccw.c | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)
target/s390x/cpu.h +0 -4
index 5b7992deda6..8dd1936e3e2 100644
@@ -900,11 +900,7 @@ static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
 }
 
 
-/* cpu_models.c */
-void s390_cpu_list(void);
 #define cpu_list s390_cpu_list
-void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
-                             const S390FeatInit feat_init);
 
 
 /* helper.c */
target/s390x/cpu_models.h +3 -0
index 71d4bc2dd4a..f701bc0b532 100644
@@ -113,6 +113,9 @@ static inline uint64_t s390_cpuid_from_cpu_model(const S390CPUModel *model)
 }
 S390CPUDef const *s390_find_cpu_def(uint16_t type, uint8_t gen, uint8_t ec_ga,
                                     S390FeatBitmap features);
+void s390_set_qemu_cpu_model(uint16_t type, uint8_t gen, uint8_t ec_ga,
+                             const S390FeatInit feat_init);
+void s390_cpu_list(void);
 
 bool kvm_s390_cpu_models_supported(void);
 bool kvm_s390_get_host_cpu_model(S390CPUModel *model, Error **errp);
hw/s390x/s390-virtio-ccw.c +1 -1
index 75b32182eb0..4f11c75b625 100644
@@ -35,7 +35,7 @@
 #include "hw/s390x/css-bridge.h"
 #include "hw/s390x/ap-bridge.h"
 #include "migration/register.h"
-#include "cpu_models.h"
+#include "target/s390x/cpu_models.h"
 #include "hw/nmi.h"
 #include "hw/qdev-properties.h"
 #include "hw/s390x/tod.h"
Signature · 1 line
2.47.1
#6 lore raw

Re: [PATCH-for-10.1 v2 5/7] target/s390x: Declare s390_set_qemu_cpu_model/cpu_list in cpu_models.h

Quoted 18 lines · Philippe Mathieu-Daudé

Both s390_cpu_list() and s390_set_qemu_cpu_model() are defined in cpu_models.c, move their declarations in the related "cpu_models.h" header. Use full path to header in s390-virtio-ccw.c file.

---
  target/s390x/cpu.h         | 4 ----
  target/s390x/cpu_models.h  | 3 +++
  hw/s390x/s390-virtio-ccw.c | 2 +-
  3 files changed, 4 insertions(+), 5 deletions(-)
target/s390x/cpu.h +0 -0
index 5b7992deda6..8dd1936e3e2 100644

target/s390x/cpu.h static inline uint8_t s390_cpu_get_state(S390CPU *cpu) · line 904

Quoted 5 lines
target/s390x/cpu.h +0 -2
@@ -900,11 +900,7 @@ static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
  }
 
 
-/* cpu_models.c */
-void s390_cpu_list(void);

Are you really able to remove this here, without the next patch?

r~

#7 lore raw

PATCH v2 6/7 target/s390x: Register CPUClass:list_cpus

In reply to Philippe Mathieu-Daudé

Register s390_cpu_list() as CPUClass:list_cpus callback and remove the cpu_list definition.

---
 target/s390x/cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/s390x/cpu.c +1 -0
index 8449bfee5a9..2876f2c4eb3 100644
@@ -385,6 +385,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
                                        &scc->parent_phases);
 
     cc->class_by_name = s390_cpu_class_by_name;
+    cc->list_cpus = s390_cpu_list;
     cc->mmu_index = s390x_cpu_mmu_index;
     cc->dump_state = s390_cpu_dump_state;
     cc->query_cpu_fast = s390_query_cpu_fast;
Signature · 1 line
2.47.1
#8 lore raw

Re: [PATCH-for-10.1 v2 6/7] target/s390x: Register CPUClass:list_cpus

Quoted 13 lines · Philippe Mathieu-Daudé

Register s390_cpu_list() as CPUClass:list_cpus callback and remove the cpu_list definition.

---
  target/s390x/cpu.c | 1 +
  1 file changed, 1 insertion(+)
target/s390x/cpu.c +0 -0
index 8449bfee5a9..2876f2c4eb3 100644

target/s390x/cpu.c static void s390_cpu_class_init(ObjectClass *oc, void *data) · line 391

Quoted 7 lines
target/s390x/cpu.c +1 -0
@@ -385,6 +385,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
                                         &scc->parent_phases);
 
      cc->class_by_name = s390_cpu_class_by_name;
+    cc->list_cpus = s390_cpu_list;
      cc->mmu_index = s390x_cpu_mmu_index;
      cc->dump_state = s390_cpu_dump_state;
      cc->query_cpu_fast = s390_query_cpu_fast;

Grr, I forgot to commit this hunk:

-- >8 --

target/s390x/cpu.h +0 -3
@@ -902,5 +902,2 @@ static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
 
-#define cpu_list s390_cpu_list
-
-
 /* helper.c */

---

#9 lore raw

PATCH v2 7/7 cpus: Remove #ifdef check on cpu_list definition

In reply to Philippe Mathieu-Daudé

Since we removed all definitions of cpu_list, the #ifdef check is always true. Remove it, inlining cpu_list().

---
 cpu-target.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
cpu-target.c +6 -13
index 5947ca31a0a..30598619581 100644
@@ -71,7 +71,6 @@ const char *parse_cpu_option(const char *cpu_option)
     return cpu_type;
 }
 
-#ifndef cpu_list
 static void cpu_list_entry(gpointer data, gpointer user_data)
 {
     CPUClass *cc = CPU_CLASS(OBJECT_CLASS(data));
@@ -85,17 +84,6 @@ static void cpu_list_entry(gpointer data, gpointer user_data)
     }
 }
 
-static void cpu_list(void)
-{
-    GSList *list;
-
-    list = object_class_get_list_sorted(TYPE_CPU, false);
-    qemu_printf("Available CPUs:\n");
-    g_slist_foreach(list, cpu_list_entry, NULL);
-    g_slist_free(list);
-}
-#endif
-
 void list_cpus(void)
 {
     CPUClass *cc = CPU_CLASS(object_class_by_name(CPU_RESOLVING_TYPE));
@@ -103,7 +91,12 @@ void list_cpus(void)
     if (cc->list_cpus) {
         cc->list_cpus();
     } else {
-        cpu_list();
+        GSList *list;
+
+        list = object_class_get_list_sorted(TYPE_CPU, false);
+        qemu_printf("Available CPUs:\n");
+        g_slist_foreach(list, cpu_list_entry, NULL);
+        g_slist_free(list);
     }
 }
 
Signature · 1 line
2.47.1
#10 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 11 lines · Philippe Mathieu-Daudé

Since v1 (Thomas review comments)
- Move s390_set_qemu_cpu_model/s390_cpu_list to "cpu_models.h"
- Correct 'target/s390x: Register CPUClass:list_cpus' subject

'cpu_list' might be defined per target, and force code to be built per-target. We can avoid that by introducing a CPUClass callback.

This series combined with another which converts CPU_RESOLVING_TYPE to a runtime helper, allows to move most of cpu-target to common.

I think we should focus on a more general solution, usable with machines, cpus, and devices, like the interface based approached we talked about.

Having an optional callback, implemented on half target, looks like a half baked solution. And it would not be desirable to do the same thing for machines, and devices later.

In case CPU_RESOLVING_TYPE is ambiguous (because 64 bit type inherit from 32 bit type, like TYPE_AARCH64_CPU inherits from TYPE_ARM_CPU), it's better to declare another interface, non ambiguous, like:
- TYPE_MACHINE_TARGET_AARCH64
- TYPE_MACHINE_TARGET_ARM
- TYPE_CPU_TARGET_AARCH64
- TYPE_CPU_TARGET_ARM
- TYPE_DEVICE_TARGET_AARCH64
- TYPE_DEVICE_TARGET_ARM

I would be in favor to introduce this for all targets, so all of them are implemented in the exact same way, and the pattern is easy to follow.

By using this approach and tagging machines/cpus/devices accordingly, there is no need for any callback anywhere, and the listing code can be generic.

As a bonus, we can assert all machines/cpus are properly tagged, something impossible to do with the callbacks approach, which opens room for a possible error, if one of the callback is buggy.

#11 lore raw
Quoted 25 lines · Pierrick Bouvier
Quoted 11 lines · Philippe Mathieu-Daudé

Since v1 (Thomas review comments)
- Move s390_set_qemu_cpu_model/s390_cpu_list to "cpu_models.h"
- Correct 'target/s390x: Register CPUClass:list_cpus' subject

'cpu_list' might be defined per target, and force code to be built per-target. We can avoid that by introducing a CPUClass callback.

This series combined with another which converts CPU_RESOLVING_TYPE to a runtime helper, allows to move most of cpu-target to common.

I think we should focus on a more general solution, usable with machines, cpus, and devices, like the interface based approached we talked about.

Having an optional callback, implemented on half target, looks like a half baked solution. And it would not be desirable to do the same thing for machines, and devices later.

In case CPU_RESOLVING_TYPE is ambiguous (because 64 bit type inherit from 32 bit type, like TYPE_AARCH64_CPU inherits from TYPE_ARM_CPU), it's better to declare another interface, non ambiguous, like:
- TYPE_MACHINE_TARGET_AARCH64
- TYPE_MACHINE_TARGET_ARM
- TYPE_CPU_TARGET_AARCH64
- TYPE_CPU_TARGET_ARM
- TYPE_DEVICE_TARGET_AARCH64
- TYPE_DEVICE_TARGET_ARM

Quoted 5 lines

I would be in favor to introduce this for all targets, so all of them are implemented in the exact same way, and the pattern is easy to follow.

By using this approach and tagging machines/cpus/devices accordingly, there is no need for any callback anywhere, and the listing code can be generic.

As a bonus, we can assert all machines/cpus are properly tagged, something impossible to do with the callbacks approach, which opens room for a possible error, if one of the callback is buggy.

This is exactly the plan (and what I've in my local branch). But since we need to remove the list_cpus definition (otherwise clash) I went with this surgical cleanup first.

#12 lore raw
Quoted 39 lines · Philippe Mathieu-Daudé
Quoted 38 lines · Pierrick Bouvier
Quoted 11 lines · Philippe Mathieu-Daudé

Since v1 (Thomas review comments)
- Move s390_set_qemu_cpu_model/s390_cpu_list to "cpu_models.h"
- Correct 'target/s390x: Register CPUClass:list_cpus' subject

'cpu_list' might be defined per target, and force code to be built per-target. We can avoid that by introducing a CPUClass callback.

This series combined with another which converts CPU_RESOLVING_TYPE to a runtime helper, allows to move most of cpu-target to common.

I think we should focus on a more general solution, usable with machines, cpus, and devices, like the interface based approached we talked about.

Having an optional callback, implemented on half target, looks like a half baked solution. And it would not be desirable to do the same thing for machines, and devices later.

In case CPU_RESOLVING_TYPE is ambiguous (because 64 bit type inherit from 32 bit type, like TYPE_AARCH64_CPU inherits from TYPE_ARM_CPU), it's better to declare another interface, non ambiguous, like:
- TYPE_MACHINE_TARGET_AARCH64
- TYPE_MACHINE_TARGET_ARM
- TYPE_CPU_TARGET_AARCH64
- TYPE_CPU_TARGET_ARM
- TYPE_DEVICE_TARGET_AARCH64
- TYPE_DEVICE_TARGET_ARM

I would be in favor to introduce this for all targets, so all of them are implemented in the exact same way, and the pattern is easy to follow.

By using this approach and tagging machines/cpus/devices accordingly, there is no need for any callback anywhere, and the listing code can be generic.

Quoted 7 lines
Quoted 3 lines

As a bonus, we can assert all machines/cpus are properly tagged, something impossible to do with the callbacks approach, which opens room for a possible error, if one of the callback is buggy.

This is exactly the plan (and what I've in my local branch). But since we need to remove the list_cpus definition (otherwise clash) I went with this surgical cleanup first.

I don't see why it's absolutely needed as a first step.
Once we get interface approach implemented, all that is needed is to remove cpu_list code completely (in cpu-target) and replace with the simple new cpu_list() function, based on type tagged by the target.

The cpu_list macros and stuff can exist until there without any harm.

Reviewed-by on the v2 series
#13 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Philippe Mathieu-Daudé (7):
cpus: IntroduceCPUClass::list_cpus() callback
target/i386: RegisterCPUClass:list_cpus
target/ppc: RegisterCPUClass:list_cpus
target/sparc: RegisterCPUClass:list_cpus
target/s390x: Declare s390_set_qemu_cpu_model/cpu_list in cpu_models.h target/s390x: RegisterCPUClass:list_cpus
cpus: Remove #ifdef check on cpu_list definition

r~