Skip to contentSkip to conversation

xen/arm Add support for non-PCI passthrough

PATCH Superseded Julien Grall 5 participants 48 messages Apr 9, 2015 – Apr 28, 2015

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover xen/arm Add support for non-PCI passthrough

Hello all,

This is the fifth version of this patch series to add support for platform device passthrough on ARM.

In order to passthrough a non-PCI device, the user will have to:
- Map manually MMIO/IRQ
- Describe the device in the newly partial device tree support
- Specify the list of device protected by an IOMMU to assign to the guest.

While this solution is primitive, this is allow us to support more complex device in Xen with an little additionnal work for the user. Attempting to do it automatically is more difficult because we may not know the dependencies between devices (for instance a Network card and a phy).

To avoid adding code in DOM0 to manage platform device deassignment, the user has to add the property "xen,passthrough" to the device tree node describing the device. This can be easily done via U-Boot. For instance, if we want to passthrough the second network card of a Midway server to the guest. The user will have to add the following line the u-boot script:

fdt set /soc/ethernet@fff51000 xen,passthrough

This series has been tested on Midway by assigning the secondary network card to a guest (see instruction below). Though, it requires a separate patch as we decide to not support the Midway SMMU within the new drivers.

I plan to do futher testing on other boards.

A working tree can be found here:
git://xenbits.xen.org/julieng/xen-unstable.git branch passthrough-v5.2

Major changes in v5:
- Series divided in 2 parts. The first part [6] has been pushed to Xen upstream
- Add more documentation
- Modify XSM check for bind_pt_irq on ARM

Major changes in v4:
- The partial device tree option can only be used with trusted device tree
- Add more documentation
- Use DOMCTL_bind_pt_irq rather than PHYSDEVOP_map_pirq
- Add XSM support for non-PCI passthrough

Major changes in v3:
- Rework the approach to passthrough a device (xen,passthrough + partial device tree).
- Extend the existing hypercalls to assign/deassign device rather than adding new one.
- Merge series [4] and [5] in this serie.

Major changes in v2:
- Drop the patch #1 of the previous version
- Virtual IRQ are not anymore equal to the physical interrupt
- Move the hypercall to get DT informations for privcmd to domctl
- Split the domain creation in 2 two parts to allow per guest VGIC configuration (such as the number of SPIs).
- Bunch of typoes, commit improvement, function renaming.

For all changes see in each patch.

Sincerely yours,

[1] http://lists.xen.org/archives/html/xen-devel/2014-07/msg04090.html
[2] http://lists.xenproject.org/archives/html/xen-devel/2014-12/msg01386.html
[3] http://lists.xenproject.org/archives/html/xen-devel/2014-12/msg01612.html
[4] http://lists.xen.org/archives/html/xen-devel/2014-11/msg01672.html
[5] http://lists.xenproject.org/archives/html/xen-devel/2014-07/msg02098.html
[6] http://lists.xenproject.org/archives/html/xen-devel/2015-04/msg00248.html

Julien Grall (19):
xen/arm: Let the toolstack configure the number of SPIs xen/arm: vgic: Add spi_to_pending
xen/arm: Release IRQ routed to a domain when it's destroying xen/arm: Implement hypercall DOMCTL_{,un}bind_pt_pirq
xen: guestcopy: Provide an helper to safely copy string from guest xen/dts: Provide an helper to get a DT node from a path provided by a
guest
xen/passthrough: Introduce iommu_construct xen/passthrough: arm: release the DT devices assigned to a guest
earlier
xen/passthrough: iommu_deassign_device_dt: By default reassign device
to nobody
xen/iommu: arm: Wire iommu DOMCTL for ARM
xen/xsm: Add helpers to check permission for device tree passthrough xen/passthrough: Extend XEN_DOMCTL_*assign_device to support DT device tools/libxl: Create a per-arch function to map IRQ to a domain tools/libxl: Check if fdt_{first,next}_subnode are present in libfdt tools/(lib)xl: Add partial device tree support for ARM tools/libxl: arm: Use an higher value for the GIC phandle
libxl: Add support for Device Tree passthrough
xl: Add new option dtdev
docs/misc: arm: Add documentation about Device Tree passthrough

 docs/man/xl.cfg.pod.5                 |  15 +++
 docs/misc/arm/passthrough.txt         |  61 +++++++++++
 tools/config.h.in                     |   6 ++
 tools/configure.ac                    |   5 +
 tools/libxc/include/xenctrl.h         |  18 ++++
 tools/libxc/xc_domain.c               | 170 +++++++++++++++++++++++++++---
 tools/libxl/Makefile                  |   2 +-
 tools/libxl/libxl.h                   |   6 ++
 tools/libxl/libxl_arch.h              |   4 +
 tools/libxl/libxl_arm.c               | 192 +++++++++++++++++++++++++++++++++-
 tools/libxl/libxl_create.c            |  38 ++++++-
 tools/libxl/libxl_internal.h          |   5 +
 tools/libxl/libxl_libfdt_compat.c     |  92 ++++++++++++++++
 tools/libxl/libxl_types.idl           |   9 ++
 tools/libxl/libxl_x86.c               |  13 +++
 tools/libxl/xl_cmdimpl.c              |  23 +++-
 xen/arch/arm/domain.c                 |   6 +-
 xen/arch/arm/domctl.c                 |  88 +++++++++++++++-
 xen/arch/arm/gic.c                    |  45 ++++++++
 xen/arch/arm/irq.c                    |  46 ++++++++
 xen/arch/arm/setup.c                  |   1 +
 xen/arch/arm/vgic.c                   |  34 +++++-
 xen/common/Makefile                   |   1 +
 xen/common/device_tree.c              |  18 ++++
 xen/common/guestcopy.c                |  31 ++++++
 xen/drivers/passthrough/arm/iommu.c   |   7 +-
 xen/drivers/passthrough/arm/smmu.c    |   8 +-
 xen/drivers/passthrough/device_tree.c | 136 ++++++++++++++++++++++--
 xen/drivers/passthrough/iommu.c       |  33 +++++-
 xen/drivers/passthrough/pci.c         |  69 ++++++------
 xen/include/asm-arm/gic.h             |   4 +
 xen/include/asm-arm/irq.h             |   2 +
 xen/include/asm-arm/vgic.h            |   3 +-
 xen/include/public/arch-arm.h         |   2 +
 xen/include/public/domctl.h           |  28 ++++-
 xen/include/xen/device_tree.h         |  14 +++
 xen/include/xen/guest_access.h        |   5 +
 xen/include/xen/iommu.h               |   7 +-
 xen/include/xsm/dummy.h               |  47 ++++++---
 xen/include/xsm/xsm.h                 |  55 +++++++---
 xen/xsm/dummy.c                       |  10 +-
 xen/xsm/flask/avc.c                   |   3 +
 xen/xsm/flask/flask_op.c              |  49 +++------
 xen/xsm/flask/hooks.c                 | 139 +++++++++++++++++-------
 xen/xsm/flask/include/avc.h           |   2 +
 xen/xsm/flask/policy/access_vectors   |   2 +-
 46 files changed, 1373 insertions(+), 181 deletions(-)
 create mode 100644 docs/misc/arm/passthrough.txt
 create mode 100644 tools/libxl/libxl_libfdt_compat.c
 create mode 100644 xen/common/guestcopy.c
Signature · 1 line
2.1.4
#1 lore raw

PATCH v5 1/19 xen/arm: Let the toolstack configure the number of SPIs

From: Julien Grall <[email protected]>

Each domain may have a different number of IRQs depending on the devices assigned to it.

Rather re-using the number of IRQs used by the hardwared GIC, let the toolstack specify the number of SPIs when the domain is created. This will avoid to waste memory.

To calculate the number of SPIs, we take advantage of the fact that the libxl interface can only expose 1:1 mapping and look for the largest SPI in the list.

Cc Ian Jackson <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Wei Liu <[email protected]>

---
Changes in v5:
- Remove stray change
- Limit the number of SPIs to (1020 - 32) rather than the number supported by the hardware

Changes in v4:
- Check the number of SPIs supported by the virtual GIC against the number supported by the hardware GIC.
- Use uint32_t rather than int in the toolstack code.
- Initialize spi after the check in the toolstack code
- Typoes

Changes in v3:
- Fix typoes
- A separate has been created to extend the DOMCTL create domain

Changes in v2:
- Patch added

---
 tools/libxc/xc_domain.c       |  1 +
 tools/libxl/libxl_arm.c       | 21 +++++++++++++++++++++
 xen/arch/arm/domain.c         |  2 +-
 xen/arch/arm/setup.c          |  1 +
 xen/arch/arm/vgic.c           | 11 ++++++-----
 xen/include/asm-arm/vgic.h    |  2 +-
 xen/include/public/arch-arm.h |  2 ++
 7 files changed, 33 insertions(+), 7 deletions(-)
tools/libxc/xc_domain.c +1 -0
index 95e3098..1f2c80c 100644
@@ -67,6 +67,7 @@ int xc_domain_create(xc_interface *xch,
     /* No arch-specific configuration for now */
 #elif defined (__arm__) || defined(__aarch64__)
     config.gic_version = XEN_DOMCTL_CONFIG_GIC_DEFAULT;
+    config.nr_spis = 0;
 #else
     errno = ENOSYS;
     return -1;
tools/libxl/libxl_arm.c +21 -0
index 946618c..5a5cb3f 100644
@@ -39,6 +39,27 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
                                       libxl_domain_config *d_config,
                                       xc_domain_configuration_t *xc_config)
 {
+    uint32_t nr_spis = 0;
+    unsigned int i;
+
+    for (i = 0; i < d_config->b_info.num_irqs; i++) {
+        uint32_t irq = d_config->b_info.irqs[i];
+        uint32_t spi;
+
+        if (irq < 32)
+            continue;
+
+        spi = irq - 32;
+
+        if (nr_spis <= spi)
+            nr_spis = spi + 1;
+    }
+
+    LOG(DEBUG, "Configure the domain");
+
+    xc_config->nr_spis = nr_spis;
+    LOG(DEBUG, " - Allocate %u SPIs", nr_spis);
+
     xc_config->gic_version = XEN_DOMCTL_CONFIG_GIC_DEFAULT;
 
     return 0;
xen/arch/arm/domain.c +1 -1
index e4d6fc8..180bccc 100644
@@ -590,7 +590,7 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags,
     if ( (rc = gicv_setup(d)) != 0 )
         goto fail;
 
-    if ( (rc = domain_vgic_init(d)) != 0 )
+    if ( (rc = domain_vgic_init(d, config->nr_spis)) != 0 )
         goto fail;
 
     if ( (rc = domain_vtimer_init(d)) != 0 )
xen/arch/arm/setup.c +1 -0
index 4ec7c13..711562cf 100644
@@ -830,6 +830,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     /* Create initial domain 0. */
     /* The vGIC for DOM0 is exactly emulating the hardware GIC */
     config.gic_version = XEN_DOMCTL_CONFIG_GIC_DEFAULT;
+    config.nr_spis = gic_number_lines() - 32;
 
     dom0 = domain_create(0, 0, 0, &config);
     if ( IS_ERR(dom0) || (alloc_dom0_vcpu0(dom0) == NULL) )
xen/arch/arm/vgic.c +6 -5
index 8f91962..150624a 100644
@@ -68,16 +68,17 @@ static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
     p->irq = virq;
 }
 
-int domain_vgic_init(struct domain *d)
+int domain_vgic_init(struct domain *d, unsigned int nr_spis)
 {
     int i;
 
     d->arch.vgic.ctlr = 0;
 
-    if ( is_hardware_domain(d) )
-        d->arch.vgic.nr_spis = gic_number_lines() - 32;
-    else
-        d->arch.vgic.nr_spis = 0; /* We don't need SPIs for the guest */
+    /* Limit the number of virtual SPIs supported to (1020 - 32) = 988  */
+    if ( (nr_spis + NR_LOCAL_IRQS) > 1020 )
+        return -EINVAL;
+
+    d->arch.vgic.nr_spis = nr_spis;
 
     switch ( gic_hw_version() )
     {
xen/include/asm-arm/vgic.h +1 -1
index aba0d80..647f2fe 100644
@@ -177,7 +177,7 @@ enum gic_sgi_mode;
 
 #define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)
 
-extern int domain_vgic_init(struct domain *d);
+extern int domain_vgic_init(struct domain *d, unsigned int nr_spis);
 extern void domain_vgic_free(struct domain *d);
 extern int vcpu_vgic_init(struct vcpu *v);
 extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int irq);
xen/include/public/arch-arm.h +2 -0
index ed7e98f..c029e0f 100644
@@ -315,6 +315,8 @@ typedef uint64_t xen_callback_t;
 struct xen_arch_domainconfig {
     /* IN/OUT */
     uint8_t gic_version;
+    /* IN */
+    uint32_t nr_spis;
 };
 
 #endif
Signature · 1 line
2.1.4
Acked-by on v5 1/19
#2 lore raw

Re: [PATCH v5 p2 01/19] xen/arm: Let the toolstack configure the number of SPIs

Quoted 6 lines · Julien Grall

From: Julien Grall <[email protected]>

Each domain may have a different number of IRQs depending on the devices assigned to it.

Rather re-using the number of IRQs used by the hardwared GIC, let the

^than

Quoted 2 lines

toolstack specify the number of SPIs when the domain is created. This will avoid to waste memory.

"will avoid wasting memory."

xen/arch/arm/vgic.c static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq) · line 79

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+    /* Limit the number of virtual SPIs supported to (1020 - 32) = 988  */
+    if ( (nr_spis + NR_LOCAL_IRQS) > 1020 )
+        return -EINVAL;

If there's any chance this can be called by not-completely trusted code (e.g. a disaggregated toolstack) then this if susceptible to an overflow (sorry, I gave you this code in a previous rev).

I think you can just move the NR_LOCAL_IRQS to the other side of the expression, i.e.

    if ( nr_spis > 1020 - NR_LOCAL_IRQS )

With that and the grammar fixed:

Ian.

#3 lore raw

Re: [PATCH v5 p2 01/19] xen/arm: Let the toolstack configure the number of SPIs

Hi Ian,

Quoted 16 lines · Ian Campbell
Quoted 6 lines · Julien Grall

From: Julien Grall <[email protected]>

Each domain may have a different number of IRQs depending on the devices assigned to it.

Rather re-using the number of IRQs used by the hardwared GIC, let the

^than

Quoted 2 lines

toolstack specify the number of SPIs when the domain is created. This will avoid to waste memory.

"will avoid wasting memory."

Quoted 7 lines

xen/arch/arm/vgic.c static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq) · line 79

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+    /* Limit the number of virtual SPIs supported to (1020 - 32) = 988  */
+    if ( (nr_spis + NR_LOCAL_IRQS) > 1020 )
+        return -EINVAL;

If there's any chance this can be called by not-completely trusted code (e.g. a disaggregated toolstack) then this if susceptible to an overflow (sorry, I gave you this code in a previous rev).

Hmmm, right.

Quoted 3 lines

I think you can just move the NR_LOCAL_IRQS to the other side of the expression, i.e.

     if ( nr_spis > 1020 - NR_LOCAL_IRQS )

I will do the change and adding a pair of parentheses for more clarity:

if ( nr_spis > (1020 - NR_LOCAL_IRQS) )

Quoted 2 lines

With that and the grammar fixed:

Thanks,

Signature · 1 line
Julien Grall
#4 lore raw

PATCH v5 2/19 xen/arm: vgic: Add spi_to_pending

In reply to Julien Grall

From: Julien Grall <[email protected]>

Introduce spi_to_pending in order retrieve the irq_pending structure for a specific SPI.

It's not possible to re-use irq_to_pending because it's required a VCPU and some call of the new function may during domain destruction after the VCPUs are freed.

---
Changes in v5:
- Add Ian's Ack

Changes in v4:
- Patch added

---
 xen/arch/arm/vgic.c        | 7 +++++++
 xen/include/asm-arm/vgic.h | 1 +
 2 files changed, 8 insertions(+)
xen/arch/arm/vgic.c +7 -0
index 150624a..55c8927 100644
@@ -371,6 +371,13 @@ struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
     return n;
 }
 
+struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq)
+{
+    ASSERT(irq >= NR_LOCAL_IRQS);
+
+    return &d->arch.vgic.pending_irqs[irq - 32];
+}
+
 void vgic_clear_pending_irqs(struct vcpu *v)
 {
     struct pending_irq *p, *t;
xen/include/asm-arm/vgic.h +1 -0
index 647f2fe..8d22532 100644
@@ -185,6 +185,7 @@ extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
 extern struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq);
