Add function 0x23 to the set of CPUID functions that are indexed, as per the SDM and the many sub-leafs defined in leaf_types.h. Reported-by: Rick Edgecombe Closes: https://lore.kernel.org/all/213d614fe73e183a230c8f4e0c8fa1cc3d45df39.camel@intel.com Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/include/asm/cpuid/api.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/include/asm/cpuid/api.h b/arch/x86/include/asm/cpuid/api.h index 82eddfa2347b..25b22b173f2a 100644 --- a/arch/x86/include/asm/cpuid/api.h +++ b/arch/x86/include/asm/cpuid/api.h @@ -181,6 +181,7 @@ static __always_inline bool cpuid_function_is_indexed(u32 function) case 0x1d: case 0x1e: case 0x1f: + case 0x23: case 0x24: case 0x8000001d: return true; -- 2.56.0.257.gf7497c7d27-goog From: Changyuan Lyu Set the KVM_CPUID_FLAG_SIGNIFCANT_INDEX flag in the kvm_cpuid_entry2 structures returned by KVM_TDX_CAPABILITIES if the CPUID is indexed, otherwise KVM could confuse userspace and induce a configuration error. This also ensures consistency with the CPUID entries returned by KVM_GET_SUPPORTED_CPUID. WARN if KVM and the TDX Module don't align on which functions have significant indices, e.g. to help ensure the kernel is updated when the TDX Module learns about new CPUID leaves, and vice versa. Opportunistically add a comment to call out that zeroing the index when it isn't significant is ABI that userspace relies on, e.g. QEMU looks for '0' instead of checking KVM_CPUID_FLAG_SIGNIFCANT_INDEX. Fixes: 61bb28279623 ("KVM: TDX: Get system-wide info about TDX module on initialization") Cc: stable@vger.kernel.org Cc: Binbin Wu Suggested-by: Sean Christopherson Signed-off-by: Changyuan Lyu [sean: keep the WARN, don't key off KVM_TDX_CPUID_NO_SUBLEAF, add comment] Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/tdx.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 271c2f94b8dd..37d088f6f7a8 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -163,12 +164,23 @@ static void td_init_cpuid_entry2(struct kvm_cpuid_entry2 *entry, unsigned char i entry->function = (u32)td_conf->cpuid_config_leaves[idx]; entry->index = td_conf->cpuid_config_leaves[idx] >> 32; + entry->flags = 0; entry->eax = (u32)td_conf->cpuid_config_values[idx][0]; entry->ebx = td_conf->cpuid_config_values[idx][0] >> 32; entry->ecx = (u32)td_conf->cpuid_config_values[idx][1]; entry->edx = td_conf->cpuid_config_values[idx][1] >> 32; - if (entry->index == KVM_TDX_CPUID_NO_SUBLEAF) + WARN_ON_ONCE(cpuid_function_is_indexed(entry->function) != + (entry->index != KVM_TDX_CPUID_NO_SUBLEAF)); + + /* + * Explicitly zero the index when it's not significant, even though it + * _should_ be meaningless. Userspace relies on the index being zero + * (the ABI was inherited from KVM_GET_SUPPORTED_CPUID). + */ + if (cpuid_function_is_indexed(entry->function)) + entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; + else entry->index = 0; /* -- 2.56.0.257.gf7497c7d27-goog Reject KVM_TDX_INIT_VM if userspace doesn't configure the canonical CPUID entries correctly with respect to KVM_CPUID_FLAG_SIGNIFCANT_INDEX to fix a bug where userspace could bypass the TSX and WAITPKG checks by simply not marking a CPUID.0x7 entry as being indexed. This also sets the stage for more in-depth validation of the incoming CPUID configuration. This is obviously an ABI change and diverges from KVM's general approach of "let userspace do what it wants". In practice, no "real" VMM that supports TDX misconfigures a TD's CPUID entries in this way, i.e. the probability of actually breaking userspace is basically nil. And unlike normal VMs, where KVM has full control and thus can deal with bizarre CPUID input without too much fuss, TDX VMs require extra care due to the TDX Module also reacting to the CPUID configuration, i.e. KVM_TDX_INIT_VM needs to be more strict than KVM_SET_CPUID{,2} to avoid creating potentially dangerous-to-the-host setups. Opportunistically rename the helper to make it more obvious that it's checking if the passed in @entry is unsupported, as opposed to returning a set of unsupported entries or something. Fixes: 6d415778f106 ("KVM: TDX: Disable support for TSX and WAITPKG") Reported-by: Sashiko Closes: https://lore.kernel.org/kvm/20260604024927.E22311F00893@smtp.kernel.org Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/tdx.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 37d088f6f7a8..6abf92ffec24 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -151,8 +151,12 @@ static void tdx_clear_unsupported_cpuid(struct kvm_cpuid_entry2 *entry) clear_waitpkg(entry); } -static bool tdx_unsupported_cpuid(const struct kvm_cpuid_entry2 *entry) +static bool tdx_is_unsupported_cpuid_entry(const struct kvm_cpuid_entry2 *entry) { + if (cpuid_function_is_indexed(entry->function) != + !!(entry->flags & KVM_CPUID_FLAG_SIGNIFCANT_INDEX)) + return true; + return has_tsx(entry) || has_waitpkg(entry); } @@ -2393,7 +2397,7 @@ static int setup_tdparams_cpuids(struct kvm_cpuid2 *cpuid, if (!entry) continue; - if (tdx_unsupported_cpuid(entry)) + if (tdx_is_unsupported_cpuid_entry(entry)) return -EINVAL; copy_cnt++; -- 2.56.0.257.gf7497c7d27-goog