Skip to contentSkip to conversation

x86: add etc/phys-bits fw_cfg file

PATCH Superseded Gerd Hoffmann 2 participants 5 messages Sep 22, 2022 KVM

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 x86: add etc/phys-bits fw_cfg file

In case phys bits are functional and can be used by the guest (aka host-phys-bits=on) add a fw_cfg file carrying the value. This can be used by the guest firmware for address space configuration.

This is only enabled for 7.2+ machine types for live migration compatibility reasons.

---
 hw/i386/fw_cfg.h     |  1 +
 include/hw/i386/pc.h |  1 +
 hw/i386/fw_cfg.c     | 12 ++++++++++++
 hw/i386/pc.c         |  5 +++++
 hw/i386/pc_piix.c    |  2 ++
 hw/i386/pc_q35.c     |  2 ++
 6 files changed, 23 insertions(+)
hw/i386/fw_cfg.h +1 -0
index 275f15c1c5e8..6ff198a6cb85 100644
@@ -26,5 +26,6 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,
 void fw_cfg_build_smbios(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg);
+void fw_cfg_phys_bits(FWCfgState *fw_cfg);
 
 #endif
include/hw/i386/pc.h +1 -0
index c95333514ed3..bedef1ee13c1 100644
@@ -119,6 +119,7 @@ struct PCMachineClass {
     bool enforce_aligned_dimm;
     bool broken_reserved_end;
     bool enforce_amd_1tb_hole;
+    bool phys_bits_in_fw_cfg;
 
     /* generate legacy CPU hotplug AML */
     bool legacy_cpu_hotplug;
hw/i386/fw_cfg.c +12 -0
index a283785a8de4..6a1f18925725 100644
@@ -219,3 +219,15 @@ void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg)
     aml_append(dev, aml_name_decl("_CRS", crs));
     aml_append(scope, dev);
 }
+
+void fw_cfg_phys_bits(FWCfgState *fw_cfg)
+{
+    X86CPU *cpu = X86_CPU(first_cpu);
+    uint64_t phys_bits = cpu->phys_bits;
+
+    if (cpu->host_phys_bits) {
+        fw_cfg_add_file(fw_cfg, "etc/phys-bits",
+                        g_memdup2(&phys_bits, sizeof(phys_bits)),
+                        sizeof(phys_bits));
+    }
+}
hw/i386/pc.c +5 -0
index 566accf7e60a..17ecc7fe4331 100644
@@ -744,6 +744,7 @@ void pc_machine_done(Notifier *notifier, void *data)
 {
     PCMachineState *pcms = container_of(notifier,
                                         PCMachineState, machine_done);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     X86MachineState *x86ms = X86_MACHINE(pcms);
 
     cxl_hook_up_pxb_registers(pcms->bus, &pcms->cxl_devices_state,
@@ -764,6 +765,9 @@ void pc_machine_done(Notifier *notifier, void *data)
         fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
         fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
+        if (pcmc->phys_bits_in_fw_cfg) {
+            fw_cfg_phys_bits(x86ms->fw_cfg);
+        }
     }
 }
 
@@ -1907,6 +1911,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     pcmc->kvmclock_enabled = true;
     pcmc->enforce_aligned_dimm = true;
     pcmc->enforce_amd_1tb_hole = true;
+    pcmc->phys_bits_in_fw_cfg = true;
     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
      * to be used at the moment, 32K should be enough for a while.  */
     pcmc->acpi_data_size = 0x20000 + 0x8000;
