Skip to contentSkip to conversation

Find automatically a PPI for the DOM0 even channel IRQ

PATCH Superseded Julien Grall 3 participants 31 messages Dec 12, 2014 – Jan 15, 2015

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Find automatically a PPI for the DOM0 even channel IRQ

Hello,

This patch series replaces the per-platform hardcoded event channel interrupt to a generic solution. It will make the port to a new platform more easier and may avoid to introduce per-platform code with the new upcoming ACPI support.

This could be done by keeping track of vIRQ (emulated and assigned) used by a domain.

Parth: I provided a branch on my personal repo [1]. It's based on the latest upstream branch. You can use vgic_allocate_virq(d, 0) to allocate the event channel PPI.

Sincerely yours,

[1] git://xenbits.xen.org/people/julieng/xen-unstable.git branch find-evtchn

Julien Grall (4):
xen/arm: vgic: Rename nr_lines into nr_spis
xen/arm: vgic: Keep track of vIRQ used by a domain xen/arm: vgic: notice if the vIRQ is not allocated when the guest
enable it
xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

 xen/arch/arm/domain.c                | 13 +++--
 xen/arch/arm/domain_build.c          | 16 ++++++
 xen/arch/arm/gic-v2.c                |  2 -
 xen/arch/arm/gic-v3.c                |  2 +-
 xen/arch/arm/platform.c              |  7 ---
 xen/arch/arm/platforms/xgene-storm.c |  5 +-
 xen/arch/arm/vgic-v2.c               |  2 +-
 xen/arch/arm/vgic-v3.c               |  2 +-
 xen/arch/arm/vgic.c                  | 95 ++++++++++++++++++++++++++++++++----
 xen/arch/arm/vtimer.c                | 15 ++++++
 xen/include/asm-arm/domain.h         |  3 +-
 xen/include/asm-arm/platform.h       |  4 --
 xen/include/asm-arm/vgic.h           | 17 ++++++-
 13 files changed, 152 insertions(+), 31 deletions(-)
Signature · 1 line
2.1.3
#1 lore raw

PATCH 1/4 xen/arm: vgic: Rename nr_lines into nr_spis

The field nr_lines in the arch_domain vgic structure contains the number of SPIs for the emulated GIC. Using the nr_lines make confusion with the GIC code, where it means the number of IRQs. This can lead to coding error.

Also introduce vgic_nr_lines to get the number of IRQ handled by the emulated GIC.

---
It was part of the platform device passthrough series: https://patches.linaro.org/34661/

Stefano: I've kept your ack from the previous version. Let me know if there is any issue.

Changes in v3:
- Add acked-by from Stefano.
- Update the patch to also modify GICv3 code which has been pushed recently

Changes in v2:
- Patch added.

---
 xen/arch/arm/gic-v2.c        |  2 --
 xen/arch/arm/gic-v3.c        |  2 +-
 xen/arch/arm/vgic-v2.c       |  2 +-
 xen/arch/arm/vgic-v3.c       |  2 +-
 xen/arch/arm/vgic.c          | 15 ++++++---------
 xen/include/asm-arm/domain.h |  2 +-
 xen/include/asm-arm/vgic.h   |  4 +++-
 7 files changed, 13 insertions(+), 16 deletions(-)
xen/arch/arm/gic-v2.c +0 -2
index faad1ff..31fb81a 100644
@@ -432,8 +432,6 @@ static int gicv2v_setup(struct domain *d)
         d->arch.vgic.cbase = GUEST_GICC_BASE;
     }
 
-    d->arch.vgic.nr_lines = 0;
-
     /*
      * Map the gic virtual cpu interface in the gic cpu interface
      * region of the guest.
xen/arch/arm/gic-v3.c +1 -1
index 076aa62..ec48fc1 100644
@@ -922,7 +922,7 @@ static int gicv_v3_init(struct domain *d)
         d->arch.vgic.rbase_size[0] = GUEST_GICV3_GICR0_SIZE;
     }
 
-    d->arch.vgic.nr_lines = 0;
+    d->arch.vgic.nr_spis = 0;
 
     return 0;
 }
xen/arch/arm/vgic-v2.c +1 -1
index 1369f78..039e19a 100644
@@ -54,7 +54,7 @@ static int vgic_v2_distr_mmio_read(struct vcpu *v, mmio_info_t *info)
         /* No secure world support for guests. */
         vgic_lock(v);
         *r = ( (v->domain->max_vcpus << 5) & GICD_TYPE_CPUS )
-            |( ((v->domain->arch.vgic.nr_lines / 32)) & GICD_TYPE_LINES );
+            |( ((v->domain->arch.vgic.nr_spis / 32)) & GICD_TYPE_LINES );
         vgic_unlock(v);
         return 1;
     case GICD_IIDR:
xen/arch/arm/vgic-v3.c +1 -1
index ff99e50..2785c10 100644
@@ -668,7 +668,7 @@ static int vgic_v3_distr_mmio_read(struct vcpu *v, mmio_info_t *info)
         if ( dabt.size != DABT_WORD ) goto bad_width;
         /* No secure world support for guests. */
         *r = (((v->domain->max_vcpus << 5) & GICD_TYPE_CPUS ) |
-              ((v->domain->arch.vgic.nr_lines / 32) & GICD_TYPE_LINES));
+              ((v->domain->arch.vgic.nr_spis / 32) & GICD_TYPE_LINES));
         return 1;
     case GICD_STATUSR:
         /*
xen/arch/arm/vgic.c +6 -9
index 97061ce..75cb7ff 100644
@@ -66,13 +66,10 @@ int domain_vgic_init(struct domain *d)
 
     d->arch.vgic.ctlr = 0;
 
-    /* Currently nr_lines in vgic and gic doesn't have the same meanings
-     * Here nr_lines = number of SPIs
-     */
     if ( is_hardware_domain(d) )
-        d->arch.vgic.nr_lines = gic_number_lines() - 32;
+        d->arch.vgic.nr_spis = gic_number_lines() - 32;
     else
