Skip to contentSkip to conversation

arm64: Dom0 ITS emulation

RFC PATCH Superseded Andre Przywara 6 participants 144 messages Sep 28, 2016 – Mar 29, 2017 Page 2 of 2

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#70 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

In reply to Andre Przywara
Quoted 1 line · Andre Przywara

Hi Julien,

Hi Andre,

Quoted 23 lines · Julien Grall
Quoted 10 lines · Andre Przywara
Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 351

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+    default:
+        return field;
+    }
+}

I am not sure to understand why we need to sanitise the value here. From my understanding of the spec (see 8.11.18 in IHI 0069C) we should support any shareability/cacheability, correct?

No, actually an ITS is free to support only _one_ of those attributes, up to the point where it is read-only:

Quoted 6 lines

"It is IMPLEMENTATION DEFINED whether this field has a fixed value or can be programmed by software. Implementing this field with a fixed value is deprecated."

So we support more than one value, but refuse any really not useful ones. This goes in line with the KVM implementation.

Looking at your quote from the spec, this behavior is deprecated. Why do we want to implement a deprecated behavior?

Quoted 6 lines

For the rest of the comments regarding the memory tables setup:
I effectively rewrote this in the new series, so I think the majority of the comments don't apply anymore, hopefully the rewrite actually fixed the issues you mentioned. So I refrain from any comments now and look forward to a review of the new approach ;-)

I will give a look to the new implementation.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#71 lore raw

Re: [RFC PATCH 11/24] ARM: vGICv3: handle virtual LPI pending and property tables

Hi,

Quoted 34 lines · Julien Grall
Quoted 1 line · Andre Przywara

Hi Julien,

Hi Andre,

Quoted 27 lines · Julien Grall
Quoted 10 lines · Andre Przywara
Quoted 9 lines
xen/arch/arm/vgic-v3.c +9 -0
+    return (reg & ~field_mask) | field;
+}
+
+/* We want to avoid outer shareable. */
+static uint64_t vgic_sanitise_shareability(uint64_t field)
+{
+    switch (field) {
+    case GIC_BASER_OuterShareable:
+        return GIC_BASER_InnerShareable;
Quoted 8 lines

xen/arch/arm/vgic-v3.c read_unknown: · line 351

Quoted 4 lines
xen/arch/arm/vgic-v3.c +4 -0
+    default:
+        return field;
+    }
+}

I am not sure to understand why we need to sanitise the value here. From my understanding of the spec (see 8.11.18 in IHI 0069C) we should support any shareability/cacheability, correct?

No, actually an ITS is free to support only _one_ of those attributes, up to the point where it is read-only:

"It is IMPLEMENTATION DEFINED whether this field has a fixed value or can be programmed by software. Implementing this field with a fixed value is deprecated."

Quoted 5 lines
Quoted 2 lines

So we support more than one value, but refuse any really not useful ones. This goes in line with the KVM implementation.

Looking at your quote from the spec, this behavior is deprecated. Why do we want to implement a deprecated behavior?

We don't. Allowing only _one_ attribute and thus making those register bits read-only is deprecated. We make sure to provide support for at least two of them.
Supporting every possible attribute in a virtualization scenario is pointless and not helpful. I believe the architecture requires software to cope with only one attribute, even though this is for some reason "deprecated" (which is a hint for an implementer, not for a driver author).

Quoted 7 lines
Quoted 5 lines

For the rest of the comments regarding the memory tables setup:
I effectively rewrote this in the new series, so I think the majority of the comments don't apply anymore, hopefully the rewrite actually fixed the issues you mentioned. So I refrain from any comments now and look forward to a review of the new approach ;-)

I will give a look to the new implementation.

Thanks!

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#72 lore raw

RFC PATCH 12/24 ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +378 -0

378 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,6 @@ static void vgic_store_irouter(struct domain *d, struct vgic_irq_rank *rank,
     rank->vcpu[offset] = new_vcpu->vcpu_id;
 }
 
-static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
-{
-    /*
-     * 64 bits registers can be accessible using 32-bit and 64-bit unless
-     * stated otherwise (See 8.1.3 ARM IHI 0069A).
-     */
-    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
-}
-
 static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
                                          uint32_t gicr_reg,
                                          register_t *r)
xen/include/asm-arm/gic_v3_defs.h +19 -0
index da5fb77..6a91f5b 100644
@@ -147,6 +147,16 @@
 #define LPI_PROP_RES1                (1 << 1)
 #define LPI_PROP_ENABLED             (1 << 0)
 
+/*
+ * PIDR2: Only bits[7:4] are not implementation defined. We are
+ * emulating a GICv3 ([7:4] = 0x3).
+ *
+ * We don't emulate a specific registers scheme so implement the others
+ * bits as RES0 as recommended by the spec (see 8.1.13 in ARM IHI 0069A).
+ */
+#define GICV3_GICD_PIDR2  0x30
+#define GICV3_GICR_PIDR2  GICV3_GICD_PIDR2
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
@@ -190,6 +200,15 @@ struct rdist_region {
     bool single_rdist;
 };
 
+/*
+ * 64 bits registers can be accessible using 32-bit and 64-bit unless
+ * stated otherwise (See 8.1.3 ARM IHI 0069A).
+ */
+static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
+{
+    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
+}
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#73 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

Quoted 178 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +146 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+           break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):

xen/arch/arm/vgic-its.c line 156

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+       break;
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

Here you are limiting DevID bits to 15, which is not enough

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#74 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

Hi,

Quoted 183 lines · Vijay Kilari
Quoted 182 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +150 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+           break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+       break;
+    case VREG32(GITS_IIDR):
Quoted 8 lines

xen/arch/arm/vgic-its.c line 156

Quoted 6 lines
xen/arch/arm/vgic-its.c new file +6 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

Here you are limiting DevID bits to 15, which is not enough

Right, I wasn't aware of those segments Cavium uses.
I will try to find out other vendor's requirements for maximum device IDs and will adjust this number.
And it seems to be one of the places where I missed to change the hacked up numbers into nice #defines ;-)

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#75 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara
Quoted 178 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +146 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+           break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):

xen/arch/arm/vgic-its.c line 156

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+       break;
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

GITS_TYPER.HCC is not set. Should be max vcpus of the domain GITS_TYPER.ID_bits are also just set to 15.

Quoted 19 lines
xen/arch/arm/vgic-its.c new file +19 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 185

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;

missing default

xen/arch/arm/vgic-its.c line 190

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+    }
+
+    return 1;
+
+bad_width:

print would be helpful

Quoted 57 lines
xen/arch/arm/vgic-its.c new file +57 -0
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+       vgic_reg32_update(&ctlr, r, info);
+       its->enabled = ctlr & GITS_CTLR_ENABLE;
+       /* TODO: trigger something ... */
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

xen/arch/arm/vgic-its.c line 257

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */
+        its->cbaser = reg;
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

Only one page of guest cmd queue is mapped. After cwriter
moving beyond 1 page,
panic is observed. Map all the guest pages

xen/arch/arm/vgic-its.c line 261

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);

Same here.

Quoted 178 lines
xen/arch/arm/vgic-its.c new file +117 -0
+            its->cmdbuf = NULL;
+        }
+
+       return 1;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        reg = its->cwriter;
+        vgic_reg64_update(&reg, r, info);
+        vgic_its_handle_cmds(d, its, reg);
+        return 1;
+    case VREG64(GITS_CREADR):
+        goto write_ignore_64;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser0;
+        vgic_reg64_update(&reg, r, info);
+
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint64_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+        /* TODO: locking(?) */
+
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->dev_table = map_guest_pages(d,
+                                             get_baser_phys_addr(reg),
+                                             its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = its_baser_nr_entries(reg);
+            memset(its->dev_table, 0, its->max_devices * sizeof(uint64_t));
+        }
+        else
+        {
+            unmap_guest_pages(its->dev_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = 0;
+        }
+
+        its->baser0 = reg;
+        return 1;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser1;
+        vgic_reg64_update(&reg, r, info);
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint16_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+
+        /* TODO: sort out locking */
+        /* TODO: repeated calls: free old mapping */
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->coll_table = map_guest_pages(d, get_baser_phys_addr(reg),
+                                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = its_baser_nr_entries(reg);
+            memset(its->coll_table, 0xff,
+                   its->max_collections * sizeof(uint16_t));
+        }
+        else
+        {
+            unmap_guest_pages(its->coll_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = 0;
+        }
+        its->baser1 = reg;
+        return 1;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        goto write_ignore_64;
+    default:
+        gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS register 0x%lx\n",
+                 info->gpa & 0xffff);
+        return 0;
+    }
+
+    return 1;
+
+write_ignore_64:
+    if ( ! vgic_reg64_check_access(info->dabt) ) goto bad_width;
+    return 1;
+
+write_ignore_32:
+    if ( info->dabt.size != DABT_WORD ) goto bad_width;
+    return 1;
+
+bad_width:
+    printk(XENLOG_G_ERR "%pv vGICR: bad read width %d r%d offset %#08lx\n",
+           v, info->dabt.size, info->dabt.reg, info->gpa & 0xffff);
+
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+static const struct mmio_handler_ops vgic_its_mmio_handler = {
+    .read  = vgic_v3_its_mmio_read,
+    .write = vgic_v3_its_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,6 @@ static void vgic_store_irouter(struct domain *d, struct vgic_irq_rank *rank,
     rank->vcpu[offset] = new_vcpu->vcpu_id;
 }
 
-static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
-{
-    /*
-     * 64 bits registers can be accessible using 32-bit and 64-bit unless
-     * stated otherwise (See 8.1.3 ARM IHI 0069A).
-     */
-    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
-}
-
 static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
                                          uint32_t gicr_reg,
                                          register_t *r)
xen/include/asm-arm/gic_v3_defs.h +19 -1
index da5fb77..6a91f5b 100644
@@ -147,6 +147,16 @@
 #define LPI_PROP_RES1                (1 << 1)
 #define LPI_PROP_ENABLED             (1 << 0)
 
+/*
+ * PIDR2: Only bits[7:4] are not implementation defined. We are
+ * emulating a GICv3 ([7:4] = 0x3).
+ *
+ * We don't emulate a specific registers scheme so implement the others
+ * bits as RES0 as recommended by the spec (see 8.1.13 in ARM IHI 0069A).
+ */
+#define GICV3_GICD_PIDR2  0x30
+#define GICV3_GICR_PIDR2  GICV3_GICD_PIDR2
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
@@ -190,6 +200,15 @@ struct rdist_region {
     bool single_rdist;
 };
 
+/*
+ * 64 bits registers can be accessible using 32-bit and 64-bit unless
+ * stated otherwise (See 8.1.3 ARM IHI 0069A).
+ */
+static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
+{
+    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
+}
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*
--

2.9.0

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#76 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

Hi,

Quoted 182 lines · Vijay Kilari
Quoted 181 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +149 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+           break;
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+       break;
Quoted 8 lines

xen/arch/arm/vgic-its.c line 156

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

GITS_TYPER.HCC is not set. Should be max vcpus of the domain

HCC is clear on purpose. We want the guest to provide memory for everything that it allocates, to avoid it to hog Xen with allocations.

Quoted 1 line

GITS_TYPER.ID_bits are also just set to 15.

Yeah, I guess it should match what the hardware ITS provides, to not impose an artificial limit here.

Quoted 29 lines
Quoted 19 lines
xen/arch/arm/vgic-its.c new file +19 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 185

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;
Quoted 7 lines

missing default

xen/arch/arm/vgic-its.c line 190

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+    }
+
+    return 1;
+
+bad_width:

print would be helpful

Yes.

Quoted 63 lines
Quoted 63 lines
xen/arch/arm/vgic-its.c new file +63 -0
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+       vgic_reg32_update(&ctlr, r, info);
+       its->enabled = ctlr & GITS_CTLR_ENABLE;
+       /* TODO: trigger something ... */
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */
+        its->cbaser = reg;
Quoted 8 lines

xen/arch/arm/vgic-its.c line 257

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

Only one page of guest cmd queue is mapped. After cwriter
moving beyond 1 page,
panic is observed. Map all the guest pages

Right, good catch.

Cheers,
Andre.

Quoted 9 lines

xen/arch/arm/vgic-its.c line 261

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);

Same here.

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+            its->cmdbuf = NULL;
+        }
+
+       return 1;

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#77 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

Hello Andre,

Quoted 15 lines · Andre Przywara
Quoted 14 lines · Vijay Kilari
Quoted 13 lines · Andre Przywara
xen/arch/arm/vgic-its.c new file +13 -0
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+       break;
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);
Quoted 4 lines
Quoted 1 line

GITS_TYPER.HCC is not set. Should be max vcpus of the domain

HCC is clear on purpose. We want the guest to provide memory for everything that it allocates, to avoid it to hog Xen with allocations.

Whilst I agree that we want to limit the memory allocated by Xen itself,
each collection entry is just 16-bit. So unless we want to support a very big number of collection, I don't see any reason to request the guest to provision memory.

This makes the code more complex and you also have to validate the collection every time.

I remembered minimum number of collection an implementation has to support is "max_vcpus + 1" but I can't find again the statement in the spec.

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#78 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara

Hi Andre,

Quoted 4 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

The ITS is a complex piece so I think it would be good to describe more in the commit message how this will work. Also a documentation in the tree would be very good to help understanding the code.

Quoted 62 lines
---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +34 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>

xen/arch/arm/vgic-its.c line 44

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;

CWRITER and CREADR are registers so they need to be described in term of number of bits. Also, while the top word of CREADR/CWRITER is RES0. I would much prefer to see uint64_t rather than uint32_t as this is the real size of the register.

xen/arch/arm/vgic-its.c line 46

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;

Please describe what contains baser0 and baser1. If I understand correctly the code, baser0 will be store Device information whilst baser1 the collection.

xen/arch/arm/vgic-its.c line 47

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    uint16_t *coll_table;

What is the layout of the device table?

xen/arch/arm/vgic-its.c line 48

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    int max_collections;

unsigned int

xen/arch/arm/vgic-its.c line 49

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    uint64_t *dev_table;

What is the layout of the device table?

xen/arch/arm/vgic-its.c line 50

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+    int max_devices;

unsigned int.

xen/arch/arm/vgic-its.c line 54

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a

Coding style:

/*
* Foo

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{

xen/arch/arm/vgic-its.c line 68

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,

Please make this function inline.

xen/arch/arm/vgic-its.c line 69

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+                                   int word, int shift, int size)

unsigned for all those parameters.

xen/arch/arm/vgic-its.c line 71

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);

It is probably better to use BIT_ULL (see my explanation on previous patches).

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)

xen/arch/arm/vgic-its.c line 86

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)

uint64_t here.

xen/arch/arm/vgic-its.c line 94

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;

You return an error value but the caller does not check it. Should not the caller do a different action when the return -1? If not, it should be documented.

xen/arch/arm/vgic-its.c line 96

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+
+    spin_lock(&its->vcmd_lock);

I am quite concerned about this locking.

xen/arch/arm/vgic-its.c line 101

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+        switch (its_cmd_get_command(cmdptr))

Coding style: switch ( ... )

xen/arch/arm/vgic-its.c line 105

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+	    break;

The indentation is wrong.

xen/arch/arm/vgic-its.c line 107

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",

gdprintk will happen XENLOG_GUEST, so you can use XENLOG_WARNING here.

Also s/%ld/%lu/

xen/arch/arm/vgic-its.c line 108

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+                   its_cmd_get_command(cmdptr));

Should not we report the error to the default, or crash it? We tend to do the latter on Xen for constrained unpredictable behavior.

xen/arch/arm/vgic-its.c line 116

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;

I think its->cwriter should be updated before the loop. So another vCPU could read the correct CWRITER whilst this vCPU is executing the commands.

xen/arch/arm/vgic-its.c line 127

Quoted 11 lines
xen/arch/arm/vgic-its.c new file +11 -0
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on

Coding style:

/*
* foo

xen/arch/arm/vgic-its.c line 133

Quoted 6 lines
xen/arch/arm/vgic-its.c new file +6 -0
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )

Please document what is bit 9.

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}
+

xen/arch/arm/vgic-its.c line 148

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);

Please use a define for BIT(31). Also, technically the ITS is not quiescent when command are executed (GITS_CTLR could be read from another vCPU).

xen/arch/arm/vgic-its.c line 155

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+	break;
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 156

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        *r = vgic_reg64_extract(0x1eff1, info);

Please document the value and add defines. Vijay's mentioned about the number of device IDs, but the number of collection likely needs to be dynamic as it depends on the number of vCPUs.

xen/arch/arm/vgic-its.c line 159

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 163

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 167

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 171

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 175

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 179

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 180

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        *r = vgic_reg64_extract(0, info);

Please introduce a label read_as_zero_64 at the end and do the implementation of RAZ there. It will acts as a documentation too (see an example in vgic-v3.c).

Also, vgic_reg64_extract(0, info) will ... always return 0. So you can optimize it ;).

xen/arch/arm/vgic-its.c line 182

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+        break;
+    case VREG32(GICD_PIDR2):

This feels odd to use GICD_PIDR2 here. Please define GITS_PIDR2 to avoid any confusion.

xen/arch/arm/vgic-its.c line 184

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);

Ditto.

xen/arch/arm/vgic-its.c line 185

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        break;

Please add all the registers even implementation defined and reserved one. Ignoring registers without any warning is usually a bad idea as it makes very difficult to debug it. You can look at vgic-v3.c for an example.

xen/arch/arm/vgic-its.c line 190

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+    }
+
+    return 1;
+
+bad_width:

Please print an error here (see vgic-v3.c).

xen/arch/arm/vgic-its.c line 200

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)

unsigned int for the return and the function would probably benefit to be inlined.

xen/arch/arm/vgic-its.c line 202

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+{
+    int page_size = 0;

unsigned int.

xen/arch/arm/vgic-its.c line 204

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+
+    switch ( (baser >> 8) & 3 )

Please define 8 and 3.

xen/arch/arm/vgic-its.c line 210

Quoted 6 lines
xen/arch/arm/vgic-its.c new file +6 -0
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }

It looks like to me that the switch could be turned into an array:

unsigned page_size[] = {SZ_4K, SZ_16K, SZ_64K, SZ_64K};

This woudl make the code simpler.

xen/arch/arm/vgic-its.c line 215

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)

unsigned int for the return and the function would probably benefit to be inlined.

xen/arch/arm/vgic-its.c line 217

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;

unsigned int for the type. Also please use a define for 48.

xen/arch/arm/vgic-its.c line 228

Quoted 11 lines
xen/arch/arm/vgic-its.c new file +11 -0
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;
+    uint32_t ctlr;

ctlr could be defined in the case...

xen/arch/arm/vgic-its.c line 146

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):

here. I tend to prefer to restrict the scope whenever it is possible.

xen/arch/arm/vgic-its.c line 237

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+	vgic_reg32_update(&ctlr, r, info);
+	its->enabled = ctlr & GITS_CTLR_ENABLE;
+	/* TODO: trigger something ... */

The indentation is wrong.

xen/arch/arm/vgic-its.c line 244

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access.

xen/arch/arm/vgic-its.c line 249

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+

There may have concurrent access to GITS_BASER, so you want to have some lock here.

xen/arch/arm/vgic-its.c line 252

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */

Please fix this todo as soon as possible.

xen/arch/arm/vgic-its.c line 253

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        its->cbaser = reg;

Also, I am not sure to understand why you need a temporary variable. Whilst you could directly update its->cbaser:

vgic_regs64_update(&its->cbaser, r, info);

Also, from the spec (8.19.2 in ARM IHI 0069C), GITS_CREADR (i.e its->creadr) should be reset to 0.

xen/arch/arm/vgic-its.c line 255

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+
+        if ( reg & BIT(63) )

Please define bit 63.

xen/arch/arm/vgic-its.c line 267

Quoted 12 lines
xen/arch/arm/vgic-its.c new file +12 -0
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);
+            its->cmdbuf = NULL;
+        }
+
+	return 1;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access.

xen/arch/arm/vgic-its.c line 269

Quoted 2 lines
xen/arch/arm/vgic-its.c new file +2 -0
+        reg = its->cwriter;
+        vgic_reg64_update(&reg, r, info);

vgic_its_handle_cmds expect CWRITER to the bit 0 (Retry) masked and bit [32:20], [4:1] should be RES0 (i.e masked).

xen/arch/arm/vgic-its.c line 270

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        vgic_its_handle_cmds(d, its, reg);

Should not you check the return value?

xen/arch/arm/vgic-its.c line 275

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        return 1;
+    case VREG64(GITS_CREADR):
+        goto write_ignore_64;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 285

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser0;
+        vgic_reg64_update(&reg, r, info);
+
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint64_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;

Where does this sizeof(uint64_t) come from?

xen/arch/arm/vgic-its.c line 288

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        reg |= GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+        /* TODO: locking(?) */

Yes, some locking is needed.

xen/arch/arm/vgic-its.c line 296

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->dev_table = map_guest_pages(d,
+                                             get_baser_phys_addr(reg),
+                                             its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = its_baser_nr_entries(reg);
+            memset(its->dev_table, 0, its->max_devices * sizeof(uint64_t));

I am not sure to understand why we need to memset and what the value corresponds to.

xen/arch/arm/vgic-its.c line 305

Quoted 9 lines
xen/arch/arm/vgic-its.c new file +9 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->dev_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = 0;
+        }
+
+        its->baser0 = reg;

Why don't you update baser0 directly (with vgic_reg64_update)?

xen/arch/arm/vgic-its.c line 308

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        return 1;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

Please use vgic_reg64_check_access

xen/arch/arm/vgic-its.c line 321

Quoted 13 lines
xen/arch/arm/vgic-its.c new file +13 -0
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser1;
+        vgic_reg64_update(&reg, r, info);
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint16_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+
+        /* TODO: sort out locking */

I am expecting this to be fixed in the next version.

xen/arch/arm/vgic-its.c line 329

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+        /* TODO: repeated calls: free old mapping */
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->coll_table = map_guest_pages(d, get_baser_phys_addr(reg),
+                                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = its_baser_nr_entries(reg);
+            memset(its->coll_table, 0xff,
+                   its->max_collections * sizeof(uint16_t));

I am not sure to understand why we need to memset and what the value corresponds to.

xen/arch/arm/vgic-its.c line 337

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->coll_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = 0;
+        }
+        its->baser1 = reg;

Why don't you update baser1 directly (with vgic_reg64_update)?

xen/arch/arm/vgic-its.c line 340

Quoted 3 lines
xen/arch/arm/vgic-its.c new file +3 -0
+        return 1;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        goto write_ignore_64;

From the ITS register map, we would have to emulate more register (at least reserved, implementation defined and RAZ).

Quoted 15 lines
xen/arch/arm/vgic-its.c new file +15 -0
+    default:
+        gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS register 0x%lx\n",
+                 info->gpa & 0xffff);
+        return 0;
+    }
+
+    return 1;
+
+write_ignore_64:
+    if ( ! vgic_reg64_check_access(info->dabt) ) goto bad_width;
+    return 1;
+
+write_ignore_32:
+    if ( info->dabt.size != DABT_WORD ) goto bad_width;
+    return 1;

xen/arch/arm/vgic-its.c line 365

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+bad_width:
+    printk(XENLOG_G_ERR "%pv vGICR: bad read width %d r%d offset %#08lx\n",
+           v, info->dabt.size, info->dabt.reg, info->gpa & 0xffff);
+
+    domain_crash_synchronous();
+
+    return 0;
+}
+

The Makefile already includes the vi

xen/arch/arm/vgic-its.c line 369

Quoted 4 lines
xen/arch/arm/vgic-its.c new file +4 -0
+static const struct mmio_handler_ops vgic_its_mmio_handler = {
+    .read  = vgic_v3_its_mmio_read,
+    .write = vgic_v3_its_mmio_write,
+};

This will break compilation with randconfig as the ITS is selectable. Please make sure that every patch built one by one. A good approach would be allowing the selection of the ITS at the end of this series.

Quoted 36 lines
xen/arch/arm/vgic-its.c new file +9 -0
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,6 @@ static void vgic_store_irouter(struct domain *d, struct vgic_irq_rank *rank,
     rank->vcpu[offset] = new_vcpu->vcpu_id;
 }
 
-static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
-{
-    /*
-     * 64 bits registers can be accessible using 32-bit and 64-bit unless
-     * stated otherwise (See 8.1.3 ARM IHI 0069A).
-     */
-    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
-}
-
 static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
                                          uint32_t gicr_reg,
                                          register_t *r)
xen/include/asm-arm/gic_v3_defs.h +0 -0
index da5fb77..6a91f5b 100644
@@ -147,6 +147,16 @@
 #define LPI_PROP_RES1                (1 << 1)
 #define LPI_PROP_ENABLED             (1 << 0)

xen/include/asm-arm/gic_v3_defs.h line 158

Quoted 10 lines
xen/include/asm-arm/gic_v3_defs.h +9 -0
 
