Skip to contentSkip to conversation

IOREQ feature (+ virtio-mmio) on Arm

RFC PATCH In review Oleksandr Tyshchenko 7 participants 140 messages Aug 3, 2020 – Aug 21, 2020 Page 2 of 2

lore
Share & tools
lore
#108 lore raw

Re: [RFC PATCH V1 05/12] hvm/dm: Introduce xendevicemodel_set_irq_level DM op

In reply to Stefano Stabellini
Quoted 130 lines · Stefano Stabellini
Quoted 123 lines · Jan Beulich
Quoted 122 lines · Stefano Stabellini
Quoted 98 lines · Jan Beulich
Quoted 97 lines · Stefano Stabellini
Quoted 96 lines · Julien Grall
Quoted 67 lines · Stefano Stabellini
Quoted 51 lines · Julien Grall
Quoted 25 lines · Stefano Stabellini
Quoted 24 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch adds ability to the device emulator to notify otherend (some entity running in the guest) using a SPI and implements Arm specific bits for it. Proposed interface allows emulator to set the logical level of a one of a domain's IRQ lines.

Please note, this is a split/cleanup of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"

---
   tools/libs/devicemodel/core.c                   | 18
++++++++++++++++++
   tools/libs/devicemodel/include/xendevicemodel.h |  4 ++++
   tools/libs/devicemodel/libxendevicemodel.map    |  1 +
   xen/arch/arm/dm.c                               | 22
+++++++++++++++++++++-
   xen/common/hvm/dm.c                             |  1 +
   xen/include/public/hvm/dm_op.h                  | 15
+++++++++++++++
   6 files changed, 60 insertions(+), 1 deletion(-)
Quoted 15 lines

tools/libs/devicemodel/core.c int xendevicemodel_set_isa_irq_level( · line 434

Quoted 11 lines
tools/libs/devicemodel/core.c +2 -0
index 4d40639..30bd79f 100644
@@ -430,6 +430,24 @@ int xendevicemodel_set_isa_irq_level(
       return xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));
   }
   +int xendevicemodel_set_irq_level(
+    xendevicemodel_handle *dmod, domid_t domid, uint32_t irq,
+    unsigned int level)

It is a pity that having xen_dm_op_set_pci_intx_level and xen_dm_op_set_isa_irq_level already we need to add a third one, but from the names alone I don't think we can reuse either of them.

The problem is not the name...

Quoted 6 lines

It is very similar to set_isa_irq_level. We could almost rename xendevicemodel_set_isa_irq_level to xendevicemodel_set_irq_level or, better, just add an alias to it so that xendevicemodel_set_irq_level is implemented by calling xendevicemodel_set_isa_irq_level. Honestly I am not sure if it is worth doing it though. Any other opinions?

Quoted 3 lines

... the problem is the interrupt field is only 8-bit. So we would only be able
to cover IRQ 0 - 255.

Argh, that's not going to work :-( I wasn't sure if it was a good idea anyway.

Quoted 7 lines

It is not entirely clear how the existing subop could be extended without breaking existing callers.

Quoted 3 lines

But I think we should plan for not needing two calls (one to set level to 1, and one to set it to 0):
https://marc.info/?l=xen-devel&m=159535112027405

Quoted 6 lines
Quoted 3 lines

I am not sure to understand your suggestion here? Are you suggesting to remove
the 'level' parameter?

My hope was to make it optional to call the hypercall with level = 0, not necessarily to remove 'level' from the struct.

From my understanding, the hypercall is meant to represent the status of the line between the device and the interrupt controller (either low or high).

This is then up to the interrupt controller to decide when the interrupt is going to be fired:
- For edge interrupt, this will fire when the line move from low to high (or
vice versa).
- For level interrupt, this will fire when line is high (assuming level
trigger high) and will keeping firing until the device decided to lower the line.

For a device, it is common to keep the line high until an OS wrote to a specific register.

Furthermore, technically, the guest OS is in charge to configure how an interrupt is triggered. Admittely this information is part of the DT, but nothing prevent a guest to change it.

As side note, we have a workaround in Xen for some buggy DT (see the arch timer) exposing the wrong trigger type.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Because of that, I don't really see a way to make optional. Maybe you have something different in mind?

For level, we need the level parameter. For edge, we are only interested in the "edge", right?

I don't think so, unless Arm has special restrictions. Edges can be both rising and falling ones.

And the same is true for level interrupts too: they could be active-low or active-high.

Instead of modelling the state of the line, which seems to be a bit error prone especially in the case of a single-device emulator that might not have enough information about the rest of the system (it might not know if the interrupt is active-high or active-low), we could model the triggering of the interrupt instead.

In the case of level=1, it would mean that the interrupt line is active, no matter if it is active-low or active-high. In the case of level=0, it would mean that it is inactive.

Quoted 5 lines
Quoted 2 lines

Similarly, in the case of an edge interrupt edge=1 or level=1 would mean that there is an edge, no matter if it is a rising or falling.

Am I understanding right that you propose to fold two properties into a single bit?

Quoted 8 lines

I don't think I understand what are the two properties that my proposal is merging into a single bit.

The hypercall specifies the state of the line in terms of "high" and "low". My proposal is to replace it with "fire the interrupt" for edge interrupts, and "interrupt enabled/disabled" for level, abstracting away the state of the line in terms of high/low and instead focusing on whether the interrupt should be injected or not.

Okay, I realize I misunderstood. There's a naming issue that I think gets in the way here: Since this is about triggering an IRQ without "setting" its specific properties, perhaps "trigger_irq" would be a better name, with your boolean distinguishing the "assert" and "deassert" cases (and the other one telling "edge" vs "level")?

Jan

#109 lore raw

Re: [RFC PATCH V1 05/12] hvm/dm: Introduce xendevicemodel_set_irq_level DM op

In reply to Oleksandr Tyshchenko
Quoted 8 lines · Oleksandr Tyshchenko
xen/include/public/hvm/dm_op.h +2 -0
@@ -417,6 +417,20 @@ struct xen_dm_op_pin_memory_cacheattr {
     uint32_t pad;
 };
 
+/*
+ * XEN_DMOP_set_irq_level: Set the logical level of a one of a domain's

xen/include/public/hvm/dm_op.h struct xen_dm_op_pin_memory_cacheattr { · line 431

Quoted 10 lines
xen/include/public/hvm/dm_op.h +10 -0
+ *                         IRQ lines.
+ * XXX Handle PPIs.
+ */
+#define XEN_DMOP_set_irq_level 19
+
+struct xen_dm_op_set_irq_level {
+    uint32_t irq;
+    /* IN - Level: 0 -> deasserted, 1 -> asserted */
+    uint8_t  level;
+};