+extern struct pending_irq *spi_to_pending(struct domain *d, unsigned int irq);
 extern struct vgic_irq_rank *vgic_rank_offset(struct vcpu *v, int b, int n, int s);
 extern struct vgic_irq_rank *vgic_rank_irq(struct vcpu *v, unsigned int irq);
 extern int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr);
Signature · 1 line
2.1.4
#5 lore raw

PATCH v5 3/19 xen/arm: Release IRQ routed to a domain when it's destroying

In reply to Julien Grall

From: Julien Grall <[email protected]>

Xen has to release IRQ routed to a domain in order to reuse later. Currently only SPIs can be routed to the guest so we only need to browse SPIs for a specific domain.

Furthermore, a guest can crash and leave the IRQ in an incorrect state (i.e has not been EOIed). Xen will have to reset the IRQ in order to be able to reuse the IRQ later.

Introduce 2 new functions for release an IRQ routed to a domain:
- release_guest_irq: upper level to retrieve the IRQ, call the GIC code and release the action
- gic_remove_guest_irq: Check if we can remove the IRQ, and reset it if necessary

---
Changes in v5:
- Typoes in the commit message
- Add Ian's Ack

Changes in v4:
- Reorder the code flow
- Typoes and coding style
- Use the newly helper spi_to_pending

Changes in v3:
- Take the vgic rank lock to protect p->desc
- Correctly check if the IRQ is disabled
- Extend the check on the virq in release_guest_irq
- Use vgic_get_target_vcpu to get the target vCPU
- Remove spurious change

Changes in v2:
- Drop the desc->handler = &no_irq_type in release_irq as it's buggy if the IRQ is routed to Xen
- Add release_guest_irq and gic_remove_guest_irq

---
 xen/arch/arm/gic.c        | 45 +++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/irq.c        | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic.c       | 16 ++++++++++++++++
 xen/include/asm-arm/gic.h |  4 ++++
 xen/include/asm-arm/irq.h |  2 ++
 5 files changed, 113 insertions(+)
xen/arch/arm/gic.c +45 -0
index 5f34997..f023e4f 100644
@@ -163,6 +163,51 @@ out:
     return res;
 }
 
+/* This function only works with SPIs for now */
+int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
+                              struct irq_desc *desc)
+{
+    struct vcpu *v_target = vgic_get_target_vcpu(d->vcpu[0], virq);
+    struct vgic_irq_rank *rank = vgic_rank_irq(v_target, virq);
+    struct pending_irq *p = irq_to_pending(v_target, virq);
+    unsigned long flags;
+
+    ASSERT(spin_is_locked(&desc->lock));
+    ASSERT(test_bit(_IRQ_GUEST, &desc->status));
+    ASSERT(p->desc == desc);
+
+    vgic_lock_rank(v_target, rank, flags);
+
+    if ( d->is_dying )
+    {
+        desc->handler->shutdown(desc);
+
+        /* EOI the IRQ it it has not been done by the guest */
+        if ( test_bit(_IRQ_INPROGRESS, &desc->status) )
+            gic_hw_ops->deactivate_irq(desc);
+        clear_bit(_IRQ_INPROGRESS, &desc->status);
+    }
+    else
+    {
+        /*
+         * TODO: Handle eviction from LRs For now, deny
+         * remove if the IRQ is inflight or not disabled.
+         */
+        if ( test_bit(_IRQ_INPROGRESS, &desc->status) ||
+             !test_bit(_IRQ_DISABLED, &desc->status) )
+            return -EBUSY;
+    }
+
+    clear_bit(_IRQ_GUEST, &desc->status);
+    desc->handler = &no_irq_type;
+
+    p->desc = NULL;
+
+    vgic_unlock_rank(v_target, rank, flags);
+
+    return 0;
+}
+
 int gic_irq_xlate(const u32 *intspec, unsigned int intsize,
                   unsigned int *out_hwirq,
                   unsigned int *out_type)
xen/arch/arm/irq.c +46 -0

52 lines not on this page · raw message

xen/arch/arm/vgic.c +16 -0
index 55c8927..05c5010 100644
@@ -135,6 +135,22 @@ void register_vgic_ops(struct domain *d, const struct vgic_ops *ops)
 
 void domain_vgic_free(struct domain *d)
 {
+    int i;
+    int ret;
+
+    for ( i = 0; i < (d->arch.vgic.nr_spis); i++ )
+    {
+        struct pending_irq *p = spi_to_pending(d, i + 32);
+
+        if ( p->desc )
+        {
+            ret = release_guest_irq(d, p->irq);
+            if ( ret )
+                dprintk(XENLOG_G_WARNING, "d%u: Failed to release virq %u ret = %d\n",
+                        d->domain_id, p->irq, ret);
+        }
+    }
+
     xfree(d->arch.vgic.shared_irqs);
     xfree(d->arch.vgic.pending_irqs);
     xfree(d->arch.vgic.allocated_irqs);
xen/include/asm-arm/gic.h +4 -0
index ef4bf9a..9e2acb7 100644
@@ -220,6 +220,10 @@ extern int gic_route_irq_to_guest(struct domain *, unsigned int virq,
                                   struct irq_desc *desc,
                                   unsigned int priority);
 
+/* Remove an IRQ passthrough to a guest */
+int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
+                              struct irq_desc *desc);
+
 extern void gic_inject(void);
 extern void gic_clear_pending_irqs(struct vcpu *v);
 extern int gic_events_need_delivery(void);
xen/include/asm-arm/irq.h +2 -0
index 71b39e7..34b492b 100644
@@ -44,6 +44,8 @@ void init_secondary_IRQ(void);
 
 int route_irq_to_guest(struct domain *d, unsigned int virq,
                        unsigned int irq, const char *devname);
+int release_guest_irq(struct domain *d, unsigned int irq);
+
 void arch_move_irqs(struct vcpu *v);
 
 /* Set IRQ type for an SPI */
Signature · 1 line
2.1.4
#6 lore raw

PATCH v5 4/19 xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

In reply to Julien Grall

From: Julien Grall <[email protected]>

On x86, an IRQ is assigned in 2 steps to an HVM guest:
- The toolstack is calling PHYSDEVOP_map_pirq in order to create a guest PIRQ (IRQ bound to an event channel)
- The emulator (QEMU) is calling DOMCTL_bind_pt_irq in order to bind the IRQ

On ARM, there is no concept of PIRQ as the IRQ can be assigned to a virtual IRQ using the interrupt controller.

It's not clear if we will need 2 different hypercalls on ARM to assign IRQ and, for now, only the toolstack will manage IRQ.

In order to avoid re-using a fixed ABI hypercall (PHYSDEVOP_*) for a different purpose and allow us more time to figure out the right out, only DOMCTL_{,un}bind_pt_pirq is implemented on ARM.

The DOMCTL is extended with a new type PT_IRQ_TYPE_SPI and only IRQ == vIRQ (i.e machine_irq == spi) is supported.

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

Note: The toolstack changes for routing an IRQ to a guest will be done in a separate patch.

---
Contrawise PHYSDEV, DOMCTL interface is not fixed. This version is using a DOMCTL in order to let us more to to see if we need a new PHYSDEV op for vIRQ assignation.

DOMCTL_unbind_pt_irq has been implemented, although I haven't test it. I'm not sure if we want to keep it.

xc_domain_{,un}bind_pt_irq are left unchanged because QEMU upstream & traditional are using them. I will send a follow-up series to drop this 2 functions in order to avoid defering this series while awaiting changes pushed for QEMU upstream.

Changes in v5:
- Do the check of XSM PHYSDEVOP_map_pirq in the ARM implementation
- Left xc_domain_{,un}bind_pt_irq unchanged
- Introduce xc_domain_{,un}bind_pt_spi_irq

Changes in v4:
- Move the implementation from PHYSDEV to DOMCTL. Reuse DOMCTL_{,un}bind_pt_irq for this purpose.

Changes in v3:
- Functions to allocate/release/reserved a VIRQ has been moved in a separate patch
- Make clear that only MAP_PIRQ_GSI is only supported for now

Changes in v2:
- Add PHYSDEVOP_unmap_pirq
- Rework commit message
- Add functions to allocate/release a VIRQ
- is_routable_irq has been renamed into is_assignable_irq

---
 tools/libxc/include/xenctrl.h |  8 +++++
 tools/libxc/xc_domain.c       | 74 +++++++++++++++++++++++++++++++++++------
 xen/arch/arm/domctl.c         | 77 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/domctl.h   |  4 +++
 xen/include/xsm/dummy.h       | 24 +++++++-------
 xen/include/xsm/xsm.h         | 28 ++++++++--------
 xen/xsm/dummy.c               |  4 +--
 xen/xsm/flask/hooks.c         | 70 ++++++++++++++++++++-------------------
 8 files changed, 217 insertions(+), 72 deletions(-)
tools/libxc/include/xenctrl.h +8 -0
index 02d0db8..cc78ed6 100644
@@ -2114,6 +2114,14 @@ int xc_domain_bind_pt_isa_irq(xc_interface *xch,
                               uint32_t domid,
                               uint8_t machine_irq);
 
+int xc_domain_bind_pt_spi_irq(xc_interface *xch,
+                              uint32_t domid,
+                              uint16_t spi);
+
+int xc_domain_unbind_pt_spi_irq(xc_interface *xch,
+                                uint32_t domid,
+                                uint16_t spi);
+
 int xc_domain_set_machine_address_size(xc_interface *xch,
 				       uint32_t domid,
 				       unsigned int width);
tools/libxc/xc_domain.c +64 -10

116 lines not on this page · raw message

xen/arch/arm/domctl.c +77 -0

89 lines not on this page · raw message

xen/include/public/domctl.h +4 -0
index d2e8db0..d94c647 100644
@@ -504,6 +504,7 @@ typedef enum pt_irq_type_e {
     PT_IRQ_TYPE_ISA,
     PT_IRQ_TYPE_MSI,
     PT_IRQ_TYPE_MSI_TRANSLATE,
+    PT_IRQ_TYPE_SPI,    /* ARM: valid range 32-1019 */
 } pt_irq_type_t;
 struct xen_domctl_bind_pt_irq {
     uint32_t machine_irq;
@@ -524,6 +525,9 @@ struct xen_domctl_bind_pt_irq {
             uint32_t gflags;
             uint64_aligned_t gtable;
         } msi;
+        struct {
+            uint16_t spi;
+        } spi;
     } u;
 };
 typedef struct xen_domctl_bind_pt_irq xen_domctl_bind_pt_irq_t;
xen/include/xsm/dummy.h +12 -12
index 4227093..c36e05f 100644
@@ -445,6 +445,18 @@ static XSM_INLINE int xsm_unmap_domain_pirq(XSM_DEFAULT_ARG struct domain *d)
     return xsm_default_action(action, current->domain, d);
 }
 
+static XSM_INLINE int xsm_bind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
+static XSM_INLINE int xsm_unbind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
 static XSM_INLINE int xsm_unmap_domain_irq(XSM_DEFAULT_ARG struct domain *d, int irq, void *data)
 {
     XSM_ASSERT_ACTION(XSM_HOOK);
@@ -631,18 +643,6 @@ static XSM_INLINE int xsm_priv_mapping(XSM_DEFAULT_ARG struct domain *d, struct
     return xsm_default_action(action, d, t);
 }
 
-static XSM_INLINE int xsm_bind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    XSM_ASSERT_ACTION(XSM_HOOK);
-    return xsm_default_action(action, current->domain, d);
-}
-
-static XSM_INLINE int xsm_unbind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    XSM_ASSERT_ACTION(XSM_HOOK);
-    return xsm_default_action(action, current->domain, d);
-}
-
 static XSM_INLINE int xsm_ioport_permission(XSM_DEFAULT_ARG struct domain *d, uint32_t s, uint32_t e, uint8_t allow)
 {
     XSM_ASSERT_ACTION(XSM_HOOK);
xen/include/xsm/xsm.h +14 -14
index c2049f3..b7446be 100644
@@ -107,6 +107,8 @@ struct xsm_operations {
     int (*map_domain_irq) (struct domain *d, int irq, void *data);
     int (*unmap_domain_pirq) (struct domain *d);
     int (*unmap_domain_irq) (struct domain *d, int irq, void *data);
+    int (*bind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
+    int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*irq_permission) (struct domain *d, int pirq, uint8_t allow);
     int (*iomem_permission) (struct domain *d, uint64_t s, uint64_t e, uint8_t allow);
     int (*iomem_mapping) (struct domain *d, uint64_t s, uint64_t e, uint8_t allow);
@@ -169,8 +171,6 @@ struct xsm_operations {
     int (*mmuext_op) (struct domain *d, struct domain *f);
     int (*update_va_mapping) (struct domain *d, struct domain *f, l1_pgentry_t pte);
     int (*priv_mapping) (struct domain *d, struct domain *t);
-    int (*bind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
-    int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
     int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
 #endif
@@ -419,6 +419,18 @@ static inline int xsm_unmap_domain_irq (xsm_default_t def, struct domain *d, int
     return xsm_ops->unmap_domain_irq(d, irq, data);
 }
 
+static inline int xsm_bind_pt_irq(xsm_default_t def, struct domain *d,
+                                  struct xen_domctl_bind_pt_irq *bind)
+{
+    return xsm_ops->bind_pt_irq(d, bind);
+}
+
+static inline int xsm_unbind_pt_irq(xsm_default_t def, struct domain *d,
+                                    struct xen_domctl_bind_pt_irq *bind)
+{
+    return xsm_ops->unbind_pt_irq(d, bind);
+}
+
 static inline int xsm_irq_permission (xsm_default_t def, struct domain *d, int pirq, uint8_t allow)
 {
     return xsm_ops->irq_permission(d, pirq, allow);
@@ -643,18 +655,6 @@ static inline int xsm_priv_mapping(xsm_default_t def, struct domain *d, struct d
     return xsm_ops->priv_mapping(d, t);
 }
 
-static inline int xsm_bind_pt_irq(xsm_default_t def, struct domain *d,
-                                                struct xen_domctl_bind_pt_irq *bind)
-{
-    return xsm_ops->bind_pt_irq(d, bind);
-}
-
-static inline int xsm_unbind_pt_irq(xsm_default_t def, struct domain *d,
-                                                struct xen_domctl_bind_pt_irq *bind)
-{
-    return xsm_ops->unbind_pt_irq(d, bind);
-}
-
 static inline int xsm_ioport_permission (xsm_default_t def, struct domain *d, uint32_t s, uint32_t e, uint8_t allow)
 {
     return xsm_ops->ioport_permission(d, s, e, allow);
xen/xsm/dummy.c +2 -2
index 25fca68..a3b8aab 100644
@@ -81,6 +81,8 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, map_domain_irq);
     set_to_dummy_if_null(ops, unmap_domain_pirq);
     set_to_dummy_if_null(ops, unmap_domain_irq);
+    set_to_dummy_if_null(ops, bind_pt_irq);
+    set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, irq_permission);
     set_to_dummy_if_null(ops, iomem_permission);
     set_to_dummy_if_null(ops, iomem_mapping);
@@ -140,8 +142,6 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, mmuext_op);
     set_to_dummy_if_null(ops, update_va_mapping);
     set_to_dummy_if_null(ops, priv_mapping);
-    set_to_dummy_if_null(ops, bind_pt_irq);
-    set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, ioport_permission);
     set_to_dummy_if_null(ops, ioport_mapping);
 #endif
xen/xsm/flask/hooks.c +36 -34

104 lines not on this page · raw message

Signature · 1 line
2.1.4
#7 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

Quoted 1 line · Julien Grall

From: Julien Grall <[email protected]>

I've left the XSM related quotes untrimmed and CCd Daniel. I think it's all code motion (making x86 specific things generic), so perhaps no ack needed but an opportunity to nack instead ;-)

Quoted 27 lines

On x86, an IRQ is assigned in 2 steps to an HVM guest:
- The toolstack is calling PHYSDEVOP_map_pirq in order to create a guest PIRQ (IRQ bound to an event channel)
- The emulator (QEMU) is calling DOMCTL_bind_pt_irq in order to bind the IRQ

On ARM, there is no concept of PIRQ as the IRQ can be assigned to a virtual IRQ using the interrupt controller.

It's not clear if we will need 2 different hypercalls on ARM to assign IRQ and, for now, only the toolstack will manage IRQ.

In order to avoid re-using a fixed ABI hypercall (PHYSDEVOP_*) for a different purpose and allow us more time to figure out the right out, only DOMCTL_{,un}bind_pt_pirq is implemented on ARM.

The DOMCTL is extended with a new type PT_IRQ_TYPE_SPI and only IRQ == vIRQ (i.e machine_irq == spi) is supported.

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

Quoted 7 lines

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

I think this paragraph makes the previous bit obsolete?

Quoted 17 lines
tools/libxc/xc_domain.c +0 -9
index 1f2c80c..676ec50 100644
@@ -1773,15 +1773,16 @@ int xc_domain_unbind_msi_irq(
 }
 
 /* Pass-through: binds machine irq to guests irq */
-int xc_domain_bind_pt_irq(
-    xc_interface *xch,
-    uint32_t domid,
-    uint8_t machine_irq,
-    uint8_t irq_type,
-    uint8_t bus,
-    uint8_t device,
-    uint8_t intx,
-    uint8_t isa_irq)

tools/libxc/xc_domain.c int xc_domain_unbind_msi_irq( · line 1785

Quoted 10 lines
tools/libxc/xc_domain.c +10 -0
+static int xc_domain_bind_pt_irq_int(
+           xc_interface *xch,
+           uint32_t domid,
+           uint8_t machine_irq,
+           uint8_t irq_type,
+           uint8_t bus,
+           uint8_t device,
+           uint8_t intx,
+           uint8_t isa_irq,
+           uint16_t spi)

Please either leave the indentation of the arguments as it is or fix it properly, i.e. put xch on the same line as the prototype and align everything else below it.

TBH I'd prefer the former even if it isn't strictly coding style since it obscures the real change you made.

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1921

Quoted 10 lines
tools/libxc/xc_domain.c +6 -0
@@ -1878,6 +1913,25 @@ int xc_domain_bind_pt_isa_irq(
                                   PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
 }
 
+int xc_domain_bind_pt_spi_irq(
+    xc_interface *xch,
+    uint32_t domid,
+    uint16_t spi)
+{
+    /* vSPI == SPI */

I wonder if to avoid API churn later we should accept both machine and guest IRQ here and rely on the check that htey are the same in the hypervisor to reject?

Fair enough we can change libxc interface if we want, but avoiding a little churn later on seems reasonable and it makes it a better fit with the existing interfaces.

Quoted 27 lines
xen/arch/arm/domctl.c +12 -0
index 6f30af7..531bb13 100644
@@ -10,6 +10,8 @@
 #include <xen/errno.h>
 #include <xen/sched.h>
 #include <xen/hypercall.h>
+#include <xen/iocap.h>
+#include <xsm/xsm.h>
 #include <public/domctl.h>
 
 long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
@@ -30,6 +32,81 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 
         return p2m_cache_flush(d, s, e);
     }
+    case XEN_DOMCTL_bind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 54

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+
+        /*
+         * XXX: For now map the interrupt 1:1. Other support will require to
+         * modify domain_pirq_to_irq macro.
+         */
+        if ( irq != virq )
+            return -EINVAL;
+
+        /*
+         * ARM doesn't require to separate IRQ assignation in 2

"ARM doesn't require separating assignment of IRQs into 2 hypercalls"

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 58

Quoted 4 lines
xen/arch/arm/domctl.c +4 -0
+         * hypercalls (PHYSDEVOP_map_pirq and DOMCTL_bind_pt_irq).
+         *
+         * Call xsm_map_domain_irq in order to keep the same XSM checks
+         * done by the 2 hypercalls

... for consistency with other architectures.

Quoted 25 lines
xen/arch/arm/domctl.c +25 -0
+         */
+        rc = xsm_map_domain_irq(XSM_HOOK, d, irq, NULL);
+        if ( rc )
+            return rc;
+
+        rc = xsm_bind_pt_irq(XSM_HOOK, d, bind);
+        if ( rc )
+            return rc;
+
+        if ( !irq_access_permitted(current->domain, irq) )
+            return -EPERM;
+
+        if ( !vgic_reserve_virq(d, virq) )
+            return -EBUSY;
+
+        rc = route_irq_to_guest(d, virq, irq, "routed IRQ");
+        if ( rc )
+            vgic_free_virq(d, virq);
+
+        return rc;
+    }
+    case XEN_DOMCTL_unbind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 93

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;
+
+        /* For now map the interrupt 1:1 */
+        if ( irq != virq )
+            return -EINVAL;

The x86 version doesn't appear to consider irq_type or irq, only virq (from ->machine_irq). That seems to be logical to me (if a little underdocumented) and I think we should be consistent.

Quoted 237 lines
xen/include/xsm/dummy.h +12 -12
index 4227093..c36e05f 100644
@@ -445,6 +445,18 @@ static XSM_INLINE int xsm_unmap_domain_pirq(XSM_DEFAULT_ARG struct domain *d)
     return xsm_default_action(action, current->domain, d);
 }
 
