xref: /linux/arch/x86/include/asm/sev.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 <linux/sev-guest.h>
13 
14 #include <asm/insn.h>
15 #include <asm/sev-common.h>
16 #include <asm/coco.h>
17 
18 #define GHCB_PROTOCOL_MIN	1ULL
19 #define GHCB_PROTOCOL_MAX	2ULL
20 #define GHCB_DEFAULT_USAGE	0ULL
21 
22 #define	VMGEXIT()			{ asm volatile("rep; vmmcall\n\r"); }
23 
24 struct boot_params;
25 
26 enum es_result {
27 	ES_OK,			/* All good */
28 	ES_UNSUPPORTED,		/* Requested operation not supported */
29 	ES_VMM_ERROR,		/* Unexpected state from the VMM */
30 	ES_DECODE_FAILED,	/* Instruction decoding failed */
31 	ES_EXCEPTION,		/* Instruction caused exception */
32 	ES_RETRY,		/* Retry instruction emulation */
33 };
34 
35 struct es_fault_info {
36 	unsigned long vector;
37 	unsigned long error_code;
38 	unsigned long cr2;
39 };
40 
41 struct pt_regs;
42 
43 /* ES instruction emulation context */
44 struct es_em_ctxt {
45 	struct pt_regs *regs;
46 	struct insn insn;
47 	struct es_fault_info fi;
48 };
49 
50 /*
51  * AMD SEV Confidential computing blob structure. The structure is
52  * defined in OVMF UEFI firmware header:
53  * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h
54  */
55 #define CC_BLOB_SEV_HDR_MAGIC	0x45444d41
56 struct cc_blob_sev_info {
57 	u32 magic;
58 	u16 version;
59 	u16 reserved;
60 	u64 secrets_phys;
61 	u32 secrets_len;
62 	u32 rsvd1;
63 	u64 cpuid_phys;
64 	u32 cpuid_len;
65 	u32 rsvd2;
66 } __packed;
67 
68 void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
69 
lower_bits(u64 val,unsigned int bits)70 static inline u64 lower_bits(u64 val, unsigned int bits)
71 {
72 	u64 mask = (1ULL << bits) - 1;
73 
74 	return (val & mask);
75 }
76 
77 struct real_mode_header;
78 enum stack_type;
79 
80 /* Early IDT entry points for #VC handler */
81 extern void vc_no_ghcb(void);
82 extern void vc_boot_ghcb(void);
83 extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
84 
85 /* PVALIDATE return codes */
86 #define PVALIDATE_FAIL_SIZEMISMATCH	6
87 
88 /* Software defined (when rFlags.CF = 1) */
89 #define PVALIDATE_FAIL_NOUPDATE		255
90 
91 /* RMUPDATE detected 4K page and 2MB page overlap. */
92 #define RMPUPDATE_FAIL_OVERLAP		4
93 
94 /* PSMASH failed due to concurrent access by another CPU */
95 #define PSMASH_FAIL_INUSE		3
96 
97 /* RMP page size */
98 #define RMP_PG_SIZE_4K			0
99 #define RMP_PG_SIZE_2M			1
100 #define RMP_TO_PG_LEVEL(level)		(((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M)
101 #define PG_LEVEL_TO_RMP(level)		(((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)
102 
103 struct rmp_state {
104 	u64 gpa;
105 	u8 assigned;
106 	u8 pagesize;
107 	u8 immutable;
108 	u8 rsvd;
109 	u32 asid;
110 } __packed;
111 
112 #define RMPADJUST_VMSA_PAGE_BIT		BIT(16)
113 
114 /* SNP Guest message request */
115 struct snp_req_data {
116 	unsigned long req_gpa;
117 	unsigned long resp_gpa;
118 	unsigned long data_gpa;
119 	unsigned int data_npages;
120 };
121 
122 #define MAX_AUTHTAG_LEN		32
123 #define AUTHTAG_LEN		16
124 #define AAD_LEN			48
125 #define MSG_HDR_VER		1
126 
127 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
128 enum msg_type {
129 	SNP_MSG_TYPE_INVALID = 0,
130 	SNP_MSG_CPUID_REQ,
131 	SNP_MSG_CPUID_RSP,
132 	SNP_MSG_KEY_REQ,
133 	SNP_MSG_KEY_RSP,
134 	SNP_MSG_REPORT_REQ,
135 	SNP_MSG_REPORT_RSP,
136 	SNP_MSG_EXPORT_REQ,
137 	SNP_MSG_EXPORT_RSP,
138 	SNP_MSG_IMPORT_REQ,
139 	SNP_MSG_IMPORT_RSP,
140 	SNP_MSG_ABSORB_REQ,
141 	SNP_MSG_ABSORB_RSP,
142 	SNP_MSG_VMRK_REQ,
143 	SNP_MSG_VMRK_RSP,
144 
145 	SNP_MSG_TYPE_MAX
146 };
147 
148 enum aead_algo {
149 	SNP_AEAD_INVALID,
150 	SNP_AEAD_AES_256_GCM,
151 };
152 
153 struct snp_guest_msg_hdr {
154 	u8 authtag[MAX_AUTHTAG_LEN];
155 	u64 msg_seqno;
156 	u8 rsvd1[8];
157 	u8 algo;
158 	u8 hdr_version;
159 	u16 hdr_sz;
160 	u8 msg_type;
161 	u8 msg_version;
162 	u16 msg_sz;
163 	u32 rsvd2;
164 	u8 msg_vmpck;
165 	u8 rsvd3[35];
166 } __packed;
167 
168 struct snp_guest_msg {
169 	struct snp_guest_msg_hdr hdr;
170 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
171 } __packed;
172 
173 struct sev_guest_platform_data {
174 	u64 secrets_gpa;
175 };
176 
177 struct snp_guest_req {
178 	void *req_buf;
179 	size_t req_sz;
180 
181 	void *resp_buf;
182 	size_t resp_sz;
183 
184 	u64 exit_code;
185 	unsigned int vmpck_id;
186 	u8 msg_version;
187 	u8 msg_type;
188 };
189 
190 /*
191  * The secrets page contains 96-bytes of reserved field that can be used by
192  * the guest OS. The guest OS uses the area to save the message sequence
193  * number for each VMPCK.
194  *
195  * See the GHCB spec section Secret page layout for the format for this area.
196  */
197 struct secrets_os_area {
198 	u32 msg_seqno_0;
199 	u32 msg_seqno_1;
200 	u32 msg_seqno_2;
201 	u32 msg_seqno_3;
202 	u64 ap_jump_table_pa;
203 	u8 rsvd[40];
204 	u8 guest_usage[32];
205 } __packed;
206 
207 #define VMPCK_KEY_LEN		32
208 
209 /* See the SNP spec version 0.9 for secrets page format */
210 struct snp_secrets_page {
211 	u32 version;
212 	u32 imien	: 1,
213 	    rsvd1	: 31;
214 	u32 fms;
215 	u32 rsvd2;
216 	u8 gosvw[16];
217 	u8 vmpck0[VMPCK_KEY_LEN];
218 	u8 vmpck1[VMPCK_KEY_LEN];
219 	u8 vmpck2[VMPCK_KEY_LEN];
220 	u8 vmpck3[VMPCK_KEY_LEN];
221 	struct secrets_os_area os_area;
222 
223 	u8 vmsa_tweak_bitmap[64];
224 
225 	/* SVSM fields */
226 	u64 svsm_base;
227 	u64 svsm_size;
228 	u64 svsm_caa;
229 	u32 svsm_max_version;
230 	u8 svsm_guest_vmpl;
231 	u8 rsvd3[3];
232 
233 	/* Remainder of page */
234 	u8 rsvd4[3744];
235 } __packed;
236 
237 struct snp_msg_desc {
238 	/* request and response are in unencrypted memory */
239 	struct snp_guest_msg *request, *response;
240 
241 	/*
242 	 * Avoid information leakage by double-buffering shared messages
243 	 * in fields that are in regular encrypted memory.
244 	 */
245 	struct snp_guest_msg secret_request, secret_response;
246 
247 	struct snp_secrets_page *secrets;
248 	struct snp_req_data input;
249 
250 	void *certs_data;
251 
252 	struct aesgcm_ctx *ctx;
253 
254 	u32 *os_area_msg_seqno;
255 	u8 *vmpck;
256 };
257 
258 /*
259  * The SVSM Calling Area (CA) related structures.
260  */
261 struct svsm_ca {
262 	u8 call_pending;
263 	u8 mem_available;
264 	u8 rsvd1[6];
265 
266 	u8 svsm_buffer[PAGE_SIZE - 8];
267 };
268 
269 #define SVSM_SUCCESS				0
270 #define SVSM_ERR_INCOMPLETE			0x80000000
271 #define SVSM_ERR_UNSUPPORTED_PROTOCOL		0x80000001
272 #define SVSM_ERR_UNSUPPORTED_CALL		0x80000002
273 #define SVSM_ERR_INVALID_ADDRESS		0x80000003
274 #define SVSM_ERR_INVALID_FORMAT			0x80000004
275 #define SVSM_ERR_INVALID_PARAMETER		0x80000005
276 #define SVSM_ERR_INVALID_REQUEST		0x80000006
277 #define SVSM_ERR_BUSY				0x80000007
278 #define SVSM_PVALIDATE_FAIL_SIZEMISMATCH	0x80001006
279 
280 /*
281  * The SVSM PVALIDATE related structures
282  */
283 struct svsm_pvalidate_entry {
284 	u64 page_size		: 2,
285 	    action		: 1,
286 	    ignore_cf		: 1,
287 	    rsvd		: 8,
288 	    pfn			: 52;
289 };
290 
291 struct svsm_pvalidate_call {
292 	u16 num_entries;
293 	u16 cur_index;
294 
295 	u8 rsvd1[4];
296 
297 	struct svsm_pvalidate_entry entry[];
298 };
299 
300 #define SVSM_PVALIDATE_MAX_COUNT	((sizeof_field(struct svsm_ca, svsm_buffer) -		\
301 					  offsetof(struct svsm_pvalidate_call, entry)) /	\
302 					 sizeof(struct svsm_pvalidate_entry))
303 
304 /*
305  * The SVSM Attestation related structures
306  */
307 struct svsm_loc_entry {
308 	u64 pa;
309 	u32 len;
310 	u8 rsvd[4];
311 };
312 
313 struct svsm_attest_call {
314 	struct svsm_loc_entry report_buf;
315 	struct svsm_loc_entry nonce;
316 	struct svsm_loc_entry manifest_buf;
317 	struct svsm_loc_entry certificates_buf;
318 
319 	/* For attesting a single service */
320 	u8 service_guid[16];
321 	u32 service_manifest_ver;
322 	u8 rsvd[4];
323 };
324 
325 /* PTE descriptor used for the prepare_pte_enc() operations. */
326 struct pte_enc_desc {
327 	pte_t *kpte;
328 	int pte_level;
329 	bool encrypt;
330 	/* pfn of the kpte above */
331 	unsigned long pfn;
332 	/* physical address of @pfn */
333 	unsigned long pa;
334 	/* virtual address of @pfn */
335 	void *va;
336 	/* memory covered by the pte */
337 	unsigned long size;
338 	pgprot_t new_pgprot;
339 };
340 
341 /*
342  * SVSM protocol structure
343  */
344 struct svsm_call {
345 	struct svsm_ca *caa;
346 	u64 rax;
347 	u64 rcx;
348 	u64 rdx;
349 	u64 r8;
350 	u64 r9;
351 	u64 rax_out;
352 	u64 rcx_out;
353 	u64 rdx_out;
354 	u64 r8_out;
355 	u64 r9_out;
356 };
357 
358 #define SVSM_CORE_CALL(x)		((0ULL << 32) | (x))
359 #define SVSM_CORE_REMAP_CA		0
360 #define SVSM_CORE_PVALIDATE		1
361 #define SVSM_CORE_CREATE_VCPU		2
362 #define SVSM_CORE_DELETE_VCPU		3
363 
364 #define SVSM_ATTEST_CALL(x)		((1ULL << 32) | (x))
365 #define SVSM_ATTEST_SERVICES		0
366 #define SVSM_ATTEST_SINGLE_SERVICE	1
367 
368 #ifdef CONFIG_AMD_MEM_ENCRYPT
369 
370 extern u8 snp_vmpl;
371 
372 extern void __sev_es_ist_enter(struct pt_regs *regs);
373 extern void __sev_es_ist_exit(void);
sev_es_ist_enter(struct pt_regs * regs)374 static __always_inline void sev_es_ist_enter(struct pt_regs *regs)
375 {
376 	if (cc_vendor == CC_VENDOR_AMD &&
377 	    cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
378 		__sev_es_ist_enter(regs);
379 }
sev_es_ist_exit(void)380 static __always_inline void sev_es_ist_exit(void)
381 {
382 	if (cc_vendor == CC_VENDOR_AMD &&
383 	    cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
384 		__sev_es_ist_exit();
385 }
386 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
387 extern void __sev_es_nmi_complete(void);
sev_es_nmi_complete(void)388 static __always_inline void sev_es_nmi_complete(void)
389 {
390 	if (cc_vendor == CC_VENDOR_AMD &&
391 	    cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
392 		__sev_es_nmi_complete();
393 }
394 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
395 extern void sev_enable(struct boot_params *bp);
396 
397 /*
398  * RMPADJUST modifies the RMP permissions of a page of a lesser-
399  * privileged (numerically higher) VMPL.
400  *
401  * If the guest is running at a higher-privilege than the privilege
402  * level the instruction is targeting, the instruction will succeed,
403  * otherwise, it will fail.
404  */
rmpadjust(unsigned long vaddr,bool rmp_psize,unsigned long attrs)405 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
406 {
407 	int rc;
408 
409 	/* "rmpadjust" mnemonic support in binutils 2.36 and newer */
410 	asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
411 		     : "=a"(rc)
412 		     : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
413 		     : "memory", "cc");
414 
415 	return rc;
416 }
pvalidate(unsigned long vaddr,bool rmp_psize,bool validate)417 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
418 {
419 	bool no_rmpupdate;
420 	int rc;
421 
422 	/* "pvalidate" mnemonic support in binutils 2.36 and newer */
423 	asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
424 		     CC_SET(c)
425 		     : CC_OUT(c) (no_rmpupdate), "=a"(rc)
426 		     : "a"(vaddr), "c"(rmp_psize), "d"(validate)
427 		     : "memory", "cc");
428 
429 	if (no_rmpupdate)
430 		return PVALIDATE_FAIL_NOUPDATE;
431 
432 	return rc;
433 }
434 
435 struct snp_guest_request_ioctl;
436 
437 void setup_ghcb(void);
438 void early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
439 				  unsigned long npages);
440 void early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
441 				 unsigned long npages);
442 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages);
443 void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
444 void snp_set_wakeup_secondary_cpu(void);
445 bool snp_init(struct boot_params *bp);
446 void __noreturn snp_abort(void);
447 void snp_dmi_setup(void);
448 int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
449 			    struct snp_guest_request_ioctl *rio);
450 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
451 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
452 u64 snp_get_unsupported_features(u64 status);
453 u64 sev_get_status(void);
454 void sev_show_status(void);
455 void snp_update_svsm_ca(void);
456 int prepare_pte_enc(struct pte_enc_desc *d);
457 void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot);
458 void snp_kexec_finish(void);
459 void snp_kexec_begin(void);
460 
461 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
462 
463 #define snp_vmpl 0
sev_es_ist_enter(struct pt_regs * regs)464 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
sev_es_ist_exit(void)465 static inline void sev_es_ist_exit(void) { }
sev_es_setup_ap_jump_table(struct real_mode_header * rmh)466 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
sev_es_nmi_complete(void)467 static inline void sev_es_nmi_complete(void) { }
sev_es_efi_map_ghcbs(pgd_t * pgd)468 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
sev_enable(struct boot_params * bp)469 static inline void sev_enable(struct boot_params *bp) { }
pvalidate(unsigned long vaddr,bool rmp_psize,bool validate)470 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; }
rmpadjust(unsigned long vaddr,bool rmp_psize,unsigned long attrs)471 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; }
setup_ghcb(void)472 static inline void setup_ghcb(void) { }
473 static inline void __init
early_snp_set_memory_private(unsigned long vaddr,unsigned long paddr,unsigned long npages)474 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
475 static inline void __init
early_snp_set_memory_shared(unsigned long vaddr,unsigned long paddr,unsigned long npages)476 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
snp_set_memory_shared(unsigned long vaddr,unsigned long npages)477 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { }
snp_set_memory_private(unsigned long vaddr,unsigned long npages)478 static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { }
snp_set_wakeup_secondary_cpu(void)479 static inline void snp_set_wakeup_secondary_cpu(void) { }
snp_init(struct boot_params * bp)480 static inline bool snp_init(struct boot_params *bp) { return false; }
snp_abort(void)481 static inline void snp_abort(void) { }
snp_dmi_setup(void)482 static inline void snp_dmi_setup(void) { }
snp_issue_guest_request(struct snp_guest_req * req,struct snp_req_data * input,struct snp_guest_request_ioctl * rio)483 static inline int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
484 					  struct snp_guest_request_ioctl *rio)
485 {
486 	return -ENOTTY;
487 }
snp_issue_svsm_attest_req(u64 call_id,struct svsm_call * call,struct svsm_attest_call * input)488 static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input)
489 {
490 	return -ENOTTY;
491 }
snp_accept_memory(phys_addr_t start,phys_addr_t end)492 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
snp_get_unsupported_features(u64 status)493 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
sev_get_status(void)494 static inline u64 sev_get_status(void) { return 0; }
sev_show_status(void)495 static inline void sev_show_status(void) { }
snp_update_svsm_ca(void)496 static inline void snp_update_svsm_ca(void) { }
prepare_pte_enc(struct pte_enc_desc * d)497 static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }
set_pte_enc_mask(pte_t * kpte,unsigned long pfn,pgprot_t new_prot)498 static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { }
snp_kexec_finish(void)499 static inline void snp_kexec_finish(void) { }
snp_kexec_begin(void)500 static inline void snp_kexec_begin(void) { }
501 
502 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
503 
504 #ifdef CONFIG_KVM_AMD_SEV
505 bool snp_probe_rmptable_info(void);
506 int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level);
507 void snp_dump_hva_rmpentry(unsigned long address);
508 int psmash(u64 pfn);
509 int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
510 int rmp_make_shared(u64 pfn, enum pg_level level);
511 void snp_leak_pages(u64 pfn, unsigned int npages);
512 void kdump_sev_callback(void);
513 void snp_fixup_e820_tables(void);
514 #else
snp_probe_rmptable_info(void)515 static inline bool snp_probe_rmptable_info(void) { return false; }
snp_lookup_rmpentry(u64 pfn,bool * assigned,int * level)516 static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; }
snp_dump_hva_rmpentry(unsigned long address)517 static inline void snp_dump_hva_rmpentry(unsigned long address) {}
psmash(u64 pfn)518 static inline int psmash(u64 pfn) { return -ENODEV; }
rmp_make_private(u64 pfn,u64 gpa,enum pg_level level,u32 asid,bool immutable)519 static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid,
520 				   bool immutable)
521 {
522 	return -ENODEV;
523 }
rmp_make_shared(u64 pfn,enum pg_level level)524 static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
snp_leak_pages(u64 pfn,unsigned int npages)525 static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
kdump_sev_callback(void)526 static inline void kdump_sev_callback(void) { }
snp_fixup_e820_tables(void)527 static inline void snp_fixup_e820_tables(void) {}
528 #endif
529 
530 #endif
531