KVM: x86: Virtualize AMD CPUID faulting
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
@@ -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
@@ -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
@@ -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
@@ -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
Quoted 7 lines · Jamie Liu
CPUID faulting via
MSR_MISC_FEATURES_ENABLES_CPUID_FAULTis 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().Signed-off-by Jamie Liu <[email protected]>
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);
}
Sean Christopherson
maintainerQuoted 4 lines · Jamie Liu
CPUID faulting via
MSR_MISC_FEATURES_ENABLES_CPUID_FAULTis 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.