]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
KVM: arm64: Calculate cptr_el2 traps on activating traps
authorFuad Tabba <tabba@google.com>
Tue, 8 Apr 2025 18:10:05 +0000 (19:10 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 2 May 2025 05:44:17 +0000 (07:44 +0200)
[ Upstream commit 2fd5b4b0e7b440602455b79977bfa64dea101e6c ]

Similar to VHE, calculate the value of cptr_el2 from scratch on
activate traps. This removes the need to store cptr_el2 in every
vcpu structure. Moreover, some traps, such as whether the guest
owns the fp registers, need to be set on every vcpu run.

Reported-by: James Clark <james.clark@linaro.org>
Fixes: 5294afdbf45a ("KVM: arm64: Exclude FP ownership from kvm_vcpu_arch")
Signed-off-by: Fuad Tabba <tabba@google.com>
Link: https://lore.kernel.org/r/20241216105057.579031-13-tabba@google.com
Signed-off-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/arm.c
arch/arm64/kvm/hyp/nvhe/switch.c

index 3d4e2396a2d7f7118531c1124ed4e0e9391174ba..2e0952134e2e1d191912958f131eb59388fdf718 100644 (file)
@@ -301,7 +301,6 @@ struct kvm_vcpu_arch {
        /* Values of trap registers for the guest. */
        u64 hcr_el2;
        u64 mdcr_el2;
-       u64 cptr_el2;
 
        /* Values of trap registers for the host before guest entry. */
        u64 mdcr_el2_host;
index 9ded5443de4875c31abafb0a60c3695de801b9cc..5ca8782edb96017c6744aafd19c64059f74c062a 100644 (file)
@@ -1138,7 +1138,6 @@ static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
        }
 
        vcpu_reset_hcr(vcpu);
-       vcpu->arch.cptr_el2 = CPTR_EL2_DEFAULT;
 
        /*
         * Handle the "start in power-off" case.
index 4db5409f40c4f530250b55cf554675040fc6e89d..c0885197f2a55a8cc692a404627a0a46c2cf9316 100644 (file)
@@ -34,21 +34,38 @@ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data);
 DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
 DEFINE_PER_CPU(unsigned long, kvm_hyp_vector);
 
-static void __activate_traps(struct kvm_vcpu *vcpu)
+static bool guest_owns_fp_regs(struct kvm_vcpu *vcpu)
 {
-       u64 val;
+       return vcpu->arch.flags & KVM_ARM64_FP_ENABLED;
+}
 
-       ___activate_traps(vcpu);
-       __activate_traps_common(vcpu);
+static void __activate_cptr_traps(struct kvm_vcpu *vcpu)
+{
+       u64 val = CPTR_EL2_TAM; /* Same bit irrespective of E2H */
 
-       val = vcpu->arch.cptr_el2;
-       val |= CPTR_EL2_TTA | CPTR_EL2_TAM;
-       if (!update_fp_enabled(vcpu)) {
-               val |= CPTR_EL2_TFP | CPTR_EL2_TZ;
-               __activate_traps_fpsimd32(vcpu);
+       /* !hVHE case upstream */
+       if (1) {
+               val |= CPTR_EL2_TTA | CPTR_NVHE_EL2_RES1;
+
+               if (!vcpu_has_sve(vcpu) || !guest_owns_fp_regs(vcpu))
+                       val |= CPTR_EL2_TZ;
+
+               if (!guest_owns_fp_regs(vcpu))
+                       val |= CPTR_EL2_TFP;
        }
 
+       if (!guest_owns_fp_regs(vcpu))
+               __activate_traps_fpsimd32(vcpu);
+
        write_sysreg(val, cptr_el2);
+}
+
+static void __activate_traps(struct kvm_vcpu *vcpu)
+{
+       ___activate_traps(vcpu);
+       __activate_traps_common(vcpu);
+       __activate_cptr_traps(vcpu);
+
        write_sysreg(__this_cpu_read(kvm_hyp_vector), vbar_el2);
 
        if (cpus_have_final_cap(ARM64_WORKAROUND_SPECULATIVE_AT)) {