Skip to contentSkip to conversation

xen/arm: vgic: Support 32-bit access for 64-bit register

PATCH Superseded Julien Grall 2 participants 20 messages Oct 5, 2015 – Oct 6, 2015

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: vgic: Support 32-bit access for 64-bit register

Hi all,

This series aims to fix the 32-bit access on 64-bit register. Some guest OS such as FreeBSD and Linux (only in the ITS) use 32-bit access and will crash at boot time.

I took the opportunity to go further and optimize the way Xen is storing registers such as GICD_IPRIORITYR, GICD_ITARGETR and GICD_IROUTER.

For all the changes see in each patch.

A branch has been pushed based on the lastest staging:

git://xenbits.xen.org/people/julieng/xen-unstable.git branch gicv3-32bit-v2

Sincerely yours,

Julien Grall (9):
xen/arm: io: remove mmio_check_t typedef
xen/arm: io: Extend write/read handler to pass the register in
parameter
xen/arm: io: Support sign-extension for every read access xen/arm: vgic: ctlr stores a 32-bit hardware register so use uint32_t xen/arm: vgic: Optimize the way to store GICD_IPRIORITYR in the rank xen/arm: vgic: Introduce a new field to store the rank index and use
it
xen/arm: vgic: Optimize the way to store the target vCPU in the rank xen/arm: vgic: Introduce helpers to extract/update/clear/set vGIC
register ...
xen/arm: vgic-v3: Support 32-bit access for 64-bit registers

 xen/arch/arm/io.c            |  34 ++++-
 xen/arch/arm/vgic-v2.c       | 308 +++++++++++++++++++------------------
 xen/arch/arm/vgic-v3.c       | 353 +++++++++++++++++++++++--------------------
 xen/arch/arm/vgic.c          |  70 +++++++--
 xen/arch/arm/vuart.c         |  20 ++-
 xen/include/asm-arm/domain.h |   2 +-
 xen/include/asm-arm/mmio.h   |   7 +-
 xen/include/asm-arm/vgic.h   | 151 ++++++++++++++----
 8 files changed, 582 insertions(+), 363 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/9 xen/arm: io: remove mmio_check_t typedef

This typedef is a left-over of the previous MMIO emulation implementation.

---
Changes in v2:
- Add Ian's Acked-by

Changes in v1:
- Patch added

---
 xen/include/asm-arm/mmio.h | 1 -
 1 file changed, 1 deletion(-)
xen/include/asm-arm/mmio.h +0 -1
index 1cd7a7a..d9b5da4 100644
@@ -34,7 +34,6 @@ typedef struct
 
 typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info, void *priv);
 typedef int (*mmio_write_t)(struct vcpu *v, mmio_info_t *info, void *priv);
-typedef int (*mmio_check_t)(struct vcpu *v, paddr_t addr);
 
 struct mmio_handler_ops {
     mmio_read_t read;
Signature · 1 line
2.1.4
#2 lore raw

PATCH v2 2/9 xen/arm: io: Extend write/read handler to pass the register in parameter

In reply to Julien Grall

Rather than letting each handler to retrieve the register used by the I/O access, add a new parameter to pass the register in parameter.

This will help to implement generic register manipulation on I/O access such as sign-extension and endianess.

Read handlers need to modify the value of the register, so a pointer to it is given in argument. Write handlers shouldn't modify the register, therefore only a plain value is given.

---

Ian, I haven't kept my Acked-by because of code clash while rebasing on top of "xen/arm: io: Extend write/read handler to pass private data" [1].

Changes in v2:
- Rebase on top of "xen/arm: io: Extend write/read handler to pass private data"

Changes in v1:
- Patch added

[1] http://lists.xen.org/archives/html/xen-devel/2015-09/msg03753.html

---
 xen/arch/arm/io.c          |   7 +++-
 xen/arch/arm/vgic-v2.c     |  44 +++++++++-----------
 xen/arch/arm/vgic-v3.c     | 101 ++++++++++++++++++++-------------------------
 xen/arch/arm/vuart.c       |  20 ++++-----
 xen/include/asm-arm/mmio.h |   6 ++-
 5 files changed, 83 insertions(+), 95 deletions(-)
xen/arch/arm/io.c +5 -2
index b418173..ffe7c21 100644
@@ -29,6 +29,9 @@ int handle_mmio(mmio_info_t *info)
     int i;
     const struct mmio_handler *handler = NULL;
     const struct vmmio *vmmio = &v->domain->arch.vmmio;
+    struct hsr_dabt dabt = info->dabt;
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+    register_t *r = select_user_reg(regs, dabt.reg);
 
     for ( i = 0; i < vmmio->num_entries; i++ )
     {
@@ -43,9 +46,9 @@ int handle_mmio(mmio_info_t *info)
         return 0;
 
     if ( info->dabt.write )
-        return handler->ops->write(v, info, handler->priv);
+        return handler->ops->write(v, info, *r, handler->priv);
     else
-        return handler->ops->read(v, info, handler->priv);
+        return handler->ops->read(v, info, r, handler->priv);
 }
 
 void register_mmio_handler(struct domain *d,
xen/arch/arm/vgic-v2.c +20 -24

157 lines not on this page · raw message

xen/arch/arm/vgic-v3.c +45 -56

330 lines not on this page · raw message

xen/arch/arm/vuart.c +9 -11
index 2495e87..b5c9288 100644
@@ -45,8 +45,10 @@
 
 #define domain_has_vuart(d) ((d)->arch.vuart.info != NULL)
 
-static int vuart_mmio_read(struct vcpu *v, mmio_info_t *info, void *priv);
-static int vuart_mmio_write(struct vcpu *v, mmio_info_t *info, void *priv);
+static int vuart_mmio_read(struct vcpu *v, mmio_info_t *info,
+                           register_t *r, void *priv);
+static int vuart_mmio_write(struct vcpu *v, mmio_info_t *info,
+                            register_t r, void *priv);
 
 static const struct mmio_handler_ops vuart_mmio_handler = {
     .read  = vuart_mmio_read,
@@ -106,12 +108,10 @@ static void vuart_print_char(struct vcpu *v, char c)
     spin_unlock(&uart->lock);
 }
 
-static int vuart_mmio_read(struct vcpu *v, mmio_info_t *info, void *priv)
+static int vuart_mmio_read(struct vcpu *v, mmio_info_t *info,
+                           register_t *r, void *priv)
 {
     struct domain *d = v->domain;
-    struct hsr_dabt dabt = info->dabt;
-    struct cpu_user_regs *regs = guest_cpu_user_regs();
-    register_t *r = select_user_reg(regs, dabt.reg);
     paddr_t offset = info->gpa - d->arch.vuart.info->base_addr;
 
     perfc_incr(vuart_reads);
@@ -126,19 +126,17 @@ static int vuart_mmio_read(struct vcpu *v, mmio_info_t *info, void *priv)
     return 1;
 }
 
-static int vuart_mmio_write(struct vcpu *v, mmio_info_t *info, void *priv)
+static int vuart_mmio_write(struct vcpu *v, mmio_info_t *info,
+                            register_t r, void *priv)
 {
     struct domain *d = v->domain;
-    struct hsr_dabt dabt = info->dabt;
-    struct cpu_user_regs *regs = guest_cpu_user_regs();
-    register_t *r = select_user_reg(regs, dabt.reg);
     paddr_t offset = info->gpa - d->arch.vuart.info->base_addr;
 
     perfc_incr(vuart_writes);
 
     if ( offset == d->arch.vuart.info->data_off )
         /* ignore any status bits */
-        vuart_print_char(v, *r & 0xFF);
+        vuart_print_char(v, r & 0xFF);
 
     return 1;
 }
xen/include/asm-arm/mmio.h +4 -2
index d9b5da4..da1cc2e 100644
@@ -32,8 +32,10 @@ typedef struct
     paddr_t gpa;
 } mmio_info_t;
 
-typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info, void *priv);
-typedef int (*mmio_write_t)(struct vcpu *v, mmio_info_t *info, void *priv);
+typedef int (*mmio_read_t)(struct vcpu *v, mmio_info_t *info,
+                           register_t *r, void *priv);
+typedef int (*mmio_write_t)(struct vcpu *v, mmio_info_t *info,
+                            register_t r, void *priv);
 
 struct mmio_handler_ops {
     mmio_read_t read;
Signature · 1 line
2.1.4
#3 lore raw

Re: [PATCH v2 2/9] xen/arm: io: Extend write/read handler to pass the register in parameter

Quoted 10 lines · Julien Grall

Rather than letting each handler to retrieve the register used by the I/O access, add a new parameter to pass the register in parameter.

This will help to implement generic register manipulation on I/O access such as sign-extension and endianess.

Read handlers need to modify the value of the register, so a pointer to it is given in argument. Write handlers shouldn't modify the register, therefore only a plain value is given.

Quoted 8 lines

---

Ian, I haven't kept my Acked-by because of code clash while rebasing on top of "xen/arm: io: Extend write/read handler to pass private data" [1].

Hmmm, this doesn't match what I did (i.e keeping the acked-by from Ian).

The conflict with "xen/arm: io: Extend write/read handler to pass private data" should not be controversial. Let me know if you disagree.

Regards,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH v2 2/9] xen/arm: io: Extend write/read handler to pass the register in parameter

