Skip to contentSkip to conversation

Unified CPU type check

PATCH Superseded Gavin Shan 3 participants 59 messages Nov 2, 2023 – Nov 13, 2023

Subsystems (2)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

v4 cover Unified CPU type check

There are two places where the user specified CPU type is checked to see if it's supported or allowed by the board: machine_run_board_init() and mc->init(). We don't have to maintain two duplicate sets of logic. This series intends to move the check to machine_run_board_init() so that we have unified CPU type check.

Note that the term "the registered CPU type name" used in this series corresponds to the names of non-abstract CPU classes, regitered by individual target.

PATCH[01-03] Come from Philippe Mathieu-Daudé. cpu_class_by_name()
is consolidated to ensure that the returned object class isn't abstract and a child object class of CPU_RESOLVING_TYPE
PATCH[04] Adds a generic helper cpu_model_from_type() to extract
the CPU model name from the CPU type name
PATCH[05-20] Uses cpu_model_from_type() in the individual target PATCH[21-24] Implements cpu_list() for the missed targets PATCH[25-33] Validates the CPU type in machine_run_board_init() for
the individual board

v1: https://lists.nongnu.org/archive/html/qemu-arm/2023-07/msg00302.html
v2: https://lists.nongnu.org/archive/html/qemu-arm/2023-07/msg00528.html
v3: https://lists.nongnu.org/archive/html/qemu-arm/2023-09/msg00157.html

Testing
=======

With the following command lines, the output messages are varied before and after the series is applied.

  ./build/qemu-system-aarch64            \
  -accel tcg -machine virt,gic-version=3 \
  -cpu cortex-a8 -smp maxcpus=2,cpus=1

Before the series is applied:

qemu-system-aarch64: mach-virt: CPU type cortex-a8-arm-cpu not supported

After the series is applied:

qemu-system-aarch64: Invalid CPU type: cortex-a8-arm-cpu The valid models are: cortex-a7, cortex-a15, cortex-a35, cortex-a55,

                        cortex-a72, cortex-a76, a64fx, neoverse-n1,
                        neoverse-v1, cortex-a53, cortex-a57, max

Changelog
=========
v4:
* Rebase to latest v8.1.2 (Gavin)
* Integrate Philippe's patches where cpu_class_by_name()
is consolidated and my duplicate code is dropped (Philippe)
* Simplified changelog and improvements (Thomas)
* g_assert() on the return value from cpu_model_from_type()
in is_cpu_type_supported() (Philippe)
* Collected r-bs from Philippe Mathieu-Daudé, Leif Lindholm, Bastian Koppelmann, Daniel Henrique Barboza, Cédric Le Goater,
Gavin Shan (Gavin)
v3:

  * Generic helper cpu_model_from_type()                        (Igor)
  * Apply cpu_model_from_type() to the individual targets       (Igor)
  * Implement cpu_list() for the missed targets                 (Gavin)
  * Remove mc->valid_cpu_models                                 (Richard)
  * Separate patch to constify mc->validate_cpu_types           (Gavin)
v2:
  * Constify mc->valid_cpu_types                                (Richard)
  * Print the supported CPU models, instead of typenames        (Peter)
  * Misc improvements for the hleper to do the check            (Igor)
  * More patches to move the check                              (Marcin)

Gavin Shan (30):
cpu: Add helper cpu_model_from_type()
target/alpha: Use generic helper to show CPU model names target/arm: Use generic helper to show CPU model names target/avr: Use generic helper to show CPU model names target/cris: Use generic helper to show CPU model names target/hexagon: Use generic helper to show CPU model names target/i386: Use generic helper to show CPU model names target/loongarch: Use generic helper to show CPU model names target/m68k: Use generic helper to show CPU model names target/mips: Use generic helper to show CPU model names target/openrisc: Use generic helper to show CPU model names target/ppc: Use generic helper to show CPU model names target/riscv: Use generic helper to show CPU model names target/rx: Use generic helper to show CPU model names target/s390x: Use generic helper to show CPU model names target/sh4: Use generic helper to show CPU model names target/tricore: Use generic helper to show CPU model names target/hppa: Implement hppa_cpu_list() target/microblaze: Implement microblaze_cpu_list() target/nios2: Implement nios2_cpu_list()
cpu: Mark cpu_list() supported on all targets
machine: Constify MachineClass::valid_cpu_types[i]
machine: Use error handling when CPU type is checked
machine: Introduce helper is_cpu_type_supported()
machine: Print CPU model name instead of CPU type name hw/arm/virt: Check CPU type in machine_run_board_init() hw/arm/virt: Hide host CPU model for tcg hw/arm/sbsa-ref: Check CPU type in machine_run_board_init() hw/arm: Check CPU type in machine_run_board_init() hw/riscv/shakti_c: Check CPU type in machine_run_board_init()

Philippe Mathieu-Daudé (3):
target/alpha: Tidy up alpha_cpu_class_by_name()
hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()
cpu: Call object_class_dynamic_cast() once in cpu_class_by_name()

 bsd-user/main.c                       |  3 -
 cpu-target.c                          | 34 +++++++++-
 hw/arm/bananapi_m2u.c                 | 12 ++--
 hw/arm/cubieboard.c                   | 12 ++--
 hw/arm/mps2-tz.c                      | 20 ++++--
 hw/arm/mps2.c                         | 25 +++++--
 hw/arm/msf2-som.c                     | 12 ++--
 hw/arm/musca.c                        | 13 ++--
 hw/arm/npcm7xx_boards.c               | 13 ++--
 hw/arm/orangepi.c                     | 12 ++--
 hw/arm/sbsa-ref.c                     | 21 +-----
 hw/arm/virt.c                         | 23 ++-----
 hw/core/cpu-common.c                  |  8 ---
 hw/core/machine.c                     | 93 ++++++++++++++++-----------
 hw/m68k/q800.c                        |  2 +-
 hw/riscv/shakti_c.c                   | 11 ++--
 include/hw/boards.h                   |  2 +-
 include/hw/core/cpu.h                 | 19 +++++-
 target/alpha/cpu.c                    | 15 ++---
 target/arm/arm-qmp-cmds.c             |  6 +-
 target/arm/cpu.c                      |  5 +-
 target/arm/helper.c                   | 12 ++--
 target/avr/cpu.c                      | 17 +++--
 target/cris/cpu.c                     | 12 ++--
 target/hexagon/cpu.c                  | 17 ++---
 target/hppa/cpu.c                     | 19 ++++++
 target/hppa/cpu.h                     |  3 +
 target/i386/cpu.c                     |  7 +-
 target/loongarch/cpu.c                | 14 ++--
 target/loongarch/loongarch-qmp-cmds.c |  3 +-
 target/m68k/cpu.c                     |  5 +-
 target/m68k/helper.c                  | 14 ++--
 target/microblaze/cpu.c               | 20 ++++++
 target/microblaze/cpu.h               |  3 +
 target/mips/cpu-defs.c.inc            |  9 ---
 target/mips/cpu.c                     | 18 ++++++
 target/mips/sysemu/mips-qmp-cmds.c    |  3 +-
 target/nios2/cpu.c                    | 20 ++++++
 target/nios2/cpu.h                    |  3 +
 target/openrisc/cpu.c                 | 19 ++----
 target/ppc/cpu_init.c                 | 12 ++--
 target/riscv/cpu.c                    | 19 +++---
 target/riscv/riscv-qmp-cmds.c         |  3 +-
 target/rx/cpu.c                       | 11 ++--
 target/s390x/cpu_models.c             | 12 ++--
 target/s390x/cpu_models_sysemu.c      |  9 ++-
 target/sh4/cpu.c                      | 26 ++++----
 target/tricore/cpu.c                  |  5 +-
 target/tricore/helper.c               | 13 ++--
 target/xtensa/cpu.c                   |  5 +-
 50 files changed, 384 insertions(+), 310 deletions(-)
Signature · 1 line
2.41.0
author
#1 lore raw

PATCH v4 1/33 target/alpha: Tidy up alpha_cpu_class_by_name()

From: Philippe Mathieu-Daudé <[email protected]>

For target/alpha, the default CPU model name is "ev67". The default CPU model is used when no matching CPU model is found. The conditions to fall back to the default CPU model can be combined so that the code looks a bit simplified.

---
 target/alpha/cpu.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
target/alpha/cpu.c +2 -5
index 51b7d8d1bf..c7ae4d6a41 100644
@@ -142,13 +142,10 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
     /* TODO: remove match everything nonsense */
-    /* Default to ev67; no reason not to emulate insns by default. */
-    if (!oc) {
+    if (!oc || object_class_is_abstract(oc)) {
+        /* Default to ev67, no reason not to emulate insns by default */
         oc = object_class_by_name(ALPHA_CPU_TYPE_NAME("ev67"));
     }
 
Signature · 1 line
2.41.0
Acked-by on v4 1/33
#2 lore raw

Re: [PATCH v4 01/33] target/alpha: Tidy up alpha_cpu_class_by_name()

Quoted 23 lines · Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

For target/alpha, the default CPU model name is "ev67". The default CPU model is used when no matching CPU model is found. The conditions to fall back to the default CPU model can be combined so that the code looks a bit simplified.

---
  target/alpha/cpu.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
target/alpha/cpu.c +0 -2
index 51b7d8d1bf..c7ae4d6a41 100644
@@ -142,13 +142,10 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 151

Quoted 10 lines
target/alpha/cpu.c +2 -3
-    }
 
      /* TODO: remove match everything nonsense */
-    /* Default to ev67; no reason not to emulate insns by default. */
-    if (!oc) {
+    if (!oc || object_class_is_abstract(oc)) {
+        /* Default to ev67, no reason not to emulate insns by default */
          oc = object_class_by_name(ALPHA_CPU_TYPE_NAME("ev67"));
      }
 

I assume this TODO gets cleaned up later?

r~

author
#3 lore raw

Re: [PATCH v4 01/33] target/alpha: Tidy up alpha_cpu_class_by_name()

Quoted 35 lines · Richard Henderson
Quoted 21 lines · Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

For target/alpha, the default CPU model name is "ev67". The default CPU model is used when no matching CPU model is found. The conditions to fall back to the default CPU model can be combined so that the code looks a bit simplified.

---
  target/alpha/cpu.c | 7 ++-----

  1 file changed, 2 insertions(+), 5 deletions(-)
target/alpha/cpu.c +0 -0
index 51b7d8d1bf..c7ae4d6a41 100644
@@ -142,13 +142,10 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
       typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
       oc = object_class_by_name(typename);
       g_free(typename);

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 149

Quoted 10 lines
target/alpha/cpu.c +2 -5
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
       /* TODO: remove match everything nonsense */
-    /* Default to ev67; no reason not to emulate insns by default. */
-    if (!oc) {
+    if (!oc || object_class_is_abstract(oc)) {
+        /* Default to ev67, no reason not to emulate insns by default */
           oc = object_class_by_name(ALPHA_CPU_TYPE_NAME("ev67"));
       }
Quoted 2 lines

I assume this TODO gets cleaned up later?

Let me drop this TODO in next respin.

Thanks,
Gavin

#4 lore raw

Re: [PATCH v4 01/33] target/alpha: Tidy up alpha_cpu_class_by_name()

In reply to Gavin Shan
Quoted 6 lines · On Thu, 2 Nov 2023 10:24:28 +1000 Gavin Shan <[email protected]> wrote:

From: Philippe Mathieu-Daudé <[email protected]>

For target/alpha, the default CPU model name is "ev67". The default CPU model is used when no matching CPU model is found. The conditions to fall back to the default CPU model can be combined so that the code looks a bit simplified.

default cpu should be specified by board not by target internals.

Quoted 11 lines
---
 target/alpha/cpu.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
target/alpha/cpu.c +0 -0
index 51b7d8d1bf..c7ae4d6a41 100644

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 146

Quoted 8 lines
target/alpha/cpu.c +0 -3
@@ -142,13 +142,10 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
     /* TODO: remove match everything nonsense */

Let's do ^^^^^ instead of just shifting code around. It will break users that specify junk as input, but it's clear users error so garbage in => error out.

Quoted 7 lines
target/alpha/cpu.c +2 -2
-    /* Default to ev67; no reason not to emulate insns by default. */
-    if (!oc) {
+    if (!oc || object_class_is_abstract(oc)) {
+        /* Default to ev67, no reason not to emulate insns by default */
         oc = object_class_by_name(ALPHA_CPU_TYPE_NAME("ev67"));
     }
 
author
#5 lore raw

Re: [PATCH v4 01/33] target/alpha: Tidy up alpha_cpu_class_by_name()

Quoted 12 lines · Igor Mammedov
Quoted 6 lines · On Thu, 2 Nov 2023 10:24:28 +1000 Gavin Shan <[email protected]> wrote:

From: Philippe Mathieu-Daudé <[email protected]>

For target/alpha, the default CPU model name is "ev67". The default CPU model is used when no matching CPU model is found. The conditions to fall back to the default CPU model can be combined so that the code looks a bit simplified.

default cpu should be specified by board not by target internals.

Yes, MachineClass::default_cpu_type used to specify the default CPU type. I will improve the changelog in next revision to avoid the confusion.

Quoted 18 lines
Quoted 18 lines
---
  target/alpha/cpu.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
target/alpha/cpu.c +0 -3
index 51b7d8d1bf..c7ae4d6a41 100644
@@ -142,13 +142,10 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(ALPHA_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
Quoted 6 lines

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 146

Quoted 1 line
target/alpha/cpu.c +0 -0
      /* TODO: remove match everything nonsense */

Let's do ^^^^^ instead of just shifting code around. It will break users that specify junk as input, but it's clear users error so garbage in => error out.

Ok. The whole chunk of code to fall back to 'ev67' will be dropped in next revision.

Quoted 8 lines

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 151

Quoted 8 lines
target/alpha/cpu.c +2 -2
-    /* Default to ev67; no reason not to emulate insns by default. */
-    if (!oc) {
+    if (!oc || object_class_is_abstract(oc)) {
+        /* Default to ev67, no reason not to emulate insns by default */
          oc = object_class_by_name(ALPHA_CPU_TYPE_NAME("ev67"));
      }
 

Thanks,
Gavin

author
#6 lore raw

PATCH v4 2/33 hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

In reply to Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

Let CPUClass::class_by_name() handlers to return abstract classes, and filter them once in the public cpu_class_by_name() method.

---
 hw/core/cpu-common.c   | 8 +++++++-
 include/hw/core/cpu.h  | 7 ++++---
 target/alpha/cpu.c     | 2 +-
 target/arm/cpu.c       | 3 +--
 target/avr/cpu.c       | 3 +--
 target/cris/cpu.c      | 3 +--
 target/hexagon/cpu.c   | 3 +--
 target/loongarch/cpu.c | 3 +--
 target/m68k/cpu.c      | 3 +--
 target/openrisc/cpu.c  | 3 +--
 target/riscv/cpu.c     | 3 +--
 target/rx/cpu.c        | 5 +----
 target/sh4/cpu.c       | 3 ---
 target/tricore/cpu.c   | 3 +--
 target/xtensa/cpu.c    | 3 +--
 15 files changed, 23 insertions(+), 32 deletions(-)
hw/core/cpu-common.c +7 -1
index bab8942c30..bca0323e9f 100644
@@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
 {
     CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
 
     assert(cpu_model && cc->class_by_name);
-    return cc->class_by_name(cpu_model);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    return NULL;
 }
 
 static void cpu_common_parse_features(const char *typename, char *features,
include/hw/core/cpu.h +4 -3
index 18593db5b2..ee85aafdf5 100644
@@ -102,7 +102,7 @@ struct SysemuCPUOps;
 /**
  * CPUClass:
  * @class_by_name: Callback to map -cpu command line model name to an
- * instantiatable CPU type.
+ *                 instantiatable CPU type.
  * @parse_features: Callback to parse command line arguments.
  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  * @has_work: Callback for checking if there is work to do.
@@ -772,9 +772,10 @@ void cpu_reset(CPUState *cpu);
  * @typename: The CPU base type.
  * @cpu_model: The model string without any parameters.
  *
- * Looks up a CPU #ObjectClass matching name @cpu_model.
+ * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
  *
- * Returns: A #CPUClass or %NULL if not matching class is found.
+ * Returns: A concrete #CPUClass or %NULL if no matching class is found
+ *          or if the matching class is abstract.
  */
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 
target/alpha/cpu.c +1 -1
index c7ae4d6a41..9436859c7b 100644
@@ -126,7 +126,7 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
     int i;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
         !object_class_is_abstract(oc)) {
         return oc;
     }
target/arm/cpu.c +1 -2
index 954328d72a..8c622d6b59 100644
@@ -2399,8 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
         return NULL;
     }
     return oc;
target/avr/cpu.c +1 -2
index 14d8b9d1f0..113d522f75 100644
@@ -157,8 +157,7 @@ static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
     ObjectClass *oc;
 
     oc = object_class_by_name(cpu_model);
-    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
         oc = NULL;
     }
     return oc;
target/cris/cpu.c +1 -2
index be4a44c218..1cb431cd46 100644
@@ -95,8 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
         oc = NULL;
     }
     return oc;
target/hexagon/cpu.c +1 -2
index 1adc11b713..bd5adb7acd 100644
@@ -63,8 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
         return NULL;
     }
     return oc;
target/loongarch/cpu.c +1 -2
index ef1bf89dac..06d1b9bb95 100644
@@ -648,8 +648,7 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
         }
     }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
