xref: /openbmc/linux/arch/x86/coco/tdx/tdcall.S (revision 017cdefe)
1/* SPDX-License-Identifier: GPL-2.0 */
2#include <asm/asm-offsets.h>
3#include <asm/asm.h>
4#include <asm/frame.h>
5#include <asm/unwind_hints.h>
6
7#include <linux/linkage.h>
8#include <linux/bits.h>
9#include <linux/errno.h>
10
11#include "../../virt/vmx/tdx/tdxcall.S"
12
13/*
14 * Bitmasks of exposed registers (with VMM).
15 */
16#define TDX_RDX		BIT(2)
17#define TDX_RBX		BIT(3)
18#define TDX_RSI		BIT(6)
19#define TDX_RDI		BIT(7)
20#define TDX_R8		BIT(8)
21#define TDX_R9		BIT(9)
22#define TDX_R10		BIT(10)
23#define TDX_R11		BIT(11)
24#define TDX_R12		BIT(12)
25#define TDX_R13		BIT(13)
26#define TDX_R14		BIT(14)
27#define TDX_R15		BIT(15)
28
29/*
30 * These registers are clobbered to hold arguments for each
31 * TDVMCALL. They are safe to expose to the VMM.
32 * Each bit in this mask represents a register ID. Bit field
33 * details can be found in TDX GHCI specification, section
34 * titled "TDCALL [TDG.VP.VMCALL] leaf".
35 */
36#define TDVMCALL_EXPOSE_REGS_MASK	\
37	( TDX_RDX | TDX_RBX | TDX_RSI | TDX_RDI | TDX_R8  | TDX_R9  | \
38	  TDX_R10 | TDX_R11 | TDX_R12 | TDX_R13 | TDX_R14 | TDX_R15 )
39
40.section .noinstr.text, "ax"
41
42/*
43 * __tdx_module_call()  - Used by TDX guests to request services from
44 * the TDX module (does not include VMM services) using TDCALL instruction.
45 *
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 *
65 * __tdx_module_call() function ABI:
66 *
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.
77 *
78 * Return status of TDCALL via RAX.
79 */
80SYM_FUNC_START(__tdx_module_call)
81	FRAME_BEGIN
82	TDX_MODULE_CALL host=0
83	FRAME_END
84	RET
85SYM_FUNC_END(__tdx_module_call)
86
87/*
88 * __tdx_hypercall() - Make hypercalls to a TDX VMM using TDVMCALL leaf
89 * of TDCALL instruction
90 *
91 * Transforms values in  function call argument struct tdx_hypercall_args @args
92 * into the TDCALL register ABI. After TDCALL operation, VMM output is saved
93 * back in @args.
94 *
95 *-------------------------------------------------------------------------
96 * TD VMCALL ABI:
97 *-------------------------------------------------------------------------
98 *
99 * Input Registers:
100 *
101 * RAX                 - TDCALL instruction leaf number (0 - TDG.VP.VMCALL)
102 * RCX                 - BITMAP which controls which part of TD Guest GPR
103 *                       is passed as-is to the VMM and back.
104 * R10                 - Set 0 to indicate TDCALL follows standard TDX ABI
105 *                       specification. Non zero value indicates vendor
106 *                       specific ABI.
107 * R11                 - VMCALL sub function number
108 * RBX, RBP, RDI, RSI  - Used to pass VMCALL sub function specific arguments.
109 * R8-R9, R12-R15      - Same as above.
110 *
111 * Output Registers:
112 *
113 * RAX                 - TDCALL instruction status (Not related to hypercall
114 *                        output).
115 * R10                 - Hypercall output error code.
116 * R11-R15             - Hypercall sub function specific output values.
117 *
118 *-------------------------------------------------------------------------
119 *
120 * __tdx_hypercall() function ABI:
121 *
122 * @args  (RDI)        - struct tdx_hypercall_args for input and output
123 * @flags (RSI)        - TDX_HCALL_* flags
124 *
125 * On successful completion, return the hypercall error code.
126 */
127SYM_FUNC_START(__tdx_hypercall)
128	FRAME_BEGIN
129
130	/* Save callee-saved GPRs as mandated by the x86_64 ABI */
131	push %r15
132	push %r14
133	push %r13
134	push %r12
135	push %rbx
136
137	/* Free RDI and RSI to be used as TDVMCALL arguments */
138	movq %rdi, %rax
139	push %rsi
140
141	/* Copy hypercall registers from arg struct: */
142	movq TDX_HYPERCALL_r8(%rax),  %r8
143	movq TDX_HYPERCALL_r9(%rax),  %r9
144	movq TDX_HYPERCALL_r10(%rax), %r10
145	movq TDX_HYPERCALL_r11(%rax), %r11
146	movq TDX_HYPERCALL_r12(%rax), %r12
147	movq TDX_HYPERCALL_r13(%rax), %r13
148	movq TDX_HYPERCALL_r14(%rax), %r14
149	movq TDX_HYPERCALL_r15(%rax), %r15
150	movq TDX_HYPERCALL_rdi(%rax), %rdi
151	movq TDX_HYPERCALL_rsi(%rax), %rsi
152	movq TDX_HYPERCALL_rbx(%rax), %rbx
153	movq TDX_HYPERCALL_rdx(%rax), %rdx
154
155	push %rax
156
157	/* Mangle function call ABI into TDCALL ABI: */
158	/* Set TDCALL leaf ID (TDVMCALL (0)) in RAX */
159	xor %eax, %eax
160
161	movl $TDVMCALL_EXPOSE_REGS_MASK, %ecx
162
163	tdcall
164
165	/*
166	 * RAX!=0 indicates a failure of the TDVMCALL mechanism itself and that
167	 * something has gone horribly wrong with the TDX module.
168	 *
169	 * The return status of the hypercall operation is in a separate
170	 * register (in R10). Hypercall errors are a part of normal operation
171	 * and are handled by callers.
172	 */
173	testq %rax, %rax
174	jne .Lpanic
175
176	pop %rax
177
178	/* Copy hypercall result registers to arg struct if needed */
179	testq $TDX_HCALL_HAS_OUTPUT, (%rsp)
180	jz .Lout
181
182	movq %r8,  TDX_HYPERCALL_r8(%rax)
183	movq %r9,  TDX_HYPERCALL_r9(%rax)
184	movq %r10, TDX_HYPERCALL_r10(%rax)
185	movq %r11, TDX_HYPERCALL_r11(%rax)
186	movq %r12, TDX_HYPERCALL_r12(%rax)
187	movq %r13, TDX_HYPERCALL_r13(%rax)
188	movq %r14, TDX_HYPERCALL_r14(%rax)
189	movq %r15, TDX_HYPERCALL_r15(%rax)
190	movq %rdi, TDX_HYPERCALL_rdi(%rax)
191	movq %rsi, TDX_HYPERCALL_rsi(%rax)
192	movq %rbx, TDX_HYPERCALL_rbx(%rax)
193	movq %rdx, TDX_HYPERCALL_rdx(%rax)
194.Lout:
195	/* TDVMCALL leaf return code is in R10 */
196	movq %r10, %rax
197
198	/*
199	 * Zero out registers exposed to the VMM to avoid speculative execution
200	 * with VMM-controlled values. This needs to include all registers
201	 * present in TDVMCALL_EXPOSE_REGS_MASK, except RBX, and R12-R15 which
202	 * will be restored.
203	 */
204	xor %r8d,  %r8d
205	xor %r9d,  %r9d
206	xor %r10d, %r10d
207	xor %r11d, %r11d
208	xor %rdi,  %rdi
209	xor %rdx,  %rdx
210
211	/* Remove TDX_HCALL_* flags from the stack */
212	pop %rsi
213
214	/* Restore callee-saved GPRs as mandated by the x86_64 ABI */
215	pop %rbx
216	pop %r12
217	pop %r13
218	pop %r14
219	pop %r15
220
221	FRAME_END
222
223	RET
224.Lpanic:
225	call __tdx_hypercall_failed
226	/* __tdx_hypercall_failed never returns */
227	REACHABLE
228	jmp .Lpanic
229SYM_FUNC_END(__tdx_hypercall)
230