[PATCH] KVM: x86: Allow XSAVES on CPUs where host doesn't use it due to an errata

From: Maciej S. Szmigiero
Date: Thu Nov 23 2023 - 14:06:54 EST


From: "Maciej S. Szmigiero" <maciej.szmigiero@xxxxxxxxxx>

Since commit b0563468eeac ("x86/CPU/AMD: Disable XSAVES on AMD family 0x17")
kernel unconditionally clears the XSAVES CPU feature bit on Zen1/2 CPUs.

Since KVM CPU caps are initialized from the kernel boot CPU features this
makes the XSAVES feature also unavailable for KVM guests in this case, even
though they might want to decide on their own whether they are affected by
this errata.

Allow KVM guests to make such decision by setting the XSAVES KVM CPU
capability bit based on the actual CPU capability.

This fixes booting Hyper-V enabled Windows Server 2016 VMs with more than
one vCPU on Zen1/2 CPUs.

Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@xxxxxxxxxx>
---
arch/x86/kvm/cpuid.c | 16 ++++++++++++++++
arch/x86/kvm/svm/svm.c | 5 ++++-
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index dda6fc4cfae8..a8820460163a 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -679,6 +679,22 @@ void kvm_set_cpu_caps(void)
F(AMX_COMPLEX)
);

+ /*
+ * It is possible that CPU supports XSAVES but the host kernel decided
+ * not to use it, for example due to AMD Erratum 1386, and cleared the
+ * relevant CPU feature bit.
+ *
+ * In such case let the guest decide on it own whether to make use of
+ * this feature.
+ */
+ if (boot_cpu_data.cpuid_level >= XSTATE_CPUID) {
+ unsigned int eax, ebx, ecx, edx;
+
+ cpuid_count(XSTATE_CPUID, 1, &eax, &ebx, &ecx, &edx);
+ if (eax & F(XSAVES))
+ kvm_cpu_cap_set(X86_FEATURE_XSAVES);
+ }
+
kvm_cpu_cap_mask(CPUID_D_1_EAX,
F(XSAVEOPT) | F(XSAVEC) | F(XGETBV1) | F(XSAVES) | f_xfd
);
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 712146312358..3cc36710eb21 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4306,9 +4306,12 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
* whether it's advertised to the guest so that KVM context switches
* XSS on VM-Enter/VM-Exit. Failure to do so would effectively give
* the guest read/write access to the host's XSS.
+ *
+ * Make sure to check for XSAVES in KVM CPU capabilities, since the
+ * boot CPU feature bit might be disabled due to Erratum 1386.
*/
if (boot_cpu_has(X86_FEATURE_XSAVE) &&
- boot_cpu_has(X86_FEATURE_XSAVES) &&
+ kvm_cpu_cap_has(X86_FEATURE_XSAVES) &&
guest_cpuid_has(vcpu, X86_FEATURE_XSAVE))
kvm_governed_feature_set(vcpu, X86_FEATURE_XSAVES);