KVM: x86: Virtualize AMD's "disable CPUID in usermode"
v3 cover KVM: x86: Virtualize AMD's "disable CPUID in usermode"
AMD's "disable CPUID in usermode" feature is analogous to Intel's "CPUID faulting" feature, but it is advertised and activated differently. The AMD feature is advertised via CPUID.80000021H:EAX.CpuidUserDis[bit 17] and activated by setting HWCR.CpuidUserDis[bit 35].
Add virtualization support for the AMD feature.
v2 -> v3:
* Logic consolidation between emulator and CPUID VM-exit handler [Sean]
* Define MSR_K7_HWCR_CPUID_USER_DIS
* Keep u64 in the selftest (oops)
* Fixe the selftest failre on Intel hosts
v1: https://lore.kernel.org/kvm/[email protected]/
v2: https://lore.kernel.org/kvm/[email protected]/
Jim Mattson (3):
KVM: x86: Remove supports_cpuid_fault() helper
KVM: x86: Virtualize AMD CPUID faulting
KVM: selftests: Update hwcr_msr_test for CPUID faulting bit
Sean Christopherson (1):
KVM: x86: Consolidate CPUID fault handling for emulator and
interception logic
arch/x86/include/asm/kvm_host.h | 1 - arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/cpuid.c | 7 ++-- arch/x86/kvm/cpuid.h | 14 ++++--- arch/x86/kvm/emulate.c | 6 +-- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 38 +++++++++---------- .../selftests/kvm/include/x86/processor.h | 1 + .../testing/selftests/kvm/x86/hwcr_msr_test.c | 9 ++++- 9 files changed, 43 insertions(+), 35 deletions(-)
base-commit: 85f871f6ba46f20d7fbc0b016b4db648c33220dd
Signature · 1 line
2.54.0.631.ge1b05301d1-goog
PATCH v3 1/4 KVM: x86: Consolidate CPUID fault handling for emulator and interception logic
From: Sean Christopherson <[email protected]>
Extract the logic for emulating CPUID faulting (where CPUID #GPs at CPL>0 outside of SMM) into a dedicated helper and use the helper for both the full emulator and the intercepted-CPUID paths.
Opportunistically drop kvm_require_cpl(), as kvm_require_cpl() was the one and only user.
No functional change intended.
--- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/cpuid.c | 5 +++-- arch/x86/kvm/cpuid.h | 8 ++++++++ arch/x86/kvm/emulate.c | 6 +----- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 18 ++++++------------ 6 files changed, 19 insertions(+), 20 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -1
@@ -2285,7 +2285,6 @@ void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault);void kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault); -bool kvm_require_cpl (struct kvm_vcpu *vcpu,int required_cpl);bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr); static inline int __kvm_irq_line_state(unsigned long *irq_state,
arch/x86/kvm/cpuid.c +3 -2
@@ -2161,9 +2161,10 @@ int kvm_emulate_cpuid(struct kvm_vcpu *vcpu) {u32 eax, ebx, ecx, edx; -if (!is_smm (vcpu) &&cpuid_fault_enabled (vcpu) && - !kvm_require_cpl (vcpu,0 )) +if (!kvm_is_cpuid_allowed (vcpu)) { +kvm_queue_exception_e (vcpu,GP_VECTOR ,0 );return 1 ; + } eax =kvm_rax_read (vcpu); ecx =kvm_rcx_read (vcpu);
arch/x86/kvm/cpuid.h +8 -0
@@ -7,6 +7,8 @@#include <asm/processor.h> #include <uapi/asm/kvm_para.h> +#include "smm.h" +extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS ]__read_mostly ;extern bool kvm_is_configuring_cpu_caps__read_mostly ;
@@ -192,6 +194,12 @@ static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; } +static inline bool kvm_is_cpuid_allowed (struct kvm_vcpu *vcpu) +{ +return !cpuid_fault_enabled (vcpu) ||is_smm (vcpu) || + !kvm_x86_call (get_cpl)(vcpu); +} + static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature) {unsigned int x86_leaf =__feature_leaf (x86_feature);
arch/x86/kvm/emulate.c +1 -5
@@ -3593,12 +3593,8 @@ static int em_sti(struct x86_emulate_ctxt *ctxt)static int em_cpuid (struct x86_emulate_ctxt *ctxt) {u32 eax, ebx, ecx, edx; -u64 msr =0 ; - ctxt->ops->get_msr (ctxt,MSR_MISC_FEATURES_ENABLES , &msr); -if (!ctxt->ops->is_smm (ctxt) && - (msr &MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) && - ctxt->ops->cpl (ctxt)) +if (!ctxt->ops->is_cpuid_allowed (ctxt))return emulate_gp (ctxt,0 ); eax =reg_read (ctxt,VCPU_REGS_RAX );
arch/x86/kvm/kvm_emulate.h +1 -0
@@ -225,6 +225,7 @@ struct x86_emulate_ops {struct x86_instruction_info *info, enum x86_intercept_stage stage); +bool (*is_cpuid_allowed)(struct x86_emulate_ctxt *ctxt);bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool exact_only);bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);
arch/x86/kvm/x86.c +6 -12
@@ -1021,18 +1021,6 @@ void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_queue_exception_e); -/* -* Checks if cpl <= required_cpl; if true, return true. Otherwise queue -* a #GP and return false. -*/ -bool kvm_require_cpl (struct kvm_vcpu *vcpu,int required_cpl) -{ -if (kvm_x86_call (get_cpl)(vcpu) <= required_cpl) -return true; -kvm_queue_exception_e (vcpu,GP_VECTOR ,0 ); -return false; -} -bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr) {if ((dr !=4 && dr !=5 ) || !kvm_is_cr4_bit_set (vcpu,X86_CR4_DE ))
@@ -8819,6 +8807,11 @@ static int emulator_intercept(struct x86_emulate_ctxt *ctxt, &ctxt->exception); } +static bool emulator_is_cpuid_allowed (struct x86_emulate_ctxt *ctxt) +{ +return kvm_is_cpuid_allowed (emul_to_vcpu (ctxt)); +} +static bool emulator_get_cpuid (struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool exact_only)
@@ -8955,6 +8948,7 @@ static const struct x86_emulate_ops emulate_ops = { .wbinvd = emulator_wbinvd, .fix_hypercall = emulator_fix_hypercall, .intercept = emulator_intercept, + .is_cpuid_allowed = emulator_is_cpuid_allowed, .get_cpuid = emulator_get_cpuid, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr,
Signature · 1 line
2.54.0.631.ge1b05301d1-goog
Re: [PATCH v3 1/4] KVM: x86: Consolidate CPUID fault handling for emulator and interception logic
Quoted 7 lines · Jim Mattson
From: Sean Christopherson <[email protected]>
Extract the logic for emulating CPUID faulting (where CPUID #GPs at CPL>0 outside of SMM) into a dedicated helper and use the helper for both the full emulator and the intercepted-CPUID paths.
Opportunistically drop
kvm_require_cpl(), askvm_require_cpl()was the one
^
Typo:
kvm_require_cpl() -> kvm_emulate_cpuid()
Quoted 6 lines
and only user.
No functional change intended.
Signed-off-by Sean Christopherson <[email protected]>Signed-off-by Jim Mattson <[email protected]>
Quoted 137 lines
--- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/cpuid.c | 5 +++-- arch/x86/kvm/cpuid.h | 8 ++++++++ arch/x86/kvm/emulate.c | 6 +----- arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/x86.c | 18 ++++++------------ 6 files changed, 19 insertions(+), 20 deletions(-)arch/x86/include/asm/kvm_host.h +0
-1@@ -2285,7 +2285,6 @@ void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned int nr,void kvm_inject_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault);void kvm_inject_emulated_page_fault (struct kvm_vcpu *vcpu,struct x86_exception *fault); -bool kvm_require_cpl (struct kvm_vcpu *vcpu,int required_cpl);bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr); static inline int __kvm_irq_line_state(unsigned long *irq_state,arch/x86/kvm/cpuid.c +3
-2@@ -2161,9 +2161,10 @@ int kvm_emulate_cpuid(struct kvm_vcpu *vcpu) {u32 eax, ebx, ecx, edx; -if (!is_smm (vcpu) &&cpuid_fault_enabled (vcpu) && - !kvm_require_cpl (vcpu,0 )) +if (!kvm_is_cpuid_allowed (vcpu)) { +kvm_queue_exception_e (vcpu,GP_VECTOR ,0 );return 1 ; + } eax =kvm_rax_read (vcpu); ecx =kvm_rcx_read (vcpu);arch/x86/kvm/cpuid.h +8
-0@@ -7,6 +7,8 @@#include <asm/processor.h> #include <uapi/asm/kvm_para.h> +#include "smm.h" +extern u32 kvm_cpu_caps[NR_KVM_CPU_CAPS ]__read_mostly ;extern bool kvm_is_configuring_cpu_caps__read_mostly ;@@ -192,6 +194,12 @@ static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; } +static inline bool kvm_is_cpuid_allowed (struct kvm_vcpu *vcpu) +{ +return !cpuid_fault_enabled (vcpu) ||is_smm (vcpu) || + !kvm_x86_call (get_cpl)(vcpu); +} + static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature) {unsigned int x86_leaf =__feature_leaf (x86_feature);arch/x86/kvm/emulate.c +1
-5@@ -3593,12 +3593,8 @@ static int em_sti(struct x86_emulate_ctxt *ctxt)static int em_cpuid (struct x86_emulate_ctxt *ctxt) {u32 eax, ebx, ecx, edx; -u64 msr =0 ; - ctxt->ops->get_msr (ctxt,MSR_MISC_FEATURES_ENABLES , &msr); -if (!ctxt->ops->is_smm (ctxt) && - (msr &MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) && - ctxt->ops->cpl (ctxt)) +if (!ctxt->ops->is_cpuid_allowed (ctxt))return emulate_gp (ctxt,0 ); eax =reg_read (ctxt,VCPU_REGS_RAX );arch/x86/kvm/kvm_emulate.h +1
-0@@ -225,6 +225,7 @@ struct x86_emulate_ops {struct x86_instruction_info *info, enum x86_intercept_stage stage); +bool (*is_cpuid_allowed)(struct x86_emulate_ctxt *ctxt);bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool exact_only);bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);arch/x86/kvm/x86.c +6
-12@@ -1021,18 +1021,6 @@ void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_queue_exception_e); -/* -* Checks if cpl <= required_cpl; if true, return true. Otherwise queue -* a #GP and return false. -*/ -bool kvm_require_cpl (struct kvm_vcpu *vcpu,int required_cpl) -{ -if (kvm_x86_call (get_cpl)(vcpu) <= required_cpl) -return true; -kvm_queue_exception_e (vcpu,GP_VECTOR ,0 ); -return false; -} -bool kvm_require_dr (struct kvm_vcpu *vcpu,int dr) {if ((dr !=4 && dr !=5 ) || !kvm_is_cr4_bit_set (vcpu,X86_CR4_DE ))@@ -8819,6 +8807,11 @@ static int emulator_intercept(struct x86_emulate_ctxt *ctxt, &ctxt->exception); } +static bool emulator_is_cpuid_allowed (struct x86_emulate_ctxt *ctxt) +{ +return kvm_is_cpuid_allowed (emul_to_vcpu (ctxt)); +} +static bool emulator_get_cpuid (struct x86_emulate_ctxt *ctxt,u32 *eax,u32 *ebx,u32 *ecx,u32 *edx,bool exact_only)@@ -8955,6 +8948,7 @@ static const struct x86_emulate_ops emulate_ops = { .wbinvd = emulator_wbinvd, .fix_hypercall = emulator_fix_hypercall, .intercept = emulator_intercept, + .is_cpuid_allowed = emulator_is_cpuid_allowed, .get_cpuid = emulator_get_cpuid, .guest_has_movbe = emulator_guest_has_movbe, .guest_has_fxsr = emulator_guest_has_fxsr,
PATCH v3 2/4 KVM: x86: Remove supports_cpuid_fault() helper
In reply to Jim MattsonThe function, supports_cpuid_fault(), tests specifically for guest support of Intel's CPUID faulting feature. It does not test for guest support of AMD's CPUID faulting feature.
To avoid confusion, remove the helper.
--- arch/x86/kvm/cpuid.h | 5 ----- arch/x86/kvm/x86.c | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-)
arch/x86/kvm/cpuid.h +0 -5
@@ -183,11 +183,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 &
arch/x86/kvm/x86.c +1 -1
@@ -4252,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))) + !(vcpu->arch.msr_platform_info &MSR_PLATFORM_INFO_CPUID_FAULT )))return 1 ; vcpu->arch.msr_misc_features_enables = data;break ;
Signature · 1 line
2.54.0.631.ge1b05301d1-goog
Re: [PATCH v3 2/4] KVM: x86: Remove supports_cpuid_fault() helper
Quoted 7 lines · Jim Mattson
The function,
supports_cpuid_fault(), tests specifically for guest support of Intel's CPUID faulting feature. It does not test for guest support of AMD's CPUID faulting feature.To avoid confusion, remove the helper.
Signed-off-by Jim Mattson <[email protected]>
Quoted 34 lines
--- arch/x86/kvm/cpuid.h | 5 ----- arch/x86/kvm/x86.c | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-)arch/x86/kvm/cpuid.h +0
-5@@ -183,11 +183,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 &arch/x86/kvm/x86.c +1
-1@@ -4252,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))) + !(vcpu->arch.msr_platform_info &MSR_PLATFORM_INFO_CPUID_FAULT )))return 1 ; vcpu->arch.msr_misc_features_enables = data;break ;
PATCH v3 3/4 KVM: x86: Virtualize AMD CPUID faulting
In reply to Jim MattsonOn AMD CPUs, CPUID faulting support is advertised via CPUID.80000021H:EAX.CpuidUserDis[bit 17] and enabled by setting HWCR.CpuidUserDis[bit 35].
Advertise the feature to userspace regardless of host CPU support. Allow writes to HWCR to set bit 35 when the guest CPUID advertises CpuidUserDis. Update cpuid_fault_enabled() to check HWCR.CpuidUserDis as well as MSR_FEATURE_ENABLES.CPUID_GP_ON_CPL_GT_0.
--- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/cpuid.h | 5 +++-- arch/x86/kvm/x86.c | 18 ++++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
@@ -888,6 +888,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
@@ -1248,7 +1248,7 @@ void kvm_initialize_cpu_caps(void)F (AUTOIBRS ),EMULATED_F (NO_SMM_CTL_MSR ),/* PrefetchCtlMsr */ -/* GpOnUserCpuid */ +EMULATED_F (GP_ON_USER_CPUID ),/* EPSF */ F (PREFETCHI ), F(AVX512_BMM),
arch/x86/kvm/cpuid.h +3 -2
@@ -185,8 +185,9 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)static inline bool cpuid_fault_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.msr_misc_features_enables & -MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; +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_is_cpuid_allowed (struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c +12 -6
@@ -3990,22 +3990,28 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)break ;case MSR_EFER :return set_efer (vcpu, msr_info); -case MSR_K7_HWCR : - data &= ~(u64 )0x40 ;/* ignore flush filter disable */ - data &= ~(u64 )0x100 ;/* ignore ignne emulation enable */ - data &= ~(u64 )0x8 ;/* ignore TLB cache disable */ - +case MSR_K7_HWCR : {/* * Allow McStatusWrEn and TscFreqSel. (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 ))) { +u64 valid =BIT_ULL (18 ) |BIT_ULL (24 ); + + data &= ~(u64 )0x40 ;/* ignore flush filter disable */ + data &= ~(u64 )0x100 ;/* ignore ignne emulation enable */ + data &= ~(u64 )0x8 ;/* ignore TLB cache disable */ + +if (guest_cpu_cap_has (vcpu,X86_FEATURE_GP_ON_USER_CPUID )) + valid |=MSR_K7_HWCR_CPUID_USER_DIS ; + +if (data & ~valid) {kvm_pr_unimpl_wrmsr (vcpu, msr, data);return 1 ; } vcpu->arch.msr_hwcr = data;break ; + }case MSR_FAM10H_MMIO_CONF_BASE :if (data !=0 ) {kvm_pr_unimpl_wrmsr (vcpu, msr, data);
Signature · 1 line
2.54.0.631.ge1b05301d1-goog
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 48 lines · Jim Mattson
On AMD CPUs, CPUID faulting support is advertised via CPUID.80000021H:EAX.CpuidUserDis[bit 17] and enabled by setting HWCR.CpuidUserDis[bit 35].
Advertise the feature to userspace regardless of host CPU support. Allow writes to HWCR to set bit 35 when the guest CPUID advertises CpuidUserDis. Update
cpuid_fault_enabled()to check HWCR.CpuidUserDis as well as MSR_FEATURE_ENABLES.CPUID_GP_ON_CPL_GT_0.Signed-off-by Jim Mattson <[email protected]>--- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/cpuid.h | 5 +++-- arch/x86/kvm/x86.c | 18 ++++++++++++------ 4 files changed, 17 insertions(+), 9 deletions(-)arch/x86/include/asm/msr-index.h +1
-0@@ -888,6 +888,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 25arch/x86/kvm/cpuid.c +1
-1@@ -1248,7 +1248,7 @@ void kvm_initialize_cpu_caps(void)F (AUTOIBRS ),EMULATED_F (NO_SMM_CTL_MSR ),/* PrefetchCtlMsr */ -/* GpOnUserCpuid */ +EMULATED_F (GP_ON_USER_CPUID ),/* EPSF */ F (PREFETCHI ), F(AVX512_BMM),arch/x86/kvm/cpuid.h +0
-0
arch/x86/kvm/cpuid.h static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) · line 191
Quoted 9 lines
arch/x86/kvm/cpuid.h +3
-2@@ -185,8 +185,9 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)static inline bool cpuid_fault_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.msr_misc_features_enables & -MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; +return (vcpu->arch.msr_misc_features_enables & +MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) || + (vcpu->arch.msr_hwcr &MSR_K7_HWCR_CPUID_USER_DIS ); }
Sashiko raises a good point here about a pre-existing issue that probably warrants a fix before propagating it further:
Quoted 4 lines
Does this emulation of CPUID faulting respect architectural fault priorities in a nested virtualization scenario?
According to the AMD APM, if CPUID faulting is enabled, a #GP fault takes precedence over a CPUID VM-exit intercept.
Because KVM emulates CPUID faulting inkvm_emulate_cpuid(), the fault check happens after nested VM-exit intercept checks. If an L1 hypervisor enables both CPUID faulting and a CPUID VM-exit intercept, L0's nested exit handlers will observe L1's intercept request and immediately reflect the VM-exit to L1. Since this reflection happens before evaluatingkvm_emulate_cpuid(), does this allow L2 guests to completely bypass the CPUID faulting restrictions imposed by L1?
Sean Christopherson
maintainerRe: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 6 lines · Jim Mattson
Quoted 6 lines
arch/x86/kvm/cpuid.h +0
-0@@ -185,8 +185,9 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
Quoted 10 lines
arch/x86/kvm/cpuid.h static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) · line 191
Quoted 8 lines
arch/x86/kvm/cpuid.h +3
-2static inline bool cpuid_fault_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.msr_misc_features_enables & -MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; +return (vcpu->arch.msr_misc_features_enables & +MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) || + (vcpu->arch.msr_hwcr &MSR_K7_HWCR_CPUID_USER_DIS ); }Sashiko raises a good point here about a pre-existing issue that
Calling this pre-existing is a bit of a stretch. I'm guessing VMX doesn't check the #GP before the VM-Exit (checking #GP before a VM-Exit is so stupid).
Yes, KVM technically emulates MSR_MISC_FEATURES_ENABLES_CPUID_FAULT for AMD, but we're firmly in "making shit up" territory when reasoning about the interactions between SVM and a feature that doesn't exist on real AMD CPUs.
Quoted 7 lines
probably warrants a fix before propagating it further:
Quoted 5 lines
Does this emulation of CPUID faulting respect architectural fault priorities in a nested virtualization scenario?
According to the AMD APM, if CPUID faulting is enabled, a #GP fault takes precedence over a CPUID VM-exit intercept.
Where in the APM? I can't find anything in the description of CPUID or CpuidUserDis that specifies the priority, and "Table 15-7. Instruction Intercepts" is flat out wrong because it just says:
CPUID CPUID No exceptions to check.
Quoted 9 lines
Quoted 9 lines
Because KVM emulates CPUID faulting in
kvm_emulate_cpuid(), the fault check happens after nested VM-exit intercept checks. If an L1 hypervisor enables both CPUID faulting and a CPUID VM-exit intercept, L0's nested exit handlers will observe L1's intercept request and immediately reflect the VM-exit to L1.Since this reflection happens before evaluating
kvm_emulate_cpuid(), does this allow L2 guests to completely bypass the CPUID faulting restrictions imposed by L1?
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 19 lines · Sean Christopherson
Quoted 6 lines · Jim Mattson
Quoted 6 lines
arch/x86/kvm/cpuid.h +0
-0@@ -185,8 +185,9 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)Quoted 10 lines
arch/x86/kvm/cpuid.h static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) · line 191
Quoted 8 lines
arch/x86/kvm/cpuid.h +3
-2static inline bool cpuid_fault_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.msr_misc_features_enables & -MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; +return (vcpu->arch.msr_misc_features_enables & +MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) || + (vcpu->arch.msr_hwcr &MSR_K7_HWCR_CPUID_USER_DIS ); }Sashiko raises a good point here about a pre-existing issue that
Quoted 2 lines
Calling this pre-existing is a bit of a stretch. I'm guessing VMX doesn't check the #GP before the VM-Exit (checking #GP before a VM-Exit is so stupid).
Per the SDM, volume 3, section 27.1.1: Relative Priority of Faults and VM Exits
Quoted 1 line
Certain exceptions have priority over VM exits. These include invalid-opcode exceptions, faults based on
privilege level,1 and general-protection exceptions that are based on checking I/O permission bits in the task-
state segment (TSS). For example, execution of RDMSR with CPL = 3 generates a general-protection exception
and not a VM exit.2
Quoted 3 lines
Yes, KVM technically emulates
MSR_MISC_FEATURES_ENABLES_CPUID_FAULTfor AMD, but we're firmly in "making shit up" territory when reasoning about the interactions between SVM and a feature that doesn't exist on real AMD CPUs.
True.
Quoted 5 lines
Quoted 5 lines
probably warrants a fix before propagating it further:
Quoted 3 lines
Does this emulation of CPUID faulting respect architectural fault priorities in a nested virtualization scenario?
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
According to the AMD APM, if CPUID faulting is enabled, a #GP fault takes precedence over a CPUID VM-exit intercept.
Where in the APM? I can't find anything in the description of CPUID or CpuidUserDis that specifies the priority, and "Table 15-7. Instruction Intercepts" is flat out wrong because it just says:
CPUID CPUID No exceptions to check.
APM volume 2, section 15.7: Intercept Operation
Quoted 1 line
Generally, instruction intercepts are checked after simple exceptions
(such as #GP—when CPL is incorrect—or #UD) have been checked, but before exceptions related to
memory accesses (such as page faults) and exceptions based on specific operand values.
Sean Christopherson
maintainerRe: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 30 lines · Jim Mattson
Quoted 19 lines · Sean Christopherson
Quoted 6 lines · Jim Mattson
Quoted 6 lines
arch/x86/kvm/cpuid.h +0
-0@@ -185,8 +185,9 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)Quoted 10 lines
arch/x86/kvm/cpuid.h static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu) · line 191
Quoted 8 lines
arch/x86/kvm/cpuid.h +3
-2static inline bool cpuid_fault_enabled (struct kvm_vcpu *vcpu) { -return vcpu->arch.msr_misc_features_enables & -MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ; +return (vcpu->arch.msr_misc_features_enables & +MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ) || + (vcpu->arch.msr_hwcr &MSR_K7_HWCR_CPUID_USER_DIS ); }Sashiko raises a good point here about a pre-existing issue that
Quoted 2 lines
Calling this pre-existing is a bit of a stretch. I'm guessing VMX doesn't check the #GP before the VM-Exit (checking #GP before a VM-Exit is so stupid).
Per the SDM, volume 3, section 27.1.1: Relative Priority of Faults and VM Exits
Certain exceptions have priority over VM exits. These include invalid-opcode exceptions, faults based on privilege level,1 and general-protection exceptions that are based on checking I/O permission bits in the task- state segment (TSS). For example, execution of RDMSR with CPL = 3 generates a general-protection exception and not a VM exit.2
...
Quoted 6 lines
Quoted 5 lines
Where in the APM? I can't find anything in the description of CPUID or CpuidUserDis that specifies the priority, and "Table 15-7. Instruction Intercepts" is flat out wrong because it just says:
CPUID CPUID No exceptions to check.
Quoted 7 lines
APM volume 2, section 15.7: Intercept Operation
Quoted 1 line
Generally, instruction intercepts are checked after simple exceptions
(such as #GP—when CPL is incorrect—or #UD) have been checked, but before exceptions related to
memory accesses (such as page faults) and exceptions based on specific operand values.
Oooh, this is based on the generic CPL rules. I didn't think about it from that perspective. So yeah, addressing that does make sense. What a pain.
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 2 lines · Sean Christopherson
Oooh, this is based on the generic CPL rules. I didn't think about it from that perspective. So yeah, addressing that does make sense. What a pain.
When I fix this in version 4, what's the correct footer for Sashiko attribution:
or
or something else?
Sean Christopherson
maintainerRe: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 11 lines · Jim Mattson
Quoted 2 lines · Sean Christopherson
Oooh, this is based on the generic CPL rules. I didn't think about it from that perspective. So yeah, addressing that does make sense. What a pain.
When I fix this in version 4, what's the correct footer for Sashiko attribution:
Assisted-by Sashiko:gemini/gemini-3.1-pro-previewor
Reported-by Sashiko:gemini/gemini-3.1-pro-preview
This, or even just:
is good enough for me. I don't expect random developers to know or care what model was used, at least not when it comes to reporting bugs. If you use AI to help write the code, then maybe I'd care?
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 14 lines · Sean Christopherson
Quoted 11 lines · Jim Mattson
Quoted 2 lines · Sean Christopherson
Oooh, this is based on the generic CPL rules. I didn't think about it from that perspective. So yeah, addressing that does make sense. What a pain.
When I fix this in version 4, what's the correct footer for Sashiko attribution:
Assisted-by Sashiko:gemini/gemini-3.1-pro-previewor
Reported-by Sashiko:gemini/gemini-3.1-pro-preview
Quoted 7 lines
This, or even just:
Reported-by Sashikois good enough for me. I don't expect random developers to know or care what model was used, at least not when it comes to reporting bugs. If you use AI to help write the code, then maybe I'd care?
I had Jetski write an empirical test to see how the hardware behaves. On the first userspace CPUID VM-exit, temporarily set #GP on userspace CPUID per the appropriate vendor's mechanism, make sure that we intercept #GP, and re-enter the guest without advancing RIP. Then, see what the next VM-exit is.
Surprise!
kvm_intel: KVM: EMPIRICAL TEST RESULT: #GP took precedence over CPUID VM-exitkvm_amd: KVM: EMPIRICAL TEST RESULT (SVM): CPUID VM-exit took precedence over #GP!
LOL!
David: Is this intentional or a bug?
RE: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
AMD General
Quoted 52 lines
-----Original Message-----
From: Jim Mattson <[email protected]>
Sent: Thursday, May 14, 2026 1:02 PM
To: Sean Christopherson <[email protected]>; Kaplan, David <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faultingCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 3 lines · Sean Christopherson
Quoted 1 line · Jim Mattson
On Thu, May 14, 2026 at 9:20 AM Sean Christopherson
Quoted 1 line · <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
Oooh, this is based on the generic CPL rules. I didn't think about it from
that
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
perspective. So yeah, addressing that does make sense. What a pain.
When I fix this in version 4, what's the correct footer for Sashiko
attribution:
Quoted 5 lines
Quoted 5 lines
Assisted-by Sashiko:gemini/gemini-3.1-pro-previewor
Quoted 7 lines
Quoted 1 line
Reported-by Sashiko:gemini/gemini-3.1-pro-previewThis, or even just:
Reported-by Sashikois good enough for me. I don't expect random developers to know or care
what
Quoted 1 line
model was used, at least not when it comes to reporting bugs. If you use AI
to
Quoted 1 line
help write the code, then maybe I'd care?
I had Jetski write an empirical test to see how the hardware behaves. On the first userspace CPUID VM-exit, temporarily set #GP on userspace CPUID per the appropriate vendor's mechanism, make sure that we intercept #GP, and re-enter the guest without advancing RIP. Then, see what the next VM-exit is.
Surprise!
Quoted 8 lines
kvm_intel: KVM: EMPIRICAL TEST RESULT: #GP took precedence over CPUID VM-exitkvm_amd: KVM: EMPIRICAL TEST RESULT (SVM): CPUID VM-exit took precedence over #GP!LOL!
David: Is this intentional or a bug?
This appears to be a bug, as most other instructions (e.g. INVPCID) that have CPL checks do the CPL checks before the VMEXIT checks. I'm following up on this internally...
Thanks for bringing this to our attention
--David Kaplan
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 2 lines · David
This appears to be a bug, as most other instructions (e.g. INVPCID) that have CPL checks do the CPL checks before the VMEXIT checks. I'm following up on this internally...
Did you learn anything from the internal follow-up? The ellipsis suggests, "stay tuned..." :)
RE: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
AMD General
Quoted 18 lines
-----Original Message-----
From: Jim Mattson <[email protected]>
Sent: Tuesday, May 26, 2026 1:39 PM
To: Kaplan, David <[email protected]>
Cc: Sean Christopherson <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faultingCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 1 line · David
Quoted 6 lines
Quoted 1 line
This appears to be a bug, as most other instructions (e.g. INVPCID) that have
CPL checks do the CPL checks before the VMEXIT checks. I'm following up on this internally...
Did you learn anything from the internal follow-up? The ellipsis suggests, "stay tuned..." :)
It appears it was a deliberate decision, due in part to the fact that the traditional flow of checking CPL exceptions before intercepts typically applies to using instructions in an illegal way, but the user is not doing anything illegal here by executing CPUID which is normally a legal instruction at CPL3.
That said, clearly the behavior is different now between the vendors, however at this point the feedback I got is that our architects want to stick with the existing behavior unless there is a strong reason that it is a problem for SW.
--David Kaplan
Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faulting
Quoted 25 lines · David
AMD General
Quoted 22 lines
-----Original Message-----
From: Jim Mattson <[email protected]>
Sent: Tuesday, May 26, 2026 1:39 PM
To: Kaplan, David <[email protected]>
Cc: Sean Christopherson <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]
Subject: Re: [PATCH v3 3/4] KVM: x86: Virtualize AMD CPUID faultingCaution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.
Quoted 2 lines · David
This appears to be a bug, as most other instructions (e.g. INVPCID) that have
CPL checks do the CPL checks before the VMEXIT checks. I'm following up on this internally...
Quoted 6 lines
Quoted 2 lines
Did you learn anything from the internal follow-up? The ellipsis suggests, "stay tuned..." :)
It appears it was a deliberate decision, due in part to the fact that the traditional flow of checking CPL exceptions before intercepts typically applies to using instructions in an illegal way, but the user is not doing anything illegal here by executing CPUID which is normally a legal instruction at CPL3.
That said, clearly the behavior is different now between the vendors, however at this point the feedback I got is that our architects want to stick with the existing behavior unless there is a strong reason that it is a problem for SW.
No problem from the standpoint of nested SVM emulation.
The APM does say, "**Generally** [emphasis mine], instruction intercepts are checked after simple exceptions (such as #GP—when CPL is incorrect—or #UD) have been checked, but before exceptions related to memory accesses (such as page faults) and exceptions based on specific operand values," but it would be nice to have exceptions such as this enumerated.
PATCH v3 4/4 KVM: selftests: Update hwcr_msr_test for CPUID faulting bit
In reply to Jim MattsonAdd BIT_ULL(35) (CpuidUserDis) to the valid mask in hwcr_msr_test, now that KVM accepts writes to this bit when the guest CPUID advertises CpuidUserDis.
--- tools/testing/selftests/kvm/include/x86/processor.h | 1 + tools/testing/selftests/kvm/x86/hwcr_msr_test.c | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
@@ -209,6 +209,7 @@ struct kvm_x86_cpu_feature {#define X86_FEATURE_SEV KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)#define X86_FEATURE_SEV_ES KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)#define X86_FEATURE_SEV_SNP KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4) +#define X86_FEATURE_GP_ON_USER_CPUID KVM_X86_CPU_FEATURE(0x80000021, 0, EAX, 17)#define X86_FEATURE_PERFMON_V2 KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 0)#define X86_FEATURE_LBR_PMC_FREEZE KVM_X86_CPU_FEATURE(0x80000022, 0, EAX, 2)
tools/testing/selftests/kvm/x86/hwcr_msr_test.c +7 -2
@@ -11,12 +11,17 @@void test_hwcr_bit (struct kvm_vcpu *vcpu,unsigned int bit) {const u64 ignored =BIT_ULL (3 ) |BIT_ULL (6 ) |BIT_ULL (8 ); -const u64 valid =BIT_ULL (18 ) |BIT_ULL (24 ); -const u64 legal = ignored | valid; +u64 valid =BIT_ULL (18 ) |BIT_ULL (24 );u64 val =BIT_ULL (bit);u64 actual; +u64 legal;int r; +if (kvm_cpu_has (X86_FEATURE_GP_ON_USER_CPUID )) + valid |=BIT_ULL (35 ); + + legal = ignored | valid; + r =_vcpu_set_msr (vcpu,MSR_K7_HWCR , val); TEST_ASSERT(val & ~legal ? !r : r == 1, "Expected KVM_SET_MSRS(MSR_K7_HWCR) = 0x%lx to %s",
Signature · 1 line
2.54.0.631.ge1b05301d1-goog