Skip to contentSkip to conversation

Add a valid_cpu_types property

RFC Superseded Alistair Francis 4 participants 8 messages Sep 6, 2017 – Sep 21, 2017

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#5 lore raw

RFC 2/2 netduino2: Specify the valid CPUs

In reply to Alistair Francis

---

 hw/arm/netduino2.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/arm/netduino2.c +5 -0
index 3cfe332dd1..accca344fd 100644
@@ -43,8 +43,13 @@ static void netduino2_init(MachineState *machine)
 
 static void netduino2_machine_init(MachineClass *mc)
 {
+    const char *val = "cortex-m3";
+
     mc->desc = "Netduino 2 Machine";
     mc->init = netduino2_init;
+
+    mc->valid_cpu_types = g_array_new(false, false, sizeof(char *));
+    g_array_append_val(mc->valid_cpu_types, val);
 }
 
 DEFINE_MACHINE("netduino2", netduino2_machine_init)
Signature · 1 line
2.11.0
#0 lore raw

cover Add a valid_cpu_types property

There are numorous QEMU machines that only have a single or a handful of valid CPU options. To simplyfy the management of specificying which CPU is/isn't valid let's create a property that can be set in the machine init. We can then check to see if the user supplied CPU is in that list or not.

This is just a quick setup, if this method is agreed apon I can add a nice macro to add the valid CPU options (similar to SET_MACHINE_COMPAT) and improve the error messages. I just wanted to get some input before I spent too much time on that.

Alistair Francis (2):
machine: Add a valid_cpu_types property
netduino2: Specify the valid CPUs

 hw/arm/netduino2.c  |  5 +++++
 hw/core/machine.c   | 27 +++++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 3 files changed, 33 insertions(+)
Signature · 1 line
2.11.0
#1 lore raw

RFC 1/2 machine: Add a valid_cpu_types property

---

 hw/core/machine.c   | 27 +++++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 2 files changed, 28 insertions(+)
hw/core/machine.c +27 -0
index 41b53a17ad..de0f127d27 100644
@@ -758,6 +758,33 @@ void machine_run_board_init(MachineState *machine)
         machine_numa_finish_init(machine);
     }
     machine_class->init(machine);
+
+    if (machine_class->valid_cpu_types && machine->cpu_model) {
+        const char *temp;
+        int i, len = machine_class->valid_cpu_types->len;
+
+        for (i = 0; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            if (!strcmp(machine->cpu_model, temp)) {
+                /* The user specificed CPU is in the valid field, we are
+                 * good to go.
+                 */
+                g_array_free(machine_class->valid_cpu_types, true);
+                return;
+            }
+        }
+        /* The user specified CPU must not be a valid CPU, print a sane error */
+        temp = g_array_index(machine_class->valid_cpu_types, char *, 0);
+        error_report("Invalid CPU: %s", machine->cpu_model);
+        error_printf("The valid options are: %s", temp);
+        for (i = 1; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            error_printf(", %s", temp);
+        }
+        error_printf("\n");
+        g_array_free(machine_class->valid_cpu_types, true);
+        exit(1);
+    }
 }
 
 static void machine_class_finalize(ObjectClass *klass, void *data)
include/hw/boards.h +1 -0
index 3363dd19fd..78678f84a9 100644
@@ -172,6 +172,7 @@ struct MachineClass {
     int minimum_page_bits;
     bool has_hotpluggable_cpus;
     int numa_mem_align_shift;
+    GArray *valid_cpu_types;
     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
                                  int nb_nodes, ram_addr_t size);
 
Signature · 1 line
2.11.0
#2 lore raw

Re: [Qemu-devel] [RFC v1 1/2] machine: Add a valid_cpu_types property

Quoted 12 lines · Alistair Francis

---

 hw/core/machine.c   | 27 +++++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 2 files changed, 28 insertions(+)
hw/core/machine.c +0 -0
index 41b53a17ad..de0f127d27 100644

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 764

