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