Skip to content

Commit 57a420b

Browse files
kaihuanghansendc
authored andcommitted
x86/tdx: Pass TDCALL/SEAMCALL input/output registers via a structure
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, R10/R11 are only used as output register but not as input by any TDCALL/SEAMCALL. Change to also use R10/R11 as input register to make input/output registers symmetric. 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' macro argument to indicate whether to save the 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. 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/483616c1762d85eb3a3c3035a7de061cfacf2f14.1692096753.git.kai.huang%40intel.com
1 parent 5efb962 commit 57a420b

File tree

6 files changed

+95
-117
lines changed

6 files changed

+95
-117
lines changed

arch/x86/coco/tdx/tdcall.S

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,33 @@
4343
* __tdcall() - Used by TDX guests to request services from the TDX
4444
* module (does not include VMM services) using TDCALL instruction.
4545
*
46-
* Transforms function call register arguments into the TDCALL register ABI.
47-
* After TDCALL operation, TDX module output is saved in @out (if it is
48-
* provided by the user).
49-
*
50-
*-------------------------------------------------------------------------
51-
* TDCALL ABI:
52-
*-------------------------------------------------------------------------
53-
* Input Registers:
54-
*
55-
* RAX - TDCALL Leaf number.
56-
* RCX,RDX,R8-R9 - TDCALL Leaf specific input registers.
57-
*
58-
* Output Registers:
59-
*
60-
* RAX - TDCALL instruction error code.
61-
* RCX,RDX,R8-R11 - TDCALL Leaf specific output registers.
62-
*
63-
*-------------------------------------------------------------------------
64-
*
6546
* __tdcall() function ABI:
6647
*
67-
* @fn (RDI) - TDCALL Leaf ID, moved to RAX
68-
* @rcx (RSI) - Input parameter 1, moved to RCX
69-
* @rdx (RDX) - Input parameter 2, moved to RDX
70-
* @r8 (RCX) - Input parameter 3, moved to R8
71-
* @r9 (R8) - Input parameter 4, moved to R9
72-
*
73-
* @out (R9) - struct tdx_module_output pointer
74-
* stored temporarily in R12 (not
75-
* shared with the TDX module). It
76-
* can be NULL.
48+
* @fn (RDI) - TDCALL Leaf ID, moved to RAX
49+
* @args (RSI) - struct tdx_module_args for input
7750
*
7851
* Return status of TDCALL via RAX.
7952
*/
8053
SYM_FUNC_START(__tdcall)
8154
TDX_MODULE_CALL host=0
8255
SYM_FUNC_END(__tdcall)
8356

57+
/*
58+
* __tdcall_ret() - Used by TDX guests to request services from the TDX
59+
* module (does not include VMM services) using TDCALL instruction, with
60+
* saving output registers to the 'struct tdx_module_args' used as input.
61+
*
62+
* __tdcall_ret() function ABI:
63+
*
64+
* @fn (RDI) - TDCALL Leaf ID, moved to RAX
65+
* @args (RSI) - struct tdx_module_args for input and output
66+
*
67+
* Return status of TDCALL via RAX.
68+
*/
69+
SYM_FUNC_START(__tdcall_ret)
70+
TDX_MODULE_CALL host=0 ret=1
71+
SYM_FUNC_END(__tdcall_ret)
72+
8473
/*
8574
* TDX_HYPERCALL - Make hypercalls to a TDX VMM using TDVMCALL leaf of TDCALL
8675
* instruction

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ static unsigned long try_accept_one(phys_addr_t start, unsigned long len,
55
enum pg_level pg_level)
66
{
77
unsigned long accept_size = page_level_size(pg_level);
8-
u64 tdcall_rcx;
8+
struct tdx_module_args args = {};
99
u8 page_size;
1010

1111
if (!IS_ALIGNED(start, accept_size))
@@ -34,8 +34,8 @@ static unsigned long try_accept_one(phys_addr_t start, unsigned long len,
3434
return 0;
3535
}
3636

37-
tdcall_rcx = start | page_size;
38-
if (__tdcall(TDG_MEM_PAGE_ACCEPT, tdcall_rcx, 0, 0, 0, NULL))
37+
args.rcx = start | page_size;
38+
if (__tdcall(TDG_MEM_PAGE_ACCEPT, &args))
3939
return 0;
4040

4141
return accept_size;

arch/x86/coco/tdx/tdx.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ EXPORT_SYMBOL_GPL(tdx_kvm_hypercall);
6666
* should only be used for calls that have no legitimate reason to fail
6767
* or where the kernel can not survive the call failing.
6868
*/
69-
static inline void tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
70-
struct tdx_module_output *out)
69+
static inline void tdcall(u64 fn, struct tdx_module_args *args)
7170
{
72-
if (__tdcall(fn, rcx, rdx, r8, r9, out))
71+
if (__tdcall_ret(fn, args))
7372
panic("TDCALL %lld failed (Buggy TDX module!)\n", fn);
7473
}
7574