+static XSM_INLINE int xsm_bind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
+static XSM_INLINE int xsm_unbind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
 static XSM_INLINE int xsm_unmap_domain_irq(XSM_DEFAULT_ARG struct domain *d, int irq, void *data)
 {
     XSM_ASSERT_ACTION(XSM_HOOK);
@@ -631,18 +643,6 @@ static XSM_INLINE int xsm_priv_mapping(XSM_DEFAULT_ARG struct domain *d, struct
     return xsm_default_action(action, d, t);
 }
 
-static XSM_INLINE int xsm_bind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    XSM_ASSERT_ACTION(XSM_HOOK);
-    return xsm_default_action(action, current->domain, d);
-}
-
-static XSM_INLINE int xsm_unbind_pt_irq(XSM_DEFAULT_ARG struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    XSM_ASSERT_ACTION(XSM_HOOK);
-    return xsm_default_action(action, current->domain, d);
-}
-
 static XSM_INLINE int xsm_ioport_permission(XSM_DEFAULT_ARG struct domain *d, uint32_t s, uint32_t e, uint8_t allow)
 {
     XSM_ASSERT_ACTION(XSM_HOOK);
xen/include/xsm/xsm.h +14 -14
index c2049f3..b7446be 100644
@@ -107,6 +107,8 @@ struct xsm_operations {
     int (*map_domain_irq) (struct domain *d, int irq, void *data);
     int (*unmap_domain_pirq) (struct domain *d);
     int (*unmap_domain_irq) (struct domain *d, int irq, void *data);
+    int (*bind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
+    int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*irq_permission) (struct domain *d, int pirq, uint8_t allow);
     int (*iomem_permission) (struct domain *d, uint64_t s, uint64_t e, uint8_t allow);
     int (*iomem_mapping) (struct domain *d, uint64_t s, uint64_t e, uint8_t allow);
@@ -169,8 +171,6 @@ struct xsm_operations {
     int (*mmuext_op) (struct domain *d, struct domain *f);
     int (*update_va_mapping) (struct domain *d, struct domain *f, l1_pgentry_t pte);
     int (*priv_mapping) (struct domain *d, struct domain *t);
-    int (*bind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
-    int (*unbind_pt_irq) (struct domain *d, struct xen_domctl_bind_pt_irq *bind);
     int (*ioport_permission) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
     int (*ioport_mapping) (struct domain *d, uint32_t s, uint32_t e, uint8_t allow);
 #endif
@@ -419,6 +419,18 @@ static inline int xsm_unmap_domain_irq (xsm_default_t def, struct domain *d, int
     return xsm_ops->unmap_domain_irq(d, irq, data);
 }
 
+static inline int xsm_bind_pt_irq(xsm_default_t def, struct domain *d,
+                                  struct xen_domctl_bind_pt_irq *bind)
+{
+    return xsm_ops->bind_pt_irq(d, bind);
+}
+
+static inline int xsm_unbind_pt_irq(xsm_default_t def, struct domain *d,
+                                    struct xen_domctl_bind_pt_irq *bind)
+{
+    return xsm_ops->unbind_pt_irq(d, bind);
+}
+
 static inline int xsm_irq_permission (xsm_default_t def, struct domain *d, int pirq, uint8_t allow)
 {
     return xsm_ops->irq_permission(d, pirq, allow);
@@ -643,18 +655,6 @@ static inline int xsm_priv_mapping(xsm_default_t def, struct domain *d, struct d
     return xsm_ops->priv_mapping(d, t);
 }
 
-static inline int xsm_bind_pt_irq(xsm_default_t def, struct domain *d,
-                                                struct xen_domctl_bind_pt_irq *bind)
-{
-    return xsm_ops->bind_pt_irq(d, bind);
-}
-
-static inline int xsm_unbind_pt_irq(xsm_default_t def, struct domain *d,
-                                                struct xen_domctl_bind_pt_irq *bind)
-{
-    return xsm_ops->unbind_pt_irq(d, bind);
-}
-
 static inline int xsm_ioport_permission (xsm_default_t def, struct domain *d, uint32_t s, uint32_t e, uint8_t allow)
 {
     return xsm_ops->ioport_permission(d, s, e, allow);
xen/xsm/dummy.c +2 -2
index 25fca68..a3b8aab 100644
@@ -81,6 +81,8 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, map_domain_irq);
     set_to_dummy_if_null(ops, unmap_domain_pirq);
     set_to_dummy_if_null(ops, unmap_domain_irq);
+    set_to_dummy_if_null(ops, bind_pt_irq);
+    set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, irq_permission);
     set_to_dummy_if_null(ops, iomem_permission);
     set_to_dummy_if_null(ops, iomem_mapping);
@@ -140,8 +142,6 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, mmuext_op);
     set_to_dummy_if_null(ops, update_va_mapping);
     set_to_dummy_if_null(ops, priv_mapping);
-    set_to_dummy_if_null(ops, bind_pt_irq);
-    set_to_dummy_if_null(ops, unbind_pt_irq);
     set_to_dummy_if_null(ops, ioport_permission);
     set_to_dummy_if_null(ops, ioport_mapping);
 #endif
xen/xsm/flask/hooks.c +36 -34
index ab5141d..688ba2a 100644
@@ -580,12 +580,14 @@ static int flask_domctl(struct domain *d, int cmd)
 #ifdef HAS_MEM_ACCESS
     case XEN_DOMCTL_vm_event_op:
 #endif
+
+    /* These have individual XSM hooks (arch/../domctl.c) */
+    case XEN_DOMCTL_bind_pt_irq:
+    case XEN_DOMCTL_unbind_pt_irq:
 #ifdef CONFIG_X86
     /* These have individual XSM hooks (arch/x86/domctl.c) */
     case XEN_DOMCTL_shadow_op:
     case XEN_DOMCTL_ioport_permission:
-    case XEN_DOMCTL_bind_pt_irq:
-    case XEN_DOMCTL_unbind_pt_irq:
     case XEN_DOMCTL_ioport_mapping:
     /* These have individual XSM hooks (drivers/passthrough/iommu.c) */
     case XEN_DOMCTL_get_device_group:
@@ -911,6 +913,36 @@ static int flask_unmap_domain_irq (struct domain *d, int irq, void *data)
     return rc;
 }
 
+static int flask_bind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    u32 dsid, rsid;
+    int rc = -EPERM;
+    int irq;
+    struct avc_audit_data ad;
+
+    rc = current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__ADD);
+    if ( rc )
+        return rc;
+
+    irq = domain_pirq_to_irq(d, bind->machine_irq);
+
+    rc = get_irq_sid(irq, &rsid, &ad);
+    if ( rc )
+        return rc;
+
+    rc = avc_current_has_perm(rsid, SECCLASS_HVM, HVM__BIND_IRQ, &ad);
+    if ( rc )
+        return rc;
+
+    dsid = domain_sid(d);
+    return avc_has_perm(dsid, rsid, SECCLASS_RESOURCE, RESOURCE__USE, &ad);
+}
+
+static int flask_unbind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq *bind)
+{
+    return current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__REMOVE);
+}
+
 static int flask_irq_permission (struct domain *d, int pirq, uint8_t access)
 {
     /* the PIRQ number is not useful; real IRQ is checked during mapping */
@@ -1468,36 +1500,6 @@ static int flask_priv_mapping(struct domain *d, struct domain *t)
 {
     return domain_has_perm(d, t, SECCLASS_MMU, MMU__TARGET_HACK);
 }
-
-static int flask_bind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    u32 dsid, rsid;
-    int rc = -EPERM;
-    int irq;
-    struct avc_audit_data ad;
-
-    rc = current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__ADD);
-    if ( rc )
-        return rc;
-
-    irq = domain_pirq_to_irq(d, bind->machine_irq);
-
-    rc = get_irq_sid(irq, &rsid, &ad);
-    if ( rc )
-        return rc;
-
-    rc = avc_current_has_perm(rsid, SECCLASS_HVM, HVM__BIND_IRQ, &ad);
-    if ( rc )
-        return rc;
-
-    dsid = domain_sid(d);
-    return avc_has_perm(dsid, rsid, SECCLASS_RESOURCE, RESOURCE__USE, &ad);
-}
-
-static int flask_unbind_pt_irq (struct domain *d, struct xen_domctl_bind_pt_irq *bind)
-{
-    return current_has_perm(d, SECCLASS_RESOURCE, RESOURCE__REMOVE);
-}
 #endif /* CONFIG_X86 */
 
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
@@ -1556,6 +1558,8 @@ static struct xsm_operations flask_ops = {
     .map_domain_irq = flask_map_domain_irq,
     .unmap_domain_pirq = flask_unmap_domain_pirq,
     .unmap_domain_irq = flask_unmap_domain_irq,
+    .bind_pt_irq = flask_bind_pt_irq,
+    .unbind_pt_irq = flask_unbind_pt_irq,
     .irq_permission = flask_irq_permission,
     .iomem_permission = flask_iomem_permission,
     .iomem_mapping = flask_iomem_mapping,
@@ -1616,8 +1620,6 @@ static struct xsm_operations flask_ops = {
     .mmuext_op = flask_mmuext_op,
     .update_va_mapping = flask_update_va_mapping,
     .priv_mapping = flask_priv_mapping,
-    .bind_pt_irq = flask_bind_pt_irq,
-    .unbind_pt_irq = flask_unbind_pt_irq,
     .ioport_permission = flask_ioport_permission,
     .ioport_mapping = flask_ioport_mapping,
 #endif
#8 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

Hi Ian,

Quoted 39 lines · Ian Campbell
Quoted 1 line · Julien Grall

From: Julien Grall <[email protected]>

I've left the XSM related quotes untrimmed and CCd Daniel. I think it's all code motion (making x86 specific things generic), so perhaps no ack needed but an opportunity to nack instead ;-)

Quoted 32 lines

On x86, an IRQ is assigned in 2 steps to an HVM guest:
- The toolstack is calling PHYSDEVOP_map_pirq in order to create a guest PIRQ (IRQ bound to an event channel)
- The emulator (QEMU) is calling DOMCTL_bind_pt_irq in order to bind the IRQ

On ARM, there is no concept of PIRQ as the IRQ can be assigned to a virtual IRQ using the interrupt controller.

It's not clear if we will need 2 different hypercalls on ARM to assign IRQ and, for now, only the toolstack will manage IRQ.

In order to avoid re-using a fixed ABI hypercall (PHYSDEVOP_*) for a different purpose and allow us more time to figure out the right out, only DOMCTL_{,un}bind_pt_pirq is implemented on ARM.

The DOMCTL is extended with a new type PT_IRQ_TYPE_SPI and only IRQ == vIRQ (i.e machine_irq == spi) is supported.

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

Quoted 4 lines
Quoted 2 lines

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

I think this paragraph makes the previous bit obsolete?

Do you mean: "Concerning XSM..." until and the 2 paragraphs for XSM hypercalls? If so, yes.

Quoted 33 lines
Quoted 18 lines
tools/libxc/xc_domain.c +0 -9
index 1f2c80c..676ec50 100644
@@ -1773,15 +1773,16 @@ int xc_domain_unbind_msi_irq(
  }
 
  /* Pass-through: binds machine irq to guests irq */
-int xc_domain_bind_pt_irq(
-    xc_interface *xch,
-    uint32_t domid,
-    uint8_t machine_irq,
-    uint8_t irq_type,
-    uint8_t bus,
-    uint8_t device,
-    uint8_t intx,
-    uint8_t isa_irq)

tools/libxc/xc_domain.c int xc_domain_unbind_msi_irq( · line 1785

Quoted 10 lines
tools/libxc/xc_domain.c +10 -0
+static int xc_domain_bind_pt_irq_int(
+           xc_interface *xch,
+           uint32_t domid,
+           uint8_t machine_irq,
+           uint8_t irq_type,
+           uint8_t bus,
+           uint8_t device,
+           uint8_t intx,
+           uint8_t isa_irq,
+           uint16_t spi)

Please either leave the indentation of the arguments as it is or fix it properly, i.e. put xch on the same line as the prototype and align everything else below it.

Quoted 2 lines

TBH I'd prefer the former even if it isn't strictly coding style since it obscures the real change you made.

Ok. I will do it.

Quoted 15 lines

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1921

Quoted 10 lines
tools/libxc/xc_domain.c +6 -0
@@ -1878,6 +1913,25 @@ int xc_domain_bind_pt_isa_irq(
                                    PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
  }
 
+int xc_domain_bind_pt_spi_irq(
+    xc_interface *xch,
+    uint32_t domid,
+    uint16_t spi)
+{
+    /* vSPI == SPI */

I wonder if to avoid API churn later we should accept both machine and guest IRQ here and rely on the check that htey are the same in the hypervisor to reject?

Quoted 3 lines

Fair enough we can change libxc interface if we want, but avoiding a little churn later on seems reasonable and it makes it a better fit with the existing interfaces.

what about the following prototype:

int xc_domain_bind_pt_spi_irq(

    xc_interface *xch,
    uint32_t domid,
    uint16_t spi,
    uint16_t vspi)

I didn't reuse the current name i.e (machine_irq) because I find the name confusing.

[..]

Quoted 10 lines
Quoted 10 lines
tools/libxc/xc_domain.c +10 -0
+    case XEN_DOMCTL_unbind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;
Quoted 8 lines

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1935

Quoted 4 lines
tools/libxc/xc_domain.c +4 -0
+
+        /* For now map the interrupt 1:1 */
+        if ( irq != virq )
+            return -EINVAL;

The x86 version doesn't appear to consider irq_type or irq, only virq (from ->machine_irq). That seems to be logical to me (if a little underdocumented) and I think we should be consistent.

On x86, the parameters are used later in pt_create_bind which take the hypercall data in parameter.

The both check are required in order to avoid future issue if we introduce new type and when we will support virq != irq.

Regards,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

Quoted 13 lines · Julien Grall
Quoted 13 lines
Quoted 13 lines

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

I think this paragraph makes the previous bit obsolete?

Do you mean: "Concerning XSM..." until and the 2 paragraphs for XSM hypercalls?

That's right.

Quoted 21 lines
Quoted 15 lines

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1921

Quoted 10 lines
tools/libxc/xc_domain.c +6 -0
@@ -1878,6 +1913,25 @@ int xc_domain_bind_pt_isa_irq(
                                    PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
  }
 
+int xc_domain_bind_pt_spi_irq(
+    xc_interface *xch,
+    uint32_t domid,
+    uint16_t spi)
+{
+    /* vSPI == SPI */

I wonder if to avoid API churn later we should accept both machine and guest IRQ here and rely on the check that htey are the same in the hypervisor to reject?

Quoted 3 lines

Fair enough we can change libxc interface if we want, but avoiding a little churn later on seems reasonable and it makes it a better fit with the existing interfaces.

what about the following prototype:

Quoted 8 lines

int xc_domain_bind_pt_spi_irq(

     xc_interface *xch,
     uint32_t domid,
     uint16_t spi,
     uint16_t vspi)

I didn't reuse the current name i.e (machine_irq) because I find the name confusing.

Sure. Although you hit machine_irq again at the next level in the stack so I think it's rather moot.

Quoted 22 lines

[..]

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+    case XEN_DOMCTL_unbind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;
Quoted 8 lines

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 93

Quoted 4 lines
xen/arch/arm/domctl.c +4 -0
+
+        /* For now map the interrupt 1:1 */
+        if ( irq != virq )
+            return -EINVAL;

The x86 version doesn't appear to consider irq_type or irq, only virq (from ->machine_irq). That seems to be logical to me (if a little underdocumented) and I think we should be consistent.

Quoted 2 lines

On x86, the parameters are used later in pt_create_bind which take the hypercall data in parameter.

Ah yes, (although you mean pt_irq_destroy_bind I think?)

Quoted 2 lines

The both check are required in order to avoid future issue if we introduce new type and when we will support virq != irq.

Shouldn't they be in pt_irq_destroy_bind then?

Ian.

#10 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

Hi Ian,

Quoted 17 lines · Ian Campbell
Quoted 16 lines · Julien Grall
Quoted 16 lines
Quoted 15 lines

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I think this paragraph makes the previous bit obsolete?

Do you mean: "Concerning XSM..." until and the 2 paragraphs for XSM hypercalls?

That's right.

Ok. I will drop it.

Quoted 55 lines
Quoted 21 lines
Quoted 15 lines

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1921

Quoted 10 lines
tools/libxc/xc_domain.c +6 -0
@@ -1878,6 +1913,25 @@ int xc_domain_bind_pt_isa_irq(
                                     PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
   }
 
+int xc_domain_bind_pt_spi_irq(
+    xc_interface *xch,
+    uint32_t domid,
+    uint16_t spi)
+{
+    /* vSPI == SPI */

I wonder if to avoid API churn later we should accept both machine and guest IRQ here and rely on the check that htey are the same in the hypervisor to reject?

Quoted 3 lines

Fair enough we can change libxc interface if we want, but avoiding a little churn later on seems reasonable and it makes it a better fit with the existing interfaces.

what about the following prototype:

Quoted 8 lines

int xc_domain_bind_pt_spi_irq(

      xc_interface *xch,
      uint32_t domid,
      uint16_t spi,
      uint16_t vspi)

I didn't reuse the current name i.e (machine_irq) because I find the name confusing.

Sure. Although you hit machine_irq again at the next level in the stack so I think it's rather moot.

Quoted 22 lines

[..]

Quoted 18 lines
Quoted 4 lines
xen/arch/arm/domctl.c +4 -0
+    case XEN_DOMCTL_unbind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 93

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;
+
+        /* For now map the interrupt 1:1 */
+        if ( irq != virq )
+            return -EINVAL;

The x86 version doesn't appear to consider irq_type or irq, only virq (from ->machine_irq). That seems to be logical to me (if a little underdocumented) and I think we should be consistent.

Quoted 4 lines
Quoted 2 lines

On x86, the parameters are used later in pt_create_bind which take the hypercall data in parameter.

Ah yes, (although you mean pt_irq_destroy_bind I think?)

No I mean pt_irq_create_bind. The function makes usage of machine_irq and irq_type. Although, there is no clear switch case, just an if in the code.

Quoted 4 lines
Quoted 2 lines

The both check are required in order to avoid future issue if we introduce new type and when we will support virq != irq.

Shouldn't they be in pt_irq_destroy_bind then?

I'm not following you. pt_irq_destroy_bind is an x86 specific function.

The check "virq != irq" is done in both DOMCTL_{,un}bind_irq on ARM even though the one in unbind is not necessary useful.

Regards,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

Quoted 84 lines · Julien Grall

Hi Ian,

Quoted 17 lines · Ian Campbell
Quoted 16 lines · Julien Grall
Quoted 16 lines
Quoted 15 lines

Concerning XSM, even if ARM is using one hypercall rather than 2, the resulting check is nearly the same.

XSM PHYSDEVOP_map_pirq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to add the IRQ
3) Check if the target domain has permission to use the IRQ

XSM DOMCTL_bind_pirq_irq:
1) Check if the current domain can add resource to the domain
2) Check if the current domain has permission to bind the IRQ
3) Check if the target domain has permission to use the IRQ

In order to keep the same XSM checks done by the 2 hypercalls on x86, call both xsm_map_domain_irq & xsm_bind_pt_irq in the ARM implementation.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I think this paragraph makes the previous bit obsolete?

Do you mean: "Concerning XSM..." until and the 2 paragraphs for XSM hypercalls?

That's right.

Ok. I will drop it.

Quoted 55 lines
Quoted 21 lines
Quoted 15 lines

tools/libxc/xc_domain.c int xc_domain_bind_pt_isa_irq( · line 1921

Quoted 10 lines
tools/libxc/xc_domain.c +6 -0
@@ -1878,6 +1913,25 @@ int xc_domain_bind_pt_isa_irq(
                                     PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
   }
 
+int xc_domain_bind_pt_spi_irq(
+    xc_interface *xch,
+    uint32_t domid,
+    uint16_t spi)
+{
+    /* vSPI == SPI */

I wonder if to avoid API churn later we should accept both machine and guest IRQ here and rely on the check that htey are the same in the hypervisor to reject?

Quoted 3 lines

Fair enough we can change libxc interface if we want, but avoiding a little churn later on seems reasonable and it makes it a better fit with the existing interfaces.

what about the following prototype:

Quoted 8 lines

int xc_domain_bind_pt_spi_irq(

      xc_interface *xch,
      uint32_t domid,
      uint16_t spi,
      uint16_t vspi)

I didn't reuse the current name i.e (machine_irq) because I find the name confusing.

Sure. Although you hit machine_irq again at the next level in the stack so I think it's rather moot.

Quoted 22 lines

[..]

Quoted 18 lines
Quoted 4 lines
xen/arch/arm/domctl.c +4 -0
+    case XEN_DOMCTL_unbind_pt_irq:
+    {
+        int rc;
+        xen_domctl_bind_pt_irq_t *bind = &domctl->u.bind_pt_irq;

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 93

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+        uint32_t irq = bind->u.spi.spi;
+        uint32_t virq = bind->machine_irq;
+
+        /* We only support PT_IRQ_TYPE_SPI */
+        if ( bind->irq_type != PT_IRQ_TYPE_SPI )
+            return -EOPNOTSUPP;
+
+        /* For now map the interrupt 1:1 */
+        if ( irq != virq )
+            return -EINVAL;

The x86 version doesn't appear to consider irq_type or irq, only virq (from ->machine_irq). That seems to be logical to me (if a little underdocumented) and I think we should be consistent.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

On x86, the parameters are used later in pt_create_bind which take the hypercall data in parameter.

Ah yes, (although you mean pt_irq_destroy_bind I think?)

No I mean pt_irq_create_bind.

My initial comment was talking specifically about XEN_DOMCTL_unbind_pt_irq, which AFAICT does not call pt_irq_create_bind (which is why I assumed you must mean unbind).

I agree that XEN_DOMCTL_bind_pt_irq should contain checks of all its inputs, of course.

Quoted 7 lines

The function makes usage of machine_irq
and irq_type. Although, there is no clear switch case, just an if in the code.

Quoted 3 lines
Quoted 2 lines

The both check are required in order to avoid future issue if we introduce new type and when we will support virq != irq.

Quoted 6 lines
Quoted 1 line

Shouldn't they be in pt_irq_destroy_bind then?

I'm not following you. pt_irq_destroy_bind is an x86 specific function.

The check "virq != irq" is done in both DOMCTL_{,un}bind_irq on ARM even though the one in unbind is not necessary useful.

This was exactly my point, on x86 XEN_DOMCTL_unbind_pt_irq didn't appear to pay attention to anything other than the virq provided, I assumed since it doesn't need any other information from the caller to do as it has been asked.

But it seems like maybe I was wrong and pt_irq_destroy_bind (called from XEN_DOMCTL_unbind_pt_irq on x86) does actually need other info (I'm not sure why, it seems like it ought to know these things without being told by the toolstack). In which case having your check for consistency with x86 is probably tolerable.

Ian.

#12 lore raw

Re: [PATCH v5 p2 04/19] xen/arm: Implement hypercall DOMCTL_{, un}bind_pt_pirq

In reply to Ian Campbell
Quoted 6 lines · Ian Campbell
Quoted 1 line · Julien Grall

From: Julien Grall <[email protected]>

I've left the XSM related quotes untrimmed and CCd Daniel. I think it's all code motion (making x86 specific things generic), so perhaps no ack needed but an opportunity to nack instead ;-)

This seems correct to me. My initial thought when looking at this problem was that a distinct XSM hook for the ARM hypercall would be better, but other than the minor overhead from doing the IRQ label lookups twice, there is no real reason to prefer that method. This method has the advantage of not making more architecture-specific hooks which are sometimes harder to test/maintain.

#13 lore raw

PATCH v5 5/19 xen: guestcopy: Provide an helper to safely copy string from guest

In reply to Julien Grall

From: Julien Grall <[email protected]>

Flask code already provides a helper to copy a string from guest. In a later patch, the new DT hypercalls will need a similar function.

To avoid code duplication, copy the flask helper (flask_copying_string) to common code:
- Rename into safe_copy_string_from_guest
- Add comment to explain the extra +1
- Return the buffer directly and use the macros provided by xen/err.h to return an error code if necessary.

---
Changes in v5:
- return a char* rather than a void*
- Use '\0' rather than 0

Changes in v4:
- Use -ENOBUFS rather than -ENOENT
- Fix coding style in comment
- Typoes in commit message
- Convert the new flask_copying_string (for DT) in safe_copy_string_from_guest
- Add Ian and Daniel's ack

Changes in v3:
- Use macros of xen/err.h to return either the buffer or an error code
- Reuse size_t instead of unsigned long
- Update comment and commit message

Changes in v2:
- Rename copy_string_from_guest into safe_copy_string_from_guest
- Update commit message and comment in the code

---
 xen/common/Makefile            |  1 +
 xen/common/guestcopy.c         | 31 ++++++++++++++++++++++++++
 xen/include/xen/guest_access.h |  5 +++++
 xen/xsm/flask/flask_op.c       | 49 +++++++++++-------------------------------
 4 files changed, 50 insertions(+), 36 deletions(-)
 create mode 100644 xen/common/guestcopy.c
xen/common/Makefile +1 -0
index e5bd75b..74deac0 100644
@@ -9,6 +9,7 @@ obj-y += event_2l.o
 obj-y += event_channel.o
 obj-y += event_fifo.o
 obj-y += grant_table.o
+obj-y += guestcopy.o
 obj-y += irq.o
 obj-y += kernel.o
 obj-y += keyhandler.o
xen/common/guestcopy.c new file +31 -0
new file mode 100644
index 0000000..6ae1815
@@ -0,0 +1,31 @@
+#include <xen/config.h>
+#include <xen/lib.h>
+#include <xen/guest_access.h>
+#include <xen/err.h>
+
+/*
+ * The function copies a string from the guest and adds a NUL to
+ * make sure the string is correctly terminated.
+ */
+char *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
+                                  size_t size, size_t max_size)
+{
+    char *tmp;
+
+    if ( size > max_size )
+        return ERR_PTR(-ENOBUFS);
+
+    /* Add an extra +1 to append \0 */
+    tmp = xmalloc_array(char, size + 1);
+    if ( !tmp )
+        return ERR_PTR(-ENOMEM);
+
+    if ( copy_from_guest(tmp, u_buf, size) )
+    {
+        xfree(tmp);
+        return ERR_PTR(-EFAULT);
+    }
+    tmp[size] = '\0';
+
+    return tmp;
+}
xen/include/xen/guest_access.h +5 -0
index 373454e..09989df 100644
@@ -8,6 +8,8 @@
 #define __XEN_GUEST_ACCESS_H__
 
 #include <asm/guest_access.h>
+#include <xen/types.h>
+#include <public/xen.h>
 
 #define copy_to_guest(hnd, ptr, nr)                     \
     copy_to_guest_offset(hnd, 0, ptr, nr)
@@ -27,4 +29,7 @@
 #define __clear_guest(hnd, nr)                          \
     __clear_guest_offset(hnd, 0, nr)
 
+char *safe_copy_string_from_guest(XEN_GUEST_HANDLE(char) u_buf,
+                                  size_t size, size_t max_size);
+
 #endif /* __XEN_GUEST_ACCESS_H__ */
xen/xsm/flask/flask_op.c +13 -36

89 lines not on this page · raw message

Signature · 1 line
2.1.4
#14 lore raw

PATCH v5 6/19 xen/dts: Provide an helper to get a DT node from a path provided by a guest

In reply to Julien Grall

From: Julien Grall <[email protected]>

The maximum size of the copied string has been chosen based on the value use by XSM in similar case.

Furthermore, Linux seems to allow path up to 4096 characters. Though this could vary from one OS to another.

---
Changes in v4:
- Drop DEVICE_TREE_MAX_PATHLEN
- Bump the value to PAGE_SIZE (i.e 4096). It's used in XSM and this value seems sensible for Linux
- Clarify how the maximum size has been chosen

Changes in v3:
- Use the new prototype of safe_copy_string_from_guest

Changes in v2:
- guest_copy_string_from_guest has been renamed into safe_copy_string_from_guest

---
 xen/common/device_tree.c      | 18 ++++++++++++++++++
 xen/include/xen/device_tree.h | 14 ++++++++++++++
 2 files changed, 32 insertions(+)
xen/common/device_tree.c +18 -0
index 02cae91..31f169b 100644
@@ -13,6 +13,7 @@
 #include <xen/config.h>
 #include <xen/types.h>
 #include <xen/init.h>
+#include <xen/guest_access.h>
 #include <xen/device_tree.h>
 #include <xen/kernel.h>
 #include <xen/lib.h>
@@ -23,6 +24,7 @@
 #include <xen/cpumask.h>
 #include <xen/ctype.h>
 #include <asm/setup.h>
+#include <xen/err.h>
 
 const void *device_tree_flattened;
 dt_irq_xlate_func dt_irq_xlate;
@@ -277,6 +279,22 @@ struct dt_device_node *dt_find_node_by_path(const char *path)
     return np;
 }
 
+int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
+                          struct dt_device_node **node)
+{
+    char *path;
+
+    path = safe_copy_string_from_guest(u_path, u_plen, PAGE_SIZE);
+    if ( IS_ERR(path) )
+        return PTR_ERR(path);
+
+    *node = dt_find_node_by_path(path);
+
+    xfree(path);
+
+    return (*node == NULL) ? -ESRCH : 0;
+}
+
 struct dt_device_node *dt_find_node_by_alias(const char *alias)
 {
     const struct dt_alias_prop *app;
xen/include/xen/device_tree.h +14 -0
index 57eb3ee..e187780 100644
@@ -456,6 +456,20 @@ struct dt_device_node *dt_find_node_by_alias(const char *alias);
  */
 struct dt_device_node *dt_find_node_by_path(const char *path);
 
+
+/**
+ * dt_find_node_by_gpath - Same as dt_find_node_by_path but retrieve the
+ * path from the guest
+ *
+ * @u_path: Xen Guest handle to the buffer containing the path
+ * @u_plen: Length of the buffer
+ * @node: TODO
+ *
+ * Return 0 if succeed otherwise -errno
+ */
+int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
+                          struct dt_device_node **node);
+
 /**
  * dt_get_parent - Get a node's parent if any
  * @node: Node to get parent
Signature · 1 line
2.1.4
Acked-by on v5 6/19
#15 lore raw

Re: [PATCH v5 p2 06/19] xen/dts: Provide an helper to get a DT node from a path provided by a guest

Quoted 9 lines · Julien Grall

From: Julien Grall <[email protected]>

The maximum size of the copied string has been chosen based on the value use by XSM in similar case.

Furthermore, Linux seems to allow path up to 4096 characters. Though this could vary from one OS to another.

#16 lore raw

PATCH v5 7/19 xen/passthrough: Introduce iommu_construct

In reply to Julien Grall

From: Julien Grall <[email protected]>

This new function will correctly initialize the IOMMU page table for the current domain.

Also use it in iommu_assign_dt_device even though the current IOMMU implementation on ARM shares P2M with the processor.

---
Changes in v5:
- Limit the scope of rc
- Add Jan's and Ian's ack

Changes in v4:
- Move memory_type_changed in iommu_construct. Added by commit 06ed8cc "x86: avoid needless EPT table ajustment and cache flush"
- And an ASSERT and a comment in iommu_assign_dt_device to explain why the call is safe for DOM0

Changes in v3:
- The ASSERT in iommu_construct was redundant with the if ()
- Remove d->need_iommu = 1 in assign_device has it's already done by iommu_construct.
- Simplify the code in the caller of iommu_construct

Changes in v2:
- Add missing Signed-off-by
- Rename iommu_buildup to iommu_construct

---
 xen/drivers/passthrough/arm/iommu.c   |  6 ++++++
 xen/drivers/passthrough/device_tree.c | 12 ++++++++++++
 xen/drivers/passthrough/iommu.c       | 26 ++++++++++++++++++++++++++
 xen/drivers/passthrough/pci.c         | 22 ++++------------------
 xen/include/xen/iommu.h               |  2 ++
 5 files changed, 50 insertions(+), 18 deletions(-)
xen/drivers/passthrough/arm/iommu.c +6 -0
index 3007b99..9234657 100644
@@ -68,3 +68,9 @@ void arch_iommu_domain_destroy(struct domain *d)
 {
     iommu_dt_domain_destroy(d);
 }
+
+int arch_iommu_populate_page_table(struct domain *d)
+{
+    /* The IOMMU shares the p2m with the CPU */
+    return -ENOSYS;
+}
xen/drivers/passthrough/device_tree.c +12 -0
index 377d41d..4d82a09 100644
@@ -41,6 +41,18 @@ int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev)
     if ( !list_empty(&dev->domain_list) )
         goto fail;
 