Quoted 7 lines
hw/core/machine.c +4 -0
@@ -758,6 +758,33 @@ void machine_run_board_init(MachineState *machine)
         machine_numa_finish_init(machine);
     }
     machine_class->init(machine);
+
+    if (machine_class->valid_cpu_types && machine->cpu_model) {
+        const char *temp;
+        int i, len = machine_class->valid_cpu_types->len;

I suggest doing this after Igor's series that replaces the cpu_model field (full -cpu string) with cpu_type (only the CPU type name).

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 773

Quoted 9 lines
hw/core/machine.c +9 -0
+
+        for (i = 0; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            if (!strcmp(machine->cpu_model, temp)) {
+                /* The user specificed CPU is in the valid field, we are
+                 * good to go.
+                 */
+                g_array_free(machine_class->valid_cpu_types, true);
+                return;

I suggest checking for:
object_class_dynamic_cast(object_class_get_name(machine->cpu_type), type)
instead. This way machines could just enumerate a common parent type to all supported CPU models.

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 784

Quoted 11 lines
hw/core/machine.c +11 -0
+            }
+        }
+        /* The user specified CPU must not be a valid CPU, print a sane error */
+        temp = g_array_index(machine_class->valid_cpu_types, char *, 0);
+        error_report("Invalid CPU: %s", machine->cpu_model);
+        error_printf("The valid options are: %s", temp);
+        for (i = 1; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            error_printf(", %s", temp);
+        }
+        error_printf("\n");

Now we have a completely new method to list the valid CPU types in addition to arch_query_cpu_definitions() and list_cpus() (which are already a bit messy and need to share more code).

I think this should share code with "-cpu help"/query-cpu-definitions instead. This means arch_query_cpu_definitions() will need a MachineClass* argument, if the user wants only the CPU types supported by a specific machine type, but I think it would be an interesting improvement to query-cpu-definitions.

Quoted 11 lines
hw/core/machine.c +3 -0
+        g_array_free(machine_class->valid_cpu_types, true);
+        exit(1);
+    }
 }
 
 static void machine_class_finalize(ObjectClass *klass, void *data)
include/hw/boards.h +0 -0
index 3363dd19fd..78678f84a9 100644

include/hw/boards.h struct MachineClass { · line 175

Quoted 4 lines
include/hw/boards.h +1 -0
@@ -172,6 +172,7 @@ struct MachineClass {
     int minimum_page_bits;
     bool has_hotpluggable_cpus;
     int numa_mem_align_shift;
+    GArray *valid_cpu_types;

The list of CPU types for a machine are very likely to be statically defined at build time. Any specific reason to not make it a simple char** pointer?

Quoted 6 lines
include/hw/boards.h +0 -0
     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
                                  int nb_nodes, ram_addr_t size);
 
Signature · 1 line
2.11.0
Signature · 1 line
Eduardo
#3 lore raw

Re: [Qemu-devel] [RFC v1 1/2] machine: Add a valid_cpu_types property

Quoted 8 lines · On Sat, 9 Sep 2017 17:16:57 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 7 lines · Alistair Francis

---

 hw/core/machine.c   | 27 +++++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 2 files changed, 28 insertions(+)
Quoted 16 lines

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 764

Quoted 12 lines
hw/core/machine.c +4 -0
index 41b53a17ad..de0f127d27 100644
@@ -758,6 +758,33 @@ void machine_run_board_init(MachineState *machine)
         machine_numa_finish_init(machine);
     }
     machine_class->init(machine);
