Skip to content

Commit c266754

Browse files
gaochaointelsean-jc
authored andcommitted
KVM: x86: Zero XSTATE components on INIT by iterating over supported features
Tweak the code a bit to facilitate resetting more xstate components in the future, e.g., CET's xstate-managed MSRs. No functional change intended. Suggested-by: Sean Christopherson <seanjc@google.com> Tested-by: Mathias Krause <minipli@grsecurity.net> Tested-by: John Allen <john.allen@amd.com> Signed-off-by: Chao Gao <chao.gao@intel.com> Tested-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Link: https://lore.kernel.org/r/20250812025606.74625-6-chao.gao@intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 41f6710 commit c266754

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

arch/x86/kvm/x86.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12401,6 +12401,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1240112401
static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)
1240212402
{
1240312403
struct fpstate *fpstate = vcpu->arch.guest_fpu.fpstate;
12404+
u64 xfeatures_mask;
12405+
int i;
1240412406

1240512407
/*
1240612408
* Guest FPU state is zero allocated and so doesn't need to be manually
@@ -12414,16 +12416,20 @@ static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)
1241412416
* are unchanged. Currently, the only components that are zeroed and
1241512417
* supported by KVM are MPX related.
1241612418
*/
12417-
if (!kvm_mpx_supported())
12419+
xfeatures_mask = (kvm_caps.supported_xcr0 | kvm_caps.supported_xss) &
12420+
(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
12421+
if (!xfeatures_mask)
1241812422
return;
1241912423

12424+
BUILD_BUG_ON(sizeof(xfeatures_mask) * BITS_PER_BYTE <= XFEATURE_MAX);
12425+
1242012426
/*
1242112427
* All paths that lead to INIT are required to load the guest's FPU
1242212428
* state (because most paths are buried in KVM_RUN).
1242312429
*/
1242412430
kvm_put_guest_fpu(vcpu);
12425-
fpstate_clear_xstate_component(fpstate, XFEATURE_BNDREGS);
12426-
fpstate_clear_xstate_component(fpstate, XFEATURE_BNDCSR);
12431+
for_each_set_bit(i, (unsigned long *)&xfeatures_mask, XFEATURE_MAX)
12432+
fpstate_clear_xstate_component(fpstate, i);
1242712433
kvm_load_guest_fpu(vcpu);
1242812434
}
1242912435

0 commit comments

Comments
 (0)