Quoted 16 lines · Julien Grall
Quoted 15 lines · Julien Grall

Rather than letting each handler to retrieve the register used by the I/O access, add a new parameter to pass the register in parameter.

This will help to implement generic register manipulation on I/O access such as sign-extension and endianess.

Read handlers need to modify the value of the register, so a pointer to it is given in argument. Write handlers shouldn't modify the register, therefore only a plain value is given.

---

Quoted 8 lines
Quoted 3 lines

Ian, I haven't kept my Acked-by because of code clash while rebasing on top of "xen/arm: io: Extend write/read handler to pass private data" [1].

Hmmm, this doesn't match what I did (i.e keeping the acked-by from Ian).

The conflict with "xen/arm: io: Extend write/read handler to pass private data" should not be controversial. Let me know if you disagree.

It's fine.

#5 lore raw

PATCH v2 3/9 xen/arm: io: Support sign-extension for every read access

In reply to Julien Grall

The guest may try to load data from the emulated MMIO region using instructions with Sign-Extension (i.e ldrs*). Any use of one those, will set the SSE bit (Syndrome Sign Extend) in the ISS (see B3-1433 in DDI 0406C.b).

Note that the bit can only be set for access size smaller than the register size (i.e byte/half-word for aarch32, byte/half-word/word for aarch32). So we don't have to worry about undefined C behavior.

Until now, the support of sign-extension was limited for byte access in vGIC emulation. Although there is no reason to not have it generically.

So move the support just after we get the data from the MMIO emulation.

---

Changes in v2:
- Rebase on top of "xen/arm: io: Extend write/read handler to pass private data" [1]
- Fix typeos
- Expand the commit message to explain the access size supported
- Add "io: " in the commit title

Changes in v1:
- Patch added

[1] http://lists.xen.org/archives/html/xen-devel/2015-09/msg03753.html

---
 xen/arch/arm/io.c          | 29 ++++++++++++++++++++++++++++-
 xen/arch/arm/vgic-v2.c     | 10 +++++-----
 xen/arch/arm/vgic-v3.c     |  4 ++--
 xen/include/asm-arm/vgic.h |  8 +++-----
 4 files changed, 38 insertions(+), 13 deletions(-)
xen/arch/arm/io.c +28 -1
index ffe7c21..75f7c82 100644
@@ -23,6 +23,33 @@
 #include <asm/current.h>
 #include <asm/mmio.h>
 
+static int handle_read(const struct mmio_handler *handler, struct vcpu *v,
+                       mmio_info_t *info, register_t *r)
+{
+    uint8_t size = (1 << info->dabt.size) * 8;
+
+    if ( !handler->ops->read(v, info, r, handler->priv) )
+        return 0;
+
+    /*
+     * Sign extend if required.
+     * Note that we expect the read handler to have zeroed the bit
+     * unused in the register.
+     */
+    if ( info->dabt.sign && (*r & (1UL << (size - 1)) ))
+    {
+        /*
+         * We are relying on register_t using the same as
+         * an unsigned long in order to keep the 32-bit assembly
+         * code smaller.
+         */
+        BUILD_BUG_ON(sizeof(register_t) != sizeof(unsigned long));
+        *r |= (~0UL) << size;
+    }
+
+    return 1;
+}
+
 int handle_mmio(mmio_info_t *info)
 {
     struct vcpu *v = current;
@@ -48,7 +75,7 @@ int handle_mmio(mmio_info_t *info)
     if ( info->dabt.write )
         return handler->ops->write(v, info, *r, handler->priv);
     else
-        return handler->ops->read(v, info, r, handler->priv);
+        return handle_read(handler, v, info, r);
 }
 
 void register_mmio_handler(struct domain *d,
xen/arch/arm/vgic-v2.c +5 -5
index dc2c2d2..dcbba0f 100644
@@ -129,7 +129,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         *r = rank->v2.itargets[REG_RANK_INDEX(8, gicd_reg - GICD_ITARGETSR,
                                               DABT_WORD)];
         if ( dabt.size == DABT_BYTE )
-            *r = vgic_byte_read(*r, dabt.sign, gicd_reg);
+            *r = vgic_byte_read(*r, gicd_reg);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -142,7 +142,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         *r = rank->ipriority[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
                                             DABT_WORD)];
         if ( dabt.size == DABT_BYTE )
-            *r = vgic_byte_read(*r, dabt.sign, gicd_reg);
+            *r = vgic_byte_read(*r, gicd_reg);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -377,7 +377,7 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
 
             new_target = i % 8;
             old_target_mask = vgic_byte_read(rank->v2.itargets[REG_RANK_INDEX(8,
-                                             gicd_reg - GICD_ITARGETSR, DABT_WORD)], 0, i/8);
+                                             gicd_reg - GICD_ITARGETSR, DABT_WORD)], i/8);
             old_target = find_first_bit(&old_target_mask, 8);
 
             if ( new_target != old_target )
@@ -503,7 +503,7 @@ static struct vcpu *vgic_v2_get_target_vcpu(struct vcpu *v, unsigned int irq)
     ASSERT(spin_is_locked(&rank->lock));
 
     target = vgic_byte_read(rank->v2.itargets[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], 0, irq & 0x3);
