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 76 /* Early IDT entry points for #VC handler */ 77 extern void vc_no_ghcb(void); 78 extern void vc_boot_ghcb(void); 79 extern bool handle_vc_boot_ghcb(struct pt_regs *regs); 80 81 /* Software defined (when rFlags.CF = 1) */ 82 #define PVALIDATE_FAIL_NOUPDATE 255 83 84 /* RMP page size */ 85 #define RMP_PG_SIZE_4K 0 86 87 #define RMPADJUST_VMSA_PAGE_BIT BIT(16) 88 89 /* SNP Guest message request */ 90 struct snp_req_data { 91 unsigned long req_gpa; 92 unsigned long resp_gpa; 93 unsigned long data_gpa; 94 unsigned int data_npages; 95 }; 96 97 struct sev_guest_platform_data { 98 u64 secrets_gpa; 99 }; 100 101 /* 102 * The secrets page contains 96-bytes of reserved field that can be used by 103 * the guest OS. The guest OS uses the area to save the message sequence 104 * number for each VMPCK. 105 * 106 * See the GHCB spec section Secret page layout for the format for this area. 107 */ 108 struct secrets_os_area { 109 u32 msg_seqno_0; 110 u32 msg_seqno_1; 111 u32 msg_seqno_2; 112 u32 msg_seqno_3; 113 u64 ap_jump_table_pa; 114 u8 rsvd[40]; 115 u8 guest_usage[32]; 116 } __packed; 117 118 #define VMPCK_KEY_LEN 32 119 120 /* See the SNP spec version 0.9 for secrets page format */ 121 struct snp_secrets_page_layout { 122 u32 version; 123 u32 imien : 1, 124 rsvd1 : 31; 125 u32 fms; 126 u32 rsvd2; 127 u8 gosvw[16]; 128 u8 vmpck0[VMPCK_KEY_LEN]; 129 u8 vmpck1[VMPCK_KEY_LEN]; 130 u8 vmpck2[VMPCK_KEY_LEN]; 131 u8 vmpck3[VMPCK_KEY_LEN]; 132 struct secrets_os_area os_area; 133 u8 rsvd3[3840]; 134 } __packed; 135 136 #ifdef CONFIG_AMD_MEM_ENCRYPT 137 extern struct static_key_false sev_es_enable_key; 138 extern void __sev_es_ist_enter(struct pt_regs *regs); 139 extern void __sev_es_ist_exit(void); 140 static __always_inline void sev_es_ist_enter(struct pt_regs *regs) 141 { 142 if (static_branch_unlikely(&sev_es_enable_key)) 143 __sev_es_ist_enter(regs); 144 } 145 static __always_inline void sev_es_ist_exit(void) 146 { 147 if (static_branch_unlikely(&sev_es_enable_key)) 148 __sev_es_ist_exit(); 149 } 150 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh); 151 extern void __sev_es_nmi_complete(void); 152 static __always_inline void sev_es_nmi_complete(void) 153 { 154 if (static_branch_unlikely(&sev_es_enable_key)) 155 __sev_es_nmi_complete(); 156 } 157 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); 158 159 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) 160 { 161 int rc; 162 163 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */ 164 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t" 165 : "=a"(rc) 166 : "a"(vaddr), "c"(rmp_psize), "d"(attrs) 167 : "memory", "cc"); 168 169 return rc; 170 } 171 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) 172 { 173 bool no_rmpupdate; 174 int rc; 175 176 /* "pvalidate" mnemonic support in binutils 2.36 and newer */ 177 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t" 178 CC_SET(c) 179 : CC_OUT(c) (no_rmpupdate), "=a"(rc) 180 : "a"(vaddr), "c"(rmp_psize), "d"(validate) 181 : "memory", "cc"); 182 183 if (no_rmpupdate) 184 return PVALIDATE_FAIL_NOUPDATE; 185 186 return rc; 187 } 188 void setup_ghcb(void); 189 void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, 190 unsigned int npages); 191 void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, 192 unsigned int npages); 193 void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); 194 void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); 195 void snp_set_memory_private(unsigned long vaddr, unsigned int npages); 196 void snp_set_wakeup_secondary_cpu(void); 197 bool snp_init(struct boot_params *bp); 198 void snp_abort(void); 199 int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); 200 #else 201 static inline void sev_es_ist_enter(struct pt_regs *regs) { } 202 static inline void sev_es_ist_exit(void) { } 203 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } 204 static inline void sev_es_nmi_complete(void) { } 205 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } 206 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } 207 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } 208 static inline void setup_ghcb(void) { } 209 static inline void __init 210 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } 211 static inline void __init 212 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } 213 static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } 214 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } 215 static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } 216 static inline void snp_set_wakeup_secondary_cpu(void) { } 217 static inline bool snp_init(struct boot_params *bp) { return false; } 218 static inline void snp_abort(void) { } 219 static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, 220 unsigned long *fw_err) 221 { 222 return -ENOTTY; 223 } 224 #endif 225 226 #endif 227