+/*
+ * PIDR2: Only bits[7:4] are not implementation defined. We are
+ * emulating a GICv3 ([7:4] = 0x3).
+ *
+ * We don't emulate a specific registers scheme so implement the others
+ * bits as RES0 as recommended by the spec (see 8.1.13 in ARM IHI 0069A).
+ */
+#define GICV3_GICD_PIDR2  0x30
+#define GICV3_GICR_PIDR2  GICV3_GICD_PIDR2

Those values should not be defined in gic_v3_defs.h but a vgic headers. My rationale is, those value are implementation defined (e.g depends on the emulation).

Quoted 7 lines
xen/include/asm-arm/gic_v3_defs.h +1 -0
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
@@ -190,6 +200,15 @@ struct rdist_region {
     bool single_rdist;
 };

xen/include/asm-arm/gic_v3_defs.h struct rdist_region { · line 211

Quoted 10 lines
xen/include/asm-arm/gic_v3_defs.h +9 -0
 
+/*
+ * 64 bits registers can be accessible using 32-bit and 64-bit unless
+ * stated otherwise (See 8.1.3 ARM IHI 0069A).
+ */
+static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
+{
+    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
+}
+

This function should be defined in vgic.h and not gic_v3_defs.h

xen/include/asm-arm/gic_v3_defs.h struct rdist_region { · line 214

Quoted 4 lines
xen/include/asm-arm/gic_v3_defs.h +0 -0
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#79 lore raw

Re: [RFC PATCH 12/24] ARM: vGICv3: introduce basic ITS emulation bits

In reply to Andre Przywara
Quoted 128 lines · Andre Przywara

Create a new file to hold the emulation code for the ITS widget. For now we emulate the memory mapped ITS registers and provide a stub to introduce the ITS command handling framework (but without actually emulating any commands at this time).

---
 xen/arch/arm/Makefile             |   1 +
 xen/arch/arm/vgic-its.c           | 378 ++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c            |   9 -
 xen/include/asm-arm/gic_v3_defs.h |  19 ++
 4 files changed, 398 insertions(+), 9 deletions(-)
 create mode 100644 xen/arch/arm/vgic-its.c
xen/arch/arm/Makefile +1 -0
index c2c4daa..cb0201f 100644
@@ -44,6 +44,7 @@ obj-y += traps.o
 obj-y += vgic.o
 obj-y += vgic-v2.o
 obj-$(CONFIG_ARM_64) += vgic-v3.o
+obj-$(CONFIG_HAS_ITS) += vgic-its.o
 obj-y += vm_event.o
 obj-y += vtimer.o
 obj-y += vpsci.o
xen/arch/arm/vgic-its.c new file +96 -0
new file mode 100644
index 0000000..875b992
@@ -0,0 +1,378 @@
+/*
+ * xen/arch/arm/vgic-its.c
+ *
+ * ARM Interrupt Translation Service (ITS) emulation
+ *
+ * Andre Przywara <[email protected]>
+ * Copyright (c) 2016 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <xen/bitops.h>
+#include <xen/config.h>
+#include <xen/domain_page.h>
+#include <xen/lib.h>
+#include <xen/init.h>
+#include <xen/softirq.h>
+#include <xen/irq.h>
+#include <xen/sched.h>
+#include <xen/sizes.h>
+#include <asm/current.h>
+#include <asm/mmio.h>
+#include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
+#include <asm/vgic.h>
+#include <asm/vgic-emul.h>
+
+/* Data structure to describe a virtual ITS */
+struct virt_its {
+    struct domain *d;
+    struct host_its *hw_its;
+    spinlock_t vcmd_lock;       /* protects the virtual command buffer */
+    uint64_t cbaser;
+    uint64_t *cmdbuf;
+    int cwriter;
+    int creadr;
+    spinlock_t its_lock;        /* protects the collection and device tables */
+    uint64_t baser0, baser1;
+    uint16_t *coll_table;
+    int max_collections;
+    uint64_t *dev_table;
+    int max_devices;
+    bool enabled;
+};
+
+/* An Interrupt Translation Table Entry: this is indexed by a
+ * DeviceID/EventID pair and is located in guest memory.
+ */
+struct vits_itte
+{
+    uint64_t hlpi:24;
+    uint64_t vlpi:24;
+    uint64_t collection:16;
+};
+
+/**************************************
+ * Functions that handle ITS commands *
+ **************************************/
+
+static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
+                                   int word, int shift, int size)
+{
+    return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT(size) - 1);
+}
+
+#define its_cmd_get_command(cmd)        its_cmd_mask_field(cmd, 0,  0,  8)
+#define its_cmd_get_deviceid(cmd)       its_cmd_mask_field(cmd, 0, 32, 32)
+#define its_cmd_get_size(cmd)           its_cmd_mask_field(cmd, 1,  0,  5)
+#define its_cmd_get_id(cmd)             its_cmd_mask_field(cmd, 1,  0, 32)
+#define its_cmd_get_physical_id(cmd)    its_cmd_mask_field(cmd, 1, 32, 32)
+#define its_cmd_get_collection(cmd)     its_cmd_mask_field(cmd, 2,  0, 16)
+#define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
+#define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
+
+#define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
+
+static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
+                                uint32_t writer)
+{
+    uint64_t *cmdptr;
+
+    if ( !its->cmdbuf )
+        return -1;
+
+    if ( writer >= ITS_CMD_BUFFER_SIZE(its->cbaser) )
+        return -1;
+
+    spin_lock(&its->vcmd_lock);

xen/arch/arm/vgic-its.c line 106

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+
+    while ( its->creadr != writer )
+    {
+        cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
+
+        switch (its_cmd_get_command(cmdptr))
+        {
+        case GITS_CMD_SYNC:
+            /* We handle ITS commands synchronously, so we ignore SYNC. */
+	    break;

indentation

Quoted 22 lines
xen/arch/arm/vgic-its.c new file +22 -0
+        default:
+            gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS command %ld\n",
+                   its_cmd_get_command(cmdptr));
+            break;
+        }
+
+        its->creadr += ITS_CMD_SIZE;
+        if ( its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser) )
+            its->creadr = 0;
+    }
+    its->cwriter = writer;
+
+    spin_unlock(&its->vcmd_lock);
+
+    return 0;
+}
+
+/*****************************
+ * ITS registers read access *
+ *****************************/
+
+/* The physical address is encoded slightly differently depending on

xen/arch/arm/vgic-its.c line 137

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+ * the used page size: the highest four bits are stored in the lowest
+ * four bits of the field for 64K pages.
+ */
+static paddr_t get_baser_phys_addr(uint64_t reg)
+{
+    if ( reg & BIT(9) )
+        return (reg & GENMASK(47, 16)) | ((reg & GENMASK(15, 12)) << 36);
+    else
+        return reg & GENMASK(47, 12);
+}

I would simplify the code by supporting only one page size, maybe 4K.

xen/arch/arm/vgic-its.c line 149

Quoted 12 lines
xen/arch/arm/vgic-its.c new file +12 -0
+
+static int vgic_v3_its_mmio_read(struct vcpu *v, mmio_info_t *info,
+                                 register_t *r, void *priv)
+{
+    struct virt_its *its = priv;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(its->enabled | BIT(31), info);
+	break;

indentation

xen/arch/arm/vgic-its.c line 156

Quoted 7 lines
xen/arch/arm/vgic-its.c new file +7 -0
+    case VREG32(GITS_IIDR):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GITS_IIDR_VALUE, info);
+        break;
+    case VREG64(GITS_TYPER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0x1eff1, info);

please #define 0x1eff1

Quoted 15 lines
xen/arch/arm/vgic-its.c new file +15 -0
+        break;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cbaser, info);
+        break;
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->cwriter, info);
+        break;
+    case VREG64(GITS_CREADR):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->creadr, info);
+        break;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;

xen/arch/arm/vgic-its.c line 181

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        *r = vgic_reg64_extract(its->baser0, info);
+        break;
+    case VREG64(GITS_BASER1):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(its->baser1, info);
+        break;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        *r = vgic_reg64_extract(0, info);
+        break;

I notice that this patch lacks the code to initialize the vits registers to sensible defaults. For example, who initializes the entry size (52:48) of GITS_BASER?

Quoted 46 lines
xen/arch/arm/vgic-its.c new file +46 -0
+    case VREG32(GICD_PIDR2):
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(GICV3_GICD_PIDR2, info);
+        break;
+    }
+
+    return 1;
+
+bad_width:
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+/******************************
+ * ITS registers write access *
+ ******************************/
+
+static int its_baser_table_size(uint64_t baser)
+{
+    int page_size = 0;
+
+    switch ( (baser >> 8) & 3 )
+    {
+    case 0: page_size = SZ_4K; break;
+    case 1: page_size = SZ_16K; break;
+    case 2:
+    case 3: page_size = SZ_64K; break;
+    }
+
+    return page_size * ((baser & GENMASK(7, 0)) + 1);
+}
+
+static int its_baser_nr_entries(uint64_t baser)
+{
+    int entry_size = ((baser & GENMASK(52, 48)) >> 48) + 1;
+
+    return its_baser_table_size(baser) / entry_size;
+}
+
+static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,
+                                  register_t r, void *priv)
+{
+    struct domain *d = v->domain;
+    struct virt_its *its = priv;
+    uint64_t reg;

xen/arch/arm/vgic-its.c line 237

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+    uint32_t ctlr;
+
+    switch ( info->gpa & 0xffff )
+    {
+    case VREG32(GITS_CTLR):
+        ctlr = its->enabled ? GITS_CTLR_ENABLE : 0;
+        if ( info->dabt.size != DABT_WORD ) goto bad_width;
+	vgic_reg32_update(&ctlr, r, info);
+	its->enabled = ctlr & GITS_CTLR_ENABLE;
+	/* TODO: trigger something ... */

indentation

xen/arch/arm/vgic-its.c line 247

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        return 1;
+    case VREG32(GITS_IIDR):
+        goto write_ignore_32;
+    case VREG32(GITS_TYPER):
+        goto write_ignore_32;
+    case VREG64(GITS_CBASER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

It is worth printing an error (gdprintk).

xen/arch/arm/vgic-its.c line 252

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+            return 1;
+
+        reg = its->cbaser;
+        vgic_reg64_update(&reg, r, info);
+        /* TODO: sanitise! */

Yeah, we really need to do that :-)

xen/arch/arm/vgic-its.c line 257

Quoted 5 lines
xen/arch/arm/vgic-its.c new file +5 -0
+        its->cbaser = reg;
+
+        if ( reg & BIT(63) )
+        {
+            its->cmdbuf = map_guest_pages(d, reg & GENMASK(51, 12), 1);

This is only one page, there is no need to use the vmap.

xen/arch/arm/vgic-its.c line 265

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+        }
+        else
+        {
+            unmap_guest_pages(its->cmdbuf, 1);
+            its->cmdbuf = NULL;
+        }
+
+	return 1;

indentation

xen/arch/arm/vgic-its.c line 278

Quoted 13 lines
xen/arch/arm/vgic-its.c new file +13 -0
+    case VREG64(GITS_CWRITER):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+        reg = its->cwriter;
+        vgic_reg64_update(&reg, r, info);
+        vgic_its_handle_cmds(d, its, reg);
+        return 1;
+    case VREG64(GITS_CREADR):
+        goto write_ignore_64;
+    case VREG64(GITS_BASER0):
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )

please add a warning

xen/arch/arm/vgic-its.c line 286

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+            return 1;
+
+        reg = its->baser0;
+        vgic_reg64_update(&reg, r, info);
+
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint64_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT;

Why not | with its->baser0?

xen/arch/arm/vgic-its.c line 288

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        /* TODO: sanitise! */

Indeed

xen/arch/arm/vgic-its.c line 288

Quoted 1 line
xen/arch/arm/vgic-its.c new file +1 -0
+        /* TODO: locking(?) */

vITS stuff can be modified concurrently by two or more vCPUs, so anything that changes a shared state accessible by multiple vCPUs need a lock.

Quoted 8 lines
xen/arch/arm/vgic-its.c new file +8 -0
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->dev_table = map_guest_pages(d,
+                                             get_baser_phys_addr(reg),
+                                             its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = its_baser_nr_entries(reg);
+            memset(its->dev_table, 0, its->max_devices * sizeof(uint64_t));
+        }

xen/arch/arm/vgic-its.c line 307

Quoted 10 lines
xen/arch/arm/vgic-its.c new file +10 -0
+        else
+        {
+            unmap_guest_pages(its->dev_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_devices = 0;
+        }
+
+        its->baser0 = reg;
+        return 1;
+    case VREG64(GITS_BASER1):

We need to be able to share this code with the GITS_BASER0 case above

Quoted 131 lines
xen/arch/arm/vgic-its.c new file +71 -0
+        if ( info->dabt.size < DABT_WORD ) goto bad_width;
+
+        /* Changing base registers with the ITS enabled is UNPREDICTABLE. */
+        if ( its->enabled )
+            return 1;
+
+        reg = its->baser1;
+        vgic_reg64_update(&reg, r, info);
+        reg &= ~GITS_BASER_RO_MASK;
+        reg |= (sizeof(uint16_t) - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
+        reg |= GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT;
+        /* TODO: sanitise! */
+
+        /* TODO: sort out locking */
+        /* TODO: repeated calls: free old mapping */
+        if ( reg & GITS_BASER_VALID )
+        {
+            its->coll_table = map_guest_pages(d, get_baser_phys_addr(reg),
+                                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = its_baser_nr_entries(reg);
+            memset(its->coll_table, 0xff,
+                   its->max_collections * sizeof(uint16_t));
+        }
+        else
+        {
+            unmap_guest_pages(its->coll_table,
+                              its_baser_table_size(reg) >> PAGE_SHIFT);
+            its->max_collections = 0;
+        }
+        its->baser1 = reg;
+        return 1;
+    case VRANGE64(GITS_BASER2, GITS_BASER7):
+        goto write_ignore_64;
+    default:
+        gdprintk(XENLOG_G_WARNING, "ITS: unhandled ITS register 0x%lx\n",
+                 info->gpa & 0xffff);
+        return 0;
+    }
+
+    return 1;
+
+write_ignore_64:
+    if ( ! vgic_reg64_check_access(info->dabt) ) goto bad_width;
+    return 1;
+
+write_ignore_32:
+    if ( info->dabt.size != DABT_WORD ) goto bad_width;
+    return 1;
+
+bad_width:
+    printk(XENLOG_G_ERR "%pv vGICR: bad read width %d r%d offset %#08lx\n",
+           v, info->dabt.size, info->dabt.reg, info->gpa & 0xffff);
+
+    domain_crash_synchronous();
+
+    return 0;
+}
+
+static const struct mmio_handler_ops vgic_its_mmio_handler = {
+    .read  = vgic_v3_its_mmio_read,
+    .write = vgic_v3_its_mmio_write,
+};
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/arm/vgic-v3.c +0 -9
index 8fe8386..aa53a1e 100644
@@ -158,15 +158,6 @@ static void vgic_store_irouter(struct domain *d, struct vgic_irq_rank *rank,
     rank->vcpu[offset] = new_vcpu->vcpu_id;
 }
 
-static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
-{
-    /*
-     * 64 bits registers can be accessible using 32-bit and 64-bit unless
-     * stated otherwise (See 8.1.3 ARM IHI 0069A).
-     */
-    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
-}
-
 static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
                                          uint32_t gicr_reg,
                                          register_t *r)
xen/include/asm-arm/gic_v3_defs.h +19 -0
index da5fb77..6a91f5b 100644
@@ -147,6 +147,16 @@
 #define LPI_PROP_RES1                (1 << 1)
 #define LPI_PROP_ENABLED             (1 << 0)
 
+/*
+ * PIDR2: Only bits[7:4] are not implementation defined. We are
+ * emulating a GICv3 ([7:4] = 0x3).
+ *
+ * We don't emulate a specific registers scheme so implement the others
+ * bits as RES0 as recommended by the spec (see 8.1.13 in ARM IHI 0069A).
+ */
+#define GICV3_GICD_PIDR2  0x30
+#define GICV3_GICR_PIDR2  GICV3_GICD_PIDR2
+
 #define GICH_VMCR_EOI                (1 << 9)
 #define GICH_VMCR_VENG1              (1 << 1)
 
@@ -190,6 +200,15 @@ struct rdist_region {
     bool single_rdist;
 };
 
+/*
+ * 64 bits registers can be accessible using 32-bit and 64-bit unless
+ * stated otherwise (See 8.1.3 ARM IHI 0069A).
+ */
+static inline bool vgic_reg64_check_access(struct hsr_dabt dabt)
+{
+    return ( dabt.size == DABT_DOUBLE_WORD || dabt.size == DABT_WORD );
+}
+
 #endif /* __ASM_ARM_GIC_V3_DEFS_H__ */
 
 /*
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#80 lore raw

RFC PATCH 13/24 ARM: vITS: handle CLEAR command

In reply to Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +115 -0
index 875b992..99d9e9c 100644
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;
+
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;
+
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/* Looks up a given deviceID/eventID pair on an ITS and returns a pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;
+
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
 
+static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    /* Remove a pending, but not yet injected guest IRQ. */
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;
+    }
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -100,6 +212,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#81 lore raw

Re: [RFC PATCH 13/24] ARM: vITS: handle CLEAR command

Hi Andre,

Quoted 17 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +0 -0
index 875b992..99d9e9c 100644

xen/arch/arm/vgic-its.c struct vits_itte · line 66

Quoted 6 lines
xen/arch/arm/vgic-its.c +3 -0
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */

This comment is a call to have an ASSERT in the function.

xen/arch/arm/vgic-its.c struct vits_itte · line 67

Quoted 1 line
xen/arch/arm/vgic-its.c +1 -0
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)

Please use unsigned int.

Quoted 5 lines
xen/arch/arm/vgic-its.c +5 -0
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;

xen/arch/arm/vgic-its.c struct vits_itte · line 82

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))

The layout of dev_table[...] really needs to be explained. It took me quite a while to understand how it works. For instance why you skip the first 8 bits for the address...

xen/arch/arm/vgic-its.c struct vits_itte · line 92

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;

Please use INVALID_PADDR here.

xen/arch/arm/vgic-its.c struct vits_itte · line 95

Quoted 3 lines
xen/arch/arm/vgic-its.c +3 -0
+
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;

Ditto.

xen/arch/arm/vgic-its.c struct vits_itte · line 102

Quoted 7 lines
xen/arch/arm/vgic-its.c +7 -0
+
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/* Looks up a given deviceID/eventID pair on an ITS and returns a pointer to

Coding style:

/*
* Foo

xen/arch/arm/vgic-its.c struct vits_itte · line 106

Quoted 4 lines
xen/arch/arm/vgic-its.c +4 -0
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.

This is a call for an ASSERT in the code.

xen/arch/arm/vgic-its.c struct vits_itte · line 109

Quoted 3 lines
xen/arch/arm/vgic-its.c +3 -0
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)

The naming of the function is confusing. It doesn't look up a device ID/event ID but an IIT. So I would rename it to find_itte.

xen/arch/arm/vgic-its.c struct vits_itte · line 114

Quoted 5 lines
xen/arch/arm/vgic-its.c +5 -0
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)

Coding style: if ( ... )

And return INVALID_PADDR.

Quoted 51 lines
xen/arch/arm/vgic-its.c +44 -0
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;
+
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
 
+static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);

xen/arch/arm/vgic-its.c static uint64_t its_cmd_mask_field(uint64_t *its_cmd, · line 187

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    spin_unlock(&its->its_lock);
+
+    /* Remove a pending, but not yet injected guest IRQ. */
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */

NIT: s/availabe/available/

xen/arch/arm/vgic-its.c static uint64_t its_cmd_mask_field(uint64_t *its_cmd, · line 189

Quoted 2 lines
xen/arch/arm/vgic-its.c +2 -0
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;

This code should be in a separate helper. It will be helpful to make the structure available again easily without open coding it.

Quoted 9 lines
xen/arch/arm/vgic-its.c +5 -0
+    }
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,

xen/arch/arm/vgic-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 216

Quoted 5 lines
xen/arch/arm/vgic-its.c +2 -0
@@ -100,6 +212,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);

Should not you check the return for its_handle_clear?

xen/arch/arm/vgic-its.c static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its, · line 220

Quoted 5 lines
xen/arch/arm/vgic-its.c +1 -0
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#82 lore raw

Re: [RFC PATCH 13/24] ARM: vITS: handle CLEAR command

In reply to Andre Przywara
Quoted 39 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +19 -0
index 875b992..99d9e9c 100644
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))

