]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address
authorMarc Zyngier <maz@kernel.org>
Wed, 14 May 2025 10:34:55 +0000 (11:34 +0100)
committerMarc Zyngier <maz@kernel.org>
Mon, 19 May 2025 07:01:19 +0000 (08:01 +0100)
Since we now have a way to map the guest's VNCR_EL2 on the host,
we can point the host's VNCR_EL2 to it and go full circle!

Note that we unconditionally assign the fixmap to VNCR_EL2,
irrespective of the guest's version being mapped or not. We want
to take a fault on first access, so the fixmap either contains
something guranteed to be either invalid or a guest mapping.

Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
Link: https://lore.kernel.org/r/20250514103501.2225951-13-maz@kernel.org
Signed-off-by: Marc Zyngier <maz@kernel.org>
arch/arm64/kvm/hyp/vhe/switch.c

index 220dee8a45e0d6ac3951dd85eb1dc3acb01b34f8..5eaff1ae32b29e8d5bb08ad6eedfb04fa09c21cd 100644 (file)
@@ -48,6 +48,7 @@ DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
 
 static u64 __compute_hcr(struct kvm_vcpu *vcpu)
 {
+       u64 guest_hcr = __vcpu_sys_reg(vcpu, HCR_EL2);
        u64 hcr = vcpu->arch.hcr_el2;
 
        if (!vcpu_has_nv(vcpu))
@@ -70,9 +71,23 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)
                write_sysreg_s(vcpu->arch.ctxt.vncr_array, SYS_VNCR_EL2);
        } else {
                host_data_clear_flag(VCPU_IN_HYP_CONTEXT);
+
+               if (guest_hcr & HCR_NV) {
+                       u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id()));
+
+                       /* Inherit the low bits from the actual register */
+                       va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0);
+                       write_sysreg_s(va, SYS_VNCR_EL2);
+
+                       /* Force NV2 in case the guest is forgetful... */
+                       guest_hcr |= HCR_NV2;
+               }
        }
 
-       return hcr | (__vcpu_sys_reg(vcpu, HCR_EL2) & ~NV_HCR_GUEST_EXCLUDE);
+       BUG_ON(host_data_test_flag(VCPU_IN_HYP_CONTEXT) &&
+              host_data_test_flag(L1_VNCR_MAPPED));
+
+       return hcr | (guest_hcr & ~NV_HCR_GUEST_EXCLUDE);
 }
 
 static void __activate_cptr_traps(struct kvm_vcpu *vcpu)