Skip to contentSkip to conversation

Unify TDCALL/SEAMCALL and TDVMCALL assembly

PATCH Superseded Kai Huang 9 participants 58 messages Jul 12, 2023 – Aug 14, 2023

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kai Huang

author
#0 lore raw

cover Unify TDCALL/SEAMCALL and TDVMCALL assembly

Hi Peter, Kirill, all,

This series unifies the assembly code for TDCALL/SEAMCALL and TDVMCALL. Now all of them use one singe TDX_MODULE_CALL asm macro. I basically followed Peter's code here:

https://lore.kernel.org/linux-mm/[email protected]/

With some differences that I found during my code writing and testing.

With this series, I have verified the TDX guest can boot successfully and the TDX module can also be initialized successfully.

The last two patches are SEAMCALL patches that are needed for TDX host patchset. They are not mandatory to be here though, i.e., can be in the TDX host support series. I put them here so we can have a complete view how TDCALL/SEAMCALL and TDVMCALL are implemented.

Could you help to review? Thanks in advance.

Also cc Sean/Paolo/Isaku and KVM list for TDH.VP.ENTER part.

Kai Huang (10):
x86/tdx: Zero out the missing RSI in TDX_HYPERCALL macro x86/tdx: Use cmovc to save a label in TDX_MODULE_CALL asm x86/tdx: Move FRAME_BEGIN/END to TDX_MODULE_CALL asm macro x86/tdx: Make macros of TDCALLs consistent with the spec x86/tdx: Rename __tdx_module_call() to __tdcall()
x86/tdx: Pass TDCALL/SEAMCALL input/output registers via a structure x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly x86/virt/tdx: Wire up basic SEAMCALL functions x86/virt/tdx: Allow SEAMCALL to handle #UD and #GP

 arch/x86/Kconfig                  |  12 ++
 arch/x86/Makefile                 |   2 +
 arch/x86/boot/compressed/tdx.c    |  26 +++-
 arch/x86/coco/tdx/tdcall.S        | 238 +++++-------------------------
 arch/x86/coco/tdx/tdx.c           | 124 +++++++++-------
 arch/x86/include/asm/shared/tdx.h |  48 ++++--
 arch/x86/include/asm/tdx.h        |  31 ++--
 arch/x86/kernel/asm-offsets.c     |  33 ++---
 arch/x86/virt/Makefile            |   2 +
 arch/x86/virt/vmx/Makefile        |   2 +
 arch/x86/virt/vmx/tdx/Makefile    |   2 +
 arch/x86/virt/vmx/tdx/seamcall.S  |  54 +++++++
 arch/x86/virt/vmx/tdx/tdxcall.S   | 206 ++++++++++++++++++++------
 13 files changed, 431 insertions(+), 349 deletions(-)
 create mode 100644 arch/x86/virt/Makefile
 create mode 100644 arch/x86/virt/vmx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/seamcall.S

base-commit: 94142c9d1bdf1c18027a42758ceb6bdd59a92012

Signature · 1 line
2.41.0
author
#1 lore raw

PATCH 1/10 x86/tdx: Zero out the missing RSI in TDX_HYPERCALL macro

In the TDX_HYPERCALL asm, after the TDCALL instruction returns from the untrusted VMM, the registers that the TDX guest shares to the VMM need to be cleared to avoid speculative execution of VMM-provided values.

RSI is specified in the bitmap of those registers, but it is missing when zeroing out those registers in the current TDX_HYPERCALL.

It was there when it was originally added in commit 752d13305c78 ("x86/tdx: Expand __tdx_hypercall() to handle more arguments"), but was later removed in commit 1e70c680375a ("x86/tdx: Do not corrupt frame-pointer in __tdx_hypercall()"), which was correct because %rsi is later restored in the "pop %rsi". However a later commit 7a3a401874be ("x86/tdx: Drop flags from __tdx_hypercall()") removed that "pop %rsi" but forgot to add the "xor %rsi, %rsi" back.

Fix by adding it back.

Fixes 7a3a401874be ("x86/tdx: Drop flags from __tdx_hypercall()")
---
 arch/x86/coco/tdx/tdcall.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/coco/tdx/tdcall.S +1 -0
index b193c0a1d8db..2eca5f43734f 100644
@@ -195,6 +195,7 @@ SYM_FUNC_END(__tdx_module_call)
 	xor %r10d, %r10d
 	xor %r11d, %r11d
 	xor %rdi,  %rdi
+	xor %rsi,  %rsi
 	xor %rdx,  %rdx
 
 	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
Signature · 1 line
2.41.0
author
#3 lore raw

PATCH 2/10 x86/tdx: Use cmovc to save a label in TDX_MODULE_CALL asm

In reply to Kai Huang

Change 'jnc .Lno_vmfailinvalid' to 'cmovc %rdi, %rax' to save the .Lno_vmfailinvalid label in the TDX_MODULE_CALL asm macro.

Note %rdi, which is used as the first argument, has been saved to %rax as TDCALL leaf ID thus is free to hold the error code for cmovc.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/virt/vmx/tdx/tdxcall.S | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/tdxcall.S +2 -4
index 49a54356ae99..3524915d8bd9 100644
@@ -57,10 +57,8 @@
 	 * This value will never be used as actual SEAMCALL error code as
 	 * it is from the Reserved status code class.
 	 */
-	jnc .Lno_vmfailinvalid
-	mov $TDX_SEAMCALL_VMFAILINVALID, %rax
-.Lno_vmfailinvalid:
-
+	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
+	cmovc %rdi, %rax
 	.else
 	tdcall
 	.endif
Signature · 1 line
2.41.0
author
#6 lore raw

PATCH 3/10 x86/tdx: Move FRAME_BEGIN/END to TDX_MODULE_CALL asm macro

In reply to Kai Huang

Currently, the TDX_MODULE_CALL asm macro and the __tdx_module_call() take registers directly as input and a 'struct tdx_module_output' as optional output. This is different from the __tdx_hypercall(), which simply uses a structure to carry all input/output. There's no point to leave __tdx_module_call() complicated as it is.

As a preparation to simplify the __tdx_module_call() to make it look like __tdx_hypercall(), move FRAME_BEGIN/END and RET from the __tdx_module_call() to the TDX_MODULE_CALL assembly macro. This also allows more implementation flexibility of the assembly inside the TDX_MODULE_CALL macro, e.g., allowing putting an _ASM_EXTABLE() after the main body of the assembly.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/coco/tdx/tdcall.S      | 3 ---
 arch/x86/virt/vmx/tdx/tdxcall.S | 5 +++++
 2 files changed, 5 insertions(+), 3 deletions(-)
arch/x86/coco/tdx/tdcall.S +0 -3
index 2eca5f43734f..e5d4b7d8ecd4 100644
@@ -78,10 +78,7 @@
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdx_module_call)
-	FRAME_BEGIN
 	TDX_MODULE_CALL host=0
-	FRAME_END
-	RET
 SYM_FUNC_END(__tdx_module_call)
 
 /*
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
index 3524915d8bd9..b5ab919c7fa8 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <asm/asm-offsets.h>
+#include <asm/frame.h>
 #include <asm/tdx.h>
 
 /*
@@ -18,6 +19,7 @@
  *            TDX module.
  */
 .macro TDX_MODULE_CALL host:req
+	FRAME_BEGIN
 	/*
 	 * R12 will be used as temporary storage for struct tdx_module_output
 	 * pointer. Since R12-R15 registers are not used by TDCALL/SEAMCALL
@@ -91,4 +93,7 @@
 .Lno_output_struct:
 	/* Restore the state of R12 register */
 	pop %r12
+
+	FRAME_END
+	RET
 .endm
Signature · 1 line
2.41.0
author
#9 lore raw

PATCH 4/10 x86/tdx: Make macros of TDCALLs consistent with the spec

In reply to Kai Huang

The TDX spec names all TDCALLs with prefix "TDG". Currently, the kernel doesn't follow such convention for the macros of those TDCALLs but uses prefix "TDX_" for all of them. Although it's arguable whether the TDX spec names those TDCALLs properly, it's better for the kernel to follow the spec when naming those macros.

Change all macros of TDCALLs to make them consistent with the spec. As a bonus, they get distinguished easily from the host-side SEAMCALLs, which all have prefix "TDH".

No functional change intended.

---
 arch/x86/coco/tdx/tdx.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/coco/tdx/tdx.c +11 -11
index 5b8056f6c83f..de021df92009 100644
@@ -15,11 +15,11 @@
 #include <asm/pgtable.h>
 
 /* TDX module Call Leaf IDs */
-#define TDX_GET_INFO			1
-#define TDX_GET_VEINFO			3
-#define TDX_GET_REPORT			4
-#define TDX_ACCEPT_PAGE			6
-#define TDX_WR				8
+#define TDG_VP_INFO			1
+#define TDG_VP_VEINFO_GET		3
+#define TDG_MR_REPORT			4
+#define TDG_MEM_PAGE_ACCEPT		6
+#define TDG_VM_WR			8
 
 /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
@@ -123,7 +123,7 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
 {
 	u64 ret;
 
-	ret = __tdx_module_call(TDX_GET_REPORT, virt_to_phys(tdreport),
+	ret = __tdx_module_call(TDG_MR_REPORT, virt_to_phys(tdreport),
 				virt_to_phys(reportdata), TDREPORT_SUBTYPE_0,
 				0, NULL);
 	if (ret) {
@@ -184,7 +184,7 @@ static void tdx_parse_tdinfo(u64 *cc_mask)
 	 * Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL
 	 * [TDG.VP.INFO].
 	 */
-	tdx_module_call(TDX_GET_INFO, 0, 0, 0, 0, &out);
+	tdx_module_call(TDG_VP_INFO, 0, 0, 0, 0, &out);
 
 	/*
 	 * The highest bit of a guest physical address is the "sharing" bit.
@@ -626,7 +626,7 @@ void tdx_get_ve_info(struct ve_info *ve)
 	 * Note, the TDX module treats virtual NMIs as inhibited if the #VE
 	 * valid flag is set. It means that NMI=>#VE will not result in a #DF.
 	 */
-	tdx_module_call(TDX_GET_VEINFO, 0, 0, 0, 0, &out);
+	tdx_module_call(TDG_VP_VEINFO_GET, 0, 0, 0, 0, &out);
 
 	/* Transfer the output parameters */
 	ve->exit_reason = out.rcx;
@@ -768,7 +768,7 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len,
 	}
 
 	tdcall_rcx = *start | page_size;
-	if (__tdx_module_call(TDX_ACCEPT_PAGE, tdcall_rcx, 0, 0, 0, NULL))
+	if (__tdx_module_call(TDG_MEM_PAGE_ACCEPT, tdcall_rcx, 0, 0, 0, NULL))
 		return false;
 
 	*start += accept_size;
@@ -805,7 +805,7 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)
 
 	/*
 	 * For shared->private conversion, accept the page using
-	 * TDX_ACCEPT_PAGE TDX module call.
+	 * TDG_MEM_PAGE_ACCEPT TDX module call.
 	 */
 	while (start < end) {
 		unsigned long len = end - start;
@@ -870,7 +870,7 @@ void __init tdx_early_init(void)
 	cc_set_mask(cc_mask);
 
 	/* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
-	tdx_module_call(TDX_WR, 0, TDCS_NOTIFY_ENABLES, 0, -1ULL, NULL);
+	tdx_module_call(TDG_VM_WR, 0, TDCS_NOTIFY_ENABLES, 0, -1ULL, NULL);
 
 	/*
 	 * All bits above GPA width are reserved and kernel treats shared bit
Signature · 1 line
2.41.0
author
#13 lore raw

PATCH 5/10 x86/tdx: Rename __tdx_module_call() to __tdcall()

In reply to Kai Huang

__tdx_module_call() is only used by the TDX guest to issue TDCALL to the TDX module. Rename it to __tdcall() to match its behaviour, e.g., it cannot be used to make host-side SEAMCALL.

Also rename tdx_module_call() which is a wrapper of __tdx_module_call() to tdcall().

No functional change intended.

---
 arch/x86/coco/tdx/tdcall.S | 10 +++++-----
 arch/x86/coco/tdx/tdx.c    | 20 ++++++++++----------
 arch/x86/include/asm/tdx.h |  4 ++--
 3 files changed, 17 insertions(+), 17 deletions(-)
arch/x86/coco/tdx/tdcall.S +5 -5
index e5d4b7d8ecd4..6aebac08f2bf 100644
@@ -40,8 +40,8 @@
 .section .noinstr.text, "ax"
 
 /*
- * __tdx_module_call()  - Used by TDX guests to request services from
- * the TDX module (does not include VMM services) using TDCALL instruction.
+ * __tdcall()  - Used by TDX guests to request services from the TDX
+ * module (does not include VMM services) using TDCALL instruction.
  *
  * Transforms function call register arguments into the TDCALL register ABI.
  * After TDCALL operation, TDX module output is saved in @out (if it is
@@ -62,7 +62,7 @@
  *
  *-------------------------------------------------------------------------
  *
- * __tdx_module_call() function ABI:
+ * __tdcall() function ABI:
  *
  * @fn  (RDI)          - TDCALL Leaf ID,    moved to RAX
  * @rcx (RSI)          - Input parameter 1, moved to RCX
@@ -77,9 +77,9 @@
  *
  * Return status of TDCALL via RAX.
  */
-SYM_FUNC_START(__tdx_module_call)
+SYM_FUNC_START(__tdcall)
 	TDX_MODULE_CALL host=0
-SYM_FUNC_END(__tdx_module_call)
+SYM_FUNC_END(__tdcall)
 
 /*
  * TDX_HYPERCALL - Make hypercalls to a TDX VMM using TDVMCALL leaf of TDCALL
arch/x86/coco/tdx/tdx.c +10 -10
index de021df92009..268f812ff595 100644
@@ -98,10 +98,10 @@ EXPORT_SYMBOL_GPL(tdx_kvm_hypercall);
  * should only be used for calls that have no legitimate reason to fail
  * or where the kernel can not survive the call failing.
  */
-static inline void tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
-				   struct tdx_module_output *out)
+static inline void tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
+			  struct tdx_module_output *out)
 {
-	if (__tdx_module_call(fn, rcx, rdx, r8, r9, out))
+	if (__tdcall(fn, rcx, rdx, r8, r9, out))
 		panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
 }
 