-        && !object_class_is_abstract(oc)) {
+    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
         return oc;
     }
     return NULL;
target/m68k/cpu.c +1 -2
index 538d9473c2..fe381cc5d3 100644
@@ -111,8 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
         return NULL;
     }
     return oc;
target/openrisc/cpu.c +1 -2
index f5a3d5273b..cc94f37e77 100644
@@ -164,8 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
         return NULL;
     }
     return oc;
target/riscv/cpu.c +1 -2
index ac4a6c7eec..018bad6f82 100644
@@ -636,8 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
         return NULL;
     }
     return oc;
target/rx/cpu.c +1 -4
index 4d0d3a0c8c..0063837e93 100644
@@ -111,16 +111,13 @@ static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
     char *typename;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
         !object_class_is_abstract(oc)) {
         return oc;
     }
     typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
     return oc;
 }
target/sh4/cpu.c +0 -3
index 788e41fea6..a8ec98b134 100644
@@ -152,9 +152,6 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 
     typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
     oc = object_class_by_name(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
 out:
     g_free(s);
target/tricore/cpu.c +1 -2
index 5ca666ee12..47e1c272cf 100644
@@ -132,8 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
         return NULL;
     }
     return oc;
target/xtensa/cpu.c +1 -2
index ea1dae7390..5d1c090467 100644
@@ -141,8 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
         return NULL;
     }
     return oc;
Signature · 1 line
2.41.0
Reviewed-by on v4 2/33
#7 lore raw

Re: [PATCH v4 02/33] hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

Quoted 15 lines · Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

Let CPUClass::class_by_name() handlers to return abstract classes, and filter them once in the public cpu_class_by_name() method.

---

hw/core/cpu-common.c +1 -0
@@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
  ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  {
      CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;

hw/core/cpu-common.c static bool cpu_common_has_work(CPUState *cs) · line 162

Quoted 10 lines
hw/core/cpu-common.c +6 -1
 
      assert(cpu_model && cc->class_by_name);
-    return cc->class_by_name(cpu_model);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    return NULL;
  }

I wonder if cpu_model_by_name wouldn't be a better name at this point? Anyway,

r~

#8 lore raw

Re: [PATCH v4 02/33] hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

In reply to Gavin Shan
Quoted 77 lines · On Thu, 2 Nov 2023 10:24:29 +1000 Gavin Shan <[email protected]> wrote:

From: Philippe Mathieu-Daudé <[email protected]>

Let CPUClass::class_by_name() handlers to return abstract classes, and filter them once in the public cpu_class_by_name() method.

---
 hw/core/cpu-common.c   | 8 +++++++-
 include/hw/core/cpu.h  | 7 ++++---
 target/alpha/cpu.c     | 2 +-
 target/arm/cpu.c       | 3 +--
 target/avr/cpu.c       | 3 +--
 target/cris/cpu.c      | 3 +--
 target/hexagon/cpu.c   | 3 +--
 target/loongarch/cpu.c | 3 +--
 target/m68k/cpu.c      | 3 +--
 target/openrisc/cpu.c  | 3 +--
 target/riscv/cpu.c     | 3 +--
 target/rx/cpu.c        | 5 +----
 target/sh4/cpu.c       | 3 ---
 target/tricore/cpu.c   | 3 +--
 target/xtensa/cpu.c    | 3 +--
 15 files changed, 23 insertions(+), 32 deletions(-)
hw/core/cpu-common.c +7 -1
index bab8942c30..bca0323e9f 100644
@@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
 {
     CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
 
     assert(cpu_model && cc->class_by_name);
-    return cc->class_by_name(cpu_model);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    return NULL;
 }
 
 static void cpu_common_parse_features(const char *typename, char *features,
include/hw/core/cpu.h +4 -3
index 18593db5b2..ee85aafdf5 100644
@@ -102,7 +102,7 @@ struct SysemuCPUOps;
 /**
  * CPUClass:
  * @class_by_name: Callback to map -cpu command line model name to an
- * instantiatable CPU type.
+ *                 instantiatable CPU type.
  * @parse_features: Callback to parse command line arguments.
  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  * @has_work: Callback for checking if there is work to do.
@@ -772,9 +772,10 @@ void cpu_reset(CPUState *cpu);
  * @typename: The CPU base type.
  * @cpu_model: The model string without any parameters.
  *
- * Looks up a CPU #ObjectClass matching name @cpu_model.
+ * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
  *
- * Returns: A #CPUClass or %NULL if not matching class is found.
+ * Returns: A concrete #CPUClass or %NULL if no matching class is found
+ *          or if the matching class is abstract.
  */
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 
target/alpha/cpu.c +0 -0
index c7ae4d6a41..9436859c7b 100644

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 129

Quoted 4 lines
target/alpha/cpu.c +0 -1
@@ -126,7 +126,7 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
     int i;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&

I'd split 'oc != NULL &&' into a separate patch

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 130

Quoted 2 lines
target/alpha/cpu.c +1 -0
+    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
         !object_class_is_abstract(oc)) {

stray abstract check leftover??

Quoted 119 lines
target/alpha/cpu.c +0 -0
         return oc;
     }
target/arm/cpu.c +1 -2
index 954328d72a..8c622d6b59 100644
@@ -2399,8 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
         return NULL;
     }
     return oc;
target/avr/cpu.c +1 -2
index 14d8b9d1f0..113d522f75 100644
@@ -157,8 +157,7 @@ static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
     ObjectClass *oc;
 
     oc = object_class_by_name(cpu_model);
-    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
         oc = NULL;
     }
     return oc;
target/cris/cpu.c +1 -2
index be4a44c218..1cb431cd46 100644
@@ -95,8 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
         oc = NULL;
     }
     return oc;
target/hexagon/cpu.c +1 -2
index 1adc11b713..bd5adb7acd 100644
@@ -63,8 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
         return NULL;
     }
     return oc;
target/loongarch/cpu.c +1 -2
index ef1bf89dac..06d1b9bb95 100644
@@ -648,8 +648,7 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
         }
     }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
-        && !object_class_is_abstract(oc)) {
+    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
         return oc;
     }
     return NULL;
target/m68k/cpu.c +1 -2
index 538d9473c2..fe381cc5d3 100644
@@ -111,8 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
         return NULL;
     }
     return oc;
target/openrisc/cpu.c +1 -2
index f5a3d5273b..cc94f37e77 100644
@@ -164,8 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
         return NULL;
     }
     return oc;
target/riscv/cpu.c +1 -2
index ac4a6c7eec..018bad6f82 100644
@@ -636,8 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
         return NULL;
     }
     return oc;
target/rx/cpu.c +0 -0
index 4d0d3a0c8c..0063837e93 100644

target/rx/cpu.c static ObjectClass *rx_cpu_class_by_name(const char *cpu_model) · line 115

Quoted 6 lines
target/rx/cpu.c +1 -1
@@ -111,16 +111,13 @@ static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
     char *typename;
 
     oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
         !object_class_is_abstract(oc)) {

ditto wrt strays

Quoted 16 lines
target/rx/cpu.c +0 -3
         return oc;
     }
     typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
     return oc;
 }
target/sh4/cpu.c +0 -0
index 788e41fea6..a8ec98b134 100644

target/sh4/cpu.c static ObjectClass *superh_cpu_class_by_name(const char *cpu_model) · line 157

Quoted 6 lines
target/sh4/cpu.c +0 -3
@@ -152,9 +152,6 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 
     typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
     oc = object_class_by_name(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }

Why they do not do
object_class_dynamic_cast(oc, TYPE_ABSTRACT_FOO_CPU)
here, do we really need this dynamic cast elsewhere at all?

Quoted 30 lines
target/sh4/cpu.c +0 -0
 out:
     g_free(s);
target/tricore/cpu.c +1 -2
index 5ca666ee12..47e1c272cf 100644
@@ -132,8 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
         return NULL;
     }
     return oc;
target/xtensa/cpu.c +1 -2
index ea1dae7390..5d1c090467 100644
@@ -141,8 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
         return NULL;
     }
     return oc;
author
#9 lore raw

Re: [PATCH v4 02/33] hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

Quoted 82 lines · Igor Mammedov
Quoted 79 lines · On Thu, 2 Nov 2023 10:24:29 +1000 Gavin Shan <[email protected]> wrote:

From: Philippe Mathieu-Daudé <[email protected]>

Let CPUClass::class_by_name() handlers to return abstract classes, and filter them once in the public cpu_class_by_name() method.

---
  hw/core/cpu-common.c   | 8 +++++++-
  include/hw/core/cpu.h  | 7 ++++---
  target/alpha/cpu.c     | 2 +-
  target/arm/cpu.c       | 3 +--
  target/avr/cpu.c       | 3 +--
  target/cris/cpu.c      | 3 +--
  target/hexagon/cpu.c   | 3 +--
  target/loongarch/cpu.c | 3 +--
  target/m68k/cpu.c      | 3 +--
  target/openrisc/cpu.c  | 3 +--
  target/riscv/cpu.c     | 3 +--
  target/rx/cpu.c        | 5 +----
  target/sh4/cpu.c       | 3 ---
  target/tricore/cpu.c   | 3 +--
  target/xtensa/cpu.c    | 3 +--
  15 files changed, 23 insertions(+), 32 deletions(-)
hw/core/cpu-common.c +7 -1
index bab8942c30..bca0323e9f 100644
@@ -150,9 +150,15 @@ static bool cpu_common_has_work(CPUState *cs)
  ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
  {
      CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
 
      assert(cpu_model && cc->class_by_name);
-    return cc->class_by_name(cpu_model);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    return NULL;
  }
 
  static void cpu_common_parse_features(const char *typename, char *features,
include/hw/core/cpu.h +4 -3
index 18593db5b2..ee85aafdf5 100644
@@ -102,7 +102,7 @@ struct SysemuCPUOps;
  /**
   * CPUClass:
   * @class_by_name: Callback to map -cpu command line model name to an
- * instantiatable CPU type.
+ *                 instantiatable CPU type.
   * @parse_features: Callback to parse command line arguments.
   * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
   * @has_work: Callback for checking if there is work to do.
@@ -772,9 +772,10 @@ void cpu_reset(CPUState *cpu);
   * @typename: The CPU base type.
   * @cpu_model: The model string without any parameters.
   *
- * Looks up a CPU #ObjectClass matching name @cpu_model.
+ * Looks up a concrete CPU #ObjectClass matching name @cpu_model.
   *
- * Returns: A #CPUClass or %NULL if not matching class is found.
+ * Returns: A concrete #CPUClass or %NULL if no matching class is found
+ *          or if the matching class is abstract.
   */
  ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 
target/alpha/cpu.c +0 -0
index c7ae4d6a41..9436859c7b 100644
@@ -126,7 +126,7 @@ static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model)
      int i;
 
Quoted 4 lines

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 129

Quoted 2 lines
target/alpha/cpu.c +0 -1
      oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) != NULL &&

I'd split 'oc != NULL &&' into a separate patch

Agree. It's a good idea, but this patch has been merged as:

3a9d0d7b64 hw/cpu: Call object_class_is_abstract() once in cpu_class_by_name()

Quoted 5 lines

target/alpha/cpu.c static ObjectClass *alpha_cpu_class_by_name(const char *cpu_model) · line 130

Quoted 2 lines
target/alpha/cpu.c +1 -0
+    if (object_class_dynamic_cast(oc, TYPE_ALPHA_CPU) &&
          !object_class_is_abstract(oc)) {

stray abstract check leftover??

Nope, It's intentional. We will fall back to @alpha_cpu_aliases if the CPU class corresponding to @cpu_model is abstract.

Quoted 121 lines
Quoted 121 lines
target/alpha/cpu.c +0 -0
          return oc;
      }
target/arm/cpu.c +1 -2
index 954328d72a..8c622d6b59 100644
@@ -2399,8 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
      oc = object_class_by_name(typename);
      g_strfreev(cpuname);
      g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_ARM_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
          return NULL;
      }
      return oc;
target/avr/cpu.c +1 -2
index 14d8b9d1f0..113d522f75 100644
@@ -157,8 +157,7 @@ static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
      ObjectClass *oc;
 
      oc = object_class_by_name(cpu_model);
-    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) == NULL ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
          oc = NULL;
      }
      return oc;
target/cris/cpu.c +1 -2
index be4a44c218..1cb431cd46 100644
@@ -95,8 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
          oc = NULL;
      }
      return oc;
target/hexagon/cpu.c +1 -2
index 1adc11b713..bd5adb7acd 100644
@@ -63,8 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
      oc = object_class_by_name(typename);
      g_strfreev(cpuname);
      g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
          return NULL;
      }
      return oc;
target/loongarch/cpu.c +1 -2
index ef1bf89dac..06d1b9bb95 100644
@@ -648,8 +648,7 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
          }
      }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)
-        && !object_class_is_abstract(oc)) {
+    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
          return oc;
      }
      return NULL;
target/m68k/cpu.c +1 -2
index 538d9473c2..fe381cc5d3 100644
@@ -111,8 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && (object_class_dynamic_cast(oc, TYPE_M68K_CPU) == NULL ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
          return NULL;
      }
      return oc;
target/openrisc/cpu.c +1 -2
index f5a3d5273b..cc94f37e77 100644
@@ -164,8 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU) ||
-                       object_class_is_abstract(oc))) {
+    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
          return NULL;
      }
      return oc;
target/riscv/cpu.c +1 -2
index ac4a6c7eec..018bad6f82 100644
@@ -636,8 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
      oc = object_class_by_name(typename);
      g_strfreev(cpuname);
      g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_RISCV_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
          return NULL;
      }
      return oc;
target/rx/cpu.c +0 -0
index 4d0d3a0c8c..0063837e93 100644
@@ -111,16 +111,13 @@ static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
      char *typename;
 
Quoted 7 lines

target/rx/cpu.c static ObjectClass *rx_cpu_class_by_name(const char *cpu_model) · line 115

Quoted 4 lines
target/rx/cpu.c +1 -1
      oc = object_class_by_name(cpu_model);
-    if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL &&
+    if (object_class_dynamic_cast(oc, TYPE_RX_CPU) &&
          !object_class_is_abstract(oc)) {

ditto wrt strays

Same as above. It's intentional. We will fall back to @cpu_model-rx-cpu if the CPU class corresponding to @cpu_model is abstract. The point is we don't want rx_cpu_class_by_name() returns an abstract CPU class in the middle. The last returned CPU class will be validated by cpu_class_by_name() like below.

ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)

{
    :
    if (oc == NULL || object_class_is_abstract(oc)) {
        return NULL;
    }
    return oc;
}
Quoted 17 lines
Quoted 17 lines
target/rx/cpu.c +0 -3
          return oc;
      }
      typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }
 
      return oc;
  }
target/sh4/cpu.c +0 -0
index 788e41fea6..a8ec98b134 100644
@@ -152,9 +152,6 @@ static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 
Quoted 10 lines

target/sh4/cpu.c static ObjectClass *superh_cpu_class_by_name(const char *cpu_model) · line 157

Quoted 5 lines
target/sh4/cpu.c +0 -3
      typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
      oc = object_class_by_name(typename);
-    if (oc != NULL && object_class_is_abstract(oc)) {
-        oc = NULL;
-    }

Why they do not do
object_class_dynamic_cast(oc, TYPE_ABSTRACT_FOO_CPU)
here, do we really need this dynamic cast elsewhere at all?

I think it was missed at the beginning. We need to ensure the class is a CPU class here. It's not harmful at least.

Quoted 22 lines
Quoted 22 lines
target/sh4/cpu.c +0 -0
  out:
      g_free(s);
target/tricore/cpu.c +1 -2
index 5ca666ee12..47e1c272cf 100644
@@ -132,8 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (!oc || !object_class_dynamic_cast(oc, TYPE_TRICORE_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
          return NULL;
      }
      return oc;
target/xtensa/cpu.c +0 -0
index ea1dae7390..5d1c090467 100644
@@ -141,8 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
      typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
Quoted 9 lines

target/xtensa/cpu.c static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model) · line 147

Quoted 8 lines
target/xtensa/cpu.c +1 -2
      oc = object_class_by_name(typename);
      g_free(typename);
-    if (oc == NULL || !object_class_dynamic_cast(oc, TYPE_XTENSA_CPU) ||
-        object_class_is_abstract(oc)) {
+    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
          return NULL;
      }
      return oc;

Thanks,
Gavin

author
#10 lore raw

PATCH v4 3/33 cpu: Call object_class_dynamic_cast() once in cpu_class_by_name()

In reply to Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

For all targets, the CPU class returned from CPUClass::class_by_name() and object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE) need to be compatible. Lets apply the check in cpu_class_by_name() for once, instead of having the check in CPUClass::class_by_name() for individual target. In order to make CPU_RESOLVING_TYPE visible to cpu_class_by_name(), the helper has to be moved to cpu-target.c