xen/arch/arm/vgic-its.c struct vits_itte · line 92

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))
+
+static paddr_t get_itte_address(struct virt_its *its,
+                                uint32_t devid, uint32_t evid)
+{
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;

Please #define the error

xen/arch/arm/vgic-its.c struct vits_itte · line 95

Quoted 2 lines
xen/arch/arm/vgic-its.c +2 -0
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;

same here

Quoted 14 lines
xen/arch/arm/vgic-its.c +14 -0
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/* Looks up a given deviceID/eventID pair on an ITS and returns a pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)
+{

xen/arch/arm/vgic-its.c struct vits_itte · line 120

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;

No need to use the vmap to map 1 page

xen/arch/arm/vgic-its.c struct vits_itte · line 122

Quoted 1 line
xen/arch/arm/vgic-its.c +1 -0
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);

Please use () around the div operation for clarity

xen/arch/arm/vgic-its.c struct vits_itte · line 128

Quoted 6 lines
xen/arch/arm/vgic-its.c +6 -0
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);

No need for this, once you use __pa instead of the vmap

Quoted 30 lines
xen/arch/arm/vgic-its.c +23 -0
+}
+
 /**************************************
  * Functions that handle ITS commands *
  **************************************/
@@ -80,6 +147,51 @@ static uint64_t its_cmd_mask_field(uint64_t *its_cmd,
 #define its_cmd_get_target_addr(cmd)    its_cmd_mask_field(cmd, 2, 16, 32)
 #define its_cmd_get_validbit(cmd)       its_cmd_mask_field(cmd, 2, 63,  1)
 
+static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+    {
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vcpu = get_vcpu_from_collection(its, itte->collection);
+    if ( !vcpu )
+    {

xen/arch/arm/vgic-its.c static uint64_t its_cmd_mask_field(uint64_t *its_cmd, · line 180

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+        spin_unlock(&its->its_lock);
+        return -1;
+    }
+
+    vlpi = itte->vlpi;
+
+    put_devid_evid(its, itte);
+    spin_unlock(&its->its_lock);
+
+    /* Remove a pending, but not yet injected guest IRQ. */

We need to check that the vlpi hasn't already been added to an LR register. We can do that with GIC_IRQ_GUEST_VISIBLE.

In case GIC_IRQ_GUEST_VISIBLE is set, we need to clear the lr (clear_lr). If we don't handle this case, we should at least print a warning.

Quoted 30 lines
xen/arch/arm/vgic-its.c +17 -0
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;
+    }
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -100,6 +212,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         cmdptr = its->cmdbuf + (its->creadr / sizeof(*its->cmdbuf));
         switch (its_cmd_get_command(cmdptr))
         {
+        case GITS_CMD_CLEAR:
+            its_handle_clear(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#83 lore raw

Re: [RFC PATCH 13/24] ARM: vITS: handle CLEAR command

Hi,

Quoted 44 lines · Stefano Stabellini
Quoted 43 lines · Andre Przywara

This introduces the ITS command handler for the CLEAR command, which clears the pending state of an LPI.
This removes a not-yet injected, but already queued IRQ from a VCPU.

In addition this patch introduces the lookup function which translates a given DeviceID/EventID pair into a pointer to our vITTE structure.

---
 xen/arch/arm/vgic-its.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 115 insertions(+)
xen/arch/arm/vgic-its.c +23 -0
index 875b992..99d9e9c 100644
@@ -61,6 +61,73 @@ struct vits_itte
     uint64_t collection:16;
 };
 
+#define UNMAPPED_COLLECTION      ((uint16_t)~0)
+
+/* Must be called with the ITS lock held. */
+static struct vcpu *get_vcpu_from_collection(struct virt_its *its, int collid)
+{
+    uint16_t vcpu_id;
+
+    if ( collid >= its->max_collections )
+        return NULL;
+
+    vcpu_id = its->coll_table[collid];
+    if ( vcpu_id == UNMAPPED_COLLECTION || vcpu_id >= its->d->max_vcpus )
+        return NULL;
+
+    return its->d->vcpu[vcpu_id];
+}
+
+#define DEV_TABLE_ITT_ADDR(x) ((x) & GENMASK(51, 8))
+#define DEV_TABLE_ITT_SIZE(x) (BIT(((x) & GENMASK(7, 0)) + 1))
+#define DEV_TABLE_ENTRY(addr, bits)                     \
+        (((addr) & GENMASK(51, 8)) | (((bits) - 1) & GENMASK(7, 0)))
+
+static paddr_t get_itte_address(struct virt_its *its,
Quoted 8 lines

xen/arch/arm/vgic-its.c struct vits_itte · line 92

Quoted 6 lines
xen/arch/arm/vgic-its.c +6 -0
+                                uint32_t devid, uint32_t evid)
+{
+    paddr_t addr;
+
+    if ( devid >= its->max_devices )
+        return ~0;

Please #define the error

Technically this should be INVALID_PADDR here.

Quoted 5 lines

xen/arch/arm/vgic-its.c struct vits_itte · line 95

Quoted 3 lines
xen/arch/arm/vgic-its.c +2 -0
+    if ( evid >= DEV_TABLE_ITT_SIZE(its->dev_table[devid]) )
+        return ~0;

same here

Ditto.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/vgic-its.c +18 -0
+    addr = DEV_TABLE_ITT_ADDR(its->dev_table[devid]);
+
+    return addr + evid * sizeof(struct vits_itte);
+}
+
+/* Looks up a given deviceID/eventID pair on an ITS and returns a pointer to
+ * the corresponding ITTE. This maps the respective guest page into Xen.
+ * Once finished with handling the ITTE, call put_devid_evid() to unmap
+ * the page again.
+ * Must be called with the ITS lock held.
+ */
+static struct vits_itte *get_devid_evid(struct virt_its *its,
+                                        uint32_t devid, uint32_t evid)
+{
+    paddr_t addr = get_itte_address(its, devid, evid);
+    struct vits_itte *itte;
+
+    if (addr == ~0)
Quoted 8 lines

xen/arch/arm/vgic-its.c struct vits_itte · line 120

Quoted 6 lines
xen/arch/arm/vgic-its.c +6 -0
+        return NULL;
+
+    /* TODO: check locking for map_guest_pages() */
+    itte = map_guest_pages(its->d, addr & PAGE_MASK, 1);
+    if ( !itte )
+        return NULL;

No need to use the vmap to map 1 page

But you do have to translate the IPA to a PA, so you cannot directly use __pa on it.

Quoted 6 lines

xen/arch/arm/vgic-its.c struct vits_itte · line 122

Quoted 2 lines
xen/arch/arm/vgic-its.c +1 -0
+    return itte + (addr & ~PAGE_MASK) / sizeof(struct vits_itte);

Please use () around the div operation for clarity

Quoted 8 lines

xen/arch/arm/vgic-its.c struct vits_itte · line 128

Quoted 6 lines
xen/arch/arm/vgic-its.c +6 -0
+}
+
+/* Must be called with the ITS lock held. */
+static void put_devid_evid(struct virt_its *its, struct vits_itte *itte)
+{
+    unmap_guest_pages(itte, 1);

No need for this, once you use __pa instead of the vmap

Well, we should at least use map_domain_page/unmap_domain_page even if they are a nop on ARM64. And not directly __pa.

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#84 lore raw

RFC PATCH 14/24 ARM: vITS: handle INT command

In reply to Andre Przywara

The INT command sets a given LPI identified by a DeviceID/EventID pair as pending and thus triggers it to be injected.

---
 xen/arch/arm/vgic-its.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/arm/vgic-its.c +34 -0
index 99d9e9c..7072753 100644
@@ -192,6 +192,37 @@ static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    int ret = -1;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    vcpu = its->d->vcpu[itte->collection];
+    vlpi = itte->vlpi;
+
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !ret)
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -215,6 +246,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_INT:
+            its_handle_int(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#85 lore raw

Re: [RFC PATCH 14/24] ARM: vITS: handle INT command

Quoted 22 lines · Andre Przywara

The INT command sets a given LPI identified by a DeviceID/EventID pair as pending and thus triggers it to be injected.

---
 xen/arch/arm/vgic-its.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
xen/arch/arm/vgic-its.c +6 -0
index 99d9e9c..7072753 100644
@@ -192,6 +192,37 @@ static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_int(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct vits_itte *itte;
+    struct vcpu *vcpu;

xen/arch/arm/vgic-its.c static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr) · line 210

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    int ret = -1;
+    uint32_t vlpi;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    vcpu = its->d->vcpu[itte->collection];

We need to check that itte->collection is a valid vcpu before using as array index.

xen/arch/arm/vgic-its.c static int its_handle_clear(struct virt_its *its, uint64_t *cmdptr) · line 220

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    vlpi = itte->vlpi;
+
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !ret)

code style

Quoted 21 lines
xen/arch/arm/vgic-its.c +8 -0
+        vgic_vcpu_inject_irq(vcpu, vlpi);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -215,6 +246,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_INT:
+            its_handle_int(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#86 lore raw

RFC PATCH 15/24 ARM: vITS: handle MAPC command

In reply to Andre Przywara

The MAPC command associates a given collection ID with a given redistributor, thus mapping collections to VCPUs.
We just store the vcpu_id in the collection table for that.

---
 xen/arch/arm/vgic-its.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
xen/arch/arm/vgic-its.c +30 -0
index 7072753..caad320 100644
@@ -223,6 +223,33 @@ out_unlock:
     return ret;
 }
 
+static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    uint64_t rdbase = its_cmd_mask_field(cmdptr, 2, 16, 44);
+    int ret = -1;
+
+    if ( collid >= its->max_collections )
+        return ret;
+
+    if ( rdbase >= its->d->max_vcpus )
+        return ret;
+
+    spin_lock(&its->its_lock);
+    if ( its->coll_table )
+    {
+        if ( its_cmd_get_validbit(cmdptr) )
+            its->coll_table[collid] = rdbase;
+        else
+            its->coll_table[collid] = UNMAPPED_COLLECTION;
+
+        ret = 0;
+    }
+    spin_unlock(&its->its_lock);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -249,6 +276,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
+        case GITS_CMD_MAPC:
+            its_handle_mapc(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#87 lore raw

Re: [RFC PATCH 15/24] ARM: vITS: handle MAPC command

Quoted 14 lines · Andre Przywara

The MAPC command associates a given collection ID with a given redistributor, thus mapping collections to VCPUs.
We just store the vcpu_id in the collection table for that.

---
 xen/arch/arm/vgic-its.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
xen/arch/arm/vgic-its.c +0 -0
index 7072753..caad320 100644

xen/arch/arm/vgic-its.c out_unlock: · line 230

Quoted 8 lines
xen/arch/arm/vgic-its.c +5 -0
@@ -223,6 +223,33 @@ out_unlock:
     return ret;
 }
 
+static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    uint64_t rdbase = its_cmd_mask_field(cmdptr, 2, 16, 44);
+    int ret = -1;

I take 44 is a bit arbitrary here? It might be best to #define it.

Quoted 37 lines
xen/arch/arm/vgic-its.c +24 -0
+    if ( collid >= its->max_collections )
+        return ret;
+
+    if ( rdbase >= its->d->max_vcpus )
+        return ret;
+
+    spin_lock(&its->its_lock);
+    if ( its->coll_table )
+    {
+        if ( its_cmd_get_validbit(cmdptr) )
+            its->coll_table[collid] = rdbase;
+        else
+            its->coll_table[collid] = UNMAPPED_COLLECTION;
+
+        ret = 0;
+    }
+    spin_unlock(&its->its_lock);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -249,6 +276,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
+        case GITS_CMD_MAPC:
+            its_handle_mapc(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#88 lore raw

RFC PATCH 16/24 ARM: vITS: handle MAPD command

In reply to Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITTEs with a certain device ID.
We just store the given guest physical address in the device table. We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. We map the device tables on demand when we need them later.

Also we propagate the MAPD request to the hardware ITS, as the device ID is only meaningful there.

---
 xen/arch/arm/vgic-its.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
xen/arch/arm/vgic-its.c +31 -0
index caad320..83d47e1 100644
@@ -250,6 +250,34 @@ static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
     return ret;
 }
 
+static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    int size = its_cmd_get_size(cmdptr);
+    bool valid = its_cmd_get_validbit(cmdptr);
+    paddr_t itt_addr = its_cmd_mask_field(cmdptr, 2, 0, 52) & GENMASK(51, 8);
+
+    if ( !its->dev_table )
+        return -1;
+
+    spin_lock(&its->its_lock);
+    if ( valid )
+        its->dev_table[devid] = DEV_TABLE_ENTRY(itt_addr, size + 1);
+    else
+        its->dev_table[devid] = 0;
+
+    spin_unlock(&its->its_lock);
+
+    /* DomUs (will later) have their ITTs allocated at domain creation time,
+     * when Dom0 configures the passthrough.
+     */
+    if ( its->hw_its )
+        return gicv3_its_map_device(its->hw_its,
+                                    its->d, devid, size + 1, valid);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -279,6 +307,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
+        case GITS_CMD_MAPD:
+            its_handle_mapd(its, cmdptr);
+	    break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#89 lore raw

Re: [RFC PATCH 16/24] ARM: vITS: handle MAPD command

Quoted 22 lines · Andre Przywara

The MAPD command maps a device by associating a memory region for storing ITTEs with a certain device ID.
We just store the given guest physical address in the device table. We don't map the device tables permanently, as their alignment requirement is only 256 Bytes, thus making mapping of several tables complicated. We map the device tables on demand when we need them later.

Also we propagate the MAPD request to the hardware ITS, as the device ID is only meaningful there.

---
 xen/arch/arm/vgic-its.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
xen/arch/arm/vgic-its.c +0 -0
index caad320..83d47e1 100644
@@ -250,6 +250,34 @@ static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
     return ret;
 }

xen/arch/arm/vgic-its.c static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr) · line 261

Quoted 10 lines
xen/arch/arm/vgic-its.c +9 -0
 
+static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    int size = its_cmd_get_size(cmdptr);
+    bool valid = its_cmd_get_validbit(cmdptr);
+    paddr_t itt_addr = its_cmd_mask_field(cmdptr, 2, 0, 52) & GENMASK(51, 8);
+
+    if ( !its->dev_table )
+        return -1;

We should validate devid, size and itt_addr?

Quoted 34 lines
xen/arch/arm/vgic-its.c +21 -0
+    spin_lock(&its->its_lock);
+    if ( valid )
+        its->dev_table[devid] = DEV_TABLE_ENTRY(itt_addr, size + 1);
+    else
+        its->dev_table[devid] = 0;
+
+    spin_unlock(&its->its_lock);
+
+    /* DomUs (will later) have their ITTs allocated at domain creation time,
+     * when Dom0 configures the passthrough.
+     */
+    if ( its->hw_its )
+        return gicv3_its_map_device(its->hw_its,
+                                    its->d, devid, size + 1, valid);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -279,6 +307,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
+        case GITS_CMD_MAPD:
+            its_handle_mapd(its, cmdptr);
+	    break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#90 lore raw

RFC PATCH 17/24 ARM: vITS: handle MAPTI command

In reply to Andre Przywara

The MAPTI commands associates a DeviceID/EventID pair with a LPI/CPU pair and actually instantiates LPI interrupts.
We allocate a new host LPI and connect that one to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/vgic-its.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
xen/arch/arm/vgic-its.c +53 -0
index 83d47e1..70897dd 100644
@@ -278,6 +278,55 @@ static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    uint32_t intid = its_cmd_get_physical_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vits_itte *itte;
+    uint32_t host_lpi;
+    struct vcpu *vcpu;
+    int ret = -1;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
+    if ( collid >= its->max_collections )
+        return -1;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    if ( !vcpu )
+        goto out_unlock;
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    if ( itte->hlpi )
+        goto out_unmap;
+
+    host_lpi = gicv3_lpi_allocate_host_lpi(its->hw_its,
+                                           devid, eventid,
+                                           vcpu, intid);
+    if ( host_lpi >= 0 )
+        itte->hlpi = host_lpi;
+
+    itte->vlpi = intid;
+    itte->collection = collid;
+
+    ret = 0;
+
+out_unmap:
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+    
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -310,6 +359,10 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPD:
             its_handle_mapd(its, cmdptr);
 	    break;
+        case GITS_CMD_MAPI:
+        case GITS_CMD_MAPTI:
+            its_handle_mapti(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#91 lore raw

Re: [RFC PATCH 17/24] ARM: vITS: handle MAPTI command

Quoted 37 lines · Andre Przywara

The MAPTI commands associates a DeviceID/EventID pair with a LPI/CPU pair and actually instantiates LPI interrupts.
We allocate a new host LPI and connect that one to this virtual LPI, so that any triggering IRQ on the host can be quickly forwarded to a guest.

---
 xen/arch/arm/vgic-its.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
xen/arch/arm/vgic-its.c +18 -0
index 83d47e1..70897dd 100644
@@ -278,6 +278,55 @@ static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr)
     return 0;
 }
 
+static int its_handle_mapti(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    uint32_t intid = its_cmd_get_physical_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vits_itte *itte;
+    uint32_t host_lpi;
+    struct vcpu *vcpu;
+    int ret = -1;
+
+    if ( its_cmd_get_command(cmdptr) == GITS_CMD_MAPI )
+        intid = eventid;
+
+    if ( collid >= its->max_collections )
+        return -1;
+
+    spin_lock(&its->its_lock);

xen/arch/arm/vgic-its.c static int its_handle_mapd(struct virt_its *its, uint64_t *cmdptr) · line 308

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    vcpu = get_vcpu_from_collection(its, collid);
+    if ( !vcpu )
+        goto out_unlock;
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    if ( itte->hlpi )
+        goto out_unmap;

get_vcpu_from_collection and get_devid_evid take care of checking the validity of devid, eventid and collid. Do we need to also check that devid, eventid and intid are valid from a host perspective, given that we are calling gicv3_lpi_allocate_host_lpi?

Quoted 37 lines
xen/arch/arm/vgic-its.c +24 -0
+    host_lpi = gicv3_lpi_allocate_host_lpi(its->hw_its,
+                                           devid, eventid,
+                                           vcpu, intid);
+    if ( host_lpi >= 0 )
+        itte->hlpi = host_lpi;
+
+    itte->vlpi = intid;
+    itte->collection = collid;
+
+    ret = 0;
+
+out_unmap:
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+    
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -310,6 +359,10 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPD:
             its_handle_mapd(its, cmdptr);
 	    break;
+        case GITS_CMD_MAPI:
+        case GITS_CMD_MAPTI:
+            its_handle_mapti(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#92 lore raw

RFC PATCH 18/24 ARM: vITS: handle MOVI command

In reply to Andre Przywara

The MOVI command moves the interrupt affinity from one redistributor (read: VCPU) to another.
For now migration of "live" LPIs is not yet implemented, but we store the changed affinity in the host LPI structure and in our virtual ITTE.

---
 xen/arch/arm/gic-its.c        | 16 +++++++++++++++
 xen/arch/arm/vgic-its.c       | 46 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 3 files changed, 63 insertions(+)
xen/arch/arm/gic-its.c +16 -0
index 6bac422..d1b1cbb 100644
@@ -618,6 +618,22 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
     return 0;
 }
 
+/* Changes the target VCPU for a given host LPI assigned to a domain. */
+int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id)
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return -1;
+
+    hlpi.data = hlpip->data;
+    hlpi.vcpu_id = new_vcpu_id;
+    hlpip->data = hlpi.data;
+
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +46 -0

58 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +1 -0
index 3b2e5c0..7e1142f 100644
@@ -133,6 +133,7 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#93 lore raw

Re: [RFC PATCH 18/24] ARM: vITS: handle MOVI command

Quoted 22 lines · Andre Przywara

The MOVI command moves the interrupt affinity from one redistributor (read: VCPU) to another.
For now migration of "live" LPIs is not yet implemented, but we store the changed affinity in the host LPI structure and in our virtual ITTE.

---
 xen/arch/arm/gic-its.c        | 16 +++++++++++++++
 xen/arch/arm/vgic-its.c       | 46 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  1 +
 3 files changed, 63 insertions(+)
xen/arch/arm/gic-its.c +2 -0
index 6bac422..d1b1cbb 100644
@@ -618,6 +618,22 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
     return 0;
 }
 
+/* Changes the target VCPU for a given host LPI assigned to a domain. */
+int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id)

xen/arch/arm/gic-its.c int gicv3_lpi_drop_host_lpi(struct host_its *its, · line 632

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return -1;
+
+    hlpi.data = hlpip->data;
+    hlpi.vcpu_id = new_vcpu_id;
+    hlpip->data = hlpi.data;

Most surely we need to call vgic_migrate_irq

Quoted 85 lines
xen/arch/arm/gic-its.c +3 -0
+    return 0;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +46 -0
index 70897dd..c0a60ad 100644
@@ -327,6 +327,46 @@ out_unlock:
     return ret;
 }
 
+static int its_handle_movi(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    int collid = its_cmd_get_collection(cmdptr);
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t host_lpi = 0;
+
+    if ( collid >= its->max_collections )
+        return -1;
+
+    spin_lock(&its->its_lock);
+
+    vcpu = get_vcpu_from_collection(its, collid);
+    if ( !vcpu )
+        goto out_unlock;
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    itte->collection = collid;
+    host_lpi = itte->hlpi;
+
+    /* TODO: lookup currently-in-guest virtual IRQs and migrate them */
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !host_lpi )
+        return -1;
+
+    gicv3_lpi_change_vcpu(its->d, host_lpi, vcpu->vcpu_id);
+
+    return 0;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -363,6 +403,12 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_MAPTI:
             its_handle_mapti(its, cmdptr);
             break;
+        case GITS_CMD_MOVALL:
+            gdprintk(XENLOG_G_INFO, "ITS: ignoring MOVALL command\n");
+            break;
+        case GITS_CMD_MOVI:
+            its_handle_movi(its, cmdptr);
+            break;
         case GITS_CMD_SYNC:
             /* We handle ITS commands synchronously, so we ignore SYNC. */
 	    break;
xen/include/asm-arm/gic-its.h +1 -0
index 3b2e5c0..7e1142f 100644
@@ -133,6 +133,7 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#94 lore raw

RFC PATCH 19/24 ARM: vITS: handle DISCARD command

In reply to Andre Przywara

The DISCARD command drops the connection between a DeviceID/EventID and an LPI/collection pair.
We mark the respective structure entries as not allocated and make sure that any queued IRQs are removed.

---
 xen/arch/arm/gic-its.c        | 21 +++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 48 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  5 +++++
 3 files changed, 74 insertions(+)
xen/arch/arm/gic-its.c +21 -0
index d1b1cbb..766a7cb 100644
@@ -634,6 +634,27 @@ int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id)
     return 0;
 }
 
+/* Looks up a given host LPI assigned to that domain and returns the
+ * connected virtual LPI number. Also stores the target vcpu ID in
+ * the passed vcpu_id pointer.
+ * Returns 0 if no host LPI could be found for that domain, or the
+ * virtual LPI number (>= 8192) if the lookup succeeded.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return 0;
+
+    hlpi.data = hlpip->data;
+    if ( vcpu_id )
+        *vcpu_id = hlpi.vcpu_id;
+
+    return hlpi.virt_lpi;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +48 -0

60 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +5 -0
index 7e1142f..3f5698d 100644
@@ -133,6 +133,11 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+/* Given a physical LPI, looks up and returns the associated virtual LPI
+ * and the target VCPU in the given domain.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
+                              int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#95 lore raw

Re: [RFC PATCH 19/24] ARM: vITS: handle DISCARD command

Quoted 60 lines · Andre Przywara

The DISCARD command drops the connection between a DeviceID/EventID and an LPI/collection pair.
We mark the respective structure entries as not allocated and make sure that any queued IRQs are removed.

---
 xen/arch/arm/gic-its.c        | 21 +++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 48 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  5 +++++
 3 files changed, 74 insertions(+)
xen/arch/arm/gic-its.c +21 -0
index d1b1cbb..766a7cb 100644
@@ -634,6 +634,27 @@ int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id)
     return 0;
 }
 
+/* Looks up a given host LPI assigned to that domain and returns the
+ * connected virtual LPI number. Also stores the target vcpu ID in
+ * the passed vcpu_id pointer.
+ * Returns 0 if no host LPI could be found for that domain, or the
+ * virtual LPI number (>= 8192) if the lookup succeeded.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
+{
+    union host_lpi *hlpip, hlpi;
+
+    hlpip = gic_find_host_lpi(host_lpi, d);
+    if ( !hlpip )
+        return 0;
+
+    hlpi.data = hlpip->data;
+    if ( vcpu_id )
+        *vcpu_id = hlpi.vcpu_id;
+
+    return hlpi.virt_lpi;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +8 -0
index c0a60ad..028d234 100644
@@ -367,6 +367,51 @@ out_unlock:
     return 0;
 }
 
+static int its_handle_discard(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);
+    struct pending_irq *pirq;
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t vlpi;

xen/arch/arm/vgic-its.c out_unlock: · line 387

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    int ret = -1, vcpu_id;
+
+    spin_lock(&its->its_lock);
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;
+
+    vlpi = gicv3_lpi_lookup_lpi(its->d, itte->hlpi, &vcpu_id);
+    if ( !vlpi )
+        goto out_unlock;

Using itte->hlpi like that is very dangerous because the guest could be modifying that field while we run gicv3_lpi_lookup_lpi or gicv3_lpi_drop_host_lpi. Actually we need a compiler barrier after reading all guest accessible fields and before using them to access our own data structures.

xen/arch/arm/vgic-its.c out_unlock: · line 399

Quoted 11 lines
xen/arch/arm/vgic-its.c +11 -0
+    vcpu = its->d->vcpu[vcpu_id];
+
+    pirq = lpi_to_pending(vcpu, vlpi, false);
+    if ( pirq )
+    {
+        clear_bit(GIC_IRQ_GUEST_QUEUED, &pirq->status);
+        gic_remove_from_queues(vcpu, vlpi);
+
+        /* Mark this pending IRQ struct as availabe again. */
+        if ( !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+            pirq->irq = 0;

We need to do something in case the vlpi is in a GICH_LR register

xen/arch/arm/vgic-its.c out_unlock: · line 402

Quoted 3 lines
xen/arch/arm/vgic-its.c +3 -0
+    }
+
+    gicv3_lpi_drop_host_lpi(its->hw_its, devid, eventid, itte->hlpi);

Same here regarding itte->hlpi

Quoted 43 lines
xen/arch/arm/vgic-its.c +14 -0
+    itte->hlpi = 0;             /* Mark this ITTE as unused. */
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    return ret;
+}
+
 #define ITS_CMD_BUFFER_SIZE(baser)      ((((baser) & 0xff) + 1) << 12)
 
 static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
@@ -390,6 +435,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_CLEAR:
             its_handle_clear(its, cmdptr);
             break;
+        case GITS_CMD_DISCARD:
+            its_handle_discard(its, cmdptr);
+            break;
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +5 -0
index 7e1142f..3f5698d 100644
@@ -133,6 +133,11 @@ int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
 int gicv3_lpi_allocate_host_lpi(struct host_its *its,
                                 uint32_t devid, uint32_t eventid,
                                 struct vcpu *v, int virt_lpi);
+/* Given a physical LPI, looks up and returns the associated virtual LPI
+ * and the target VCPU in the given domain.
+ */
+uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
+                              int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#96 lore raw

RFC PATCH 20/24 ARM: vITS: handle INV command

In reply to Andre Przywara

The INV command instructs the ITS to update the configuration data for a given LPI by re-reading its entry from the property table. We don't need to care so much about the priority value, but enabling or disabling an LPI has some effect: We remove or push virtual LPIs to their VCPUs, also propagate the enable bit to the hardware.

---
 xen/arch/arm/gic-its.c        | 35 ++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 74 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  3 ++
 3 files changed, 112 insertions(+)