@@ -123,9 +123,9 @@ int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
 {
 	u64 ret;
 
-	ret = __tdx_module_call(TDG_MR_REPORT, virt_to_phys(tdreport),
-				virt_to_phys(reportdata), TDREPORT_SUBTYPE_0,
-				0, NULL);
+	ret = __tdcall(TDG_MR_REPORT, virt_to_phys(tdreport),
+			virt_to_phys(reportdata), TDREPORT_SUBTYPE_0,
+			0, NULL);
 	if (ret) {
 		if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
 			return -EINVAL;
@@ -184,7 +184,7 @@ static void tdx_parse_tdinfo(u64 *cc_mask)
 	 * Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL
 	 * [TDG.VP.INFO].
 	 */
-	tdx_module_call(TDG_VP_INFO, 0, 0, 0, 0, &out);
+	tdcall(TDG_VP_INFO, 0, 0, 0, 0, &out);
 
 	/*
 	 * The highest bit of a guest physical address is the "sharing" bit.
@@ -626,7 +626,7 @@ void tdx_get_ve_info(struct ve_info *ve)
 	 * Note, the TDX module treats virtual NMIs as inhibited if the #VE
 	 * valid flag is set. It means that NMI=>#VE will not result in a #DF.
 	 */
-	tdx_module_call(TDG_VP_VEINFO_GET, 0, 0, 0, 0, &out);
+	tdcall(TDG_VP_VEINFO_GET, 0, 0, 0, 0, &out);
 
 	/* Transfer the output parameters */
 	ve->exit_reason = out.rcx;
@@ -768,7 +768,7 @@ static bool try_accept_one(phys_addr_t *start, unsigned long len,
 	}
 
 	tdcall_rcx = *start | page_size;
-	if (__tdx_module_call(TDG_MEM_PAGE_ACCEPT, tdcall_rcx, 0, 0, 0, NULL))
+	if (__tdcall(TDG_MEM_PAGE_ACCEPT, tdcall_rcx, 0, 0, 0, NULL))
 		return false;
 
 	*start += accept_size;
@@ -870,7 +870,7 @@ void __init tdx_early_init(void)
 	cc_set_mask(cc_mask);
 
 	/* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
-	tdx_module_call(TDG_VM_WR, 0, TDCS_NOTIFY_ENABLES, 0, -1ULL, NULL);
+	tdcall(TDG_VM_WR, 0, TDCS_NOTIFY_ENABLES, 0, -1ULL, NULL);
 
 	/*
 	 * All bits above GPA width are reserved and kernel treats shared bit
arch/x86/include/asm/tdx.h +2 -2
index 28d889c9aa16..f19e329c4044 100644
@@ -56,8 +56,8 @@ struct ve_info {
 void __init tdx_early_init(void);
 
 /* Used to communicate with the TDX module */
-u64 __tdx_module_call(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
-		      struct tdx_module_output *out);
+u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
+	     struct tdx_module_output *out);
 
 void tdx_get_ve_info(struct ve_info *ve);
 
Signature · 1 line
2.41.0
author
#14 lore raw

PATCH 6/10 x86/tdx: Pass TDCALL/SEAMCALL input/output registers via a structure

In reply to Kai Huang

Currently, the TDX_MODULE_CALL asm macro, which handles both TDCALL and SEAMCALL, takes one parameter for each input register and an optional 'struct tdx_module_output' (a collection of output registers) as output. This is different from the TDX_HYPERCALL macro which uses a single 'struct tdx_hypercall_args' to carry all input/output registers.

The newer TDX versions introduce more TDCALLs/SEAMCALLs which use more input/output registers. Also, the TDH.VP.ENTER (which isn't covered by the current TDX_MODULE_CALL macro) basically can use all registers that the TDX_HYPERCALL does. The current TDX_MODULE_CALL macro isn't extendible to cover those cases.

Similar to the TDX_HYPERCALL macro, simplify the TDX_MODULE_CALL macro to use a single structure 'struct tdx_module_args' to carry all the input/output registers.

Currently, the TDX_MODULE_CALL macro depends on the caller to pass a non-NULL 'struct tdx_module_output' to get additional output registers. Similar to the TDX_HYPERCALL macro, change the TDX_MODULE_CALL macro to take a new 'ret' parameter to indicate whether to save the additional output registers to the 'struct tdx_module_args'. Also introduce a new __tdcall_ret() for that purpose, similar to the __tdx_hypercall_ret().

Note the tdcall(), which is a wrapper of __tdcall(), is called by three
callers: tdx_parse_tdinfo(), tdx_get_ve_info() and tdx_early_init(). The former two need the additional output but the last one doesn't. For simplicity, make tdcall() always call __tdcall_ret() to avoid another "_ret()" wrapper. The last caller tdx_early_init() isn't performance critical anyway.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/coco/tdx/tdcall.S      | 48 ++++++++-----------
 arch/x86/coco/tdx/tdx.c         | 50 +++++++++++---------
 arch/x86/include/asm/tdx.h      | 10 ++--
 arch/x86/kernel/asm-offsets.c   | 12 ++---
 arch/x86/virt/vmx/tdx/tdxcall.S | 82 +++++++++++++--------------------
 5 files changed, 89 insertions(+), 113 deletions(-)
arch/x86/coco/tdx/tdcall.S +18 -30
index 6aebac08f2bf..46847e85c372 100644
@@ -43,44 +43,32 @@
  * __tdcall()  - Used by TDX guests to request services from the TDX
  * module (does not include VMM services) using TDCALL instruction.
  *
- * Transforms function call register arguments into the TDCALL register ABI.
- * After TDCALL operation, TDX module output is saved in @out (if it is
- * provided by the user).
- *
- *-------------------------------------------------------------------------
- * TDCALL ABI:
- *-------------------------------------------------------------------------
- * Input Registers:
- *
- * RAX                 - TDCALL Leaf number.
- * RCX,RDX,R8-R9       - TDCALL Leaf specific input registers.
- *
- * Output Registers:
- *
- * RAX                 - TDCALL instruction error code.
- * RCX,RDX,R8-R11      - TDCALL Leaf specific output registers.
- *
- *-------------------------------------------------------------------------
- *
  * __tdcall() function ABI:
  *
- * @fn  (RDI)          - TDCALL Leaf ID,    moved to RAX
- * @rcx (RSI)          - Input parameter 1, moved to RCX
- * @rdx (RDX)          - Input parameter 2, moved to RDX
- * @r8  (RCX)          - Input parameter 3, moved to R8
- * @r9  (R8)           - Input parameter 4, moved to R9
- *
- * @out (R9)           - struct tdx_module_output pointer
- *                       stored temporarily in R12 (not
- *                       shared with the TDX module). It
- *                       can be NULL.
+ * @fn   (RDI)          - TDCALL Leaf ID, moved to RAX
+ * @args (RSI)          - struct tdx_module_args for input
  *
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdcall)
-	TDX_MODULE_CALL host=0
+	TDX_MODULE_CALL host=0 ret=0
 SYM_FUNC_END(__tdcall)
 
+/*
+ * __tdcall_ret() - Used by TDX guests to request services from the TDX
+ * module (does not include VMM services) using TDCALL instruction.
+ *
+ * __tdcall_ret() function ABI:
+ *
+ * @fn   (RDI)          - TDCALL Leaf ID, moved to RAX
+ * @args (RSI)          - struct tdx_module_args for input and output
+ *
+ * Return status of TDCALL via RAX.
+ */
+SYM_FUNC_START(__tdcall_ret)
+	TDX_MODULE_CALL host=0 ret=1
+SYM_FUNC_END(__tdcall_ret)
+
 /*
  * TDX_HYPERCALL - Make hypercalls to a TDX VMM using TDVMCALL leaf of TDCALL
  * instruction
arch/x86/coco/tdx/tdx.c +28 -22

127 lines not on this page · raw message

arch/x86/include/asm/tdx.h +6 -4
index f19e329c4044..e353c7ec5f24 100644
@@ -21,16 +21,18 @@
 #ifndef __ASSEMBLY__
 
 /*
- * Used to gather the output registers values of the TDCALL and SEAMCALL
+ * Used to gather input/output registers of the TDCALL and SEAMCALL
  * instructions when requesting services from the TDX module.
  *
  * This is a software only structure and not part of the TDX module/VMM ABI.
  */
-struct tdx_module_output {
+struct tdx_module_args {
+	/* input/output */
 	u64 rcx;
 	u64 rdx;
 	u64 r8;
 	u64 r9;
+	/* additional output */
 	u64 r10;
 	u64 r11;
 };
@@ -56,8 +58,8 @@ struct ve_info {
 void __init tdx_early_init(void);
 
 /* Used to communicate with the TDX module */
-u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
-	     struct tdx_module_output *out);
+u64 __tdcall(u64 fn, struct tdx_module_args *args);
+u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
 
 void tdx_get_ve_info(struct ve_info *ve);
 
arch/x86/kernel/asm-offsets.c +6 -6
index dc3576303f1a..50383bc46dd7 100644
@@ -68,12 +68,12 @@ static void __used common(void)
 #endif
 
 	BLANK();
-	OFFSET(TDX_MODULE_rcx, tdx_module_output, rcx);
-	OFFSET(TDX_MODULE_rdx, tdx_module_output, rdx);
-	OFFSET(TDX_MODULE_r8,  tdx_module_output, r8);
-	OFFSET(TDX_MODULE_r9,  tdx_module_output, r9);
-	OFFSET(TDX_MODULE_r10, tdx_module_output, r10);
-	OFFSET(TDX_MODULE_r11, tdx_module_output, r11);
+	OFFSET(TDX_MODULE_rcx, tdx_module_args, rcx);
+	OFFSET(TDX_MODULE_rdx, tdx_module_args, rdx);
+	OFFSET(TDX_MODULE_r8,  tdx_module_args, r8);
+	OFFSET(TDX_MODULE_r9,  tdx_module_args, r9);
+	OFFSET(TDX_MODULE_r10, tdx_module_args, r10);
+	OFFSET(TDX_MODULE_r11, tdx_module_args, r11);
 
 	BLANK();
 	OFFSET(TDX_HYPERCALL_r8,  tdx_hypercall_args, r8);
arch/x86/virt/vmx/tdx/tdxcall.S +31 -51

99 lines not on this page · raw message

Signature · 1 line
2.41.0
author
#15 lore raw

PATCH 7/10 x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Kai Huang

The TDX guest live migration support (TDX 1.5) adds new TDCALL/SEAMCALL leaf functions. Those new TDCALLs/SEAMCALLs take additional registers for input (R10-R13) and output (R12-R13). TDG.SERVTD.RD is an example.

Also, the current TDX_MODULE_CALL doesn't aim to handle TDH.VP.ENTER SEAMCALL, which monitors the TDG.VP.VMCALL in input/output registers when it returns in case of VMCALL from TDX guest.

With those new TDCALLs/SEAMCALLs and the TDH.VP.ENTER covered, the TDX_MODULE_CALL macro basically needs to handle the same input/output registers as the TDX_HYPERCALL does. And as a result, they also share similar logic in the assembly, thus should be unified to use one common assembly.

Extend the TDX_MODULE_CALL asm to support the new TDCALLs/SEAMCALLs and also the TDH.VP.ENTER SEAMCALL. Eventually it will be unified with the TDX_HYPERCALL.

The new input/output registers fit with the "callee-saved" registers in the x86 calling convention. Add a new "saved" parameter to support those new TDCALLs/SEAMCALLs and TDH.VP.ENTER and keep the existing TDCALLs/SEAMCALLs minimally impacted.

For TDH.VP.ENTER, after it returns the registers shared by the guest contain guest's values. Explicitly clear them to prevent speculative use of guest's values.

Note most TDX live migration related SEAMCALLs may also clobber AVX* state ("AVX, AVX2 and AVX512 state: may be reset to the architectural INIT state" -- see TDH.EXPORT.MEM for example). And TDH.VP.ENTER also clobbers XMM0-XMM15 when the corresponding bit is set in RCX. Don't handle them in the TDX_MODULE_CALL macro but let the caller save and restore when needed.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/coco/tdx/tdcall.S      |   4 +-
 arch/x86/include/asm/tdx.h      |  12 +++-
 arch/x86/kernel/asm-offsets.c   |   7 ++
 arch/x86/virt/vmx/tdx/tdxcall.S | 121 ++++++++++++++++++++++++++++++--
 4 files changed, 134 insertions(+), 10 deletions(-)
arch/x86/coco/tdx/tdcall.S +2 -2
index 46847e85c372..d5693330ee9b 100644
@@ -51,7 +51,7 @@
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdcall)
-	TDX_MODULE_CALL host=0 ret=0
+	TDX_MODULE_CALL host=0 ret=0 saved=0
 SYM_FUNC_END(__tdcall)
 
 /*
@@ -66,7 +66,7 @@ SYM_FUNC_END(__tdcall)
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdcall_ret)
-	TDX_MODULE_CALL host=0 ret=1
+	TDX_MODULE_CALL host=0 ret=1 saved=0
 SYM_FUNC_END(__tdcall_ret)
 
 /*
arch/x86/include/asm/tdx.h +10 -2
index e353c7ec5f24..a549bcc77f4f 100644
@@ -27,14 +27,22 @@
  * This is a software only structure and not part of the TDX module/VMM ABI.
  */
 struct tdx_module_args {
-	/* input/output */
+	/* callee-clobbered */
 	u64 rcx;
 	u64 rdx;
 	u64 r8;
 	u64 r9;
-	/* additional output */
+	/* extra callee-clobbered */
 	u64 r10;
 	u64 r11;
+	/* callee-saved + rdi/rsi */
+	u64 r12;
+	u64 r13;
+	u64 r14;
+	u64 r15;
+	u64 rbx;
+	u64 rdi;
+	u64 rsi;
 };
 
 /*
arch/x86/kernel/asm-offsets.c +7 -0
index 50383bc46dd7..1581564a67b7 100644
@@ -74,6 +74,13 @@ static void __used common(void)
 	OFFSET(TDX_MODULE_r9,  tdx_module_args, r9);
 	OFFSET(TDX_MODULE_r10, tdx_module_args, r10);
 	OFFSET(TDX_MODULE_r11, tdx_module_args, r11);
+	OFFSET(TDX_MODULE_r12, tdx_module_args, r12);
+	OFFSET(TDX_MODULE_r13, tdx_module_args, r13);
+	OFFSET(TDX_MODULE_r14, tdx_module_args, r14);
+	OFFSET(TDX_MODULE_r15, tdx_module_args, r15);
+	OFFSET(TDX_MODULE_rbx, tdx_module_args, rbx);
+	OFFSET(TDX_MODULE_rdi, tdx_module_args, rdi);
+	OFFSET(TDX_MODULE_rsi, tdx_module_args, rsi);
 
 	BLANK();
 	OFFSET(TDX_HYPERCALL_r8,  tdx_hypercall_args, r8);
arch/x86/virt/vmx/tdx/tdxcall.S +115 -6

151 lines not on this page · raw message

Signature · 1 line
2.41.0
author
#37 lore raw

PATCH 8/10 x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

In reply to Kai Huang

Now TDX_HYPERCALL macro shares very similar code pattern with the TDX_MODULE_CALL macro, except that TDX_HYPERCALL handles below additional things:

1) Sets RAX to 0 (TDG.VP.VMCALL leaf) internally;
2) Sets RCX to the (fixed) bitmap of shared registers;
3) Panics internally if the TDCALL return code is not zero;
4) After TDCALL, moves R10 to RAX for returning the return code of the TDVMCALL itself, regardless the "\ret" parameter;
5) Clears shared registers to prevent speculative use of VMM's values.

Using the TDX_MODULE_CALL macro, 1) - 3) can be moved out to the C code.
4) can be done by always turning on the TDX_MODULE_CALL macro's "\ret" parameter, and moving it to the C function. 5) is already covered by the TDH.VP.ENTER in the TDX_MODULE_CALL.

In other words, the TDX_HYPERCALL macro is redundant now and should be removed to use TDX_MODULE_CALL macro instead.

TDVMCALL always uses the extra "callee-saved" registers. Replace the existing __tdx_hypercall{_ret}() assembly functions with a new __tdcall_saved_ret(), as mentioned above TDVMCALL relies on saving R10 to the register structure so that the caller (C function) can use it as the return code of the TDVMCALL itself.

As a result, reimplement the existing __tdx_hypercall() using the __tdcall_saved_ret() function. __tdx_hypercall_ret() isn't necessary anymore because __tdx_hypercall() always turns on the "\ret" parameter.

Also remove the 'struct tdx_hypercall_args', which has the exact members as the 'struct tdx_module_args' (except the RCX which isn't used as shared register). It's not worth to keep the structure just for one member.

With this change, the __tdx_hypercall() variants are not implemented in tdcall.S anymore thus not available to the compressed code. Implement the __tdx_hypercall() variants in the compressed code directly using the __tdcall_saved_ret().

Opportunistically fix a checkpatch error complaining using space around parenthesis '(' and ')' while moving the bitmap of shared registers to <asm/shared/tdx.h>.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/boot/compressed/tdx.c    |  26 +++-
 arch/x86/coco/tdx/tdcall.S        | 194 ++++--------------------------
 arch/x86/coco/tdx/tdx.c           |  62 ++++++----
 arch/x86/include/asm/shared/tdx.h |  48 ++++++--
 arch/x86/include/asm/tdx.h        |  25 ----
 arch/x86/kernel/asm-offsets.c     |  14 ---
 arch/x86/virt/vmx/tdx/tdxcall.S   |  10 +-
 7 files changed, 127 insertions(+), 252 deletions(-)
arch/x86/boot/compressed/tdx.c +22 -4
index 8841b945a1e2..4df2259f9f7f 100644
@@ -11,14 +11,32 @@
 #include <asm/shared/tdx.h>
 
 /* Called from __tdx_hypercall() for unrecoverable failure */
-void __tdx_hypercall_failed(void)
+static inline void __tdx_hypercall_failed(void)
 {
 	error("TDVMCALL failed. TDX module bug?");
 }
 
+static inline u64 __tdx_hypercall(struct tdx_module_args *args)
+{
+	u64 ret;
+
+	args->rcx = TDVMCALL_EXPOSE_REGS_MASK;
+	ret = __tdcall_saved_ret(TDG_VP_VMCALL, args);
+
+	/*
+	 * RAX!=0 indicates a failure of the TDVMCALL mechanism itself and that
+	 * something has gone horribly wrong with the TDX module.
+	 */
+	if (ret)
+		__tdx_hypercall_failed();
+
+	/* The return status of the hypercall itself is in R10. */
+	return args->r10;
+}
+
 static inline unsigned int tdx_io_in(int size, u16 port)
 {
-	struct tdx_hypercall_args args = {
+	struct tdx_module_args args = {
 		.r10 = TDX_HYPERCALL_STANDARD,
 		.r11 = hcall_func(EXIT_REASON_IO_INSTRUCTION),
 		.r12 = size,
@@ -26,7 +44,7 @@ static inline unsigned int tdx_io_in(int size, u16 port)
 		.r14 = port,
 	};
 
-	if (__tdx_hypercall_ret(&args))
+	if (__tdx_hypercall(&args))
 		return UINT_MAX;
 
 	return args.r11;
@@ -34,7 +52,7 @@ static inline unsigned int tdx_io_in(int size, u16 port)
 
 static inline void tdx_io_out(int size, u16 port, u32 value)
 {
-	struct tdx_hypercall_args args = {
+	struct tdx_module_args args = {
 		.r10 = TDX_HYPERCALL_STANDARD,
 		.r11 = hcall_func(EXIT_REASON_IO_INSTRUCTION),
 		.r12 = size,
arch/x86/coco/tdx/tdcall.S +22 -172

223 lines not on this page · raw message

arch/x86/coco/tdx/tdx.c +40 -22

148 lines not on this page · raw message

arch/x86/include/asm/shared/tdx.h +38 -10

70 lines not on this page · raw message

arch/x86/include/asm/tdx.h +0 -25
index a549bcc77f4f..9b0ad0176e58 100644
@@ -20,31 +20,6 @@
 
 #ifndef __ASSEMBLY__
 
-/*
- * Used to gather input/output registers of the TDCALL and SEAMCALL
- * instructions when requesting services from the TDX module.
- *
- * This is a software only structure and not part of the TDX module/VMM ABI.
- */
-struct tdx_module_args {
-	/* callee-clobbered */
-	u64 rcx;
-	u64 rdx;
-	u64 r8;
-	u64 r9;
-	/* extra callee-clobbered */
-	u64 r10;
-	u64 r11;
-	/* callee-saved + rdi/rsi */
-	u64 r12;
-	u64 r13;
-	u64 r14;
-	u64 r15;
-	u64 rbx;
-	u64 rdi;
-	u64 rsi;
-};
-
 /*
  * Used by the #VE exception handler to gather the #VE exception
  * info from the TDX module. This is a software only structure
arch/x86/kernel/asm-offsets.c +0 -14
index 1581564a67b7..6913b372ccf7 100644
@@ -82,20 +82,6 @@ static void __used common(void)
 	OFFSET(TDX_MODULE_rdi, tdx_module_args, rdi);
 	OFFSET(TDX_MODULE_rsi, tdx_module_args, rsi);
 
-	BLANK();
-	OFFSET(TDX_HYPERCALL_r8,  tdx_hypercall_args, r8);
-	OFFSET(TDX_HYPERCALL_r9,  tdx_hypercall_args, r9);
-	OFFSET(TDX_HYPERCALL_r10, tdx_hypercall_args, r10);
-	OFFSET(TDX_HYPERCALL_r11, tdx_hypercall_args, r11);
-	OFFSET(TDX_HYPERCALL_r12, tdx_hypercall_args, r12);
-	OFFSET(TDX_HYPERCALL_r13, tdx_hypercall_args, r13);
-	OFFSET(TDX_HYPERCALL_r14, tdx_hypercall_args, r14);
-	OFFSET(TDX_HYPERCALL_r15, tdx_hypercall_args, r15);
-	OFFSET(TDX_HYPERCALL_rdi, tdx_hypercall_args, rdi);
-	OFFSET(TDX_HYPERCALL_rsi, tdx_hypercall_args, rsi);
-	OFFSET(TDX_HYPERCALL_rbx, tdx_hypercall_args, rbx);
-	OFFSET(TDX_HYPERCALL_rdx, tdx_hypercall_args, rdx);
-
 	BLANK();
 	OFFSET(BP_scratch, boot_params, scratch);
 	OFFSET(BP_secure_boot, boot_params, secure_boot);
arch/x86/virt/vmx/tdx/tdxcall.S +5 -5
index 389f4ec5eee8..e4e90ebf5dad 100644
@@ -145,11 +145,11 @@
 	.endif	/* \ret */
 
 	.if \saved
-	.if \ret && \host
+	.if \ret
 	/*
-	 * Clear registers shared by guest for VP.ENTER to prevent
-	 * speculative use of guest's values, including those are
-	 * restored from the stack.
+	 * Clear registers shared by guest for VP.ENTER and VP.VMCALL to
+	 * prevent speculative use of values from guest/VMM, including
+	 * those are restored from the stack.
 	 *
 	 * See arch/x86/kvm/vmx/vmenter.S:
 	 *
@@ -173,7 +173,7 @@
 	xorq %r15, %r15
 	xorq %rbx, %rbx
 	xorq %rdi, %rdi
-	.endif	/* \ret && \host */
+	.endif	/* \ret */
 
 	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
 	popq	%r15
Signature · 1 line
2.41.0
author
#43 lore raw

PATCH 9/10 x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Kai Huang

Intel Trust Domain Extensions (TDX) protects guest VMs from malicious host and certain physical attacks. A CPU-attested software module called 'the TDX module' runs inside a new isolated memory range as a trusted hypervisor to manage and run protected VMs.

TDX introduces a new CPU mode: Secure Arbitration Mode (SEAM). This mode runs only the TDX module itself or other code to load the TDX module.

The host kernel communicates with SEAM software via a new SEAMCALL instruction. This is conceptually similar to a guest->host hypercall, except it is made from the host to SEAM software instead. The TDX module establishes a new SEAMCALL ABI which allows the host to initialize the module and to manage VMs.

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure. Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

To start to support TDX, create a new arch/x86/virt/vmx/tdx/tdx.c for TDX host kernel support. Add a new Kconfig option CONFIG_INTEL_TDX_HOST to opt-in TDX host kernel support (to distinguish with TDX guest kernel support). So far only KVM uses TDX. Make the new config option depend on KVM_INTEL.

---
 arch/x86/Kconfig                 | 12 +++++++
 arch/x86/Makefile                |  2 ++
 arch/x86/include/asm/tdx.h       |  7 +++++
 arch/x86/virt/Makefile           |  2 ++
 arch/x86/virt/vmx/Makefile       |  2 ++
 arch/x86/virt/vmx/tdx/Makefile   |  2 ++
 arch/x86/virt/vmx/tdx/seamcall.S | 54 ++++++++++++++++++++++++++++++++
 7 files changed, 81 insertions(+)
 create mode 100644 arch/x86/virt/Makefile
 create mode 100644 arch/x86/virt/vmx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/seamcall.S
arch/x86/Kconfig +12 -0
index 53bab123a8ee..191587f75810 100644
@@ -1952,6 +1952,18 @@ config X86_SGX
 
 	  If unsure, say N.
 
+config INTEL_TDX_HOST
+	bool "Intel Trust Domain Extensions (TDX) host support"
+	depends on CPU_SUP_INTEL
+	depends on X86_64
+	depends on KVM_INTEL
+	help
+	  Intel Trust Domain Extensions (TDX) protects guest VMs from malicious
+	  host and certain physical attacks.  This option enables necessary TDX
+	  support in the host kernel to run confidential VMs.
+
+	  If unsure, say N.
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/Makefile +2 -0
index b39975977c03..ec0e71d8fa30 100644
@@ -252,6 +252,8 @@ archheaders:
 
 libs-y  += arch/x86/lib/
 
+core-y += arch/x86/virt/
+
 # drivers-y are linked after core-y
 drivers-$(CONFIG_MATH_EMULATION) += arch/x86/math-emu/
 drivers-$(CONFIG_PCI)            += arch/x86/pci/
arch/x86/include/asm/tdx.h +7 -0
index 9b0ad0176e58..a82e5249d079 100644
@@ -74,5 +74,12 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,
 	return -ENODEV;
 }
 #endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */
+
+#ifdef CONFIG_INTEL_TDX_HOST
+u64 __seamcall(u64 fn, struct tdx_module_args *args);
+u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
+u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
+#endif	/* CONFIG_INTEL_TDX_HOST */
+
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_TDX_H */
arch/x86/virt/Makefile new file +2 -0
new file mode 100644
index 000000000000..1e36502cd738
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-y	+= vmx/
arch/x86/virt/vmx/Makefile new file +2 -0
new file mode 100644
index 000000000000..feebda21d793
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_INTEL_TDX_HOST)	+= tdx/
arch/x86/virt/vmx/tdx/Makefile new file +2 -0
new file mode 100644
index 000000000000..46ef8f73aebb
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-y += seamcall.o
arch/x86/virt/vmx/tdx/seamcall.S new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.41.0
author
#55 lore raw

PATCH 10/10 x86/virt/tdx: Allow SEAMCALL to handle #UD and #GP

In reply to Kai Huang

On the platform with the "partial write machine check" erratum, a kernel partial write to TDX private memory may cause unexpected machine check. It would be nice if the #MC handler could print additional information to show the #MC was TDX private memory error due to possible kernel bug.

To do that, the machine check handler needs to use SEAMCALL to query page type of the error memory from the TDX module, because there's no existing infrastructure to track TDX private pages.

SEAMCALL instruction causes #UD if CPU isn't in VMX operation. In #MC handler, it is legal that CPU isn't in VMX operation when making this SEAMCALL. Extend the TDX_MODULE_CALL macro to handle #UD so the SEAMCALL can return error code instead of Oops in the #MC handler. Opportunistically handles #GP too since they share the same code.

A bonus is when kernel mistakenly calls SEAMCALL when CPU isn't in VMX operation, or when TDX isn't enabled by the BIOS, or when the BIOS is buggy, the kernel can get a nicer error message rather than a less understandable Oops.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/include/asm/tdx.h      |  5 +++++
 arch/x86/virt/vmx/tdx/tdxcall.S | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+)
arch/x86/include/asm/tdx.h +5 -0
index a82e5249d079..feb85316346e 100644
@@ -8,6 +8,8 @@
 #include <asm/ptrace.h>
 #include <asm/shared/tdx.h>
 
+#include <asm/trapnr.h>
+
 /*
  * SW-defined error codes.
  *
@@ -18,6 +20,9 @@
 #define TDX_SW_ERROR			(TDX_ERROR | GENMASK_ULL(47, 40))
 #define TDX_SEAMCALL_VMFAILINVALID	(TDX_SW_ERROR | _UL(0xFFFF0000))
 
+#define TDX_SEAMCALL_GP			(TDX_SW_ERROR | X86_TRAP_GP)
+#define TDX_SEAMCALL_UD			(TDX_SW_ERROR | X86_TRAP_UD)
+
 #ifndef __ASSEMBLY__
 
 /*
arch/x86/virt/vmx/tdx/tdxcall.S +20 -0
index e4e90ebf5dad..04b0c466f38c 100644
@@ -2,6 +2,7 @@
 #include <asm/asm-offsets.h>
 #include <asm/frame.h>
 #include <asm/tdx.h>
+#include <asm/asm.h>
 
 /*
  * TDCALL and SEAMCALL are supported in Binutils >= 2.36.
@@ -85,6 +86,7 @@
 	.endif	/* \saved */
 
 	.if \host
+1:
 	seamcall
 	/*
 	 * SEAMCALL instruction is essentially a VMExit from VMX root
@@ -99,6 +101,7 @@
 	 */
 	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
 	cmovc %rdi, %rax
+2:
 	.else
 	tdcall
 	.endif
@@ -185,4 +188,21 @@
 
 	FRAME_END
 	RET
+
+	.if \host
+3:
+	/*
+	 * SEAMCALL caused #GP or #UD.  By reaching here %eax contains
+	 * the trap number.  Convert the trap number to the TDX error
+	 * code by setting TDX_SW_ERROR to the high 32-bits of %rax.
+	 *
+	 * Note cannot OR TDX_SW_ERROR directly to %rax as OR instruction
+	 * only accepts 32-bit immediate at most.
+	 */
+	movq $TDX_SW_ERROR, %r12
+	orq  %r12, %rax
+	jmp  2b
+
+	_ASM_EXTABLE_FAULT(1b, 3b)
+	.endif	/* \host */
 .endm
Signature · 1 line
2.41.0
#16 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Kai Huang
Quoted 26 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +21 -1
@@ -72,7 +142,46 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif
+	.endif	/* \ret */
+
+	.if \saved
+	.if \ret && \host
+	/*
+	 * Clear registers shared by guest for VP.ENTER to prevent
+	 * speculative use of guest's values, including those are
+	 * restored from the stack.
+	 *
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.
+	 *
+	 * Note: RBP/RSP are not used as shared register.  RSI has been
+	 * restored already.
+	 *
+	 * XOR is cheap, thus unconditionally do for all leafs.
+	 */
+	xorq %rcx, %rcx
+	xorq %rdx, %rdx

arch/x86/virt/vmx/tdx/tdxcall.S line 175

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdxcall.S +9 -0
+	xorq %r8,  %r8
+	xorq %r9,  %r9
+	xorq %r10, %r10
+	xorq %r11, %r11
 
+	xorq %r12, %r12
+	xorq %r13, %r13
+	xorq %r14, %r14
+	xorq %r15, %r15
+	xorq %rbx, %rbx

^ those are an instant pop below, seems daft to clear them.

Quoted 16 lines
arch/x86/virt/vmx/tdx/tdxcall.S +10 -0
+	xorq %rdi, %rdi
+	.endif	/* \ret && \host */
+
+	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
+	popq	%r15
+	popq	%r14
+	popq	%r13
+	popq	%r12
+	popq	%rbx
+	.endif	/* \saved */
 
 	FRAME_END

RET

Signature · 1 line
2.41.0
#17 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

Quoted 34 lines · Peter Zijlstra
Quoted 30 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +25 -1
@@ -72,7 +142,46 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif
+	.endif	/* \ret */
+
+	.if \saved
+	.if \ret && \host
+	/*
+	 * Clear registers shared by guest for VP.ENTER to prevent
+	 * speculative use of guest's values, including those are
+	 * restored from the stack.
+	 *
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.
+	 *
+	 * Note: RBP/RSP are not used as shared register.  RSI has been
+	 * restored already.
+	 *
+	 * XOR is cheap, thus unconditionally do for all leafs.
+	 */
+	xorq %rcx, %rcx
+	xorq %rdx, %rdx
+	xorq %r8,  %r8
+	xorq %r9,  %r9
+	xorq %r10, %r10
+	xorq %r11, %r11
Quoted 7 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 174

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	xorq %r12, %r12
+	xorq %r13, %r13
+	xorq %r14, %r14
+	xorq %r15, %r15
+	xorq %rbx, %rbx

^ those are an instant pop below, seems daft to clear them.

Also, please use the 32bit variant:

	xorl	%ecx, %ecx

saves a RAX prefix each.

Quoted 16 lines
Quoted 16 lines
arch/x86/virt/vmx/tdx/tdxcall.S +10 -0
+	xorq %rdi, %rdi
+	.endif	/* \ret && \host */
+
+	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
+	popq	%r15
+	popq	%r14
+	popq	%r13
+	popq	%r12
+	popq	%rbx
+	.endif	/* \saved */
 
 	FRAME_END
 	RET
Signature · 1 line
2.41.0
#31 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Kai Huang
Quoted 15 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +11 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdxcall.S +10 -0
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

	/* xor-swap (%rsp) and %rax */
	xorq	(%rsp), %rax
	xorq	%rax, (%rsp)
	xorq	(%rsp), %rax

	movq	%rsi, TDX_MODULE_rsi(%rax);
	movq	%rax, %rsi

	popq	%rax

Isn't much better is it :/ Too bad xchg implies lock prefix.

Quoted 13 lines
arch/x86/virt/vmx/tdx/tdxcall.S +10 -0
+
+	/* Copy additional output regs to the structure  */
+	movq %r12, TDX_MODULE_r12(%rsi)
+	movq %r13, TDX_MODULE_r13(%rsi)
+	movq %r14, TDX_MODULE_r14(%rsi)
+	movq %r15, TDX_MODULE_r15(%rsi)
+	movq %rbx, TDX_MODULE_rbx(%rsi)
+	movq %rdi, TDX_MODULE_rdi(%rsi)
+	.endif	/* \saved */
+
 	/* Copy output regs to the structure */
 	movq %rcx, TDX_MODULE_rcx(%rsi)
 	movq %rdx, TDX_MODULE_rdx(%rsi)
Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH 01/10] x86/tdx: Zero out the missing RSI in TDX_HYPERCALL macro

In reply to Kai Huang
Quoted 15 lines · Kai Huang

In the TDX_HYPERCALL asm, after the TDCALL instruction returns from the untrusted VMM, the registers that the TDX guest shares to the VMM need to be cleared to avoid speculative execution of VMM-provided values.

RSI is specified in the bitmap of those registers, but it is missing when zeroing out those registers in the current TDX_HYPERCALL.

It was there when it was originally added in commit 752d13305c78 ("x86/tdx: Expand __tdx_hypercall() to handle more arguments"), but was later removed in commit 1e70c680375a ("x86/tdx: Do not corrupt frame-pointer in __tdx_hypercall()"), which was correct because %rsi is later restored in the "pop %rsi". However a later commit 7a3a401874be ("x86/tdx: Drop flags from __tdx_hypercall()") removed that "pop %rsi" but forgot to add the "xor %rsi, %rsi" back.

Quoted 5 lines

Fix by adding it back.

Fixes 7a3a401874be ("x86/tdx: Drop flags from __tdx_hypercall()")

---

Looks fine to me.

Quoted 15 lines
 arch/x86/coco/tdx/tdcall.S | 1 +
 1 file changed, 1 insertion(+)
arch/x86/coco/tdx/tdcall.S +1 -0
index b193c0a1d8db..2eca5f43734f 100644
@@ -195,6 +195,7 @@ SYM_FUNC_END(__tdx_module_call)
 	xor %r10d, %r10d
 	xor %r11d, %r11d
 	xor %rdi,  %rdi
+	xor %rsi,  %rsi
 	xor %rdx,  %rdx
 
 	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#4 lore raw

Re: [PATCH 02/10] x86/tdx: Use cmovc to save a label in TDX_MODULE_CALL asm

In reply to Kai Huang
Quoted 2 lines · Kai Huang

Change 'jnc .Lno_vmfailinvalid' to 'cmovc %rdi, %rax' to save the .Lno_vmfailinvalid label in the TDX_MODULE_CALL asm macro.

You are removing the label, right? What use "save"?

Quoted 33 lines

Note %rdi, which is used as the first argument, has been saved to %rax as TDCALL leaf ID thus is free to hold the error code for cmovc.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/virt/vmx/tdx/tdxcall.S | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/virt/vmx/tdx/tdxcall.S +2 -4
index 49a54356ae99..3524915d8bd9 100644
@@ -57,10 +57,8 @@
 	 * This value will never be used as actual SEAMCALL error code as
 	 * it is from the Reserved status code class.
 	 */
-	jnc .Lno_vmfailinvalid
-	mov $TDX_SEAMCALL_VMFAILINVALID, %rax
-.Lno_vmfailinvalid:
-
+	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
+	cmovc %rdi, %rax
 	.else
 	tdcall
 	.endif
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by on v1 3/10
#7 lore raw

Re: [PATCH 03/10] x86/tdx: Move FRAME_BEGIN/END to TDX_MODULE_CALL asm macro

In reply to Kai Huang
Quoted 13 lines · Kai Huang

Currently, the TDX_MODULE_CALL asm macro and the __tdx_module_call() take registers directly as input and a 'struct tdx_module_output' as optional output. This is different from the __tdx_hypercall(), which simply uses a structure to carry all input/output. There's no point to leave __tdx_module_call() complicated as it is.

As a preparation to simplify the __tdx_module_call() to make it look like __tdx_hypercall(), move FRAME_BEGIN/END and RET from the __tdx_module_call() to the TDX_MODULE_CALL assembly macro. This also allows more implementation flexibility of the assembly inside the TDX_MODULE_CALL macro, e.g., allowing putting an _ASM_EXTABLE() after the main body of the assembly.

Quoted 8 lines

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>

---

Looks fine to me.

Quoted 46 lines
 arch/x86/coco/tdx/tdcall.S      | 3 ---
 arch/x86/virt/vmx/tdx/tdxcall.S | 5 +++++
 2 files changed, 5 insertions(+), 3 deletions(-)
arch/x86/coco/tdx/tdcall.S +0 -3
index 2eca5f43734f..e5d4b7d8ecd4 100644
@@ -78,10 +78,7 @@
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdx_module_call)
-	FRAME_BEGIN
 	TDX_MODULE_CALL host=0
-	FRAME_END
-	RET
 SYM_FUNC_END(__tdx_module_call)
 
 /*
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
index 3524915d8bd9..b5ab919c7fa8 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <asm/asm-offsets.h>
+#include <asm/frame.h>
 #include <asm/tdx.h>
 
 /*
@@ -18,6 +19,7 @@
  *            TDX module.
  */
 .macro TDX_MODULE_CALL host:req
+	FRAME_BEGIN
 	/*
 	 * R12 will be used as temporary storage for struct tdx_module_output
 	 * pointer. Since R12-R15 registers are not used by TDCALL/SEAMCALL
@@ -91,4 +93,7 @@
 .Lno_output_struct:
 	/* Restore the state of R12 register */
 	pop %r12
+
+	FRAME_END
+	RET
 .endm
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Reviewed-by on v1 3/10
#8 lore raw

Re: [PATCH 03/10] x86/tdx: Move FRAME_BEGIN/END to TDX_MODULE_CALL asm macro

In reply to Kai Huang
Quoted 70 lines · Kai Huang

Currently, the TDX_MODULE_CALL asm macro and the __tdx_module_call() take registers directly as input and a 'struct tdx_module_output' as optional output. This is different from the __tdx_hypercall(), which simply uses a structure to carry all input/output. There's no point to leave __tdx_module_call() complicated as it is.

As a preparation to simplify the __tdx_module_call() to make it look like __tdx_hypercall(), move FRAME_BEGIN/END and RET from the __tdx_module_call() to the TDX_MODULE_CALL assembly macro. This also allows more implementation flexibility of the assembly inside the TDX_MODULE_CALL macro, e.g., allowing putting an _ASM_EXTABLE() after the main body of the assembly.

This is basically based on Peter's code.

Cc Kirill A. Shutemov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 arch/x86/coco/tdx/tdcall.S      | 3 ---
 arch/x86/virt/vmx/tdx/tdxcall.S | 5 +++++
 2 files changed, 5 insertions(+), 3 deletions(-)
arch/x86/coco/tdx/tdcall.S +0 -3
index 2eca5f43734f..e5d4b7d8ecd4 100644
@@ -78,10 +78,7 @@
  * Return status of TDCALL via RAX.
  */
 SYM_FUNC_START(__tdx_module_call)
-	FRAME_BEGIN
 	TDX_MODULE_CALL host=0
-	FRAME_END
-	RET
 SYM_FUNC_END(__tdx_module_call)
 
 /*
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
index 3524915d8bd9..b5ab919c7fa8 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #include <asm/asm-offsets.h>
+#include <asm/frame.h>
 #include <asm/tdx.h>
 
 /*
@@ -18,6 +19,7 @@
  *            TDX module.
  */
 .macro TDX_MODULE_CALL host:req
+	FRAME_BEGIN
 	/*
 	 * R12 will be used as temporary storage for struct tdx_module_output
 	 * pointer. Since R12-R15 registers are not used by TDCALL/SEAMCALL
@@ -91,4 +93,7 @@
 .Lno_output_struct:
 	/* Restore the state of R12 register */
 	pop %r12
+
+	FRAME_END
+	RET
 .endm
Signature · 1 line
2.41.0
Signature · 1 line
Isaku Yamahata <[email protected]>
#44 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Kai Huang
Quoted 15 lines · Kai Huang

Intel Trust Domain Extensions (TDX) protects guest VMs from malicious host and certain physical attacks. A CPU-attested software module called 'the TDX module' runs inside a new isolated memory range as a trusted hypervisor to manage and run protected VMs.

TDX introduces a new CPU mode: Secure Arbitration Mode (SEAM). This mode runs only the TDX module itself or other code to load the TDX module.

The host kernel communicates with SEAM software via a new SEAMCALL instruction. This is conceptually similar to a guest->host hypercall, except it is made from the host to SEAM software instead. The TDX module establishes a new SEAMCALL ABI which allows the host to initialize the module and to manage VMs.

Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure. Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Hi. __seamcall_saved_ret() uses struct tdx_module_arg as input and output. For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[]. It's silly to move those values twice. From kvm_vcpu_arch::regs to tdx_module_args. From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]? Maybe I can make the change with TDX KVM patch series.

Thanks,

Quoted 161 lines

To start to support TDX, create a new arch/x86/virt/vmx/tdx/tdx.c for TDX host kernel support. Add a new Kconfig option CONFIG_INTEL_TDX_HOST to opt-in TDX host kernel support (to distinguish with TDX guest kernel support). So far only KVM uses TDX. Make the new config option depend on KVM_INTEL.

---
 arch/x86/Kconfig                 | 12 +++++++
 arch/x86/Makefile                |  2 ++
 arch/x86/include/asm/tdx.h       |  7 +++++
 arch/x86/virt/Makefile           |  2 ++
 arch/x86/virt/vmx/Makefile       |  2 ++
 arch/x86/virt/vmx/tdx/Makefile   |  2 ++
 arch/x86/virt/vmx/tdx/seamcall.S | 54 ++++++++++++++++++++++++++++++++
 7 files changed, 81 insertions(+)
 create mode 100644 arch/x86/virt/Makefile
 create mode 100644 arch/x86/virt/vmx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/Makefile
 create mode 100644 arch/x86/virt/vmx/tdx/seamcall.S
arch/x86/Kconfig +12 -0
index 53bab123a8ee..191587f75810 100644
@@ -1952,6 +1952,18 @@ config X86_SGX
 
 	  If unsure, say N.
 
+config INTEL_TDX_HOST
+	bool "Intel Trust Domain Extensions (TDX) host support"
+	depends on CPU_SUP_INTEL
+	depends on X86_64
+	depends on KVM_INTEL
+	help
+	  Intel Trust Domain Extensions (TDX) protects guest VMs from malicious
+	  host and certain physical attacks.  This option enables necessary TDX
+	  support in the host kernel to run confidential VMs.
+
+	  If unsure, say N.
+
 config EFI
 	bool "EFI runtime service support"
 	depends on ACPI
arch/x86/Makefile +2 -0
index b39975977c03..ec0e71d8fa30 100644
@@ -252,6 +252,8 @@ archheaders:
 
 libs-y  += arch/x86/lib/
 
+core-y += arch/x86/virt/
+
 # drivers-y are linked after core-y
 drivers-$(CONFIG_MATH_EMULATION) += arch/x86/math-emu/
 drivers-$(CONFIG_PCI)            += arch/x86/pci/
arch/x86/include/asm/tdx.h +7 -0
index 9b0ad0176e58..a82e5249d079 100644
@@ -74,5 +74,12 @@ static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,
 	return -ENODEV;
 }
 #endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */
