qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Xiaoyao Li <xiaoyao.li@intel.com>
To: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Isaku Yamahata" <isaku.yamahata@gmail.com>,
	isaku.yamahata@intel.com, "Gerd Hoffmann" <kraxel@redhat.com>,
	"Daniel P . Berrangé" <berrange@redhat.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Michael S . Tsirkin" <mst@redhat.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Cornelia Huck" <cohuck@redhat.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Laszlo Ersek" <lersek@redhat.com>,
	"Eric Blake" <eblake@redhat.com>
Cc: Connor Kuehl <ckuehl@redhat.com>,
	erdemaktas@google.com, kvm@vger.kernel.org,
	qemu-devel@nongnu.org, seanjc@google.com, xiaoyao.li@intel.com
Subject: [RFC PATCH v4 08/36] i386/tdx: Adjust get_supported_cpuid() for TDX VM
Date: Thu, 12 May 2022 11:17:35 +0800	[thread overview]
Message-ID: <20220512031803.3315890-9-xiaoyao.li@intel.com> (raw)
In-Reply-To: <20220512031803.3315890-1-xiaoyao.li@intel.com>

For TDX, the allowable CPUID configuration differs from what KVM
reports for KVM scope via KVM_GET_SUPPORTED_CPUID.

- Some CPUID bits are not supported for TDX VM while KVM reports the
  support. Mask them off for TDX VM. e.g., CPUID_EXT_VMX, some PV
  features.

- The supported XCR0 and XSS bits needs to be cap'ed by tdx_caps, because
  KVM uses them to setup XFAM of TD.

Introduce tdx_get_supported_cpuid() to adjust the
kvm_arch_get_supported_cpuid() for TDX VM.

Signed-off-by: Xiaoyao Li <xiaoyao.li@intel.com>
---
 target/i386/cpu.h     |  5 +++++
 target/i386/kvm/kvm.c |  4 ++++
 target/i386/kvm/tdx.c | 44 +++++++++++++++++++++++++++++++++++++++++++
 target/i386/kvm/tdx.h |  2 ++
 4 files changed, 55 insertions(+)

diff --git a/target/i386/cpu.h b/target/i386/cpu.h
index 9661f9fbd1c6..0c922e5a305a 100644
--- a/target/i386/cpu.h
+++ b/target/i386/cpu.h
@@ -567,6 +567,11 @@ typedef enum X86Seg {
 #define ESA_FEATURE_XFD_MASK            (1U << ESA_FEATURE_XFD_BIT)
 
 
+#define XCR0_MASK       (XSTATE_FP_MASK | XSTATE_SSE_MASK | XSTATE_YMM_MASK | \
+                         XSTATE_BNDREGS_MASK | XSTATE_BNDCSR_MASK | \
+                         XSTATE_OPMASK_MASK | XSTATE_ZMM_Hi256_MASK | \
+                         XSTATE_Hi16_ZMM_MASK | XSTATE_PKRU_MASK)
+
 /* CPUID feature words */
 typedef enum FeatureWord {
     FEAT_1_EDX,         /* CPUID[1].EDX */
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index f257ffda259d..0751e6e102cc 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -498,6 +498,10 @@ uint32_t kvm_arch_get_supported_cpuid(KVMState *s, uint32_t function,
         ret |= 1U << KVM_HINTS_REALTIME;
     }
 
+    if (is_tdx_vm()) {
+        tdx_get_supported_cpuid(function, index, reg, &ret);
+    }
+
     return ret;
 }
 
diff --git a/target/i386/kvm/tdx.c b/target/i386/kvm/tdx.c
index 803154efdb91..6e3b15ba8a4a 100644
--- a/target/i386/kvm/tdx.c
+++ b/target/i386/kvm/tdx.c
@@ -14,11 +14,22 @@
 #include "qemu/osdep.h"
 #include "qapi/error.h"
 #include "qom/object_interfaces.h"
+#include "standard-headers/asm-x86/kvm_para.h"
 #include "sysemu/kvm.h"
 
 #include "hw/i386/x86.h"
 #include "tdx.h"
 
+#define TDX_SUPPORTED_KVM_FEATURES  ((1ULL << KVM_FEATURE_NOP_IO_DELAY) | \
+                                     (1ULL << KVM_FEATURE_STEAL_TIME) | \
+                                     (1ULL << KVM_FEATURE_PV_EOI) | \
+                                     (1ULL << KVM_FEATURE_PV_UNHALT) | \
+                                     (1ULL << KVM_FEATURE_PV_TLB_FLUSH) | \
+                                     (1ULL << KVM_FEATURE_PV_SEND_IPI) | \
+                                     (1ULL << KVM_FEATURE_POLL_CONTROL) | \
+                                     (1ULL << KVM_FEATURE_PV_SCHED_YIELD) | \
+                                     (1ULL << KVM_FEATURE_MSI_EXT_DEST_ID))
+
 static TdxGuest *tdx_guest;
 
 /* It's valid after kvm_confidential_guest_init()->kvm_tdx_init() */