+
+    if (machine_class->valid_cpu_types && machine->cpu_model) {
+        const char *temp;
+        int i, len = machine_class->valid_cpu_types->len;  

I suggest doing this after Igor's series that replaces the cpu_model field (full -cpu string) with cpu_type (only the CPU type name).

in addition check should run before
machine_class->init(machine);
it should be possible after cpu_model -> cpu_type refactoring is complete

[...]

Quoted 5 lines
Quoted 5 lines
include/hw/boards.h +0 -0
index 3363dd19fd..78678f84a9 100644
@@ -172,6 +172,7 @@ struct MachineClass {
Quoted 8 lines

include/hw/boards.h struct MachineClass { · line 175

Quoted 4 lines
include/hw/boards.h +1 -0
     int minimum_page_bits;
     bool has_hotpluggable_cpus;
     int numa_mem_align_shift;
+    GArray *valid_cpu_types;  

The list of CPU types for a machine are very likely to be statically defined at build time. Any specific reason to not make it a simple char** pointer?

wile working on cpu_model, I've seen only static checks used in some machines. So +1 to static null terminated array [
TYPE_FOO1,
TYPE_FOO2,
NULL
]

Quoted 8 lines
Quoted 7 lines
include/hw/boards.h +0 -0
     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
                                  int nb_nodes, ram_addr_t size);
 
Signature · 1 line
2.11.0
#6 lore raw

Re: [Qemu-devel] [RFC v1 2/2] netduino2: Specify the valid CPUs

In reply to Alistair Francis
Quoted 12 lines · Alistair Francis

---

  hw/arm/netduino2.c | 5 +++++
  1 file changed, 5 insertions(+)
hw/arm/netduino2.c +0 -0
index 3cfe332dd1..accca344fd 100644
@@ -43,8 +43,13 @@ static void netduino2_init(MachineState *machine)
 

hw/arm/netduino2.c static void netduino2_init(MachineState *machine) · line 53

Quoted 10 lines
hw/arm/netduino2.c +5 -0
  static void netduino2_machine_init(MachineClass *mc)
  {
+    const char *val = "cortex-m3";
+
      mc->desc = "Netduino 2 Machine";
      mc->init = netduino2_init;
+
+    mc->valid_cpu_types = g_array_new(false, false, sizeof(char *));
+    g_array_append_val(mc->valid_cpu_types, val);
  }

Hi Alistair,

Nice idea! Why not adding an helper function which take a const char** in argument?

Fred

Quoted 3 lines
hw/arm/netduino2.c +0 -0
  DEFINE_MACHINE("netduino2", netduino2_machine_init)
 
#4 lore raw

Re: [Qemu-devel] [RFC v1 1/2] machine: Add a valid_cpu_types property

In reply to Eduardo Habkost
Quoted 56 lines · Eduardo Habkost
Quoted 12 lines · Alistair Francis

---

 hw/core/machine.c   | 27 +++++++++++++++++++++++++++
 include/hw/boards.h |  1 +
 2 files changed, 28 insertions(+)
hw/core/machine.c +0 -0
index 41b53a17ad..de0f127d27 100644

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 764

Quoted 7 lines
hw/core/machine.c +4 -0
@@ -758,6 +758,33 @@ void machine_run_board_init(MachineState *machine)
         machine_numa_finish_init(machine);
     }
     machine_class->init(machine);
