Skip to contentSkip to conversation

xen/arm: trap guest WFI

PATCH Superseded Stefano Stabellini 2 participants 5 messages Apr 22, 2013 – Apr 23, 2013

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 2/2 xen/arm: trap guest WFI

Trap guest WFI, block the guest VCPU unless it has pending interrupts (WFI should return if any interrupts arrive even if interrupts are disabled).

Awake the guest vcpu when a new interrupt for it arrives.

Introduce gic_events_need_delivery: it checks whether the current vcpu has any interrupts that need to be delivered either on the lrs or in lr_pending.

Properly implement local_events_need_delivery: check if the guest disabled interrupts, if they aren't disabled, return positive if gic_events_need_delivery returns positive. Otherwise we still need to check whether evtchn_upcall_pending is set but no VGIC_IRQ_EVTCHN_CALLBACK irqs are in flight: it could be the race described by commit db453468d92369e7182663fb13e14d83ec4ce456 "arm: vgic:
fix race between evtchn upcall and evtchnop_send". If that is the case it means that an event needs to be injected.
If all these tests are negative then no events need to be delivered.

Implement local_event_delivery_enable by clearing PSR_IRQ_MASK.

Changes in v8:
- remove the mask check from _local_events_need_delivery, add an unconditional mask check in local_events_need_delivery.

Changes in v7:
- clear PSR_IRQ_MASK in the implementation of
local_event_delivery_enable.

---
 xen/arch/arm/gic.c          |    8 +++++++-
 xen/arch/arm/traps.c        |   17 ++++++++++++++++-
 xen/arch/arm/vgic.c         |    1 +
 xen/include/asm-arm/event.h |   41 +++++++++++++++++++++++++++++++++--------
 xen/include/asm-arm/gic.h   |    1 +
 5 files changed, 58 insertions(+), 10 deletions(-)
xen/arch/arm/gic.c +7 -1
index 0d1ab5a..5e83c50 100644
@@ -589,13 +589,19 @@ static void gic_inject_irq_stop(void)
     }
 }
 
+int gic_events_need_delivery(void)
+{
+    return (!list_empty(&current->arch.vgic.lr_pending) ||
+            this_cpu(lr_mask));
+}
+
 void gic_inject(void)
 {
     if ( vcpu_info(current, evtchn_upcall_pending) )
         vgic_vcpu_inject_irq(current, VGIC_IRQ_EVTCHN_CALLBACK, 1);
 
     gic_restore_pending_irqs(current);
-    if (!this_cpu(lr_mask))
+    if (!gic_events_need_delivery())
         gic_inject_irq_stop();
     else
         gic_inject_irq_start();
xen/arch/arm/traps.c +16 -1
index b7487b7..b6dcd1e 100644
@@ -29,7 +29,9 @@
 #include <xen/hypercall.h>
 #include <xen/softirq.h>
 #include <xen/domain_page.h>
+#include <public/sched.h>
 #include <public/xen.h>
+#include <asm/event.h>
 #include <asm/regs.h>
 #include <asm/cpregs.h>
 
@@ -59,7 +61,7 @@ void __cpuinit init_traps(void)
     WRITE_SYSREG((vaddr_t)hyp_traps_vector, VBAR_EL2);
 
     /* Setup hypervisor traps */
-    WRITE_SYSREG(HCR_PTW|HCR_BSU_OUTER|HCR_AMO|HCR_IMO|HCR_VM, HCR_EL2);
+    WRITE_SYSREG(HCR_PTW|HCR_BSU_OUTER|HCR_AMO|HCR_IMO|HCR_VM|HCR_TWI, HCR_EL2);
     isb();
 }
 