-        d->arch.vgic.nr_lines = 0; /* We don't need SPIs for the guest */
+        d->arch.vgic.nr_spis = 0; /* We don't need SPIs for the guest */
 
     switch ( gic_hw_version() )
     {
@@ -96,11 +93,11 @@ int domain_vgic_init(struct domain *d)
         return -ENOMEM;
 
     d->arch.vgic.pending_irqs =
-        xzalloc_array(struct pending_irq, d->arch.vgic.nr_lines);
+        xzalloc_array(struct pending_irq, d->arch.vgic.nr_spis);
     if ( d->arch.vgic.pending_irqs == NULL )
         return -ENOMEM;
 
-    for (i=0; i<d->arch.vgic.nr_lines; i++)
+    for (i=0; i<d->arch.vgic.nr_spis; i++)
     {
         INIT_LIST_HEAD(&d->arch.vgic.pending_irqs[i].inflight);
         INIT_LIST_HEAD(&d->arch.vgic.pending_irqs[i].lr_queue);
@@ -218,7 +215,7 @@ void arch_move_irqs(struct vcpu *v)
     struct vcpu *v_target;
     int i;
 
-    for ( i = 32; i < (d->arch.vgic.nr_lines + 32); i++ )
+    for ( i = 32; i < vgic_num_irqs(d); i++ )
     {
         v_target = vgic_get_target_vcpu(v, i);
         p = irq_to_pending(v_target, i);
@@ -344,7 +341,7 @@ int vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, int
 struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)
 {
     struct pending_irq *n;
-    /* Pending irqs allocation strategy: the first vgic.nr_lines irqs
+    /* Pending irqs allocation strategy: the first vgic.nr_spis irqs
      * are used for SPIs; the rests are used for per cpu irqs */
     if ( irq < 32 )
         n = &v->arch.vgic.pending_irqs[irq];
xen/include/asm-arm/domain.h +1 -1
index 787e93c..8b7dd85 100644
@@ -89,7 +89,7 @@ struct arch_domain
          */
         spinlock_t lock;
         int ctlr;
-        int nr_lines; /* Number of SPIs */
+        int nr_spis; /* Number of SPIs */
         struct vgic_irq_rank *shared_irqs;
         /*
          * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
xen/include/asm-arm/vgic.h +3 -1
index 5160f17..74d5a4e 100644
@@ -113,7 +113,7 @@ struct vgic_ops {
 };
 
 /* Number of ranks of interrupt registers for a domain */
-#define DOMAIN_NR_RANKS(d) (((d)->arch.vgic.nr_lines+31)/32)
+#define DOMAIN_NR_RANKS(d) (((d)->arch.vgic.nr_spis+31)/32)
 
 #define vgic_lock(v)   spin_lock_irq(&(v)->domain->arch.vgic.lock)
 #define vgic_unlock(v) spin_unlock_irq(&(v)->domain->arch.vgic.lock)
@@ -175,6 +175,8 @@ enum gic_sgi_mode;
  */
 #define REG_RANK_INDEX(b, n, s) ((((n) >> s) & ((b)-1)) % 32)
 
+#define vgic_num_irqs(d)        ((d)->arch.vgic.nr_spis + 32)
+
 extern int domain_vgic_init(struct domain *d);
 extern void domain_vgic_free(struct domain *d);
 extern int vcpu_vgic_init(struct vcpu *v);
Signature · 1 line
2.1.3
#2 lore raw

Re: [PATCH for-4.6 1/4] xen/arm: vgic: Rename nr_lines into nr_spis

Quoted 14 lines · Julien Grall

The field nr_lines in the arch_domain vgic structure contains the number of SPIs for the emulated GIC. Using the nr_lines make confusion with the GIC code, where it means the number of IRQs. This can lead to coding error.

Also introduce vgic_nr_lines to get the number of IRQ handled by the emulated GIC.

From the code you seem to have called it vgic_nr_irqs, which I prefer.

xen/arch/arm/gic-v2.c +0 -0
index faad1ff..31fb81a 100644

xen/arch/arm/gic-v2.c static int gicv2v_setup(struct domain *d) · line 435

Quoted 4 lines
xen/arch/arm/gic-v2.c +0 -1
@@ -432,8 +432,6 @@ static int gicv2v_setup(struct domain *d)
         d->arch.vgic.cbase = GUEST_GICC_BASE;
     }
 
-    d->arch.vgic.nr_lines = 0;

Not replaced with an assignment to nr_spis, as you do in the v3 case?

We should be consistent, which also makes me wonder if nr_lines^Wspis should be the responsibility of the common vgic code to setup.

Quoted 9 lines
xen/arch/arm/gic-v2.c +0 -1
-
     /*
      * Map the gic virtual cpu interface in the gic cpu interface
      * region of the guest.
xen/arch/arm/gic-v3.c +0 -0
index 076aa62..ec48fc1 100644

xen/arch/arm/gic-v3.c static int gicv_v3_init(struct domain *d) · line 928

Quoted 8 lines
xen/arch/arm/gic-v3.c +1 -1
@@ -922,7 +922,7 @@ static int gicv_v3_init(struct domain *d)
         d->arch.vgic.rbase_size[0] = GUEST_GICV3_GICR0_SIZE;
     }
 
-    d->arch.vgic.nr_lines = 0;
+    d->arch.vgic.nr_spis = 0;
 
     return 0;
 }

Ian.

#3 lore raw

Re: [PATCH for-4.6 1/4] xen/arm: vgic: Rename nr_lines into nr_spis

Hi Ian,

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

The field nr_lines in the arch_domain vgic structure contains the number of SPIs for the emulated GIC. Using the nr_lines make confusion with the GIC code, where it means the number of IRQs. This can lead to coding error.

Also introduce vgic_nr_lines to get the number of IRQ handled by the emulated GIC.

From the code you seem to have called it vgic_nr_irqs, which I prefer.

It's an error in the commit message. I will fix it in the next version.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index faad1ff..31fb81a 100644
@@ -432,8 +432,6 @@ static int gicv2v_setup(struct domain *d)
         d->arch.vgic.cbase = GUEST_GICC_BASE;
     }
 
Quoted 6 lines

xen/arch/arm/gic-v2.c static int gicv2v_setup(struct domain *d) · line 435

Quoted 1 line
xen/arch/arm/gic-v2.c +0 -1
-    d->arch.vgic.nr_lines = 0;

Not replaced with an assignment to nr_spis, as you do in the v3 case?

We should be consistent, which also makes me wonder if nr_lines^Wspis should be the responsibility of the common vgic code to setup.

Actually it's already in common vgic code (see domain_vgic_init).

The initialization in gicv2v_setup was unnecessary, so does in gic_v3_init. I just forgot to drop it.

Shall I create a separate patch for the 2 lines drop? Or add a comment in the commit message?

Regards,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH for-4.6 1/4] xen/arm: vgic: Rename nr_lines into nr_spis

Quoted 31 lines · Julien Grall

Hi Ian,

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

The field nr_lines in the arch_domain vgic structure contains the number of SPIs for the emulated GIC. Using the nr_lines make confusion with the GIC code, where it means the number of IRQs. This can lead to coding error.

Also introduce vgic_nr_lines to get the number of IRQ handled by the emulated GIC.

From the code you seem to have called it vgic_nr_irqs, which I prefer.

It's an error in the commit message. I will fix it in the next version.

Quoted 8 lines
Quoted 8 lines
xen/arch/arm/gic-v2.c +0 -0
index faad1ff..31fb81a 100644
@@ -432,8 +432,6 @@ static int gicv2v_setup(struct domain *d)
         d->arch.vgic.cbase = GUEST_GICC_BASE;
     }
 
Quoted 6 lines

xen/arch/arm/gic-v2.c static int gicv2v_setup(struct domain *d) · line 435

Quoted 1 line
xen/arch/arm/gic-v2.c +0 -1
-    d->arch.vgic.nr_lines = 0;

Not replaced with an assignment to nr_spis, as you do in the v3 case?

We should be consistent, which also makes me wonder if nr_lines^Wspis should be the responsibility of the common vgic code to setup.

Quoted 7 lines

Actually it's already in common vgic code (see domain_vgic_init).

The initialization in gicv2v_setup was unnecessary, so does in gic_v3_init. I just forgot to drop it.

Shall I create a separate patch for the 2 lines drop? Or add a comment in the commit message?

Either works for me.

Ian.

#5 lore raw

PATCH 2/4 xen/arm: vgic: Keep track of vIRQ used by a domain

In reply to Julien Grall

While it's easy to know which hardware IRQ is assigned to a domain, there is no way to know which IRQ is emulated by Xen for a specific domain.

Introduce a bitmap to keep track of every vIRQ used by a domain. This will be used later to find free vIRQ for interrupt device assignment and emulated interrupt.

---
 xen/arch/arm/domain_build.c          |  6 +++
 xen/arch/arm/platforms/xgene-storm.c |  4 ++
 xen/arch/arm/vgic.c                  | 76 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vtimer.c                | 15 +++++++
 xen/include/asm-arm/domain.h         |  1 +
 xen/include/asm-arm/vgic.h           | 13 ++++++
 6 files changed, 115 insertions(+)
xen/arch/arm/domain_build.c +6 -0
index de180d8..c238c8f 100644
@@ -968,6 +968,12 @@ static int map_device(struct domain *d, struct dt_device_node *dev)
         irq = res;
 
         DPRINT("irq %u = %u\n", i, irq);
+        /*
+         * Checking the return of vgic_reserve_virq is not
+         * necessary. It should not fail except when we try to map
+         * twice the IRQ. This can happen if the IRQ is shared
+         */
+        vgic_reserve_virq(d, irq);
         res = route_irq_to_guest(d, irq, dt_node_name(dev));
         if ( res )
         {
xen/arch/arm/platforms/xgene-storm.c +4 -0
index 0b3492d..416d42c 100644
@@ -71,6 +71,10 @@ static int map_one_spi(struct domain *d, const char *what,
 
     printk("Additional IRQ %u (%s)\n", irq, what);
 
+    if ( !vgic_reserve_virq(d, irq) )
+        printk("Failed to reserve the vIRQ %u on dom%d\n",
+               irq, d->domain_id);
+
     ret = route_irq_to_guest(d, irq, what);
     if ( ret )
         printk("Failed to route %s to dom%d\n", what, d->domain_id);
xen/arch/arm/vgic.c +76 -0

100 lines not on this page · raw message

xen/arch/arm/vtimer.c +15 -0
index 2e95ceb..de660bb 100644
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));
+    }
+
     return 0;
 }
 
xen/include/asm-arm/domain.h +1 -0
index 8b7dd85..d302fc9 100644
@@ -90,6 +90,7 @@ struct arch_domain
         spinlock_t lock;
         int ctlr;
         int nr_spis; /* Number of SPIs */
+        unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
         struct vgic_irq_rank *shared_irqs;
         /*
          * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
xen/include/asm-arm/vgic.h +13 -0
index 74d5a4e..9e167fa 100644
@@ -199,6 +199,19 @@ extern int vgic_to_sgi(struct vcpu *v, register_t sgir,
                        enum gic_sgi_mode irqmode, int virq,
                        unsigned long vcpu_mask);
 extern void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
+
+/* Reserve a specific guest vIRQ */
+extern bool_t vgic_reserve_virq(struct domain *d, unsigned int virq);
+
+/*
+ * Allocate a guest VIRQ
+ *  - spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - spi == 0 => allocate an SGI
+ */
+extern int vgic_allocate_virq(struct domain *d, bool_t spi);
+
+extern void vgic_free_virq(struct domain *d, unsigned int irq);
+
 #endif /* __ASM_ARM_VGIC_H__ */
 
 /*
Signature · 1 line
2.1.3
#6 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Quoted 54 lines · Julien Grall

While it's easy to know which hardware IRQ is assigned to a domain, there is no way to know which IRQ is emulated by Xen for a specific domain.

Introduce a bitmap to keep track of every vIRQ used by a domain. This will be used later to find free vIRQ for interrupt device assignment and emulated interrupt.

---
 xen/arch/arm/domain_build.c          |  6 +++
 xen/arch/arm/platforms/xgene-storm.c |  4 ++
 xen/arch/arm/vgic.c                  | 76 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vtimer.c                | 15 +++++++
 xen/include/asm-arm/domain.h         |  1 +
 xen/include/asm-arm/vgic.h           | 13 ++++++
 6 files changed, 115 insertions(+)
xen/arch/arm/domain_build.c +6 -0
index de180d8..c238c8f 100644
@@ -968,6 +968,12 @@ static int map_device(struct domain *d, struct dt_device_node *dev)
         irq = res;
 
         DPRINT("irq %u = %u\n", i, irq);
+        /*
+         * Checking the return of vgic_reserve_virq is not
+         * necessary. It should not fail except when we try to map
+         * twice the IRQ. This can happen if the IRQ is shared
+         */
+        vgic_reserve_virq(d, irq);
         res = route_irq_to_guest(d, irq, dt_node_name(dev));
         if ( res )
         {
xen/arch/arm/platforms/xgene-storm.c +4 -0
index 0b3492d..416d42c 100644
@@ -71,6 +71,10 @@ static int map_one_spi(struct domain *d, const char *what,
 
     printk("Additional IRQ %u (%s)\n", irq, what);
 
+    if ( !vgic_reserve_virq(d, irq) )
+        printk("Failed to reserve the vIRQ %u on dom%d\n",
+               irq, d->domain_id);
+
     ret = route_irq_to_guest(d, irq, what);
     if ( ret )
         printk("Failed to route %s to dom%d\n", what, d->domain_id);
xen/arch/arm/vgic.c +0 -0
index 75cb7ff..dbfc259 100644

xen/arch/arm/vgic.c int domain_vgic_init(struct domain *d) · line 90

Quoted 4 lines
xen/arch/arm/vgic.c +1 -0
@@ -87,6 +87,8 @@ int domain_vgic_init(struct domain *d)
         return -ENODEV;
     }
 
+    spin_lock_init(&d->arch.vgic.lock);

you should probably explain in the commit message the reason why you are making changes to the vgic lock

Quoted 31 lines
xen/arch/arm/vgic.c +10 -0
     d->arch.vgic.shared_irqs =
         xzalloc_array(struct vgic_irq_rank, DOMAIN_NR_RANKS(d));
     if ( d->arch.vgic.shared_irqs == NULL )
@@ -107,6 +109,15 @@ int domain_vgic_init(struct domain *d)
 
     d->arch.vgic.handler->domain_init(d);
 
+    d->arch.vgic.allocated_irqs =
+        xzalloc_array(unsigned long, BITS_TO_LONGS(vgic_num_irqs(d)));
+    if ( !d->arch.vgic.allocated_irqs )
+        return -ENOMEM;
+
+    /* vIRQ0-15 (SGIs) are reserved */
+    for (i = 0; i <= 15; i++)
+        set_bit(i, d->arch.vgic.allocated_irqs);
+
     return 0;
 }
 
