Skip to contentSkip to conversation

Add Virtio support to Xenpvh machine for arm

PATCH RESEND Superseded Vikram Garhwal 3 participants 7 messages Jun 29, 2023 – Jul 5, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Add Virtio support to Xenpvh machine for arm

Hi,
We added virtio-mmio support in xenpvh machine. Now, it can support upto 10 virtio mmio.

I think none of previous patches were delivered to mailing list so Resending this series.

Regards,
Vikram

Oleksandr Tyshchenko (2):
xen_arm: Create virtio-mmio devices during initialization
xen_arm: Initialize RAM and add hi/low memory regions

 hw/arm/xen_arm.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
Signature · 1 line
2.25.1
#1 lore raw

PATCH RESEND 1/2 xen_arm: Create virtio-mmio devices during initialization

From: Oleksandr Tyshchenko <[email protected]>

In order to use virtio backends we need to allocate virtio-mmio parameters (irq and base) and register corresponding buses.

Use the constants defined in public header arch-arm.h to be aligned with the toolstack. So the number of current supported virtio-mmio devices is 10.

For the interrupts triggering use already existing on Arm device-model hypercall.

The toolstack should then insert the same amount of device nodes into guest device-tree.

---
 hw/arm/xen_arm.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
hw/arm/xen_arm.c +29 -0
index 60dcd1bcc7..c0a93f2c9d 100644
@@ -26,6 +26,7 @@
 #include "qapi/qapi-commands-migration.h"
 #include "qapi/visitor.h"
 #include "hw/boards.h"
+#include "hw/irq.h"
 #include "hw/sysbus.h"
 #include "sysemu/block-backend.h"
 #include "sysemu/tpm_backend.h"
@@ -59,6 +60,32 @@ struct XenArmState {
     } cfg;
 };
 
+#define VIRTIO_MMIO_DEV_SIZE   0x200
+
+#define NR_VIRTIO_MMIO_DEVICES   \
+   (GUEST_VIRTIO_MMIO_SPI_LAST - GUEST_VIRTIO_MMIO_SPI_FIRST)
+
+static void xen_set_irq(void *opaque, int irq, int level)
+{
+    xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level);
+}
+
+static void xen_create_virtio_mmio_devices(XenArmState *xam)
+{
+    int i;
+
+    for (i = 0; i < NR_VIRTIO_MMIO_DEVICES; i++) {
+        hwaddr base = GUEST_VIRTIO_MMIO_BASE + i * VIRTIO_MMIO_DEV_SIZE;
+        qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL,
+                                         GUEST_VIRTIO_MMIO_SPI_FIRST + i);
+
+        sysbus_create_simple("virtio-mmio", base, irq);
+
+        DPRINTF("Created virtio-mmio device %d: irq %d base 0x%lx\n",
+                i, GUEST_VIRTIO_MMIO_SPI_FIRST + i, base);
+    }
+}
+
 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     hw_error("Invalid ioreq type 0x%x\n", req->type);
@@ -110,6 +137,8 @@ static void xen_arm_init(MachineState *machine)
 
     xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
