Skip to content

Commit 90f5ecd

Browse files
kaihuanghansendc
authored andcommitted
x86/tdx: Reimplement __tdx_hypercall() using TDX_MODULE_CALL asm
Now the TDX_HYPERCALL asm is basically identical to the TDX_MODULE_CALL with both '\saved' and '\ret' enabled, with two minor things though: 1) The way to restore the structure pointer is different The TDX_HYPERCALL uses RCX as spare to restore the structure pointer, but the TDX_MODULE_CALL assumes no spare register can be used. In other words, TDX_MODULE_CALL already covers what TDX_HYPERCALL does. 2) TDX_MODULE_CALL only clears shared registers for TDH.VP.ENTER For this just need to make that code available for the non-host case. Thus, remove the TDX_HYPERCALL and reimplement the __tdx_hypercall() using the TDX_MODULE_CALL. Extend the TDX_MODULE_CALL to cover "clear shared registers" for TDG.VP.VMCALL. Introduce a new __tdcall_saved_ret() to replace the temporary __tdcall_hypercall(). The __tdcall_saved_ret() can also be used for those new TDCALLs which require more input/output registers than the basic TDCALLs do. Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Kai Huang <kai.huang@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/all/e68a2473fb6f5bcd78b078cae7510e9d0753b3df.1692096753.git.kai.huang%40intel.com
1 parent c641cfb commit 90f5ecd

File tree

4 files changed

+15
-132
lines changed

4 files changed

+15
-132
lines changed

arch/x86/coco/tdx/tdcall.S

Lines changed: 8 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <asm/asm-offsets.h>
33
#include <asm/asm.h>
4-
#include <asm/frame.h>
54

65
#include <linux/linkage.h>
76
#include <linux/errno.h>
@@ -46,135 +45,19 @@ SYM_FUNC_START(__tdcall_ret)
4645
SYM_FUNC_END(__tdcall_ret)
4746

4847
/*
49-
* TDX_HYPERCALL - Make hypercalls to a TDX VMM using TDVMCALL leaf of TDCALL
50-
* instruction
51-
*
52-
* Transforms values in function call argument struct tdx_module_args @args
53-
* into the TDCALL register ABI. After TDCALL operation, VMM output is saved
54-
* back in @args, if \ret is 1.
55-
*
56-
* Depends on the caller to pass TDG.VP.VMCALL as the TDCALL leaf, and set
57-
* @args::rcx to TDVMCALL_EXPOSE_REGS_MASK.
58-
*
59-
*-------------------------------------------------------------------------
60-
* TD VMCALL ABI:
61-
*-------------------------------------------------------------------------
62-
*
63-
* Input Registers:
64-
*
65-
* RAX - TDCALL instruction leaf number (0 - TDG.VP.VMCALL)
66-
* RCX - BITMAP which controls which part of TD Guest GPR
67-
* is passed as-is to the VMM and back.
68-
* R10 - Set 0 to indicate TDCALL follows standard TDX ABI
69-
* specification. Non zero value indicates vendor
70-
* specific ABI.
71-
* R11 - VMCALL sub function number
72-
* RBX, RDX, RDI, RSI - Used to pass VMCALL sub function specific arguments.
73-
* R8-R9, R12-R15 - Same as above.
74-
*
75-
* Output Registers:
76-
*
77-
* RAX - TDCALL instruction status (Not related to hypercall
78-
* output).
79-
* RBX, RDX, RDI, RSI - Hypercall sub function specific output values.
80-
* R8-R15 - Same as above.
81-
*
82-
*/
83-
.macro TDX_HYPERCALL
84-
FRAME_BEGIN
85-
86-
/* Save callee-saved GPRs as mandated by the x86_64 ABI */
87-
push %r15
88-
push %r14
89-
push %r13
90-
push %r12
91-
push %rbx
92-
93-
/* Move Leaf ID to RAX */
94-
movq %rdi, %rax
95-
96-
/* Move bitmap of shared registers to RCX */
97-
movq TDX_MODULE_rcx(%rsi), %rcx
98-
99-
/* Copy hypercall registers from arg struct: */
100-
movq TDX_MODULE_r8(%rsi), %r8
101-
movq TDX_MODULE_r9(%rsi), %r9
102-
movq TDX_MODULE_r10(%rsi), %r10
103-
movq TDX_MODULE_r11(%rsi), %r11
104-
movq TDX_MODULE_r12(%rsi), %r12
105-
movq TDX_MODULE_r13(%rsi), %r13
106-
movq TDX_MODULE_r14(%rsi), %r14
107-
movq TDX_MODULE_r15(%rsi), %r15
108-
movq TDX_MODULE_rdi(%rsi), %rdi
109-
movq TDX_MODULE_rbx(%rsi), %rbx
110-
movq TDX_MODULE_rdx(%rsi), %rdx
111-
112-
pushq %rsi
113-
movq TDX_MODULE_rsi(%rsi), %rsi
114-
115-
tdcall
116-
117-
/*
118-
* Restore the pointer of the structure to save output registers.
119-
*
120-
* RCX is used as bitmap of shared registers and doesn't hold any
121-
* value provided by the VMM, thus it can be used as spare to
122-
* restore the structure pointer.
123-
*/
124-
popq %rcx
125-
movq %rsi, TDX_MODULE_rsi(%rcx)
126-
movq %rcx, %rsi
127-
128-
movq %r8, TDX_MODULE_r8(%rsi)
129-
movq %r9, TDX_MODULE_r9(%rsi)
130-
movq %r10, TDX_MODULE_r10(%rsi)
131-
movq %r11, TDX_MODULE_r11(%rsi)
132-
movq %r12, TDX_MODULE_r12(%rsi)
133-
movq %r13, TDX_MODULE_r13(%rsi)
134-
movq %r14, TDX_MODULE_r14(%rsi)
135-
movq %r15, TDX_MODULE_r15(%rsi)
136-
movq %rdi, TDX_MODULE_rdi(%rsi)
137-
movq %rbx, TDX_MODULE_rbx(%rsi)
138-
movq %rdx, TDX_MODULE_rdx(%rsi)
139-
140-
/*
141-
* Zero out registers exposed to the VMM to avoid speculative execution
142-
* with VMM-controlled values. This needs to include all registers
143-
* present in TDVMCALL_EXPOSE_REGS_MASK, except RBX, and R12-R15 which
144-
* will be restored.
145-
*/
146-
xor %r8d, %r8d
147-
xor %r9d, %r9d
148-
xor %r10d, %r10d
149-
xor %r11d, %r11d
150-
xor %rdi, %rdi
151-
xor %rsi, %rsi
152-
xor %rdx, %rdx
153-
154-
/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
155-
pop %rbx
156-
pop %r12
157-
pop %r13
158-
pop %r14
159-
pop %r15
160-
161-
FRAME_END
162-
163-
RET
164-
.endm
165-
166-
/*
48+
* __tdcall_saved_ret() - Used by TDX guests to request services from the
49+
* TDX module (including VMM services) using TDCALL instruction, with
50+
* saving output registers to the 'struct tdx_module_args' used as input.
16751
*
168-
* __tdcall_hypercall() function ABI:
52+
* __tdcall_saved_ret() function ABI:
16953
*
17054
* @fn (RDI) - TDCALL leaf ID, moved to RAX
17155
* @args (RSI) - struct tdx_module_args for input/output
17256
*
173-
* @fn and @args::rcx from the caller must be TDG_VP_VMCALL and
174-
* TDVMCALL_EXPOSE_REGS_MASK respectively.
57+
* All registers in @args are used as input/output registers.
17558
*
17659
* On successful completion, return the hypercall error code.
17760
*/
178-
SYM_FUNC_START(__tdcall_hypercall)
179-
TDX_HYPERCALL
180-
SYM_FUNC_END(__tdcall_hypercall)
61+
SYM_FUNC_START(__tdcall_saved_ret)
62+
TDX_MODULE_CALL host=0 ret=1 saved=1
63+
SYM_FUNC_END(__tdcall_saved_ret)