xen/arch/arm/gic-its.c +35 -0
index 766a7cb..6f4329f 100644
@@ -215,6 +215,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -655,6 +668,28 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled)
+{
+    host_lpi -= 8192;
+
+    if ( host_lpi >= MAX_HOST_LPIS )
+        return;
+
+    if ( !its )
+        return;
+
+    if (enabled)
+        lpi_data.lpi_property[host_lpi] |= LPI_PROP_ENABLED;
+    else
+        lpi_data.lpi_property[host_lpi] &= ~LPI_PROP_ENABLED;
+
+    __flush_dcache_area(&lpi_data.lpi_property[host_lpi], 1);
+    its_send_cmd_inv(its, deviceid, eventid);
+    its_send_cmd_sync(its, 0);
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +74 -0

86 lines not on this page · raw message

xen/include/asm-arm/gic-its.h +3 -0
index 3f5698d..2cdb3e1 100644
@@ -139,6 +139,9 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
                               int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#97 lore raw

Re: [RFC PATCH 20/24] ARM: vITS: handle INV command

Quoted 77 lines · Andre Przywara

The INV command instructs the ITS to update the configuration data for a given LPI by re-reading its entry from the property table. We don't need to care so much about the priority value, but enabling or disabling an LPI has some effect: We remove or push virtual LPIs to their VCPUs, also propagate the enable bit to the hardware.

---
 xen/arch/arm/gic-its.c        | 35 ++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 74 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  3 ++
 3 files changed, 112 insertions(+)
xen/arch/arm/gic-its.c +36 -0
index 766a7cb..6f4329f 100644
@@ -215,6 +215,19 @@ static int its_send_cmd_mapd(struct host_its *its, uint32_t deviceid,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_inv(struct host_its *its,
+                            uint32_t deviceid, uint32_t eventid)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
+    cmd[1] = eventid;
+    cmd[2] = 0x00;
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -655,6 +668,28 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled)
+{
+    host_lpi -= 8192;
+
+    if ( host_lpi >= MAX_HOST_LPIS )
+        return;
+
+    if ( !its )
+        return;
+
+    if (enabled)
+        lpi_data.lpi_property[host_lpi] |= LPI_PROP_ENABLED;
+    else
+        lpi_data.lpi_property[host_lpi] &= ~LPI_PROP_ENABLED;
+
+    __flush_dcache_area(&lpi_data.lpi_property[host_lpi], 1);
+
+    its_send_cmd_inv(its, deviceid, eventid);
+    its_send_cmd_sync(its, 0);
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/vgic-its.c +2 -0
index 028d234..74da8fc 100644
@@ -223,6 +223,77 @@ out_unlock:
     return ret;
 }
 
+/* For a given virtual LPI read the enabled bit from the virtual property
+ * table and update the virtual IRQ's state.

xen/arch/arm/vgic-its.c out_unlock: · line 237

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+ * This enables or disables the associated hardware LPI, also takes care
+ * of removing or pushing of virtual LPIs to their VCPUs.
+ */
+static void update_lpi_enabled_status(struct virt_its* its,
+                                      struct vcpu *vcpu, uint32_t vlpi,
+                                      uint32_t deviceid, uint32_t eventid,
+                                      uint32_t hlpi)
+{
+    struct pending_irq *pirq = lpi_to_pending(vcpu, vlpi, false);
+    uint8_t property = its->d->arch.vgic.proptable[vlpi - 8192];

We need to check vlpi before using to access an array. We also need a barrier before using property.

Quoted 31 lines
xen/arch/arm/vgic-its.c +31 -0
+    if ( property & LPI_PROP_ENABLED )
+    {
+        if ( pirq )
+        {
+            unsigned long flags;
+
+            set_bit(GIC_IRQ_GUEST_ENABLED, &pirq->status);
+            spin_lock_irqsave(&vcpu->arch.vgic.lock, flags);
+            if ( !list_empty(&pirq->inflight) &&
+                 !test_bit(GIC_IRQ_GUEST_VISIBLE, &pirq->status) )
+                gic_raise_guest_irq(vcpu, vlpi, property & 0xfc);
+            spin_unlock_irqrestore(&vcpu->arch.vgic.lock, flags);
+
+        }
+        gicv3_lpi_set_enable(its->hw_its, deviceid, eventid, hlpi, true);
+    }
+    else
+    {
+        if ( pirq )
+        {
+            clear_bit(GIC_IRQ_GUEST_ENABLED, &pirq->status);
+            gic_remove_from_queues(vcpu, vlpi);
+        }
+        gicv3_lpi_set_enable(its->hw_its, deviceid, eventid, hlpi, false);
+    }
+}
+
+static int its_handle_inv(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t devid = its_cmd_get_deviceid(cmdptr);
+    uint32_t eventid = its_cmd_get_id(cmdptr);

xen/arch/arm/vgic-its.c out_unlock: · line 278

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+    struct vits_itte *itte;
+    struct vcpu *vcpu;
+    uint32_t hlpi, vlpi;
+    int ret = -1;
+
+    spin_lock(&its->its_lock);
+
+    itte = get_devid_evid(its, devid, eventid);
+    if ( !itte )
+        goto out_unlock;

We need to check itte->collection before using it to access d->vcpu.

Quoted 35 lines
xen/arch/arm/vgic-its.c +20 -0
+    vcpu = its->d->vcpu[itte->collection];
+    vlpi = itte->vlpi;
+    hlpi = itte->hlpi;
+
+    ret = 0;
+
+    put_devid_evid(its, itte);
+
+out_unlock:
+    spin_unlock(&its->its_lock);
+
+    if ( !ret )
+        update_lpi_enabled_status(its, vcpu, vlpi, devid, eventid, hlpi);
+
+    return ret;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -441,6 +512,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INT:
             its_handle_int(its, cmdptr);
             break;
+        case GITS_CMD_INV:
+            its_handle_inv(its, cmdptr);
+	    break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +0 -0
index 3f5698d..2cdb3e1 100644

xen/include/asm-arm/gic-its.h int gicv3_lpi_allocate_host_lpi(struct host_its *its, · line 147

Quoted 9 lines
xen/include/asm-arm/gic-its.h +3 -0
@@ -139,6 +139,9 @@ int gicv3_lpi_allocate_host_lpi(struct host_its *its,
 uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi,
                               int *vcpu_id);
 int gicv3_lpi_change_vcpu(struct domain *d, uint32_t host_lpi, int new_vcpu_id);
+void gicv3_lpi_set_enable(struct host_its *its,
+                          uint32_t deviceid, uint32_t eventid,
+                          uint32_t host_lpi, bool enabled);
 int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#98 lore raw

RFC PATCH 21/24 ARM: vITS: handle INVALL command

In reply to Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +58 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
 	    break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+	    break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +2 -0
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#99 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 68 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +40 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 10 lines
xen/arch/arm/gic-its.c +10 -0
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

Quoted 76 lines
xen/arch/arm/gic-its.c +8 -0
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
            break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+           break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +2 -1
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
--

2.9.0

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#100 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi,

Quoted 74 lines · Vijay Kilari
Quoted 73 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +45 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
Quoted 8 lines

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

Cheers,
Andre.

Quoted 76 lines
Quoted 76 lines
xen/arch/arm/gic-its.c +8 -0
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
            break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+           break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +2 -1
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
--

2.9.0

Quoted 5 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#101 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 86 lines · Andre Przywara

Hi,

Quoted 74 lines · Vijay Kilari
Quoted 73 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +45 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
Quoted 8 lines

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

Quoted 5 lines

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

We need to protect Xen from too frequent and too expensive requests like this.

Quoted 76 lines
Quoted 76 lines
Quoted 76 lines
xen/arch/arm/gic-its.c +8 -0
+
+    /* Tell all ITSes that they should update the property table for CPU 0,
+     * which is where we map all LPIs to.
+     */
+    list_for_each_entry(its, &host_its_list, entry)
+        its_send_cmd_invall(its, 0);
+}
+
 void gicv3_lpi_set_enable(struct host_its *its,
                           uint32_t deviceid, uint32_t eventid,
                           uint32_t host_lpi, bool enabled)
xen/arch/arm/vgic-its.c +30 -0
index 74da8fc..1e429b7 100644
@@ -294,6 +294,33 @@ out_unlock:
     return ret;
 }
 
+/* INVALL updates the per-LPI configuration status for every LPI mapped to
+ * this redistributor. For the guest side we don't need to update anything,
+ * as we always refer to the actual table for the enabled bit and the
+ * priority.
+ * Enabling or disabling a virtual LPI however needs to be propagated to
+ * the respective host LPI. Instead of iterating over all mapped LPIs in our
+ * emulated GIC (which is expensive due to the required on-demand mapping),
+ * we iterate over all mapped _host_ LPIs and filter for those which are
+ * forwarded to this virtual redistributor.
+ */
+static int its_handle_invall(struct virt_its *its, uint64_t *cmdptr)
+{
+    uint32_t collid = its_cmd_get_collection(cmdptr);
+    struct vcpu *vcpu;
+
+    spin_lock(&its->its_lock);
+    vcpu = get_vcpu_from_collection(its, collid);
+    spin_unlock(&its->its_lock);
+
+    if ( !vcpu )
+        return -1;
+
+    gicv3_lpi_update_configurations(vcpu, its->d->arch.vgic.proptable);
+
+    return 0;
+}
+
 static int its_handle_mapc(struct virt_its *its, uint64_t *cmdptr)
 {
     uint32_t collid = its_cmd_get_collection(cmdptr);
@@ -515,6 +542,9 @@ static int vgic_its_handle_cmds(struct domain *d, struct virt_its *its,
         case GITS_CMD_INV:
             its_handle_inv(its, cmdptr);
            break;
+        case GITS_CMD_INVALL:
+            its_handle_invall(its, cmdptr);
+           break;
         case GITS_CMD_MAPC:
             its_handle_mapc(its, cmdptr);
             break;
xen/include/asm-arm/gic-its.h +2 -1
index 2cdb3e1..ba6b2d5 100644
@@ -146,6 +146,8 @@ int gicv3_lpi_drop_host_lpi(struct host_its *its,
                             uint32_t devid, uint32_t eventid,
                             uint32_t host_lpi);
 
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable);
+
 static inline int gicv3_lpi_get_priority(struct domain *d, uint32_t lpi)
 {
     return d->arch.vgic.proptable[lpi - 8192] & 0xfc;
--

2.9.0

Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#102 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi,

Quoted 94 lines · Stefano Stabellini
Quoted 84 lines · Andre Przywara
Quoted 74 lines · Vijay Kilari
Quoted 73 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through the host LPI table to find any LPIs targetting this VCPU. We then update the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +45 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE; chunk++)
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i], hlpi;
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
Quoted 8 lines

xen/arch/arm/gic-its.c uint32_t gicv3_lpi_lookup_lpi(struct domain *d, uint32_t host_lpi, int *vcpu_id) · line 720

Quoted 5 lines
xen/arch/arm/gic-its.c +5 -0
+                lpi_data.lpi_property[hlpi_nr - 8192] |= LPI_PROP_ENABLED;
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &= ~LPI_PROP_ENABLED;
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

Quoted 5 lines

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Quoted 7 lines

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

It is not necessary for an ITS implementation to wait until an INVALL/INV command is issued to take into account the change of the LPI configuration tables (aka property table in this thread).

So how about trapping the property table? We would still have to go through the property table the first time (i.e when writing into the GICR_PROPBASER), but INVALL would be a nop.

The idea would be unmapping the region when GICR_PROPBASER is written. So any read/write access would be trapped. For a write access, Xen will update the LPIs internal data structures and write the value in the guest page unmapped. If we don't want to have an overhead for the read access, we could just write-protect the page in stage-2 page table. So only write access would be trapped.

Going further, for the ITS, Xen is using the guest memory to store the ITS information. This means Xen has to validate the information at every access. So how about restricting the access in stage-2 page table? That would remove the overhead of validating data.

Any thoughts?

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#103 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 133 lines · Julien Grall

Hi,

Quoted 107 lines · Stefano Stabellini
Quoted 97 lines · Andre Przywara
Quoted 87 lines · Vijay Kilari
Quoted 85 lines · Andre Przywara

The INVALL command instructs an ITS to invalidate the configuration data for all LPIs associated with a given redistributor (read: VCPU). To avoid iterating (and mapping!) all guest tables, we instead go through
the host LPI table to find any LPIs targetting this VCPU. We then update
the configuration bits for the connected virtual LPIs.

---
 xen/arch/arm/gic-its.c        | 58
+++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vgic-its.c       | 30 ++++++++++++++++++++++
 xen/include/asm-arm/gic-its.h |  2 ++
 3 files changed, 90 insertions(+)
xen/arch/arm/gic-its.c +13 -0
index 6f4329f..5129d6e 100644
@@ -228,6 +228,18 @@ static int its_send_cmd_inv(struct host_its *its,
     return its_send_command(its, cmd);
 }
 
+static int its_send_cmd_invall(struct host_its *its, int cpu)
+{
+    uint64_t cmd[4];
+
+    cmd[0] = GITS_CMD_INVALL;
+    cmd[1] = 0x00;
+    cmd[2] = cpu & GENMASK(15, 0);
+    cmd[3] = 0x00;
+
+    return its_send_command(its, cmd);
+}
+
 int gicv3_its_map_device(struct host_its *hw_its, struct domain *d,
                          int devid, int bits, bool valid)
 {
@@ -668,6 +680,52 @@ uint32_t gicv3_lpi_lookup_lpi(struct domain *d,
 uint32_t host_lpi, int *vcpu_id)
     return hlpi.virt_lpi;
 }
 
+/* Iterate over all host LPIs, and updating the "enabled" state for a

given
+ * guest redistributor (VCPU) given the respective state in the provided
+ * proptable. This proptable is indexed by the stored virtual LPI number.
+ * This is to implement a guest INVALL command.
+ */
+void gicv3_lpi_update_configurations(struct vcpu *v, uint8_t *proptable)

xen/arch/arm/gic-its.c +5 -0
+{
+    int chunk, i;
+    struct host_its *its;
+
+    for (chunk = 0; chunk < MAX_HOST_LPIS / HOST_LPIS_PER_PAGE;

chunk++)

xen/arch/arm/gic-its.c +7 -0
+    {
+        if ( !lpi_data.host_lpis[chunk] )
+            continue;
+
+        for (i = 0; i < HOST_LPIS_PER_PAGE; i++)
+        {
+            union host_lpi *hlpip = &lpi_data.host_lpis[chunk][i],

hlpi;

xen/arch/arm/gic-its.c +16 -0
+            uint32_t hlpi_nr;
+
+            hlpi.data = hlpip->data;
+            if ( !hlpi.virt_lpi )
+                continue;
+
+            if ( hlpi.dom_id != v->domain->domain_id )
+                continue;
+
+            if ( hlpi.vcpu_id != v->vcpu_id )
+                continue;
+
+            hlpi_nr = chunk * HOST_LPIS_PER_PAGE + i;
+
+            if ( proptable[hlpi.virt_lpi] & LPI_PROP_ENABLED )
+                lpi_data.lpi_property[hlpi_nr - 8192] |=

LPI_PROP_ENABLED;

Quoted 8 lines

xen/arch/arm/gic-its.c

Quoted 5 lines
xen/arch/arm/gic-its.c +2 -0
+            else
+                lpi_data.lpi_property[hlpi_nr - 8192] &=

~LPI_PROP_ENABLED;

xen/arch/arm/gic-its.c +2 -0
+        }
+    }

AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

Quoted 5 lines

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Quoted 7 lines

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

It is not necessary for an ITS implementation to wait until an INVALL/INV command is issued to take into account the change of the LPI configuration tables (aka property table in this thread).

So how about trapping the property table? We would still have to go through the property table the first time (i.e when writing into the GICR_PROPBASER), but INVALL would be a nop.

The idea would be unmapping the region when GICR_PROPBASER is written. So any read/write access would be trapped. For a write access, Xen will update the LPIs internal data structures and write the value in the guest page unmapped. If we don't want to have an overhead for the read access, we could just write-protect the page in stage-2 page table. So only write access would be trapped.

Quoted 6 lines

Going further, for the ITS, Xen is using the guest memory to store the ITS information. This means Xen has to validate the information at every access. So how about restricting the access in stage-2 page table? That would remove the overhead of validating data.

Any thoughts?

It is a promising idea. Let me expand on this.

I agree that on INVALL if we need to do anything, we should go through the virtual property table rather than the full list of host lpis.

Once we agree on that, the two options we have are:

1) We let the guest write anything to the table, then we do a full validation of the table on INVALL. We also do a validation of the table entries used as parameters for any other commands.

2) We map the table read-only, then do a validation of every guest write. INVALL becomes a NOP and parameters validation for many commands could be removed or at least reduced.

Conceptually the two options should both lead to exactly the same result. Therefore I think the decision should be made purely on
performance: which one is faster? If it is true that INVALL is only typically called once I suspect that 1) is faster, but I would like to see some simple benchmarks, such as the time that it takes to configure the ITS from scratch with the two approaches.

That said, even if 1) turns out to be faster and the approach of choice, the idea of making the tables read-only in stage-2 could still be useful to simplify parameters validation and protect Xen from concurrent changes of the table entries from another guest vcpu. If the tables as RW, we need to be very careful in Xen and use barriers to avoid re-reading any guest table entry twice, as the guest could be changing it in parallel to exploit the hypervisor.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#104 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Stefano,

Quoted 46 lines · Stefano Stabellini
Quoted 45 lines · Julien Grall
Quoted 16 lines · Stefano Stabellini
Quoted 11 lines · Andre Przywara
Quoted 4 lines · Vijay Kilari

On Wed, Sep 28, 2016 at 11:54 PM, Andre Przywara
AFAIK, the initial design is to use tasklet to update property
table as it consumes
lot of time to update the table.

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Quoted 7 lines

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

It is not necessary for an ITS implementation to wait until an INVALL/INV command is issued to take into account the change of the LPI configuration tables (aka property table in this thread).

So how about trapping the property table? We would still have to go through the property table the first time (i.e when writing into the GICR_PROPBASER), but INVALL would be a nop.

The idea would be unmapping the region when GICR_PROPBASER is written. So any read/write access would be trapped. For a write access, Xen will update the LPIs internal data structures and write the value in the guest page unmapped. If we don't want to have an overhead for the read access, we could just write-protect the page in stage-2 page table. So only write access would be trapped.

Going further, for the ITS, Xen is using the guest memory to store the ITS information. This means Xen has to validate the information at every access. So how about restricting the access in stage-2 page table? That would remove the overhead of validating data.

Quoted 6 lines
Quoted 1 line

Any thoughts?

It is a promising idea. Let me expand on this.

I agree that on INVALL if we need to do anything, we should go through the virtual property table rather than the full list of host lpis.

I agree on that.

Quoted 2 lines

Once we agree on that, the two options we have are:

I believe we had a similar discussion when Vijay worked on the vITS (see [1]). I would have hoped that this new proposal took into account the constraint mentioned back then.

Quoted 9 lines

1) We let the guest write anything to the table, then we do a full validation of the table on INVALL. We also do a validation of the table entries used as parameters for any other commands.

2) We map the table read-only, then do a validation of every guest write. INVALL becomes a NOP and parameters validation for many commands could be removed or at least reduced.

Quoted 6 lines

Conceptually the two options should both lead to exactly the same result. Therefore I think the decision should be made purely on
performance: which one is faster? If it is true that INVALL is only typically called once I suspect that 1) is faster, but I would like to see some simple benchmarks, such as the time that it takes to configure the ITS from scratch with the two approaches.

The problem is not which one is faster but which one will not take down the hypervisor.

The guest is allowed to create a command queue as big as 1MB, a command use 32 bytes, so the command queue can fit up 32640 commands.

Now imagine a malicious guest filling up the command queue with INVALL and then notify the via (via GITS_CWRITER). Based on patch #5, all those commands will be handled in one. So you have to multiple the time of one command by 32640 times.

Given that the hypervisor is not preemptible, it likely means a DOS.
A similar problem would happen if an vITS command is translate to an ITS command (see the implementation of INVALL). Multiple malicious guest could slow down the other guest by filling up the host command queue. Worst, a command from a normal guest could be discarded because the host ITS command queue is full (see its_send_command in gic-its.c).

That's why in the approach we had on the previous series was "host ITS command should be limited when emulating guest ITS command". From my recall, in that series the host and guest LPIs was fully separated (enabling a guest LPIs was not enabling host LPIs).

That said, a design doc explaining all the constraints and code flow would have been really helpful. It took me a while to digest and understand the interaction between each part of the code. The design document would have also been a good place to discuss about problems that span across multiple patches (like the command queue emulation).

Quoted 9 lines

That said, even if 1) turns out to be faster and the approach of choice, the idea of making the tables read-only in stage-2 could still be useful to simplify parameters validation and protect Xen from concurrent changes of the table entries from another guest vcpu. If the tables as RW, we need to be very careful in Xen and use barriers to avoid re-reading any guest table entry twice, as the guest could be changing it in parallel to exploit the hypervisor.

Yes, and this is true for all the tables (PROPBASER, BASER,...) that reside on guest memory. Most of them should not be touched by the guest.

This is the same for the command queue (patch #12), accessing the command directly from the guest memory is not safe. A guest could modify the value behind our back.

Regards,

[1] https://xenbits.xen.org/people/ianc/vits/draftG.html

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#105 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 97 lines · Julien Grall

Hi Stefano,

Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Julien Grall
Quoted 18 lines · Stefano Stabellini
Quoted 7 lines · Andre Przywara
Quoted 5 lines · Vijay Kilari

On Wed, Sep 28, 2016 at 11:54 PM, Andre Przywara
AFAIK, the initial design is to use tasklet to update
property
table as it consumes
lot of time to update the table.

Quoted 6 lines

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the
whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Quoted 7 lines

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

It is not necessary for an ITS implementation to wait until an INVALL/INV command is issued to take into account the change of the LPI configuration tables (aka property table in this thread).

So how about trapping the property table? We would still have to go through
the property table the first time (i.e when writing into the GICR_PROPBASER),
but INVALL would be a nop.

The idea would be unmapping the region when GICR_PROPBASER is written. So any
read/write access would be trapped. For a write access, Xen will update the
LPIs internal data structures and write the value in the guest page unmapped.
If we don't want to have an overhead for the read access, we could just write-protect the page in stage-2 page table. So only write access would be
trapped.

Going further, for the ITS, Xen is using the guest memory to store the ITS information. This means Xen has to validate the information at every access.
So how about restricting the access in stage-2 page table? That would remove
the overhead of validating data.

Quoted 6 lines
Quoted 1 line

Any thoughts?

It is a promising idea. Let me expand on this.

I agree that on INVALL if we need to do anything, we should go through the virtual property table rather than the full list of host lpis.

I agree on that.

Quoted 2 lines

Once we agree on that, the two options we have are:

I believe we had a similar discussion when Vijay worked on the vITS (see [1]). I would have hoped that this new proposal took into account the constraint mentioned back then.

Quoted 9 lines

1) We let the guest write anything to the table, then we do a full validation of the table on INVALL. We also do a validation of the table entries used as parameters for any other commands.

2) We map the table read-only, then do a validation of every guest write. INVALL becomes a NOP and parameters validation for many commands could be removed or at least reduced.

Quoted 6 lines

Conceptually the two options should both lead to exactly the same result. Therefore I think the decision should be made purely on
performance: which one is faster? If it is true that INVALL is only typically called once I suspect that 1) is faster, but I would like to see some simple benchmarks, such as the time that it takes to configure the ITS from scratch with the two approaches.

The problem is not which one is faster but which one will not take down the hypervisor.

The guest is allowed to create a command queue as big as 1MB, a command use 32 bytes, so the command queue can fit up 32640 commands.

Quoted 6 lines

Now imagine a malicious guest filling up the command queue with INVALL and then notify the via (via GITS_CWRITER). Based on patch #5, all those commands will be handled in one. So you have to multiple the time of one command by 32640 times.

Given that the hypervisor is not preemptible, it likely means a DOS.

I think it can be made to work safely using a rate-limiting technique. Such as: Xen is only going to emulate an INVALL for a given domain only once every one or two seconds and no more often than that. x86 has something like that under xen/arch/x86/irq.c, see irq_ratelimit.

But to be clear, I am not saying that this is necessarily the best way to do it. I would like to see some benchmarks first.

Quoted 5 lines

A similar problem would happen if an vITS command is translate to an ITS command (see the implementation of INVALL). Multiple malicious guest could slow down the other guest by filling up the host command queue. Worst, a command from a normal guest could be discarded because the host ITS command queue is full (see its_send_command in gic-its.c).

Looking at the patches, nothing checks for discarded physical ITS commands. Not good.

Quoted 4 lines

That's why in the approach we had on the previous series was "host ITS command should be limited when emulating guest ITS command". From my recall, in that series the host and guest LPIs was fully separated (enabling a guest LPIs was not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 19 lines

That said, a design doc explaining all the constraints and code flow would have been really helpful. It took me a while to digest and understand the interaction between each part of the code. The design document would have also been a good place to discuss about problems that span across multiple patches (like the command queue emulation).

Quoted 9 lines

That said, even if 1) turns out to be faster and the approach of choice, the idea of making the tables read-only in stage-2 could still be useful to simplify parameters validation and protect Xen from concurrent changes of the table entries from another guest vcpu. If the tables as RW, we need to be very careful in Xen and use barriers to avoid re-reading any guest table entry twice, as the guest could be changing it in parallel to exploit the hypervisor.

Yes, and this is true for all the tables (PROPBASER, BASER,...) that reside on guest memory. Most of them should not be touched by the guest.

Quoted 7 lines

This is the same for the command queue (patch #12), accessing the command directly from the guest memory is not safe. A guest could modify the value behind our back.

Regards,

[1] https://xenbits.xen.org/people/ianc/vits/draftG.html

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#106 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 133 lines · Stefano Stabellini
Quoted 97 lines · Julien Grall

Hi Stefano,

Quoted 56 lines · Stefano Stabellini
Quoted 55 lines · Julien Grall
Quoted 18 lines · Stefano Stabellini
Quoted 7 lines · Andre Przywara
Quoted 5 lines · Vijay Kilari

On Wed, Sep 28, 2016 at 11:54 PM, Andre Przywara
AFAIK, the initial design is to use tasklet to update
property
table as it consumes
lot of time to update the table.

Quoted 6 lines

This is a possible, but premature optimization.
Linux (at the moment, at least) only calls INVALL _once_, just after initialising the collections. And at this point no LPI is mapped, so the
whole routine does basically nothing - and that quite fast. We can later have any kind of fancy algorithm if there is a need for.

I understand, but as-is it's so expensive that could be a DOS vector. Also other OSes could issue INVALL much more often than Linux.

Quoted 7 lines

Considering that we might support device assigment with ITS soon, I think it might be best to parse per-domain virtual tables rather than the full list of physical LPIs, which theoretically could be much larger. Or alternatively we need to think about adding another field to lpi_data, to link together all lpis assigned to the same domain, but that would cost even more memory. Or we could rate-limit the INVALL calls to one every few seconds or something. Or all of the above :-)

