Skip to contentSkip to conversation

cpus: Convert cpu_list definition to CPUClass:list_cpus callback

PATCH Superseded Philippe Mathieu-Daudé 3 participants 16 messages Mar 23, 2025 – Mar 24, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover cpus: Convert cpu_list definition to CPUClass:list_cpus callback

'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.

Philippe Mathieu-Daudé (6):
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/sparc: 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    |  1 -
 target/sparc/cpu.h    |  3 ---
 cpu-target.c          | 25 ++++++++++++-------------
 target/i386/cpu.c     |  3 ++-
 target/ppc/cpu_init.c |  3 ++-
 target/s390x/cpu.c    |  1 +
 target/sparc/cpu.c    |  3 ++-
 10 files changed, 21 insertions(+), 27 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/6 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 5d11d26556a..ccfcd3eb3a6 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
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH-for-10.1 1/6] cpus: Introduce CPUClass::list_cpus() callback

Quoted 11 lines · Philippe Mathieu-Daudé

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(-)
#3 lore raw

PATCH 2/6 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
Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH-for-10.1 2/6] target/i386: Register CPUClass:list_cpus

Quoted 8 lines · 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(-)

Nice clean-up!

#5 lore raw

PATCH 3/6 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 8b590e7f17c..0ccb9068c89 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;
 
@@ -7525,6 +7525,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
Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH-for-10.1 3/6] target/ppc: Register CPUClass:list_cpus

Quoted 8 lines · 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(-)
#7 lore raw

PATCH 4/6 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 57161201173..635d5d81143 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;
 
@@ -1031,6 +1031,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
Reviewed-by on v1 4/6
#8 lore raw

Re: [PATCH-for-10.1 4/6] target/sparc: Register CPUClass:list_cpus

Quoted 8 lines · 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(-)
#9 lore raw

PATCH 5/6 target/sparc: Register CPUClass:list_cpus

In reply to Philippe Mathieu-Daudé

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

---
 target/s390x/cpu.h | 1 -
 target/s390x/cpu.c | 1 +
 2 files changed, 1 insertion(+), 1 deletion(-)
target/s390x/cpu.h +0 -1
index 5b7992deda6..1aac967a0ce 100644
@@ -902,7 +902,6 @@ 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);
 
target/s390x/cpu.c +1 -0
index 1f75629ddc2..ac05e82f0ac 100644
@@ -378,6 +378,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
#10 lore raw

Re: [PATCH-for-10.1 5/6] target/sparc: Register CPUClass:list_cpus

Quoted 2 lines · Philippe Mathieu-Daudé

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

Copy-n-paste error in both, subject and patch description: This should be about s390x, not sparc.

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

Quoted 8 lines
target/s390x/cpu.h +0 -0
index 5b7992deda6..1aac967a0ce 100644
@@ -902,7 +902,6 @@ static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
 
  /* cpu_models.c */
  void s390_cpu_list(void);

Don't you want to remove the prototype here, too? (and make the function static in the .c file)

Thomas

Quoted 16 lines
target/s390x/cpu.h +0 -1
-#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);
 
target/s390x/cpu.c +1 -0
index 1f75629ddc2..ac05e82f0ac 100644
@@ -378,6 +378,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;
#11 lore raw

Re: [PATCH-for-10.1 5/6] target/sparc: Register CPUClass:list_cpus

Quoted 5 lines · Thomas Huth
Quoted 2 lines · Philippe Mathieu-Daudé

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

Copy-n-paste error in both, subject and patch description: This should be about s390x, not sparc.

Oh oops.

Quoted 10 lines

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

Quoted 8 lines
target/s390x/cpu.h +0 -0
index 5b7992deda6..1aac967a0ce 100644
@@ -902,7 +902,6 @@ static inline uint8_t s390_cpu_get_state(S390CPU 
 *cpu)
   /* cpu_models.c */
   void s390_cpu_list(void);

Don't you want to remove the prototype here, too? (and make the function static in the .c file)

s390_set_qemu_cpu_model is defined in cpu_models.c, while CPUClass in cpu.c.

Quoted 2 lines

 Thomas

#12 lore raw

Re: [PATCH-for-10.1 5/6] target/sparc: Register CPUClass:list_cpus

Quoted 19 lines · Philippe Mathieu-Daudé
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Philippe Mathieu-Daudé

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

Copy-n-paste error in both, subject and patch description: This should be about s390x, not sparc.

Oh oops.

Quoted 9 lines

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

Quoted 7 lines
target/s390x/cpu.h +0 -0
index 5b7992deda6..1aac967a0ce 100644
@@ -902,7 +902,6 @@ static inline uint8_t s390_cpu_get_state(S390CPU *cpu)
   /* cpu_models.c */
   void s390_cpu_list(void);

Don't you want to remove the prototype here, too? (and make the function static in the .c file)

Quoted 2 lines

s390_set_qemu_cpu_model is defined in cpu_models.c, while CPUClass in cpu.c.

Oh, right! So maybe you could move the prototype into cpu_models.h instead?

Thomas

#13 lore raw

PATCH 6/6 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
Reviewed-by on v1 6/6
#14 lore raw

Re: [PATCH-for-10.1 6/6] cpus: Remove #ifdef check on cpu_list definition

Quoted 7 lines · 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(-)
Reviewed-by on the v1 series
#15 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

'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.

Quoted 7 lines

Philippe Mathieu-Daudé (6):
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/sparc: Register CPUClass:list_cpus
cpus: Remove #ifdef check on cpu_list definition

Modulo the s390x niggles Thomas pointed out,

r~