hw/i386/pc_piix.c +2 -0
index 8043a250adf3..c6a4dbd5c0b0 100644
@@ -447,9 +447,11 @@ DEFINE_I440FX_MACHINE(v7_2, "pc-i440fx-7.2", NULL,
 
 static void pc_i440fx_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_i440fx_7_2_machine_options(m);
     m->alias = NULL;
     m->is_default = false;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
hw/i386/pc_q35.c +2 -0
index 53eda50e818c..c2b56daa1550 100644
@@ -384,8 +384,10 @@ DEFINE_Q35_MACHINE(v7_2, "pc-q35-7.2", NULL,
 
 static void pc_q35_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_q35_7_2_machine_options(m);
     m->alias = NULL;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
Signature · 1 line
2.37.3
#1 lore raw
Quoted 8 lines · Gerd Hoffmann

In case phys bits are functional and can be used by the guest (aka host-phys-bits=on) add a fw_cfg file carrying the value. This can be used by the guest firmware for address space configuration.

This is only enabled for 7.2+ machine types for live migration compatibility reasons.

I'm curious why you decided to switch from a cpuid flag to fw cfg. I guess firmware reads fw cfg anyway. But would the guest kernel then need to load a fw cfg driver very early to detect this, too?

Quoted 42 lines
---
 hw/i386/fw_cfg.h     |  1 +
 include/hw/i386/pc.h |  1 +
 hw/i386/fw_cfg.c     | 12 ++++++++++++
 hw/i386/pc.c         |  5 +++++
 hw/i386/pc_piix.c    |  2 ++
 hw/i386/pc_q35.c     |  2 ++
 6 files changed, 23 insertions(+)
hw/i386/fw_cfg.h +1 -0
index 275f15c1c5e8..6ff198a6cb85 100644
@@ -26,5 +26,6 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,
 void fw_cfg_build_smbios(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg);
+void fw_cfg_phys_bits(FWCfgState *fw_cfg);
 
 #endif
include/hw/i386/pc.h +1 -0
index c95333514ed3..bedef1ee13c1 100644
@@ -119,6 +119,7 @@ struct PCMachineClass {
     bool enforce_aligned_dimm;
     bool broken_reserved_end;
     bool enforce_amd_1tb_hole;
+    bool phys_bits_in_fw_cfg;
 
     /* generate legacy CPU hotplug AML */
     bool legacy_cpu_hotplug;
hw/i386/fw_cfg.c +2 -0
index a283785a8de4..6a1f18925725 100644
@@ -219,3 +219,15 @@ void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg)
     aml_append(dev, aml_name_decl("_CRS", crs));
     aml_append(scope, dev);
 }
+
+void fw_cfg_phys_bits(FWCfgState *fw_cfg)

hw/i386/fw_cfg.c void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg) · line 233

Quoted 10 lines
hw/i386/fw_cfg.c +10 -0
+{
+    X86CPU *cpu = X86_CPU(first_cpu);
+    uint64_t phys_bits = cpu->phys_bits;
+
+    if (cpu->host_phys_bits) {
+        fw_cfg_add_file(fw_cfg, "etc/phys-bits",
+                        g_memdup2(&phys_bits, sizeof(phys_bits)),
+                        sizeof(phys_bits));
+    }
+}

So, this allows a lot of flexibility, any phys_bits value at all can now be used. Do you expect a use-case for such a flexible mechanism? If this ends up merely repeating CPUID at all times then we are just creating confusion.
Could you add the motivation to the commit log pls?

Quoted 63 lines
hw/i386/pc.c +5 -0
index 566accf7e60a..17ecc7fe4331 100644
@@ -744,6 +744,7 @@ void pc_machine_done(Notifier *notifier, void *data)
 {
     PCMachineState *pcms = container_of(notifier,
                                         PCMachineState, machine_done);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     X86MachineState *x86ms = X86_MACHINE(pcms);
 
     cxl_hook_up_pxb_registers(pcms->bus, &pcms->cxl_devices_state,
@@ -764,6 +765,9 @@ void pc_machine_done(Notifier *notifier, void *data)
         fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
         fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
+        if (pcmc->phys_bits_in_fw_cfg) {
+            fw_cfg_phys_bits(x86ms->fw_cfg);
+        }
     }
 }
 
@@ -1907,6 +1911,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     pcmc->kvmclock_enabled = true;
     pcmc->enforce_aligned_dimm = true;
     pcmc->enforce_amd_1tb_hole = true;
+    pcmc->phys_bits_in_fw_cfg = true;
     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
      * to be used at the moment, 32K should be enough for a while.  */
     pcmc->acpi_data_size = 0x20000 + 0x8000;