+
+#ifdef CONFIG_INTEL_TDX_HOST
+u64 __seamcall(u64 fn, struct tdx_module_args *args);
+u64 __seamcall_ret(u64 fn, struct tdx_module_args *args);
+u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args);
+#endif	/* CONFIG_INTEL_TDX_HOST */
+
 #endif /* !__ASSEMBLY__ */
 #endif /* _ASM_X86_TDX_H */
arch/x86/virt/Makefile new file +2 -0
new file mode 100644
index 000000000000..1e36502cd738
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-y	+= vmx/
arch/x86/virt/vmx/Makefile new file +2 -0
new file mode 100644
index 000000000000..feebda21d793
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_INTEL_TDX_HOST)	+= tdx/
arch/x86/virt/vmx/tdx/Makefile new file +2 -0
new file mode 100644
index 000000000000..46ef8f73aebb
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-y += seamcall.o
arch/x86/virt/vmx/tdx/seamcall.S new file +54 -0
new file mode 100644
index 000000000000..650a40843afe
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/linkage.h>
+#include <asm/frame.h>
+
+#include "tdxcall.S"
+
+/*
+ * __seamcall() - Host-side interface functions to SEAM software
+ *                (the P-SEAMLDR or the TDX module).
+ *
+ * __seamcall() function ABI:
+ *
+ * @fn   (RDI)  - SEAMCALL Leaf number, moved to RAX
+ * @args (RSI)  - struct tdx_module_args for input
+ *
+ * Return (via RAX) TDX_SEAMCALL_VMFAILINVALID if the SEAMCALL itself
+ * fails, or the completion status of the SEAMCALL leaf function.
+ */
+SYM_FUNC_START(__seamcall)
+	TDX_MODULE_CALL host=1 ret=0 saved=0
+SYM_FUNC_END(__seamcall)
+
+/*
+ * __seamcall_ret() - Host-side interface functions to SEAM software
+ *                    (the P-SEAMLDR or the TDX module).
+ *
+ * __seamcall_ret() function ABI:
+ *
+ * @fn   (RDI)  - SEAMCALL Leaf number, moved to RAX
+ * @args (RSI)  - struct tdx_module_args for input and output
+ *
+ * Return (via RAX) TDX_SEAMCALL_VMFAILINVALID if the SEAMCALL itself
+ * fails, or the completion status of the SEAMCALL leaf function.
+ */
+SYM_FUNC_START(__seamcall_ret)
+	TDX_MODULE_CALL host=1 ret=1 saved=0
+SYM_FUNC_END(__seamcall_ret)
+
+/*
+ * __seamcall_saved_ret() - Host-side interface functions to SEAM software
+ *                          (the P-SEAMLDR or the TDX module) with extra
+ *                          "callee-saved" registers as input/output.
+ *
+ * __seamcall_saved_ret() function ABI:
+ *
+ * @fn   (RDI)          - SEAMCALL Leaf number, moved to RAX
+ * @args (RSI)          - struct tdx_module_args for input and output
+ *
+ * Return (via RAX) TDX_SEAMCALL_VMFAILINVALID if the SEAMCALL itself
+ * fails, or the completion status of the SEAMCALL leaf function.
+ */
+SYM_FUNC_START(__seamcall_saved_ret)
+	TDX_MODULE_CALL host=1 ret=1 saved=0
+SYM_FUNC_END(__seamcall_saved_ret)
Signature · 1 line
2.41.0
Signature · 1 line
Isaku Yamahata <[email protected]>
author
#45 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

