Skip to contentSkip to conversation

KVM: arm64: Fixes for 'writable' ID registers in protected mode

PATCH Applied Oliver Upton 3 participants 8 messages Mar 5, 2025 – Mar 6, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover KVM: arm64: Fixes for 'writable' ID registers in protected mode

Address a few issues with ID register handling in protected mode:

- CTR_EL0 isn't trapped on FEAT_EVT systems if userspace changed the guest value

- VPIDR_EL2 is set to 0 if userspace enabled writable 'implementation ID' registers

- Fix in capability documentation

Oliver Upton (3):
KVM: arm64: Copy guest CTR_EL0 into hyp VM
KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable
KVM: arm64: Fix documentation for KVM_CAP_ARM_WRITABLE_IMP_ID_REGS

 Documentation/virt/kvm/api.rst | 2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

base-commit: 164f95739b6893716add9439cf765edd5254d0f6

Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 KVM: arm64: Copy guest CTR_EL0 into hyp VM

Since commit 2843cae26644 ("KVM: arm64: Treat CTR_EL0 as a VM feature ID register") KVM has allowed userspace to configure the VM-wide view of CTR_EL0, falling back to trap-n-emulate if the value doesn't match hardware. It appears that this has worked by chance in protected-mode for some time, and on systems with FEAT_EVT protected-mode unconditionally sets TID4 (i.e. TID2 traps sans CTR_EL0).

Forward the guest CTR_EL0 value through to the hyp VM and align the TID2/TID4 configuration with the non-protected setup.

Fixes 2843cae26644 ("KVM: arm64: Treat CTR_EL0 as a VM feature ID register")
---
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/pkvm.c +3 -1
index 3927fe52a3dd..2e2975248cd3 100644
@@ -46,7 +46,8 @@ static void pkvm_vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 		vcpu->arch.hcr_el2 |= HCR_FWB;
 
 	if (cpus_have_final_cap(ARM64_HAS_EVT) &&
-	    !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE))
+	    !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE) &&
+	    kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0) == read_cpuid(CTR_EL0))
 		vcpu->arch.hcr_el2 |= HCR_TID4;
 	else
 		vcpu->arch.hcr_el2 |= HCR_TID2;
@@ -374,6 +375,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 	hyp_vm->kvm.arch.pkvm.enabled = READ_ONCE(host_kvm->arch.pkvm.enabled);
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
+	hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0;
 }
 
 static void pkvm_vcpu_init_sve(struct pkvm_hyp_vcpu *hyp_vcpu, struct kvm_vcpu *host_vcpu)
Signature · 1 line
2.39.5
maintainer
#2 lore raw

Re: [PATCH 1/3] KVM: arm64: Copy guest CTR_EL0 into hyp VM

Quoted 32 lines · Oliver Upton

Since commit 2843cae26644 ("KVM: arm64: Treat CTR_EL0 as a VM feature ID register") KVM has allowed userspace to configure the VM-wide view of CTR_EL0, falling back to trap-n-emulate if the value doesn't match hardware. It appears that this has worked by chance in protected-mode for some time, and on systems with FEAT_EVT protected-mode unconditionally sets TID4 (i.e. TID2 traps sans CTR_EL0).

Forward the guest CTR_EL0 value through to the hyp VM and align the TID2/TID4 configuration with the non-protected setup.

Fixes 2843cae26644 ("KVM: arm64: Treat CTR_EL0 as a VM feature ID register")
---
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/pkvm.c +2 -1
index 3927fe52a3dd..2e2975248cd3 100644
@@ -46,7 +46,8 @@ static void pkvm_vcpu_reset_hcr(struct kvm_vcpu *vcpu)
 		vcpu->arch.hcr_el2 |= HCR_FWB;
 
 	if (cpus_have_final_cap(ARM64_HAS_EVT) &&
-	    !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE))
+	    !cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE) &&
+	    kvm_read_vm_id_reg(vcpu->kvm, SYS_CTR_EL0) == read_cpuid(CTR_EL0))
 		vcpu->arch.hcr_el2 |= HCR_TID4;
 	else
 		vcpu->arch.hcr_el2 |= HCR_TID2;

arch/arm64/kvm/hyp/nvhe/pkvm.c static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, · line 378

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -0
@@ -374,6 +375,7 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 	hyp_vm->kvm.arch.pkvm.enabled = READ_ONCE(host_kvm->arch.pkvm.enabled);
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
+	hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0;

Maybe it'd be better to stick this into pkvm_init_features_from_host() in order to keep all the "inherit stuff from host" together?

Same thing for the following patch.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw

PATCH 2/3 KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable

In reply to Oliver Upton

KVM recently added a capability that allows userspace to override the 'implementation ID' registers presented to the VM. MIDR_EL1 is a special example, where the hypervisor can directly set the value when read from EL1 using VPIDR_EL2.

Copy the VM-wide value for MIDR_EL1 into the hyp VM for non-protected guests when the capability is enabled so VPIDR_EL2 gets set up correctly.

