1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * AMD Encrypted Register State Support 4 * 5 * Author: Joerg Roedel <jroedel@suse.de> 6 */ 7 8 #ifndef __ASM_ENCRYPTED_STATE_H 9 #define __ASM_ENCRYPTED_STATE_H 10 11 #include <linux/types.h> 12 #include <linux/sev-guest.h> 13 14 #include <asm/insn.h> 15 #include <asm/sev-common.h> 16 #include <asm/bootparam.h> 17 18 #define GHCB_PROTOCOL_MIN 1ULL 19 #define GHCB_PROTOCOL_MAX 2ULL 20 #define GHCB_DEFAULT_USAGE 0ULL 21 22 #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); } 23 24 enum es_result { 25 ES_OK, /* All good */ 26 ES_UNSUPPORTED, /* Requested operation not supported */ 27 ES_VMM_ERROR, /* Unexpected state from the VMM */ 28 ES_DECODE_FAILED, /* Instruction decoding failed */ 29 ES_EXCEPTION, /* Instruction caused exception */ 30 ES_RETRY, /* Retry instruction emulation */ 31 }; 32 33 struct es_fault_info { 34 unsigned long vector; 35 unsigned long error_code; 36 unsigned long cr2; 37 }; 38 39 struct pt_regs; 40 41 /* ES instruction emulation context */ 42 struct es_em_ctxt { 43 struct pt_regs *regs; 44 struct insn insn; 45 struct es_fault_info fi; 46 }; 47 48 /* 49 * AMD SEV Confidential computing blob structure. The structure is 50 * defined in OVMF UEFI firmware header: 51 * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h 52 */ 53 #define CC_BLOB_SEV_HDR_MAGIC 0x45444d41 54 struct cc_blob_sev_info { 55 u32 magic; 56 u16 version; 57 u16 reserved; 58 u64 secrets_phys; 59 u32 secrets_len; 60 u32 rsvd1; 61 u64 cpuid_phys; 62 u32 cpuid_len; 63 u32 rsvd2; 64 } __packed; 65 66 void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code); 67 68 static inline u64 lower_bits(u64 val, unsigned int bits) 69 { 70 u64 mask = (1ULL << bits) - 1; 71 72 return (val & mask); 73 } 74 75 struct real_mode_header; 76 enum stack_type; 77 78 /* Early IDT entry points for #VC handler */ 79 extern void vc_no_ghcb(void); 80 extern void vc_boot_ghcb(void); 81 extern bool handle_vc_boot_ghcb(struct pt_regs *regs); 82 83 /* PVALIDATE return codes */ 84 #define PVALIDATE_FAIL_SIZEMISMATCH 6 85 86 /* Software defined (when rFlags.CF = 1) */ 87 #define PVALIDATE_FAIL_NOUPDATE 255 88 89 /* RMP page size */ 90 #define RMP_PG_SIZE_4K 0 91 #define RMP_PG_SIZE_2M 1 92 93 #define RMPADJUST_VMSA_PAGE_BIT BIT(16) 94 95 /* SNP Guest message request */ 96 struct snp_req_data { 97 unsigned long req_gpa; 98 unsigned long resp_gpa; 99 unsigned long data_gpa; 100 unsigned int data_npages; 101 }; 102 103 struct sev_guest_platform_data { 104 u64 secrets_gpa; 105 }; 106 107 /* 108 * The secrets page contains 96-bytes of reserved field that can be used by 109 * the guest OS. The guest OS uses the area to save the message sequence 110 * number for each VMPCK. 111 * 112 * See the GHCB spec section Secret page layout for the format for this area. 113 */ 114 struct secrets_os_area { 115 u32 msg_seqno_0; 116 u32 msg_seqno_1; 117 u32 msg_seqno_2; 118 u32 msg_seqno_3; 119 u64 ap_jump_table_pa; 120 u8 rsvd[40]; 121 u8 guest_usage[32]; 122 } __packed; 123 124 #define VMPCK_KEY_LEN 32 125 126 /* See the SNP spec version 0.9 for secrets page format */ 127 struct snp_secrets_page_layout { 128 u32 version; 129 u32 imien : 1, 130 rsvd1 : 31; 131 u32 fms; 132 u32 rsvd2; 133 u8 gosvw[16]; 134 u8 vmpck0[VMPCK_KEY_LEN]; 135 u8 vmpck1[VMPCK_KEY_LEN]; 136 u8 vmpck2[VMPCK_KEY_LEN]; 137 u8 vmpck3[VMPCK_KEY_LEN]; 138 struct secrets_os_area os_area; 139 u8 rsvd3[3840]; 140 } __packed; 141 142 #ifdef CONFIG_AMD_MEM_ENCRYPT 143 extern struct static_key_false sev_es_enable_key; 144 extern void __sev_es_ist_enter(struct pt_regs *regs); 145 extern void __sev_es_ist_exit(void); 146 static __always_inline void sev_es_ist_enter(struct pt_regs *regs) 147 { 148 if (static_branch_unlikely(&sev_es_enable_key)) 149 __sev_es_ist_enter(regs); 150 } 151 static __always_inline void sev_es_ist_exit(void) 152 { 153 if (static_branch_unlikely(&sev_es_enable_key)) 154 __sev_es_ist_exit(); 155 } 156 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh); 157 extern void __sev_es_nmi_complete(void); 158 static __always_inline void sev_es_nmi_complete(void) 159 { 160 if (static_branch_unlikely(&sev_es_enable_key)) 161 __sev_es_nmi_complete(); 162 } 163 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); 164 165 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) 166 { 167 int rc; 168 169 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */ 170 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t" 171 : "=a"(rc) 172 : "a"(vaddr), "c"(rmp_psize), "d"(attrs) 173 : "memory", "cc"); 174 175 return rc; 176 } 177 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) 178 { 179 bool no_rmpupdate; 180 int rc; 181 182 /* "pvalidate" mnemonic support in binutils 2.36 and newer */ 183 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t" 184 CC_SET(c) 185 : CC_OUT(c) (no_rmpupdate), "=a"(rc) 186 : "a"(vaddr), "c"(rmp_psize), "d"(validate) 187 : "memory", "cc"); 188 189 if (no_rmpupdate) 190 return PVALIDATE_FAIL_NOUPDATE; 191 192 return rc; 193 } 194 195 struct snp_guest_request_ioctl; 196 197 void setup_ghcb(void); 198 void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, 199 unsigned long npages); 200 void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, 201 unsigned long npages); 202 void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); 203 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages); 204 void snp_set_memory_private(unsigned long vaddr, unsigned long npages); 205 void snp_set_wakeup_secondary_cpu(void); 206 bool snp_init(struct boot_params *bp); 207 void __init __noreturn snp_abort(void); 208 int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio); 209 void snp_accept_memory(phys_addr_t start, phys_addr_t end); 210 #else 211 static inline void sev_es_ist_enter(struct pt_regs *regs) { } 212 static inline void sev_es_ist_exit(void) { } 213 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } 214 static inline void sev_es_nmi_complete(void) { } 215 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } 216 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } 217 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } 218 static inline void setup_ghcb(void) { } 219 static inline void __init 220 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } 221 static inline void __init 222 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { } 223 static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } 224 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { } 225 static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { } 226 static inline void snp_set_wakeup_secondary_cpu(void) { } 227 static inline bool snp_init(struct boot_params *bp) { return false; } 228 static inline void snp_abort(void) { } 229 static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio) 230 { 231 return -ENOTTY; 232 } 233 234 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { } 235 #endif 236 237 #endif 238