1 // SPDX-License-Identifier: GPL-2.0
3 * AMD Encrypted Register State Support
9 * misc.h needs to be first because it knows how to include the other kernel
10 * headers in the pre-decompression code in a way that does not break
15 #include <asm/bootparam.h>
16 #include <asm/pgtable_types.h>
18 #include <asm/trapnr.h>
19 #include <asm/trap_pf.h>
20 #include <asm/msr-index.h>
21 #include <asm/fpu/xcr.h>
22 #include <asm/ptrace.h>
24 #include <asm/cpuid.h>
29 static struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
30 struct ghcb *boot_ghcb;
33 * Copy a version of this function here - insn-eval.c can't be used in
34 * pre-decompression code.
36 static bool insn_has_rep_prefix(struct insn *insn)
41 insn_get_prefixes(insn);
43 for_each_insn_prefix(insn, i, p) {
44 if (p == 0xf2 || p == 0xf3)
52 * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
53 * doesn't use segments.
55 static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
60 static inline u64 sev_es_rd_ghcb_msr(void)
64 boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
69 static inline void sev_es_wr_ghcb_msr(u64 val)
74 boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
77 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
79 char buffer[MAX_INSN_SIZE];
82 memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
84 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
86 return ES_DECODE_FAILED;
91 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
92 void *dst, char *buf, size_t size)
94 memcpy(dst, buf, size);
99 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
100 void *src, char *buf, size_t size)
102 memcpy(buf, src, size);
107 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
112 static bool fault_in_kernel_space(unsigned long address)
123 #define __BOOT_COMPRESSED
125 /* Basic instruction decoding support needed */
126 #include "../../lib/inat.c"
127 #include "../../lib/insn.c"
129 /* Include code for early handlers */
130 #include "../../coco/sev/shared.c"
132 static struct svsm_ca *svsm_get_caa(void)
134 return boot_svsm_caa;
137 static u64 svsm_get_caa_pa(void)
139 return boot_svsm_caa_pa;
142 static int svsm_perform_call_protocol(struct svsm_call *call)
153 ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)
154 : svsm_perform_msr_protocol(call);
155 } while (ret == -EAGAIN);
160 bool sev_snp_enabled(void)
162 return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
165 static void __page_state_change(unsigned long paddr, enum psc_op op)
169 if (!sev_snp_enabled())
173 * If private -> shared then invalidate the page before requesting the
174 * state change in the RMP table.
176 if (op == SNP_PAGE_STATE_SHARED)
177 pvalidate_4k_page(paddr, paddr, false);
179 /* Issue VMGEXIT to change the page state in RMP table. */
180 sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
183 /* Read the response of the VMGEXIT. */
184 val = sev_es_rd_ghcb_msr();
185 if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
186 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
189 * Now that page state is changed in the RMP table, validate it so that it is
190 * consistent with the RMP entry.
192 if (op == SNP_PAGE_STATE_PRIVATE)
193 pvalidate_4k_page(paddr, paddr, true);
196 void snp_set_page_private(unsigned long paddr)
198 __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
201 void snp_set_page_shared(unsigned long paddr)
203 __page_state_change(paddr, SNP_PAGE_STATE_SHARED);
206 static bool early_setup_ghcb(void)
208 if (set_page_decrypted((unsigned long)&boot_ghcb_page))
211 /* Page is now mapped decrypted, clear it */
212 memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
214 boot_ghcb = &boot_ghcb_page;
216 /* Initialize lookup tables for the instruction decoder */
219 /* SNP guest requires the GHCB GPA must be registered */
220 if (sev_snp_enabled())
221 snp_register_ghcb_early(__pa(&boot_ghcb_page));
226 static phys_addr_t __snp_accept_memory(struct snp_psc_desc *desc,
227 phys_addr_t pa, phys_addr_t pa_end)
234 memset(hdr, 0, sizeof(*hdr));
239 while (pa < pa_end && i < VMGEXIT_PSC_MAX_ENTRY) {
242 e->gfn = pa >> PAGE_SHIFT;
243 e->operation = SNP_PAGE_STATE_PRIVATE;
244 if (IS_ALIGNED(pa, PMD_SIZE) && (pa_end - pa) >= PMD_SIZE) {
245 e->pagesize = RMP_PG_SIZE_2M;
248 e->pagesize = RMP_PG_SIZE_4K;
256 if (vmgexit_psc(boot_ghcb, desc))
257 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
259 pvalidate_pages(desc);
264 void snp_accept_memory(phys_addr_t start, phys_addr_t end)
266 struct snp_psc_desc desc = {};
270 if (!boot_ghcb && !early_setup_ghcb())
271 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
275 pa = __snp_accept_memory(&desc, pa, end);
278 void sev_es_shutdown_ghcb(void)
283 if (!sev_es_check_cpu_features())
284 error("SEV-ES CPU Features missing.");
287 * This denotes whether to use the GHCB MSR protocol or the GHCB
288 * shared page to perform a GHCB request. Since the GHCB page is
289 * being changed to encrypted, it can't be used to perform GHCB
290 * requests. Clear the boot_ghcb variable so that the GHCB MSR
291 * protocol is used to change the GHCB page over to an encrypted
297 * GHCB Page must be flushed from the cache and mapped encrypted again.
298 * Otherwise the running kernel will see strange cache effects when
299 * trying to use that page.
301 if (set_page_encrypted((unsigned long)&boot_ghcb_page))
302 error("Can't map GHCB page encrypted");
305 * GHCB page is mapped encrypted again and flushed from the cache.
306 * Mark it non-present now to catch bugs when #VC exceptions trigger
309 if (set_page_non_present((unsigned long)&boot_ghcb_page))
310 error("Can't unmap GHCB page");
313 static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
314 unsigned int reason, u64 exit_info_2)
316 u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
318 vc_ghcb_invalidate(ghcb);
319 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
320 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
321 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
323 sev_es_wr_ghcb_msr(__pa(ghcb));
327 asm volatile("hlt\n" : : : "memory");
330 bool sev_es_check_ghcb_fault(unsigned long address)
332 /* Check whether the fault was on the GHCB page */
333 return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
336 void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
338 struct es_em_ctxt ctxt;
339 enum es_result result;
341 if (!boot_ghcb && !early_setup_ghcb())
342 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
344 vc_ghcb_invalidate(boot_ghcb);
345 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
349 result = vc_check_opcode_bytes(&ctxt, exit_code);
355 case SVM_EXIT_RDTSCP:
356 result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
359 result = vc_handle_ioio(boot_ghcb, &ctxt);
362 result = vc_handle_cpuid(boot_ghcb, &ctxt);
365 result = ES_UNSUPPORTED;
371 vc_finish_insn(&ctxt);
372 else if (result != ES_RETRY)
373 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
377 * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
378 * guest side implementation for proper functioning of the guest. If any
379 * of these features are enabled in the hypervisor but are lacking guest
380 * side implementation, the behavior of the guest will be undefined. The
381 * guest could fail in non-obvious way making it difficult to debug.
383 * As the behavior of reserved feature bits is unknown to be on the
384 * safe side add them to the required features mask.
386 #define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \
387 MSR_AMD64_SNP_REFLECT_VC | \
388 MSR_AMD64_SNP_RESTRICTED_INJ | \
389 MSR_AMD64_SNP_ALT_INJ | \
390 MSR_AMD64_SNP_DEBUG_SWAP | \
391 MSR_AMD64_SNP_VMPL_SSS | \
392 MSR_AMD64_SNP_SECURE_TSC | \
393 MSR_AMD64_SNP_VMGEXIT_PARAM | \
394 MSR_AMD64_SNP_VMSA_REG_PROT | \
395 MSR_AMD64_SNP_RESERVED_BIT13 | \
396 MSR_AMD64_SNP_RESERVED_BIT15 | \
397 MSR_AMD64_SNP_RESERVED_MASK)
400 * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
401 * by the guest kernel. As and when a new feature is implemented in the
402 * guest kernel, a corresponding bit should be added to the mask.
404 #define SNP_FEATURES_PRESENT MSR_AMD64_SNP_DEBUG_SWAP
406 u64 snp_get_unsupported_features(u64 status)
408 if (!(status & MSR_AMD64_SEV_SNP_ENABLED))
411 return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
414 void snp_check_features(void)
419 * Terminate the boot if hypervisor has enabled any feature lacking
420 * guest side implementation. Pass on the unsupported features mask through
421 * EXIT_INFO_2 of the GHCB protocol so that those features can be reported
422 * as part of the guest boot failure.
424 unsupported = snp_get_unsupported_features(sev_status);
426 if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
427 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
429 sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
430 GHCB_SNP_UNSUPPORTED, unsupported);
434 /* Search for Confidential Computing blob in the EFI config table. */
435 static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
437 unsigned long cfg_table_pa;
438 unsigned int cfg_table_len;
441 ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
445 return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
451 * Initial set up of SNP relies on information provided by the
452 * Confidential Computing blob, which can be passed to the boot kernel
453 * by firmware/bootloader in the following ways:
455 * - via an entry in the EFI config table
456 * - via a setup_data structure, as defined by the Linux Boot Protocol
458 * Scan for the blob in that order.
460 static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
462 struct cc_blob_sev_info *cc_info;
464 cc_info = find_cc_blob_efi(bp);
468 cc_info = find_cc_blob_setup_data(bp);
473 if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
474 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
480 * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
481 * will verify the SNP CPUID/MSR bits.
483 static bool early_snp_init(struct boot_params *bp)
485 struct cc_blob_sev_info *cc_info;
490 cc_info = find_cc_blob(bp);
495 * If a SNP-specific Confidential Computing blob is present, then
496 * firmware/bootloader have indicated SNP support. Verifying this
497 * involves CPUID checks which will be more reliable if the SNP
498 * CPUID table is used. See comments over snp_setup_cpuid_table() for
501 setup_cpuid_table(cc_info);
504 * Record the SVSM Calling Area (CA) address if the guest is not
505 * running at VMPL0. The CA will be used to communicate with the
506 * SVSM and request its services.
508 svsm_setup_ca(cc_info);
511 * Pass run-time kernel a pointer to CC info via boot_params so EFI
512 * config table doesn't need to be searched again during early startup
515 bp->cc_blob_address = (u32)(unsigned long)cc_info;
521 * sev_check_cpu_support - Check for SEV support in the CPU capabilities
523 * Returns < 0 if SEV is not supported, otherwise the position of the
524 * encryption bit in the page table descriptors.
526 static int sev_check_cpu_support(void)
528 unsigned int eax, ebx, ecx, edx;
530 /* Check for the SME/SEV support leaf */
533 native_cpuid(&eax, &ebx, &ecx, &edx);
534 if (eax < 0x8000001f)
538 * Check for the SME/SEV feature:
539 * CPUID Fn8000_001F[EAX]
540 * - Bit 0 - Secure Memory Encryption support
541 * - Bit 1 - Secure Encrypted Virtualization support
542 * CPUID Fn8000_001F[EBX]
543 * - Bits 5:0 - Pagetable bit position used to indicate encryption
547 native_cpuid(&eax, &ebx, &ecx, &edx);
548 /* Check whether SEV is supported */
555 void sev_enable(struct boot_params *bp)
562 * bp->cc_blob_address should only be set by boot/compressed kernel.
563 * Initialize it to 0 to ensure that uninitialized values from
564 * buggy bootloaders aren't propagated.
567 bp->cc_blob_address = 0;
570 * Do an initial SEV capability check before early_snp_init() which
571 * loads the CPUID page and the same checks afterwards are done
572 * without the hypervisor and are trustworthy.
574 * If the HV fakes SEV support, the guest will crash'n'burn
575 * which is good enough.
578 if (sev_check_cpu_support() < 0)
582 * Setup/preliminary detection of SNP. This will be sanity-checked
583 * against CPUID/MSR values later.
585 snp = early_snp_init(bp);
587 /* Now repeat the checks with the SNP CPUID table. */
589 bitpos = sev_check_cpu_support();
592 error("SEV-SNP support indicated by CC blob, but not CPUID.");
596 /* Set the SME mask if this is an SEV guest. */
597 boot_rdmsr(MSR_AMD64_SEV, &m);
599 if (!(sev_status & MSR_AMD64_SEV_ENABLED))
602 /* Negotiate the GHCB protocol version. */
603 if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
604 if (!sev_es_negotiate_protocol())
605 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
609 * SNP is supported in v2 of the GHCB spec which mandates support for HV
612 if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
616 hv_features = get_hv_features();
617 if (!(hv_features & GHCB_HV_FT_SNP))
618 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
621 * Enforce running at VMPL0 or with an SVSM.
623 * Use RMPADJUST (see the rmpadjust() function for a description of
624 * what the instruction does) to update the VMPL1 permissions of a
625 * page. If the guest is running at VMPL0, this will succeed. If the
626 * guest is running at any other VMPL, this will fail. Linux SNP guests
627 * only ever run at a single VMPL level so permission mask changes of a
628 * lesser-privileged VMPL are a don't-care.
630 ret = rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, 1);
633 * Running at VMPL0 is not required if an SVSM is present and the hypervisor
634 * supports the required SVSM GHCB events.
637 !(snp_vmpl && (hv_features & GHCB_HV_FT_SNP_MULTI_VMPL)))
638 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
641 if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
642 error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
644 sme_me_mask = BIT_ULL(bitpos);
648 * sev_get_status - Retrieve the SEV status mask
650 * Returns 0 if the CPU is not SEV capable, otherwise the value of the
653 u64 sev_get_status(void)
657 if (sev_check_cpu_support() < 0)
660 boot_rdmsr(MSR_AMD64_SEV, &m);
664 void sev_prep_identity_maps(unsigned long top_level_pgt)
667 * The Confidential Computing blob is used very early in uncompressed
668 * kernel to find the in-memory CPUID table to handle CPUID
669 * instructions. Make sure an identity-mapping exists so it can be
670 * accessed after switchover.
672 if (sev_snp_enabled()) {
673 unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;
674 struct cc_blob_sev_info *cc_info;
676 kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
678 cc_info = (struct cc_blob_sev_info *)cc_info_pa;
679 kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
682 sev_verify_cbit(top_level_pgt);