mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Binbin Wu <binbin.wu@linux.intel.com>
To: pbonzini@redhat.com, seanjc@google.com, kvm@vger.kernel.org
Cc: rick.p.edgecombe@intel.com, kai.huang@intel.com,
	adrian.hunter@intel.com, reinette.chatre@intel.com,
	xiaoyao.li@intel.com, tony.lindgren@intel.com,
	isaku.yamahata@intel.com, yan.y.zhao@intel.com,
	mikko.ylinen@linux.intel.com, linux-kernel@vger.kernel.org,
	kirill.shutemov@intel.com, jiewen.yao@intel.com,
	binbin.wu@linux.intel.com
Subject: [RFC PATCH 4/4] KVM: TDX: Check KVM exit on KVM_HC_MAP_GPA_RANGE when TD finalize
Date: Tue, 10 Jun 2025 10:14:22 +0800	[thread overview]
Message-ID: <20250610021422.1214715-5-binbin.wu@linux.intel.com> (raw)
In-Reply-To: <20250610021422.1214715-1-binbin.wu@linux.intel.com>

Check userspace has enabled KVM exit on KVM_HC_MAP_GPA_RANGE during
KVM_TDX_FINALIZE_VM.

TDVMCALL_MAP_GPA is one of the GHCI base TDVMCALLs, so it must be
implemented by VMM to support TDX guests. KVM converts TDVMCALL_MAP_GPA
to KVM_HC_MAP_GPA_RANGE, which requires userspace to enable
KVM_CAP_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE bit set. Check it when
userspace requests KVM_TDX_FINALIZE_VM, so that there is no need to check
it during TDX guests running.

Signed-off-by: Binbin Wu <binbin.wu@linux.intel.com>
---
 Documentation/virt/kvm/x86/intel-tdx.rst |  3 +++
 arch/x86/kvm/vmx/tdx.c                   | 20 +++++++++-----------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/Documentation/virt/kvm/x86/intel-tdx.rst b/Documentation/virt/kvm/x86/intel-tdx.rst
index 69c64453e9ca..41e38c38b034 100644
--- a/Documentation/virt/kvm/x86/intel-tdx.rst
+++ b/Documentation/virt/kvm/x86/intel-tdx.rst
@@ -246,6 +246,9 @@ control flow is as follows:
    * Prepare content of initial guest memory.
    * KVM_TDX_INIT_MEM_REGION: Add initial guest memory.
    * KVM_TDX_FINALIZE_VM: Finalize the measurement of the TDX guest.
+     Note: To support TDVMCALL_MAP_GPA, userspace must opt-in
+     KVM_CAP_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE before calling
+     KVM_TDX_FINALIZE_VM.
 
 #. Run VCPU
 
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index a3cd68f44a9c..7fc6e6b9c131 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -1209,17 +1209,6 @@ static int tdx_map_gpa(struct kvm_vcpu *vcpu)
 	u64 size = tdx->vp_enter_args.r13;
 	u64 ret;
 