@@ -89,11 +88,14 @@ static inline void tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
8988
*/
9089
int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport)
9190
{
91+
struct tdx_module_args args = {
92+
.rcx = virt_to_phys(tdreport),
93+
.rdx = virt_to_phys(reportdata),
94+
.r8 = TDREPORT_SUBTYPE_0,
95+
};
9296
u64 ret;
9397

94-
ret = __tdcall(TDG_MR_REPORT, virt_to_phys(tdreport),
95-
virt_to_phys(reportdata), TDREPORT_SUBTYPE_0,
96-
0, NULL);
98+
ret = __tdcall(TDG_MR_REPORT, &args);
9799
if (ret) {
98100
if (TDCALL_RETURN_CODE(ret) == TDCALL_INVALID_OPERAND)
99101
return -EINVAL;
@@ -141,7 +143,7 @@ static void __noreturn tdx_panic(const char *msg)
141143

142144
static void tdx_parse_tdinfo(u64 *cc_mask)
143145
{
144-
struct tdx_module_output out;
146+
struct tdx_module_args args = {};
145147
unsigned int gpa_width;
146148
u64 td_attr;
147149

@@ -152,7 +154,7 @@ static void tdx_parse_tdinfo(u64 *cc_mask)
152154
* Guest-Host-Communication Interface (GHCI), section 2.4.2 TDCALL
153155
* [TDG.VP.INFO].
154156
*/
155-
tdcall(TDG_VP_INFO, 0, 0, 0, 0, &out);
157+
tdcall(TDG_VP_INFO, &args);
156158

157159
/*
158160
* The highest bit of a guest physical address is the "sharing" bit.
@@ -161,15 +163,15 @@ static void tdx_parse_tdinfo(u64 *cc_mask)
161163
* The GPA width that comes out of this call is critical. TDX guests
162164
* can not meaningfully run without it.
163165
*/
164-
gpa_width = out.rcx & GENMASK(5, 0);
166+
gpa_width = args.rcx & GENMASK(5, 0);
165167
*cc_mask = BIT_ULL(gpa_width - 1);
166168

167169
/*
168170
* The kernel can not handle #VE's when accessing normal kernel
169171
* memory. Ensure that no #VE will be delivered for accesses to
170172
* TD-private memory. Only VMM-shared memory (MMIO) will #VE.
171173
*/
172-
td_attr = out.rdx;
174+
td_attr = args.rdx;
173175
if (!(td_attr & ATTR_SEPT_VE_DISABLE)) {
174176
const char *msg = "TD misconfiguration: SEPT_VE_DISABLE attribute must be set.";
175177

@@ -577,7 +579,7 @@ __init bool tdx_early_handle_ve(struct pt_regs *regs)
577579

578580
void tdx_get_ve_info(struct ve_info *ve)
579581
{
580-
struct tdx_module_output out;
582+
struct tdx_module_args args = {};
581583

582584
/*
583585
* Called during #VE handling to retrieve the #VE info from the
@@ -594,15 +596,15 @@ void tdx_get_ve_info(struct ve_info *ve)
594596
* Note, the TDX module treats virtual NMIs as inhibited if the #VE
595597
* valid flag is set. It means that NMI=>#VE will not result in a #DF.
596598
*/
597-
tdcall(TDG_VP_VEINFO_GET, 0, 0, 0, 0, &out);
599+
tdcall(TDG_VP_VEINFO_GET, &args);
598600

599601
/* Transfer the output parameters */
600-
ve->exit_reason = out.rcx;
601-
ve->exit_qual = out.rdx;
602-
ve->gla = out.r8;
603-
ve->gpa = out.r9;
604-
ve->instr_len = lower_32_bits(out.r10);
605-
ve->instr_info = upper_32_bits(out.r10);
602+
ve->exit_reason = args.rcx;
603+
ve->exit_qual = args.rdx;
604+
ve->gla = args.r8;
605+
ve->gpa = args.r9;
606+
ve->instr_len = lower_32_bits(args.r10);
607+
ve->instr_info = upper_32_bits(args.r10);
606608
}
607609

608610
/*
@@ -799,6 +801,10 @@ static bool tdx_enc_status_change_finish(unsigned long vaddr, int numpages,
799801

800802
void __init tdx_early_init(void)
801803
{
804+
struct tdx_module_args args = {
805+
.rdx = TDCS_NOTIFY_ENABLES,
806+
.r9 = -1ULL,
807+
};
802808
u64 cc_mask;
803809
u32 eax, sig[3];
804810

@@ -814,7 +820,7 @@ void __init tdx_early_init(void)
814820
cc_set_mask(cc_mask);
815821

816822
/* Kernel does not use NOTIFY_ENABLES and does not need random #VEs */
817-
tdcall(TDG_VM_WR, 0, TDCS_NOTIFY_ENABLES, 0, -1ULL, NULL);
823+
tdcall(TDG_VM_WR, &args);
818824

819825
/*
820826
* All bits above GPA width are reserved and kernel treats shared bit

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
7676
void __tdx_hypercall_failed(void);
7777

7878
/*
79-
* Used in __tdx_module_call() to gather the output registers' values of the
79+
* Used in __tdcall*() to gather the input/output registers' values of the
8080
* TDCALL instruction when requesting services from the TDX module. This is a
8181
* software only structure and not part of the TDX module/VMM ABI
8282
*/
83-
struct tdx_module_output {
83+
struct tdx_module_args {
8484
u64 rcx;
8585
u64 rdx;
8686
u64 r8;
@@ -90,8 +90,8 @@ struct tdx_module_output {
9090
};
9191

9292
/* Used to communicate with the TDX module */
93-
u64 __tdcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9,
94-
struct tdx_module_output *out);
93+
u64 __tdcall(u64 fn, struct tdx_module_args *args);
94+
u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
9595

9696
bool tdx_accept_memory(phys_addr_t start, phys_addr_t end);
9797

arch/x86/kernel/asm-offsets.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ static void __used common(void)
6868
#endif
6969

7070
BLANK();
71-
OFFSET(TDX_MODULE_rcx, tdx_module_output, rcx);
72-
OFFSET(TDX_MODULE_rdx, tdx_module_output, rdx);
73-
OFFSET(TDX_MODULE_r8, tdx_module_output, r8);
74-
OFFSET(TDX_MODULE_r9, tdx_module_output, r9);
75-
OFFSET(TDX_MODULE_r10, tdx_module_output, r10);
76-
OFFSET(TDX_MODULE_r11, tdx_module_output, r11);
71+
OFFSET(TDX_MODULE_rcx, tdx_module_args, rcx);
72+
OFFSET(TDX_MODULE_rdx, tdx_module_args, rdx);
73+
OFFSET(TDX_MODULE_r8, tdx_module_args, r8);
74+
OFFSET(TDX_MODULE_r9, tdx_module_args, r9);
75+
OFFSET(TDX_MODULE_r10, tdx_module_args, r10);
76+
OFFSET(TDX_MODULE_r11, tdx_module_args, r11);
7777

7878
BLANK();
7979
OFFSET(TDX_HYPERCALL_r8, tdx_hypercall_args, r8);

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

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,35 @@
1717
* TDX module and hypercalls to the VMM.
1818
* SEAMCALL - used by TDX hosts to make requests to the
1919
* TDX module.
20+
*
21+
*-------------------------------------------------------------------------
22+
* TDCALL/SEAMCALL ABI:
23+
*-------------------------------------------------------------------------
24+
* Input Registers:
25+
*
26+
* RAX - TDCALL/SEAMCALL Leaf number.
27+
* RCX,RDX,R8-R11 - TDCALL/SEAMCALL Leaf specific input registers.
28+
*
29+
* Output Registers:
30+
*
31+
* RAX - TDCALL/SEAMCALL instruction error code.
32+
* RCX,RDX,R8-R11 - TDCALL/SEAMCALL Leaf specific output registers.
33+
*
34+
*-------------------------------------------------------------------------
2035
*/
21-
.macro TDX_MODULE_CALL host:req
36+
.macro TDX_MODULE_CALL host:req ret=0
2237
FRAME_BEGIN
23-
/*
24-
* R12 will be used as temporary storage for struct tdx_module_output
25-
* pointer. Since R12-R15 registers are not used by TDCALL/SEAMCALL
26-
* services supported by this function, it can be reused.
27-
*/
28-
29-
/* Callee saved, so preserve it */
30-
push %r12
31-
32-
/*
33-
* Push output pointer to stack.
34-
* After the operation, it will be fetched into R12 register.
35-
*/
36-
push %r9
3738

38-
/* Mangle function call ABI into TDCALL/SEAMCALL ABI: */
3939
/* Move Leaf ID to RAX */
4040
mov %rdi, %rax
41-
/* Move input 4 to R9 */
42-
mov %r8, %r9
43-
/* Move input 3 to R8 */
44-
mov %rcx, %r8
45-
/* Move input 1 to RCX */
46-
mov %rsi, %rcx
47-
/* Leave input param 2 in RDX */
41+
42+
/* Move other input regs from 'struct tdx_module_args' */
43+
movq TDX_MODULE_rcx(%rsi), %rcx
44+
movq TDX_MODULE_rdx(%rsi), %rdx
45+
movq TDX_MODULE_r8(%rsi), %r8
46+
movq TDX_MODULE_r9(%rsi), %r9
47+
movq TDX_MODULE_r10(%rsi), %r10
48+
movq TDX_MODULE_r11(%rsi), %r11
4849

4950
.if \host
5051
seamcall
@@ -59,49 +60,31 @@
5960
* This value will never be used as actual SEAMCALL error code as
6061
* it is from the Reserved status code class.
6162
*/
62-
jc .Lseamcall_vmfailinvalid
63+
jc .Lseamcall_vmfailinvalid\@
6364
.else
6465
tdcall
6566
.endif
6667

67-
/*
68-
* Fetch output pointer from stack to R12 (It is used
69-
* as temporary storage)
70-
*/
71-
pop %r12
72-
73-
/*
74-
* Since this macro can be invoked with NULL as an output pointer,
75-
* check if caller provided an output struct before storing output
76-
* registers.
77-
*
78-
* Update output registers, even if the call failed (RAX != 0).
79-
* Other registers may contain details of the failure.
80-
*/
81-
test %r12, %r12
82-
jz .Lout
83-
84-
/* Copy result registers to output struct: */
85-
movq %rcx, TDX_MODULE_rcx(%r12)
86-
movq %rdx, TDX_MODULE_rdx(%r12)
87-
movq %r8, TDX_MODULE_r8(%r12)
88-
movq %r9, TDX_MODULE_r9(%r12)
89-
movq %r10, TDX_MODULE_r10(%r12)
90-
movq %r11, TDX_MODULE_r11(%r12)
91-
92-
.Lout:
93-
/* Restore the state of R12 register */
94-
pop %r12
68+
.if \ret
69+
/* Copy output registers to the structure */
70+
movq %rcx, TDX_MODULE_rcx(%rsi)
71+
movq %rdx, TDX_MODULE_rdx(%rsi)
72+
movq %r8, TDX_MODULE_r8(%rsi)
73+
movq %r9, TDX_MODULE_r9(%rsi)
74+
movq %r10, TDX_MODULE_r10(%rsi)
75+
movq %r11, TDX_MODULE_r11(%rsi)
76+
.endif
9577

78+
.if \host
79+
.Lout\@:
80+
.endif
9681
FRAME_END
9782
RET
9883

9984
.if \host
100-
.Lseamcall_vmfailinvalid:
85+
.Lseamcall_vmfailinvalid\@:
10186
mov $TDX_SEAMCALL_VMFAILINVALID, %rax
102-
/* pop the unused output pointer back to %r9 */
103-
pop %r9
104-
jmp .Lout
87+
jmp .Lout\@
10588
.endif /* \host */
10689

10790
.endm

0 commit comments

Comments
 (0)