Skip to contentSkip to conversation

hw/i386/cpu: remove default_cpu_version and simplify

PATCH Superseded Ani Sinha 2 participants 3 messages Jan 22, 2025 – Jan 23, 2025

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH v4 1/1 hw/i386/cpu: remove default_cpu_version and simplify

commit 0788a56bd1ae3 ("i386: Make unversioned CPU models be aliases") introduced 'default_cpu_version' for PCMachineClass. This created three categories of CPU models:
- Most unversioned CPU models would use version 1 by default.
- For machines 4.0.1 and older that do not support cpu model aliases, a special default_cpu_version value of CPU_VERSION_LEGACY is used.
- It was thought that future machines would use the latest value of cpu versions corresponding to default_cpu_version value of CPU_VERSION_LATEST [1].

All pc machines still use the default cpu version of 1 for unversioned cpu models. CPU_VERSION_LATEST is a moving target and changes with time. Therefore, if machines use CPU_VERSION_LATEST, it would mean that over a period of time, for the same machine type, the cpu version would be different depending on what is latest at that time. This would break guests even when they use a constant machine type. Therefore, for pc machines, use of CPU_VERSION_LATEST is not possible. Currently, only microvms use CPU_VERSION_LATEST.

This change cleans up the complicated logic around default_cpu_version including getting rid of default_cpu_version property itself. A couple of new flags are introduced, one for the legacy model for machines 4.0.1 and older and other for microvms. For older machines, a new pc machine property is introduced that separates pc machine versions 4.0.1 and older from the newer machines. 4.0.1 and older machines are scheduled to be deleted towards end of 2025 since they would be 6 years old by then. At that time, we can remove all logic around legacy cpus. Microvms are the only machines that continue to use the latest cpu version. If this changes later, we can remove all logic around x86_cpu_model_last_version(). Default cpu version for unversioned cpu models is hardcoded to the value 1 and applies unconditionally for all pc machine types of version 4.1 and above.

This change also removes all complications around CPU_VERSION_AUTO including removal of the value itself.

1) See commit dcafd1ef0af227 ("i386: Register versioned CPU models")

---
 hw/i386/microvm.c     |  3 +-
 hw/i386/pc_piix.c     |  6 ++--
 hw/i386/pc_q35.c      |  6 ++--
 hw/i386/x86-common.c  |  4 +--
 include/hw/i386/pc.h  | 19 ++++++++++--
 include/hw/i386/x86.h |  5 +++-
 target/i386/cpu.c     | 69 ++++++++++++++++++++++---------------------
 target/i386/cpu.h     | 23 ++++-----------
 8 files changed, 70 insertions(+), 65 deletions(-)

changelog:
v2: explain in commit log why use of CPU_VERSION_LATEST for machines is problematic.
v3: fix a bug that broke the pipeline
https://gitlab.com/mstredhat/qemu/-/pipelines/1626171267
when cpu versions are explicitly specified in the command line, respect that and do not enforce legacy (unversioned) cpu logic. The pipeline is green now with the fix:
https://gitlab.com/anisinha/qemu/-/pipelines/1626783632
v4: made changes as per Zhao's suggestions.
Pipeline passes https://gitlab.com/anisinha/qemu/-/pipelines/1635829877

hw/i386/microvm.c +2 -1
index a8d354aabe..ffb1b37fe5 100644
@@ -458,7 +458,8 @@ static void microvm_machine_state_init(MachineState *machine)
 
     microvm_memory_init(mms);
 
-    x86_cpus_init(x86ms, CPU_VERSION_LATEST);
+    x86_cpu_uses_lastest_version();
+    x86_cpus_init(x86ms);
 
     microvm_devices_init(mms);
 }
hw/i386/pc_piix.c +3 -3
index 04d2957adc..dc684cb011 100644
@@ -181,7 +181,8 @@ static void pc_init1(MachineState *machine, const char *pci_type)
     }
 
     pc_machine_init_sgx_epc(pcms);
