mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xu Yilun <yilun.xu@linux.intel.com>
To: kas@kernel.org, djbw@kernel.org, rick.p.edgecombe@intel.com,
	x86@kernel.org, peter.fang@intel.com
Cc: linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org,
	kvm@vger.kernel.org, sohil.mehta@intel.com, yilun.xu@intel.com,
	yilun.xu@linux.intel.com, baolu.lu@linux.intel.com,
	zhenzhong.duan@intel.com, xiaoyao.li@intel.com
Subject: [RFC PATCH 12/15] KVM: TDX: Add in-kernel Quote generation
Date: Fri, 22 May 2026 11:41:25 +0800	[thread overview]
Message-ID: <20260522034128.3144354-13-yilun.xu@linux.intel.com> (raw)
In-Reply-To: <20260522034128.3144354-1-yilun.xu@linux.intel.com>

From: Peter Fang <peter.fang@intel.com>

Provide an in-kernel path for TDX Quote generation when handling
TDG.VP.VMCALL<GetQuote>, without requiring an exit to userspace.

Use the core TDX API when the TDX Quoting extension is available. For
simplicity, each KVM guest checks for availability only once during
initialization. KVM does not handle Quoting service disruptions.

Signed-off-by: Peter Fang <peter.fang@intel.com>
Signed-off-by: Xu Yilun <yilun.xu@linux.intel.com>
---
 arch/x86/include/asm/tdx.h |   9 +++
 arch/x86/kvm/vmx/tdx.h     |   6 ++
 arch/x86/kvm/vmx/tdx.c     | 135 ++++++++++++++++++++++++++++++++++++-
 virt/kvm/kvm_main.c        |   1 +
 4 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 945e6817abb2..5863d6748100 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -115,6 +115,15 @@ struct tdx_quote_req {
 	u32 out_len;
 	u8 data[];
 };
+
+#define TDX_QUOTE_REQ_HDR_SIZE		(offsetof(struct tdx_quote_req, data))
+
+/*
+ * TDG.VP.VMCALL<GetQuote> Status Codes
+ */
+#define TDX_QUOTE_STATUS_SUCCESS	0x0000000000000000ULL
+#define TDX_QUOTE_STATUS_ERROR		0x8000000000000000ULL
+#define TDX_QUOTE_STATUS_UNAVAILABLE	0x8000000000000001ULL
 #endif /* CONFIG_INTEL_TDX_GUEST || CONFIG_KVM_INTEL_TDX */
 
 #ifdef CONFIG_INTEL_TDX_HOST
diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h
index ac8323a68b16..18c93e80c0ec 100644
--- a/arch/x86/kvm/vmx/tdx.h
+++ b/arch/x86/kvm/vmx/tdx.h
@@ -47,6 +47,12 @@ struct kvm_tdx {
 	 * Set/unset is protected with kvm->mmu_lock.
 	 */
 	bool wait_for_sept_zap;
+
+	/*
+	 * Whether to get TDX quote directly in kernel, without exiting to
+	 * userspace.
+	 */
+	bool get_quote_in_kernel;
 };
 
 /* TDX module vCPU states */
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 9f7c39e0d4b5..bade046da5a1 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1538,11 +1538,133 @@ static int tdx_get_quote_user(struct kvm_vcpu *vcpu, u64 gpa, u64 size)
 	return 0;
 }
 