It is not necessary for an ITS implementation to wait until an INVALL/INV command is issued to take into account the change of the LPI configuration tables (aka property table in this thread).

So how about trapping the property table? We would still have to go through
the property table the first time (i.e when writing into the GICR_PROPBASER),
but INVALL would be a nop.

The idea would be unmapping the region when GICR_PROPBASER is written. So any
read/write access would be trapped. For a write access, Xen will update the
LPIs internal data structures and write the value in the guest page unmapped.
If we don't want to have an overhead for the read access, we could just write-protect the page in stage-2 page table. So only write access would be
trapped.

Going further, for the ITS, Xen is using the guest memory to store the ITS information. This means Xen has to validate the information at every access.
So how about restricting the access in stage-2 page table? That would remove
the overhead of validating data.

Quoted 6 lines
Quoted 1 line

Any thoughts?

It is a promising idea. Let me expand on this.

I agree that on INVALL if we need to do anything, we should go through the virtual property table rather than the full list of host lpis.

I agree on that.

Quoted 2 lines

Once we agree on that, the two options we have are:

I believe we had a similar discussion when Vijay worked on the vITS (see [1]). I would have hoped that this new proposal took into account the constraint mentioned back then.

Quoted 9 lines

1) We let the guest write anything to the table, then we do a full validation of the table on INVALL. We also do a validation of the table entries used as parameters for any other commands.

2) We map the table read-only, then do a validation of every guest write. INVALL becomes a NOP and parameters validation for many commands could be removed or at least reduced.

Quoted 6 lines

Conceptually the two options should both lead to exactly the same result. Therefore I think the decision should be made purely on
performance: which one is faster? If it is true that INVALL is only typically called once I suspect that 1) is faster, but I would like to see some simple benchmarks, such as the time that it takes to configure the ITS from scratch with the two approaches.

The problem is not which one is faster but which one will not take down the hypervisor.

The guest is allowed to create a command queue as big as 1MB, a command use 32 bytes, so the command queue can fit up 32640 commands.

Quoted 6 lines

Now imagine a malicious guest filling up the command queue with INVALL and then notify the via (via GITS_CWRITER). Based on patch #5, all those commands will be handled in one. So you have to multiple the time of one command by 32640 times.

Given that the hypervisor is not preemptible, it likely means a DOS.

I think it can be made to work safely using a rate-limiting technique. Such as: Xen is only going to emulate an INVALL for a given domain only once every one or two seconds and no more often than that. x86 has something like that under xen/arch/x86/irq.c, see irq_ratelimit.

But to be clear, I am not saying that this is necessarily the best way to do it. I would like to see some benchmarks first.

Quoted 5 lines

A similar problem would happen if an vITS command is translate to an ITS command (see the implementation of INVALL). Multiple malicious guest could slow down the other guest by filling up the host command queue. Worst, a command from a normal guest could be discarded because the host ITS command queue is full (see its_send_command in gic-its.c).

Looking at the patches, nothing checks for discarded physical ITS commands. Not good.

Quoted 4 lines

That's why in the approach we had on the previous series was "host ITS command should be limited when emulating guest ITS command". From my recall, in that series the host and guest LPIs was fully separated (enabling a guest LPIs was not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 6 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?) It also helps toward solving the INVALL potential DOS issue, because it significantly reduces the computation needed when an INVALL is issued by the guest.

On the other end, this approach has the potential of consuming much more memory to map all the possible pLPIs that a device could use up to the theoretical max. Of course that is not good either. But fortunately for PCI devices we know how many events a device can generate. Also we should be able to get that info on device tree for other devices. So I suggest Xen only maps as many pLPIs as events the device can generate, when the device is assigned to the guest. This way there would be no wasted memory.

Does it make sense? Do you think it could work?

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#107 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi,

Quoted 19 lines · Stefano Stabellini
Quoted 11 lines · Stefano Stabellini
Quoted 5 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command should be limited when emulating guest ITS command". From my recall, in that series the host and guest LPIs was fully separated (enabling a guest LPIs was not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 6 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Quoted 13 lines

It also helps
toward solving the INVALL potential DOS issue, because it significantly reduces the computation needed when an INVALL is issued by the guest.

On the other end, this approach has the potential of consuming much more memory to map all the possible pLPIs that a device could use up to the theoretical max. Of course that is not good either. But fortunately for PCI devices we know how many events a device can generate. Also we should be able to get that info on device tree for other devices. So I suggest Xen only maps as many pLPIs as events the device can generate, when the device is assigned to the guest. This way there would be no wasted memory.

Quoted 1 line

Does it make sense? Do you think it could work?

Aside the point I raised above, I think the approach looks sensible.

Regards,

[1] https://xenbits.xen.org/people/ianc/vits/draftG.html#device-assignment

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#108 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.

Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#109 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi,

sorry for chiming in late ....

I've been spending some time thinking about this, and I think we can in fact get away without ever propagating command from domains to the host.

I made a list of all commands that possible require host ITS command propagation. There are two groups:
1: enabling/disabling LPIs: INV, INVALL
2: mapping/unmapping devices/events: DISCARD, MAPD, MAPTI.

The second group can be handled by mapping all required devices up front, I will elaborate on that in a different email.

For the first group, read below ...

Quoted 58 lines · Stefano Stabellini
Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

Quoted 6 lines

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

So why do we need a _counter_? This is about edge triggered interrupts, I think we can just accumulate all of them into one.

So here is what I think:
- We use the guest provided pending table to hold a pending bit for each VLPI. We can unmap the memory from the guest, since software is not supposed to access this table as per the spec.
- We use the guest provided property table, without trapping it. There is nothing to be "validated" in that table, since it's a really tight encoding and every value written in there is legal. We only look at bit 0 for this exercise here anyway.
- Upon reception of a physical LPI, we look it up to find the VCPU and virtual LPI number. This is what we need to do anyway and it's a quick two-level table lookup at the moment.
- We use the VCPU's domain and the VLPI number to index the guest's property table and read the enabled bit. Again a quick table lookup.
- If the VLPI is enabled, we EOI it on the host and inject it.
- If the VLPI is disabled, we set the pending bit in the VCPU's pending table and EOI on the host - to allow other IRQs.
- On a guest INV command, we check whether that vLPI is now enabled:
- If it is disabled now, we don't need to do anything.
- If it is enabled now, we check the pending bit for that VLPI:
- If it is 0, we don't do anything.
- If it is 1, we inject the VLPI and clear the pending bit.
- On a guest INVALL command, we just need to iterate over the virtual LPIs. If you look at the conditions above, the only immediate action is when a VLPI gets enabled _and_ its pending bit is set. So we can do 64-bit read accesses over the whole pending table to find non-zero words and thus set bits, which should be rare in practice. We can store the highest mapped VLPI to avoid iterating over the whole of the table. Ideally the guest has no direct control over the pending bits, since this is what the device generates. Also we limit the number of VLPIs in total per guest anyway.

If that still sounds like a DOS vector, we could additionally rate-limit INVALLs, and/or track additions to the pending table after the last
INVALL: if there haven't been any new pending bits since the last scan, INVALL is a NOP.

Does that makes sense so far?

So that just leaves us with this IRQ storm issue, which I am thinking about now. But I guess this is not a show stopper given we can disable the physical LPI if we sense this situation.

Quoted 7 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.
Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Why would that be necessary?

Cheers,
Andre

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#110 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 80 lines · Andre Przywara

Hi,

sorry for chiming in late ....

I've been spending some time thinking about this, and I think we can in fact get away without ever propagating command from domains to the host.

I made a list of all commands that possible require host ITS command propagation. There are two groups:
1: enabling/disabling LPIs: INV, INVALL
2: mapping/unmapping devices/events: DISCARD, MAPD, MAPTI.

The second group can be handled by mapping all required devices up front, I will elaborate on that in a different email.

For the first group, read below ...

Quoted 62 lines · Stefano Stabellini
Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

Quoted 5 lines
Quoted 2 lines

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

So why do we need a _counter_? This is about edge triggered interrupts, I think we can just accumulate all of them into one.

The counter is not to re-inject the same amount of interrupts into the guest, but to detect interrupt storms.

Quoted 8 lines

So here is what I think:
- We use the guest provided pending table to hold a pending bit for each VLPI. We can unmap the memory from the guest, since software is not supposed to access this table as per the spec.
- We use the guest provided property table, without trapping it. There is nothing to be "validated" in that table, since it's a really tight encoding and every value written in there is legal. We only look at bit 0 for this exercise here anyway.

I am following...

Quoted 5 lines

- Upon reception of a physical LPI, we look it up to find the VCPU and virtual LPI number. This is what we need to do anyway and it's a quick two-level table lookup at the moment.
- We use the VCPU's domain and the VLPI number to index the guest's property table and read the enabled bit. Again a quick table lookup.

They should be both O(2), correct?

Quoted 10 lines

- If the VLPI is enabled, we EOI it on the host and inject it.
- If the VLPI is disabled, we set the pending bit in the VCPU's pending table and EOI on the host - to allow other IRQs.
- On a guest INV command, we check whether that vLPI is now enabled:
- If it is disabled now, we don't need to do anything.
- If it is enabled now, we check the pending bit for that VLPI:
- If it is 0, we don't do anything.
- If it is 1, we inject the VLPI and clear the pending bit.
- On a guest INVALL command, we just need to iterate over the virtual LPIs.

Right, much better.

Quoted 8 lines

If you look at the conditions above, the only immediate action is when a VLPI gets enabled _and_ its pending bit is set. So we can do 64-bit read accesses over the whole pending table to find non-zero words and thus set bits, which should be rare in practice. We can store the highest mapped VLPI to avoid iterating over the whole of the table. Ideally the guest has no direct control over the pending bits, since this is what the device generates. Also we limit the number of VLPIs in total per guest anyway.

I wonder if we could even use a fully packed bitmask with only the pending bits, so 1 bit per vLPI, rather than 1 byte per vLPI. That would be a nice improvement.

Quoted 6 lines

If that still sounds like a DOS vector, we could additionally rate-limit INVALLs, and/or track additions to the pending table after the last
INVALL: if there haven't been any new pending bits since the last scan, INVALL is a NOP.

Does that makes sense so far?

It makes sense. It should be OK.

Quoted 3 lines

So that just leaves us with this IRQ storm issue, which I am thinking about now. But I guess this is not a show stopper given we can disable the physical LPI if we sense this situation.

That is true and it's exactly what we should do.

Quoted 9 lines
Quoted 7 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.
Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Why would that be necessary?

Because the state of the LRs of other pCPUs won't be up to date: we wouldn't know for sure whether the guest EOI'ed the vLPI or not.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#111 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Stefano,

Quoted 9 lines · Stefano Stabellini
Quoted 8 lines · Andre Przywara
Quoted 7 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.
Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Quoted 4 lines
Quoted 1 line

Why would that be necessary?

Because the state of the LRs of other pCPUs won't be up to date: we wouldn't know for sure whether the guest EOI'ed the vLPI or not.

Well, there is still a small window when the interrupt may be received on the previous pCPU. So we have to take into account this case.

This window may be bigger with LPIs, because a single vCPU may have thousand interrupts routed. This would take a long time to move all of them when the vCPU is migrating. So we may want to take a lazy approach and moving them when they are received on the "wrong" pCPU.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#112 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 12 lines · Julien Grall

Hi Stefano,

Quoted 9 lines · Stefano Stabellini
Quoted 8 lines · Andre Przywara
Quoted 7 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.
Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Why would that be necessary?

Because the state of the LRs of other pCPUs won't be up to date: we wouldn't know for sure whether the guest EOI'ed the vLPI or not.

Well, there is still a small window when the interrupt may be received on the previous pCPU. So we have to take into account this case.

That's right. We already have a mechanism to deal with that, based on the GIC_IRQ_GUEST_MIGRATING flag. It should work with LPIs too.

Quoted 4 lines

This window may be bigger with LPIs, because a single vCPU may have thousand interrupts routed. This would take a long time to move all of them when the vCPU is migrating. So we may want to take a lazy approach and moving them when they are received on the "wrong" pCPU.

That's possible. The only downside is that modifying the irq migration workflow is difficult and we might want to avoid it if possible.

