1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Memory Encryption Support
5 * Copyright (C) 2019 SUSE
10 #define pr_fmt(fmt) "SEV: " fmt
12 #include <linux/sched/debug.h> /* For show_regs() */
13 #include <linux/percpu-defs.h>
14 #include <linux/cc_platform.h>
15 #include <linux/printk.h>
16 #include <linux/mm_types.h>
17 #include <linux/set_memory.h>
18 #include <linux/memblock.h>
19 #include <linux/kernel.h>
21 #include <linux/cpumask.h>
22 #include <linux/efi.h>
23 #include <linux/platform_device.h>
25 #include <linux/psp-sev.h>
26 #include <linux/dmi.h>
27 #include <uapi/linux/sev-guest.h>
30 #include <asm/cpu_entry_area.h>
31 #include <asm/stacktrace.h>
33 #include <asm/insn-eval.h>
34 #include <asm/fpu/xcr.h>
35 #include <asm/processor.h>
36 #include <asm/realmode.h>
37 #include <asm/setup.h>
38 #include <asm/traps.h>
43 #include <asm/cpuid.h>
44 #include <asm/cmdline.h>
46 #define DR7_RESET_VALUE 0x400
48 /* AP INIT values as documented in the APM2 section "Processor Initialization State" */
49 #define AP_INIT_CS_LIMIT 0xffff
50 #define AP_INIT_DS_LIMIT 0xffff
51 #define AP_INIT_LDTR_LIMIT 0xffff
52 #define AP_INIT_GDTR_LIMIT 0xffff
53 #define AP_INIT_IDTR_LIMIT 0xffff
54 #define AP_INIT_TR_LIMIT 0xffff
55 #define AP_INIT_RFLAGS_DEFAULT 0x2
56 #define AP_INIT_DR6_DEFAULT 0xffff0ff0
57 #define AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL
58 #define AP_INIT_XCR0_DEFAULT 0x1
59 #define AP_INIT_X87_FTW_DEFAULT 0x5555
60 #define AP_INIT_X87_FCW_DEFAULT 0x0040
61 #define AP_INIT_CR0_DEFAULT 0x60000010
62 #define AP_INIT_MXCSR_DEFAULT 0x1f80
64 static const char * const sev_status_feat_names[] = {
65 [MSR_AMD64_SEV_ENABLED_BIT] = "SEV",
66 [MSR_AMD64_SEV_ES_ENABLED_BIT] = "SEV-ES",
67 [MSR_AMD64_SEV_SNP_ENABLED_BIT] = "SEV-SNP",
68 [MSR_AMD64_SNP_VTOM_BIT] = "vTom",
69 [MSR_AMD64_SNP_REFLECT_VC_BIT] = "ReflectVC",
70 [MSR_AMD64_SNP_RESTRICTED_INJ_BIT] = "RI",
71 [MSR_AMD64_SNP_ALT_INJ_BIT] = "AI",
72 [MSR_AMD64_SNP_DEBUG_SWAP_BIT] = "DebugSwap",
73 [MSR_AMD64_SNP_PREVENT_HOST_IBS_BIT] = "NoHostIBS",
74 [MSR_AMD64_SNP_BTB_ISOLATION_BIT] = "BTBIsol",
75 [MSR_AMD64_SNP_VMPL_SSS_BIT] = "VmplSSS",
76 [MSR_AMD64_SNP_SECURE_TSC_BIT] = "SecureTSC",
77 [MSR_AMD64_SNP_VMGEXIT_PARAM_BIT] = "VMGExitParam",
78 [MSR_AMD64_SNP_IBS_VIRT_BIT] = "IBSVirt",
79 [MSR_AMD64_SNP_VMSA_REG_PROT_BIT] = "VMSARegProt",
80 [MSR_AMD64_SNP_SMT_PROT_BIT] = "SMTProt",
83 /* For early boot hypervisor communication in SEV-ES enabled guests */
84 static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
87 * Needs to be in the .data section because we need it NULL before bss is
90 static struct ghcb *boot_ghcb __section(".data");
92 /* Bitmap of SEV features supported by the hypervisor */
93 static u64 sev_hv_features __ro_after_init;
95 /* Secrets page physical address from the CC blob */
96 static u64 secrets_pa __ro_after_init;
98 /* #VC handler runtime per-CPU data */
99 struct sev_es_runtime_data {
100 struct ghcb ghcb_page;
103 * Reserve one page per CPU as backup storage for the unencrypted GHCB.
104 * It is needed when an NMI happens while the #VC handler uses the real
105 * GHCB, and the NMI handler itself is causing another #VC exception. In
106 * that case the GHCB content of the first handler needs to be backed up
109 struct ghcb backup_ghcb;
112 * Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions.
113 * There is no need for it to be atomic, because nothing is written to
114 * the GHCB between the read and the write of ghcb_active. So it is safe
115 * to use it when a nested #VC exception happens before the write.
117 * This is necessary for example in the #VC->NMI->#VC case when the NMI
118 * happens while the first #VC handler uses the GHCB. When the NMI code
119 * raises a second #VC handler it might overwrite the contents of the
120 * GHCB written by the first handler. To avoid this the content of the
121 * GHCB is saved and restored when the GHCB is detected to be in use
125 bool backup_ghcb_active;
128 * Cached DR7 value - write it on DR7 writes and return it on reads.
129 * That value will never make it to the real hardware DR7 as debugging
130 * is currently unsupported in SEV-ES guests.
139 /* For early boot SVSM communication */
140 static struct svsm_ca boot_svsm_ca_page __aligned(PAGE_SIZE);
142 static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
143 static DEFINE_PER_CPU(struct sev_es_save_area *, sev_vmsa);
144 static DEFINE_PER_CPU(struct svsm_ca *, svsm_caa);
145 static DEFINE_PER_CPU(u64, svsm_caa_pa);
147 static __always_inline bool on_vc_stack(struct pt_regs *regs)
149 unsigned long sp = regs->sp;
151 /* User-mode RSP is not trusted */
155 /* SYSCALL gap still has user-mode RSP */
156 if (ip_within_syscall_gap(regs))
159 return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
163 * This function handles the case when an NMI is raised in the #VC
164 * exception handler entry code, before the #VC handler has switched off
165 * its IST stack. In this case, the IST entry for #VC must be adjusted,
166 * so that any nested #VC exception will not overwrite the stack
167 * contents of the interrupted #VC handler.
169 * The IST entry is adjusted unconditionally so that it can be also be
170 * unconditionally adjusted back in __sev_es_ist_exit(). Otherwise a
171 * nested sev_es_ist_exit() call may adjust back the IST entry too
174 * The __sev_es_ist_enter() and __sev_es_ist_exit() functions always run
175 * on the NMI IST stack, as they are only called from NMI handling code
178 void noinstr __sev_es_ist_enter(struct pt_regs *regs)
180 unsigned long old_ist, new_ist;
182 /* Read old IST entry */
183 new_ist = old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
186 * If NMI happened while on the #VC IST stack, set the new IST
187 * value below regs->sp, so that the interrupted stack frame is
188 * not overwritten by subsequent #VC exceptions.
190 if (on_vc_stack(regs))
194 * Reserve additional 8 bytes and store old IST value so this
195 * adjustment can be unrolled in __sev_es_ist_exit().
197 new_ist -= sizeof(old_ist);
198 *(unsigned long *)new_ist = old_ist;
200 /* Set new IST entry */
201 this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], new_ist);
204 void noinstr __sev_es_ist_exit(void)
209 ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
211 if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
214 /* Read back old IST entry and write it to the TSS */
215 this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist);
219 * Nothing shall interrupt this code path while holding the per-CPU
220 * GHCB. The backup GHCB is only for NMIs interrupting this path.
222 * Callers must disable local interrupts around it.
224 static noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)
226 struct sev_es_runtime_data *data;
229 WARN_ON(!irqs_disabled());
231 data = this_cpu_read(runtime_data);
232 ghcb = &data->ghcb_page;
234 if (unlikely(data->ghcb_active)) {
235 /* GHCB is already in use - save its contents */
237 if (unlikely(data->backup_ghcb_active)) {
239 * Backup-GHCB is also already in use. There is no way
240 * to continue here so just kill the machine. To make
241 * panic() work, mark GHCBs inactive so that messages
242 * can be printed out.
244 data->ghcb_active = false;
245 data->backup_ghcb_active = false;
247 instrumentation_begin();
248 panic("Unable to handle #VC exception! GHCB and Backup GHCB are already in use");
249 instrumentation_end();
252 /* Mark backup_ghcb active before writing to it */
253 data->backup_ghcb_active = true;
255 state->ghcb = &data->backup_ghcb;
257 /* Backup GHCB content */
258 *state->ghcb = *ghcb;
261 data->ghcb_active = true;
267 static inline u64 sev_es_rd_ghcb_msr(void)
269 return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
272 static __always_inline void sev_es_wr_ghcb_msr(u64 val)
277 high = (u32)(val >> 32);
279 native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
282 static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
283 unsigned char *buffer)
285 return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
288 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
290 char buffer[MAX_INSN_SIZE];
293 insn_bytes = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
294 if (insn_bytes == 0) {
295 /* Nothing could be copied */
296 ctxt->fi.vector = X86_TRAP_PF;
297 ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
298 ctxt->fi.cr2 = ctxt->regs->ip;
300 } else if (insn_bytes == -EINVAL) {
301 /* Effective RIP could not be calculated */
302 ctxt->fi.vector = X86_TRAP_GP;
303 ctxt->fi.error_code = 0;
308 if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, insn_bytes))
309 return ES_DECODE_FAILED;
311 if (ctxt->insn.immediate.got)
314 return ES_DECODE_FAILED;
317 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
319 char buffer[MAX_INSN_SIZE];
322 res = vc_fetch_insn_kernel(ctxt, buffer);
324 ctxt->fi.vector = X86_TRAP_PF;
325 ctxt->fi.error_code = X86_PF_INSTR;
326 ctxt->fi.cr2 = ctxt->regs->ip;
330 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
332 return ES_DECODE_FAILED;
337 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
339 if (user_mode(ctxt->regs))
340 return __vc_decode_user_insn(ctxt);
342 return __vc_decode_kern_insn(ctxt);
345 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
346 char *dst, char *buf, size_t size)
348 unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
351 * This function uses __put_user() independent of whether kernel or user
352 * memory is accessed. This works fine because __put_user() does no
353 * sanity checks of the pointer being accessed. All that it does is
354 * to report when the access failed.
356 * Also, this function runs in atomic context, so __put_user() is not
357 * allowed to sleep. The page-fault handler detects that it is running
358 * in atomic context and will not try to take mmap_sem and handle the
359 * fault, so additional pagefault_enable()/disable() calls are not
362 * The access can't be done via copy_to_user() here because
363 * vc_write_mem() must not use string instructions to access unsafe
364 * memory. The reason is that MOVS is emulated by the #VC handler by
365 * splitting the move up into a read and a write and taking a nested #VC
366 * exception on whatever of them is the MMIO access. Using string
367 * instructions here would cause infinite nesting.
372 u8 __user *target = (u8 __user *)dst;
375 if (__put_user(d1, target))
381 u16 __user *target = (u16 __user *)dst;
384 if (__put_user(d2, target))
390 u32 __user *target = (u32 __user *)dst;
393 if (__put_user(d4, target))
399 u64 __user *target = (u64 __user *)dst;
402 if (__put_user(d8, target))
407 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
408 return ES_UNSUPPORTED;
414 if (user_mode(ctxt->regs))
415 error_code |= X86_PF_USER;
417 ctxt->fi.vector = X86_TRAP_PF;
418 ctxt->fi.error_code = error_code;
419 ctxt->fi.cr2 = (unsigned long)dst;
424 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
425 char *src, char *buf, size_t size)
427 unsigned long error_code = X86_PF_PROT;
430 * This function uses __get_user() independent of whether kernel or user
431 * memory is accessed. This works fine because __get_user() does no
432 * sanity checks of the pointer being accessed. All that it does is
433 * to report when the access failed.
435 * Also, this function runs in atomic context, so __get_user() is not
436 * allowed to sleep. The page-fault handler detects that it is running
437 * in atomic context and will not try to take mmap_sem and handle the
438 * fault, so additional pagefault_enable()/disable() calls are not
441 * The access can't be done via copy_from_user() here because
442 * vc_read_mem() must not use string instructions to access unsafe
443 * memory. The reason is that MOVS is emulated by the #VC handler by
444 * splitting the move up into a read and a write and taking a nested #VC
445 * exception on whatever of them is the MMIO access. Using string
446 * instructions here would cause infinite nesting.
451 u8 __user *s = (u8 __user *)src;
453 if (__get_user(d1, s))
460 u16 __user *s = (u16 __user *)src;
462 if (__get_user(d2, s))
469 u32 __user *s = (u32 __user *)src;
471 if (__get_user(d4, s))
478 u64 __user *s = (u64 __user *)src;
479 if (__get_user(d8, s))
485 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
486 return ES_UNSUPPORTED;
492 if (user_mode(ctxt->regs))
493 error_code |= X86_PF_USER;
495 ctxt->fi.vector = X86_TRAP_PF;
496 ctxt->fi.error_code = error_code;
497 ctxt->fi.cr2 = (unsigned long)src;
502 static enum es_result vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
503 unsigned long vaddr, phys_addr_t *paddr)
505 unsigned long va = (unsigned long)vaddr;
511 pgd = __va(read_cr3_pa());
512 pgd = &pgd[pgd_index(va)];
513 pte = lookup_address_in_pgd(pgd, va, &level);
515 ctxt->fi.vector = X86_TRAP_PF;
516 ctxt->fi.cr2 = vaddr;
517 ctxt->fi.error_code = 0;
519 if (user_mode(ctxt->regs))
520 ctxt->fi.error_code |= X86_PF_USER;
525 if (WARN_ON_ONCE(pte_val(*pte) & _PAGE_ENC))
526 /* Emulated MMIO to/from encrypted memory not supported */
527 return ES_UNSUPPORTED;
529 pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
530 pa |= va & ~page_level_mask(level);
537 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
541 if (user_mode(ctxt->regs)) {
542 struct thread_struct *t = ¤t->thread;
543 struct io_bitmap *iobm = t->io_bitmap;
549 for (idx = port; idx < port + size; ++idx) {
550 if (test_bit(idx, iobm->bitmap))
558 ctxt->fi.vector = X86_TRAP_GP;
559 ctxt->fi.error_code = 0;
564 static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
566 long error_code = ctxt->fi.error_code;
567 int trapnr = ctxt->fi.vector;
569 ctxt->regs->orig_ax = ctxt->fi.error_code;
573 exc_general_protection(ctxt->regs, error_code);
576 exc_invalid_op(ctxt->regs);
579 write_cr2(ctxt->fi.cr2);
580 exc_page_fault(ctxt->regs, error_code);
583 exc_alignment_check(ctxt->regs, error_code);
586 pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
591 /* Include code shared with pre-decompression boot stage */
594 static inline struct svsm_ca *svsm_get_caa(void)
597 * Use rIP-relative references when called early in the boot. If
598 * ->use_cas is set, then it is late in the boot and no need
599 * to worry about rIP-relative references.
601 if (RIP_REL_REF(sev_cfg).use_cas)
602 return this_cpu_read(svsm_caa);
604 return RIP_REL_REF(boot_svsm_caa);
607 static u64 svsm_get_caa_pa(void)
610 * Use rIP-relative references when called early in the boot. If
611 * ->use_cas is set, then it is late in the boot and no need
612 * to worry about rIP-relative references.
614 if (RIP_REL_REF(sev_cfg).use_cas)
615 return this_cpu_read(svsm_caa_pa);
617 return RIP_REL_REF(boot_svsm_caa_pa);
620 static noinstr void __sev_put_ghcb(struct ghcb_state *state)
622 struct sev_es_runtime_data *data;
625 WARN_ON(!irqs_disabled());
627 data = this_cpu_read(runtime_data);
628 ghcb = &data->ghcb_page;
631 /* Restore GHCB from Backup */
632 *ghcb = *state->ghcb;
633 data->backup_ghcb_active = false;
637 * Invalidate the GHCB so a VMGEXIT instruction issued
638 * from userspace won't appear to be valid.
640 vc_ghcb_invalidate(ghcb);
641 data->ghcb_active = false;
645 static int svsm_perform_call_protocol(struct svsm_call *call)
647 struct ghcb_state state;
653 * This can be called very early in the boot, use native functions in
654 * order to avoid paravirt issues.
656 flags = native_local_irq_save();
659 * Use rip-relative references when called early in the boot. If
660 * ghcbs_initialized is set, then it is late in the boot and no need
661 * to worry about rip-relative references in called functions.
663 if (RIP_REL_REF(sev_cfg).ghcbs_initialized)
664 ghcb = __sev_get_ghcb(&state);
665 else if (RIP_REL_REF(boot_ghcb))
666 ghcb = RIP_REL_REF(boot_ghcb);
671 ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)
672 : svsm_perform_msr_protocol(call);
673 } while (ret == -EAGAIN);
675 if (RIP_REL_REF(sev_cfg).ghcbs_initialized)
676 __sev_put_ghcb(&state);
678 native_local_irq_restore(flags);
683 void noinstr __sev_es_nmi_complete(void)
685 struct ghcb_state state;
688 ghcb = __sev_get_ghcb(&state);
690 vc_ghcb_invalidate(ghcb);
691 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
692 ghcb_set_sw_exit_info_1(ghcb, 0);
693 ghcb_set_sw_exit_info_2(ghcb, 0);
695 sev_es_wr_ghcb_msr(__pa_nodebug(ghcb));
698 __sev_put_ghcb(&state);
701 static u64 __init get_snp_jump_table_addr(void)
703 struct snp_secrets_page *secrets;
707 mem = ioremap_encrypted(secrets_pa, PAGE_SIZE);
709 pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n");
713 secrets = (__force struct snp_secrets_page *)mem;
715 addr = secrets->os_area.ap_jump_table_pa;
721 static u64 __init get_jump_table_addr(void)
723 struct ghcb_state state;
728 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
729 return get_snp_jump_table_addr();
731 local_irq_save(flags);
733 ghcb = __sev_get_ghcb(&state);
735 vc_ghcb_invalidate(ghcb);
736 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_JUMP_TABLE);
737 ghcb_set_sw_exit_info_1(ghcb, SVM_VMGEXIT_GET_AP_JUMP_TABLE);
738 ghcb_set_sw_exit_info_2(ghcb, 0);
740 sev_es_wr_ghcb_msr(__pa(ghcb));
743 if (ghcb_sw_exit_info_1_is_valid(ghcb) &&
744 ghcb_sw_exit_info_2_is_valid(ghcb))
745 ret = ghcb->save.sw_exit_info_2;
747 __sev_put_ghcb(&state);
749 local_irq_restore(flags);
755 early_set_pages_state(unsigned long vaddr, unsigned long paddr,
756 unsigned long npages, enum psc_op op)
758 unsigned long paddr_end;
761 vaddr = vaddr & PAGE_MASK;
763 paddr = paddr & PAGE_MASK;
764 paddr_end = paddr + (npages << PAGE_SHIFT);
766 while (paddr < paddr_end) {
767 /* Page validation must be rescinded before changing to shared */
768 if (op == SNP_PAGE_STATE_SHARED)
769 pvalidate_4k_page(vaddr, paddr, false);
772 * Use the MSR protocol because this function can be called before
773 * the GHCB is established.
775 sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
778 val = sev_es_rd_ghcb_msr();
780 if (WARN(GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP,
781 "Wrong PSC response code: 0x%x\n",
782 (unsigned int)GHCB_RESP_CODE(val)))
785 if (WARN(GHCB_MSR_PSC_RESP_VAL(val),
786 "Failed to change page state to '%s' paddr 0x%lx error 0x%llx\n",
787 op == SNP_PAGE_STATE_PRIVATE ? "private" : "shared",
788 paddr, GHCB_MSR_PSC_RESP_VAL(val)))
791 /* Page validation must be performed after changing to private */
792 if (op == SNP_PAGE_STATE_PRIVATE)
793 pvalidate_4k_page(vaddr, paddr, true);
802 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
805 void __head early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
806 unsigned long npages)
809 * This can be invoked in early boot while running identity mapped, so
810 * use an open coded check for SNP instead of using cc_platform_has().
811 * This eliminates worries about jump tables or checking boot_cpu_data
812 * in the cc_platform_has() function.
814 if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
818 * Ask the hypervisor to mark the memory pages as private in the RMP
821 early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_PRIVATE);
824 void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
825 unsigned long npages)
828 * This can be invoked in early boot while running identity mapped, so
829 * use an open coded check for SNP instead of using cc_platform_has().
830 * This eliminates worries about jump tables or checking boot_cpu_data
831 * in the cc_platform_has() function.
833 if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
836 /* Ask hypervisor to mark the memory pages shared in the RMP table. */
837 early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_SHARED);
840 static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr,
841 unsigned long vaddr_end, int op)
843 struct ghcb_state state;
844 bool use_large_entry;
855 memset(data, 0, sizeof(*data));
858 while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) {
861 if (is_vmalloc_addr((void *)vaddr)) {
862 pfn = vmalloc_to_pfn((void *)vaddr);
863 use_large_entry = false;
865 pfn = __pa(vaddr) >> PAGE_SHIFT;
866 use_large_entry = true;
872 if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) &&
873 (vaddr_end - vaddr) >= PMD_SIZE) {
874 e->pagesize = RMP_PG_SIZE_2M;
877 e->pagesize = RMP_PG_SIZE_4K;
885 /* Page validation must be rescinded before changing to shared */
886 if (op == SNP_PAGE_STATE_SHARED)
887 pvalidate_pages(data);
889 local_irq_save(flags);
891 if (sev_cfg.ghcbs_initialized)
892 ghcb = __sev_get_ghcb(&state);
896 /* Invoke the hypervisor to perform the page state changes */
897 if (!ghcb || vmgexit_psc(ghcb, data))
898 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
900 if (sev_cfg.ghcbs_initialized)
901 __sev_put_ghcb(&state);
903 local_irq_restore(flags);
905 /* Page validation must be performed after changing to private */
906 if (op == SNP_PAGE_STATE_PRIVATE)
907 pvalidate_pages(data);
912 static void set_pages_state(unsigned long vaddr, unsigned long npages, int op)
914 struct snp_psc_desc desc;
915 unsigned long vaddr_end;
917 /* Use the MSR protocol when a GHCB is not available. */
919 return early_set_pages_state(vaddr, __pa(vaddr), npages, op);
921 vaddr = vaddr & PAGE_MASK;
922 vaddr_end = vaddr + (npages << PAGE_SHIFT);
924 while (vaddr < vaddr_end)
925 vaddr = __set_pages_state(&desc, vaddr, vaddr_end, op);
928 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages)
930 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
933 set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED);
936 void snp_set_memory_private(unsigned long vaddr, unsigned long npages)
938 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
941 set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
944 void snp_accept_memory(phys_addr_t start, phys_addr_t end)
946 unsigned long vaddr, npages;
948 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
951 vaddr = (unsigned long)__va(start);
952 npages = (end - start) >> PAGE_SHIFT;
954 set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
957 static void set_pte_enc(pte_t *kpte, int level, void *va)
959 struct pte_enc_desc d = {
967 set_pte_enc_mask(kpte, d.pfn, d.new_pgprot);
970 static void unshare_all_memory(void)
972 unsigned long addr, end, size, ghcb;
973 struct sev_es_runtime_data *data;
974 unsigned int npages, level;
979 /* Unshare the direct mapping. */
981 end = PAGE_OFFSET + get_max_mapped();
984 pte = lookup_address(addr, &level);
985 size = page_level_size(level);
986 npages = size / PAGE_SIZE;
987 skipped_addr = false;
989 if (!pte || !pte_decrypted(*pte) || pte_none(*pte)) {
995 * Ensure that all the per-CPU GHCBs are made private at the
996 * end of the unsharing loop so that the switch to the slower
997 * MSR protocol happens last.
999 for_each_possible_cpu(cpu) {
1000 data = per_cpu(runtime_data, cpu);
1001 ghcb = (unsigned long)&data->ghcb_page;
1003 if (addr <= ghcb && ghcb <= addr + size) {
1004 skipped_addr = true;
1009 if (!skipped_addr) {
1010 set_pte_enc(pte, level, (void *)addr);
1011 snp_set_memory_private(addr, npages);
1016 /* Unshare all bss decrypted memory. */
1017 addr = (unsigned long)__start_bss_decrypted;
1018 end = (unsigned long)__start_bss_decrypted_unused;
1019 npages = (end - addr) >> PAGE_SHIFT;
1021 for (; addr < end; addr += PAGE_SIZE) {
1022 pte = lookup_address(addr, &level);
1023 if (!pte || !pte_decrypted(*pte) || pte_none(*pte))
1026 set_pte_enc(pte, level, (void *)addr);
1028 addr = (unsigned long)__start_bss_decrypted;
1029 snp_set_memory_private(addr, npages);
1034 /* Stop new private<->shared conversions */
1035 void snp_kexec_begin(void)
1037 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1040 if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1044 * Crash kernel ends up here with interrupts disabled: can't wait for
1045 * conversions to finish.
1047 * If race happened, just report and proceed.
1049 if (!set_memory_enc_stop_conversion())
1050 pr_warn("Failed to stop shared<->private conversions\n");
1053 void snp_kexec_finish(void)
1055 struct sev_es_runtime_data *data;
1056 unsigned int level, cpu;
1061 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1064 if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1067 unshare_all_memory();
1070 * Switch to using the MSR protocol to change per-CPU GHCBs to
1071 * private. All the per-CPU GHCBs have been switched back to private,
1072 * so can't do any more GHCB calls to the hypervisor beyond this point
1073 * until the kexec'ed kernel starts running.
1076 sev_cfg.ghcbs_initialized = false;
1078 for_each_possible_cpu(cpu) {
1079 data = per_cpu(runtime_data, cpu);
1080 ghcb = &data->ghcb_page;
1081 pte = lookup_address((unsigned long)ghcb, &level);
1082 size = page_level_size(level);
1083 set_pte_enc(pte, level, (void *)ghcb);
1084 snp_set_memory_private((unsigned long)ghcb, (size / PAGE_SIZE));
1088 static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa)
1093 struct svsm_call call = {};
1094 unsigned long flags;
1096 local_irq_save(flags);
1098 call.caa = this_cpu_read(svsm_caa);
1099 call.rcx = __pa(va);
1102 /* Protocol 0, Call ID 2 */
1103 call.rax = SVSM_CORE_CALL(SVSM_CORE_CREATE_VCPU);
1104 call.rdx = __pa(caa);
1107 /* Protocol 0, Call ID 3 */
1108 call.rax = SVSM_CORE_CALL(SVSM_CORE_DELETE_VCPU);
1111 ret = svsm_perform_call_protocol(&call);
1113 local_irq_restore(flags);
1116 * If the kernel runs at VMPL0, it can change the VMSA
1117 * bit for a page using the RMPADJUST instruction.
1118 * However, for the instruction to succeed it must
1119 * target the permissions of a lesser privileged (higher
1120 * numbered) VMPL level, so use VMPL1.
1125 attrs |= RMPADJUST_VMSA_PAGE_BIT;
1127 ret = rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs);
1133 #define __ATTR_BASE (SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK)
1134 #define INIT_CS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK)
1135 #define INIT_DS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_WRITE_MASK)
1137 #define INIT_LDTR_ATTRIBS (SVM_SELECTOR_P_MASK | 2)
1138 #define INIT_TR_ATTRIBS (SVM_SELECTOR_P_MASK | 3)
1140 static void *snp_alloc_vmsa_page(int cpu)
1145 * Allocate VMSA page to work around the SNP erratum where the CPU will
1146 * incorrectly signal an RMP violation #PF if a large page (2MB or 1GB)
1147 * collides with the RMP entry of VMSA page. The recommended workaround
1148 * is to not use a large page.
1150 * Allocate an 8k page which is also 8k-aligned.
1152 p = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1);
1158 /* Free the first 4k. This page may be 2M/1G aligned and cannot be used. */
1161 return page_address(p + 1);
1164 static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa, int apic_id)
1168 err = snp_set_vmsa(vmsa, NULL, apic_id, false);
1170 pr_err("clear VMSA page failed (%u), leaking page\n", err);
1172 free_page((unsigned long)vmsa);
1175 static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
1177 struct sev_es_save_area *cur_vmsa, *vmsa;
1178 struct ghcb_state state;
1179 struct svsm_ca *caa;
1180 unsigned long flags;
1187 * The hypervisor SNP feature support check has happened earlier, just check
1188 * the AP_CREATION one here.
1190 if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION))
1194 * Verify the desired start IP against the known trampoline start IP
1195 * to catch any future new trampolines that may be introduced that
1196 * would require a new protected guest entry point.
1198 if (WARN_ONCE(start_ip != real_mode_header->trampoline_start,
1199 "Unsupported SNP start_ip: %lx\n", start_ip))
1202 /* Override start_ip with known protected guest start IP */
1203 start_ip = real_mode_header->sev_es_trampoline_start;
1205 /* Find the logical CPU for the APIC ID */
1206 for_each_present_cpu(cpu) {
1207 if (arch_match_cpu_phys_id(cpu, apic_id))
1210 if (cpu >= nr_cpu_ids)
1213 cur_vmsa = per_cpu(sev_vmsa, cpu);
1216 * A new VMSA is created each time because there is no guarantee that
1217 * the current VMSA is the kernels or that the vCPU is not running. If
1218 * an attempt was done to use the current VMSA with a running vCPU, a
1219 * #VMEXIT of that vCPU would wipe out all of the settings being done
1222 vmsa = (struct sev_es_save_area *)snp_alloc_vmsa_page(cpu);
1226 /* If an SVSM is present, the SVSM per-CPU CAA will be !NULL */
1227 caa = per_cpu(svsm_caa, cpu);
1229 /* CR4 should maintain the MCE value */
1230 cr4 = native_read_cr4() & X86_CR4_MCE;
1232 /* Set the CS value based on the start_ip converted to a SIPI vector */
1233 sipi_vector = (start_ip >> 12);
1234 vmsa->cs.base = sipi_vector << 12;
1235 vmsa->cs.limit = AP_INIT_CS_LIMIT;
1236 vmsa->cs.attrib = INIT_CS_ATTRIBS;
1237 vmsa->cs.selector = sipi_vector << 8;
1239 /* Set the RIP value based on start_ip */
1240 vmsa->rip = start_ip & 0xfff;
1242 /* Set AP INIT defaults as documented in the APM */
1243 vmsa->ds.limit = AP_INIT_DS_LIMIT;
1244 vmsa->ds.attrib = INIT_DS_ATTRIBS;
1245 vmsa->es = vmsa->ds;
1246 vmsa->fs = vmsa->ds;
1247 vmsa->gs = vmsa->ds;
1248 vmsa->ss = vmsa->ds;
1250 vmsa->gdtr.limit = AP_INIT_GDTR_LIMIT;
1251 vmsa->ldtr.limit = AP_INIT_LDTR_LIMIT;
1252 vmsa->ldtr.attrib = INIT_LDTR_ATTRIBS;
1253 vmsa->idtr.limit = AP_INIT_IDTR_LIMIT;
1254 vmsa->tr.limit = AP_INIT_TR_LIMIT;
1255 vmsa->tr.attrib = INIT_TR_ATTRIBS;
1258 vmsa->cr0 = AP_INIT_CR0_DEFAULT;
1259 vmsa->dr7 = DR7_RESET_VALUE;
1260 vmsa->dr6 = AP_INIT_DR6_DEFAULT;
1261 vmsa->rflags = AP_INIT_RFLAGS_DEFAULT;
1262 vmsa->g_pat = AP_INIT_GPAT_DEFAULT;
1263 vmsa->xcr0 = AP_INIT_XCR0_DEFAULT;
1264 vmsa->mxcsr = AP_INIT_MXCSR_DEFAULT;
1265 vmsa->x87_ftw = AP_INIT_X87_FTW_DEFAULT;
1266 vmsa->x87_fcw = AP_INIT_X87_FCW_DEFAULT;
1268 /* SVME must be set. */
1269 vmsa->efer = EFER_SVME;
1272 * Set the SNP-specific fields for this VMSA:
1274 * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits)
1276 vmsa->vmpl = snp_vmpl;
1277 vmsa->sev_features = sev_status >> 2;
1279 /* Switch the page over to a VMSA page now that it is initialized */
1280 ret = snp_set_vmsa(vmsa, caa, apic_id, true);
1282 pr_err("set VMSA page failed (%u)\n", ret);
1283 free_page((unsigned long)vmsa);
1288 /* Issue VMGEXIT AP Creation NAE event */
1289 local_irq_save(flags);
1291 ghcb = __sev_get_ghcb(&state);
1293 vc_ghcb_invalidate(ghcb);
1294 ghcb_set_rax(ghcb, vmsa->sev_features);
1295 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_CREATION);
1296 ghcb_set_sw_exit_info_1(ghcb,
1297 ((u64)apic_id << 32) |
1298 ((u64)snp_vmpl << 16) |
1299 SVM_VMGEXIT_AP_CREATE);
1300 ghcb_set_sw_exit_info_2(ghcb, __pa(vmsa));
1302 sev_es_wr_ghcb_msr(__pa(ghcb));
1305 if (!ghcb_sw_exit_info_1_is_valid(ghcb) ||
1306 lower_32_bits(ghcb->save.sw_exit_info_1)) {
1307 pr_err("SNP AP Creation error\n");
1311 __sev_put_ghcb(&state);
1313 local_irq_restore(flags);
1315 /* Perform cleanup if there was an error */
1317 snp_cleanup_vmsa(vmsa, apic_id);
1321 /* Free up any previous VMSA page */
1323 snp_cleanup_vmsa(cur_vmsa, apic_id);
1325 /* Record the current VMSA page */
1326 per_cpu(sev_vmsa, cpu) = vmsa;
1331 void __init snp_set_wakeup_secondary_cpu(void)
1333 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1337 * Always set this override if SNP is enabled. This makes it the
1338 * required method to start APs under SNP. If the hypervisor does
1339 * not support AP creation, then no APs will be started.
1341 apic_update_callback(wakeup_secondary_cpu, wakeup_cpu_via_vmgexit);
1344 int __init sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
1346 u16 startup_cs, startup_ip;
1347 phys_addr_t jump_table_pa;
1348 u64 jump_table_addr;
1349 u16 __iomem *jump_table;
1351 jump_table_addr = get_jump_table_addr();
1353 /* On UP guests there is no jump table so this is not a failure */
1354 if (!jump_table_addr)
1357 /* Check if AP Jump Table is page-aligned */
1358 if (jump_table_addr & ~PAGE_MASK)
1361 jump_table_pa = jump_table_addr & PAGE_MASK;
1363 startup_cs = (u16)(rmh->trampoline_start >> 4);
1364 startup_ip = (u16)(rmh->sev_es_trampoline_start -
1365 rmh->trampoline_start);
1367 jump_table = ioremap_encrypted(jump_table_pa, PAGE_SIZE);
1371 writew(startup_ip, &jump_table[0]);
1372 writew(startup_cs, &jump_table[1]);
1374 iounmap(jump_table);
1380 * This is needed by the OVMF UEFI firmware which will use whatever it finds in
1381 * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
1382 * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
1384 int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
1386 struct sev_es_runtime_data *data;
1387 unsigned long address, pflags;
1391 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1394 pflags = _PAGE_NX | _PAGE_RW;
1396 for_each_possible_cpu(cpu) {
1397 data = per_cpu(runtime_data, cpu);
1399 address = __pa(&data->ghcb_page);
1400 pfn = address >> PAGE_SHIFT;
1402 if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
1409 /* Writes to the SVSM CAA MSR are ignored */
1410 static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
1415 regs->ax = lower_32_bits(this_cpu_read(svsm_caa_pa));
1416 regs->dx = upper_32_bits(this_cpu_read(svsm_caa_pa));
1421 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1423 struct pt_regs *regs = ctxt->regs;
1427 /* Is it a WRMSR? */
1428 write = ctxt->insn.opcode.bytes[1] == 0x30;
1430 if (regs->cx == MSR_SVSM_CAA)
1431 return __vc_handle_msr_caa(regs, write);
1433 ghcb_set_rcx(ghcb, regs->cx);
1435 ghcb_set_rax(ghcb, regs->ax);
1436 ghcb_set_rdx(ghcb, regs->dx);
1439 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, write, 0);
1441 if ((ret == ES_OK) && !write) {
1442 regs->ax = ghcb->save.rax;
1443 regs->dx = ghcb->save.rdx;
1449 static void snp_register_per_cpu_ghcb(void)
1451 struct sev_es_runtime_data *data;
1454 data = this_cpu_read(runtime_data);
1455 ghcb = &data->ghcb_page;
1457 snp_register_ghcb_early(__pa(ghcb));
1460 void setup_ghcb(void)
1462 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1466 * Check whether the runtime #VC exception handler is active. It uses
1467 * the per-CPU GHCB page which is set up by sev_es_init_vc_handling().
1469 * If SNP is active, register the per-CPU GHCB page so that the runtime
1470 * exception handler can use it.
1472 if (initial_vc_handler == (unsigned long)kernel_exc_vmm_communication) {
1473 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1474 snp_register_per_cpu_ghcb();
1476 sev_cfg.ghcbs_initialized = true;
1482 * Make sure the hypervisor talks a supported protocol.
1483 * This gets called only in the BSP boot phase.
1485 if (!sev_es_negotiate_protocol())
1486 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
1489 * Clear the boot_ghcb. The first exception comes in before the bss
1490 * section is cleared.
1492 memset(&boot_ghcb_page, 0, PAGE_SIZE);
1494 /* Alright - Make the boot-ghcb public */
1495 boot_ghcb = &boot_ghcb_page;
1497 /* SNP guest requires that GHCB GPA must be registered. */
1498 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1499 snp_register_ghcb_early(__pa(&boot_ghcb_page));
1502 #ifdef CONFIG_HOTPLUG_CPU
1503 static void sev_es_ap_hlt_loop(void)
1505 struct ghcb_state state;
1508 ghcb = __sev_get_ghcb(&state);
1511 vc_ghcb_invalidate(ghcb);
1512 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_HLT_LOOP);
1513 ghcb_set_sw_exit_info_1(ghcb, 0);
1514 ghcb_set_sw_exit_info_2(ghcb, 0);
1516 sev_es_wr_ghcb_msr(__pa(ghcb));
1519 /* Wakeup signal? */
1520 if (ghcb_sw_exit_info_2_is_valid(ghcb) &&
1521 ghcb->save.sw_exit_info_2)
1525 __sev_put_ghcb(&state);
1529 * Play_dead handler when running under SEV-ES. This is needed because
1530 * the hypervisor can't deliver an SIPI request to restart the AP.
1531 * Instead the kernel has to issue a VMGEXIT to halt the VCPU until the
1532 * hypervisor wakes it up again.
1534 static void sev_es_play_dead(void)
1538 /* IRQs now disabled */
1540 sev_es_ap_hlt_loop();
1543 * If we get here, the VCPU was woken up again. Jump to CPU
1544 * startup code to get it back online.
1548 #else /* CONFIG_HOTPLUG_CPU */
1549 #define sev_es_play_dead native_play_dead
1550 #endif /* CONFIG_HOTPLUG_CPU */
1553 static void __init sev_es_setup_play_dead(void)
1555 smp_ops.play_dead = sev_es_play_dead;
1558 static inline void sev_es_setup_play_dead(void) { }
1561 static void __init alloc_runtime_data(int cpu)
1563 struct sev_es_runtime_data *data;
1565 data = memblock_alloc_node(sizeof(*data), PAGE_SIZE, cpu_to_node(cpu));
1567 panic("Can't allocate SEV-ES runtime data");
1569 per_cpu(runtime_data, cpu) = data;
1572 struct svsm_ca *caa;
1574 /* Allocate the SVSM CA page if an SVSM is present */
1575 caa = memblock_alloc(sizeof(*caa), PAGE_SIZE);
1577 panic("Can't allocate SVSM CA page\n");
1579 per_cpu(svsm_caa, cpu) = caa;
1580 per_cpu(svsm_caa_pa, cpu) = __pa(caa);
1584 static void __init init_ghcb(int cpu)
1586 struct sev_es_runtime_data *data;
1589 data = per_cpu(runtime_data, cpu);
1591 err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
1592 sizeof(data->ghcb_page));
1594 panic("Can't map GHCBs unencrypted");
1596 memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
1598 data->ghcb_active = false;
1599 data->backup_ghcb_active = false;
1602 void __init sev_es_init_vc_handling(void)
1606 BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
1608 if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1611 if (!sev_es_check_cpu_features())
1612 panic("SEV-ES CPU Features missing");
1615 * SNP is supported in v2 of the GHCB spec which mandates support for HV
1618 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
1619 sev_hv_features = get_hv_features();
1621 if (!(sev_hv_features & GHCB_HV_FT_SNP))
1622 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
1625 /* Initialize per-cpu GHCB pages */
1626 for_each_possible_cpu(cpu) {
1627 alloc_runtime_data(cpu);
1631 /* If running under an SVSM, switch to the per-cpu CA */
1633 struct svsm_call call = {};
1634 unsigned long flags;
1637 local_irq_save(flags);
1640 * SVSM_CORE_REMAP_CA call:
1641 * RAX = 0 (Protocol=0, CallID=0)
1644 call.caa = svsm_get_caa();
1645 call.rax = SVSM_CORE_CALL(SVSM_CORE_REMAP_CA);
1646 call.rcx = this_cpu_read(svsm_caa_pa);
1647 ret = svsm_perform_call_protocol(&call);
1649 panic("Can't remap the SVSM CA, ret=%d, rax_out=0x%llx\n",
1652 sev_cfg.use_cas = true;
1654 local_irq_restore(flags);
1657 sev_es_setup_play_dead();
1659 /* Secondary CPUs use the runtime #VC handler */
1660 initial_vc_handler = (unsigned long)kernel_exc_vmm_communication;
1663 static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
1665 int trapnr = ctxt->fi.vector;
1667 if (trapnr == X86_TRAP_PF)
1668 native_write_cr2(ctxt->fi.cr2);
1670 ctxt->regs->orig_ax = ctxt->fi.error_code;
1671 do_early_exception(ctxt->regs, trapnr);
1674 static long *vc_insn_get_rm(struct es_em_ctxt *ctxt)
1679 reg_array = (long *)ctxt->regs;
1680 offset = insn_get_modrm_rm_off(&ctxt->insn, ctxt->regs);
1685 offset /= sizeof(long);
1687 return reg_array + offset;
1689 static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
1690 unsigned int bytes, bool read)
1692 u64 exit_code, exit_info_1, exit_info_2;
1693 unsigned long ghcb_pa = __pa(ghcb);
1698 ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
1699 if (ref == (void __user *)-1L)
1700 return ES_UNSUPPORTED;
1702 exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
1704 res = vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr);
1706 if (res == ES_EXCEPTION && !read)
1707 ctxt->fi.error_code |= X86_PF_WRITE;
1712 exit_info_1 = paddr;
1713 /* Can never be greater than 8 */
1714 exit_info_2 = bytes;
1716 ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
1718 return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
1722 * The MOVS instruction has two memory operands, which raises the
1723 * problem that it is not known whether the access to the source or the
1724 * destination caused the #VC exception (and hence whether an MMIO read
1725 * or write operation needs to be emulated).
1727 * Instead of playing games with walking page-tables and trying to guess
1728 * whether the source or destination is an MMIO range, split the move
1729 * into two operations, a read and a write with only one memory operand.
1730 * This will cause a nested #VC exception on the MMIO address which can
1733 * This implementation has the benefit that it also supports MOVS where
1734 * source _and_ destination are MMIO regions.
1736 * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a
1737 * rare operation. If it turns out to be a performance problem the split
1738 * operations can be moved to memcpy_fromio() and memcpy_toio().
1740 static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,
1743 unsigned long ds_base, es_base;
1744 unsigned char *src, *dst;
1745 unsigned char buffer[8];
1750 ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS);
1751 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
1753 if (ds_base == -1L || es_base == -1L) {
1754 ctxt->fi.vector = X86_TRAP_GP;
1755 ctxt->fi.error_code = 0;
1756 return ES_EXCEPTION;
1759 src = ds_base + (unsigned char *)ctxt->regs->si;
1760 dst = es_base + (unsigned char *)ctxt->regs->di;
1762 ret = vc_read_mem(ctxt, src, buffer, bytes);
1766 ret = vc_write_mem(ctxt, dst, buffer, bytes);
1770 if (ctxt->regs->flags & X86_EFLAGS_DF)
1775 ctxt->regs->si += off;
1776 ctxt->regs->di += off;
1778 rep = insn_has_rep_prefix(&ctxt->insn);
1780 ctxt->regs->cx -= 1;
1782 if (!rep || ctxt->regs->cx == 0)
1788 static enum es_result vc_handle_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1790 struct insn *insn = &ctxt->insn;
1791 enum insn_mmio_type mmio;
1792 unsigned int bytes = 0;
1797 mmio = insn_decode_mmio(insn, &bytes);
1798 if (mmio == INSN_MMIO_DECODE_FAILED)
1799 return ES_DECODE_FAILED;
1801 if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
1802 reg_data = insn_get_modrm_reg_ptr(insn, ctxt->regs);
1804 return ES_DECODE_FAILED;
1807 if (user_mode(ctxt->regs))
1808 return ES_UNSUPPORTED;
1811 case INSN_MMIO_WRITE:
1812 memcpy(ghcb->shared_buffer, reg_data, bytes);
1813 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1815 case INSN_MMIO_WRITE_IMM:
1816 memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
1817 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1819 case INSN_MMIO_READ:
1820 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1824 /* Zero-extend for 32-bit operation */
1828 memcpy(reg_data, ghcb->shared_buffer, bytes);
1830 case INSN_MMIO_READ_ZERO_EXTEND:
1831 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1835 /* Zero extend based on operand size */
1836 memset(reg_data, 0, insn->opnd_bytes);
1837 memcpy(reg_data, ghcb->shared_buffer, bytes);
1839 case INSN_MMIO_READ_SIGN_EXTEND:
1840 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1845 u8 *val = (u8 *)ghcb->shared_buffer;
1847 sign_byte = (*val & 0x80) ? 0xff : 0x00;
1849 u16 *val = (u16 *)ghcb->shared_buffer;
1851 sign_byte = (*val & 0x8000) ? 0xff : 0x00;
1854 /* Sign extend based on operand size */
1855 memset(reg_data, sign_byte, insn->opnd_bytes);
1856 memcpy(reg_data, ghcb->shared_buffer, bytes);
1858 case INSN_MMIO_MOVS:
1859 ret = vc_handle_mmio_movs(ctxt, bytes);
1862 ret = ES_UNSUPPORTED;
1869 static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
1870 struct es_em_ctxt *ctxt)
1872 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1873 long val, *reg = vc_insn_get_rm(ctxt);
1876 if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1877 return ES_VMM_ERROR;
1880 return ES_DECODE_FAILED;
1884 /* Upper 32 bits must be written as zeroes */
1886 ctxt->fi.vector = X86_TRAP_GP;
1887 ctxt->fi.error_code = 0;
1888 return ES_EXCEPTION;
1891 /* Clear out other reserved bits and set bit 10 */
1892 val = (val & 0xffff23ffL) | BIT(10);
1894 /* Early non-zero writes to DR7 are not supported */
1895 if (!data && (val & ~DR7_RESET_VALUE))
1896 return ES_UNSUPPORTED;
1898 /* Using a value of 0 for ExitInfo1 means RAX holds the value */
1899 ghcb_set_rax(ghcb, val);
1900 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
1910 static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
1911 struct es_em_ctxt *ctxt)
1913 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1914 long *reg = vc_insn_get_rm(ctxt);
1916 if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1917 return ES_VMM_ERROR;
1920 return ES_DECODE_FAILED;
1925 *reg = DR7_RESET_VALUE;
1930 static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
1931 struct es_em_ctxt *ctxt)
1933 return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
1936 static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1940 ghcb_set_rcx(ghcb, ctxt->regs->cx);
1942 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
1946 if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
1947 return ES_VMM_ERROR;
1949 ctxt->regs->ax = ghcb->save.rax;
1950 ctxt->regs->dx = ghcb->save.rdx;
1955 static enum es_result vc_handle_monitor(struct ghcb *ghcb,
1956 struct es_em_ctxt *ctxt)
1959 * Treat it as a NOP and do not leak a physical address to the
1965 static enum es_result vc_handle_mwait(struct ghcb *ghcb,
1966 struct es_em_ctxt *ctxt)
1968 /* Treat the same as MONITOR/MONITORX */
1972 static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
1973 struct es_em_ctxt *ctxt)
1977 ghcb_set_rax(ghcb, ctxt->regs->ax);
1978 ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
1980 if (x86_platform.hyper.sev_es_hcall_prepare)
1981 x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
1983 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
1987 if (!ghcb_rax_is_valid(ghcb))
1988 return ES_VMM_ERROR;
1990 ctxt->regs->ax = ghcb->save.rax;
1993 * Call sev_es_hcall_finish() after regs->ax is already set.
1994 * This allows the hypervisor handler to overwrite it again if
1997 if (x86_platform.hyper.sev_es_hcall_finish &&
1998 !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
1999 return ES_VMM_ERROR;
2004 static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
2005 struct es_em_ctxt *ctxt)
2008 * Calling ecx_alignment_check() directly does not work, because it
2009 * enables IRQs and the GHCB is active. Forward the exception and call
2010 * it later from vc_forward_exception().
2012 ctxt->fi.vector = X86_TRAP_AC;
2013 ctxt->fi.error_code = 0;
2014 return ES_EXCEPTION;
2017 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
2019 unsigned long exit_code)
2021 enum es_result result = vc_check_opcode_bytes(ctxt, exit_code);
2023 if (result != ES_OK)
2026 switch (exit_code) {
2027 case SVM_EXIT_READ_DR7:
2028 result = vc_handle_dr7_read(ghcb, ctxt);
2030 case SVM_EXIT_WRITE_DR7:
2031 result = vc_handle_dr7_write(ghcb, ctxt);
2033 case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
2034 result = vc_handle_trap_ac(ghcb, ctxt);
2036 case SVM_EXIT_RDTSC:
2037 case SVM_EXIT_RDTSCP:
2038 result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
2040 case SVM_EXIT_RDPMC:
2041 result = vc_handle_rdpmc(ghcb, ctxt);
2044 pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
2045 result = ES_UNSUPPORTED;
2047 case SVM_EXIT_CPUID:
2048 result = vc_handle_cpuid(ghcb, ctxt);
2051 result = vc_handle_ioio(ghcb, ctxt);
2054 result = vc_handle_msr(ghcb, ctxt);
2056 case SVM_EXIT_VMMCALL:
2057 result = vc_handle_vmmcall(ghcb, ctxt);
2059 case SVM_EXIT_WBINVD:
2060 result = vc_handle_wbinvd(ghcb, ctxt);
2062 case SVM_EXIT_MONITOR:
2063 result = vc_handle_monitor(ghcb, ctxt);
2065 case SVM_EXIT_MWAIT:
2066 result = vc_handle_mwait(ghcb, ctxt);
2069 result = vc_handle_mmio(ghcb, ctxt);
2073 * Unexpected #VC exception
2075 result = ES_UNSUPPORTED;
2081 static __always_inline bool is_vc2_stack(unsigned long sp)
2083 return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
2086 static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
2088 unsigned long sp, prev_sp;
2090 sp = (unsigned long)regs;
2094 * If the code was already executing on the VC2 stack when the #VC
2095 * happened, let it proceed to the normal handling routine. This way the
2096 * code executing on the VC2 stack can cause #VC exceptions to get handled.
2098 return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
2101 static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
2103 struct ghcb_state state;
2104 struct es_em_ctxt ctxt;
2105 enum es_result result;
2109 ghcb = __sev_get_ghcb(&state);
2111 vc_ghcb_invalidate(ghcb);
2112 result = vc_init_em_ctxt(&ctxt, regs, error_code);
2114 if (result == ES_OK)
2115 result = vc_handle_exitcode(&ctxt, ghcb, error_code);
2117 __sev_put_ghcb(&state);
2119 /* Done - now check the result */
2122 vc_finish_insn(&ctxt);
2124 case ES_UNSUPPORTED:
2125 pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
2126 error_code, regs->ip);
2130 pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
2131 error_code, regs->ip);
2134 case ES_DECODE_FAILED:
2135 pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
2136 error_code, regs->ip);
2140 vc_forward_exception(&ctxt);
2146 pr_emerg("Unknown result in %s():%d\n", __func__, result);
2148 * Emulating the instruction which caused the #VC exception
2149 * failed - can't continue so print debug information
2157 static __always_inline bool vc_is_db(unsigned long error_code)
2159 return error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB;
2163 * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode
2164 * and will panic when an error happens.
2166 DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
2168 irqentry_state_t irq_state;
2171 * With the current implementation it is always possible to switch to a
2172 * safe stack because #VC exceptions only happen at known places, like
2173 * intercepted instructions or accesses to MMIO areas/IO ports. They can
2174 * also happen with code instrumentation when the hypervisor intercepts
2175 * #DB, but the critical paths are forbidden to be instrumented, so #DB
2176 * exceptions currently also only happen in safe places.
2178 * But keep this here in case the noinstr annotations are violated due
2181 if (unlikely(vc_from_invalid_context(regs))) {
2182 instrumentation_begin();
2183 panic("Can't handle #VC exception from unsupported context\n");
2184 instrumentation_end();
2188 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
2190 if (vc_is_db(error_code)) {
2195 irq_state = irqentry_nmi_enter(regs);
2197 instrumentation_begin();
2199 if (!vc_raw_handle_exception(regs, error_code)) {
2200 /* Show some debug info */
2203 /* Ask hypervisor to sev_es_terminate */
2204 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
2206 /* If that fails and we get here - just panic */
2207 panic("Returned from Terminate-Request to Hypervisor\n");
2210 instrumentation_end();
2211 irqentry_nmi_exit(regs, irq_state);
2215 * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode
2216 * and will kill the current task with SIGBUS when an error happens.
2218 DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
2221 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
2223 if (vc_is_db(error_code)) {
2224 noist_exc_debug(regs);
2228 irqentry_enter_from_user_mode(regs);
2229 instrumentation_begin();
2231 if (!vc_raw_handle_exception(regs, error_code)) {
2233 * Do not kill the machine if user-space triggered the
2234 * exception. Send SIGBUS instead and let user-space deal with
2237 force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
2240 instrumentation_end();
2241 irqentry_exit_to_user_mode(regs);
2244 bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
2246 unsigned long exit_code = regs->orig_ax;
2247 struct es_em_ctxt ctxt;
2248 enum es_result result;
2250 vc_ghcb_invalidate(boot_ghcb);
2252 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
2253 if (result == ES_OK)
2254 result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
2256 /* Done - now check the result */
2259 vc_finish_insn(&ctxt);
2261 case ES_UNSUPPORTED:
2262 early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
2263 exit_code, regs->ip);
2266 early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
2267 exit_code, regs->ip);
2269 case ES_DECODE_FAILED:
2270 early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
2271 exit_code, regs->ip);
2274 vc_early_forward_exception(&ctxt);
2288 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
2292 * Initial set up of SNP relies on information provided by the
2293 * Confidential Computing blob, which can be passed to the kernel
2294 * in the following ways, depending on how it is booted:
2296 * - when booted via the boot/decompress kernel:
2299 * - when booted directly by firmware/bootloader (e.g. CONFIG_PVH):
2300 * - via a setup_data entry, as defined by the Linux Boot Protocol
2302 * Scan for the blob in that order.
2304 static __head struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
2306 struct cc_blob_sev_info *cc_info;
2308 /* Boot kernel would have passed the CC blob via boot_params. */
2309 if (bp->cc_blob_address) {
2310 cc_info = (struct cc_blob_sev_info *)(unsigned long)bp->cc_blob_address;
2315 * If kernel was booted directly, without the use of the
2316 * boot/decompression kernel, the CC blob may have been passed via
2317 * setup_data instead.
2319 cc_info = find_cc_blob_setup_data(bp);
2324 if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
2330 static __head void svsm_setup(struct cc_blob_sev_info *cc_info)
2332 struct svsm_call call = {};
2337 * Record the SVSM Calling Area address (CAA) if the guest is not
2338 * running at VMPL0. The CA will be used to communicate with the
2339 * SVSM to perform the SVSM services.
2341 if (!svsm_setup_ca(cc_info))
2345 * It is very early in the boot and the kernel is running identity
2346 * mapped but without having adjusted the pagetables to where the
2347 * kernel was loaded (physbase), so the get the CA address using
2348 * RIP-relative addressing.
2350 pa = (u64)&RIP_REL_REF(boot_svsm_ca_page);
2353 * Switch over to the boot SVSM CA while the current CA is still
2354 * addressable. There is no GHCB at this point so use the MSR protocol.
2356 * SVSM_CORE_REMAP_CA call:
2357 * RAX = 0 (Protocol=0, CallID=0)
2360 call.caa = svsm_get_caa();
2361 call.rax = SVSM_CORE_CALL(SVSM_CORE_REMAP_CA);
2363 ret = svsm_perform_call_protocol(&call);
2365 panic("Can't remap the SVSM CA, ret=%d, rax_out=0x%llx\n", ret, call.rax_out);
2367 RIP_REL_REF(boot_svsm_caa) = (struct svsm_ca *)pa;
2368 RIP_REL_REF(boot_svsm_caa_pa) = pa;
2371 bool __head snp_init(struct boot_params *bp)
2373 struct cc_blob_sev_info *cc_info;
2378 cc_info = find_cc_blob(bp);
2382 if (cc_info->secrets_phys && cc_info->secrets_len == PAGE_SIZE)
2383 secrets_pa = cc_info->secrets_phys;
2387 setup_cpuid_table(cc_info);
2389 svsm_setup(cc_info);
2392 * The CC blob will be used later to access the secrets page. Cache
2393 * it here like the boot kernel does.
2395 bp->cc_blob_address = (u32)(unsigned long)cc_info;
2400 void __head __noreturn snp_abort(void)
2402 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
2406 * SEV-SNP guests should only execute dmi_setup() if EFI_CONFIG_TABLES are
2407 * enabled, as the alternative (fallback) logic for DMI probing in the legacy
2408 * ROM region can cause a crash since this region is not pre-validated.
2410 void __init snp_dmi_setup(void)
2412 if (efi_enabled(EFI_CONFIG_TABLES))
2416 static void dump_cpuid_table(void)
2418 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
2421 pr_info("count=%d reserved=0x%x reserved2=0x%llx\n",
2422 cpuid_table->count, cpuid_table->__reserved1, cpuid_table->__reserved2);
2424 for (i = 0; i < SNP_CPUID_COUNT_MAX; i++) {
2425 const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
2427 pr_info("index=%3d fn=0x%08x subfn=0x%08x: eax=0x%08x ebx=0x%08x ecx=0x%08x edx=0x%08x xcr0_in=0x%016llx xss_in=0x%016llx reserved=0x%016llx\n",
2428 i, fn->eax_in, fn->ecx_in, fn->eax, fn->ebx, fn->ecx,
2429 fn->edx, fn->xcr0_in, fn->xss_in, fn->__reserved);
2434 * It is useful from an auditing/testing perspective to provide an easy way
2435 * for the guest owner to know that the CPUID table has been initialized as
2436 * expected, but that initialization happens too early in boot to print any
2437 * sort of indicator, and there's not really any other good place to do it,
2440 * If running as an SNP guest, report the current VM privilege level (VMPL).
2442 static int __init report_snp_info(void)
2444 const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
2446 if (cpuid_table->count) {
2447 pr_info("Using SNP CPUID table, %d entries present.\n",
2448 cpuid_table->count);
2454 if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2455 pr_info("SNP running at VMPL%u.\n", snp_vmpl);
2459 arch_initcall(report_snp_info);
2461 static void update_attest_input(struct svsm_call *call, struct svsm_attest_call *input)
2463 /* If (new) lengths have been returned, propagate them up */
2464 if (call->rcx_out != call->rcx)
2465 input->manifest_buf.len = call->rcx_out;
2467 if (call->rdx_out != call->rdx)
2468 input->certificates_buf.len = call->rdx_out;
2470 if (call->r8_out != call->r8)
2471 input->report_buf.len = call->r8_out;
2474 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call,
2475 struct svsm_attest_call *input)
2477 struct svsm_attest_call *ac;
2478 unsigned long flags;
2485 local_irq_save(flags);
2487 call->caa = svsm_get_caa();
2489 ac = (struct svsm_attest_call *)call->caa->svsm_buffer;
2490 attest_call_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
2495 * Set input registers for the request and set RDX and R8 to known
2496 * values in order to detect length values being returned in them.
2498 call->rax = call_id;
2499 call->rcx = attest_call_pa;
2502 ret = svsm_perform_call_protocol(call);
2503 update_attest_input(call, input);
2505 local_irq_restore(flags);
2509 EXPORT_SYMBOL_GPL(snp_issue_svsm_attest_req);
2511 int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
2512 struct snp_guest_request_ioctl *rio)
2514 struct ghcb_state state;
2515 struct es_em_ctxt ctxt;
2516 unsigned long flags;
2520 rio->exitinfo2 = SEV_RET_NO_FW_CALL;
2523 * __sev_get_ghcb() needs to run with IRQs disabled because it is using
2526 local_irq_save(flags);
2528 ghcb = __sev_get_ghcb(&state);
2534 vc_ghcb_invalidate(ghcb);
2536 if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2537 ghcb_set_rax(ghcb, input->data_gpa);
2538 ghcb_set_rbx(ghcb, input->data_npages);
2541 ret = sev_es_ghcb_hv_call(ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa);
2545 rio->exitinfo2 = ghcb->save.sw_exit_info_2;
2546 switch (rio->exitinfo2) {
2550 case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY):
2554 case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
2555 /* Number of expected pages are returned in RBX */
2556 if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2557 input->data_npages = ghcb_get_rbx(ghcb);
2568 __sev_put_ghcb(&state);
2570 local_irq_restore(flags);
2574 EXPORT_SYMBOL_GPL(snp_issue_guest_request);
2576 static struct platform_device sev_guest_device = {
2577 .name = "sev-guest",
2581 static int __init snp_init_platform_device(void)
2583 struct sev_guest_platform_data data;
2585 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2588 data.secrets_gpa = secrets_pa;
2589 if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
2592 if (platform_device_register(&sev_guest_device))
2595 pr_info("SNP guest platform device initialized.\n");
2598 device_initcall(snp_init_platform_device);
2600 void sev_show_status(void)
2604 pr_info("Status: ");
2605 for (i = 0; i < MSR_AMD64_SNP_RESV_BIT; i++) {
2606 if (sev_status & BIT_ULL(i)) {
2607 if (!sev_status_feat_names[i])
2610 pr_cont("%s ", sev_status_feat_names[i]);
2616 void __init snp_update_svsm_ca(void)
2621 /* Update the CAA to a proper kernel address */
2622 boot_svsm_caa = &boot_svsm_ca_page;
2626 static ssize_t vmpl_show(struct kobject *kobj,
2627 struct kobj_attribute *attr, char *buf)
2629 return sysfs_emit(buf, "%d\n", snp_vmpl);
2632 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
2634 static struct attribute *vmpl_attrs[] = {
2639 static struct attribute_group sev_attr_group = {
2640 .attrs = vmpl_attrs,
2643 static int __init sev_sysfs_init(void)
2645 struct kobject *sev_kobj;
2646 struct device *dev_root;
2649 if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2652 dev_root = bus_get_dev_root(&cpu_subsys);
2656 sev_kobj = kobject_create_and_add("sev", &dev_root->kobj);
2657 put_device(dev_root);
2662 ret = sysfs_create_group(sev_kobj, &sev_attr_group);
2664 kobject_put(sev_kobj);
2668 arch_initcall(sev_sysfs_init);
2669 #endif // CONFIG_SYSFS