-	/*
-	 * Converting TDVMCALL_MAP_GPA to KVM_HC_MAP_GPA_RANGE requires
-	 * userspace to enable KVM_CAP_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE
-	 * bit set.  If not, the error code is not defined in GHCI for TDX, use
-	 * TDVMCALL_STATUS_INVALID_OPERAND for this case.
-	 */
-	if (!user_exit_on_hypercall(vcpu->kvm, KVM_HC_MAP_GPA_RANGE)) {
-		ret = TDVMCALL_STATUS_INVALID_OPERAND;
-		goto error;
-	}
-
 	if (gpa + size <= gpa || !kvm_vcpu_is_legal_gpa(vcpu, gpa) ||
 	    !kvm_vcpu_is_legal_gpa(vcpu, gpa + size - 1) ||
 	    (vt_is_tdx_private_gpa(vcpu->kvm, gpa) !=
@@ -2821,6 +2810,15 @@ static int tdx_td_finalize(struct kvm *kvm, struct kvm_tdx_cmd *cmd)
 {
 	struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
 
+	/*
+	 * TDVMCALL_MAP_GPA is one of the GHCI base TDVMCALLs, so it must be
+	 * implemented by VMM to support TDX guests. KVM converts
+	 * TDVMCALL_MAP_GPA to KVM_HC_MAP_GPA_RANGE, which requires userspace to
+	 * enable KVM_CAP_EXIT_HYPERCALL with KVM_HC_MAP_GPA_RANGE bit set.
+	 */
+	if (!user_exit_on_hypercall(kvm, KVM_HC_MAP_GPA_RANGE))
+		return -EINVAL;
+
 	guard(mutex)(&kvm->slots_lock);
 
 	if (!is_hkid_assigned(kvm_tdx) || kvm_tdx->state == TD_STATE_RUNNABLE)
-- 
2.46.0


  parent reply	other threads:[~2025-06-10  2:13 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-10  2:14 [RFC PATCH 0/4] TDX attestation support and GHCI fixup Binbin Wu
2025-06-10  2:14 ` [RFC PATCH 1/4] KVM: TDX: Add new TDVMCALL status code for unsupported subfuncs Binbin Wu
2025-06-10  2:14 ` [RFC PATCH 2/4] KVM: TDX: Handle TDG.VP.VMCALL<GetQuote> Binbin Wu
2025-06-10  2:14 ` [RFC PATCH 3/4] KVM: TDX: Exit to userspace for GetTdVmCallInfo Binbin Wu
2025-06-10  9:16   ` Xiaoyao Li
2025-06-10 16:50     ` Edgecombe, Rick P
2025-06-10 16:54       ` Edgecombe, Rick P
2025-06-11  2:04         ` Binbin Wu
2025-06-11  2:37           ` Xiaoyao Li
2025-06-11 14:17             ` Edgecombe, Rick P
2025-06-11 14:34               ` Xiaoyao Li
2025-06-11 14:41                 ` Edgecombe, Rick P
2025-06-11  1:37     ` Binbin Wu
2025-06-11  2:17       ` Xiaoyao Li
2025-06-11 14:54         ` Sean Christopherson
2025-06-11 14:58           ` Edgecombe, Rick P
2025-06-11 16:26             ` Sean Christopherson
2025-06-11 16:53               ` Edgecombe, Rick P
2025-06-11 18:13                 ` Sean Christopherson
2025-06-11 18:52                   ` Edgecombe, Rick P
2025-06-12  8:27                   ` Huang, Kai
2025-06-12 15:26                     ` Edgecombe, Rick P
2025-06-20 18:27                   ` Edgecombe, Rick P
2025-06-10  2:14 ` Binbin Wu [this message]
2025-06-10 17:01   ` [RFC PATCH 4/4] KVM: TDX: Check KVM exit on KVM_HC_MAP_GPA_RANGE when TD finalize Edgecombe, Rick P
2025-06-10 19:58     ` Sean Christopherson
2025-06-11  1:22       ` Binbin Wu
2025-06-11 13:36         ` Sean Christopherson
2025-06-11 14:01           ` Xiaoyao Li
2025-06-11 14:04             ` Edgecombe, Rick P
2025-06-11 14:26               ` Xiaoyao Li
2025-06-11 16:00                 ` Binbin Wu
2025-06-11 15:33           ` Binbin Wu

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=20250610021422.1214715-5-binbin.wu@linux.intel.com \
    --to=binbin.wu@linux.intel.com \
    --cc=adrian.hunter@intel.com \
    --cc=isaku.yamahata@intel.com \
    --cc=jiewen.yao@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kirill.shutemov@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mikko.ylinen@linux.intel.com \
    --cc=pbonzini@redhat.com \
    --cc=reinette.chatre@intel.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=seanjc@google.com \
    --cc=tony.lindgren@intel.com \
    --cc=xiaoyao.li@intel.com \
    --cc=yan.y.zhao@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

all inboxes | Powered by JetHome®