+
+    if (machine_class->valid_cpu_types && machine->cpu_model) {
+        const char *temp;
+        int i, len = machine_class->valid_cpu_types->len;

I suggest doing this after Igor's series that replaces the cpu_model field (full -cpu string) with cpu_type (only the CPU type name).

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 773

Quoted 9 lines
hw/core/machine.c +9 -0
+
+        for (i = 0; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            if (!strcmp(machine->cpu_model, temp)) {
+                /* The user specificed CPU is in the valid field, we are
+                 * good to go.
+                 */
+                g_array_free(machine_class->valid_cpu_types, true);
+                return;

I suggest checking for:
object_class_dynamic_cast(object_class_get_name(machine->cpu_type), type)
instead. This way machines could just enumerate a common parent type to all supported CPU models.

hw/core/machine.c void machine_run_board_init(MachineState *machine) · line 784

Quoted 11 lines
hw/core/machine.c +11 -0
+            }
+        }
+        /* The user specified CPU must not be a valid CPU, print a sane error */
+        temp = g_array_index(machine_class->valid_cpu_types, char *, 0);
+        error_report("Invalid CPU: %s", machine->cpu_model);
+        error_printf("The valid options are: %s", temp);
+        for (i = 1; i < len; i++) {
+            temp = g_array_index(machine_class->valid_cpu_types, char *, i);
+            error_printf(", %s", temp);
+        }
+        error_printf("\n");

Now we have a completely new method to list the valid CPU types in addition to arch_query_cpu_definitions() and list_cpus() (which are already a bit messy and need to share more code).

Quoted 6 lines

I think this should share code with "-cpu help"/query-cpu-definitions instead. This means arch_query_cpu_definitions() will need a MachineClass* argument, if the user wants only the CPU types supported by a specific machine type, but I think it would be an interesting improvement to query-cpu-definitions.

I don't see the improvement here. arch_query_cpu_definitions() is just a pretty simple list of that arch's CPUs. I don't really see how it helps in this case. We don't re-use much code at all.

Ideally I would like to see less arch dependent code in QEMU, and relying on arch_query_cpu_definitions() is moving in the opposite direction. It looks like arch_query_cpu_definitions() isn't supported by every architecture as well. I only see ARM, x86, PPC and S390x support.

I have addressed all the other comments so I'm going to send an RFCv2 out later today. The code looks a lot nicer now. I'm happy to keep discussing this though, just want to keep the ball rolling.

Thanks,
Alistair

Quoted 30 lines
Quoted 12 lines
hw/core/machine.c +3 -0
+        g_array_free(machine_class->valid_cpu_types, true);
+        exit(1);
+    }
 }
 
 static void machine_class_finalize(ObjectClass *klass, void *data)
include/hw/boards.h +0 -0
index 3363dd19fd..78678f84a9 100644

include/hw/boards.h struct MachineClass { · line 175

Quoted 4 lines
include/hw/boards.h +1 -0
@@ -172,6 +172,7 @@ struct MachineClass {
     int minimum_page_bits;
     bool has_hotpluggable_cpus;
     int numa_mem_align_shift;
+    GArray *valid_cpu_types;

The list of CPU types for a machine are very likely to be statically defined at build time. Any specific reason to not make it a simple char** pointer?

Quoted 6 lines
include/hw/boards.h +0 -1
     void (*numa_auto_assign_ram)(MachineClass *mc, NodeInfo *nodes,
                                  int nb_nodes, ram_addr_t size);
 
--

2.11.0

--
Eduardo

#7 lore raw

Re: [Qemu-devel] [RFC v1 2/2] netduino2: Specify the valid CPUs

In reply to KONRAD Frederic
Quoted 29 lines · KONRAD Frederic
Quoted 12 lines · Alistair Francis

---

  hw/arm/netduino2.c | 5 +++++
  1 file changed, 5 insertions(+)
hw/arm/netduino2.c +0 -0
index 3cfe332dd1..accca344fd 100644

hw/arm/netduino2.c static void netduino2_init(MachineState *machine) · line 52

Quoted 10 lines
hw/arm/netduino2.c +5 -0
@@ -43,8 +43,13 @@ static void netduino2_init(MachineState *machine)
    static void netduino2_machine_init(MachineClass *mc)
  {
+    const char *val = "cortex-m3";
+
      mc->desc = "Netduino 2 Machine";
      mc->init = netduino2_init;
+
+    mc->valid_cpu_types = g_array_new(false, false, sizeof(char *));
+    g_array_append_val(mc->valid_cpu_types, val);
  }

Hi Alistair,

Quoted 2 lines

Nice idea! Why not adding an helper function which take a const char** in argument?

Thanks Fred.

I'm going to send a v2 with just that :)

Thanks,
Alistair

Quoted 6 lines

Fred

Quoted 2 lines
hw/arm/netduino2.c +0 -0
    DEFINE_MACHINE("netduino2", netduino2_machine_init)