xref: /linux/arch/x86/include/asm/tdx.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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 
40 #ifndef __ASSEMBLER__
41 
42 #include <uapi/asm/mce.h>
43 #include <asm/tdx_global_metadata.h>
44 #include <linux/pgtable.h>
45 
46 /*
47  * TDX module and P-SEAMLDR version convention: "major.minor.update"
48  * (e.g., "1.5.08") with zero-padded two-digit update field.
49  */
50 #define TDX_VERSION_FMT "%u.%u.%02u"
51 
52 /*
53  * Used by the #VE exception handler to gather the #VE exception
54  * info from the TDX module. This is a software only structure
55  * and not part of the TDX module/VMM ABI.
56  */
57 struct ve_info {
58 	u64 exit_reason;
59 	u64 exit_qual;
60 	/* Guest Linear (virtual) Address */
61 	u64 gla;
62 	/* Guest Physical Address */
63 	u64 gpa;
64 	u32 instr_len;
65 	u32 instr_info;
66 };
67 
68 #ifdef CONFIG_INTEL_TDX_GUEST
69 
70 void __init tdx_early_init(void);
71 
72 void tdx_get_ve_info(struct ve_info *ve);
73 
74 bool tdx_handle_virt_exception(struct pt_regs *regs, struct ve_info *ve);
75 
76 void tdx_halt(void);
77 
78 bool tdx_early_handle_ve(struct pt_regs *regs);
79 
80 int tdx_mcall_get_report0(u8 *reportdata, u8 *tdreport);
81 
82 int tdx_mcall_extend_rtmr(u8 index, u8 *data);
83 
84 u64 tdx_hcall_get_quote(u8 *buf, size_t size);
85 
86 void __init tdx_dump_attributes(u64 td_attr);
87 void __init tdx_dump_td_ctls(u64 td_ctls);
88 
89 #else
90 
91 static inline void tdx_early_init(void) { };
92 static inline void tdx_halt(void) { };
93 
94 static inline bool tdx_early_handle_ve(struct pt_regs *regs) { return false; }
95 
96 #endif /* CONFIG_INTEL_TDX_GUEST */
97 
98 #if defined(CONFIG_KVM_GUEST) && defined(CONFIG_INTEL_TDX_GUEST)
99 long tdx_kvm_hypercall(unsigned int nr, unsigned long p1, unsigned long p2,
100 		       unsigned long p3, unsigned long p4);
101 #else
102 static inline long tdx_kvm_hypercall(unsigned int nr, unsigned long p1,
103 				     unsigned long p2, unsigned long p3,
104 				     unsigned long p4)
105 {
106 	return -ENODEV;
107 }
108 #endif /* CONFIG_INTEL_TDX_GUEST && CONFIG_KVM_GUEST */
109 
110 #ifdef CONFIG_INTEL_TDX_HOST
111 void tdx_init(void);
112 int tdx_cpu_enable(void);
113 const char *tdx_dump_mce_info(struct mce *m);
114 const struct tdx_sys_info *tdx_get_sysinfo(void);
115 
116 static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinfo)
117 {
118 	return sysinfo->features.tdx_features0 & TDX_FEATURES0_TD_PRESERVING;
119 }
120 
121 int tdx_guest_keyid_alloc(void);
122 u32 tdx_get_nr_guest_keyids(void);
123 void tdx_guest_keyid_free(unsigned int keyid);
124 
125 void tdx_quirk_reset_paddr(unsigned long base, unsigned long size);
126 
127 struct tdx_td {
128 	/* TD root structure: */
129 	struct page *tdr_page;
130 
131 	int tdcs_nr_pages;
132 	/* TD control structure: */
133 	struct page **tdcs_pages;
134 
135 	/* Size of `tdcx_pages` in struct tdx_vp */
136 	int tdcx_nr_pages;
137 };
138 
139 struct tdx_vp {
140 	/* TDVP root page */
141 	struct page *tdvpr_page;
142 	/* precalculated page_to_phys(tdvpr_page) for use in noinstr code */
143 	phys_addr_t tdvpr_pa;
144 
145 	/* TD vCPU control structure: */
146 	struct page **tdcx_pages;
147 };
148 
149 void tdx_sys_disable(void);
150 
151 u64 tdh_vp_enter(struct tdx_vp *vp, struct tdx_module_args *args);
152 u64 tdh_mng_addcx(struct tdx_td *td, struct page *tdcs_page);
153 u64 tdh_mem_page_add(struct tdx_td *td, u64 gpa, kvm_pfn_t pfn, struct page *source,
154 		     u64 *ext_err1, u64 *ext_err2);
155 u64 tdh_mem_sept_add(struct tdx_td *td, u64 gpa, enum pg_level level, struct page *page, u64 *ext_err1, u64 *ext_err2);
156 u64 tdh_vp_addcx(struct tdx_vp *vp, struct page *tdcx_page);
157 u64 tdh_mem_page_aug(struct tdx_td *td, u64 gpa, enum pg_level level, kvm_pfn_t pfn,
158 		     u64 *ext_err1, u64 *ext_err2);
159 u64 tdh_mem_range_block(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ext_err1, u64 *ext_err2);
160 u64 tdh_mng_key_config(struct tdx_td *td);
161 u64 tdh_mng_create(struct tdx_td *td, u16 hkid);
162 u64 tdh_vp_create(struct tdx_td *td, struct tdx_vp *vp);
163 u64 tdh_mng_rd(struct tdx_td *td, u64 field, u64 *data);
164 u64 tdh_mr_extend(struct tdx_td *td, u64 gpa, u64 *ext_err1, u64 *ext_err2);
165 u64 tdh_mr_finalize(struct tdx_td *td);
166 u64 tdh_vp_flush(struct tdx_vp *vp);
167 u64 tdh_mng_vpflushdone(struct tdx_td *td);
168 u64 tdh_mng_key_freeid(struct tdx_td *td);
169 u64 tdh_mng_init(struct tdx_td *td, u64 td_params, u64 *extended_err);
170 u64 tdh_vp_init(struct tdx_vp *vp, u64 initial_rcx, u32 x2apicid);
171 u64 tdh_vp_rd(struct tdx_vp *vp, u64 field, u64 *data);
172 u64 tdh_vp_wr(struct tdx_vp *vp, u64 field, u64 data, u64 mask);
173 u64 tdh_phymem_page_reclaim(struct page *page, u64 *tdx_pt, u64 *tdx_owner, u64 *tdx_size);
174 u64 tdh_mem_track(struct tdx_td *tdr);
175 u64 tdh_mem_page_remove(struct tdx_td *td, u64 gpa, enum pg_level level, u64 *ext_err1, u64 *ext_err2);
176 u64 tdh_phymem_cache_wb(bool resume);
177 u64 tdh_phymem_page_wbinvd_tdr(struct tdx_td *td);
178 u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn);
179 #else
180 static inline void tdx_init(void) { }
181 static inline u32 tdx_get_nr_guest_keyids(void) { return 0; }
182 static inline const char *tdx_dump_mce_info(struct mce *m) { return NULL; }
183 static inline const struct tdx_sys_info *tdx_get_sysinfo(void) { return NULL; }
184 static inline void tdx_sys_disable(void) { }
185 #endif	/* CONFIG_INTEL_TDX_HOST */
186 
187 #endif /* !__ASSEMBLER__ */
188 #endif /* _ASM_X86_TDX_H */
189