This was one concern that I mentioned VP.ENTER can be implemented by KVM in its own assembly in the TDX host v12 discussion. I kinda agree we should leverage KVM's existing kvm_vcpu_arch::regs[NR_CPU_REGS] infrastructure to minimize the code change to the KVM's common infrastructure. If so, I guess we have to carry this memory copy burden between two structures.

Btw, I do find KVM's VP.ENTER code is a little bit redundant to the common SEAMCALL assembly, which is a good reason for KVM to use __seamcall() variants for TDH.VP.ENTER.

So it's a tradeoff I think.

On the other hand, given CoCo VMs normally don't expose all GPRs to VMM, it's also debatable whether we should invent another infrastructure to the KVM code to handle register access of CoCo VMs too, e.g., we can catch bugs easily when KVM tries to access the registers that it shouldn't access.

It's better KVM maintainer can provide some input here. :)

#46 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);
author
#27 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 34 lines · Peter Zijlstra
Quoted 30 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +25 -1
@@ -72,7 +142,46 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif
+	.endif	/* \ret */
+
+	.if \saved
+	.if \ret && \host
+	/*
+	 * Clear registers shared by guest for VP.ENTER to prevent
+	 * speculative use of guest's values, including those are
+	 * restored from the stack.
+	 *
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.
+	 *
+	 * Note: RBP/RSP are not used as shared register.  RSI has been
+	 * restored already.
+	 *
+	 * XOR is cheap, thus unconditionally do for all leafs.
+	 */
+	xorq %rcx, %rcx
+	xorq %rdx, %rdx
+	xorq %r8,  %r8
+	xorq %r9,  %r9
+	xorq %r10, %r10
+	xorq %r11, %r11
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 174

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	xorq %r12, %r12
+	xorq %r13, %r13
+	xorq %r14, %r14
+	xorq %r15, %r15
+	xorq %rbx, %rbx