---
 cpu-target.c           | 15 +++++++++++++++
 hw/core/cpu-common.c   | 14 --------------
 target/arm/cpu.c       |  4 +---
 target/avr/cpu.c       |  8 +-------
 target/cris/cpu.c      |  4 +---
 target/hexagon/cpu.c   |  4 +---
 target/loongarch/cpu.c |  8 +-------
 target/m68k/cpu.c      |  4 +---
 target/openrisc/cpu.c  |  4 +---
 target/riscv/cpu.c     |  4 +---
 target/tricore/cpu.c   |  4 +---
 target/xtensa/cpu.c    |  4 +---
 12 files changed, 25 insertions(+), 52 deletions(-)
cpu-target.c +15 -0
index 79363ae370..876b498233 100644
@@ -250,6 +250,21 @@ void cpu_exec_initfn(CPUState *cpu)
 #endif
 }
 
+ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
+{
+    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
+
+    assert(cpu_model && cc->class_by_name);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc) &&
+        object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE)) {
+        return oc;
+    }
+
+    return NULL;
+}
+
 const char *parse_cpu_option(const char *cpu_option)
 {
     ObjectClass *oc;
hw/core/cpu-common.c +0 -14
index bca0323e9f..1024de53bb 100644
@@ -147,20 +147,6 @@ static bool cpu_common_has_work(CPUState *cs)
     return false;
 }
 
-ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
-{
-    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
-    ObjectClass *oc;
-
-    assert(cpu_model && cc->class_by_name);
-    oc = cc->class_by_name(cpu_model);
-    if (oc && !object_class_is_abstract(oc)) {
-        return oc;
-    }
-
-    return NULL;
-}
-
 static void cpu_common_parse_features(const char *typename, char *features,
                                       Error **errp)
 {
target/arm/cpu.c +1 -3
index 8c622d6b59..4942239b34 100644
@@ -2399,9 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/avr/cpu.c +1 -7
index 113d522f75..a36cc48aae 100644
@@ -154,13 +154,7 @@ static void avr_cpu_initfn(Object *obj)
 
 static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
 {
-    ObjectClass *oc;
-
-    oc = object_class_by_name(cpu_model);
-    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
-        oc = NULL;
-    }
-    return oc;
+    return object_class_by_name(cpu_model);
 }
 
 static void avr_cpu_dump_state(CPUState *cs, FILE *f, int flags)
target/cris/cpu.c +1 -3
index 1cb431cd46..a5083a0077 100644
@@ -95,9 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
-        oc = NULL;
-    }
+
     return oc;
 }
 
target/hexagon/cpu.c +1 -3
index bd5adb7acd..aa48f5fe89 100644
@@ -63,9 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/loongarch/cpu.c +1 -7
index 06d1b9bb95..c6712e13f9 100644
@@ -643,15 +643,9 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
         g_autofree char *typename
             = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
         oc = object_class_by_name(typename);
-        if (!oc) {
-            return NULL;
-        }
     }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
-        return oc;
-    }
-    return NULL;
+    return oc;
 }
 
 void loongarch_cpu_dump_state(CPUState *cs, FILE *f, int flags)
target/m68k/cpu.c +1 -3
index fe381cc5d3..6cd5b56d6f 100644
@@ -111,9 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/openrisc/cpu.c +1 -3
index cc94f37e77..f7d53c592a 100644
@@ -164,9 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/riscv/cpu.c +1 -3
index 018bad6f82..8b4024338c 100644
@@ -636,9 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/tricore/cpu.c +1 -3
index 47e1c272cf..8acacdf0c0 100644
@@ -132,9 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/xtensa/cpu.c +1 -3
index 5d1c090467..93e782a6e0 100644
@@ -141,9 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
Signature · 1 line
2.41.0
#11 lore raw

Re: [PATCH v4 03/33] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name()

Quoted 63 lines · Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

For all targets, the CPU class returned from CPUClass::class_by_name() and object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE) need to be compatible. Lets apply the check in cpu_class_by_name() for once, instead of having the check in CPUClass::class_by_name() for individual target. In order to make CPU_RESOLVING_TYPE visible to cpu_class_by_name(), the helper has to be moved to cpu-target.c

---
  cpu-target.c           | 15 +++++++++++++++
  hw/core/cpu-common.c   | 14 --------------
  target/arm/cpu.c       |  4 +---
  target/avr/cpu.c       |  8 +-------
  target/cris/cpu.c      |  4 +---
  target/hexagon/cpu.c   |  4 +---
  target/loongarch/cpu.c |  8 +-------
  target/m68k/cpu.c      |  4 +---
  target/openrisc/cpu.c  |  4 +---
  target/riscv/cpu.c     |  4 +---
  target/tricore/cpu.c   |  4 +---
  target/xtensa/cpu.c    |  4 +---
  12 files changed, 25 insertions(+), 52 deletions(-)
cpu-target.c +15 -0
index 79363ae370..876b498233 100644
@@ -250,6 +250,21 @@ void cpu_exec_initfn(CPUState *cpu)
  #endif
  }
 
+ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
+{
+    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
+
+    assert(cpu_model && cc->class_by_name);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc) &&
+        object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE)) {
+        return oc;
+    }
+
+    return NULL;
+}
+
  const char *parse_cpu_option(const char *cpu_option)
  {
      ObjectClass *oc;
hw/core/cpu-common.c +0 -3
index bca0323e9f..1024de53bb 100644
@@ -147,20 +147,6 @@ static bool cpu_common_has_work(CPUState *cs)
      return false;
  }
 
-ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
-{
-    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));

hw/core/cpu-common.c static bool cpu_common_has_work(CPUState *cs) · line 162

Quoted 10 lines
hw/core/cpu-common.c +0 -10
-    ObjectClass *oc;
-
-    assert(cpu_model && cc->class_by_name);
-    oc = cc->class_by_name(cpu_model);
-    if (oc && !object_class_is_abstract(oc)) {
-        return oc;
-    }
-
-    return NULL;
-}

This move is unnecessary: typename == CPU_RESOLVING_TYPE.

Indeed, since you've grabbed class_by_name from CPUClass, it is completely logical that that's the base class against which we should verify.

r~

author
#12 lore raw

Re: [PATCH v4 03/33] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name()

Quoted 75 lines · Richard Henderson
Quoted 61 lines · Gavin Shan

From: Philippe Mathieu-Daudé <[email protected]>

For all targets, the CPU class returned from CPUClass::class_by_name() and object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE) need to be compatible. Lets apply the check in cpu_class_by_name() for once, instead of having the check in CPUClass::class_by_name() for individual target. In order to make CPU_RESOLVING_TYPE visible to cpu_class_by_name(), the helper has to be moved to cpu-target.c

---
  cpu-target.c           | 15 +++++++++++++++
  hw/core/cpu-common.c   | 14 --------------
  target/arm/cpu.c       |  4 +---
  target/avr/cpu.c       |  8 +-------
  target/cris/cpu.c      |  4 +---
  target/hexagon/cpu.c   |  4 +---
  target/loongarch/cpu.c |  8 +-------
  target/m68k/cpu.c      |  4 +---
  target/openrisc/cpu.c  |  4 +---
  target/riscv/cpu.c     |  4 +---
  target/tricore/cpu.c   |  4 +---
  target/xtensa/cpu.c    |  4 +---

  12 files changed, 25 insertions(+), 52 deletions(-)
cpu-target.c +15 -0
index 79363ae370..876b498233 100644
@@ -250,6 +250,21 @@ void cpu_exec_initfn(CPUState *cpu)
   #endif
   }
+ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
+{
+    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
+
+    assert(cpu_model && cc->class_by_name);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc) &&
+        object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE)) {
+        return oc;
+    }
+
+    return NULL;
+}
+
   const char *parse_cpu_option(const char *cpu_option)
   {
       ObjectClass *oc;
hw/core/cpu-common.c +0 -3
index bca0323e9f..1024de53bb 100644
@@ -147,20 +147,6 @@ static bool cpu_common_has_work(CPUState *cs)
       return false;
   }
-ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
-{
-    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));

hw/core/cpu-common.c static bool cpu_common_has_work(CPUState *cs) · line 161

Quoted 10 lines
hw/core/cpu-common.c +0 -10
-    ObjectClass *oc;
-
-    assert(cpu_model && cc->class_by_name);
-    oc = cc->class_by_name(cpu_model);
-    if (oc && !object_class_is_abstract(oc)) {
-        return oc;
-    }
-
-    return NULL;
-}

This move is unnecessary: typename == CPU_RESOLVING_TYPE.

Quoted 2 lines

Indeed, since you've grabbed class_by_name from CPUClass, it is completely logical that that's the base class against which we should verify.

Good point. I just realized typename == CPU_RESOLVING_TYPE, so the movement isn't needed at all.

[gshan@gshan q]$ git grep \ cpu_class_by_name\(

cpu-target.c:    oc = cpu_class_by_name(CPU_RESOLVING_TYPE, model_pieces[0]);
target/arm/arm-qmp-cmds.c:    oc = cpu_class_by_name(TYPE_ARM_CPU, model->name);
target/s390x/cpu_models_sysemu.c:    oc = cpu_class_by_name(TYPE_S390_CPU, info->name);

[gshan@gshan q]$ git grep 'define CPU_RESOLVING_TYPE' target/arm/* target/arm/cpu.h:#define CPU_RESOLVING_TYPE TYPE_ARM_CPU [gshan@gshan q]$ git grep 'define CPU_RESOLVING_TYPE' target/s390x/* target/s390x/cpu.h:#define CPU_RESOLVING_TYPE TYPE_S390_CPU

Thanks,
Gavin

Reviewed-by on v4 3/33
#13 lore raw

Re: [PATCH v4 03/33] cpu: Call object_class_dynamic_cast() once in cpu_class_by_name()

In reply to Gavin Shan
Quoted 11 lines · On Thu, 2 Nov 2023 10:24:30 +1000 Gavin Shan <[email protected]> wrote:

From: Philippe Mathieu-Daudé <[email protected]>

For all targets, the CPU class returned from CPUClass::class_by_name() and object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE) need to be compatible. Lets apply the check in cpu_class_by_name() for once, instead of having the check in CPUClass::class_by_name() for individual target. In order to make CPU_RESOLVING_TYPE visible to cpu_class_by_name(), the helper has to be moved to cpu-target.c

Quoted 226 lines
---
 cpu-target.c           | 15 +++++++++++++++
 hw/core/cpu-common.c   | 14 --------------
 target/arm/cpu.c       |  4 +---
 target/avr/cpu.c       |  8 +-------
 target/cris/cpu.c      |  4 +---
 target/hexagon/cpu.c   |  4 +---
 target/loongarch/cpu.c |  8 +-------
 target/m68k/cpu.c      |  4 +---
 target/openrisc/cpu.c  |  4 +---
 target/riscv/cpu.c     |  4 +---
 target/tricore/cpu.c   |  4 +---
 target/xtensa/cpu.c    |  4 +---
 12 files changed, 25 insertions(+), 52 deletions(-)
cpu-target.c +15 -0
index 79363ae370..876b498233 100644
@@ -250,6 +250,21 @@ void cpu_exec_initfn(CPUState *cpu)
 #endif
 }
 
+ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
+{
+    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
+    ObjectClass *oc;
+
+    assert(cpu_model && cc->class_by_name);
+    oc = cc->class_by_name(cpu_model);
+    if (oc && !object_class_is_abstract(oc) &&
+        object_class_dynamic_cast(oc, CPU_RESOLVING_TYPE)) {
+        return oc;
+    }
+
+    return NULL;
+}
+
 const char *parse_cpu_option(const char *cpu_option)
 {
     ObjectClass *oc;
hw/core/cpu-common.c +0 -14
index bca0323e9f..1024de53bb 100644
@@ -147,20 +147,6 @@ static bool cpu_common_has_work(CPUState *cs)
     return false;
 }
 
-ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
-{
-    CPUClass *cc = CPU_CLASS(object_class_by_name(typename));
-    ObjectClass *oc;
-
-    assert(cpu_model && cc->class_by_name);
-    oc = cc->class_by_name(cpu_model);
-    if (oc && !object_class_is_abstract(oc)) {
-        return oc;
-    }
-
-    return NULL;
-}
-
 static void cpu_common_parse_features(const char *typename, char *features,
                                       Error **errp)
 {
target/arm/cpu.c +1 -3
index 8c622d6b59..4942239b34 100644
@@ -2399,9 +2399,7 @@ static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_ARM_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/avr/cpu.c +1 -7
index 113d522f75..a36cc48aae 100644
@@ -154,13 +154,7 @@ static void avr_cpu_initfn(Object *obj)
 
 static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
 {
-    ObjectClass *oc;
-
-    oc = object_class_by_name(cpu_model);
-    if (!object_class_dynamic_cast(oc, TYPE_AVR_CPU)) {
-        oc = NULL;
-    }
-    return oc;
+    return object_class_by_name(cpu_model);
 }
 
 static void avr_cpu_dump_state(CPUState *cs, FILE *f, int flags)
target/cris/cpu.c +1 -3
index 1cb431cd46..a5083a0077 100644
@@ -95,9 +95,7 @@ static ObjectClass *cris_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(CRIS_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_CRIS_CPU)) {
-        oc = NULL;
-    }
+
     return oc;
 }
 
target/hexagon/cpu.c +1 -3
index bd5adb7acd..aa48f5fe89 100644
@@ -63,9 +63,7 @@ static ObjectClass *hexagon_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_HEXAGON_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/loongarch/cpu.c +1 -7
index 06d1b9bb95..c6712e13f9 100644
@@ -643,15 +643,9 @@ static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
         g_autofree char *typename
             = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
         oc = object_class_by_name(typename);
-        if (!oc) {
-            return NULL;
-        }
     }
 
-    if (object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU)) {
-        return oc;
-    }
-    return NULL;
+    return oc;
 }
 
 void loongarch_cpu_dump_state(CPUState *cs, FILE *f, int flags)
target/m68k/cpu.c +1 -3
index fe381cc5d3..6cd5b56d6f 100644
@@ -111,9 +111,7 @@ static ObjectClass *m68k_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(M68K_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_M68K_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/openrisc/cpu.c +1 -3
index cc94f37e77..f7d53c592a 100644
@@ -164,9 +164,7 @@ static ObjectClass *openrisc_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(OPENRISC_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_OPENRISC_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/riscv/cpu.c +1 -3
index 018bad6f82..8b4024338c 100644
@@ -636,9 +636,7 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
     oc = object_class_by_name(typename);
     g_strfreev(cpuname);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_RISCV_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/tricore/cpu.c +1 -3
index 47e1c272cf..8acacdf0c0 100644
@@ -132,9 +132,7 @@ static ObjectClass *tricore_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(TRICORE_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_TRICORE_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
target/xtensa/cpu.c +1 -3
index 5d1c090467..93e782a6e0 100644
@@ -141,9 +141,7 @@ static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
     oc = object_class_by_name(typename);
     g_free(typename);
-    if (!object_class_dynamic_cast(oc, TYPE_XTENSA_CPU)) {
-        return NULL;
-    }
+
     return oc;
 }
 
author
#14 lore raw

PATCH v4 4/33 cpu: Add helper cpu_model_from_type()

In reply to Gavin Shan

Add helper cpu_model_from_type() to extract the CPU model name from the CPU type name in two circumstances: (1) The CPU type name is the combination of the CPU model name and suffix. (2) The CPU type name is same to the CPU model name.

The helper will be used in the subsequent commits to conver the CPU type name to the CPU model name.

---
 cpu-target.c          | 16 ++++++++++++++++
 include/hw/core/cpu.h | 12 ++++++++++++
 2 files changed, 28 insertions(+)
cpu-target.c +16 -0
index 876b498233..344bad5736 100644
@@ -265,6 +265,22 @@ ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model)
     return NULL;
 }
 
+char *cpu_model_from_type(const char *typename)
+{
+    const char *suffix = "-" CPU_RESOLVING_TYPE;
+
+    if (!object_class_by_name(typename)) {
+        return NULL;
+    }
+
+    if (strlen(typename) > strlen(suffix) &&
+        !strcmp(typename + strlen(typename) - strlen(suffix), suffix)) {
+        return g_strndup(typename, strlen(typename) - strlen(suffix));
+    }
+
+    return g_strdup(typename);
+}
+
 const char *parse_cpu_option(const char *cpu_option)
 {
     ObjectClass *oc;
include/hw/core/cpu.h +12 -0
index ee85aafdf5..8179c55759 100644
@@ -779,6 +779,18 @@ void cpu_reset(CPUState *cpu);
  */
 ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model);
 