hw/i386/pc_piix.c +2 -0
index 8043a250adf3..c6a4dbd5c0b0 100644
@@ -447,9 +447,11 @@ DEFINE_I440FX_MACHINE(v7_2, "pc-i440fx-7.2", NULL,
 
 static void pc_i440fx_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_i440fx_7_2_machine_options(m);
     m->alias = NULL;
     m->is_default = false;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
hw/i386/pc_q35.c +2 -0
index 53eda50e818c..c2b56daa1550 100644
@@ -384,8 +384,10 @@ DEFINE_Q35_MACHINE(v7_2, "pc-q35-7.2", NULL,
 
 static void pc_q35_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_q35_7_2_machine_options(m);
     m->alias = NULL;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
Signature · 1 line
2.37.3
#2 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 8 lines · Gerd Hoffmann

In case phys bits are functional and can be used by the guest (aka host-phys-bits=on) add a fw_cfg file carrying the value. This can be used by the guest firmware for address space configuration.

This is only enabled for 7.2+ machine types for live migration compatibility reasons.

I'm curious why you decided to switch from a cpuid flag to fw cfg.

The kernel people didn't like the cpuid approach.

Quoted 1 line

I guess firmware reads fw cfg anyway.

Correct.

Quoted 2 lines

But would the guest kernel then need to load a fw cfg driver very early to detect this, too?

Nope, the guest kernel can just work with the address space layout created by the firmware. The firmware can for example reserve a larger 64-bit mmio window in case there is enough address space for that. So it programs the bridge windows etc accordingly, qemu generates matching acpi tables and the kernel picks up the changes via _CRS.

Quoted 8 lines
Quoted 8 lines
hw/i386/fw_cfg.c +8 -0
+void fw_cfg_phys_bits(FWCfgState *fw_cfg)
+{
+    X86CPU *cpu = X86_CPU(first_cpu);
+    uint64_t phys_bits = cpu->phys_bits;
+
+    if (cpu->host_phys_bits) {
+        fw_cfg_add_file(fw_cfg, "etc/phys-bits",
+                        g_memdup2(&phys_bits, sizeof(phys_bits)),
Quoted 8 lines

hw/i386/fw_cfg.c void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg) · line 233

Quoted 3 lines
hw/i386/fw_cfg.c +3 -0
+                        sizeof(phys_bits));
+    }
+}

So, this allows a lot of flexibility, any phys_bits value at all can now be used. Do you expect a use-case for such a flexible mechanism? If this ends up merely repeating CPUID at all times then we are just creating confusion.

Yes, it'll just repeat CPUID. Advantage is that the guest gets the information it needs right away.

Alternatively I could create a "etc/reliable-phys-bits" bool. The firmware must consult both fw_cfg and cpuid then.

take care,
Gerd

#3 lore raw
Quoted 46 lines · Gerd Hoffmann
Quoted 11 lines · Michael S. Tsirkin
Quoted 8 lines · Gerd Hoffmann

In case phys bits are functional and can be used by the guest (aka host-phys-bits=on) add a fw_cfg file carrying the value. This can be used by the guest firmware for address space configuration.

This is only enabled for 7.2+ machine types for live migration compatibility reasons.

I'm curious why you decided to switch from a cpuid flag to fw cfg.

The kernel people didn't like the cpuid approach.

Quoted 1 line

I guess firmware reads fw cfg anyway.

Correct.

Quoted 2 lines

But would the guest kernel then need to load a fw cfg driver very early to detect this, too?

Nope, the guest kernel can just work with the address space layout created by the firmware. The firmware can for example reserve a larger 64-bit mmio window in case there is enough address space for that. So it programs the bridge windows etc accordingly, qemu generates matching acpi tables and the kernel picks up the changes via _CRS.

Quoted 8 lines
Quoted 8 lines
hw/i386/fw_cfg.c +8 -0
+void fw_cfg_phys_bits(FWCfgState *fw_cfg)
+{
+    X86CPU *cpu = X86_CPU(first_cpu);
+    uint64_t phys_bits = cpu->phys_bits;
+
+    if (cpu->host_phys_bits) {
+        fw_cfg_add_file(fw_cfg, "etc/phys-bits",
+                        g_memdup2(&phys_bits, sizeof(phys_bits)),
Quoted 8 lines

hw/i386/fw_cfg.c void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg) · line 233

Quoted 3 lines
hw/i386/fw_cfg.c +3 -0
+                        sizeof(phys_bits));
+    }
+}

So, this allows a lot of flexibility, any phys_bits value at all can now be used. Do you expect a use-case for such a flexible mechanism? If this ends up merely repeating CPUID at all times then we are just creating confusion.

Quoted 8 lines

Yes, it'll just repeat CPUID. Advantage is that the guest gets the information it needs right away.

Alternatively I could create a "etc/reliable-phys-bits" bool. The firmware must consult both fw_cfg and cpuid then.

take care,
Gerd

It might not be too bad if we actually allow these two to be different theoretically (even if unused for now).
This is up to you. But my point is, if we do let's document what is the expected behaviour if fw cfg and CPUID differ.

Signature · 1 line
MST
Reviewed-by on v3 1/1
#4 lore raw
In reply to Gerd Hoffmann
Quoted 8 lines · Gerd Hoffmann

In case phys bits are functional and can be used by the guest (aka host-phys-bits=on) add a fw_cfg file carrying the value. This can be used by the guest firmware for address space configuration.

This is only enabled for 7.2+ machine types for live migration compatibility reasons.

The patch looks good to me. A comment explaining what is going on as I posted elsewhere in this thread can't hurt.

Besides that