^ those are an instant pop below, seems daft to clear them.

Quoted 1 line

I found below comment in KVM code:

arch/x86/virt/vmx/tdx/tdxcall.S line 157

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdxcall.S +4 -0
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.

And KVM explicitly does XOR for the registers that gets "pop"ed almost instantly, so I followed.

But to be honest I don't quite understand this. :-)

author
#18 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 43 lines · Peter Zijlstra
Quoted 34 lines · Peter Zijlstra
Quoted 30 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +25 -1
@@ -72,7 +142,46 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif
+	.endif	/* \ret */
+
+	.if \saved
+	.if \ret && \host
+	/*
+	 * Clear registers shared by guest for VP.ENTER to prevent
+	 * speculative use of guest's values, including those are
+	 * restored from the stack.
+	 *
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.
+	 *
+	 * Note: RBP/RSP are not used as shared register.  RSI has been
+	 * restored already.
+	 *
+	 * XOR is cheap, thus unconditionally do for all leafs.
+	 */
+	xorq %rcx, %rcx
+	xorq %rdx, %rdx
+	xorq %r8,  %r8
+	xorq %r9,  %r9
+	xorq %r10, %r10
+	xorq %r11, %r11
Quoted 7 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 174

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	xorq %r12, %r12
+	xorq %r13, %r13
+	xorq %r14, %r14
+	xorq %r15, %r15
+	xorq %rbx, %rbx

^ those are an instant pop below, seems daft to clear them.

Quoted 5 lines

Also, please use the 32bit variant:

	xorl	%ecx, %ecx

saves a RAX prefix each.

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

I see KVM code uses:

xor %eax, %eax
xor %ecx, %ecx
xor %edx, %edx
xor %ebp, %ebp
xor %esi, %esi
xor %edi, %edi

#ifdef CONFIG_X86_64
        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d
        xor %r11d, %r11d
        xor %r12d, %r12d
        xor %r13d, %r13d
        xor %r14d, %r14d
        xor %r15d, %r15d

#endif

Which makes sense because KVM wants to support 32-bit too.

However for TDX is 64-bit only.

And I also see the current TDVMCALL code has:

        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d                                                       
        xor %r11d, %r11d                                                       
        xor %rdi,  %rdi                                                        
        xor %rdx,  %rdx

Why does it need to use "d" postfix for all r* registers?

Sorry for those questions but I struggled when I wrote those assembly and am hoping to get my mind cleared on this. :-)

Thanks!

#56 lore raw

Re: [PATCH 10/10] x86/virt/tdx: Allow SEAMCALL to handle #UD and #GP

In reply to Kai Huang
Quoted 9 lines · Kai Huang
(quoted diff) +1 -0
@@ -85,6 +86,7 @@
 	.endif	/* \saved */
 
 	.if \host
+1:
 	seamcall
 	/*
 	 * SEAMCALL instruction is essentially a VMExit from VMX root

Review line 107

Quoted 7 lines
(quoted diff) +1 -0
@@ -99,6 +101,7 @@
 	 */
 	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
 	cmovc %rdi, %rax
+2:
 	.else
 	tdcall
 	.endif

This is just wrong, if the thing traps you should not do the return registers. And at this point the mov/cmovc thing doesn't make much sense either.

Quoted 12 lines
(quoted diff) +8 -0
@@ -185,4 +188,21 @@
 
 	FRAME_END
 	RET
+
+	.if \host
+3:
+	/*
+	 * SEAMCALL caused #GP or #UD.  By reaching here %eax contains
+	 * the trap number.  Convert the trap number to the TDX error
+	 * code by setting TDX_SW_ERROR to the high 32-bits of %rax.
+	 *

Review line 208

Quoted 10 lines
(quoted diff) +9 -0
+	 * Note cannot OR TDX_SW_ERROR directly to %rax as OR instruction
+	 * only accepts 32-bit immediate at most.
+	 */
+	movq $TDX_SW_ERROR, %r12
+	orq  %r12, %rax
+	jmp  2b
+
+	_ASM_EXTABLE_FAULT(1b, 3b)
+	.endif	/* \host */
 .endm

Also, please used named labels where possible and *please* keep asm directives unindented.

arch/x86/virt/vmx/tdx/tdxcall.S +27 -29
@@ -56,7 +56,7 @@
 	movq	TDX_MODULE_r10(%rsi), %r10
 	movq	TDX_MODULE_r11(%rsi), %r11
 
-	.if \saved
+.if \saved
 	/*
 	 * Move additional input regs from the structure.  For simplicity
 	 * assume that anything needs the callee-saved regs also tramples
@@ -75,18 +75,18 @@
 	movq	TDX_MODULE_r15(%rsi), %r15
 	movq	TDX_MODULE_rbx(%rsi), %rbx
 
-	.if \ret
+.if \ret
 	/* Save the structure pointer as %rsi is about to be clobbered */
 	pushq	%rsi
-	.endif
+.endif
 
 	movq	TDX_MODULE_rdi(%rsi), %rdi
 	/* %rsi needs to be done at last */
 	movq	TDX_MODULE_rsi(%rsi), %rsi
-	.endif	/* \saved */
+.endif	/* \saved */
 
-	.if \host
-1:
+.if \host
+.Lseamcall:
 	seamcall
 	/*
 	 * SEAMCALL instruction is essentially a VMExit from VMX root
@@ -99,15 +99,13 @@
 	 * This value will never be used as actual SEAMCALL error code as
 	 * it is from the Reserved status code class.
 	 */
-	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
-	cmovc %rdi, %rax
-2:
-	.else
+	jc	.Lseamfail
+.else
 	tdcall
-	.endif
+.endif
 
-	.if \ret
-	.if \saved
+.if \ret
+.if \saved
 	/*
 	 * Restore the structure from stack to saved the output registers
 	 *
@@ -136,7 +134,7 @@
 	movq %r15, TDX_MODULE_r15(%rsi)
 	movq %rbx, TDX_MODULE_rbx(%rsi)
 	movq %rdi, TDX_MODULE_rdi(%rsi)
-	.endif	/* \saved */
+.endif	/* \saved */
 
 	/* Copy output regs to the structure */
 	movq %rcx, TDX_MODULE_rcx(%rsi)
@@ -145,10 +143,11 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif	/* \ret */
+.endif	/* \ret */
 
-	.if \saved
-	.if \ret
+.Lout:
+.if \saved
+.if \ret
 	/*
 	 * Clear registers shared by guest for VP.ENTER and VP.VMCALL to
 	 * prevent speculative use of values from guest/VMM, including
@@ -170,13 +169,8 @@
 	xorq %r9,  %r9
 	xorq %r10, %r10
 	xorq %r11, %r11
-	xorq %r12, %r12
-	xorq %r13, %r13
-	xorq %r14, %r14
-	xorq %r15, %r15
-	xorq %rbx, %rbx
 	xorq %rdi, %rdi
-	.endif	/* \ret */
+.endif	/* \ret */
 
 	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
 	popq	%r15
@@ -184,13 +178,17 @@
 	popq	%r13
 	popq	%r12
 	popq	%rbx
-	.endif	/* \saved */
+.endif	/* \saved */
 
 	FRAME_END
 	RET
 
-	.if \host
-3:
+.if \host
+.Lseamfail:
+	mov	$TDX_SEAMCALL_VMFAILINVALID, %rax
+	jmp	.Lout
+
+.Lseamtrap:
 	/*
 	 * SEAMCALL caused #GP or #UD.  By reaching here %eax contains
 	 * the trap number.  Convert the trap number to the TDX error
@@ -201,8 +199,8 @@
 	 */
 	movq $TDX_SW_ERROR, %r12
 	orq  %r12, %rax
-	jmp  2b
+	jmp .Lout
 
-	_ASM_EXTABLE_FAULT(1b, 3b)
-	.endif	/* \host */
+	_ASM_EXTABLE_FAULT(.Lseamcall, .Lseamtrap)
+.endif	/* \host */
 .endm
author
#32 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +16 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

No VP.ENTER doesn't return 0 for RAX. Firstly, VP.ENTER can return for many reasons but not limited to TDVMCALL (e.g., due to interrupt), and for those cases RAX contains valid non-zero value. Secondly, even for TDVMCALL, the RAX isn't 0:

Table 6.256 TDX 1.5 ABI spec:

RAX SEAMCALL instruction return code
The DETAILS_L2 field in bits 31:0 contain the VMCS exit reason, indicating TDCALL (77).

author
#47 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Peter Zijlstra
Quoted 22 lines · Peter Zijlstra
Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

Quoted 9 lines

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);

I don't think the layout matches hardware, especially I think there's no "hardware layout" for GPRs that are concerned here. They are just for KVM itself to save guest's registers when the guest exits to KVM, so that KVM can restore them when returning back to the guest.

#19 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Quoted 17 lines

I see KVM code uses:

xor %eax, %eax
xor %ecx, %ecx
xor %edx, %edx
xor %ebp, %ebp
xor %esi, %esi
xor %edi, %edi

#ifdef CONFIG_X86_64
        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d
        xor %r11d, %r11d
        xor %r12d, %r12d
        xor %r13d, %r13d
        xor %r14d, %r14d
        xor %r15d, %r15d
Quoted 3 lines

#endif

Which makes sense because KVM wants to support 32-bit too.

Encoding for the first lot is shorter, the 64bit regs obviously need the RAX byte anyway.

Quoted 4 lines

However for TDX is 64-bit only.

And I also see the current TDVMCALL code has:

Quoted 8 lines
        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d                                                       
        xor %r11d, %r11d                                                       
        xor %rdi,  %rdi                                                        
        xor %rdx,  %rdx

Why does it need to use "d" postfix for all r* registers?

That's the name of the 32bit subword, r#[bwd] for byte, word, double-word. SDM v1 3.7.2.1 has the whole list, I couldn't quicky find one for the zero-extention thing.

Quoted 2 lines

Sorry for those questions but I struggled when I wrote those assembly and am hoping to get my mind cleared on this. :-)

No problem.

#28 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 11 lines · Kai

I found below comment in KVM code:

arch/x86/virt/vmx/tdx/tdxcall.S line 157

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdxcall.S +4 -0
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.

And KVM explicitly does XOR for the registers that gets "pop"ed almost instantly, so I followed.

But to be honest I don't quite understand this. :-)

Urgh, I suppose that actually makes sense. Since pop is a load it might continue speculation with the previous value. Whereas the xor-clear idiom is impossible to speculate through.

Oh well...

#33 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 31 lines · Kai
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +16 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

Quoted 1 line

No VP.ENTER doesn't return 0 for RAX. Firstly, VP.ENTER can return for many

No, but it *does* return 0 for: RBX,RSI,RDI,R10-R15.

