xen/arm: trap guest WFI
PATCH v10 1/1 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 on v10:
- improve comment in code.
Changes in v9:
- rename _local_events_need_delivery tolocal_events_need_delivery_nomask;
- remove evtchn_upcall_mask checks and comments.
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 oflocal_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, 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 (); +/* 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_nomask () ) +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
Quoted 35 lines · Stefano Stabellini
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 inlr_pending.Properly implement
local_events_need_delivery: check if the guest disabled interrupts, if they aren't disabled, return positive ifgic_events_need_deliveryreturns positive. Otherwise we still need to check whetherevtchn_upcall_pendingis set but noVGIC_IRQ_EVTCHN_CALLBACKirqs are in flight: it could be the race described by commitdb453468d92369e7182663fb13e14d83ec4ce456"arm: vgic:
fix race between evtchn upcall andevtchnop_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_enableby clearingPSR_IRQ_MASK.Changes on v10:
- improve comment in code.Changes in v9:
- rename_local_events_need_deliverytolocal_events_need_delivery_nomask;
- removeevtchn_upcall_maskchecks and comments.Changes in v8:
- remove the mask check from_local_events_need_delivery, add an unconditional mask check inlocal_events_need_delivery.
Quoted 5 lines
Changes in v7:
- clearPSR_IRQ_MASKin the implementation oflocal_event_delivery_enable.Signed-off-by Stefano Stabellini <[email protected]>
Acked + applied.