+    if ( need_iommu(d) <= 0 )
+    {
+        /*
+         * The hwdom is forced to use IOMMU for protecting assigned
+         * device. Therefore the IOMMU data is already set up.
+         */
+        ASSERT(!is_hardware_domain(d));
+        rc = iommu_construct(d);
+        if ( rc )
+            goto fail;
+    }
+
     rc = hd->platform_ops->assign_device(d, 0, dt_to_dev(dev));
 
     if ( rc )
xen/drivers/passthrough/iommu.c +26 -0
index 92ea26f..ae42aae 100644
@@ -187,6 +187,32 @@ void iommu_teardown(struct domain *d)
     tasklet_schedule(&iommu_pt_cleanup_tasklet);
 }
 
+int iommu_construct(struct domain *d)
+{
+    if ( need_iommu(d) > 0 )
+        return 0;
+
+    if ( !iommu_use_hap_pt(d) )
+    {
+        int rc;
+
+        rc = arch_iommu_populate_page_table(d);
+        if ( rc )
+            return rc;
+    }
+
+    d->need_iommu = 1;
+    /*
+     * There may be dirty cache lines when a device is assigned
+     * and before need_iommu(d) becoming true, this will cause
+     * memory_type_changed lose effect if memory type changes.
+     * Call memory_type_changed here to amend this.
+     */
+    memory_type_changed(d);
+
+    return 0;
+}
+
 void iommu_domain_destroy(struct domain *d)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
