[PATCH v7 079/120] KVM: VMX: Use standard CPUID(0x12) types

"Ahmed S. Darwish" <[email protected]> Thu, 28 May 2026 17:38:41 +0200
Newsgroups dev.linux.lists.x86-cpuid,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
For VMX processor compatibility checks, use standard CPUID(0x12) data
types from x86-cpuid-db instead of doing manual bitwise operations.

Do not use parsed CPUID access since mutating X86_FEATURE_SGX1 state
will affect its cached hardware-backed CPUID bit in the future.

Update cpu_has_sgx()'s comment to match current x86 kernel state.

Signed-off-by: Ahmed S. Darwish <[email protected]>
---
 arch/x86/kvm/vmx/vmx.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index ede773ce065a..20d5397436bf 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -2642,14 +2642,21 @@ void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
 }
 
 /*
- * There is no X86_FEATURE for SGX yet, but anyway we need to query CPUID
- * directly instead of going through cpu_has(), to ensure KVM is trapping
- * ENCLS whenever it's supported in hardware.  It does not matter whether
- * the host OS supports or has enabled SGX.
+ * Do a direct CPUID query instead of checking X86_FEATURE_SGX1 or parsed
+ * CPUID access. KVM needs to trap ENCLS whenever SGX is supported in
+ * hardware, regardless of whether the host OS supports or has enabled SGX.
  */
 static bool cpu_has_sgx(void)
 {
-	return cpuid_eax(0) >= 0x12 && (cpuid_eax(0x12) & BIT(0));
+	struct leaf_0x12_0 l12;
+	struct leaf_0x0_0 l0;
+
+	cpuid_read(0, &l0);
+	if (l0.max_std_leaf < 0x12)
+		return false;
+
+	cpuid_read(0x12, &l12);
+	return l12.sgx1;
 }
 
 static int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, u32 msr, u32 *result)
-- 
2.54.0