Fixes 3adaee783061 ("KVM: arm64: Allow userspace to change the implementation ID registers")
---
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 3 +++
 1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/pkvm.c +3 -0
index 2e2975248cd3..afc1b5c403c9 100644
@@ -376,6 +376,9 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
 	hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0;
+
+	if (test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &hyp_vm->kvm.arch.flags))
+		hyp_vm->kvm.arch.midr_el1 = host_kvm->arch.midr_el1;
 }
 
 static void pkvm_vcpu_init_sve(struct pkvm_hyp_vcpu *hyp_vcpu, struct kvm_vcpu *host_vcpu)
Signature · 1 line
2.39.5
#4 lore raw

Re: [PATCH 2/3] KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable

Quoted 22 lines · Oliver Upton

KVM recently added a capability that allows userspace to override the 'implementation ID' registers presented to the VM. MIDR_EL1 is a special example, where the hypervisor can directly set the value when read from EL1 using VPIDR_EL2.

Copy the VM-wide value for MIDR_EL1 into the hyp VM for non-protected guests when the capability is enabled so VPIDR_EL2 gets set up correctly.

Fixes 3adaee783061 ("KVM: arm64: Allow userspace to change the implementation ID registers")

---
arch/arm64/kvm/hyp/nvhe/pkvm.c | 3 +++

1 file changed, 3 insertions(+)
arch/arm64/kvm/hyp/nvhe/pkvm.c +0 -0
index 2e2975248cd3..afc1b5c403c9 100644

arch/arm64/kvm/hyp/nvhe/pkvm.c static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm, · line 382

Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +3 -0
@@ -376,6 +376,9 @@ static void init_pkvm_hyp_vm(struct kvm *host_kvm, struct pkvm_hyp_vm *hyp_vm,
 	hyp_vm->kvm.arch.flags = 0;
 	pkvm_init_features_from_host(hyp_vm, host_kvm);
 	hyp_vm->kvm.arch.ctr_el0 = host_kvm->arch.ctr_el0;
+
+	if (test_bit(KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS, &hyp_vm->kvm.arch.flags))
+		hyp_vm->kvm.arch.midr_el1 = host_kvm->arch.midr_el1;
 }

Oh, I didn't even think that all that's missing was to update the trusted kvm instance ... I was looking on where to set VPIDR_EL2 for pkvm.

Thanks for the fix, I guess I'll have some more code reading todo...

Sebastian

#5 lore raw

PATCH 3/3 KVM: arm64: Fix documentation for KVM_CAP_ARM_WRITABLE_IMP_ID_REGS

In reply to Oliver Upton

The capability actually fails with EINVAL if vCPUs have already been created.

---
 Documentation/virt/kvm/api.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +1 -1
index 56f3dcdc4477..3468a2a7de6b 100644
@@ -8264,7 +8264,7 @@ take care to differentiate between these cases.
 :Architectures: arm64
 :Target: VM
 :Parameters: None
-:Returns: 0 on success, -EBUSY if vCPUs have been created before enabling this
+:Returns: 0 on success, -EINVAL if vCPUs have been created before enabling this
           capability.
 
 This capability changes the behavior of the registers that identify a PE
Signature · 1 line
2.39.5
maintainer Reviewed-by on the v1 series
#6 lore raw
In reply to Oliver Upton
Quoted 11 lines · Oliver Upton

Address a few issues with ID register handling in protected mode:

- CTR_EL0 isn't trapped on FEAT_EVT systems if userspace changed the guest value

- VPIDR_EL2 is set to 0 if userspace enabled writable 'implementation ID' registers

- Fix in capability documentation

Quoted 8 lines

Oliver Upton (3):
KVM: arm64: Copy guest CTR_EL0 into hyp VM
KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable
KVM: arm64: Fix documentation for KVM_CAP_ARM_WRITABLE_IMP_ID_REGS

 Documentation/virt/kvm/api.rst | 2 +-
 arch/arm64/kvm/hyp/nvhe/pkvm.c | 7 ++++++-
 2 files changed, 7 insertions(+), 2 deletions(-)

Irrespective of the comments for the first two patches:

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
author Applied
#7 lore raw
In reply to Oliver Upton
Quoted 9 lines · Oliver Upton

Address a few issues with ID register handling in protected mode:

- CTR_EL0 isn't trapped on FEAT_EVT systems if userspace changed the guest value

- VPIDR_EL2 is set to 0 if userspace enabled writable 'implementation ID' registers

[...]

Marc, I fixed it in post :)

Applied to next, thanks!

[1/3] KVM: arm64: Copy guest CTR_EL0 into hyp VM

      https://git.kernel.org/kvmarm/kvmarm/c/9d9122736433
[2/3] KVM: arm64: Copy MIDR_EL1 into hyp VM when it is writable
      https://git.kernel.org/kvmarm/kvmarm/c/03e1b89d051f
[3/3] KVM: arm64: Fix documentation for KVM_CAP_ARM_WRITABLE_IMP_ID_REGS
      https://git.kernel.org/kvmarm/kvmarm/c/5980a6937012

--
Best,
Oliver