+/**
+ * cpu_model_from_type:
+ * @typename: The CPU type name
+ *
+ * Extract the CPU model name from the CPU type name. The
+ * CPU type name is either the combination of the CPU model
+ * name and suffix, or same to the CPU model name.
+ *
+ * Returns: CPU model name or NULL if the CPU class doesn't exist
+ */
+char *cpu_model_from_type(const char *typename);
+
 /**
  * cpu_create:
  * @typename: The CPU type.
Signature · 1 line
2.41.0
#15 lore raw

Re: [PATCH v4 04/33] cpu: Add helper cpu_model_from_type()

cpu-target.c ObjectClass *cpu_class_by_name(const char *typename, const char *cpu_model) · line 277

Quoted 10 lines · Gavin Shan
cpu-target.c +10 -0
+char *cpu_model_from_type(const char *typename)
+{
+    const char *suffix = "-" CPU_RESOLVING_TYPE;
+
+    if (!object_class_by_name(typename)) {
+        return NULL;
+    }
+
+    if (strlen(typename) > strlen(suffix) &&
+        !strcmp(typename + strlen(typename) - strlen(suffix), suffix)) {

g_str_has_suffix

r~

author
#16 lore raw

PATCH v4 5/33 target/alpha: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/alpha, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names instead of the CPU type names.

---
 target/alpha/cpu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/alpha/cpu.c +4 -2
index 9436859c7b..d4b35434cd 100644
@@ -89,9 +89,11 @@ static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
 
 static void alpha_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *oc = data;
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("  %s\n", object_class_get_name(oc));
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void alpha_cpu_list(void)
Signature · 1 line
2.41.0
#17 lore raw

Re: [PATCH v4 05/33] target/alpha: Use generic helper to show CPU model names

Quoted 14 lines · Gavin Shan

For target/alpha, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names instead of the CPU type names.

---
  target/alpha/cpu.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
target/alpha/cpu.c +0 -0
index 9436859c7b..d4b35434cd 100644

target/alpha/cpu.c static void alpha_cpu_realizefn(DeviceState *dev, Error **errp) · line 93

Quoted 6 lines
target/alpha/cpu.c +2 -1
@@ -89,9 +89,11 @@ static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
 
  static void alpha_cpu_list_entry(gpointer data, gpointer user_data)
  {
-    ObjectClass *oc = data;
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);

g_autofree

r~

Quoted 7 lines
target/alpha/cpu.c +2 -1
-    qemu_printf("  %s\n", object_class_get_name(oc));
+    qemu_printf("  %s\n", model);
+    g_free(model);
  }
 
  void alpha_cpu_list(void)
author
#18 lore raw

PATCH v4 6/33 target/arm: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/arm, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names. In arm_cpu_list_entry(), @name is renamed to @model since it points to CPU model name instead of CPU type name.

---
 target/arm/arm-qmp-cmds.c |  6 ++----
 target/arm/helper.c       | 12 +++++-------
 2 files changed, 7 insertions(+), 11 deletions(-)
target/arm/arm-qmp-cmds.c +2 -4
index b53d5efe13..c8c712a2c6 100644
@@ -233,12 +233,10 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
     ObjectClass *oc = data;
     CpuDefinitionInfoList **cpu_list = user_data;
     CpuDefinitionInfo *info;
-    const char *typename;
+    const char *typename = object_class_get_name(oc);
 
-    typename = object_class_get_name(oc);
     info = g_malloc0(sizeof(*info));
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
+    info->name = cpu_model_from_type(typename);
     info->q_typename = g_strdup(typename);
 
     QAPI_LIST_PREPEND(*cpu_list, info);
target/arm/helper.c +5 -7
index 5dc0d20a84..baf4b104ef 100644
@@ -9436,17 +9436,15 @@ static void arm_cpu_list_entry(gpointer data, gpointer user_data)
 {
     ObjectClass *oc = data;
     CPUClass *cc = CPU_CLASS(oc);
-    const char *typename;
-    char *name;
+    const char *typename = object_class_get_name(oc);
+    char *model = cpu_model_from_type(typename);
 
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
     if (cc->deprecation_note) {
-        qemu_printf("  %s (deprecated)\n", name);
+        qemu_printf("  %s (deprecated)\n", model);
     } else {
-        qemu_printf("  %s\n", name);
+        qemu_printf("  %s\n", model);
     }
-    g_free(name);
+    g_free(model);
 }
 
 void arm_cpu_list(void)
Signature · 1 line
2.41.0
#19 lore raw

Re: [PATCH v4 06/33] target/arm: Use generic helper to show CPU model names

target/arm/helper.c static void arm_cpu_list_entry(gpointer data, gpointer user_data) · line 9440

Quoted 12 lines · Gavin Shan
target/arm/helper.c +2 -2
index 5dc0d20a84..baf4b104ef 100644
@@ -9436,17 +9436,15 @@ static void arm_cpu_list_entry(gpointer data, gpointer user_data)
  {
      ObjectClass *oc = data;
      CPUClass *cc = CPU_CLASS(oc);
-    const char *typename;
-    char *name;
+    const char *typename = object_class_get_name(oc);
+    char *model = cpu_model_from_type(typename);

g_autofree.

r~

#20 lore raw

Re: [PATCH v4 06/33] target/arm: Use generic helper to show CPU model names

In reply to Gavin Shan
Quoted 45 lines · Gavin Shan

For target/arm, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names. In arm_cpu_list_entry(), @name is renamed to @model since it points to CPU model name instead of CPU type name.

---
  target/arm/arm-qmp-cmds.c |  6 ++----
  target/arm/helper.c       | 12 +++++-------
  2 files changed, 7 insertions(+), 11 deletions(-)
target/arm/arm-qmp-cmds.c +2 -4
index b53d5efe13..c8c712a2c6 100644
@@ -233,12 +233,10 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
      ObjectClass *oc = data;
      CpuDefinitionInfoList **cpu_list = user_data;
      CpuDefinitionInfo *info;
-    const char *typename;
+    const char *typename = object_class_get_name(oc);
 
-    typename = object_class_get_name(oc);
      info = g_malloc0(sizeof(*info));
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
+    info->name = cpu_model_from_type(typename);
      info->q_typename = g_strdup(typename);
 
      QAPI_LIST_PREPEND(*cpu_list, info);
target/arm/helper.c +2 -4
index 5dc0d20a84..baf4b104ef 100644
@@ -9436,17 +9436,15 @@ static void arm_cpu_list_entry(gpointer data, gpointer user_data)
  {
      ObjectClass *oc = data;
      CPUClass *cc = CPU_CLASS(oc);
-    const char *typename;
-    char *name;
+    const char *typename = object_class_get_name(oc);
+    char *model = cpu_model_from_type(typename);
 
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));

target/arm/helper.c static void arm_cpu_list_entry(gpointer data, gpointer user_data) · line 9448

Quoted 10 lines
target/arm/helper.c +3 -3
      if (cc->deprecation_note) {
-        qemu_printf("  %s (deprecated)\n", name);
+        qemu_printf("  %s (deprecated)\n", model);
      } else {
-        qemu_printf("  %s\n", name);
+        qemu_printf("  %s\n", model);
      }
-    g_free(name);
+    g_free(model);
  }

Rather than adjust this target by target, I think you can create one single cpu_list function out to cpu-common.c using TYPE_CPU, not even CPU_RESOLVING_TYPE. All of the target-specific code could go away.

r~

Quoted 2 lines
target/arm/helper.c +0 -0
  void arm_cpu_list(void)
author
#21 lore raw

Re: [PATCH v4 06/33] target/arm: Use generic helper to show CPU model names

Quoted 47 lines · Richard Henderson
Quoted 46 lines · Gavin Shan

For target/arm, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names. In arm_cpu_list_entry(), @name is renamed to @model since it points to CPU model name instead of CPU type name.

---
  target/arm/arm-qmp-cmds.c |  6 ++----
  target/arm/helper.c       | 12 +++++-------

  2 files changed, 7 insertions(+), 11 deletions(-)
target/arm/arm-qmp-cmds.c +2 -4
index b53d5efe13..c8c712a2c6 100644
@@ -233,12 +233,10 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
       ObjectClass *oc = data;
       CpuDefinitionInfoList **cpu_list = user_data;
       CpuDefinitionInfo *info;
-    const char *typename;
+    const char *typename = object_class_get_name(oc);
-    typename = object_class_get_name(oc);
       info = g_malloc0(sizeof(*info));
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
+    info->name = cpu_model_from_type(typename);
       info->q_typename = g_strdup(typename);
       QAPI_LIST_PREPEND(*cpu_list, info);
target/arm/helper.c +3 -5
index 5dc0d20a84..baf4b104ef 100644
@@ -9436,17 +9436,15 @@ static void arm_cpu_list_entry(gpointer data, gpointer user_data)
   {
       ObjectClass *oc = data;
       CPUClass *cc = CPU_CLASS(oc);
-    const char *typename;
-    char *name;
+    const char *typename = object_class_get_name(oc);
+    char *model = cpu_model_from_type(typename);
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
       if (cc->deprecation_note) {
-        qemu_printf("  %s (deprecated)\n", name);
+        qemu_printf("  %s (deprecated)\n", model);
       } else {
Quoted 9 lines

target/arm/helper.c static void arm_cpu_list_entry(gpointer data, gpointer user_data) · line 9448

Quoted 6 lines
target/arm/helper.c +2 -2
-        qemu_printf("  %s\n", name);
+        qemu_printf("  %s\n", model);
       }
-    g_free(name);
+    g_free(model);
   }

Rather than adjust this target by target, I think you can create one single cpu_list function out to cpu-common.c using TYPE_CPU, not even CPU_RESOLVING_TYPE.  All of the target-specific code could go away.

Actually I thought of this scheme before. Some targets like x86/ppc/s390 don't follow the unified pattern. However, I think it's reasonable to have a common helper for other targets who follow the fixed pattern. The point is to avoid duplicate code. I will have something like below in v5.

--- cpu-target.c ---

#ifndef cpu_list
static void cpu_list_entry(gpointer data, gpointer user_data)

{
    const char *typename = object_class_get_name(OBJECT_CLASS(data));
    g_autofree char *model = cpu_model_from_type(typename);

    if (cc->deprecation_note) {
        qemu_printf("  %s (deprecated)\n", model);
    } else {
        qemu_printf("  %s\n", model);
    }
}

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)

{
    cpu_list();
}

One question is how list_cpus() can be called from bsd-user/main.c where cpu_list() is still used.

Thanks,
Gavin

author
#22 lore raw

Re: [PATCH v4 06/33] target/arm: Use generic helper to show CPU model names

Quoted 89 lines · Gavin Shan
Quoted 47 lines · Richard Henderson
Quoted 46 lines · Gavin Shan

For target/arm, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names. In arm_cpu_list_entry(), @name is renamed to @model since it points to CPU model name instead of CPU type name.

---
  target/arm/arm-qmp-cmds.c |  6 ++----
  target/arm/helper.c       | 12 +++++-------

  2 files changed, 7 insertions(+), 11 deletions(-)
target/arm/arm-qmp-cmds.c +2 -4
index b53d5efe13..c8c712a2c6 100644
@@ -233,12 +233,10 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data)
       ObjectClass *oc = data;
       CpuDefinitionInfoList **cpu_list = user_data;
       CpuDefinitionInfo *info;
-    const char *typename;
+    const char *typename = object_class_get_name(oc);
-    typename = object_class_get_name(oc);
       info = g_malloc0(sizeof(*info));
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_ARM_CPU));
+    info->name = cpu_model_from_type(typename);
       info->q_typename = g_strdup(typename);
       QAPI_LIST_PREPEND(*cpu_list, info);
target/arm/helper.c +3 -5
index 5dc0d20a84..baf4b104ef 100644
@@ -9436,17 +9436,15 @@ static void arm_cpu_list_entry(gpointer data, gpointer user_data)
   {
       ObjectClass *oc = data;
       CPUClass *cc = CPU_CLASS(oc);
-    const char *typename;
-    char *name;
+    const char *typename = object_class_get_name(oc);
+    char *model = cpu_model_from_type(typename);
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
       if (cc->deprecation_note) {
-        qemu_printf("  %s (deprecated)\n", name);
+        qemu_printf("  %s (deprecated)\n", model);
       } else {
Quoted 9 lines

target/arm/helper.c static void arm_cpu_list_entry(gpointer data, gpointer user_data) · line 9448

Quoted 6 lines
target/arm/helper.c +2 -2
-        qemu_printf("  %s\n", name);
+        qemu_printf("  %s\n", model);
       }
-    g_free(name);
+    g_free(model);
   }

Rather than adjust this target by target, I think you can create one single cpu_list function out to cpu-common.c using TYPE_CPU, not even CPU_RESOLVING_TYPE.  All of the target-specific code could go away.

Actually I thought of this scheme before. Some targets like x86/ppc/s390 don't follow the unified pattern. However, I think it's reasonable to have a common helper for other targets who follow the fixed pattern. The point is to avoid duplicate code. I will have something like below in v5.

--- cpu-target.c ---

#ifndef cpu_list
static void cpu_list_entry(gpointer data, gpointer user_data) {
    const char *typename = object_class_get_name(OBJECT_CLASS(data)); g_autofree char *model = cpu_model_from_type(typename);

    if (cc->deprecation_note) { qemu_printf("  %s (deprecated)\n", model); } else {
        qemu_printf("  %s\n", model);
    }
}

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

Quoted 8 lines

void list_cpus(void)
{
    cpu_list();
}

One question is how list_cpus() can be called from bsd-user/main.c where cpu_list() is still used.

I think all functions in cpu-target.c are visible to bsd-user/main.c because parse_cpu_option() is also called from bsd-user/main.c. Hopefully I get correct in this regard.

Thanks,
Gavin

author
#23 lore raw

PATCH v4 7/33 target/avr: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/avr, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names. Besides, the conversion from CPU model name to CPU type name needs to be supported in avr_cpu_class_by_name().

---
 target/avr/cpu.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
target/avr/cpu.c +18 -2
index a36cc48aae..4203ea1368 100644
@@ -154,7 +154,20 @@ static void avr_cpu_initfn(Object *obj)
 
 static ObjectClass *avr_cpu_class_by_name(const char *cpu_model)
 {
-    return object_class_by_name(cpu_model);
+    ObjectClass *oc;
+    char *typename;
+
+    oc = object_class_by_name(cpu_model);
+    if (object_class_dynamic_cast(oc, TYPE_AVR_CPU) &&
+        !object_class_is_abstract(oc)) {
+        return oc;
+    }
+
+    typename = g_strdup_printf(AVR_CPU_TYPE_NAME("%s"), cpu_model);
+    oc = object_class_by_name(typename);
+    g_free(typename);
+
+    return oc;
 }
 
 static void avr_cpu_dump_state(CPUState *cs, FILE *f, int flags)
@@ -357,14 +370,17 @@ typedef struct AVRCPUInfo {
 static void avr_cpu_list_entry(gpointer data, gpointer user_data)
 {
     const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("%s\n", typename);
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void avr_cpu_list(void)
 {
     GSList *list;
     list = object_class_get_list_sorted(TYPE_AVR_CPU, false);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, avr_cpu_list_entry, NULL);
     g_slist_free(list);
 }
Signature · 1 line
2.41.0
author
#24 lore raw

PATCH v4 8/33 target/cris: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/cris, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/cris/cpu.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/cris/cpu.c +3 -4
index a5083a0077..73fe1510f1 100644
@@ -119,11 +119,10 @@ static void cris_cpu_list_entry(gpointer data, gpointer user_data)
 {
     ObjectClass *oc = data;
     const char *typename = object_class_get_name(oc);
-    char *name;
+    char *model = cpu_model_from_type(typename);
 
-    name = g_strndup(typename, strlen(typename) - strlen(CRIS_CPU_TYPE_SUFFIX));
-    qemu_printf("  %s\n", name);
-    g_free(name);
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void cris_cpu_list(void)
Signature · 1 line
2.41.0
author
#25 lore raw

PATCH v4 9/33 target/hexagon: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/hexagon, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/hexagon/cpu.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
target/hexagon/cpu.c +5 -7
index aa48f5fe89..cfff005a1a 100644
@@ -34,13 +34,11 @@ static void hexagon_v73_cpu_init(Object *obj) { }
 
 static void hexagon_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *oc = data;
-    char *name = g_strdup(object_class_get_name(oc));
-    if (g_str_has_suffix(name, HEXAGON_CPU_TYPE_SUFFIX)) {
-        name[strlen(name) - strlen(HEXAGON_CPU_TYPE_SUFFIX)] = '\0';
-    }
-    qemu_printf("  %s\n", name);
-    g_free(name);
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void hexagon_cpu_list(void)
Signature · 1 line
2.41.0
author
#26 lore raw

PATCH v4 10/33 target/i386: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/i386, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to convert the CPU type name to the CPU model name.

---
 target/i386/cpu.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/i386/cpu.c +3 -4
index fc8484cb5e..f77149ed25 100644
@@ -1744,8 +1744,7 @@ static char *x86_cpu_class_get_model_name(X86CPUClass *cc)
 {
     const char *class_name = object_class_get_name(OBJECT_CLASS(cc));
     assert(g_str_has_suffix(class_name, X86_CPU_TYPE_SUFFIX));
-    return g_strndup(class_name,
-                     strlen(class_name) - strlen(X86_CPU_TYPE_SUFFIX));
+    return cpu_model_from_type(class_name);
 }
 
 typedef struct X86CPUVersionDefinition {
@@ -5547,7 +5546,7 @@ static void x86_cpu_list_entry(gpointer data, gpointer user_data)
 {
     ObjectClass *oc = data;
     X86CPUClass *cc = X86_CPU_CLASS(oc);
-    g_autofree char *name = x86_cpu_class_get_model_name(cc);
+    g_autofree char *model = x86_cpu_class_get_model_name(cc);
     g_autofree char *desc = g_strdup(cc->model_description);
     g_autofree char *alias_of = x86_cpu_class_get_alias_of(cc);
     g_autofree char *model_id = x86_cpu_class_get_model_id(cc);
@@ -5571,7 +5570,7 @@ static void x86_cpu_list_entry(gpointer data, gpointer user_data)
         desc = g_strdup_printf("%s (deprecated)", olddesc);
     }
 
-    qemu_printf("x86 %-20s  %s\n", name, desc);
+    qemu_printf("x86 %-20s  %s\n", model, desc);
 }
 
 /* list available CPU models and flags */
Signature · 1 line
2.41.0
author
#27 lore raw

PATCH v4 11/33 target/loongarch: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/loongarch, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/loongarch/cpu.c                | 5 ++++-
 target/loongarch/loongarch-qmp-cmds.c | 3 +--
 2 files changed, 5 insertions(+), 3 deletions(-)
target/loongarch/cpu.c +4 -1
index c6712e13f9..8c6b6f47ed 100644
@@ -477,14 +477,17 @@ static void loongarch_la132_initfn(Object *obj)
 static void loongarch_cpu_list_entry(gpointer data, gpointer user_data)
 {
     const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("%s\n", typename);
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void loongarch_cpu_list(void)
 {
     GSList *list;
     list = object_class_get_list_sorted(TYPE_LOONGARCH_CPU, false);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, loongarch_cpu_list_entry, NULL);
     g_slist_free(list);
 }
target/loongarch/loongarch-qmp-cmds.c +1 -2
index 6c25957881..815ceaf0ea 100644
@@ -17,8 +17,7 @@ static void loongarch_cpu_add_definition(gpointer data, gpointer user_data)
     CpuDefinitionInfo *info = g_new0(CpuDefinitionInfo, 1);
     const char *typename = object_class_get_name(oc);
 
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_LOONGARCH_CPU));
+    info->name = cpu_model_from_type(typename);
     info->q_typename = g_strdup(typename);
 
     QAPI_LIST_PREPEND(*cpu_list, info);
Signature · 1 line
2.41.0
author
#28 lore raw

PATCH v4 12/33 target/m68k: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/m68k, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/m68k/helper.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
target/m68k/helper.c +6 -8
index 0a1544cd68..47f2cee69a 100644
@@ -49,14 +49,11 @@ static gint m68k_cpu_list_compare(gconstpointer a, gconstpointer b)
 
 static void m68k_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *c = data;
-    const char *typename;
-    char *name;
-
-    typename = object_class_get_name(c);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_M68K_CPU));
-    qemu_printf("%s\n", name);
-    g_free(name);
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void m68k_cpu_list(void)
@@ -65,6 +62,7 @@ void m68k_cpu_list(void)
 
     list = object_class_get_list(TYPE_M68K_CPU, false);
     list = g_slist_sort(list, m68k_cpu_list_compare);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, m68k_cpu_list_entry, NULL);
     g_slist_free(list);
 }
Signature · 1 line
2.41.0
author
#29 lore raw

PATCH v4 13/33 target/mips: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/mips, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

Besides, mips_cpu_list() is reimplemented to dynamically fetch the CPU model names from the registered CPU types , instead of the staticly defined array.

---
 target/mips/cpu-defs.c.inc         |  9 ---------
 target/mips/cpu.c                  | 18 ++++++++++++++++++
 target/mips/sysemu/mips-qmp-cmds.c |  3 +--
 3 files changed, 19 insertions(+), 11 deletions(-)
target/mips/cpu-defs.c.inc +0 -9
index c0c389c59a..fbf787d8ce 100644
@@ -1018,15 +1018,6 @@ const mips_def_t mips_defs[] =
 };
 const int mips_defs_number = ARRAY_SIZE(mips_defs);
 
-void mips_cpu_list(void)
-{
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(mips_defs); i++) {
-        qemu_printf("MIPS '%s'\n", mips_defs[i].name);
-    }
-}
-
 static void fpu_init (CPUMIPSState *env, const mips_def_t *def)
 {
     int i;
target/mips/cpu.c +18 -0
index a0023edd43..131978563b 100644
@@ -565,6 +565,24 @@ static const struct TCGCPUOps mips_tcg_ops = {
 };
 #endif /* CONFIG_TCG */
 
+static void mips_cpu_list_entry(gpointer data, gpointer user_data)
+{
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
+}
+
+void mips_cpu_list(void)
+{
+    GSList *list;
+    list = object_class_get_list_sorted(TYPE_MIPS_CPU, false);
+    qemu_printf("Available CPUs:\n");
+    g_slist_foreach(list, mips_cpu_list_entry, NULL);
+    g_slist_free(list);
+}
+
 static void mips_cpu_class_init(ObjectClass *c, void *data)
 {
     MIPSCPUClass *mcc = MIPS_CPU_CLASS(c);
target/mips/sysemu/mips-qmp-cmds.c +1 -2
index 6db4626412..7340ac70ba 100644
@@ -19,8 +19,7 @@ static void mips_cpu_add_definition(gpointer data, gpointer user_data)
 
     typename = object_class_get_name(oc);
     info = g_malloc0(sizeof(*info));
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_MIPS_CPU));
+    info->name = cpu_model_from_type(typename);
     info->q_typename = g_strdup(typename);
 
     QAPI_LIST_PREPEND(*cpu_list, info);
Signature · 1 line
2.41.0
author
#30 lore raw

PATCH v4 14/33 target/openrisc: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/openrisc, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/openrisc/cpu.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
target/openrisc/cpu.c +5 -9
index f7d53c592a..701aa62293 100644
@@ -273,15 +273,11 @@ static gint openrisc_cpu_list_compare(gconstpointer a, gconstpointer b)
 
 static void openrisc_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *oc = data;
-    const char *typename;
-    char *name;
-
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename,
-                     strlen(typename) - strlen("-" TYPE_OPENRISC_CPU));
-    qemu_printf("  %s\n", name);
-    g_free(name);
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void cpu_openrisc_list(void)
Signature · 1 line
2.41.0
author
#31 lore raw

PATCH v4 15/33 target/ppc: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/ppc, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/ppc/cpu_init.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
target/ppc/cpu_init.c +6 -6
index 40fe14a6c2..a8ab698700 100644
@@ -7029,16 +7029,15 @@ static void ppc_cpu_list_entry(gpointer data, gpointer user_data)
     PowerPCCPUClass *pcc = POWERPC_CPU_CLASS(oc);
     DeviceClass *family = DEVICE_CLASS(ppc_cpu_get_family_class(pcc));
     const char *typename = object_class_get_name(oc);
-    char *name;
+    char *model;
     int i;
 
     if (unlikely(strcmp(typename, TYPE_HOST_POWERPC_CPU) == 0)) {
         return;
     }
 
-    name = g_strndup(typename,
-                     strlen(typename) - strlen(POWERPC_CPU_TYPE_SUFFIX));
-    qemu_printf("PowerPC %-16s PVR %08x\n", name, pcc->pvr);
+    model = cpu_model_from_type(typename);
+    qemu_printf("PowerPC %-16s PVR %08x\n", model, pcc->pvr);
     for (i = 0; ppc_cpu_aliases[i].alias != NULL; i++) {
         PowerPCCPUAlias *alias = &ppc_cpu_aliases[i];
         ObjectClass *alias_oc = ppc_cpu_class_by_name(alias->model);
@@ -7055,10 +7054,10 @@ static void ppc_cpu_list_entry(gpointer data, gpointer user_data)
                         alias->alias, family->desc);
         } else {
             qemu_printf("PowerPC %-16s (alias for %s)\n",
-                        alias->alias, name);
+                        alias->alias, model);
         }
     }
-    g_free(name);
+    g_free(model);
 }
 
 void ppc_cpu_list(void)
@@ -7067,6 +7066,7 @@ void ppc_cpu_list(void)
 
     list = object_class_get_list(TYPE_POWERPC_CPU, false);
     list = g_slist_sort(list, ppc_cpu_list_compare);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, ppc_cpu_list_entry, NULL);
     g_slist_free(list);
 
Signature · 1 line
2.41.0
author
#32 lore raw

PATCH v4 16/33 target/riscv: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/riscv, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

Besides, @cpuname is renamed to @model in riscv_cpu_class_by_name() since it points to CPU model name instead of CPU type name.

---
 target/riscv/cpu.c            | 14 ++++++++------
 target/riscv/riscv-qmp-cmds.c |  3 +--
 2 files changed, 9 insertions(+), 8 deletions(-)
target/riscv/cpu.c +8 -6
index 8b4024338c..9f47379145 100644
@@ -629,12 +629,12 @@ static ObjectClass *riscv_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
     char *typename;
-    char **cpuname;
+    char **model;
 
-    cpuname = g_strsplit(cpu_model, ",", 1);
-    typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), cpuname[0]);
+    model = g_strsplit(cpu_model, ",", 1);
+    typename = g_strdup_printf(RISCV_CPU_TYPE_NAME("%s"), model[0]);
     oc = object_class_by_name(typename);
-    g_strfreev(cpuname);
+    g_strfreev(model);
     g_free(typename);
 
     return oc;
@@ -1644,9 +1644,10 @@ static gint riscv_cpu_list_compare(gconstpointer a, gconstpointer b)
 static void riscv_cpu_list_entry(gpointer data, gpointer user_data)
 {
     const char *typename = object_class_get_name(OBJECT_CLASS(data));
-    int len = strlen(typename) - strlen(RISCV_CPU_TYPE_SUFFIX);
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("%.*s\n", len, typename);
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void riscv_cpu_list(void)
@@ -1655,6 +1656,7 @@ void riscv_cpu_list(void)
 
     list = object_class_get_list(TYPE_RISCV_CPU, false);
     list = g_slist_sort(list, riscv_cpu_list_compare);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, riscv_cpu_list_entry, NULL);
     g_slist_free(list);
 }
target/riscv/riscv-qmp-cmds.c +1 -2
index 5ecff1afb3..22f728673f 100644
@@ -35,8 +35,7 @@ static void riscv_cpu_add_definition(gpointer data, gpointer user_data)
     const char *typename = object_class_get_name(oc);
     ObjectClass *dyn_class;
 
-    info->name = g_strndup(typename,
-                           strlen(typename) - strlen("-" TYPE_RISCV_CPU));
+    info->name = cpu_model_from_type(typename);
     info->q_typename = g_strdup(typename);
 
     dyn_class = object_class_dynamic_cast(oc, TYPE_RISCV_DYNAMIC_CPU);
Signature · 1 line
2.41.0
author
#33 lore raw

PATCH v4 17/33 target/rx: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/rx, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/rx/cpu.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/rx/cpu.c +4 -2
index 0063837e93..fea7f596ca 100644
@@ -91,9 +91,11 @@ static void rx_cpu_reset_hold(Object *obj)
 
 static void rx_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *oc = data;
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("  %s\n", object_class_get_name(oc));
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void rx_cpu_list(void)
Signature · 1 line
2.41.0
author
#34 lore raw

PATCH v4 18/33 target/s390x: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/s390x, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/s390x/cpu_models.c        | 12 ++++++------
 target/s390x/cpu_models_sysemu.c |  9 ++++-----
 2 files changed, 10 insertions(+), 11 deletions(-)
target/s390x/cpu_models.c +6 -6
index 4dead48650..71635b92c8 100644
@@ -339,7 +339,8 @@ static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
 {
     const S390CPUClass *scc = S390_CPU_CLASS((ObjectClass *)data);
     CPUClass *cc = CPU_CLASS(scc);
-    char *name = g_strdup(object_class_get_name((ObjectClass *)data));
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
     g_autoptr(GString) details = g_string_new("");
 
     if (scc->is_static) {
@@ -356,14 +357,12 @@ static void s390_print_cpu_model_list_entry(gpointer data, gpointer user_data)
         g_string_truncate(details, details->len - 2);
     }
 
-    /* strip off the -s390x-cpu */
-    g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
     if (details->len) {
-        qemu_printf("s390 %-15s %-35s (%s)\n", name, scc->desc, details->str);
+        qemu_printf("s390 %-15s %-35s (%s)\n", model, scc->desc, details->str);
     } else {
-        qemu_printf("s390 %-15s %-35s\n", name, scc->desc);
+        qemu_printf("s390 %-15s %-35s\n", model, scc->desc);
     }
-    g_free(name);
+    g_free(model);
 }
 
 static gint s390_cpu_list_compare(gconstpointer a, gconstpointer b)
@@ -408,6 +407,7 @@ void s390_cpu_list(void)
 
     list = object_class_get_list(TYPE_S390_CPU, false);
     list = g_slist_sort(list, s390_cpu_list_compare);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, s390_print_cpu_model_list_entry, NULL);
     g_slist_free(list);
 
target/s390x/cpu_models_sysemu.c +4 -5
index 63981bf36b..c41af253d3 100644
@@ -55,17 +55,16 @@ static void create_cpu_model_list(ObjectClass *klass, void *opaque)
     struct CpuDefinitionInfoListData *cpu_list_data = opaque;
     CpuDefinitionInfoList **cpu_list = &cpu_list_data->list;
     CpuDefinitionInfo *info;
-    char *name = g_strdup(object_class_get_name(klass));
+    const char *typename = object_class_get_name(klass);
+    char *model = cpu_model_from_type(typename);
     S390CPUClass *scc = S390_CPU_CLASS(klass);
 
-    /* strip off the -s390x-cpu */
-    g_strrstr(name, "-" TYPE_S390_CPU)[0] = 0;
     info = g_new0(CpuDefinitionInfo, 1);
-    info->name = name;
+    info->name = model;
     info->has_migration_safe = true;
     info->migration_safe = scc->is_migration_safe;
     info->q_static = scc->is_static;
-    info->q_typename = g_strdup(object_class_get_name(klass));
+    info->q_typename = g_strdup(typename);
     /* check for unavailable features */
     if (cpu_list_data->model) {
         Object *obj;
Signature · 1 line
2.41.0
author
#35 lore raw

PATCH v4 19/33 target/sh4: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/sh4, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

Besides, superh_cpu_class_by_name() is improved by avoiding "goto out" tag and renaming @s to @model since it points to CPU model name.

---
 target/sh4/cpu.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
target/sh4/cpu.c +12 -11
index a8ec98b134..6097ddd52d 100644
@@ -125,9 +125,10 @@ static void superh_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
 static void superh_cpu_list_entry(gpointer data, gpointer user_data)
 {
     const char *typename = object_class_get_name(OBJECT_CLASS(data));
-    int len = strlen(typename) - strlen(SUPERH_CPU_TYPE_SUFFIX);
+    char *model = cpu_model_from_type(typename);
 
-    qemu_printf("%.*s\n", len, typename);
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void sh4_cpu_list(void)
@@ -135,6 +136,7 @@ void sh4_cpu_list(void)
     GSList *list;
 
     list = object_class_get_list_sorted(TYPE_SUPERH_CPU, false);
+    qemu_printf("Available CPUs:\n");
     g_slist_foreach(list, superh_cpu_list_entry, NULL);
     g_slist_free(list);
 }
@@ -142,20 +144,19 @@ void sh4_cpu_list(void)
 static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
-    char *s, *typename = NULL;
+    char *model, *typename;
 
-    s = g_ascii_strdown(cpu_model, -1);
-    if (strcmp(s, "any") == 0) {
-        oc = object_class_by_name(TYPE_SH7750R_CPU);
-        goto out;
+    model = g_ascii_strdown(cpu_model, -1);
+    if (strcmp(model, "any") == 0) {
+        typename = g_strdup(TYPE_SH7750R_CPU);
+    } else {
+        typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), model);
     }
 
-    typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
     oc = object_class_by_name(typename);
-
-out:
-    g_free(s);
+    g_free(model);
     g_free(typename);
+
     return oc;
 }
 
Signature · 1 line
2.41.0
author
#36 lore raw

PATCH v4 20/33 target/tricore: Use generic helper to show CPU model names

In reply to Gavin Shan

For target/tricore, the registered CPU type name is always the combination of the CPU model name and suffix. Use cpu_model_from_type() to show the CPU model names.

---
 target/tricore/helper.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
target/tricore/helper.c +5 -8
index 7e5da3cb23..1b5fbd4ab0 100644
@@ -98,14 +98,11 @@ bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
 
 static void tricore_cpu_list_entry(gpointer data, gpointer user_data)
 {
-    ObjectClass *oc = data;
-    const char *typename;
-    char *name;
-
-    typename = object_class_get_name(oc);
-    name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_TRICORE_CPU));
-    qemu_printf("  %s\n", name);
-    g_free(name);
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
 }
 
 void tricore_cpu_list(void)
Signature · 1 line
2.41.0
author
#37 lore raw

PATCH v4 21/33 target/hppa: Implement hppa_cpu_list()

In reply to Gavin Shan

Implement hppa_cpu_list() to support cpu_list(). With this applied, the available CPU model names, same to the CPU type names, are shown as below.

  $ ./build/qemu-system-hppa -cpu ?
  Available CPUs:
    hppa-cpu