+    xen_create_virtio_mmio_devices(xam);
+
 #ifdef CONFIG_TPM
     if (xam->cfg.tpm_base_addr) {
         xen_enable_tpm(xam);
Signature · 1 line
2.25.1
#2 lore raw

Re: [RESEND][PATCH v1 1/2] xen_arm: Create virtio-mmio devices during initialization

Quoted 34 lines · Vikram Garhwal

From: Oleksandr Tyshchenko <[email protected]>

In order to use virtio backends we need to allocate virtio-mmio parameters (irq and base) and register corresponding buses.

Use the constants defined in public header arch-arm.h to be aligned with the toolstack. So the number of current supported virtio-mmio devices is 10.

For the interrupts triggering use already existing on Arm device-model hypercall.

The toolstack should then insert the same amount of device nodes into guest device-tree.

---
 hw/arm/xen_arm.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
hw/arm/xen_arm.c +1 -0
index 60dcd1bcc7..c0a93f2c9d 100644
@@ -26,6 +26,7 @@
 #include "qapi/qapi-commands-migration.h"
 #include "qapi/visitor.h"
 #include "hw/boards.h"
+#include "hw/irq.h"
 #include "hw/sysbus.h"
 #include "sysemu/block-backend.h"
 #include "sysemu/tpm_backend.h"

hw/arm/xen_arm.c struct XenArmState { · line 63

Quoted 4 lines
hw/arm/xen_arm.c +1 -0
@@ -59,6 +60,32 @@ struct XenArmState {
     } cfg;
 };
 
+#define VIRTIO_MMIO_DEV_SIZE   0x200

Is this coming from QEMU? Or is it standard virtio?

Just asking to make sure that we don't run into a virtio device that needs more than 0x200 of MMIO size.

hw/arm/xen_arm.c struct XenArmState { · line 71

Quoted 7 lines
hw/arm/xen_arm.c +7 -0
+#define NR_VIRTIO_MMIO_DEVICES   \
+   (GUEST_VIRTIO_MMIO_SPI_LAST - GUEST_VIRTIO_MMIO_SPI_FIRST)
+
+static void xen_set_irq(void *opaque, int irq, int level)
+{
+    xendevicemodel_set_irq_level(xen_dmod, xen_domid, irq, level);
+}

Just a note: likely the xendevicemodel_set_irq_level call needs privileges. Just something to keep in mind for when we try to run QEMU in a domain other than Dom0. No need to do anything for now.

Everything looks good. If we can be sure 0x200 is the right MMIO size for virtio devices then I would provide by Ack.

Quoted 31 lines
hw/arm/xen_arm.c +18 -0
+static void xen_create_virtio_mmio_devices(XenArmState *xam)
+{
+    int i;
+
+    for (i = 0; i < NR_VIRTIO_MMIO_DEVICES; i++) {
+        hwaddr base = GUEST_VIRTIO_MMIO_BASE + i * VIRTIO_MMIO_DEV_SIZE;
+        qemu_irq irq = qemu_allocate_irq(xen_set_irq, NULL,
+                                         GUEST_VIRTIO_MMIO_SPI_FIRST + i);
+
+        sysbus_create_simple("virtio-mmio", base, irq);
+
+        DPRINTF("Created virtio-mmio device %d: irq %d base 0x%lx\n",
+                i, GUEST_VIRTIO_MMIO_SPI_FIRST + i, base);
+    }
+}
+
 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     hw_error("Invalid ioreq type 0x%x\n", req->type);
@@ -110,6 +137,8 @@ static void xen_arm_init(MachineState *machine)
 
     xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
+    xen_create_virtio_mmio_devices(xam);
+
 #ifdef CONFIG_TPM
     if (xam->cfg.tpm_base_addr) {
         xen_enable_tpm(xam);
Signature · 1 line
2.25.1
#3 lore raw

PATCH RESEND 2/2 xen_arm: Initialize RAM and add hi/low memory regions

In reply to Vikram Garhwal

From: Oleksandr Tyshchenko <[email protected]>

In order to use virtio backends we need to initialize RAM for the xen-mapcache (which is responsible for mapping guest memory using foreign mapping) to work. Calculate and add hi/low memory regions based on machine->ram_size.

Use the constants defined in public header arch-arm.h to be aligned with the xen toolstack.

While using this machine, the toolstack should then pass real ram_size using "-m" arg. If "-m" is not given, create a QEMU machine without IOREQ, TPM and VIRTIO to keep it usable for /etc/init.d/xencommons.

---
 hw/arm/xen_arm.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
hw/arm/xen_arm.c +45 -0
index c0a93f2c9d..cc4dffee70 100644
@@ -60,6 +60,8 @@ struct XenArmState {
     } cfg;
 };
 
+static MemoryRegion ram_lo, ram_hi;
+
 #define VIRTIO_MMIO_DEV_SIZE   0x200
 
 #define NR_VIRTIO_MMIO_DEVICES   \
@@ -86,6 +88,39 @@ static void xen_create_virtio_mmio_devices(XenArmState *xam)
     }
 }
 
+static void xen_init_ram(MachineState *machine)
+{
+    MemoryRegion *sysmem = get_system_memory();
+    ram_addr_t block_len, ram_size[GUEST_RAM_BANKS];
+
+    if (machine->ram_size <= GUEST_RAM0_SIZE) {
+        ram_size[0] = machine->ram_size;
+        ram_size[1] = 0;
+        block_len = GUEST_RAM0_BASE + ram_size[0];
+    } else {
+        ram_size[0] = GUEST_RAM0_SIZE;
+        ram_size[1] = machine->ram_size - GUEST_RAM0_SIZE;
+        block_len = GUEST_RAM1_BASE + ram_size[1];
+    }
+
+    memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
+                           &error_fatal);
+
+    memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &ram_memory,
+                             GUEST_RAM0_BASE, ram_size[0]);
+    memory_region_add_subregion(sysmem, GUEST_RAM0_BASE, &ram_lo);
+    DPRINTF("Initialized region xen.ram.lo: base 0x%llx size 0x%lx\n",
+            GUEST_RAM0_BASE, ram_size[0]);
+
+    if (ram_size[1] > 0) {
+        memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", &ram_memory,
+                                 GUEST_RAM1_BASE, ram_size[1]);
+        memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi);
+        DPRINTF("Initialized region xen.ram.hi: base 0x%llx size 0x%lx\n",
+                GUEST_RAM1_BASE, ram_size[1]);
+    }
+}
+
 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     hw_error("Invalid ioreq type 0x%x\n", req->type);