@@ -121,6 +132,39 @@ int tdx_kvm_init(MachineState *ms, Error **errp)
     return 0;
 }
 
+void tdx_get_supported_cpuid(uint32_t function, uint32_t index, int reg,
+                             uint32_t *ret)
+{
+    switch (function) {
+    case 1:
+        if (reg == R_ECX) {
+            *ret &= ~CPUID_EXT_VMX;
+        }
+        break;
+    case 0xd:
+        if (index == 0) {
+            if (reg == R_EAX) {
+                *ret &= (uint32_t)tdx_caps->xfam_fixed0 & XCR0_MASK;
+                *ret |= (uint32_t)tdx_caps->xfam_fixed1 & XCR0_MASK;
+            } else if (reg == R_EDX) {
+                *ret &= (tdx_caps->xfam_fixed0 & XCR0_MASK) >> 32;
+                *ret |= (tdx_caps->xfam_fixed1 & XCR0_MASK) >> 32;
+            }
+        } else if (index == 1) {
+            /* TODO: Adjust XSS when it's supported. */
+        }
+        break;
+    case KVM_CPUID_FEATURES:
+        if (reg == R_EAX) {
+            *ret &= TDX_SUPPORTED_KVM_FEATURES;
+        }
+        break;
+    default:
+        /* TODO: Use tdx_caps to adjust CPUID leafs. */
+        break;
+    }
+}
+
 /* tdx guest */
 OBJECT_DEFINE_TYPE_WITH_INTERFACES(TdxGuest,
                                    tdx_guest,
diff --git a/target/i386/kvm/tdx.h b/target/i386/kvm/tdx.h
index 4036ca2f3f99..06599b65b827 100644
--- a/target/i386/kvm/tdx.h
+++ b/target/i386/kvm/tdx.h
@@ -27,5 +27,7 @@ bool is_tdx_vm(void);
 #endif /* CONFIG_TDX */
 
 int tdx_kvm_init(MachineState *ms, Error **errp);
+void tdx_get_supported_cpuid(uint32_t function, uint32_t index, int reg,
+                             uint32_t *ret);
 
 #endif /* QEMU_I386_TDX_H */
-- 
2.27.0



  parent reply	other threads:[~2022-05-12  3:31 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-12  3:17 [RFC PATCH v4 00/36] TDX QEMU support Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 01/36] *** HACK *** linux-headers: Update headers to pull in TDX API changes Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 02/36] i386: Introduce tdx-guest object Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 03/36] target/i386: Implement mc->kvm_type() to get VM type Xiaoyao Li
