Skip to contentSkip to conversation

hw/i386/microvm: add uefi-vars-sysbus support via platform bus

PATCH In review Luigi Leonardi 1 participant 5 messages Jul 1 – Aug 17

lore
Share & tools
lore
#0 lore raw

cover hw/i386/microvm: add uefi-vars-sysbus support via platform bus

This series adds support for the uefi-vars-sysbus device on the microvm machine type, enabling persistent UEFI variable storage.

The uefi-vars-sysbus device is a sysbus device that is attached to the platform bus, which microvm does not currently support. To address this, the series adds a platform bus to microvm, following the same pattern used by the ARM virt machine.

The platform bus is mapped at 0xfec20000 with a 1 MB window, placed after the IOAPICs. It is created without IRQ lines since the uefi-vars-sysbus device only requires MMIO.

Patch 1 fixes a crash in the platform bus core when num_irqs is zero,
which is needed to support an MMIO-only platform bus.
Patch 2 adds the platform bus to microvm with MMIO mapping and FDT
integration.
Patch 3 allows the uefi-vars-sysbus device to be instantiated on
microvm.

Corresponding edk2 changes[1]

[1]https://github.com/tianocore/edk2/pull/12751

To: [email protected]
Cc: Paolo Bonzini <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Sergio Lopez <[email protected]>
Cc: Gerd Hoffmann <[email protected]>

---
Luigi Leonardi (3):
hw/core/platform-bus: fix crash when num_irqs is zero hw/i386/microvm: add platform bus support hw/i386/microvm: add uefi-vars-sysbus support

 hw/core/platform-bus.c    |  4 ++++
 hw/i386/Kconfig           |  1 +
 hw/i386/microvm-dt.c      |  9 +++++++++
 hw/i386/microvm.c         | 38 ++++++++++++++++++++++++++++++++++++++
 include/hw/i386/microvm.h |  4 ++++
 5 files changed, 56 insertions(+)

---
base-commit: f38d6ae70f100fc8d3f095a842d2f1ca29262218
change-id: 20260701-microvm_uefi_var-8a434ce7f8b9

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>
#1 lore raw

PATCH 1/3 hw/core/platform-bus: fix crash when num_irqs is zero

bitmap_new(0) returns a zero-size allocation, and the subsequent bitmap_zero() dereferences it because small_nbits(0) is true. Skip IRQ initialization entirely when num_irqs is zero, which is valid for platform buses that only provide MMIO mapping.

This will be the case for the microvm platform bus which initially has no IRQ lines.

---
 hw/core/platform-bus.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/core/platform-bus.c +4 -0
index a2217a2dee..4f765582e7 100644
@@ -194,6 +194,10 @@ static void platform_bus_realize(DeviceState *dev, Error **errp)
                        pbus->mmio_size);
     sysbus_init_mmio(d, &pbus->mmio);
 