@@ -135,6 +170,14 @@ static void xen_arm_init(MachineState *machine)
 
     xam->state =  g_new0(XenIOState, 1);
 
+    if (machine->ram_size == 0) {
+        DPRINTF("ram_size not specified. QEMU machine will be started without"
+                " TPM, IOREQ and Virtio-MMIO backends\n");
+        return;
+    }
+
+    xen_init_ram(machine);
+
     xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
     xen_create_virtio_mmio_devices(xam);
@@ -182,6 +225,8 @@ static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
     mc->init = xen_arm_init;
     mc->max_cpus = 1;
     mc->default_machine_opts = "accel=xen";
+    /* Set explicitly here to make sure that real ram_size is passed */
+    mc->default_ram_size = 0;
 
     printf("CHECK for NEW BUILD\n");
 #ifdef CONFIG_TPM
Signature · 1 line
2.25.1
#4 lore raw

Re: [RESEND][PATCH v1 2/2] xen_arm: Initialize RAM and add hi/low memory regions

Quoted 74 lines · Vikram Garhwal

From: Oleksandr Tyshchenko <[email protected]>

In order to use virtio backends we need to initialize RAM for the xen-mapcache (which is responsible for mapping guest memory using foreign mapping) to work. Calculate and add hi/low memory regions based on machine->ram_size.

Use the constants defined in public header arch-arm.h to be aligned with the xen toolstack.

While using this machine, the toolstack should then pass real ram_size using "-m" arg. If "-m" is not given, create a QEMU machine without IOREQ, TPM and VIRTIO to keep it usable for /etc/init.d/xencommons.

---
 hw/arm/xen_arm.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)
hw/arm/xen_arm.c +35 -0
index c0a93f2c9d..cc4dffee70 100644
@@ -60,6 +60,8 @@ struct XenArmState {
     } cfg;
 };
 
+static MemoryRegion ram_lo, ram_hi;
+
 #define VIRTIO_MMIO_DEV_SIZE   0x200
 
 #define NR_VIRTIO_MMIO_DEVICES   \
@@ -86,6 +88,39 @@ static void xen_create_virtio_mmio_devices(XenArmState *xam)
     }
 }
 
+static void xen_init_ram(MachineState *machine)
+{
+    MemoryRegion *sysmem = get_system_memory();
+    ram_addr_t block_len, ram_size[GUEST_RAM_BANKS];
+
+    if (machine->ram_size <= GUEST_RAM0_SIZE) {
+        ram_size[0] = machine->ram_size;
+        ram_size[1] = 0;
+        block_len = GUEST_RAM0_BASE + ram_size[0];
+    } else {
+        ram_size[0] = GUEST_RAM0_SIZE;
+        ram_size[1] = machine->ram_size - GUEST_RAM0_SIZE;
+        block_len = GUEST_RAM1_BASE + ram_size[1];
+    }
+
+    memory_region_init_ram(&ram_memory, NULL, "xen.ram", block_len,
+                           &error_fatal);
+
+    memory_region_init_alias(&ram_lo, NULL, "xen.ram.lo", &ram_memory,
+                             GUEST_RAM0_BASE, ram_size[0]);
+    memory_region_add_subregion(sysmem, GUEST_RAM0_BASE, &ram_lo);
+    DPRINTF("Initialized region xen.ram.lo: base 0x%llx size 0x%lx\n",
+            GUEST_RAM0_BASE, ram_size[0]);
+
+    if (ram_size[1] > 0) {
+        memory_region_init_alias(&ram_hi, NULL, "xen.ram.hi", &ram_memory,
+                                 GUEST_RAM1_BASE, ram_size[1]);
+        memory_region_add_subregion(sysmem, GUEST_RAM1_BASE, &ram_hi);
+        DPRINTF("Initialized region xen.ram.hi: base 0x%llx size 0x%lx\n",
+                GUEST_RAM1_BASE, ram_size[1]);
+    }
+}
+
 void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     hw_error("Invalid ioreq type 0x%x\n", req->type);

hw/arm/xen_arm.c static void xen_arm_init(MachineState *machine) · line 177

Quoted 8 lines
hw/arm/xen_arm.c +5 -0
@@ -135,6 +170,14 @@ static void xen_arm_init(MachineState *machine)
 
     xam->state =  g_new0(XenIOState, 1);
 
+    if (machine->ram_size == 0) {
+        DPRINTF("ram_size not specified. QEMU machine will be started without"
+                " TPM, IOREQ and Virtio-MMIO backends\n");
+        return;
+    }

I would say "ram_size not specified. QEMU machine started without IOREQ (no emulated devices including Virtio)."