-    x86_cpus_init(x86ms, pcmc->default_cpu_version);
+
+    pc_init_cpus(machine);
 
     if (kvm_enabled()) {
         kvmclock_create(pcmc->kvmclock_create_always);
@@ -457,7 +458,6 @@ static void pc_i440fx_machine_options(MachineClass *m)
     ObjectClass *oc = OBJECT_CLASS(m);
     pcmc->default_south_bridge = TYPE_PIIX3_DEVICE;
     pcmc->pci_root_uid = 0;
-    pcmc->default_cpu_version = 1;
 
     m->family = "pc_piix";
     m->desc = "Standard PC (i440FX + PIIX, 1996)";
@@ -669,7 +669,7 @@ static void pc_i440fx_machine_4_0_options(MachineClass *m)
 {
     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_i440fx_machine_4_1_options(m);
-    pcmc->default_cpu_version = CPU_VERSION_LEGACY;
+    pcmc->no_versioned_cpu_model = true;
     compat_props_add(m->compat_props, hw_compat_4_0, hw_compat_4_0_len);
     compat_props_add(m->compat_props, pc_compat_4_0, pc_compat_4_0_len);
 }
hw/i386/pc_q35.c +3 -3
index 77536dd697..045b05da64 100644
@@ -187,7 +187,8 @@ static void pc_q35_init(MachineState *machine)
     }
 
     pc_machine_init_sgx_epc(pcms);
-    x86_cpus_init(x86ms, pcmc->default_cpu_version);
+
+    pc_init_cpus(machine);
 
     if (kvm_enabled()) {
         kvmclock_create(pcmc->kvmclock_create_always);
@@ -339,7 +340,6 @@ static void pc_q35_machine_options(MachineClass *m)
 {
     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pcmc->pci_root_uid = 0;
-    pcmc->default_cpu_version = 1;
 
     m->family = "pc_q35";
     m->desc = "Standard PC (Q35 + ICH9, 2009)";
@@ -547,7 +547,7 @@ static void pc_q35_machine_4_0_1_options(MachineClass *m)
 {
     PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_q35_machine_4_1_options(m);
-    pcmc->default_cpu_version = CPU_VERSION_LEGACY;
+    pcmc->no_versioned_cpu_model = true;
     /*
      * This is the default machine for the 4.0-stable branch. It is basically
      * a 4.0 that doesn't use split irqchip by default. It MUST hence apply the
hw/i386/x86-common.c +1 -3
index 008496b5b8..1ed5bc6010 100644
@@ -66,15 +66,13 @@ out:
     object_unref(cpu);
 }
 
-void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
+void x86_cpus_init(X86MachineState *x86ms)
 {
     int i;
     const CPUArchIdList *possible_cpus;
     MachineState *ms = MACHINE(x86ms);
     MachineClass *mc = MACHINE_GET_CLASS(x86ms);
 
-    x86_cpu_set_default_version(default_cpu_version);
-
     /*
      * Calculates the limit to CPU APIC ID values
      *
include/hw/i386/pc.h +16 -3
index a558705cb9..34106bb52d 100644
@@ -92,9 +92,6 @@ struct PCMachineClass {
 
     /* Compat options: */
 
-    /* Default CPU model version.  See x86_cpu_set_default_version(). */
-    int default_cpu_version;
-
     /* ACPI compat: */
     bool has_acpi_build;
     int pci_root_uid;
@@ -125,11 +122,27 @@ struct PCMachineClass {
      * check for memory.
      */
     bool broken_32bit_mem_addr_check;
+
+    /* whether the machine supports versioned cpu models */
+    bool no_versioned_cpu_model;
 };
 
 #define TYPE_PC_MACHINE "generic-pc-machine"
 OBJECT_DECLARE_TYPE(PCMachineState, PCMachineClass, PC_MACHINE)
 
+static inline void pc_init_cpus(MachineState *ms)
+{
+    X86MachineState *x86ms = X86_MACHINE(ms);
+    PCMachineState *pcms = PC_MACHINE(ms);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+    if (pcmc->no_versioned_cpu_model) {
+        /* use legacy cpu as it does not support versions */
+        x86_cpu_set_legacy_version();
+    }
+    x86_cpus_init(x86ms);
+}
+
 /* ioapic.c */
 
 GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
include/hw/i386/x86.h +4 -1
index d43cb3908e..41236d2a80 100644
@@ -114,7 +114,10 @@ void init_topo_info(X86CPUTopoInfo *topo_info, const X86MachineState *x86ms);
 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
                                     unsigned int cpu_index);
 
-void x86_cpus_init(X86MachineState *pcms, int default_cpu_version);
+void x86_cpus_init(X86MachineState *pcms);
+#ifndef I386_CPU_H
+void x86_cpu_set_legacy_version(void);
+#endif
 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count);
 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
                       DeviceState *dev, Error **errp);