If this is the way to go (I've seen other discussion going on), please make sure you add explicit padding fields and ...

xen/include/public/hvm/dm_op.h struct xen_dm_op_pin_memory_cacheattr { · line 434

Quoted 3 lines
xen/include/public/hvm/dm_op.h +2 -0
+
+
 struct xen_dm_op {

... you don't add double blank lines.

Jan

author
#110 lore raw

Re: [RFC PATCH V1 05/12] hvm/dm: Introduce xendevicemodel_set_irq_level DM op

On 05.08.20 19:15, Jan Beulich wrote:

Hi, Jan

Quoted 7 lines · Oleksandr Tyshchenko
Quoted 6 lines
xen/include/public/hvm/dm_op.h +0 -0
@@ -417,6 +417,20 @@ struct xen_dm_op_pin_memory_cacheattr {
      uint32_t pad;
  };
 
Quoted 14 lines

xen/include/public/hvm/dm_op.h struct xen_dm_op_pin_memory_cacheattr { · line 431

Quoted 12 lines
xen/include/public/hvm/dm_op.h +12 -0
+/*
+ * XEN_DMOP_set_irq_level: Set the logical level of a one of a domain's
+ *                         IRQ lines.
+ * XXX Handle PPIs.
+ */
+#define XEN_DMOP_set_irq_level 19
+
+struct xen_dm_op_set_irq_level {
+    uint32_t irq;
+    /* IN - Level: 0 -> deasserted, 1 -> asserted */
+    uint8_t  level;
+};

If this is the way to go (I've seen other discussion going on), please make sure you add explicit padding fields and ...

ok

Quoted 5 lines

xen/include/public/hvm/dm_op.h struct xen_dm_op_pin_memory_cacheattr { · line 434

Quoted 4 lines
xen/include/public/hvm/dm_op.h +2 -0
+
+
  struct xen_dm_op {

... you don't add double blank lines.

ok

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#111 lore raw

RFC PATCH 6/12 libxl: Introduce basic virtio-mmio support on Arm

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch creates specific device node in the Guest device-tree with allocated MMIO range and SPI interrupt if specific 'virtio' property is present in domain config.

Please note, this patch breaks device passthrough use-case which will be fixed in one of the follow-up patches.

Please note, this is a split/cleanup of Julien's PoC:
"Add support for Guest IO forwarding to a device emulator"

---
 tools/libxl/libxl_arm.c     | 39 +++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl |  1 +
 tools/xl/xl_parse.c         |  1 +
 3 files changed, 41 insertions(+)
tools/libxl/libxl_arm.c +39 -0
index 34f8a29..620b499 100644
@@ -68,6 +68,10 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             nr_spis = spi + 1;
     }
 
+
+    /* XXX: Handle properly virtio */
+    nr_spis = 1;
+
     LOG(DEBUG, "Configure the domain");
 
     config->arch.nr_spis = nr_spis;
@@ -659,6 +663,37 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
+#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
+#define GUEST_VIRTIO_MMIO_SPI   33
+
+static int make_virtio_mmio_node(libxl__gc *gc, void *fdt)
+{
+    int res;
+    gic_interrupt intr;
+
+    /* XXX: Add address in the node name */
+    res = fdt_begin_node(fdt, "virtio");
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "virtio,mmio");
+    if (res) return res;
+
+    res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROOT_SIZE_CELLS,
+                            1, GUEST_VIRTIO_MMIO_BASE, GUEST_VIRTIO_MMIO_SIZE);
+    if (res) return res;
+
+    set_interrupt(intr, GUEST_VIRTIO_MMIO_SPI, 0xf, DT_IRQ_TYPE_EDGE_RISING);
+    res = fdt_property_interrupts(gc, fdt, &intr, 1);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return 0;
+
+}
+
 static const struct arch_info *get_arch_info(libxl__gc *gc,
                                              const struct xc_dom_image *dom)
 {
@@ -962,6 +997,9 @@ next_resize:
         if (info->tee == LIBXL_TEE_TYPE_OPTEE)
             FDT( make_optee_node(gc, fdt) );
 
+        if (libxl_defbool_val(info->arch_arm.virtio))
+            FDT( make_virtio_mmio_node(gc, fdt) );
+
         if (pfdt)
             FDT( copy_partial_fdt(gc, fdt, pfdt) );
 
@@ -1179,6 +1217,7 @@ void libxl__arch_domain_build_info_setdefault(libxl__gc *gc,
 {
     /* ACPI is disabled by default */
     libxl_defbool_setdefault(&b_info->acpi, false);
+    libxl_defbool_setdefault(&b_info->arch_arm.virtio, false);
 
     if (b_info->type != LIBXL_DOMAIN_TYPE_PV)
         return;
tools/libxl/libxl_types.idl +1 -0
index 9d3f05f..b054bf9 100644
@@ -639,6 +639,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
 
 
     ("arch_arm", Struct(None, [("gic_version", libxl_gic_version),
+                               ("virtio", libxl_defbool),
                                ("vuart", libxl_vuart_type),
                               ])),
     # Alternate p2m is not bound to any architecture or guest type, as it is
tools/xl/xl_parse.c +1 -0
index 61b4ef7..b8306aa 100644
@@ -2579,6 +2579,7 @@ skip_usbdev:
     }
 
     xlu_cfg_get_defbool(config, "dm_restrict", &b_info->dm_restrict, 0);
+    xlu_cfg_get_defbool(config, "virtio", &b_info->arch_arm.virtio, 0);
 
     if (c_info->type == LIBXL_DOMAIN_TYPE_HVM) {
         if (!xlu_cfg_get_string (config, "vga", &buf, 0)) {
Signature · 1 line
2.7.4
#112 lore raw

RFC PATCH 7/12 A collection of tweaks to be able to run emulator in driver domain

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

---
 xen/common/domain.c        | 15 +++++++++++++++
 xen/common/domctl.c        |  8 +++++++-
 xen/common/event_channel.c | 14 ++++++++++++--
 xen/common/memory.c        |  6 ++++++
 xen/include/xsm/dummy.h    | 16 +++++++++++++---
 5 files changed, 53 insertions(+), 6 deletions(-)
xen/common/domain.c +15 -0
index e9be05f..5c9fef2 100644
@@ -695,6 +695,7 @@ int domain_kill(struct domain *d)
 {
     int rc = 0;
     struct vcpu *v;
+    struct domain *td;
 
     if ( d == current->domain )
         return -EINVAL;
@@ -733,6 +734,20 @@ int domain_kill(struct domain *d)
          * have to be put before we call put_domain. */
         vm_event_cleanup(d);
         put_domain(d);
+        /*
+         * XEN_DOMCTL_set_target implementation holds reference on
+         * target domain which doesn't allow to completely destroy it.
+         * Check if the reference are hold by someone and drop it
+         * when destroying target domain.
+         */
+        for_each_domain ( td ) {
+            if ( td->target == d ) {
+                td->target = NULL;
+                put_domain(d);
+                break;
+            }
+        }
+
         send_global_virq(VIRQ_DOM_EXC);
         /* fallthrough */
     case DOMDYING_dead:
xen/common/domctl.c +7 -1
index a69b3b5..079c7b0 100644
@@ -871,6 +871,12 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
         if ( (d == e) || (d->target != NULL) )
         {
             put_domain(e);
+            /*
+             * Be a little bit more polite here, looks like the emulator
+             * has just been restarted.
+             */
+            if ( d->target == e )
+                ret = 0;
             break;
         }
 
@@ -883,7 +889,7 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
             break;
         }
 
-        /* Hold reference on @e until we destroy @d. */
+        /* Hold reference on @e until we destroy either @d or @e */
         d->target = e;
         break;
     }
xen/common/event_channel.c +12 -2
index a8d182b5..2aa497a 100644
@@ -235,7 +235,12 @@ static long evtchn_alloc_unbound(evtchn_alloc_unbound_t *alloc)
         ERROR_EXIT_DOM(port, d);
     chn = evtchn_from_port(d, port);
 
-    rc = xsm_evtchn_unbound(XSM_TARGET, d, chn, alloc->remote_dom);
+    /*
+     * XXX: XSM_TARGET is not functional for emulator running in driver domain.
+     * See xsm_default_action for details. Probably XSM_DM_PRIV could work,
+     * but there is a risk to break other users.
+     */
+    rc = xsm_evtchn_unbound(XSM_HOOK, d, chn, alloc->remote_dom);
     if ( rc )
         goto out;
 
@@ -1218,7 +1223,12 @@ int alloc_unbound_xen_event_channel(
     port = rc;
     chn = evtchn_from_port(ld, port);
 
-    rc = xsm_evtchn_unbound(XSM_TARGET, ld, chn, remote_domid);
+    /*
+     * XXX: XSM_TARGET is not functional for emulator running in driver domain.
+     * See xsm_default_action for details. Probably XSM_DM_PRIV could work,
+     * but there is a risk to break other users.
+     */
+    rc = xsm_evtchn_unbound(XSM_HOOK, ld, chn, remote_domid);
     if ( rc )
         goto out;
 
xen/common/memory.c +6 -0
index 0000477..8b306f6 100644
@@ -1153,12 +1153,18 @@ static int acquire_resource(
         unsigned int i;
 
         /*
+         * XXX: Ugly hack for now to let emulator running in driver domain
+         * to succeeded in acquiring resource.
+         */
+#if 0
+        /*
          * FIXME: Until foreign pages inserted into the P2M are properly
          *        reference counted, it is unsafe to allow mapping of
          *        resource pages unless the caller is the hardware domain.
          */
         if ( !is_hardware_domain(currd) )
             return -EACCES;
+#endif
 
         if ( copy_from_guest(gfn_list, xmar.frame_list, xmar.nr_frames) )
             rc = -EFAULT;
xen/include/xsm/dummy.h +13 -3
index 317455a..c0813c0 100644
@@ -139,13 +139,23 @@ static XSM_INLINE int xsm_domctl(XSM_DEFAULT_ARG struct domain *d, int cmd)
     XSM_ASSERT_ACTION(XSM_OTHER);
     switch ( cmd )
     {
+    /*
+     * XXX: Emulator running in driver domain tries to get vcpus num.
+     * Probably we could avoid that change by modifying emulator to not use
+     * domctl for getting vcpus num.
+     */
+    case XEN_DOMCTL_getdomaininfo:
+    /*
+     * XXX: XSM_DM_PRIV is not functional for emulator running in driver domain
+     * without setting a target in advance. See xsm_default_action for details.
+     */
+    case XEN_DOMCTL_set_target:
+        return xsm_default_action(XSM_HOOK, current->domain, d);
     case XEN_DOMCTL_ioport_mapping:
     case XEN_DOMCTL_memory_mapping:
     case XEN_DOMCTL_bind_pt_irq:
     case XEN_DOMCTL_unbind_pt_irq:
         return xsm_default_action(XSM_DM_PRIV, current->domain, d);
-    case XEN_DOMCTL_getdomaininfo:
-        return xsm_default_action(XSM_XS_PRIV, current->domain, d);
     default:
         return xsm_default_action(XSM_PRIV, current->domain, d);
     }
@@ -275,7 +285,7 @@ static XSM_INLINE int xsm_claim_pages(XSM_DEFAULT_ARG struct domain *d)
 static XSM_INLINE int xsm_evtchn_unbound(XSM_DEFAULT_ARG struct domain *d, struct evtchn *chn,
                                          domid_t id2)
 {
-    XSM_ASSERT_ACTION(XSM_TARGET);
+    XSM_ASSERT_ACTION(XSM_HOOK);
     return xsm_default_action(action, current->domain, d);
 }
 
Signature · 1 line
2.7.4
#113 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

From going over the comments I can only derive you want to run an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Jan

#114 lore raw

RE: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 26 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

From going over the comments I can only derive you want to run an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Quoted 3 lines

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out). That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

Paul

author
#115 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

On 05.08.20 19:40, Paul Durrant wrote:

Hi Jan, Paul

Quoted 25 lines
Quoted 25 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

From going over the comments I can only derive you want to run
an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Quoted 4 lines
Quoted 3 lines

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out).That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

I may abuse "driver domain" terminology, but indeed in our use-case we pass through a set of H/W devices to a dedicated domain which is running the device drivers for that H/Ws. Our target system comprises a thin Dom0 (without H/W devices at all), DomD (which owns most of the H/W devices) and DomU which runs on virtual devices. This patch tries to make changes at Xen side to be able run standalone ioreq server (emulator) in that dedicated (driver?) domain. Actually the virtio-mmio PoC is based on IOREQ/DM features and emulator (based on demu) acting as a virtio-mmio backend. But it may be various use-cases for that (some mediator for sharing specific H/W resource between Guests or custom PCI emulator for example). If this is valid from Xen PoV I would be happy to get feedback how to transform tweaks (hacks) in current patch into the proper support.

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#116 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 35 lines · Oleksandr

On 05.08.20 19:40, Paul Durrant wrote:

Hi Jan, Paul

Quoted 25 lines
Quoted 25 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

From going over the comments I can only derive you want to run
an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Quoted 4 lines
Quoted 3 lines

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out).That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

Quoted 7 lines

I may abuse "driver domain" terminology, but indeed in our use-case we pass through a set of H/W devices to a dedicated domain which is running the device drivers for that H/Ws. Our target system comprises a thin Dom0 (without H/W devices at all), DomD (which owns most of the H/W devices) and DomU which runs on virtual devices. This patch tries to make changes at Xen side to be able run standalone ioreq server (emulator) in that dedicated (driver?) domain.

Okay, in which case I'm fine with the term. I simply wasn't aware of the targeted scenario, sorry.

Jan

author
#117 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Hello all.

Quoted 36 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

From going over the comments I can only derive you want to run
an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Quoted 8 lines
Quoted 1 line

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out).That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

I may abuse "driver domain" terminology, but indeed in our use-case we pass through a set of H/W devices to a dedicated domain which is running the device drivers for that H/Ws. Our target system comprises a thin Dom0 (without H/W devices at all), DomD (which owns most of the H/W devices) and DomU which runs on virtual devices. This patch tries to make changes at Xen side to be able run standalone ioreq server (emulator) in that dedicated (driver?) domain.

Quoted 2 lines

Okay, in which case I'm fine with the term. I simply wasn't aware of the targeted scenario, sorry.

May I kindly ask to suggest me the pointers how to *properly* resolve various policy related issues described in that patch? Without having them resolved it wouldn't be able to run standalone IOREQ server in driver domain.

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#118 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 40 lines · Oleksandr

Hello all.

Quoted 36 lines
Quoted 36 lines
Quoted 25 lines
Quoted 25 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

  From going over the comments I can only derive you want to run an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Quoted 4 lines
Quoted 3 lines

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out).That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

I may abuse "driver domain" terminology, but indeed in our use-case we pass through a set of H/W devices to a dedicated domain which is running the device drivers for that H/Ws. Our target system comprises a thin Dom0 (without H/W devices at all), DomD (which owns most of the H/W devices) and DomU which runs on virtual devices. This patch tries to make changes at Xen side to be able run standalone ioreq server (emulator) in that dedicated (driver?) domain.

Quoted 5 lines
Quoted 2 lines

Okay, in which case I'm fine with the term. I simply wasn't aware of the targeted scenario, sorry.

May I kindly ask to suggest me the pointers how to *properly* resolve various policy related issues described in that patch? Without having them resolved it wouldn't be able to run standalone IOREQ server in driver domain.

You could already do that by writing your own XSM policy. Did you explore it? If so, may I ask why this wouldn't be suitable?

Also, I would like to emphasis that because of XSA-295 (Unlimited Arm Atomics Operations), you can only run emulators in trusted domain on Arm.

There would be more work to do if you wanted to run them in non-trusted environment.

Cheers,

Signature · 1 line
Julien Grall
author
#119 lore raw

Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

On 16.08.20 18:36, Julien Grall wrote:

Hi Julien.

Quoted 47 lines · Oleksandr
Quoted 44 lines

Hello all.

Quoted 38 lines
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 05 August 2020 17:20
To: Oleksandr Tyshchenko <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Oleksandr Tyshchenko <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Stefano Stabellini <[email protected]>; Wei Liu <[email protected]>; Daniel De Graaf <[email protected]>
Subject: Re: [RFC PATCH V1 07/12] A collection of tweaks to be able to run emulator in driver domain

Quoted 7 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Trying to run emulator in driver domain I ran into various issues mostly policy-related. So this patch tries to resolve all them plobably in a hackish way. I would like to get feedback how to implement them properly as having an emulator in driver domain is a completely valid use-case.

  From going over the comments I can only derive you want to run an emulator in a driver domain, which doesn't really make sense to me. A driver domain has a different purpose after all. If instead you mean it to be run in just some other domain (which also isn't the domain controlling the target), then there may be more infrastructure changes needed.

Paul - was/is your standalone ioreq server (demu?) able to run in other than the domain controlling a guest?

Quoted 8 lines
Quoted 1 line

Not something I've done yet, but it was always part of the idea so that we could e.g. pass through a device to a dedicated domain and then run multiple demu instances there to virtualize it for many domUs. (I'm thinking here of a device that is not SR-IOV and hence would need some bespoke emulation code to share it out).That dedicated domain would be termed the 'driver domain' simply because it is running the device driver for the h/w that underpins the emulation.

I may abuse "driver domain" terminology, but indeed in our use-case we pass through a set of H/W devices to a dedicated domain which is running the device drivers for that H/Ws. Our target system comprises a thin Dom0 (without H/W devices at all), DomD (which owns most of the H/W devices) and DomU which runs on virtual devices. This patch tries to make changes at Xen side to be able run standalone ioreq server (emulator) in that dedicated (driver?) domain.

Okay, in which case I'm fine with the term. I simply wasn't aware of the targeted scenario, sorry.

Quoted 7 lines
Quoted 1 line

May I kindly ask to suggest me the pointers how to *properly* resolve various policy related issues described in that patch? Without having them resolved it wouldn't be able to run standalone IOREQ server in driver domain.

You could already do that by writing your own XSM policy. Did you explore it? If so, may I ask why this wouldn't be suitable?

Also, I would like to emphasis that because of XSA-295 (Unlimited Arm Atomics Operations), you can only run emulators in trusted domain on Arm.

There would be more work to do if you wanted to run them in non-trusted environment.

Thank you for the explanation. Yes, we consider driver domain as a trusted domain, there is no plan to run emulator in non-trusted domains. Indeed, it worth trying to write our own policy which will cover our use case (with emulator in driver domain) rather than tweak Xen's default one.

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#120 lore raw

RFC PATCH 8/12 xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Borrow x86's logic to invalidate qemu mapcache.

TODO: Move send_invalidate_req() to common code (ioreq.c?).

---
 xen/arch/arm/ioreq.c            | 14 ++++++++++++++
 xen/arch/arm/traps.c            |  6 ++++++
 xen/common/memory.c             |  6 ++++++
 xen/include/asm-arm/domain.h    |  2 ++
 xen/include/asm-arm/hvm/ioreq.h |  2 ++
 5 files changed, 30 insertions(+)
xen/arch/arm/ioreq.c +14 -0
index a9cc839..8f60c41 100644
@@ -75,6 +75,20 @@ bool handle_mmio(void)
     return true;
 }
 
+/* Ask ioemu mapcache to invalidate mappings. */
+void send_invalidate_req(void)
+{
+    ioreq_t p = {
+        .type = IOREQ_TYPE_INVALIDATE,
+        .size = 4,
+        .dir = IOREQ_WRITE,
+        .data = ~0UL, /* flush all */
+    };
+
+    if ( hvm_broadcast_ioreq(&p, false) != 0 )
+        gprintk(XENLOG_ERR, "Unsuccessful map-cache invalidate\n");
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/traps.c +6 -0
index 4cdf098..ea472d1 100644
@@ -1490,6 +1490,12 @@ static void do_trap_hypercall(struct cpu_user_regs *regs, register_t *nr,
     /* Ensure the hypercall trap instruction is re-executed. */
     if ( current->hcall_preempted )
         regs->pc -= 4;  /* re-execute 'hvc #XEN_HYPERCALL_TAG' */
+
+#ifdef CONFIG_IOREQ_SERVER
+    if ( unlikely(current->domain->arch.hvm.qemu_mapcache_invalidate) &&
+         test_and_clear_bool(current->domain->arch.hvm.qemu_mapcache_invalidate) )
+        send_invalidate_req();
+#endif
 }
 
 void arch_hypercall_tasklet_result(struct vcpu *v, long res)
xen/common/memory.c +6 -0
index 8b306f6..8d9f0a8 100644
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif
+
     return rc;
 }
 
xen/include/asm-arm/domain.h +2 -0
index e060b0a..0db8bb4 100644
@@ -69,6 +69,8 @@ struct hvm_domain
         spinlock_t              lock;
         struct hvm_ioreq_server *server[MAX_NR_IOREQ_SERVERS];
     } ioreq_server;
