Skip to contentSkip to conversation

KVM: x86: Virtualize AMD CPUID faulting

PATCH In review Jamie Liu 3 participants 3 messages Feb 10 – Feb 11

Subsystems (3)
lore
Share & tools
lore

Jamie Liu

author
#0 lore raw

PATCH 1/1 KVM: x86: Virtualize AMD CPUID faulting

CPUID faulting via MSR_MISC_FEATURES_ENABLES_CPUID_FAULT is only used on Intel CPUs. The mechanism virtualized by this change is used on AMD CPUs. See arch/x86/kernel/cpu/amd.c:bsp_init_amd(), arch/x86/kernel/process.c:set_cpuid_faulting().

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/kvm/cpuid.c             |  2 +-
 arch/x86/kvm/cpuid.h             | 28 +++++++++++++++++-----------
 arch/x86/kvm/x86.c               | 14 +++++++++-----
 4 files changed, 28 insertions(+), 17 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
index 3d0a0950d20a..79600fb551cf 100644
@@ -880,6 +880,7 @@
 #define MSR_K7_HWCR_IRPERF_EN_BIT	30
 #define MSR_K7_HWCR_IRPERF_EN		BIT_ULL(MSR_K7_HWCR_IRPERF_EN_BIT)
 #define MSR_K7_HWCR_CPUID_USER_DIS_BIT	35
+#define MSR_K7_HWCR_CPUID_USER_DIS	BIT_ULL(MSR_K7_HWCR_CPUID_USER_DIS_BIT)
 #define MSR_K7_FID_VID_CTL		0xc0010041
 #define MSR_K7_FID_VID_STATUS		0xc0010042
 #define MSR_K7_HWCR_CPB_DIS_BIT		25
arch/x86/kvm/cpuid.c +1 -1
index 88a5426674a1..1dba0982e543 100644
@@ -1221,7 +1221,7 @@ void kvm_set_cpu_caps(void)
 		F(PREFETCHI),
 		EMULATED_F(NO_SMM_CTL_MSR),
 		/* PrefetchCtlMsr */
-		/* GpOnUserCpuid */
+		EMULATED_F(GP_ON_USER_CPUID),
 		/* EPSF */
 		SYNTHESIZED_F(SBPB),
 		SYNTHESIZED_F(IBPB_BRTYPE),
arch/x86/kvm/cpuid.h +17 -11
index d3f5ae15a7ca..9ca8321762fb 100644
@@ -173,17 +173,6 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
 	return x86_stepping(best->eax);
 }
 
-static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
-{
-	return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
-}
-
-static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
-{
-	return vcpu->arch.msr_misc_features_enables &
-		  MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
-}
-
 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
 {
 	unsigned int x86_leaf = __feature_leaf(x86_feature);
@@ -267,6 +256,23 @@ static __always_inline bool guest_cpu_cap_has(struct kvm_vcpu *vcpu,
 	return vcpu->arch.cpu_caps[x86_leaf] & __feature_bit(x86_feature);
 }
 
+static inline bool supports_cpuid_fault_intel(struct kvm_vcpu *vcpu)
+{
+	return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
+}
+
+static inline bool supports_cpuid_fault_amd(struct kvm_vcpu *vcpu)
+{
+	return guest_cpu_cap_has(vcpu, X86_FEATURE_GP_ON_USER_CPUID);
+}
+
+static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
+{
+	return (vcpu->arch.msr_misc_features_enables &
+		MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) ||
+	       (vcpu->arch.msr_hwcr & MSR_K7_HWCR_CPUID_USER_DIS);
+}
+
 static inline bool kvm_vcpu_is_legal_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
 {
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_LAM))
arch/x86/kvm/x86.c +9 -5
index 72d37c8930ad..9140f66b21c6 100644
@@ -3992,14 +3992,18 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		data &= ~(u64)0x8;	/* ignore TLB cache disable */
 
 		/*
-		 * Allow McStatusWrEn and TscFreqSel. (Linux guests from v3.2
-		 * through at least v6.6 whine if TscFreqSel is clear,
-		 * depending on F/M/S.
+		 * Allow McStatusWrEn, TscFreqSel, and CpuidUserDis. (Linux
+		 * guests from v3.2 through at least v6.6 whine if TscFreqSel
+		 * is clear, depending on F/M/S.)
 		 */
-		if (data & ~(BIT_ULL(18) | BIT_ULL(24))) {
+		if (data & ~(BIT_ULL(18) | BIT_ULL(24) |
+			     MSR_K7_HWCR_CPUID_USER_DIS)) {
 			kvm_pr_unimpl_wrmsr(vcpu, msr, data);
 			return 1;
 		}
+		if (data & MSR_K7_HWCR_CPUID_USER_DIS &&
+		    !supports_cpuid_fault_amd(vcpu))
+			return 1;
 		vcpu->arch.msr_hwcr = data;
 		break;
 	case MSR_FAM10H_MMIO_CONF_BASE:
@@ -4248,7 +4252,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_MISC_FEATURES_ENABLES:
 		if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
 		    (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
-		     !supports_cpuid_fault(vcpu)))
+		     !supports_cpuid_fault_intel(vcpu)))
 			return 1;
 		vcpu->arch.msr_misc_features_enables = data;
 		break;
Signature · 1 line
2.53.0.239.g8d8fc8a987-goog
#1 lore raw
Quoted 7 lines · Jamie Liu

CPUID faulting via MSR_MISC_FEATURES_ENABLES_CPUID_FAULT is only used on Intel CPUs. The mechanism virtualized by this change is used on AMD CPUs. See arch/x86/kernel/cpu/amd.c:bsp_init_amd(), arch/x86/kernel/process.c:set_cpuid_faulting().

You missed the cpuid faulting check in em_cpuid():

ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr); if (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&

    ctxt->ops->cpl(ctxt)) {

return emulate_gp(ctxt, 0);
}

#2 lore raw
In reply to Jamie Liu
Quoted 4 lines · Jamie Liu

CPUID faulting via MSR_MISC_FEATURES_ENABLES_CPUID_FAULT is only used on Intel CPUs. The mechanism virtualized by this change is used on AMD CPUs. See arch/x86/kernel/cpu/amd.c:bsp_init_amd(), arch/x86/kernel/process.c:set_cpuid_faulting().

Please rewrite this to state what is being changed, e.g. how KVM is virtualizing the feature, and most importantly why it is "safe" to do so. Specifically, this needs to call out that CPUID_USER_DIS is documented in the APM as an architectural MSR, which for me at least, is mandatory for virtualizing/emulating any of the MSR_K7_HWCR.

The fact that Intel uses some other mechanism is irrelevant, and the kernel source code is not authoritative for things like this.