---
 target/hppa/cpu.c | 19 +++++++++++++++++++
 target/hppa/cpu.h |  3 +++
 2 files changed, 22 insertions(+)
target/hppa/cpu.c +19 -0
index 1644297bf8..2b5198d7c8 100644
@@ -143,6 +143,25 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
 #endif
 }
 
+static void hppa_cpu_list_entry(gpointer data, gpointer user_data)
+{
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
+}
+
+void hppa_cpu_list(void)
+{
+    GSList *list;
+
+    list = object_class_get_list_sorted(TYPE_HPPA_CPU, false);
+    qemu_printf("Available CPUs:\n");
+    g_slist_foreach(list, hppa_cpu_list_entry, NULL);
+    g_slist_free(list);
+}
+
 static void hppa_cpu_initfn(Object *obj)
 {
     CPUState *cs = CPU(obj);
target/hppa/cpu.h +3 -0
index 798d0c26d7..d8106c89dc 100644
@@ -357,5 +357,8 @@ void hppa_cpu_alarm_timer(void *);
 int hppa_artype_for_page(CPUHPPAState *env, target_ulong vaddr);
 #endif
 G_NORETURN void hppa_dynamic_excp(CPUHPPAState *env, int excp, uintptr_t ra);
+void hppa_cpu_list(void);
+
+#define cpu_list hppa_cpu_list
 
 #endif /* HPPA_CPU_H */
Signature · 1 line
2.41.0
author
#38 lore raw

PATCH v4 22/33 target/microblaze: Implement microblaze_cpu_list()

In reply to Gavin Shan

Implement microblaze_cpu_list() to support cpu_list(). With this applied, the available CPU model names, same to the CPU type names, are shown as below.

  $ ./build/qemu-system-hppa -cpu ?
  Available CPUs:
    microblaze-cpu
---
 target/microblaze/cpu.c | 20 ++++++++++++++++++++
 target/microblaze/cpu.h |  3 +++
 2 files changed, 23 insertions(+)
target/microblaze/cpu.c +20 -0
index bbb3335cad..7d05dd954c 100644
@@ -24,6 +24,7 @@
 #include "qemu/osdep.h"
 #include "qemu/log.h"
 #include "qapi/error.h"
+#include "qemu/qemu-print.h"
 #include "cpu.h"
 #include "qemu/module.h"
 #include "hw/qdev-properties.h"
@@ -291,6 +292,25 @@ static void mb_cpu_realizefn(DeviceState *dev, Error **errp)
     mcc->parent_realize(dev, errp);
 }
 
+static void microblaze_cpu_list_entry(gpointer data, gpointer user_data)
+{
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
+}
+
+void microblaze_cpu_list(void)
+{
+    GSList *list;
+
+    list = object_class_get_list_sorted(TYPE_MICROBLAZE_CPU, false);
+    qemu_printf("Available CPUs:\n");
+    g_slist_foreach(list, microblaze_cpu_list_entry, NULL);
+    g_slist_free(list);
+}
+
 static void mb_cpu_initfn(Object *obj)
 {
     MicroBlazeCPU *cpu = MICROBLAZE_CPU(obj);
target/microblaze/cpu.h +3 -0
index e43c49d4af..d5ad25a866 100644
@@ -372,6 +372,9 @@ int mb_cpu_gdb_read_register(CPUState *cpu, GByteArray *buf, int reg);
 int mb_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg);
 int mb_cpu_gdb_read_stack_protect(CPUArchState *cpu, GByteArray *buf, int reg);
 int mb_cpu_gdb_write_stack_protect(CPUArchState *cpu, uint8_t *buf, int reg);
+void microblaze_cpu_list(void);
+
+#define cpu_list microblaze_cpu_list
 
 static inline uint32_t mb_cpu_read_msr(const CPUMBState *env)
 {
Signature · 1 line
2.41.0
author
#39 lore raw

PATCH v4 23/33 target/nios2: Implement nios2_cpu_list()

In reply to Gavin Shan

Implement nios2_cpu_list() to support cpu_list(). With this applied, the available CPU model names, same to the CPU type names, are shown as below.

  $ ./build/qemu-system-nios2 -cpu ?
  Available CPUs:
    nios2-cpu
---
 target/nios2/cpu.c | 20 ++++++++++++++++++++
 target/nios2/cpu.h |  3 +++
 2 files changed, 23 insertions(+)
target/nios2/cpu.c +20 -0
index 15e499f828..5981418aee 100644
@@ -21,6 +21,7 @@
 #include "qemu/osdep.h"
 #include "qemu/module.h"
 #include "qapi/error.h"
+#include "qemu/qemu-print.h"
 #include "cpu.h"
 #include "exec/log.h"
 #include "gdbstub/helpers.h"
@@ -111,6 +112,25 @@ static void iic_set_irq(void *opaque, int irq, int level)
 }
 #endif
 
+static void nios2_cpu_list_entry(gpointer data, gpointer user_data)
+{
+    const char *typename = object_class_get_name(OBJECT_CLASS(data));
+    char *model = cpu_model_from_type(typename);
+
+    qemu_printf("  %s\n", model);
+    g_free(model);
+}
+
+void nios2_cpu_list(void)
+{
+    GSList *list;
+
+    list = object_class_get_list_sorted(TYPE_NIOS2_CPU, false);
+    qemu_printf("Available CPUs:\n");
+    g_slist_foreach(list, nios2_cpu_list_entry, NULL);
+    g_slist_free(list);
+}
+
 static void nios2_cpu_initfn(Object *obj)
 {
 #if !defined(CONFIG_USER_ONLY)
target/nios2/cpu.h +3 -0
index 70b6377a4f..93bf84b620 100644
@@ -291,6 +291,9 @@ bool nios2_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                         MMUAccessType access_type, int mmu_idx,
                         bool probe, uintptr_t retaddr);
 #endif
+void nios2_cpu_list(void);
+
+#define cpu_list nios2_cpu_list
 
 typedef CPUNios2State CPUArchState;
 typedef Nios2CPU ArchCPU;
Signature · 1 line
2.41.0
author
#40 lore raw

PATCH v4 24/33 cpu: Mark cpu_list() supported on all targets

In reply to Gavin Shan

Remove the false conditions and comments since cpu_list() has been supported on all targets.

---
 bsd-user/main.c | 3 ---
 cpu-target.c    | 3 ---
 2 files changed, 6 deletions(-)
bsd-user/main.c +0 -3
index c402fadf46..d3612ef0f5 100644
@@ -378,10 +378,7 @@ int main(int argc, char **argv)
         } else if (!strcmp(r, "cpu")) {
             cpu_model = argv[optind++];
             if (is_help_option(cpu_model)) {
-                /* XXX: implement xxx_cpu_list for targets that still miss it */
-#if defined(cpu_list)
                 cpu_list();
-#endif
                 exit(1);
             }
         } else if (!strcmp(r, "B")) {
cpu-target.c +0 -3
index 344bad5736..3037a2c6ee 100644
@@ -310,10 +310,7 @@ const char *parse_cpu_option(const char *cpu_option)
 
 void list_cpus(void)
 {
-    /* XXX: implement xxx_cpu_list for targets that still miss it */
-#if defined(cpu_list)
     cpu_list();
-#endif
 }
 
 #if defined(CONFIG_USER_ONLY)
Signature · 1 line
2.41.0
author
#41 lore raw

PATCH v4 25/33 machine: Constify MachineClass::valid_cpu_types[i]

In reply to Gavin Shan

Constify MachineClass::valid_cpu_types[i], as suggested by Richard Henderson.

---
 hw/m68k/q800.c      | 2 +-
 include/hw/boards.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/m68k/q800.c +1 -1
index 1d7cd5ff1c..38d4bc2013 100644
@@ -726,7 +726,7 @@ static GlobalProperty hw_compat_q800[] = {
 };
 static const size_t hw_compat_q800_len = G_N_ELEMENTS(hw_compat_q800);
 
-static const char *q800_machine_valid_cpu_types[] = {
+static const char * const q800_machine_valid_cpu_types[] = {
     M68K_CPU_TYPE_NAME("m68040"),
     NULL
 };
include/hw/boards.h +1 -1
index a735999298..da85f86efb 100644
@@ -273,7 +273,7 @@ struct MachineClass {
     bool has_hotpluggable_cpus;
     bool ignore_memory_transaction_failures;
     int numa_mem_align_shift;
-    const char **valid_cpu_types;
+    const char * const *valid_cpu_types;
     strList *allowed_dynamic_sysbus_devices;
     bool auto_enable_numa_with_memhp;
     bool auto_enable_numa_with_memdev;
Signature · 1 line
2.41.0
Reviewed-by on v4 25/33
#42 lore raw

Re: [PATCH v4 25/33] machine: Constify MachineClass::valid_cpu_types[i]

Quoted 10 lines · Gavin Shan

Constify MachineClass::valid_cpu_types[i], as suggested by Richard Henderson.

---
  hw/m68k/q800.c      | 2 +-
  include/hw/boards.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)

r~

author
#43 lore raw

PATCH v4 26/33 machine: Use error handling when CPU type is checked

In reply to Gavin Shan

QEMU will be terminated if the specified CPU type isn't supported in machine_run_board_init(). The list of supported CPU type names is tracked by mc->valid_cpu_types.

The error handling can be used to propagate error messages, to be consistent how the errors are handled for other situations in the same function.

No functional change intended.

---
 hw/core/machine.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
hw/core/machine.c +8 -6
index 50edaab737..1c17a0d5bf 100644
@@ -1393,6 +1393,7 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
     ObjectClass *oc = object_class_by_name(machine->cpu_type);
     CPUClass *cc;
+    Error *local_err = NULL;
 
     /* This checkpoint is required by replay to separate prior clock
        reading from the other reads, because timer polling functions query
@@ -1465,15 +1466,16 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
 
         if (!machine_class->valid_cpu_types[i]) {
             /* The user specified CPU is not valid */
-            error_report("Invalid CPU type: %s", machine->cpu_type);
-            error_printf("The valid types are: %s",
-                         machine_class->valid_cpu_types[0]);
+            error_setg(&local_err, "Invalid CPU type: %s", machine->cpu_type);
+            error_append_hint(&local_err, "The valid types are: %s",
+                              machine_class->valid_cpu_types[0]);
             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
-                error_printf(", %s", machine_class->valid_cpu_types[i]);
+                error_append_hint(&local_err, ", %s",
+                                  machine_class->valid_cpu_types[i]);
             }
-            error_printf("\n");
+            error_append_hint(&local_err, "\n");
 
-            exit(1);
+            error_propagate(errp, local_err);
         }
     }
 
Signature · 1 line
2.41.0
#44 lore raw

Re: [PATCH v4 26/33] machine: Use error handling when CPU type is checked

Quoted 21 lines · Gavin Shan

QEMU will be terminated if the specified CPU type isn't supported in machine_run_board_init(). The list of supported CPU type names is tracked by mc->valid_cpu_types.

The error handling can be used to propagate error messages, to be consistent how the errors are handled for other situations in the same function.

No functional change intended.

---
  hw/core/machine.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
hw/core/machine.c +0 -0
index 50edaab737..1c17a0d5bf 100644