2022-05-23  8:36   ` Gerd Hoffmann
2022-05-23 14:55     ` Isaku Yamahata
2022-05-12  3:17 ` [RFC PATCH v4 04/36] target/i386: Introduce kvm_confidential_guest_init() Xiaoyao Li
2022-05-23  8:37   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 05/36] i386/tdx: Implement tdx_kvm_init() to initialize TDX VM context Xiaoyao Li
2022-05-23  8:38   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 06/36] i386/tdx: Get tdx_capabilities via KVM_TDX_CAPABILITIES Xiaoyao Li
2022-05-12 17:38   ` Isaku Yamahata
2022-05-23  8:45   ` Gerd Hoffmann
2022-05-23 15:30     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 07/36] i386/tdx: Introduce is_tdx_vm() helper and cache tdx_guest object Xiaoyao Li
2022-05-23  8:48   ` Gerd Hoffmann
2022-05-23 14:59     ` Isaku Yamahata
2022-05-24  6:42       ` Gerd Hoffmann
2022-05-12  3:17 ` Xiaoyao Li [this message]
2022-05-23  9:01   ` [RFC PATCH v4 08/36] i386/tdx: Adjust get_supported_cpuid() for TDX VM Gerd Hoffmann
2022-05-23 15:37     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 09/36] KVM: Introduce kvm_arch_pre_create_vcpu() Xiaoyao Li
2022-05-12 17:50   ` Isaku Yamahata
2022-05-13  0:15     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 10/36] i386/kvm: Move architectural CPUID leaf generation to separate helper Xiaoyao Li
2022-05-12 17:48   ` Isaku Yamahata
2022-05-13  0:37     ` Xiaoyao Li
2022-05-23  9:06     ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 11/36] i386/tdx: Initialize TDX before creating TD vcpus Xiaoyao Li
2022-05-23  9:20   ` Gerd Hoffmann
2022-05-23 15:42     ` Xiaoyao Li
2022-05-24  6:57       ` Gerd Hoffmann
2022-06-01  7:20         ` Xiaoyao Li
2022-06-01  7:54           ` Gerd Hoffmann
2022-06-02  1:01             ` Xiaoyao Li
2022-06-07 11:16               ` Gerd Hoffmann
2022-06-08  1:50                 ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 12/36] i386/tdx: Wire CPU features up with attributes of TD guest Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 13/36] i386/tdx: Validate TD attributes Xiaoyao Li
2022-05-23  9:39   ` Gerd Hoffmann
2022-05-24  4:19     ` Xiaoyao Li
2022-05-24  6:59       ` Gerd Hoffmann
2022-05-24  8:11         ` Xiaoyao Li
2022-05-24  8:29           ` Gerd Hoffmann
2022-05-26  3:44             ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 14/36] i386/tdx: Implement user specified tsc frequency Xiaoyao Li
2022-05-12 18:04   ` Isaku Yamahata
2022-05-13  0:46     ` Xiaoyao Li
2022-05-23  9:43   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 15/36] i386/tdx: Set kvm_readonly_mem_enabled to false for TDX VM Xiaoyao Li
2022-05-23  9:45   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 16/36] i386/tdvf: Introduce function to parse TDVF metadata Xiaoyao Li
2022-05-24  7:02   ` Gerd Hoffmann
2022-05-26  2:25     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 17/36] i386/tdx: Parse TDVF metadata for TDX VM Xiaoyao Li
2022-05-24  7:03   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 18/36] i386/tdx: Skip BIOS shadowing setup Xiaoyao Li
2022-05-24  7:08   ` Gerd Hoffmann
2022-05-26  2:48     ` Xiaoyao Li
2022-05-30 11:49       ` Gerd Hoffmann
2022-07-29  7:14         ` Xiaoyao Li
2022-08-16  7:13           ` Gerd Hoffmann
2022-08-16  7:16           ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 19/36] i386/tdx: Don't initialize pc.rom for TDX VMs Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 20/36] i386/tdx: Register a machine_init_done callback for TD Xiaoyao Li
2022-05-24  7:09   ` Gerd Hoffmann
2022-05-26  2:52     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 21/36] i386/tdx: Track mem_ptr for each firmware entry of TDVF Xiaoyao Li
2022-05-24  7:11   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 22/36] i386/tdx: Track RAM entries for TDX VM Xiaoyao Li
2022-05-24  7:37   ` Gerd Hoffmann
2022-05-26  7:33     ` Xiaoyao Li
2022-05-26 18:48       ` Isaku Yamahata
2022-05-27  8:39         ` Xiaoyao Li
2022-05-30 11:59           ` Gerd Hoffmann
2022-05-31  2:09             ` Xiaoyao Li
2022-05-27  8:36       ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 23/36] i386/tdx: Setup the TD HOB list Xiaoyao Li
2022-05-12 18:33   ` Isaku Yamahata
2022-05-24  7:56   ` Gerd Hoffmann
2022-06-02  9:27     ` Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 24/36] i386/tdx: Add TDVF memory via KVM_TDX_INIT_MEM_REGION Xiaoyao Li
2022-05-12 18:34   ` Isaku Yamahata
2022-05-13  0:46     ` Xiaoyao Li
2022-05-24  7:57   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 25/36] i386/tdx: Call KVM_TDX_INIT_VCPU to initialize TDX vcpu Xiaoyao Li
2022-05-24  7:59   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 26/36] i386/tdx: Finalize TDX VM Xiaoyao Li
2022-05-24  7:59   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 27/36] i386/tdx: Disable SMM for TDX VMs Xiaoyao Li
2022-05-24  8:00   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 28/36] i386/tdx: Disable PIC " Xiaoyao Li
2022-05-24  8:00   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 29/36] i386/tdx: Don't allow system reset " Xiaoyao Li
2022-05-24  8:01   ` Gerd Hoffmann
2022-05-12  3:17 ` [RFC PATCH v4 30/36] hw/i386: add eoi_intercept_unsupported member to X86MachineState Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 31/36] hw/i386: add option to forcibly report edge trigger in acpi tables Xiaoyao Li
2022-05-12  3:17 ` [RFC PATCH v4 32/36] i386/tdx: Don't synchronize guest tsc for TDs Xiaoyao Li
2022-05-24  8:04   ` Gerd Hoffmann
2022-05-12  3:18 ` [RFC PATCH v4 33/36] i386/tdx: Only configure MSR_IA32_UCODE_REV in kvm_init_msrs() " Xiaoyao Li
2022-05-24  8:05   ` Gerd Hoffmann
2022-05-12  3:18 ` [RFC PATCH v4 34/36] i386/tdx: Skip kvm_put_apicbase() " Xiaoyao Li
2022-05-12  3:18 ` [RFC PATCH v4 35/36] i386/tdx: Don't get/put guest state for TDX VMs Xiaoyao Li
2022-05-12  3:18 ` [RFC PATCH v4 36/36] docs: Add TDX documentation Xiaoyao Li
2022-05-12 18:42   ` Isaku Yamahata

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220512031803.3315890-9-xiaoyao.li@intel.com \
    --to=xiaoyao.li@intel.com \
    --cc=berrange@redhat.com \
    --cc=ckuehl@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=eblake@redhat.com \
    --cc=erdemaktas@google.com \
    --cc=f4bug@amsat.org \
    --cc=isaku.yamahata@gmail.com \
    --cc=isaku.yamahata@intel.com \
    --cc=kraxel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=lersek@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).