+static bool write_quote_status_to_guest(struct kvm_vcpu *vcpu, u64 status,
+					gpa_t gpa)
+{
+	if (kvm_vcpu_write_guest(vcpu,
+				 gpa + offsetof(struct tdx_quote_req, status),
+				 &status, sizeof(status)))
+		return false;
+
+	return true;
+}
+
+static bool write_quote_to_guest(struct kvm_vcpu *vcpu, void *quote_data,
+				 u32 quote_len, gpa_t gpa)
+{
+	if (kvm_vcpu_write_guest(vcpu,
+				 gpa + TDX_QUOTE_REQ_HDR_SIZE,
+				 quote_data, quote_len))
+		return false;
+
+	if (kvm_vcpu_write_guest(vcpu,
+				 gpa + offsetof(struct tdx_quote_req, out_len),
+				 &quote_len, sizeof(quote_len)))
+		return false;
+
+	return true;
+}
+
+static u64 __get_quote_kernel(struct kvm_vcpu *vcpu, struct tdx_quote_req *req,
+			      size_t req_len, gpa_t req_gpa, size_t total_len)
+{
+	struct tdx_td *td = &to_kvm_tdx(vcpu->kvm)->td;
+
+	/* Only support version 1 as defined in the GHCI spec */
+	if (req->version != 1)
+		return TDX_QUOTE_STATUS_ERROR;
+
+	if ((size_t)req->in_len + TDX_QUOTE_REQ_HDR_SIZE > req_len)
+		return TDX_QUOTE_STATUS_ERROR;
+
+	/* The caller frees the quote data */
+	void *quote_data __free(kvfree) =
+		tdx_quote_generate(td, req->data, req->in_len, &req->out_len);
+
+	if (!quote_data)
+		return TDX_QUOTE_STATUS_UNAVAILABLE;
+
+	if ((size_t)req->out_len + TDX_QUOTE_REQ_HDR_SIZE > total_len)
+		return TDX_QUOTE_STATUS_ERROR;
+
+	if (!write_quote_to_guest(vcpu, quote_data, req->out_len, req_gpa))
+		return TDX_QUOTE_STATUS_ERROR;
+
+	return TDX_QUOTE_STATUS_SUCCESS;
+}
+
+static u64 tdx_get_quote_check_args(struct kvm_vcpu *vcpu, u64 gpa, u64 size)
+{
+	gfn_t gfn_start, gfn_end;
+	u64 end;
+
+	if (!size)
+		return TDVMCALL_STATUS_INVALID_OPERAND;
+
+	if (!PAGE_ALIGNED(gpa) || !PAGE_ALIGNED(size))
+		return TDVMCALL_STATUS_ALIGN_ERROR;
+
+	if (check_add_overflow(gpa, size, &end))
+		return TDVMCALL_STATUS_INVALID_OPERAND;
+
+	gfn_start = gpa_to_gfn(gpa);
+	gfn_end = gpa_to_gfn(end);
+
+	/*
+	 * Reject if the guest didn't explicitly convert its quote pages to
+	 * shared.
+	 */
+	if (!kvm_range_has_memory_attributes(vcpu->kvm, gfn_start, gfn_end,
+					     KVM_MEMORY_ATTRIBUTE_PRIVATE, 0))
+		return TDVMCALL_STATUS_INVALID_OPERAND;
+
+	return TDVMCALL_STATUS_SUCCESS;
+}
+
+static int tdx_get_quote_kernel(struct kvm_vcpu *vcpu, u64 gpa, u64 size)
+{
+	void *first_page = NULL;
+	u64 err, qerr;
+
+	err = tdx_get_quote_check_args(vcpu, gpa, size);
+	if (err != TDVMCALL_STATUS_SUCCESS)
+		goto out;
+
+	err = TDVMCALL_STATUS_INVALID_OPERAND;
+
+	first_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!first_page)
+		goto out;
+
+	/*
+	 * Read the first GetQuote page for its header + in_data. The check
+	 * above ensures that this GetQuote message is at least one page in
+	 * size. in_data spanning more than a page is not supported.
+	 */
+	if (kvm_vcpu_read_guest(vcpu, gpa, first_page, PAGE_SIZE))
+		goto out;
+
+	qerr = __get_quote_kernel(vcpu, first_page, PAGE_SIZE,
+				  (gpa_t)gpa, size);
+
+	if (write_quote_status_to_guest(vcpu, qerr, (gpa_t)gpa) &&
+	    qerr == TDX_QUOTE_STATUS_SUCCESS)
+		err = TDVMCALL_STATUS_SUCCESS;
+
+out:
+	kfree(first_page);
+	tdvmcall_set_return_code(vcpu, err);
+
+	return 1;
+}
+
 static int tdx_get_quote(struct kvm_vcpu *vcpu)
 {
+	struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm);
 	struct vcpu_tdx *tdx = to_tdx(vcpu);
 	u64 gpa = tdx->vp_enter_args.r12;
 	u64 size = tdx->vp_enter_args.r13;
+	int ret;
 
 	/* The gpa of buffer must have shared bit set. */
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
@@ -1552,7 +1674,12 @@ static int tdx_get_quote(struct kvm_vcpu *vcpu)
 
 	gpa &= ~gfn_to_gpa(kvm_gfn_direct_bits(vcpu->kvm));
 
-	return tdx_get_quote_user(vcpu, gpa, size);
+	if (kvm_tdx->get_quote_in_kernel)
+		ret = tdx_get_quote_kernel(vcpu, gpa, size);
+	else
+		ret = tdx_get_quote_user(vcpu, gpa, size);
+
+	return ret;
 }
 
 static int tdx_setup_event_notify_interrupt(struct kvm_vcpu *vcpu)