+    if (pbus->num_irqs == 0) {
+        return;
+    }
+
     pbus->used_irqs = bitmap_new(pbus->num_irqs);
     pbus->irqs = g_new0(qemu_irq, pbus->num_irqs);
     for (i = 0; i < pbus->num_irqs; i++) {
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/3 hw/i386/microvm: add platform bus support

In reply to Luigi Leonardi

Add a platform bus to microvm to support dynamic sysbus devices with automatic MMIO address allocation. The bus is mapped at 0xfec20000 with a 1 MB window, placed after the IOAPICs.

This is necessary to support the uefi-vars-sysbus device, which will be attached to microvm using the platform bus.

The platform bus is created without IRQ lines for now; IRQ support can be added later as needed.

---
 hw/i386/Kconfig           |  1 +
 hw/i386/microvm-dt.c      |  9 +++++++++
 hw/i386/microvm.c         | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/i386/microvm.h |  4 ++++
 4 files changed, 50 insertions(+)
hw/i386/Kconfig +1 -0
index 12473acaa7..e5f5e11980 100644
@@ -133,6 +133,7 @@ config MICROVM
     select PCI_EXPRESS_GENERIC_BRIDGE
     select USB_XHCI_SYSBUS
     select I8254
+    select PLATFORM_BUS
 
 config NITRO_ENCLAVE
     default y
hw/i386/microvm-dt.c +9 -0
index 45fbb5bbd1..69feddc715 100644
@@ -38,6 +38,7 @@
 #include "hw/i386/fw_cfg.h"
 #include "hw/rtc/mc146818rtc.h"
 #include "hw/core/sysbus.h"
+#include "hw/core/sysbus-fdt.h"
 #include "hw/virtio/virtio-mmio.h"
 #include "hw/usb/xhci.h"
 
@@ -337,6 +338,14 @@ void dt_setup_microvm(MicrovmMachineState *mms)
     qemu_fdt_add_subnode(ms->fdt, "/chosen");
     dt_setup_sys_bus(mms);
 
+    if (mms->platform_bus_dev) {
+        /* Platform bus IRQs are not supported, intc is required but unused */
+        platform_bus_add_all_fdt_nodes(ms->fdt, "/ioapic1",
+                                       MICROVM_PLATFORM_BUS_BASE,
+                                       MICROVM_PLATFORM_BUS_SIZE,
+                                       0);
+    }
+
     /* add to fw_cfg */
     if (debug) {
         fprintf(stderr, "%s: add etc/fdt to fw_cfg\n", __func__);
hw/i386/microvm.c +36 -0
index 779741ec76..d4836b5e12 100644
@@ -47,6 +47,7 @@
 #include "hw/virtio/virtio-mmio.h"
 #include "hw/acpi/acpi.h"
 #include "hw/acpi/generic_event_device.h"
+#include "hw/core/platform-bus.h"
 #include "hw/pci-host/gpex.h"
 #include "hw/usb/xhci.h"
 #include "hw/vfio/types.h"
@@ -146,6 +147,24 @@ static void create_gpex(MicrovmMachineState *mms)
     }
 }
 
+static void create_platform_bus(MicrovmMachineState *mms)
+{
+    DeviceState *dev;
+    SysBusDevice *s;
+
+    dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE);
+    dev->id = g_strdup(TYPE_PLATFORM_BUS_DEVICE);
+    qdev_prop_set_uint32(dev, "num_irqs", 0);
+    qdev_prop_set_uint32(dev, "mmio_size", MICROVM_PLATFORM_BUS_SIZE);
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+    mms->platform_bus_dev = dev;
+
+    s = SYS_BUS_DEVICE(dev);
+    memory_region_add_subregion(get_system_memory(),
+                                MICROVM_PLATFORM_BUS_BASE,
+                                sysbus_mmio_get_region(s, 0));
+}
+
 static int microvm_ioapics(MicrovmMachineState *mms)
 {
     if (!x86_machine_is_acpi_enabled(X86_MACHINE(mms))) {
@@ -248,6 +267,8 @@ static void microvm_devices_init(MicrovmMachineState *mms)
         x86ms->pci_irq_mask = 0;
     }
 
+    create_platform_bus(mms);
+
     if (x86ms->pic == ON_OFF_AUTO_ON || x86ms->pic == ON_OFF_AUTO_AUTO) {
         qemu_irq *i8259;
 
@@ -453,12 +474,27 @@ static HotplugHandler *microvm_get_hotplug_handler(MachineState *machine,
     return NULL;
 }
 
+static void link_one_device(SysBusDevice *sbdev, void *opaque)
+{
+    platform_bus_link_device(PLATFORM_BUS_DEVICE(opaque), sbdev);
+}
+
+static void microvm_link_platform_bus_devices(MicrovmMachineState *mms)
+{
+    if (!mms->platform_bus_dev) {
+        return;
+    }
+
+    foreach_dynamic_sysbus_device(link_one_device, mms->platform_bus_dev);
+}
+
 static void microvm_machine_done(Notifier *notifier, void *data)
 {
     MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
                                             machine_done);
     X86MachineState *x86ms = X86_MACHINE(mms);
 
+    microvm_link_platform_bus_devices(mms);
     acpi_setup_microvm(mms);
     dt_setup_microvm(mms);
     fw_cfg_add_e820(x86ms->fw_cfg);
include/hw/i386/microvm.h +4 -0
index 184b7a8c09..bd2897aa78 100644
@@ -61,6 +61,9 @@
 #define MICROVM_XHCI_BASE     0xfe900000
 #define MICROVM_XHCI_IRQ      10
 
+#define MICROVM_PLATFORM_BUS_BASE 0xfec20000
+#define MICROVM_PLATFORM_BUS_SIZE 0x00100000
+
 #define PCIE_MMIO_BASE        0xc0000000
 #define PCIE_MMIO_SIZE        0x20000000
 #define PCIE_ECAM_BASE        0xe0000000
@@ -101,6 +104,7 @@ struct MicrovmMachineState {
     Notifier machine_done;
     Notifier powerdown_req;
     struct GPEXConfig gpex;
+    DeviceState *platform_bus_dev;
 
     uint32_t ioapic_phandle[2];
 };
Signature · 1 line
2.54.0
#3 lore raw

PATCH 3/3 hw/i386/microvm: add uefi-vars-sysbus support

In reply to Luigi Leonardi

Allow the uefi-vars-sysbus device on microvm.

This is to support persistent UEFI variables on this architecture.

---
 hw/i386/microvm.c | 2 ++
 1 file changed, 2 insertions(+)
hw/i386/microvm.c +2 -0
index d4836b5e12..44a5006b5a 100644
@@ -55,6 +55,7 @@
 #include "elf.h"
 #include "kvm/kvm_i386.h"
 #include "hw/xen/start_info.h"
+#include "hw/uefi/var-service-api.h"
 
 #define MICROVM_QBOOT_FILENAME "qboot.rom"
 #define MICROVM_BIOS_FILENAME  "bios-microvm.bin"
@@ -751,6 +752,7 @@ static void microvm_class_init(ObjectClass *oc, const void *data)
         "Set off to disable adding virtio-mmio devices to the kernel cmdline");
 
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_UEFI_VARS_SYSBUS);
 
     compat_props_add(mc->compat_props, microvm_properties,
                      G_N_ELEMENTS(microvm_properties));