+
+    bool_t qemu_mapcache_invalidate;
 };
 
 #ifdef CONFIG_ARM_64
xen/include/asm-arm/hvm/ioreq.h +2 -0
index 83a560c..392ce64 100644
@@ -90,6 +90,8 @@ static inline void arch_hvm_ioreq_destroy(struct domain *d)
 #define IOREQ_IO_UNHANDLED   IO_UNHANDLED
 #define IOREQ_IO_RETRY       IO_RETRY
 
+void send_invalidate_req(void);
+
 #endif /* __ASM_X86_HVM_IOREQ_H__ */
 
 /*
Signature · 1 line
2.7.4
#121 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 11 lines · Oleksandr Tyshchenko
xen/common/memory.c +5 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         break;
     }
 
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Jan

#122 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

Hi Jan,

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksandr Tyshchenko
xen/common/memory.c +0 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          break;
      }
 
Quoted 8 lines

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 5 lines
xen/common/memory.c +5 -0
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Given that IOREQ is now moved under common/, I think it would make sense to have this set in common code as well for all the architecture.

IOW, I would suggest to drop the #ifdef CONFIG_ARM64. In addition, we may want to introduce an helper to check if a domain is using ioreq.

Cheers,

Signature · 1 line
Julien Grall
#123 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

Quoted 17 lines · Julien Grall
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Oleksandr Tyshchenko
xen/common/memory.c +0 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          break;
      }
 
Quoted 8 lines

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 5 lines
xen/common/memory.c +5 -0
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Quoted 5 lines

Given that IOREQ is now moved under common/, I think it would make sense to have this set in common code as well for all the architecture.

IOW, I would suggest to drop the #ifdef CONFIG_ARM64. In addition, we may want to introduce an helper to check if a domain is using ioreq.

Of course, with the (part of the) conditional dropped and the struct field moved out of the arch sub-struct, this is fine to live here.

Jan

author
#124 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

On 06.08.20 14:50, Jan Beulich wrote:

Hi Jan

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines · Oleksandr Tyshchenko

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 11 lines
xen/common/memory.c +5 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
           break;
       }
 
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Quoted 7 lines
Quoted 5 lines

Given that IOREQ is now moved under common/, I think it would make sense to have this set in common code as well for all the architecture.

IOW, I would suggest to drop the #ifdef CONFIG_ARM64. In addition, we may want to introduce an helper to check if a domain is using ioreq.

Of course, with the (part of the) conditional dropped and the struct field moved out of the arch sub-struct, this is fine to live here.

ok.

I suspect this should *also* live in compat_memory_op(). Please confirm whether my understanding correct.

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#125 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

Quoted 24 lines · Oleksandr

On 06.08.20 14:50, Jan Beulich wrote:

Hi Jan

Quoted 19 lines
Quoted 19 lines
Quoted 7 lines · Oleksandr Tyshchenko
Quoted 6 lines
xen/common/memory.c +0 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
           break;
       }
 
Quoted 7 lines

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 5 lines
xen/common/memory.c +5 -0
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Given that IOREQ is now moved under common/, I think it would make sense to have this set in common code as well for all the architecture.

IOW, I would suggest to drop the #ifdef CONFIG_ARM64. In addition, we may want to introduce an helper to check if a domain is using ioreq.

Quoted 8 lines
Quoted 2 lines

Of course, with the (part of the) conditional dropped and the struct field moved out of the arch sub-struct, this is fine to live here.

ok.

I suspect this should *also* live in compat_memory_op(). Please confirm whether my understanding correct.

Doesn't compat_memory_op() simply call here, so will have the flag set as needed?

Jan

author
#126 lore raw

Re: [RFC PATCH V1 08/12] xen/arm: Invalidate qemu mapcache on XENMEM_decrease_reservation

On 06.08.20 19:33, Jan Beulich wrote:

Hi Jan.

Quoted 26 lines · Oleksandr
Quoted 25 lines · Jan Beulich

Hi Jan

Quoted 21 lines
Quoted 14 lines
Quoted 7 lines · Oleksandr Tyshchenko
Quoted 6 lines
xen/common/memory.c +0 -0
@@ -1652,6 +1652,12 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
            break;
        }
 
Quoted 7 lines

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 1659

Quoted 5 lines
xen/common/memory.c +5 -0
+    /* x86 already sets the flag in hvm_memory_op() */
+#if defined(CONFIG_ARM64) && defined(CONFIG_IOREQ_SERVER)
+    if ( op == XENMEM_decrease_reservation )
+        curr_d->arch.hvm.qemu_mapcache_invalidate = true;
+#endif