Another approach is to let the scheduler know that migration is slower. In fact this is not a new problem: it can be slow to migrate interrupts, even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler has any knowledge of that (CC'ing George and Dario). I guess that's the reason why most people run with dom0_vcpus_pin.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#113 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Stefano,

Quoted 15 lines · Stefano Stabellini
Quoted 14 lines · Julien Grall

Hi Stefano,

Quoted 11 lines · Stefano Stabellini
Quoted 9 lines · Andre Przywara
Quoted 7 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.
Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Why would that be necessary?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Because the state of the LRs of other pCPUs won't be up to date: we wouldn't know for sure whether the guest EOI'ed the vLPI or not.

Well, there is still a small window when the interrupt may be received on the previous pCPU. So we have to take into account this case.

That's right. We already have a mechanism to deal with that, based on the GIC_IRQ_GUEST_MIGRATING flag. It should work with LPIs too.

Right.

Quoted 7 lines
Quoted 4 lines

This window may be bigger with LPIs, because a single vCPU may have thousand interrupts routed. This would take a long time to move all of them when the vCPU is migrating. So we may want to take a lazy approach and moving them when they are received on the "wrong" pCPU.

That's possible. The only downside is that modifying the irq migration workflow is difficult and we might want to avoid it if possible.

I don't think this would modify the irq migration work flow. If you look at the implementation of arch_move_irqs, it will just go over the vIRQ and call irq_set_affinity.

irq_set_affinity will directly modify the hardware and that's all.

Quoted 6 lines

Another approach is to let the scheduler know that migration is slower. In fact this is not a new problem: it can be slow to migrate interrupts, even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler has any knowledge of that (CC'ing George and Dario). I guess that's the reason why most people run with dom0_vcpus_pin.

I gave a quick look at x86, arch_move_irqs is not implemented. Only PIRQ are migrated when a vCPU is moving to another pCPU.

The function pirq_set_affinity, will change the affinity of a PIRQ but only in software (see irq_set_affinity). This is not yet replicated the configuration into the hardware.

In the case of ARM, we directly modify the configuration of the hardware. This adds much more overhead because you have to do an hardware access for every single IRQ.

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#114 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 27 lines · Julien Grall
Quoted 10 lines
Quoted 7 lines

This window may be bigger with LPIs, because a single vCPU may have thousand
interrupts routed. This would take a long time to move all of them when the
vCPU is migrating. So we may want to take a lazy approach and moving them when
they are received on the "wrong" pCPU.

That's possible. The only downside is that modifying the irq migration workflow is difficult and we might want to avoid it if possible.

I don't think this would modify the irq migration work flow. If you look at the implementation of arch_move_irqs, it will just go over the vIRQ and call irq_set_affinity.

irq_set_affinity will directly modify the hardware and that's all.

Quoted 6 lines

Another approach is to let the scheduler know that migration is slower. In fact this is not a new problem: it can be slow to migrate interrupts, even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler has any knowledge of that (CC'ing George and Dario). I guess that's the reason why most people run with dom0_vcpus_pin.

I gave a quick look at x86, arch_move_irqs is not implemented. Only PIRQ are migrated when a vCPU is moving to another pCPU.

Quoted 7 lines

The function pirq_set_affinity, will change the affinity of a PIRQ but only in software (see irq_set_affinity). This is not yet replicated the configuration into the hardware.

In the case of ARM, we directly modify the configuration of the hardware. This adds much more overhead because you have to do an hardware access for every single IRQ.

George, Dario, any comments on whether this would make sense and how to do it?

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#115 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 22 lines · Stefano Stabellini
Quoted 11 lines · Julien Grall

Quoted 9 lines

Another approach is to let the scheduler know that migration is slower.
In fact this is not a new problem: it can be slow to migrate interrupts,
even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler
has any knowledge of that (CC'ing George and Dario). I guess that's the
reason why most people run with dom0_vcpus_pin.

Quoted 9 lines

I gave a quick look at x86, arch_move_irqs is not implemented. Only PIRQ are
migrated when a vCPU is moving to another pCPU.

In the case of ARM, we directly modify the configuration of the hardware. This
adds much more overhead because you have to do an hardware access for every
single IRQ.

Quoted 4 lines

George, Dario, any comments on whether this would make sense and how to
do it?

I was actually looking into this, but I think I don't know enough of ARM in general, and about this issue in particular to be useful.

That being said, perhaps you could clarify a bit what you mean with "let the scheduler know that migration is slower". What you'd expect the scheduler to do?

Checking the code, as Julien says, on x86 all we do when we move vCPUs around is calling evtchn_move_pirqs(). In fact, it was right that function that was called multiple times in schedule.c, and it was you that (as Julien pointed out already):
1) in 5bd62a757b9 ("xen/arm: physical irq follow virtual irq"), created arch_move_irqs() as something that does something on ARM, and as an empty stub in x86.
2) in 14f7e3b8a70 ("xen: introduce sched_move_irqs"), generalized schedule.c code by implementing sched_move_irqs().

So, if I understood correctly what Julien said here "I don't think this would modify the irq migration work flow. etc.", it looks to me that the suggested lazy approach could be a good solution (but I'm saying that lacking the knowledge of what it would actually mean to implement that).

If you want something inside the scheduler that sort of delays the wakeup of a domain on the new pCPU until some condition in IRQ handling code is verified (but, please, confirm whether or not it was this that you were thinking of), my thoughts, out of the top of my head about this are:
- in general, I think it should be possible;
- it has to be arch-specific, I think?
- It's easy to avoid the vCPU being woken as a consequence of vcpu_wake() being called, e.g., at the end of vcpu_migrate();
- we must be careful about not forgetting/failing to (re)wakeup the vCPU when the condition verifies

Sorry if I can't be more useful than this for now. :-/

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#116 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 62 lines · Dario Faggioli
Quoted 22 lines · Stefano Stabellini
Quoted 11 lines · Julien Grall

Quoted 9 lines

Another approach is to let the scheduler know that migration is slower.
In fact this is not a new problem: it can be slow to migrate interrupts,
even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler
has any knowledge of that (CC'ing George and Dario). I guess that's the
reason why most people run with dom0_vcpus_pin.

Quoted 9 lines

I gave a quick look at x86, arch_move_irqs is not implemented. Only PIRQ are
migrated when a vCPU is moving to another pCPU.

In the case of ARM, we directly modify the configuration of the hardware. This
adds much more overhead because you have to do an hardware access for every
single IRQ.

Quoted 4 lines

George, Dario, any comments on whether this would make sense and how to
do it?

I was actually looking into this, but I think I don't know enough of ARM in general, and about this issue in particular to be useful.

That being said, perhaps you could clarify a bit what you mean with "let the scheduler know that migration is slower". What you'd expect the scheduler to do?

Checking the code, as Julien says, on x86 all we do when we move vCPUs around is calling evtchn_move_pirqs(). In fact, it was right that function that was called multiple times in schedule.c, and it was you that (as Julien pointed out already):
1) in 5bd62a757b9 ("xen/arm: physical irq follow virtual irq"), created arch_move_irqs() as something that does something on ARM, and as an empty stub in x86.
2) in 14f7e3b8a70 ("xen: introduce sched_move_irqs"), generalized schedule.c code by implementing sched_move_irqs().

So, if I understood correctly what Julien said here "I don't think this would modify the irq migration work flow. etc.", it looks to me that the suggested lazy approach could be a good solution (but I'm saying that lacking the knowledge of what it would actually mean to implement that).

If you want something inside the scheduler that sort of delays the wakeup of a domain on the new pCPU until some condition in IRQ handling code is verified (but, please, confirm whether or not it was this that you were thinking of), my thoughts, out of the top of my head about this are:
- in general, I think it should be possible;
- it has to be arch-specific, I think?
- It's easy to avoid the vCPU being woken as a consequence of vcpu_wake() being called, e.g., at the end of vcpu_migrate();
- we must be careful about not forgetting/failing to (re)wakeup the vCPU when the condition verifies

Quoted 1 line

Sorry if I can't be more useful than this for now. :-/

We don't need scheduler support to implement interrupt migration. The question was much simpler than that: moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU. Does the scheduler know that? Or blindly moves vCPUs around? Also see below.

Quoted 17 lines · Stefano Stabellini
Quoted 10 lines

Another approach is to let the scheduler know that migration is slower.
In fact this is not a new problem: it can be slow to migrate interrupts,
even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler
has any knowledge of that (CC'ing George and Dario). I guess that's the
reason why most people run with dom0_vcpus_pin.

Oh, and about this last sentence.

I may indeed be lacking knowledge/understanding, but if you think this is a valid use case for dom0_vcpus_pin, I'd indeed be interested in knowing why.

Quoted 6 lines

In fact, that configuration has always looked rather awkward to me, and I think we should start thinking stopping providing the option at all (or changing/extending its behavior).

So, if you think you need it, please spell that out, and let's see if there are better ways to achieve the same. :-)

That's right, I think dom0_vcpus_pin is a good work-around for the lack of scheduler knowledge about interrupts. If the scheduler knew that moving vCPU0 from pCPU0 to pCPU1 is far more expensive than moving vCPU3 from pCPU3 to pCPU1 then it would make better decision and we wouldn't need dom0_vcpus_pin.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#117 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#118 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 19 lines · Stefano Stabellini
Quoted 7 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a
vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned
to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed
to a given pCPU and is set to be forwarded to a guest vCPU running on it
by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of
these physical interrupts, a corresponding virtual interrupt (could be a
different irq) will be injected into the guest vCPU.

Quoted 7 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are
migrated with it. The physical interrupt migration has a cost. However,
receiving physical interrupts on the wrong pCPU has an higher cost.

Yeah, I got in what sense you said "assigned", but thanks anyway for this clarification. It indeed makes the picture more clear (even just FTR) :-)

Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#119 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Stefano Stabellini

Hi Stefano,

Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#120 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 26 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Quoted 7 lines

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

Regardless of how we implement interrupts migrations on ARM, I think it still makes sense for the scheduler to know about it. I realize that this is a separate point. Even if we spread interrupts migrations over time, it still has a cost, in terms of latency as I wrote above, but also in terms of interactions with interrupt controllers and ITSes. A vCPU with no interrupts assigned to it poses no such problems. The scheduler should be aware of the difference. If the scheduler knew, I bet that vCPU migration would be a rare event for vCPUs that have many interrupts assigned to them. For example, Dom0 vCPU0 would never be moved, and dom0_pin_vcpus would be superfluous.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#121 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Stefano,

Quoted 42 lines · Stefano Stabellini
Quoted 26 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Quoted 7 lines

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

Quoted 13 lines

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

Migrating all the interrupts can be really expensive because in the current state we have to go through every single interrupt and check whether the interrupt has been routed to this vCPU. We will also route disabled interrupt. And this seems really pointless. This may need some optimization here.

With ITS, we may have thousand of interrupts routed to a vCPU. This means that for every interrupt we have to issue a command in the host ITS queue. You will likely fill up the command queue and add much more latency.

Even if you consider the vCPU migration to be a rare case. You could still get the pCPU stuck for tens of milliseconds, the time to migrate everything. And I don't think this is not acceptable.

Anyway, I would like to see measurement in both situation before deciding when LPIs will be migrated.

Quoted 10 lines

Regardless of how we implement interrupts migrations on ARM, I think it still makes sense for the scheduler to know about it. I realize that this is a separate point. Even if we spread interrupts migrations over time, it still has a cost, in terms of latency as I wrote above, but also in terms of interactions with interrupt controllers and ITSes. A vCPU with no interrupts assigned to it poses no such problems. The scheduler should be aware of the difference. If the scheduler knew, I bet that vCPU migration would be a rare event for vCPUs that have many interrupts assigned to them. For example, Dom0 vCPU0 would never be moved, and dom0_pin_vcpus would be superfluous.

The number of interrupts routed to a vCPU will vary over the time, this will depend what the guest decides to do, so you need scheduler to adapt. And in fine, you give the guest a chance to "control" the scheduler depending how the interrupts are spread between vCPU.

If the number increases, you may end up to have the scheduler to decide to not migrate the vCPU because it will be too expensive. But you may have a situation where migrating a vCPU with many interrupts is the only possible choice and you will slow down the platform.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#122 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 66 lines · Julien Grall

Hi Stefano,

Quoted 49 lines · Stefano Stabellini
Quoted 37 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt
to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received
interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a
single command queue).

Furthermore, the current approach will move every single interrupt routed a
the vCPU, even those disabled. That's pointless and a waste of resource. You
may argue that we can skip the ones disabled, but in that case what would be
the benefits to migrate the IRQs while migrate the vCPUs?

Quoted 3 lines

So I would suggest to spread it over the time. This also means less headache
for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

Quoted 13 lines

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

Quoted 4 lines

Migrating all the interrupts can be really expensive because in the current state we have to go through every single interrupt and check whether the interrupt has been routed to this vCPU. We will also route disabled interrupt. And this seems really pointless. This may need some optimization here.

Indeed, that should be fixed.

Quoted 7 lines

With ITS, we may have thousand of interrupts routed to a vCPU. This means that for every interrupt we have to issue a command in the host ITS queue. You will likely fill up the command queue and add much more latency.

Even if you consider the vCPU migration to be a rare case. You could still get the pCPU stuck for tens of milliseconds, the time to migrate everything. And I don't think this is not acceptable.

[...]

Quoted 4 lines

If the number increases, you may end up to have the scheduler to decide to not migrate the vCPU because it will be too expensive. But you may have a situation where migrating a vCPU with many interrupts is the only possible choice and you will slow down the platform.

A vCPU with thousand of interrupts routed to it, is the case where I would push back to the scheduler. It should know that moving the vcpu would be very costly.

Regardless, we need to figure out a way to move the interrupts without "blocking" the platform for long. In practice, we might find a
threshold: a number of active interrupts above which we cannot move them all at once anymore. Something like: we move the first 500 active interrupts immediately, we delay the rest. We can find this threshold only with practical measurements.

Quoted 2 lines

Anyway, I would like to see measurement in both situation before deciding when LPIs will be migrated.

Yes, let's be scientific about this.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#123 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Stefano Stabellini

Hi,

Quoted 42 lines · Stefano Stabellini
Quoted 26 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Quoted 7 lines

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

Quoted 13 lines

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

So if this is such a crucial issue, why don't we use the ITS for good this time? The ITS hardware probably supports 16 bits worth of collection IDs, so what about we assign each VCPU (in every guest) a unique collection ID on the host and do a MAPC & MOVALL on a VCPU migration to let it point to the right physical redistributor. I see that this does not cover all use cases (> 65536 VCPUs, for instance), also depends much of many implementation details:
- How costly is a MOVALL? It needs to scan the pending table and transfer set bits to the other redistributor, which may take a while.
- Is there an impact if we exceed the number of hardware backed collections (GITS_TYPE[HCC])? If the ITS is forced to access system memory for every table lookup, this may slow down everyday operations.
- How likely are those misdirected interrupts in the first place? How often do we migrate VCPU compared to the the interrupt frequency?

There are more, subtle parameters to consider, so I guess we just need to try and measure.

Quoted 10 lines

Regardless of how we implement interrupts migrations on ARM, I think it still makes sense for the scheduler to know about it. I realize that this is a separate point. Even if we spread interrupts migrations over time, it still has a cost, in terms of latency as I wrote above, but also in terms of interactions with interrupt controllers and ITSes. A vCPU with no interrupts assigned to it poses no such problems. The scheduler should be aware of the difference. If the scheduler knew, I bet that vCPU migration would be a rare event for vCPUs that have many interrupts assigned to them. For example, Dom0 vCPU0 would never be moved, and dom0_pin_vcpus would be superfluous.

That's a good point, so indeed the "interrupt load" should be a scheduler parameter. But as you said: that's a different story.

Cheers,
Andre.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#124 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 57 lines · Andre Przywara
Quoted 42 lines · Stefano Stabellini
Quoted 26 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Quoted 7 lines

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

Quoted 13 lines

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

Quoted 7 lines

So if this is such a crucial issue, why don't we use the ITS for good this time? The ITS hardware probably supports 16 bits worth of collection IDs, so what about we assign each VCPU (in every guest) a unique collection ID on the host and do a MAPC & MOVALL on a VCPU migration to let it point to the right physical redistributor. I see that this does not cover all use cases (> 65536 VCPUs, for instance), also depends much of many implementation details:

This is certainly an idea worth exploring. We don't need to assign a collection ID to every vCPU, just the ones that have LPIs assigned to them, which should be considerably fewer.

Quoted 2 lines

- How costly is a MOVALL? It needs to scan the pending table and transfer set bits to the other redistributor, which may take a while.

This is an hardware operation, even if it is not fast, I'd prefer to rely on that, rather than implementing something complex in software. Usually hardware gets better over time at this sort of things.

Quoted 3 lines

- Is there an impact if we exceed the number of hardware backed collections (GITS_TYPE[HCC])? If the ITS is forced to access system memory for every table lookup, this may slow down everyday operations.

We'll have to fall back to manually moving them one by one.

Quoted 2 lines

- How likely are those misdirected interrupts in the first place? How often do we migrate VCPU compared to the the interrupt frequency?

This is where is scheduler work comes in.

Quoted 2 lines

There are more, subtle parameters to consider, so I guess we just need to try and measure.

That's right. This is why I have been saying that we need numbers. This is difficult hardware, difficult code and difficult scenarios. Intuition only gets us so far. We need to be scientific and measure the approach we decide to take, and maybe even one that we decided not to take, to figure out whether it is actually acceptable.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#125 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 47 lines · Stefano Stabellini
Quoted 43 lines · Andre Przywara
Quoted 42 lines · Stefano Stabellini
Quoted 26 lines · Julien Grall
Quoted 12 lines · Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned to it. You could say that the slowness is directly proportional do the number of interrupts assigned to the vCPU.

To be pedantic, by "assigned" I mean that a physical interrupt is routed to a given pCPU and is set to be forwarded to a guest vCPU running on it by the _IRQ_GUEST flag. The guest could be dom0. Upon receiving one of these physical interrupts, a corresponding virtual interrupt (could be a different irq) will be injected into the guest vCPU.

Quoted 4 lines

When the vCPU is migrated to a new pCPU, the physical interrupts that are configured to be injected as virtual interrupts into the vCPU, are migrated with it. The physical interrupt migration has a cost. However, receiving physical interrupts on the wrong pCPU has an higher cost.

I don't understand why it is a problem for you to receive the first interrupt to the wrong pCPU and moving it if necessary.

While this may have an higher cost (I don't believe so) on the first received interrupt, migrating thousands of interrupts at the same time is very expensive and will likely get Xen stuck for a while (think about ITS with a single command queue).

Quoted 7 lines

Furthermore, the current approach will move every single interrupt routed a the vCPU, even those disabled. That's pointless and a waste of resource. You may argue that we can skip the ones disabled, but in that case what would be the benefits to migrate the IRQs while migrate the vCPUs?

So I would suggest to spread it over the time. This also means less headache for the scheduler developers.

The most important aspect of interrupts handling in Xen is latency, measured as the time between Xen receiving a physical interrupt and the guest receiving it. This latency should be both small and deterministic.

We all agree so far, right?

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines

The issue with spreading interrupts migrations over time is that it makes interrupt latency less deterministic. It is OK, in the uncommon case of vCPU migration with interrupts, to take a hit for a short time. This "hit" can be measured. It can be known. If your workload cannot tolerate it, vCPUs can be pinned. It should be a rare event anyway. On the other

Quoted 8 lines
Quoted 8 lines

hand, by spreading interrupts migrations, we make it harder to predict latency. Aside from determinism, another problem with this approach is that it ensures that every interrupt assigned to a vCPU will first hit the wrong pCPU, then it will be moved. It guarantees the worst-case scenario for interrupt latency for the vCPU that has been moved. If we migrated all interrupts as soon as possible, we would minimize the amount of interrupts delivered to the wrong pCPU. Most interrupts would be delivered to the new pCPU right away, reducing interrupt latency.

OK, so ultimately for each interrupt we can take an “eager” approach and move it as soon as the vcpu moves, or a “lazy” approach and move it after it fires.

The two options which have been discussed are:
1. Always take an eager approach, and try to tell the scheduler to limit the migration frequency for these vcpus more than others
2. Always take a lazy approach, and leave the scheduler the way it is.

Another approach which one might take:
3. Eagerly migrate a subset of the interrupts and lazily migrate the others. For instance, we could eagerly migrate all the interrupts which have fired since the last vcpu migration. In a system where migrations happen frequently, this should only be a handful; in a system that migrates infrequently, this will be more, but it won’t matter, because it will happen less often.

Workloads which need predictable IRQ latencies should probably be pinning their vcpus anyway.

So at the moment, the scheduler already tries to avoid migrating things *a little bit* if it can (see migrate_resist). It’s not clear to me at the moment whether this is enough or not. Or to put it a different way — how long should the scheduler try to wait before moving one of these vcpus? At the moment I haven’t seen a good way of calculating this.

#3 to me has the feeling of being somewhat more satisfying, but also potentially fairly complicated. Since the scheduler already does migration resistance somewhat, #1 would be a simpler to implement in the sort run. If it turns out that #1 has other drawbacks, we can implement #3 as and when needed.

Thoughts?

-George

Quoted 1 line

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#126 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 28 lines · George Dunlap
Quoted 20 lines
Quoted 18 lines · Stefano Stabellini
Quoted 18 lines

The issue with spreading interrupts migrations over time is that it makes
interrupt latency less deterministic. It is OK, in the uncommon case of
vCPU migration with interrupts, to take a hit for a short time. This
"hit" can be measured. It can be known. If your workload cannot tolerate
it, vCPUs can be pinned. It should be a rare event anyway. On the other
hand, by spreading interrupts migrations, we make it harder to predict
latency. Aside from determinism, another problem with this approach is
that it ensures that every interrupt assigned to a vCPU will first hit
the wrong pCPU, then it will be moved. It guarantees the worst- case

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

scenario for interrupt latency for the vCPU that has been moved. If we
migrated all interrupts as soon as possible, we would minimize the
amount of interrupts delivered to the wrong pCPU. Most interrupts would
be delivered to the new pCPU right away, reducing interrupt latency.

Quoted 9 lines


Another approach which one might take:
3. Eagerly migrate a subset of the interrupts and lazily migrate the others.  For instance, we could eagerly migrate all the interrupts which have fired since the last vcpu migration.  In a system where migrations happen frequently, this should only be a handful; in a system that migrates infrequently, this will be more, but it won’t matter, because it will happen less often.

Yes, if doable (e.g., I don't know how easy and practical is to know and keep track of fired interrupts) this looks a good solution to me too.

Quoted 4 lines

So at the moment, the scheduler already tries to avoid migrating things *a little bit* if it can (see migrate_resist).  It’s not clear to me at the moment whether this is enough or not.

Well, true, but migration resistance, in Credit2, is just a fixed value which:
 1. is set at boot time;
 2. is always the same for all vcpus;
 3. is always the same, no matter what a vcpu is doing.

And even if we make it tunable and changeable at runtime (which I intend to do), it's still something pretty "static" because of 2 and 3.

And even if we make it tunable per-vcpu (which is doable), it would be rather hard to decide to what value to set it, for each vcpu. And, of course, 3 would still apply (i.e., it would change according to the vcpu workload or characteristics).

So, it's guessing. More or less fine grained, but always guessing.

On the other hand, using something proportional to nr. of routed interrupt as the migration resistance threshold would overcome all 1, 2 and 3. It would give us a migrate_resist value which is adaptive, and is determined according to actual workload of properties of a specific vcpu.
Feeding routed interrupt info to the load balancer comes from similar reasoning (and we actually may want to do both).

FTR, Credit1 has a similar mechanism, i.e., it *even wilded guesses* whether a vcpu could still have some of its data in cache, and tries not to migrate it if it's likely (see __csched_vcpu_is_cache_hot()). We can improve that too, although it is a lot more complex and less predictable, as usual with Credit1.

Quoted 3 lines

Or to put it a different way — how long should the scheduler try to wait before moving one of these vcpus?

Yep, it's similar to the "anticipation" problem in I/O schedulers (where "excessive seeks" ~= "too frequent migrations").

 https://en.wikipedia.org/wiki/Anticipatory_scheduling

Quoted 2 lines

At the moment I haven’t seen a good way of calculating this.

Exactly, and basing the calculation on the number of routed interrupt --and, if possible, other metrics too-- could be that "good way" we're looking for.

It would need experimenting, of course, but I like the idea.

Quoted 8 lines

#3 to me has the feeling of being somewhat more satisfying, but also potentially fairly complicated.  Since the scheduler already does migration resistance somewhat, #1 would be a simpler to implement in the sort run.  If it turns out that #1 has other drawbacks, we can implement #3 as and when needed.

Thoughts?

Yes, we can do things incrementally, which is always good. I like your #1 proposal because it has the really positive side effect of bringing us in the camp of adaptive migration resistance, which is something pretty advanced and pretty cool, if we manage to do it right. :-)

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#127 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Stefano Stabellini
Quoted 13 lines · Stefano Stabellini
Quoted 1 line · Dario Faggioli

Sorry if I can't be more useful than this for now. :-/

We don't need scheduler support to implement interrupt migration. The question was much simpler than that: moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned
to it. You could say that the slowness is directly proportional do the
number of interrupts assigned to the vCPU. Does the scheduler know that?
Or blindly moves vCPUs around? Also see below.

Ah, ok, it is indeed a simpler question than I thought! :-)

Answer: no, the scheduler does not use the information of how many or what interrupts are being routed to a vCPU in any way.

Just for the sake of correctness and precision, it does not "blindly moves vCPUs around", as in, it follows some criteria for deciding whether or not to move a vCPU, and if yes, where to, but among those criteria, there is no trace of anything related to routed interrupts.

Let me also add that the criteria are scheduler specific, so they're different, e.g., between Credit and Credit2.

Starting considering routed interrupt as a migration criteria in Credit would be rather difficult. Credit use a 'best effort' approach for migrating vCPUs, which is hard to augment.

Starting considering routed interrupt as a migration criteria in Credit2 would be much easier. Credit2's load balancer is specifically designed for being extendible with things like that. It would require some thinking, though, in order to figure out how important this particular aspect would be, wrt others that are considered.

E.g., if I have pCPU 0 loaded at 75% and pCPU 1 loaded at 25%, vCPU A has a lot of routed interrupts, and moving it gives me perfect load balancing (i.e., load will become 50% on pCPU 0 and 50% on pCPU 1) should I move it or not?
Well, it depends if whether or not we think that the overhead we save by not migrating outweights the benefit of a perfectly balanced system.

Something like that...

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#128 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Dario,

Quoted 37 lines · Dario Faggioli
Quoted 13 lines · Stefano Stabellini
Quoted 1 line · Dario Faggioli

Sorry if I can't be more useful than this for now. :-/

We don't need scheduler support to implement interrupt migration. The question was much simpler than that: moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned
to it. You could say that the slowness is directly proportional do the
number of interrupts assigned to the vCPU. Does the scheduler know that?
Or blindly moves vCPUs around? Also see below.

Ah, ok, it is indeed a simpler question than I thought! :-)

Answer: no, the scheduler does not use the information of how many or what interrupts are being routed to a vCPU in any way.

Just for the sake of correctness and precision, it does not "blindly moves vCPUs around", as in, it follows some criteria for deciding whether or not to move a vCPU, and if yes, where to, but among those criteria, there is no trace of anything related to routed interrupts.

Let me also add that the criteria are scheduler specific, so they're different, e.g., between Credit and Credit2.

Starting considering routed interrupt as a migration criteria in Credit would be rather difficult. Credit use a 'best effort' approach for migrating vCPUs, which is hard to augment.

Starting considering routed interrupt as a migration criteria in Credit2 would be much easier. Credit2's load balancer is specifically designed for being extendible with things like that. It would require some thinking, though, in order to figure out how important this particular aspect would be, wrt others that are considered.

Quoted 8 lines

E.g., if I have pCPU 0 loaded at 75% and pCPU 1 loaded at 25%, vCPU A has a lot of routed interrupts, and moving it gives me perfect load balancing (i.e., load will become 50% on pCPU 0 and 50% on pCPU 1) should I move it or not?
Well, it depends if whether or not we think that the overhead we save by not migrating outweights the benefit of a perfectly balanced system.

Something like that...

This idea to migrate IRQ while migrating vCPU is already wrong to me as this is really expensive, I am talking in term of milliseconds easily as in some case Xen will have to interact with a shared command queue.

Spreading the load is much better and avoid to migrate directly IRQ that are disabled when the vCPU is been migrated.

I cannot see any reason to not do that as changing the interrupt affinity may be not taken into account directly. By that, I mean it might be possible to receive another interrupt on the wrong pCPU.

I really think we should make the vCPU migration much simpler (e.g avoid this big loop over interrupt). In fine, if we really expect the scheduler to migrate the vCPU on a different pCPU. We should also expect receiving the interrupt on the wrong pCPU may not happen often.

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#129 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 7 lines · Julien Grall

I really think we should make the vCPU migration much simpler (e.g avoid
this big loop over interrupt). In fine, if we really expect the scheduler to migrate the vCPU on a different pCPU. We should also expect
receiving the interrupt on the wrong pCPU may not happen often.

This makes sense to me, but as I said, I don't really know. I mean, I understand what you're explaining but I didn't consider this before, and I don't have any performance figure.

I hope I manage to explain that, if we want to take this into account during migration, as Stefano was asking about, there's a way to do that in Credit2. That's it. I'll be happy to help dealing with whatever you withing yourselves decide it's best for ARM, if it has scheduling implications. :-)

And while we're here, if considering this specific aspect is not a good idea, but you (anyone!) have in mind other things that it could be interesting to take into account when evaluating whether or not to migrate a vCPU, I'd be interested to know that.

After all, the advantage of having our own scheduler (e.g., wrt KVM that has to use the Linux one), is exactly this, i.e., that we can focus a lot more on virtualization specific aspects. So, really, I'm all ears. :-D

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#130 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Dario Faggioli
Quoted 37 lines · Dario Faggioli
Quoted 13 lines · Stefano Stabellini
Quoted 1 line · Dario Faggioli

Sorry if I can't be more useful than this for now. :-/

We don't need scheduler support to implement interrupt migration. The question was much simpler than that: moving a vCPU with interrupts assigned to it is slower than moving a vCPU without interrupts assigned
to it. You could say that the slowness is directly proportional do the
number of interrupts assigned to the vCPU. Does the scheduler know that?
Or blindly moves vCPUs around? Also see below.

Ah, ok, it is indeed a simpler question than I thought! :-)

Answer: no, the scheduler does not use the information of how many or what interrupts are being routed to a vCPU in any way.

Just for the sake of correctness and precision, it does not "blindly moves vCPUs around", as in, it follows some criteria for deciding whether or not to move a vCPU, and if yes, where to, but among those criteria, there is no trace of anything related to routed interrupts.

Let me also add that the criteria are scheduler specific, so they're different, e.g., between Credit and Credit2.

Starting considering routed interrupt as a migration criteria in Credit would be rather difficult. Credit use a 'best effort' approach for migrating vCPUs, which is hard to augment.

Starting considering routed interrupt as a migration criteria in Credit2 would be much easier. Credit2's load balancer is specifically designed for being extendible with things like that. It would require some thinking, though, in order to figure out how important this particular aspect would be, wrt others that are considered.

Quoted 6 lines

E.g., if I have pCPU 0 loaded at 75% and pCPU 1 loaded at 25%, vCPU A has a lot of routed interrupts, and moving it gives me perfect load balancing (i.e., load will become 50% on pCPU 0 and 50% on pCPU 1) should I move it or not?
Well, it depends if whether or not we think that the overhead we save by not migrating outweights the benefit of a perfectly balanced system.

Right. I don't know where to draw the line. I don't how much weight it should have, but certainly it shouldn't be considered the same thing as moving any other vCPU.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#131 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 11 lines · Stefano Stabellini
Quoted 9 lines · Dario Faggioli

E.g., if I have pCPU 0 loaded at 75% and pCPU 1 loaded at 25%, vCPU A
has a lot of routed interrupts, and moving it gives me perfect load balancing (i.e., load will become 50% on pCPU 0 and 50% on pCPU 1) should I move it or not?
Well, it depends if whether or not we think that the overhead we save
by not migrating outweights the benefit of a perfectly balanced system.

Quoted 6 lines

Right. I don't know where to draw the line. I don't how much weight it
should have, but certainly it shouldn't be considered the same thing as
moving any other vCPU.

Right. As I said, Credit2 load balancer is nice and easy to extend already --and needs to become nicer and easier to extend in order to deal with soft-affinity, so I'll work on that soon (there's patches out for soft-affinity which does sort of something like that, but I'm not entirely satisfied of them, so I'll probably rework that part).

At that point, I'll be more than happy to consider this, and try to reason about how much it should be weighted. After all, the only thing we need to take this information into account when making load balancing decisions is a mechanism for knowing how many of these routed interrupt a vCPU has, and of course this needs to be:
 - easy to use,
 - super quick (load balancing is an hot path),
 - architecture independent,

is this the case already? :-)

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#132 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Stefano Stabellini
Quoted 10 lines · Stefano Stabellini

