]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: x86: Consolidate CPUID fault handling for emulator and interception logic
authorSean Christopherson <seanjc@google.com>
Wed, 27 May 2026 17:43:43 +0000 (10:43 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 27 May 2026 18:21:41 +0000 (11:21 -0700)
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_emulate_cpuid() was the
one and only user.

No functional change intended.

[jim: Add EXPORT_STATIC_CALL_GPL(kvm_x86_get_cpl) so that KVM vendor
modules can call kvm_is_cpuid_allowed(). Fix typo in commit message.]

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Link: https://patch.msgid.link/20260527174347.2356165-2-jmattson@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/cpuid.c
arch/x86/kvm/cpuid.h
arch/x86/kvm/emulate.c
arch/x86/kvm/kvm_emulate.h
arch/x86/kvm/x86.c

index beb83eea65efaf326d95e17c9ac33c4ec64ad656..f5a35136ebb83cad715f81c8ed6d66fa084d3bdf 100644 (file)
@@ -2320,7 +2320,6 @@ static inline void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,
        __kvm_inject_emulated_page_fault(vcpu, fault, false);
 }
 
-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,
index e69156b54cfffca0e7d65641bd02f29def56b920..1c95d1fa3ead2b8ee9e0e6e8e351e28c69897347 100644 (file)
@@ -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);
index 039b8e6f40baf77d5d38aefcad6f958203bf780e..bc4a8428b836d09f82534e7ceee3d146addf20b4 100644 (file)
@@ -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);
index dd0e19af49972042955b6e3a285470d7e33a36aa..a367828ce86996b11d3bfe027866731b20384d85 100644 (file)
@@ -3594,12 +3594,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);
index f5df31a52996af076b4c6f83ba183ddbcd67920c..3e375af15c035fc4c90ba42093f89c65698cc803 100644 (file)
@@ -230,6 +230,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);
index c560c52b95f088d28576d717c0a07bc7bd35c136..2bad3fa987dfc01f6a1beb1453e2849357f94f8e 100644 (file)
@@ -151,6 +151,7 @@ struct kvm_x86_ops kvm_x86_ops __read_mostly;
 #include <asm/kvm-x86-ops.h>
 EXPORT_STATIC_CALL_GPL(kvm_x86_get_cs_db_l_bits);
 EXPORT_STATIC_CALL_GPL(kvm_x86_cache_reg);
+EXPORT_STATIC_CALL_GPL(kvm_x86_get_cpl);
 
 static bool __read_mostly ignore_msrs = 0;
 module_param(ignore_msrs, bool, 0644);
@@ -1022,18 +1023,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))
@@ -8824,6 +8813,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)
@@ -8961,6 +8955,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,