xen/drivers/passthrough/pci.c +4 -18
index 9f3413c..af26423 100644
@@ -1358,25 +1358,11 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
     if ( !spin_trylock(&pcidevs_lock) )
         return -ERESTART;
 
-    if ( need_iommu(d) <= 0 )
+    rc = iommu_construct(d);
+    if ( rc )
     {
-        if ( !iommu_use_hap_pt(d) )
-        {
-            rc = arch_iommu_populate_page_table(d);
-            if ( rc )
-            {
-                spin_unlock(&pcidevs_lock);
-                return rc;
-            }
-        }
-        d->need_iommu = 1;
-        /*
-         * There may be dirty cache lines when a device is assigned
-         * and before need_iommu(d) becoming true, this will cause
-         * memory_type_changed lose effect if memory type changes.
-         * Call memory_type_changed here to amend this.
-         */
-        memory_type_changed(d);
+        spin_unlock(&pcidevs_lock);
+        return rc;
     }
 
     pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus, devfn);
xen/include/xen/iommu.h +2 -0
index bf4aff0..e9d2d5c 100644
@@ -65,6 +65,8 @@ int arch_iommu_domain_init(struct domain *d);
 int arch_iommu_populate_page_table(struct domain *d);
 void arch_iommu_check_autotranslated_hwdom(struct domain *d);
 
+int iommu_construct(struct domain *d);
+
 /* Function used internally, use iommu_domain_destroy */
 void iommu_teardown(struct domain *d);
 
Signature · 1 line
2.1.4
#17 lore raw

PATCH v5 8/19 xen/passthrough: arm: release the DT devices assigned to a guest earlier

In reply to Julien Grall

From: Julien Grall <[email protected]>

The toolstack may not have deassigned every device used by a guest. Therefore we have to go through the device list and remove them before asking the IOMMU drivers to release memory for this domain.

This can be done by moving the call to the release function when we relinquish the resources. The IOMMU part will be destroyed later when the domain is freed.

---
Changes in v5:
- Add Ian's ack

Changes in v4:
- Typoes in commit message
- Add Jan's ack
- iommu_release_dt_devices was only release the first device by mistake. Thanks for Robert VanVossen for spotting it.

Changes in v3:
- Patch added. Superseed the patch "xen/passthrough: call arch_iommu_domain_destroy before calling iommu teardown" in the previous patch series.

---
 xen/arch/arm/domain.c                 | 4 ++++
 xen/drivers/passthrough/arm/iommu.c   | 1 -
 xen/drivers/passthrough/device_tree.c | 7 ++++++-
 xen/include/xen/iommu.h               | 2 +-
 4 files changed, 11 insertions(+), 3 deletions(-)
xen/arch/arm/domain.c +4 -0
index 180bccc..24b8938 100644
@@ -795,6 +795,10 @@ int domain_relinquish_resources(struct domain *d)
     switch ( d->arch.relmem )
     {
     case RELMEM_not_started:
+        ret = iommu_release_dt_devices(d);
+        if ( ret )
+            return ret;
+
         d->arch.relmem = RELMEM_xen;
         /* Fallthrough */
 
xen/drivers/passthrough/arm/iommu.c +0 -1
index 9234657..95b1abb 100644
@@ -66,7 +66,6 @@ int arch_iommu_domain_init(struct domain *d)
 
 void arch_iommu_domain_destroy(struct domain *d)
 {
-    iommu_dt_domain_destroy(d);
 }
 
 int arch_iommu_populate_page_table(struct domain *d)
xen/drivers/passthrough/device_tree.c +6 -1
index 4d82a09..05ab274 100644
@@ -105,7 +105,7 @@ int iommu_dt_domain_init(struct domain *d)
     return 0;
 }
 
-void iommu_dt_domain_destroy(struct domain *d)
+int iommu_release_dt_devices(struct domain *d)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct dt_device_node *dev, *_dev;
@@ -115,7 +115,12 @@ void iommu_dt_domain_destroy(struct domain *d)
     {
         rc = iommu_deassign_dt_device(d, dev);
         if ( rc )
+        {
             dprintk(XENLOG_ERR, "Failed to deassign %s in domain %u\n",
                     dt_node_full_name(dev), d->domain_id);
+            return rc;
+        }
     }
+
+    return 0;
 }
xen/include/xen/iommu.h +1 -1
index e9d2d5c..d9c9ede 100644
@@ -117,7 +117,7 @@ void iommu_read_msi_from_ire(struct msi_desc *msi_desc, struct msi_msg *msg);
 int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev);
 int iommu_deassign_dt_device(struct domain *d, struct dt_device_node *dev);
 int iommu_dt_domain_init(struct domain *d);
-void iommu_dt_domain_destroy(struct domain *d);
+int iommu_release_dt_devices(struct domain *d);
 
 #endif /* HAS_DEVICE_TREE */
 
Signature · 1 line
2.1.4
#18 lore raw

PATCH v5 9/19 xen/passthrough: iommu_deassign_device_dt: By default reassign device to nobody

In reply to Julien Grall

From: Julien Grall <[email protected]>

Currently, when the device is deassigned from a domain, we directly reassign to DOM0.

As the device may not have been correctly reset, this may lead to corruption or expose some part of DOM0 memory. Also, we may have no way to reset some platform devices.

If Xen reassigns the device to "nobody", it may receive some global/context fault because the transaction has failed (indeed the context has been marked invalid). Unfortunately there is no simple way to quiesce a buggy hardware. I think we could live with that for a first version of platform device passthrough.

DOM0 will have to issue an hypercall to assign the device to itself if it wants to use it.

---
Note: This behavior is documented in a following patch which extend DOMCT_*assign_device to support non-PCI passthrough.

Changes in v5:
- Add Ian's ack

Changes in v4:
- Add Stefano's ack

Changes in v3:
- Use the coding style of the new SMMU drivers

Changes in v2:
- Fix typoes in the commit message
- Update commit message

---
 xen/drivers/passthrough/arm/smmu.c    | 8 +++++++-
 xen/drivers/passthrough/device_tree.c | 9 +++------
 2 files changed, 10 insertions(+), 7 deletions(-)
xen/drivers/passthrough/arm/smmu.c +7 -1
index 8a9b58b..65de50b 100644
@@ -2692,7 +2692,7 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 	int ret = 0;
 
 	/* Don't allow remapping on other domain than hwdom */
-	if (t != hardware_domain)
+	if (t && t != hardware_domain)
 		return -EPERM;
 
 	if (t == s)
@@ -2702,6 +2702,12 @@ static int arm_smmu_reassign_dev(struct domain *s, struct domain *t,
 	if (ret)
 		return ret;
 
+	if (t) {
+		ret = arm_smmu_assign_dev(t, devfn, dev);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
 
xen/drivers/passthrough/device_tree.c +3 -6
index 05ab274..0ec4103 100644
@@ -80,15 +80,12 @@ int iommu_deassign_dt_device(struct domain *d, struct dt_device_node *dev)
 
     spin_lock(&dtdevs_lock);
 
-    rc = hd->platform_ops->reassign_device(d, hardware_domain,
-                                           0, dt_to_dev(dev));
+    rc = hd->platform_ops->reassign_device(d, NULL, 0, dt_to_dev(dev));
     if ( rc )
         goto fail;
 
-    list_del(&dev->domain_list);
-
-    dt_device_set_used_by(dev, hardware_domain->domain_id);
-    list_add(&dev->domain_list, &domain_hvm_iommu(hardware_domain)->dt_devices);
+    list_del_init(&dev->domain_list);
+    dt_device_set_used_by(dev, DOMID_IO);
 
 fail:
     spin_unlock(&dtdevs_lock);
Signature · 1 line
2.1.4
#19 lore raw

PATCH v5 10/19 xen/iommu: arm: Wire iommu DOMCTL for ARM

In reply to Julien Grall

From: Julien Grall <[email protected]>

---
Changes in v4:
- Add Ian's ack

Changes in v3:
- Add Stefano's ack

Changes in v2:
- Don't move the call in common code.

---
 xen/arch/arm/domctl.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/arch/arm/domctl.c +10 -1
index 531bb13..fd1e610 100644
@@ -108,7 +108,16 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
         return 0;
     }
     default:
-        return subarch_do_domctl(domctl, d, u_domctl);
+    {
+        int rc;
+
+        rc = subarch_do_domctl(domctl, d, u_domctl);
+
+        if ( rc == -ENOSYS )
+            rc = iommu_do_domctl(domctl, d, u_domctl);
+
+        return rc;
+    }
     }
 }
 
Signature · 1 line
2.1.4
#20 lore raw

PATCH v5 11/19 xen/xsm: Add helpers to check permission for device tree passthrough

In reply to Julien Grall

From: Julien Grall <[email protected]>

This is a follow-up of commit 525ee49 "xsm: add device tree labeling support" which add support for device tree labelling in flask.

Those helpers will be use latter when non-pci passthrough (i.e device tree) will be added.

---
Changes in v5:
- Add Ian and Daniel's ack

Changes in v4:
- Patch added

---
 xen/include/xsm/dummy.h             | 23 +++++++++++++
 xen/include/xsm/xsm.h               | 27 +++++++++++++++
 xen/xsm/dummy.c                     |  6 ++++
 xen/xsm/flask/avc.c                 |  3 ++
 xen/xsm/flask/hooks.c               | 69 ++++++++++++++++++++++++++++++++++++-
 xen/xsm/flask/include/avc.h         |  2 ++
 xen/xsm/flask/policy/access_vectors |  2 +-
 7 files changed, 130 insertions(+), 2 deletions(-)