Another approach is to let the scheduler know that migration is slower.
In fact this is not a new problem: it can be slow to migrate interrupts,
even few non-LPIs interrupts, even on x86. I wonder if the Xen scheduler
has any knowledge of that (CC'ing George and Dario). I guess that's the
reason why most people run with dom0_vcpus_pin.

Oh, and about this last sentence.

I may indeed be lacking knowledge/understanding, but if you think this is a valid use case for dom0_vcpus_pin, I'd indeed be interested in knowing why.

In fact, that configuration has always looked rather awkward to me, and I think we should start thinking stopping providing the option at all (or changing/extending its behavior).

So, if you think you need it, please spell that out, and let's see if there are better ways to achieve the same. :-)

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#133 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

In reply to Stefano Stabellini
Quoted 2 lines · Stefano Stabellini
Quoted 1 line · Andre Przywara

Hi,

Hi Stefano,

I started to answer this email some days ago, but then spend some time on actually implementing what I suggested, hence the delay ...

Quoted 79 lines
Quoted 79 lines

sorry for chiming in late ....

I've been spending some time thinking about this, and I think we can in fact get away without ever propagating command from domains to the host.

I made a list of all commands that possible require host ITS command propagation. There are two groups:
1: enabling/disabling LPIs: INV, INVALL
2: mapping/unmapping devices/events: DISCARD, MAPD, MAPTI.

The second group can be handled by mapping all required devices up front, I will elaborate on that in a different email.

For the first group, read below ...

Quoted 62 lines · Stefano Stabellini
Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

So why do we need a _counter_? This is about edge triggered interrupts, I think we can just accumulate all of them into one.

The counter is not to re-inject the same amount of interrupts into the guest, but to detect interrupt storms.

I was wondering if an interrupt "storm" could already be defined by "receiving an LPI while there is already one pending (in the guest's virtual pending table) and it being disabled by the guest". I admit that declaring two interrupts as a storm is a bit of a stretch, but in fact the guest had probably a reason for disabling it even though it fires, so Xen should just follow suit.
The only difference is that we don't do it _immediately_ when the guest tells us (via INV), but only if needed (LPI actually fires).

Quoted 12 lines
Quoted 8 lines

So here is what I think:
- We use the guest provided pending table to hold a pending bit for each VLPI. We can unmap the memory from the guest, since software is not supposed to access this table as per the spec.
- We use the guest provided property table, without trapping it. There is nothing to be "validated" in that table, since it's a really tight encoding and every value written in there is legal. We only look at bit 0 for this exercise here anyway.

I am following...

Quoted 7 lines
Quoted 5 lines

- Upon reception of a physical LPI, we look it up to find the VCPU and virtual LPI number. This is what we need to do anyway and it's a quick two-level table lookup at the moment.
- We use the VCPU's domain and the VLPI number to index the guest's property table and read the enabled bit. Again a quick table lookup.

They should be both O(2), correct?

The second is even O(1). And even the first one could be a single table, if desperately needed.

Quoted 23 lines
Quoted 10 lines

- If the VLPI is enabled, we EOI it on the host and inject it.
- If the VLPI is disabled, we set the pending bit in the VCPU's pending table and EOI on the host - to allow other IRQs.
- On a guest INV command, we check whether that vLPI is now enabled:
- If it is disabled now, we don't need to do anything.
- If it is enabled now, we check the pending bit for that VLPI:
- If it is 0, we don't do anything.
- If it is 1, we inject the VLPI and clear the pending bit.
- On a guest INVALL command, we just need to iterate over the virtual LPIs.

Right, much better.

Quoted 8 lines

If you look at the conditions above, the only immediate action is when a VLPI gets enabled _and_ its pending bit is set. So we can do 64-bit read accesses over the whole pending table to find non-zero words and thus set bits, which should be rare in practice. We can store the highest mapped VLPI to avoid iterating over the whole of the table. Ideally the guest has no direct control over the pending bits, since this is what the device generates. Also we limit the number of VLPIs in total per guest anyway.

Quoted 3 lines

I wonder if we could even use a fully packed bitmask with only the pending bits, so 1 bit per vLPI, rather than 1 byte per vLPI. That would be a nice improvement.

The _pending_ table is exactly that: one bit per VLPI. So by doing a 64-bit read we cover 64 VLPIs. And normally if an LPI fires it will probably be enabled (otherwise the guest would have disabled it in the device), so we inject it and don't need this table. It's really just for storing the pending status should an LPI arrive while the guest had _disabled_ it. I assume this is rather rare, so the table will mostly be empty: that's why I expect most reads to be 0 and the iteration of the table to be very quick. As an additional optimization we could store the highest and lowest virtually pending LPI, to avoid scanning the whole table.

We can't do so much about the property table, though, because its layout is described in the spec - in contrast to the ITS tables, which are IMPDEF. But as we only need to do something if the LPI is _both_ enabled _and_ pending, scanning the pending table gives us a quite good filter already. For the few LPIs that hit here, we can just access the right byte in the property table.

Quoted 7 lines
Quoted 6 lines

If that still sounds like a DOS vector, we could additionally rate-limit INVALLs, and/or track additions to the pending table after the last
INVALL: if there haven't been any new pending bits since the last scan, INVALL is a NOP.

Does that makes sense so far?

Quoted 12 lines

It makes sense. It should be OK.

Quoted 3 lines

So that just leaves us with this IRQ storm issue, which I am thinking about now. But I guess this is not a show stopper given we can disable the physical LPI if we sense this situation.

That is true and it's exactly what we should do.

Quoted 2 lines
Quoted 2 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.

So I was thinking why you would need to wait for the guest to actually EOI it?
Can't we end the interrupt storm condition at the moment the guest enables the interrupt? LPIs are edge triggered and so a storm in the past is easily merged into a single guest LPI once the guest enables it again. From there on we inject every triggered LPI into the guest.

This special handling for the interrupt storm just stems from the fact that have to keep LPIs enabled on the h/w interrupt controller level, despite the guest having disabled it on it's own _virtual_ GIC. So once the guest enables it again, we are in line with the current GICv2/GICv3, aren't we? Do we have interrupt storm detection/prevention in the moment? And please keep in mind that LPIs are _always_ edge triggered: So once we EOI an LPI on the host, this "very same" LPI is gone from a h/w GIC point of view, the next incoming interrupt must have been triggered by a new interrupt condition in the device (new network packet, for instance). In contrast to GICv2 this applies to _every_ LPI.

So I am not sure we should really care _too_ much about this (apart from the "guest has disabled it" part): Once we assign a device to a guest, we lose some control over the machine anyway and at least trust the device to not completely block the system. I don't see how the ITS differs in that respect from the GICv3/GICv2.

A quick update on my side: I implemented the scheme I described in my earlier mail now and it boots to the Dom0 prompt on a fastmodel with an ITS:
- On receiving the PHYSDEVOP_manage_pci_add hypercall in xen/arch/arm/physdev.c, we MAPD the device on the host, MAPTI a bunch of interrupts and enable them. We keep them unassigned in our host pLPI->VLPI table, so we discard them should they fire.
This hypercall is issued by Dom0 Linux before bringing up any PCI devices, so it works even for Dom0 without any Linux changes. For DomUs with PCI passthrough Dom0 is expected to issue this hypercall on behalf of the to-be-created domain.
- When a guest (be it Dom0 or Domu) actually maps an LPI (MAPTI), we just enter the virtual LPI number and the target VCPU in our pLPI-vLPI table and be done. Should it fire now, we know where to inject it, but refer to the enabled bit in the guest's property table before doing so.
- When a guest (be it Dom0 or DomU) enables or disabled an interrupt, we don't do much, as we refer to the enable bit every time we want to inject already. The only thing I actually do is to inject an LPI if there is a virtual LPI pending and the LPI is now enabled.

I will spend some time next week on updating the design document, describing the new approach. I hope it becomes a bit clearer then.

Cheers,
Andre.

Quoted 10 lines
Quoted 7 lines
Quoted 5 lines

Given that the state of the LRs is sync'ed before calling gic_interrupt, we can be sure to know exactly in what state the vLPI is at any given time. But for this to work correctly, it is important to configure the pLPI to be delivered to the same pCPU running the vCPU which handles the vLPI (as it is already the case today for SPIs).

Why would that be necessary?

Because the state of the LRs of other pCPUs won't be up to date: we wouldn't know for sure whether the guest EOI'ed the vLPI or not.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#134 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 85 lines · Andre Przywara
Quoted 76 lines
Quoted 76 lines

I've been spending some time thinking about this, and I think we can in fact get away without ever propagating command from domains to the host.

I made a list of all commands that possible require host ITS command propagation. There are two groups:
1: enabling/disabling LPIs: INV, INVALL
2: mapping/unmapping devices/events: DISCARD, MAPD, MAPTI.

The second group can be handled by mapping all required devices up front, I will elaborate on that in a different email.

For the first group, read below ...

Quoted 62 lines · Stefano Stabellini
Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

So why do we need a _counter_? This is about edge triggered interrupts, I think we can just accumulate all of them into one.

The counter is not to re-inject the same amount of interrupts into the guest, but to detect interrupt storms.

Quoted 8 lines

I was wondering if an interrupt "storm" could already be defined by "receiving an LPI while there is already one pending (in the guest's virtual pending table) and it being disabled by the guest". I admit that declaring two interrupts as a storm is a bit of a stretch, but in fact the guest had probably a reason for disabling it even though it fires, so Xen should just follow suit.
The only difference is that we don't do it _immediately_ when the guest tells us (via INV), but only if needed (LPI actually fires).

Either way should work OK, I think.

Quoted 23 lines
Quoted 23 lines
Quoted 10 lines

- If the VLPI is enabled, we EOI it on the host and inject it.
- If the VLPI is disabled, we set the pending bit in the VCPU's pending table and EOI on the host - to allow other IRQs.
- On a guest INV command, we check whether that vLPI is now enabled:
- If it is disabled now, we don't need to do anything.
- If it is enabled now, we check the pending bit for that VLPI:
- If it is 0, we don't do anything.
- If it is 1, we inject the VLPI and clear the pending bit.
- On a guest INVALL command, we just need to iterate over the virtual LPIs.

Right, much better.

Quoted 8 lines

If you look at the conditions above, the only immediate action is when a VLPI gets enabled _and_ its pending bit is set. So we can do 64-bit read accesses over the whole pending table to find non-zero words and thus set bits, which should be rare in practice. We can store the highest mapped VLPI to avoid iterating over the whole of the table. Ideally the guest has no direct control over the pending bits, since this is what the device generates. Also we limit the number of VLPIs in total per guest anyway.

Quoted 5 lines
Quoted 3 lines

I wonder if we could even use a fully packed bitmask with only the pending bits, so 1 bit per vLPI, rather than 1 byte per vLPI. That would be a nice improvement.

The _pending_ table is exactly that: one bit per VLPI.

Actually the spec says about the pending table, ch 6.1.2:

"Each Redistributor maintains entries in a separate LPI Pending table that indicates the pending state of each LPI when GICR_CTLR.EnableLPIs == 1 in the Redistributor:
0 The LPI is not pending.
1 The LPI is pending.

For a given LPI:
• The corresponding byte in the LPI Pending table is (base address + (N / 8)).
• The bit position in the byte is (N MOD 8)."

It seems to me that each LPI is supposed to have a byte, not a bit. Am I looking at the wrong table?

In any case you suggested to trap the pending table, so we can actually write anything we want to those guest provided pages.

Quoted 11 lines

So by doing a
64-bit read we cover 64 VLPIs. And normally if an LPI fires it will probably be enabled (otherwise the guest would have disabled it in the device), so we inject it and don't need this table. It's really just for storing the pending status should an LPI arrive while the guest had _disabled_ it. I assume this is rather rare, so the table will mostly be empty: that's why I expect most reads to be 0 and the iteration of the table to be very quick. As an additional optimization we could store the highest and lowest virtually pending LPI, to avoid scanning the whole table.

Quoted 6 lines

We can't do so much about the property table, though, because its layout is described in the spec - in contrast to the ITS tables, which are IMPDEF. But as we only need to do something if the LPI is _both_ enabled _and_ pending, scanning the pending table gives us a quite good filter already. For the few LPIs that hit here, we can just access the right byte in the property table.

OK

Quoted 20 lines
Quoted 7 lines
Quoted 6 lines

If that still sounds like a DOS vector, we could additionally rate-limit INVALLs, and/or track additions to the pending table after the last
INVALL: if there haven't been any new pending bits since the last scan, INVALL is a NOP.

Does that makes sense so far?

Quoted 12 lines

It makes sense. It should be OK.

Quoted 3 lines

So that just leaves us with this IRQ storm issue, which I am thinking about now. But I guess this is not a show stopper given we can disable the physical LPI if we sense this situation.

That is true and it's exactly what we should do.

Quoted 2 lines
Quoted 2 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.

Quoted 6 lines

So I was thinking why you would need to wait for the guest to actually EOI it?
Can't we end the interrupt storm condition at the moment the guest enables the interrupt? LPIs are edge triggered and so a storm in the past is easily merged into a single guest LPI once the guest enables it again. From there on we inject every triggered LPI into the guest.

What you describe works if the guest disables the interrupt. But what if it doesn't? Xen should also be able to cope with non-cooperative guests which might even have triggered the interrupt storm on purpose.

I think that you are asking this question because we are actually talking about two different issues. See below.

Quoted 5 lines

This special handling for the interrupt storm just stems from the fact that have to keep LPIs enabled on the h/w interrupt controller level, despite the guest having disabled it on it's own _virtual_ GIC. So once the guest enables it again, we are in line with the current GICv2/GICv3, aren't we? Do we have interrupt storm detection/prevention in the moment?

No, that's not the cause of the storm. Julien described well before:

given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

The problem is that Xen has to do priority drop upon receiving an pLPI, but, given that LPIs don't have a deactivate state, the priority drop is enough to let the hardware inject a second LPI, even if the guest didn't EOI the first one yet.

In the case of SPIs, the hardware cannot inject a second interrupt after Xen does priority drop, it has to wait for the guest to EOI it.

Quoted 5 lines

And please keep in mind that LPIs are _always_ edge triggered: So once we EOI an LPI on the host, this "very same" LPI is gone from a h/w GIC point of view, the next incoming interrupt must have been triggered by a new interrupt condition in the device (new network packet, for instance).

This is actually the problem: what if the guest configured the device on purpose to keep generating LPIs without pause? Nice and simple way to take down the host.

Quoted 5 lines

In contrast to GICv2 this applies to _every_ LPI.
So I am not sure we should really care _too_ much about this (apart from the "guest has disabled it" part): Once we assign a device to a guest, we lose some control over the machine anyway and at least trust the device to not completely block the system.

No we don't! Hardware engineers make mistakes too! We have to protect Xen from devices which purposely or mistakenly generate interrupt storms. This is actually a pretty common problem.

Quoted 1 line

I don't see how the ITS differs in that respect from the GICv3/GICv2.

It differs because in the case of GICv2, every single interrupt has to be EOI'd by the guest. Therefore the Xen scheduler can still decide to schedule it out. In the case of the ITS, Xen could be stuck in an interrupt handling loop.

Quoted 2 lines

A quick update on my side: I implemented the scheme I described in my earlier mail now and it boots to the Dom0 prompt on a fastmodel with an ITS:

Nice!

Quoted 8 lines

- On receiving the PHYSDEVOP_manage_pci_add hypercall in xen/arch/arm/physdev.c, we MAPD the device on the host, MAPTI a bunch of interrupts and enable them. We keep them unassigned in our host pLPI->VLPI table, so we discard them should they fire.
This hypercall is issued by Dom0 Linux before bringing up any PCI devices, so it works even for Dom0 without any Linux changes. For DomUs with PCI passthrough Dom0 is expected to issue this hypercall on behalf of the to-be-created domain.

Quoted 8 lines

- When a guest (be it Dom0 or Domu) actually maps an LPI (MAPTI), we just enter the virtual LPI number and the target VCPU in our pLPI-vLPI table and be done. Should it fire now, we know where to inject it, but refer to the enabled bit in the guest's property table before doing so.
- When a guest (be it Dom0 or DomU) enables or disabled an interrupt, we don't do much, as we refer to the enable bit every time we want to inject already. The only thing I actually do is to inject an LPI if there is a virtual LPI pending and the LPI is now enabled.

Sounds good, well done!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#135 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Hi Stefano,

thanks for the prompt and helpful answer!

Quoted 135 lines · Stefano Stabellini
Quoted 85 lines · Andre Przywara
Quoted 76 lines
Quoted 76 lines

I've been spending some time thinking about this, and I think we can in fact get away without ever propagating command from domains to the host.

I made a list of all commands that possible require host ITS command propagation. There are two groups:
1: enabling/disabling LPIs: INV, INVALL
2: mapping/unmapping devices/events: DISCARD, MAPD, MAPTI.

The second group can be handled by mapping all required devices up front, I will elaborate on that in a different email.

For the first group, read below ...

Quoted 62 lines · Stefano Stabellini
Quoted 34 lines · Julien Grall

Hi,

Quoted 23 lines · Stefano Stabellini
Quoted 14 lines · Stefano Stabellini
Quoted 8 lines · Julien Grall

On 10/11/16 20:42, Stefano Stabellini wrote:
That's why in the approach we had on the previous series was "host ITS command
should be limited when emulating guest ITS command". From my recall, in that
series the host and guest LPIs was fully separated (enabling a guest LPIs was
not enabling host LPIs).

I am interested in reading what Ian suggested to do when the physical ITS queue is full, but I cannot find anything specific about it in the doc.

Quoted 7 lines

Do you have a suggestion for this?

The only things that come to mind right now are:

1) check if the ITS queue is full and busy loop until it is not (spin_lock style)
2) check if the ITS queue is full and sleep until it is not (mutex style)

Quoted 7 lines

Another, probably better idea, is to map all pLPIs of a device when the device is assigned to a guest (including Dom0). This is what was written in Ian's design doc. The advantage of this approach is that Xen doesn't need to take any actions on the physical ITS command queue when the guest issues virtual ITS commands, therefore completely solving this problem at the root. (Although I am not sure about enable/disable
commands: could we avoid issuing enable/disable on pLPIs?)

Quoted 8 lines

In the previous design document (see [1]), the pLPIs are enabled when the device is assigned to the guest. This means that it is not necessary to send command there. This is also means we may receive a pLPI before the associated vLPI has been configured.

That said, given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Thank you for raising this important point. You are correct.

Quoted 7 lines

The priority drop is necessary if we don't want to block the reception of interrupt for the current physical CPU.

What I am more concerned about is this problem can also happen in normal running (i.e the pLPI is bound to an vLPI and the vLPI is enabled). For edge-triggered interrupt, there is no way to prevent them to fire again. Maybe it is time to introduce rate-limit interrupt for ARM. Any opinions?

Yes. It could be as simple as disabling the pLPI when Xen receives a second pLPI before the guest EOIs the first corresponding vLPI, which shouldn't happen in normal circumstances.

We need a simple per-LPI inflight counter, incremented when a pLPI is received, decremented when the corresponding vLPI is EOIed (the LR is cleared).

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

When the counter > 1, we disable the pLPI and request a maintenance interrupt for the corresponding vLPI.

So why do we need a _counter_? This is about edge triggered interrupts, I think we can just accumulate all of them into one.

The counter is not to re-inject the same amount of interrupts into the guest, but to detect interrupt storms.

Quoted 8 lines

I was wondering if an interrupt "storm" could already be defined by "receiving an LPI while there is already one pending (in the guest's virtual pending table) and it being disabled by the guest". I admit that declaring two interrupts as a storm is a bit of a stretch, but in fact the guest had probably a reason for disabling it even though it fires, so Xen should just follow suit.
The only difference is that we don't do it _immediately_ when the guest tells us (via INV), but only if needed (LPI actually fires).

Either way should work OK, I think.

Quoted 23 lines
Quoted 23 lines
Quoted 10 lines

- If the VLPI is enabled, we EOI it on the host and inject it.
- If the VLPI is disabled, we set the pending bit in the VCPU's pending table and EOI on the host - to allow other IRQs.
- On a guest INV command, we check whether that vLPI is now enabled:
- If it is disabled now, we don't need to do anything.
- If it is enabled now, we check the pending bit for that VLPI:
- If it is 0, we don't do anything.
- If it is 1, we inject the VLPI and clear the pending bit.
- On a guest INVALL command, we just need to iterate over the virtual LPIs.

Right, much better.

Quoted 8 lines

If you look at the conditions above, the only immediate action is when a VLPI gets enabled _and_ its pending bit is set. So we can do 64-bit read accesses over the whole pending table to find non-zero words and thus set bits, which should be rare in practice. We can store the highest mapped VLPI to avoid iterating over the whole of the table. Ideally the guest has no direct control over the pending bits, since this is what the device generates. Also we limit the number of VLPIs in total per guest anyway.

Quoted 5 lines
Quoted 3 lines

I wonder if we could even use a fully packed bitmask with only the pending bits, so 1 bit per vLPI, rather than 1 byte per vLPI. That would be a nice improvement.

The _pending_ table is exactly that: one bit per VLPI.

Actually the spec says about the pending table, ch 6.1.2:

"Each Redistributor maintains entries in a separate LPI Pending table that indicates the pending state of each LPI when GICR_CTLR.EnableLPIs == 1 in the Redistributor:
0 The LPI is not pending.
1 The LPI is pending.

Quoted 3 lines

For a given LPI:
• The corresponding byte in the LPI Pending table is (base address + (N / 8)).
• The bit position in the byte is (N MOD 8)."

        ^^^
Quoted 2 lines

It seems to me that each LPI is supposed to have a byte, not a bit. Am I looking at the wrong table?

Well, the explanation could indeed be a bit more explicit, but it's really meant to be a bit:
1) The two lines above describe how to address a single bit in a byte-addressed array.
2) The following paragraphs talk about "the first 1KB" when it comes to non-LPI interrupts. This matches 8192 bits.
3) In section 6.1 the spec states: "Memory-backed storage for LPI pending _bits_ in an LPI Pending table."
4) The actual instance, Marc's Linux driver, also speaks of a bit:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c#n785

Quoted 2 lines

In any case you suggested to trap the pending table, so we can actually write anything we want to those guest provided pages.

Indeed:
"During normal operation, the LPI Pending table is maintained solely by the Redistributor."

Quoted 48 lines
Quoted 11 lines

So by doing a
64-bit read we cover 64 VLPIs. And normally if an LPI fires it will probably be enabled (otherwise the guest would have disabled it in the device), so we inject it and don't need this table. It's really just for storing the pending status should an LPI arrive while the guest had _disabled_ it. I assume this is rather rare, so the table will mostly be empty: that's why I expect most reads to be 0 and the iteration of the table to be very quick. As an additional optimization we could store the highest and lowest virtually pending LPI, to avoid scanning the whole table.

Quoted 6 lines

We can't do so much about the property table, though, because its layout is described in the spec - in contrast to the ITS tables, which are IMPDEF. But as we only need to do something if the LPI is _both_ enabled _and_ pending, scanning the pending table gives us a quite good filter already. For the few LPIs that hit here, we can just access the right byte in the property table.

OK

Quoted 20 lines
Quoted 7 lines
Quoted 6 lines

If that still sounds like a DOS vector, we could additionally rate-limit INVALLs, and/or track additions to the pending table after the last
INVALL: if there haven't been any new pending bits since the last scan, INVALL is a NOP.

Does that makes sense so far?

Quoted 12 lines

It makes sense. It should be OK.

Quoted 3 lines

So that just leaves us with this IRQ storm issue, which I am thinking about now. But I guess this is not a show stopper given we can disable the physical LPI if we sense this situation.

That is true and it's exactly what we should do.

Quoted 2 lines
Quoted 2 lines

When we receive the maintenance interrupt and we clear the LR of the vLPI, Xen should re-enable the pLPI.

Quoted 6 lines

So I was thinking why you would need to wait for the guest to actually EOI it?
Can't we end the interrupt storm condition at the moment the guest enables the interrupt? LPIs are edge triggered and so a storm in the past is easily merged into a single guest LPI once the guest enables it again. From there on we inject every triggered LPI into the guest.

Quoted 6 lines

What you describe works if the guest disables the interrupt. But what if it doesn't? Xen should also be able to cope with non-cooperative guests which might even have triggered the interrupt storm on purpose.

I think that you are asking this question because we are actually talking about two different issues. See below.

Indeed I was wondering about that ...

Quoted 13 lines
Quoted 6 lines

This special handling for the interrupt storm just stems from the fact that have to keep LPIs enabled on the h/w interrupt controller level, despite the guest having disabled it on it's own _virtual_ GIC. So once the guest enables it again, we are in line with the current GICv2/GICv3, aren't we? Do we have interrupt storm detection/prevention in the moment?

No, that's not the cause of the storm. Julien described well before:

given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Quoted 7 lines

The problem is that Xen has to do priority drop upon receiving an pLPI, but, given that LPIs don't have a deactivate state, the priority drop is enough to let the hardware inject a second LPI, even if the guest didn't EOI the first one yet.

In the case of SPIs, the hardware cannot inject a second interrupt after Xen does priority drop, it has to wait for the guest to EOI it.

I understand that ...

Quoted 9 lines
Quoted 5 lines

And please keep in mind that LPIs are _always_ edge triggered: So once we EOI an LPI on the host, this "very same" LPI is gone from a h/w GIC point of view, the next incoming interrupt must have been triggered by a new interrupt condition in the device (new network packet, for instance).

This is actually the problem: what if the guest configured the device on purpose to keep generating LPIs without pause? Nice and simple way to take down the host.

I see, I just wasn't sure we were talking about the same thing: actual interrupt storm triggered by the device vs. "virtual" interrupt storm due to an interrupt line not being lowered by the IRQ handler. And I was hoping for the latter, but well ...
So thanks for the clarification.

Quoted 7 lines
Quoted 5 lines

In contrast to GICv2 this applies to _every_ LPI.
So I am not sure we should really care _too_ much about this (apart from the "guest has disabled it" part): Once we assign a device to a guest, we lose some control over the machine anyway and at least trust the device to not completely block the system.

No we don't! Hardware engineers make mistakes too!

You tell me ... ;-)

Quoted 3 lines

We have to protect
Xen from devices which purposely or mistakenly generate interrupt storms. This is actually a pretty common problem.

I see, though this doesn't make this whole problem easier ;-)

Quoted 4 lines
Quoted 1 line

I don't see how the ITS differs in that respect from the GICv3/GICv2.

It differs because in the case of GICv2, every single interrupt has to be EOI'd by the guest.

Sure, though I think technically it's "deactivated" here that matters (we EOI LPIs as well). And since LPIs have no active state, this makes the difference.

Quoted 3 lines

Therefore the Xen scheduler can still decide to schedule it out. In the case of the ITS, Xen could be stuck in an interrupt handling loop.

So I was wondering as we might need to relax our new strict "No (unprivileged) guest ever causes a host ITS command to be queued" rule a bit, because:
- Disabling an LPI is a separate issue, as we can trigger this in Xen interrupt context once we decide that this is an interrupt storm.
- But enabling it again has to both happen in timely manner (as the guest expects interrupts to come in) and to be triggered by a guest action, which causes the INV command to be send when handling a guest fault.

Now this INV command (and possibly a follow-up SYNC) for enabling an LPI would be the only critical ones, so I was wondering if we could ensure that these commands can always be queued immediately, by making sure we have at least two ITS command queue slots available all of the time. Other ITS commands (triggered by device pass-throughs, for instance), would then have to potentially wait if we foresee that they could fill up the host command queue.
Something like QoS for ITS commands.
And I think we should map the maximum command queue size on the host (1MB => 32768 commands) to make this scenario less likely.

I will need to think about this a bit further, maybe implement something as a proof of concept.

Cheers,
Andre.

Quoted 24 lines
Quoted 3 lines

A quick update on my side: I implemented the scheme I described in my earlier mail now and it boots to the Dom0 prompt on a fastmodel with an ITS:

Nice!

Quoted 8 lines

- On receiving the PHYSDEVOP_manage_pci_add hypercall in xen/arch/arm/physdev.c, we MAPD the device on the host, MAPTI a bunch of interrupts and enable them. We keep them unassigned in our host pLPI->VLPI table, so we discard them should they fire.
This hypercall is issued by Dom0 Linux before bringing up any PCI devices, so it works even for Dom0 without any Linux changes. For DomUs with PCI passthrough Dom0 is expected to issue this hypercall on behalf of the to-be-created domain.

Quoted 8 lines

- When a guest (be it Dom0 or Domu) actually maps an LPI (MAPTI), we just enter the virtual LPI number and the target VCPU in our pLPI-vLPI table and be done. Should it fire now, we know where to inject it, but refer to the enabled bit in the guest's property table before doing so.
- When a guest (be it Dom0 or DomU) enables or disabled an interrupt, we don't do much, as we refer to the enable bit every time we want to inject already. The only thing I actually do is to inject an LPI if there is a virtual LPI pending and the LPI is now enabled.

Quoted 2 lines