@@ -931,6 +933,19 @@ asmlinkage void do_trap_hypervisor(struct cpu_user_regs *regs)
     union hsr hsr = { .bits = READ_SYSREG32(ESR_EL2) };
 
     switch (hsr.ec) {
+    case HSR_EC_WFI_WFE:
+        /* at the moment we only trap WFI */
+        vcpu_block();
+        /* The ARM spec declares that even if local irqs are masked in
+         * the CPSR register, an irq should wake up a cpu from WFI anyway.
+         * For this reason we need to check for irqs that need delivery,
+         * ignoring the CPSR register, *after* calling SCHEDOP_block to
+         * avoid races with vgic_vcpu_inject_irq.
+         */
+        if ( _local_events_need_delivery() )
+            vcpu_unblock(current);
+        regs->pc += hsr.len ? 4 : 2;
+        break;
     case HSR_EC_CP15_32:
         if ( ! is_pv32_domain(current->domain) )
             goto bad_trap;
xen/arch/arm/vgic.c +1 -0
index 4d8da02..b30da78 100644
@@ -618,6 +618,7 @@ void vgic_vcpu_inject_irq(struct vcpu *v, unsigned int irq, int virtual)
 out:
     spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
     /* we have a new higher priority irq, inject it into the guest */
+    vcpu_unblock(v);
 }
 
 /*
xen/include/asm-arm/event.h +33 -8

60 lines not on this page · raw message

xen/include/asm-arm/gic.h +1 -0
index 24c0d5c..92711d5 100644
@@ -143,6 +143,7 @@ extern void gic_route_ppis(void);
 extern void gic_route_spis(void);
 
 extern void gic_inject(void);
+extern int gic_events_need_delivery(void);
 
 extern void __cpuinit init_maintenance_interrupt(void);
 extern void gic_set_guest_irq(struct vcpu *v, unsigned int irq,
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 1 line · Stefano Stabellini

+static inline int _local_events_need_delivery(void)

Can we call this local_events_need_delivery_nomask or something to make it clear why it is special (which the leading _ doesn't really do).

Quoted 6 lines
xen/include/asm-arm/event.h +6 -0
+{
+    struct pending_irq *p = irq_to_pending(current, VGIC_IRQ_EVTCHN_CALLBACK);
+
+    /* XXX: if the first interrupt has already been delivered, we should
+     * check whether any higher priority interrupts are in the
+     * lr_pending queue or in the LR registers and return 1 only in that

xen/include/asm-arm/event.h line 26

Quoted 10 lines
xen/include/asm-arm/event.h +10 -0
+     * case.
+     * In practice the guest interrupt handler should run with
+     * interrupts disabled so this shouldn't be a problem in the general
+     * case.
+     */
+    if ( gic_events_need_delivery() )
+        return 1;
+
+    if ( vcpu_info(current, evtchn_upcall_pending) &&
+        !vcpu_info(current, evtchn_upcall_mask) &&

I don't think you need this upcall_mask check.

Quoted 19 lines
xen/include/asm-arm/event.h +11 -3
+        list_empty(&p->inflight) )
+        return 1;
+
+    return 0;
+}
+
 static inline int local_events_need_delivery(void)
 {
-    /* TODO
-     * return (vcpu_info(v, evtchn_upcall_pending) &&
-                        !vcpu_info(v, evtchn_upcall_mask)); */
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+
+    /* guest IRQs are masked */
+    if ( (regs->cpsr & PSR_IRQ_MASK) )
         return 0;
+    return _local_events_need_delivery();
 }
 

xen/include/asm-arm/event.h line 47

Quoted 10 lines
xen/include/asm-arm/event.h +0 -5
 int local_event_delivery_is_enabled(void);
 
-static inline void local_event_delivery_disable(void)
-{
-    /* TODO current->vcpu_info->evtchn_upcall_mask = 1; */
-}
-
 static inline void local_event_delivery_enable(void)
 {
     /* TODO current->vcpu_info->evtchn_upcall_mask = 0; */

No reason to leave this TODO IMHO.

Ian.

#2 lore raw
Quoted 6 lines · Ian Campbell
Quoted 1 line · Stefano Stabellini

+static inline int _local_events_need_delivery(void)

Can we call this local_events_need_delivery_nomask or something to make it clear why it is special (which the leading _ doesn't really do).

Yeah

Quoted 19 lines
Quoted 6 lines
xen/include/asm-arm/event.h +6 -0
+{
+    struct pending_irq *p = irq_to_pending(current, VGIC_IRQ_EVTCHN_CALLBACK);
+
+    /* XXX: if the first interrupt has already been delivered, we should
+     * check whether any higher priority interrupts are in the
+     * lr_pending queue or in the LR registers and return 1 only in that

xen/include/asm-arm/event.h line 26

Quoted 10 lines
xen/include/asm-arm/event.h +10 -0
+     * case.
+     * In practice the guest interrupt handler should run with
+     * interrupts disabled so this shouldn't be a problem in the general
+     * case.
+     */
+    if ( gic_events_need_delivery() )
+        return 1;
+
+    if ( vcpu_info(current, evtchn_upcall_pending) &&
+        !vcpu_info(current, evtchn_upcall_mask) &&

I don't think you need this upcall_mask check.

Quoted 11 lines

xen/include/asm-arm/event.h line 47

Quoted 8 lines
xen/include/asm-arm/event.h +0 -5
-static inline void local_event_delivery_disable(void)
-{
-    /* TODO current->vcpu_info->evtchn_upcall_mask = 1; */
-}
-
 static inline void local_event_delivery_enable(void)
 {
     /* TODO current->vcpu_info->evtchn_upcall_mask = 0; */

No reason to leave this TODO IMHO.

I can remove both, but I would need to add a comment somewhere else saying that evtchn_upcall_mask is going to be removed. Where do you think is the right place for it?

#3 lore raw
Quoted 3 lines · Stefano Stabellini

I can remove both, but I would need to add a comment somewhere else saying that evtchn_upcall_mask is going to be removed. Where do you think is the right place for it?

I'm working on such a patch now, will that help?

#4 lore raw
Quoted 7 lines · Ian Campbell
Quoted 3 lines · Stefano Stabellini

I can remove both, but I would need to add a comment somewhere else saying that evtchn_upcall_mask is going to be removed. Where do you think is the right place for it?

I'm working on such a patch now, will that help?

All right then