Doesn't the comment already indicate a route towards an approach not requiring to alter common code?

Quoted 5 lines

Given that IOREQ is now moved under common/, I think it would make sense to have this set in common code as well for all the architecture.

IOW, I would suggest to drop the #ifdef CONFIG_ARM64. In addition, we may want to introduce an helper to check if a domain is using ioreq.

Of course, with the (part of the) conditional dropped and the struct field moved out of the arch sub-struct, this is fine to live here.

Quoted 7 lines
Quoted 5 lines

ok.

I suspect this should *also* live in compat_memory_op(). Please confirm whether my understanding correct.

Doesn't compat_memory_op() simply call here, so will have the flag set as needed?

Indeed, sorry for the noise.

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#127 lore raw

RFC PATCH 9/12 libxl: Handle virtio-mmio irq in more correct way

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch makes possible to use device passthrough again.

---
 tools/libxl/libxl_arm.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
tools/libxl/libxl_arm.c +23 -10
index 620b499..4f748e3 100644
@@ -9,6 +9,10 @@
 #include <assert.h>
 #include <xen/device_tree_defs.h>
 
+#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
+#define GUEST_VIRTIO_MMIO_SPI   33
+
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -27,8 +31,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0;
     unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq;
+    bool vuart_enabled = false, virtio_enabled = false;
 
     /*
      * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -40,6 +44,17 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    /*
+     * XXX: Handle properly virtio
+     * A proper solution would be the toolstack to allocate the interrupts
+     * used by each virtio backend and let the backend now which one is used
+     */
+    if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) {
+        nr_spis += (GUEST_VIRTIO_MMIO_SPI - 32) + 1;
+        virtio_irq = GUEST_VIRTIO_MMIO_SPI;
+        virtio_enabled = true;
+    }
+
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
         uint32_t spi;
@@ -59,6 +74,12 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return ERROR_FAIL;
         }
 
+        /* The same check as for vpl011 */
+        if (virtio_enabled && irq == virtio_irq) {
+            LOG(ERROR, "Physical IRQ %u conflicting with virtio SPI\n", irq);
+            return ERROR_FAIL;
+        }
+
         if (irq < 32)
             continue;
 
@@ -68,10 +89,6 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             nr_spis = spi + 1;
     }
 
-
-    /* XXX: Handle properly virtio */
-    nr_spis = 1;
-
     LOG(DEBUG, "Configure the domain");
 
     config->arch.nr_spis = nr_spis;
@@ -663,10 +680,6 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
-#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
-#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
-#define GUEST_VIRTIO_MMIO_SPI   33
-
 static int make_virtio_mmio_node(libxl__gc *gc, void *fdt)
 {
     int res;
Signature · 1 line
2.7.4
#128 lore raw

Re: [RFC PATCH V1 09/12] libxl: Handle virtio-mmio irq in more correct way

Quoted 14 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch makes possible to use device passthrough again.

---
 tools/libxl/libxl_arm.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)
tools/libxl/libxl_arm.c +0 -0
index 620b499..4f748e3 100644

tools/libxl/libxl_arm.c line 14

Quoted 6 lines
tools/libxl/libxl_arm.c +3 -0
@@ -9,6 +9,10 @@
 #include <assert.h>
 #include <xen/device_tree_defs.h>
 
+#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
+#define GUEST_VIRTIO_MMIO_SPI   33

They should be in xen/include/public/arch-arm.h

Is one interrupt enough if there are multiple virtio devices? Is it one interrupt for all virtio devices, or one for each device?

Of course this patch should be folded in the patch to add virtio support to libxl.

Quoted 70 lines
tools/libxl/libxl_arm.c +19 -10
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -27,8 +31,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0;
     unsigned int i;
-    uint32_t vuart_irq;
-    bool vuart_enabled = false;
+    uint32_t vuart_irq, virtio_irq;
+    bool vuart_enabled = false, virtio_enabled = false;
 
     /*
      * If pl011 vuart is enabled then increment the nr_spis to allow allocation
@@ -40,6 +44,17 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    /*
+     * XXX: Handle properly virtio
+     * A proper solution would be the toolstack to allocate the interrupts
+     * used by each virtio backend and let the backend now which one is used
+     */
+    if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) {
+        nr_spis += (GUEST_VIRTIO_MMIO_SPI - 32) + 1;
+        virtio_irq = GUEST_VIRTIO_MMIO_SPI;
+        virtio_enabled = true;
+    }
+
     for (i = 0; i < d_config->b_info.num_irqs; i++) {
         uint32_t irq = d_config->b_info.irqs[i];
         uint32_t spi;
@@ -59,6 +74,12 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return ERROR_FAIL;
         }
 
