xen/arm: trap guest WFI
PATCH v6 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.
--- 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, 57 insertions(+), 11 deletions(-)
xen/arch/arm/gic.c +7 -1
@@ -589,13 +589,19 @@ static void gic_inject_irq_stop(void) } } +int gic_events_need_delivery (void ) +{ +return (!list_empty (¤t->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
@@ -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 (current); +/* 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 (0 ) ) +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
@@ -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 +32 -9
59 lines not on this page · raw message
xen/include/asm-arm/gic.h +1 -0
@@ -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
xen/include/asm-arm/event.h line 47
Quoted 4 lines · Stefano Stabellini
static inline void
local_event_delivery_enable(void){xen/include/asm-arm/event.h +1
-1-/* TODO current->vcpu_info->evtchn_upcall_mask = 0; */ + current->vcpu_info->evtchn_upcall_mask =0 ;
Did you miss my comments re the meaninglessness of evtchn_upcall_mask on ARM?
Nothing in the guest ever sets this bit and nothing pays it any attention on the actual interrupt/evtchn delivery path either, so looking at it in the "are events pending" path doesn't make sense.
Look at
$ rgrep evtchn_upcall_mask xen/arch/arm/ xen/include/asm-arm/ and all you will see is false positives like the above commented out code. The only generic user is the dump_domains() debugkey output (which is therefore wrong on ARM).
This logic needs to be based around looking at the I bit in the CPSR. To avoid this confusion reoccuring perhaps we should #define HAVE_PV_INTERRUPT_MASK and use it to turn this field in vcpu_info into a padding field?
Ian.
Quoted 15 lines
}
/* No arch specific virq definition now. Default to global. */
xen/include/asm-arm/gic.h +1
-0@@ -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,
Quoted 8 lines · Ian Campbell
xen/include/asm-arm/event.h line 47
Quoted 4 lines · Stefano Stabellini
static inline void
local_event_delivery_enable(void){xen/include/asm-arm/event.h +1
-1-/* TODO current->vcpu_info->evtchn_upcall_mask = 0; */ + current->vcpu_info->evtchn_upcall_mask =0 ;Did you miss my comments re the meaninglessness of
evtchn_upcall_maskon ARM?
I've just seen your early reply to v5 and replied there...