@@ -119,6 +130,7 @@ void domain_vgic_free(struct domain *d)
 {
     xfree(d->arch.vgic.shared_irqs);
     xfree(d->arch.vgic.pending_irqs);
+    xfree(d->arch.vgic.allocated_irqs);
 }
 
 int vcpu_vgic_init(struct vcpu *v)
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 465

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;
+
+    spin_lock(&d->arch.vgic.lock);
+    reserved = !test_and_set_bit(virq, d->arch.vgic.allocated_irqs);
+    spin_unlock(&d->arch.vgic.lock);

test_and_set_bit is atomic, why do you need to take the lock?

Quoted 18 lines
xen/arch/arm/vgic.c +18 -0
+    return reserved;
+}
+
+int vgic_allocate_virq(struct domain *d, bool_t spi)
+{
+    int ret = -1;
+    unsigned int virq;
+
+    spin_lock(&d->arch.vgic.lock);
+    if ( !spi )
+    {
+        virq = find_first_zero_bit(d->arch.vgic.allocated_irqs, 32);
+        if ( virq >= 32 )
+            goto unlock;
+    }
+    else
+    {
+        virq = find_next_zero_bit(d->arch.vgic.allocated_irqs,

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 494

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+                                  32, vgic_num_irqs(d));
+        if ( virq >= vgic_num_irqs(d) )
+            goto unlock;
+    }
+
+    set_bit(virq, d->arch.vgic.allocated_irqs);
+    ret = virq;
+
+unlock:
+    spin_unlock(&d->arch.vgic.lock);

you might be able to write this function without taking the lock too, by using test_and_set_bit and retries:

retry:

    virq = find_first_zero_bit;
    if (test_and_set_bit(virq))
        goto retry;
Quoted 92 lines
xen/arch/arm/vgic.c +24 -0
+    return ret;
+}
+
+void vgic_free_virq(struct domain *d, unsigned int virq)
+{
+    unsigned int spi;
+
+    if ( is_hardware_domain(d) )
+        return;
+
+    if ( virq < 32 && virq >= vgic_num_irqs(d) )
+        return;
+
+    spi = virq - 32;
+
+    /* Taking the vGIC domain lock is not necessary. We don't care if
+     * the bit is cleared a bit later. What only matters is bit to 1.
+     *
+     * With this solution vgic_allocate may fail to find an vIRQ if the
+     * allocated_irqs is fully. But we don't care.
+     */
+    clear_bit(spi, d->arch.vgic.allocated_irqs);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vtimer.c +15 -0
index 2e95ceb..de660bb 100644
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));
+    }
+
     return 0;
 }
 
xen/include/asm-arm/domain.h +1 -0
index 8b7dd85..d302fc9 100644
@@ -90,6 +90,7 @@ struct arch_domain
         spinlock_t lock;
         int ctlr;
         int nr_spis; /* Number of SPIs */
+        unsigned long *allocated_irqs; /* bitmap of IRQs allocated */
         struct vgic_irq_rank *shared_irqs;
         /*
          * SPIs are domain global, SGIs and PPIs are per-VCPU and stored in
xen/include/asm-arm/vgic.h +13 -0
index 74d5a4e..9e167fa 100644
@@ -199,6 +199,19 @@ extern int vgic_to_sgi(struct vcpu *v, register_t sgir,
                        enum gic_sgi_mode irqmode, int virq,
                        unsigned long vcpu_mask);
 extern void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
+
+/* Reserve a specific guest vIRQ */
+extern bool_t vgic_reserve_virq(struct domain *d, unsigned int virq);
+
+/*
+ * Allocate a guest VIRQ
+ *  - spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - spi == 0 => allocate an SGI
+ */
+extern int vgic_allocate_virq(struct domain *d, bool_t spi);
+
+extern void vgic_free_virq(struct domain *d, unsigned int irq);
+
 #endif /* __ASM_ARM_VGIC_H__ */
 
 /*
Signature · 1 line
2.1.3
#7 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Hi Stefano,

Quoted 5 lines · Stefano Stabellini
Quoted 1 line · Julien Grall

+ spin_lock_init(&d->arch.vgic.lock);

you should probably explain in the commit message the reason why you are making changes to the vgic lock

Actually the domain vgic lock was never used. Only the per-vcpu vgic lock was in used.

So I don't make any change to the vgic lock. If I don't use it, I will add a patch to drop it.

Quoted 4 lines
Quoted 4 lines
xen/arch/arm/vgic.c +0 -0
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 
Quoted 12 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 465

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;
+
+    spin_lock(&d->arch.vgic.lock);
+    reserved = !test_and_set_bit(virq, d->arch.vgic.allocated_irqs);
+    spin_unlock(&d->arch.vgic.lock);

test_and_set_bit is atomic, why do you need to take the lock?

To avoid race condition with vgic_allocate_virq.

Anyway, I will dropped it with your suggestion to implement vgic_allocate_virq without lock.

[..]

Quoted 29 lines
Quoted 15 lines
xen/arch/arm/vgic.c +15 -0
+int vgic_allocate_virq(struct domain *d, bool_t spi)
+{
+    int ret = -1;
+    unsigned int virq;
+
+    spin_lock(&d->arch.vgic.lock);
+    if ( !spi )
+    {
+        virq = find_first_zero_bit(d->arch.vgic.allocated_irqs, 32);
+        if ( virq >= 32 )
+            goto unlock;
+    }
+    else
+    {
+        virq = find_next_zero_bit(d->arch.vgic.allocated_irqs,

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 494

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+                                  32, vgic_num_irqs(d));
+        if ( virq >= vgic_num_irqs(d) )
+            goto unlock;
+    }
+
+    set_bit(virq, d->arch.vgic.allocated_irqs);
+    ret = virq;
+
+unlock:
+    spin_unlock(&d->arch.vgic.lock);

you might be able to write this function without taking the lock too, by using test_and_set_bit and retries:

Quoted 4 lines

retry:

    virq = find_first_zero_bit;
    if (test_and_set_bit(virq))
        goto retry;

I will give a look to it. I will also to limit the number of retry (maybe to the number of vIRQ) for safety.

Regards,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Quoted 30 lines · Julien Grall
Quoted 5 lines · Stefano Stabellini
Quoted 1 line · Julien Grall

+ spin_lock_init(&d->arch.vgic.lock);

you should probably explain in the commit message the reason why you are making changes to the vgic lock

Actually the domain vgic lock was never used. Only the per-vcpu vgic lock was in used.

So I don't make any change to the vgic lock. If I don't use it, I will add a patch to drop it.

Quoted 4 lines
Quoted 4 lines
xen/arch/arm/vgic.c +0 -0
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 
Quoted 12 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 465

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;
+
+    spin_lock(&d->arch.vgic.lock);
+    reserved = !test_and_set_bit(virq, d->arch.vgic.allocated_irqs);
+    spin_unlock(&d->arch.vgic.lock);

test_and_set_bit is atomic, why do you need to take the lock?

Quoted 4 lines

To avoid race condition with vgic_allocate_virq.

Anyway, I will dropped it with your suggestion to implement vgic_allocate_virq without lock.

Hmmm ... I was wrong on this one. The domain vgic lock is used in the macro vgic_lock.

But it has never been initialized. I will send a separate patch for correctly initialize it.

Regards,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

In reply to Julien Grall
Quoted 2 lines · Julien Grall

While it's easy to know which hardware IRQ is assigned to a domain, there is no way to know which IRQ is emulated by Xen for a specific domain.

It seems you are tracking all valid interrupts, including hardware ones, not just those for emulated devices? Perhaps rather than emulated you meant "allocated to the guest" or "routed" or something?

Quoted 3 lines

Introduce a bitmap to keep track of every vIRQ used by a domain. This will be used later to find free vIRQ for interrupt device assignment and emulated interrupt.

Actually, don't you implement the alloc/free of vIRQs here too?

Is there a usecase for tracking SPIs in this way, or would tracking PPIs only be sufficient?

Quoted 15 lines
---
 xen/arch/arm/domain_build.c          |  6 +++
 xen/arch/arm/platforms/xgene-storm.c |  4 ++
 xen/arch/arm/vgic.c                  | 76 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vtimer.c                | 15 +++++++
 xen/include/asm-arm/domain.h         |  1 +
 xen/include/asm-arm/vgic.h           | 13 ++++++
 6 files changed, 115 insertions(+)
xen/arch/arm/domain_build.c +0 -0
index de180d8..c238c8f 100644

xen/arch/arm/domain_build.c static int map_device(struct domain *d, struct dt_device_node *dev) · line 974

Quoted 7 lines
xen/arch/arm/domain_build.c +4 -0
@@ -968,6 +968,12 @@ static int map_device(struct domain *d, struct dt_device_node *dev)
         irq = res;
 
         DPRINT("irq %u = %u\n", i, irq);
+        /*
+         * Checking the return of vgic_reserve_virq is not
+         * necessary. It should not fail except when we try to map
+         * twice the IRQ. This can happen if the IRQ is shared

Return and handle EBUSY to distinguish other errors?

("try to map the IRQ twice")

Quoted 10 lines
xen/arch/arm/domain_build.c +2 -0
+         */
+        vgic_reserve_virq(d, irq);
         res = route_irq_to_guest(d, irq, dt_node_name(dev));
         if ( res )
         {
xen/arch/arm/platforms/xgene-storm.c +0 -0
index 0b3492d..416d42c 100644

xen/arch/arm/platforms/xgene-storm.c static int map_one_spi(struct domain *d, const char *what, · line 75

Quoted 5 lines
xen/arch/arm/platforms/xgene-storm.c +2 -0
@@ -71,6 +71,10 @@ static int map_one_spi(struct domain *d, const char *what,
 
     printk("Additional IRQ %u (%s)\n", irq, what);
 
+    if ( !vgic_reserve_virq(d, irq) )
+        printk("Failed to reserve the vIRQ %u on dom%d\n",

Drop "the".

Quoted 19 lines
xen/arch/arm/platforms/xgene-storm.c +2 -0
+               irq, d->domain_id);
+
     ret = route_irq_to_guest(d, irq, what);
     if ( ret )
         printk("Failed to route %s to dom%d\n", what, d->domain_id);
xen/arch/arm/vgic.c +2 -0
index 75cb7ff..dbfc259 100644
@@ -87,6 +87,8 @@ int domain_vgic_init(struct domain *d)
         return -ENODEV;
     }
 
+    spin_lock_init(&d->arch.vgic.lock);
+
     d->arch.vgic.shared_irqs =
         xzalloc_array(struct vgic_irq_rank, DOMAIN_NR_RANKS(d));
     if ( d->arch.vgic.shared_irqs == NULL )

xen/arch/arm/vgic.c int domain_vgic_init(struct domain *d) · line 113

Quoted 5 lines
xen/arch/arm/vgic.c +2 -0
@@ -107,6 +109,15 @@ int domain_vgic_init(struct domain *d)
 
     d->arch.vgic.handler->domain_init(d);
 
+    d->arch.vgic.allocated_irqs =
+        xzalloc_array(unsigned long, BITS_TO_LONGS(vgic_num_irqs(d)));

(this was why I asked if tracking SPIs was needed...)

Quoted 19 lines
xen/arch/arm/vgic.c +8 -0
+    if ( !d->arch.vgic.allocated_irqs )
+        return -ENOMEM;
+
+    /* vIRQ0-15 (SGIs) are reserved */
+    for (i = 0; i <= 15; i++)
+        set_bit(i, d->arch.vgic.allocated_irqs);
+
     return 0;
 }
 
@@ -119,6 +130,7 @@ void domain_vgic_free(struct domain *d)
 {
     xfree(d->arch.vgic.shared_irqs);
     xfree(d->arch.vgic.pending_irqs);
+    xfree(d->arch.vgic.allocated_irqs);
 }
 
 int vcpu_vgic_init(struct vcpu *v)

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 461

Quoted 9 lines
xen/arch/arm/vgic.c +6 -0
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;

EINVAL?

Quoted 6 lines
xen/arch/arm/vgic.c +6 -0
+    spin_lock(&d->arch.vgic.lock);
+    reserved = !test_and_set_bit(virq, d->arch.vgic.allocated_irqs);
+    spin_unlock(&d->arch.vgic.lock);
+
+    return reserved;
+}

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 478

