[RFC PATCH v2 2/4] KVM: x86: TDX: Hide unsupported configurable CPUID bits
From: Binbin Wu
Date: Wed Jun 03 2026 - 22:30:22 EST
Filter the CPUID capabilities reported by KVM_TDX_CAPABILITIES through
KVM's supported TDX configurable CPUID allowlist.
The TDX module reports all configurable CPUID bits it supports for a TD,
but KVM must not expose bits to userspace that it doesn't support.
Blindly exposing unsupported features could lead to host state corruption.
Add a helper get_supported_cfg_cpuid() to retrieve KVM's supported TDX
configurable CPUID bit mask for a given leaf, subleaf, and register.
Additionally, add a comment explicitly noting that the allowlist array
must remain sorted by CPUID function. This allows the helper's linear
search to safely terminate early once it iterates past the target leaf,
optimizing the lookup process.
Replace the existing hardcoded denylist with a secure-by-default approach.
By using the get_supported_cfg_cpuid() helper, KVM now strictly reports
only the configurable CPUID bits it explicitly supports. This ensures
any newly introduced configurable CPUID bits are automatically hidden from
userspace until KVM specifically opts in.
Signed-off-by: Binbin Wu <binbin.wu@xxxxxxxxxxxxxxx>
---
arch/x86/kvm/vmx/tdx.c | 45 +++++++++++++++++++++++-------------------
1 file changed, 25 insertions(+), 20 deletions(-)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index e0567088ebf5..e6bfec87a484 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -64,6 +64,8 @@ struct tdx_supported_cpuid_reg {
/*
* Multi-bit fields are statically initialized, feature bits are initialized
* in tdx_initialize_cpu_cfg_caps().
+ *
+ * Keep the list sorted by CPUID function.
*/
static struct tdx_supported_cpuid_reg tdx_kvm_supported_cpuid[] __ro_after_init = {
{ 0x1, 0, 0, CPUID_EAX, GENMASK_U32(27, 16) | GENMASK_U32(13, 0) },
@@ -300,34 +302,31 @@ static bool has_tsx(const struct kvm_cpuid_entry2 *entry)
(entry->ebx & TDX_FEATURE_TSX);
}
-static void clear_tsx(struct kvm_cpuid_entry2 *entry)
-{
- entry->ebx &= ~TDX_FEATURE_TSX;
-}
-
static bool has_waitpkg(const struct kvm_cpuid_entry2 *entry)
{
return entry->function == 7 && entry->index == 0 &&
(entry->ecx & __feature_bit(X86_FEATURE_WAITPKG));
}
-static void clear_waitpkg(struct kvm_cpuid_entry2 *entry)
+static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry)
{
- entry->ecx &= ~__feature_bit(X86_FEATURE_WAITPKG);
+ return has_tsx(entry) || has_waitpkg(entry);
}
-static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry)
+static u32 get_supported_cfg_cpuid(u32 function, u32 index, u8 reg)
{
- if (has_tsx(entry))
- clear_tsx(entry);
+ for (int i = 0; i < ARRAY_SIZE(tdx_kvm_supported_cpuid); i++) {
+ struct tdx_supported_cpuid_reg *r = &tdx_kvm_supported_cpuid[i];
- if (has_waitpkg(entry))
- clear_waitpkg(entry);
-}
+ if (r->function > function)
+ break;
-static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry)
-{
- return has_tsx(entry) || has_waitpkg(entry);
+ if (r->function == function && r->reg == reg &&
+ (r->index == index || (r->flags & TDX_CPUID_IGNORE_INDEX)))
+ return r->mask;
+ }
+
+ return 0;
}
#define KVM_TDX_CPUID_NO_SUBLEAF ((__u32)-1)
@@ -353,8 +352,6 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i
*/
if (entry->function == 0x80000008)
entry->eax = tdx_set_guest_phys_addr_bits(entry->eax, 0xff);
-
- tdx_clear_unsupported_cpuid(entry);
}
#define TDVMCALLINFO_SETUP_EVENT_NOTIFY_INTERRUPT BIT(1)
@@ -377,8 +374,16 @@ static int init_kvm_tdx_caps(const struct tdx_sys_info_td_conf *td_conf,
caps->user_tdvmcallinfo_1_r11 =
TDVMCALLINFO_SETUP_EVENT_NOTIFY_INTERRUPT;
- for (i = 0; i < td_conf->num_cpuid_config; i++)
- td_init_cpuid_entry2(&caps->cpuid.entries[i], i);
+ for (i = 0; i < td_conf->num_cpuid_config; i++) {
+ struct kvm_cpuid_entry2 *e = &caps->cpuid.entries[i];
+
+ td_init_cpuid_entry2(e, i);
+ /* Only report the configurable bits supported by KVM. */
+ e->eax &= get_supported_cfg_cpuid(e->function, e->index, CPUID_EAX);
+ e->ebx &= get_supported_cfg_cpuid(e->function, e->index, CPUID_EBX);
+ e->ecx &= get_supported_cfg_cpuid(e->function, e->index, CPUID_ECX);
+ e->edx &= get_supported_cfg_cpuid(e->function, e->index, CPUID_EDX);
+ }
return 0;
}
--
2.46.0