+        /* The same check as for vpl011 */
+        if (virtio_enabled && irq == virtio_irq) {
+            LOG(ERROR, "Physical IRQ %u conflicting with virtio SPI\n", irq);
+            return ERROR_FAIL;
+        }
+
         if (irq < 32)
             continue;
 
@@ -68,10 +89,6 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             nr_spis = spi + 1;
     }
 
-
-    /* XXX: Handle properly virtio */
-    nr_spis = 1;
-
     LOG(DEBUG, "Configure the domain");
 
     config->arch.nr_spis = nr_spis;
@@ -663,10 +680,6 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
-#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
-#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
-#define GUEST_VIRTIO_MMIO_SPI   33
-
 static int make_virtio_mmio_node(libxl__gc *gc, void *fdt)
 {
     int res;
Signature · 1 line
2.7.4
author
#129 lore raw

Re: [RFC PATCH V1 09/12] libxl: Handle virtio-mmio irq in more correct way

On 05.08.20 02:22, Stefano Stabellini wrote:

Hi Stefano

Quoted 18 lines · Oleksandr Tyshchenko
Quoted 17 lines

From: Oleksandr Tyshchenko <[email protected]>

This patch makes possible to use device passthrough again.

---
  tools/libxl/libxl_arm.c | 33 +++++++++++++++++++++++----------
  1 file changed, 23 insertions(+), 10 deletions(-)
tools/libxl/libxl_arm.c +0 -0
index 620b499..4f748e3 100644
@@ -9,6 +9,10 @@
  #include <assert.h>
  #include <xen/device_tree_defs.h>
 
Quoted 4 lines

tools/libxl/libxl_arm.c line 14

Quoted 3 lines
tools/libxl/libxl_arm.c +3 -0
+#define GUEST_VIRTIO_MMIO_BASE  xen_mk_ullong(0x02000000)
+#define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
+#define GUEST_VIRTIO_MMIO_SPI   33

They should be in xen/include/public/arch-arm.h

ok

Quoted 3 lines

Is one interrupt enough if there are multiple virtio devices? Is it one interrupt for all virtio devices, or one for each device?

  One interrupt for each virtio device. I experimented with current series and assigned 4 disk partitions to the guest. This resulted in 4 separate device-tree nodes, and each node had individual SPI and MMIO range.

Quoted 3 lines

Of course this patch should be folded in the patch to add virtio support to libxl.

ok

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#130 lore raw

RFC PATCH 10/12 libxl: Add support for virtio-disk configuration

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-disk backend (emualator) which is intended to run out of Qemu and could be run in any domain.

Xenstore was chosen as a communication interface for the emulator running in non-toolstack domain to be able to get configuration either by reading Xenstore directly or by receiving command line parameters (an updated 'xl devd' running in the same domain would read Xenstore beforehand and call backend executable with the required arguments).

An example of domain configuration (two disks are assigned to the guest, the latter is in readonly mode):

vdisk = [ 'backend=DomD, disks=rw:/dev/mmcblk0p3;ro:/dev/mmcblk1p3' ]

Where per-disk Xenstore entries are:
- filename and readonly flag (configured via "vdisk" property)
- base and irq (allocated dynamically)

Besides handling 'visible' params described in configuration file, patch also allocates virtio-mmio specific ones for each device and writes them into Xenstore. virtio-mmio params (irq and base) are unique per guest domain, they allocated at the domain creation time and passed through to the emulator. Each VirtIO device has at least one pair of these params.

TODO:
1. An extra "virtio" property could be removed.
2. Update documentation.

---
 tools/libxl/Makefile                 |   4 +-
 tools/libxl/libxl_arm.c              |  63 +++++++++++++++----
 tools/libxl/libxl_create.c           |   1 +
 tools/libxl/libxl_internal.h         |   1 +
 tools/libxl/libxl_types.idl          |  15 +++++
 tools/libxl/libxl_types_internal.idl |   1 +
 tools/libxl/libxl_virtio_disk.c      | 109 +++++++++++++++++++++++++++++++++
 tools/xl/Makefile                    |   2 +-
 tools/xl/xl.h                        |   3 +
 tools/xl/xl_cmdtable.c               |  15 +++++
 tools/xl/xl_parse.c                  | 115 +++++++++++++++++++++++++++++++++++
 tools/xl/xl_virtio_disk.c            |  46 ++++++++++++++
 12 files changed, 360 insertions(+), 15 deletions(-)
 create mode 100644 tools/libxl/libxl_virtio_disk.c
 create mode 100644 tools/xl/xl_virtio_disk.c
tools/libxl/Makefile +3 -1
index 38cd43a..df94b13 100644
@@ -141,7 +141,9 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \
 			libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \
 			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
-			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y)
+			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o \
+			libxl_virtio_disk.o $(LIBXL_OBJS-y)
+
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
tools/libxl/libxl_arm.c +50 -13

103 lines not on this page · raw message

tools/libxl/libxl_create.c +1 -0
index 2814818..8a0651e 100644
@@ -1817,6 +1817,7 @@ const libxl__device_type *device_type_tbl[] = {
     &libxl__dtdev_devtype,
     &libxl__vdispl_devtype,
     &libxl__vsnd_devtype,
+    &libxl__virtio_disk_devtype,
     NULL
 };
 
tools/libxl/libxl_internal.h +1 -0
index 94a2317..4e2024d 100644
@@ -3988,6 +3988,7 @@ extern const libxl__device_type libxl__vdispl_devtype;
 extern const libxl__device_type libxl__p9_devtype;
 extern const libxl__device_type libxl__pvcallsif_devtype;
 extern const libxl__device_type libxl__vsnd_devtype;
+extern const libxl__device_type libxl__virtio_disk_devtype;
 
 extern const libxl__device_type *device_type_tbl[];
 
tools/libxl/libxl_types.idl +15 -0
index b054bf9..5f8a3ff 100644
@@ -935,6 +935,20 @@ libxl_device_vsnd = Struct("device_vsnd", [
     ("pcms", Array(libxl_vsnd_pcm, "num_vsnd_pcms"))
     ])
 