Quoted 10 lines
xen/arch/arm/vgic.c +10 -0
+
+int vgic_allocate_virq(struct domain *d, bool_t spi)
+{
+    int ret = -1;
+    unsigned int virq;
+
+    spin_lock(&d->arch.vgic.lock);
+    if ( !spi )
+    {
+        virq = find_first_zero_bit(d->arch.vgic.allocated_irqs, 32);

I think you could use find_next_zero_bit here to start the search at bit 16 and stop at bit 31. Having done so, it might be nicer to if (spi) to select min and max IRQs and have the bit manipulation all be common.

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 499

Quoted 1 line
xen/arch/arm/vgic.c +1 -0
+void vgic_free_virq(struct domain *d, unsigned int virq)

It only frees spis, but the alloc version can do SPI or PPI. Is that on purpose?

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 512

Quoted 13 lines
xen/arch/arm/vgic.c +13 -0
+{
+    unsigned int spi;
+
+    if ( is_hardware_domain(d) )
+        return;
+
+    if ( virq < 32 && virq >= vgic_num_irqs(d) )
+        return;
+
+    spi = virq - 32;
+
+    /* Taking the vGIC domain lock is not necessary. We don't care if
+     * the bit is cleared a bit later. What only matters is bit to 1.

I don't grok the last sentence here.

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 515

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+     *
+     * With this solution vgic_allocate may fail to find an vIRQ if the
+     * allocated_irqs is fully. But we don't care.

are some words missing after fully?

Quoted 18 lines
xen/arch/arm/vgic.c +4 -0
+     */
+    clear_bit(spi, d->arch.vgic.allocated_irqs);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vtimer.c +3 -0
index 2e95ceb..de660bb 100644
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 10 lines
xen/arch/arm/vtimer.c +10 -0
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

Quoted 6 lines
xen/include/asm-arm/vgic.h +0 -0
index 74d5a4e..9e167fa 100644
@@ -199,6 +199,19 @@ extern int vgic_to_sgi(struct vcpu *v, register_t sgir,
                        enum gic_sgi_mode irqmode, int virq,

xen/include/asm-arm/vgic.h extern int vgic_to_sgi(struct vcpu *v, register_t sgir, · line 209

Quoted 10 lines
xen/include/asm-arm/vgic.h +8 -0
                        unsigned long vcpu_mask);
 extern void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
+
+/* Reserve a specific guest vIRQ */
+extern bool_t vgic_reserve_virq(struct domain *d, unsigned int virq);
+
+/*
+ * Allocate a guest VIRQ
+ *  - spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - spi == 0 => allocate an SGI

s/== 0/== 1/ and s/SGI/SPI/ in the last line.

Quoted 8 lines
xen/include/asm-arm/vgic.h +5 -0
+ */
+extern int vgic_allocate_virq(struct domain *d, bool_t spi);
+
+extern void vgic_free_virq(struct domain *d, unsigned int irq);
+
 #endif /* __ASM_ARM_VGIC_H__ */
 
 /*
#10 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Hi Ian,

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

While it's easy to know which hardware IRQ is assigned to a domain, there is no way to know which IRQ is emulated by Xen for a specific domain.

It seems you are tracking all valid interrupts, including hardware ones, not just those for emulated devices? Perhaps rather than emulated you meant "allocated to the guest" or "routed" or something?

Quoted 5 lines
Quoted 3 lines

Introduce a bitmap to keep track of every vIRQ used by a domain. This will be used later to find free vIRQ for interrupt device assignment and emulated interrupt.

Actually, don't you implement the alloc/free of vIRQs here too?

Yes.

Quoted 2 lines

Is there a usecase for tracking SPIs in this way, or would tracking PPIs only be sufficient?

We need to track everything for interrupt assignment to a guest/dom0. So if the guest ask for a free vIRQ we can give it directly.

Quoted 17 lines
Quoted 17 lines
---
 xen/arch/arm/domain_build.c          |  6 +++
 xen/arch/arm/platforms/xgene-storm.c |  4 ++
 xen/arch/arm/vgic.c                  | 76 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/vtimer.c                | 15 +++++++
 xen/include/asm-arm/domain.h         |  1 +
 xen/include/asm-arm/vgic.h           | 13 ++++++
 6 files changed, 115 insertions(+)
xen/arch/arm/domain_build.c +0 -0
index de180d8..c238c8f 100644
@@ -968,6 +968,12 @@ static int map_device(struct domain *d, struct dt_device_node *dev)
         irq = res;
 
Quoted 7 lines

xen/arch/arm/domain_build.c static int map_device(struct domain *d, struct dt_device_node *dev) · line 974

Quoted 5 lines
xen/arch/arm/domain_build.c +4 -0
         DPRINT("irq %u = %u\n", i, irq);
+        /*
+         * Checking the return of vgic_reserve_virq is not
+         * necessary. It should not fail except when we try to map
+         * twice the IRQ. This can happen if the IRQ is shared

Return and handle EBUSY to distinguish other errors?

vgic_reserve_virq can fail for 2 reasons:
- The IRQ is too high to handle by the vGIC => Unlikely as DOM0 use the
same IRQ number as the hardware.
- The vIRQ is already reserved.

The former will be catch just after with route_irq_to_guest. So I don't think it's worth to change the return from a bool to an int and return -EBUSY.

Quoted 13 lines

("try to map the IRQ twice")

Quoted 11 lines
xen/arch/arm/domain_build.c +2 -0
+         */
+        vgic_reserve_virq(d, irq);
         res = route_irq_to_guest(d, irq, dt_node_name(dev));
         if ( res )
         {
xen/arch/arm/platforms/xgene-storm.c +0 -0
index 0b3492d..416d42c 100644
@@ -71,6 +71,10 @@ static int map_one_spi(struct domain *d, const char *what,
 
Quoted 6 lines

xen/arch/arm/platforms/xgene-storm.c static int map_one_spi(struct domain *d, const char *what, · line 75

Quoted 4 lines
xen/arch/arm/platforms/xgene-storm.c +2 -0
     printk("Additional IRQ %u (%s)\n", irq, what);
 
+    if ( !vgic_reserve_virq(d, irq) )
+        printk("Failed to reserve the vIRQ %u on dom%d\n",

Drop "the".

Ok.

Quoted 20 lines
Quoted 20 lines
xen/arch/arm/platforms/xgene-storm.c +2 -0
+               irq, d->domain_id);
+
     ret = route_irq_to_guest(d, irq, what);
     if ( ret )
         printk("Failed to route %s to dom%d\n", what, d->domain_id);
xen/arch/arm/vgic.c +2 -0
index 75cb7ff..dbfc259 100644
@@ -87,6 +87,8 @@ int domain_vgic_init(struct domain *d)
         return -ENODEV;
     }
 
+    spin_lock_init(&d->arch.vgic.lock);
+
     d->arch.vgic.shared_irqs =
         xzalloc_array(struct vgic_irq_rank, DOMAIN_NR_RANKS(d));
     if ( d->arch.vgic.shared_irqs == NULL )
@@ -107,6 +109,15 @@ int domain_vgic_init(struct domain *d)
 
Quoted 6 lines

xen/arch/arm/vgic.c int domain_vgic_init(struct domain *d) · line 113

Quoted 4 lines
xen/arch/arm/vgic.c +2 -0
     d->arch.vgic.handler->domain_init(d);
 
+    d->arch.vgic.allocated_irqs =
+        xzalloc_array(unsigned long, BITS_TO_LONGS(vgic_num_irqs(d)));

(this was why I asked if tracking SPIs was needed...)

To complete my answer above:
- dom0: vgic_num_irqs() = number of hardware IRQS
- guest: vgic_num_irqs() = 32.

So we don't waste memory.

Quoted 23 lines
Quoted 23 lines
xen/arch/arm/vgic.c +8 -0
+    if ( !d->arch.vgic.allocated_irqs )
+        return -ENOMEM;
+
+    /* vIRQ0-15 (SGIs) are reserved */
+    for (i = 0; i <= 15; i++)
+        set_bit(i, d->arch.vgic.allocated_irqs);
+
     return 0;
 }
 
@@ -119,6 +130,7 @@ void domain_vgic_free(struct domain *d)
 {
     xfree(d->arch.vgic.shared_irqs);
     xfree(d->arch.vgic.pending_irqs);
+    xfree(d->arch.vgic.allocated_irqs);
 }
 
 int vcpu_vgic_init(struct vcpu *v)
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 
Quoted 8 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 461

Quoted 6 lines
xen/arch/arm/vgic.c +6 -0
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;

EINVAL?

vgic_reserve_irq returns a boolean:
0 => not reserved
1 => reserved

I don't see why we should return an int in this case, as the caller should know how to use it.

Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vgic.c +12 -0
+    spin_lock(&d->arch.vgic.lock);
+    reserved = !test_and_set_bit(virq, d->arch.vgic.allocated_irqs);
+    spin_unlock(&d->arch.vgic.lock);
+
+    return reserved;
+}
+
+int vgic_allocate_virq(struct domain *d, bool_t spi)
+{
+    int ret = -1;
+    unsigned int virq;
+
Quoted 8 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 478

Quoted 4 lines
xen/arch/arm/vgic.c +4 -0
+    spin_lock(&d->arch.vgic.lock);
+    if ( !spi )
+    {
+        virq = find_first_zero_bit(d->arch.vgic.allocated_irqs, 32);

I think you could use find_next_zero_bit here to start the search at bit 16 and stop at bit 31. Having done so, it might be nicer to if (spi) to select min and max IRQs and have the bit manipulation all be common.

I will give a look for the next version.

Quoted 5 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 499

Quoted 2 lines
xen/arch/arm/vgic.c +1 -0
+void vgic_free_virq(struct domain *d, unsigned int virq)

It only frees spis, but the alloc version can do SPI or PPI. Is that on purpose?

I forgot to update vgic_free_virq when I made the support for PPIs.

Quoted 16 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 512

Quoted 13 lines
xen/arch/arm/vgic.c +13 -0
+{
+    unsigned int spi;
+
+    if ( is_hardware_domain(d) )
+        return;
+
+    if ( virq < 32 && virq >= vgic_num_irqs(d) )
+        return;
+
+    spi = virq - 32;
+
+    /* Taking the vGIC domain lock is not necessary. We don't care if
+     * the bit is cleared a bit later. What only matters is bit to 1.

I don't grok the last sentence here.

Quoted 5 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 515

Quoted 3 lines
xen/arch/arm/vgic.c +3 -0
+     *
+     * With this solution vgic_allocate may fail to find an vIRQ if the
+     * allocated_irqs is fully. But we don't care.

are some words missing after fully?

This will be dropped in the next version. So forget this part :).

Quoted 34 lines
Quoted 34 lines
xen/arch/arm/vgic.c +4 -0
+     */
+    clear_bit(spi, d->arch.vgic.allocated_irqs);
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/vtimer.c +5 -0
index 2e95ceb..de660bb 100644
+ */
+extern int vgic_allocate_virq(struct domain *d, bool_t spi);
+
+extern void vgic_free_virq(struct domain *d, unsigned int irq);
+
 #endif /* __ASM_ARM_VGIC_H__ */
 
 /*
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

I would prefer to keep the BUG_ON here.

Quoted 11 lines
Quoted 11 lines
xen/include/asm-arm/vgic.h +2 -0
index 74d5a4e..9e167fa 100644
@@ -199,6 +199,19 @@ extern int vgic_to_sgi(struct vcpu *v, register_t sgir,
                        enum gic_sgi_mode irqmode, int virq,
                        unsigned long vcpu_mask);
 extern void vgic_migrate_irq(struct vcpu *old, struct vcpu *new, unsigned int irq);
+
+/* Reserve a specific guest vIRQ */
Quoted 8 lines

xen/include/asm-arm/vgic.h extern int vgic_to_sgi(struct vcpu *v, register_t sgir, · line 209

Quoted 6 lines
xen/include/asm-arm/vgic.h +6 -0
+extern bool_t vgic_reserve_virq(struct domain *d, unsigned int virq);
+
+/*
+ * Allocate a guest VIRQ
+ *  - spi == 0 => allocate a PPI. It will be the same on every vCPU
+ *  - spi == 0 => allocate an SGI

s/== 0/== 1/ and s/SGI/SPI/ in the last line.

I will fix it.

Regards,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Quoted 17 lines · Julien Grall

Hi Ian,

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

While it's easy to know which hardware IRQ is assigned to a domain, there is no way to know which IRQ is emulated by Xen for a specific domain.

It seems you are tracking all valid interrupts, including hardware ones, not just those for emulated devices? Perhaps rather than emulated you meant "allocated to the guest" or "routed" or something?

Quoted 5 lines
Quoted 3 lines

Introduce a bitmap to keep track of every vIRQ used by a domain. This will be used later to find free vIRQ for interrupt device assignment and emulated interrupt.

Actually, don't you implement the alloc/free of vIRQs here too?

Quoted 7 lines

Yes.

Quoted 2 lines

Is there a usecase for tracking SPIs in this way, or would tracking PPIs only be sufficient?

We need to track everything for interrupt assignment to a guest/dom0. So if the guest ask for a free vIRQ we can give it directly.

Makes sense.

In that case you 0/4 mail doesn't fully describe the use case for the series, since it talks about the dom0 PPI only.

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
xen/arch/arm/vgic.c +8 -0
+    if ( !d->arch.vgic.allocated_irqs )
+        return -ENOMEM;
+
+    /* vIRQ0-15 (SGIs) are reserved */
+    for (i = 0; i <= 15; i++)
+        set_bit(i, d->arch.vgic.allocated_irqs);
+
     return 0;
 }
@@ -119,6 +130,7 @@ void domain_vgic_free(struct domain *d)
 {
     xfree(d->arch.vgic.shared_irqs);
     xfree(d->arch.vgic.pending_irqs);
+    xfree(d->arch.vgic.allocated_irqs);
 }
 
 int vcpu_vgic_init(struct vcpu *v)
@@ -441,6 +453,70 @@ int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)
     return v->domain->arch.vgic.handler->emulate_sysreg(regs, hsr);
 }
 
Quoted 8 lines

xen/arch/arm/vgic.c int vgic_emulate(struct cpu_user_regs *regs, union hsr hsr) · line 461

Quoted 6 lines
xen/arch/arm/vgic.c +6 -0
+bool_t vgic_reserve_virq(struct domain *d, unsigned int virq)
+{
+    bool_t reserved;
+
+    if ( virq >= vgic_num_irqs(d) )
+        return 0;

EINVAL?

Quoted 1 line

vgic_reserve_irq returns a boolean:

Please use true/false then.

In Xen we have xen/stdbool.h which differs from normal stdboot.h. I'm not sure what the rules are for use.

Quoted 5 lines

0 => not reserved
1 => reserved

I don't see why we should return an int in this case, as the caller should know how to use it.

It's slightly more conventional to return error codes, but I guess I don't mind much.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

Quoted 5 lines

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

I would prefer to keep the BUG_ON here

I'm not objecting the the BUG_ON itself but to the fact that the condition has a side effect. Please use:

        if (!do_something())
        	BUG()

instead to avoid this.

Ian.

#12 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

(CC Jan)

Hi Ian,

Quoted 6 lines · Ian Campbell
Quoted 1 line

vgic_reserve_irq returns a boolean:

Please use true/false then.

In Xen we have xen/stdbool.h which differs from normal stdboot.h. I'm not sure what the rules are for use.

Jan please correct me if I'm wrong, xen/stdbool.h has been introduced for the ELF code and should not be used anywhere else.

true/false is defined in xen/stdbool.h together with Bool not bool_t.

Quoted 8 lines
Quoted 5 lines

0 => not reserved
1 => reserved

I don't see why we should return an int in this case, as the caller should know how to use it.

It's slightly more conventional to return error codes, but I guess I don't mind much.

Agree, but in this particular case we don't have to know the error code. So it's pointless to return it.

Quoted 25 lines
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

Quoted 7 lines
Quoted 1 line

I would prefer to keep the BUG_ON here

I'm not objecting the the BUG_ON itself but to the fact that the condition has a side effect. Please use:

        if (!do_something())
        	BUG()

instead to avoid this.

We have other place in the code where BUG_ON as a side-effect.

IHMO, if (!do_something()) BUG() <=> BUG_ON.

On the latter you know directly why it's failing, on the former you have to look at the code.

Regards,

Signature · 1 line
Julien Grall
#13 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

Quoted 1 line · Julien Grall

(CC Jan)

I think you forget, I added him.

Quoted 27 lines
Quoted 27 lines
Quoted 26 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

I would prefer to keep the BUG_ON here

Quoted 7 lines
Quoted 5 lines

I'm not objecting the the BUG_ON itself but to the fact that the condition has a side effect. Please use:

        if (!do_something())
        	BUG()

instead to avoid this.

We have other place in the code where BUG_ON as a side-effect.

If we do then it is a tiny minority of places, and they are IMHO wrong. I spotted one in the 600+ results of grepping for BUG_ON.

Quoted 1 line

IHMO, if (!do_something()) BUG() <=> BUG_ON.

No, BUG_ON() is a variant of ASSERT(), with the distinction being that the former is not only included when debug=y. It is as wrong to have a side-effect in the BUG_ON as it is to have one in an ASSERT.

Quoted 2 lines

On the latter you know directly why it's failing, on the former you have to look at the code.

If it's important/possible to fail then a log message would be appropriate.

Ian.

#14 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

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

(CC Jan)

I think you forget, I added him.

Quoted 33 lines
Quoted 32 lines
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

Quoted 5 lines

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

I would prefer to keep the BUG_ON here

I'm not objecting the the BUG_ON itself but to the fact that the condition has a side effect. Please use:

        if (!do_something())
        	BUG()

instead to avoid this.

Quoted 4 lines
Quoted 1 line

We have other place in the code where BUG_ON as a side-effect.

If we do then it is a tiny minority of places, and they are IMHO wrong. I spotted one in the 600+ results of grepping for BUG_ON.

I spotted more. Anyway, I will move to a if (!do_smth()) BUG() form.

Regards,

Signature · 1 line
Julien Grall
#15 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

In reply to Julien Grall
Quoted 1 line · Julien Grall

(CC Jan)

Forgot to really CC Jan for the bool stuff.

Quoted 69 lines

Hi Ian,

Quoted 6 lines · Ian Campbell
Quoted 1 line

vgic_reserve_irq returns a boolean:

Please use true/false then.

In Xen we have xen/stdbool.h which differs from normal stdboot.h. I'm not sure what the rules are for use.

Jan please correct me if I'm wrong, xen/stdbool.h has been introduced for the ELF code and should not be used anywhere else.

true/false is defined in xen/stdbool.h together with Bool not bool_t.

Quoted 8 lines
Quoted 5 lines

0 => not reserved
1 => reserved

I don't see why we should return an int in this case, as the caller should know how to use it.

It's slightly more conventional to return error codes, but I guess I don't mind much.

Agree, but in this particular case we don't have to know the error code. So it's pointless to return it.

Quoted 25 lines
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
xen/arch/arm/vtimer.c +8 -0
@@ -49,6 +49,21 @@ int domain_vtimer_init(struct domain *d)
 {
     d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
+
+    /* At this stage vgic_reserve_virq can't fail */
+    if ( is_hardware_domain(d) )
+    {
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_SECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_PHYS_NONSECURE_PPI)));
+        BUG_ON(!vgic_reserve_virq(d, timer_get_irq(TIMER_VIRT_PPI)));
+    }
Quoted 8 lines

xen/arch/arm/vtimer.c int domain_vtimer_init(struct domain *d) · line 64

Quoted 5 lines
xen/arch/arm/vtimer.c +5 -0
+    else
+    {
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_S_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_PHYS_NS_PPI));
+        BUG_ON(!vgic_reserve_virq(d, GUEST_TIMER_VIRT_PPI));

Although BUG_ON is not conditional on $debug I think we still should avoid side effects in the condition.

I know, but this should never fail as it called during on domain construction. If so we may have some other issue later if we decide to assign PPI to a guest.

Quoted 7 lines
Quoted 1 line

I would prefer to keep the BUG_ON here

I'm not objecting the the BUG_ON itself but to the fact that the condition has a side effect. Please use:

        if (!do_something())
        	BUG()

instead to avoid this.

We have other place in the code where BUG_ON as a side-effect.

IHMO, if (!do_something()) BUG() <=> BUG_ON.

On the latter you know directly why it's failing, on the former you have to look at the code.

Regards,

Signature · 1 line
Julien Grall
#16 lore raw

Re: [PATCH for-4.6 2/4] xen/arm: vgic: Keep track of vIRQ used by a domain

In reply to Ian Campbell
Quoted 7 lines · Ian Campbell
Quoted 2 lines

We need to track everything for interrupt assignment to a guest/dom0. So if the guest ask for a free vIRQ we can give it directly.

Makes sense.

In that case you 0/4 mail doesn't fully describe the use case for the series, since it talks about the dom0 PPI only.

Sorry I skipped this comment by inadvertence. My cover letter was explaining the current use case, I didn't think to explain the future use case. I will update the cover letter.

Regards,

Signature · 1 line
Julien Grall
#17 lore raw

PATCH 3/4 xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

In reply to Julien Grall

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

---
 xen/arch/arm/vgic.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/arm/vgic.c +4 -0
index dbfc259..719cb9f 100644
@@ -282,6 +282,10 @@ void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n)
         if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
             gic_raise_guest_irq(v_target, irq, p->priority);
         spin_unlock_irqrestore(&v_target->arch.vgic.lock, flags);
+
+        if ( !test_bit(irq, d->arch.vgic.allocated_irqs) )
+            gdprintk(XENLOG_DEBUG, "vIRQ %u is not allocated\n", irq);
+
         if ( p->desc != NULL )
         {
             irq_set_affinity(p->desc, cpumask_of(v_target->processor));
Signature · 1 line
2.1.3
#18 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

Quoted 2 lines · Julien Grall

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

Quoted 10 lines
---
 xen/arch/arm/vgic.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/arm/vgic.c +0 -0
index dbfc259..719cb9f 100644

xen/arch/arm/vgic.c void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n) · line 287

Quoted 6 lines
xen/arch/arm/vgic.c +3 -0
@@ -282,6 +282,10 @@ void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n)
         if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
             gic_raise_guest_irq(v_target, irq, p->priority);
         spin_unlock_irqrestore(&v_target->arch.vgic.lock, flags);
+
+        if ( !test_bit(irq, d->arch.vgic.allocated_irqs) )
+            gdprintk(XENLOG_DEBUG, "vIRQ %u is not allocated\n", irq);

Should this not be first after the irq= line inside this loop?

Quoted 4 lines
xen/arch/arm/vgic.c +1 -0
+
         if ( p->desc != NULL )
         {
             irq_set_affinity(p->desc, cpumask_of(v_target->processor));
#19 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

Hi Ian,

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

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

"A register bit corresponding to an unimplemented interrupt is RAZ/WI."

The goal of this print was mostly for debugging physical IRQ routed to a guest.

I could extend to ignore write to any register that should be RAZ/WI for this specific interrupt.
But, I will have to think about possible race condition with the hypercall to route a physical IRQ to the guest (see [1] and [2]).

The vIRQ is reserved before the physical IRQ is effectively routed. So a guest may enable the vIRQ before this time lapse. Though, the patch
[2] protected for a such case.

Not sure if we should take care of a such case.

Regards,

Signature · 1 line
Julien Grall
#20 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

Quoted 13 lines · Julien Grall

Hi Ian,

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

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

Quoted 7 lines

"A register bit corresponding to an unimplemented interrupt is RAZ/WI."

The goal of this print was mostly for debugging physical IRQ routed to a guest.

I could extend to ignore write to any register that should be RAZ/WI for this specific interrupt.

Since those are the defined semantics I think that is the best thing to do.

Quoted 8 lines

But, I will have to think about possible race condition with the hypercall to route a physical IRQ to the guest (see [1] and [2]).

The vIRQ is reserved before the physical IRQ is effectively routed. So a guest may enable the vIRQ before this time lapse. Though, the patch
[2] protected for a such case.

Not sure if we should take care of a such case.

I don't think so, that routing hypercall ought to be happening strictly before any hotplug event visible to the guest causes it to think there is a device (vacuously true for coldplug too), so the guest should have no expectation of being able to do anything with the irq in question.

Ian.

#21 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

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

Hi Ian,

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

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

"A register bit corresponding to an unimplemented interrupt is RAZ/WI."

Quoted 8 lines
Quoted 5 lines

The goal of this print was mostly for debugging physical IRQ routed to a guest.

I could extend to ignore write to any register that should be RAZ/WI for this specific interrupt.

Since those are the defined semantics I think that is the best thing to do.

Ok. I will look at it to see how we can implement it.

Quoted 7 lines
Quoted 7 lines

But, I will have to think about possible race condition with the hypercall to route a physical IRQ to the guest (see [1] and [2]).

The vIRQ is reserved before the physical IRQ is effectively routed. So a guest may enable the vIRQ before this time lapse. Though, the patch
[2] protected for a such case.

Quoted 6 lines
Quoted 1 line

Not sure if we should take care of a such case.

I don't think so, that routing hypercall ought to be happening strictly before any hotplug event visible to the guest causes it to think there is a device (vacuously true for coldplug too), so the guest should have no expectation of being able to do anything with the irq in question.

Good, one less headache :)

Regards,

Signature · 1 line
Julien Grall
#22 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

Hi Ian,

Quoted 18 lines · Julien Grall
Quoted 17 lines · Ian Campbell
Quoted 16 lines · Julien Grall
Quoted 9 lines · Ian Campbell
Quoted 2 lines · Julien Grall

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

"A register bit corresponding to an unimplemented interrupt is RAZ/WI."

The goal of this print was mostly for debugging physical IRQ routed to a guest.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I could extend to ignore write to any register that should be RAZ/WI for this specific interrupt.

Since those are the defined semantics I think that is the best thing to do.

Ok. I will look at it to see how we can implement it.

So I looked to the code. It may need some rework to effectively implement most of registers bits RAZ/WI when the interrupt doesn't exist.

As this series is required for the ACPI series, I suggest to defer this work in a follow-up series.

Regards,

Signature · 1 line
Julien Grall
#23 lore raw

Re: [PATCH for-4.6 3/4] xen/arm: vgic: notice if the vIRQ is not allocated when the guest enable it

Quoted 27 lines · Julien Grall

Hi Ian,

Quoted 24 lines · Julien Grall
Quoted 22 lines · Ian Campbell
Quoted 11 lines · Julien Grall
Quoted 9 lines · Ian Campbell
Quoted 2 lines · Julien Grall

This help for guest interrupts debugging. If the vIRQ is not allocate, this means that nothing is wired to it.

Should we short circuit the rest of the enable operation for this IRQ then? i.e. implement such writes as ignored, e.g. not reflect it in reads of ISENABLER etc.

What (if anything) does the GIC spec have to say on the subject?

Quoted 7 lines

"A register bit corresponding to an unimplemented interrupt is RAZ/WI."

The goal of this print was mostly for debugging physical IRQ routed to a guest.

I could extend to ignore write to any register that should be RAZ/WI for this specific interrupt.

Since those are the defined semantics I think that is the best thing to do.

Quoted 7 lines
Quoted 1 line

Ok. I will look at it to see how we can implement it.

So I looked to the code. It may need some rework to effectively implement most of registers bits RAZ/WI when the interrupt doesn't exist.

As this series is required for the ACPI series, I suggest to defer this work in a follow-up series.

OK.

#24 lore raw

PATCH 4/4 xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

In reply to Julien Grall

Use the new vgic interface to know which virtual PPI is free and use it for the event channel code.

At the DOM0 creation time, Xen still don't know which vIRQ will be free. All the vIRQ will be reserved when we parse the device tree. So allocate when the hypervisor node is created.

It's safe to defer the allocation because no vIRQ can be injected as long as the vCPU is not online.

Also correct the check in arch_domain_create to use is_hardware_domain.

---
 xen/arch/arm/domain.c                | 13 ++++++++++---
 xen/arch/arm/domain_build.c          | 10 ++++++++++
 xen/arch/arm/platform.c              |  7 -------
 xen/arch/arm/platforms/xgene-storm.c |  1 -
 xen/include/asm-arm/platform.h       |  4 ----
 5 files changed, 20 insertions(+), 15 deletions(-)
xen/arch/arm/domain.c +10 -3
index 7221bc8..7d14377 100644
@@ -543,10 +543,17 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
-    if ( d->domain_id )
+    /*
+     * The hardware domain will get a PPI later in
+     * arch/arm/domain_build.c  depending on the
+     * interrupt map of the hardware.
+     */
+    if ( !is_hardware_domain(d) )
+    {
         d->arch.evtchn_irq = GUEST_EVTCHN_PPI;
-    else
-        d->arch.evtchn_irq = platform_dom0_evtchn_ppi();
+        /* At this stage vgic_reserve_virq should never fail */
+        BUG_ON(vgic_reserve_virq(d, GUEST_EVTCHN_PPI));
+    }
 
     /*
      * Virtual UART is only used by linux early printk and decompress code.
xen/arch/arm/domain_build.c +10 -0
index c238c8f..8dedc60 100644
@@ -625,6 +625,16 @@ static int make_hypervisor_node(struct domain *d,
         return res;
 
     /*
+     * The allocation of the event channel IRQ has been deferred until
+     * now. At this time, all PPIs use by DOM0 has been registered
+     */
+    res = vgic_allocate_virq(d, 0);
+    if ( res < 0 )
+        return -FDT_ERR_XEN(ENOSPC);
+
+    d->arch.evtchn_irq = res;
+
+    /*
      * interrupts is evtchn upcall:
      *  - Active-low level-sensitive
      *  - All cpus
xen/arch/arm/platform.c +0 -7
index cb4cda8..d016797 100644
@@ -160,13 +160,6 @@ bool_t platform_device_is_blacklisted(const struct dt_device_node *node)
     return dt_match_node(blacklist, node);
 }
 
-unsigned int platform_dom0_evtchn_ppi(void)
-{
-    if ( platform && platform->dom0_evtchn_ppi )
-        return platform->dom0_evtchn_ppi;
-    return GUEST_EVTCHN_PPI;
-}
-
 void platform_dom0_gnttab(paddr_t *start, paddr_t *size)
 {
     if ( platform && platform->dom0_gnttab_size )
xen/arch/arm/platforms/xgene-storm.c +0 -1
index 416d42c..b0808b8 100644
@@ -232,7 +232,6 @@ PLATFORM_START(xgene_storm, "APM X-GENE STORM")
     .quirks = xgene_storm_quirks,
     .specific_mapping = xgene_storm_specific_mapping,
 
-    .dom0_evtchn_ppi = 24,
     .dom0_gnttab_start = 0x1f800000,
     .dom0_gnttab_size = 0x20000,
 PLATFORM_END
xen/include/asm-arm/platform.h +0 -4
index eefaca6..4eba37b 100644
@@ -38,10 +38,6 @@ struct platform_desc {
      */
     const struct dt_device_match *blacklist_dev;
     /*
-     * The IRQ (PPI) to use to inject event channels to dom0.
-     */
-    unsigned int dom0_evtchn_ppi;
-    /*
      * The location of a region of physical address space which dom0
      * can use for grant table mappings. If size is zero defaults to
      * 0xb0000000-0xb0020000.
Signature · 1 line
2.1.3
#25 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

Quoted 11 lines · Julien Grall
xen/arch/arm/domain.c +2 -1
index 7221bc8..7d14377 100644
@@ -543,10 +543,17 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
-    if ( d->domain_id )
+    /*
+     * The hardware domain will get a PPI later in

xen/arch/arm/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags) · line 555

Quoted 10 lines
xen/arch/arm/domain.c +7 -2
+     * arch/arm/domain_build.c  depending on the
+     * interrupt map of the hardware.
+     */
+    if ( !is_hardware_domain(d) )
+    {
         d->arch.evtchn_irq = GUEST_EVTCHN_PPI;
-    else
-        d->arch.evtchn_irq = platform_dom0_evtchn_ppi();
+        /* At this stage vgic_reserve_virq should never fail */
+        BUG_ON(vgic_reserve_virq(d, GUEST_EVTCHN_PPI));

I forgot the "!" in the BUG_ON. This line should be:

BUG_ON(!..)

Signature · 1 line
Julien Grall
#26 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

In reply to Julien Grall
Quoted 33 lines · Julien Grall

Use the new vgic interface to know which virtual PPI is free and use it for the event channel code.

At the DOM0 creation time, Xen still don't know which vIRQ will be free. All the vIRQ will be reserved when we parse the device tree. So allocate when the hypervisor node is created.

It's safe to defer the allocation because no vIRQ can be injected as long as the vCPU is not online.

Also correct the check in arch_domain_create to use is_hardware_domain.

---
 xen/arch/arm/domain.c                | 13 ++++++++++---
 xen/arch/arm/domain_build.c          | 10 ++++++++++
 xen/arch/arm/platform.c              |  7 -------
 xen/arch/arm/platforms/xgene-storm.c |  1 -
 xen/include/asm-arm/platform.h       |  4 ----
 5 files changed, 20 insertions(+), 15 deletions(-)
xen/arch/arm/domain.c +3 -1
index 7221bc8..7d14377 100644
@@ -543,10 +543,17 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
-    if ( d->domain_id )
+    /*
+     * The hardware domain will get a PPI later in
+     * arch/arm/domain_build.c  depending on the

xen/arch/arm/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags) · line 556

Quoted 10 lines
xen/arch/arm/domain.c +7 -2
+     * interrupt map of the hardware.
+     */
+    if ( !is_hardware_domain(d) )
+    {
         d->arch.evtchn_irq = GUEST_EVTCHN_PPI;
-    else
-        d->arch.evtchn_irq = platform_dom0_evtchn_ppi();
+        /* At this stage vgic_reserve_virq should never fail */
+        BUG_ON(vgic_reserve_virq(d, GUEST_EVTCHN_PPI));
+    }

Why do we still need this, if we have another vgic_allocate_virq call in make_hypervisor_node? Wouldn't that work for DomUs too?

Quoted 71 lines
xen/arch/arm/domain.c +0 -0
     /*
      * Virtual UART is only used by linux early printk and decompress code.
xen/arch/arm/domain_build.c +10 -0
index c238c8f..8dedc60 100644
@@ -625,6 +625,16 @@ static int make_hypervisor_node(struct domain *d,
         return res;
 
     /*
+     * The allocation of the event channel IRQ has been deferred until
+     * now. At this time, all PPIs use by DOM0 has been registered
+     */
+    res = vgic_allocate_virq(d, 0);
+    if ( res < 0 )
+        return -FDT_ERR_XEN(ENOSPC);
+
+    d->arch.evtchn_irq = res;
+
+    /*
      * interrupts is evtchn upcall:
      *  - Active-low level-sensitive
      *  - All cpus
xen/arch/arm/platform.c +0 -7
index cb4cda8..d016797 100644
@@ -160,13 +160,6 @@ bool_t platform_device_is_blacklisted(const struct dt_device_node *node)
     return dt_match_node(blacklist, node);
 }
 
-unsigned int platform_dom0_evtchn_ppi(void)
-{
-    if ( platform && platform->dom0_evtchn_ppi )
-        return platform->dom0_evtchn_ppi;
-    return GUEST_EVTCHN_PPI;
-}
-
 void platform_dom0_gnttab(paddr_t *start, paddr_t *size)
 {
     if ( platform && platform->dom0_gnttab_size )
xen/arch/arm/platforms/xgene-storm.c +0 -1
index 416d42c..b0808b8 100644
@@ -232,7 +232,6 @@ PLATFORM_START(xgene_storm, "APM X-GENE STORM")
     .quirks = xgene_storm_quirks,
     .specific_mapping = xgene_storm_specific_mapping,
 
-    .dom0_evtchn_ppi = 24,
     .dom0_gnttab_start = 0x1f800000,
     .dom0_gnttab_size = 0x20000,
 PLATFORM_END
xen/include/asm-arm/platform.h +0 -4
index eefaca6..4eba37b 100644
@@ -38,10 +38,6 @@ struct platform_desc {
      */
     const struct dt_device_match *blacklist_dev;
     /*
-     * The IRQ (PPI) to use to inject event channels to dom0.
-     */
-    unsigned int dom0_evtchn_ppi;
-    /*
      * The location of a region of physical address space which dom0
      * can use for grant table mappings. If size is zero defaults to
      * 0xb0000000-0xb0020000.
Signature · 1 line
2.1.3
#27 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

Hi Stefano,

Quoted 39 lines · Stefano Stabellini
Quoted 38 lines · Julien Grall

Use the new vgic interface to know which virtual PPI is free and use it for the event channel code.

At the DOM0 creation time, Xen still don't know which vIRQ will be free. All the vIRQ will be reserved when we parse the device tree. So allocate when the hypervisor node is created.

It's safe to defer the allocation because no vIRQ can be injected as long as the vCPU is not online.

Also correct the check in arch_domain_create to use is_hardware_domain.

---
 xen/arch/arm/domain.c                | 13 ++++++++++---
 xen/arch/arm/domain_build.c          | 10 ++++++++++
 xen/arch/arm/platform.c              |  7 -------
 xen/arch/arm/platforms/xgene-storm.c |  1 -
 xen/include/asm-arm/platform.h       |  4 ----
 5 files changed, 20 insertions(+), 15 deletions(-)
xen/arch/arm/domain.c +7 -1
index 7221bc8..7d14377 100644
@@ -543,10 +543,17 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
-    if ( d->domain_id )
+    /*
+     * The hardware domain will get a PPI later in
+     * arch/arm/domain_build.c  depending on the
+     * interrupt map of the hardware.
+     */
+    if ( !is_hardware_domain(d) )
+    {
         d->arch.evtchn_irq = GUEST_EVTCHN_PPI;
Quoted 8 lines

xen/arch/arm/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags) · line 556

Quoted 5 lines
xen/arch/arm/domain.c +3 -2
-    else
-        d->arch.evtchn_irq = platform_dom0_evtchn_ppi();
+        /* At this stage vgic_reserve_virq should never fail */
+        BUG_ON(vgic_reserve_virq(d, GUEST_EVTCHN_PPI));
+    }

Why do we still need this, if we have another vgic_allocate_virq call in make_hypervisor_node? Wouldn't that work for DomUs too?

Because make_hypervisor_node is only used for DOM0. Futhermore, DOMUs are using a specific hardcoded layout (see xen/include/public/arch-arm.h).

Regards,

Signature · 1 line
Julien Grall
#28 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

Quoted 51 lines · Julien Grall

Hi Stefano,

Quoted 39 lines · Stefano Stabellini
Quoted 38 lines · Julien Grall

Use the new vgic interface to know which virtual PPI is free and use it for the event channel code.

At the DOM0 creation time, Xen still don't know which vIRQ will be free. All the vIRQ will be reserved when we parse the device tree. So allocate when the hypervisor node is created.

It's safe to defer the allocation because no vIRQ can be injected as long as the vCPU is not online.

Also correct the check in arch_domain_create to use is_hardware_domain.

---
 xen/arch/arm/domain.c                | 13 ++++++++++---
 xen/arch/arm/domain_build.c          | 10 ++++++++++
 xen/arch/arm/platform.c              |  7 -------
 xen/arch/arm/platforms/xgene-storm.c |  1 -
 xen/include/asm-arm/platform.h       |  4 ----
 5 files changed, 20 insertions(+), 15 deletions(-)
xen/arch/arm/domain.c +7 -1
index 7221bc8..7d14377 100644
@@ -543,10 +543,17 @@ int arch_domain_create(struct domain *d, unsigned int domcr_flags)
     if ( (rc = domain_vtimer_init(d)) != 0 )
         goto fail;
 
-    if ( d->domain_id )
+    /*
+     * The hardware domain will get a PPI later in
+     * arch/arm/domain_build.c  depending on the
+     * interrupt map of the hardware.
+     */
+    if ( !is_hardware_domain(d) )
+    {
         d->arch.evtchn_irq = GUEST_EVTCHN_PPI;
Quoted 8 lines

xen/arch/arm/domain.c int arch_domain_create(struct domain *d, unsigned int domcr_flags) · line 556

Quoted 5 lines
xen/arch/arm/domain.c +3 -2
-    else
-        d->arch.evtchn_irq = platform_dom0_evtchn_ppi();
+        /* At this stage vgic_reserve_virq should never fail */
+        BUG_ON(vgic_reserve_virq(d, GUEST_EVTCHN_PPI));
+    }

Why do we still need this, if we have another vgic_allocate_virq call in make_hypervisor_node? Wouldn't that work for DomUs too?

Quoted 1 line

Because make_hypervisor_node is only used for DOM0.

Yeah.

Quoted 2 lines

Futhermore, DOMUs
are using a specific hardcoded layout (see xen/include/public/arch-arm.h).

They don't actually have to be, but exposing vgic_allocate_virq to the tools would be overkill, so hardcoding is the pragmatic choice.

We could e.g. randomise the PPI in the tools, to stop people making assumptions. (If we were feeling mean of course...)

Ian.

#29 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

Quoted 5 lines · Ian Campbell

They don't actually have to be, but exposing vgic_allocate_virq to the tools would be overkill, so hardcoding is the pragmatic choice.

We could e.g. randomise the PPI in the tools, to stop people making assumptions. (If we were feeling mean of course...)

It would be nice to have a such think. If not randomize at each boot, maybe per Xen version.

Though it would break mini-os on ARM as the IRQ is hardcoded in the OS.

Regards,

Signature · 1 line
Julien Grall
#30 lore raw

Re: [PATCH for-4.6 4/4] xen/arm: Find automatically a PPI for the DOM0 event channel interrupt

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

They don't actually have to be, but exposing vgic_allocate_virq to the tools would be overkill, so hardcoding is the pragmatic choice.

We could e.g. randomise the PPI in the tools, to stop people making assumptions. (If we were feeling mean of course...)

It would be nice to have a such think. If not randomize at each boot, maybe per Xen version.

Like I say, that would be quite mean of us. (I was 99% joking about this FWIW, perhaps I needed to ring that bell a bit more though).

Quoted 1 line

Though it would break mini-os on ARM as the IRQ is hardcoded in the OS.

AIUI there is an eventual desire to have an ARM mini-os binary be portable across Xen versions, which will require dtb parsing for this stuff (actually, I thought the latest patches had added a bunch of such stuff, but maybe not this specific aspect).

Ian.