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>
12 #include <linux/sev-guest.h>
15 #include <asm/sev-common.h>
17 #include <asm/set_memory.h>
20 #define GHCB_PROTOCOL_MIN 1ULL
21 #define GHCB_PROTOCOL_MAX 2ULL
22 #define GHCB_DEFAULT_USAGE 0ULL
24 #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); }
30 ES_UNSUPPORTED, /* Requested operation not supported */
31 ES_VMM_ERROR, /* Unexpected state from the VMM */
32 ES_DECODE_FAILED, /* Instruction decoding failed */
33 ES_EXCEPTION, /* Instruction caused exception */
34 ES_RETRY, /* Retry instruction emulation */
37 struct es_fault_info {
39 unsigned long error_code;
45 /* ES instruction emulation context */
49 struct es_fault_info fi;
53 * AMD SEV Confidential computing blob structure. The structure is
54 * defined in OVMF UEFI firmware header:
55 * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h
57 #define CC_BLOB_SEV_HDR_MAGIC 0x45444d41
58 struct cc_blob_sev_info {
70 void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
72 static inline u64 lower_bits(u64 val, unsigned int bits)
74 u64 mask = (1ULL << bits) - 1;
79 struct real_mode_header;
82 /* Early IDT entry points for #VC handler */
83 extern void vc_no_ghcb(void);
84 extern void vc_boot_ghcb(void);
85 extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
88 * Individual entries of the SNP CPUID table, as defined by the SNP
89 * Firmware ABI, Revision 0.9, Section 7.1, Table 14.
104 * SNP CPUID table, as defined by the SNP Firmware ABI, Revision 0.9,
105 * Section 8.14.2.6. Also noted there is the SNP firmware-enforced limit
106 * of 64 entries per CPUID table.
108 #define SNP_CPUID_COUNT_MAX 64
110 struct snp_cpuid_table {
114 struct snp_cpuid_fn fn[SNP_CPUID_COUNT_MAX];
117 /* PVALIDATE return codes */
118 #define PVALIDATE_FAIL_SIZEMISMATCH 6
120 /* Software defined (when rFlags.CF = 1) */
121 #define PVALIDATE_FAIL_NOUPDATE 255
123 /* RMUPDATE detected 4K page and 2MB page overlap. */
124 #define RMPUPDATE_FAIL_OVERLAP 4
126 /* PSMASH failed due to concurrent access by another CPU */
127 #define PSMASH_FAIL_INUSE 3
130 #define RMP_PG_SIZE_4K 0
131 #define RMP_PG_SIZE_2M 1
132 #define RMP_TO_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M)
133 #define PG_LEVEL_TO_RMP(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)
144 #define RMPADJUST_VMSA_PAGE_BIT BIT(16)
146 /* SNP Guest message request */
147 struct snp_req_data {
148 unsigned long req_gpa;
149 unsigned long resp_gpa;
150 unsigned long data_gpa;
151 unsigned int data_npages;
154 #define MAX_AUTHTAG_LEN 32
155 #define AUTHTAG_LEN 16
157 #define MSG_HDR_VER 1
159 #define SNP_REQ_MAX_RETRY_DURATION (60*HZ)
160 #define SNP_REQ_RETRY_DELAY (2*HZ)
162 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
164 SNP_MSG_TYPE_INVALID = 0,
180 SNP_MSG_TSC_INFO_REQ = 17,
181 SNP_MSG_TSC_INFO_RSP,
188 SNP_AEAD_AES_256_GCM,
191 struct snp_guest_msg_hdr {
192 u8 authtag[MAX_AUTHTAG_LEN];
206 struct snp_guest_msg {
207 struct snp_guest_msg_hdr hdr;
208 u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
211 #define SNP_TSC_INFO_REQ_SZ 128
213 struct snp_tsc_info_req {
214 u8 rsvd[SNP_TSC_INFO_REQ_SZ];
217 struct snp_tsc_info_resp {
227 * Obtain the mean TSC frequency by decreasing the nominal TSC frequency with
228 * TSC_FACTOR as documented in the SNP Firmware ABI specification:
230 * GUEST_TSC_FREQ * (1 - (TSC_FACTOR * 0.00001))
232 * which is equivalent to:
234 * GUEST_TSC_FREQ -= (GUEST_TSC_FREQ * TSC_FACTOR) / 100000;
236 #define SNP_SCALE_TSC_FREQ(freq, factor) ((freq) - (freq) * (factor) / 100000)
238 struct snp_guest_req {
247 unsigned int vmpck_id;
251 struct snp_req_data input;
256 * The secrets page contains 96-bytes of reserved field that can be used by
257 * the guest OS. The guest OS uses the area to save the message sequence
258 * number for each VMPCK.
260 * See the GHCB spec section Secret page layout for the format for this area.
262 struct secrets_os_area {
267 u64 ap_jump_table_pa;
272 #define VMPCK_KEY_LEN 32
274 /* See the SNP spec version 0.9 for secrets page format */
275 struct snp_secrets_page {
282 u8 vmpck0[VMPCK_KEY_LEN];
283 u8 vmpck1[VMPCK_KEY_LEN];
284 u8 vmpck2[VMPCK_KEY_LEN];
285 u8 vmpck3[VMPCK_KEY_LEN];
286 struct secrets_os_area os_area;
288 u8 vmsa_tweak_bitmap[64];
294 u32 svsm_max_version;
298 /* The percentage decrease from nominal to mean TSC frequency. */
301 /* Remainder of page */
305 struct snp_msg_desc {
306 /* request and response are in unencrypted memory */
307 struct snp_guest_msg *request, *response;
310 * Avoid information leakage by double-buffering shared messages
311 * in fields that are in regular encrypted memory.
313 struct snp_guest_msg secret_request, secret_response;
315 struct snp_secrets_page *secrets;
317 struct aesgcm_ctx *ctx;
319 u32 *os_area_msg_seqno;
325 * The SVSM Calling Area (CA) related structures.
332 u8 svsm_buffer[PAGE_SIZE - 8];
335 #define SVSM_SUCCESS 0
336 #define SVSM_ERR_INCOMPLETE 0x80000000
337 #define SVSM_ERR_UNSUPPORTED_PROTOCOL 0x80000001
338 #define SVSM_ERR_UNSUPPORTED_CALL 0x80000002
339 #define SVSM_ERR_INVALID_ADDRESS 0x80000003
340 #define SVSM_ERR_INVALID_FORMAT 0x80000004
341 #define SVSM_ERR_INVALID_PARAMETER 0x80000005
342 #define SVSM_ERR_INVALID_REQUEST 0x80000006
343 #define SVSM_ERR_BUSY 0x80000007
344 #define SVSM_PVALIDATE_FAIL_SIZEMISMATCH 0x80001006
347 * The SVSM PVALIDATE related structures
349 struct svsm_pvalidate_entry {
357 struct svsm_pvalidate_call {
363 struct svsm_pvalidate_entry entry[];
366 #define SVSM_PVALIDATE_MAX_COUNT ((sizeof_field(struct svsm_ca, svsm_buffer) - \
367 offsetof(struct svsm_pvalidate_call, entry)) / \
368 sizeof(struct svsm_pvalidate_entry))
371 * The SVSM Attestation related structures
373 struct svsm_loc_entry {
379 struct svsm_attest_call {
380 struct svsm_loc_entry report_buf;
381 struct svsm_loc_entry nonce;
382 struct svsm_loc_entry manifest_buf;
383 struct svsm_loc_entry certificates_buf;
385 /* For attesting a single service */
387 u32 service_manifest_ver;
391 /* PTE descriptor used for the prepare_pte_enc() operations. */
392 struct pte_enc_desc {
396 /* pfn of the kpte above */
398 /* physical address of @pfn */
400 /* virtual address of @pfn */
402 /* memory covered by the pte */
408 * SVSM protocol structure
424 #define SVSM_CORE_CALL(x) ((0ULL << 32) | (x))
425 #define SVSM_CORE_REMAP_CA 0
426 #define SVSM_CORE_PVALIDATE 1
427 #define SVSM_CORE_CREATE_VCPU 2
428 #define SVSM_CORE_DELETE_VCPU 3
430 #define SVSM_ATTEST_CALL(x) ((1ULL << 32) | (x))
431 #define SVSM_ATTEST_SERVICES 0
432 #define SVSM_ATTEST_SINGLE_SERVICE 1
434 #define SVSM_VTPM_CALL(x) ((2ULL << 32) | (x))
435 #define SVSM_VTPM_QUERY 0
436 #define SVSM_VTPM_CMD 1
438 #ifdef CONFIG_AMD_MEM_ENCRYPT
442 extern void __sev_es_ist_enter(struct pt_regs *regs);
443 extern void __sev_es_ist_exit(void);
444 static __always_inline void sev_es_ist_enter(struct pt_regs *regs)
446 if (cc_vendor == CC_VENDOR_AMD &&
447 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
448 __sev_es_ist_enter(regs);
450 static __always_inline void sev_es_ist_exit(void)
452 if (cc_vendor == CC_VENDOR_AMD &&
453 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
456 extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
457 extern void __sev_es_nmi_complete(void);
458 static __always_inline void sev_es_nmi_complete(void)
460 if (cc_vendor == CC_VENDOR_AMD &&
461 cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
462 __sev_es_nmi_complete();
464 extern int __init sev_es_efi_map_ghcbs_cas(pgd_t *pgd);
465 extern void sev_enable(struct boot_params *bp);
468 * RMPADJUST modifies the RMP permissions of a page of a lesser-
469 * privileged (numerically higher) VMPL.
471 * If the guest is running at a higher-privilege than the privilege
472 * level the instruction is targeting, the instruction will succeed,
473 * otherwise, it will fail.
475 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
479 /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
480 asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
482 : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
487 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
492 /* "pvalidate" mnemonic support in binutils 2.36 and newer */
493 asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
495 : CC_OUT(c) (no_rmpupdate), "=a"(rc)
496 : "a"(vaddr), "c"(rmp_psize), "d"(validate)
500 return PVALIDATE_FAIL_NOUPDATE;
505 void setup_ghcb(void);
506 void early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
507 unsigned long npages);
508 void early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
509 unsigned long npages);
510 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages);
511 void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
512 void snp_set_wakeup_secondary_cpu(void);
513 bool snp_init(struct boot_params *bp);
514 void __noreturn snp_abort(void);
515 void snp_dmi_setup(void);
516 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
517 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
518 u64 snp_get_unsupported_features(u64 status);
519 u64 sev_get_status(void);
520 void sev_show_status(void);
521 void snp_update_svsm_ca(void);
522 int prepare_pte_enc(struct pte_enc_desc *d);
523 void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot);
524 void snp_kexec_finish(void);
525 void snp_kexec_begin(void);
527 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
528 struct snp_msg_desc *snp_msg_alloc(void);
529 void snp_msg_free(struct snp_msg_desc *mdesc);
530 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req);
532 int snp_svsm_vtpm_send_command(u8 *buffer);
534 void __init snp_secure_tsc_prepare(void);
535 void __init snp_secure_tsc_init(void);
537 static __always_inline void vc_ghcb_invalidate(struct ghcb *ghcb)
539 ghcb->save.sw_exit_code = 0;
540 __builtin_memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
543 void vc_forward_exception(struct es_em_ctxt *ctxt);
545 /* I/O parameters for CPUID-related helpers */
555 int snp_cpuid(struct ghcb *ghcb, struct es_em_ctxt *ctxt, struct cpuid_leaf *leaf);
557 void __noreturn sev_es_terminate(unsigned int set, unsigned int reason);
558 enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
559 struct es_em_ctxt *ctxt,
560 u64 exit_code, u64 exit_info_1,
563 extern struct ghcb *boot_ghcb;
565 #else /* !CONFIG_AMD_MEM_ENCRYPT */
568 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
569 static inline void sev_es_ist_exit(void) { }
570 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
571 static inline void sev_es_nmi_complete(void) { }
572 static inline int sev_es_efi_map_ghcbs_cas(pgd_t *pgd) { return 0; }
573 static inline void sev_enable(struct boot_params *bp) { }
574 static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; }
575 static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; }
576 static inline void setup_ghcb(void) { }
577 static inline void __init
578 early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
579 static inline void __init
580 early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
581 static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { }
582 static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { }
583 static inline void snp_set_wakeup_secondary_cpu(void) { }
584 static inline bool snp_init(struct boot_params *bp) { return false; }
585 static inline void snp_abort(void) { }
586 static inline void snp_dmi_setup(void) { }
587 static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input)
591 static inline void snp_accept_memory(phys_addr_t start, phys_addr_t end) { }
592 static inline u64 snp_get_unsupported_features(u64 status) { return 0; }
593 static inline u64 sev_get_status(void) { return 0; }
594 static inline void sev_show_status(void) { }
595 static inline void snp_update_svsm_ca(void) { }
596 static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }
597 static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { }
598 static inline void snp_kexec_finish(void) { }
599 static inline void snp_kexec_begin(void) { }
600 static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
601 static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
602 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
603 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc,
604 struct snp_guest_req *req) { return -ENODEV; }
605 static inline int snp_svsm_vtpm_send_command(u8 *buffer) { return -ENODEV; }
606 static inline void __init snp_secure_tsc_prepare(void) { }
607 static inline void __init snp_secure_tsc_init(void) { }
609 #endif /* CONFIG_AMD_MEM_ENCRYPT */
611 #ifdef CONFIG_KVM_AMD_SEV
612 bool snp_probe_rmptable_info(void);
613 int snp_rmptable_init(void);
614 int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level);
615 void snp_dump_hva_rmpentry(unsigned long address);
617 int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid, bool immutable);
618 int rmp_make_shared(u64 pfn, enum pg_level level);
619 void snp_leak_pages(u64 pfn, unsigned int npages);
620 void kdump_sev_callback(void);
621 void snp_fixup_e820_tables(void);
623 static inline void sev_evict_cache(void *va, int npages)
625 volatile u8 val __always_unused;
630 * For SEV guests, a read from the first/last cache-lines of a 4K page
631 * using the guest key is sufficient to cause a flush of all cache-lines
632 * associated with that 4K page without incurring all the overhead of a
633 * full CLFLUSH sequence.
635 for (page_idx = 0; page_idx < npages; page_idx++) {
636 val = bytes[page_idx * PAGE_SIZE];
637 val = bytes[page_idx * PAGE_SIZE + PAGE_SIZE - 1];
641 static inline bool snp_probe_rmptable_info(void) { return false; }
642 static inline int snp_rmptable_init(void) { return -ENOSYS; }
643 static inline int snp_lookup_rmpentry(u64 pfn, bool *assigned, int *level) { return -ENODEV; }
644 static inline void snp_dump_hva_rmpentry(unsigned long address) {}
645 static inline int psmash(u64 pfn) { return -ENODEV; }
646 static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, u32 asid,
651 static inline int rmp_make_shared(u64 pfn, enum pg_level level) { return -ENODEV; }
652 static inline void snp_leak_pages(u64 pfn, unsigned int npages) {}
653 static inline void kdump_sev_callback(void) { }
654 static inline void snp_fixup_e820_tables(void) {}
655 static inline void sev_evict_cache(void *va, int npages) {}