KVM: arm64: Add idempotent controls to migrate guest counter
Subsystems (4)
v7 cover KVM: arm64: Add idempotent controls to migrate guest counter
Currently, on KVM/arm64, we only allow a VMM to migrate the guest's virtual counter by-value. Saving and restoring the counter by value is problematic in the fact that the recorded state is not idempotent. Furthermore, we obfuscate from userspace the fact that the architecture actually provides offset-based controls.
Another issue is that KVM/arm64 doesn't provide userspace with the controls of the physical counter-timer. This series aims to address both issues by adding offset-based controls for the virtual and physical counters.
Patches 1-2 are refactor changes required to provide offset controls to userspace and putting in some generic plumbing to use for both physical and virtual offsets.
Patch 3 exposes a vCPU's virtual offset through the KVM_*_ONE_REG ioctls. When NV support is added to KVM, CNTVOFF_EL2 will be considered a guest system register. So, it is safe to expose it now through that ioctl.
Patch 4 adds a cpufeature bit to detect 'full' ECV implementations, providing EL2 with the ability to offset the physical counter-timer.
Patch 5 exposes a vCPU's physical offset as a vCPU device attribute. This is deliberate, as the attribute is not architectural; KVM uses this attribute to track the host<->guest offset.
Patch 6 is a prepatory change for the sake of physical offset emulation, as counter-timer traps must be configured separately for each vCPU.
Patch 7 allows non-ECV hosts to support the physical offset vCPU device attribute, by trapping and emulating the physical counter registers.
This series was tested on an Ampere Mt. Jade system (non-ECV, VHE and nVHE) as well as the ARM Base RevC FVP (ECV, VHE and nVHE). Patches apply to kvmarm/next at the following commit:
ae280335cdb5 ("Merge branch kvm-arm64/mmu/el2-tracking into kvmarm-master/next")
Selftests for these changes are being mailed as a separate series, since there exist dependencies betwen both x86 and arm64.
v6: https://lore.kernel.org/r/[email protected]
v6 -> v7:
- Fixed typo in documentation (Marc)
- Clean up some unused variables (Drew)
- Added trap configuration for ECV+nVHE (Marc)
- Documented dependency on SCR_EL3.ECVEn (Marc)
- wrap up ptimer_emulation_required() for use in hyp and kernel code (Drew)
- check static branch condition first (Drew)
- s/cpus_have_const_cap/cpus_have_final_cap/ (Marc)
- s/ARM64_ECV/ARM64_HAS_ECV2/
- Emulate CNTPCTSS_EL2 if ECV2 not present (Marc)
- Reordered the introduction of some functions to ensure that we don't have unused functions in the middle of the series.
- Cleaned up the read side of CNTVOFF_EL2 (from userspace). Don't open-code the answer based on the difference of hardware offsets, just use the guest system register value we stashed on the write side.
Oliver Upton (7):
KVM: arm64: Refactor update_vtimer_cntvoff()
KVM: arm64: Separate guest/host counter offset values
KVM: arm64: Allow userspace to configure a vCPU's virtual offset
arm64: cpufeature: Enumerate support for FEAT_ECV >= 0x2
KVM: arm64: Allow userspace to configure a guest's counter-timer
offset
KVM: arm64: Configure timer traps in vcpu_load() for VHE
KVM: arm64: Emulate physical counter offsetting on non-ECV systems
Documentation/arm64/booting.rst | 7 + Documentation/virt/kvm/api.rst | 10 ++ Documentation/virt/kvm/devices/vcpu.rst | 28 ++++ arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/sysreg.h | 5 + arch/arm64/include/uapi/asm/kvm.h | 2 + arch/arm64/kernel/cpufeature.c | 10 ++ arch/arm64/kvm/arch_timer.c | 196 +++++++++++++++++++++--- arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/guest.c | 6 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 32 ++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 + arch/arm64/kvm/hyp/nvhe/timer-sr.c | 20 ++- arch/arm64/kvm/hyp/vhe/timer-sr.c | 5 + arch/arm64/tools/cpucaps | 1 + include/clocksource/arm_arch_timer.h | 1 + include/kvm/arm_arch_timer.h | 9 +- 17 files changed, 315 insertions(+), 29 deletions(-)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH v7 1/7 KVM: arm64: Refactor update_vtimer_cntvoff()
Make the implementation of update_vtimer_cntvoff() generic w.r.t. guest timer context and spin off into a new helper method for later use. Require callers of this new helper method to grab the kvm lock beforehand.
No functional change intended.
--- arch/arm64/kvm/arch_timer.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +15 -5
@@ -747,22 +747,32 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)return 0 ; } -/* Make the updates of cntvoff for all vtimer contexts atomic */ -static void update_vtimer_cntvoff (struct kvm_vcpu *vcpu,u64 cntvoff) +/* Make offset updates for all timer contexts atomic */ +static void update_timer_offset (struct kvm_vcpu *vcpu, +enum kvm_arch_timers timer,u64 offset) {int i;struct kvm *kvm = vcpu->kvm;struct kvm_vcpu *tmp; -mutex_lock (&kvm->lock); +lockdep_assert_held (&kvm->lock); +kvm_for_each_vcpu (i, tmp, kvm) -timer_set_offset (vcpu_vtimer (tmp), cntvoff); +timer_set_offset (vcpu_get_timer (tmp, timer), offset);/* * When called from the vcpu create path, the CPU being created is not * included in the loop above, so we just set it here as well. */ -timer_set_offset (vcpu_vtimer (vcpu), cntvoff); +timer_set_offset (vcpu_get_timer (vcpu, timer), offset); +} + +static void update_vtimer_cntvoff (struct kvm_vcpu *vcpu,u64 cntvoff) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->lock); +update_timer_offset (vcpu,TIMER_VTIMER , cntvoff);mutex_unlock (&kvm->lock); }
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH v7 2/7 KVM: arm64: Separate guest/host counter offset values
In reply to Oliver UptonIn some instances, a VMM may want to update the guest's counter-timer offset in a transparent manner, meaning that changes to the hardware value do not affect the synthetic register presented to the guest or the VMM through said guest's architectural state. Lay the groundwork to separate guest offset register writes from the hardware values utilized by KVM.
--- arch/arm64/kvm/arch_timer.c | 42 +++++++++++++++++++++++++++--------- include/kvm/arm_arch_timer.h | 3 +++ 2 files changed, 35 insertions(+), 10 deletions(-)
arch/arm64/kvm/arch_timer.c +32 -10
@@ -84,11 +84,9 @@ u64 timer_get_cval(struct arch_timer_context *ctxt)static u64 timer_get_offset (struct arch_timer_context *ctxt) { -struct kvm_vcpu *vcpu = ctxt->vcpu; -switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : -return __vcpu_sys_reg (vcpu,CNTVOFF_EL2 ); +return ctxt->host_offset;default :return 0 ; }
@@ -128,17 +126,33 @@ static void timer_set_cval(struct arch_timer_context *ctxt, u64 cval)static void timer_set_offset (struct arch_timer_context *ctxt,u64 offset) { -struct kvm_vcpu *vcpu = ctxt->vcpu; -switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : -__vcpu_sys_reg (vcpu,CNTVOFF_EL2 ) = offset; + ctxt->host_offset = offset;break ;default :WARN (offset,"timer %ld\n" ,arch_timer_ctx_index (ctxt)); } } +static void timer_set_guest_offset (struct arch_timer_context *ctxt,u64 offset) +{ +struct kvm_vcpu *vcpu = ctxt->vcpu; + +switch (arch_timer_ctx_index (ctxt)) { +case TIMER_VTIMER : { +u64 host_offset =timer_get_offset (ctxt); + + host_offset += offset -__vcpu_sys_reg (vcpu,CNTVOFF_EL2 ); +__vcpu_sys_reg (vcpu,CNTVOFF_EL2 ) = offset; +timer_set_offset (ctxt, host_offset); +break ; + } +default : +WARN_ONCE (offset,"timer %ld\n" ,arch_timer_ctx_index (ctxt)); + } +} +u64 kvm_phys_timer_read (void ) {return timecounter->cc->read (timecounter->cc);
@@ -749,7 +763,8 @@ int kvm_timer_vcpu_reset(struct kvm_vcpu *vcpu)/* Make offset updates for all timer contexts atomic */ static void update_timer_offset (struct kvm_vcpu *vcpu, -enum kvm_arch_timers timer,u64 offset) +enum kvm_arch_timers timer,u64 offset, +bool guest_visible) {int i;struct kvm *kvm = vcpu->kvm;
@@ -758,13 +773,20 @@ static void update_timer_offset(struct kvm_vcpu *vcpu,lockdep_assert_held (&kvm->lock);kvm_for_each_vcpu (i, tmp, kvm) -timer_set_offset (vcpu_get_timer (tmp, timer), offset); +if (guest_visible) +timer_set_guest_offset (vcpu_get_timer (tmp, timer), + offset); +else +timer_set_offset (vcpu_get_timer (tmp, timer), offset);/* * When called from the vcpu create path, the CPU being created is not * included in the loop above, so we just set it here as well. */ -timer_set_offset (vcpu_get_timer (vcpu, timer), offset); +if (guest_visible) +timer_set_guest_offset (vcpu_get_timer (vcpu, timer), offset); +else +timer_set_offset (vcpu_get_timer (vcpu, timer), offset); }static void update_vtimer_cntvoff (struct kvm_vcpu *vcpu,u64 cntvoff)
@@ -772,7 +794,7 @@ static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)struct kvm *kvm = vcpu->kvm;mutex_lock (&kvm->lock); -update_timer_offset (vcpu,TIMER_VTIMER , cntvoff); +update_timer_offset (vcpu,TIMER_VTIMER , cntvoff, true);mutex_unlock (&kvm->lock); }
include/kvm/arm_arch_timer.h +3 -0
@@ -42,6 +42,9 @@ struct arch_timer_context {/* Duplicated state from arch_timer.c for convenience */ u32 host_timer_irq;u32 host_timer_irq_flags; + +/* offset relative to the host's physical counter-timer */ +u64 host_offset; };struct timer_map {
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH v7 3/7 KVM: arm64: Allow userspace to configure a vCPU's virtual offset
In reply to Oliver UptonAllow userspace to access the guest's virtual counter-timer offset through the ONE_REG interface. The value read or written is defined to be an offset from the guest's physical counter-timer. Add some documentation to clarify how a VMM should use this and the existing CNTVCT_EL0.
--- Documentation/virt/kvm/api.rst | 10 ++++++++++ arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 23 +++++++++++++++++++++++ arch/arm64/kvm/guest.c | 6 +++++- include/kvm/arm_arch_timer.h | 1 + 5 files changed, 40 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +10 -0
@@ -2463,6 +2463,16 @@ arm64 system registers have the following id bit patterns:: derived from the register encoding for CNTV_CVAL_EL0. As this is API, it must remain this way. +.. warning:: + + The value of KVM_REG_ARM_TIMER_OFFSET is defined as an offset from + the guest's view of the physical counter-timer. + + Userspace should use either KVM_REG_ARM_TIMER_OFFSET or + KVM_REG_ARM_TIMER_CNT to pause and resume a guest's virtual + counter-timer. Mixed use of these registers could result in an + unpredictable guest counter value. + arm64 firmware pseudo-registers have the following bit pattern:: 0x6030 0000 0014 <regno:16>
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -255,6 +255,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_TIMER_CTL ARM64_SYS_REG(3, 3, 14, 3, 1)#define KVM_REG_ARM_TIMER_CVAL ARM64_SYS_REG(3, 3, 14, 0, 2)#define KVM_REG_ARM_TIMER_CNT ARM64_SYS_REG(3, 3, 14, 3, 2) +#define KVM_REG_ARM_TIMER_OFFSET ARM64_SYS_REG(3, 4, 14, 0, 3)/* KVM-as-firmware specific pseudo-registers */ #define KVM_REG_ARM_FW (0x0014 << KVM_REG_ARM_COPROC_SHIFT)
arch/arm64/kvm/arch_timer.c +23 -0
@@ -92,6 +92,18 @@ static u64 timer_get_offset(struct arch_timer_context *ctxt) } } +static u64 timer_get_guest_offset (struct arch_timer_context *ctxt) +{ +struct kvm_vcpu *vcpu = ctxt->vcpu; + +switch (arch_timer_ctx_index (ctxt)) { +case TIMER_VTIMER : +return __vcpu_sys_reg (vcpu,CNTVOFF_EL2 ); +default : +return 0 ; + } +} +static void timer_set_ctl (struct arch_timer_context *ctxt,u32 ctl) {struct kvm_vcpu *vcpu = ctxt->vcpu;
@@ -852,6 +864,10 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) timer =vcpu_vtimer (vcpu);kvm_arm_timer_write (vcpu, timer,TIMER_REG_CVAL , value);break ; +case KVM_REG_ARM_TIMER_OFFSET : + timer =vcpu_vtimer (vcpu); +update_vtimer_cntvoff (vcpu, value); +break ;case KVM_REG_ARM_PTIMER_CTL : timer =vcpu_ptimer (vcpu);kvm_arm_timer_write (vcpu, timer,TIMER_REG_CTL , value);
@@ -896,6 +912,9 @@ u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid)case KVM_REG_ARM_TIMER_CVAL :return kvm_arm_timer_read (vcpu,vcpu_vtimer (vcpu),TIMER_REG_CVAL ); +case KVM_REG_ARM_TIMER_OFFSET : +return kvm_arm_timer_read (vcpu, +vcpu_vtimer (vcpu),TIMER_REG_OFFSET );case KVM_REG_ARM_PTIMER_CTL :return kvm_arm_timer_read (vcpu,vcpu_ptimer (vcpu),TIMER_REG_CTL );
@@ -933,6 +952,10 @@ static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, val =kvm_phys_timer_read () -timer_get_offset (timer);break ; +case TIMER_REG_OFFSET : + val =timer_get_guest_offset (timer); +break ; +default :BUG (); }
arch/arm64/kvm/guest.c +5 -1
@@ -591,7 +591,7 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)* ARM64 versions of the TIMER registers, always available on arm64 */ -#define NUM_TIMER_REGS 3 +#define NUM_TIMER_REGS 4static bool is_timer_reg (u64 index) {
@@ -599,6 +599,7 @@ static bool is_timer_reg(u64 index)case KVM_REG_ARM_TIMER_CTL :case KVM_REG_ARM_TIMER_CNT :case KVM_REG_ARM_TIMER_CVAL : +case KVM_REG_ARM_TIMER_OFFSET :return true; }return false;
@@ -614,6 +615,9 @@ static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) uindices++;if (put_user (KVM_REG_ARM_TIMER_CVAL , uindices))return -EFAULT ; + uindices++; +if (put_user (KVM_REG_ARM_TIMER_OFFSET , uindices)) +return -EFAULT ;return 0 ; }
include/kvm/arm_arch_timer.h +1 -0
@@ -21,6 +21,7 @@ enum kvm_arch_timer_regs {TIMER_REG_CVAL ,TIMER_REG_TVAL ,TIMER_REG_CTL , + TIMER_REG_OFFSET, }; struct arch_timer_context {
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
Marc Zyngier
maintainerRe: [PATCH v7 3/7] KVM: arm64: Allow userspace to configure a vCPU's virtual offset
Quoted 43 lines · Oliver Upton
Allow userspace to access the guest's virtual counter-timer offset through the
ONE_REGinterface. The value read or written is defined to be an offset from the guest's physical counter-timer. Add some documentation to clarify how a VMM should use this and the existingCNTVCT_EL0.Signed-off-by Oliver Upton <[email protected]>Reviewed-by Andrew Jones <[email protected]>--- Documentation/virt/kvm/api.rst | 10 ++++++++++ arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 23 +++++++++++++++++++++++ arch/arm64/kvm/guest.c | 6 +++++- include/kvm/arm_arch_timer.h | 1 + 5 files changed, 40 insertions(+), 1 deletion(-)Documentation/virt/kvm/api.rst +10
-0@@ -2463,6 +2463,16 @@ arm64 system registers have the following id bit patterns:: derived from the register encoding for CNTV_CVAL_EL0. As this is API, it must remain this way. +.. warning:: + + The value of KVM_REG_ARM_TIMER_OFFSET is defined as an offset from + the guest's view of the physical counter-timer. + + Userspace should use either KVM_REG_ARM_TIMER_OFFSET or + KVM_REG_ARM_TIMER_CNT to pause and resume a guest's virtual + counter-timer. Mixed use of these registers could result in an + unpredictable guest counter value. + arm64 firmware pseudo-registers have the following bit pattern:: 0x6030 0000 0014 <regno:16>arch/arm64/include/uapi/asm/kvm.h +0
-0
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 259
Quoted 5 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -255,6 +255,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_TIMER_CTL ARM64_SYS_REG(3, 3, 14, 3, 1)#define KVM_REG_ARM_TIMER_CVAL ARM64_SYS_REG(3, 3, 14, 0, 2)#define KVM_REG_ARM_TIMER_CNT ARM64_SYS_REG(3, 3, 14, 3, 2) +#define KVM_REG_ARM_TIMER_OFFSET ARM64_SYS_REG(3, 4, 14, 0, 3)
Andrew, does this warrant an update to the selftest that checks for sysreg visibility?
I am also wondering how a VMM such as QEMU is going to deal with the above restriction, given the way it blindly saves/restores all the registers that KVM exposes, hence hitting that mixed-use that the documentation warns about...
Thanks,
M.
Signature · 5 lines
Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v7 3/7] KVM: arm64: Allow userspace to configure a vCPU's virtual offset
Quoted 39 lines · Marc Zyngier
Quoted 37 lines · Oliver Upton
Allow userspace to access the guest's virtual counter-timer offset through the
ONE_REGinterface. The value read or written is defined to be an offset from the guest's physical counter-timer. Add some documentation to clarify how a VMM should use this and the existingCNTVCT_EL0.Signed-off-by Oliver Upton <[email protected]>Reviewed-by Andrew Jones <[email protected]>--- Documentation/virt/kvm/api.rst | 10 ++++++++++ arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 23 +++++++++++++++++++++++ arch/arm64/kvm/guest.c | 6 +++++- include/kvm/arm_arch_timer.h | 1 + 5 files changed, 40 insertions(+), 1 deletion(-)Documentation/virt/kvm/api.rst +10
-0@@ -2463,6 +2463,16 @@ arm64 system registers have the following id bit patterns:: derived from the register encoding for CNTV_CVAL_EL0. As this is API, it must remain this way. +.. warning:: + + The value of KVM_REG_ARM_TIMER_OFFSET is defined as an offset from + the guest's view of the physical counter-timer. + + Userspace should use either KVM_REG_ARM_TIMER_OFFSET or + KVM_REG_ARM_TIMER_CNT to pause and resume a guest's virtual + counter-timer. Mixed use of these registers could result in an + unpredictable guest counter value. + arm64 firmware pseudo-registers have the following bit pattern::
Quoted 14 lines
arch/arm64/include/uapi/asm/kvm.h struct kvm_arm_copy_mte_tags { · line 259
Quoted 11 lines
Documentation/virt/kvm/api.rst +0
-00x6030 0000 0014 <regno:16>arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -255,6 +255,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_REG_ARM_TIMER_CTL ARM64_SYS_REG(3, 3, 14, 3, 1)#define KVM_REG_ARM_TIMER_CVAL ARM64_SYS_REG(3, 3, 14, 0, 2)#define KVM_REG_ARM_TIMER_CNT ARM64_SYS_REG(3, 3, 14, 3, 2) +#define KVM_REG_ARM_TIMER_OFFSET ARM64_SYS_REG(3, 4, 14, 0, 3)Andrew, does this warrant an update to the selftest that checks for sysreg visibility?
Yup, until we do, the test will emit a warning with a suggestion to add the new register to the list. It won't be a test FAIL, because adding new registers doesn't break migration from older kernels, but we might as well update the list sooner than later.
Quoted 5 lines
I am also wondering how a VMM such as QEMU is going to deal with the above restriction, given the way it blindly saves/restores all the registers that KVM exposes, hence hitting that mixed-use that the documentation warns about...
You're right and I think it's a problem. While we can special case registers in QEMU using a cpreg "level" so they won't get saved/restored all the time, it doesn't help here since we won't be special casing KVM_REG_ARM_TIMER_OFFSET in older QEMU. We need a way for the VMM to opt in to using KVM_REG_ARM_TIMER_OFFSET, such as with a CAP we can enable.
Thanks,
drew
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v7 3/7] KVM: arm64: Allow userspace to configure a vCPU's virtual offset
In reply to Oliver UptonQuoted 8 lines · Oliver Upton
Allow userspace to access the guest's virtual counter-timer offset through the
ONE_REGinterface. The value read or written is defined to be an offset from the guest's physical counter-timer. Add some documentation to clarify how a VMM should use this and the existingCNTVCT_EL0.Signed-off-by Oliver Upton <[email protected]>Reviewed-by Andrew Jones <[email protected]>
Hrm...
I was mulling on this patch a bit more and had a thought. As previously discussed, the patch implements virtual offsets by broadcasting the same offset to all vCPUs in a guest. I wonder if this may tolerate bad practices from userspace that will break when KVM supports NV.
Consider that a nested guest may set CNTVOFF_EL2 to whatever value it wants. Presumably, we will need to patch the handling of CNTVOFF_EL2 to *not* broadcast to all vCPUs to save/restore NV properly. If a maligned VMM only wrote to a single vCPU, banking on this broadcasting implementation, it will fall flat on its face when handling an NV guest.
So, should we preemptively move to the new way of the world, wherein userspace accesses to CNTVOFF_EL2 are vCPU-local rather than VM-wide?
No strong opinions in either direction, but figured I'd address it since I'll need to respin this series anyway to fix ECV+nVHE.
--
Thanks,
Oliver
Marc Zyngier
maintainerRe: [PATCH v7 3/7] KVM: arm64: Allow userspace to configure a vCPU's virtual offset
Quoted 24 lines · Oliver Upton
Quoted 8 lines · Oliver Upton
Allow userspace to access the guest's virtual counter-timer offset through the
ONE_REGinterface. The value read or written is defined to be an offset from the guest's physical counter-timer. Add some documentation to clarify how a VMM should use this and the existingCNTVCT_EL0.Signed-off-by Oliver Upton <[email protected]>Reviewed-by Andrew Jones <[email protected]>Hrm...
I was mulling on this patch a bit more and had a thought. As previously discussed, the patch implements virtual offsets by broadcasting the same offset to all vCPUs in a guest. I wonder if this may tolerate bad practices from userspace that will break when KVM supports NV.
Consider that a nested guest may set
CNTVOFF_EL2to whatever value it wants. Presumably, we will need to patch the handling ofCNTVOFF_EL2to *not* broadcast to all vCPUs to save/restore NV properly. If a maligned VMM only wrote to a single vCPU, banking on this broadcasting implementation, it will fall flat on its face when handling an NV guest.
Quoted 5 lines
So, should we preemptively move to the new way of the world, wherein userspace accesses to
CNTVOFF_EL2are vCPU-local rather than VM-wide?No strong opinions in either direction, but figured I'd address it since I'll need to respin this series anyway to fix ECV+nVHE.
Thought about this a bit more whilst being away from a computer...
It all boils down to what we expose as an abstraction of a machine. If there is no EL2 in the VM, then there shouldn't be any way for the guest to observe different values for the counters as seen from different vcpus. That's what the architecture guarantees for a physical system, and we shouldn't deviate from that. Opening the door for userspace to do anything differently is a recipe for disaster.
It actually is an argument in favour of setting the various offsets to a value that keep the two physical and virtual counters in sync, instead of the current behaviour that allows different values to be observed.
The above is in contrast with what the architecture allows when EL2 is present. The hypervisor can naturally deal with the offsets as it sees fit, and no offset should have any bearing on it (this later point is of course to be moderated by CNTPOFF on the host).
To sum it up, I'd rather keep the CNTVOFF behaviour what it is today for guest that have their highest exception level at EL1. For EL2 guests, the setting will obviously have to become per-CPU.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v7 4/7 arm64: cpufeature: Enumerate support for FEAT_ECV >= 0x2
In reply to Oliver UptonIntroduce a new cpucap to indicate if the system supports full enhanced counter virtualization (i.e. ID_AA64MMFR0_EL1.ECV>=0x2).
--- arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/kernel/cpufeature.c | 10 ++++++++++ arch/arm64/tools/cpucaps | 1 + 3 files changed, 12 insertions(+)
arch/arm64/include/asm/sysreg.h +1 -0
@@ -847,6 +847,7 @@#define ID_AA64MMFR0_ASID_SHIFT 4#define ID_AA64MMFR0_PARANGE_SHIFT 0 +#define ID_AA64MMFR0_ECV_PHYS 0x2#define ID_AA64MMFR0_TGRAN4_NI 0xf#define ID_AA64MMFR0_TGRAN4_SUPPORTED 0x0#define ID_AA64MMFR0_TGRAN64_NI 0xf
arch/arm64/kernel/cpufeature.c +10 -0
@@ -2301,6 +2301,16 @@ static const struct arm64_cpu_capabilities arm64_features[] = { .matches = has_cpuid_feature, .min_field_value = 1, }, + { + .desc = "Enhanced Counter Virtualization (Physical)", + .capability = ARM64_HAS_ECV2, + .type = ARM64_CPUCAP_SYSTEM_FEATURE, + .sys_reg = SYS_ID_AA64MMFR0_EL1, + .sign = FTR_UNSIGNED, + .field_pos = ID_AA64MMFR0_ECV_SHIFT, + .matches = has_cpuid_feature, + .min_field_value = ID_AA64MMFR0_ECV_PHYS, + }, {}, };
arch/arm64/tools/cpucaps +1 -0
@@ -18,6 +18,7 @@ HAS_CRC32 HAS_DCPODP HAS_DCPOP HAS_E0PD +HAS_ECV2 HAS_EPAN HAS_GENERIC_AUTH HAS_GENERIC_AUTH_ARCH
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH v7 5/7 KVM: arm64: Allow userspace to configure a guest's counter-timer offset
In reply to Oliver UptonPresently, KVM provides no facilities for correctly migrating a guest that depends on the physical counter-timer. While most guests (barring NV, of course) should not depend on the physical counter-timer, an operator may wish to provide a consistent view of the physical counter-timer across migrations.
Provide userspace with a new vCPU attribute to modify the guest counter-timer offset. Unlike KVM_REG_ARM_TIMER_OFFSET, this attribute is hidden from the guest's architectural state. The value offsets *both* the virtual and physical counter-timer views for the guest. Only support this attribute on ECV systems as ECV is required for hardware offsetting of the physical counter-timer.
--- Documentation/arm64/booting.rst | 7 ++ Documentation/virt/kvm/devices/vcpu.rst | 28 ++++++++ arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/sysreg.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 96 ++++++++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 ++ arch/arm64/kvm/hyp/nvhe/timer-sr.c | 9 +++ arch/arm64/kvm/hyp/vhe/timer-sr.c | 5 ++ include/clocksource/arm_arch_timer.h | 1 + 10 files changed, 155 insertions(+), 2 deletions(-)
Documentation/arm64/booting.rst +7 -0
@@ -311,6 +311,13 @@ Before jumping into the kernel, the following conditions must be met: - ZCR_EL2.LEN must be initialised to the same value for all CPUs the kernel will execute on. + For CPUs with the Enhanced Counter Virtualization (FEAT_ECV) extension + present with ID_AA64MMFR0_EL1.ECV >= 0x2: + + - if EL3 is present and the kernel is entered at EL2: + + - SCR_EL3.ECVEn (bit 28) must be initialized to 0b1. + The requirements described above for CPU mode, caches, MMUs, architected timers, coherency and system registers apply to all CPUs. All CPUs must enter the kernel in the same exception level. Where the values documented
Documentation/virt/kvm/devices/vcpu.rst +28 -0
@@ -139,6 +139,34 @@ configured values on other VCPUs. Userspace should configure the interrupt numbers on at least one VCPU after creating all VCPUs and before running any VCPUs. +2.2. ATTRIBUTE: KVM_ARM_VCPU_TIMER_PHYS_OFFSET +----------------------------------------- + +:Parameters: in kvm_device_attr.addr the address for the timer offset is a + pointer to a __u64 + +Returns: + + ======= ================================== + -EFAULT Error reading/writing the provided + parameter address + -ENXIO Timer offsetting not implemented + ======= ================================== + +Specifies the guest's counter-timer offset from the host's virtual counter. +The guest's physical counter value is then derived by the following +equation: + + guest_cntpct = host_cntvct - KVM_ARM_VCPU_TIMER_PHYS_OFFSET + +The guest's virtual counter value is derived by the following equation: + + guest_cntvct = host_cntvct - KVM_REG_ARM_TIMER_OFFSET + - KVM_ARM_VCPU_TIMER_PHYS_OFFSET + +KVM does not allow the use of varying offset values for different vCPUs; +the last written offset value will be broadcasted to all vCPUs in a VM. + 3. GROUP: KVM_ARM_VCPU_PVTIME_CTRL ==================================
arch/arm64/include/asm/kvm_asm.h +2 -0
@@ -64,6 +64,7 @@#define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector 18#define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize 19#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 20 +#define __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntpoff 21#ifndef __ASSEMBLY__
@@ -199,6 +200,7 @@ extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa,extern void __kvm_tlb_flush_vmid (struct kvm_s2_mmu *mmu);extern void __kvm_timer_set_cntvoff (u64 cntvoff); +extern void __kvm_timer_set_cntpoff (u64 cntpoff);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);
arch/arm64/include/asm/sysreg.h +2 -0
@@ -586,6 +586,8 @@#define SYS_ICH_LR14_EL2 __SYS__LR8_EL2(6)#define SYS_ICH_LR15_EL2 __SYS__LR8_EL2(7) +#define SYS_CNTPOFF_EL2 sys_reg(3, 4, 14, 0, 6) +/* VHE encodings for architectural EL0/1 system registers */ #define SYS_SCTLR_EL12 sys_reg(3, 5, 1, 0, 0)#define SYS_CPACR_EL12 sys_reg(3, 5, 1, 0, 2)
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -366,6 +366,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_ARM_VCPU_TIMER_CTRL 1#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 +#define KVM_ARM_VCPU_TIMER_PHYS_OFFSET 2#define KVM_ARM_VCPU_PVTIME_CTRL 2#define KVM_ARM_VCPU_PVTIME_IPA 0
arch/arm64/kvm/arch_timer.c +94 -2
158 lines not on this page · raw message
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -0
@@ -68,6 +68,11 @@ static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt)__kvm_timer_set_cntvoff (cpu_reg (host_ctxt,1 )); } +static void handle___kvm_timer_set_cntpoff (struct kvm_cpu_context *host_ctxt) +{ +__kvm_timer_set_cntpoff (cpu_reg (host_ctxt,1 )); +} +static void handle___kvm_enable_ssbs (struct kvm_cpu_context *host_ctxt) {u64 tmp;
@@ -185,6 +190,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_create_private_mapping),HANDLE_FUNC (__pkvm_prot_finalize), + HANDLE_FUNC(__kvm_timer_set_cntpoff), };static void handle_host_hcall (struct kvm_cpu_context *host_ctxt)
arch/arm64/kvm/hyp/nvhe/timer-sr.c +9 -0
@@ -15,6 +15,11 @@ void __kvm_timer_set_cntvoff(u64 cntvoff)write_sysreg (cntvoff, cntvoff_el2); } +void __kvm_timer_set_cntpoff (u64 cntpoff) +{ +write_sysreg_s (cntpoff,SYS_CNTPOFF_EL2 ); +} +/* * Should only be called on non-VHE systems. * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe().
@@ -26,6 +31,8 @@ void __timer_disable_traps(struct kvm_vcpu *vcpu)/* Allow physical timer/counter access for the host */ val =read_sysreg (cnthctl_el2); val |=CNTHCTL_EL1PCTEN |CNTHCTL_EL1PCEN ; +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) + val &= ~CNTHCTL_ECV ;write_sysreg (val, cnthctl_el2); }
@@ -42,6 +49,8 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu)* Physical counter access is allowed */ val =read_sysreg (cnthctl_el2); +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) + val |=CNTHCTL_ECV ; val &= ~CNTHCTL_EL1PCEN ; val |=CNTHCTL_EL1PCTEN ;write_sysreg (val, cnthctl_el2);
arch/arm64/kvm/hyp/vhe/timer-sr.c +5 -0
@@ -10,3 +10,8 @@ void __kvm_timer_set_cntvoff(u64 cntvoff) {write_sysreg (cntvoff, cntvoff_el2); } + +void __kvm_timer_set_cntpoff (u64 cntpoff) +{ +write_sysreg_s (cntpoff,SYS_CNTPOFF_EL2 ); +}
include/clocksource/arm_arch_timer.h +1 -0
@@ -21,6 +21,7 @@#define CNTHCTL_EVNTEN (1 << 2)#define CNTHCTL_EVNTDIR (1 << 3)#define CNTHCTL_EVNTI (0xF << 4) +#define CNTHCTL_ECV (1 << 12)enum arch_timer_reg {ARCH_TIMER_REG_CTRL ,
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
Marc Zyngier
maintainerRe: [PATCH v7 5/7] KVM: arm64: Allow userspace to configure a guest's counter-timer offset
Quoted 168 lines · Oliver Upton
Presently, KVM provides no facilities for correctly migrating a guest that depends on the physical counter-timer. While most guests (barring NV, of course) should not depend on the physical counter-timer, an operator may wish to provide a consistent view of the physical counter-timer across migrations.
Provide userspace with a new vCPU attribute to modify the guest counter-timer offset. Unlike
KVM_REG_ARM_TIMER_OFFSET, this attribute is hidden from the guest's architectural state. The value offsets *both* the virtual and physical counter-timer views for the guest. Only support this attribute on ECV systems as ECV is required for hardware offsetting of the physical counter-timer.Signed-off-by Oliver Upton <[email protected]>Reviewed-by Andrew Jones <[email protected]>--- Documentation/arm64/booting.rst | 7 ++ Documentation/virt/kvm/devices/vcpu.rst | 28 ++++++++ arch/arm64/include/asm/kvm_asm.h | 2 + arch/arm64/include/asm/sysreg.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 96 ++++++++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 6 ++ arch/arm64/kvm/hyp/nvhe/timer-sr.c | 9 +++ arch/arm64/kvm/hyp/vhe/timer-sr.c | 5 ++ include/clocksource/arm_arch_timer.h | 1 + 10 files changed, 155 insertions(+), 2 deletions(-)Documentation/arm64/booting.rst +7
-0@@ -311,6 +311,13 @@ Before jumping into the kernel, the following conditions must be met: - ZCR_EL2.LEN must be initialised to the same value for all CPUs the kernel will execute on. + For CPUs with the Enhanced Counter Virtualization (FEAT_ECV) extension + present with ID_AA64MMFR0_EL1.ECV >= 0x2: + + - if EL3 is present and the kernel is entered at EL2: + + - SCR_EL3.ECVEn (bit 28) must be initialized to 0b1. + The requirements described above for CPU mode, caches, MMUs, architected timers, coherency and system registers apply to all CPUs. All CPUs must enter the kernel in the same exception level. Where the values documentedDocumentation/virt/kvm/devices/vcpu.rst +28
-0@@ -139,6 +139,34 @@ configured values on other VCPUs. Userspace should configure the interrupt numbers on at least one VCPU after creating all VCPUs and before running any VCPUs. +2.2. ATTRIBUTE: KVM_ARM_VCPU_TIMER_PHYS_OFFSET +----------------------------------------- + +:Parameters: in kvm_device_attr.addr the address for the timer offset is a + pointer to a __u64 + +Returns: + + ======= ================================== + -EFAULT Error reading/writing the provided + parameter address + -ENXIO Timer offsetting not implemented + ======= ================================== + +Specifies the guest's counter-timer offset from the host's virtual counter. +The guest's physical counter value is then derived by the following +equation: + + guest_cntpct = host_cntvct - KVM_ARM_VCPU_TIMER_PHYS_OFFSET + +The guest's virtual counter value is derived by the following equation: + + guest_cntvct = host_cntvct - KVM_REG_ARM_TIMER_OFFSET + - KVM_ARM_VCPU_TIMER_PHYS_OFFSET + +KVM does not allow the use of varying offset values for different vCPUs; +the last written offset value will be broadcasted to all vCPUs in a VM. + 3. GROUP: KVM_ARM_VCPU_PVTIME_CTRL ==================================arch/arm64/include/asm/kvm_asm.h +2
-0@@ -64,6 +64,7 @@#define __KVM_HOST_SMCCC_FUNC___pkvm_cpu_set_vector 18#define __KVM_HOST_SMCCC_FUNC___pkvm_prot_finalize 19#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 20 +#define __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntpoff 21#ifndef __ASSEMBLY__@@ -199,6 +200,7 @@ extern void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu, phys_addr_t ipa,extern void __kvm_tlb_flush_vmid (struct kvm_s2_mmu *mmu);extern void __kvm_timer_set_cntvoff (u64 cntvoff); +extern void __kvm_timer_set_cntpoff (u64 cntpoff);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);arch/arm64/include/asm/sysreg.h +2
-0@@ -586,6 +586,8 @@#define SYS_ICH_LR14_EL2 __SYS__LR8_EL2(6)#define SYS_ICH_LR15_EL2 __SYS__LR8_EL2(7) +#define SYS_CNTPOFF_EL2 sys_reg(3, 4, 14, 0, 6) +/* VHE encodings for architectural EL0/1 system registers */ #define SYS_SCTLR_EL12 sys_reg(3, 5, 1, 0, 0)#define SYS_CPACR_EL12 sys_reg(3, 5, 1, 0, 2)arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -366,6 +366,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_ARM_VCPU_TIMER_CTRL 1#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 +#define KVM_ARM_VCPU_TIMER_PHYS_OFFSET 2#define KVM_ARM_VCPU_PVTIME_CTRL 2#define KVM_ARM_VCPU_PVTIME_IPA 0arch/arm64/kvm/arch_timer.c +9
-0@@ -86,8 +86,11 @@ static u64 timer_get_offset(struct arch_timer_context *ctxt) {switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : +case TIMER_PTIMER :return ctxt->host_offset;default : +WARN_ONCE (1 ,"unrecognized timer %ld\n" , +arch_timer_ctx_index (ctxt));return 0 ; } }@@ -140,6 +143,7 @@ static void timer_set_offset(struct arch_timer_context *ctxt, u64 offset) {switch (arch_timer_ctx_index (ctxt)) {case TIMER_VTIMER : +case TIMER_PTIMER : ctxt->host_offset = offset;break ;default :@@ -568,6 +572,11 @@ static void set_cntvoff(u64 cntvoff)kvm_call_hyp (__kvm_timer_set_cntvoff, cntvoff); } +static void set_cntpoff (u64 cntpoff) +{ +kvm_call_hyp (__kvm_timer_set_cntpoff, cntpoff); +} +static inline void set_timer_irq_phys_active (struct arch_timer_context *ctx,bool active) {int r;
arch/arm64/kvm/arch_timer.c void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) · line 656
Quoted 5 lines
arch/arm64/kvm/arch_timer.c +2
-0@@ -643,6 +652,8 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu) }set_cntvoff (timer_get_offset (map.direct_vtimer)); +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) +set_cntpoff (timer_get_offset (vcpu_ptimer (vcpu)));
You probably want to change vcpu_ptimer(vcpu) to map.direct_ptimer. This will work for NV, and won't impact nVHE (see below for the details).
Quoted 179 lines
arch/arm64/kvm/arch_timer.c +83
-2kvm_timer_unblocking (vcpu);@@ -810,6 +821,22 @@ static void update_vtimer_cntvoff(struct kvm_vcpu *vcpu, u64 cntvoff)mutex_unlock (&kvm->lock); } +static void update_ptimer_cntpoff (struct kvm_vcpu *vcpu,u64 cntpoff) +{ +struct kvm *kvm = vcpu->kvm; +u64 cntvoff; + +mutex_lock (&kvm->lock); + +/* adjustments to the physical offset also affect vtimer */ + cntvoff =timer_get_offset (vcpu_vtimer (vcpu)); + cntvoff += cntpoff -timer_get_offset (vcpu_ptimer (vcpu)); + +update_timer_offset (vcpu,TIMER_PTIMER , cntpoff, false); +update_timer_offset (vcpu,TIMER_VTIMER , cntvoff, false); +mutex_unlock (&kvm->lock); +} +void kvm_timer_vcpu_init (struct kvm_vcpu *vcpu) {struct arch_timer_cpu *timer =vcpu_timer (vcpu);@@ -1346,6 +1373,9 @@ void kvm_timer_init_vhe(void) val =read_sysreg (cnthctl_el2); val |= (CNTHCTL_EL1PCEN << cnthctl_shift); val |= (CNTHCTL_EL1PCTEN << cnthctl_shift); + +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) + val |=CNTHCTL_ECV ;write_sysreg (val, cnthctl_el2); }@@ -1360,7 +1390,8 @@ static void set_timer_irqs(struct kvm *kvm, int vtimer_irq, int ptimer_irq) } } -int kvm_arm_timer_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) +static int kvm_arm_timer_set_attr_irq (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) {int __user *uaddr = (int __user *)(long )attr->addr;struct arch_timer_context *vtimer =vcpu_vtimer (vcpu);@@ -1393,7 +1424,37 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return 0 ; } -int kvm_arm_timer_get_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) +static int kvm_arm_timer_set_attr_offset (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 offset; + +if (!cpus_have_final_cap (ARM64_HAS_ECV2 )) +return -ENXIO ; + +if (get_user (offset, uaddr)) +return -EFAULT ; + +update_ptimer_cntpoff (vcpu, offset); +return 0 ; +} + +int kvm_arm_timer_set_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) +{ +switch (attr->attr) { +case KVM_ARM_VCPU_TIMER_IRQ_VTIMER : +case KVM_ARM_VCPU_TIMER_IRQ_PTIMER : +return kvm_arm_timer_set_attr_irq (vcpu, attr); +case KVM_ARM_VCPU_TIMER_PHYS_OFFSET : +return kvm_arm_timer_set_attr_offset (vcpu, attr); +default : +return -ENXIO ; + } +} + +static int kvm_arm_timer_get_attr_irq (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) {int __user *uaddr = (int __user *)(long )attr->addr;struct arch_timer_context *timer;@@ -1414,12 +1475,43 @@ int kvm_arm_timer_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)return put_user (irq, uaddr); } +static int kvm_arm_timer_get_attr_offset (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +u64 __user *uaddr = (u64 __user *)(long )attr->addr; +u64 offset; + +if (!cpus_have_final_cap (ARM64_HAS_ECV2 )) +return -ENXIO ; + + offset =timer_get_offset (vcpu_ptimer (vcpu)); +return put_user (offset, uaddr); +} + +int kvm_arm_timer_get_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +switch (attr->attr) { +case KVM_ARM_VCPU_TIMER_IRQ_VTIMER : +case KVM_ARM_VCPU_TIMER_IRQ_PTIMER : +return kvm_arm_timer_get_attr_irq (vcpu, attr); +case KVM_ARM_VCPU_TIMER_PHYS_OFFSET : +return kvm_arm_timer_get_attr_offset (vcpu, attr); +default : +return -ENXIO ; + } +} +int kvm_arm_timer_has_attr (struct kvm_vcpu *vcpu,struct kvm_device_attr *attr) {switch (attr->attr) {case KVM_ARM_VCPU_TIMER_IRQ_VTIMER :case KVM_ARM_VCPU_TIMER_IRQ_PTIMER :return 0 ; +case KVM_ARM_VCPU_TIMER_PHYS_OFFSET : +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) +return 0 ; +break ; }return -ENXIO ;arch/arm64/kvm/hyp/nvhe/hyp-main.c +6
-0@@ -68,6 +68,11 @@ static void handle___kvm_timer_set_cntvoff(struct kvm_cpu_context *host_ctxt)__kvm_timer_set_cntvoff (cpu_reg (host_ctxt,1 )); } +static void handle___kvm_timer_set_cntpoff (struct kvm_cpu_context *host_ctxt) +{ +__kvm_timer_set_cntpoff (cpu_reg (host_ctxt,1 )); +} +static void handle___kvm_enable_ssbs (struct kvm_cpu_context *host_ctxt) {u64 tmp;@@ -185,6 +190,7 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__pkvm_host_share_hyp),HANDLE_FUNC (__pkvm_create_private_mapping),HANDLE_FUNC (__pkvm_prot_finalize), + HANDLE_FUNC(__kvm_timer_set_cntpoff), };static void handle_host_hcall (struct kvm_cpu_context *host_ctxt)arch/arm64/kvm/hyp/nvhe/timer-sr.c +7
-0@@ -15,6 +15,11 @@ void __kvm_timer_set_cntvoff(u64 cntvoff)write_sysreg (cntvoff, cntvoff_el2); } +void __kvm_timer_set_cntpoff (u64 cntpoff) +{ +write_sysreg_s (cntpoff,SYS_CNTPOFF_EL2 ); +} +/* * Should only be called on non-VHE systems. * VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe(). @@ -26,6 +31,8 @@ void __timer_disable_traps(struct kvm_vcpu *vcpu)/* Allow physical timer/counter access for the host */ val =read_sysreg (cnthctl_el2); val |=CNTHCTL_EL1PCTEN |CNTHCTL_EL1PCEN ; +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) + val &= ~CNTHCTL_ECV ;write_sysreg (val, cnthctl_el2); }
arch/arm64/kvm/hyp/nvhe/timer-sr.c void __timer_enable_traps(struct kvm_vcpu *vcpu) · line 53
Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/timer-sr.c +2
-0@@ -42,6 +49,8 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu)* Physical counter access is allowed */ val =read_sysreg (cnthctl_el2); +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) + val |=CNTHCTL_ECV ;
There is something here that has been nagging me for some time, and I just had an epiphany: ECV2 is fundamentally incompatible with the way we use the physical timer in nVHE.
The main reason is that we never use the EL2 timer like VHE does, but instead stick with the EL1 timer. Which is fine, since we only provide access to the *counter* in a guest.
However, with ECV2, we allow the counter to be offset to an arbitrary value. On the face of it, it should be OK, as only the guest can access the counter while the offset is active. However, there is a small nugget in the spec that says:
<quote>
The CNTPOFF_EL2 offset applies to:
* Direct reads of the physical counter from EL0 or EL1.
* Indirect reads of the physical counter by the EL1 physical timer. </quote>
and this last point is absolutely critical. The timer itself can observe the offset, which means that depending on how the comparator is implemented, the offset could influence the firing of the timer that the *host* uses. And that's clearly not acceptable.
I can see two ways to work around this:
- we stick to the tried and trusted SW emulation for the counter on nVHE,
- we use the EL2 timer as an offload for the host EL1 timer and switch it on entry/exit. This requires some additional setup code (the interrupt needs to be enabled), and the overhead of the offload is currently unknown.
My advise would be to stick to the emulation until someone shouts, which is pretty unlikely. We can gate the detection of ECV2 on VHE for the time being, and drop the setup code.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v7 6/7 KVM: arm64: Configure timer traps in vcpu_load() for VHE
In reply to Oliver UptonIn preparation for emulated physical counter-timer offsetting, configure traps on every vcpu_load() for VHE systems. As before, these trap settings do not affect host userspace, and are only active for the guest.
--- arch/arm64/kvm/arch_timer.c | 10 +++++++--- arch/arm64/kvm/arm.c | 4 +--- include/kvm/arm_arch_timer.h | 2 -- 3 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/kvm/arch_timer.c +7 -3
@@ -51,6 +51,7 @@ static void kvm_arm_timer_write(struct kvm_vcpu *vcpu,static u64 kvm_arm_timer_read (struct kvm_vcpu *vcpu,struct arch_timer_context *timer,enum kvm_arch_timer_regs treg); +static void kvm_timer_enable_traps_vhe (void );u32 timer_get_ctl (struct arch_timer_context *ctxt) {
@@ -663,6 +664,9 @@ void kvm_timer_vcpu_load(struct kvm_vcpu *vcpu)if (map.emul_ptimer)timer_emulate (map.emul_ptimer); + +if (has_vhe ()) +kvm_timer_enable_traps_vhe (); }bool kvm_timer_should_notify_user (struct kvm_vcpu *vcpu)
@@ -1355,12 +1359,12 @@ int kvm_timer_enable(struct kvm_vcpu *vcpu) }/* -* On VHE system, we only need to configure the EL2 timer trap register once, -* not for every world switch. +* On VHE system, we only need to configure the EL2 timer trap register on +* vcpu_load(), but not every world switch into the guest. * The host kernel runs at EL2 with HCR_EL2.TGE == 1, * and this makes those bits have no effect for the host kernel execution. */ -void kvm_timer_init_vhe (void ) +static void kvm_timer_enable_traps_vhe (void ) {/* When HCR_EL2.E2H ==1, EL1PCEN and EL1PCTEN are shifted by 10 */ u32 cnthctl_shift =10 ;
arch/arm64/kvm/arm.c +1 -3
@@ -1559,9 +1559,7 @@ static void cpu_hyp_reinit(void)cpu_hyp_reset (); -if (is_kernel_in_hyp_mode ()) -kvm_timer_init_vhe (); -else +if (!is_kernel_in_hyp_mode ())cpu_init_hyp_mode ();cpu_set_hyp_vector ();
include/kvm/arm_arch_timer.h +0 -2
@@ -87,8 +87,6 @@ u64 kvm_phys_timer_read(void);void kvm_timer_vcpu_load (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_put (struct kvm_vcpu *vcpu); -void kvm_timer_init_vhe (void ); -bool kvm_arch_timer_get_input_level (int vintid);#define vcpu_timer (v) (&(v)->arch.timer_cpu)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH v7 7/7 KVM: arm64: Emulate physical counter offsetting on non-ECV systems
In reply to Oliver UptonUnfortunately, ECV hasn't yet arrived in any tangible hardware. At the same time, controlling the guest view of the physical counter-timer is useful. Support guest counter-timer offsetting on non-ECV systems by trapping guest accesses to the physical counter-timer. Emulate reads of the physical counter in the fast exit path.
--- arch/arm64/include/asm/sysreg.h | 2 ++ arch/arm64/kvm/arch_timer.c | 47 +++++++++++++------------ arch/arm64/kvm/hyp/include/hyp/switch.h | 32 +++++++++++++++++ arch/arm64/kvm/hyp/nvhe/timer-sr.c | 11 ++++-- include/kvm/arm_arch_timer.h | 3 ++ 5 files changed, 71 insertions(+), 24 deletions(-)
arch/arm64/include/asm/sysreg.h +2 -0
@@ -505,6 +505,8 @@#define SYS_AMEVCNTR0_MEM_STALL SYS_AMEVCNTR0_EL0(3)#define SYS_CNTFRQ_EL0 sys_reg(3, 3, 14, 0, 0) +#define SYS_CNTPCT_EL0 sys_reg(3, 3, 14, 0, 1) +#define SYS_CNTPCTSS_EL0 sys_reg(3, 3, 14, 0, 5)#define SYS_CNTP_TVAL_EL0 sys_reg(3, 3, 14, 2, 0)#define SYS_CNTP_CTL_EL0 sys_reg(3, 3, 14, 2, 1)
arch/arm64/kvm/arch_timer.c +25 -22
98 lines not on this page · raw message
arch/arm64/kvm/hyp/include/hyp/switch.h +32 -0
@@ -15,6 +15,7 @@#include <linux/jump_label.h> #include <uapi/linux/psci.h> +#include <kvm/arm_arch_timer.h> #include <kvm/arm_psci.h> #include <asm/barrier.h>
@@ -405,6 +406,34 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)return true; } +static inline u64 __timer_read_cntpct (struct kvm_vcpu *vcpu) +{ +return __arch_counter_get_cntpct () -vcpu_ptimer (vcpu)->host_offset; +} + +static inline bool __hyp_handle_counter (struct kvm_vcpu *vcpu) +{ +u32 sysreg; +int rt; +u64 rv; + +if (cpus_have_final_cap (ARM64_HAS_ECV2 )) +return false; + +if (kvm_vcpu_trap_get_class (vcpu) != ESR_ELx_EC_SYS64) +return false; + + sysreg =esr_sys64_to_sysreg (kvm_vcpu_get_esr (vcpu)); +if (sysreg !=SYS_CNTPCT_EL0 && sysreg !=SYS_CNTPCTSS_EL0 ) +return false; + + rt =kvm_vcpu_sys_get_rt (vcpu); + rv =__timer_read_cntpct (vcpu); +vcpu_set_reg (vcpu, rt, rv); +__kvm_skip_instr (vcpu); +return true; +} +/* * Return true when we were able to fixup the guest exit and should return to * the guest, false when we should restore the host state and return to the
@@ -439,6 +468,9 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)if (*exit_code !=ARM_EXCEPTION_TRAP ) goto exit; +if (__hyp_handle_counter (vcpu)) + goto guest; +if (cpus_have_final_cap (ARM64_WORKAROUND_CAVIUM_TX2_219_TVM ) &&kvm_vcpu_trap_get_class (vcpu) == ESR_ELx_EC_SYS64 &&handle_tx2_tvm (vcpu))
arch/arm64/kvm/hyp/nvhe/timer-sr.c +9 -2
@@ -46,12 +46,19 @@ void __timer_enable_traps(struct kvm_vcpu *vcpu)/* * Disallow physical timer access for the guest -* Physical counter access is allowed */ val =read_sysreg (cnthctl_el2);if (cpus_have_final_cap (ARM64_HAS_ECV2 )) val |=CNTHCTL_ECV ; val &= ~CNTHCTL_EL1PCEN ; - val |=CNTHCTL_EL1PCTEN ; + +/* +* Disallow physical counter access for the guest if offsetting is +* requested on a non-ECV system. +*/ +if (ptimer_emulation_required (vcpu)) + val &= ~CNTHCTL_EL1PCTEN ; +else + val |=CNTHCTL_EL1PCTEN ;write_sysreg (val, cnthctl_el2); }
include/kvm/arm_arch_timer.h +3 -0
@@ -96,6 +96,9 @@ bool kvm_arch_timer_get_input_level(int vintid);#define arch_timer_ctx_index (ctx) ((ctx) - vcpu_timer((ctx)->vcpu)->timers) +#define ptimer_emulation_required (v) \ + (!cpus_have_final_cap(ARM64_HAS_ECV2) && vcpu_ptimer(v)->host_offset) +u64 kvm_arm_timer_read_sysreg (struct kvm_vcpu *vcpu,enum kvm_arch_timers tmr,enum kvm_arch_timer_regs treg);
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog