From: Tom Lendacky <[email protected]>

The guest FPU is automatically restored on VMRUN and saved on VMEXIT by
the hardware, so there is no reason to do this in KVM.

Signed-off-by: Tom Lendacky <[email protected]>
---
 arch/x86/kvm/svm/svm.c |  8 ++++++--
 arch/x86/kvm/x86.c     | 18 ++++++++++++++----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index b35c2de1130c..48699c41b62a 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -3682,7 +3682,9 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu 
*vcpu)
                svm_set_dr6(svm, DR6_FIXED_1 | DR6_RTM);
 
        clgi();
-       kvm_load_guest_xsave_state(vcpu);
+
+       if (!sev_es_guest(svm->vcpu.kvm))
+               kvm_load_guest_xsave_state(vcpu);
 
        if (lapic_in_kernel(vcpu) &&
                vcpu->arch.apic->lapic_timer.timer_advance_ns)
@@ -3728,7 +3730,9 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu 
*vcpu)
        if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
                kvm_before_interrupt(&svm->vcpu);
 
-       kvm_load_host_xsave_state(vcpu);
+       if (!sev_es_guest(svm->vcpu.kvm))
+               kvm_load_host_xsave_state(vcpu);
+
        stgi();
 
        /* Any pending NMI will happen here */
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 76efe70cd635..a53e24c1c5d1 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8896,9 +8896,14 @@ static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
 
        kvm_save_current_fpu(vcpu->arch.user_fpu);
 
-       /* PKRU is separately restored in kvm_x86_ops.run.  */
-       __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
-                               ~XFEATURE_MASK_PKRU);
+       /*
+        * An encrypted save area means that the guest state can't be
+        * set by the hypervisor, so skip trying to set it.
+        */
+       if (!vcpu->arch.vmsa_encrypted)
+               /* PKRU is separately restored in kvm_x86_ops.run. */
+               __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
+                                       ~XFEATURE_MASK_PKRU);
 
        fpregs_mark_activate();
        fpregs_unlock();
@@ -8911,7 +8916,12 @@ static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
 {
        fpregs_lock();
 
-       kvm_save_current_fpu(vcpu->arch.guest_fpu);
+       /*
+        * An encrypted save area means that the guest state can't be
+        * read/saved by the hypervisor, so skip trying to save it.
+        */
+       if (!vcpu->arch.vmsa_encrypted)
+               kvm_save_current_fpu(vcpu->arch.guest_fpu);
 
        copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
 
-- 
2.28.0

Reply via email to