[PATCH 25/25] KVM: x86: Add CPUID bits missing from KVM_GET_SUPPORTED_CPUID

Rick Edgecombe
2024-08-12T22:48:50Z

Originally, the plan was to filter the directly configurable CPUID bits
exposed by KVM_TDX_CAPABILITIES, and the final configured bit values
provided by KVM_TDX_GET_CPUID. However, several issues were found with
this. Both the filtering done with KVM_TDX_CAPABILITIES and
KVM_TDX_GET_CPUID had the issue that the get_supported_cpuid() provided
default values instead of supported masks for multi-bit fields (i.e. those
encoding a multi-bit number).

For KVM_TDX_CAPABILITIES, there was also the problem of bits that are
actually supported by KVM, but missing from get_supported_cpuid() for one
reason or another. These include X86_FEATURE_MWAIT, X86_FEATURE_HT and
X86_FEATURE_TSC_DEADLINE_TIMER. This is currently worked around in QEMU by
adjusting which features are expected. Some of these are going to be added
to get_supported_cpuid(), and that is probably the right long term fix.

For KVM_TDX_GET_CPUID, there is another problem. Some CPUID bits are fixed
on by the TDX module, but unsupported by KVM. This means that the TD will
have them set, but KVM and userspace won't know about them. This class of
bits is dealt with by having QEMU expect not to see them. The bits include:
X86_FEATURE_HYPERVISOR. The proper fix for this specifically is probably to
change KVM to show it as supported (currently a patch exists). But this
scenario could be expected in the end of TDX module ever setting and
default 1, or fixed 1 bits. It would be good to have discussion on whether
KVM community should mandate that this doesn't happen.

Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
---
uAPI breakout v1:
- New patch
---
arch/x86/kvm/vmx/tdx.c | 96 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 95 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index d45b4f7b69ba..34e838d8f7fd 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1086,13 +1086,24 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
return ret;
}

+/*
+ * This function is used in two cases:
+ * 1. mask KVM unsupported/unknown bits from the configurable CPUIDs reported
+ * by TDX module. in setup_kvm_tdx_caps().
+ * 2. mask KVM unsupported/unknown bits from the actual CPUID value of TD that
+ * read from TDX module. in tdx_vcpu_get_cpuid().
+ *
+ * For both cases, it needs fixup for the field that consists of multiple bits.
+ * For multi-bits field, we need a mask however what
+ * kvm_get_supported_cpuid_internal() returns is just a default value.
+ */
static int tdx_get_kvm_supported_cpuid(struct kvm_cpuid2 **cpuid)
{
-
int r;
static const u32 funcs[] = {
0, 0x80000000, KVM_CPUID_SIGNATURE,
};
+ struct kvm_cpuid_entry2 *entry;

*cpuid = kzalloc(sizeof(struct kvm_cpuid2) +
sizeof(struct kvm_cpuid_entry2) * KVM_MAX_CPUID_ENTRIES,
@@ -1104,6 +1115,89 @@ static int tdx_get_kvm_supported_cpuid(struct kvm_cpuid2 **cpuid)
if (r)
goto err;

+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x0, 0);
+ if (WARN_ON(!entry))
+ goto err;
+ /* Fixup of maximum basic leaf */
+ entry->eax |= 0x000000FF;
+
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x1, 0);
+ if (WARN_ON(!entry))
+ goto err;
+ /* Fixup of FMS */
+ entry->eax |= 0x0fff3fff;
+ /* Fixup of maximum logical processors per package */
+ entry->ebx |= 0x00ff0000;
+
+ /*
+ * Fixup of CPUID leaf 4, which enmerates cache info, all of the
+ * non-reserved fields except EBX[11:0] (System Coherency Line Size)
+ * are configurable for TDs.
+ */
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x4, 0);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax |= 0xffffc3ff;
+ entry->ebx |= 0xfffff000;
+ entry->ecx |= 0xffffffff;
+ entry->edx |= 0x00000007;
+
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x4, 1);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax |= 0xffffc3ff;
+ entry->ebx |= 0xfffff000;
+ entry->ecx |= 0xffffffff;
+ entry->edx |= 0x00000007;
+
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x4, 2);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax |= 0xffffc3ff;
+ entry->ebx |= 0xfffff000;
+ entry->ecx |= 0xffffffff;
+ entry->edx |= 0x00000007;
+
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x4, 3);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax |= 0xffffc3ff;
+ entry->ebx |= 0xfffff000;
+ entry->ecx |= 0xffffffff;
+ entry->edx |= 0x00000007;
+
+ /* Fixup of CPUID leaf 0xB */
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0xb, 0);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax = 0x0000001f;
+ entry->ebx = 0x0000ffff;
+ entry->ecx = 0x0000ffff;
+
+ /*
+ * Fixup of CPUID leaf 0x1f, which is totally configurable for TDs.
+ */
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x1f, 0);
+ if (WARN_ON(!entry))
+ goto err;
+ entry->eax = 0x0000001f;
+ entry->ebx = 0x0000ffff;
+ entry->ecx = 0x0000ffff;
+
+ for (int i = 1; i <= 5; i++) {
+ entry = kvm_find_cpuid_entry2((*cpuid)->entries, (*cpuid)->nent, 0x1f, i);
+ if (!entry) {
+ entry = &(*cpuid)->entries[(*cpuid)->nent];
+ entry->function = 0x1f;
+ entry->index = i;
+ entry->flags = KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+ (*cpuid)->nent++;
+ }
+ entry->eax = 0x0000001f;
+ entry->ebx = 0x0000ffff;
+ entry->ecx = 0x0000ffff;
+ }
+
return 0;
err:
kfree(*cpuid);
--
2.34.1