xref: /openbmc/linux/arch/x86/kernel/sev.c (revision 86aa961bb4619a68077ebeba21c52e9ba0eab43d)
1e759959fSBrijesh Singh // SPDX-License-Identifier: GPL-2.0-only
2e759959fSBrijesh Singh /*
3e759959fSBrijesh Singh  * AMD Memory Encryption Support
4e759959fSBrijesh Singh  *
5e759959fSBrijesh Singh  * Copyright (C) 2019 SUSE
6e759959fSBrijesh Singh  *
7e759959fSBrijesh Singh  * Author: Joerg Roedel <jroedel@suse.de>
8e759959fSBrijesh Singh  */
9e759959fSBrijesh Singh 
108d9d46bbSJoerg Roedel #define pr_fmt(fmt)	"SEV: " fmt
11e759959fSBrijesh Singh 
12e759959fSBrijesh Singh #include <linux/sched/debug.h>	/* For show_regs() */
13e759959fSBrijesh Singh #include <linux/percpu-defs.h>
146283f2efSTom Lendacky #include <linux/cc_platform.h>
15e759959fSBrijesh Singh #include <linux/printk.h>
16e759959fSBrijesh Singh #include <linux/mm_types.h>
17e759959fSBrijesh Singh #include <linux/set_memory.h>
18e759959fSBrijesh Singh #include <linux/memblock.h>
19e759959fSBrijesh Singh #include <linux/kernel.h>
20e759959fSBrijesh Singh #include <linux/mm.h>
210afb6b66STom Lendacky #include <linux/cpumask.h>
223a45b375SBrijesh Singh #include <linux/efi.h>
233a45b375SBrijesh Singh #include <linux/platform_device.h>
243a45b375SBrijesh Singh #include <linux/io.h>
250144e3b8SDionna Glaze #include <linux/psp-sev.h>
264338e40dSKevin Loughlin #include <linux/dmi.h>
270144e3b8SDionna Glaze #include <uapi/linux/sev-guest.h>
28e759959fSBrijesh Singh 
29*56408ed9SArd Biesheuvel #include <asm/init.h>
30e759959fSBrijesh Singh #include <asm/cpu_entry_area.h>
31e759959fSBrijesh Singh #include <asm/stacktrace.h>
32e759959fSBrijesh Singh #include <asm/sev.h>
33e759959fSBrijesh Singh #include <asm/insn-eval.h>
34ff0c37e1SThomas Gleixner #include <asm/fpu/xcr.h>
35e759959fSBrijesh Singh #include <asm/processor.h>
36e759959fSBrijesh Singh #include <asm/realmode.h>
3775cc9a84SBorislav Petkov #include <asm/setup.h>
38e759959fSBrijesh Singh #include <asm/traps.h>
39e759959fSBrijesh Singh #include <asm/svm.h>
40e759959fSBrijesh Singh #include <asm/smp.h>
41e759959fSBrijesh Singh #include <asm/cpu.h>
420afb6b66STom Lendacky #include <asm/apic.h>
43801baa69SMichael Roth #include <asm/cpuid.h>
4430612045SMichael Roth #include <asm/cmdline.h>
45e759959fSBrijesh Singh 
46e759959fSBrijesh Singh #define DR7_RESET_VALUE        0x400
47e759959fSBrijesh Singh 
480afb6b66STom Lendacky /* AP INIT values as documented in the APM2  section "Processor Initialization State" */
490afb6b66STom Lendacky #define AP_INIT_CS_LIMIT		0xffff
500afb6b66STom Lendacky #define AP_INIT_DS_LIMIT		0xffff
510afb6b66STom Lendacky #define AP_INIT_LDTR_LIMIT		0xffff
520afb6b66STom Lendacky #define AP_INIT_GDTR_LIMIT		0xffff
530afb6b66STom Lendacky #define AP_INIT_IDTR_LIMIT		0xffff
540afb6b66STom Lendacky #define AP_INIT_TR_LIMIT		0xffff
550afb6b66STom Lendacky #define AP_INIT_RFLAGS_DEFAULT		0x2
560afb6b66STom Lendacky #define AP_INIT_DR6_DEFAULT		0xffff0ff0
570afb6b66STom Lendacky #define AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
580afb6b66STom Lendacky #define AP_INIT_XCR0_DEFAULT		0x1
590afb6b66STom Lendacky #define AP_INIT_X87_FTW_DEFAULT		0x5555
600afb6b66STom Lendacky #define AP_INIT_X87_FCW_DEFAULT		0x0040
610afb6b66STom Lendacky #define AP_INIT_CR0_DEFAULT		0x60000010
620afb6b66STom Lendacky #define AP_INIT_MXCSR_DEFAULT		0x1f80
630afb6b66STom Lendacky 
64e759959fSBrijesh Singh /* For early boot hypervisor communication in SEV-ES enabled guests */
65e759959fSBrijesh Singh static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
66e759959fSBrijesh Singh 
67e759959fSBrijesh Singh /*
68e759959fSBrijesh Singh  * Needs to be in the .data section because we need it NULL before bss is
69e759959fSBrijesh Singh  * cleared
70e759959fSBrijesh Singh  */
7195d33bfaSBrijesh Singh static struct ghcb *boot_ghcb __section(".data");
72e759959fSBrijesh Singh 
73cbd3d4f7SBrijesh Singh /* Bitmap of SEV features supported by the hypervisor */
74cbd3d4f7SBrijesh Singh static u64 sev_hv_features __ro_after_init;
75cbd3d4f7SBrijesh Singh 
76e759959fSBrijesh Singh /* #VC handler runtime per-CPU data */
77e759959fSBrijesh Singh struct sev_es_runtime_data {
78e759959fSBrijesh Singh 	struct ghcb ghcb_page;
79e759959fSBrijesh Singh 
80e759959fSBrijesh Singh 	/*
81e759959fSBrijesh Singh 	 * Reserve one page per CPU as backup storage for the unencrypted GHCB.
82e759959fSBrijesh Singh 	 * It is needed when an NMI happens while the #VC handler uses the real
83e759959fSBrijesh Singh 	 * GHCB, and the NMI handler itself is causing another #VC exception. In
84e759959fSBrijesh Singh 	 * that case the GHCB content of the first handler needs to be backed up
85e759959fSBrijesh Singh 	 * and restored.
86e759959fSBrijesh Singh 	 */
87e759959fSBrijesh Singh 	struct ghcb backup_ghcb;
88e759959fSBrijesh Singh 
89e759959fSBrijesh Singh 	/*
90e759959fSBrijesh Singh 	 * Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions.
91e759959fSBrijesh Singh 	 * There is no need for it to be atomic, because nothing is written to
92e759959fSBrijesh Singh 	 * the GHCB between the read and the write of ghcb_active. So it is safe
93e759959fSBrijesh Singh 	 * to use it when a nested #VC exception happens before the write.
94e759959fSBrijesh Singh 	 *
95e759959fSBrijesh Singh 	 * This is necessary for example in the #VC->NMI->#VC case when the NMI
96e759959fSBrijesh Singh 	 * happens while the first #VC handler uses the GHCB. When the NMI code
97e759959fSBrijesh Singh 	 * raises a second #VC handler it might overwrite the contents of the
98e759959fSBrijesh Singh 	 * GHCB written by the first handler. To avoid this the content of the
99e759959fSBrijesh Singh 	 * GHCB is saved and restored when the GHCB is detected to be in use
100e759959fSBrijesh Singh 	 * already.
101e759959fSBrijesh Singh 	 */
102e759959fSBrijesh Singh 	bool ghcb_active;
103e759959fSBrijesh Singh 	bool backup_ghcb_active;
104e759959fSBrijesh Singh 
105e759959fSBrijesh Singh 	/*
106e759959fSBrijesh Singh 	 * Cached DR7 value - write it on DR7 writes and return it on reads.
107e759959fSBrijesh Singh 	 * That value will never make it to the real hardware DR7 as debugging
108e759959fSBrijesh Singh 	 * is currently unsupported in SEV-ES guests.
109e759959fSBrijesh Singh 	 */
110e759959fSBrijesh Singh 	unsigned long dr7;
111e759959fSBrijesh Singh };
112e759959fSBrijesh Singh 
113e759959fSBrijesh Singh struct ghcb_state {
114e759959fSBrijesh Singh 	struct ghcb *ghcb;
115e759959fSBrijesh Singh };
116e759959fSBrijesh Singh 
117e759959fSBrijesh Singh static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
1180afb6b66STom Lendacky static DEFINE_PER_CPU(struct sev_es_save_area *, sev_vmsa);
1190afb6b66STom Lendacky 
120ba37a143SMichael Roth struct sev_config {
121ba37a143SMichael Roth 	__u64 debug		: 1,
1227006b755STom Lendacky 
1237006b755STom Lendacky 	      /*
1247006b755STom Lendacky 	       * A flag used by __set_pages_state() that indicates when the
1257006b755STom Lendacky 	       * per-CPU GHCB has been created and registered and thus can be
1267006b755STom Lendacky 	       * used by the BSP instead of the early boot GHCB.
1277006b755STom Lendacky 	       *
1287006b755STom Lendacky 	       * For APs, the per-CPU GHCB is created before they are started
1297006b755STom Lendacky 	       * and registered upon startup, so this flag can be used globally
1307006b755STom Lendacky 	       * for the BSP and APs.
1317006b755STom Lendacky 	       */
1327006b755STom Lendacky 	      ghcbs_initialized	: 1,
1337006b755STom Lendacky 
1347006b755STom Lendacky 	      __reserved	: 62;
135ba37a143SMichael Roth };
136ba37a143SMichael Roth 
137ba37a143SMichael Roth static struct sev_config sev_cfg __read_mostly;
138ba37a143SMichael Roth 
on_vc_stack(struct pt_regs * regs)139e759959fSBrijesh Singh static __always_inline bool on_vc_stack(struct pt_regs *regs)
140e759959fSBrijesh Singh {
141e759959fSBrijesh Singh 	unsigned long sp = regs->sp;
142e759959fSBrijesh Singh 
143e759959fSBrijesh Singh 	/* User-mode RSP is not trusted */
144e759959fSBrijesh Singh 	if (user_mode(regs))
145e759959fSBrijesh Singh 		return false;
146e759959fSBrijesh Singh 
147e759959fSBrijesh Singh 	/* SYSCALL gap still has user-mode RSP */
148e759959fSBrijesh Singh 	if (ip_within_syscall_gap(regs))
149e759959fSBrijesh Singh 		return false;
150e759959fSBrijesh Singh 
151e759959fSBrijesh Singh 	return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
152e759959fSBrijesh Singh }
153e759959fSBrijesh Singh 
154e759959fSBrijesh Singh /*
155e759959fSBrijesh Singh  * This function handles the case when an NMI is raised in the #VC
156e759959fSBrijesh Singh  * exception handler entry code, before the #VC handler has switched off
157e759959fSBrijesh Singh  * its IST stack. In this case, the IST entry for #VC must be adjusted,
158e759959fSBrijesh Singh  * so that any nested #VC exception will not overwrite the stack
159e759959fSBrijesh Singh  * contents of the interrupted #VC handler.
160e759959fSBrijesh Singh  *
161e759959fSBrijesh Singh  * The IST entry is adjusted unconditionally so that it can be also be
162e759959fSBrijesh Singh  * unconditionally adjusted back in __sev_es_ist_exit(). Otherwise a
163e759959fSBrijesh Singh  * nested sev_es_ist_exit() call may adjust back the IST entry too
164e759959fSBrijesh Singh  * early.
165e759959fSBrijesh Singh  *
166e759959fSBrijesh Singh  * The __sev_es_ist_enter() and __sev_es_ist_exit() functions always run
167e759959fSBrijesh Singh  * on the NMI IST stack, as they are only called from NMI handling code
168e759959fSBrijesh Singh  * right now.
169e759959fSBrijesh Singh  */
__sev_es_ist_enter(struct pt_regs * regs)170e759959fSBrijesh Singh void noinstr __sev_es_ist_enter(struct pt_regs *regs)
171e759959fSBrijesh Singh {
172e759959fSBrijesh Singh 	unsigned long old_ist, new_ist;
173e759959fSBrijesh Singh 
174e759959fSBrijesh Singh 	/* Read old IST entry */
175e759959fSBrijesh Singh 	new_ist = old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
176e759959fSBrijesh Singh 
177e759959fSBrijesh Singh 	/*
178e759959fSBrijesh Singh 	 * If NMI happened while on the #VC IST stack, set the new IST
179e759959fSBrijesh Singh 	 * value below regs->sp, so that the interrupted stack frame is
180e759959fSBrijesh Singh 	 * not overwritten by subsequent #VC exceptions.
181e759959fSBrijesh Singh 	 */
182e759959fSBrijesh Singh 	if (on_vc_stack(regs))
183e759959fSBrijesh Singh 		new_ist = regs->sp;
184e759959fSBrijesh Singh 
185e759959fSBrijesh Singh 	/*
186e759959fSBrijesh Singh 	 * Reserve additional 8 bytes and store old IST value so this
187e759959fSBrijesh Singh 	 * adjustment can be unrolled in __sev_es_ist_exit().
188e759959fSBrijesh Singh 	 */
189e759959fSBrijesh Singh 	new_ist -= sizeof(old_ist);
190e759959fSBrijesh Singh 	*(unsigned long *)new_ist = old_ist;
191e759959fSBrijesh Singh 
192e759959fSBrijesh Singh 	/* Set new IST entry */
193e759959fSBrijesh Singh 	this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], new_ist);
194e759959fSBrijesh Singh }
195e759959fSBrijesh Singh 
__sev_es_ist_exit(void)196e759959fSBrijesh Singh void noinstr __sev_es_ist_exit(void)
197e759959fSBrijesh Singh {
198e759959fSBrijesh Singh 	unsigned long ist;
199e759959fSBrijesh Singh 
200e759959fSBrijesh Singh 	/* Read IST entry */
201e759959fSBrijesh Singh 	ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
202e759959fSBrijesh Singh 
203e759959fSBrijesh Singh 	if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
204e759959fSBrijesh Singh 		return;
205e759959fSBrijesh Singh 
206e759959fSBrijesh Singh 	/* Read back old IST entry and write it to the TSS */
207e759959fSBrijesh Singh 	this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist);
208e759959fSBrijesh Singh }
209e759959fSBrijesh Singh 
210d187f217SJoerg Roedel /*
211d187f217SJoerg Roedel  * Nothing shall interrupt this code path while holding the per-CPU
212d187f217SJoerg Roedel  * GHCB. The backup GHCB is only for NMIs interrupting this path.
213d187f217SJoerg Roedel  *
214d187f217SJoerg Roedel  * Callers must disable local interrupts around it.
215d187f217SJoerg Roedel  */
__sev_get_ghcb(struct ghcb_state * state)216d187f217SJoerg Roedel static noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)
217e759959fSBrijesh Singh {
218e759959fSBrijesh Singh 	struct sev_es_runtime_data *data;
219e759959fSBrijesh Singh 	struct ghcb *ghcb;
220e759959fSBrijesh Singh 
221d187f217SJoerg Roedel 	WARN_ON(!irqs_disabled());
222d187f217SJoerg Roedel 
223e759959fSBrijesh Singh 	data = this_cpu_read(runtime_data);
224e759959fSBrijesh Singh 	ghcb = &data->ghcb_page;
225e759959fSBrijesh Singh 
226e759959fSBrijesh Singh 	if (unlikely(data->ghcb_active)) {
227e759959fSBrijesh Singh 		/* GHCB is already in use - save its contents */
228e759959fSBrijesh Singh 
229b250f2f7SJoerg Roedel 		if (unlikely(data->backup_ghcb_active)) {
230b250f2f7SJoerg Roedel 			/*
231b250f2f7SJoerg Roedel 			 * Backup-GHCB is also already in use. There is no way
232b250f2f7SJoerg Roedel 			 * to continue here so just kill the machine. To make
233b250f2f7SJoerg Roedel 			 * panic() work, mark GHCBs inactive so that messages
234b250f2f7SJoerg Roedel 			 * can be printed out.
235b250f2f7SJoerg Roedel 			 */
236b250f2f7SJoerg Roedel 			data->ghcb_active        = false;
237b250f2f7SJoerg Roedel 			data->backup_ghcb_active = false;
238b250f2f7SJoerg Roedel 
239d187f217SJoerg Roedel 			instrumentation_begin();
240b250f2f7SJoerg Roedel 			panic("Unable to handle #VC exception! GHCB and Backup GHCB are already in use");
241d187f217SJoerg Roedel 			instrumentation_end();
242b250f2f7SJoerg Roedel 		}
243e759959fSBrijesh Singh 
244e759959fSBrijesh Singh 		/* Mark backup_ghcb active before writing to it */
245e759959fSBrijesh Singh 		data->backup_ghcb_active = true;
246e759959fSBrijesh Singh 
247e759959fSBrijesh Singh 		state->ghcb = &data->backup_ghcb;
248e759959fSBrijesh Singh 
249e759959fSBrijesh Singh 		/* Backup GHCB content */
250e759959fSBrijesh Singh 		*state->ghcb = *ghcb;
251e759959fSBrijesh Singh 	} else {
252e759959fSBrijesh Singh 		state->ghcb = NULL;
253e759959fSBrijesh Singh 		data->ghcb_active = true;
254e759959fSBrijesh Singh 	}
255e759959fSBrijesh Singh 
256e759959fSBrijesh Singh 	return ghcb;
257e759959fSBrijesh Singh }
258e759959fSBrijesh Singh 
sev_es_rd_ghcb_msr(void)259e759959fSBrijesh Singh static inline u64 sev_es_rd_ghcb_msr(void)
260e759959fSBrijesh Singh {
261e759959fSBrijesh Singh 	return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
262e759959fSBrijesh Singh }
263e759959fSBrijesh Singh 
sev_es_wr_ghcb_msr(u64 val)264e759959fSBrijesh Singh static __always_inline void sev_es_wr_ghcb_msr(u64 val)
265e759959fSBrijesh Singh {
266e759959fSBrijesh Singh 	u32 low, high;
267e759959fSBrijesh Singh 
268e759959fSBrijesh Singh 	low  = (u32)(val);
269e759959fSBrijesh Singh 	high = (u32)(val >> 32);
270e759959fSBrijesh Singh 
271e759959fSBrijesh Singh 	native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
272e759959fSBrijesh Singh }
273e759959fSBrijesh Singh 
vc_fetch_insn_kernel(struct es_em_ctxt * ctxt,unsigned char * buffer)274e759959fSBrijesh Singh static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
275e759959fSBrijesh Singh 				unsigned char *buffer)
276e759959fSBrijesh Singh {
277e759959fSBrijesh Singh 	return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
278e759959fSBrijesh Singh }
279e759959fSBrijesh Singh 
__vc_decode_user_insn(struct es_em_ctxt * ctxt)280e759959fSBrijesh Singh static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
281e759959fSBrijesh Singh {
282e759959fSBrijesh Singh 	char buffer[MAX_INSN_SIZE];
2834aaa7eacSJoerg Roedel 	int insn_bytes;
284e759959fSBrijesh Singh 
2854aaa7eacSJoerg Roedel 	insn_bytes = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
28607570cefSJoerg Roedel 	if (insn_bytes == 0) {
28707570cefSJoerg Roedel 		/* Nothing could be copied */
288e759959fSBrijesh Singh 		ctxt->fi.vector     = X86_TRAP_PF;
289e759959fSBrijesh Singh 		ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
290e759959fSBrijesh Singh 		ctxt->fi.cr2        = ctxt->regs->ip;
291e759959fSBrijesh Singh 		return ES_EXCEPTION;
29207570cefSJoerg Roedel 	} else if (insn_bytes == -EINVAL) {
29307570cefSJoerg Roedel 		/* Effective RIP could not be calculated */
29407570cefSJoerg Roedel 		ctxt->fi.vector     = X86_TRAP_GP;
29507570cefSJoerg Roedel 		ctxt->fi.error_code = 0;
29607570cefSJoerg Roedel 		ctxt->fi.cr2        = 0;
29707570cefSJoerg Roedel 		return ES_EXCEPTION;
298e759959fSBrijesh Singh 	}
299e759959fSBrijesh Singh 
3004aaa7eacSJoerg Roedel 	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, insn_bytes))
301e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
302e759959fSBrijesh Singh 
303e759959fSBrijesh Singh 	if (ctxt->insn.immediate.got)
304e759959fSBrijesh Singh 		return ES_OK;
305e759959fSBrijesh Singh 	else
306e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
307e759959fSBrijesh Singh }
308e759959fSBrijesh Singh 
__vc_decode_kern_insn(struct es_em_ctxt * ctxt)309e759959fSBrijesh Singh static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
310e759959fSBrijesh Singh {
311e759959fSBrijesh Singh 	char buffer[MAX_INSN_SIZE];
312e759959fSBrijesh Singh 	int res, ret;
313e759959fSBrijesh Singh 
314e759959fSBrijesh Singh 	res = vc_fetch_insn_kernel(ctxt, buffer);
315e759959fSBrijesh Singh 	if (res) {
316e759959fSBrijesh Singh 		ctxt->fi.vector     = X86_TRAP_PF;
317e759959fSBrijesh Singh 		ctxt->fi.error_code = X86_PF_INSTR;
318e759959fSBrijesh Singh 		ctxt->fi.cr2        = ctxt->regs->ip;
319e759959fSBrijesh Singh 		return ES_EXCEPTION;
320e759959fSBrijesh Singh 	}
321e759959fSBrijesh Singh 
322e759959fSBrijesh Singh 	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
323e759959fSBrijesh Singh 	if (ret < 0)
324e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
325e759959fSBrijesh Singh 	else
326e759959fSBrijesh Singh 		return ES_OK;
327e759959fSBrijesh Singh }
328e759959fSBrijesh Singh 
vc_decode_insn(struct es_em_ctxt * ctxt)329e759959fSBrijesh Singh static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
330e759959fSBrijesh Singh {
331e759959fSBrijesh Singh 	if (user_mode(ctxt->regs))
332e759959fSBrijesh Singh 		return __vc_decode_user_insn(ctxt);
333e759959fSBrijesh Singh 	else
334e759959fSBrijesh Singh 		return __vc_decode_kern_insn(ctxt);
335e759959fSBrijesh Singh }
336e759959fSBrijesh Singh 
vc_write_mem(struct es_em_ctxt * ctxt,char * dst,char * buf,size_t size)337e759959fSBrijesh Singh static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
338e759959fSBrijesh Singh 				   char *dst, char *buf, size_t size)
339e759959fSBrijesh Singh {
340e759959fSBrijesh Singh 	unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
341e759959fSBrijesh Singh 
3424954f5b8SJoerg Roedel 	/*
3434954f5b8SJoerg Roedel 	 * This function uses __put_user() independent of whether kernel or user
3444954f5b8SJoerg Roedel 	 * memory is accessed. This works fine because __put_user() does no
3454954f5b8SJoerg Roedel 	 * sanity checks of the pointer being accessed. All that it does is
3464954f5b8SJoerg Roedel 	 * to report when the access failed.
3474954f5b8SJoerg Roedel 	 *
3484954f5b8SJoerg Roedel 	 * Also, this function runs in atomic context, so __put_user() is not
3494954f5b8SJoerg Roedel 	 * allowed to sleep. The page-fault handler detects that it is running
3504954f5b8SJoerg Roedel 	 * in atomic context and will not try to take mmap_sem and handle the
3514954f5b8SJoerg Roedel 	 * fault, so additional pagefault_enable()/disable() calls are not
3524954f5b8SJoerg Roedel 	 * needed.
3534954f5b8SJoerg Roedel 	 *
3544954f5b8SJoerg Roedel 	 * The access can't be done via copy_to_user() here because
3554954f5b8SJoerg Roedel 	 * vc_write_mem() must not use string instructions to access unsafe
3564954f5b8SJoerg Roedel 	 * memory. The reason is that MOVS is emulated by the #VC handler by
3574954f5b8SJoerg Roedel 	 * splitting the move up into a read and a write and taking a nested #VC
3584954f5b8SJoerg Roedel 	 * exception on whatever of them is the MMIO access. Using string
3594954f5b8SJoerg Roedel 	 * instructions here would cause infinite nesting.
3604954f5b8SJoerg Roedel 	 */
361e759959fSBrijesh Singh 	switch (size) {
3621d5379d0SMichael Sterritt 	case 1: {
3631d5379d0SMichael Sterritt 		u8 d1;
3641d5379d0SMichael Sterritt 		u8 __user *target = (u8 __user *)dst;
3651d5379d0SMichael Sterritt 
366e759959fSBrijesh Singh 		memcpy(&d1, buf, 1);
3674954f5b8SJoerg Roedel 		if (__put_user(d1, target))
368e759959fSBrijesh Singh 			goto fault;
369e759959fSBrijesh Singh 		break;
3701d5379d0SMichael Sterritt 	}
3711d5379d0SMichael Sterritt 	case 2: {
3721d5379d0SMichael Sterritt 		u16 d2;
3731d5379d0SMichael Sterritt 		u16 __user *target = (u16 __user *)dst;
3741d5379d0SMichael Sterritt 
375e759959fSBrijesh Singh 		memcpy(&d2, buf, 2);
3764954f5b8SJoerg Roedel 		if (__put_user(d2, target))
377e759959fSBrijesh Singh 			goto fault;
378e759959fSBrijesh Singh 		break;
3791d5379d0SMichael Sterritt 	}
3801d5379d0SMichael Sterritt 	case 4: {
3811d5379d0SMichael Sterritt 		u32 d4;
3821d5379d0SMichael Sterritt 		u32 __user *target = (u32 __user *)dst;
3831d5379d0SMichael Sterritt 
384e759959fSBrijesh Singh 		memcpy(&d4, buf, 4);
3854954f5b8SJoerg Roedel 		if (__put_user(d4, target))
386e759959fSBrijesh Singh 			goto fault;
387e759959fSBrijesh Singh 		break;
3881d5379d0SMichael Sterritt 	}
3891d5379d0SMichael Sterritt 	case 8: {
3901d5379d0SMichael Sterritt 		u64 d8;
3911d5379d0SMichael Sterritt 		u64 __user *target = (u64 __user *)dst;
3921d5379d0SMichael Sterritt 
393e759959fSBrijesh Singh 		memcpy(&d8, buf, 8);
3944954f5b8SJoerg Roedel 		if (__put_user(d8, target))
395e759959fSBrijesh Singh 			goto fault;
396e759959fSBrijesh Singh 		break;
3971d5379d0SMichael Sterritt 	}
398e759959fSBrijesh Singh 	default:
399e759959fSBrijesh Singh 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
400e759959fSBrijesh Singh 		return ES_UNSUPPORTED;
401e759959fSBrijesh Singh 	}
402e759959fSBrijesh Singh 
403e759959fSBrijesh Singh 	return ES_OK;
404e759959fSBrijesh Singh 
405e759959fSBrijesh Singh fault:
406e759959fSBrijesh Singh 	if (user_mode(ctxt->regs))
407e759959fSBrijesh Singh 		error_code |= X86_PF_USER;
408e759959fSBrijesh Singh 
409e759959fSBrijesh Singh 	ctxt->fi.vector = X86_TRAP_PF;
410e759959fSBrijesh Singh 	ctxt->fi.error_code = error_code;
411e759959fSBrijesh Singh 	ctxt->fi.cr2 = (unsigned long)dst;
412e759959fSBrijesh Singh 
413e759959fSBrijesh Singh 	return ES_EXCEPTION;
414e759959fSBrijesh Singh }
415e759959fSBrijesh Singh 
vc_read_mem(struct es_em_ctxt * ctxt,char * src,char * buf,size_t size)416e759959fSBrijesh Singh static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
417e759959fSBrijesh Singh 				  char *src, char *buf, size_t size)
418e759959fSBrijesh Singh {
419e759959fSBrijesh Singh 	unsigned long error_code = X86_PF_PROT;
420e759959fSBrijesh Singh 
4214954f5b8SJoerg Roedel 	/*
4224954f5b8SJoerg Roedel 	 * This function uses __get_user() independent of whether kernel or user
4234954f5b8SJoerg Roedel 	 * memory is accessed. This works fine because __get_user() does no
4244954f5b8SJoerg Roedel 	 * sanity checks of the pointer being accessed. All that it does is
4254954f5b8SJoerg Roedel 	 * to report when the access failed.
4264954f5b8SJoerg Roedel 	 *
4274954f5b8SJoerg Roedel 	 * Also, this function runs in atomic context, so __get_user() is not
4284954f5b8SJoerg Roedel 	 * allowed to sleep. The page-fault handler detects that it is running
4294954f5b8SJoerg Roedel 	 * in atomic context and will not try to take mmap_sem and handle the
4304954f5b8SJoerg Roedel 	 * fault, so additional pagefault_enable()/disable() calls are not
4314954f5b8SJoerg Roedel 	 * needed.
4324954f5b8SJoerg Roedel 	 *
4334954f5b8SJoerg Roedel 	 * The access can't be done via copy_from_user() here because
4344954f5b8SJoerg Roedel 	 * vc_read_mem() must not use string instructions to access unsafe
4354954f5b8SJoerg Roedel 	 * memory. The reason is that MOVS is emulated by the #VC handler by
4364954f5b8SJoerg Roedel 	 * splitting the move up into a read and a write and taking a nested #VC
4374954f5b8SJoerg Roedel 	 * exception on whatever of them is the MMIO access. Using string
4384954f5b8SJoerg Roedel 	 * instructions here would cause infinite nesting.
4394954f5b8SJoerg Roedel 	 */
440e759959fSBrijesh Singh 	switch (size) {
4411d5379d0SMichael Sterritt 	case 1: {
4421d5379d0SMichael Sterritt 		u8 d1;
4431d5379d0SMichael Sterritt 		u8 __user *s = (u8 __user *)src;
4441d5379d0SMichael Sterritt 
4454954f5b8SJoerg Roedel 		if (__get_user(d1, s))
446e759959fSBrijesh Singh 			goto fault;
447e759959fSBrijesh Singh 		memcpy(buf, &d1, 1);
448e759959fSBrijesh Singh 		break;
4491d5379d0SMichael Sterritt 	}
4501d5379d0SMichael Sterritt 	case 2: {
4511d5379d0SMichael Sterritt 		u16 d2;
4521d5379d0SMichael Sterritt 		u16 __user *s = (u16 __user *)src;
4531d5379d0SMichael Sterritt 
4544954f5b8SJoerg Roedel 		if (__get_user(d2, s))
455e759959fSBrijesh Singh 			goto fault;
456e759959fSBrijesh Singh 		memcpy(buf, &d2, 2);
457e759959fSBrijesh Singh 		break;
4581d5379d0SMichael Sterritt 	}
4591d5379d0SMichael Sterritt 	case 4: {
4601d5379d0SMichael Sterritt 		u32 d4;
4611d5379d0SMichael Sterritt 		u32 __user *s = (u32 __user *)src;
4621d5379d0SMichael Sterritt 
4634954f5b8SJoerg Roedel 		if (__get_user(d4, s))
464e759959fSBrijesh Singh 			goto fault;
465e759959fSBrijesh Singh 		memcpy(buf, &d4, 4);
466e759959fSBrijesh Singh 		break;
4671d5379d0SMichael Sterritt 	}
4681d5379d0SMichael Sterritt 	case 8: {
4691d5379d0SMichael Sterritt 		u64 d8;
4701d5379d0SMichael Sterritt 		u64 __user *s = (u64 __user *)src;
4714954f5b8SJoerg Roedel 		if (__get_user(d8, s))
472e759959fSBrijesh Singh 			goto fault;
473e759959fSBrijesh Singh 		memcpy(buf, &d8, 8);
474e759959fSBrijesh Singh 		break;
4751d5379d0SMichael Sterritt 	}
476e759959fSBrijesh Singh 	default:
477e759959fSBrijesh Singh 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
478e759959fSBrijesh Singh 		return ES_UNSUPPORTED;
479e759959fSBrijesh Singh 	}
480e759959fSBrijesh Singh 
481e759959fSBrijesh Singh 	return ES_OK;
482e759959fSBrijesh Singh 
483e759959fSBrijesh Singh fault:
484e759959fSBrijesh Singh 	if (user_mode(ctxt->regs))
485e759959fSBrijesh Singh 		error_code |= X86_PF_USER;
486e759959fSBrijesh Singh 
487e759959fSBrijesh Singh 	ctxt->fi.vector = X86_TRAP_PF;
488e759959fSBrijesh Singh 	ctxt->fi.error_code = error_code;
489e759959fSBrijesh Singh 	ctxt->fi.cr2 = (unsigned long)src;
490e759959fSBrijesh Singh 
491e759959fSBrijesh Singh 	return ES_EXCEPTION;
492e759959fSBrijesh Singh }
493e759959fSBrijesh Singh 
vc_slow_virt_to_phys(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned long vaddr,phys_addr_t * paddr)494e759959fSBrijesh Singh static enum es_result vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
495e759959fSBrijesh Singh 					   unsigned long vaddr, phys_addr_t *paddr)
496e759959fSBrijesh Singh {
497e759959fSBrijesh Singh 	unsigned long va = (unsigned long)vaddr;
498e759959fSBrijesh Singh 	unsigned int level;
499e759959fSBrijesh Singh 	phys_addr_t pa;
500e759959fSBrijesh Singh 	pgd_t *pgd;
501e759959fSBrijesh Singh 	pte_t *pte;
502e759959fSBrijesh Singh 
503e759959fSBrijesh Singh 	pgd = __va(read_cr3_pa());
504e759959fSBrijesh Singh 	pgd = &pgd[pgd_index(va)];
505e759959fSBrijesh Singh 	pte = lookup_address_in_pgd(pgd, va, &level);
506e759959fSBrijesh Singh 	if (!pte) {
507e759959fSBrijesh Singh 		ctxt->fi.vector     = X86_TRAP_PF;
508e759959fSBrijesh Singh 		ctxt->fi.cr2        = vaddr;
509e759959fSBrijesh Singh 		ctxt->fi.error_code = 0;
510e759959fSBrijesh Singh 
511e759959fSBrijesh Singh 		if (user_mode(ctxt->regs))
512e759959fSBrijesh Singh 			ctxt->fi.error_code |= X86_PF_USER;
513e759959fSBrijesh Singh 
514e759959fSBrijesh Singh 		return ES_EXCEPTION;
515e759959fSBrijesh Singh 	}
516e759959fSBrijesh Singh 
517e759959fSBrijesh Singh 	if (WARN_ON_ONCE(pte_val(*pte) & _PAGE_ENC))
518e759959fSBrijesh Singh 		/* Emulated MMIO to/from encrypted memory not supported */
519e759959fSBrijesh Singh 		return ES_UNSUPPORTED;
520e759959fSBrijesh Singh 
521e759959fSBrijesh Singh 	pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
522e759959fSBrijesh Singh 	pa |= va & ~page_level_mask(level);
523e759959fSBrijesh Singh 
524e759959fSBrijesh Singh 	*paddr = pa;
525e759959fSBrijesh Singh 
526e759959fSBrijesh Singh 	return ES_OK;
527e759959fSBrijesh Singh }
528e759959fSBrijesh Singh 
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)529b9cb9c45SJoerg Roedel static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
530b9cb9c45SJoerg Roedel {
531b9cb9c45SJoerg Roedel 	BUG_ON(size > 4);
532b9cb9c45SJoerg Roedel 
533b9cb9c45SJoerg Roedel 	if (user_mode(ctxt->regs)) {
534b9cb9c45SJoerg Roedel 		struct thread_struct *t = &current->thread;
535b9cb9c45SJoerg Roedel 		struct io_bitmap *iobm = t->io_bitmap;
536b9cb9c45SJoerg Roedel 		size_t idx;
537b9cb9c45SJoerg Roedel 
538b9cb9c45SJoerg Roedel 		if (!iobm)
539b9cb9c45SJoerg Roedel 			goto fault;
540b9cb9c45SJoerg Roedel 
541b9cb9c45SJoerg Roedel 		for (idx = port; idx < port + size; ++idx) {
542b9cb9c45SJoerg Roedel 			if (test_bit(idx, iobm->bitmap))
543b9cb9c45SJoerg Roedel 				goto fault;
544b9cb9c45SJoerg Roedel 		}
545b9cb9c45SJoerg Roedel 	}
546b9cb9c45SJoerg Roedel 
547b9cb9c45SJoerg Roedel 	return ES_OK;
548b9cb9c45SJoerg Roedel 
549b9cb9c45SJoerg Roedel fault:
550b9cb9c45SJoerg Roedel 	ctxt->fi.vector = X86_TRAP_GP;
551b9cb9c45SJoerg Roedel 	ctxt->fi.error_code = 0;
552b9cb9c45SJoerg Roedel 
553b9cb9c45SJoerg Roedel 	return ES_EXCEPTION;
554b9cb9c45SJoerg Roedel }
555b9cb9c45SJoerg Roedel 
556e759959fSBrijesh Singh /* Include code shared with pre-decompression boot stage */
557e759959fSBrijesh Singh #include "sev-shared.c"
558e759959fSBrijesh Singh 
__sev_put_ghcb(struct ghcb_state * state)559d187f217SJoerg Roedel static noinstr void __sev_put_ghcb(struct ghcb_state *state)
560fea63d54STom Lendacky {
561fea63d54STom Lendacky 	struct sev_es_runtime_data *data;
562fea63d54STom Lendacky 	struct ghcb *ghcb;
563fea63d54STom Lendacky 
564d187f217SJoerg Roedel 	WARN_ON(!irqs_disabled());
565d187f217SJoerg Roedel 
566fea63d54STom Lendacky 	data = this_cpu_read(runtime_data);
567fea63d54STom Lendacky 	ghcb = &data->ghcb_page;
568fea63d54STom Lendacky 
569fea63d54STom Lendacky 	if (state->ghcb) {
570fea63d54STom Lendacky 		/* Restore GHCB from Backup */
571fea63d54STom Lendacky 		*ghcb = *state->ghcb;
572fea63d54STom Lendacky 		data->backup_ghcb_active = false;
573fea63d54STom Lendacky 		state->ghcb = NULL;
574fea63d54STom Lendacky 	} else {
575a50c5bebSTom Lendacky 		/*
576a50c5bebSTom Lendacky 		 * Invalidate the GHCB so a VMGEXIT instruction issued
577a50c5bebSTom Lendacky 		 * from userspace won't appear to be valid.
578a50c5bebSTom Lendacky 		 */
579a50c5bebSTom Lendacky 		vc_ghcb_invalidate(ghcb);
580fea63d54STom Lendacky 		data->ghcb_active = false;
581fea63d54STom Lendacky 	}
582fea63d54STom Lendacky }
583fea63d54STom Lendacky 
__sev_es_nmi_complete(void)584e759959fSBrijesh Singh void noinstr __sev_es_nmi_complete(void)
585e759959fSBrijesh Singh {
586e759959fSBrijesh Singh 	struct ghcb_state state;
587e759959fSBrijesh Singh 	struct ghcb *ghcb;
588e759959fSBrijesh Singh 
589d187f217SJoerg Roedel 	ghcb = __sev_get_ghcb(&state);
590e759959fSBrijesh Singh 
591e759959fSBrijesh Singh 	vc_ghcb_invalidate(ghcb);
592e759959fSBrijesh Singh 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
593e759959fSBrijesh Singh 	ghcb_set_sw_exit_info_1(ghcb, 0);
594e759959fSBrijesh Singh 	ghcb_set_sw_exit_info_2(ghcb, 0);
595e759959fSBrijesh Singh 
596e759959fSBrijesh Singh 	sev_es_wr_ghcb_msr(__pa_nodebug(ghcb));
597e759959fSBrijesh Singh 	VMGEXIT();
598e759959fSBrijesh Singh 
599d187f217SJoerg Roedel 	__sev_put_ghcb(&state);
600e759959fSBrijesh Singh }
601e759959fSBrijesh Singh 
get_secrets_page(void)602c2106a23SBrijesh Singh static u64 __init get_secrets_page(void)
603c2106a23SBrijesh Singh {
604c2106a23SBrijesh Singh 	u64 pa_data = boot_params.cc_blob_address;
605c2106a23SBrijesh Singh 	struct cc_blob_sev_info info;
606c2106a23SBrijesh Singh 	void *map;
607c2106a23SBrijesh Singh 
608c2106a23SBrijesh Singh 	/*
609c2106a23SBrijesh Singh 	 * The CC blob contains the address of the secrets page, check if the
610c2106a23SBrijesh Singh 	 * blob is present.
611c2106a23SBrijesh Singh 	 */
612c2106a23SBrijesh Singh 	if (!pa_data)
613c2106a23SBrijesh Singh 		return 0;
614c2106a23SBrijesh Singh 
615c2106a23SBrijesh Singh 	map = early_memremap(pa_data, sizeof(info));
616c2106a23SBrijesh Singh 	if (!map) {
617c2106a23SBrijesh Singh 		pr_err("Unable to locate SNP secrets page: failed to map the Confidential Computing blob.\n");
618c2106a23SBrijesh Singh 		return 0;
619c2106a23SBrijesh Singh 	}
620c2106a23SBrijesh Singh 	memcpy(&info, map, sizeof(info));
621c2106a23SBrijesh Singh 	early_memunmap(map, sizeof(info));
622c2106a23SBrijesh Singh 
623c2106a23SBrijesh Singh 	/* smoke-test the secrets page passed */
624c2106a23SBrijesh Singh 	if (!info.secrets_phys || info.secrets_len != PAGE_SIZE)
625c2106a23SBrijesh Singh 		return 0;
626c2106a23SBrijesh Singh 
627c2106a23SBrijesh Singh 	return info.secrets_phys;
628c2106a23SBrijesh Singh }
629c2106a23SBrijesh Singh 
get_snp_jump_table_addr(void)630c2106a23SBrijesh Singh static u64 __init get_snp_jump_table_addr(void)
631c2106a23SBrijesh Singh {
632c2106a23SBrijesh Singh 	struct snp_secrets_page_layout *layout;
633ab65f492SBorislav Petkov 	void __iomem *mem;
634c2106a23SBrijesh Singh 	u64 pa, addr;
635c2106a23SBrijesh Singh 
636c2106a23SBrijesh Singh 	pa = get_secrets_page();
637c2106a23SBrijesh Singh 	if (!pa)
638c2106a23SBrijesh Singh 		return 0;
639c2106a23SBrijesh Singh 
640ab65f492SBorislav Petkov 	mem = ioremap_encrypted(pa, PAGE_SIZE);
641ab65f492SBorislav Petkov 	if (!mem) {
642c2106a23SBrijesh Singh 		pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n");
643c2106a23SBrijesh Singh 		return 0;
644c2106a23SBrijesh Singh 	}
645c2106a23SBrijesh Singh 
646ab65f492SBorislav Petkov 	layout = (__force struct snp_secrets_page_layout *)mem;
647ab65f492SBorislav Petkov 
648c2106a23SBrijesh Singh 	addr = layout->os_area.ap_jump_table_pa;
649ab65f492SBorislav Petkov 	iounmap(mem);
650c2106a23SBrijesh Singh 
651c2106a23SBrijesh Singh 	return addr;
652c2106a23SBrijesh Singh }
653c2106a23SBrijesh Singh 
get_jump_table_addr(void)65475d359ecSMichael Roth static u64 __init get_jump_table_addr(void)
655e759959fSBrijesh Singh {
656e759959fSBrijesh Singh 	struct ghcb_state state;
657e759959fSBrijesh Singh 	unsigned long flags;
658e759959fSBrijesh Singh 	struct ghcb *ghcb;
659e759959fSBrijesh Singh 	u64 ret = 0;
660e759959fSBrijesh Singh 
661c2106a23SBrijesh Singh 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
662c2106a23SBrijesh Singh 		return get_snp_jump_table_addr();
663c2106a23SBrijesh Singh 
664e759959fSBrijesh Singh 	local_irq_save(flags);
665e759959fSBrijesh Singh 
666d187f217SJoerg Roedel 	ghcb = __sev_get_ghcb(&state);
667e759959fSBrijesh Singh 
668e759959fSBrijesh Singh 	vc_ghcb_invalidate(ghcb);
669e759959fSBrijesh Singh 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_JUMP_TABLE);
670e759959fSBrijesh Singh 	ghcb_set_sw_exit_info_1(ghcb, SVM_VMGEXIT_GET_AP_JUMP_TABLE);
671e759959fSBrijesh Singh 	ghcb_set_sw_exit_info_2(ghcb, 0);
672e759959fSBrijesh Singh 
673e759959fSBrijesh Singh 	sev_es_wr_ghcb_msr(__pa(ghcb));
674e759959fSBrijesh Singh 	VMGEXIT();
675e759959fSBrijesh Singh 
676e759959fSBrijesh Singh 	if (ghcb_sw_exit_info_1_is_valid(ghcb) &&
677e759959fSBrijesh Singh 	    ghcb_sw_exit_info_2_is_valid(ghcb))
678e759959fSBrijesh Singh 		ret = ghcb->save.sw_exit_info_2;
679e759959fSBrijesh Singh 
680d187f217SJoerg Roedel 	__sev_put_ghcb(&state);
681e759959fSBrijesh Singh 
682e759959fSBrijesh Singh 	local_irq_restore(flags);
683e759959fSBrijesh Singh 
684e759959fSBrijesh Singh 	return ret;
685e759959fSBrijesh Singh }
686e759959fSBrijesh Singh 
687*56408ed9SArd Biesheuvel static void __head
early_set_pages_state(unsigned long vaddr,unsigned long paddr,unsigned long npages,enum psc_op op)688*56408ed9SArd Biesheuvel early_set_pages_state(unsigned long vaddr, unsigned long paddr,
68915d90887STom Lendacky 		      unsigned long npages, enum psc_op op)
6905e5ccff6SBrijesh Singh {
6915e5ccff6SBrijesh Singh 	unsigned long paddr_end;
6925e5ccff6SBrijesh Singh 	u64 val;
69315d90887STom Lendacky 	int ret;
69415d90887STom Lendacky 
69515d90887STom Lendacky 	vaddr = vaddr & PAGE_MASK;
6965e5ccff6SBrijesh Singh 
6975e5ccff6SBrijesh Singh 	paddr = paddr & PAGE_MASK;
6985e5ccff6SBrijesh Singh 	paddr_end = paddr + (npages << PAGE_SHIFT);
6995e5ccff6SBrijesh Singh 
7005e5ccff6SBrijesh Singh 	while (paddr < paddr_end) {
70115d90887STom Lendacky 		if (op == SNP_PAGE_STATE_SHARED) {
70215d90887STom Lendacky 			/* Page validation must be rescinded before changing to shared */
70315d90887STom Lendacky 			ret = pvalidate(vaddr, RMP_PG_SIZE_4K, false);
70415d90887STom Lendacky 			if (WARN(ret, "Failed to validate address 0x%lx ret %d", paddr, ret))
70515d90887STom Lendacky 				goto e_term;
70615d90887STom Lendacky 		}
70715d90887STom Lendacky 
7085e5ccff6SBrijesh Singh 		/*
7095e5ccff6SBrijesh Singh 		 * Use the MSR protocol because this function can be called before
7105e5ccff6SBrijesh Singh 		 * the GHCB is established.
7115e5ccff6SBrijesh Singh 		 */
7125e5ccff6SBrijesh Singh 		sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
7135e5ccff6SBrijesh Singh 		VMGEXIT();
7145e5ccff6SBrijesh Singh 
7155e5ccff6SBrijesh Singh 		val = sev_es_rd_ghcb_msr();
7165e5ccff6SBrijesh Singh 
7175e5ccff6SBrijesh Singh 		if (WARN(GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP,
7185e5ccff6SBrijesh Singh 			 "Wrong PSC response code: 0x%x\n",
7195e5ccff6SBrijesh Singh 			 (unsigned int)GHCB_RESP_CODE(val)))
7205e5ccff6SBrijesh Singh 			goto e_term;
7215e5ccff6SBrijesh Singh 
7225e5ccff6SBrijesh Singh 		if (WARN(GHCB_MSR_PSC_RESP_VAL(val),
7235e5ccff6SBrijesh Singh 			 "Failed to change page state to '%s' paddr 0x%lx error 0x%llx\n",
7245e5ccff6SBrijesh Singh 			 op == SNP_PAGE_STATE_PRIVATE ? "private" : "shared",
7255e5ccff6SBrijesh Singh 			 paddr, GHCB_MSR_PSC_RESP_VAL(val)))
7265e5ccff6SBrijesh Singh 			goto e_term;
7275e5ccff6SBrijesh Singh 
72815d90887STom Lendacky 		if (op == SNP_PAGE_STATE_PRIVATE) {
72915d90887STom Lendacky 			/* Page validation must be performed after changing to private */
73015d90887STom Lendacky 			ret = pvalidate(vaddr, RMP_PG_SIZE_4K, true);
73115d90887STom Lendacky 			if (WARN(ret, "Failed to validate address 0x%lx ret %d", paddr, ret))
73215d90887STom Lendacky 				goto e_term;
73315d90887STom Lendacky 		}
73415d90887STom Lendacky 
73515d90887STom Lendacky 		vaddr += PAGE_SIZE;
73615d90887STom Lendacky 		paddr += PAGE_SIZE;
7375e5ccff6SBrijesh Singh 	}
7385e5ccff6SBrijesh Singh 
7395e5ccff6SBrijesh Singh 	return;
7405e5ccff6SBrijesh Singh 
7415e5ccff6SBrijesh Singh e_term:
7425e5ccff6SBrijesh Singh 	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
7435e5ccff6SBrijesh Singh }
7445e5ccff6SBrijesh Singh 
early_snp_set_memory_private(unsigned long vaddr,unsigned long paddr,unsigned long npages)745*56408ed9SArd Biesheuvel void __head early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
7465dee19b6STom Lendacky 					 unsigned long npages)
7475e5ccff6SBrijesh Singh {
748cdaa0a40STom Lendacky 	/*
749cdaa0a40STom Lendacky 	 * This can be invoked in early boot while running identity mapped, so
750cdaa0a40STom Lendacky 	 * use an open coded check for SNP instead of using cc_platform_has().
751cdaa0a40STom Lendacky 	 * This eliminates worries about jump tables or checking boot_cpu_data
752cdaa0a40STom Lendacky 	 * in the cc_platform_has() function.
753cdaa0a40STom Lendacky 	 */
7540982fd6bSArd Biesheuvel 	if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
7555e5ccff6SBrijesh Singh 		return;
7565e5ccff6SBrijesh Singh 
7575e5ccff6SBrijesh Singh 	 /*
7585e5ccff6SBrijesh Singh 	  * Ask the hypervisor to mark the memory pages as private in the RMP
7595e5ccff6SBrijesh Singh 	  * table.
7605e5ccff6SBrijesh Singh 	  */
76115d90887STom Lendacky 	early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_PRIVATE);
7625e5ccff6SBrijesh Singh }
7635e5ccff6SBrijesh Singh 
early_snp_set_memory_shared(unsigned long vaddr,unsigned long paddr,unsigned long npages)7645e5ccff6SBrijesh Singh void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
7655dee19b6STom Lendacky 					unsigned long npages)
7665e5ccff6SBrijesh Singh {
767cdaa0a40STom Lendacky 	/*
768cdaa0a40STom Lendacky 	 * This can be invoked in early boot while running identity mapped, so
769cdaa0a40STom Lendacky 	 * use an open coded check for SNP instead of using cc_platform_has().
770cdaa0a40STom Lendacky 	 * This eliminates worries about jump tables or checking boot_cpu_data
771cdaa0a40STom Lendacky 	 * in the cc_platform_has() function.
772cdaa0a40STom Lendacky 	 */
7730982fd6bSArd Biesheuvel 	if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
7745e5ccff6SBrijesh Singh 		return;
7755e5ccff6SBrijesh Singh 
7765e5ccff6SBrijesh Singh 	 /* Ask hypervisor to mark the memory pages shared in the RMP table. */
77715d90887STom Lendacky 	early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_SHARED);
7785e5ccff6SBrijesh Singh }
7795e5ccff6SBrijesh Singh 
__set_pages_state(struct snp_psc_desc * data,unsigned long vaddr,unsigned long vaddr_end,int op)78015d90887STom Lendacky static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr,
781dc3f3d24SBrijesh Singh 				       unsigned long vaddr_end, int op)
782dc3f3d24SBrijesh Singh {
7837006b755STom Lendacky 	struct ghcb_state state;
78415d90887STom Lendacky 	bool use_large_entry;
785dc3f3d24SBrijesh Singh 	struct psc_hdr *hdr;
786dc3f3d24SBrijesh Singh 	struct psc_entry *e;
7877006b755STom Lendacky 	unsigned long flags;
788dc3f3d24SBrijesh Singh 	unsigned long pfn;
7897006b755STom Lendacky 	struct ghcb *ghcb;
790dc3f3d24SBrijesh Singh 	int i;
791dc3f3d24SBrijesh Singh 
792dc3f3d24SBrijesh Singh 	hdr = &data->hdr;
793dc3f3d24SBrijesh Singh 	e = data->entries;
794dc3f3d24SBrijesh Singh 
795dc3f3d24SBrijesh Singh 	memset(data, 0, sizeof(*data));
796dc3f3d24SBrijesh Singh 	i = 0;
797dc3f3d24SBrijesh Singh 
79815d90887STom Lendacky 	while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) {
79915d90887STom Lendacky 		hdr->end_entry = i;
80015d90887STom Lendacky 
80115d90887STom Lendacky 		if (is_vmalloc_addr((void *)vaddr)) {
802dc3f3d24SBrijesh Singh 			pfn = vmalloc_to_pfn((void *)vaddr);
80315d90887STom Lendacky 			use_large_entry = false;
80415d90887STom Lendacky 		} else {
805dc3f3d24SBrijesh Singh 			pfn = __pa(vaddr) >> PAGE_SHIFT;
80615d90887STom Lendacky 			use_large_entry = true;
80715d90887STom Lendacky 		}
808dc3f3d24SBrijesh Singh 
809dc3f3d24SBrijesh Singh 		e->gfn = pfn;
810dc3f3d24SBrijesh Singh 		e->operation = op;
811dc3f3d24SBrijesh Singh 
81215d90887STom Lendacky 		if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) &&
81315d90887STom Lendacky 		    (vaddr_end - vaddr) >= PMD_SIZE) {
81415d90887STom Lendacky 			e->pagesize = RMP_PG_SIZE_2M;
81515d90887STom Lendacky 			vaddr += PMD_SIZE;
81615d90887STom Lendacky 		} else {
817dc3f3d24SBrijesh Singh 			e->pagesize = RMP_PG_SIZE_4K;
81815d90887STom Lendacky 			vaddr += PAGE_SIZE;
81915d90887STom Lendacky 		}
820dc3f3d24SBrijesh Singh 
821dc3f3d24SBrijesh Singh 		e++;
822dc3f3d24SBrijesh Singh 		i++;
823dc3f3d24SBrijesh Singh 	}
824dc3f3d24SBrijesh Singh 
82515d90887STom Lendacky 	/* Page validation must be rescinded before changing to shared */
82615d90887STom Lendacky 	if (op == SNP_PAGE_STATE_SHARED)
82715d90887STom Lendacky 		pvalidate_pages(data);
82815d90887STom Lendacky 
8297006b755STom Lendacky 	local_irq_save(flags);
8307006b755STom Lendacky 
8317006b755STom Lendacky 	if (sev_cfg.ghcbs_initialized)
8327006b755STom Lendacky 		ghcb = __sev_get_ghcb(&state);
8337006b755STom Lendacky 	else
8347006b755STom Lendacky 		ghcb = boot_ghcb;
8357006b755STom Lendacky 
83615d90887STom Lendacky 	/* Invoke the hypervisor to perform the page state changes */
8377006b755STom Lendacky 	if (!ghcb || vmgexit_psc(ghcb, data))
838dc3f3d24SBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
8397006b755STom Lendacky 
8407006b755STom Lendacky 	if (sev_cfg.ghcbs_initialized)
8417006b755STom Lendacky 		__sev_put_ghcb(&state);
8427006b755STom Lendacky 
8437006b755STom Lendacky 	local_irq_restore(flags);
84415d90887STom Lendacky 
84515d90887STom Lendacky 	/* Page validation must be performed after changing to private */
84615d90887STom Lendacky 	if (op == SNP_PAGE_STATE_PRIVATE)
84715d90887STom Lendacky 		pvalidate_pages(data);
84815d90887STom Lendacky 
84915d90887STom Lendacky 	return vaddr;
850dc3f3d24SBrijesh Singh }
851dc3f3d24SBrijesh Singh 
set_pages_state(unsigned long vaddr,unsigned long npages,int op)8525dee19b6STom Lendacky static void set_pages_state(unsigned long vaddr, unsigned long npages, int op)
853dc3f3d24SBrijesh Singh {
85469dcb1e3STom Lendacky 	struct snp_psc_desc desc;
85515d90887STom Lendacky 	unsigned long vaddr_end;
856dc3f3d24SBrijesh Singh 
8577006b755STom Lendacky 	/* Use the MSR protocol when a GHCB is not available. */
8587006b755STom Lendacky 	if (!boot_ghcb)
85915d90887STom Lendacky 		return early_set_pages_state(vaddr, __pa(vaddr), npages, op);
860dc3f3d24SBrijesh Singh 
861dc3f3d24SBrijesh Singh 	vaddr = vaddr & PAGE_MASK;
862dc3f3d24SBrijesh Singh 	vaddr_end = vaddr + (npages << PAGE_SHIFT);
863dc3f3d24SBrijesh Singh 
86415d90887STom Lendacky 	while (vaddr < vaddr_end)
86515d90887STom Lendacky 		vaddr = __set_pages_state(&desc, vaddr, vaddr_end, op);
866dc3f3d24SBrijesh Singh }
867dc3f3d24SBrijesh Singh 
snp_set_memory_shared(unsigned long vaddr,unsigned long npages)8685dee19b6STom Lendacky void snp_set_memory_shared(unsigned long vaddr, unsigned long npages)
869dc3f3d24SBrijesh Singh {
870dc3f3d24SBrijesh Singh 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
871dc3f3d24SBrijesh Singh 		return;
872dc3f3d24SBrijesh Singh 
873dc3f3d24SBrijesh Singh 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED);
874dc3f3d24SBrijesh Singh }
875dc3f3d24SBrijesh Singh 
snp_set_memory_private(unsigned long vaddr,unsigned long npages)8765dee19b6STom Lendacky void snp_set_memory_private(unsigned long vaddr, unsigned long npages)
877dc3f3d24SBrijesh Singh {
878dc3f3d24SBrijesh Singh 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
879dc3f3d24SBrijesh Singh 		return;
880dc3f3d24SBrijesh Singh 
881dc3f3d24SBrijesh Singh 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
882dc3f3d24SBrijesh Singh }
883dc3f3d24SBrijesh Singh 
snp_accept_memory(phys_addr_t start,phys_addr_t end)8846c321179STom Lendacky void snp_accept_memory(phys_addr_t start, phys_addr_t end)
8856c321179STom Lendacky {
88662d5e970STom Lendacky 	unsigned long vaddr, npages;
8876c321179STom Lendacky 
8886c321179STom Lendacky 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
8896c321179STom Lendacky 		return;
8906c321179STom Lendacky 
8916c321179STom Lendacky 	vaddr = (unsigned long)__va(start);
8926c321179STom Lendacky 	npages = (end - start) >> PAGE_SHIFT;
8936c321179STom Lendacky 
8946c321179STom Lendacky 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
895dc3f3d24SBrijesh Singh }
896dc3f3d24SBrijesh Singh 
snp_set_vmsa(void * va,bool vmsa)8970afb6b66STom Lendacky static int snp_set_vmsa(void *va, bool vmsa)
8980afb6b66STom Lendacky {
8990afb6b66STom Lendacky 	u64 attrs;
9000afb6b66STom Lendacky 
9010afb6b66STom Lendacky 	/*
9020afb6b66STom Lendacky 	 * Running at VMPL0 allows the kernel to change the VMSA bit for a page
9030afb6b66STom Lendacky 	 * using the RMPADJUST instruction. However, for the instruction to
9040afb6b66STom Lendacky 	 * succeed it must target the permissions of a lesser privileged
9050afb6b66STom Lendacky 	 * (higher numbered) VMPL level, so use VMPL1 (refer to the RMPADJUST
9060afb6b66STom Lendacky 	 * instruction in the AMD64 APM Volume 3).
9070afb6b66STom Lendacky 	 */
9080afb6b66STom Lendacky 	attrs = 1;
9090afb6b66STom Lendacky 	if (vmsa)
9100afb6b66STom Lendacky 		attrs |= RMPADJUST_VMSA_PAGE_BIT;
9110afb6b66STom Lendacky 
9120afb6b66STom Lendacky 	return rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs);
9130afb6b66STom Lendacky }
9140afb6b66STom Lendacky 
9150afb6b66STom Lendacky #define __ATTR_BASE		(SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK)
9160afb6b66STom Lendacky #define INIT_CS_ATTRIBS		(__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK)
9170afb6b66STom Lendacky #define INIT_DS_ATTRIBS		(__ATTR_BASE | SVM_SELECTOR_WRITE_MASK)
9180afb6b66STom Lendacky 
9190afb6b66STom Lendacky #define INIT_LDTR_ATTRIBS	(SVM_SELECTOR_P_MASK | 2)
9200afb6b66STom Lendacky #define INIT_TR_ATTRIBS		(SVM_SELECTOR_P_MASK | 3)
9210afb6b66STom Lendacky 
snp_alloc_vmsa_page(void)9220afb6b66STom Lendacky static void *snp_alloc_vmsa_page(void)
9230afb6b66STom Lendacky {
9240afb6b66STom Lendacky 	struct page *p;
9250afb6b66STom Lendacky 
9260afb6b66STom Lendacky 	/*
9270afb6b66STom Lendacky 	 * Allocate VMSA page to work around the SNP erratum where the CPU will
9280afb6b66STom Lendacky 	 * incorrectly signal an RMP violation #PF if a large page (2MB or 1GB)
9290afb6b66STom Lendacky 	 * collides with the RMP entry of VMSA page. The recommended workaround
9300afb6b66STom Lendacky 	 * is to not use a large page.
9310afb6b66STom Lendacky 	 *
9320afb6b66STom Lendacky 	 * Allocate an 8k page which is also 8k-aligned.
9330afb6b66STom Lendacky 	 */
9340afb6b66STom Lendacky 	p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1);
9350afb6b66STom Lendacky 	if (!p)
9360afb6b66STom Lendacky 		return NULL;
9370afb6b66STom Lendacky 
9380afb6b66STom Lendacky 	split_page(p, 1);
9390afb6b66STom Lendacky 
9400afb6b66STom Lendacky 	/* Free the first 4k. This page may be 2M/1G aligned and cannot be used. */
9410afb6b66STom Lendacky 	__free_page(p);
9420afb6b66STom Lendacky 
9430afb6b66STom Lendacky 	return page_address(p + 1);
9440afb6b66STom Lendacky }
9450afb6b66STom Lendacky 
snp_cleanup_vmsa(struct sev_es_save_area * vmsa)9460afb6b66STom Lendacky static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
9470afb6b66STom Lendacky {
9480afb6b66STom Lendacky 	int err;
9490afb6b66STom Lendacky 
9500afb6b66STom Lendacky 	err = snp_set_vmsa(vmsa, false);
9510afb6b66STom Lendacky 	if (err)
9520afb6b66STom Lendacky 		pr_err("clear VMSA page failed (%u), leaking page\n", err);
9530afb6b66STom Lendacky 	else
9540afb6b66STom Lendacky 		free_page((unsigned long)vmsa);
9550afb6b66STom Lendacky }
9560afb6b66STom Lendacky 
wakeup_cpu_via_vmgexit(int apic_id,unsigned long start_ip)9570afb6b66STom Lendacky static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip)
9580afb6b66STom Lendacky {
9590afb6b66STom Lendacky 	struct sev_es_save_area *cur_vmsa, *vmsa;
9600afb6b66STom Lendacky 	struct ghcb_state state;
9610afb6b66STom Lendacky 	unsigned long flags;
9620afb6b66STom Lendacky 	struct ghcb *ghcb;
9630afb6b66STom Lendacky 	u8 sipi_vector;
9640afb6b66STom Lendacky 	int cpu, ret;
9650afb6b66STom Lendacky 	u64 cr4;
9660afb6b66STom Lendacky 
9670afb6b66STom Lendacky 	/*
9680afb6b66STom Lendacky 	 * The hypervisor SNP feature support check has happened earlier, just check
9690afb6b66STom Lendacky 	 * the AP_CREATION one here.
9700afb6b66STom Lendacky 	 */
9710afb6b66STom Lendacky 	if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION))
9720afb6b66STom Lendacky 		return -EOPNOTSUPP;
9730afb6b66STom Lendacky 
9740afb6b66STom Lendacky 	/*
9750afb6b66STom Lendacky 	 * Verify the desired start IP against the known trampoline start IP
9760afb6b66STom Lendacky 	 * to catch any future new trampolines that may be introduced that
9770afb6b66STom Lendacky 	 * would require a new protected guest entry point.
9780afb6b66STom Lendacky 	 */
9790afb6b66STom Lendacky 	if (WARN_ONCE(start_ip != real_mode_header->trampoline_start,
9800afb6b66STom Lendacky 		      "Unsupported SNP start_ip: %lx\n", start_ip))
9810afb6b66STom Lendacky 		return -EINVAL;
9820afb6b66STom Lendacky 
9830afb6b66STom Lendacky 	/* Override start_ip with known protected guest start IP */
9840afb6b66STom Lendacky 	start_ip = real_mode_header->sev_es_trampoline_start;
9850afb6b66STom Lendacky 
9860afb6b66STom Lendacky 	/* Find the logical CPU for the APIC ID */
9870afb6b66STom Lendacky 	for_each_present_cpu(cpu) {
9880afb6b66STom Lendacky 		if (arch_match_cpu_phys_id(cpu, apic_id))
9890afb6b66STom Lendacky 			break;
9900afb6b66STom Lendacky 	}
9910afb6b66STom Lendacky 	if (cpu >= nr_cpu_ids)
9920afb6b66STom Lendacky 		return -EINVAL;
9930afb6b66STom Lendacky 
9940afb6b66STom Lendacky 	cur_vmsa = per_cpu(sev_vmsa, cpu);
9950afb6b66STom Lendacky 
9960afb6b66STom Lendacky 	/*
9970afb6b66STom Lendacky 	 * A new VMSA is created each time because there is no guarantee that
9980afb6b66STom Lendacky 	 * the current VMSA is the kernels or that the vCPU is not running. If
9990afb6b66STom Lendacky 	 * an attempt was done to use the current VMSA with a running vCPU, a
10000afb6b66STom Lendacky 	 * #VMEXIT of that vCPU would wipe out all of the settings being done
10010afb6b66STom Lendacky 	 * here.
10020afb6b66STom Lendacky 	 */
10030afb6b66STom Lendacky 	vmsa = (struct sev_es_save_area *)snp_alloc_vmsa_page();
10040afb6b66STom Lendacky 	if (!vmsa)
10050afb6b66STom Lendacky 		return -ENOMEM;
10060afb6b66STom Lendacky 
10070afb6b66STom Lendacky 	/* CR4 should maintain the MCE value */
10080afb6b66STom Lendacky 	cr4 = native_read_cr4() & X86_CR4_MCE;
10090afb6b66STom Lendacky 
10100afb6b66STom Lendacky 	/* Set the CS value based on the start_ip converted to a SIPI vector */
10110afb6b66STom Lendacky 	sipi_vector		= (start_ip >> 12);
10120afb6b66STom Lendacky 	vmsa->cs.base		= sipi_vector << 12;
10130afb6b66STom Lendacky 	vmsa->cs.limit		= AP_INIT_CS_LIMIT;
10140afb6b66STom Lendacky 	vmsa->cs.attrib		= INIT_CS_ATTRIBS;
10150afb6b66STom Lendacky 	vmsa->cs.selector	= sipi_vector << 8;
10160afb6b66STom Lendacky 
10170afb6b66STom Lendacky 	/* Set the RIP value based on start_ip */
10180afb6b66STom Lendacky 	vmsa->rip		= start_ip & 0xfff;
10190afb6b66STom Lendacky 
10200afb6b66STom Lendacky 	/* Set AP INIT defaults as documented in the APM */
10210afb6b66STom Lendacky 	vmsa->ds.limit		= AP_INIT_DS_LIMIT;
10220afb6b66STom Lendacky 	vmsa->ds.attrib		= INIT_DS_ATTRIBS;
10230afb6b66STom Lendacky 	vmsa->es		= vmsa->ds;
10240afb6b66STom Lendacky 	vmsa->fs		= vmsa->ds;
10250afb6b66STom Lendacky 	vmsa->gs		= vmsa->ds;
10260afb6b66STom Lendacky 	vmsa->ss		= vmsa->ds;
10270afb6b66STom Lendacky 
10280afb6b66STom Lendacky 	vmsa->gdtr.limit	= AP_INIT_GDTR_LIMIT;
10290afb6b66STom Lendacky 	vmsa->ldtr.limit	= AP_INIT_LDTR_LIMIT;
10300afb6b66STom Lendacky 	vmsa->ldtr.attrib	= INIT_LDTR_ATTRIBS;
10310afb6b66STom Lendacky 	vmsa->idtr.limit	= AP_INIT_IDTR_LIMIT;
10320afb6b66STom Lendacky 	vmsa->tr.limit		= AP_INIT_TR_LIMIT;
10330afb6b66STom Lendacky 	vmsa->tr.attrib		= INIT_TR_ATTRIBS;
10340afb6b66STom Lendacky 
10350afb6b66STom Lendacky 	vmsa->cr4		= cr4;
10360afb6b66STom Lendacky 	vmsa->cr0		= AP_INIT_CR0_DEFAULT;
10370afb6b66STom Lendacky 	vmsa->dr7		= DR7_RESET_VALUE;
10380afb6b66STom Lendacky 	vmsa->dr6		= AP_INIT_DR6_DEFAULT;
10390afb6b66STom Lendacky 	vmsa->rflags		= AP_INIT_RFLAGS_DEFAULT;
10400afb6b66STom Lendacky 	vmsa->g_pat		= AP_INIT_GPAT_DEFAULT;
10410afb6b66STom Lendacky 	vmsa->xcr0		= AP_INIT_XCR0_DEFAULT;
10420afb6b66STom Lendacky 	vmsa->mxcsr		= AP_INIT_MXCSR_DEFAULT;
10430afb6b66STom Lendacky 	vmsa->x87_ftw		= AP_INIT_X87_FTW_DEFAULT;
10440afb6b66STom Lendacky 	vmsa->x87_fcw		= AP_INIT_X87_FCW_DEFAULT;
10450afb6b66STom Lendacky 
10460afb6b66STom Lendacky 	/* SVME must be set. */
10470afb6b66STom Lendacky 	vmsa->efer		= EFER_SVME;
10480afb6b66STom Lendacky 
10490afb6b66STom Lendacky 	/*
10500afb6b66STom Lendacky 	 * Set the SNP-specific fields for this VMSA:
10510afb6b66STom Lendacky 	 *   VMPL level
10520afb6b66STom Lendacky 	 *   SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits)
10530afb6b66STom Lendacky 	 */
10540afb6b66STom Lendacky 	vmsa->vmpl		= 0;
10550afb6b66STom Lendacky 	vmsa->sev_features	= sev_status >> 2;
10560afb6b66STom Lendacky 
10570afb6b66STom Lendacky 	/* Switch the page over to a VMSA page now that it is initialized */
10580afb6b66STom Lendacky 	ret = snp_set_vmsa(vmsa, true);
10590afb6b66STom Lendacky 	if (ret) {
10600afb6b66STom Lendacky 		pr_err("set VMSA page failed (%u)\n", ret);
10610afb6b66STom Lendacky 		free_page((unsigned long)vmsa);
10620afb6b66STom Lendacky 
10630afb6b66STom Lendacky 		return -EINVAL;
10640afb6b66STom Lendacky 	}
10650afb6b66STom Lendacky 
10660afb6b66STom Lendacky 	/* Issue VMGEXIT AP Creation NAE event */
10670afb6b66STom Lendacky 	local_irq_save(flags);
10680afb6b66STom Lendacky 
10690afb6b66STom Lendacky 	ghcb = __sev_get_ghcb(&state);
10700afb6b66STom Lendacky 
10710afb6b66STom Lendacky 	vc_ghcb_invalidate(ghcb);
10720afb6b66STom Lendacky 	ghcb_set_rax(ghcb, vmsa->sev_features);
10730afb6b66STom Lendacky 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_CREATION);
10740afb6b66STom Lendacky 	ghcb_set_sw_exit_info_1(ghcb, ((u64)apic_id << 32) | SVM_VMGEXIT_AP_CREATE);
10750afb6b66STom Lendacky 	ghcb_set_sw_exit_info_2(ghcb, __pa(vmsa));
10760afb6b66STom Lendacky 
10770afb6b66STom Lendacky 	sev_es_wr_ghcb_msr(__pa(ghcb));
10780afb6b66STom Lendacky 	VMGEXIT();
10790afb6b66STom Lendacky 
10800afb6b66STom Lendacky 	if (!ghcb_sw_exit_info_1_is_valid(ghcb) ||
10810afb6b66STom Lendacky 	    lower_32_bits(ghcb->save.sw_exit_info_1)) {
10820afb6b66STom Lendacky 		pr_err("SNP AP Creation error\n");
10830afb6b66STom Lendacky 		ret = -EINVAL;
10840afb6b66STom Lendacky 	}
10850afb6b66STom Lendacky 
10860afb6b66STom Lendacky 	__sev_put_ghcb(&state);
10870afb6b66STom Lendacky 
10880afb6b66STom Lendacky 	local_irq_restore(flags);
10890afb6b66STom Lendacky 
10900afb6b66STom Lendacky 	/* Perform cleanup if there was an error */
10910afb6b66STom Lendacky 	if (ret) {
10920afb6b66STom Lendacky 		snp_cleanup_vmsa(vmsa);
10930afb6b66STom Lendacky 		vmsa = NULL;
10940afb6b66STom Lendacky 	}
10950afb6b66STom Lendacky 
10960afb6b66STom Lendacky 	/* Free up any previous VMSA page */
10970afb6b66STom Lendacky 	if (cur_vmsa)
10980afb6b66STom Lendacky 		snp_cleanup_vmsa(cur_vmsa);
10990afb6b66STom Lendacky 
11000afb6b66STom Lendacky 	/* Record the current VMSA page */
11010afb6b66STom Lendacky 	per_cpu(sev_vmsa, cpu) = vmsa;
11020afb6b66STom Lendacky 
11030afb6b66STom Lendacky 	return ret;
11040afb6b66STom Lendacky }
11050afb6b66STom Lendacky 
snp_set_wakeup_secondary_cpu(void)1106d6f361eaSThomas Gleixner void __init snp_set_wakeup_secondary_cpu(void)
11070afb6b66STom Lendacky {
11080afb6b66STom Lendacky 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
11090afb6b66STom Lendacky 		return;
11100afb6b66STom Lendacky 
11110afb6b66STom Lendacky 	/*
11120afb6b66STom Lendacky 	 * Always set this override if SNP is enabled. This makes it the
11130afb6b66STom Lendacky 	 * required method to start APs under SNP. If the hypervisor does
11140afb6b66STom Lendacky 	 * not support AP creation, then no APs will be started.
11150afb6b66STom Lendacky 	 */
1116d6f361eaSThomas Gleixner 	apic_update_callback(wakeup_secondary_cpu, wakeup_cpu_via_vmgexit);
11170afb6b66STom Lendacky }
11180afb6b66STom Lendacky 
sev_es_setup_ap_jump_table(struct real_mode_header * rmh)111975d359ecSMichael Roth int __init sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
1120e759959fSBrijesh Singh {
1121e759959fSBrijesh Singh 	u16 startup_cs, startup_ip;
1122e759959fSBrijesh Singh 	phys_addr_t jump_table_pa;
1123e759959fSBrijesh Singh 	u64 jump_table_addr;
1124e759959fSBrijesh Singh 	u16 __iomem *jump_table;
1125e759959fSBrijesh Singh 
1126e759959fSBrijesh Singh 	jump_table_addr = get_jump_table_addr();
1127e759959fSBrijesh Singh 
1128e759959fSBrijesh Singh 	/* On UP guests there is no jump table so this is not a failure */
1129e759959fSBrijesh Singh 	if (!jump_table_addr)
1130e759959fSBrijesh Singh 		return 0;
1131e759959fSBrijesh Singh 
1132e759959fSBrijesh Singh 	/* Check if AP Jump Table is page-aligned */
1133e759959fSBrijesh Singh 	if (jump_table_addr & ~PAGE_MASK)
1134e759959fSBrijesh Singh 		return -EINVAL;
1135e759959fSBrijesh Singh 
1136e759959fSBrijesh Singh 	jump_table_pa = jump_table_addr & PAGE_MASK;
1137e759959fSBrijesh Singh 
1138e759959fSBrijesh Singh 	startup_cs = (u16)(rmh->trampoline_start >> 4);
1139e759959fSBrijesh Singh 	startup_ip = (u16)(rmh->sev_es_trampoline_start -
1140e759959fSBrijesh Singh 			   rmh->trampoline_start);
1141e759959fSBrijesh Singh 
1142e759959fSBrijesh Singh 	jump_table = ioremap_encrypted(jump_table_pa, PAGE_SIZE);
1143e759959fSBrijesh Singh 	if (!jump_table)
1144e759959fSBrijesh Singh 		return -EIO;
1145e759959fSBrijesh Singh 
1146e759959fSBrijesh Singh 	writew(startup_ip, &jump_table[0]);
1147e759959fSBrijesh Singh 	writew(startup_cs, &jump_table[1]);
1148e759959fSBrijesh Singh 
1149e759959fSBrijesh Singh 	iounmap(jump_table);
1150e759959fSBrijesh Singh 
1151e759959fSBrijesh Singh 	return 0;
1152e759959fSBrijesh Singh }
1153e759959fSBrijesh Singh 
1154e759959fSBrijesh Singh /*
1155e759959fSBrijesh Singh  * This is needed by the OVMF UEFI firmware which will use whatever it finds in
1156e759959fSBrijesh Singh  * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
1157e759959fSBrijesh Singh  * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
1158e759959fSBrijesh Singh  */
sev_es_efi_map_ghcbs(pgd_t * pgd)1159e759959fSBrijesh Singh int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
1160e759959fSBrijesh Singh {
1161e759959fSBrijesh Singh 	struct sev_es_runtime_data *data;
1162e759959fSBrijesh Singh 	unsigned long address, pflags;
1163e759959fSBrijesh Singh 	int cpu;
1164e759959fSBrijesh Singh 	u64 pfn;
1165e759959fSBrijesh Singh 
11666283f2efSTom Lendacky 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1167e759959fSBrijesh Singh 		return 0;
1168e759959fSBrijesh Singh 
1169e759959fSBrijesh Singh 	pflags = _PAGE_NX | _PAGE_RW;
1170e759959fSBrijesh Singh 
1171e759959fSBrijesh Singh 	for_each_possible_cpu(cpu) {
1172e759959fSBrijesh Singh 		data = per_cpu(runtime_data, cpu);
1173e759959fSBrijesh Singh 
1174e759959fSBrijesh Singh 		address = __pa(&data->ghcb_page);
1175e759959fSBrijesh Singh 		pfn = address >> PAGE_SHIFT;
1176e759959fSBrijesh Singh 
1177e759959fSBrijesh Singh 		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
1178e759959fSBrijesh Singh 			return 1;
1179e759959fSBrijesh Singh 	}
1180e759959fSBrijesh Singh 
1181e759959fSBrijesh Singh 	return 0;
1182e759959fSBrijesh Singh }
1183e759959fSBrijesh Singh 
vc_handle_msr(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1184e759959fSBrijesh Singh static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1185e759959fSBrijesh Singh {
1186e759959fSBrijesh Singh 	struct pt_regs *regs = ctxt->regs;
1187e759959fSBrijesh Singh 	enum es_result ret;
1188e759959fSBrijesh Singh 	u64 exit_info_1;
1189e759959fSBrijesh Singh 
1190e759959fSBrijesh Singh 	/* Is it a WRMSR? */
1191e759959fSBrijesh Singh 	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;
1192e759959fSBrijesh Singh 
1193e759959fSBrijesh Singh 	ghcb_set_rcx(ghcb, regs->cx);
1194e759959fSBrijesh Singh 	if (exit_info_1) {
1195e759959fSBrijesh Singh 		ghcb_set_rax(ghcb, regs->ax);
1196e759959fSBrijesh Singh 		ghcb_set_rdx(ghcb, regs->dx);
1197e759959fSBrijesh Singh 	}
1198e759959fSBrijesh Singh 
11995bb6c1d1SBorislav Petkov 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, exit_info_1, 0);
1200e759959fSBrijesh Singh 
1201e759959fSBrijesh Singh 	if ((ret == ES_OK) && (!exit_info_1)) {
1202e759959fSBrijesh Singh 		regs->ax = ghcb->save.rax;
1203e759959fSBrijesh Singh 		regs->dx = ghcb->save.rdx;
1204e759959fSBrijesh Singh 	}
1205e759959fSBrijesh Singh 
1206e759959fSBrijesh Singh 	return ret;
1207e759959fSBrijesh Singh }
1208e759959fSBrijesh Singh 
snp_register_per_cpu_ghcb(void)120995d33bfaSBrijesh Singh static void snp_register_per_cpu_ghcb(void)
1210e759959fSBrijesh Singh {
121195d33bfaSBrijesh Singh 	struct sev_es_runtime_data *data;
121295d33bfaSBrijesh Singh 	struct ghcb *ghcb;
121395d33bfaSBrijesh Singh 
121495d33bfaSBrijesh Singh 	data = this_cpu_read(runtime_data);
121595d33bfaSBrijesh Singh 	ghcb = &data->ghcb_page;
121695d33bfaSBrijesh Singh 
121795d33bfaSBrijesh Singh 	snp_register_ghcb_early(__pa(ghcb));
121895d33bfaSBrijesh Singh }
121995d33bfaSBrijesh Singh 
setup_ghcb(void)122095d33bfaSBrijesh Singh void setup_ghcb(void)
122195d33bfaSBrijesh Singh {
122295d33bfaSBrijesh Singh 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
122395d33bfaSBrijesh Singh 		return;
122495d33bfaSBrijesh Singh 
122595d33bfaSBrijesh Singh 	/*
122695d33bfaSBrijesh Singh 	 * Check whether the runtime #VC exception handler is active. It uses
122795d33bfaSBrijesh Singh 	 * the per-CPU GHCB page which is set up by sev_es_init_vc_handling().
122895d33bfaSBrijesh Singh 	 *
122995d33bfaSBrijesh Singh 	 * If SNP is active, register the per-CPU GHCB page so that the runtime
123095d33bfaSBrijesh Singh 	 * exception handler can use it.
123195d33bfaSBrijesh Singh 	 */
123295d33bfaSBrijesh Singh 	if (initial_vc_handler == (unsigned long)kernel_exc_vmm_communication) {
123395d33bfaSBrijesh Singh 		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
123495d33bfaSBrijesh Singh 			snp_register_per_cpu_ghcb();
123595d33bfaSBrijesh Singh 
12367006b755STom Lendacky 		sev_cfg.ghcbs_initialized = true;
12377006b755STom Lendacky 
123895d33bfaSBrijesh Singh 		return;
123995d33bfaSBrijesh Singh 	}
1240e759959fSBrijesh Singh 
1241e759959fSBrijesh Singh 	/*
124273bbca12SAshwin Dayanand Kamat 	 * Make sure the hypervisor talks a supported protocol.
124373bbca12SAshwin Dayanand Kamat 	 * This gets called only in the BSP boot phase.
124473bbca12SAshwin Dayanand Kamat 	 */
124573bbca12SAshwin Dayanand Kamat 	if (!sev_es_negotiate_protocol())
124673bbca12SAshwin Dayanand Kamat 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
124773bbca12SAshwin Dayanand Kamat 
124873bbca12SAshwin Dayanand Kamat 	/*
1249e759959fSBrijesh Singh 	 * Clear the boot_ghcb. The first exception comes in before the bss
1250e759959fSBrijesh Singh 	 * section is cleared.
1251e759959fSBrijesh Singh 	 */
1252e759959fSBrijesh Singh 	memset(&boot_ghcb_page, 0, PAGE_SIZE);
1253e759959fSBrijesh Singh 
1254e759959fSBrijesh Singh 	/* Alright - Make the boot-ghcb public */
1255e759959fSBrijesh Singh 	boot_ghcb = &boot_ghcb_page;
1256e759959fSBrijesh Singh 
125795d33bfaSBrijesh Singh 	/* SNP guest requires that GHCB GPA must be registered. */
125895d33bfaSBrijesh Singh 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
125995d33bfaSBrijesh Singh 		snp_register_ghcb_early(__pa(&boot_ghcb_page));
1260e759959fSBrijesh Singh }
1261e759959fSBrijesh Singh 
1262e759959fSBrijesh Singh #ifdef CONFIG_HOTPLUG_CPU
sev_es_ap_hlt_loop(void)1263e759959fSBrijesh Singh static void sev_es_ap_hlt_loop(void)
1264e759959fSBrijesh Singh {
1265e759959fSBrijesh Singh 	struct ghcb_state state;
1266e759959fSBrijesh Singh 	struct ghcb *ghcb;
1267e759959fSBrijesh Singh 
1268d187f217SJoerg Roedel 	ghcb = __sev_get_ghcb(&state);
1269e759959fSBrijesh Singh 
1270e759959fSBrijesh Singh 	while (true) {
1271e759959fSBrijesh Singh 		vc_ghcb_invalidate(ghcb);
1272e759959fSBrijesh Singh 		ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_HLT_LOOP);
1273e759959fSBrijesh Singh 		ghcb_set_sw_exit_info_1(ghcb, 0);
1274e759959fSBrijesh Singh 		ghcb_set_sw_exit_info_2(ghcb, 0);
1275e759959fSBrijesh Singh 
1276e759959fSBrijesh Singh 		sev_es_wr_ghcb_msr(__pa(ghcb));
1277e759959fSBrijesh Singh 		VMGEXIT();
1278e759959fSBrijesh Singh 
1279e759959fSBrijesh Singh 		/* Wakeup signal? */
1280e759959fSBrijesh Singh 		if (ghcb_sw_exit_info_2_is_valid(ghcb) &&
1281e759959fSBrijesh Singh 		    ghcb->save.sw_exit_info_2)
1282e759959fSBrijesh Singh 			break;
1283e759959fSBrijesh Singh 	}
1284e759959fSBrijesh Singh 
1285d187f217SJoerg Roedel 	__sev_put_ghcb(&state);
1286e759959fSBrijesh Singh }
1287e759959fSBrijesh Singh 
1288e759959fSBrijesh Singh /*
1289e759959fSBrijesh Singh  * Play_dead handler when running under SEV-ES. This is needed because
1290e759959fSBrijesh Singh  * the hypervisor can't deliver an SIPI request to restart the AP.
1291e759959fSBrijesh Singh  * Instead the kernel has to issue a VMGEXIT to halt the VCPU until the
1292e759959fSBrijesh Singh  * hypervisor wakes it up again.
1293e759959fSBrijesh Singh  */
sev_es_play_dead(void)1294e759959fSBrijesh Singh static void sev_es_play_dead(void)
1295e759959fSBrijesh Singh {
1296e759959fSBrijesh Singh 	play_dead_common();
1297e759959fSBrijesh Singh 
1298e759959fSBrijesh Singh 	/* IRQs now disabled */
1299e759959fSBrijesh Singh 
1300e759959fSBrijesh Singh 	sev_es_ap_hlt_loop();
1301e759959fSBrijesh Singh 
1302e759959fSBrijesh Singh 	/*
1303e759959fSBrijesh Singh 	 * If we get here, the VCPU was woken up again. Jump to CPU
1304e759959fSBrijesh Singh 	 * startup code to get it back online.
1305e759959fSBrijesh Singh 	 */
1306666e1156SThomas Gleixner 	soft_restart_cpu();
1307e759959fSBrijesh Singh }
1308e759959fSBrijesh Singh #else  /* CONFIG_HOTPLUG_CPU */
1309e759959fSBrijesh Singh #define sev_es_play_dead	native_play_dead
1310e759959fSBrijesh Singh #endif /* CONFIG_HOTPLUG_CPU */
1311e759959fSBrijesh Singh 
1312e759959fSBrijesh Singh #ifdef CONFIG_SMP
sev_es_setup_play_dead(void)1313e759959fSBrijesh Singh static void __init sev_es_setup_play_dead(void)
1314e759959fSBrijesh Singh {
1315e759959fSBrijesh Singh 	smp_ops.play_dead = sev_es_play_dead;
1316e759959fSBrijesh Singh }
1317e759959fSBrijesh Singh #else
sev_es_setup_play_dead(void)1318e759959fSBrijesh Singh static inline void sev_es_setup_play_dead(void) { }
1319e759959fSBrijesh Singh #endif
1320e759959fSBrijesh Singh 
alloc_runtime_data(int cpu)1321e759959fSBrijesh Singh static void __init alloc_runtime_data(int cpu)
1322e759959fSBrijesh Singh {
1323e759959fSBrijesh Singh 	struct sev_es_runtime_data *data;
1324e759959fSBrijesh Singh 
1325e759959fSBrijesh Singh 	data = memblock_alloc(sizeof(*data), PAGE_SIZE);
1326e759959fSBrijesh Singh 	if (!data)
1327e759959fSBrijesh Singh 		panic("Can't allocate SEV-ES runtime data");
1328e759959fSBrijesh Singh 
1329e759959fSBrijesh Singh 	per_cpu(runtime_data, cpu) = data;
1330e759959fSBrijesh Singh }
1331e759959fSBrijesh Singh 
init_ghcb(int cpu)1332e759959fSBrijesh Singh static void __init init_ghcb(int cpu)
1333e759959fSBrijesh Singh {
1334e759959fSBrijesh Singh 	struct sev_es_runtime_data *data;
1335e759959fSBrijesh Singh 	int err;
1336e759959fSBrijesh Singh 
1337e759959fSBrijesh Singh 	data = per_cpu(runtime_data, cpu);
1338e759959fSBrijesh Singh 
1339e759959fSBrijesh Singh 	err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
1340e759959fSBrijesh Singh 					 sizeof(data->ghcb_page));
1341e759959fSBrijesh Singh 	if (err)
1342e759959fSBrijesh Singh 		panic("Can't map GHCBs unencrypted");
1343e759959fSBrijesh Singh 
1344e759959fSBrijesh Singh 	memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
1345e759959fSBrijesh Singh 
1346e759959fSBrijesh Singh 	data->ghcb_active = false;
1347e759959fSBrijesh Singh 	data->backup_ghcb_active = false;
1348e759959fSBrijesh Singh }
1349e759959fSBrijesh Singh 
sev_es_init_vc_handling(void)1350e759959fSBrijesh Singh void __init sev_es_init_vc_handling(void)
1351e759959fSBrijesh Singh {
1352e759959fSBrijesh Singh 	int cpu;
1353e759959fSBrijesh Singh 
1354e759959fSBrijesh Singh 	BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
1355e759959fSBrijesh Singh 
13566283f2efSTom Lendacky 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1357e759959fSBrijesh Singh 		return;
1358e759959fSBrijesh Singh 
1359e759959fSBrijesh Singh 	if (!sev_es_check_cpu_features())
1360e759959fSBrijesh Singh 		panic("SEV-ES CPU Features missing");
1361e759959fSBrijesh Singh 
1362cbd3d4f7SBrijesh Singh 	/*
1363cbd3d4f7SBrijesh Singh 	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
1364cbd3d4f7SBrijesh Singh 	 * features.
1365cbd3d4f7SBrijesh Singh 	 */
1366cbd3d4f7SBrijesh Singh 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
1367cbd3d4f7SBrijesh Singh 		sev_hv_features = get_hv_features();
1368cbd3d4f7SBrijesh Singh 
1369cbd3d4f7SBrijesh Singh 		if (!(sev_hv_features & GHCB_HV_FT_SNP))
1370cbd3d4f7SBrijesh Singh 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
1371cbd3d4f7SBrijesh Singh 	}
1372cbd3d4f7SBrijesh Singh 
1373e759959fSBrijesh Singh 	/* Initialize per-cpu GHCB pages */
1374e759959fSBrijesh Singh 	for_each_possible_cpu(cpu) {
1375e759959fSBrijesh Singh 		alloc_runtime_data(cpu);
1376e759959fSBrijesh Singh 		init_ghcb(cpu);
1377e759959fSBrijesh Singh 	}
1378e759959fSBrijesh Singh 
1379e759959fSBrijesh Singh 	sev_es_setup_play_dead();
1380e759959fSBrijesh Singh 
1381e759959fSBrijesh Singh 	/* Secondary CPUs use the runtime #VC handler */
1382be1a5408SJoerg Roedel 	initial_vc_handler = (unsigned long)kernel_exc_vmm_communication;
1383e759959fSBrijesh Singh }
1384e759959fSBrijesh Singh 
vc_early_forward_exception(struct es_em_ctxt * ctxt)1385e759959fSBrijesh Singh static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
1386e759959fSBrijesh Singh {
1387e759959fSBrijesh Singh 	int trapnr = ctxt->fi.vector;
1388e759959fSBrijesh Singh 
1389e759959fSBrijesh Singh 	if (trapnr == X86_TRAP_PF)
1390e759959fSBrijesh Singh 		native_write_cr2(ctxt->fi.cr2);
1391e759959fSBrijesh Singh 
1392e759959fSBrijesh Singh 	ctxt->regs->orig_ax = ctxt->fi.error_code;
1393e759959fSBrijesh Singh 	do_early_exception(ctxt->regs, trapnr);
1394e759959fSBrijesh Singh }
1395e759959fSBrijesh Singh 
vc_insn_get_rm(struct es_em_ctxt * ctxt)1396e759959fSBrijesh Singh static long *vc_insn_get_rm(struct es_em_ctxt *ctxt)
1397e759959fSBrijesh Singh {
1398e759959fSBrijesh Singh 	long *reg_array;
1399e759959fSBrijesh Singh 	int offset;
1400e759959fSBrijesh Singh 
1401e759959fSBrijesh Singh 	reg_array = (long *)ctxt->regs;
1402e759959fSBrijesh Singh 	offset    = insn_get_modrm_rm_off(&ctxt->insn, ctxt->regs);
1403e759959fSBrijesh Singh 
1404e759959fSBrijesh Singh 	if (offset < 0)
1405e759959fSBrijesh Singh 		return NULL;
1406e759959fSBrijesh Singh 
1407e759959fSBrijesh Singh 	offset /= sizeof(long);
1408e759959fSBrijesh Singh 
1409e759959fSBrijesh Singh 	return reg_array + offset;
1410e759959fSBrijesh Singh }
vc_do_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned int bytes,bool read)1411e759959fSBrijesh Singh static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
1412e759959fSBrijesh Singh 				 unsigned int bytes, bool read)
1413e759959fSBrijesh Singh {
1414e759959fSBrijesh Singh 	u64 exit_code, exit_info_1, exit_info_2;
1415e759959fSBrijesh Singh 	unsigned long ghcb_pa = __pa(ghcb);
1416e759959fSBrijesh Singh 	enum es_result res;
1417e759959fSBrijesh Singh 	phys_addr_t paddr;
1418e759959fSBrijesh Singh 	void __user *ref;
1419e759959fSBrijesh Singh 
1420e759959fSBrijesh Singh 	ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
1421e759959fSBrijesh Singh 	if (ref == (void __user *)-1L)
1422e759959fSBrijesh Singh 		return ES_UNSUPPORTED;
1423e759959fSBrijesh Singh 
1424e759959fSBrijesh Singh 	exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
1425e759959fSBrijesh Singh 
1426e759959fSBrijesh Singh 	res = vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr);
1427e759959fSBrijesh Singh 	if (res != ES_OK) {
1428e759959fSBrijesh Singh 		if (res == ES_EXCEPTION && !read)
1429e759959fSBrijesh Singh 			ctxt->fi.error_code |= X86_PF_WRITE;
1430e759959fSBrijesh Singh 
1431e759959fSBrijesh Singh 		return res;
1432e759959fSBrijesh Singh 	}
1433e759959fSBrijesh Singh 
1434e759959fSBrijesh Singh 	exit_info_1 = paddr;
1435e759959fSBrijesh Singh 	/* Can never be greater than 8 */
1436e759959fSBrijesh Singh 	exit_info_2 = bytes;
1437e759959fSBrijesh Singh 
1438e759959fSBrijesh Singh 	ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
1439e759959fSBrijesh Singh 
14405bb6c1d1SBorislav Petkov 	return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
1441e759959fSBrijesh Singh }
1442e759959fSBrijesh Singh 
1443e759959fSBrijesh Singh /*
1444e759959fSBrijesh Singh  * The MOVS instruction has two memory operands, which raises the
1445e759959fSBrijesh Singh  * problem that it is not known whether the access to the source or the
1446e759959fSBrijesh Singh  * destination caused the #VC exception (and hence whether an MMIO read
1447e759959fSBrijesh Singh  * or write operation needs to be emulated).
1448e759959fSBrijesh Singh  *
1449e759959fSBrijesh Singh  * Instead of playing games with walking page-tables and trying to guess
1450e759959fSBrijesh Singh  * whether the source or destination is an MMIO range, split the move
1451e759959fSBrijesh Singh  * into two operations, a read and a write with only one memory operand.
1452e759959fSBrijesh Singh  * This will cause a nested #VC exception on the MMIO address which can
1453e759959fSBrijesh Singh  * then be handled.
1454e759959fSBrijesh Singh  *
1455e759959fSBrijesh Singh  * This implementation has the benefit that it also supports MOVS where
1456e759959fSBrijesh Singh  * source _and_ destination are MMIO regions.
1457e759959fSBrijesh Singh  *
1458e759959fSBrijesh Singh  * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a
1459e759959fSBrijesh Singh  * rare operation. If it turns out to be a performance problem the split
1460e759959fSBrijesh Singh  * operations can be moved to memcpy_fromio() and memcpy_toio().
1461e759959fSBrijesh Singh  */
vc_handle_mmio_movs(struct es_em_ctxt * ctxt,unsigned int bytes)1462e759959fSBrijesh Singh static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,
1463e759959fSBrijesh Singh 					  unsigned int bytes)
1464e759959fSBrijesh Singh {
1465e759959fSBrijesh Singh 	unsigned long ds_base, es_base;
1466e759959fSBrijesh Singh 	unsigned char *src, *dst;
1467e759959fSBrijesh Singh 	unsigned char buffer[8];
1468e759959fSBrijesh Singh 	enum es_result ret;
1469e759959fSBrijesh Singh 	bool rep;
1470e759959fSBrijesh Singh 	int off;
1471e759959fSBrijesh Singh 
1472e759959fSBrijesh Singh 	ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS);
1473e759959fSBrijesh Singh 	es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
1474e759959fSBrijesh Singh 
1475e759959fSBrijesh Singh 	if (ds_base == -1L || es_base == -1L) {
1476e759959fSBrijesh Singh 		ctxt->fi.vector = X86_TRAP_GP;
1477e759959fSBrijesh Singh 		ctxt->fi.error_code = 0;
1478e759959fSBrijesh Singh 		return ES_EXCEPTION;
1479e759959fSBrijesh Singh 	}
1480e759959fSBrijesh Singh 
1481e759959fSBrijesh Singh 	src = ds_base + (unsigned char *)ctxt->regs->si;
1482e759959fSBrijesh Singh 	dst = es_base + (unsigned char *)ctxt->regs->di;
1483e759959fSBrijesh Singh 
1484e759959fSBrijesh Singh 	ret = vc_read_mem(ctxt, src, buffer, bytes);
1485e759959fSBrijesh Singh 	if (ret != ES_OK)
1486e759959fSBrijesh Singh 		return ret;
1487e759959fSBrijesh Singh 
1488e759959fSBrijesh Singh 	ret = vc_write_mem(ctxt, dst, buffer, bytes);
1489e759959fSBrijesh Singh 	if (ret != ES_OK)
1490e759959fSBrijesh Singh 		return ret;
1491e759959fSBrijesh Singh 
1492e759959fSBrijesh Singh 	if (ctxt->regs->flags & X86_EFLAGS_DF)
1493e759959fSBrijesh Singh 		off = -bytes;
1494e759959fSBrijesh Singh 	else
1495e759959fSBrijesh Singh 		off =  bytes;
1496e759959fSBrijesh Singh 
1497e759959fSBrijesh Singh 	ctxt->regs->si += off;
1498e759959fSBrijesh Singh 	ctxt->regs->di += off;
1499e759959fSBrijesh Singh 
1500e759959fSBrijesh Singh 	rep = insn_has_rep_prefix(&ctxt->insn);
1501e759959fSBrijesh Singh 	if (rep)
1502e759959fSBrijesh Singh 		ctxt->regs->cx -= 1;
1503e759959fSBrijesh Singh 
1504e759959fSBrijesh Singh 	if (!rep || ctxt->regs->cx == 0)
1505e759959fSBrijesh Singh 		return ES_OK;
1506e759959fSBrijesh Singh 	else
1507e759959fSBrijesh Singh 		return ES_RETRY;
1508e759959fSBrijesh Singh }
1509e759959fSBrijesh Singh 
vc_handle_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1510c494eb36SKirill A. Shutemov static enum es_result vc_handle_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1511e759959fSBrijesh Singh {
1512e759959fSBrijesh Singh 	struct insn *insn = &ctxt->insn;
151372bb8f8cSJason A. Donenfeld 	enum insn_mmio_type mmio;
1514e759959fSBrijesh Singh 	unsigned int bytes = 0;
1515e759959fSBrijesh Singh 	enum es_result ret;
1516c494eb36SKirill A. Shutemov 	u8 sign_byte;
1517e759959fSBrijesh Singh 	long *reg_data;
1518e759959fSBrijesh Singh 
1519c494eb36SKirill A. Shutemov 	mmio = insn_decode_mmio(insn, &bytes);
152072bb8f8cSJason A. Donenfeld 	if (mmio == INSN_MMIO_DECODE_FAILED)
1521e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
1522e759959fSBrijesh Singh 
152372bb8f8cSJason A. Donenfeld 	if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
1524c494eb36SKirill A. Shutemov 		reg_data = insn_get_modrm_reg_ptr(insn, ctxt->regs);
1525c494eb36SKirill A. Shutemov 		if (!reg_data)
1526c494eb36SKirill A. Shutemov 			return ES_DECODE_FAILED;
1527c494eb36SKirill A. Shutemov 	}
1528c494eb36SKirill A. Shutemov 
1529a37cd2a5SBorislav Petkov (AMD) 	if (user_mode(ctxt->regs))
1530a37cd2a5SBorislav Petkov (AMD) 		return ES_UNSUPPORTED;
1531a37cd2a5SBorislav Petkov (AMD) 
1532c494eb36SKirill A. Shutemov 	switch (mmio) {
153372bb8f8cSJason A. Donenfeld 	case INSN_MMIO_WRITE:
1534e759959fSBrijesh Singh 		memcpy(ghcb->shared_buffer, reg_data, bytes);
1535e759959fSBrijesh Singh 		ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1536e759959fSBrijesh Singh 		break;
153772bb8f8cSJason A. Donenfeld 	case INSN_MMIO_WRITE_IMM:
1538e759959fSBrijesh Singh 		memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
1539e759959fSBrijesh Singh 		ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1540e759959fSBrijesh Singh 		break;
154172bb8f8cSJason A. Donenfeld 	case INSN_MMIO_READ:
1542e759959fSBrijesh Singh 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1543e759959fSBrijesh Singh 		if (ret)
1544e759959fSBrijesh Singh 			break;
1545e759959fSBrijesh Singh 
1546e759959fSBrijesh Singh 		/* Zero-extend for 32-bit operation */
1547e759959fSBrijesh Singh 		if (bytes == 4)
1548e759959fSBrijesh Singh 			*reg_data = 0;
1549e759959fSBrijesh Singh 
1550e759959fSBrijesh Singh 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1551e759959fSBrijesh Singh 		break;
155272bb8f8cSJason A. Donenfeld 	case INSN_MMIO_READ_ZERO_EXTEND:
1553c494eb36SKirill A. Shutemov 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1554c494eb36SKirill A. Shutemov 		if (ret)
1555e759959fSBrijesh Singh 			break;
1556c494eb36SKirill A. Shutemov 
1557c494eb36SKirill A. Shutemov 		/* Zero extend based on operand size */
1558c494eb36SKirill A. Shutemov 		memset(reg_data, 0, insn->opnd_bytes);
1559c494eb36SKirill A. Shutemov 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1560c494eb36SKirill A. Shutemov 		break;
156172bb8f8cSJason A. Donenfeld 	case INSN_MMIO_READ_SIGN_EXTEND:
1562c494eb36SKirill A. Shutemov 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1563c494eb36SKirill A. Shutemov 		if (ret)
1564c494eb36SKirill A. Shutemov 			break;
1565c494eb36SKirill A. Shutemov 
1566c494eb36SKirill A. Shutemov 		if (bytes == 1) {
1567c494eb36SKirill A. Shutemov 			u8 *val = (u8 *)ghcb->shared_buffer;
1568c494eb36SKirill A. Shutemov 
1569c494eb36SKirill A. Shutemov 			sign_byte = (*val & 0x80) ? 0xff : 0x00;
1570c494eb36SKirill A. Shutemov 		} else {
1571c494eb36SKirill A. Shutemov 			u16 *val = (u16 *)ghcb->shared_buffer;
1572c494eb36SKirill A. Shutemov 
1573c494eb36SKirill A. Shutemov 			sign_byte = (*val & 0x8000) ? 0xff : 0x00;
1574c494eb36SKirill A. Shutemov 		}
1575c494eb36SKirill A. Shutemov 
1576c494eb36SKirill A. Shutemov 		/* Sign extend based on operand size */
1577c494eb36SKirill A. Shutemov 		memset(reg_data, sign_byte, insn->opnd_bytes);
1578c494eb36SKirill A. Shutemov 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1579c494eb36SKirill A. Shutemov 		break;
158072bb8f8cSJason A. Donenfeld 	case INSN_MMIO_MOVS:
1581c494eb36SKirill A. Shutemov 		ret = vc_handle_mmio_movs(ctxt, bytes);
1582e759959fSBrijesh Singh 		break;
1583e759959fSBrijesh Singh 	default:
1584e759959fSBrijesh Singh 		ret = ES_UNSUPPORTED;
1585c494eb36SKirill A. Shutemov 		break;
1586e759959fSBrijesh Singh 	}
1587e759959fSBrijesh Singh 
1588e759959fSBrijesh Singh 	return ret;
1589e759959fSBrijesh Singh }
1590e759959fSBrijesh Singh 
vc_handle_dr7_write(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1591e759959fSBrijesh Singh static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
1592e759959fSBrijesh Singh 					  struct es_em_ctxt *ctxt)
1593e759959fSBrijesh Singh {
1594e759959fSBrijesh Singh 	struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1595e759959fSBrijesh Singh 	long val, *reg = vc_insn_get_rm(ctxt);
1596e759959fSBrijesh Singh 	enum es_result ret;
1597e759959fSBrijesh Singh 
1598e221804dSAlexey Kardashevskiy 	if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1599e221804dSAlexey Kardashevskiy 		return ES_VMM_ERROR;
1600e221804dSAlexey Kardashevskiy 
1601e759959fSBrijesh Singh 	if (!reg)
1602e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
1603e759959fSBrijesh Singh 
1604e759959fSBrijesh Singh 	val = *reg;
1605e759959fSBrijesh Singh 
1606e759959fSBrijesh Singh 	/* Upper 32 bits must be written as zeroes */
1607e759959fSBrijesh Singh 	if (val >> 32) {
1608e759959fSBrijesh Singh 		ctxt->fi.vector = X86_TRAP_GP;
1609e759959fSBrijesh Singh 		ctxt->fi.error_code = 0;
1610e759959fSBrijesh Singh 		return ES_EXCEPTION;
1611e759959fSBrijesh Singh 	}
1612e759959fSBrijesh Singh 
1613e759959fSBrijesh Singh 	/* Clear out other reserved bits and set bit 10 */
1614e759959fSBrijesh Singh 	val = (val & 0xffff23ffL) | BIT(10);
1615e759959fSBrijesh Singh 
1616e759959fSBrijesh Singh 	/* Early non-zero writes to DR7 are not supported */
1617e759959fSBrijesh Singh 	if (!data && (val & ~DR7_RESET_VALUE))
1618e759959fSBrijesh Singh 		return ES_UNSUPPORTED;
1619e759959fSBrijesh Singh 
1620e759959fSBrijesh Singh 	/* Using a value of 0 for ExitInfo1 means RAX holds the value */
1621e759959fSBrijesh Singh 	ghcb_set_rax(ghcb, val);
16225bb6c1d1SBorislav Petkov 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
1623e759959fSBrijesh Singh 	if (ret != ES_OK)
1624e759959fSBrijesh Singh 		return ret;
1625e759959fSBrijesh Singh 
1626e759959fSBrijesh Singh 	if (data)
1627e759959fSBrijesh Singh 		data->dr7 = val;
1628e759959fSBrijesh Singh 
1629e759959fSBrijesh Singh 	return ES_OK;
1630e759959fSBrijesh Singh }
1631e759959fSBrijesh Singh 
vc_handle_dr7_read(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1632e759959fSBrijesh Singh static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
1633e759959fSBrijesh Singh 					 struct es_em_ctxt *ctxt)
1634e759959fSBrijesh Singh {
1635e759959fSBrijesh Singh 	struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1636e759959fSBrijesh Singh 	long *reg = vc_insn_get_rm(ctxt);
1637e759959fSBrijesh Singh 
1638e221804dSAlexey Kardashevskiy 	if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1639e221804dSAlexey Kardashevskiy 		return ES_VMM_ERROR;
1640e221804dSAlexey Kardashevskiy 
1641e759959fSBrijesh Singh 	if (!reg)
1642e759959fSBrijesh Singh 		return ES_DECODE_FAILED;
1643e759959fSBrijesh Singh 
1644e759959fSBrijesh Singh 	if (data)
1645e759959fSBrijesh Singh 		*reg = data->dr7;
1646e759959fSBrijesh Singh 	else
1647e759959fSBrijesh Singh 		*reg = DR7_RESET_VALUE;
1648e759959fSBrijesh Singh 
1649e759959fSBrijesh Singh 	return ES_OK;
1650e759959fSBrijesh Singh }
1651e759959fSBrijesh Singh 
vc_handle_wbinvd(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1652e759959fSBrijesh Singh static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
1653e759959fSBrijesh Singh 				       struct es_em_ctxt *ctxt)
1654e759959fSBrijesh Singh {
16555bb6c1d1SBorislav Petkov 	return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
1656e759959fSBrijesh Singh }
1657e759959fSBrijesh Singh 
vc_handle_rdpmc(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1658e759959fSBrijesh Singh static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1659e759959fSBrijesh Singh {
1660e759959fSBrijesh Singh 	enum es_result ret;
1661e759959fSBrijesh Singh 
1662e759959fSBrijesh Singh 	ghcb_set_rcx(ghcb, ctxt->regs->cx);
1663e759959fSBrijesh Singh 
16645bb6c1d1SBorislav Petkov 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
1665e759959fSBrijesh Singh 	if (ret != ES_OK)
1666e759959fSBrijesh Singh 		return ret;
1667e759959fSBrijesh Singh 
1668e759959fSBrijesh Singh 	if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
1669e759959fSBrijesh Singh 		return ES_VMM_ERROR;
1670e759959fSBrijesh Singh 
1671e759959fSBrijesh Singh 	ctxt->regs->ax = ghcb->save.rax;
1672e759959fSBrijesh Singh 	ctxt->regs->dx = ghcb->save.rdx;
1673e759959fSBrijesh Singh 
1674e759959fSBrijesh Singh 	return ES_OK;
1675e759959fSBrijesh Singh }
1676e759959fSBrijesh Singh 
vc_handle_monitor(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1677e759959fSBrijesh Singh static enum es_result vc_handle_monitor(struct ghcb *ghcb,
1678e759959fSBrijesh Singh 					struct es_em_ctxt *ctxt)
1679e759959fSBrijesh Singh {
1680e759959fSBrijesh Singh 	/*
1681e759959fSBrijesh Singh 	 * Treat it as a NOP and do not leak a physical address to the
1682e759959fSBrijesh Singh 	 * hypervisor.
1683e759959fSBrijesh Singh 	 */
1684e759959fSBrijesh Singh 	return ES_OK;
1685e759959fSBrijesh Singh }
1686e759959fSBrijesh Singh 
vc_handle_mwait(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1687e759959fSBrijesh Singh static enum es_result vc_handle_mwait(struct ghcb *ghcb,
1688e759959fSBrijesh Singh 				      struct es_em_ctxt *ctxt)
1689e759959fSBrijesh Singh {
1690e759959fSBrijesh Singh 	/* Treat the same as MONITOR/MONITORX */
1691e759959fSBrijesh Singh 	return ES_OK;
1692e759959fSBrijesh Singh }
1693e759959fSBrijesh Singh 
vc_handle_vmmcall(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1694e759959fSBrijesh Singh static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
1695e759959fSBrijesh Singh 					struct es_em_ctxt *ctxt)
1696e759959fSBrijesh Singh {
1697e759959fSBrijesh Singh 	enum es_result ret;
1698e759959fSBrijesh Singh 
1699e759959fSBrijesh Singh 	ghcb_set_rax(ghcb, ctxt->regs->ax);
1700e759959fSBrijesh Singh 	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
1701e759959fSBrijesh Singh 
1702e759959fSBrijesh Singh 	if (x86_platform.hyper.sev_es_hcall_prepare)
1703e759959fSBrijesh Singh 		x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
1704e759959fSBrijesh Singh 
17055bb6c1d1SBorislav Petkov 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
1706e759959fSBrijesh Singh 	if (ret != ES_OK)
1707e759959fSBrijesh Singh 		return ret;
1708e759959fSBrijesh Singh 
1709e759959fSBrijesh Singh 	if (!ghcb_rax_is_valid(ghcb))
1710e759959fSBrijesh Singh 		return ES_VMM_ERROR;
1711e759959fSBrijesh Singh 
1712e759959fSBrijesh Singh 	ctxt->regs->ax = ghcb->save.rax;
1713e759959fSBrijesh Singh 
1714e759959fSBrijesh Singh 	/*
1715e759959fSBrijesh Singh 	 * Call sev_es_hcall_finish() after regs->ax is already set.
1716e759959fSBrijesh Singh 	 * This allows the hypervisor handler to overwrite it again if
1717e759959fSBrijesh Singh 	 * necessary.
1718e759959fSBrijesh Singh 	 */
1719e759959fSBrijesh Singh 	if (x86_platform.hyper.sev_es_hcall_finish &&
1720e759959fSBrijesh Singh 	    !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
1721e759959fSBrijesh Singh 		return ES_VMM_ERROR;
1722e759959fSBrijesh Singh 
1723e759959fSBrijesh Singh 	return ES_OK;
1724e759959fSBrijesh Singh }
1725e759959fSBrijesh Singh 
vc_handle_trap_ac(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1726e759959fSBrijesh Singh static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
1727e759959fSBrijesh Singh 					struct es_em_ctxt *ctxt)
1728e759959fSBrijesh Singh {
1729e759959fSBrijesh Singh 	/*
1730e759959fSBrijesh Singh 	 * Calling ecx_alignment_check() directly does not work, because it
1731e759959fSBrijesh Singh 	 * enables IRQs and the GHCB is active. Forward the exception and call
1732e759959fSBrijesh Singh 	 * it later from vc_forward_exception().
1733e759959fSBrijesh Singh 	 */
1734e759959fSBrijesh Singh 	ctxt->fi.vector = X86_TRAP_AC;
1735e759959fSBrijesh Singh 	ctxt->fi.error_code = 0;
1736e759959fSBrijesh Singh 	return ES_EXCEPTION;
1737e759959fSBrijesh Singh }
1738e759959fSBrijesh Singh 
vc_handle_exitcode(struct es_em_ctxt * ctxt,struct ghcb * ghcb,unsigned long exit_code)1739e759959fSBrijesh Singh static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
1740e759959fSBrijesh Singh 					 struct ghcb *ghcb,
1741e759959fSBrijesh Singh 					 unsigned long exit_code)
1742e759959fSBrijesh Singh {
1743e759959fSBrijesh Singh 	enum es_result result;
1744e759959fSBrijesh Singh 
1745e759959fSBrijesh Singh 	switch (exit_code) {
1746e759959fSBrijesh Singh 	case SVM_EXIT_READ_DR7:
1747e759959fSBrijesh Singh 		result = vc_handle_dr7_read(ghcb, ctxt);
1748e759959fSBrijesh Singh 		break;
1749e759959fSBrijesh Singh 	case SVM_EXIT_WRITE_DR7:
1750e759959fSBrijesh Singh 		result = vc_handle_dr7_write(ghcb, ctxt);
1751e759959fSBrijesh Singh 		break;
1752e759959fSBrijesh Singh 	case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
1753e759959fSBrijesh Singh 		result = vc_handle_trap_ac(ghcb, ctxt);
1754e759959fSBrijesh Singh 		break;
1755e759959fSBrijesh Singh 	case SVM_EXIT_RDTSC:
1756e759959fSBrijesh Singh 	case SVM_EXIT_RDTSCP:
1757e759959fSBrijesh Singh 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
1758e759959fSBrijesh Singh 		break;
1759e759959fSBrijesh Singh 	case SVM_EXIT_RDPMC:
1760e759959fSBrijesh Singh 		result = vc_handle_rdpmc(ghcb, ctxt);
1761e759959fSBrijesh Singh 		break;
1762e759959fSBrijesh Singh 	case SVM_EXIT_INVD:
1763e759959fSBrijesh Singh 		pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
1764e759959fSBrijesh Singh 		result = ES_UNSUPPORTED;
1765e759959fSBrijesh Singh 		break;
1766e759959fSBrijesh Singh 	case SVM_EXIT_CPUID:
1767e759959fSBrijesh Singh 		result = vc_handle_cpuid(ghcb, ctxt);
1768e759959fSBrijesh Singh 		break;
1769e759959fSBrijesh Singh 	case SVM_EXIT_IOIO:
1770e759959fSBrijesh Singh 		result = vc_handle_ioio(ghcb, ctxt);
1771e759959fSBrijesh Singh 		break;
1772e759959fSBrijesh Singh 	case SVM_EXIT_MSR:
1773e759959fSBrijesh Singh 		result = vc_handle_msr(ghcb, ctxt);
1774e759959fSBrijesh Singh 		break;
1775e759959fSBrijesh Singh 	case SVM_EXIT_VMMCALL:
1776e759959fSBrijesh Singh 		result = vc_handle_vmmcall(ghcb, ctxt);
1777e759959fSBrijesh Singh 		break;
1778e759959fSBrijesh Singh 	case SVM_EXIT_WBINVD:
1779e759959fSBrijesh Singh 		result = vc_handle_wbinvd(ghcb, ctxt);
1780e759959fSBrijesh Singh 		break;
1781e759959fSBrijesh Singh 	case SVM_EXIT_MONITOR:
1782e759959fSBrijesh Singh 		result = vc_handle_monitor(ghcb, ctxt);
1783e759959fSBrijesh Singh 		break;
1784e759959fSBrijesh Singh 	case SVM_EXIT_MWAIT:
1785e759959fSBrijesh Singh 		result = vc_handle_mwait(ghcb, ctxt);
1786e759959fSBrijesh Singh 		break;
1787e759959fSBrijesh Singh 	case SVM_EXIT_NPF:
1788e759959fSBrijesh Singh 		result = vc_handle_mmio(ghcb, ctxt);
1789e759959fSBrijesh Singh 		break;
1790e759959fSBrijesh Singh 	default:
1791e759959fSBrijesh Singh 		/*
1792e759959fSBrijesh Singh 		 * Unexpected #VC exception
1793e759959fSBrijesh Singh 		 */
1794e759959fSBrijesh Singh 		result = ES_UNSUPPORTED;
1795e759959fSBrijesh Singh 	}
1796e759959fSBrijesh Singh 
1797e759959fSBrijesh Singh 	return result;
1798e759959fSBrijesh Singh }
1799e759959fSBrijesh Singh 
vc_forward_exception(struct es_em_ctxt * ctxt)1800e759959fSBrijesh Singh static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
1801e759959fSBrijesh Singh {
1802e759959fSBrijesh Singh 	long error_code = ctxt->fi.error_code;
1803e759959fSBrijesh Singh 	int trapnr = ctxt->fi.vector;
1804e759959fSBrijesh Singh 
1805e759959fSBrijesh Singh 	ctxt->regs->orig_ax = ctxt->fi.error_code;
1806e759959fSBrijesh Singh 
1807e759959fSBrijesh Singh 	switch (trapnr) {
1808e759959fSBrijesh Singh 	case X86_TRAP_GP:
1809e759959fSBrijesh Singh 		exc_general_protection(ctxt->regs, error_code);
1810e759959fSBrijesh Singh 		break;
1811e759959fSBrijesh Singh 	case X86_TRAP_UD:
1812e759959fSBrijesh Singh 		exc_invalid_op(ctxt->regs);
1813e759959fSBrijesh Singh 		break;
1814c25bbdb5SJoerg Roedel 	case X86_TRAP_PF:
1815c25bbdb5SJoerg Roedel 		write_cr2(ctxt->fi.cr2);
1816c25bbdb5SJoerg Roedel 		exc_page_fault(ctxt->regs, error_code);
1817c25bbdb5SJoerg Roedel 		break;
1818e759959fSBrijesh Singh 	case X86_TRAP_AC:
1819e759959fSBrijesh Singh 		exc_alignment_check(ctxt->regs, error_code);
1820e759959fSBrijesh Singh 		break;
1821e759959fSBrijesh Singh 	default:
1822e759959fSBrijesh Singh 		pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
1823e759959fSBrijesh Singh 		BUG();
1824e759959fSBrijesh Singh 	}
1825e759959fSBrijesh Singh }
1826e759959fSBrijesh Singh 
is_vc2_stack(unsigned long sp)1827ce47d0c0SJoerg Roedel static __always_inline bool is_vc2_stack(unsigned long sp)
1828e759959fSBrijesh Singh {
1829e759959fSBrijesh Singh 	return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
1830e759959fSBrijesh Singh }
1831e759959fSBrijesh Singh 
vc_from_invalid_context(struct pt_regs * regs)1832ce47d0c0SJoerg Roedel static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
1833ce47d0c0SJoerg Roedel {
1834ce47d0c0SJoerg Roedel 	unsigned long sp, prev_sp;
1835ce47d0c0SJoerg Roedel 
1836ce47d0c0SJoerg Roedel 	sp      = (unsigned long)regs;
1837ce47d0c0SJoerg Roedel 	prev_sp = regs->sp;
1838ce47d0c0SJoerg Roedel 
1839ce47d0c0SJoerg Roedel 	/*
1840ce47d0c0SJoerg Roedel 	 * If the code was already executing on the VC2 stack when the #VC
1841ce47d0c0SJoerg Roedel 	 * happened, let it proceed to the normal handling routine. This way the
1842ce47d0c0SJoerg Roedel 	 * code executing on the VC2 stack can cause #VC exceptions to get handled.
1843ce47d0c0SJoerg Roedel 	 */
1844ce47d0c0SJoerg Roedel 	return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
1845ce47d0c0SJoerg Roedel }
1846ce47d0c0SJoerg Roedel 
vc_raw_handle_exception(struct pt_regs * regs,unsigned long error_code)1847be1a5408SJoerg Roedel static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
1848e759959fSBrijesh Singh {
1849e759959fSBrijesh Singh 	struct ghcb_state state;
1850e759959fSBrijesh Singh 	struct es_em_ctxt ctxt;
1851e759959fSBrijesh Singh 	enum es_result result;
1852e759959fSBrijesh Singh 	struct ghcb *ghcb;
1853be1a5408SJoerg Roedel 	bool ret = true;
1854e759959fSBrijesh Singh 
1855d187f217SJoerg Roedel 	ghcb = __sev_get_ghcb(&state);
1856e759959fSBrijesh Singh 
1857e759959fSBrijesh Singh 	vc_ghcb_invalidate(ghcb);
1858e759959fSBrijesh Singh 	result = vc_init_em_ctxt(&ctxt, regs, error_code);
1859e759959fSBrijesh Singh 
1860e759959fSBrijesh Singh 	if (result == ES_OK)
1861e759959fSBrijesh Singh 		result = vc_handle_exitcode(&ctxt, ghcb, error_code);
1862e759959fSBrijesh Singh 
1863d187f217SJoerg Roedel 	__sev_put_ghcb(&state);
1864e759959fSBrijesh Singh 
1865e759959fSBrijesh Singh 	/* Done - now check the result */
1866e759959fSBrijesh Singh 	switch (result) {
1867e759959fSBrijesh Singh 	case ES_OK:
1868e759959fSBrijesh Singh 		vc_finish_insn(&ctxt);
1869e759959fSBrijesh Singh 		break;
1870e759959fSBrijesh Singh 	case ES_UNSUPPORTED:
18714aca2d99SJoerg Roedel 		pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
1872e759959fSBrijesh Singh 				   error_code, regs->ip);
1873be1a5408SJoerg Roedel 		ret = false;
1874be1a5408SJoerg Roedel 		break;
1875e759959fSBrijesh Singh 	case ES_VMM_ERROR:
1876e759959fSBrijesh Singh 		pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
1877e759959fSBrijesh Singh 				   error_code, regs->ip);
1878be1a5408SJoerg Roedel 		ret = false;
1879be1a5408SJoerg Roedel 		break;
1880e759959fSBrijesh Singh 	case ES_DECODE_FAILED:
1881e759959fSBrijesh Singh 		pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
1882e759959fSBrijesh Singh 				   error_code, regs->ip);
1883be1a5408SJoerg Roedel 		ret = false;
1884be1a5408SJoerg Roedel 		break;
1885e759959fSBrijesh Singh 	case ES_EXCEPTION:
1886e759959fSBrijesh Singh 		vc_forward_exception(&ctxt);
1887e759959fSBrijesh Singh 		break;
1888e759959fSBrijesh Singh 	case ES_RETRY:
1889e759959fSBrijesh Singh 		/* Nothing to do */
1890e759959fSBrijesh Singh 		break;
1891e759959fSBrijesh Singh 	default:
1892e759959fSBrijesh Singh 		pr_emerg("Unknown result in %s():%d\n", __func__, result);
1893e759959fSBrijesh Singh 		/*
1894e759959fSBrijesh Singh 		 * Emulating the instruction which caused the #VC exception
1895e759959fSBrijesh Singh 		 * failed - can't continue so print debug information
1896e759959fSBrijesh Singh 		 */
1897e759959fSBrijesh Singh 		BUG();
1898e759959fSBrijesh Singh 	}
1899e759959fSBrijesh Singh 
1900be1a5408SJoerg Roedel 	return ret;
1901be1a5408SJoerg Roedel }
1902e759959fSBrijesh Singh 
vc_is_db(unsigned long error_code)1903be1a5408SJoerg Roedel static __always_inline bool vc_is_db(unsigned long error_code)
1904be1a5408SJoerg Roedel {
1905be1a5408SJoerg Roedel 	return error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB;
1906be1a5408SJoerg Roedel }
1907e759959fSBrijesh Singh 
1908e759959fSBrijesh Singh /*
1909be1a5408SJoerg Roedel  * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode
1910be1a5408SJoerg Roedel  * and will panic when an error happens.
1911e759959fSBrijesh Singh  */
DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)1912be1a5408SJoerg Roedel DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
1913be1a5408SJoerg Roedel {
1914be1a5408SJoerg Roedel 	irqentry_state_t irq_state;
1915e759959fSBrijesh Singh 
1916be1a5408SJoerg Roedel 	/*
1917be1a5408SJoerg Roedel 	 * With the current implementation it is always possible to switch to a
1918be1a5408SJoerg Roedel 	 * safe stack because #VC exceptions only happen at known places, like
1919be1a5408SJoerg Roedel 	 * intercepted instructions or accesses to MMIO areas/IO ports. They can
1920be1a5408SJoerg Roedel 	 * also happen with code instrumentation when the hypervisor intercepts
1921be1a5408SJoerg Roedel 	 * #DB, but the critical paths are forbidden to be instrumented, so #DB
1922be1a5408SJoerg Roedel 	 * exceptions currently also only happen in safe places.
1923be1a5408SJoerg Roedel 	 *
1924be1a5408SJoerg Roedel 	 * But keep this here in case the noinstr annotations are violated due
1925be1a5408SJoerg Roedel 	 * to bug elsewhere.
1926be1a5408SJoerg Roedel 	 */
1927ce47d0c0SJoerg Roedel 	if (unlikely(vc_from_invalid_context(regs))) {
1928be1a5408SJoerg Roedel 		instrumentation_begin();
1929be1a5408SJoerg Roedel 		panic("Can't handle #VC exception from unsupported context\n");
1930be1a5408SJoerg Roedel 		instrumentation_end();
1931be1a5408SJoerg Roedel 	}
1932be1a5408SJoerg Roedel 
1933be1a5408SJoerg Roedel 	/*
1934be1a5408SJoerg Roedel 	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
1935be1a5408SJoerg Roedel 	 */
1936be1a5408SJoerg Roedel 	if (vc_is_db(error_code)) {
1937be1a5408SJoerg Roedel 		exc_debug(regs);
1938be1a5408SJoerg Roedel 		return;
1939be1a5408SJoerg Roedel 	}
1940be1a5408SJoerg Roedel 
1941be1a5408SJoerg Roedel 	irq_state = irqentry_nmi_enter(regs);
1942be1a5408SJoerg Roedel 
1943be1a5408SJoerg Roedel 	instrumentation_begin();
1944be1a5408SJoerg Roedel 
1945be1a5408SJoerg Roedel 	if (!vc_raw_handle_exception(regs, error_code)) {
1946e759959fSBrijesh Singh 		/* Show some debug info */
1947e759959fSBrijesh Singh 		show_regs(regs);
1948e759959fSBrijesh Singh 
1949e759959fSBrijesh Singh 		/* Ask hypervisor to sev_es_terminate */
19506c0f74d6SBrijesh Singh 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
1951e759959fSBrijesh Singh 
1952e759959fSBrijesh Singh 		/* If that fails and we get here - just panic */
1953e759959fSBrijesh Singh 		panic("Returned from Terminate-Request to Hypervisor\n");
1954e759959fSBrijesh Singh 	}
1955e759959fSBrijesh Singh 
1956e759959fSBrijesh Singh 	instrumentation_end();
1957be1a5408SJoerg Roedel 	irqentry_nmi_exit(regs, irq_state);
1958e759959fSBrijesh Singh }
1959e759959fSBrijesh Singh 
1960be1a5408SJoerg Roedel /*
1961be1a5408SJoerg Roedel  * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode
1962be1a5408SJoerg Roedel  * and will kill the current task with SIGBUS when an error happens.
1963be1a5408SJoerg Roedel  */
DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)1964be1a5408SJoerg Roedel DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
1965e759959fSBrijesh Singh {
1966be1a5408SJoerg Roedel 	/*
1967be1a5408SJoerg Roedel 	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
1968be1a5408SJoerg Roedel 	 */
1969be1a5408SJoerg Roedel 	if (vc_is_db(error_code)) {
1970be1a5408SJoerg Roedel 		noist_exc_debug(regs);
1971be1a5408SJoerg Roedel 		return;
1972be1a5408SJoerg Roedel 	}
1973be1a5408SJoerg Roedel 
1974be1a5408SJoerg Roedel 	irqentry_enter_from_user_mode(regs);
1975be1a5408SJoerg Roedel 	instrumentation_begin();
1976be1a5408SJoerg Roedel 
1977be1a5408SJoerg Roedel 	if (!vc_raw_handle_exception(regs, error_code)) {
1978be1a5408SJoerg Roedel 		/*
1979be1a5408SJoerg Roedel 		 * Do not kill the machine if user-space triggered the
1980be1a5408SJoerg Roedel 		 * exception. Send SIGBUS instead and let user-space deal with
1981be1a5408SJoerg Roedel 		 * it.
1982be1a5408SJoerg Roedel 		 */
1983be1a5408SJoerg Roedel 		force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
1984be1a5408SJoerg Roedel 	}
1985be1a5408SJoerg Roedel 
1986be1a5408SJoerg Roedel 	instrumentation_end();
1987be1a5408SJoerg Roedel 	irqentry_exit_to_user_mode(regs);
1988e759959fSBrijesh Singh }
1989e759959fSBrijesh Singh 
handle_vc_boot_ghcb(struct pt_regs * regs)1990e759959fSBrijesh Singh bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
1991e759959fSBrijesh Singh {
1992e759959fSBrijesh Singh 	unsigned long exit_code = regs->orig_ax;
1993e759959fSBrijesh Singh 	struct es_em_ctxt ctxt;
1994e759959fSBrijesh Singh 	enum es_result result;
1995e759959fSBrijesh Singh 
1996e759959fSBrijesh Singh 	vc_ghcb_invalidate(boot_ghcb);
1997e759959fSBrijesh Singh 
1998e759959fSBrijesh Singh 	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
1999e759959fSBrijesh Singh 	if (result == ES_OK)
2000e759959fSBrijesh Singh 		result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
2001e759959fSBrijesh Singh 
2002e759959fSBrijesh Singh 	/* Done - now check the result */
2003e759959fSBrijesh Singh 	switch (result) {
2004e759959fSBrijesh Singh 	case ES_OK:
2005e759959fSBrijesh Singh 		vc_finish_insn(&ctxt);
2006e759959fSBrijesh Singh 		break;
2007e759959fSBrijesh Singh 	case ES_UNSUPPORTED:
2008e759959fSBrijesh Singh 		early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
2009e759959fSBrijesh Singh 				exit_code, regs->ip);
2010e759959fSBrijesh Singh 		goto fail;
2011e759959fSBrijesh Singh 	case ES_VMM_ERROR:
2012e759959fSBrijesh Singh 		early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
2013e759959fSBrijesh Singh 				exit_code, regs->ip);
2014e759959fSBrijesh Singh 		goto fail;
2015e759959fSBrijesh Singh 	case ES_DECODE_FAILED:
2016e759959fSBrijesh Singh 		early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
2017e759959fSBrijesh Singh 				exit_code, regs->ip);
2018e759959fSBrijesh Singh 		goto fail;
2019e759959fSBrijesh Singh 	case ES_EXCEPTION:
2020e759959fSBrijesh Singh 		vc_early_forward_exception(&ctxt);
2021e759959fSBrijesh Singh 		break;
2022e759959fSBrijesh Singh 	case ES_RETRY:
2023e759959fSBrijesh Singh 		/* Nothing to do */
2024e759959fSBrijesh Singh 		break;
2025e759959fSBrijesh Singh 	default:
2026e759959fSBrijesh Singh 		BUG();
2027e759959fSBrijesh Singh 	}
2028e759959fSBrijesh Singh 
2029e759959fSBrijesh Singh 	return true;
2030e759959fSBrijesh Singh 
2031e759959fSBrijesh Singh fail:
2032e759959fSBrijesh Singh 	show_regs(regs);
2033e759959fSBrijesh Singh 
2034e720ea52SPeter Gonda 	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
2035e759959fSBrijesh Singh }
2036b190a043SMichael Roth 
2037b190a043SMichael Roth /*
2038b190a043SMichael Roth  * Initial set up of SNP relies on information provided by the
2039b190a043SMichael Roth  * Confidential Computing blob, which can be passed to the kernel
2040b190a043SMichael Roth  * in the following ways, depending on how it is booted:
2041b190a043SMichael Roth  *
2042b190a043SMichael Roth  * - when booted via the boot/decompress kernel:
2043b190a043SMichael Roth  *   - via boot_params
2044b190a043SMichael Roth  *
2045b190a043SMichael Roth  * - when booted directly by firmware/bootloader (e.g. CONFIG_PVH):
2046b190a043SMichael Roth  *   - via a setup_data entry, as defined by the Linux Boot Protocol
2047b190a043SMichael Roth  *
2048b190a043SMichael Roth  * Scan for the blob in that order.
2049b190a043SMichael Roth  */
find_cc_blob(struct boot_params * bp)2050*56408ed9SArd Biesheuvel static __head struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
2051b190a043SMichael Roth {
2052b190a043SMichael Roth 	struct cc_blob_sev_info *cc_info;
2053b190a043SMichael Roth 
2054b190a043SMichael Roth 	/* Boot kernel would have passed the CC blob via boot_params. */
2055b190a043SMichael Roth 	if (bp->cc_blob_address) {
2056b190a043SMichael Roth 		cc_info = (struct cc_blob_sev_info *)(unsigned long)bp->cc_blob_address;
2057b190a043SMichael Roth 		goto found_cc_info;
2058b190a043SMichael Roth 	}
2059b190a043SMichael Roth 
2060b190a043SMichael Roth 	/*
2061b190a043SMichael Roth 	 * If kernel was booted directly, without the use of the
2062b190a043SMichael Roth 	 * boot/decompression kernel, the CC blob may have been passed via
2063b190a043SMichael Roth 	 * setup_data instead.
2064b190a043SMichael Roth 	 */
2065b190a043SMichael Roth 	cc_info = find_cc_blob_setup_data(bp);
2066b190a043SMichael Roth 	if (!cc_info)
2067b190a043SMichael Roth 		return NULL;
2068b190a043SMichael Roth 
2069b190a043SMichael Roth found_cc_info:
2070b190a043SMichael Roth 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
2071b190a043SMichael Roth 		snp_abort();
2072b190a043SMichael Roth 
2073b190a043SMichael Roth 	return cc_info;
2074b190a043SMichael Roth }
2075b190a043SMichael Roth 
snp_init(struct boot_params * bp)2076*56408ed9SArd Biesheuvel bool __head snp_init(struct boot_params *bp)
2077b190a043SMichael Roth {
2078b190a043SMichael Roth 	struct cc_blob_sev_info *cc_info;
2079b190a043SMichael Roth 
2080b190a043SMichael Roth 	if (!bp)
2081b190a043SMichael Roth 		return false;
2082b190a043SMichael Roth 
2083b190a043SMichael Roth 	cc_info = find_cc_blob(bp);
2084b190a043SMichael Roth 	if (!cc_info)
2085b190a043SMichael Roth 		return false;
2086b190a043SMichael Roth 
208730612045SMichael Roth 	setup_cpuid_table(cc_info);
208830612045SMichael Roth 
2089b190a043SMichael Roth 	/*
2090b190a043SMichael Roth 	 * The CC blob will be used later to access the secrets page. Cache
2091b190a043SMichael Roth 	 * it here like the boot kernel does.
2092b190a043SMichael Roth 	 */
2093b190a043SMichael Roth 	bp->cc_blob_address = (u32)(unsigned long)cc_info;
2094b190a043SMichael Roth 
2095b190a043SMichael Roth 	return true;
2096b190a043SMichael Roth }
2097b190a043SMichael Roth 
snp_abort(void)2098*56408ed9SArd Biesheuvel void __head __noreturn snp_abort(void)
2099b190a043SMichael Roth {
2100b190a043SMichael Roth 	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
2101b190a043SMichael Roth }
210230612045SMichael Roth 
21034338e40dSKevin Loughlin /*
21044338e40dSKevin Loughlin  * SEV-SNP guests should only execute dmi_setup() if EFI_CONFIG_TABLES are
21054338e40dSKevin Loughlin  * enabled, as the alternative (fallback) logic for DMI probing in the legacy
21064338e40dSKevin Loughlin  * ROM region can cause a crash since this region is not pre-validated.
21074338e40dSKevin Loughlin  */
snp_dmi_setup(void)21084338e40dSKevin Loughlin void __init snp_dmi_setup(void)
21094338e40dSKevin Loughlin {
21104338e40dSKevin Loughlin 	if (efi_enabled(EFI_CONFIG_TABLES))
21114338e40dSKevin Loughlin 		dmi_setup();
21124338e40dSKevin Loughlin }
21134338e40dSKevin Loughlin 
dump_cpuid_table(void)2114ba37a143SMichael Roth static void dump_cpuid_table(void)
2115ba37a143SMichael Roth {
2116ba37a143SMichael Roth 	const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
2117ba37a143SMichael Roth 	int i = 0;
2118ba37a143SMichael Roth 
2119ba37a143SMichael Roth 	pr_info("count=%d reserved=0x%x reserved2=0x%llx\n",
2120ba37a143SMichael Roth 		cpuid_table->count, cpuid_table->__reserved1, cpuid_table->__reserved2);
2121ba37a143SMichael Roth 
2122ba37a143SMichael Roth 	for (i = 0; i < SNP_CPUID_COUNT_MAX; i++) {
2123ba37a143SMichael Roth 		const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
2124ba37a143SMichael Roth 
2125ba37a143SMichael Roth 		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",
2126ba37a143SMichael Roth 			i, fn->eax_in, fn->ecx_in, fn->eax, fn->ebx, fn->ecx,
2127ba37a143SMichael Roth 			fn->edx, fn->xcr0_in, fn->xss_in, fn->__reserved);
2128ba37a143SMichael Roth 	}
2129ba37a143SMichael Roth }
2130ba37a143SMichael Roth 
213130612045SMichael Roth /*
213230612045SMichael Roth  * It is useful from an auditing/testing perspective to provide an easy way
213330612045SMichael Roth  * for the guest owner to know that the CPUID table has been initialized as
213430612045SMichael Roth  * expected, but that initialization happens too early in boot to print any
213530612045SMichael Roth  * sort of indicator, and there's not really any other good place to do it,
213630612045SMichael Roth  * so do it here.
213730612045SMichael Roth  */
report_cpuid_table(void)213830612045SMichael Roth static int __init report_cpuid_table(void)
213930612045SMichael Roth {
214030612045SMichael Roth 	const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
214130612045SMichael Roth 
214230612045SMichael Roth 	if (!cpuid_table->count)
214330612045SMichael Roth 		return 0;
214430612045SMichael Roth 
214530612045SMichael Roth 	pr_info("Using SNP CPUID table, %d entries present.\n",
214630612045SMichael Roth 		cpuid_table->count);
214730612045SMichael Roth 
2148ba37a143SMichael Roth 	if (sev_cfg.debug)
2149ba37a143SMichael Roth 		dump_cpuid_table();
2150ba37a143SMichael Roth 
215130612045SMichael Roth 	return 0;
215230612045SMichael Roth }
215330612045SMichael Roth arch_initcall(report_cpuid_table);
2154ba37a143SMichael Roth 
init_sev_config(char * str)2155ba37a143SMichael Roth static int __init init_sev_config(char *str)
2156ba37a143SMichael Roth {
2157ba37a143SMichael Roth 	char *s;
2158ba37a143SMichael Roth 
2159ba37a143SMichael Roth 	while ((s = strsep(&str, ","))) {
2160ba37a143SMichael Roth 		if (!strcmp(s, "debug")) {
2161ba37a143SMichael Roth 			sev_cfg.debug = true;
2162ba37a143SMichael Roth 			continue;
2163ba37a143SMichael Roth 		}
2164ba37a143SMichael Roth 
2165ba37a143SMichael Roth 		pr_info("SEV command-line option '%s' was not recognized\n", s);
2166ba37a143SMichael Roth 	}
2167ba37a143SMichael Roth 
2168ba37a143SMichael Roth 	return 1;
2169ba37a143SMichael Roth }
2170ba37a143SMichael Roth __setup("sev=", init_sev_config);
2171d5af44ddSBrijesh Singh 
snp_issue_guest_request(u64 exit_code,struct snp_req_data * input,struct snp_guest_request_ioctl * rio)21720144e3b8SDionna Glaze int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
2173d5af44ddSBrijesh Singh {
2174d5af44ddSBrijesh Singh 	struct ghcb_state state;
2175d5af44ddSBrijesh Singh 	struct es_em_ctxt ctxt;
2176d5af44ddSBrijesh Singh 	unsigned long flags;
2177d5af44ddSBrijesh Singh 	struct ghcb *ghcb;
2178d5af44ddSBrijesh Singh 	int ret;
2179d5af44ddSBrijesh Singh 
21800144e3b8SDionna Glaze 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
2181d5af44ddSBrijesh Singh 
2182d5af44ddSBrijesh Singh 	/*
2183d5af44ddSBrijesh Singh 	 * __sev_get_ghcb() needs to run with IRQs disabled because it is using
2184d5af44ddSBrijesh Singh 	 * a per-CPU GHCB.
2185d5af44ddSBrijesh Singh 	 */
2186d5af44ddSBrijesh Singh 	local_irq_save(flags);
2187d5af44ddSBrijesh Singh 
2188d5af44ddSBrijesh Singh 	ghcb = __sev_get_ghcb(&state);
2189d5af44ddSBrijesh Singh 	if (!ghcb) {
2190d5af44ddSBrijesh Singh 		ret = -EIO;
2191d5af44ddSBrijesh Singh 		goto e_restore_irq;
2192d5af44ddSBrijesh Singh 	}
2193d5af44ddSBrijesh Singh 
2194d5af44ddSBrijesh Singh 	vc_ghcb_invalidate(ghcb);
2195d5af44ddSBrijesh Singh 
2196d5af44ddSBrijesh Singh 	if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2197d5af44ddSBrijesh Singh 		ghcb_set_rax(ghcb, input->data_gpa);
2198d5af44ddSBrijesh Singh 		ghcb_set_rbx(ghcb, input->data_npages);
2199d5af44ddSBrijesh Singh 	}
2200d5af44ddSBrijesh Singh 
22015bb6c1d1SBorislav Petkov 	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, exit_code, input->req_gpa, input->resp_gpa);
2202d5af44ddSBrijesh Singh 	if (ret)
2203d5af44ddSBrijesh Singh 		goto e_put;
2204d5af44ddSBrijesh Singh 
22050144e3b8SDionna Glaze 	rio->exitinfo2 = ghcb->save.sw_exit_info_2;
22060144e3b8SDionna Glaze 	switch (rio->exitinfo2) {
2207fa4ae42cSBorislav Petkov (AMD) 	case 0:
2208fa4ae42cSBorislav Petkov (AMD) 		break;
2209fa4ae42cSBorislav Petkov (AMD) 
22100144e3b8SDionna Glaze 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY):
221172f7754dSDionna Glaze 		ret = -EAGAIN;
221272f7754dSDionna Glaze 		break;
221372f7754dSDionna Glaze 
22140144e3b8SDionna Glaze 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
2215d5af44ddSBrijesh Singh 		/* Number of expected pages are returned in RBX */
2216fa4ae42cSBorislav Petkov (AMD) 		if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2217d5af44ddSBrijesh Singh 			input->data_npages = ghcb_get_rbx(ghcb);
2218970ab823SBorislav Petkov (AMD) 			ret = -ENOSPC;
2219fa4ae42cSBorislav Petkov (AMD) 			break;
2220d5af44ddSBrijesh Singh 		}
2221fa4ae42cSBorislav Petkov (AMD) 		fallthrough;
2222fa4ae42cSBorislav Petkov (AMD) 	default:
2223fa4ae42cSBorislav Petkov (AMD) 		ret = -EIO;
2224fa4ae42cSBorislav Petkov (AMD) 		break;
2225970ab823SBorislav Petkov (AMD) 	}
2226d5af44ddSBrijesh Singh 
2227d5af44ddSBrijesh Singh e_put:
2228d5af44ddSBrijesh Singh 	__sev_put_ghcb(&state);
2229d5af44ddSBrijesh Singh e_restore_irq:
2230d5af44ddSBrijesh Singh 	local_irq_restore(flags);
2231d5af44ddSBrijesh Singh 
2232d5af44ddSBrijesh Singh 	return ret;
2233d5af44ddSBrijesh Singh }
2234d5af44ddSBrijesh Singh EXPORT_SYMBOL_GPL(snp_issue_guest_request);
22353a45b375SBrijesh Singh 
22362bf93ffbSTom Lendacky static struct platform_device sev_guest_device = {
22372bf93ffbSTom Lendacky 	.name		= "sev-guest",
22383a45b375SBrijesh Singh 	.id		= -1,
22393a45b375SBrijesh Singh };
22403a45b375SBrijesh Singh 
snp_init_platform_device(void)22413a45b375SBrijesh Singh static int __init snp_init_platform_device(void)
22423a45b375SBrijesh Singh {
22432bf93ffbSTom Lendacky 	struct sev_guest_platform_data data;
22443a45b375SBrijesh Singh 	u64 gpa;
22453a45b375SBrijesh Singh 
22463a45b375SBrijesh Singh 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
22473a45b375SBrijesh Singh 		return -ENODEV;
22483a45b375SBrijesh Singh 
22493a45b375SBrijesh Singh 	gpa = get_secrets_page();
22503a45b375SBrijesh Singh 	if (!gpa)
22513a45b375SBrijesh Singh 		return -ENODEV;
22523a45b375SBrijesh Singh 
22533a45b375SBrijesh Singh 	data.secrets_gpa = gpa;
22542bf93ffbSTom Lendacky 	if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
22553a45b375SBrijesh Singh 		return -ENODEV;
22563a45b375SBrijesh Singh 
22572bf93ffbSTom Lendacky 	if (platform_device_register(&sev_guest_device))
22583a45b375SBrijesh Singh 		return -ENODEV;
22593a45b375SBrijesh Singh 
22603a45b375SBrijesh Singh 	pr_info("SNP guest platform device initialized.\n");
22613a45b375SBrijesh Singh 	return 0;
22623a45b375SBrijesh Singh }
22633a45b375SBrijesh Singh device_initcall(snp_init_platform_device);
2264