So for \host you can simply do:

	pop	%rsi
	mov	$0, TDX_MODULE_rsi(%rsi)

and call it a day.

#48 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Huang, Kai
Quoted 29 lines · Kai
Quoted 28 lines · Peter Zijlstra
Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

Quoted 8 lines
Quoted 3 lines
	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);

I don't think the layout matches hardware, especially I think there's no "hardware layout" for GPRs that are concerned here. They are just for KVM itself to save guest's registers when the guest exits to KVM, so that KVM can restore them when returning back to the guest.

Either way around it should be possible to make them match I suppose. Ideally we get the callee-clobbered regs first, but if not, I don't think that's too big of a problem.

author
#34 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 34 lines · Peter Zijlstra
Quoted 31 lines · Kai
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +16 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

Quoted 1 line

No VP.ENTER doesn't return 0 for RAX. Firstly, VP.ENTER can return for many

Quoted 8 lines

No, but it *does* return 0 for: RBX,RSI,RDI,R10-R15.

So for \host you can simply do:

	pop	%rsi
	mov	$0, TDX_MODULE_rsi(%rsi)

and call it a day.

This isn't true for the case that VP.ENTER returns due to a TDVMCALL. In that case RCX contains the bitmap of shared registers, and RBX/RDX/RDI/RSI/R8-R15 contains guest value if the corresponding bit is set in RCX (RBP will be excluded by updating the spec I assume).

Or are you suggesting we need to decode RAX to decide whether the VP.ENTER return is due to TDVMCALL vs other reasons, and act differently?

author
#49 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Peter Zijlstra
Quoted 34 lines · Peter Zijlstra
Quoted 33 lines · Kai
Quoted 31 lines · Peter Zijlstra
Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);
Quoted 9 lines
Quoted 4 lines

I don't think the layout matches hardware, especially I think there's no "hardware layout" for GPRs that are concerned here. They are just for KVM itself to save guest's registers when the guest exits to KVM, so that KVM can restore them when returning back to the guest.

Either way around it should be possible to make them match I suppose. Ideally we get the callee-clobbered regs first, but if not, I don't think that's too big of a problem.

Yeah we can. I'll leave this to KVM TDX patches.

#35 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 44 lines · Kai
Quoted 34 lines · Peter Zijlstra
Quoted 31 lines · Kai
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +16 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

Quoted 1 line

No VP.ENTER doesn't return 0 for RAX. Firstly, VP.ENTER can return for many

Quoted 8 lines

No, but it *does* return 0 for: RBX,RSI,RDI,R10-R15.

So for \host you can simply do:

	pop	%rsi
	mov	$0, TDX_MODULE_rsi(%rsi)

and call it a day.

Quoted 7 lines

This isn't true for the case that VP.ENTER returns due to a TDVMCALL. In that case RCX contains the bitmap of shared registers, and RBX/RDX/RDI/RSI/R8-R15 contains guest value if the corresponding bit is set in RCX (RBP will be excluded by updating the spec I assume).

Or are you suggesting we need to decode RAX to decide whether the VP.ENTER return is due to TDVMCALL vs other reasons, and act differently?

Urgh, no I had missed there are *TWO* tables for output :/ Who does something like that :-(

So yeah, sucks.

author
#29 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra
Quoted 10 lines · Kai

I found below comment in KVM code:

arch/x86/virt/vmx/tdx/tdxcall.S line 157

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdxcall.S +4 -0
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.

And KVM explicitly does XOR for the registers that gets "pop"ed almost instantly, so I followed.

Quoted 7 lines
Quoted 1 line

But to be honest I don't quite understand this. :-)

Urgh, I suppose that actually makes sense. Since pop is a load it might continue speculation with the previous value. Whereas the xor-clear idiom is impossible to speculate through.

Oh well...

Then should I keep those registers that are "pop"ed immediately afterwards?

#30 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

Quoted 15 lines · Kai
Quoted 14 lines · Peter Zijlstra
Quoted 11 lines · Kai

I found below comment in KVM code:

arch/x86/virt/vmx/tdx/tdxcall.S line 157

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdxcall.S +4 -0
+	 * See arch/x86/kvm/vmx/vmenter.S:
+	 *
+	 * In theory, a L1 cache miss when restoring register from stack
+	 * could lead to speculative execution with guest's values.

And KVM explicitly does XOR for the registers that gets "pop"ed almost instantly, so I followed.

But to be honest I don't quite understand this. :-)

Quoted 7 lines
Quoted 5 lines

Urgh, I suppose that actually makes sense. Since pop is a load it might continue speculation with the previous value. Whereas the xor-clear idiom is impossible to speculate through.

Oh well...

Then should I keep those registers that are "pop"ed immediately afterwards?

Yeah, I suppose so.

author
#57 lore raw

Re: [PATCH 10/10] x86/virt/tdx: Allow SEAMCALL to handle #UD and #GP

In reply to Peter Zijlstra
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +1 -0
@@ -85,6 +86,7 @@
 	.endif	/* \saved */
 
 	.if \host
+1:
 	seamcall
 	/*
 	 * SEAMCALL instruction is essentially a VMExit from VMX root
@@ -99,6 +101,7 @@
 	 */
 	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
 	cmovc %rdi, %rax
Quoted 8 lines

Review line 107

Quoted 4 lines
(quoted diff) +1 -0
+2:
 	.else
 	tdcall
 	.endif

This is just wrong, if the thing traps you should not do the return registers. And at this point the mov/cmovc thing doesn't make much sense either.

OK will do. Yes "do return registers" isn't necessary. I thought to keep code simple we can just do it. The trap/VMFAILINVALID code path isn't in performance path anyway.

This is a problem in the current upstream code too. I'll fix it first in a separate patch.

Quoted 18 lines
Quoted 18 lines
(quoted diff) +13 -0
@@ -185,4 +188,21 @@
 
 	FRAME_END
 	RET
+
+	.if \host
+3:
+	/*
+	 * SEAMCALL caused #GP or #UD.  By reaching here %eax contains
+	 * the trap number.  Convert the trap number to the TDX error
+	 * code by setting TDX_SW_ERROR to the high 32-bits of %rax.
+	 *
+	 * Note cannot OR TDX_SW_ERROR directly to %rax as OR instruction
+	 * only accepts 32-bit immediate at most.
+	 */
+	movq $TDX_SW_ERROR, %r12
+	orq  %r12, %rax
Quoted 8 lines

Review line 208

Quoted 5 lines
(quoted diff) +4 -0
+	jmp  2b
+
+	_ASM_EXTABLE_FAULT(1b, 3b)
+	.endif	/* \host */
 .endm

Also, please used named labels where possible and *please* keep asm directives unindented.

Yes will do.

Quoted 119 lines
arch/x86/virt/vmx/tdx/tdxcall.S +24 -26
@@ -56,7 +56,7 @@
 	movq	TDX_MODULE_r10(%rsi), %r10
 	movq	TDX_MODULE_r11(%rsi), %r11
 
-	.if \saved
+.if \saved
 	/*
 	 * Move additional input regs from the structure.  For simplicity
 	 * assume that anything needs the callee-saved regs also tramples
@@ -75,18 +75,18 @@
 	movq	TDX_MODULE_r15(%rsi), %r15
 	movq	TDX_MODULE_rbx(%rsi), %rbx
 
-	.if \ret
+.if \ret
 	/* Save the structure pointer as %rsi is about to be clobbered */
 	pushq	%rsi
-	.endif
+.endif
 
 	movq	TDX_MODULE_rdi(%rsi), %rdi
 	/* %rsi needs to be done at last */
 	movq	TDX_MODULE_rsi(%rsi), %rsi
-	.endif	/* \saved */
+.endif	/* \saved */
 
-	.if \host
-1:
+.if \host
+.Lseamcall:
 	seamcall
 	/*
 	 * SEAMCALL instruction is essentially a VMExit from VMX root
@@ -99,15 +99,13 @@
 	 * This value will never be used as actual SEAMCALL error code as
 	 * it is from the Reserved status code class.
 	 */
-	mov $TDX_SEAMCALL_VMFAILINVALID, %rdi
-	cmovc %rdi, %rax
-2:
-	.else
+	jc	.Lseamfail
+.else
 	tdcall
-	.endif
+.endif
 
-	.if \ret
-	.if \saved
+.if \ret
+.if \saved
 	/*
 	 * Restore the structure from stack to saved the output registers
 	 *
@@ -136,7 +134,7 @@
 	movq %r15, TDX_MODULE_r15(%rsi)
 	movq %rbx, TDX_MODULE_rbx(%rsi)
 	movq %rdi, TDX_MODULE_rdi(%rsi)
-	.endif	/* \saved */
+.endif	/* \saved */
 
 	/* Copy output regs to the structure */
 	movq %rcx, TDX_MODULE_rcx(%rsi)
@@ -145,10 +143,11 @@
 	movq %r9,  TDX_MODULE_r9(%rsi)
 	movq %r10, TDX_MODULE_r10(%rsi)
 	movq %r11, TDX_MODULE_r11(%rsi)
-	.endif	/* \ret */
+.endif	/* \ret */
 
-	.if \saved
-	.if \ret
+.Lout:
+.if \saved
+.if \ret
 	/*
 	 * Clear registers shared by guest for VP.ENTER and VP.VMCALL to
 	 * prevent speculative use of values from guest/VMM, including
@@ -170,13 +169,8 @@
 	xorq %r9,  %r9
 	xorq %r10, %r10
 	xorq %r11, %r11
-	xorq %r12, %r12
-	xorq %r13, %r13
-	xorq %r14, %r14
-	xorq %r15, %r15
-	xorq %rbx, %rbx
 	xorq %rdi, %rdi
-	.endif	/* \ret */
+.endif	/* \ret */
 
 	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
 	popq	%r15
@@ -184,13 +178,17 @@
 	popq	%r13
 	popq	%r12
 	popq	%rbx
-	.endif	/* \saved */
+.endif	/* \saved */
 
 	FRAME_END
 	RET
 
-	.if \host
-3:
+.if \host
+.Lseamfail:
+	mov	$TDX_SEAMCALL_VMFAILINVALID, %rax
+	jmp	.Lout
+
+.Lseamtrap:
 	/*
 	 * SEAMCALL caused #GP or #UD.  By reaching here %eax contains
 	 * the trap number.  Convert the trap number to the TDX error

arch/x86/virt/vmx/tdx/tdxcall.S line 202

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +1 -1
@@ -201,8 +199,8 @@
 	 */
 	movq $TDX_SW_ERROR, %r12
 	orq  %r12, %rax
-	jmp  2b
+	jmp .Lout

Thanks for the code.

There might be stack balancing issue here. I'll double check when updating this patch.

Thanks!

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdxcall.S +2 -2
-	_ASM_EXTABLE_FAULT(1b, 3b)
-	.endif	/* \host */
+	_ASM_EXTABLE_FAULT(.Lseamcall, .Lseamtrap)
+.endif	/* \host */
 .endm
author
#36 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 50 lines · Peter Zijlstra
Quoted 49 lines · Kai
Quoted 34 lines · Peter Zijlstra
Quoted 31 lines · Kai
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Kai Huang
arch/x86/virt/vmx/tdx/tdxcall.S +16 -0
@@ -65,6 +104,37 @@
 	.endif
 
 	.if \ret
+	.if \saved
+	/*
+	 * Restore the structure from stack to saved the output registers
+	 *
+	 * In case of VP.ENTER returns due to TDVMCALL, all registers are
+	 * valid thus no register can be used as spare to restore the
+	 * structure from the stack (see "TDH.VP.ENTER Output Operands
+	 * Definition on TDCALL(TDG.VP.VMCALL) Following a TD Entry").
+	 * For this case, need to make one register as spare by saving it
+	 * to the stack and then manually load the structure pointer to
+	 * the spare register.
+	 *
+	 * Note for other TDCALLs/SEAMCALLs there are spare registers
+	 * thus no need for such hack but just use this for all for now.
+	 */
+	pushq	%rax		/* save the TDCALL/SEAMCALL return code */
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdxcall.S line 127

Quoted 5 lines
arch/x86/virt/vmx/tdx/tdxcall.S +5 -0
+	movq	8(%rsp), %rax	/* restore the structure pointer */
+	movq	%rsi, TDX_MODULE_rsi(%rax)	/* save %rsi */
+	movq	%rax, %rsi	/* use %rsi as structure pointer */
+	popq	%rax		/* restore the return code */
+	popq	%rsi		/* pop the structure pointer */

Urgghh... At least for the \host case you can simply pop %rsi, no? VP.ENTER returns with 0 there IIRC.

Quoted 1 line

No VP.ENTER doesn't return 0 for RAX. Firstly, VP.ENTER can return for many

Quoted 8 lines

No, but it *does* return 0 for: RBX,RSI,RDI,R10-R15.

So for \host you can simply do:

	pop	%rsi
	mov	$0, TDX_MODULE_rsi(%rsi)

and call it a day.

This isn't true for the case that VP.ENTER returns due to a TDVMCALL. In that case RCX contains the bitmap of shared registers, and RBX/RDX/RDI/RSI/R8-R15 contains guest value if the corresponding bit is set in RCX (RBP will be excluded by updating the spec I assume).

Quoted 7 lines
Quoted 2 lines

Or are you suggesting we need to decode RAX to decide whether the VP.ENTER return is due to TDVMCALL vs other reasons, and act differently?

Urgh, no I had missed there are *TWO* tables for output :/ Who does something like that :-(

So yeah, sucks.

Yeah. I think for code simplicity we should just do the way the current patch has implemented :-)

author
#20 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Quoted 38 lines
Quoted 18 lines

I see KVM code uses:

xor %eax, %eax
xor %ecx, %ecx
xor %edx, %edx
xor %ebp, %ebp
xor %esi, %esi
xor %edi, %edi

#ifdef CONFIG_X86_64
        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d
        xor %r11d, %r11d
        xor %r12d, %r12d
        xor %r13d, %r13d
        xor %r14d, %r14d
        xor %r15d, %r15d
Quoted 3 lines

#endif

Which makes sense because KVM wants to support 32-bit too.

Encoding for the first lot is shorter, the 64bit regs obviously need the RAX byte anyway.

Quoted 4 lines

However for TDX is 64-bit only.

And I also see the current TDVMCALL code has:

Quoted 8 lines
        xor %r8d,  %r8d
        xor %r9d,  %r9d
        xor %r10d, %r10d                                                       
        xor %r11d, %r11d                                                       
        xor %rdi,  %rdi                                                        
        xor %rdx,  %rdx

Why does it need to use "d" postfix for all r* registers?

Quoted 9 lines

That's the name of the 32bit subword, r#[bwd] for byte, word, double-word. SDM v1 3.7.2.1 has the whole list, I couldn't quicky find one for the zero-extention thing

Quoted 2 lines

Sorry for those questions but I struggled when I wrote those assembly and am hoping to get my mind cleared on this. :-)

No problem.

I _think_ I understand now? In 64-bit mode

xor %eax, %eax

equals to

xor %rax, %rax

(due to "architecture zero-extends 32bit stores")

Thus using the former (plus using "d" for %r*) can save some memory?

author
#21 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

Quoted 11 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Hmm.. I think I found it -- it's in SDM vol 1:

3.4.1.1 General-Purpose Registers in 64-Bit Mode

32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register.

Sorry for the noise!

author
#5 lore raw

Re: [PATCH 02/10] x86/tdx: Use cmovc to save a label in TDX_MODULE_CALL asm

In reply to Sathyanarayanan Kuppuswamy
Quoted 6 lines · Sathyanarayanan Kuppuswamy
Quoted 2 lines · Kai Huang

Change 'jnc .Lno_vmfailinvalid' to 'cmovc %rdi, %rax' to save the .Lno_vmfailinvalid label in the TDX_MODULE_CALL asm macro.

You are removing the label, right? What use "save"?

Per comments to patch 10 I'll drop this patch, so doesn't matter anymore.

#23 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 9 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Yeah, I couldn't find it in a hurry either, but bpetkov pasted me this from the AMD document:

"In 64-bit mode, the following general rules apply to instructions and their operands:
“Promoted to 64 Bit”: If an instruction’s operand size (16-bit or 32-bit) in legacy and compatibility modes depends on the CS.D bit and the operand-size override prefix, then the operand-size choices in 64-bit mode are extended from 16-bit and 32-bit to include 64 bits (with a REX prefix), or the operand size is fixed at 64 bits. Such instructions are said to be “Promoted to 64 bits” in Table B-1. However, byte-operand opcodes of such instructions are not promoted."

Quoted 11 lines

I _think_ I understand now? In 64-bit mode

xor %eax, %eax

equals to

xor %rax, %rax

(due to "architecture zero-extends 32bit stores")

Thus using the former (plus using "d" for %r*) can save some memory?

Yes, 64bit wide instruction get a REX prefix 0x4X (somehow I keep typing RAX) byte in front to tell it's a 64bit wide op.

   31 c0                   xor    %eax,%eax
   48 31 c0                xor    %rax,%rax

The REX byte will show up for rN usage, because then we need the actual Register Extention part of that prefix irrespective of the width.

   45 31 d2                xor    %r10d,%r10d
   4d 31 d2                xor    %r10,%r10

x86 instruction encoding is 'fun' :-)

See SDM Vol 2 2.2.1.2 if you want to know more about the REX prefix.

#22 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Huang, Kai
Quoted 13 lines · Kai
Quoted 11 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Quoted 6 lines

Hmm.. I think I found it -- it's in SDM vol 1:

3.4.1.1 General-Purpose Registers in 64-Bit Mode

32-bit operands generate a 32-bit result, zero-extended to a 64-bit result in the destination general-purpose register.

Yes, that's it.

author
#24 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

In reply to Peter Zijlstra
Quoted 42 lines · Peter Zijlstra
Quoted 9 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Yeah, I couldn't find it in a hurry either, but bpetkov pasted me this from the AMD document:

"In 64-bit mode, the following general rules apply to instructions and their operands:
“Promoted to 64 Bit”: If an instruction’s operand size (16-bit or 32-bit) in legacy and compatibility modes depends on the CS.D bit and the operand-size override prefix, then the operand-size choices in 64-bit mode are extended from 16-bit and 32-bit to include 64 bits (with a REX prefix), or the operand size is fixed at 64 bits. Such instructions are said to be “Promoted to 64 bits” in Table B-1. However, byte-operand opcodes of such instructions are not promoted."

Quoted 11 lines

I _think_ I understand now? In 64-bit mode

xor %eax, %eax

equals to

xor %rax, %rax

(due to "architecture zero-extends 32bit stores")

Thus using the former (plus using "d" for %r*) can save some memory?

Yes, 64bit wide instruction get a REX prefix 0x4X (somehow I keep typing RAX) byte in front to tell it's a 64bit wide op.

   31 c0                   xor    %eax,%eax
   48 31 c0                xor    %rax,%rax

The REX byte will show up for rN usage, because then we need the actual Register Extention part of that prefix irrespective of the width.

Quoted 6 lines
   45 31 d2                xor    %r10d,%r10d
   4d 31 d2                xor    %r10,%r10

x86 instruction encoding is 'fun' :-)

See SDM Vol 2 2.2.1.2 if you want to know more about the REX prefix.

Learned something new. Appreciate your time! :-)

#25 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

Quoted 43 lines · Kai
Quoted 42 lines · Peter Zijlstra
Quoted 9 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Yeah, I couldn't find it in a hurry either, but bpetkov pasted me this from the AMD document:

"In 64-bit mode, the following general rules apply to instructions and their operands:
“Promoted to 64 Bit”: If an instruction’s operand size (16-bit or 32-bit) in legacy and compatibility modes depends on the CS.D bit and the operand-size override prefix, then the operand-size choices in 64-bit mode are extended from 16-bit and 32-bit to include 64 bits (with a REX prefix), or the operand size is fixed at 64 bits. Such instructions are said to be “Promoted to 64 bits” in Table B-1. However, byte-operand opcodes of such instructions are not promoted."

Quoted 11 lines

I _think_ I understand now? In 64-bit mode

xor %eax, %eax

equals to

xor %rax, %rax

(due to "architecture zero-extends 32bit stores")

Thus using the former (plus using "d" for %r*) can save some memory?

Yes, 64bit wide instruction get a REX prefix 0x4X (somehow I keep typing RAX) byte in front to tell it's a 64bit wide op.

   31 c0                   xor    %eax,%eax
   48 31 c0                xor    %rax,%rax

The REX byte will show up for rN usage, because then we need the actual Register Extention part of that prefix irrespective of the width.

Quoted 8 lines
Quoted 6 lines
   45 31 d2                xor    %r10d,%r10d
   4d 31 d2                xor    %r10,%r10

x86 instruction encoding is 'fun' :-)

See SDM Vol 2 2.2.1.2 if you want to know more about the REX prefix.

Learned something new. Appreciate your time! :-)

And now for the extra fun...

The Silvermont uarch is 64bit, but only recognises 32bit XORs as zeroing idioms.

So for best performance on as many uarches as possible, you should *always* use the 32bit forms, even for %r8-15.

~Andrew

author
#26 lore raw

Re: [PATCH 07/10] x86/tdx: Extend TDX_MODULE_CALL to support more TDCALL/SEAMCALL leafs

Quoted 53 lines · Andrew Cooper
Quoted 43 lines · Kai
Quoted 42 lines · Peter Zijlstra
Quoted 9 lines · Kai
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Kai

Sorry I am ignorant here. Won't "clearing ECX only" leave high bits of registers still containing guest's value?

architecture zero-extends 32bit stores

Sorry, where can I find this information? Looking at SDM I couldn't find :-(

Yeah, I couldn't find it in a hurry either, but bpetkov pasted me this from the AMD document:

"In 64-bit mode, the following general rules apply to instructions and their operands:
“Promoted to 64 Bit”: If an instruction’s operand size (16-bit or 32-bit) in legacy and compatibility modes depends on the CS.D bit and the operand-size override prefix, then the operand-size choices in 64-bit mode are extended from 16-bit and 32-bit to include 64 bits (with a REX prefix), or the operand size is fixed at 64 bits. Such instructions are said to be “Promoted to 64 bits” in Table B-1. However, byte-operand opcodes of such instructions are not promoted."

Quoted 11 lines

I _think_ I understand now? In 64-bit mode

xor %eax, %eax

equals to

xor %rax, %rax

(due to "architecture zero-extends 32bit stores")

Thus using the former (plus using "d" for %r*) can save some memory?

Yes, 64bit wide instruction get a REX prefix 0x4X (somehow I keep typing RAX) byte in front to tell it's a 64bit wide op.

   31 c0                   xor    %eax,%eax
   48 31 c0                xor    %rax,%rax

The REX byte will show up for rN usage, because then we need the actual Register Extention part of that prefix irrespective of the width.

Quoted 8 lines
Quoted 6 lines
   45 31 d2                xor    %r10d,%r10d
   4d 31 d2                xor    %r10,%r10

x86 instruction encoding is 'fun' :-)

See SDM Vol 2 2.2.1.2 if you want to know more about the REX prefix.

Learned something new. Appreciate your time! :-)

Quoted 9 lines

And now for the extra fun...

The Silvermont uarch is 64bit, but only recognises 32bit XORs as zeroing idioms.

So for best performance on as many uarches as possible, you should *always* use the 32bit forms, even for %r8-15.

Ah thanks Andrew for the tip :)

#50 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Huang, Kai
Quoted 29 lines · Kai Huang
Quoted 28 lines · Peter Zijlstra
Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.� Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.� __seamcall_saved_ret() uses struct tdx_module_arg as input and output.� For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].� It's silly to move those values twice.� From kvm_vcpu_arch::regs to tdx_module_args.� From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?� Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

Quoted 8 lines
Quoted 3 lines
	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);

I don't think the layout matches hardware, especially I think there's no "hardware layout" for GPRs that are concerned here. They are just for KVM itself to save guest's registers when the guest exits to KVM, so that KVM can restore them when returning back to the guest.

kvm_vcpu_arch::regs does follow the hardware-defined indices, and that's required for myriad emulation flows, e.g. saving GPRs into SMRAM, anywhere KVM gets a GPR index from an instruction opcode or vmcs.VMX_INSTRUCTION_INFO, etc.

#52 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Huang, Kai
Quoted 31 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

This was one concern that I mentioned VP.ENTER can be implemented by KVM in its own assembly in the TDX host v12 discussion. I kinda agree we should leverage KVM's existing kvm_vcpu_arch::regs[NR_CPU_REGS] infrastructure to minimize the code change to the KVM's common infrastructure. If so, I guess we have to carry this memory copy burden between two structures.

Btw, I do find KVM's VP.ENTER code is a little bit redundant to the common SEAMCALL assembly, which is a good reason for KVM to use __seamcall() variants for TDH.VP.ENTER.

Quoted 6 lines

So it's a tradeoff I think.

On the other hand, given CoCo VMs normally don't expose all GPRs to VMM, it's also debatable whether we should invent another infrastructure to the KVM code to handle register access of CoCo VMs too, e.g., we can catch bugs easily when KVM tries to access the registers that it shouldn't access.

Yes, we'd like to save/restore GPRs only for TDVMCALL. Otherwise skip save/restore.

Signature · 1 line
Isaku Yamahata <[email protected]>
#10 lore raw

Re: [PATCH 04/10] x86/tdx: Make macros of TDCALLs consistent with the spec

In reply to Kai Huang
Quoted 26 lines · Kai Huang

The TDX spec names all TDCALLs with prefix "TDG". Currently, the kernel doesn't follow such convention for the macros of those TDCALLs but uses prefix "TDX_" for all of them. Although it's arguable whether the TDX spec names those TDCALLs properly, it's better for the kernel to follow the spec when naming those macros.

Change all macros of TDCALLs to make them consistent with the spec. As a bonus, they get distinguished easily from the host-side SEAMCALLs, which all have prefix "TDH".

No functional change intended.

---
  arch/x86/coco/tdx/tdx.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/coco/tdx/tdx.c +0 -1
index 5b8056f6c83f..de021df92009 100644
@@ -15,11 +15,11 @@
  #include <asm/pgtable.h>
 
  /* TDX module Call Leaf IDs */
-#define TDX_GET_INFO			1

arch/x86/coco/tdx/tdx.c line 23

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +5 -4
-#define TDX_GET_VEINFO			3
-#define TDX_GET_REPORT			4
-#define TDX_ACCEPT_PAGE			6
-#define TDX_WR				8
+#define TDG_VP_INFO			1
+#define TDG_VP_VEINFO_GET		3
+#define TDG_MR_REPORT			4
+#define TDG_MEM_PAGE_ACCEPT		6
+#define TDG_VM_WR			8
 

What branch is this patch set based off? Because the existing TDX_GET_* defines are in arch/x86/include/asm/shared/tdx.h due to ff40b5769a50f ?

<snip>

#11 lore raw

Re: [PATCH 04/10] x86/tdx: Make macros of TDCALLs consistent with the spec

In reply to Kai Huang
Quoted 26 lines · Kai Huang

The TDX spec names all TDCALLs with prefix "TDG". Currently, the kernel doesn't follow such convention for the macros of those TDCALLs but uses prefix "TDX_" for all of them. Although it's arguable whether the TDX spec names those TDCALLs properly, it's better for the kernel to follow the spec when naming those macros.

Change all macros of TDCALLs to make them consistent with the spec. As a bonus, they get distinguished easily from the host-side SEAMCALLs, which all have prefix "TDH".

No functional change intended.

---
  arch/x86/coco/tdx/tdx.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/coco/tdx/tdx.c +0 -1
index 5b8056f6c83f..de021df92009 100644
@@ -15,11 +15,11 @@
  #include <asm/pgtable.h>
 
  /* TDX module Call Leaf IDs */
-#define TDX_GET_INFO			1

arch/x86/coco/tdx/tdx.c line 23

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +5 -4
-#define TDX_GET_VEINFO			3
-#define TDX_GET_REPORT			4
-#define TDX_ACCEPT_PAGE			6
-#define TDX_WR				8
+#define TDG_VP_INFO			1
+#define TDG_VP_VEINFO_GET		3
+#define TDG_MR_REPORT			4
+#define TDG_MEM_PAGE_ACCEPT		6
+#define TDG_VM_WR			8
 

What branch is this patch set based off? Because the existing TDX_GET_* defines are in arch/x86/include/asm/shared/tdx.h due to ff40b5769a50f ?

<snip>

#38 lore raw

Re: [PATCH 08/10] x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

In reply to Kai Huang

<snip>

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +0 -0
index 0f16ba52ae62..a5e77893b2c0 100644

arch/x86/coco/tdx/tdx.c line 59

Quoted 9 lines
arch/x86/coco/tdx/tdx.c +6 -0
@@ -51,13 +51,38 @@
 
  #define TDREPORT_SUBTYPE_0	0
 
+/* Called from __tdx_hypercall() for unrecoverable failure */
+static noinstr void __tdx_hypercall_failed(void)
+{
+	instrumentation_begin();
+	panic("TDVMCALL failed. TDX module bug?");
+}

So what's the deal with this instrumentation here. The instruction is noinstr, so you want to make just the panic call itself instrumentable?, if so where's the instrumentation_end() cal;?No instrumentation_end() call. Actually is this complexity really worth it for the failure case?

AFAICS there is a single call site for __tdx_hypercall_failed so why noot call panic() directly ?

arch/x86/coco/tdx/tdx.c line 69