hw/core/machine.c void machine_run_board_init(MachineState *machine, const char *mem_path, Error * · line 1396

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -1393,6 +1393,7 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
      MachineClass *machine_class = MACHINE_GET_CLASS(machine);
      ObjectClass *oc = object_class_by_name(machine->cpu_type);
      CPUClass *cc;
+    Error *local_err = NULL;

This...

hw/core/machine.c void machine_run_board_init(MachineState *machine, const char *mem_path, Error * · line 1469

Quoted 11 lines
hw/core/machine.c +1 -3
      /* This checkpoint is required by replay to separate prior clock
         reading from the other reads, because timer polling functions query
@@ -1465,15 +1466,16 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
 
          if (!machine_class->valid_cpu_types[i]) {
              /* The user specified CPU is not valid */
-            error_report("Invalid CPU type: %s", machine->cpu_type);
-            error_printf("The valid types are: %s",
-                         machine_class->valid_cpu_types[0]);
+            error_setg(&local_err, "Invalid CPU type: %s", machine->cpu_type);

... could go in this block.

Though I don't see why you can't write to errp directly?

r~

author
#45 lore raw

PATCH v4 27/33 machine: Introduce helper is_cpu_type_supported()

In reply to Gavin Shan

The logic, to check if the specified CPU type is supported in machine_run_board_init(), is independent enough. Factor it out into helper is_cpu_type_supported(). machine_run_board_init() looks a bit clean with this. Since we're here, @machine_class is renamed to @mc to avoid multiple line spanning of code. The comments are tweaked a bit either.

No functional change intended.

---
 hw/core/machine.c | 82 +++++++++++++++++++++++++----------------------
 1 file changed, 44 insertions(+), 38 deletions(-)
hw/core/machine.c +44 -38
index 1c17a0d5bf..2d78692df1 100644
@@ -1386,13 +1386,51 @@ out:
     return r;
 }
 
+static void is_cpu_type_supported(MachineState *machine, Error **errp)
+{
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+    ObjectClass *oc = object_class_by_name(machine->cpu_type);
+    CPUClass *cc;
+    int i;
+
+    /*
+     * Check if the user specified CPU type is supported when the valid
+     * CPU types have been determined. Note that the user specified CPU
+     * type is provided through '-cpu' option.
+     */
+    if (mc->valid_cpu_types && machine->cpu_type) {
+        for (i = 0; mc->valid_cpu_types[i]; i++) {
+            if (object_class_dynamic_cast(oc, mc->valid_cpu_types[i])) {
+                break;
+            }
+        }
+
+        /* The user specified CPU type isn't valid */
+        if (!mc->valid_cpu_types[i]) {
+            error_setg(errp, "Invalid CPU type: %s", machine->cpu_type);
+            error_append_hint(errp, "The valid types are: %s",
+                              mc->valid_cpu_types[0]);
+            for (i = 1; mc->valid_cpu_types[i]; i++) {
+                error_append_hint(errp, ", %s", mc->valid_cpu_types[i]);
+            }
+
+            error_append_hint(errp, "\n");
+            return;
+        }
+    }
+
+    /* Check if CPU type is deprecated and warn if so */
+    cc = CPU_CLASS(oc);
+    if (cc && cc->deprecation_note) {
+        warn_report("CPU model %s is deprecated -- %s",
+                    machine->cpu_type, cc->deprecation_note);
+    }
+}
 
 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
 {
     ERRP_GUARD();
     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
-    ObjectClass *oc = object_class_by_name(machine->cpu_type);
-    CPUClass *cc;
     Error *local_err = NULL;
 
     /* This checkpoint is required by replay to separate prior clock
@@ -1448,42 +1486,10 @@ void machine_run_board_init(MachineState *machine, const char *mem_path, Error *
         machine->ram = machine_consume_memdev(machine, machine->memdev);
     }
 
-    /* If the machine supports the valid_cpu_types check and the user
-     * specified a CPU with -cpu check here that the user CPU is supported.
-     */
-    if (machine_class->valid_cpu_types && machine->cpu_type) {
-        int i;
-
-        for (i = 0; machine_class->valid_cpu_types[i]; i++) {
-            if (object_class_dynamic_cast(oc,
-                                          machine_class->valid_cpu_types[i])) {
-                /* The user specified CPU is in the valid field, we are
-                 * good to go.
-                 */
-                break;
-            }
-        }
-
-        if (!machine_class->valid_cpu_types[i]) {
-            /* The user specified CPU is not valid */
-            error_setg(&local_err, "Invalid CPU type: %s", machine->cpu_type);
-            error_append_hint(&local_err, "The valid types are: %s",
-                              machine_class->valid_cpu_types[0]);
-            for (i = 1; machine_class->valid_cpu_types[i]; i++) {
-                error_append_hint(&local_err, ", %s",
-                                  machine_class->valid_cpu_types[i]);
-            }
-            error_append_hint(&local_err, "\n");
-
-            error_propagate(errp, local_err);
-        }
-    }
-
-    /* Check if CPU type is deprecated and warn if so */
-    cc = CPU_CLASS(oc);
-    if (cc && cc->deprecation_note) {
-        warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
-                    cc->deprecation_note);
+    /* Check if the CPU type is supported */
+    is_cpu_type_supported(machine, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
     }
 
     if (machine->cgs) {
Signature · 1 line
2.41.0
#46 lore raw

Re: [PATCH v4 27/33] machine: Introduce helper is_cpu_type_supported()

Quoted 19 lines · Gavin Shan

The logic, to check if the specified CPU type is supported in machine_run_board_init(), is independent enough. Factor it out into helper is_cpu_type_supported(). machine_run_board_init() looks a bit clean with this. Since we're here, @machine_class is renamed to @mc to avoid multiple line spanning of code. The comments are tweaked a bit either.

No functional change intended.

---
  hw/core/machine.c | 82 +++++++++++++++++++++++++----------------------
  1 file changed, 44 insertions(+), 38 deletions(-)
hw/core/machine.c +0 -0
index 1c17a0d5bf..2d78692df1 100644

hw/core/machine.c out: · line 1392

Quoted 7 lines
hw/core/machine.c +4 -0
@@ -1386,13 +1386,51 @@ out:
      return r;
  }
 
+static void is_cpu_type_supported(MachineState *machine, Error **errp)
+{
+    MachineClass *mc = MACHINE_GET_CLASS(machine);
+    ObjectClass *oc = object_class_by_name(machine->cpu_type);

machine->cpu_type is used...

hw/core/machine.c out: · line 1401

Quoted 9 lines
hw/core/machine.c +9 -0
+    CPUClass *cc;
+    int i;
+
+    /*
+     * Check if the user specified CPU type is supported when the valid
+     * CPU types have been determined. Note that the user specified CPU
+     * type is provided through '-cpu' option.
+     */
+    if (mc->valid_cpu_types && machine->cpu_type) {

... before checking that it is set.

Quoted 12 lines
hw/core/machine.c +12 -0
+        for (i = 0; mc->valid_cpu_types[i]; i++) {
+            if (object_class_dynamic_cast(oc, mc->valid_cpu_types[i])) {
+                break;
+            }
+        }
+
+        /* The user specified CPU type isn't valid */
+        if (!mc->valid_cpu_types[i]) {
+            error_setg(errp, "Invalid CPU type: %s", machine->cpu_type);
+            error_append_hint(errp, "The valid types are: %s",
+                              mc->valid_cpu_types[0]);
+            for (i = 1; mc->valid_cpu_types[i]; i++) {

hw/core/machine.c out: · line 1423

Quoted 10 lines
hw/core/machine.c +10 -0
+                error_append_hint(errp, ", %s", mc->valid_cpu_types[i]);
+            }
+
+            error_append_hint(errp, "\n");
+            return;
+        }
+    }
+
+    /* Check if CPU type is deprecated and warn if so */
+    cc = CPU_CLASS(oc);

... and here you've not even checked that oc resolved correctly.

hw/core/machine.c out: · line 1424

Quoted 1 line
hw/core/machine.c +1 -0
+    if (cc && cc->deprecation_note) {

I guess you're assuming that CPU_CLASS is a plain cast, so this second check filters NULL.

r~

author
#47 lore raw

PATCH v4 28/33 machine: Print CPU model name instead of CPU type name

In reply to Gavin Shan

The names of supported CPU models instead of CPU types should be printed when the user specified CPU type isn't supported, to be consistent with the output from '-cpu ?'.

Correct the error messages to print CPU model names instead of CPU type names.

---
 hw/core/machine.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
hw/core/machine.c +15 -4
index 2d78692df1..1dd0f8831b 100644
@@ -1391,6 +1391,7 @@ static void is_cpu_type_supported(MachineState *machine, Error **errp)
     MachineClass *mc = MACHINE_GET_CLASS(machine);
     ObjectClass *oc = object_class_by_name(machine->cpu_type);
     CPUClass *cc;
+    char *model;
     int i;
 
     /*
@@ -1407,11 +1408,21 @@ static void is_cpu_type_supported(MachineState *machine, Error **errp)
 
         /* The user specified CPU type isn't valid */
         if (!mc->valid_cpu_types[i]) {
-            error_setg(errp, "Invalid CPU type: %s", machine->cpu_type);
-            error_append_hint(errp, "The valid types are: %s",
-                              mc->valid_cpu_types[0]);
+            model = cpu_model_from_type(machine->cpu_type);
+            g_assert(model != NULL);
+            error_setg(errp, "Invalid CPU type: %s", model);
+            g_free(model);
+
+            model = cpu_model_from_type(mc->valid_cpu_types[0]);
+            g_assert(model != NULL);
+            error_append_hint(errp, "The valid types are: %s", model);
+            g_free(model);
+
             for (i = 1; mc->valid_cpu_types[i]; i++) {
-                error_append_hint(errp, ", %s", mc->valid_cpu_types[i]);
+                model = cpu_model_from_type(mc->valid_cpu_types[i]);
+                g_assert(model != NULL);
+                error_append_hint(errp, ", %s", model);
+                g_free(model);
             }
 
             error_append_hint(errp, "\n");
Signature · 1 line
2.41.0
#48 lore raw

Re: [PATCH v4 28/33] machine: Print CPU model name instead of CPU type name

Quoted 17 lines · Gavin Shan

The names of supported CPU models instead of CPU types should be printed when the user specified CPU type isn't supported, to be consistent with the output from '-cpu ?'.

Correct the error messages to print CPU model names instead of CPU type names.

---
  hw/core/machine.c | 19 +++++++++++++++----
  1 file changed, 15 insertions(+), 4 deletions(-)
hw/core/machine.c +0 -0
index 2d78692df1..1dd0f8831b 100644

hw/core/machine.c static void is_cpu_type_supported(MachineState *machine, Error **errp) · line 1394

Quoted 4 lines
hw/core/machine.c +1 -0
@@ -1391,6 +1391,7 @@ static void is_cpu_type_supported(MachineState *machine, Error **errp)
      MachineClass *mc = MACHINE_GET_CLASS(machine);
      ObjectClass *oc = object_class_by_name(machine->cpu_type);
      CPUClass *cc;
+    char *model;

No need for this outside...

hw/core/machine.c static void is_cpu_type_supported(MachineState *machine, Error **errp) · line 1410

Quoted 7 lines
hw/core/machine.c +0 -0
      int i;
 
      /*
@@ -1407,11 +1408,21 @@ static void is_cpu_type_supported(MachineState *machine, Error **errp)
 
          /* The user specified CPU type isn't valid */
          if (!mc->valid_cpu_types[i]) {

... this block.

Quoted 10 lines
hw/core/machine.c +7 -3
-            error_setg(errp, "Invalid CPU type: %s", machine->cpu_type);
-            error_append_hint(errp, "The valid types are: %s",
-                              mc->valid_cpu_types[0]);
+            model = cpu_model_from_type(machine->cpu_type);
+            g_assert(model != NULL);
+            error_setg(errp, "Invalid CPU type: %s", model);
+            g_free(model);
+
+            model = cpu_model_from_type(mc->valid_cpu_types[0]);
+            g_assert(model != NULL);

hw/core/machine.c static void is_cpu_type_supported(MachineState *machine, Error **errp) · line 1426

Quoted 10 lines
hw/core/machine.c +7 -1
+            error_append_hint(errp, "The valid types are: %s", model);
+            g_free(model);
+
              for (i = 1; mc->valid_cpu_types[i]; i++) {
-                error_append_hint(errp, ", %s", mc->valid_cpu_types[i]);
+                model = cpu_model_from_type(mc->valid_cpu_types[i]);
+                g_assert(model != NULL);
+                error_append_hint(errp, ", %s", model);
+                g_free(model);
              }

I really don't like all of the replicated asserts for non-null.

You already know there's no path through cpu_model_from_type that doesn't return something... And anyway, in extremis, "(nil)" will print just fine.

r~

author
#49 lore raw

PATCH v4 29/33 hw/arm/virt: Check CPU type in machine_run_board_init()

In reply to Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do the check by ourselves.

---
 hw/arm/virt.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
hw/arm/virt.c +3 -18
index 92085d2d8f..5bacd3a7f0 100644
@@ -203,7 +203,7 @@ static const int a15irqmap[] = {
     [VIRT_PLATFORM_BUS] = 112, /* ...to 112 + PLATFORM_BUS_NUM_IRQS -1 */
 };
 
-static const char *valid_cpus[] = {
+static const char * const valid_cpu_types[] = {
 #ifdef CONFIG_TCG
     ARM_CPU_TYPE_NAME("cortex-a7"),
     ARM_CPU_TYPE_NAME("cortex-a15"),
@@ -221,20 +221,9 @@ static const char *valid_cpus[] = {
     ARM_CPU_TYPE_NAME("cortex-a57"),
     ARM_CPU_TYPE_NAME("host"),
     ARM_CPU_TYPE_NAME("max"),
+    NULL
 };
 
-static bool cpu_type_valid(const char *cpu)
-{
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
-        if (strcmp(cpu, valid_cpus[i]) == 0) {
-            return true;
-        }
-    }
-    return false;
-}
-
 static void create_randomness(MachineState *ms, const char *node)
 {
     struct {
@@ -2034,11 +2023,6 @@ static void machvirt_init(MachineState *machine)
     unsigned int smp_cpus = machine->smp.cpus;
     unsigned int max_cpus = machine->smp.max_cpus;
 
-    if (!cpu_type_valid(machine->cpu_type)) {
-        error_report("mach-virt: CPU type %s not supported", machine->cpu_type);
-        exit(1);
-    }
-
     possible_cpus = mc->possible_cpu_arch_ids(machine);
 
     /*
@@ -2957,6 +2941,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
 #else
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("max");
 #endif
+    mc->valid_cpu_types = valid_cpu_types;
     mc->get_default_cpu_node_id = virt_get_default_cpu_node_id;
     mc->kvm_type = virt_kvm_type;
     assert(!mc->get_hotplug_handler);
Signature · 1 line
2.41.0
Reviewed-by on v4 29/33
#50 lore raw

Re: [PATCH v4 29/33] hw/arm/virt: Check CPU type in machine_run_board_init()

Quoted 9 lines · Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do the check by ourselves.

---
  hw/arm/virt.c | 21 +++------------------
  1 file changed, 3 insertions(+), 18 deletions(-)

r~

author
#51 lore raw

PATCH v4 30/33 hw/arm/virt: Hide host CPU model for tcg

In reply to Gavin Shan

The 'host' CPU model isn't available until KVM or HVF is enabled. For example, the following error messages are seen when the guest is started with option '-cpu cortex-a8' on tcg.

  ERROR:../hw/core/machine.c:1423:is_cpu_type_supported: \
  assertion failed: (model != NULL)
  Bail out! ERROR:../hw/core/machine.c:1423:is_cpu_type_supported: \
  assertion failed: (model != NULL)
  Aborted (core dumped)

Hide 'host' CPU model until KVM or HVF is enabled. With this applied, the valid CPU models can be shown.

  qemu-system-aarch64: Invalid CPU type: cortex-a8
  The valid types are: cortex-a7, cortex-a15, cortex-a35, \
  cortex-a55, cortex-a72, cortex-a76, cortex-a710, a64fx, \
  neoverse-n1, neoverse-v1, neoverse-n2, cortex-a53,      \
  cortex-a57, max
---
 hw/arm/virt.c | 2 ++
 1 file changed, 2 insertions(+)
hw/arm/virt.c +2 -0
index 5bacd3a7f0..1ac2e29244 100644
@@ -219,7 +219,9 @@ static const char * const valid_cpu_types[] = {
 #endif
     ARM_CPU_TYPE_NAME("cortex-a53"),
     ARM_CPU_TYPE_NAME("cortex-a57"),
+#if defined(CONFIG_KVM) || defined(CONFIG_HVF)
     ARM_CPU_TYPE_NAME("host"),
+#endif
     ARM_CPU_TYPE_NAME("max"),
     NULL
 };
Signature · 1 line
2.41.0
Reviewed-by on v4 30/33
#52 lore raw

Re: [PATCH v4 30/33] hw/arm/virt: Hide host CPU model for tcg

Quoted 13 lines · Gavin Shan

The 'host' CPU model isn't available until KVM or HVF is enabled. For example, the following error messages are seen when the guest is started with option '-cpu cortex-a8' on tcg.

   ERROR:../hw/core/machine.c:1423:is_cpu_type_supported: \
   assertion failed: (model != NULL)
   Bail out! ERROR:../hw/core/machine.c:1423:is_cpu_type_supported: \
   assertion failed: (model != NULL)
   Aborted (core dumped)

Hide 'host' CPU model until KVM or HVF is enabled. With this applied, the valid CPU models can be shown.

Quoted 7 lines
   qemu-system-aarch64: Invalid CPU type: cortex-a8
   The valid types are: cortex-a7, cortex-a15, cortex-a35, \
   cortex-a55, cortex-a72, cortex-a76, cortex-a710, a64fx, \
   neoverse-n1, neoverse-v1, neoverse-n2, cortex-a53,      \
   cortex-a57, max

This patch has to be sorted before the previous patch, for exactly this assert. With that,

r~

author
#53 lore raw

PATCH v4 31/33 hw/arm/sbsa-ref: Check CPU type in machine_run_board_init()

In reply to Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

---
 hw/arm/sbsa-ref.c | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
hw/arm/sbsa-ref.c +3 -18
index bce44690e5..64c321f627 100644
@@ -144,27 +144,16 @@ static const int sbsa_ref_irqmap[] = {
     [SBSA_GWDT_WS0] = 16,
 };
 
-static const char * const valid_cpus[] = {
+static const char * const valid_cpu_types[] = {
     ARM_CPU_TYPE_NAME("cortex-a57"),
     ARM_CPU_TYPE_NAME("cortex-a72"),
     ARM_CPU_TYPE_NAME("neoverse-n1"),
     ARM_CPU_TYPE_NAME("neoverse-v1"),
     ARM_CPU_TYPE_NAME("neoverse-n2"),
     ARM_CPU_TYPE_NAME("max"),
+    NULL,
 };
 
-static bool cpu_type_valid(const char *cpu)
-{
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
-        if (strcmp(cpu, valid_cpus[i]) == 0) {
-            return true;
-        }
-    }
-    return false;
-}
-
 static uint64_t sbsa_ref_cpu_mp_affinity(SBSAMachineState *sms, int idx)
 {
     uint8_t clustersz = ARM_DEFAULT_CPUS_PER_CLUSTER;
@@ -730,11 +719,6 @@ static void sbsa_ref_init(MachineState *machine)
     const CPUArchIdList *possible_cpus;
     int n, sbsa_max_cpus;
 
-    if (!cpu_type_valid(machine->cpu_type)) {
-        error_report("sbsa-ref: CPU type %s not supported", machine->cpu_type);
-        exit(1);
-    }
-
     if (kvm_enabled()) {
         error_report("sbsa-ref: KVM is not supported for this machine");
         exit(1);
@@ -899,6 +883,7 @@ static void sbsa_ref_class_init(ObjectClass *oc, void *data)
     mc->init = sbsa_ref_init;
     mc->desc = "QEMU 'SBSA Reference' ARM Virtual Machine";
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("neoverse-n1");
+    mc->valid_cpu_types = valid_cpu_types;
     mc->max_cpus = 512;
     mc->pci_allow_0_address = true;
     mc->minimum_page_bits = 12;
Signature · 1 line
2.41.0
Reviewed-by on v4 31/33
#54 lore raw

Re: [PATCH v4 31/33] hw/arm/sbsa-ref: Check CPU type in machine_run_board_init()

Quoted 10 lines · Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

---
  hw/arm/sbsa-ref.c | 21 +++------------------
  1 file changed, 3 insertions(+), 18 deletions(-)

r~

author
#55 lore raw

PATCH v4 32/33 hw/arm: Check CPU type in machine_run_board_init()

In reply to Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

---
 hw/arm/bananapi_m2u.c   | 12 ++++++------
 hw/arm/cubieboard.c     | 12 ++++++------
 hw/arm/mps2-tz.c        | 20 ++++++++++++++------
 hw/arm/mps2.c           | 25 +++++++++++++++++++------
 hw/arm/msf2-som.c       | 12 ++++++------
 hw/arm/musca.c          | 13 ++++++-------
 hw/arm/npcm7xx_boards.c | 13 ++++++-------
 hw/arm/orangepi.c       | 12 ++++++------
 8 files changed, 69 insertions(+), 50 deletions(-)
hw/arm/bananapi_m2u.c +6 -6
index 8f24b18d8c..5eed11fe03 100644
@@ -30,6 +30,11 @@
 
 static struct arm_boot_info bpim2u_binfo;
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-a7"),
+    NULL
+};
+
 /*
  * R40 can boot from mmc0 and mmc2, and bpim2u has two mmc interface, one is
  * connected to sdcard and another mount an emmc media.
@@ -71,12 +76,6 @@ static void bpim2u_init(MachineState *machine)
         exit(1);
     }
 
-    /* Only allow Cortex-A7 for this board */
-    if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a7")) != 0) {
-        error_report("This board can only be used with cortex-a7 CPU");
-        exit(1);
-    }
-
     r40 = AW_R40(object_new(TYPE_AW_R40));
     object_property_add_child(OBJECT(machine), "soc", OBJECT(r40));
     object_unref(OBJECT(r40));
@@ -139,6 +138,7 @@ static void bpim2u_machine_init(MachineClass *mc)
     mc->max_cpus = AW_R40_NUM_CPUS;
     mc->default_cpus = AW_R40_NUM_CPUS;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
+    mc->valid_cpu_types = valid_cpu_types;
     mc->default_ram_size = 1 * GiB;
     mc->default_ram_id = "bpim2u.ram";
 }
hw/arm/cubieboard.c +6 -6
index 29146f5018..7d9bb6c837 100644
@@ -29,6 +29,11 @@ static struct arm_boot_info cubieboard_binfo = {
     .board_id = 0x1008,
 };
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-a8"),
+    NULL
+};
+
 static void cubieboard_init(MachineState *machine)
 {
     AwA10State *a10;
@@ -52,12 +57,6 @@ static void cubieboard_init(MachineState *machine)
         exit(1);
     }
 
-    /* Only allow Cortex-A8 for this board */
-    if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a8")) != 0) {
-        error_report("This board can only be used with cortex-a8 CPU");
-        exit(1);
-    }
-
     a10 = AW_A10(object_new(TYPE_AW_A10));
     object_property_add_child(OBJECT(machine), "soc", OBJECT(a10));
     object_unref(OBJECT(a10));
@@ -116,6 +115,7 @@ static void cubieboard_machine_init(MachineClass *mc)
 {
     mc->desc = "cubietech cubieboard (Cortex-A8)";
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a8");
+    mc->valid_cpu_types = valid_cpu_types;
     mc->default_ram_size = 1 * GiB;
     mc->init = cubieboard_init;
     mc->block_default_type = IF_IDE;
hw/arm/mps2-tz.c +14 -6
index eae3639da2..d7bb6d965f 100644
@@ -190,6 +190,16 @@ OBJECT_DECLARE_TYPE(MPS2TZMachineState, MPS2TZMachineClass, MPS2TZ_MACHINE)
 /* For cpu{0,1}_mpu_{ns,s}, means "leave at SSE's default value" */
 #define MPU_REGION_DEFAULT UINT32_MAX
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m33"),
+    NULL
+};
+
+static const char * const mps3tz_an547_valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m55"),
+    NULL
+};
+
 static const uint32_t an505_oscclk[] = {
     40000000,
     24580000,
@@ -809,12 +819,6 @@ static void mps2tz_common_init(MachineState *machine)
     int num_ppcs;
     int i;
 
-    if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
-        error_report("This board can only be used with CPU %s",
-                     mc->default_cpu_type);
-        exit(1);
-    }
-
     if (machine->ram_size != mc->default_ram_size) {
         char *sz = size_to_str(mc->default_ram_size);
         error_report("Invalid RAM size, should be %s", sz);
@@ -1321,6 +1325,7 @@ static void mps2tz_an505_class_init(ObjectClass *oc, void *data)
     mc->max_cpus = mc->default_cpus;
     mmc->fpga_type = FPGA_AN505;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
+    mc->valid_cpu_types = valid_cpu_types;
     mmc->scc_id = 0x41045050;
     mmc->sysclk_frq = 20 * 1000 * 1000; /* 20MHz */
     mmc->apb_periph_frq = mmc->sysclk_frq;
@@ -1350,6 +1355,7 @@ static void mps2tz_an521_class_init(ObjectClass *oc, void *data)
     mc->max_cpus = mc->default_cpus;
     mmc->fpga_type = FPGA_AN521;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
+    mc->valid_cpu_types = valid_cpu_types;
     mmc->scc_id = 0x41045210;
     mmc->sysclk_frq = 20 * 1000 * 1000; /* 20MHz */
     mmc->apb_periph_frq = mmc->sysclk_frq;
@@ -1379,6 +1385,7 @@ static void mps3tz_an524_class_init(ObjectClass *oc, void *data)
     mc->max_cpus = mc->default_cpus;
     mmc->fpga_type = FPGA_AN524;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
+    mc->valid_cpu_types = valid_cpu_types;
     mmc->scc_id = 0x41045240;
     mmc->sysclk_frq = 32 * 1000 * 1000; /* 32MHz */
     mmc->apb_periph_frq = mmc->sysclk_frq;
@@ -1413,6 +1420,7 @@ static void mps3tz_an547_class_init(ObjectClass *oc, void *data)
     mc->max_cpus = mc->default_cpus;
     mmc->fpga_type = FPGA_AN547;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m55");
+    mc->valid_cpu_types = mps3tz_an547_valid_cpu_types;
     mmc->scc_id = 0x41055470;
     mmc->sysclk_frq = 32 * 1000 * 1000; /* 32MHz */
     mmc->apb_periph_frq = 25 * 1000 * 1000; /* 25MHz */
hw/arm/mps2.c +19 -6
index d92fd60684..691b0803cf 100644
@@ -110,6 +110,21 @@ OBJECT_DECLARE_TYPE(MPS2MachineState, MPS2MachineClass, MPS2_MACHINE)
  */
 #define REFCLK_FRQ (1 * 1000 * 1000)
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m3"),
+    NULL
+};
+
+static const char * const mps2_an386_valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m4"),
+    NULL
+};
+
+static const char * const mps2_an500_valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m7"),
+    NULL
+};
+
 /* Initialize the auxiliary RAM region @mr and map it into
  * the memory map at @base.
  */
@@ -140,12 +155,6 @@ static void mps2_common_init(MachineState *machine)
     DeviceState *armv7m, *sccdev;
     int i;
 
-    if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
-        error_report("This board can only be used with CPU %s",
-                     mc->default_cpu_type);
-        exit(1);
-    }
-
     if (machine->ram_size != mc->default_ram_size) {
         char *sz = size_to_str(mc->default_ram_size);
         error_report("Invalid RAM size, should be %s", sz);
@@ -484,6 +493,7 @@ static void mps2_an385_class_init(ObjectClass *oc, void *data)
     mc->desc = "ARM MPS2 with AN385 FPGA image for Cortex-M3";
     mmc->fpga_type = FPGA_AN385;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
+    mc->valid_cpu_types = valid_cpu_types;
     mmc->scc_id = 0x41043850;
     mmc->psram_base = 0x21000000;
     mmc->ethernet_base = 0x40200000;
@@ -498,6 +508,7 @@ static void mps2_an386_class_init(ObjectClass *oc, void *data)
     mc->desc = "ARM MPS2 with AN386 FPGA image for Cortex-M4";
     mmc->fpga_type = FPGA_AN386;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m4");
+    mc->valid_cpu_types = mps2_an386_valid_cpu_types;
     mmc->scc_id = 0x41043860;
     mmc->psram_base = 0x21000000;
     mmc->ethernet_base = 0x40200000;
@@ -512,6 +523,7 @@ static void mps2_an500_class_init(ObjectClass *oc, void *data)
     mc->desc = "ARM MPS2 with AN500 FPGA image for Cortex-M7";
     mmc->fpga_type = FPGA_AN500;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m7");
+    mc->valid_cpu_types = mps2_an500_valid_cpu_types;
     mmc->scc_id = 0x41045000;
     mmc->psram_base = 0x60000000;
     mmc->ethernet_base = 0xa0000000;
@@ -526,6 +538,7 @@ static void mps2_an511_class_init(ObjectClass *oc, void *data)
     mc->desc = "ARM MPS2 with AN511 DesignStart FPGA image for Cortex-M3";
     mmc->fpga_type = FPGA_AN511;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
+    mc->valid_cpu_types = valid_cpu_types;
     mmc->scc_id = 0x41045110;
     mmc->psram_base = 0x21000000;
     mmc->ethernet_base = 0x40200000;
hw/arm/msf2-som.c +6 -6
index 7b3106c790..470951a006 100644
@@ -42,6 +42,11 @@
 #define M2S010_ENVM_SIZE      (256 * KiB)
 #define M2S010_ESRAM_SIZE     (64 * KiB)
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m3"),
+    NULL
+};
+
 static void emcraft_sf2_s2s010_init(MachineState *machine)
 {
     DeviceState *dev;
@@ -55,12 +60,6 @@ static void emcraft_sf2_s2s010_init(MachineState *machine)
     MemoryRegion *ddr = g_new(MemoryRegion, 1);
     Clock *m3clk;
 
-    if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
-        error_report("This board can only be used with CPU %s",
-                     mc->default_cpu_type);
-        exit(1);
-    }
-
     memory_region_init_ram(ddr, NULL, "ddr-ram", DDR_SIZE,
                            &error_fatal);
     memory_region_add_subregion(sysmem, DDR_BASE_ADDRESS, ddr);
@@ -109,6 +108,7 @@ static void emcraft_sf2_machine_init(MachineClass *mc)
     mc->desc = "SmartFusion2 SOM kit from Emcraft (M2S010)";
     mc->init = emcraft_sf2_s2s010_init;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m3");
+    mc->valid_cpu_types = valid_cpu_types;
 }
 
 DEFINE_MACHINE("emcraft-sf2", emcraft_sf2_machine_init)
