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 snp_guest_platform_data { 99 u64 secrets_gpa; 100 }; 101 102 #ifdef CONFIG_AMD_MEM_ENCRYPT 103 extern struct static_key_false sev_es_enable_key; 104 extern void __sev_es_ist_enter(struct pt_regs *regs); 105 extern void __sev_es_ist_exit(void); 106 static __always_inline void sev_es_ist_enter(struct pt_regs *regs) 107 { 108 if (static_branch_unlikely(&sev_es_enable_key)) 109 __sev_es_ist_enter(regs); 110 } 111 static __always_inline void sev_es_ist_exit(void) 112 { 113 if (static_branch_unlikely(&sev_es_enable_key)) 114 __sev_es_ist_exit(); 115 } 116 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh); 117 extern void __sev_es_nmi_complete(void); 118 static __always_inline void sev_es_nmi_complete(void) 119 { 120 if (static_branch_unlikely(&sev_es_enable_key)) 121 __sev_es_nmi_complete(); 122 } 123 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); 124 extern enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, 125 bool set_ghcb_msr, 126 struct es_em_ctxt *ctxt, 127 u64 exit_code, u64 exit_info_1, 128 u64 exit_info_2); 129 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) 130 { 131 int rc; 132 133 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */ 134 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t" 135 : "=a"(rc) 136 : "a"(vaddr), "c"(rmp_psize), "d"(attrs) 137 : "memory", "cc"); 138 139 return rc; 140 } 141 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) 142 { 143 bool no_rmpupdate; 144 int rc; 145 146 /* "pvalidate" mnemonic support in binutils 2.36 and newer */ 147 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t" 148 CC_SET(c) 149 : CC_OUT(c) (no_rmpupdate), "=a"(rc) 150 : "a"(vaddr), "c"(rmp_psize), "d"(validate) 151 : "memory", "cc"); 152 153 if (no_rmpupdate) 154 return PVALIDATE_FAIL_NOUPDATE; 155 156 return rc; 157 } 158 void setup_ghcb(void); 159 void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, 160 unsigned int npages); 161 void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, 162 unsigned int npages); 163 void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op); 164 void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); 165 void snp_set_memory_private(unsigned long vaddr, unsigned int npages); 166 void snp_set_wakeup_secondary_cpu(void); 167 bool snp_init(struct boot_params *bp); 168 void snp_abort(void); 169 int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); 170 #else 171 static inline void sev_es_ist_enter(struct pt_regs *regs) { } 172 static inline void sev_es_ist_exit(void) { } 173 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } 174 static inline void sev_es_nmi_complete(void) { } 175 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } 176 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } 177 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; } 178 static inline void setup_ghcb(void) { } 179 static inline void __init 180 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } 181 static inline void __init 182 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } 183 static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { } 184 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } 185 static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } 186 static inline void snp_set_wakeup_secondary_cpu(void) { } 187 static inline bool snp_init(struct boot_params *bp) { return false; } 188 static inline void snp_abort(void) { } 189 static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, 190 unsigned long *fw_err) 191 { 192 return -ENOTTY; 193 } 194 #endif 195 196 #endif 197