Quoted 10 lines
arch/x86/coco/tdx/tdx.c +10 -0
+
+static inline u64 __tdx_hypercall(struct tdx_module_args *args)
+{
+	u64 ret;
+
+	args->rcx = TDVMCALL_EXPOSE_REGS_MASK;
+	ret = __tdcall_saved_ret(TDG_VP_VMCALL, args);
+
+	/*
+	 * RAX!=0 indicates a failure of the TDVMCALL mechanism itself and that

nit: Why mention the register explicitly, just say that if __tdcall_saved_ret returns non-zero ....

Quoted 22 lines
arch/x86/coco/tdx/tdx.c +12 -3
+	 * something has gone horribly wrong with the TDX module.
+	 */
+	if (ret)
+		__tdx_hypercall_failed();
+
+	/* The return status of the hypercall itself is in R10. */
+	return args->r10;
+}
+
  /*
- * Wrapper for standard use of __tdx_hypercall with no output aside from
- * return code.
+ * Wrapper for standard use of __tdx_hypercall() w/o needing any output
+ * register except the return code.
   */
  static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
  {
-	struct tdx_hypercall_args args = {
+	struct tdx_module_args args = {
  		.r10 = TDX_HYPERCALL_STANDARD,
  		.r11 = fn,
  		.r12 = r12,

<snip>

author
#12 lore raw

Re: [PATCH 04/10] x86/tdx: Make macros of TDCALLs consistent with the spec

In reply to Nikolay Borisov
Quoted 26 lines · Nikolay Borisov
Quoted 24 lines · Kai Huang

The TDX spec names all TDCALLs with prefix "TDG". Currently, the kernel doesn't follow such convention for the macros of those TDCALLs but uses prefix "TDX_" for all of them. Although it's arguable whether the TDX spec names those TDCALLs properly, it's better for the kernel to follow the spec when naming those macros.

Change all macros of TDCALLs to make them consistent with the spec. As a bonus, they get distinguished easily from the host-side SEAMCALLs, which all have prefix "TDH".

No functional change intended.

---
  arch/x86/coco/tdx/tdx.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
arch/x86/coco/tdx/tdx.c +0 -0
index 5b8056f6c83f..de021df92009 100644
@@ -15,11 +15,11 @@
  #include <asm/pgtable.h>
 
Quoted 15 lines

arch/x86/coco/tdx/tdx.c line 23

Quoted 12 lines
arch/x86/coco/tdx/tdx.c +5 -5
  /* TDX module Call Leaf IDs */
-#define TDX_GET_INFO			1
-#define TDX_GET_VEINFO			3
-#define TDX_GET_REPORT			4
-#define TDX_ACCEPT_PAGE			6
-#define TDX_WR				8
+#define TDG_VP_INFO			1
+#define TDG_VP_VEINFO_GET		3
+#define TDG_MR_REPORT			4
+#define TDG_MEM_PAGE_ACCEPT		6
+#define TDG_VM_WR			8
 

What branch is this patch set based off? Because the existing TDX_GET_* defines are in arch/x86/include/asm/shared/tdx.h due to ff40b5769a50f ?

It was based on tip/x86/tdx, which didn't have above patch. I'll rebase to the Linus's tree for the next round since it now has all TDX patches anyway.

author
#51 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Sean Christopherson
Quoted 34 lines · Sean Christopherson
Quoted 33 lines · Kai Huang
Quoted 31 lines · Peter Zijlstra
Quoted 15 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.� Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.� __seamcall_saved_ret() uses struct tdx_module_arg as input and output.� For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].� It's silly to move those values twice.� From kvm_vcpu_arch::regs to tdx_module_args.� From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?� Maybe I can make the change with TDX KVM patch series.

Quoted 5 lines

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

I suspect the kvm_vcpu_arch::regs layout is given by hardware; so the only option would be to make tdx_module_args match that. It's a slightly unfortunate layout, but meh.

Then you can simply do:

	__seamcall_saved_ret(leaf, (struct tdx_module_args *)vcpu->arch->regs);
Quoted 8 lines
Quoted 4 lines

I don't think the layout matches hardware, especially I think there's no "hardware layout" for GPRs that are concerned here. They are just for KVM itself to save guest's registers when the guest exits to KVM, so that KVM can restore them when returning back to the guest.

kvm_vcpu_arch::regs does follow the hardware-defined indices, and that's required for myriad emulation flows, e.g. saving GPRs into SMRAM, anywhere KVM gets a GPR index from an instruction opcode or vmcs.VMX_INSTRUCTION_INFO, etc.

Yes. Sorry I missed this. I forgot x86 uses "index register" and does have a "hardware layout".

	IndexReg:
	0 = RAX
	1 = RCX
	2 = RDX
	3 = RBX
	4 = RSP
	5 = RBP
	6 = RSI
	7 = RDI
	8–15 represent R8–R15, respectively...
author
#39 lore raw

Re: [PATCH 08/10] x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

In reply to Nikolay Borisov
Quoted 13 lines

arch/x86/coco/tdx/tdx.c line 59

Quoted 7 lines
arch/x86/coco/tdx/tdx.c +6 -0
+/* Called from __tdx_hypercall() for unrecoverable failure */
+static noinstr void __tdx_hypercall_failed(void)
+{
+	instrumentation_begin();
+	panic("TDVMCALL failed. TDX module bug?");
+}

So what's the deal with this instrumentation here. The instruction is noinstr, so you want to make just the panic call itself instrumentable?, if so where's the instrumentation_end() cal;?No instrumentation_end() call. Actually is this complexity really worth it for the failure case?

Quoted 2 lines

AFAICS there is a single call site for __tdx_hypercall_failed so why noot call panic() directly ?

W/o this patch, the __tdx_hypercall_failed() is called from the TDX_HYPERCALL assembly, which is in .noinstr.text, and 'instrumentation_begin()' was needed to avoid the build warning I suppose.

However now with this patch __tdx_hypercall_failed() is called from __tdx_hypercall() which is a C function w/o 'noinstr' annotation, thus I believe instrumentation_begin() and 'noinstr' annotation are not needed anymore.

I didn't notice this while moving this function around and my kernel build test didn't warn me about this. I'll change in next version.

In fact, perhaps this patch perhaps is too big for review. I will also try to split it to smaller ones.

Quoted 14 lines

arch/x86/coco/tdx/tdx.c line 69

Quoted 11 lines
arch/x86/coco/tdx/tdx.c +10 -0
+
+static inline u64 __tdx_hypercall(struct tdx_module_args *args)
+{
+	u64 ret;
+
+	args->rcx = TDVMCALL_EXPOSE_REGS_MASK;
+	ret = __tdcall_saved_ret(TDG_VP_VMCALL, args);
+
+	/*
+	 * RAX!=0 indicates a failure of the TDVMCALL mechanism itself and that

nit: Why mention the register explicitly, just say that if __tdcall_saved_ret returns non-zero ....

OK will do. I basically moved the comment from assembly to here.

#40 lore raw

Re: [PATCH 08/10] x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

Quoted 24 lines · Kai
Quoted 13 lines

arch/x86/coco/tdx/tdx.c line 59

Quoted 7 lines
arch/x86/coco/tdx/tdx.c +6 -0
+/* Called from __tdx_hypercall() for unrecoverable failure */
+static noinstr void __tdx_hypercall_failed(void)
+{
+	instrumentation_begin();
+	panic("TDVMCALL failed. TDX module bug?");
+}

So what's the deal with this instrumentation here. The instruction is noinstr, so you want to make just the panic call itself instrumentable?, if so where's the instrumentation_end() cal;?No instrumentation_end() call. Actually is this complexity really worth it for the failure case?

Quoted 2 lines

AFAICS there is a single call site for __tdx_hypercall_failed so why noot call panic() directly ?

W/o this patch, the __tdx_hypercall_failed() is called from the TDX_HYPERCALL assembly, which is in .noinstr.text, and 'instrumentation_begin()' was needed to avoid the build warning I suppose.

However now with this patch __tdx_hypercall_failed() is called from __tdx_hypercall() which is a C function w/o 'noinstr' annotation, thus I believe instrumentation_begin() and 'noinstr' annotation are not needed anymore.

Quoted 5 lines

I didn't notice this while moving this function around and my kernel build test didn't warn me about this. I'll change in next version.

In fact, perhaps this patch perhaps is too big for review. I will also try to split it to smaller ones.

Can't you simply call panic() directly? Less going around the code while someone is reading it?

<snip>

author
#41 lore raw

RE: [PATCH 08/10] x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

Quoted 25 lines · Kai
Quoted 10 lines
Quoted 10 lines

arch/x86/coco/tdx/tdx.c

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +4 -0
+/* Called from __tdx_hypercall() for unrecoverable failure */
+static noinstr void __tdx_hypercall_failed(void) {
+	instrumentation_begin();
+	panic("TDVMCALL failed. TDX module bug?"); }

So what's the deal with this instrumentation here. The instruction is noinstr, so you want to make just the panic call itself instrumentable?, if so where's the instrumentation_end() cal;?No instrumentation_end() call. Actually is this complexity really worth it for the

failure case?

Quoted 13 lines
Quoted 3 lines

AFAICS there is a single call site for __tdx_hypercall_failed so why noot call panic() directly ?

W/o this patch, the __tdx_hypercall_failed() is called from the TDX_HYPERCALL assembly, which is in .noinstr.text, and 'instrumentation_begin()' was needed to avoid the build warning I suppose.

However now with this patch __tdx_hypercall_failed() is called from __tdx_hypercall() which is a C function w/o 'noinstr' annotation, thus I believe
instrumentation_begin() and 'noinstr' annotation are not needed anymore.

Quoted 8 lines
Quoted 5 lines

I didn't notice this while moving this function around and my kernel build test didn't warn me about this. I'll change in next version.

In fact, perhaps this patch perhaps is too big for review. I will also try to split it to smaller ones.

Can't you simply call panic() directly? Less going around the code while someone is reading it?

I can and will do.

author
#42 lore raw

Re: [PATCH 08/10] x86/tdx: Unify TDX_HYPERCALL and TDX_MODULE_CALL assembly

Quoted 28 lines · Kai
Quoted 28 lines · Kai
Quoted 10 lines
Quoted 10 lines

arch/x86/coco/tdx/tdx.c

Quoted 5 lines
arch/x86/coco/tdx/tdx.c +4 -0
+/* Called from __tdx_hypercall() for unrecoverable failure */
+static noinstr void __tdx_hypercall_failed(void) {
+	instrumentation_begin();
+	panic("TDVMCALL failed. TDX module bug?"); }

So what's the deal with this instrumentation here. The instruction is noinstr, so you want to make just the panic call itself instrumentable?, if so where's the instrumentation_end() cal;?No instrumentation_end() call. Actually is this complexity really worth it for the

failure case?

Quoted 16 lines
Quoted 3 lines

AFAICS there is a single call site for __tdx_hypercall_failed so why noot call panic() directly ?

W/o this patch, the __tdx_hypercall_failed() is called from the TDX_HYPERCALL assembly, which is in .noinstr.text, and 'instrumentation_begin()' was needed to avoid the build warning I suppose.

However now with this patch __tdx_hypercall_failed() is called from __tdx_hypercall() which is a C function w/o 'noinstr' annotation, thus I believe
instrumentation_begin() and 'noinstr' annotation are not needed anymore.

I didn't notice this while moving this function around and my kernel build test didn't warn me about this. I'll change in next version.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

In fact, perhaps this patch perhaps is too big for review. I will also try to split it to smaller ones.

Can't you simply call panic() directly? Less going around the code while someone is reading it?

I can and will do.

After rebasing to the latest TDX code, I found we should keep the __tdx_hypercall_failed(). The reason is both the core-kernel (vmlinux) and the compressed code need the __tdx_hypercall() implementation. Implementing the __tdx_hypercall_failed() in both core-kernel and compressed code separately allows the __tdx_hypercall() to be shared by both code, otherwise both of them need to implement their own __tdx_hypercall().

Note __tdx_hypercall_failed() in the vmlinux calls panic(), but the one in the compressed code calls error().

#53 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

In reply to Isaku Yamahata
Quoted 36 lines · Isaku Yamahata
Quoted 33 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

This was one concern that I mentioned VP.ENTER can be implemented by KVM in its own assembly in the TDX host v12 discussion. I kinda agree we should leverage KVM's existing kvm_vcpu_arch::regs[NR_CPU_REGS] infrastructure to minimize the code change to the KVM's common infrastructure. If so, I guess we have to carry this memory copy burden between two structures.

Btw, I do find KVM's VP.ENTER code is a little bit redundant to the common SEAMCALL assembly, which is a good reason for KVM to use __seamcall() variants for TDH.VP.ENTER.

So it's a tradeoff I think.

Quoted 7 lines
Quoted 4 lines

On the other hand, given CoCo VMs normally don't expose all GPRs to VMM, it's also debatable whether we should invent another infrastructure to the KVM code to handle register access of CoCo VMs too, e.g., we can catch bugs easily when KVM tries to access the registers that it shouldn't access.

Yes, we'd like to save/restore GPRs only for TDVMCALL. Otherwise skip save/restore.

And another case to save/restore GPRs: supports DEBUG TD, which is type of TD guest allows VMM to change its register context, for debugging purpose.

Quoted 3 lines

--
Isaku Yamahata <[email protected]>

#54 lore raw

Re: [PATCH 09/10] x86/virt/tdx: Wire up basic SEAMCALL functions

Quoted 42 lines · Yuan Yao
Quoted 41 lines · Isaku Yamahata
Quoted 37 lines · Kai
Quoted 5 lines · Isaku Yamahata
Quoted 4 lines

The SEAMCALL ABI is very similar to the TDCALL ABI and leverages much TDCALL infrastructure.  Wire up basic functions to make SEAMCALLs for the basic TDX support: __seamcall(), __seamcall_ret() and __seamcall_saved_ret() which is for TDH.VP.ENTER leaf function.

Quoted 8 lines

Hi.  __seamcall_saved_ret() uses struct tdx_module_arg as input and output.  For KVM TDH.VP.ENTER case, those arguments are already in unsigned long kvm_vcpu_arch::regs[].  It's silly to move those values twice.  From kvm_vcpu_arch::regs to tdx_module_args.  From tdx_module_args to real registers.

If TDH.VP.ENTER is the only user of __seamcall_saved_ret(), can we make it to take unsigned long kvm_vcpu_argh::regs[NR_VCPU_REGS]?  Maybe I can make the change with TDX KVM patch series.

The assembly code assumes the second argument is a pointer to 'struct tdx_module_args'. I don't know how can we change __seamcall_saved_ret() to achieve what you said. We might change the kvm_vcpu_argh::regs[NR_VCPU_REGS] to match 'struct tdx_module_args''s layout and manually convert part of "regs" to the structure and pass to __seamcall_saved_ret(), but it's too hacky I suppose.

This was one concern that I mentioned VP.ENTER can be implemented by KVM in its own assembly in the TDX host v12 discussion. I kinda agree we should leverage KVM's existing kvm_vcpu_arch::regs[NR_CPU_REGS] infrastructure to minimize the code change to the KVM's common infrastructure. If so, I guess we have to carry this memory copy burden between two structures.

Btw, I do find KVM's VP.ENTER code is a little bit redundant to the common SEAMCALL assembly, which is a good reason for KVM to use __seamcall() variants for TDH.VP.ENTER.

So it's a tradeoff I think.

On the other hand, given CoCo VMs normally don't expose all GPRs to VMM, it's also debatable whether we should invent another infrastructure to the KVM code to handle register access of CoCo VMs too, e.g., we can catch bugs easily when KVM tries to access the registers that it shouldn't access.

Quoted 6 lines
Quoted 2 lines

Yes, we'd like to save/restore GPRs only for TDVMCALL. Otherwise skip save/restore.

And another case to save/restore GPRs: supports DEBUG TD, which is type of TD guest allows VMM to change its register context, for debugging purpose.

For Debug TD case, we can use TDH.VP.RD(general purpose register) specifically. We don't need to optimize for Debug TD case.

Signature · 1 line
Isaku Yamahata <[email protected]>