+                                              irq, DABT_WORD)], irq & 0x3);
 
     /* 1-N SPI should be delivered as pending to all the vcpus in the
      * mask, but here we just return the first vcpu for simplicity and
@@ -521,7 +521,7 @@ static int vgic_v2_get_irq_priority(struct vcpu *v, unsigned int irq)
 
     ASSERT(spin_is_locked(&rank->lock));
     priority = vgic_byte_read(rank->ipriority[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], 0, irq & 0x3);
+                                              irq, DABT_WORD)], irq & 0x3);
 
     return priority;
 }
xen/arch/arm/vgic-v3.c +2 -2
index 2a09f86..6de7f00 100644
@@ -336,7 +336,7 @@ static int __vgic_v3_distr_common_mmio_read(const char *name, struct vcpu *v,
         *r = rank->ipriority[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR,
                                             DABT_WORD)];
         if ( dabt.size == DABT_BYTE )
-            *r = vgic_byte_read(*r, dabt.sign, reg);
+            *r = vgic_byte_read(*r, reg);
         vgic_unlock_rank(v, rank, flags);
         return 1;
     case GICD_ICFGR ... GICD_ICFGRN:
@@ -1042,7 +1042,7 @@ static int vgic_v3_get_irq_priority(struct vcpu *v, unsigned int irq)
 
     ASSERT(spin_is_locked(&rank->lock));
     priority = vgic_byte_read(rank->ipriority[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], 0, irq & 0x3);
+                                              irq, DABT_WORD)], irq & 0x3);
 
     return priority;
 }
xen/include/asm-arm/vgic.h +3 -5
index 96839f0..354c0d4 100644
@@ -158,15 +158,13 @@ static inline int REG_RANK_NR(int b, uint32_t n)
     }
 }
 
-static inline uint32_t vgic_byte_read(uint32_t val, int sign, int offset)
+static inline uint32_t vgic_byte_read(uint32_t val, int offset)
 {
     int byte = offset & 0x3;
 
     val = val >> (8*byte);
-    if ( sign && (val & 0x80) )
-        val |= 0xffffff00;
-    else
-        val &= 0x000000ff;
+    val &= 0x000000ff;
+
     return val;
 }
 
Signature · 1 line
2.1.4
Acked-by on v2 3/9
#6 lore raw

Re: [PATCH v2 3/9] xen/arm: io: Support sign-extension for every read access

xen/arch/arm/io.c line 37

Quoted 5 lines · Julien Grall

[...]

xen/arch/arm/io.c +4 -0
+    /*
+     * Sign extend if required.
+     * Note that we expect the read handler to have zeroed the bit
+     * unused in the register.

You were (correctly) going to change this to
Note that we expect the read handler to have zeroed the bits outside the requested access size.

Apart from that (which I can fix on commit):

#7 lore raw

Re: [PATCH v2 3/9] xen/arm: io: Support sign-extension for every read access

Quoted 10 lines · Ian Campbell

xen/arch/arm/io.c line 37

Quoted 5 lines · Julien Grall

[...]

xen/arch/arm/io.c +4 -0
+    /*
+     * Sign extend if required.
+     * Note that we expect the read handler to have zeroed the bit
+     * unused in the register.

You were (correctly) going to change this to
Note that we expect the read handler to have zeroed the bits outside the requested access size.

Right and I forgot to update the patch :/.

Quoted 2 lines

Apart from that (which I can fix on commit):

Thank you!

Regards,

Signature · 1 line
Julien Grall
#8 lore raw

PATCH v2 4/9 xen/arm: vgic: ctlr stores a 32-bit hardware register so use uint32_t

In reply to Julien Grall

---
Changes in v2:
- Add Ian's acked-by

Changes in v1:
- Patch added

---
 xen/include/asm-arm/domain.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-arm/domain.h +1 -1
index b89727e..e7e40da 100644
@@ -91,7 +91,7 @@ struct arch_domain
          * rank order.
          */
         spinlock_t lock;
-        int ctlr;
+        uint32_t ctlr;
         int nr_spis; /* Number of SPIs */
         unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
         struct vgic_irq_rank *shared_irqs;
Signature · 1 line
2.1.4
#9 lore raw

PATCH v2 5/9 xen/arm: vgic: Optimize the way to store GICD_IPRIORITYR in the rank

In reply to Julien Grall

Xen is currently directly storing the value of GICD_IPRIORITYR register in the rank. This makes emulation of the register access very simple but makes the code to get the priority for a given vIRQ more complex.

While the priority of an vIRQ is retrieved every time an vIRQ is injected to the guest, the access to register occurs less often.

Each GICD_IPRIORITYR register stores 4 priorities associated for 4 vIRQs (see 4.3.11 in IHI 0048B). As Xen is using little endian, we can use an union to access directly a register or a priority for a given IRQ.

Note that the field "ipriority" has been renamed to "ipriorityr" to match the name of the register in the GIC spec.

Finally, the implementation of the callback get_irq_priority is exactly the same for both vGIC drivers. Consolidate the implementation in the common vGIC code and drop the callback.

---

The real reason is I'd like to drop vgic_byte_* helpers in favors of more generic access helper. Although it would make the code to retrieve the priority more complex. So reworking the way to get the priority was the best solution.

Changes in v2:
- Use an union to get the best of the emulation and getting the priority quickly
- Remove the callback get_irq_priority
- Update commit message

Changes in v1:
- Patch added

---
 xen/arch/arm/vgic-v2.c     | 23 +++++------------------
 xen/arch/arm/vgic-v3.c     | 23 +++++------------------
 xen/arch/arm/vgic.c        | 18 ++++++++++++++----
 xen/include/asm-arm/vgic.h | 17 ++++++++++++++---
 4 files changed, 38 insertions(+), 43 deletions(-)
xen/arch/arm/vgic-v2.c +5 -18
index dcbba0f..ad1bb15 100644
@@ -139,8 +139,8 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         if ( rank == NULL) goto read_as_zero;
 
         vgic_lock_rank(v, rank, flags);
-        *r = rank->ipriority[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
-                                            DABT_WORD)];
+        *r = rank->ipriorityr[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
+                                             DABT_WORD)];
         if ( dabt.size == DABT_BYTE )
             *r = vgic_byte_read(*r, gicd_reg);
         vgic_unlock_rank(v, rank, flags);
@@ -405,10 +405,10 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
         if ( dabt.size == DABT_WORD )
-            rank->ipriority[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
-                                           DABT_WORD)] = r;
+            rank->ipriorityr[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
+                                            DABT_WORD)] = r;
         else