Quoted 115 lines
---
 hw/i386/fw_cfg.h     |  1 +
 include/hw/i386/pc.h |  1 +
 hw/i386/fw_cfg.c     | 12 ++++++++++++
 hw/i386/pc.c         |  5 +++++
 hw/i386/pc_piix.c    |  2 ++
 hw/i386/pc_q35.c     |  2 ++
 6 files changed, 23 insertions(+)
hw/i386/fw_cfg.h +1 -0
index 275f15c1c5e8..6ff198a6cb85 100644
@@ -26,5 +26,6 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,
 void fw_cfg_build_smbios(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_build_feature_control(MachineState *ms, FWCfgState *fw_cfg);
 void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg);
+void fw_cfg_phys_bits(FWCfgState *fw_cfg);
 
 #endif
include/hw/i386/pc.h +1 -0
index c95333514ed3..bedef1ee13c1 100644
@@ -119,6 +119,7 @@ struct PCMachineClass {
     bool enforce_aligned_dimm;
     bool broken_reserved_end;
     bool enforce_amd_1tb_hole;
+    bool phys_bits_in_fw_cfg;
 
     /* generate legacy CPU hotplug AML */
     bool legacy_cpu_hotplug;
hw/i386/fw_cfg.c +12 -0
index a283785a8de4..6a1f18925725 100644
@@ -219,3 +219,15 @@ void fw_cfg_add_acpi_dsdt(Aml *scope, FWCfgState *fw_cfg)
     aml_append(dev, aml_name_decl("_CRS", crs));
     aml_append(scope, dev);
 }
+
+void fw_cfg_phys_bits(FWCfgState *fw_cfg)
+{
+    X86CPU *cpu = X86_CPU(first_cpu);
+    uint64_t phys_bits = cpu->phys_bits;
+
+    if (cpu->host_phys_bits) {
+        fw_cfg_add_file(fw_cfg, "etc/phys-bits",
+                        g_memdup2(&phys_bits, sizeof(phys_bits)),
+                        sizeof(phys_bits));
+    }
+}
hw/i386/pc.c +5 -0
index 566accf7e60a..17ecc7fe4331 100644
@@ -744,6 +744,7 @@ void pc_machine_done(Notifier *notifier, void *data)
 {
     PCMachineState *pcms = container_of(notifier,
                                         PCMachineState, machine_done);
+    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(pcms);
     X86MachineState *x86ms = X86_MACHINE(pcms);
 
     cxl_hook_up_pxb_registers(pcms->bus, &pcms->cxl_devices_state,
@@ -764,6 +765,9 @@ void pc_machine_done(Notifier *notifier, void *data)
         fw_cfg_build_feature_control(MACHINE(pcms), x86ms->fw_cfg);
         /* update FW_CFG_NB_CPUS to account for -device added CPUs */
         fw_cfg_modify_i16(x86ms->fw_cfg, FW_CFG_NB_CPUS, x86ms->boot_cpus);
+        if (pcmc->phys_bits_in_fw_cfg) {
+            fw_cfg_phys_bits(x86ms->fw_cfg);
+        }
     }
 }
 
@@ -1907,6 +1911,7 @@ static void pc_machine_class_init(ObjectClass *oc, void *data)
     pcmc->kvmclock_enabled = true;
     pcmc->enforce_aligned_dimm = true;
     pcmc->enforce_amd_1tb_hole = true;
+    pcmc->phys_bits_in_fw_cfg = true;
     /* BIOS ACPI tables: 128K. Other BIOS datastructures: less than 4K reported
      * to be used at the moment, 32K should be enough for a while.  */
     pcmc->acpi_data_size = 0x20000 + 0x8000;
hw/i386/pc_piix.c +2 -0
index 8043a250adf3..c6a4dbd5c0b0 100644
@@ -447,9 +447,11 @@ DEFINE_I440FX_MACHINE(v7_2, "pc-i440fx-7.2", NULL,
 
 static void pc_i440fx_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_i440fx_7_2_machine_options(m);
     m->alias = NULL;
     m->is_default = false;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
hw/i386/pc_q35.c +2 -0
index 53eda50e818c..c2b56daa1550 100644
@@ -384,8 +384,10 @@ DEFINE_Q35_MACHINE(v7_2, "pc-q35-7.2", NULL,
 
 static void pc_q35_7_1_machine_options(MachineClass *m)
 {
+    PCMachineClass *pcmc = PC_MACHINE_CLASS(m);
     pc_q35_7_2_machine_options(m);
     m->alias = NULL;
+    pcmc->phys_bits_in_fw_cfg = false;
     compat_props_add(m->compat_props, hw_compat_7_1, hw_compat_7_1_len);
     compat_props_add(m->compat_props, pc_compat_7_1, pc_compat_7_1_len);
 }
Signature · 1 line
2.37.3