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 #include <asm/shared/tdx_errno.h>
8
9 #define TDX_HYPERCALL_STANDARD 0
10
11 #define TDX_CPUID_LEAF_ID 0x21
12 #define TDX_IDENT "IntelTDX "
13
14 /* TDX module Call Leaf IDs */
15 #define TDG_VP_VMCALL 0
16 #define TDG_VP_INFO 1
17 #define TDG_MR_RTMR_EXTEND 2
18 #define TDG_VP_VEINFO_GET 3
19 #define TDG_MR_REPORT 4
20 #define TDG_MEM_PAGE_ACCEPT 6
21 #define TDG_VM_RD 7
22 #define TDG_VM_WR 8
23
24 /* TDX TD attributes */
25 #define TDX_TD_ATTR_DEBUG_BIT 0
26 #define TDX_TD_ATTR_DEBUG BIT_ULL(TDX_TD_ATTR_DEBUG_BIT)
27 #define TDX_TD_ATTR_HGS_PLUS_PROF_BIT 4
28 #define TDX_TD_ATTR_HGS_PLUS_PROF BIT_ULL(TDX_TD_ATTR_HGS_PLUS_PROF_BIT)
29 #define TDX_TD_ATTR_PERF_PROF_BIT 5
30 #define TDX_TD_ATTR_PERF_PROF BIT_ULL(TDX_TD_ATTR_PERF_PROF_BIT)
31 #define TDX_TD_ATTR_PMT_PROF_BIT 6
32 #define TDX_TD_ATTR_PMT_PROF BIT_ULL(TDX_TD_ATTR_PMT_PROF_BIT)
33 #define TDX_TD_ATTR_ICSSD_BIT 16
34 #define TDX_TD_ATTR_ICSSD BIT_ULL(TDX_TD_ATTR_ICSSD_BIT)
35 #define TDX_TD_ATTR_LASS_BIT 27
36 #define TDX_TD_ATTR_LASS BIT_ULL(TDX_TD_ATTR_LASS_BIT)
37 #define TDX_TD_ATTR_SEPT_VE_DISABLE_BIT 28
38 #define TDX_TD_ATTR_SEPT_VE_DISABLE BIT_ULL(TDX_TD_ATTR_SEPT_VE_DISABLE_BIT)
39 #define TDX_TD_ATTR_MIGRATABLE_BIT 29
40 #define TDX_TD_ATTR_MIGRATABLE BIT_ULL(TDX_TD_ATTR_MIGRATABLE_BIT)
41 #define TDX_TD_ATTR_PKS_BIT 30
42 #define TDX_TD_ATTR_PKS BIT_ULL(TDX_TD_ATTR_PKS_BIT)
43 #define TDX_TD_ATTR_KL_BIT 31
44 #define TDX_TD_ATTR_KL BIT_ULL(TDX_TD_ATTR_KL_BIT)
45 #define TDX_TD_ATTR_TPA_BIT 62
46 #define TDX_TD_ATTR_TPA BIT_ULL(TDX_TD_ATTR_TPA_BIT)
47 #define TDX_TD_ATTR_PERFMON_BIT 63
48 #define TDX_TD_ATTR_PERFMON BIT_ULL(TDX_TD_ATTR_PERFMON_BIT)
49
50 /* TDX TD-Scope Metadata. To be used by TDG.VM.WR and TDG.VM.RD */
51 #define TDCS_CONFIG_FLAGS 0x1110000300000016
52 #define TDCS_TD_CTLS 0x1110000300000017
53 #define TDCS_QUOTE_MAX_SIZE 0x9010000200000007
54 #define TDCS_NOTIFY_ENABLES 0x9100000000000010
55 #define TDCS_TOPOLOGY_ENUM_CONFIGURED 0x9100000000000019
56
57 /* TDCS_CONFIG_FLAGS bits */
58 #define TDCS_CONFIG_FLEXIBLE_PENDING_VE BIT_ULL(1)
59
60 /* TDCS_TD_CTLS bits */
61 #define TD_CTLS_PENDING_VE_DISABLE_BIT 0
62 #define TD_CTLS_PENDING_VE_DISABLE BIT_ULL(TD_CTLS_PENDING_VE_DISABLE_BIT)
63 #define TD_CTLS_ENUM_TOPOLOGY_BIT 1
64 #define TD_CTLS_ENUM_TOPOLOGY BIT_ULL(TD_CTLS_ENUM_TOPOLOGY_BIT)
65 #define TD_CTLS_VIRT_CPUID2_BIT 2
66 #define TD_CTLS_VIRT_CPUID2 BIT_ULL(TD_CTLS_VIRT_CPUID2_BIT)
67 #define TD_CTLS_REDUCE_VE_BIT 3
68 #define TD_CTLS_REDUCE_VE BIT_ULL(TD_CTLS_REDUCE_VE_BIT)
69 #define TD_CTLS_LOCK_BIT 63
70 #define TD_CTLS_LOCK BIT_ULL(TD_CTLS_LOCK_BIT)
71
72 /* TDX hypercall Leaf IDs */
73 #define TDVMCALL_GET_TD_VM_CALL_INFO 0x10000
74 #define TDVMCALL_MAP_GPA 0x10001
75 #define TDVMCALL_GET_QUOTE 0x10002
76 #define TDVMCALL_REPORT_FATAL_ERROR 0x10003
77 #define TDVMCALL_SETUP_EVENT_NOTIFY_INTERRUPT 0x10004ULL
78
79 /*
80 * TDG.VP.VMCALL Status Codes (returned in R10)
81 */
82 #define TDVMCALL_STATUS_SUCCESS 0x0000000000000000ULL
83 #define TDVMCALL_STATUS_RETRY 0x0000000000000001ULL
84 #define TDVMCALL_STATUS_INVALID_OPERAND 0x8000000000000000ULL
85 #define TDVMCALL_STATUS_ALIGN_ERROR 0x8000000000000002ULL
86 #define TDVMCALL_STATUS_SUBFUNC_UNSUPPORTED 0x8000000000000003ULL
87
88 /*
89 * Bitmasks of exposed registers (with VMM).
90 */
91 #define TDX_RDX BIT(2)
92 #define TDX_RBX BIT(3)
93 #define TDX_RSI BIT(6)
94 #define TDX_RDI BIT(7)
95 #define TDX_R8 BIT(8)
96 #define TDX_R9 BIT(9)
97 #define TDX_R10 BIT(10)
98 #define TDX_R11 BIT(11)
99 #define TDX_R12 BIT(12)
100 #define TDX_R13 BIT(13)
101 #define TDX_R14 BIT(14)
102 #define TDX_R15 BIT(15)
103
104 /*
105 * These registers are clobbered to hold arguments for each
106 * TDVMCALL. They are safe to expose to the VMM.
107 * Each bit in this mask represents a register ID. Bit field
108 * details can be found in TDX GHCI specification, section
109 * titled "TDCALL [TDG.VP.VMCALL] leaf".
110 */
111 #define TDVMCALL_EXPOSE_REGS_MASK \
112 (TDX_RDX | TDX_RBX | TDX_RSI | TDX_RDI | TDX_R8 | TDX_R9 | \
113 TDX_R10 | TDX_R11 | TDX_R12 | TDX_R13 | TDX_R14 | TDX_R15)
114
115 /* TDX supported page sizes from the TDX module ABI. */
116 #define TDX_PS_4K 0
117 #define TDX_PS_2M 1
118 #define TDX_PS_1G 2
119 #define TDX_PS_NR (TDX_PS_1G + 1)
120
121 #ifndef __ASSEMBLER__
122
123 #include <linux/compiler_attributes.h>
124
125 /*
126 * Used in __tdcall*() to gather the input/output registers' values of the
127 * TDCALL instruction when requesting services from the TDX module. This is a
128 * software only structure and not part of the TDX module/VMM ABI
129 */
130 struct tdx_module_args {
131 /* callee-clobbered */
132 u64 rcx;
133 u64 rdx;
134 u64 r8;
135 u64 r9;
136 /* extra callee-clobbered */
137 u64 r10;
138 u64 r11;
139 /* callee-saved + rdi/rsi */
140 u64 r12;
141 u64 r13;
142 u64 r14;
143 u64 r15;
144 u64 rbx;
145 u64 rdi;
146 u64 rsi;
147 /*
148 * Leaf ABI version. Note that it gets encoded into RAX along with the
149 * leaf number.
150 */
151 u8 version;
152 };
153
154 /* Used to communicate with the TDX module */
155 u64 __tdcall(u64 fn, struct tdx_module_args *args);
156 u64 __tdcall_ret(u64 fn, struct tdx_module_args *args);
157 u64 __tdcall_saved_ret(u64 fn, struct tdx_module_args *args);
158
159 /* Used to request services from the VMM */
160 u64 __tdx_hypercall(struct tdx_module_args *args);
161
162 /*
163 * Wrapper for standard use of __tdx_hypercall with no output aside from
164 * return code.
165 */
_tdx_hypercall(u64 fn,u64 r12,u64 r13,u64 r14,u64 r15)166 static inline u64 _tdx_hypercall(u64 fn, u64 r12, u64 r13, u64 r14, u64 r15)
167 {
168 struct tdx_module_args args = {
169 .r10 = TDX_HYPERCALL_STANDARD,
170 .r11 = fn,
171 .r12 = r12,
172 .r13 = r13,
173 .r14 = r14,
174 .r15 = r15,
175 };
176
177 return __tdx_hypercall(&args);
178 }
179
180 void __noreturn tdx_panic(const char *msg);
181
182 /* Called from __tdx_hypercall() for unrecoverable failure */
183 void __noreturn __tdx_hypercall_failed(void);
184
185 bool tdx_accept_memory(phys_addr_t start, phys_addr_t end);
186
187 /*
188 * The TDG.VP.VMCALL-Instruction-execution sub-functions are defined
189 * independently from but are currently matched 1:1 with VMX EXIT_REASONs.
190 * Reusing the KVM EXIT_REASON macros makes it easier to connect the host and
191 * guest sides of these calls.
192 */
hcall_func(u64 exit_reason)193 static __always_inline u64 hcall_func(u64 exit_reason)
194 {
195 return exit_reason;
196 }
197
198 #endif /* !__ASSEMBLER__ */
199 #endif /* _ASM_X86_SHARED_TDX_H */
200