target/i386/cpu.c +35 -34

130 lines not on this page · raw message

target/i386/cpu.h +6 -17
index b26e25ba15..9c0ce2adaf 100644
@@ -2679,28 +2679,17 @@ void cpu_report_tpr_access(CPUX86State *env, TPRAccess access);
 void apic_handle_tpr_access_report(DeviceState *d, target_ulong ip,
                                    TPRAccess access);
 
-/* Special values for X86CPUVersion: */
-
-/* Resolve to latest CPU version */
-#define CPU_VERSION_LATEST -1
-
-/*
- * Resolve to version defined by current machine type.
- * See x86_cpu_set_default_version()
- */
-#define CPU_VERSION_AUTO   -2
-
-/* Don't resolve to any versioned CPU models, like old QEMU versions */
-#define CPU_VERSION_LEGACY  0
-
 typedef int X86CPUVersion;
 
 /*
- * Set default CPU model version for CPU models having
- * version == CPU_VERSION_AUTO.
+ * Set CPU model version to the lastest version.
+ * Currently, this is only used by microvm.
  */
-void x86_cpu_set_default_version(X86CPUVersion version);
+void x86_cpu_uses_lastest_version(void);
 
+#ifndef HW_I386_X86_H
+void x86_cpu_set_legacy_version(void);
+#endif
 #ifndef CONFIG_USER_ONLY
 
 void do_cpu_sipi(X86CPU *cpu);
Signature · 1 line
2.45.2
#1 lore raw

Hi Ani,

[snip]

Quoted 14 lines
include/hw/i386/x86.h +4 -1
@@ -114,7 +114,10 @@ void init_topo_info(X86CPUTopoInfo *topo_info, const X86MachineState *x86ms);
 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
                                     unsigned int cpu_index);
 
-void x86_cpus_init(X86MachineState *pcms, int default_cpu_version);
+void x86_cpus_init(X86MachineState *pcms);
+#ifndef I386_CPU_H
+void x86_cpu_set_legacy_version(void);
+#endif
 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count);
 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
                       DeviceState *dev, Error **errp);

[snip]

Quoted 2 lines
target/i386/cpu.h +0 -0

...

target/i386/cpu.h

Quoted 6 lines
target/i386/cpu.h +3 -0
+#ifndef HW_I386_X86_H
+void x86_cpu_set_legacy_version(void);
+#endif
 #ifndef CONFIG_USER_ONLY
 
 void do_cpu_sipi(X86CPU *cpu);

I see your problem: Due to the complex reference relationships, you have to check the header files and make declarations twice.

What about the following solution?

* Declare pc_init_cpus() in pc.h and define it in pc.c (including cpu.h in pc.c).
* Only declare x86_cpu_set_legacy_version() in cpu.h.

An example would be like:

hw/i386/pc.c +14 -0
index b46975c8a4db..9e8b5fa33596 100644
@@ -61,6 +61,7 @@
 #include "hw/i386/kvm/xen_xenstore.h"
 #include "hw/mem/memory-device.h"
 #include "e820_memory_layout.h"
+#include "target/i386/cpu.h"
 #include "trace.h"
 #include "sev.h"
 #include CONFIG_DEVICES
@@ -615,6 +616,19 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
     }
 }
 