hw/arm/musca.c +6 -7
index 6eeee57c9d..9bd78fd531 100644
@@ -102,6 +102,11 @@ OBJECT_DECLARE_TYPE(MuscaMachineState, MuscaMachineClass, MUSCA_MACHINE)
 /* Slow 32Khz S32KCLK frequency in Hz */
 #define S32KCLK_FRQ (32 * 1000)
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-m33"),
+    NULL
+};
+
 static qemu_irq get_sse_irq_in(MuscaMachineState *mms, int irqno)
 {
     /* Return a qemu_irq which will signal IRQ n to all CPUs in the SSE. */
@@ -355,7 +360,6 @@ static void musca_init(MachineState *machine)
 {
     MuscaMachineState *mms = MUSCA_MACHINE(machine);
     MuscaMachineClass *mmc = MUSCA_MACHINE_GET_CLASS(mms);
-    MachineClass *mc = MACHINE_GET_CLASS(machine);
     MemoryRegion *system_memory = get_system_memory();
     DeviceState *ssedev;
     DeviceState *dev_splitter;
@@ -366,12 +370,6 @@ static void musca_init(MachineState *machine)
     assert(mmc->num_irqs <= MUSCA_NUMIRQ_MAX);
     assert(mmc->num_mpcs <= MUSCA_MPC_MAX);
 
-    if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
-        error_report("This board can only be used with CPU %s",
-                     mc->default_cpu_type);
-        exit(1);
-    }
-
     mms->sysclk = clock_new(OBJECT(machine), "SYSCLK");
     clock_set_hz(mms->sysclk, SYSCLK_FRQ);
     mms->s32kclk = clock_new(OBJECT(machine), "S32KCLK");
@@ -609,6 +607,7 @@ static void musca_class_init(ObjectClass *oc, void *data)
     mc->min_cpus = mc->default_cpus;
     mc->max_cpus = mc->default_cpus;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
+    mc->valid_cpu_types = valid_cpu_types;
     mc->init = musca_init;
 }
 
hw/arm/npcm7xx_boards.c +6 -7
index 2aef579aac..ca37c893b4 100644
@@ -55,6 +55,11 @@
 
 static const char npcm7xx_default_bootrom[] = "npcm7xx_bootrom.bin";
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-a9"),
+    NULL
+};
+
 static void npcm7xx_load_bootrom(MachineState *machine, NPCM7xxState *soc)
 {
     const char *bios_name = machine->firmware ?: npcm7xx_default_bootrom;
@@ -121,15 +126,8 @@ static NPCM7xxState *npcm7xx_create_soc(MachineState *machine,
                                         uint32_t hw_straps)
 {
     NPCM7xxMachineClass *nmc = NPCM7XX_MACHINE_GET_CLASS(machine);
-    MachineClass *mc = MACHINE_CLASS(nmc);
     Object *obj;
 
-    if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
-        error_report("This board can only be used with %s",
-                     mc->default_cpu_type);
-        exit(1);
-    }
-
     obj = object_new_with_props(nmc->soc_type, OBJECT(machine), "soc",
                                 &error_abort, NULL);
     object_property_set_uint(obj, "power-on-straps", hw_straps, &error_abort);
@@ -469,6 +467,7 @@ static void npcm7xx_machine_class_init(ObjectClass *oc, void *data)
     mc->no_parallel = 1;
     mc->default_ram_id = "ram";
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a9");
+    mc->valid_cpu_types = valid_cpu_types;
 }
 
 /*
hw/arm/orangepi.c +6 -6
index f3784d45ca..4e8a43978d 100644
@@ -29,6 +29,11 @@
 
 static struct arm_boot_info orangepi_binfo;
 
+static const char * const valid_cpu_types[] = {
+    ARM_CPU_TYPE_NAME("cortex-a7"),
+    NULL
+};
+
 static void orangepi_init(MachineState *machine)
 {
     AwH3State *h3;
@@ -49,12 +54,6 @@ static void orangepi_init(MachineState *machine)
         exit(1);
     }
 
-    /* Only allow Cortex-A7 for this board */
-    if (strcmp(machine->cpu_type, ARM_CPU_TYPE_NAME("cortex-a7")) != 0) {
-        error_report("This board can only be used with cortex-a7 CPU");
-        exit(1);
-    }
-
     h3 = AW_H3(object_new(TYPE_AW_H3));
     object_property_add_child(OBJECT(machine), "soc", OBJECT(h3));
     object_unref(OBJECT(h3));
@@ -119,6 +118,7 @@ static void orangepi_machine_init(MachineClass *mc)
     mc->max_cpus = AW_H3_NUM_CPUS;
     mc->default_cpus = AW_H3_NUM_CPUS;
     mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a7");
+    mc->valid_cpu_types = valid_cpu_types;
     mc->default_ram_size = 1 * GiB;
     mc->default_ram_id = "orangepi.ram";
 }
Signature · 1 line
2.41.0
Reviewed-by on v4 32/33
#56 lore raw

Re: [PATCH v4 32/33] hw/arm: Check CPU type in machine_run_board_init()

Quoted 5 lines · Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

Quoted 10 lines
---
  hw/arm/bananapi_m2u.c   | 12 ++++++------
  hw/arm/cubieboard.c     | 12 ++++++------
  hw/arm/mps2-tz.c        | 20 ++++++++++++++------
  hw/arm/mps2.c           | 25 +++++++++++++++++++------
  hw/arm/msf2-som.c       | 12 ++++++------
  hw/arm/musca.c          | 13 ++++++-------
  hw/arm/npcm7xx_boards.c | 13 ++++++-------
  hw/arm/orangepi.c       | 12 ++++++------
  8 files changed, 69 insertions(+), 50 deletions(-)

r~

author
#57 lore raw

PATCH v4 33/33 hw/riscv/shakti_c: Check CPU type in machine_run_board_init()

In reply to Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

---
 hw/riscv/shakti_c.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
hw/riscv/shakti_c.c +5 -6
index 12ea74b032..fc83ed4db4 100644
@@ -28,6 +28,10 @@
 #include "exec/address-spaces.h"
 #include "hw/riscv/boot.h"
 
+static const char * const valid_cpu_types[] = {
+    RISCV_CPU_TYPE_NAME("shakti-c"),
+    NULL
+};
 
 static const struct MemmapEntry {
     hwaddr base;
@@ -47,12 +51,6 @@ static void shakti_c_machine_state_init(MachineState *mstate)
     ShaktiCMachineState *sms = RISCV_SHAKTI_MACHINE(mstate);
     MemoryRegion *system_memory = get_system_memory();
 
-    /* Allow only Shakti C CPU for this platform */
-    if (strcmp(mstate->cpu_type, TYPE_RISCV_CPU_SHAKTI_C) != 0) {
-        error_report("This board can only be used with Shakti C CPU");
-        exit(1);
-    }
-
     /* Initialize SoC */
     object_initialize_child(OBJECT(mstate), "soc", &sms->soc,
                             TYPE_RISCV_SHAKTI_SOC);
@@ -85,6 +83,7 @@ static void shakti_c_machine_class_init(ObjectClass *klass, void *data)
     mc->desc = "RISC-V Board compatible with Shakti SDK";
     mc->init = shakti_c_machine_state_init;
     mc->default_cpu_type = TYPE_RISCV_CPU_SHAKTI_C;
+    mc->valid_cpu_types = valid_cpu_types;
     mc->default_ram_id = "riscv.shakti.c.ram";
 }
 
Signature · 1 line
2.41.0
#58 lore raw

Re: [PATCH v4 33/33] hw/riscv/shakti_c: Check CPU type in machine_run_board_init()

Quoted 15 lines · Gavin Shan

Set mc->valid_cpu_types so that the user specified CPU type can be validated in machine_run_board_init(). We needn't to do it by ourselves.

---
  hw/riscv/shakti_c.c | 11 +++++------
  1 file changed, 5 insertions(+), 6 deletions(-)
hw/riscv/shakti_c.c +0 -0
index 12ea74b032..fc83ed4db4 100644

hw/riscv/shakti_c.c line 34

Quoted 7 lines
hw/riscv/shakti_c.c +4 -0
@@ -28,6 +28,10 @@
  #include "exec/address-spaces.h"
  #include "hw/riscv/boot.h"
 
+static const char * const valid_cpu_types[] = {
+    RISCV_CPU_TYPE_NAME("shakti-c"),
+    NULL
+};

Why are you using a different form here...

hw/riscv/shakti_c.c static void shakti_c_machine_class_init(ObjectClass *klass, void *data) · line 85

Quoted 4 lines
hw/riscv/shakti_c.c +0 -0
@@ -85,6 +83,7 @@ static void shakti_c_machine_class_init(ObjectClass *klass, void *data)
      mc->desc = "RISC-V Board compatible with Shakti SDK";
      mc->init = shakti_c_machine_state_init;
      mc->default_cpu_type = TYPE_RISCV_CPU_SHAKTI_C;

... than here?

r~

Quoted 4 lines
hw/riscv/shakti_c.c +1 -0
+    mc->valid_cpu_types = valid_cpu_types;
      mc->default_ram_id = "riscv.shakti.c.ram";
  }