1 /* SPDX-License-Identifier: GPL-2.0 */
3 * AMD Encrypted Register State Support
5 * Author: Joerg Roedel <jroedel@suse.de>
8 #ifndef __ASM_ENCRYPTED_STATE_H
9 #define __ASM_ENCRYPTED_STATE_H
11 #include <linux/types.h>
13 #include <asm/sev-common.h>
14 #include <asm/bootparam.h>
16 #define GHCB_PROTOCOL_MIN 1ULL
17 #define GHCB_PROTOCOL_MAX 2ULL
18 #define GHCB_DEFAULT_USAGE 0ULL
20 #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); }
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 */
31 struct es_fault_info {
33 unsigned long error_code;
39 /* ES instruction emulation context */
43 struct es_fault_info fi;
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
51 #define CC_BLOB_SEV_HDR_MAGIC 0x45444d41
52 struct cc_blob_sev_info {
64 void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
66 static inline u64 lower_bits(u64 val, unsigned int bits)
68 u64 mask = (1ULL << bits) - 1;
73 struct real_mode_header;
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);
82 /* Software defined (when rFlags.CF = 1) */
83 #define PVALIDATE_FAIL_NOUPDATE 255
86 #define RMP_PG_SIZE_4K 0
88 #define RMPADJUST_VMSA_PAGE_BIT BIT(16)
90 /* SNP Guest message request */
92 unsigned long req_gpa;
93 unsigned long resp_gpa;
94 unsigned long data_gpa;
95 unsigned int data_npages;
98 struct sev_guest_platform_data {
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.
107 * See the GHCB spec section Secret page layout for the format for this area.
109 struct secrets_os_area {
114 u64 ap_jump_table_pa;
119 #define VMPCK_KEY_LEN 32
121 /* See the SNP spec version 0.9 for secrets page format */
122 struct snp_secrets_page_layout {
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;
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)
143 if (static_branch_unlikely(&sev_es_enable_key))
144 __sev_es_ist_enter(regs);
146 static __always_inline void sev_es_ist_exit(void)
148 if (static_branch_unlikely(&sev_es_enable_key))
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)
155 if (static_branch_unlikely(&sev_es_enable_key))
156 __sev_es_nmi_complete();
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,
161 struct es_em_ctxt *ctxt,
162 u64 exit_code, u64 exit_info_1,
164 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
168 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
169 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
171 : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
176 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
181 /* "pvalidate" mnemonic support in binutils 2.36 and newer */
182 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
184 : CC_OUT(c) (no_rmpupdate), "=a"(rc)
185 : "a"(vaddr), "c"(rmp_psize), "d"(validate)
189 return PVALIDATE_FAIL_NOUPDATE;
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);
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)