+libxl_virtio_disk_param = Struct("virtio_disk_param", [
+    ("filename", string),
+    ("readonly", bool),
+    ("irq", uint32),
+    ("base", uint64),
+    ])
+
+libxl_device_virtio_disk = Struct("device_virtio_disk", [
+    ("backend_domid", libxl_domid),
+    ("backend_domname", string),
+    ("devid", libxl_devid),
+    ("disks", Array(libxl_virtio_disk_param, "num_disks")),
+    ])
+
 libxl_domain_config = Struct("domain_config", [
     ("c_info", libxl_domain_create_info),
     ("b_info", libxl_domain_build_info),
@@ -951,6 +965,7 @@ libxl_domain_config = Struct("domain_config", [
     ("pvcallsifs", Array(libxl_device_pvcallsif, "num_pvcallsifs")),
     ("vdispls", Array(libxl_device_vdispl, "num_vdispls")),
     ("vsnds", Array(libxl_device_vsnd, "num_vsnds")),
+    ("virtio_disks", Array(libxl_device_virtio_disk, "num_virtio_disks")),
     # a channel manifests as a console with a name,
     # see docs/misc/channels.txt
     ("channels", Array(libxl_device_channel, "num_channels")),
tools/libxl/libxl_types_internal.idl +1 -0
index 3593e21..8f71980 100644
@@ -32,6 +32,7 @@ libxl__device_kind = Enumeration("device_kind", [
     (14, "PVCALLS"),
     (15, "VSND"),
     (16, "VINPUT"),
+    (17, "VIRTIO_DISK"),
     ])
 
 libxl__console_backend = Enumeration("console_backend", [
tools/libxl/libxl_virtio_disk.c new file +109 -0

109 lines not on this page · raw message

tools/xl/Makefile +1 -1
index af4912e..38e4701 100644
@@ -22,7 +22,7 @@ XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
 XL_OBJS += xl_info.o xl_console.o xl_misc.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
-XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o
+XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o xl_virtio_disk.o
 
 $(XL_OBJS): CFLAGS += $(CFLAGS_libxentoollog)
 $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
tools/xl/xl.h +3 -0
index 06569c6..3d26f19 100644
@@ -178,6 +178,9 @@ int main_vsnddetach(int argc, char **argv);
 int main_vkbattach(int argc, char **argv);
 int main_vkblist(int argc, char **argv);
 int main_vkbdetach(int argc, char **argv);
+int main_virtio_diskattach(int argc, char **argv);
+int main_virtio_disklist(int argc, char **argv);
+int main_virtio_diskdetach(int argc, char **argv);
 int main_usbctrl_attach(int argc, char **argv);
 int main_usbctrl_detach(int argc, char **argv);
 int main_usbdev_attach(int argc, char **argv);
tools/xl/xl_cmdtable.c +15 -0
index 0833539..2bdf0b7 100644
@@ -434,6 +434,21 @@ struct cmd_spec cmd_table[] = {
       "Destroy a domain's virtual sound device",
       "<Domain> <DevId>",
     },
+    { "virtio-disk-attach",
+      &main_virtio_diskattach, 1, 1,
+      "Create a new virtio block device",
+      " TBD\n"
+    },
+    { "virtio-disk-list",
+      &main_virtio_disklist, 0, 0,
+      "List virtio block devices for a domain",
+      "<Domain(s)>",
+    },
+    { "virtio-disk-detach",
+      &main_virtio_diskdetach, 0, 1,
+      "Destroy a domain's virtio block device",
+      "<Domain> <DevId>",
+    },
     { "uptime",
       &main_uptime, 0, 0,
       "Print uptime for all/some domains",
tools/xl/xl_parse.c +115 -0

127 lines not on this page · raw message

tools/xl/xl_virtio_disk.c new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.7.4
#131 lore raw

Re: [RFC PATCH V1 10/12] libxl: Add support for virtio-disk configuration

Quoted 108 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-disk backend (emualator) which is intended to run out of Qemu and could be run in any domain.

Xenstore was chosen as a communication interface for the emulator running in non-toolstack domain to be able to get configuration either by reading Xenstore directly or by receiving command line parameters (an updated 'xl devd' running in the same domain would read Xenstore beforehand and call backend executable with the required arguments).

An example of domain configuration (two disks are assigned to the guest, the latter is in readonly mode):

vdisk = [ 'backend=DomD, disks=rw:/dev/mmcblk0p3;ro:/dev/mmcblk1p3' ]

Where per-disk Xenstore entries are:
- filename and readonly flag (configured via "vdisk" property)
- base and irq (allocated dynamically)

Besides handling 'visible' params described in configuration file, patch also allocates virtio-mmio specific ones for each device and writes them into Xenstore. virtio-mmio params (irq and base) are unique per guest domain, they allocated at the domain creation time and passed through to the emulator. Each VirtIO device has at least one pair of these params.

TODO:
1. An extra "virtio" property could be removed.
2. Update documentation.

---
 tools/libxl/Makefile                 |   4 +-
 tools/libxl/libxl_arm.c              |  63 +++++++++++++++----
 tools/libxl/libxl_create.c           |   1 +
 tools/libxl/libxl_internal.h         |   1 +
 tools/libxl/libxl_types.idl          |  15 +++++
 tools/libxl/libxl_types_internal.idl |   1 +
 tools/libxl/libxl_virtio_disk.c      | 109 +++++++++++++++++++++++++++++++++
 tools/xl/Makefile                    |   2 +-
 tools/xl/xl.h                        |   3 +
 tools/xl/xl_cmdtable.c               |  15 +++++
 tools/xl/xl_parse.c                  | 115 +++++++++++++++++++++++++++++++++++
 tools/xl/xl_virtio_disk.c            |  46 ++++++++++++++
 12 files changed, 360 insertions(+), 15 deletions(-)
 create mode 100644 tools/libxl/libxl_virtio_disk.c
 create mode 100644 tools/xl/xl_virtio_disk.c
tools/libxl/Makefile +3 -1
index 38cd43a..df94b13 100644
@@ -141,7 +141,9 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
 			libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \
 			libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \
 			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
-			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y)
+			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o \
+			libxl_virtio_disk.o $(LIBXL_OBJS-y)
+
 LIBXL_OBJS += libxl_genid.o
 LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
tools/libxl/libxl_arm.c +20 -6
index 4f748e3..469a8b0 100644
@@ -13,6 +13,12 @@
 #define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
 #define GUEST_VIRTIO_MMIO_SPI   33
 
+#ifndef container_of
+#define container_of(ptr, type, member) ({			\
+        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+#endif
+
 static const char *gicv_to_string(libxl_gic_version gic_version)
 {
     switch (gic_version) {
@@ -44,14 +50,32 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
-    /*
-     * XXX: Handle properly virtio
-     * A proper solution would be the toolstack to allocate the interrupts
-     * used by each virtio backend and let the backend now which one is used
-     */
     if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) {
-        nr_spis += (GUEST_VIRTIO_MMIO_SPI - 32) + 1;
+        uint64_t virtio_base;
+        libxl_device_virtio_disk *virtio_disk;
+
+        virtio_base = GUEST_VIRTIO_MMIO_BASE;
         virtio_irq = GUEST_VIRTIO_MMIO_SPI;
+
+        if (!d_config->num_virtio_disks) {
+            LOG(ERROR, "Virtio is enabled, but no Virtio devices present\n");
+            return ERROR_FAIL;
+        }
+        virtio_disk = &d_config->virtio_disks[0];
+
+        for (i = 0; i < virtio_disk->num_disks; i++) {
+            virtio_disk->disks[i].base = virtio_base;
+            virtio_disk->disks[i].irq = virtio_irq;

tools/libxl/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 78

Quoted 10 lines
tools/libxl/libxl_arm.c +10 -0
+
+            LOG(DEBUG, "Allocate Virtio MMIO params: IRQ %u BASE 0x%"PRIx64,
+                virtio_irq, virtio_base);
+
+            virtio_irq ++;
+            virtio_base += GUEST_VIRTIO_MMIO_SIZE;
+        }
+        virtio_irq --;
+
+        nr_spis += (virtio_irq - 32) + 1;

It looks like it is an interrupt per device, which could lead to quite a few of them being allocated.

The issue is that today we don't really handle virtual interrupts different from physical interrupts in Xen. So, if we end up allocating let's say 6 virtio interrupts for a domain, the chance of a clash with a physical interrupt of a passthrough device is real.

I am not entirely sure how to solve it, but these are a few ideas:
- choosing virtio interrupts that are less likely to conflict (maybe > 1000)
- make the virtio irq (optionally) configurable so that a user could override the default irq and specify one that doesn't conflict
- implementing support for virq != pirq (even the xl interface doesn't allow to specify the virq number for passthrough devices, see "irqs")

author
#132 lore raw

Re: [RFC PATCH V1 10/12] libxl: Add support for virtio-disk configuration

On 05.08.20 02:23, Stefano Stabellini wrote:

Hi Stefano

Quoted 113 lines · Oleksandr Tyshchenko
Quoted 112 lines

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-disk backend (emualator) which is intended to run out of Qemu and could be run in any domain.

Xenstore was chosen as a communication interface for the emulator running in non-toolstack domain to be able to get configuration either by reading Xenstore directly or by receiving command line parameters (an updated 'xl devd' running in the same domain would read Xenstore beforehand and call backend executable with the required arguments).

An example of domain configuration (two disks are assigned to the guest, the latter is in readonly mode):

vdisk = [ 'backend=DomD, disks=rw:/dev/mmcblk0p3;ro:/dev/mmcblk1p3' ]

Where per-disk Xenstore entries are:
- filename and readonly flag (configured via "vdisk" property)
- base and irq (allocated dynamically)

Besides handling 'visible' params described in configuration file, patch also allocates virtio-mmio specific ones for each device and writes them into Xenstore. virtio-mmio params (irq and base) are unique per guest domain, they allocated at the domain creation time and passed through to the emulator. Each VirtIO device has at least one pair of these params.

TODO:
1. An extra "virtio" property could be removed.
2. Update documentation.

---
  tools/libxl/Makefile                 |   4 +-
  tools/libxl/libxl_arm.c              |  63 +++++++++++++++----
  tools/libxl/libxl_create.c           |   1 +
  tools/libxl/libxl_internal.h         |   1 +
  tools/libxl/libxl_types.idl          |  15 +++++
  tools/libxl/libxl_types_internal.idl |   1 +
  tools/libxl/libxl_virtio_disk.c      | 109 +++++++++++++++++++++++++++++++++
  tools/xl/Makefile                    |   2 +-
  tools/xl/xl.h                        |   3 +
  tools/xl/xl_cmdtable.c               |  15 +++++
  tools/xl/xl_parse.c                  | 115 +++++++++++++++++++++++++++++++++++
  tools/xl/xl_virtio_disk.c            |  46 ++++++++++++++
  12 files changed, 360 insertions(+), 15 deletions(-)

create mode 100644 tools/libxl/libxl_virtio_disk.c create mode 100644 tools/xl/xl_virtio_disk.c

tools/libxl/Makefile +3 -1
index 38cd43a..df94b13 100644
@@ -141,7 +141,9 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \
  			libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o \
  			libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o \
  			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
-			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o $(LIBXL_OBJS-y)
+			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o \
+			libxl_virtio_disk.o $(LIBXL_OBJS-y)
+
  LIBXL_OBJS += libxl_genid.o
  LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
 
tools/libxl/libxl_arm.c +24 -6
index 4f748e3..469a8b0 100644
@@ -13,6 +13,12 @@
  #define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
  #define GUEST_VIRTIO_MMIO_SPI   33
 
+#ifndef container_of
+#define container_of(ptr, type, member) ({			\
+        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+#endif
+
  static const char *gicv_to_string(libxl_gic_version gic_version)
  {
      switch (gic_version) {
@@ -44,14 +50,32 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
          vuart_enabled = true;
      }
 
-    /*
-     * XXX: Handle properly virtio
-     * A proper solution would be the toolstack to allocate the interrupts
-     * used by each virtio backend and let the backend now which one is used
-     */
      if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) {
-        nr_spis += (GUEST_VIRTIO_MMIO_SPI - 32) + 1;
+        uint64_t virtio_base;
+        libxl_device_virtio_disk *virtio_disk;
+
+        virtio_base = GUEST_VIRTIO_MMIO_BASE;
          virtio_irq = GUEST_VIRTIO_MMIO_SPI;
+
+        if (!d_config->num_virtio_disks) {
+            LOG(ERROR, "Virtio is enabled, but no Virtio devices present\n");
+            return ERROR_FAIL;
+        }
+        virtio_disk = &d_config->virtio_disks[0];
+
+        for (i = 0; i < virtio_disk->num_disks; i++) {
+            virtio_disk->disks[i].base = virtio_base;
+            virtio_disk->disks[i].irq = virtio_irq;
+
+            LOG(DEBUG, "Allocate Virtio MMIO params: IRQ %u BASE 0x%"PRIx64,
+                virtio_irq, virtio_base);
+
Quoted 8 lines

tools/libxl/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 78

Quoted 6 lines
tools/libxl/libxl_arm.c +6 -0
+            virtio_irq ++;
+            virtio_base += GUEST_VIRTIO_MMIO_SIZE;
+        }
+        virtio_irq --;
+
+        nr_spis += (virtio_irq - 32) + 1;

It looks like it is an interrupt per device, which could lead to quite a few of them being allocated.

Yes.

Quoted 3 lines

So, if we end up allocating
let's say 6 virtio interrupts for a domain, the chance of a clash with a physical interrupt of a passthrough device is real.

Yes.

Quoted 8 lines

I am not entirely sure how to solve it, but these are a few ideas:
- choosing virtio interrupts that are less likely to conflict (maybe > 1000)
- make the virtio irq (optionally) configurable so that a user could override the default irq and specify one that doesn't conflict
- implementing support for virq != pirq (even the xl interface doesn't allow to specify the virq number for passthrough devices, see "irqs")

Good ideas. The first requires minimum effort. Couldn't we choose virtio interrupt to allocate after making sure it is absent in guest "irqs" (d_config->b_info.irqs[i]), I mean to find some holes?

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#133 lore raw

Re: [RFC PATCH V1 10/12] libxl: Add support for virtio-disk configuration

Quoted 150 lines · Oleksandr
Quoted 121 lines · Stefano Stabellini
Quoted 120 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

This patch adds basic support for configuring and assisting virtio-disk backend (emualator) which is intended to run out of Qemu and could be run in any domain.

Xenstore was chosen as a communication interface for the emulator running in non-toolstack domain to be able to get configuration either by reading Xenstore directly or by receiving command line parameters (an updated 'xl devd'
running in the same domain would read Xenstore beforehand and call backend executable with the required arguments).

An example of domain configuration (two disks are assigned to the guest, the latter is in readonly mode):

vdisk = [ 'backend=DomD, disks=rw:/dev/mmcblk0p3;ro:/dev/mmcblk1p3' ]

Where per-disk Xenstore entries are:
- filename and readonly flag (configured via "vdisk" property)
- base and irq (allocated dynamically)

Besides handling 'visible' params described in configuration file, patch also allocates virtio-mmio specific ones for each device and writes them into Xenstore. virtio-mmio params (irq and base) are unique per guest domain, they allocated at the domain creation time and passed through to the emulator. Each VirtIO device has at least one pair of these params.

TODO:
1. An extra "virtio" property could be removed.
2. Update documentation.

---
  tools/libxl/Makefile                 |   4 +-
  tools/libxl/libxl_arm.c              |  63 +++++++++++++++----
  tools/libxl/libxl_create.c           |   1 +
  tools/libxl/libxl_internal.h         |   1 +
  tools/libxl/libxl_types.idl          |  15 +++++
  tools/libxl/libxl_types_internal.idl |   1 +
  tools/libxl/libxl_virtio_disk.c      | 109
+++++++++++++++++++++++++++++++++
  tools/xl/Makefile                    |   2 +-
  tools/xl/xl.h                        |   3 +
  tools/xl/xl_cmdtable.c               |  15 +++++
  tools/xl/xl_parse.c                  | 115
+++++++++++++++++++++++++++++++++++
  tools/xl/xl_virtio_disk.c            |  46 ++++++++++++++
  12 files changed, 360 insertions(+), 15 deletions(-)

create mode 100644 tools/libxl/libxl_virtio_disk.c create mode 100644 tools/xl/xl_virtio_disk.c

tools/libxl/Makefile +0 -1
index 38cd43a..df94b13 100644
@@ -141,7 +141,9 @@ LIBXL_OBJS = flexarray.o libxl.o libxl_create.o
 libxl_dm.o libxl_pci.o \
  			libxl_vtpm.o libxl_nic.o libxl_disk.o libxl_console.o
 \
  			libxl_cpupool.o libxl_mem.o libxl_sched.o libxl_tmem.o
 \
  			libxl_9pfs.o libxl_domain.o libxl_vdispl.o \
-			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o

$(LIBXL_OBJS-y)

tools/libxl/Makefile +3 -0
+			libxl_pvcalls.o libxl_vsnd.o libxl_vkb.o \
+			libxl_virtio_disk.o $(LIBXL_OBJS-y)
+
  LIBXL_OBJS += libxl_genid.o
  LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o
  diff --git a/tools/libxl/libxl_arm.c b/tools/libxl/libxl_arm.c

index 4f748e3..469a8b0 100644

tools/libxl/libxl_arm.c +5 -2
@@ -13,6 +13,12 @@
  #define GUEST_VIRTIO_MMIO_SIZE  xen_mk_ullong(0x200)
  #define GUEST_VIRTIO_MMIO_SPI   33
  +#ifndef container_of
+#define container_of(ptr, type, member) ({			\
+        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        (type *)( (char *)__mptr - offsetof(type,member) );})
+#endif
+
  static const char *gicv_to_string(libxl_gic_version gic_version)
  {
      switch (gic_version) {
@@ -44,14 +50,32 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
          vuart_enabled = true;
      }
  -    /*
-     * XXX: Handle properly virtio
-     * A proper solution would be the toolstack to allocate the

interrupts
- * used by each virtio backend and let the backend now which one is
used

tools/libxl/libxl_arm.c +7 -2
-     */
      if (libxl_defbool_val(d_config->b_info.arch_arm.virtio)) {
-        nr_spis += (GUEST_VIRTIO_MMIO_SPI - 32) + 1;
+        uint64_t virtio_base;
+        libxl_device_virtio_disk *virtio_disk;
+
+        virtio_base = GUEST_VIRTIO_MMIO_BASE;
          virtio_irq = GUEST_VIRTIO_MMIO_SPI;
+
+        if (!d_config->num_virtio_disks) {
+            LOG(ERROR, "Virtio is enabled, but no Virtio devices

present\n");

tools/libxl/libxl_arm.c +9 -0
+            return ERROR_FAIL;
+        }
+        virtio_disk = &d_config->virtio_disks[0];
+
+        for (i = 0; i < virtio_disk->num_disks; i++) {
+            virtio_disk->disks[i].base = virtio_base;
+            virtio_disk->disks[i].irq = virtio_irq;
+
+            LOG(DEBUG, "Allocate Virtio MMIO params: IRQ %u BASE

0x%"PRIx64,

tools/libxl/libxl_arm.c +2 -0
+                virtio_irq, virtio_base);
+
Quoted 8 lines

tools/libxl/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 78

Quoted 6 lines
tools/libxl/libxl_arm.c +6 -0
+            virtio_irq ++;
+            virtio_base += GUEST_VIRTIO_MMIO_SIZE;
+        }
+        virtio_irq --;
+
+        nr_spis += (virtio_irq - 32) + 1;

It looks like it is an interrupt per device, which could lead to quite a few of them being allocated.

Yes.

Quoted 3 lines

So, if we end up allocating
let's say 6 virtio interrupts for a domain, the chance of a clash with a physical interrupt of a passthrough device is real.

Yes.

Quoted 8 lines

I am not entirely sure how to solve it, but these are a few ideas:
- choosing virtio interrupts that are less likely to conflict (maybe > 1000)
- make the virtio irq (optionally) configurable so that a user could override the default irq and specify one that doesn't conflict
- implementing support for virq != pirq (even the xl interface doesn't allow to specify the virq number for passthrough devices, see "irqs")

Quoted 3 lines

Good ideas. The first requires minimum effort. Couldn't we choose virtio interrupt to allocate after making sure it is absent in guest "irqs" (d_config->b_info.irqs[i]), I mean to find some holes?

Yes, that might be possible too.

So far we have tried to stay away from dynamic irq allocation for guests virtual devices, but we also didn't have to deal with potentially large amount of them :-)

#134 lore raw

RFC PATCH 11/12 libxl: Insert "dma-coherent" property into virtio-mmio device node

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Without "dma-coherent" property present in virtio-mmio device node, guest assumes it is non-coherent and making non-cacheable accesses to the vring when the DMA API is used for vring operations. But virtio-mmio device which runs at the host size is making cacheable accesses to vring. This all may result in a loss of coherency between the guest and host.

With this patch we can avoid modifying guest at all, otherwise we need to force VirtIO framework to not use DMA API for vring operations.

---
 tools/libxl/libxl_arm.c | 3 +++
 1 file changed, 3 insertions(+)
tools/libxl/libxl_arm.c +3 -0
index 469a8b0..a68fb14 100644
@@ -726,6 +726,9 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
     res = fdt_property_interrupts(gc, fdt, &intr, 1);
     if (res) return res;
 
+    res = fdt_property(fdt, "dma-coherent", NULL, 0);
+    if (res) return res;
+
     res = fdt_end_node(fdt);
     if (res) return res;
 
Signature · 1 line
2.7.4
#135 lore raw

Re: [RFC PATCH V1 11/12] libxl: Insert "dma-coherent" property into virtio-mmio device node

Quoted 9 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Without "dma-coherent" property present in virtio-mmio device node, guest assumes it is non-coherent and making non-cacheable accesses to the vring when the DMA API is used for vring operations. But virtio-mmio device which runs at the host size is making cacheable accesses to vring. This all may result in a loss of coherency between the guest and host.

Quoted 4 lines

With this patch we can avoid modifying guest at all, otherwise we need to force VirtIO framework to not use DMA API for vring operations.

This should also be folded in the first patch for libxl

Quoted 21 lines
---
 tools/libxl/libxl_arm.c | 3 +++
 1 file changed, 3 insertions(+)
tools/libxl/libxl_arm.c +3 -0
index 469a8b0..a68fb14 100644
@@ -726,6 +726,9 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
     res = fdt_property_interrupts(gc, fdt, &intr, 1);
     if (res) return res;
 
+    res = fdt_property(fdt, "dma-coherent", NULL, 0);
+    if (res) return res;
+
     res = fdt_end_node(fdt);
     if (res) return res;
 
Signature · 1 line
2.7.4
author
#136 lore raw

Re: [RFC PATCH V1 11/12] libxl: Insert "dma-coherent" property into virtio-mmio device node

On 05.08.20 02:23, Stefano Stabellini wrote:

Hi Stefano

Quoted 10 lines · Oleksandr Tyshchenko
Quoted 9 lines

From: Oleksandr Tyshchenko <[email protected]>

Without "dma-coherent" property present in virtio-mmio device node, guest assumes it is non-coherent and making non-cacheable accesses to the vring when the DMA API is used for vring operations. But virtio-mmio device which runs at the host size is making cacheable accesses to vring. This all may result in a loss of coherency between the guest and host.

Quoted 5 lines
Quoted 4 lines

With this patch we can avoid modifying guest at all, otherwise we need to force VirtIO framework to not use DMA API for vring operations.

This should also be folded in the first patch for libxl

Agree, will do

Signature · 3 lines
Regards,

Oleksandr Tyshchenko
#137 lore raw

RFC PATCH 12/12 libxl: Fix duplicate memory node in DT

In reply to Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

When two or more VirtIO devices are passed to DomU the following message is observed:
OF: Duplicate name in base, renamed to "virtio#1"

---
 tools/libxl/libxl_arm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/libxl/libxl_arm.c +4 -2
index a68fb14..9671a44 100644
@@ -710,9 +710,11 @@ static int make_virtio_mmio_node(libxl__gc *gc, void *fdt,
 {
     int res;
     gic_interrupt intr;
+    /* Placeholder for virtio@ + a 64-bit number + \0 */
+    char buf[24];
 
-    /* XXX: Add address in the node name */
-    res = fdt_begin_node(fdt, "virtio");
+    snprintf(buf, sizeof(buf), "virtio@%"PRIx64, base);
+    res = fdt_begin_node(fdt, buf);
     if (res) return res;
 
     res = fdt_property_compat(gc, fdt, 1, "virtio,mmio");
Signature · 1 line
2.7.4
#138 lore raw
In reply to Oleksandr Tyshchenko

Hi Oleksandr,

Quoted 46 lines · Oleksandr Tyshchenko

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

The purpose of this patch series is to add IOREQ/DM support to Xen on Arm. You can find an initial discussion at [1]. Xen on Arm requires some implementation to forward guest MMIO access to a device model in order to implement virtio-mmio backend or even mediator outside of hypervisor. As Xen on x86 already contains required support this patch series tries to make it common and introduce Arm specific bits plus some new functionality. Patch series is based on Julien's PoC "xen/arm: Add support for Guest IO forwarding to a device emulator". Besides splitting existing IOREQ/DM support and introducing Arm side, the patch series also includes virtio-mmio related changes (toolstack) for the reviewers to be able to see how the whole picture could look like. For a non-RFC, the IOREQ/DM and virtio-mmio support will be sent separately.

According to the initial discussion there are a few open questions/concerns regarding security, performance in VirtIO solution:
1. virtio-mmio vs virtio-pci, SPI vs MSI, different use-cases require different transport...
2. virtio backend is able to access all guest memory, some kind of protection is needed: 'virtio-iommu in Xen' vs 'pre-shared-memory & memcpys in guest'
3. interface between toolstack and 'out-of-qemu' virtio backend, avoid using Xenstore in virtio backend if possible.
4. a lot of 'foreing mapping' could lead to the memory exhaustion, Julien has some idea regarding that.

Looks like all of them are valid and worth considering, but the first thing which we need on Arm is a mechanism to forward guest IO to a device emulator, so let's focus on it in the first place.

***

Patch series [2] was rebased on Xen v4.14 release and tested on Renesas Salvator-X board + H3 ES3.0 SoC (Arm64) with virtio-mmio disk backend (we will share it later) running in driver domain and unmodified Linux Guest running on existing virtio-blk driver (frontend). No issues were observed. Guest domain 'reboot/destroy' use-cases work properly. Patch series was only build-tested on x86.

Please note, build-test passed for the following modes:
1. x86: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y (default)
2. x86: #CONFIG_HVM is not set / #CONFIG_IOREQ_SERVER is not set
3. Arm64: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y (default)
4. Arm64: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set
5. Arm32: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set

Quoted 2 lines

Build-test didn't pass for Arm32 mode with 'CONFIG_IOREQ_SERVER=y' due to the lack of cmpxchg_64 support on Arm32. See cmpxchg usage in hvm_send_buffered_ioreq()).

I have sent a patch to implement cmpxchg64() and guest_cmpxchg64() (see [1]).

Cheers,

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#u

Signature · 1 line
Julien Grall
author
#139 lore raw
Quoted 1 line · Julien Grall

Hi Oleksandr,

Hi Julien.

Quoted 48 lines · Oleksandr Tyshchenko
Quoted 46 lines

From: Oleksandr Tyshchenko <[email protected]>

Hello all.

The purpose of this patch series is to add IOREQ/DM support to Xen on Arm. You can find an initial discussion at [1]. Xen on Arm requires some implementation to forward guest MMIO access to a device model in order to implement virtio-mmio backend or even mediator outside of hypervisor. As Xen on x86 already contains required support this patch series tries to make it common and introduce Arm specific bits plus some new functionality. Patch series is based on Julien's PoC "xen/arm: Add support for Guest IO forwarding to a device emulator". Besides splitting existing IOREQ/DM support and introducing Arm side, the patch series also includes virtio-mmio related changes (toolstack) for the reviewers to be able to see how the whole picture could look like. For a non-RFC, the IOREQ/DM and virtio-mmio support will be sent separately.

According to the initial discussion there are a few open questions/concerns regarding security, performance in VirtIO solution:
1. virtio-mmio vs virtio-pci, SPI vs MSI, different use-cases require different transport...
2. virtio backend is able to access all guest memory, some kind of protection is needed: 'virtio-iommu in Xen' vs 'pre-shared-memory & memcpys in guest'
3. interface between toolstack and 'out-of-qemu' virtio backend, avoid using Xenstore in virtio backend if possible.
4. a lot of 'foreing mapping' could lead to the memory exhaustion, Julien has some idea regarding that.

Looks like all of them are valid and worth considering, but the first thing which we need on Arm is a mechanism to forward guest IO to a device emulator, so let's focus on it in the first place.

***

Patch series [2] was rebased on Xen v4.14 release and tested on Renesas Salvator-X board + H3 ES3.0 SoC (Arm64) with virtio-mmio disk backend (we will share it later) running in driver domain and unmodified Linux Guest running on existing virtio-blk driver (frontend). No issues were observed. Guest domain 'reboot/destroy' use-cases work properly. Patch series was only build-tested on x86.

Please note, build-test passed for the following modes:
1. x86: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y (default)
2. x86: #CONFIG_HVM is not set / #CONFIG_IOREQ_SERVER is not set
3. Arm64: CONFIG_HVM=y / CONFIG_IOREQ_SERVER=y (default)
4. Arm64: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set
5. Arm32: CONFIG_HVM=y / #CONFIG_IOREQ_SERVER is not set

Quoted 8 lines
Quoted 2 lines

Build-test didn't pass for Arm32 mode with 'CONFIG_IOREQ_SERVER=y' due to the lack of cmpxchg_64 support on Arm32. See cmpxchg usage in hvm_send_buffered_ioreq()).

I have sent a patch to implement cmpxchg64() and guest_cmpxchg64() (see [1]).

Cheers,

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#u

 Thank you! I have already build-tested it. No issues). I will update corresponding patch to select IOREQ_SERVER for "config ARM" instead of "config ARM64".

Signature · 3 lines
Regards,

Oleksandr Tyshchenko