Skip to content

Commit 4c60e36

Browse files
committed
KVM: arm/arm64: Provide a get_input_level for the arch timer
The VGIC can now support the life-cycle of mapped level-triggered interrupts, and we no longer have to read back the timer state on every exit from the VM if we had an asserted timer interrupt signal, because the VGIC already knows if we hit the unlikely case where the guest disables the timer without ACKing the virtual timer interrupt. This means we rework a bit of the code to factor out the functionality to snapshot the timer state from vtimer_save_state(), and we can reuse this functionality in the sync path when we have an irqchip in userspace, and also to support our implementation of the get_input_level() function for the timer. This change also means that we can no longer rely on the timer's view of the interrupt line to set the active state, because we no longer maintain this state for mapped interrupts when exiting from the guest. Instead, we only set the active state if the virtual interrupt is active, and otherwise we simply let the timer fire again and raise the virtual interrupt from the ISR. Reviewed-by: Eric Auger <[email protected]> Reviewed-by: Marc Zyngier <[email protected]> Signed-off-by: Christoffer Dall <[email protected]>
1 parent df635c5 commit 4c60e36

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

include/kvm/arm_arch_timer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu);
9090

9191
void kvm_timer_init_vhe(void);
9292

93+
bool kvm_arch_timer_get_input_level(int vintid);
94+
9395
#define vcpu_vtimer(v) (&(v)->arch.timer_cpu.vtimer)
9496
#define vcpu_ptimer(v) (&(v)->arch.timer_cpu.ptimer)
9597

virt/kvm/arm/arch_timer.c

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ static void kvm_timer_update_state(struct kvm_vcpu *vcpu)
343343
phys_timer_emulate(vcpu);
344344
}
345345

346+
static void __timer_snapshot_state(struct arch_timer_context *timer)
347+
{
348+
timer->cnt_ctl = read_sysreg_el0(cntv_ctl);
349+
timer->cnt_cval = read_sysreg_el0(cntv_cval);
350+
}
351+
346352
static void vtimer_save_state(struct kvm_vcpu *vcpu)
347353
{
348354
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -354,10 +360,8 @@ static void vtimer_save_state(struct kvm_vcpu *vcpu)
354360
if (!vtimer->loaded)
355361
goto out;
356362

357-
if (timer->enabled) {
358-
vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
359-
vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
360-
}
363+
if (timer->enabled)
364+
__timer_snapshot_state(vtimer);
361365

362366
/* Disable the virtual timer */
363367
write_sysreg_el0(0, cntv_ctl);
@@ -454,8 +458,7 @@ static void kvm_timer_vcpu_load_vgic(struct kvm_vcpu *vcpu)
454458
bool phys_active;
455459
int ret;
456460

457-
phys_active = vtimer->irq.level ||
458-
kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
461+
phys_active = kvm_vgic_map_is_active(vcpu, vtimer->irq.irq);
459462

460463
ret = irq_set_irqchip_state(host_vtimer_irq,
461464
IRQCHIP_STATE_ACTIVE,
@@ -535,54 +538,27 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
535538
set_cntvoff(0);
536539
}
537540

538-
static void unmask_vtimer_irq(struct kvm_vcpu *vcpu)
541+
/*
542+
* With a userspace irqchip we have to check if the guest de-asserted the
543+
* timer and if so, unmask the timer irq signal on the host interrupt
544+
* controller to ensure that we see future timer signals.
545+
*/
546+
static void unmask_vtimer_irq_user(struct kvm_vcpu *vcpu)
539547
{
540548
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
541549

542550
if (unlikely(!irqchip_in_kernel(vcpu->kvm))) {
543-
kvm_vtimer_update_mask_user(vcpu);
544-
return;
545-
}
546-
547-
/*
548-
* If the guest disabled the timer without acking the interrupt, then
549-
* we must make sure the physical and virtual active states are in
550-
* sync by deactivating the physical interrupt, because otherwise we
551-
* wouldn't see the next timer interrupt in the host.
552-
*/
553-
if (!kvm_vgic_map_is_active(vcpu, vtimer->irq.irq)) {
554-
int ret;
555-
ret = irq_set_irqchip_state(host_vtimer_irq,
556-
IRQCHIP_STATE_ACTIVE,
557-
false);
558-
WARN_ON(ret);
551+
__timer_snapshot_state(vtimer);
552+
if (!kvm_timer_should_fire(vtimer)) {
553+
kvm_timer_update_irq(vcpu, false, vtimer);
554+
kvm_vtimer_update_mask_user(vcpu);
555+
}
559556
}
560557
}
561558

562-
/**
563-
* kvm_timer_sync_hwstate - sync timer state from cpu
564-
* @vcpu: The vcpu pointer
565-
*
566-
* Check if any of the timers have expired while we were running in the guest,
567-
* and inject an interrupt if that was the case.
568-
*/
569559
void kvm_timer_sync_hwstate(struct kvm_vcpu *vcpu)
570560
{
571-
struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
572-
573-
/*
574-
* If we entered the guest with the vtimer output asserted we have to
575-
* check if the guest has modified the timer so that we should lower
576-
* the line at this point.
577-
*/
578-
if (vtimer->irq.level) {
579-
vtimer->cnt_ctl = read_sysreg_el0(cntv_ctl);
580-
vtimer->cnt_cval = read_sysreg_el0(cntv_cval);
581-
if (!kvm_timer_should_fire(vtimer)) {
582-
kvm_timer_update_irq(vcpu, false, vtimer);
583-
unmask_vtimer_irq(vcpu);
584-
}
585-
}
561+
unmask_vtimer_irq_user(vcpu);
586562
}
587563

588564
int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)
@@ -813,6 +789,22 @@ static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
813789
return true;
814790
}
815791

792+
bool kvm_arch_timer_get_input_level(int vintid)
793+
{
794+
struct kvm_vcpu *vcpu = kvm_arm_get_running_vcpu();
795+
struct arch_timer_context *timer;
796+
797+
if (vintid == vcpu_vtimer(vcpu)->irq.irq)
798+
timer = vcpu_vtimer(vcpu);
799+
else
800+
BUG(); /* We only map the vtimer so far */
801+
802+
if (timer->loaded)
803+
__timer_snapshot_state(timer);
804+
805+
return kvm_timer_should_fire(timer);
806+
}
807+
816808
int kvm_timer_enable(struct kvm_vcpu *vcpu)
817809
{
818810
struct arch_timer_cpu *timer = &vcpu->arch.timer_cpu;
@@ -835,7 +827,7 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu)
835827
}
836828

837829
ret = kvm_vgic_map_phys_irq(vcpu, host_vtimer_irq, vtimer->irq.irq,
838-
NULL);
830+
kvm_arch_timer_get_input_level);
839831
if (ret)
840832
return ret;
841833

0 commit comments

Comments
 (0)