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