+void pc_init_cpus(MachineState *ms)
+{
+    X86MachineState *x86ms = X86_MACHINE(ms);
+    PCMachineState *pcms = PC_MACHINE(ms);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+    if (pcmc->no_versioned_cpu_model) {
+        /* use legacy cpu as it does not support versions */
+        x86_cpu_set_legacy_version();
+    }
+    x86_cpus_init(x86ms);
+}
+
 static
 void pc_machine_done(Notifier *notifier, void *data)
 {
include/hw/i386/pc.h +1 -13
index 34106bb52db8..dc0ca5bcb2a5 100644
@@ -130,19 +130,6 @@ struct PCMachineClass {
 #define TYPE_PC_MACHINE "generic-pc-machine"
 OBJECT_DECLARE_TYPE(PCMachineState, PCMachineClass, PC_MACHINE)
 
-static inline void pc_init_cpus(MachineState *ms)
-{
-    X86MachineState *x86ms = X86_MACHINE(ms);
-    PCMachineState *pcms = PC_MACHINE(ms);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-
-    if (pcmc->no_versioned_cpu_model) {
-        /* use legacy cpu as it does not support versions */
-        x86_cpu_set_legacy_version();
-    }
-    x86_cpus_init(x86ms);
-}
-
 /* ioapic.c */
 
 GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
@@ -150,6 +137,7 @@ GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
 /* pc.c */
 
 void pc_acpi_smi_interrupt(void *opaque, int irq, int level);
+void pc_init_cpus(MachineState *ms);
 
 #define PCI_HOST_PROP_RAM_MEM          "ram-mem"
 #define PCI_HOST_PROP_PCI_MEM          "pci-mem"
include/hw/i386/x86.h +0 -3
index 41236d2a8081..2d2b987fa135 100644
@@ -115,9 +115,6 @@ uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
                                     unsigned int cpu_index);
 
 void x86_cpus_init(X86MachineState *pcms);
-#ifndef I386_CPU_H
-void x86_cpu_set_legacy_version(void);
-#endif
 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count);
 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
                       DeviceState *dev, Error **errp);
target/i386/cpu.h +1 -3
index 9c0ce2adafe7..bdbe54b26f60 100644
@@ -2686,10 +2686,8 @@ typedef int X86CPUVersion;
  * Currently, this is only used by microvm.
  */
 void x86_cpu_uses_lastest_version(void);
-
-#ifndef HW_I386_X86_H
 void x86_cpu_set_legacy_version(void);
-#endif
+
 #ifndef CONFIG_USER_ONLY
 
 void do_cpu_sipi(X86CPU *cpu);

---

The change can be applied on this patch I think. If you like this approach, you can try if the pipeline is happy with it.

Regards,
Zhao

author
#2 lore raw
Quoted 32 lines · Zhao Liu

Hi Ani,

[snip]

Quoted 14 lines
include/hw/i386/x86.h +4 -1
@@ -114,7 +114,10 @@ void init_topo_info(X86CPUTopoInfo *topo_info, const X86MachineState *x86ms);
 uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
                                     unsigned int cpu_index);
 
-void x86_cpus_init(X86MachineState *pcms, int default_cpu_version);
+void x86_cpus_init(X86MachineState *pcms);
+#ifndef I386_CPU_H
+void x86_cpu_set_legacy_version(void);
+#endif
 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count);
 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
                       DeviceState *dev, Error **errp);

[snip]

Quoted 2 lines
target/i386/cpu.h +0 -0

...

Quoted 5 lines
target/i386/cpu.h +3 -0
+#ifndef HW_I386_X86_H
+void x86_cpu_set_legacy_version(void);
+#endif
 #ifndef CONFIG_USER_ONLY
Quoted 4 lines
Quoted 1 line

void do_cpu_sipi(X86CPU *cpu);

I see your problem: Due to the complex reference relationships, you have to check the header files and make declarations twice.

yeah nothing that I did try was working except when I was checking explicitly if a header has been included in that path already.

Quoted 6 lines