xen/include/xsm/dummy.h +23 -0
index c36e05f..05641f9 100644
@@ -350,6 +350,29 @@ static XSM_INLINE int xsm_deassign_device(XSM_DEFAULT_ARG struct domain *d, uint
 
 #endif /* HAS_PASSTHROUGH && HAS_PCI */
 
+#if defined(HAS_PASSTHROUGH) && defined(HAS_DEVICE_TREE)
+static XSM_INLINE int xsm_test_assign_dtdevice(XSM_DEFAULT_ARG const char *dtpath)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, NULL);
+}
+
+static XSM_INLINE int xsm_assign_dtdevice(XSM_DEFAULT_ARG struct domain *d,
+                                          const char *dtpath)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
+static XSM_INLINE int xsm_deassign_dtdevice(XSM_DEFAULT_ARG struct domain *d,
+                                            const char *dtpath)
+{
+    XSM_ASSERT_ACTION(XSM_HOOK);
+    return xsm_default_action(action, current->domain, d);
+}
+
+#endif /* HAS_PASSTHROUGH && HAS_DEVICE_TREE */
+
 static XSM_INLINE int xsm_resource_plug_core(XSM_DEFAULT_VOID)
 {
     XSM_ASSERT_ACTION(XSM_HOOK);
xen/include/xsm/xsm.h +27 -0
index b7446be..c830b47 100644
@@ -121,6 +121,12 @@ struct xsm_operations {
     int (*deassign_device) (struct domain *d, uint32_t machine_bdf);
 #endif
 
+#if defined(HAS_PASSTHROUGH) && defined(HAS_DEVICE_TREE)
+    int (*test_assign_dtdevice) (const char *dtpath);
+    int (*assign_dtdevice) (struct domain *d, const char *dtpath);
+    int (*deassign_dtdevice) (struct domain *d, const char *dtpath);
+#endif
+
     int (*resource_plug_core) (void);
     int (*resource_unplug_core) (void);
     int (*resource_plug_pci) (uint32_t machine_bdf);
@@ -473,6 +479,27 @@ static inline int xsm_deassign_device(xsm_default_t def, struct domain *d, uint3
 }
 #endif /* HAS_PASSTHROUGH && HAS_PCI) */
 
+#if defined(HAS_PASSTHROUGH) && defined(HAS_DEVICE_TREE)
+static inline int xsm_assign_dtdevice(xsm_default_t def, struct domain *d,
+                                      const char *dtpath)
+{
+    return xsm_ops->assign_dtdevice(d, dtpath);
+}
+
+static inline int xsm_test_assign_dtdevice(xsm_default_t def,
+                                           const char *dtpath)
+{
+    return xsm_ops->test_assign_dtdevice(dtpath);
+}
+
+static inline int xsm_deassign_dtdevice(xsm_default_t def, struct domain *d,
+                                        const char *dtpath)
+{
+    return xsm_ops->deassign_dtdevice(d, dtpath);
+}
+
+#endif /* HAS_PASSTHROUGH && HAS_DEVICE_TREE */
+
 static inline int xsm_resource_plug_pci (xsm_default_t def, uint32_t machine_bdf)
 {
     return xsm_ops->resource_plug_pci(machine_bdf);
xen/xsm/dummy.c +6 -0
index a3b8aab..ef856dc 100644
@@ -96,6 +96,12 @@ void xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, deassign_device);
 #endif
 
+#if defined(HAS_PASSTHROUGH) && defined(HAS_DEVICE_TREE)
+    set_to_dummy_if_null(ops, test_assign_dtdevice);
+    set_to_dummy_if_null(ops, assign_dtdevice);
+    set_to_dummy_if_null(ops, deassign_dtdevice);
+#endif
+
     set_to_dummy_if_null(ops, resource_plug_core);
     set_to_dummy_if_null(ops, resource_unplug_core);
     set_to_dummy_if_null(ops, resource_plug_pci);
xen/xsm/flask/avc.c +3 -0
index b1a4f8a..31bc702 100644
@@ -600,6 +600,9 @@ void avc_audit(u32 ssid, u32 tsid, u16 tclass, u32 requested,
     case AVC_AUDIT_DATA_MEMORY:
         avc_printk(&buf, "pte=%#lx mfn=%#lx ", a->memory.pte, a->memory.mfn);
         break;
+    case AVC_AUDIT_DATA_DTDEV:
+        avc_printk(&buf, "dtdevice=%s ", a->dtdev);
+        break;
     }
 
     avc_dump_query(&buf, ssid, tsid, tclass);
xen/xsm/flask/hooks.c +68 -1

87 lines not on this page · raw message

xen/xsm/flask/include/avc.h +2 -0
index c7a99fc..4283562 100644
@@ -39,6 +39,7 @@ struct avc_audit_data {
 #define AVC_AUDIT_DATA_IRQ   2
 #define AVC_AUDIT_DATA_RANGE 3
 #define AVC_AUDIT_DATA_MEMORY 4
+#define AVC_AUDIT_DATA_DTDEV 5
     struct domain *sdom;
     struct domain *tdom;
     union {
@@ -52,6 +53,7 @@ struct avc_audit_data {
             unsigned long pte;
             unsigned long mfn;
         } memory;
+        const char *dtdev;
     };
 };
 
xen/xsm/flask/policy/access_vectors +1 -1
index 128250e..5a760c5 100644
@@ -416,7 +416,7 @@ class resource
     remove_iomem
 # XEN_DOMCTL_get_device_group, XEN_DOMCTL_test_assign_device:
 #  source = domain making the hypercall
-#  target = PCI device being queried
+#  target = device being queried
     stat_device
 # XEN_DOMCTL_assign_device
     add_device
Signature · 1 line
2.1.4
#21 lore raw

PATCH v5 12/19 xen/passthrough: Extend XEN_DOMCTL_*assign_device to support DT device

In reply to Julien Grall

From: Julien Grall <[email protected]>

A device node is described by a path. It will be used to retrieved the node in the device tree and assign the related device to the domain.

Only non-PCI protected by an IOMMU can be assigned to a guest.

Also document the behavior of XEN_DOMCTL_deassign_device in the public headers which differ between non-PCI and PCI.

---
Changes in v5:
- Fix comment in public/domctl.h
- Remove unecessary comment in drivers/passthrough/device_tree.c
- Check d->is_dying before assigning a device (consistency with PCI code)
- Invert the if in iommu.c in order to avoid extra return
- Add Jan's ack for non-ARM part

Changes in v4:
- Add XSM bits
- Return -ENODEV rather than -ENOSYS
- Move the if (...) into the ifdef (see iommu.c)
- Document the behavior of XEN_DOMCTL_deassign_device
- Use PCI_BUS and PCI_DEVFN2 when it's possible
- iommu_dt_device_is_assigned now returns 0 when the device is not protected

Changes in v2:
- Use a different number for XEN_DOMCTL_assign_dt_device

---
 tools/libxc/include/xenctrl.h         |  10 ++++
 tools/libxc/xc_domain.c               |  95 ++++++++++++++++++++++++++++--
 xen/drivers/passthrough/device_tree.c | 108 +++++++++++++++++++++++++++++++++-
 xen/drivers/passthrough/iommu.c       |   7 ++-
 xen/drivers/passthrough/pci.c         |  47 ++++++++++-----
 xen/include/public/domctl.h           |  24 +++++++-
 xen/include/xen/iommu.h               |   3 +
 7 files changed, 269 insertions(+), 25 deletions(-)
tools/libxc/include/xenctrl.h +10 -0
index cc78ed6..a26d222 100644
@@ -2057,6 +2057,16 @@ int xc_deassign_device(xc_interface *xch,
                      uint32_t domid,
                      uint32_t machine_sbdf);
 
+int xc_assign_dt_device(xc_interface *xch,
+                        uint32_t domid,
+                        char *path);
+int xc_test_assign_dt_device(xc_interface *xch,
+                             uint32_t domid,
+                             char *path);
+int xc_deassign_dt_device(xc_interface *xch,
+                          uint32_t domid,
+                          char *path);
+
 int xc_domain_memory_mapping(xc_interface *xch,
                              uint32_t domid,
                              unsigned long first_gfn,
tools/libxc/xc_domain.c +91 -4

116 lines not on this page · raw message

xen/drivers/passthrough/device_tree.c +105 -3

131 lines not on this page · raw message

xen/drivers/passthrough/iommu.c +6 -1
index ae42aae..06cb38f 100644
@@ -335,7 +335,7 @@ int iommu_do_domctl(
     struct xen_domctl *domctl, struct domain *d,
     XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 {
-    int ret = -ENOSYS;
+    int ret = -ENODEV;
 
     if ( !iommu_enabled )
         return -ENOSYS;
@@ -344,6 +344,11 @@ int iommu_do_domctl(
     ret = iommu_do_pci_domctl(domctl, d, u_domctl);
 #endif
 
+#ifdef HAS_DEVICE_TREE
+    if ( ret == -ENODEV )
+        ret = iommu_do_dt_domctl(domctl, d, u_domctl);
+#endif
+
     return ret;
 }
 
xen/drivers/passthrough/pci.c +33 -14

92 lines not on this page · raw message

xen/include/public/domctl.h +21 -3
index d94c647..993944e 100644
@@ -475,12 +475,30 @@ typedef struct xen_domctl_sendtrigger xen_domctl_sendtrigger_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_sendtrigger_t);
 
 
-/* Assign PCI device to HVM guest. Sets up IOMMU structures. */
+/* Assign a device to a guest. Sets up IOMMU structures. */
 /* XEN_DOMCTL_assign_device */
 /* XEN_DOMCTL_test_assign_device */
-/* XEN_DOMCTL_deassign_device */
+/*
+ * XEN_DOMCTL_deassign_device: The behavior of this DOMCTL differs
+ * between the different type of device:
+ *  - PCI device (XEN_DOMCTL_DEV_PCI) will be reassigned to DOM0
+ *  - DT device (XEN_DOMCTL_DT_PCI) will left unassigned. DOM0
+ *  will have to call XEN_DOMCTL_assign_device in order to use the
+ *  device.
+ */
+#define XEN_DOMCTL_DEV_PCI      0
+#define XEN_DOMCTL_DEV_DT       1
 struct xen_domctl_assign_device {
-    uint32_t  machine_sbdf;   /* machine PCI ID of assigned device */
+    uint32_t dev;   /* XEN_DOMCTL_DEV_* */
+    union {
+        struct {
+            uint32_t machine_sbdf;   /* machine PCI ID of assigned device */
+        } pci;
+        struct {
+            uint32_t size; /* Length of the path */
+            XEN_GUEST_HANDLE_64(char) path; /* path to the device tree node */
+        } dt;
+    } u;
 };
 typedef struct xen_domctl_assign_device xen_domctl_assign_device_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_assign_device_t);
xen/include/xen/iommu.h +3 -0
index d9c9ede..b30bf41 100644
@@ -119,6 +119,9 @@ int iommu_deassign_dt_device(struct domain *d, struct dt_device_node *dev);
 int iommu_dt_domain_init(struct domain *d);
 int iommu_release_dt_devices(struct domain *d);
 
+int iommu_do_dt_domctl(struct xen_domctl *, struct domain *,
+                       XEN_GUEST_HANDLE_PARAM(xen_domctl_t));
+
 #endif /* HAS_DEVICE_TREE */
 
 struct page_info;
Signature · 1 line
2.1.4
Acked-by on v5 12/19
#22 lore raw

Re: [PATCH v5 p2 12/19] xen/passthrough: Extend XEN_DOMCTL_*assign_device to support DT device

Quoted 3 lines · Julien Grall

From: Julien Grall <[email protected]>

A device node is described by a path. It will be used to retrieved the

"retrieve"

Quoted 3 lines

node in the device tree and assign the related device to the domain.

Only non-PCI protected by an IOMMU can be assigned to a guest.

I think a word is missing between PCI and protected, perhaps "devices"?

Quoted 7 lines

Also document the behavior of XEN_DOMCTL_deassign_device in the public headers which differ between non-PCI and PCI.

With the above fixed:

#23 lore raw

Re: [PATCH v5 p2 12/19] xen/passthrough: Extend XEN_DOMCTL_*assign_device to support DT device

Hi Ian,

Quoted 5 lines · Ian Campbell
Quoted 3 lines · Julien Grall

From: Julien Grall <[email protected]>

A device node is described by a path. It will be used to retrieved the

Quoted 7 lines

"retrieve"

Quoted 3 lines

node in the device tree and assign the related device to the domain.

Only non-PCI protected by an IOMMU can be assigned to a guest.

I think a word is missing between PCI and protected, perhaps "devices"?

Yes.

Quoted 4 lines
Quoted 4 lines

Also document the behavior of XEN_DOMCTL_deassign_device in the public headers which differ between non-PCI and PCI.

Quoted 8 lines
Quoted 4 lines

With the above fixed:

Thanks,

Signature · 1 line
Julien Grall
#24 lore raw

PATCH v5 13/19 tools/libxl: Create a per-arch function to map IRQ to a domain

In reply to Julien Grall

From: Julien Grall <[email protected]>

ARM and x86 use a different hypercall to map an IRQ to a domain.

The hypercall to give IRQ permission to the domain has also been moved to be an x86 specific function as ARM guest won't be able to manage the IRQ. We may want to support it later.

---
Changes in v5:
- Use the new function xc_domain_bind_pt_spi_irq
- Fix typoes

Changes in v4:
- Patch added

---
 tools/libxl/libxl_arch.h   |  4 ++++
 tools/libxl/libxl_arm.c    |  5 +++++
 tools/libxl/libxl_create.c |  6 ++----
 tools/libxl/libxl_x86.c    | 13 +++++++++++++
 4 files changed, 24 insertions(+), 4 deletions(-)
tools/libxl/libxl_arch.h +4 -0
index cae64c0..77b1f2a 100644
@@ -39,4 +39,8 @@ int libxl__arch_vnuma_build_vmemrange(libxl__gc *gc,
                                       uint32_t domid,
                                       libxl_domain_build_info *b_info,
                                       libxl__domain_build_state *state);
+
+/* arch specific irq map function */
+int libxl__arch_domain_map_irq(libxl__gc *gc, uint32_t domid, int irq);
+
 #endif
tools/libxl/libxl_arm.c +5 -0
index 5a5cb3f..aa302fd 100644
@@ -742,6 +742,11 @@ int libxl__arch_vnuma_build_vmemrange(libxl__gc *gc,
     return libxl__vnuma_build_vmemrange_pv_generic(gc, domid, info, state);
 }
 
+int libxl__arch_domain_map_irq(libxl__gc *gc, uint32_t domid, int irq)
+{
+    return xc_domain_bind_pt_spi_irq(CTX->xch, domid, irq);
+}
+
 /*
  * Local variables:
  * mode: C
tools/libxl/libxl_create.c +2 -4
index e5a343f..15b464e 100644
@@ -1205,11 +1205,9 @@ static void domcreate_launch_dm(libxl__egc *egc, libxl__multidev *multidev,
 
         LOG(DEBUG, "dom%d irq %d", domid, irq);
 
-        ret = irq >= 0 ? xc_physdev_map_pirq(CTX->xch, domid, irq, &irq)
+        ret = irq >= 0 ? libxl__arch_domain_map_irq(gc, domid, irq)
                        : -EOVERFLOW;
-        if (!ret)
-            ret = xc_domain_irq_permission(CTX->xch, domid, irq, 1);
-        if (ret < 0) {
+        if (ret) {
             LOGE(ERROR, "failed give dom%d access to irq %d", domid, irq);
             ret = ERROR_FAIL;
             goto error_out;
tools/libxl/libxl_x86.c +13 -0
index 5e9a8d2..ed2bd38 100644
@@ -424,6 +424,19 @@ out:
     return rc;
 }
 
+int libxl__arch_domain_map_irq(libxl__gc *gc, uint32_t domid, int irq)
+{
+    int ret;
+
+    ret = xc_physdev_map_pirq(CTX->xch, domid, irq, &irq);
+    if (ret)
+        return ret;
+
+    ret = xc_domain_irq_permission(CTX->xch, domid, irq, 1);
+
+    return ret;
+}
+
 /*
  * Local variables:
  * mode: C
Signature · 1 line
2.1.4
Acked-by on v5 13/19
#25 lore raw

Re: [PATCH v5 p2 13/19] tools/libxl: Create a per-arch function to map IRQ to a domain

Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

ARM and x86 use a different hypercall to map an IRQ to a domain.

The hypercall to give IRQ permission to the domain has also been moved to be an x86 specific function as ARM guest won't be able to manage the IRQ. We may want to support it later.

(as I did v4 with the typoes fixed)

#26 lore raw

Re: [PATCH v5 p2 13/19] tools/libxl: Create a per-arch function to map IRQ to a domain

Hi Ian,

Quoted 2 lines · Ian Campbell

(as I did v4 with the typoes fixed)

I wasn't sure if I could keep your ack because the function used to map the IRQ wasn't the same on v4.

Regards,

Signature · 1 line
Julien Grall
#27 lore raw

Re: [PATCH v5 p2 13/19] tools/libxl: Create a per-arch function to map IRQ to a domain

Quoted 8 lines · Julien Grall

Hi Ian,

Quoted 2 lines · Ian Campbell

(as I did v4 with the typoes fixed)

I wasn't sure if I could keep your ack because the function used to map the IRQ wasn't the same on v4.

I wouldn't have minded, but no harm done.

Ian.

#28 lore raw

PATCH v5 14/19 tools/libxl: Check if fdt_{first, next}_subnode are present in libfdt

In reply to Julien Grall

From: Julien Grall <[email protected]>

The functions fdt_{fisrt,next}_subnode may not be available because:
* It has been introduced in 2013 => Doesn't work on Wheezy
* The prototype exists but the functions are not exposed. Don't ask why...

The later has been fixed recently in the dtc repo [1]

When the functions are not available, implement our own in order to use them in a following patch.

[1] git://git.kernel.org/pub/scm/utils/dtc/dtc.git

    commit a4b093f7366fdb429ca1781144d3985fa50d0fbb

---
tools/configure needs to be regenerate as this patch is modifying tools/configure.ac

Changes in v5:
- Add Ian's Signed-off-by for the license part and update the license
- Rename libxl_fdt to libxl_libfdt_compat.c

Changes in v4:
- Patch added

---
 tools/config.h.in                 |  6 +++
 tools/configure.ac                |  5 +++
 tools/libxl/Makefile              |  2 +-
 tools/libxl/libxl_libfdt_compat.c | 92 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 104 insertions(+), 1 deletion(-)
 create mode 100644 tools/libxl/libxl_libfdt_compat.c
tools/config.h.in +6 -0
index 2a0ae48..06f8b6c 100644
@@ -3,6 +3,12 @@
 /* Blktap2 enabled */
 #undef HAVE_BLKTAP2
 
+/* Define to 1 if you have the `fdt_first_subnode' function. */
+#undef HAVE_FDT_FIRST_SUBNODE
+
+/* Define to 1 if you have the `fdt_next_subnode' function. */
+#undef HAVE_FDT_NEXT_SUBNODE
+
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
tools/configure.ac +5 -0
index d31c2f3..cc13336 100644
@@ -355,6 +355,11 @@ AC_SUBST(libiconv)
 case "$host_cpu" in
 arm*|aarch64)
 AC_CHECK_LIB([fdt], [fdt_create], [], [AC_MSG_ERROR([Could not find libfdt])])
+
+# The functions fdt_{first,next}_subnode may not be available because:
+#   * It has been introduced in 2013 => Doesn't work on Wheezy
+#   * The prototype exists but the functions are not exposed. Don't ask why...
+AC_CHECK_FUNCS([fdt_first_subnode fdt_next_subnode])
 esac
 
 # Checks for header files.
tools/libxl/Makefile +1 -1
index 1b16598..2afb146 100644
@@ -59,7 +59,7 @@ endif
 LIBXL_OBJS-y += libxl_remus_device.o libxl_remus_disk_drbd.o
 
 LIBXL_OBJS-$(CONFIG_X86) += libxl_cpuid.o libxl_x86.o libxl_psr.o
-LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o
+LIBXL_OBJS-$(CONFIG_ARM) += libxl_nocpuid.o libxl_arm.o libxl_libfdt_compat.o
 
 ifeq ($(CONFIG_NetBSD),y)
 LIBXL_OBJS-y += libxl_netbsd.o
tools/libxl/libxl_libfdt_compat.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
2.1.4
Acked-by on v5 14/19
#29 lore raw

Re: [PATCH v5 p2 14/19] tools/libxl: Check if fdt_{first, next}_subnode are present in libfdt

Quoted 12 lines · Julien Grall

From: Julien Grall <[email protected]>

The functions fdt_{fisrt,next}_subnode may not be available because:
* It has been introduced in 2013 => Doesn't work on Wheezy
* The prototype exists but the functions are not exposed. Don't ask why...

The later has been fixed recently in the dtc repo [1]

When the functions are not available, implement our own in order to use them in a following patch.

Quoted 6 lines

[1] git://git.kernel.org/pub/scm/utils/dtc/dtc.git

    commit a4b093f7366fdb429ca1781144d3985fa50d0fbb
#30 lore raw

PATCH v5 15/19 tools/(lib)xl: Add partial device tree support for ARM

In reply to Julien Grall

From: Julien Grall <[email protected]>

Let the user to pass additional nodes to the guest device tree. For this purpose, everything in the node /passthrough from the partial device tree will be copied into the guest device tree.

The node /aliases will be also copied to allow the user to define aliases which can be used by the guest kernel.

A simple partial device tree will look like:

/dts-v1/;

/ {
        #address-cells = <2>;
        #size-cells = <2>;

        passthrough {
            compatible = "simple-bus";
            ranges;
            #address-cells = <2>;
            #size-cells = <2>;

            /* List of your nodes */
        }
};

Note that:
* The interrupt-parent property will be added by the toolstack in the root node
* The properties compatible, ranges, #address-cells and #size-cells in /passthrough are mandatory.

The helpers provided by the libfdt don't perform all the necessary security check on a given device tree. Therefore, only trusted device tree should be used.

---
An example of the partial device tree, as long as how to passthrough a non-pci device will be added to the tree in a follow-up patch.

A new LIBXL_HAVE_* will be added in the patch which add support for non-PCI passthrough as both are tight.

Changes in v5:
- Add a warning in the IDL
- Remove the requirement to use only the version 17 of the FDT format.

Changes in v4:
- Mark the option as unsafe
- The _fdt_* helpers has been moved in a separate patch/file. Only the prototype is declared
- The partial DT is considered valid. Remove some security check which make the code cleaner
- Typoes

Changes in v3:
- Patch added

---
 docs/man/xl.cfg.pod.5       |  10 +++
 tools/libxl/libxl_arm.c     | 157 ++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_types.idl |   4 ++
 tools/libxl/xl_cmdimpl.c    |   1 +
 4 files changed, 172 insertions(+)
docs/man/xl.cfg.pod.5 +10 -0
index f936dfc..ad95a9a 100644
@@ -460,6 +460,16 @@ not emulated.
 Specify that this domain is a driver domain. This enables certain
 features needed in order to run a driver domain.
 
+=item B<device_tree=PATH>
+
+Specify a partial device tree (compiled via the Device Tree Compiler).
+Everything under the node "/passthrough" will be copied into the guest
+device tree. For convenience, the node "/aliases" is also copied to allow
+the user to defined aliases which can be used by the guest kernel.
+
+Given the complexity of verifying the validity of a device tree, this
+option should only be used with trusted device tree.
+
 =back
 
 =head2 Devices
tools/libxl/libxl_arm.c +157 -0

183 lines not on this page · raw message

tools/libxl/libxl_types.idl +4 -0
index 0866433..9aada5a 100644
@@ -411,6 +411,10 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("kernel",           string),
     ("cmdline",          string),
     ("ramdisk",          string),
+    # Given the complexity of verifying the validity of a device tree,
+    # libxl doesn't do any security check on it. It's the responsability
+    # of the caller to provide only trusted device tree.
+    ("device_tree",      string),
     ("u", KeyedUnion(None, libxl_domain_type, "type",
                 [("hvm", Struct(None, [("firmware",         string),
                                        ("bios",             libxl_bios_type),
tools/libxl/xl_cmdimpl.c +1 -0
index 394b55d..c2415ba 100644
@@ -1379,6 +1379,7 @@ static void parse_config_data(const char *config_source,
 
     xlu_cfg_replace_string (config, "kernel", &b_info->kernel, 0);
     xlu_cfg_replace_string (config, "ramdisk", &b_info->ramdisk, 0);
+    xlu_cfg_replace_string (config, "device_tree", &b_info->device_tree, 0);
     b_info->cmdline = parse_cmdline(config);
 
     xlu_cfg_get_defbool(config, "driver_domain", &c_info->driver_domain, 0);
Signature · 1 line
2.1.4
Acked-by on v5 15/19
#31 lore raw

Re: [PATCH v5 p2 15/19] tools/(lib)xl: Add partial device tree support for ARM

Julien Grall writes ("[PATCH v5 p2 15/19] tools/(lib)xl: Add partial device tree support for ARM"):

Quoted 3 lines

Let the user to pass additional nodes to the guest device tree. For this purpose, everything in the node /passthrough from the partial device tree will be copied into the guest device tree.

Thanks for explaining to me in person what I was missing about the semantics of DTs. As I said, you may wish to explain some of this in the commit message or some of the in-tree documents, perhaps with references. I will leave that up to you.

In any case,

Although:

tools/libxl/libxl_types.idl libxl_domain_build_info = Struct("domain_build_info",[ · line 415

Quoted 2 lines
tools/libxl/libxl_types.idl +2 -0
+    # Given the complexity of verifying the validity of a device tree,
+    # libxl doesn't do any security check on it. It's the responsability

Spelling mistake - should be "responsibility".

Thanks,
Ian.

#32 lore raw

Re: [PATCH v5 p2 15/19] tools/(lib)xl: Add partial device tree support for ARM

Hi Ian,

Quoted 9 lines · Ian Jackson

Julien Grall writes ("[PATCH v5 p2 15/19] tools/(lib)xl: Add partial device tree support for ARM"):

Quoted 3 lines

Let the user to pass additional nodes to the guest device tree. For this purpose, everything in the node /passthrough from the partial device tree will be copied into the guest device tree.

Thanks for explaining to me in person what I was missing about the semantics of DTs. As I said, you may wish to explain some of this in the commit message or some of the in-tree documents, perhaps with references. I will leave that up to you.

I will add a link to [1] in the in-tree documentation (patch #19).

[1] http://www.devicetree.org/Device_Tree_Usage#Basic_Data_Format

Quoted 10 lines

In any case,

Although:

tools/libxl/libxl_types.idl libxl_domain_build_info = Struct("domain_build_info",[ · line 415

Quoted 2 lines
tools/libxl/libxl_types.idl +2 -0
+    # Given the complexity of verifying the validity of a device tree,
+    # libxl doesn't do any security check on it. It's the responsability

Spelling mistake - should be "responsibility".

I will fix it.

Thanks,

Signature · 1 line
Julien Grall
Acked-by on v5 15/19
#33 lore raw

Re: [PATCH v5 p2 15/19] tools/(lib)xl: Add partial device tree support for ARM

In reply to Julien Grall
Quoted 3 lines · Julien Grall

From: Julien Grall <[email protected]>

Let the user to pass additional nodes to the guest device tree. For

"Allow the user to pass" or "Let the user pass".

I prefer the first FWIW.

Quoted 3 lines

With one of the above:

#34 lore raw

PATCH v5 16/19 tools/libxl: arm: Use an higher value for the GIC phandle

In reply to Julien Grall

From: Julien Grall <[email protected]>

The partial device tree may contains phandle. The Device Tree Compiler tends to allocate the phandle from 1.

Reserve the ID 65000 for the GIC phandle. I think we can safely assume that the partial device tree will never contain a such ID.

---
To allocate dynamically the phandle, we would need to fill in post-hoc (like we do with e.g the initramfs location) the #interrupt-parent in "/". That would also require some refactoring in the code to pass the phandle every time.

Defer this solution to a follow-up in order as having 65000 would be very unlikely.

Changes in v5:
- Add Ian's Ack.

Changes in v3:
- Patch added

---
 tools/libxl/libxl_arm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/libxl/libxl_arm.c +5 -4
index 2ce7e23..cf1379d 100644
@@ -80,10 +80,11 @@ static struct arch_info {
     {"xen-3.0-aarch64", "arm,armv8-timer", "arm,armv8" },
 };
 
-enum {
-    PHANDLE_NONE = 0,
-    PHANDLE_GIC,
-};
+/*
+ * The device tree compiler (DTC) is allocating the phandle from 1 to
+ * onwards. Reserve a high value for the GIC phandle.
+ */
+#define PHANDLE_GIC (65000)
 
 typedef uint32_t be32;
 typedef be32 gic_interrupt[3];
Signature · 1 line
2.1.4
#35 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Julien Grall writes ("[PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 2 lines

The partial device tree may contains phandle. The Device Tree Compiler tends to allocate the phandle from 1.

I have to say I have no idea what a phandle is...

Quoted 2 lines

Reserve the ID 65000 for the GIC phandle. I think we can safely assume that the partial device tree will never contain a such ID.

Do we control the DT compiler ? What if it should change its phandle allocation algorithm ?

Quoted 4 lines

+/*
+ * The device tree compiler (DTC) is allocating the phandle from 1 to
+ * onwards. Reserve a high value for the GIC phandle.
+ */

FYI this should read "The device tree compiler (DTC) allocates phandle values frrom 1 onwards".

Thanks,
Ian.

#36 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Hi Ian,

Quoted 5 lines · Ian Jackson

Julien Grall writes ("[PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 2 lines

The partial device tree may contains phandle. The Device Tree Compiler tends to allocate the phandle from 1.

I have to say I have no idea what a phandle is...

A phandle is a way to reference another node in the device tree. Any node that can referenced defines a phandle property with a unique unsigned 32 bit value.

Quoted 5 lines
Quoted 2 lines

Reserve the ID 65000 for the GIC phandle. I think we can safely assume that the partial device tree will never contain a such ID.

Do we control the DT compiler ? What if it should change its phandle allocation algorithm ?

We don't control the DT compiler. But the algorithm of the phandle will unlikely change. FWIW, the compiler is very tiny, it's not GCC.

I only expect people using the partial device tree in very specific use case. Generic use case is not even possible with the current status of non-PCI (i.e device tree) passthrough. So people control their environment.

As I said later in patch, supporting dynamic allocation will require some rework in the device tree creation for the guest.

So I was suggesting this solution as temporary in order to not block the DT passthrough.

Quoted 7 lines
Quoted 4 lines

+/*
+ * The device tree compiler (DTC) is allocating the phandle from 1 to
+ * onwards. Reserve a high value for the GIC phandle.
+ */

FYI this should read "The device tree compiler (DTC) allocates phandle values frrom 1 onwards".

Thanks, I will update the comment.

Regards,

Signature · 1 line
Julien Grall
#37 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Julien Grall writes ("Re: [Xen-devel] [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 6 lines · Ian Jackson
Quoted 1 line

I have to say I have no idea what a phandle is...

A phandle is a way to reference another node in the device tree. Any node that can referenced defines a phandle property with a unique unsigned 32 bit value.

Thanks for the explanation.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Reserve the ID 65000 for the GIC phandle. I think we can safely assume that the partial device tree will never contain a such ID.

Do we control the DT compiler ? What if it should change its phandle allocation algorithm ?

We don't control the DT compiler. But the algorithm of the phandle will unlikely change. FWIW, the compiler is very tiny, it's not GCC.

Right.

Quoted 9 lines

I only expect people using the partial device tree in very specific use case. Generic use case is not even possible with the current status of non-PCI (i.e device tree) passthrough. So people control their environment.

As I said later in patch, supporting dynamic allocation will require some rework in the device tree creation for the guest.

So I was suggesting this solution as temporary in order to not block the DT passthrough.

What would happen if our assumption about the DT compiler were violated ?

Ian.

#38 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Quoted 6 lines · Ian Jackson

Julien Grall writes ("Re: [Xen-devel] [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 5 lines · Ian Jackson

I only expect people using the partial device tree in very specific use case. Generic use case is not even possible with the current status of non-PCI (i.e device tree) passthrough. So people control their environment.

Quoted 8 lines
Quoted 5 lines

As I said later in patch, supporting dynamic allocation will require some rework in the device tree creation for the guest.

So I was suggesting this solution as temporary in order to not block the DT passthrough.

What would happen if our assumption about the DT compiler were violated ?

The phandle would be present in 2 different nodes of the DT. FYI, that may also happen if a user use 2 times the same phandle in the partial DT.

The guest may retrieve the wrong node and warn/crash depending on the implementation.

Although, it won't impact neither Xen nor the toolstack.

Regards,

Signature · 1 line
Julien Grall
#39 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Quoted 16 lines · Julien Grall
Quoted 6 lines · Ian Jackson

Julien Grall writes ("Re: [Xen-devel] [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 5 lines · Ian Jackson

I only expect people using the partial device tree in very specific use case. Generic use case is not even possible with the current status of non-PCI (i.e device tree) passthrough. So people control their environment.

Quoted 8 lines
Quoted 5 lines

As I said later in patch, supporting dynamic allocation will require some rework in the device tree creation for the guest.

So I was suggesting this solution as temporary in order to not block the DT passthrough.

What would happen if our assumption about the DT compiler were violated ?

Quoted 7 lines

The phandle would be present in 2 different nodes of the DT. FYI, that may also happen if a user use 2 times the same phandle in the partial DT.

The guest may retrieve the wrong node and warn/crash depending on the implementation.

Although, it won't impact neither Xen nor the toolstack.

(To be more complete) Because Xen and the toolstack doesn't made any usage of the phandle. The toolstack only generates the device tree and copy necessary node from the partial DT.

Signature · 1 line
Julien Grall
#40 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

In reply to Julien Grall

Julien Grall writes ("Re: [Xen-devel] [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 6 lines · Ian Jackson
Quoted 2 lines

What would happen if our assumption about the DT compiler were violated ?

The phandle would be present in 2 different nodes of the DT. FYI, that may also happen if a user use 2 times the same phandle in the partial DT.

So we are making an assumption about the DT compiler which we don't check, and when the assumption is violated, we produce a corrupted DT which produces undefined behaviour in the guest.

Can we do some kind of check ?

Ian.

#41 lore raw

Re: [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle

Hi Ian,

Quoted 5 lines · Ian Jackson

Julien Grall writes ("Re: [Xen-devel] [PATCH v5 p2 16/19] tools/libxl: arm: Use an higher value for the GIC phandle"):

Quoted 4 lines · Ian Jackson
Quoted 2 lines

What would happen if our assumption about the DT compiler were violated ?

Quoted 8 lines
Quoted 2 lines

The phandle would be present in 2 different nodes of the DT. FYI, that may also happen if a user use 2 times the same phandle in the partial DT.

So we are making an assumption about the DT compiler which we don't check, and when the assumption is violated, we produce a corrupted DT which produces undefined behaviour in the guest.

Can we do some kind of check ?

As discussed IRL, I will add a note in the documentation about this restriction.

This will be removed when the phandle will be generated dynamically.

Regards,

Signature · 1 line
Julien Grall
#42 lore raw

PATCH v5 17/19 libxl: Add support for Device Tree passthrough

In reply to Julien Grall

From: Julien Grall <[email protected]>

On ARM, every non-PCI device are described in the device tree. Each of them can be found via a path.

This patch introduces a very basic support, only the IOMMU will be set up correctly. The user will have to:
- Describe the device in the partial device tree
- Map manually MMIO/IRQ

This is a first approach, that will allow to have a basic Device Tree passthrough support in Xen. This could be improved later.

Furthermore add LIBXL_HAVE_DEVICETREE_PASSTHROUGH to indicate we support Device Tree passthrough and partial device tree (introduced by a previous patch).

---
Changes in v5:
- Replace some "non-PCI" to "Device Tree"

Changes in v4:
- Add LIBXL_HAVE_DEVICTREE_PASSTHROUGH to indicate we support non-PCI passthrough. This is also used in order to indicate partial device tree is supported
- Remove libxl_dtdev.c as it contains only a 2 lines functions and call directly xc_* from libxl_create.c
- Introduce domcreate_attach_dtdev

Changes in v3:
- Dynamic allocation has been dropped
- Rework the commit message in accordance with the previous item

Changes in v2:
- Get DT infos earlier
- Allocate/map IRQ in libxl__arch_domain_create rather than in libxl__device_dt_add
- Use VIRQ rather than the PIRQ to construct the interrupts properties of the device tree
- Correct cpumask in make_dtdev_node. We allow the interrupt to be used on the 8 CPUs
- Fix LOGE when we map the MMIO region in the guest in libxl__device_dt_add. The domain and the IRQ were inverted
- Calculate the number of SPIs to configure the VGIC
- xc_physdev_dtdev_* helpers has been renamed to xc_dtdev_*
- Rename libxl_device_dt to libxl_device_dtdev

---
 tools/libxl/libxl.h          |  6 ++++++
 tools/libxl/libxl_create.c   | 32 ++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |  5 +++++
 tools/libxl/libxl_types.idl  |  5 +++++
 4 files changed, 48 insertions(+)
tools/libxl/libxl.h +6 -0
index 6bc75c5..029354d 100644
@@ -179,6 +179,12 @@
 #define LIBXL_HAVE_BUILDINFO_HVM_MMIO_HOLE_MEMKB 1
 
 /*
+ * libxl_domain_build_info has device_tree and libxl_device_dtdev
+ * exists. This mean Device Tree passthrough is supported for ARM
+ */
+#define LIBXL_HAVE_DEVICETREE_PASSTHROUGH 1
+
+/*
  * libxl ABI compatibility
  *
  * The only guarantee which libxl makes regarding ABI compatibility
tools/libxl/libxl_create.c +32 -0
index 15b464e..39c828b 100644
@@ -751,6 +751,8 @@ static void domcreate_attach_vtpms(libxl__egc *egc, libxl__multidev *multidev,
                                    int ret);
 static void domcreate_attach_pci(libxl__egc *egc, libxl__multidev *aodevs,
                                  int ret);
+static void domcreate_attach_dtdev(libxl__egc *egc,
+                                   libxl__domain_create_state *dcs);
 
 static void domcreate_console_available(libxl__egc *egc,
                                         libxl__domain_create_state *dcs);
@@ -1444,6 +1446,36 @@ static void domcreate_attach_pci(libxl__egc *egc, libxl__multidev *multidev,
         }
     }
 
+    domcreate_attach_dtdev(egc, dcs);
+    return;
+
+error_out:
+    assert(ret);
+    domcreate_complete(egc, dcs, ret);
+}
+
+static void domcreate_attach_dtdev(libxl__egc *egc,
+                                   libxl__domain_create_state *dcs)
+{
+    STATE_AO_GC(dcs->ao);
+    int i;
+    int ret;
+    int domid = dcs->guest_domid;
+
+    /* convenience aliases */
+    libxl_domain_config *const d_config = dcs->guest_config;
+
+    for (i = 0; i < d_config->num_dtdevs; i++) {
+        const libxl_device_dtdev *dtdev = &d_config->dtdevs[i];
+
+        LOG(DEBUG, "Assign device \"%s\" to dom%u", dtdev->path, domid);
+        ret = xc_assign_dt_device(CTX->xch, domid, dtdev->path);
+        if (ret < 0) {
+            LOG(ERROR, "xc_assign_dtdevice failed: %d\n", ret);
+            goto error_out;
+        }
+    }
+
     domcreate_console_available(egc, dcs);
 
     domcreate_complete(egc, dcs, 0);
tools/libxl/libxl_internal.h +5 -0
index 3aba221..5a7e791 100644
@@ -1199,6 +1199,11 @@ _hidden int libxl__create_pci_backend(libxl__gc *gc, uint32_t domid,
                                       libxl_device_pci *pcidev, int num);
 _hidden int libxl__device_pci_destroy_all(libxl__gc *gc, uint32_t domid);
 
+/* from libxl_dtdev */
+
+_hidden int libxl__device_dt_add(libxl__gc *gc, uint32_t domid,
+                                 const libxl_device_dtdev *dtdev);
+
 /*----- xswait: wait for a xenstore node to be suitable -----*/
 
 typedef struct libxl__xswait_state libxl__xswait_state;
tools/libxl/libxl_types.idl +5 -0
index 9aada5a..4182cbd 100644
@@ -537,6 +537,10 @@ libxl_device_pci = Struct("device_pci", [
     ("seize", bool),
     ])
 
+libxl_device_dtdev = Struct("device_dtdev", [
+    ("path", string),
+    ])
+
 libxl_device_vtpm = Struct("device_vtpm", [
     ("backend_domid",    libxl_domid),
     ("backend_domname",  string),
@@ -563,6 +567,7 @@ libxl_domain_config = Struct("domain_config", [
     ("disks", Array(libxl_device_disk, "num_disks")),
     ("nics", Array(libxl_device_nic, "num_nics")),
     ("pcidevs", Array(libxl_device_pci, "num_pcidevs")),
+    ("dtdevs", Array(libxl_device_dtdev, "num_dtdevs")),
     ("vfbs", Array(libxl_device_vfb, "num_vfbs")),
     ("vkbs", Array(libxl_device_vkb, "num_vkbs")),
     ("vtpms", Array(libxl_device_vtpm, "num_vtpms")),
Signature · 1 line
2.1.4
Acked-by on v5 17/19
#43 lore raw

Re: [PATCH v5 p2 17/19] libxl: Add support for Device Tree passthrough

Julien Grall writes ("[PATCH v5 p2 17/19] libxl: Add support for Device Tree passthrough"):

Quoted 10 lines

From: Julien Grall <[email protected]>

On ARM, every non-PCI device are described in the device tree. Each of them can be found via a path.

This patch introduces a very basic support, only the IOMMU will be set up correctly. The user will have to:
- Describe the device in the partial device tree
- Map manually MMIO/IRQ

Quoted 6 lines

This is a first approach, that will allow to have a basic Device Tree passthrough support in Xen. This could be improved later.

Furthermore add LIBXL_HAVE_DEVICETREE_PASSTHROUGH to indicate we support Device Tree passthrough and partial device tree (introduced by a previous patch).

Acked-by on v5 17/19
#44 lore raw

Re: [PATCH v5 p2 17/19] libxl: Add support for Device Tree passthrough

In reply to Julien Grall
Quoted 10 lines · Julien Grall

From: Julien Grall <[email protected]>

On ARM, every non-PCI device are described in the device tree. Each of them can be found via a path.

This patch introduces a very basic support, only the IOMMU will be set up correctly. The user will have to:
- Describe the device in the partial device tree
- Map manually MMIO/IRQ

Quoted 8 lines

This is a first approach, that will allow to have a basic Device Tree passthrough support in Xen. This could be improved later.

Furthermore add LIBXL_HAVE_DEVICETREE_PASSTHROUGH to indicate we support Device Tree passthrough and partial device tree (introduced by a previous patch).

#45 lore raw

PATCH v5 18/19 xl: Add new option dtdev

In reply to Julien Grall

From: Julien Grall <[email protected]>

The option "dtdev" will be used to passthrough a device described in the device tree to a guest.

---
Changes in v5:
- Drop "non-PCI" in the commit message
- Add Ian's ack

Changes in v4:
- Typoes in the documentation
- Wrap the line in xl_cmdimpl.c

Changes in v2:
- libxl_device_dt has been rename to libxl_device_dtdev
- use xrealloc instead of realloc

---
 docs/man/xl.cfg.pod.5    |  5 +++++
 tools/libxl/xl_cmdimpl.c | 22 +++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
docs/man/xl.cfg.pod.5 +5 -0
index ad95a9a..8357ebe 100644
@@ -786,6 +786,11 @@ More information about Xen gfx_passthru feature is available
 on the XenVGAPassthrough L<http://wiki.xen.org/wiki/XenVGAPassthrough>
 wiki page.
 
+=item B<dtdev=[ "DTDEV_PATH", "DTDEV_PATH", ... ]>
+
+Specifies the host device tree nodes to passthrough to this guest. Each
+DTDEV_PATH is the absolute path in the device tree.
+
 =item B<ioports=[ "IOPORT_RANGE", "IOPORT_RANGE", ... ]>
 
 Allow guest to access specific legacy I/O ports. Each B<IOPORT_RANGE>
tools/libxl/xl_cmdimpl.c +21 -1
index c2415ba..ed82b6a 100644
@@ -1169,7 +1169,7 @@ static void parse_config_data(const char *config_source,
     long l, vcpus = 0;
     XLU_Config *config;
     XLU_ConfigList *cpus, *vbds, *nics, *pcis, *cvfbs, *cpuids, *vtpms;
-    XLU_ConfigList *channels, *ioports, *irqs, *iomem, *viridian;
+    XLU_ConfigList *channels, *ioports, *irqs, *iomem, *viridian, *dtdevs;
     int num_ioports, num_irqs, num_iomem, num_cpus, num_viridian;
     int pci_power_mgmt = 0;
     int pci_msitranslate = 0;
@@ -1941,6 +1941,26 @@ skip_vfb:
             libxl_defbool_set(&b_info->u.pv.e820_host, true);
     }
 
+    if (!xlu_cfg_get_list (config, "dtdev", &dtdevs, 0, 0)) {
+        d_config->num_dtdevs = 0;
+        d_config->dtdevs = NULL;
+        for (i = 0; (buf = xlu_cfg_get_listitem(dtdevs, i)) != NULL; i++) {
+            libxl_device_dtdev *dtdev;
+
+            d_config->dtdevs = xrealloc(d_config->dtdevs,
+                                        sizeof (libxl_device_dtdev) * (i + 1));
+            dtdev = d_config->dtdevs + d_config->num_dtdevs;
+            libxl_device_dtdev_init(dtdev);
+
+            dtdev->path = strdup(buf);
+            if (dtdev->path == NULL) {
+                fprintf(stderr, "unable to duplicate string for dtdevs\n");
+                exit(-1);
+            }
+            d_config->num_dtdevs++;
+        }
+    }
+
     switch (xlu_cfg_get_list(config, "cpuid", &cpuids, 0, 1)) {
     case 0:
         {
Signature · 1 line
2.1.4
#46 lore raw

PATCH v5 19/19 docs/misc: arm: Add documentation about Device Tree passthrough

In reply to Julien Grall

From: Julien Grall <[email protected]>

Note that the example is done on Midway whose SMMU driver is not supported on Xen upstream.

Currently, I don't have other platform where I can test Device Tree passthrough.

---
Changes in v5:
- Drop references to "non-PCI" in favor of "Device Tree"
- Typoes and update the docs

Changes in v4:
- Patch added

---
 docs/misc/arm/passthrough.txt | 61 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 docs/misc/arm/passthrough.txt
docs/misc/arm/passthrough.txt new file +61 -0
new file mode 100644
index 0000000..93da99c
@@ -0,0 +1,61 @@
+Passthrough a device described in the Device Tree to a guest
+============================================================
+
+The example will use the secondary network card for the midway server.
+
+1) Mark the device to let Xen know the device will be used for passthrough.
+This is done in the device tree node describing the device by adding the
+property "xen,passthrough". The command to do it in U-Boot is:
+
+    fdt set /soc/ethernet@fff51000 xen,passthrough
+
+2) Create a partial device tree describing the device. The IRQ are mapped
+1:1 to the guest (i.e VIRQ == IRQ). For MMIO, you will have to find a hole
+in the guest memory layout (see xen/include/public/arch-arm.h, note that
+the layout is not stable and can change between versions of Xen).
+
+/dts-v1/;
+
+/ {
+    /* #*cells are here to keep DTC happy */
+    #address-cells = <2>;
+    #size-cells = <2>;
+
+    aliases {
+        net = &mac0;
+    };
+
+    passthrough {
+        compatible = "simple-bus";
+        ranges;
+        #address-cells = <2>;
+        #size-cells = <2>;
+        mac0: ethernet@10000000 {
+            compatible = "calxeda,hb-xgmac";
+            reg = <0 0x10000000 0 0x1000>;
+            interrupts = <0 80 4  0 81 4  0 82 4>;
+        };
+    };
+};
+
+Note:
+    * The interrupt-parent property will be added by the toolstack in the
+    root node;
+    * The following properies are mandatory with the /passthrough node:
+        - compatible: It should always contain "simple-bus"
+        - ranges
+        - #address-cells
+        - #size-cells
+
+3) Compile the partial guest device with dtc (Device Tree Compiler).
+For our purpose, the compiled file will be called guest-midway.dtb and
+placed in /root in DOM0.
+
+3) Add the following options in the guest configuration file:
+
+device_tree = "/root/guest-midway.dtb"
+dtdev = [ "/soc/ethernet@fff51000" ]
+irqs = [ 112, 113, 114 ]
+iomem = [ "0xfff51,1@0x10000" ]
+
+
Signature · 1 line
2.1.4
#47 lore raw

Re: [PATCH v5 p2 19/19] docs/misc: arm: Add documentation about Device Tree passthrough

docs/misc/arm/passthrough.txt line 44

Quoted 2 lines · Julien Grall
docs/misc/arm/passthrough.txt new file +2 -0
+    root node;
+    * The following properies are mandatory with the /passthrough node:

"properties"

Otherwise: Acked-by: Ian Campbell <[email protected]>

Quoted 17 lines
docs/misc/arm/passthrough.txt new file +17 -0
+        - compatible: It should always contain "simple-bus"
+        - ranges
+        - #address-cells
+        - #size-cells
+
+3) Compile the partial guest device with dtc (Device Tree Compiler).
+For our purpose, the compiled file will be called guest-midway.dtb and
+placed in /root in DOM0.
+
+3) Add the following options in the guest configuration file:
+
+device_tree = "/root/guest-midway.dtb"
+dtdev = [ "/soc/ethernet@fff51000" ]
+irqs = [ 112, 113, 114 ]
+iomem = [ "0xfff51,1@0x10000" ]
+
+