Signature · 1 line
2.54.0
#4 lore raw
In reply to Luigi Leonardi
Quoted 44 lines · Luigi Leonardi

This series adds support for the uefi-vars-sysbus device on the microvm machine type, enabling persistent UEFI variable storage.

The uefi-vars-sysbus device is a sysbus device that is attached to the platform bus, which microvm does not currently support. To address this, the series adds a platform bus to microvm, following the same pattern used by the ARM virt machine.

The platform bus is mapped at 0xfec20000 with a 1 MB window, placed after the IOAPICs. It is created without IRQ lines since the uefi-vars-sysbus device only requires MMIO.

Patch 1 fixes a crash in the platform bus core when num_irqs is zero,
which is needed to support an MMIO-only platform bus.
Patch 2 adds the platform bus to microvm with MMIO mapping and FDT
integration.
Patch 3 allows the uefi-vars-sysbus device to be instantiated on
microvm.

Corresponding edk2 changes[1]

[1]https://github.com/tianocore/edk2/pull/12751

To: [email protected]
Cc: Paolo Bonzini <[email protected]>
Cc: Michael S. Tsirkin <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Sergio Lopez <[email protected]>
Cc: Gerd Hoffmann <[email protected]>

---
Luigi Leonardi (3):
hw/core/platform-bus: fix crash when num_irqs is zero hw/i386/microvm: add platform bus support hw/i386/microvm: add uefi-vars-sysbus support

hw/core/platform-bus.c    |  4 ++++
hw/i386/Kconfig           |  1 +
hw/i386/microvm-dt.c      |  9 +++++++++
hw/i386/microvm.c         | 38 ++++++++++++++++++++++++++++++++++++++
include/hw/i386/microvm.h |  4 ++++
5 files changed, 56 insertions(+)
Quoted 9 lines

---
base-commit: f38d6ae70f100fc8d3f095a842d2f1ca29262218
change-id: 20260701-microvm_uefi_var-8a434ce7f8b9

Best regards,

Signature · 1 line
Luigi Leonardi <[email protected]>

Gentle ping :)

Thanks,
Luigi