xref: /linux/arch/x86/include/asm/tdx.h (revision bb29b957eaaa15be33243550d9ee576c2d601669)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2021-2022 Intel Corporation */
3 #ifndef _ASM_X86_TDX_H
4 #define _ASM_X86_TDX_H
5 
6 #include <linux/init.h>
7 #include <linux/bits.h>
8 #include <linux/mmzone.h>
9 #include <linux/kvm_types.h>
10 
11 #include <asm/errno.h>
12 #include <asm/ptrace.h>
13 #include <asm/trapnr.h>
14 #include <asm/shared/tdx.h>
15 
16 /*
17  * SW-defined error codes.
18  *
19  * Bits 47:40 == 0xFF indicate Reserved status code class that never used by
20  * TDX module.
21  */
22 #define TDX_ERROR			_BITUL(63)
23 #define TDX_NON_RECOVERABLE		_BITUL(62)
24 #define TDX_SW_ERROR			(TDX_ERROR | GENMASK_ULL(47, 40))
25 #define TDX_SEAMCALL_VMFAILINVALID	(TDX_SW_ERROR | _UL(0xFFFF0000))
26 
27 #define TDX_SEAMCALL_GP			(TDX_SW_ERROR | X86_TRAP_GP)
28 #define TDX_SEAMCALL_UD			(TDX_SW_ERROR | X86_TRAP_UD)
29 
30 /*
31  * TDX module SEAMCALL leaf function error codes
32  */
33 #define TDX_SUCCESS		0ULL
34 #define TDX_RND_NO_ENTROPY	0x8000020300000000ULL
35 
36 /* Bit definitions of TDX_FEATURES0 metadata field */
37 #define TDX_FEATURES0_TD_PRESERVING	BIT_ULL(1)
38 #define TDX_FEATURES0_NO_RBP_MOD	BIT_ULL(18)
39 #define TDX_FEATURES0_DYNAMIC_PAMT	BIT_ULL(36)
40 
41 #ifndef __ASSEMBLER__
42 
43 #include <uapi/asm/mce.h>
44 #include <asm/tdx_global_metadata.h>
45 #include <linux/pgtable.h>
46 
47 /*
48  * TDX module and P-SEAMLDR version convention: "major.minor.update"
49  * (e.g., "1.5.08") with zero-padded two-digit update field.
50  */
51 #define TDX_VERSION_FMT "%u.%u.%02u"
52 
53 /*
54  * Used by the #VE exception handler to gather the #VE exception
55  * info from the TDX module. This is a software only structure
56  * and not part of the TDX module/VMM ABI.
57  */
58 struct ve_info {
59 	u64 exit_reason;
60 	u64 exit_qual;
61 	/* Guest Linear (virtual) Address */
62 	u64 gla;
63 	/* Guest Physical Address */
64 	u64 gpa;
65 	u32 instr_len;
66 	u32 instr_info;
67 };
68 
69 #ifdef CONFIG_INTEL_TDX_GUEST
70 
71 void __init tdx_early_init(void);
72 
73 void tdx_get_ve_info(struct ve_info *ve);
74 
75 bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve);
76 
77 void tdx_halt(void);
78 
79 bool tdx_early_handle_ve(struct pt_regs *regs);
80 
81 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport);
82 
83 int tdx_mcall_extend_rtmr(u8 index, u8 *data);
84 
85 u64 tdx_hcall_get_quote(void *buf, size_t size);
86 
87 int tdx_get_max_quote_size(u64 *max_quote_size);
88 
89 void __init tdx_dump_attributes(u64 td_attr);
90 void __init tdx_dump_td_ctls(u64 td_ctls);
91 
92 #else
93 
94 static inline void tdx_early_init(void) { };
95 static inline void tdx_halt(void) { };
96 
97 static inline bool tdx_early_handle_ve(struct pt_regs *regs) { return false; }
98 
99 #endif /* CONFIG_INTEL_TDX_GUEST */
100 
101 #if defined(CONFIG_KVM_GUEST) && defined(CONFIG_INTEL_TDX_GUEST)
102 long tdx_kvm_hypercall(unsigned int nr, unsigned long p1, unsigned long p2,
103 		       unsigned long p3, unsigned long p4);
104 #else
105 static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,
106 				     unsigned long p2, unsigned long p3,
107 				     unsigned long p4)
108 {
109 	return -ENODEV;
110 }
111 #endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */
112 
113 #ifdef CONFIG_INTEL_TDX_HOST
114 void tdx_init(void);
115 int tdx_cpu_enable(void);
116 const char *tdx_dump_mce_info(struct mce *m);
117 const struct tdx_sys_info *tdx_get_sysinfo(void);
118 
119 static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinfo)
120 {
121 	return sysinfo->features.tdx_features0 & TDX_FEATURES0_TD_PRESERVING;
122 }
123 
124 bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo);
125 
126 /* Simple structure for pre-allocating DPAMT pages outside of spinlocks. */
127 struct tdx_pamt_cache {
128 	struct list_head page_list;
129 	int cnt;
130 };
131 
132 static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)
133 {
134 	INIT_LIST_HEAD(&cache->page_list);
135 	cache->cnt = 0;
136 }
137 
138 void tdx_free_pamt_cache(struct tdx_pamt_cache *cache);
139 int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);
140 int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);
141 void tdx_pamt_put(kvm_pfn_t pfn);
142 
143 int tdx_guest_keyid_alloc(void);
144 u32 tdx_get_nr_guest_keyids(void);
145 void tdx_guest_keyid_free(unsigned int keyid);
146 
147 void tdx_quirk_reset_paddr(unsigned long base, unsigned long size);
148 
149 struct page *tdx_alloc_control_page(void);
150 void tdx_free_control_page(struct page *page);
151 
152 struct tdx_td {
153 	/* TD root structure: */
154 	struct page *tdr_page;
155 
156 	int tdcs_nr_pages;
157 	/* TD control structure: */
158 	struct page **tdcs_pages;
159 
160 	/* Size of `tdcx_pages` in struct tdx_vp */
161 	int tdcx_nr_pages;
162 };
163 
164 struct tdx_vp {
165 	/* TDVP root page */
166 	struct page *tdvpr_page;
167 	/* precalculated page_to_phys(tdvpr_page) for use in noinstr code */
168 	phys_addr_t tdvpr_pa;
169 
170 	/* TD vCPU control structure: */
171 	struct page **tdcx_pages;
172 };
173 
174 void tdx_sys_disable(void);
175 
176 u64 tdh_vp_enter(struct tdx_vp *vp, struct tdx_module_args *args);
177 u64 tdh_mng_addcx(struct tdx_td *td, struct page *tdcs_page);
178 u64 tdh_mem_page_add(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *source,
179 		     u64 *ext_err1, u64 *ext_err2);
180 u64 tdh_mem_sept_add(struct tdx_td *td, u64 gpa, enum pg_level level, struct page *page, u64 *ext_err1, u64 *ext_err2);
181 u64 tdh_vp_addcx(struct tdx_vp *vp, struct page *tdcx_page);
182 u64 tdh_mem_page_aug(struct tdx_td *td, u64 gpa, enum pg_level level, kvm_pfn_t pfn,
183 		     u64 *ext_err1, u64 *ext_err2);
184 u64 tdh_mem_range_block(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ext_err1, u64 *ext_err2);
185 u64 tdh_mng_key_config(struct tdx_td *td);
186 u64 tdh_mng_create(struct tdx_td *td, u16 hkid);
187 u64 tdh_vp_create(struct tdx_td *td, struct tdx_vp *vp);
188 u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data);
189 u64 tdh_mr_extend(struct tdx_td *td, u64 gpa, u64 *ext_err1, u64 *ext_err2);
190 u64 tdh_mr_finalize(struct tdx_td *td);
191 u64 tdh_vp_flush(struct tdx_vp *vp);
192 u64 tdh_mng_vpflushdone(struct tdx_td *td);
193 u64 tdh_mng_key_freeid(struct tdx_td *td);
194 u64 tdh_mng_init(struct tdx_td *td, u64 td_params, u64 *extended_err);
195 u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid);
196 u64 tdh_vp_rd(struct tdx_vp *vp, u64 field, u64 *data);
197 u64 tdh_vp_wr(struct tdx_vp *vp, u64 field, u64 data, u64 mask);
198 u64 tdh_phymem_page_reclaim(struct page *page, u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size);
199 u64 tdh_mem_track(struct tdx_td *tdr);
200 u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ext_err1, u64 *ext_err2);
201 u64 tdh_phymem_cache_wb(bool resume);
202 u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
203 u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn);
204 #else
205 static inline void tdx_init(void) { }
206 static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
207 static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; }
208 static inline const struct tdx_sys_info *tdx_get_sysinfo(void) { return NULL; }
209 static inline void tdx_sys_disable(void) { }
210 #endif	/* CONFIG_INTEL_TDX_HOST */
211 
212 #endif /* !__ASSEMBLER__ */
213 #endif /* _ASM_X86_TDX_H */
214