180 lines
6.6 KiB
Diff
180 lines
6.6 KiB
Diff
From 8f50b918a55f8a30e782eef028a625a209a19fde Mon Sep 17 00:00:00 2001
|
|
From: Paolo Bonzini <pbonzini@redhat.com>
|
|
Date: Fri, 18 Jul 2025 18:03:48 +0200
|
|
Subject: [PATCH 073/115] i386/tdx: Implement adjust_cpuid_features() for TDX
|
|
|
|
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
RH-MergeRequest: 391: TDX support, including attestation and device assignment
|
|
RH-Jira: RHEL-15710 RHEL-20798 RHEL-49728
|
|
RH-Acked-by: Yash Mankad <None>
|
|
RH-Acked-by: Peter Xu <peterx@redhat.com>
|
|
RH-Acked-by: David Hildenbrand <david@redhat.com>
|
|
RH-Commit: [73/115] 115b0cd2bbc276b2cf915e9ad48fd89c4ee7ce8e (bonzini/rhel-qemu-kvm)
|
|
|
|
Maintain a TDX specific supported CPUID set, and use it to mask the
|
|
common supported CPUID value of KVM. It can avoid newly added supported
|
|
features (reported via KVM_GET_SUPPORTED_CPUID) for common VMs being
|
|
falsely reported as supported for TDX.
|
|
|
|
As the first step, initialize the TDX supported CPUID set with all the
|
|
configurable CPUID bits. It's not complete because there are other CPUID
|
|
bits are supported for TDX but not reported as directly configurable.
|
|
E.g. the XFAM related bits, attribute related bits and fixed-1 bits.
|
|
They will be handled in the future.
|
|
|
|
Also, what matters are the CPUID bits related to QEMU's feature word.
|
|
Only mask the CPUID leafs which are feature word leaf.
|
|
|
|
Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
|
|
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
|
|
Link: https://lore.kernel.org/r/20250508150002.689633-45-xiaoyao.li@intel.com
|
|
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
(cherry picked from commit 75ec6189f5c65cab210dd9f16cf4eef368038d45)
|
|
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
|
|
---
|
|
target/i386/cpu.c | 16 ++++++++++++++++
|
|
target/i386/cpu.h | 1 +
|
|
target/i386/kvm/kvm.c | 2 +-
|
|
target/i386/kvm/kvm_i386.h | 1 +
|
|
target/i386/kvm/tdx.c | 34 ++++++++++++++++++++++++++++++++++
|
|
5 files changed, 53 insertions(+), 1 deletion(-)
|
|
|
|
diff --git a/target/i386/cpu.c b/target/i386/cpu.c
|
|
index ab34626b19..2da456da64 100644
|
|
--- a/target/i386/cpu.c
|
|
+++ b/target/i386/cpu.c
|
|
@@ -1644,6 +1644,22 @@ FeatureWordInfo feature_word_info[FEATURE_WORDS] = {
|
|
},
|
|
};
|
|
|
|
+bool is_feature_word_cpuid(uint32_t feature, uint32_t index, int reg)
|
|
+{
|
|
+ FeatureWordInfo *wi;
|
|
+ FeatureWord w;
|
|
+
|
|
+ for (w = 0; w < FEATURE_WORDS; w++) {
|
|
+ wi = &feature_word_info[w];
|
|
+ if (wi->type == CPUID_FEATURE_WORD && wi->cpuid.eax == feature &&
|
|
+ (!wi->cpuid.needs_ecx || wi->cpuid.ecx == index) &&
|
|
+ wi->cpuid.reg == reg) {
|
|
+ return true;
|
|
+ }
|
|
+ }
|
|
+ return false;
|
|
+}
|
|
+
|
|
typedef struct FeatureMask {
|
|
FeatureWord index;
|
|
uint64_t mask;
|
|
diff --git a/target/i386/cpu.h b/target/i386/cpu.h
|
|
index 83fa89bf0a..601e828577 100644
|
|
--- a/target/i386/cpu.h
|
|
+++ b/target/i386/cpu.h
|
|
@@ -2431,6 +2431,7 @@ void cpu_set_apic_feature(CPUX86State *env);
|
|
void host_cpuid(uint32_t function, uint32_t count,
|
|
uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx);
|
|
bool cpu_has_x2apic_feature(CPUX86State *env);
|
|
+bool is_feature_word_cpuid(uint32_t feature, uint32_t index, int reg);
|
|
|
|
static inline bool x86_has_cpuid_0x1f(X86CPU *cpu)
|
|
{
|
|
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
|
|
index 5349ff4db7..76352323e4 100644
|
|
--- a/target/i386/kvm/kvm.c
|
|
+++ b/target/i386/kvm/kvm.c
|
|
@@ -385,7 +385,7 @@ static bool host_tsx_broken(void)
|
|
|
|
/* Returns the value for a specific register on the cpuid entry
|
|
*/
|
|
-static uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg)
|
|
+uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg)
|
|
{
|
|
uint32_t ret = 0;
|
|
switch (reg) {
|
|
diff --git a/target/i386/kvm/kvm_i386.h b/target/i386/kvm/kvm_i386.h
|
|
index f2fbf3f99f..797610496a 100644
|
|
--- a/target/i386/kvm/kvm_i386.h
|
|
+++ b/target/i386/kvm/kvm_i386.h
|
|
@@ -62,6 +62,7 @@ void kvm_update_msi_routes_all(void *private, bool global,
|
|
struct kvm_cpuid_entry2 *cpuid_find_entry(struct kvm_cpuid2 *cpuid,
|
|
uint32_t function,
|
|
uint32_t index);
|
|
+uint32_t cpuid_entry_get_reg(struct kvm_cpuid_entry2 *entry, int reg);
|
|
uint32_t kvm_x86_build_cpuid(CPUX86State *env, struct kvm_cpuid_entry2 *entries,
|
|
uint32_t cpuid_i);
|
|
#endif /* CONFIG_KVM */
|
|
diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
|
|
index 58fb68cab0..4949d01f22 100644
|
|
--- a/target/i386/kvm/tdx.c
|
|
+++ b/target/i386/kvm/tdx.c
|
|
@@ -45,6 +45,7 @@
|
|
static TdxGuest *tdx_guest;
|
|
|
|
static struct kvm_tdx_capabilities *tdx_caps;
|
|
+static struct kvm_cpuid2 *tdx_supported_cpuid;
|
|
|
|
/* Valid after kvm_arch_init()->confidential_guest_kvm_init()->tdx_kvm_init() */
|
|
bool is_tdx_vm(void)
|
|
@@ -366,6 +367,20 @@ static Notifier tdx_machine_done_notify = {
|
|
.notify = tdx_finalize_vm,
|
|
};
|
|
|
|
+static void tdx_setup_supported_cpuid(void)
|
|
+{
|
|
+ if (tdx_supported_cpuid) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ tdx_supported_cpuid = g_malloc0(sizeof(*tdx_supported_cpuid) +
|
|
+ KVM_MAX_CPUID_ENTRIES * sizeof(struct kvm_cpuid_entry2));
|
|
+
|
|
+ memcpy(tdx_supported_cpuid->entries, tdx_caps->cpuid.entries,
|
|
+ tdx_caps->cpuid.nent * sizeof(struct kvm_cpuid_entry2));
|
|
+ tdx_supported_cpuid->nent = tdx_caps->cpuid.nent;
|
|
+}
|
|
+
|
|
static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
|
|
{
|
|
MachineState *ms = MACHINE(qdev_get_machine());
|
|
@@ -403,6 +418,8 @@ static int tdx_kvm_init(ConfidentialGuestSupport *cgs, Error **errp)
|
|
}
|
|
}
|
|
|
|
+ tdx_setup_supported_cpuid();
|
|
+
|
|
/* TDX relies on KVM_HC_MAP_GPA_RANGE to handle TDG.VP.VMCALL<MapGPA> */
|
|
if (!kvm_enable_hypercall(BIT_ULL(KVM_HC_MAP_GPA_RANGE))) {
|
|
return -EOPNOTSUPP;
|
|
@@ -440,6 +457,22 @@ static void tdx_cpu_instance_init(X86ConfidentialGuest *cg, CPUState *cpu)
|
|
x86cpu->enable_cpuid_0x1f = true;
|
|
}
|
|
|
|
+static uint32_t tdx_adjust_cpuid_features(X86ConfidentialGuest *cg,
|
|
+ uint32_t feature, uint32_t index,
|
|
+ int reg, uint32_t value)
|
|
+{
|
|
+ struct kvm_cpuid_entry2 *e;
|
|
+
|
|
+ if (is_feature_word_cpuid(feature, index, reg)) {
|
|
+ e = cpuid_find_entry(tdx_supported_cpuid, feature, index);
|
|
+ if (e) {
|
|
+ value &= cpuid_entry_get_reg(e, reg);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ return value;
|
|
+}
|
|
+
|
|
static int tdx_validate_attributes(TdxGuest *tdx, Error **errp)
|
|
{
|
|
if ((tdx->attributes & ~tdx_caps->supported_attrs)) {
|
|
@@ -834,4 +867,5 @@ static void tdx_guest_class_init(ObjectClass *oc, void *data)
|
|
klass->kvm_init = tdx_kvm_init;
|
|
x86_klass->kvm_type = tdx_kvm_type;
|
|
x86_klass->cpu_instance_init = tdx_cpu_instance_init;
|
|
+ x86_klass->adjust_cpuid_features = tdx_adjust_cpuid_features;
|
|
}
|
|
--
|
|
2.50.1
|
|
|