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 {
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)
108 if (static_branch_unlikely(&sev_es_enable_key))
109 __sev_es_ist_enter(regs);
111 static __always_inline void sev_es_ist_exit(void)
113 if (static_branch_unlikely(&sev_es_enable_key))
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)
120 if (static_branch_unlikely(&sev_es_enable_key))
121 __sev_es_nmi_complete();
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,
126 struct es_em_ctxt *ctxt,
127 u64 exit_code, u64 exit_info_1,
129 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
133 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
134 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
136 : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
141 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
146 /* "pvalidate" mnemonic support in binutils 2.36 and newer */
147 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
149 : CC_OUT(c) (no_rmpupdate), "=a"(rc)
150 : "a"(vaddr), "c"(rmp_psize), "d"(validate)
154 return PVALIDATE_FAIL_NOUPDATE;
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);
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)