What about the following solution?

* Declare pc_init_cpus() in pc.h and define it in pc.c (including cpu.h in pc.c).
* Only declare x86_cpu_set_legacy_version() in cpu.h.

yeah this is something I wondered but did not try in the end.

Quoted 100 lines

An example would be like:

hw/i386/pc.c +14 -0
index b46975c8a4db..9e8b5fa33596 100644
@@ -61,6 +61,7 @@
 #include "hw/i386/kvm/xen_xenstore.h"
 #include "hw/mem/memory-device.h"
 #include "e820_memory_layout.h"
+#include "target/i386/cpu.h"
 #include "trace.h"
 #include "sev.h"
 #include CONFIG_DEVICES
@@ -615,6 +616,19 @@ void pc_acpi_smi_interrupt(void *opaque, int irq, int level)
     }
 }
 
+void pc_init_cpus(MachineState *ms)
+{
+    X86MachineState *x86ms = X86_MACHINE(ms);
+    PCMachineState *pcms = PC_MACHINE(ms);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
+
+    if (pcmc->no_versioned_cpu_model) {
+        /* use legacy cpu as it does not support versions */
+        x86_cpu_set_legacy_version();
+    }
+    x86_cpus_init(x86ms);
+}
+
 static
 void pc_machine_done(Notifier *notifier, void *data)
 {
include/hw/i386/pc.h +1 -13
index 34106bb52db8..dc0ca5bcb2a5 100644
@@ -130,19 +130,6 @@ struct PCMachineClass {
 #define TYPE_PC_MACHINE "generic-pc-machine"
 OBJECT_DECLARE_TYPE(PCMachineState, PCMachineClass, PC_MACHINE)
 
-static inline void pc_init_cpus(MachineState *ms)
-{
-    X86MachineState *x86ms = X86_MACHINE(ms);
-    PCMachineState *pcms = PC_MACHINE(ms);
-    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
-
-    if (pcmc->no_versioned_cpu_model) {
-        /* use legacy cpu as it does not support versions */
-        x86_cpu_set_legacy_version();
-    }
-    x86_cpus_init(x86ms);
-}
-
 /* ioapic.c */
 
 GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
@@ -150,6 +137,7 @@ GSIState *pc_gsi_create(qemu_irq **irqs, bool pci_enabled);
 /* pc.c */
 
 void pc_acpi_smi_interrupt(void *opaque, int irq, int level);
+void pc_init_cpus(MachineState *ms);
 
 #define PCI_HOST_PROP_RAM_MEM          "ram-mem"
 #define PCI_HOST_PROP_PCI_MEM          "pci-mem"
include/hw/i386/x86.h +0 -3
index 41236d2a8081..2d2b987fa135 100644
@@ -115,9 +115,6 @@ uint32_t x86_cpu_apic_id_from_index(X86MachineState *x86ms,
                                     unsigned int cpu_index);
 
 void x86_cpus_init(X86MachineState *pcms);
-#ifndef I386_CPU_H
-void x86_cpu_set_legacy_version(void);
-#endif
 void x86_rtc_set_cpus_count(ISADevice *rtc, uint16_t cpus_count);
 void x86_cpu_pre_plug(HotplugHandler *hotplug_dev,
                       DeviceState *dev, Error **errp);
target/i386/cpu.h +1 -4
index 9c0ce2adafe7..bdbe54b26f60 100644
@@ -2686,10 +2686,8 @@ typedef int X86CPUVersion;
  * Currently, this is only used by microvm.
  */
 void x86_cpu_uses_lastest_version(void);
-
-#ifndef HW_I386_X86_H
 void x86_cpu_set_legacy_version(void);
-#endif
+
 #ifndef CONFIG_USER_ONLY
 
 void do_cpu_sipi(X86CPU *cpu);
 
---
Quoted 2 lines

The change can be applied on this patch I think. If you like this approach, you can try if the pipeline is happy with it.

yes it works. Thanks for the suggestion. Pipeline passes.

Quoted 5 lines

Regards,
Zhao