Sounds good, well done!

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#136 lore raw

Re: [RFC PATCH 21/24] ARM: vITS: handle INVALL command

Quoted 19 lines · Andre Przywara
Quoted 10 lines
Quoted 1 line

The _pending_ table is exactly that: one bit per VLPI.

Actually the spec says about the pending table, ch 6.1.2:

"Each Redistributor maintains entries in a separate LPI Pending table that indicates the pending state of each LPI when GICR_CTLR.EnableLPIs == 1 in the Redistributor:
0 The LPI is not pending.
1 The LPI is pending.

Quoted 3 lines

For a given LPI:
• The corresponding byte in the LPI Pending table is (base address + (N / 8)).
• The bit position in the byte is (N MOD 8)."

        ^^^
Quoted 2 lines

It seems to me that each LPI is supposed to have a byte, not a bit. Am I looking at the wrong table?

Quoted 10 lines

Well, the explanation could indeed be a bit more explicit, but it's really meant to be a bit:
1) The two lines above describe how to address a single bit in a byte-addressed array.
2) The following paragraphs talk about "the first 1KB" when it comes to non-LPI interrupts. This matches 8192 bits.
3) In section 6.1 the spec states: "Memory-backed storage for LPI pending _bits_ in an LPI Pending table."
4) The actual instance, Marc's Linux driver, also speaks of a bit:
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/irqchip/irq-gic-v3-its.c#n785

I was mislead by the doc. Better this way.

Quoted 87 lines
Quoted 12 lines
Quoted 5 lines

This special handling for the interrupt storm just stems from the fact that have to keep LPIs enabled on the h/w interrupt controller level, despite the guest having disabled it on it's own _virtual_ GIC. So once the guest enables it again, we are in line with the current GICv2/GICv3, aren't we? Do we have interrupt storm detection/prevention in the moment?

No, that's not the cause of the storm. Julien described well before:

given that LPIs are edge-triggered, there is no deactivate state (see 4.1 in ARM IHI 0069C). So as soon as the priority drop is done, the same LPIs could potentially be raised again. This could generate a storm.

Quoted 7 lines

The problem is that Xen has to do priority drop upon receiving an pLPI, but, given that LPIs don't have a deactivate state, the priority drop is enough to let the hardware inject a second LPI, even if the guest didn't EOI the first one yet.

In the case of SPIs, the hardware cannot inject a second interrupt after Xen does priority drop, it has to wait for the guest to EOI it.

I understand that ...

Quoted 9 lines
Quoted 5 lines

And please keep in mind that LPIs are _always_ edge triggered: So once we EOI an LPI on the host, this "very same" LPI is gone from a h/w GIC point of view, the next incoming interrupt must have been triggered by a new interrupt condition in the device (new network packet, for instance).

This is actually the problem: what if the guest configured the device on purpose to keep generating LPIs without pause? Nice and simple way to take down the host.

I see, I just wasn't sure we were talking about the same thing: actual interrupt storm triggered by the device vs. "virtual" interrupt storm due to an interrupt line not being lowered by the IRQ handler. And I was hoping for the latter, but well ...
So thanks for the clarification.

Quoted 7 lines
Quoted 5 lines

In contrast to GICv2 this applies to _every_ LPI.
So I am not sure we should really care _too_ much about this (apart from the "guest has disabled it" part): Once we assign a device to a guest, we lose some control over the machine anyway and at least trust the device to not completely block the system.

No we don't! Hardware engineers make mistakes too!

You tell me ... ;-)

Quoted 3 lines

We have to protect
Xen from devices which purposely or mistakenly generate interrupt storms. This is actually a pretty common problem.

I see, though this doesn't make this whole problem easier ;-)

Quoted 4 lines
Quoted 1 line

I don't see how the ITS differs in that respect from the GICv3/GICv2.

It differs because in the case of GICv2, every single interrupt has to be EOI'd by the guest.

Sure, though I think technically it's "deactivated" here that matters (we EOI LPIs as well). And since LPIs have no active state, this makes the difference.

Quoted 3 lines

Therefore the Xen scheduler can still decide to schedule it out. In the case of the ITS, Xen could be stuck in an interrupt handling loop.

So I was wondering as we might need to relax our new strict "No (unprivileged) guest ever causes a host ITS command to be queued" rule a bit, because:
- Disabling an LPI is a separate issue, as we can trigger this in Xen interrupt context once we decide that this is an interrupt storm.
- But enabling it again has to both happen in timely manner (as the guest expects interrupts to come in) and to be triggered by a guest action, which causes the INV command to be send when handling a guest fault.

Now this INV command (and possibly a follow-up SYNC) for enabling an LPI would be the only critical ones, so I was wondering if we could ensure that these commands can always be queued immediately, by making sure we have at least two ITS command queue slots available all of the time. Other ITS commands (triggered by device pass-throughs, for instance), would then have to potentially wait if we foresee that they could fill up the host command queue.
Something like QoS for ITS commands.
And I think we should map the maximum command queue size on the host (1MB => 32768 commands) to make this scenario less likely.

Quoted 2 lines

I will need to think about this a bit further, maybe implement something as a proof of concept.

On one hand, I'd say that it should be rare to re-enable an interrupt, after it was disabled due to a storm. Extremely rare. It should be OK to issue a physical INV and SYNC in that event.

However, if that happens, it could very well be because the guest is trying to take down the host, and issuing physical ITS commands in response to a malicious guest action, could be a good way to help the attacker. We need to be extra careful.

Given that a storm is supposed to be an exceptional circumstance, it is OK to enforce very strict limits on the amount of times we are willing to issue physical ITS commands as a consequence of a guest action. For example, we could decide to do it just once, or twice, then label the guest as "untrustworthy" and destroy it. After all if a storm keeps happening, it must be due to a malicious guest or faulty hardware - in both cases it is best to terminate the VM.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#137 lore raw

RFC PATCH 22/24 ARM: vITS: create and initialize virtual ITSes for Dom0

In reply to Andre Przywara

For each hardware ITS create and initialize a virtual ITS for Dom0. We use the same memory mapped address to keep the doorbell working.

---
 xen/arch/arm/vgic-its.c       | 22 ++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c        | 12 ++++++++++++
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 13 +++++++++++++
 4 files changed, 48 insertions(+)
xen/arch/arm/vgic-its.c +22 -0
index 1e429b7..5c605b5 100644
@@ -829,6 +829,28 @@ static const struct mmio_handler_ops vgic_its_mmio_handler = {
     .write = vgic_v3_its_mmio_write,
 };
 
+int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
+                             paddr_t guest_addr)
+{
+    struct virt_its *its;
+
+    its = xzalloc(struct virt_its);
+    if ( ! its )
+        return -ENOMEM;
+
+    its->d = d;
+    its->hw_its = hw_its;
+    its->baser0 = 0x7917000000000400;
+    its->baser1 = 0x3c01000000000400;
+    its->cbaser = 0x380e000000000400;
+    spin_lock_init(&its->vcmd_lock);
+    spin_lock_init(&its->its_lock);
+
+    register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic-v3.c +12 -0
index aa53a1e..d230a1f 100644
@@ -31,6 +31,7 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/vgic.h>
 #include <asm/vgic-emul.h>
 
@@ -1572,6 +1573,7 @@ static int vgic_v3_domain_init(struct domain *d)
      */
     if ( is_hardware_domain(d) )
     {
+        struct host_its *hw_its;
         unsigned int first_cpu = 0;
 
         d->arch.vgic.dbase = vgic_v3_hw.dbase;
@@ -1597,6 +1599,16 @@ static int vgic_v3_domain_init(struct domain *d)
 
             first_cpu += size / d->arch.vgic.rdist_stride;
         }
+        d->arch.vgic.nr_regions = vgic_v3_hw.nr_rdist_regions;
+
+        list_for_each_entry(hw_its, &host_its_list, entry)
+        {
+            /* Emulate the control registers frame (lower 64K). */
+            vgic_v3_its_init_virtual(d, hw_its, hw_its->addr);
+
+            d->arch.vgic.has_its = true;
+        }
+
     }
     else
     {
xen/include/asm-arm/domain.h +1 -0
index 0cd3500..1c2f7c7 100644
@@ -111,6 +111,7 @@ struct arch_domain
         uint32_t rdist_stride;              /* Re-Distributor stride */
         uint64_t rdist_propbase;
         uint8_t *proptable;
+        bool has_its;
 #endif
     } vgic;
 
xen/include/asm-arm/gic-its.h +13 -0
index ba6b2d5..b58e092 100644
@@ -123,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* Create and register a virtual ITS at the given guest address.
+ * If a host ITS is specified, a hardware domain can reach out to that host
+ * ITS to deal with devices and LPI mappings and can enable/disable LPIs.
+ */
+int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
+                             paddr_t guest_addr);
+
 /* Map a device on the host by allocating an ITT on the host (ITS).
  * "bits" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -204,6 +211,12 @@ static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
 {
     return false;
 }
+static inline int vgic_v3_its_init_virtual(struct domain *d,
+                                           struct host_its *hw_its,
+                                           paddr_t guest_addr)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#138 lore raw

Re: [RFC PATCH 22/24] ARM: vITS: create and initialize virtual ITSes for Dom0

Quoted 23 lines · Andre Przywara

For each hardware ITS create and initialize a virtual ITS for Dom0. We use the same memory mapped address to keep the doorbell working.

---
 xen/arch/arm/vgic-its.c       | 22 ++++++++++++++++++++++
 xen/arch/arm/vgic-v3.c        | 12 ++++++++++++
 xen/include/asm-arm/domain.h  |  1 +
 xen/include/asm-arm/gic-its.h | 13 +++++++++++++
 4 files changed, 48 insertions(+)
xen/arch/arm/vgic-its.c +4 -0
index 1e429b7..5c605b5 100644
@@ -829,6 +829,28 @@ static const struct mmio_handler_ops vgic_its_mmio_handler = {
     .write = vgic_v3_its_mmio_write,
 };
 
+int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
+                             paddr_t guest_addr)
+{
+    struct virt_its *its;

xen/arch/arm/vgic-its.c static const struct mmio_handler_ops vgic_its_mmio_handler = { · line 845

Quoted 10 lines
xen/arch/arm/vgic-its.c +10 -0
+
+    its = xzalloc(struct virt_its);
+    if ( ! its )
+        return -ENOMEM;
+
+    its->d = d;
+    its->hw_its = hw_its;
+    its->baser0 = 0x7917000000000400;
+    its->baser1 = 0x3c01000000000400;
+    its->cbaser = 0x380e000000000400;

Please #define these values.

Quoted 94 lines
xen/arch/arm/vgic-its.c +8 -0
+    spin_lock_init(&its->vcmd_lock);
+    spin_lock_init(&its->its_lock);
+
+    register_mmio_handler(d, &vgic_its_mmio_handler, guest_addr, SZ_64K, its);
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vgic-v3.c +12 -0
index aa53a1e..d230a1f 100644
@@ -31,6 +31,7 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 #include <asm/gic_v3_defs.h>
+#include <asm/gic-its.h>
 #include <asm/vgic.h>
 #include <asm/vgic-emul.h>
 
@@ -1572,6 +1573,7 @@ static int vgic_v3_domain_init(struct domain *d)
      */
     if ( is_hardware_domain(d) )
     {
+        struct host_its *hw_its;
         unsigned int first_cpu = 0;
 
         d->arch.vgic.dbase = vgic_v3_hw.dbase;
@@ -1597,6 +1599,16 @@ static int vgic_v3_domain_init(struct domain *d)
 
             first_cpu += size / d->arch.vgic.rdist_stride;
         }
+        d->arch.vgic.nr_regions = vgic_v3_hw.nr_rdist_regions;
+
+        list_for_each_entry(hw_its, &host_its_list, entry)
+        {
+            /* Emulate the control registers frame (lower 64K). */
+            vgic_v3_its_init_virtual(d, hw_its, hw_its->addr);
+
+            d->arch.vgic.has_its = true;
+        }
+
     }
     else
     {
xen/include/asm-arm/domain.h +1 -0
index 0cd3500..1c2f7c7 100644
@@ -111,6 +111,7 @@ struct arch_domain
         uint32_t rdist_stride;              /* Re-Distributor stride */
         uint64_t rdist_propbase;
         uint8_t *proptable;
+        bool has_its;
 #endif
     } vgic;
 
xen/include/asm-arm/gic-its.h +13 -0
index ba6b2d5..b58e092 100644
@@ -123,6 +123,13 @@ void gicv3_set_redist_addr(paddr_t address, int redist_id);
 /* Map a collection for this host CPU to each host ITS. */
 void gicv3_its_setup_collection(int cpu);
 
+/* Create and register a virtual ITS at the given guest address.
+ * If a host ITS is specified, a hardware domain can reach out to that host
+ * ITS to deal with devices and LPI mappings and can enable/disable LPIs.
+ */
+int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
+                             paddr_t guest_addr);
+
 /* Map a device on the host by allocating an ITT on the host (ITS).
  * "bits" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -204,6 +211,12 @@ static inline bool gicv3_lpi_is_enabled(struct domain *d, uint32_t lpi)
 {
     return false;
 }
+static inline int vgic_v3_its_init_virtual(struct domain *d,
+                                           struct host_its *hw_its,
+                                           paddr_t guest_addr)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#139 lore raw

RFC PATCH 23/24 ARM: vITS: create ITS subnodes for Dom0 DT

In reply to Andre Przywara

Dom0 expects all ITSes in the system to be propagated to be able to use MSIs.
Create Dom0 DT nodes for each hardware ITS, keeping the register frame address the same, as the doorbell address that the Dom0 drivers program into the BARs has to match the hardware.

---
 xen/arch/arm/gic-its.c        | 68 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/arm/gic-v3.c         |  4 ++-
 xen/include/asm-arm/gic-its.h | 13 +++++++++
 3 files changed, 84 insertions(+), 1 deletion(-)
xen/arch/arm/gic-its.c +68 -0
index 5129d6e..bb0b80a 100644
@@ -748,6 +748,74 @@ void gicv3_lpi_set_enable(struct host_its *its,
     its_send_cmd_sync(its, 0);
 }
 
+int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                            const struct domain *d,
+                            const struct dt_device_node *gic,
+                            void *fdt)
+{
+    uint32_t len;
+    int res;
+    const void *prop = NULL;
+    const struct dt_device_node *its = NULL;
+    const struct host_its *its_data;
+
+    if ( list_empty(its_list) )
+        return 0;
+
+    /* The sub-nodes require the ranges property */
+    prop = dt_get_property(gic, "ranges", &len);
+    if ( !prop )
+    {
+        printk(XENLOG_ERR "Can't find ranges property for the gic node\n");
+        return -FDT_ERR_XEN(ENOENT);
+    }
+
+    res = fdt_property(fdt, "ranges", prop, len);
+    if ( res )
+        return res;
+
+    list_for_each_entry(its_data, its_list, entry)
+    {
+        its = its_data->dt_node;
+
+        res = fdt_begin_node(fdt, its->name);
+        if ( res )
+            return res;
+
+        res = fdt_property_string(fdt, "compatible", "arm,gic-v3-its");
+        if ( res )
+            return res;
+
+        res = fdt_property(fdt, "msi-controller", NULL, 0);
+        if ( res )
+            return res;
+
+        if ( its->phandle )
+        {
+            res = fdt_property_cell(fdt, "phandle", its->phandle);
+            if ( res )
+                return res;
+        }
+
+        /* Use the same reg regions as the ITS node in host DTB. */
+        prop = dt_get_property(its, "reg", &len);
+        if ( !prop )
+        {
+            printk(XENLOG_ERR "GICv3: Can't find ITS reg property.\n");
+            res = -FDT_ERR_XEN(ENOENT);
+            return res;
+        }
+
+        res = fdt_property(fdt, "reg", prop, len);
+        if ( res )
+            return res;
+
+        fdt_end_node(fdt);
+    }
+
+    return res;
+}
+
 void gicv3_its_dt_init(const struct dt_device_node *node)
 {
     const struct dt_device_node *its = NULL;
xen/arch/arm/gic-v3.c +3 -1
index 57009c6..9fba3eb 100644
@@ -1176,8 +1176,10 @@ static int gicv3_make_hwdom_dt_node(const struct domain *d,
 
     res = fdt_property(fdt, "reg", new_cells, len);
     xfree(new_cells);
+    if ( res )
+        return res;
 
-    return res;
+    return gicv3_its_make_dt_nodes(&host_its_list, d, gic, fdt);
 }
 
 static const hw_irq_controller gicv3_host_irq_type = {
xen/include/asm-arm/gic-its.h +13 -0
index b58e092..e20b5bc 100644
@@ -130,6 +130,12 @@ void gicv3_its_setup_collection(int cpu);
 int vgic_v3_its_init_virtual(struct domain *d, struct host_its *hw_its,
                              paddr_t guest_addr);
 
+/* Given a list of ITSes, create the appropriate DT nodes for a domain. */
+int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                            const struct domain *d,
+                            const struct dt_device_node *gic,
+                            void *fdt);
+
 /* Map a device on the host by allocating an ITT on the host (ITS).
  * "bits" specifies how many events (interrupts) this device will need.
  * Setting "valid" to false deallocates the device.
@@ -217,6 +223,13 @@ static inline int vgic_v3_its_init_virtual(struct domain *d,
 {
     return 0;
 }
+static inline int gicv3_its_make_dt_nodes(struct list_head *its_list,
+                                       const struct domain *d,
+                                       const struct dt_device_node *gic,
+                                       void *fdt)
+{
+    return 0;
+}
 
 #endif /* CONFIG_HAS_ITS */
 
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#140 lore raw

RFC PATCH 24/24 ARM: vGIC: advertising LPI support

In reply to Andre Przywara

To let a guest know about the availability of virtual LPIs, set the respective bits in the virtual GIC registers and let a guest control the LPI enable bit.
Only report the LPI capability if the host has initialized at least one ITS.

---
 xen/arch/arm/vgic-v3.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
xen/arch/arm/vgic-v3.c +23 -5
index d230a1f..61c97a2 100644
@@ -168,8 +168,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* We have not implemented LPI's, read zero */
-        goto read_as_zero_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+                                info);
+        return 1;
 
     case VREG32(GICR_IIDR):
         if ( dabt.size != DABT_WORD ) goto bad_width;
@@ -181,16 +183,19 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         uint64_t typer, aff;
 
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-        /* TBD: Update processor id in [23:8] when ITS support is added */
         aff = (MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 3) << 56 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 2) << 48 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 1) << 40 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 0) << 32);
         typer = aff;
+        typer |= (v->vcpu_id & 0xffff) << 8;
 
         if ( v->arch.vgic.flags & VGIC_V3_RDIST_LAST )
             typer |= GICR_TYPER_LAST;
 
+        if ( v->domain->arch.vgic.has_its )
+            typer |= GICR_TYPER_PLPIS;
+
         *r = vgic_reg64_extract(typer, info);
 
         return 1;
@@ -468,8 +473,16 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* LPI's not implemented */
-        goto write_ignore_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        if ( !v->domain->arch.vgic.has_its )
+            return 1;
+
+        if ( r & 1 )
+            v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+        else
+            v->arch.vgic.flags &= !VGIC_V3_LPIS_ENABLED;
+
+        return 1;
 
     case VREG32(GICR_IIDR):
         /* RO */
@@ -1075,6 +1088,11 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         typer = ((ncpus - 1) << GICD_TYPE_CPUS_SHIFT |
                  DIV_ROUND_UP(v->domain->arch.vgic.nr_spis, 32));
 
+        if ( v->domain->arch.vgic.has_its )
+        {
+            typer |= GICD_TYPE_LPIS;
+            irq_bits = 16;
+        }
         typer |= (irq_bits - 1) << GICD_TYPE_ID_BITS_SHIFT;
 
         *r = vgic_reg32_extract(typer, info);
Signature · 7 lines
2.9.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#141 lore raw

Re: [RFC PATCH 24/24] ARM: vGIC: advertising LPI support

Quoted 16 lines · Andre Przywara

To let a guest know about the availability of virtual LPIs, set the respective bits in the virtual GIC registers and let a guest control the LPI enable bit.
Only report the LPI capability if the host has initialized at least one ITS.

---
 xen/arch/arm/vgic-v3.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
xen/arch/arm/vgic-v3.c +0 -0
index d230a1f..61c97a2 100644

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info, · line 173

Quoted 8 lines
xen/arch/arm/vgic-v3.c +3 -2
@@ -168,8 +168,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* We have not implemented LPI's, read zero */
-        goto read_as_zero_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+                                info);

I don't think it is useful to call vgic_reg32_extract in this case. vgic.flags is not a register.

Quoted 59 lines
xen/arch/arm/vgic-v3.c +20 -3
+        return 1;
 
     case VREG32(GICR_IIDR):
         if ( dabt.size != DABT_WORD ) goto bad_width;
@@ -181,16 +183,19 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         uint64_t typer, aff;
 
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
-        /* TBD: Update processor id in [23:8] when ITS support is added */
         aff = (MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 3) << 56 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 2) << 48 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 1) << 40 |
                MPIDR_AFFINITY_LEVEL(v->arch.vmpidr, 0) << 32);
         typer = aff;
+        typer |= (v->vcpu_id & 0xffff) << 8;
 
         if ( v->arch.vgic.flags & VGIC_V3_RDIST_LAST )
             typer |= GICR_TYPER_LAST;
 
+        if ( v->domain->arch.vgic.has_its )
+            typer |= GICR_TYPER_PLPIS;
+
         *r = vgic_reg64_extract(typer, info);
 
         return 1;
@@ -468,8 +473,16 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* LPI's not implemented */
-        goto write_ignore_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        if ( !v->domain->arch.vgic.has_its )
+            return 1;
+
+        if ( r & 1 )
+            v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+        else
+            v->arch.vgic.flags &= !VGIC_V3_LPIS_ENABLED;
+
+        return 1;
 
     case VREG32(GICR_IIDR):
         /* RO */
@@ -1075,6 +1088,11 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         typer = ((ncpus - 1) << GICD_TYPE_CPUS_SHIFT |
                  DIV_ROUND_UP(v->domain->arch.vgic.nr_spis, 32));
 
+        if ( v->domain->arch.vgic.has_its )
+        {
+            typer |= GICD_TYPE_LPIS;
+            irq_bits = 16;
+        }
         typer |= (irq_bits - 1) << GICD_TYPE_ID_BITS_SHIFT;
 
         *r = vgic_reg32_extract(typer, info);
Signature · 1 line
2.9.0

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#142 lore raw

Re: [RFC PATCH 24/24] ARM: vGIC: advertising LPI support

Quoted 12 lines · Stefano Stabellini
Quoted 11 lines · Andre Przywara

To let a guest know about the availability of virtual LPIs, set the respective bits in the virtual GIC registers and let a guest control the LPI enable bit.
Only report the LPI capability if the host has initialized at least one ITS.

---
 xen/arch/arm/vgic-v3.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
Quoted 16 lines

xen/arch/arm/vgic-v3.c static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info, · line 173

Quoted 13 lines
xen/arch/arm/vgic-v3.c +3 -2
index d230a1f..61c97a2 100644
@@ -168,8 +168,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
     switch ( gicr_reg )
     {
     case VREG32(GICR_CTLR):
-        /* We have not implemented LPI's, read zero */
-        goto read_as_zero_32;
+        if ( dabt.size != DABT_WORD ) goto bad_width;
+        *r = vgic_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+                                info);

I don't think it is useful to call vgic_reg32_extract in this case. vgic.flags is not a register.

All the emulation is using vgic_reg*_extract and construct a register when necessary. So I would keep the vgic_reg32_extract.

However, it would be more readable to have:

uint32_t ctlr;

ctlr = !!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED); *r = vgic_reg32_extract(ctlr, info);

Regards,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#143 lore raw
In reply to Andre Przywara

Hi,

Quoted 8 lines · Andre Przywara

Andre Przywara (24):
ARM: GICv3 ITS: parse and store ITS subnodes from hardware DT
ARM: GICv3: allocate LPI pending and property table
ARM: GICv3 ITS: allocate device and collection table
ARM: GICv3 ITS: map ITS command buffer
ARM: GICv3 ITS: introduce ITS command handling
ARM: GICv3 ITS: introduce host LPI array
ARM: GICv3 ITS: introduce device mapping

Andre and I talked IRL about GICv3 ITS host driver, I will summarize here for the other.

Whilst reviewing the host driver, I was wondering if we could share the driver with Linux. I remember that Vijay's managed to do it in his series [1] and I quite liked the idea. It makes easier to track bugs and errata as we would rely on Linux.

We already did that in quite few place in Xen (e.g SMMUv2).

Any opinions?

Regards,

[1]
https://lists.xenproject.org/archives/html/xen-devel/2016-02/msg00047.html

Quoted 3 lines

ARM: GICv3: introduce separate pending_irq structs for LPIs
ARM: GICv3: forward pending LPIs to guests
ARM: GICv3: enable ITS and LPIs on the host

--
Julien Grall
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel