Skip to content

Commit 0b28f21

Browse files
Binbin Wusean-jc
authored andcommitted
KVM: x86: Add a helper to dedup loading guest/host XCR0 and XSS
Add and use a helper, kvm_load_xfeatures(), to dedup the code that loads guest/host xfeatures. Opportunistically return early if X86_CR4_OSXSAVE is not set to reduce indentations. No functional change intended. Suggested-by: Chao Gao <chao.gao@intel.com> Reviewed-by: Chao Gao <chao.gao@intel.com> Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Link: https://patch.msgid.link/20251110050539.3398759-1-binbin.wu@linux.intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 7649412 commit 0b28f21

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

arch/x86/kvm/x86.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,34 +1205,21 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
12051205
}
12061206
EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_lmsw);
12071207

1208-
static void kvm_load_guest_xfeatures(struct kvm_vcpu *vcpu)
1208+
static void kvm_load_xfeatures(struct kvm_vcpu *vcpu, bool load_guest)
12091209
{
12101210
if (vcpu->arch.guest_state_protected)
12111211
return;
12121212

1213-
if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
1214-
if (vcpu->arch.xcr0 != kvm_host.xcr0)
1215-
xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
1216-
1217-
if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
1218-
vcpu->arch.ia32_xss != kvm_host.xss)
1219-
wrmsrq(MSR_IA32_XSS, vcpu->arch.ia32_xss);
1220-
}
1221-
}
1222-
1223-
static void kvm_load_host_xfeatures(struct kvm_vcpu *vcpu)
1224-
{
1225-
if (vcpu->arch.guest_state_protected)
1213+
if (!kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE))
12261214
return;
12271215

1228-
if (kvm_is_cr4_bit_set(vcpu, X86_CR4_OSXSAVE)) {
1229-
if (vcpu->arch.xcr0 != kvm_host.xcr0)
1230-
xsetbv(XCR_XFEATURE_ENABLED_MASK, kvm_host.xcr0);
1216+
if (vcpu->arch.xcr0 != kvm_host.xcr0)
1217+
xsetbv(XCR_XFEATURE_ENABLED_MASK,
1218+
load_guest ? vcpu->arch.xcr0 : kvm_host.xcr0);
12311219

1232-
if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
1233-
vcpu->arch.ia32_xss != kvm_host.xss)
1234-
wrmsrq(MSR_IA32_XSS, kvm_host.xss);
1235-
}
1220+
if (guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVES) &&
1221+
vcpu->arch.ia32_xss != kvm_host.xss)
1222+
wrmsrq(MSR_IA32_XSS, load_guest ? vcpu->arch.ia32_xss : kvm_host.xss);
12361223
}
12371224

12381225
static void kvm_load_guest_pkru(struct kvm_vcpu *vcpu)
@@ -11271,7 +11258,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1127111258
if (vcpu->arch.guest_fpu.xfd_err)
1127211259
wrmsrq(MSR_IA32_XFD_ERR, vcpu->arch.guest_fpu.xfd_err);
1127311260

11274-
kvm_load_guest_xfeatures(vcpu);
11261+
kvm_load_xfeatures(vcpu, true);
1127511262

1127611263
if (unlikely(vcpu->arch.switch_db_regs &&
1127711264
!(vcpu->arch.switch_db_regs & KVM_DEBUGREG_AUTO_SWITCH))) {
@@ -11367,7 +11354,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
1136711354
vcpu->mode = OUTSIDE_GUEST_MODE;
1136811355
smp_wmb();
1136911356

11370-
kvm_load_host_xfeatures(vcpu);
11357+
kvm_load_xfeatures(vcpu, false);
1137111358

1137211359
/*
1137311360
* Sync xfd before calling handle_exit_irqoff() which may

0 commit comments

Comments
 (0)