We might add more devices in the future beyond Virtio and TPM. I don't think we want to call out the whole list here.

Quoted 17 lines
hw/arm/xen_arm.c +4 -0
+    xen_init_ram(machine);
+
     xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
     xen_create_virtio_mmio_devices(xam);
@@ -182,6 +225,8 @@ static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
     mc->init = xen_arm_init;
     mc->max_cpus = 1;
     mc->default_machine_opts = "accel=xen";
+    /* Set explicitly here to make sure that real ram_size is passed */
+    mc->default_ram_size = 0;
 
     printf("CHECK for NEW BUILD\n");
 #ifdef CONFIG_TPM
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH v1 2/2] xen_arm: Initialize RAM and add hi/low memory regions

In reply to Vikram Garhwal

Hi Vikram,

On Thu, Jun 29, 2023 at 10:43:10AM -0700, Oleksandr Tyshchenko wrote:

[...]

Quoted 19 lines

void arch_handle_ioreq(XenIOState *state, ioreq_t *req)

 {
     hw_error("Invalid ioreq type 0x%x\n", req->type);
hw/arm/xen_arm.c +8 -0
@@ -135,6 +170,14 @@ static void xen_arm_init(MachineState *machine)
 
     xam->state =  g_new0(XenIOState, 1);
 
+    if (machine->ram_size == 0) {
+        DPRINTF("ram_size not specified. QEMU machine will be started without"
+                " TPM, IOREQ and Virtio-MMIO backends\n");
+        return;
+    }
+
+    xen_init_ram(machine);
+
     xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
     xen_create_virtio_mmio_devices(xam);

hw/arm/xen_arm.c static void xen_arm_machine_class_init(ObjectClass *oc, void *data) · line 229

Quoted 5 lines
hw/arm/xen_arm.c +2 -0
@@ -182,6 +225,8 @@ static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
     mc->init = xen_arm_init;
     mc->max_cpus = 1;
     mc->default_machine_opts = "accel=xen";
+    /* Set explicitly here to make sure that real ram_size is passed */
+    mc->default_ram_size = 0;

This patch fails to apply on my side on QEMU 8.0.0.

hw/arm/xen_arm.c static void xen_arm_machine_class_init(ObjectClass *oc, void *data) · line 231

Quoted 1 line
hw/arm/xen_arm.c +0 -0
     printf("CHECK for NEW BUILD\n");

The printf sentence is introduced unexpectly, right?

Thanks,
Leo

Quoted 3 lines
hw/arm/xen_arm.c +0 -0
 #ifdef CONFIG_TPM
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH v1 2/2] xen_arm: Initialize RAM and add hi/low memory regions

HI Leo,

Quoted 32 lines · Leo Yan

Hi Vikram,

On Thu, Jun 29, 2023 at 10:43:10AM -0700, Oleksandr Tyshchenko wrote:

[...]

Quoted 19 lines
  void arch_handle_ioreq(XenIOState *state, ioreq_t *req)
  {
      hw_error("Invalid ioreq type 0x%x\n", req->type);
hw/arm/xen_arm.c +8 -0
@@ -135,6 +170,14 @@ static void xen_arm_init(MachineState *machine)
 
      xam->state =  g_new0(XenIOState, 1);
 
+    if (machine->ram_size == 0) {
+        DPRINTF("ram_size not specified. QEMU machine will be started without"
+                " TPM, IOREQ and Virtio-MMIO backends\n");
+        return;
+    }
+
+    xen_init_ram(machine);
+
      xen_register_ioreq(xam->state, machine->smp.cpus, xen_memory_listener);
 
      xen_create_virtio_mmio_devices(xam);

hw/arm/xen_arm.c static void xen_arm_machine_class_init(ObjectClass *oc, void *data) · line 229

Quoted 5 lines
hw/arm/xen_arm.c +2 -0
@@ -182,6 +225,8 @@ static void xen_arm_machine_class_init(ObjectClass *oc, void *data)
      mc->init = xen_arm_init;
      mc->max_cpus = 1;
      mc->default_machine_opts = "accel=xen";
+    /* Set explicitly here to make sure that real ram_size is passed */
+    mc->default_ram_size = 0;

This patch fails to apply on my side on QEMU 8.0.0.

Quoted 2 lines

hw/arm/xen_arm.c static void xen_arm_machine_class_init(ObjectClass *oc, void *data) · line 231

Quoted 1 line
hw/arm/xen_arm.c +0 -0
      printf("CHECK for NEW BUILD\n");

The printf sentence is introduced unexpectly, right?

I will rebase it with latest and resend v2.
Thank you!

Quoted 7 lines

Thanks,
Leo

Quoted 3 lines
hw/arm/xen_arm.c +0 -0
  #ifdef CONFIG_TPM
Signature · 1 line
2.25.1