xref: /linux/arch/x86/include/asm/shared/tdx.h (revision 031fba65fc202abf1f193e321be7a2c274fd88ba)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_SHARED_TDX_H
3 #define _ASM_X86_SHARED_TDX_H
4 
5 #include <linux/bits.h>
6 #include <linux/types.h>
7 
8 #define TDX_HYPERCALL_STANDARD  0
9 
10 #define TDX_CPUID_LEAF_ID	0x21
11 #define TDX_IDENT		"IntelTDX    "
12 
13 /* TDX module Call Leaf IDs */
14 #define TDG_VP_VMCALL			0
15 #define TDG_VP_INFO			1
16 #define TDG_VP_VEINFO_GET		3
17 #define TDG_MR_REPORT			4
18 #define TDG_MEM_PAGE_ACCEPT		6
19 #define TDG_VM_WR			8
20 
21 /* TDCS fields. To be used by TDG.VM.WR and TDG.VM.RD module calls */
22 #define TDCS_NOTIFY_ENABLES		0x9100000000000010
23 
24 /* TDX hypercall Leaf IDs */
25 #define TDVMCALL_MAP_GPA		0x10001
26 #define TDVMCALL_REPORT_FATAL_ERROR	0x10003
27 
28 #define TDVMCALL_STATUS_RETRY		1
29 
30 /*
31  * Bitmasks of exposed registers (with VMM).
32  */
33 #define TDX_RDX		BIT(2)
34 #define TDX_RBX		BIT(3)
35 #define TDX_RSI		BIT(6)
36 #define TDX_RDI		BIT(7)
37 #define TDX_R8		BIT(8)
38 #define TDX_R9		BIT(9)
39 #define TDX_R10		BIT(10)
40 #define TDX_R11		BIT(11)
41 #define TDX_R12		BIT(12)
42 #define TDX_R13		BIT(13)
43 #define TDX_R14		BIT(14)
44 #define TDX_R15		BIT(15)
45 
46 /*
47  * These registers are clobbered to hold arguments for each
48  * TDVMCALL. They are safe to expose to the VMM.
49  * Each bit in this mask represents a register ID. Bit field
50  * details can be found in TDX GHCI specification, section
51  * titled "TDCALL [TDG.VP.VMCALL] leaf".
52  */
53 #define TDVMCALL_EXPOSE_REGS_MASK	\
54 	(TDX_RDX | TDX_RBX | TDX_RSI | TDX_RDI | TDX_R8  | TDX_R9  | \
55 	 TDX_R10 | TDX_R11 | TDX_R12 | TDX_R13 | TDX_R14 | TDX_R15)
56 
57 #ifndef __ASSEMBLY__
58 
59 #include <linux/compiler_attributes.h>
60 
61 /*
62  * Used in __tdcall*() to gather the input/output registers' values of the
63  * TDCALL instruction when requesting services from the TDX module. This is a
64  * software only structure and not part of the TDX module/VMM ABI
65  */
66 struct tdx_module_args {
67 	/* callee-clobbered */
68 	u64 rcx;
69 	u64 rdx;
70 	u64 r8;
71 	u64 r9;
72 	/* extra callee-clobbered */
73 	u64 r10;
74 	u64 r11;
75 	/* callee-saved + rdi/rsi */
76 	u64 r12;
77 	u64 r13;
78 	u64 r14;
79 	u64 r15;
80 	u64 rbx;
81 	u64 rdi;
82 	u64 rsi;
83 };
84 
85 /* Used to communicate with the TDX module */
86 u64 __tdcall(u64 fn, struct tdx_module_args *args);
87 u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
88 u64 __tdcall_saved_ret(u64 fn, struct tdx_module_args *args);
89 
90 /* Used to request services from the VMM */
91 u64 __tdx_hypercall(struct tdx_module_args *args);
92 
93 /*
94  * Wrapper for standard use of __tdx_hypercall with no output aside from
95  * return code.
96  */
97 static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
98 {
99 	struct tdx_module_args args = {
100 		.r10 = TDX_HYPERCALL_STANDARD,
101 		.r11 = fn,
102 		.r12 = r12,
103 		.r13 = r13,
104 		.r14 = r14,
105 		.r15 = r15,
106 	};
107 
108 	return __tdx_hypercall(&args);
109 }
110 
111 
112 /* Called from __tdx_hypercall() for unrecoverable failure */
113 void __noreturn __tdx_hypercall_failed(void);
114 
115 bool tdx_accept_memory(phys_addr_t start, phys_addr_t end);
116 
117 /*
118  * The TDG.VP.VMCALL-Instruction-execution sub-functions are defined
119  * independently from but are currently matched 1:1 with VMX EXIT_REASONs.
120  * Reusing the KVM EXIT_REASON macros makes it easier to connect the host and
121  * guest sides of these calls.
122  */
123 static __always_inline u64 hcall_func(u64 exit_reason)
124 {
125         return exit_reason;
126 }
127 
128 #endif /* !__ASSEMBLY__ */
129 #endif /* _ASM_X86_SHARED_TDX_H */
130