@@ -2751,6 +2878,12 @@ static int tdx_td_init(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
 	else
 		kvm->arch.gfn_direct_bits = TDX_SHARED_BIT_PWL_4;
 
+	/*
+	 * Check only once at TD creation. If the quoting service gets disrupted
+	 * during TD runtime, let the user handle it.
+	 */
+	kvm_tdx->get_quote_in_kernel = tdx_quote_enabled();
+
 	kvm_tdx->state = TD_STATE_INITIALIZED;
 out:
 	/* kfree() accepts NULL. */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 89489996fbc1..599f88a13071 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2461,6 +2461,7 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
 
 	return true;
 }
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_range_has_memory_attributes);
 
 static __always_inline void kvm_handle_gfn_range(struct kvm *kvm,
 						 struct kvm_mmu_notifier_range *range)
-- 
2.25.1


  parent reply	other threads:[~2026-05-22  4:05 UTC|newest]

Thread overview: 127+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22  3:41 [PATCH 00/15] Enable TDX Module Extensions and DICE-based TDX Quoting Xu Yilun
2026-05-22  3:41 ` [PATCH 01/15] x86/virt/tdx: Read global metadata for TDX Module Extensions Xu Yilun
2026-05-25  6:24   ` Xiaoyao Li
2026-05-25  6:54   ` Xiaoyao Li
2026-05-27 15:35     ` Kiryl Shutsemau
2026-05-28  4:25       ` Xu Yilun
2026-05-28 21:17         ` Edgecombe, Rick P
2026-05-29 15:34           ` Xu Yilun
2026-05-27  6:05   ` Sohil Mehta
2026-05-27  7:11     ` Xu Yilun
2026-05-27 17:17       ` Sohil Mehta
2026-05-28  3:48         ` Xu Yilun
2026-05-28 21:00   ` Edgecombe, Rick P
2026-05-29 16:59     ` Xu Yilun
2026-06-09 13:06   ` Adrian Hunter
2026-06-10  3:20     ` Xu Yilun
2026-06-12 22:20   ` Dan Williams (nvidia)
2026-06-15 15:24     ` Xu Yilun
2026-06-15 16:05     ` Dave Hansen
2026-05-22  3:41 ` [PATCH 02/15] x86/virt/tdx: Add extra memory to TDX Module for Extensions Xu Yilun
2026-05-25  8:56   ` Xiaoyao Li
2026-05-27  3:47     ` Xu Yilun
2026-05-27  6:38       ` Xiaoyao Li
2026-05-27  7:32         ` Xu Yilun
2026-05-27  8:18           ` Xiaoyao Li
2026-06-07  4:38   ` Kishen Maloor
2026-06-08  9:41     ` Xu Yilun
2026-06-09 13:38   ` Adrian Hunter
2026-06-10  5:13     ` Xu Yilun
2026-06-10  5:43       ` Adrian Hunter
2026-06-10  7:44         ` Xu Yilun
2026-06-12 23:49   ` Dan Williams (nvidia)
2026-06-15 15:55     ` Xu Yilun
2026-05-22  3:41 ` [PATCH 03/15] x86/virt/tdx: Make TDX Module initialize Extensions Xu Yilun
2026-05-25  8:58   ` Xiaoyao Li
2026-06-05  8:46   ` Tony Lindgren
2026-06-09 15:14   ` Adrian Hunter
2026-06-10  8:09     ` Xu Yilun
2026-05-22  3:41 ` [PATCH 04/15] x86/virt/tdx: Enable the Extensions right after basic TDX Module init Xu Yilun
2026-05-25  6:00   ` Tony Lindgren
2026-05-27  4:02     ` Xu Yilun
2026-05-25  8:05   ` Xiaoyao Li
2026-05-28 21:32   ` Edgecombe, Rick P
2026-05-29 17:19     ` Xu Yilun
2026-06-07  4:38   ` Kishen Maloor
2026-06-08 10:12     ` Xu Yilun
2026-06-14  7:00       ` Peter Fang
2026-06-13  0:08   ` Dan Williams (nvidia)
2026-06-15 15:58     ` Xu Yilun
2026-05-22  3:41 ` [RFC PATCH 05/15] x86/virt/tdx: Move tdx_tdr_pa() up in the file Xu Yilun
2026-05-28 21:32   ` Edgecombe, Rick P
2026-06-11 16:21   ` Adrian Hunter
2026-06-14  7:04     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 06/15] x86/virt/tdx: Initialize Quoting extension during bringup Xu Yilun
2026-05-28 21:35   ` Edgecombe, Rick P
2026-06-14  7:10     ` Peter Fang
2026-06-11 16:22   ` Adrian Hunter
2026-06-14  7:20     ` Peter Fang
2026-06-13  0:00   ` Dan Williams (nvidia)
2026-06-14  7:50     ` Peter Fang
2026-06-29 18:11       ` Edgecombe, Rick P
2026-07-02  9:48         ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 07/15] x86/virt/tdx: Prepare Quote buffer during extension bringup Xu Yilun
2026-05-28 22:30   ` Edgecombe, Rick P
2026-06-14 10:28     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 08/15] x86/virt/tdx: Add interface to check Quoting availability Xu Yilun
2026-07-01 11:25   ` Nikolay Borisov
2026-07-02  0:19     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 09/15] x86/virt/tdx: Add interface to generate a Quote Xu Yilun
2026-05-28 22:30   ` Edgecombe, Rick P
2026-06-14 11:29     ` Peter Fang
2026-06-26  9:58       ` Peter Fang
2026-06-11 17:15   ` Adrian Hunter
2026-06-14 11:36     ` Peter Fang
2026-07-01 11:46   ` Nikolay Borisov
2026-07-02 15:58     ` Xu Yilun
2026-07-07  3:45     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 10/15] x86/tdx: Move and rename Quote request structure Xu Yilun
2026-06-11 17:16   ` Adrian Hunter
2026-06-14 11:50     ` Peter Fang
2026-06-13  0:04   ` Dan Williams (nvidia)
2026-06-14 11:51     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 11/15] KVM: TDX: Factor out userspace return path from tdx_get_quote() Xu Yilun
2026-07-02 10:08   ` Nikolay Borisov
2026-07-07  5:01     ` Peter Fang
2026-05-22  3:41 ` Xu Yilun [this message]
2026-06-13  0:20   ` [RFC PATCH 12/15] KVM: TDX: Add in-kernel Quote generation Dan Williams (nvidia)
2026-06-14 11:57     ` Peter Fang
2026-07-02 15:26   ` Nikolay Borisov
2026-07-07  5:04     ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 13/15] KVM: TDX: Support event-notify interrupts only with userspace quoting Xu Yilun
2026-06-11 19:36   ` Adrian Hunter
2026-06-14 12:57     ` Peter Fang
2026-06-15  4:39       ` Adrian Hunter
2026-06-15 18:14         ` Peter Fang
2026-05-22  3:41 ` [RFC PATCH 14/15] x86/virt/tdx: Embed version info in SEAMCALL leaf function definitions Xu Yilun
2026-05-25  9:00   ` Xiaoyao Li
2026-05-27  6:45     ` Xu Yilun
2026-05-27  7:44       ` Xiaoyao Li
2026-05-27 11:45         ` Xu Yilun
2026-06-12  5:47   ` Adrian Hunter
2026-06-13 15:55     ` Xu Yilun
2026-05-22  3:41 ` [RFC PATCH 15/15] x86/virt/tdx: Enable TDX Quoting extension Xu Yilun
2026-05-25  5:17   ` Tony Lindgren
2026-05-25 10:51     ` Xiaoyao Li
2026-05-26  9:00       ` Tony Lindgren
2026-05-26 15:45       ` Xu Yilun
2026-05-27  1:30         ` Xiaoyao Li
2026-06-07  4:41   ` Kishen Maloor
2026-06-08 15:10     ` Xu Yilun
2026-07-03  7:57   ` Nikolay Borisov
2026-07-07  5:07     ` Peter Fang
2026-05-27  5:23 ` [PATCH 00/15] Enable TDX Module Extensions and DICE-based TDX Quoting Sohil Mehta
2026-05-27 10:38   ` Xu Yilun
2026-05-27 17:09     ` Sohil Mehta
2026-05-28  4:52       ` Xu Yilun
2026-05-28 19:50         ` Sohil Mehta
2026-06-01  9:36           ` Xu Yilun
2026-06-01 20:17             ` Sohil Mehta
2026-06-02  5:36               ` Xu Yilun
2026-06-07  4:36 ` Kishen Maloor
2026-06-08  6:54   ` Xu Yilun
2026-06-08 18:31 ` Adrian Hunter
2026-06-12 22:03 ` Dan Williams (nvidia)
2026-06-15 15:22   ` Xu Yilun
2026-06-15 15:57     ` Dave Hansen
2026-06-16 15:19       ` Xu Yilun

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=20260522034128.3144354-13-yilun.xu@linux.intel.com \
    --to=yilun.xu@linux.intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=djbw@kernel.org \
    --cc=kas@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-coco@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.fang@intel.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=sohil.mehta@intel.com \
    --cc=x86@kernel.org \
    --cc=xiaoyao.li@intel.com \
    --cc=yilun.xu@intel.com \
    --cc=zhenzhong.duan@intel.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

Powered by JetHome