KVM: arm/arm64: Route vtimer events to user space
Subsystems (5)
PATCH v7 1/1 KVM: arm/arm64: Route vtimer events to user space
We have 2 modes for dealing with interrupts in the ARM world. We can either handle them all using hardware acceleration through the vgic or we can emulate a gic in user space and only drive CPU IRQ pins from there.
Unfortunately, when driving IRQs from user space, we never tell user space about timer events that may result in interrupt line state changes, so we lose out on timer events if we run with user space gic emulation.
This patch fixes that by exposing kvm's view of the vtimer irq line to user space and giving it a chance to sync that when it changes.
With this patch I can successfully run edk2 and Linux with user space gic emulation.
---
v1 -> v2:
- Add back curly brace that got lost
v2 -> v3:
- Split into patch set
v3 -> v4:
- Improve documentation
v4 -> v5:
- Rewrite to use pending state sync in sregs (marc)
- Remove redundant checks of vgic_initialized()
v5 -> v6:
- s/pending/asserted/
- split kvm_timer_flush_hwstate function
- remove user_timer_asserted
- rename kernel_timer_asserted to timer_irq_level
- exit only once per level change
v6 -> v7:
- Move run struct sync out of the run loop
- Only check for run sync in kvm_timer_flush_hwstate()
- qemu tree to try this out: https://github.com/agraf/qemu.git no-kvm-irqchip-for-v6
--- Documentation/virtual/kvm/api.txt | 27 +++++++++ arch/arm/include/uapi/asm/kvm.h | 2 + arch/arm/kvm/arm.c | 18 +++--- arch/arm64/include/uapi/asm/kvm.h | 2 + include/kvm/arm_arch_timer.h | 3 +- include/uapi/linux/kvm.h | 6 ++ virt/kvm/arm/arch_timer.c | 122 ++++++++++++++++++++++++++++++++------ 7 files changed, 150 insertions(+), 30 deletions(-)
Documentation/virtual/kvm/api.txt +27 -0
@@ -3928,3 +3928,30 @@ In order to use SynIC, it has to be activated by setting this capability via KVM_ENABLE_CAP ioctl on the vcpu fd. Note that this will disable the use of APIC hardware virtualization even if supported by the CPU, as it's incompatible with SynIC auto-EOI behavior. + +8.3 KVM_CAP_ARM_TIMER + +Architectures: arm, arm64 +This capability, if KVM_CHECK_EXTENSION indicates that it is available and no +in-kernel interrupt controller is in use, means that that the kernel populates +the vcpu's run->s.regs.timer_irq_level field with timers that are currently +considered asserted by kvm. + +If active, kvm guarantees at least one exit happens after it detects an +assertion change. This exit could either be a KVM_EXIT_INTR or any other exit +event, like KVM_EXIT_MMIO. This way kvm gives user space a chance to update its +own interrupt asserted status. This usually involves triggering an interrupt +line on a user space emulated interrupt controller, so user space should always +check the state of run->s.regs.timer_irq_level on every kvm exit. + +The field run->s.regs.timer_irq_level is available independent of +run->kvm_valid_regs or run->kvm_dirty_regs bits. If no in-kernel interrupt +controller is used and the capability exists, it will always be available and +populated. + +Currently the following bits are defined for the timer_irq_level bitmap: + + KVM_ARM_TIMER_VTIMER - virtual timer + +Future versions of kvm may implement additional timer events. These will get +indicated by additional KVM_CAP extensions.
arch/arm/include/uapi/asm/kvm.h +2 -0
@@ -105,6 +105,8 @@ struct kvm_debug_exit_arch { };struct kvm_sync_regs { +/* Used with KVM_CAP_ARM_TIMER */ +u8 timer_irq_level; };struct kvm_arch_memory_slot {
arch/arm/kvm/arm.c +8 -10
@@ -187,6 +187,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_ARM_PSCI_0_2 :case KVM_CAP_READONLY_MEM :case KVM_CAP_MP_STATE : +case KVM_CAP_ARM_TIMER : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
@@ -474,13 +475,7 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)return ret; } -/* -* Enable the arch timers only if we have an in-kernel VGIC -* and it has been properly initialized, since we cannot handle -* interrupts from the virtual timer with a userspace gic. -*/ -if (irqchip_in_kernel (kvm) &&vgic_initialized (kvm)) - ret =kvm_timer_enable (vcpu); + ret =kvm_timer_enable (vcpu);return ret; }
@@ -549,7 +544,7 @@ static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu)*/ int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *run) { -int ret; +int ret, timer_ret;sigset_t sigsaved;if (unlikely (!kvm_vcpu_initialized (vcpu)))
@@ -588,7 +583,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)*/ preempt_disable ();kvm_pmu_flush_hwstate (vcpu); -kvm_timer_flush_hwstate (vcpu); + timer_ret =kvm_timer_flush_hwstate (vcpu);kvm_vgic_flush_hwstate (vcpu);local_irq_disable ();
@@ -596,7 +591,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)/* * Re-check atomic conditions */ -if (signal_pending (current)) { +if (timer_ret ||signal_pending (current)) { ret = -EINTR ; run->exit_reason =KVM_EXIT_INTR ; }
@@ -668,6 +663,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) ret =handle_exit (vcpu, run, ret); } +/* Resynchronize the internal timer and vcpu run irq level bits */ +kvm_timer_sync_run (vcpu); +if (vcpu->sigset_active)sigprocmask (SIG_SETMASK , &sigsaved,NULL );return ret;
arch/arm64/include/uapi/asm/kvm.h +2 -0
@@ -143,6 +143,8 @@ struct kvm_debug_exit_arch {#define KVM_GUESTDBG_USE_HW (1 << 17)struct kvm_sync_regs { +/* Used with KVM_CAP_ARM_TIMER */ +u8 timer_irq_level; };struct kvm_arch_memory_slot {
include/kvm/arm_arch_timer.h +2 -1
@@ -63,8 +63,9 @@ void kvm_timer_init(struct kvm *kvm);int kvm_timer_vcpu_reset (struct kvm_vcpu *vcpu,const struct kvm_irq_level *irq);void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu); -void kvm_timer_flush_hwstate (struct kvm_vcpu *vcpu); +int kvm_timer_flush_hwstate (struct kvm_vcpu *vcpu);void kvm_timer_sync_hwstate (struct kvm_vcpu *vcpu); +void kvm_timer_sync_run (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_terminate (struct kvm_vcpu *vcpu);u64 kvm_arm_timer_get_reg (struct kvm_vcpu *,u64 regid);
include/uapi/linux/kvm.h +6 -0
@@ -870,6 +870,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_S390_USER_INSTR0 130#define KVM_CAP_MSI_DEVID 131#define KVM_CAP_PPC_HTM 132 +#define KVM_CAP_ARM_TIMER 133#ifdef KVM_CAP_IRQ_ROUTING
@@ -1327,4 +1328,9 @@ struct kvm_assigned_msix_entry {#define KVM_X2APIC_API_USE_32BIT_IDS (1ULL << 0)#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK (1ULL << 1) +/* Available with KVM_CAP_ARM_TIMER */ + +/* Bits for run->s.regs.{user,kernel}_timer_asserted */ +#define KVM_ARM_TIMER_VTIMER (1 << 0) +#endif /* __LINUX_KVM_H */
virt/kvm/arm/arch_timer.c +103 -19
176 lines not on this page · raw message
Signature · 1 line
1.8.5.6