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