-            vgic_byte_write(&rank->ipriority[REG_RANK_INDEX(8,
+            vgic_byte_write(&rank->ipriorityr[REG_RANK_INDEX(8,
                         gicd_reg - GICD_IPRIORITYR, DABT_WORD)], r, gicd_reg);
         vgic_unlock_rank(v, rank, flags);
         return 1;
@@ -514,18 +514,6 @@ static struct vcpu *vgic_v2_get_target_vcpu(struct vcpu *v, unsigned int irq)
     return v_target;
 }
 
-static int vgic_v2_get_irq_priority(struct vcpu *v, unsigned int irq)
-{
-    int priority;
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, irq);
-
-    ASSERT(spin_is_locked(&rank->lock));
-    priority = vgic_byte_read(rank->ipriority[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], irq & 0x3);
-
-    return priority;
-}
-
 static int vgic_v2_vcpu_init(struct vcpu *v)
 {
     int i;
@@ -597,7 +585,6 @@ static int vgic_v2_domain_init(struct domain *d)
 static const struct vgic_ops vgic_v2_ops = {
     .vcpu_init   = vgic_v2_vcpu_init,
     .domain_init = vgic_v2_domain_init,
-    .get_irq_priority = vgic_v2_get_irq_priority,
     .get_target_vcpu = vgic_v2_get_target_vcpu,
     .max_vcpus = 8,
 };
xen/arch/arm/vgic-v3.c +5 -18
index 6de7f00..92a3ccf 100644
@@ -333,8 +333,8 @@ static int __vgic_v3_distr_common_mmio_read(const char *name, struct vcpu *v,
         if ( rank == NULL ) goto read_as_zero;
 
         vgic_lock_rank(v, rank, flags);
-        *r = rank->ipriority[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR,
-                                            DABT_WORD)];
+        *r = rank->ipriorityr[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR,
+                                             DABT_WORD)];
         if ( dabt.size == DABT_BYTE )
             *r = vgic_byte_read(*r, reg);
         vgic_unlock_rank(v, rank, flags);
@@ -435,10 +435,10 @@ static int __vgic_v3_distr_common_mmio_write(const char *name, struct vcpu *v,
         if ( rank == NULL ) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
         if ( dabt.size == DABT_WORD )
-            rank->ipriority[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR,
-                                           DABT_WORD)] = r;
+            rank->ipriorityr[REG_RANK_INDEX(8, reg - GICD_IPRIORITYR,
+                                            DABT_WORD)] = r;
         else
-            vgic_byte_write(&rank->ipriority[REG_RANK_INDEX(8,
+            vgic_byte_write(&rank->ipriorityr[REG_RANK_INDEX(8,
                        reg - GICD_IPRIORITYR, DABT_WORD)], r, reg);
         vgic_unlock_rank(v, rank, flags);
         return 1;
@@ -1035,18 +1035,6 @@ static const struct mmio_handler_ops vgic_distr_mmio_handler = {
     .write = vgic_v3_distr_mmio_write,
 };
 
-static int vgic_v3_get_irq_priority(struct vcpu *v, unsigned int irq)
-{
-    int priority;
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, irq);
-
-    ASSERT(spin_is_locked(&rank->lock));
-    priority = vgic_byte_read(rank->ipriority[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], irq & 0x3);
-
-    return priority;
-}
-
 static int vgic_v3_vcpu_init(struct vcpu *v)
 {
     int i;
@@ -1196,7 +1184,6 @@ static int vgic_v3_domain_init(struct domain *d)
 static const struct vgic_ops v3_ops = {
     .vcpu_init   = vgic_v3_vcpu_init,
     .domain_init = vgic_v3_domain_init,
-    .get_irq_priority = vgic_v3_get_irq_priority,
     .get_target_vcpu  = vgic_v3_get_target_vcpu,
     .emulate_sysreg  = vgic_v3_emulate_sysreg,
     /*
xen/arch/arm/vgic.c +14 -4
index a6835a8..2128d29 100644
@@ -204,6 +204,19 @@ struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int irq)
     return v_target;
 }
 
+static int vgic_get_virq_priority(struct vcpu *v, unsigned int virq)
+{
+    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
+    unsigned long flags;
+    int priority;
+
+    vgic_lock_rank(v, rank, flags);
+    priority = rank->priority[virq & INTERRUPT_RANK_MASK];
+    vgic_unlock_rank(v, rank, flags);
+
+    return priority;
+}
+
 void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq)
 {
     unsigned long flags;
@@ -407,14 +420,11 @@ void vgic_clear_pending_irqs(struct vcpu *v)
 void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq)
 {
     uint8_t priority;
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, virq);
     struct pending_irq *iter, *n = irq_to_pending(v, virq);
     unsigned long flags;
     bool_t running;
 
-    vgic_lock_rank(v, rank, flags);
-    priority = v->domain->arch.vgic.handler->get_irq_priority(v, virq);
-    vgic_unlock_rank(v, rank, flags);
+    priority = vgic_get_virq_priority(v, virq);
 
     spin_lock_irqsave(&v->arch.vgic.lock, flags);
 
xen/include/asm-arm/vgic.h +14 -3
index 354c0d4..ff98913 100644
@@ -82,12 +82,25 @@ struct pending_irq
     struct list_head lr_queue;
 };
 
+#define NR_INTERRUPT_PER_RANK   32
+#define INTERRUPT_RANK_MASK (NR_INTERRUPT_PER_RANK - 1)
+
 /* Represents state corresponding to a block of 32 interrupts */
 struct vgic_irq_rank {
     spinlock_t lock; /* Covers access to all other members of this struct */
     uint32_t ienable;
     uint32_t icfg[2];
-    uint32_t ipriority[8];
+
+    /*
+     * Provide efficient access to the priority of an vIRQ while keeping
+     * the emulation simple.
+     * Note, this is working fine as long as Xen is using little endian.
+     */
+    union {
+        uint8_t priority[32];
+        uint32_t ipriorityr[8];
+    };
+
     union {
         struct {
             uint32_t itargets[8];
@@ -114,8 +127,6 @@ struct vgic_ops {
     int (*vcpu_init)(struct vcpu *v);
     /* Domain specific initialization of vGIC */
     int (*domain_init)(struct domain *d);
-    /* Get priority for a given irq stored in vgic structure */
-    int (*get_irq_priority)(struct vcpu *v, unsigned int irq);
     /* Get the target vcpu for a given virq. The rank lock is already taken
      * when calling this. */
     struct vcpu *(*get_target_vcpu)(struct vcpu *v, unsigned int irq);
Signature · 1 line
2.1.4
Acked-by on v2 5/9
#10 lore raw

Re: [PATCH v2 5/9] xen/arm: vgic: Optimize the way to store GICD_IPRIORITYR in the rank

Quoted 11 lines · Julien Grall

Xen is currently directly storing the value of GICD_IPRIORITYR register in the rank. This makes emulation of the register access very simple but makes the code to get the priority for a given vIRQ more complex.

While the priority of an vIRQ is retrieved every time an vIRQ is injected to the guest, the access to register occurs less often.

Each GICD_IPRIORITYR register stores 4 priorities associated for 4 vIRQs (see 4.3.11 in IHI 0048B). As Xen is using little endian, we can use an union to access directly a register or a priority for a given IRQ.

Quoted 8 lines

Note that the field "ipriority" has been renamed to "ipriorityr" to match the name of the register in the GIC spec.

Finally, the implementation of the callback get_irq_priority is exactly the same for both vGIC drivers. Consolidate the implementation in the common vGIC code and drop the callback.

#11 lore raw

PATCH v2 6/9 xen/arm: vgic: Introduce a new field to store the rank index and use it

In reply to Julien Grall

Having in hand the index for the rank is very handy to avoid computing it everytime.

For now, use it when enabling/disabling the vIRQs rather than a formula which is not obvious to understand.

Also drop the comments which where wrong because a shift by DABT_WORD will not give the IRQ number but the index of the register.

---
Changes in v3:
- Patch added

---
 xen/arch/arm/vgic-v2.c     | 12 ++----------
 xen/arch/arm/vgic-v3.c     |  6 ++----
 xen/arch/arm/vgic.c        | 13 ++++++++++---
 xen/include/asm-arm/vgic.h |  3 +++
 4 files changed, 17 insertions(+), 17 deletions(-)
xen/arch/arm/vgic-v2.c +2 -10
index ad1bb15..2d63e12 100644
@@ -288,11 +288,7 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
         rank->ienable |= r;
-        /* The virtual irq is derived from register offset.
-         * The register difference is word difference. So divide by 2(DABT_WORD)
-         * to get Virtual irq number */
-        vgic_enable_irqs(v, r & (~tr),
-                         (gicd_reg - GICD_ISENABLER) >> DABT_WORD);
+        vgic_enable_irqs(v, r & (~tr), rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -303,11 +299,7 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
         rank->ienable &= ~r;
-        /* The virtual irq is derived from register offset.
-         * The register difference is word difference. So divide by 2(DABT_WORD)
-         * to get  Virtual irq number */
-        vgic_disable_irqs(v, r & tr,
-                         (gicd_reg - GICD_ICENABLER) >> DABT_WORD);
+        vgic_disable_irqs(v, r & tr, rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
xen/arch/arm/vgic-v3.c +2 -4
index 92a3ccf..b5249ff 100644
@@ -386,8 +386,7 @@ static int __vgic_v3_distr_common_mmio_write(const char *name, struct vcpu *v,
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
         rank->ienable |= r;
-        /* The irq number is extracted from offset. so shift by register size */
-        vgic_enable_irqs(v, r & (~tr), (reg - GICD_ISENABLER) >> DABT_WORD);
+        vgic_enable_irqs(v, r & (~tr), rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
     case GICD_ICENABLER ... GICD_ICENABLERN:
@@ -397,8 +396,7 @@ static int __vgic_v3_distr_common_mmio_write(const char *name, struct vcpu *v,
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
         rank->ienable &= ~r;
-        /* The irq number is extracted from offset. so shift by register size */
-        vgic_disable_irqs(v, r & tr, (reg - GICD_ICENABLER) >> DABT_WORD);
+        vgic_disable_irqs(v, r & tr, rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
     case GICD_ISPENDR ... GICD_ISPENDRN:
xen/arch/arm/vgic.c +10 -3
index 2128d29..7bb4570 100644
@@ -68,6 +68,13 @@ static void vgic_init_pending_irq(struct pending_irq *p, unsigned int virq)
     p->irq = virq;
 }
 
+static void vgic_rank_init(struct vgic_irq_rank *rank, uint8_t index)
+{
+    spin_lock_init(&rank->lock);
+
+    rank->index = index;
+}
+
 int domain_vgic_init(struct domain *d, unsigned int nr_spis)
 {
     int i;
@@ -114,8 +121,8 @@ int domain_vgic_init(struct domain *d, unsigned int nr_spis)
     for (i=0; i<d->arch.vgic.nr_spis; i++)
         vgic_init_pending_irq(&d->arch.vgic.pending_irqs[i], i + 32);
 
-    for (i=0; i<DOMAIN_NR_RANKS(d); i++)
-        spin_lock_init(&d->arch.vgic.shared_irqs[i].lock);
+    for ( i = 0; i < DOMAIN_NR_RANKS(d); i++ )
+        vgic_rank_init(&d->arch.vgic.shared_irqs[i], i + 1);
 
     ret = d->arch.vgic.handler->domain_init(d);
     if ( ret )
@@ -169,7 +176,7 @@ int vcpu_vgic_init(struct vcpu *v)
     if ( v->arch.vgic.private_irqs == NULL )
       return -ENOMEM;
 
-    spin_lock_init(&v->arch.vgic.private_irqs->lock);
+    vgic_rank_init(v->arch.vgic.private_irqs, 0);
 
     v->domain->arch.vgic.handler->vcpu_init(v);
 
xen/include/asm-arm/vgic.h +3 -0
index ff98913..ba74d0f 100644
@@ -88,6 +88,9 @@ struct pending_irq
 /* Represents state corresponding to a block of 32 interrupts */
 struct vgic_irq_rank {
     spinlock_t lock; /* Covers access to all other members of this struct */
+
+    uint8_t index;
+
     uint32_t ienable;
     uint32_t icfg[2];
 
Signature · 1 line
2.1.4
Acked-by on v2 6/9
#12 lore raw

Re: [PATCH v2 6/9] xen/arm: vgic: Introduce a new field to store the rank index and use it

Quoted 2 lines · Julien Grall

Having in hand the index for the rank is very handy to avoid computing it everytime.

"every time".

Quoted 4 lines

For now, use it when enabling/disabling the vIRQs rather than a formula which is not obvious to understand.

Also drop the comments which where wrong because a shift by DABT_WORD

"were" instead of "where".

Quoted 1 line

will not give the IRQ number but the index of the register.

You forgot your S-o-b.

Apart from those:

#13 lore raw

Re: [PATCH v2 6/9] xen/arm: vgic: Introduce a new field to store the rank index and use it

Quoted 9 lines · Ian Campbell
Quoted 2 lines · Julien Grall

Having in hand the index for the rank is very handy to avoid computing it everytime.

"every time".

Quoted 3 lines

For now, use it when enabling/disabling the vIRQs rather than a formula which is not obvious to understand.

Quoted 7 lines
Quoted 1 line

Also drop the comments which where wrong because a shift by DABT_WORD

"were" instead of "where".

Quoted 1 line

will not give the IRQ number but the index of the register.

You forgot your S-o-b.

oops right.

Quoted 3 lines

Apart from those:

Regards,

Signature · 1 line
Julien Grall
#14 lore raw

PATCH v2 7/9 xen/arm: vgic: Optimize the way to store the target vCPU in the rank

In reply to Julien Grall

Xen is currently directly storing the value of GICD_ITARGETSR register (for GICv2) and GICD_IROUTER (for GICv3) in the rank. This makes the emulation of the registers access very simple but makes the code to get the target vCPU for a given vIRQ more complex.

While the target vCPU of an vIRQ is retrieved every time an vIRQ is injected to the guest, the access to the register occurs less often.

So the data structure should be optimized for the most common case rather than the inverse.

This patch introduces the usage of an array to store the target vCPU for every interrupt in the rank. This will make the code to get the target very quick. The emulation code will now have to generate the GICD_ITARGETSR and GICD_IROUTER register for read access and split it to store in a convenient way.

With the new way to store the target vCPU, the structure vgic_irq_rank is shrinked down from 320 bytes to 92 bytes. This is saving about 228 bytes of memory allocated separately per vCPU.

Note that with these changes, any read to those registers will list only the target vCPU used by Xen. This is fine because the GIC spec doesn't require to return exactly the value written and it can be seen as if we decide to implement the register read-only.

Furthermore, the implementation of the callback get_target_vcpu is now exactly the same. Consolidate the implementation in the common vGIC code and drop the callback.

Finally take the opportunity to fix coding style and replace "irq" by "virq" to make clear that we are dealing with virtual IRQ in section we are modifying.

---
The real reason is I'd like to drop vgic_byte_* helpers in favor of more generic access helpers. Although it would make the code to retrieve the priority more complex. So reworking the code to get the target vCPU was the best solution.

Changes in v2:
- Remove get_target_vcpu callback
- s/irq/virq/ to make clear we are using virtual IRQ
- Update the commit message
- Rename vgic_generate_* to vgic_fetch
- Improve code comment
- Move the introduction of vgic_rank_init in a separate patch
- Make use of rank->idx

Changes in v1:
- Patch added

---
 xen/arch/arm/vgic-v2.c     | 184 +++++++++++++++++++++++++--------------------
 xen/arch/arm/vgic-v3.c     | 119 +++++++++++++++--------------
 xen/arch/arm/vgic.c        |  45 ++++++++---
 xen/include/asm-arm/vgic.h |  18 ++---
 4 files changed, 207 insertions(+), 159 deletions(-)
xen/arch/arm/vgic-v2.c +103 -81
index 2d63e12..bc03785 100644
@@ -50,6 +50,97 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t vbase)
     vgic_v2_hw.vbase = vbase;
 }
 
+#define NR_TARGET_PER_REG 4U
+#define NR_BIT_PER_TARGET 8U
+
+/*
+ * Fetch a ITARGETSR register based on the offset from ITARGETSR0. Only
+ * one vCPU will be listed for a given vIRQ.
+ *
+ * Note the offset will be aligned to the appropriate boundary.
+ */
+static uint32_t vgic_fetch_itargetsr(struct vgic_irq_rank *rank,
+                                     unsigned int offset)
+{
+    uint32_t reg = 0;
+    unsigned int i;
+
+    ASSERT(spin_is_locked(&rank->lock));
+
+    offset &= INTERRUPT_RANK_MASK;
+    offset &= ~(NR_TARGET_PER_REG - 1);
+
+    for ( i = 0; i < NR_TARGET_PER_REG; i++, offset++ )
+        reg |= (1 << rank->vcpu[offset]) << (i * NR_BIT_PER_TARGET);
+
+    return reg;
+}
+
+/*
+ * Store a ITARGETSR register in a convenient way and migrate the vIRQ
+ * if necessary. This function only deals with ITARGETSR8 and onwards.
+ *
+ * Note the offset will be aligned to the appropriate boundary.
+ */
+static void vgic_store_itargetsr(struct domain *d, struct vgic_irq_rank *rank,
+                                 unsigned int offset, uint32_t itargetsr)
+{
+    unsigned int i;
+
+    ASSERT(spin_is_locked(&rank->lock));
+
+    /*
+     * The ITARGETSR0-7, used for SGIs/PPIs, are implemented RO in the
+     * emulation and should never call this function.
+     *
+     * They all live in the first rank.
+     */
+    BUILD_BUG_ON(NR_INTERRUPT_PER_RANK != 32);
+    ASSERT(rank->index >= 1);
+
+    offset &= INTERRUPT_RANK_MASK;
+    offset &= ~(NR_TARGET_PER_REG - 1);
+
+    for ( i = 0; i < NR_TARGET_PER_REG; i++, offset++ )
+    {
+        unsigned int new_target, old_target;
+
+        /*
+         * SPIs are using the 1-N model (see 1.4.3 in ARM IHI 0048B).
+         * While the interrupt could be set pending to all the vCPUs in
+         * target list, it's not guarantee by the spec.
+         * For simplicity, always route the vIRQ to the first interrupt
+         * in the target list
+         */
+        new_target = ffs(itargetsr & ((1 << NR_BIT_PER_TARGET) - 1));
+        old_target = rank->vcpu[offset];
+
+        itargetsr >>= NR_BIT_PER_TARGET;
+
+        /*
+         * Ignore the write request for this interrupt if the new target
+         * is invalid.
+         */
+        if ( !new_target || (new_target > d->max_vcpus) )
+            continue;
+
+        /* The vCPU ID always start from 0 */
+        new_target--;
+
+        /* Only migrate the vIRQ if the target vCPU has changed */
+        if ( new_target != old_target )
+        {
+            unsigned int virq = rank->index * NR_INTERRUPT_PER_RANK + offset;
+
+            vgic_migrate_irq(d->vcpu[old_target],
+                             d->vcpu[new_target],
+                             virq);
+        }
+
+        rank->vcpu[offset] = new_target;
+    }
+}
+
 static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
                                    register_t *r, void *priv)
 {
@@ -126,8 +217,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_ITARGETSR, DABT_WORD);
         if ( rank == NULL) goto read_as_zero;
         vgic_lock_rank(v, rank, flags);
-        *r = rank->v2.itargets[REG_RANK_INDEX(8, gicd_reg - GICD_ITARGETSR,
-                                              DABT_WORD)];
+        *r = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
         if ( dabt.size == DABT_BYTE )
             *r = vgic_byte_read(*r, gicd_reg);
         vgic_unlock_rank(v, rank, flags);
@@ -337,56 +427,21 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
 
     case GICD_ITARGETSR + 8 ... GICD_ITARGETSRN:
     {
-        /* unsigned long needed for find_next_bit */
-        unsigned long target;
-        int i;
+        uint32_t itargetsr;
+
         if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width;
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_ITARGETSR, DABT_WORD);
         if ( rank == NULL) goto write_ignore;
-        /* 8-bit vcpu mask for this domain */
-        BUG_ON(v->domain->max_vcpus > 8);
-        target = (1 << v->domain->max_vcpus) - 1;
-        if ( dabt.size == 2 )
-            target = target | (target << 8) | (target << 16) | (target << 24);
-        else
-            target = (target << (8 * (gicd_reg & 0x3)));
-        target &= r;
-        /* ignore zero writes */
-        if ( !target )
-            goto write_ignore;
-        /* For word reads ignore writes where any single byte is zero */
-        if ( dabt.size == 2 &&
-            !((target & 0xff) && (target & (0xff << 8)) &&
-             (target & (0xff << 16)) && (target & (0xff << 24))))
-            goto write_ignore;
         vgic_lock_rank(v, rank, flags);
-        i = 0;
-        while ( (i = find_next_bit(&target, 32, i)) < 32 )
-        {
-            unsigned int irq, new_target, old_target;
-            unsigned long old_target_mask;
-            struct vcpu *v_target, *v_old;
-
-            new_target = i % 8;
-            old_target_mask = vgic_byte_read(rank->v2.itargets[REG_RANK_INDEX(8,
-                                             gicd_reg - GICD_ITARGETSR, DABT_WORD)], i/8);
-            old_target = find_first_bit(&old_target_mask, 8);
-
-            if ( new_target != old_target )
-            {
-                irq = gicd_reg - GICD_ITARGETSR + (i / 8);
-                v_target = v->domain->vcpu[new_target];
-                v_old = v->domain->vcpu[old_target];
-                vgic_migrate_irq(v_old, v_target, irq);
-            }
-            i += 8 - new_target;
-        }
         if ( dabt.size == DABT_WORD )
-            rank->v2.itargets[REG_RANK_INDEX(8, gicd_reg - GICD_ITARGETSR,
-                                             DABT_WORD)] = target;
+            itargetsr = r;
         else
-            vgic_byte_write(&rank->v2.itargets[REG_RANK_INDEX(8,
-                      gicd_reg - GICD_ITARGETSR, DABT_WORD)], target, gicd_reg);
+        {
+            itargetsr = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
+            vgic_byte_write(&itargetsr, r, gicd_reg);
+        }
+        vgic_store_itargetsr(v->domain, rank, gicd_reg - GICD_ITARGETSR,
+                             itargetsr);
         vgic_unlock_rank(v, rank, flags);
         return 1;
     }
@@ -487,43 +542,16 @@ static const struct mmio_handler_ops vgic_v2_distr_mmio_handler = {
     .write = vgic_v2_distr_mmio_write,
 };
 
-static struct vcpu *vgic_v2_get_target_vcpu(struct vcpu *v, unsigned int irq)
-{
-    unsigned long target;
-    struct vcpu *v_target;
-    struct vgic_irq_rank *rank = vgic_rank_irq(v, irq);
-    ASSERT(spin_is_locked(&rank->lock));
-
-    target = vgic_byte_read(rank->v2.itargets[REG_RANK_INDEX(8,
-                                              irq, DABT_WORD)], irq & 0x3);
-
-    /* 1-N SPI should be delivered as pending to all the vcpus in the
-     * mask, but here we just return the first vcpu for simplicity and
-     * because it would be too slow to do otherwise. */
-    target = find_first_bit(&target, 8);
-    ASSERT(target >= 0 && target < v->domain->max_vcpus);
-    v_target = v->domain->vcpu[target];
-    return v_target;
-}
-
 static int vgic_v2_vcpu_init(struct vcpu *v)
 {
-    int i;
-
-    /* For SGI and PPI the target is always this CPU */
-    for ( i = 0 ; i < 8 ; i++ )
-        v->arch.vgic.private_irqs->v2.itargets[i] =
-              (1<<(v->vcpu_id+0))
-            | (1<<(v->vcpu_id+8))
-            | (1<<(v->vcpu_id+16))
-            | (1<<(v->vcpu_id+24));
+    /* Nothing specific to initialize for this driver */
 
     return 0;
 }
 
 static int vgic_v2_domain_init(struct domain *d)
 {
-    int i, ret;
+    int ret;
     paddr_t cbase;
 
     /*
@@ -563,11 +591,6 @@ static int vgic_v2_domain_init(struct domain *d)
     if ( ret )
         return ret;
 
-    /* By default deliver to CPU0 */
-    for ( i = 0; i < DOMAIN_NR_RANKS(d); i++ )
-        memset(d->arch.vgic.shared_irqs[i].v2.itargets, 0x1,
-               sizeof(d->arch.vgic.shared_irqs[i].v2.itargets));
-
     register_mmio_handler(d, &vgic_v2_distr_mmio_handler, d->arch.vgic.dbase,
                           PAGE_SIZE, NULL);
 
@@ -577,7 +600,6 @@ static int vgic_v2_domain_init(struct domain *d)
 static const struct vgic_ops vgic_v2_ops = {
     .vcpu_init   = vgic_v2_vcpu_init,
     .domain_init = vgic_v2_domain_init,
-    .get_target_vcpu = vgic_v2_get_target_vcpu,
     .max_vcpus = 8,
 };
 
xen/arch/arm/vgic-v3.c +64 -55

179 lines not on this page · raw message

xen/arch/arm/vgic.c +34 -11

105 lines not on this page · raw message

xen/include/asm-arm/vgic.h +6 -12
index ba74d0f..4df7755 100644
@@ -104,14 +104,11 @@ struct vgic_irq_rank {
         uint32_t ipriorityr[8];
     };
 
-    union {
-        struct {
-            uint32_t itargets[8];
-        }v2;
-        struct {
-            uint64_t irouter[32];
-        }v3;
-    };
+    /*
+     * It's more convenient to store a target VCPU per vIRQ
+     * than the register ITARGETSR/IROUTER itself
+     */
+    uint8_t vcpu[32];
 };
 
 struct sgi_target {
@@ -130,9 +127,6 @@ struct vgic_ops {
     int (*vcpu_init)(struct vcpu *v);
     /* Domain specific initialization of vGIC */
     int (*domain_init)(struct domain *d);
-    /* Get the target vcpu for a given virq. The rank lock is already taken
-     * when calling this. */
-    struct vcpu *(*get_target_vcpu)(struct vcpu *v, unsigned int irq);
     /* vGIC sysreg emulation */
     int (*emulate_sysreg)(struct cpu_user_regs *regs, union hsr hsr);
     /* Maximum number of vCPU supported */
@@ -205,7 +199,7 @@ enum gic_sgi_mode;
 extern int domain_vgic_init(struct domain *d, unsigned int nr_spis);
 extern void domain_vgic_free(struct domain *d);
 extern int vcpu_vgic_init(struct vcpu *v);
-extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int irq);
+extern struct vcpu *vgic_get_target_vcpu(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int virq);
 extern void vgic_vcpu_inject_spi(struct domain *d, unsigned int virq);
 extern void vgic_clear_pending_irqs(struct vcpu *v);
Signature · 1 line
2.1.4
#15 lore raw

Re: [PATCH v2 7/9] xen/arm: vgic: Optimize the way to store the target vCPU in the rank

Quoted 18 lines · Julien Grall

Xen is currently directly storing the value of GICD_ITARGETSR register (for GICv2) and GICD_IROUTER (for GICv3) in the rank. This makes the emulation of the registers access very simple but makes the code to get the target vCPU for a given vIRQ more complex.

While the target vCPU of an vIRQ is retrieved every time an vIRQ is injected to the guest, the access to the register occurs less often.

So the data structure should be optimized for the most common case rather than the inverse.

This patch introduces the usage of an array to store the target vCPU for every interrupt in the rank. This will make the code to get the target very quick. The emulation code will now have to generate the GICD_ITARGETSR
and GICD_IROUTER register for read access and split it to store in a convenient way.

Quoted 2 lines

With the new way to store the target vCPU, the structure vgic_irq_rank is shrinked down from 320 bytes to 92 bytes. This is saving about 228

"shrunk"

Quoted 6 lines

bytes of memory allocated separately per vCPU.

Note that with these changes, any read to those registers will list only the target vCPU used by Xen. This is fine because the GIC spec doesn't require to return exactly the value written and it can be seen as if we decide to implement the register read-only.

I'd prefer if instead of "this is fine..." this said something like "We think this is likely to be OK because...(stuff)...".

It is not the case that the spec actually says what is implemented here is acceptable and I think that needs to be clear here along with why we currently think it is something we can get away with, for future reference.

xen/arch/arm/vgic-v3.c static struct vcpu *vgic_v3_irouter_to_vcpu(struct domain *d, uint64_t irouter) · line 143

Quoted 3 lines
xen/arch/arm/vgic-v3.c +3 -0
+    /*
+     * From the spec (see 8.9.13 in IHI 0069A), any write with an
+     * invalid vCPU will lead to ignore the interrupt.

"will lead to the interrupt being ignored"

#16 lore raw

Re: [PATCH v2 7/9] xen/arm: vgic: Optimize the way to store the target vCPU in the rank

Quoted 26 lines · Ian Campbell
Quoted 18 lines · Julien Grall

Xen is currently directly storing the value of GICD_ITARGETSR register (for GICv2) and GICD_IROUTER (for GICv3) in the rank. This makes the emulation of the registers access very simple but makes the code to get the target vCPU for a given vIRQ more complex.

While the target vCPU of an vIRQ is retrieved every time an vIRQ is injected to the guest, the access to the register occurs less often.

So the data structure should be optimized for the most common case rather than the inverse.

This patch introduces the usage of an array to store the target vCPU for every interrupt in the rank. This will make the code to get the target very quick. The emulation code will now have to generate the GICD_ITARGETSR
and GICD_IROUTER register for read access and split it to store in a convenient way.

Quoted 2 lines

With the new way to store the target vCPU, the structure vgic_irq_rank is shrinked down from 320 bytes to 92 bytes. This is saving about 228

"shrunk"

Quoted 2 lines

bytes of memory allocated separately per vCPU.

Quoted 7 lines
Quoted 4 lines

Note that with these changes, any read to those registers will list only the target vCPU used by Xen. This is fine because the GIC spec doesn't require to return exactly the value written and it can be seen as if we decide to implement the register read-only.

I'd prefer if instead of "this is fine..." this said something like "We think this is likely to be OK because...(stuff)...".

Will do. I will resend the whole series fixing the different error you mentioned within the series and add your various acked.

Regards,

Signature · 1 line
Julien Grall
#17 lore raw

PATCH v2 8/9 xen/arm: vgic: Introduce helpers to extract/update/clear/set vGIC register ...

In reply to Julien Grall

and use them in the vGIC emulation.

The GIC registers may support different access sizes. Rather than open coding the access for every registers, provide a set of helpers to access them.

The caller will have to call vgic_regN_* where N is the size of the emulated registers.

The new helpers supports any access size and expect the caller to validate the access size supported by the emulated registers.

Finally, take the opportunity to fix the coding style in section we are modifying.

---
Changes in v2:
- Rename read/write helpers to extract/update
- Add a 's' to clearbit/setbit because we update multiple bits

Changes in v1:
- Make use of the new helpers in the vGICv2 code
- Drop vgic_byte_*
- Use unsigned long rather than uint64_t to optimize the code on ARM. (~about 40% less instruction per helpers).

---
 xen/arch/arm/vgic-v2.c     |  89 +++++++++++++++++++++-------------
 xen/arch/arm/vgic-v3.c     | 116 ++++++++++++++++++++++++++++++---------------
 xen/include/asm-arm/vgic.h | 111 +++++++++++++++++++++++++++++++++++++++----
 3 files changed, 235 insertions(+), 81 deletions(-)
xen/arch/arm/vgic-v2.c +56 -33
index bc03785..690cf2e 100644
@@ -156,24 +156,31 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
     case GICD_CTLR:
         if ( dabt.size != DABT_WORD ) goto bad_width;
         vgic_lock(v);
-        *r = v->domain->arch.vgic.ctlr;
+        *r = vgic_reg32_extract(v->domain->arch.vgic.ctlr, info);
         vgic_unlock(v);
         return 1;
     case GICD_TYPER:
+    {
+        uint32_t typer;
+
         if ( dabt.size != DABT_WORD ) goto bad_width;
         /* No secure world support for guests. */
         vgic_lock(v);
-        *r = ( ((v->domain->max_vcpus - 1) << GICD_TYPE_CPUS_SHIFT) )
+        typer = ((v->domain->max_vcpus - 1) << GICD_TYPE_CPUS_SHIFT)
             | DIV_ROUND_UP(v->domain->arch.vgic.nr_spis, 32);
         vgic_unlock(v);
+
+        *r = vgic_reg32_extract(typer, info);
+
         return 1;
+    }
     case GICD_IIDR:
         if ( dabt.size != DABT_WORD ) goto bad_width;
         /*
          * XXX Do we need a JEP106 manufacturer ID?
          * Just use the physical h/w value for now
          */
-        *r = 0x0000043b;
+        *r = vgic_reg32_extract(0x0000043b, info);
         return 1;
 
     /* Implementation defined -- read as zero */
@@ -189,7 +196,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         rank = vgic_rank_offset(v, 1, gicd_reg - GICD_ISENABLER, DABT_WORD);
         if ( rank == NULL) goto read_as_zero;
         vgic_lock_rank(v, rank, flags);
-        *r = rank->ienable;
+        *r = vgic_reg32_extract(rank->ienable, info);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -198,7 +205,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         rank = vgic_rank_offset(v, 1, gicd_reg - GICD_ICENABLER, DABT_WORD);
         if ( rank == NULL) goto read_as_zero;
         vgic_lock_rank(v, rank, flags);
-        *r = rank->ienable;
+        *r = vgic_reg32_extract(rank->ienable, info);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -213,37 +220,53 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
         goto read_as_zero;
 
     case GICD_ITARGETSR ... GICD_ITARGETSRN:
+    {
+        uint32_t itargetsr;
+
         if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width;
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_ITARGETSR, DABT_WORD);
         if ( rank == NULL) goto read_as_zero;
         vgic_lock_rank(v, rank, flags);
-        *r = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
-        if ( dabt.size == DABT_BYTE )
-            *r = vgic_byte_read(*r, gicd_reg);
+        itargetsr = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
         vgic_unlock_rank(v, rank, flags);
+        *r = vgic_reg32_extract(itargetsr, info);
+
         return 1;
+    }
 
     case GICD_IPRIORITYR ... GICD_IPRIORITYRN:
+    {
+        uint32_t ipriorityr;
+
         if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width;
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_IPRIORITYR, DABT_WORD);
-        if ( rank == NULL) goto read_as_zero;
+        if ( rank == NULL ) goto read_as_zero;
 
         vgic_lock_rank(v, rank, flags);
-        *r = rank->ipriorityr[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
-                                             DABT_WORD)];
-        if ( dabt.size == DABT_BYTE )
-            *r = vgic_byte_read(*r, gicd_reg);
+        ipriorityr = rank->ipriorityr[REG_RANK_INDEX(8,
+                                                     gicd_reg - GICD_IPRIORITYR,
+                                                     DABT_WORD)];
         vgic_unlock_rank(v, rank, flags);
+        *r = vgic_reg32_extract(ipriorityr, info);
+
         return 1;
+    }
 
     case GICD_ICFGR ... GICD_ICFGRN:
+    {
+        uint32_t icfgr;
+
         if ( dabt.size != DABT_WORD ) goto bad_width;
         rank = vgic_rank_offset(v, 2, gicd_reg - GICD_ICFGR, DABT_WORD);
         if ( rank == NULL) goto read_as_zero;
         vgic_lock_rank(v, rank, flags);
-        *r = rank->icfg[REG_RANK_INDEX(2, gicd_reg - GICD_ICFGR, DABT_WORD)];
+        icfgr = rank->icfg[REG_RANK_INDEX(2, gicd_reg - GICD_ICFGR, DABT_WORD)];
         vgic_unlock_rank(v, rank, flags);
+
+        *r = vgic_reg32_extract(icfgr, info);
+
         return 1;
+    }
 
     case GICD_NSACR ... GICD_NSACRN:
         /* We do not implement security extensions for guests, read zero */
@@ -353,7 +376,8 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         if ( dabt.size != DABT_WORD ) goto bad_width;
         /* Ignore all but the enable bit */
         vgic_lock(v);
-        v->domain->arch.vgic.ctlr = r & GICD_CTL_ENABLE;
+        vgic_reg32_update(&v->domain->arch.vgic.ctlr, r, info);
+        v->domain->arch.vgic.ctlr &= GICD_CTL_ENABLE;
         vgic_unlock(v);
 
         return 1;
@@ -377,8 +401,8 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
-        rank->ienable |= r;
-        vgic_enable_irqs(v, r & (~tr), rank->index);
+        vgic_reg32_setbits(&rank->ienable, r, info);
+        vgic_enable_irqs(v, (rank->ienable) & (~tr), rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -388,8 +412,8 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
         tr = rank->ienable;
-        rank->ienable &= ~r;
-        vgic_disable_irqs(v, r & tr, rank->index);
+        vgic_reg32_clearbits(&rank->ienable, r, info);
+        vgic_disable_irqs(v, (~rank->ienable) & tr, rank->index);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
@@ -433,13 +457,8 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_ITARGETSR, DABT_WORD);
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
-        if ( dabt.size == DABT_WORD )
-            itargetsr = r;
-        else
-        {
-            itargetsr = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
-            vgic_byte_write(&itargetsr, r, gicd_reg);
-        }
+        itargetsr = vgic_fetch_itargetsr(rank, gicd_reg - GICD_ITARGETSR);
+        vgic_reg32_update(&itargetsr, r, info);
         vgic_store_itargetsr(v->domain, rank, gicd_reg - GICD_ITARGETSR,
                              itargetsr);
         vgic_unlock_rank(v, rank, flags);
@@ -447,18 +466,20 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
     }
 
     case GICD_IPRIORITYR ... GICD_IPRIORITYRN:
+    {
+        uint32_t *ipriorityr;
+
         if ( dabt.size != DABT_BYTE && dabt.size != DABT_WORD ) goto bad_width;
         rank = vgic_rank_offset(v, 8, gicd_reg - GICD_IPRIORITYR, DABT_WORD);
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
-        if ( dabt.size == DABT_WORD )
-            rank->ipriorityr[REG_RANK_INDEX(8, gicd_reg - GICD_IPRIORITYR,
-                                            DABT_WORD)] = r;
-        else
-            vgic_byte_write(&rank->ipriorityr[REG_RANK_INDEX(8,
-                        gicd_reg - GICD_IPRIORITYR, DABT_WORD)], r, gicd_reg);
+        ipriorityr = &rank->ipriorityr[REG_RANK_INDEX(8,
+                                                      gicd_reg - GICD_IPRIORITYR,
+                                                      DABT_WORD)];
+        vgic_reg32_update(ipriorityr, r, info);
         vgic_unlock_rank(v, rank, flags);
         return 1;
+    }
 
     case GICD_ICFGR: /* SGIs */
         goto write_ignore_32;
@@ -470,7 +491,9 @@ static int vgic_v2_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
         rank = vgic_rank_offset(v, 2, gicd_reg - GICD_ICFGR, DABT_WORD);
         if ( rank == NULL) goto write_ignore;
         vgic_lock_rank(v, rank, flags);
-        rank->icfg[REG_RANK_INDEX(2, gicd_reg - GICD_ICFGR, DABT_WORD)] = r;
+        vgic_reg32_update(&rank->icfg[REG_RANK_INDEX(2, gicd_reg - GICD_ICFGR,
+                                                     DABT_WORD)],
+                          r, info);
         vgic_unlock_rank(v, rank, flags);
         return 1;
 
xen/arch/arm/vgic-v3.c +78 -38

294 lines not on this page · raw message

xen/include/asm-arm/vgic.h +101 -10

133 lines not on this page · raw message

Signature · 1 line
2.1.4
Acked-by on v2 8/9
#18 lore raw

Re: [PATCH v2 8/9] xen/arm: vgic: Introduce helpers to extract/update/clear/set vGIC register ...

Quoted 9 lines · Julien Grall

and use them in the vGIC emulation.

The GIC registers may support different access sizes. Rather than open coding the access for every registers, provide a set of helpers to access them.

The caller will have to call vgic_regN_* where N is the size of the emulated registers.

Quoted 7 lines

The new helpers supports any access size and expect the caller to validate the access size supported by the emulated registers.

Finally, take the opportunity to fix the coding style in section we are modifying.

#19 lore raw

PATCH v2 9/9 xen/arm: vgic-v3: Support 32-bit access for 64-bit registers

In reply to Julien Grall

Based on 8.1.3 (IHI 0069A), unless stated otherwise, the 64-bit registers supports both 32-bit and 64-bits access.

All the registers we properly emulate (i.e not RAZ/WI) supports 32-bit access.

For RAZ/WI, it's also seems to be the case but I'm not 100% sure. Anyway, emulating 32-bit access for them doesn't hurt. Note that we would need some extra care when they will be implemented (for instance GICR_PROPBASER).

---
This is technically fixing boot of FreeBSD ARM64 guest with GICv3.

AFAICT, Linux is not using 32-bit access in the GICv3 code expect for the ITS (which we don't support yet).

So this patch is a good candidate for Xen 4.6.1. Although this patch is heavily depend on previous patches. It may be possible to shuffle and move the "opmitization" patches towards the end. I haven't yet done that because I feel this series makes more sense in the current order.

Also, I haven't move vgic_reg64_check_access in vgic.h because there is no usage in this series outside of vgic-v3.c and the helpers is GICv3 oriented.

Changes in v2:
- Add Ian's acked-by

Changes in v1:
- Support 32bit access on the most significant word of GICR_TYPER

---
 xen/arch/arm/vgic-v3.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
xen/arch/arm/vgic-v3.c +17 -7
index 27d49a8..c3c5cdc 100644
@@ -157,6 +157,15 @@ 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)
@@ -173,10 +182,11 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         *r = vgic_reg32_extract(GICV3_GICR_IIDR_VAL, info);
         return 1;
     case GICR_TYPER:
+    case GICR_TYPER + 4:
     {
         uint64_t typer, aff;
 
-        if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+        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 |
@@ -262,7 +272,7 @@ bad_width:
     return 0;
 
 read_as_zero_64:
-    if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+    if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
     *r = 0;
     return 1;
 
@@ -338,7 +348,7 @@ bad_width:
     return 0;
 
 write_ignore_64:
-    if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+    if ( vgic_reg64_check_access(dabt) ) goto bad_width;
     return 1;
 
 write_ignore_32:
@@ -803,7 +813,7 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info,
     {
         uint64_t irouter;
 
-        if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
         rank = vgic_rank_offset(v, 64, gicd_reg - GICD_IROUTER,
                                 DABT_DOUBLE_WORD);
         if ( rank == NULL ) goto read_as_zero;
@@ -878,7 +888,7 @@ bad_width:
     return 0;
 
 read_as_zero_64:
-    if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+    if ( vgic_reg64_check_access(dabt) ) goto bad_width;
     *r = 0;
     return 1;
 
@@ -964,7 +974,7 @@ static int vgic_v3_distr_mmio_write(struct vcpu *v, mmio_info_t *info,
     {
         uint64_t irouter;
 
-        if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+        if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
         rank = vgic_rank_offset(v, 64, gicd_reg - GICD_IROUTER,
                                 DABT_DOUBLE_WORD);
         if ( rank == NULL ) goto write_ignore;
@@ -1023,7 +1033,7 @@ write_ignore_32:
     return 1;
 
 write_ignore_64:
-    if ( dabt.size != DABT_DOUBLE_WORD ) goto bad_width;
+    if ( vgic_reg64_check_access(dabt) ) goto bad_width;
     return 1;
 
 write_ignore:
Signature · 1 line
2.1.4