Skip to content

Commit 6bfa6d8

Browse files
yamahatabonzini
authored andcommitted
KVM: TDX: restore host xsave state when exit from the guest TD
On exiting from the guest TD, xsave state is clobbered; restore it. Do not use kvm_load_host_xsave_state(), as it relies on vcpu->arch to find out whether other KVM_RUN code has loaded guest state into XCR0/PKRU/XSS or not. In the case of TDX, the exit values are known independent of the guest CR0 and CR4, and in fact the latter are not available. Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Message-ID: <20250129095902.16391-8-adrian.hunter@intel.com> [Rewrite to not use kvm_load_host_xsave_state. - Paolo] Reviewed-by: Xiayao Li <xiaoyao.li@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 81bf40d commit 6bfa6d8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

arch/x86/kvm/vmx/tdx.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <linux/cleanup.h>
33
#include <linux/cpu.h>
44
#include <asm/cpufeature.h>
5+
#include <asm/fpu/xcr.h>
56
#include <linux/misc_cgroup.h>
67
#include <linux/mmu_context.h>
78
#include <asm/tdx.h>
@@ -740,6 +741,30 @@ static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu)
740741
BIT_ULL(VCPU_REGS_R14) | \
741742
BIT_ULL(VCPU_REGS_R15))
742743

744+
static void tdx_load_host_xsave_state(struct kvm_vcpu *vcpu)
745+
{
746+
struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm);
747+
748+
/*
749+
* All TDX hosts support PKRU; but even if they didn't,
750+
* vcpu->arch.host_pkru would be 0 and the wrpkru would be
751+
* skipped.
752+
*/
753+
if (vcpu->arch.host_pkru != 0)
754+
wrpkru(vcpu->arch.host_pkru);
755+
756+
if (kvm_host.xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0))
757+
xsetbv(XCR_XFEATURE_ENABLED_MASK, kvm_host.xcr0);
758+
759+
/*
760+
* Likewise, even if a TDX hosts didn't support XSS both arms of
761+
* the comparison would be 0 and the wrmsrl would be skipped.
762+
*/
763+
if (kvm_host.xss != (kvm_tdx->xfam & kvm_caps.supported_xss))
764+
wrmsrl(MSR_IA32_XSS, kvm_host.xss);
765+
}
766+
EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
767+
743768
fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
744769
{
745770
/*
@@ -756,6 +781,8 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, bool force_immediate_exit)
756781

757782
tdx_vcpu_enter_exit(vcpu);
758783

784+
tdx_load_host_xsave_state(vcpu);
785+
759786
vcpu->arch.regs_avail &= TDX_REGS_AVAIL_SET;
760787

761788
trace_kvm_exit(vcpu, KVM_ISA_VMX);
@@ -2332,6 +2359,11 @@ int __init tdx_bringup(void)
23322359
goto success_disable_tdx;
23332360
}
23342361

2362+
if (!cpu_feature_enabled(X86_FEATURE_OSXSAVE)) {
2363+
pr_err("tdx: OSXSAVE is required for TDX\n");
2364+
goto success_disable_tdx;
2365+
}
2366+
23352367
if (!cpu_feature_enabled(X86_FEATURE_MOVDIR64B)) {
23362368
pr_err("tdx: MOVDIR64B is required for TDX\n");
23372369
goto success_disable_tdx;

0 commit comments

Comments
 (0)