arch/x86/coco/tdx/tdx-shared.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ noinstr u64 __tdx_hypercall(struct tdx_hypercall_args *args)
8989
};
9090

9191
/*
92-
* Failure of __tdcall_hypercall() indicates a failure of the TDVMCALL
92+
* Failure of __tdcall_saved_ret() indicates a failure of the TDVMCALL
9393
* mechanism itself and that something has gone horribly wrong with
9494
* the TDX module. __tdx_hypercall_failed() never returns.
9595
*/
96-
if (__tdcall_hypercall(TDG_VP_VMCALL, &margs))
96+
if (__tdcall_saved_ret(TDG_VP_VMCALL, &margs))
9797
__tdx_hypercall_failed();
9898

9999
args->r8 = margs.r8;

arch/x86/include/asm/shared/tdx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct tdx_module_args {
8383
/* Used to communicate with the TDX module */
8484
u64 __tdcall(u64 fn, struct tdx_module_args *args);
8585
u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
86+
u64 __tdcall_saved_ret(u64 fn, struct tdx_module_args *args);
8687

8788
/*
8889
* Used in __tdx_hypercall() to pass down and get back registers' values of
@@ -106,7 +107,6 @@ struct tdx_hypercall_args {
106107
};
107108

108109
/* Used to request services from the VMM */
109-
u64 __tdcall_hypercall(u64 fn, struct tdx_module_args *args);
110110
u64 __tdx_hypercall(struct tdx_hypercall_args *args);
111111

112112
/*

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@
142142
movq %r11, TDX_MODULE_r11(%rsi)
143143
.endif /* \ret */
144144

145-
.if \host && \saved && \ret
145+
.if \saved && \ret
146146
/*
147-
* Clear registers shared by guest for VP.ENTER to prevent
148-
* speculative use of guest's values, including those are
147+
* Clear registers shared by guest for VP.VMCALL/VP.ENTER to prevent
148+
* speculative use of guest's/VMM's values, including those are
149149
* restored from the stack.
150150
*
151151
* See arch/x86/kvm/vmx/vmenter.S:
@@ -170,7 +170,7 @@
170170
xorl %r15d, %r15d
171171
xorl %ebx, %ebx
172172
xorl %edi, %edi
173-
.endif /* \host && \ret && \host */
173+
.endif /* \ret && \host */
174174

175175
.if \host
176176
.Lout\@:

0 commit comments

Comments
 (0)