1 /* SPDX-License-Identifier: GPL-2.0 */
3 * AMD Encrypted Register State Support
8 #ifndef __ASM_ENCRYPTED_STATE_H
9 #define __ASM_ENCRYPTED_STATE_H
11 #include <linux/types.h>
12 #include <linux/sev-guest.h>
15 #include <asm/sev-common.h>
17 #include <asm/set_memory.h>
19 #define GHCB_PROTOCOL_MIN 1ULL
20 #define GHCB_PROTOCOL_MAX 2ULL
21 #define GHCB_DEFAULT_USAGE 0ULL
23 #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); }
29 ES_UNSUPPORTED, /* Requested operation not supported */
30 ES_VMM_ERROR, /* Unexpected state from the VMM */
31 ES_DECODE_FAILED, /* Instruction decoding failed */
32 ES_EXCEPTION, /* Instruction caused exception */
33 ES_RETRY, /* Retry instruction emulation */
36 struct es_fault_info {
38 unsigned long error_code;
44 /* ES instruction emulation context */
48 struct es_fault_info fi;
52 * AMD SEV Confidential computing blob structure. The structure is
53 * defined in OVMF UEFI firmware header:
54 * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h
56 #define CC_BLOB_SEV_HDR_MAGIC 0x45444d41
57 struct cc_blob_sev_info {
69 void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
71 static inline u64 lower_bits(u64 val, unsigned int bits)
73 u64 mask = (1ULL << bits) - 1;
78 struct real_mode_header;
81 /* Early IDT entry points for #VC handler */
82 extern void vc_no_ghcb(void);
83 extern void vc_boot_ghcb(void);
84 extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
86 /* PVALIDATE return codes */
87 #define PVALIDATE_FAIL_SIZEMISMATCH 6
89 /* Software defined (when rFlags.CF = 1) */
90 #define PVALIDATE_FAIL_NOUPDATE 255
92 /* RMUPDATE detected 4K page and 2MB page overlap. */
93 #define RMPUPDATE_FAIL_OVERLAP 4
95 /* PSMASH failed due to concurrent access by another CPU */
96 #define PSMASH_FAIL_INUSE 3
99 #define RMP_PG_SIZE_4K 0
100 #define RMP_PG_SIZE_2M 1
101 #define RMP_TO_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M)
102 #define PG_LEVEL_TO_RMP(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)
113 #define RMPADJUST_VMSA_PAGE_BIT BIT(16)
115 /* SNP Guest message request */
116 struct snp_req_data {
117 unsigned long req_gpa;
118 unsigned long resp_gpa;
119 unsigned long data_gpa;
120 unsigned int data_npages;
123 #define MAX_AUTHTAG_LEN 32
124 #define AUTHTAG_LEN 16
126 #define MSG_HDR_VER 1
128 #define SNP_REQ_MAX_RETRY_DURATION (60*HZ)
129 #define SNP_REQ_RETRY_DELAY (2*HZ)
131 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
133 SNP_MSG_TYPE_INVALID = 0,
149 SNP_MSG_TSC_INFO_REQ = 17,
150 SNP_MSG_TSC_INFO_RSP,
157 SNP_AEAD_AES_256_GCM,
160 struct snp_guest_msg_hdr {
161 u8 authtag[MAX_AUTHTAG_LEN];
175 struct snp_guest_msg {
176 struct snp_guest_msg_hdr hdr;
177 u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
180 #define SNP_TSC_INFO_REQ_SZ 128
182 struct snp_tsc_info_req {
183 u8 rsvd[SNP_TSC_INFO_REQ_SZ];
186 struct snp_tsc_info_resp {
195 struct snp_guest_req {
203 unsigned int vmpck_id;
209 * The secrets page contains 96-bytes of reserved field that can be used by
210 * the guest OS. The guest OS uses the area to save the message sequence
211 * number for each VMPCK.
213 * See the GHCB spec section Secret page layout for the format for this area.
215 struct secrets_os_area {
220 u64 ap_jump_table_pa;
225 #define VMPCK_KEY_LEN 32
227 /* See the SNP spec version 0.9 for secrets page format */
228 struct snp_secrets_page {
235 u8 vmpck0[VMPCK_KEY_LEN];
236 u8 vmpck1[VMPCK_KEY_LEN];
237 u8 vmpck2[VMPCK_KEY_LEN];
238 u8 vmpck3[VMPCK_KEY_LEN];
239 struct secrets_os_area os_area;
241 u8 vmsa_tweak_bitmap[64];
247 u32 svsm_max_version;
251 /* Remainder of page */
255 struct snp_msg_desc {
256 /* request and response are in unencrypted memory */
257 struct snp_guest_msg *request, *response;
260 * Avoid information leakage by double-buffering shared messages
261 * in fields that are in regular encrypted memory.
263 struct snp_guest_msg secret_request, secret_response;
265 struct snp_secrets_page *secrets;
266 struct snp_req_data input;
270 struct aesgcm_ctx *ctx;
272 u32 *os_area_msg_seqno;
278 * The SVSM Calling Area (CA) related structures.
285 u8 svsm_buffer[PAGE_SIZE - 8];
288 #define SVSM_SUCCESS 0
289 #define SVSM_ERR_INCOMPLETE 0x80000000
290 #define SVSM_ERR_UNSUPPORTED_PROTOCOL 0x80000001
291 #define SVSM_ERR_UNSUPPORTED_CALL 0x80000002
292 #define SVSM_ERR_INVALID_ADDRESS 0x80000003
293 #define SVSM_ERR_INVALID_FORMAT 0x80000004
294 #define SVSM_ERR_INVALID_PARAMETER 0x80000005
295 #define SVSM_ERR_INVALID_REQUEST 0x80000006
296 #define SVSM_ERR_BUSY 0x80000007
297 #define SVSM_PVALIDATE_FAIL_SIZEMISMATCH 0x80001006
300 * The SVSM PVALIDATE related structures
302 struct svsm_pvalidate_entry {
310 struct svsm_pvalidate_call {
316 struct svsm_pvalidate_entry entry[];
319 #define SVSM_PVALIDATE_MAX_COUNT ((sizeof_field(struct svsm_ca, svsm_buffer) - \
320 offsetof(struct svsm_pvalidate_call, entry)) / \
321 sizeof(struct svsm_pvalidate_entry))
324 * The SVSM Attestation related structures
326 struct svsm_loc_entry {
332 struct svsm_attest_call {
333 struct svsm_loc_entry report_buf;
334 struct svsm_loc_entry nonce;
335 struct svsm_loc_entry manifest_buf;
336 struct svsm_loc_entry certificates_buf;
338 /* For attesting a single service */
340 u32 service_manifest_ver;
344 /* PTE descriptor used for the prepare_pte_enc() operations. */
345 struct pte_enc_desc {
349 /* pfn of the kpte above */
351 /* physical address of @pfn */
353 /* virtual address of @pfn */
355 /* memory covered by the pte */
361 * SVSM protocol structure
377 #define SVSM_CORE_CALL(x) ((0ULL << 32) | (x))
378 #define SVSM_CORE_REMAP_CA 0
379 #define SVSM_CORE_PVALIDATE 1
380 #define SVSM_CORE_CREATE_VCPU 2
381 #define SVSM_CORE_DELETE_VCPU 3
383 #define SVSM_ATTEST_CALL(x) ((1ULL << 32) | (x))
384 #define SVSM_ATTEST_SERVICES 0
385 #define SVSM_ATTEST_SINGLE_SERVICE 1
387 #ifdef CONFIG_AMD_MEM_ENCRYPT
391 extern void __sev_es_ist_enter(struct pt_regs *regs);
392 extern void __sev_es_ist_exit(void);
393 static __always_inline void sev_es_ist_enter(struct pt_regs *regs)
395 if (cc_vendor == CC_VENDOR_AMD &&
396 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
397 __sev_es_ist_enter(regs);
399 static __always_inline void sev_es_ist_exit(void)
401 if (cc_vendor == CC_VENDOR_AMD &&
402 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
405 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
406 extern void __sev_es_nmi_complete(void);
407 static __always_inline void sev_es_nmi_complete(void)
409 if (cc_vendor == CC_VENDOR_AMD &&
410 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
411 __sev_es_nmi_complete();
413 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
414 extern void sev_enable(struct boot_params *bp);
417 * RMPADJUST modifies the RMP permissions of a page of a lesser-
418 * privileged (numerically higher) VMPL.
420 * If the guest is running at a higher-privilege than the privilege
421 * level the instruction is targeting, the instruction will succeed,
422 * otherwise, it will fail.
424 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
428 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
429 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
431 : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
436 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
441 /* "pvalidate" mnemonic support in binutils 2.36 and newer */
442 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
444 : CC_OUT(c) (no_rmpupdate), "=a"(rc)
445 : "a"(vaddr), "c"(rmp_psize), "d"(validate)
449 return PVALIDATE_FAIL_NOUPDATE;
454 struct snp_guest_request_ioctl;
456 void setup_ghcb(void);
457 void early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
458 unsigned long npages);
459 void early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
460 unsigned long npages);
461 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages);
462 void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
463 void snp_set_wakeup_secondary_cpu(void);
464 bool snp_init(struct boot_params *bp);
465 void __noreturn snp_abort(void);
466 void snp_dmi_setup(void);
467 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
468 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
469 u64 snp_get_unsupported_features(u64 status);
470 u64 sev_get_status(void);
471 void sev_show_status(void);
472 void snp_update_svsm_ca(void);
473 int prepare_pte_enc(struct pte_enc_desc *d);
474 void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot);
475 void snp_kexec_finish(void);
476 void snp_kexec_begin(void);
478 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
479 struct snp_msg_desc *snp_msg_alloc(void);
480 void snp_msg_free(struct snp_msg_desc *mdesc);
481 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
482 struct snp_guest_request_ioctl *rio);
484 void __init snp_secure_tsc_prepare(void);
485 void __init snp_secure_tsc_init(void);
487 #else /* !CONFIG_AMD_MEM_ENCRYPT */
490 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
491 static inline void sev_es_ist_exit(void) { }
492 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
493 static inline void sev_es_nmi_complete(void) { }
494 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
495 static inline void sev_enable(struct boot_params *bp) { }
496 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; }
497 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; }
498 static inline void setup_ghcb(void) { }
499 static inline void __init
500 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
501 static inline void __init
502 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
503 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { }
504 static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { }
505 static inline void snp_set_wakeup_secondary_cpu(void) { }
506 static inline bool snp_init(struct boot_params *bp) { return false; }
507 static inline void snp_abort(void) { }
508 static inline void snp_dmi_setup(void) { }
509 static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input)
513 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
514 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
515 static inline u64 sev_get_status(void) { return 0; }
516 static inline void sev_show_status(void) { }
517 static inline void snp_update_svsm_ca(void) { }
518 static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }
519 static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { }
520 static inline void snp_kexec_finish(void) { }
521 static inline void snp_kexec_begin(void) { }
522 static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
523 static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
524 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
525 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
526 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
527 static inline void __init snp_secure_tsc_prepare(void) { }
528 static inline void __init snp_secure_tsc_init(void) { }
530 #endif /* CONFIG_AMD_MEM_ENCRYPT */
532 #ifdef CONFIG_KVM_AMD_SEV
533 bool snp_probe_rmptable_info(void);
534 int snp_rmptable_init(void);
535 int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level);
536 void snp_dump_hva_rmpentry(unsigned long address);
538 int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
539 int rmp_make_shared(u64 pfn, enum pg_level level);
540 void snp_leak_pages(u64 pfn, unsigned int npages);
541 void kdump_sev_callback(void);
542 void snp_fixup_e820_tables(void);
544 static inline bool snp_probe_rmptable_info(void) { return false; }
545 static inline int snp_rmptable_init(void) { return -ENOSYS; }
546 static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; }
547 static inline void snp_dump_hva_rmpentry(unsigned long address) {}
548 static inline int psmash(u64 pfn) { return -ENODEV; }
549 static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid,
554 static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
555 static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
556 static inline void kdump_sev_callback(void) { }
557 static inline void snp_fixup_e820_tables(void) {}