1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AMD Memory Encryption Support
4  *
5  * Copyright (C) 2019 SUSE
6  *
7  * Author: Joerg Roedel <[email protected]>
8  */
9 
10 #define pr_fmt(fmt)	"SEV: " fmt
11 
12 #include <linux/sched/debug.h>	/* For show_regs() */
13 #include <linux/percpu-defs.h>
14 #include <linux/cc_platform.h>
15 #include <linux/printk.h>
16 #include <linux/mm_types.h>
17 #include <linux/set_memory.h>
18 #include <linux/memblock.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/cpumask.h>
22 #include <linux/efi.h>
23 #include <linux/platform_device.h>
24 #include <linux/io.h>
25 #include <linux/psp-sev.h>
26 #include <linux/dmi.h>
27 #include <uapi/linux/sev-guest.h>
28 #include <crypto/gcm.h>
29 
30 #include <asm/init.h>
31 #include <asm/cpu_entry_area.h>
32 #include <asm/stacktrace.h>
33 #include <asm/sev.h>
34 #include <asm/insn-eval.h>
35 #include <asm/fpu/xcr.h>
36 #include <asm/processor.h>
37 #include <asm/realmode.h>
38 #include <asm/setup.h>
39 #include <asm/traps.h>
40 #include <asm/svm.h>
41 #include <asm/smp.h>
42 #include <asm/cpu.h>
43 #include <asm/apic.h>
44 #include <asm/cpuid.h>
45 #include <asm/cmdline.h>
46 
47 #define DR7_RESET_VALUE        0x400
48 
49 /* AP INIT values as documented in the APM2  section "Processor Initialization State" */
50 #define AP_INIT_CS_LIMIT		0xffff
51 #define AP_INIT_DS_LIMIT		0xffff
52 #define AP_INIT_LDTR_LIMIT		0xffff
53 #define AP_INIT_GDTR_LIMIT		0xffff
54 #define AP_INIT_IDTR_LIMIT		0xffff
55 #define AP_INIT_TR_LIMIT		0xffff
56 #define AP_INIT_RFLAGS_DEFAULT		0x2
57 #define AP_INIT_DR6_DEFAULT		0xffff0ff0
58 #define AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
59 #define AP_INIT_XCR0_DEFAULT		0x1
60 #define AP_INIT_X87_FTW_DEFAULT		0x5555
61 #define AP_INIT_X87_FCW_DEFAULT		0x0040
62 #define AP_INIT_CR0_DEFAULT		0x60000010
63 #define AP_INIT_MXCSR_DEFAULT		0x1f80
64 
65 static const char * const sev_status_feat_names[] = {
66 	[MSR_AMD64_SEV_ENABLED_BIT]		= "SEV",
67 	[MSR_AMD64_SEV_ES_ENABLED_BIT]		= "SEV-ES",
68 	[MSR_AMD64_SEV_SNP_ENABLED_BIT]		= "SEV-SNP",
69 	[MSR_AMD64_SNP_VTOM_BIT]		= "vTom",
70 	[MSR_AMD64_SNP_REFLECT_VC_BIT]		= "ReflectVC",
71 	[MSR_AMD64_SNP_RESTRICTED_INJ_BIT]	= "RI",
72 	[MSR_AMD64_SNP_ALT_INJ_BIT]		= "AI",
73 	[MSR_AMD64_SNP_DEBUG_SWAP_BIT]		= "DebugSwap",
74 	[MSR_AMD64_SNP_PREVENT_HOST_IBS_BIT]	= "NoHostIBS",
75 	[MSR_AMD64_SNP_BTB_ISOLATION_BIT]	= "BTBIsol",
76 	[MSR_AMD64_SNP_VMPL_SSS_BIT]		= "VmplSSS",
77 	[MSR_AMD64_SNP_SECURE_TSC_BIT]		= "SecureTSC",
78 	[MSR_AMD64_SNP_VMGEXIT_PARAM_BIT]	= "VMGExitParam",
79 	[MSR_AMD64_SNP_IBS_VIRT_BIT]		= "IBSVirt",
80 	[MSR_AMD64_SNP_VMSA_REG_PROT_BIT]	= "VMSARegProt",
81 	[MSR_AMD64_SNP_SMT_PROT_BIT]		= "SMTProt",
82 };
83 
84 /* For early boot hypervisor communication in SEV-ES enabled guests */
85 static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
86 
87 /*
88  * Needs to be in the .data section because we need it NULL before bss is
89  * cleared
90  */
91 static struct ghcb *boot_ghcb __section(".data");
92 
93 /* Bitmap of SEV features supported by the hypervisor */
94 static u64 sev_hv_features __ro_after_init;
95 
96 /* Secrets page physical address from the CC blob */
97 static u64 secrets_pa __ro_after_init;
98 
99 /*
100  * For Secure TSC guests, the BSP fetches TSC_INFO using SNP guest messaging and
101  * initializes snp_tsc_scale and snp_tsc_offset. These values are replicated
102  * across the APs VMSA fields (TSC_SCALE and TSC_OFFSET).
103  */
104 static u64 snp_tsc_scale __ro_after_init;
105 static u64 snp_tsc_offset __ro_after_init;
106 static u64 snp_tsc_freq_khz __ro_after_init;
107 
108 /* #VC handler runtime per-CPU data */
109 struct sev_es_runtime_data {
110 	struct ghcb ghcb_page;
111 
112 	/*
113 	 * Reserve one page per CPU as backup storage for the unencrypted GHCB.
114 	 * It is needed when an NMI happens while the #VC handler uses the real
115 	 * GHCB, and the NMI handler itself is causing another #VC exception. In
116 	 * that case the GHCB content of the first handler needs to be backed up
117 	 * and restored.
118 	 */
119 	struct ghcb backup_ghcb;
120 
121 	/*
122 	 * Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions.
123 	 * There is no need for it to be atomic, because nothing is written to
124 	 * the GHCB between the read and the write of ghcb_active. So it is safe
125 	 * to use it when a nested #VC exception happens before the write.
126 	 *
127 	 * This is necessary for example in the #VC->NMI->#VC case when the NMI
128 	 * happens while the first #VC handler uses the GHCB. When the NMI code
129 	 * raises a second #VC handler it might overwrite the contents of the
130 	 * GHCB written by the first handler. To avoid this the content of the
131 	 * GHCB is saved and restored when the GHCB is detected to be in use
132 	 * already.
133 	 */
134 	bool ghcb_active;
135 	bool backup_ghcb_active;
136 
137 	/*
138 	 * Cached DR7 value - write it on DR7 writes and return it on reads.
139 	 * That value will never make it to the real hardware DR7 as debugging
140 	 * is currently unsupported in SEV-ES guests.
141 	 */
142 	unsigned long dr7;
143 };
144 
145 struct ghcb_state {
146 	struct ghcb *ghcb;
147 };
148 
149 /* For early boot SVSM communication */
150 static struct svsm_ca boot_svsm_ca_page __aligned(PAGE_SIZE);
151 
152 static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
153 static DEFINE_PER_CPU(struct sev_es_save_area *, sev_vmsa);
154 static DEFINE_PER_CPU(struct svsm_ca *, svsm_caa);
155 static DEFINE_PER_CPU(u64, svsm_caa_pa);
156 
on_vc_stack(struct pt_regs * regs)157 static __always_inline bool on_vc_stack(struct pt_regs *regs)
158 {
159 	unsigned long sp = regs->sp;
160 
161 	/* User-mode RSP is not trusted */
162 	if (user_mode(regs))
163 		return false;
164 
165 	/* SYSCALL gap still has user-mode RSP */
166 	if (ip_within_syscall_gap(regs))
167 		return false;
168 
169 	return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
170 }
171 
172 /*
173  * This function handles the case when an NMI is raised in the #VC
174  * exception handler entry code, before the #VC handler has switched off
175  * its IST stack. In this case, the IST entry for #VC must be adjusted,
176  * so that any nested #VC exception will not overwrite the stack
177  * contents of the interrupted #VC handler.
178  *
179  * The IST entry is adjusted unconditionally so that it can be also be
180  * unconditionally adjusted back in __sev_es_ist_exit(). Otherwise a
181  * nested sev_es_ist_exit() call may adjust back the IST entry too
182  * early.
183  *
184  * The __sev_es_ist_enter() and __sev_es_ist_exit() functions always run
185  * on the NMI IST stack, as they are only called from NMI handling code
186  * right now.
187  */
__sev_es_ist_enter(struct pt_regs * regs)188 void noinstr __sev_es_ist_enter(struct pt_regs *regs)
189 {
190 	unsigned long old_ist, new_ist;
191 
192 	/* Read old IST entry */
193 	new_ist = old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
194 
195 	/*
196 	 * If NMI happened while on the #VC IST stack, set the new IST
197 	 * value below regs->sp, so that the interrupted stack frame is
198 	 * not overwritten by subsequent #VC exceptions.
199 	 */
200 	if (on_vc_stack(regs))
201 		new_ist = regs->sp;
202 
203 	/*
204 	 * Reserve additional 8 bytes and store old IST value so this
205 	 * adjustment can be unrolled in __sev_es_ist_exit().
206 	 */
207 	new_ist -= sizeof(old_ist);
208 	*(unsigned long *)new_ist = old_ist;
209 
210 	/* Set new IST entry */
211 	this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], new_ist);
212 }
213 
__sev_es_ist_exit(void)214 void noinstr __sev_es_ist_exit(void)
215 {
216 	unsigned long ist;
217 
218 	/* Read IST entry */
219 	ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
220 
221 	if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
222 		return;
223 
224 	/* Read back old IST entry and write it to the TSS */
225 	this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist);
226 }
227 
228 /*
229  * Nothing shall interrupt this code path while holding the per-CPU
230  * GHCB. The backup GHCB is only for NMIs interrupting this path.
231  *
232  * Callers must disable local interrupts around it.
233  */
__sev_get_ghcb(struct ghcb_state * state)234 static noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)
235 {
236 	struct sev_es_runtime_data *data;
237 	struct ghcb *ghcb;
238 
239 	WARN_ON(!irqs_disabled());
240 
241 	data = this_cpu_read(runtime_data);
242 	ghcb = &data->ghcb_page;
243 
244 	if (unlikely(data->ghcb_active)) {
245 		/* GHCB is already in use - save its contents */
246 
247 		if (unlikely(data->backup_ghcb_active)) {
248 			/*
249 			 * Backup-GHCB is also already in use. There is no way
250 			 * to continue here so just kill the machine. To make
251 			 * panic() work, mark GHCBs inactive so that messages
252 			 * can be printed out.
253 			 */
254 			data->ghcb_active        = false;
255 			data->backup_ghcb_active = false;
256 
257 			instrumentation_begin();
258 			panic("Unable to handle #VC exception! GHCB and Backup GHCB are already in use");
259 			instrumentation_end();
260 		}
261 
262 		/* Mark backup_ghcb active before writing to it */
263 		data->backup_ghcb_active = true;
264 
265 		state->ghcb = &data->backup_ghcb;
266 
267 		/* Backup GHCB content */
268 		*state->ghcb = *ghcb;
269 	} else {
270 		state->ghcb = NULL;
271 		data->ghcb_active = true;
272 	}
273 
274 	return ghcb;
275 }
276 
sev_es_rd_ghcb_msr(void)277 static inline u64 sev_es_rd_ghcb_msr(void)
278 {
279 	return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
280 }
281 
sev_es_wr_ghcb_msr(u64 val)282 static __always_inline void sev_es_wr_ghcb_msr(u64 val)
283 {
284 	u32 low, high;
285 
286 	low  = (u32)(val);
287 	high = (u32)(val >> 32);
288 
289 	native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
290 }
291 
vc_fetch_insn_kernel(struct es_em_ctxt * ctxt,unsigned char * buffer)292 static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
293 				unsigned char *buffer)
294 {
295 	return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
296 }
297 
__vc_decode_user_insn(struct es_em_ctxt * ctxt)298 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
299 {
300 	char buffer[MAX_INSN_SIZE];
301 	int insn_bytes;
302 
303 	insn_bytes = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
304 	if (insn_bytes == 0) {
305 		/* Nothing could be copied */
306 		ctxt->fi.vector     = X86_TRAP_PF;
307 		ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
308 		ctxt->fi.cr2        = ctxt->regs->ip;
309 		return ES_EXCEPTION;
310 	} else if (insn_bytes == -EINVAL) {
311 		/* Effective RIP could not be calculated */
312 		ctxt->fi.vector     = X86_TRAP_GP;
313 		ctxt->fi.error_code = 0;
314 		ctxt->fi.cr2        = 0;
315 		return ES_EXCEPTION;
316 	}
317 
318 	if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, insn_bytes))
319 		return ES_DECODE_FAILED;
320 
321 	if (ctxt->insn.immediate.got)
322 		return ES_OK;
323 	else
324 		return ES_DECODE_FAILED;
325 }
326 
__vc_decode_kern_insn(struct es_em_ctxt * ctxt)327 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
328 {
329 	char buffer[MAX_INSN_SIZE];
330 	int res, ret;
331 
332 	res = vc_fetch_insn_kernel(ctxt, buffer);
333 	if (res) {
334 		ctxt->fi.vector     = X86_TRAP_PF;
335 		ctxt->fi.error_code = X86_PF_INSTR;
336 		ctxt->fi.cr2        = ctxt->regs->ip;
337 		return ES_EXCEPTION;
338 	}
339 
340 	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
341 	if (ret < 0)
342 		return ES_DECODE_FAILED;
343 	else
344 		return ES_OK;
345 }
346 
vc_decode_insn(struct es_em_ctxt * ctxt)347 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
348 {
349 	if (user_mode(ctxt->regs))
350 		return __vc_decode_user_insn(ctxt);
351 	else
352 		return __vc_decode_kern_insn(ctxt);
353 }
354 
vc_write_mem(struct es_em_ctxt * ctxt,char * dst,char * buf,size_t size)355 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
356 				   char *dst, char *buf, size_t size)
357 {
358 	unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
359 
360 	/*
361 	 * This function uses __put_user() independent of whether kernel or user
362 	 * memory is accessed. This works fine because __put_user() does no
363 	 * sanity checks of the pointer being accessed. All that it does is
364 	 * to report when the access failed.
365 	 *
366 	 * Also, this function runs in atomic context, so __put_user() is not
367 	 * allowed to sleep. The page-fault handler detects that it is running
368 	 * in atomic context and will not try to take mmap_sem and handle the
369 	 * fault, so additional pagefault_enable()/disable() calls are not
370 	 * needed.
371 	 *
372 	 * The access can't be done via copy_to_user() here because
373 	 * vc_write_mem() must not use string instructions to access unsafe
374 	 * memory. The reason is that MOVS is emulated by the #VC handler by
375 	 * splitting the move up into a read and a write and taking a nested #VC
376 	 * exception on whatever of them is the MMIO access. Using string
377 	 * instructions here would cause infinite nesting.
378 	 */
379 	switch (size) {
380 	case 1: {
381 		u8 d1;
382 		u8 __user *target = (u8 __user *)dst;
383 
384 		memcpy(&d1, buf, 1);
385 		if (__put_user(d1, target))
386 			goto fault;
387 		break;
388 	}
389 	case 2: {
390 		u16 d2;
391 		u16 __user *target = (u16 __user *)dst;
392 
393 		memcpy(&d2, buf, 2);
394 		if (__put_user(d2, target))
395 			goto fault;
396 		break;
397 	}
398 	case 4: {
399 		u32 d4;
400 		u32 __user *target = (u32 __user *)dst;
401 
402 		memcpy(&d4, buf, 4);
403 		if (__put_user(d4, target))
404 			goto fault;
405 		break;
406 	}
407 	case 8: {
408 		u64 d8;
409 		u64 __user *target = (u64 __user *)dst;
410 
411 		memcpy(&d8, buf, 8);
412 		if (__put_user(d8, target))
413 			goto fault;
414 		break;
415 	}
416 	default:
417 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
418 		return ES_UNSUPPORTED;
419 	}
420 
421 	return ES_OK;
422 
423 fault:
424 	if (user_mode(ctxt->regs))
425 		error_code |= X86_PF_USER;
426 
427 	ctxt->fi.vector = X86_TRAP_PF;
428 	ctxt->fi.error_code = error_code;
429 	ctxt->fi.cr2 = (unsigned long)dst;
430 
431 	return ES_EXCEPTION;
432 }
433 
vc_read_mem(struct es_em_ctxt * ctxt,char * src,char * buf,size_t size)434 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
435 				  char *src, char *buf, size_t size)
436 {
437 	unsigned long error_code = X86_PF_PROT;
438 
439 	/*
440 	 * This function uses __get_user() independent of whether kernel or user
441 	 * memory is accessed. This works fine because __get_user() does no
442 	 * sanity checks of the pointer being accessed. All that it does is
443 	 * to report when the access failed.
444 	 *
445 	 * Also, this function runs in atomic context, so __get_user() is not
446 	 * allowed to sleep. The page-fault handler detects that it is running
447 	 * in atomic context and will not try to take mmap_sem and handle the
448 	 * fault, so additional pagefault_enable()/disable() calls are not
449 	 * needed.
450 	 *
451 	 * The access can't be done via copy_from_user() here because
452 	 * vc_read_mem() must not use string instructions to access unsafe
453 	 * memory. The reason is that MOVS is emulated by the #VC handler by
454 	 * splitting the move up into a read and a write and taking a nested #VC
455 	 * exception on whatever of them is the MMIO access. Using string
456 	 * instructions here would cause infinite nesting.
457 	 */
458 	switch (size) {
459 	case 1: {
460 		u8 d1;
461 		u8 __user *s = (u8 __user *)src;
462 
463 		if (__get_user(d1, s))
464 			goto fault;
465 		memcpy(buf, &d1, 1);
466 		break;
467 	}
468 	case 2: {
469 		u16 d2;
470 		u16 __user *s = (u16 __user *)src;
471 
472 		if (__get_user(d2, s))
473 			goto fault;
474 		memcpy(buf, &d2, 2);
475 		break;
476 	}
477 	case 4: {
478 		u32 d4;
479 		u32 __user *s = (u32 __user *)src;
480 
481 		if (__get_user(d4, s))
482 			goto fault;
483 		memcpy(buf, &d4, 4);
484 		break;
485 	}
486 	case 8: {
487 		u64 d8;
488 		u64 __user *s = (u64 __user *)src;
489 		if (__get_user(d8, s))
490 			goto fault;
491 		memcpy(buf, &d8, 8);
492 		break;
493 	}
494 	default:
495 		WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
496 		return ES_UNSUPPORTED;
497 	}
498 
499 	return ES_OK;
500 
501 fault:
502 	if (user_mode(ctxt->regs))
503 		error_code |= X86_PF_USER;
504 
505 	ctxt->fi.vector = X86_TRAP_PF;
506 	ctxt->fi.error_code = error_code;
507 	ctxt->fi.cr2 = (unsigned long)src;
508 
509 	return ES_EXCEPTION;
510 }
511 
vc_slow_virt_to_phys(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned long vaddr,phys_addr_t * paddr)512 static enum es_result vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
513 					   unsigned long vaddr, phys_addr_t *paddr)
514 {
515 	unsigned long va = (unsigned long)vaddr;
516 	unsigned int level;
517 	phys_addr_t pa;
518 	pgd_t *pgd;
519 	pte_t *pte;
520 
521 	pgd = __va(read_cr3_pa());
522 	pgd = &pgd[pgd_index(va)];
523 	pte = lookup_address_in_pgd(pgd, va, &level);
524 	if (!pte) {
525 		ctxt->fi.vector     = X86_TRAP_PF;
526 		ctxt->fi.cr2        = vaddr;
527 		ctxt->fi.error_code = 0;
528 
529 		if (user_mode(ctxt->regs))
530 			ctxt->fi.error_code |= X86_PF_USER;
531 
532 		return ES_EXCEPTION;
533 	}
534 
535 	if (WARN_ON_ONCE(pte_val(*pte) & _PAGE_ENC))
536 		/* Emulated MMIO to/from encrypted memory not supported */
537 		return ES_UNSUPPORTED;
538 
539 	pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
540 	pa |= va & ~page_level_mask(level);
541 
542 	*paddr = pa;
543 
544 	return ES_OK;
545 }
546 
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)547 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
548 {
549 	BUG_ON(size > 4);
550 
551 	if (user_mode(ctxt->regs)) {
552 		struct thread_struct *t = &current->thread;
553 		struct io_bitmap *iobm = t->io_bitmap;
554 		size_t idx;
555 
556 		if (!iobm)
557 			goto fault;
558 
559 		for (idx = port; idx < port + size; ++idx) {
560 			if (test_bit(idx, iobm->bitmap))
561 				goto fault;
562 		}
563 	}
564 
565 	return ES_OK;
566 
567 fault:
568 	ctxt->fi.vector = X86_TRAP_GP;
569 	ctxt->fi.error_code = 0;
570 
571 	return ES_EXCEPTION;
572 }
573 
vc_forward_exception(struct es_em_ctxt * ctxt)574 static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
575 {
576 	long error_code = ctxt->fi.error_code;
577 	int trapnr = ctxt->fi.vector;
578 
579 	ctxt->regs->orig_ax = ctxt->fi.error_code;
580 
581 	switch (trapnr) {
582 	case X86_TRAP_GP:
583 		exc_general_protection(ctxt->regs, error_code);
584 		break;
585 	case X86_TRAP_UD:
586 		exc_invalid_op(ctxt->regs);
587 		break;
588 	case X86_TRAP_PF:
589 		write_cr2(ctxt->fi.cr2);
590 		exc_page_fault(ctxt->regs, error_code);
591 		break;
592 	case X86_TRAP_AC:
593 		exc_alignment_check(ctxt->regs, error_code);
594 		break;
595 	default:
596 		pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
597 		BUG();
598 	}
599 }
600 
601 /* Include code shared with pre-decompression boot stage */
602 #include "shared.c"
603 
svsm_get_caa(void)604 static inline struct svsm_ca *svsm_get_caa(void)
605 {
606 	/*
607 	 * Use rIP-relative references when called early in the boot. If
608 	 * ->use_cas is set, then it is late in the boot and no need
609 	 * to worry about rIP-relative references.
610 	 */
611 	if (RIP_REL_REF(sev_cfg).use_cas)
612 		return this_cpu_read(svsm_caa);
613 	else
614 		return RIP_REL_REF(boot_svsm_caa);
615 }
616 
svsm_get_caa_pa(void)617 static u64 svsm_get_caa_pa(void)
618 {
619 	/*
620 	 * Use rIP-relative references when called early in the boot. If
621 	 * ->use_cas is set, then it is late in the boot and no need
622 	 * to worry about rIP-relative references.
623 	 */
624 	if (RIP_REL_REF(sev_cfg).use_cas)
625 		return this_cpu_read(svsm_caa_pa);
626 	else
627 		return RIP_REL_REF(boot_svsm_caa_pa);
628 }
629 
__sev_put_ghcb(struct ghcb_state * state)630 static noinstr void __sev_put_ghcb(struct ghcb_state *state)
631 {
632 	struct sev_es_runtime_data *data;
633 	struct ghcb *ghcb;
634 
635 	WARN_ON(!irqs_disabled());
636 
637 	data = this_cpu_read(runtime_data);
638 	ghcb = &data->ghcb_page;
639 
640 	if (state->ghcb) {
641 		/* Restore GHCB from Backup */
642 		*ghcb = *state->ghcb;
643 		data->backup_ghcb_active = false;
644 		state->ghcb = NULL;
645 	} else {
646 		/*
647 		 * Invalidate the GHCB so a VMGEXIT instruction issued
648 		 * from userspace won't appear to be valid.
649 		 */
650 		vc_ghcb_invalidate(ghcb);
651 		data->ghcb_active = false;
652 	}
653 }
654 
svsm_perform_call_protocol(struct svsm_call * call)655 static int svsm_perform_call_protocol(struct svsm_call *call)
656 {
657 	struct ghcb_state state;
658 	unsigned long flags;
659 	struct ghcb *ghcb;
660 	int ret;
661 
662 	/*
663 	 * This can be called very early in the boot, use native functions in
664 	 * order to avoid paravirt issues.
665 	 */
666 	flags = native_local_irq_save();
667 
668 	/*
669 	 * Use rip-relative references when called early in the boot. If
670 	 * ghcbs_initialized is set, then it is late in the boot and no need
671 	 * to worry about rip-relative references in called functions.
672 	 */
673 	if (RIP_REL_REF(sev_cfg).ghcbs_initialized)
674 		ghcb = __sev_get_ghcb(&state);
675 	else if (RIP_REL_REF(boot_ghcb))
676 		ghcb = RIP_REL_REF(boot_ghcb);
677 	else
678 		ghcb = NULL;
679 
680 	do {
681 		ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)
682 			   : svsm_perform_msr_protocol(call);
683 	} while (ret == -EAGAIN);
684 
685 	if (RIP_REL_REF(sev_cfg).ghcbs_initialized)
686 		__sev_put_ghcb(&state);
687 
688 	native_local_irq_restore(flags);
689 
690 	return ret;
691 }
692 
__sev_es_nmi_complete(void)693 void noinstr __sev_es_nmi_complete(void)
694 {
695 	struct ghcb_state state;
696 	struct ghcb *ghcb;
697 
698 	ghcb = __sev_get_ghcb(&state);
699 
700 	vc_ghcb_invalidate(ghcb);
701 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
702 	ghcb_set_sw_exit_info_1(ghcb, 0);
703 	ghcb_set_sw_exit_info_2(ghcb, 0);
704 
705 	sev_es_wr_ghcb_msr(__pa_nodebug(ghcb));
706 	VMGEXIT();
707 
708 	__sev_put_ghcb(&state);
709 }
710 
get_snp_jump_table_addr(void)711 static u64 __init get_snp_jump_table_addr(void)
712 {
713 	struct snp_secrets_page *secrets;
714 	void __iomem *mem;
715 	u64 addr;
716 
717 	mem = ioremap_encrypted(secrets_pa, PAGE_SIZE);
718 	if (!mem) {
719 		pr_err("Unable to locate AP jump table address: failed to map the SNP secrets page.\n");
720 		return 0;
721 	}
722 
723 	secrets = (__force struct snp_secrets_page *)mem;
724 
725 	addr = secrets->os_area.ap_jump_table_pa;
726 	iounmap(mem);
727 
728 	return addr;
729 }
730 
get_jump_table_addr(void)731 static u64 __init get_jump_table_addr(void)
732 {
733 	struct ghcb_state state;
734 	unsigned long flags;
735 	struct ghcb *ghcb;
736 	u64 ret = 0;
737 
738 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
739 		return get_snp_jump_table_addr();
740 
741 	local_irq_save(flags);
742 
743 	ghcb = __sev_get_ghcb(&state);
744 
745 	vc_ghcb_invalidate(ghcb);
746 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_JUMP_TABLE);
747 	ghcb_set_sw_exit_info_1(ghcb, SVM_VMGEXIT_GET_AP_JUMP_TABLE);
748 	ghcb_set_sw_exit_info_2(ghcb, 0);
749 
750 	sev_es_wr_ghcb_msr(__pa(ghcb));
751 	VMGEXIT();
752 
753 	if (ghcb_sw_exit_info_1_is_valid(ghcb) &&
754 	    ghcb_sw_exit_info_2_is_valid(ghcb))
755 		ret = ghcb->save.sw_exit_info_2;
756 
757 	__sev_put_ghcb(&state);
758 
759 	local_irq_restore(flags);
760 
761 	return ret;
762 }
763 
764 static void __head
early_set_pages_state(unsigned long vaddr,unsigned long paddr,unsigned long npages,enum psc_op op)765 early_set_pages_state(unsigned long vaddr, unsigned long paddr,
766 		      unsigned long npages, enum psc_op op)
767 {
768 	unsigned long paddr_end;
769 	u64 val;
770 
771 	vaddr = vaddr & PAGE_MASK;
772 
773 	paddr = paddr & PAGE_MASK;
774 	paddr_end = paddr + (npages << PAGE_SHIFT);
775 
776 	while (paddr < paddr_end) {
777 		/* Page validation must be rescinded before changing to shared */
778 		if (op == SNP_PAGE_STATE_SHARED)
779 			pvalidate_4k_page(vaddr, paddr, false);
780 
781 		/*
782 		 * Use the MSR protocol because this function can be called before
783 		 * the GHCB is established.
784 		 */
785 		sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
786 		VMGEXIT();
787 
788 		val = sev_es_rd_ghcb_msr();
789 
790 		if (GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP)
791 			goto e_term;
792 
793 		if (GHCB_MSR_PSC_RESP_VAL(val))
794 			goto e_term;
795 
796 		/* Page validation must be performed after changing to private */
797 		if (op == SNP_PAGE_STATE_PRIVATE)
798 			pvalidate_4k_page(vaddr, paddr, true);
799 
800 		vaddr += PAGE_SIZE;
801 		paddr += PAGE_SIZE;
802 	}
803 
804 	return;
805 
806 e_term:
807 	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
808 }
809 
early_snp_set_memory_private(unsigned long vaddr,unsigned long paddr,unsigned long npages)810 void __head early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
811 					 unsigned long npages)
812 {
813 	/*
814 	 * This can be invoked in early boot while running identity mapped, so
815 	 * use an open coded check for SNP instead of using cc_platform_has().
816 	 * This eliminates worries about jump tables or checking boot_cpu_data
817 	 * in the cc_platform_has() function.
818 	 */
819 	if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
820 		return;
821 
822 	 /*
823 	  * Ask the hypervisor to mark the memory pages as private in the RMP
824 	  * table.
825 	  */
826 	early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_PRIVATE);
827 }
828 
early_snp_set_memory_shared(unsigned long vaddr,unsigned long paddr,unsigned long npages)829 void __head early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
830 					unsigned long npages)
831 {
832 	/*
833 	 * This can be invoked in early boot while running identity mapped, so
834 	 * use an open coded check for SNP instead of using cc_platform_has().
835 	 * This eliminates worries about jump tables or checking boot_cpu_data
836 	 * in the cc_platform_has() function.
837 	 */
838 	if (!(RIP_REL_REF(sev_status) & MSR_AMD64_SEV_SNP_ENABLED))
839 		return;
840 
841 	 /* Ask hypervisor to mark the memory pages shared in the RMP table. */
842 	early_set_pages_state(vaddr, paddr, npages, SNP_PAGE_STATE_SHARED);
843 }
844 
__set_pages_state(struct snp_psc_desc * data,unsigned long vaddr,unsigned long vaddr_end,int op)845 static unsigned long __set_pages_state(struct snp_psc_desc *data, unsigned long vaddr,
846 				       unsigned long vaddr_end, int op)
847 {
848 	struct ghcb_state state;
849 	bool use_large_entry;
850 	struct psc_hdr *hdr;
851 	struct psc_entry *e;
852 	unsigned long flags;
853 	unsigned long pfn;
854 	struct ghcb *ghcb;
855 	int i;
856 
857 	hdr = &data->hdr;
858 	e = data->entries;
859 
860 	memset(data, 0, sizeof(*data));
861 	i = 0;
862 
863 	while (vaddr < vaddr_end && i < ARRAY_SIZE(data->entries)) {
864 		hdr->end_entry = i;
865 
866 		if (is_vmalloc_addr((void *)vaddr)) {
867 			pfn = vmalloc_to_pfn((void *)vaddr);
868 			use_large_entry = false;
869 		} else {
870 			pfn = __pa(vaddr) >> PAGE_SHIFT;
871 			use_large_entry = true;
872 		}
873 
874 		e->gfn = pfn;
875 		e->operation = op;
876 
877 		if (use_large_entry && IS_ALIGNED(vaddr, PMD_SIZE) &&
878 		    (vaddr_end - vaddr) >= PMD_SIZE) {
879 			e->pagesize = RMP_PG_SIZE_2M;
880 			vaddr += PMD_SIZE;
881 		} else {
882 			e->pagesize = RMP_PG_SIZE_4K;
883 			vaddr += PAGE_SIZE;
884 		}
885 
886 		e++;
887 		i++;
888 	}
889 
890 	/* Page validation must be rescinded before changing to shared */
891 	if (op == SNP_PAGE_STATE_SHARED)
892 		pvalidate_pages(data);
893 
894 	local_irq_save(flags);
895 
896 	if (sev_cfg.ghcbs_initialized)
897 		ghcb = __sev_get_ghcb(&state);
898 	else
899 		ghcb = boot_ghcb;
900 
901 	/* Invoke the hypervisor to perform the page state changes */
902 	if (!ghcb || vmgexit_psc(ghcb, data))
903 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
904 
905 	if (sev_cfg.ghcbs_initialized)
906 		__sev_put_ghcb(&state);
907 
908 	local_irq_restore(flags);
909 
910 	/* Page validation must be performed after changing to private */
911 	if (op == SNP_PAGE_STATE_PRIVATE)
912 		pvalidate_pages(data);
913 
914 	return vaddr;
915 }
916 
set_pages_state(unsigned long vaddr,unsigned long npages,int op)917 static void set_pages_state(unsigned long vaddr, unsigned long npages, int op)
918 {
919 	struct snp_psc_desc desc;
920 	unsigned long vaddr_end;
921 
922 	/* Use the MSR protocol when a GHCB is not available. */
923 	if (!boot_ghcb)
924 		return early_set_pages_state(vaddr, __pa(vaddr), npages, op);
925 
926 	vaddr = vaddr & PAGE_MASK;
927 	vaddr_end = vaddr + (npages << PAGE_SHIFT);
928 
929 	while (vaddr < vaddr_end)
930 		vaddr = __set_pages_state(&desc, vaddr, vaddr_end, op);
931 }
932 
snp_set_memory_shared(unsigned long vaddr,unsigned long npages)933 void snp_set_memory_shared(unsigned long vaddr, unsigned long npages)
934 {
935 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
936 		return;
937 
938 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_SHARED);
939 }
940 
snp_set_memory_private(unsigned long vaddr,unsigned long npages)941 void snp_set_memory_private(unsigned long vaddr, unsigned long npages)
942 {
943 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
944 		return;
945 
946 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
947 }
948 
snp_accept_memory(phys_addr_t start,phys_addr_t end)949 void snp_accept_memory(phys_addr_t start, phys_addr_t end)
950 {
951 	unsigned long vaddr, npages;
952 
953 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
954 		return;
955 
956 	vaddr = (unsigned long)__va(start);
957 	npages = (end - start) >> PAGE_SHIFT;
958 
959 	set_pages_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
960 }
961 
set_pte_enc(pte_t * kpte,int level,void * va)962 static void set_pte_enc(pte_t *kpte, int level, void *va)
963 {
964 	struct pte_enc_desc d = {
965 		.kpte	   = kpte,
966 		.pte_level = level,
967 		.va	   = va,
968 		.encrypt   = true
969 	};
970 
971 	prepare_pte_enc(&d);
972 	set_pte_enc_mask(kpte, d.pfn, d.new_pgprot);
973 }
974 
unshare_all_memory(void)975 static void unshare_all_memory(void)
976 {
977 	unsigned long addr, end, size, ghcb;
978 	struct sev_es_runtime_data *data;
979 	unsigned int npages, level;
980 	bool skipped_addr;
981 	pte_t *pte;
982 	int cpu;
983 
984 	/* Unshare the direct mapping. */
985 	addr = PAGE_OFFSET;
986 	end  = PAGE_OFFSET + get_max_mapped();
987 
988 	while (addr < end) {
989 		pte = lookup_address(addr, &level);
990 		size = page_level_size(level);
991 		npages = size / PAGE_SIZE;
992 		skipped_addr = false;
993 
994 		if (!pte || !pte_decrypted(*pte) || pte_none(*pte)) {
995 			addr += size;
996 			continue;
997 		}
998 
999 		/*
1000 		 * Ensure that all the per-CPU GHCBs are made private at the
1001 		 * end of the unsharing loop so that the switch to the slower
1002 		 * MSR protocol happens last.
1003 		 */
1004 		for_each_possible_cpu(cpu) {
1005 			data = per_cpu(runtime_data, cpu);
1006 			ghcb = (unsigned long)&data->ghcb_page;
1007 
1008 			if (addr <= ghcb && ghcb <= addr + size) {
1009 				skipped_addr = true;
1010 				break;
1011 			}
1012 		}
1013 
1014 		if (!skipped_addr) {
1015 			set_pte_enc(pte, level, (void *)addr);
1016 			snp_set_memory_private(addr, npages);
1017 		}
1018 		addr += size;
1019 	}
1020 
1021 	/* Unshare all bss decrypted memory. */
1022 	addr = (unsigned long)__start_bss_decrypted;
1023 	end  = (unsigned long)__start_bss_decrypted_unused;
1024 	npages = (end - addr) >> PAGE_SHIFT;
1025 
1026 	for (; addr < end; addr += PAGE_SIZE) {
1027 		pte = lookup_address(addr, &level);
1028 		if (!pte || !pte_decrypted(*pte) || pte_none(*pte))
1029 			continue;
1030 
1031 		set_pte_enc(pte, level, (void *)addr);
1032 	}
1033 	addr = (unsigned long)__start_bss_decrypted;
1034 	snp_set_memory_private(addr, npages);
1035 
1036 	__flush_tlb_all();
1037 }
1038 
1039 /* Stop new private<->shared conversions */
snp_kexec_begin(void)1040 void snp_kexec_begin(void)
1041 {
1042 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1043 		return;
1044 
1045 	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1046 		return;
1047 
1048 	/*
1049 	 * Crash kernel ends up here with interrupts disabled: can't wait for
1050 	 * conversions to finish.
1051 	 *
1052 	 * If race happened, just report and proceed.
1053 	 */
1054 	if (!set_memory_enc_stop_conversion())
1055 		pr_warn("Failed to stop shared<->private conversions\n");
1056 }
1057 
snp_kexec_finish(void)1058 void snp_kexec_finish(void)
1059 {
1060 	struct sev_es_runtime_data *data;
1061 	unsigned int level, cpu;
1062 	unsigned long size;
1063 	struct ghcb *ghcb;
1064 	pte_t *pte;
1065 
1066 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1067 		return;
1068 
1069 	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1070 		return;
1071 
1072 	unshare_all_memory();
1073 
1074 	/*
1075 	 * Switch to using the MSR protocol to change per-CPU GHCBs to
1076 	 * private. All the per-CPU GHCBs have been switched back to private,
1077 	 * so can't do any more GHCB calls to the hypervisor beyond this point
1078 	 * until the kexec'ed kernel starts running.
1079 	 */
1080 	boot_ghcb = NULL;
1081 	sev_cfg.ghcbs_initialized = false;
1082 
1083 	for_each_possible_cpu(cpu) {
1084 		data = per_cpu(runtime_data, cpu);
1085 		ghcb = &data->ghcb_page;
1086 		pte = lookup_address((unsigned long)ghcb, &level);
1087 		size = page_level_size(level);
1088 		set_pte_enc(pte, level, (void *)ghcb);
1089 		snp_set_memory_private((unsigned long)ghcb, (size / PAGE_SIZE));
1090 	}
1091 }
1092 
snp_set_vmsa(void * va,void * caa,int apic_id,bool make_vmsa)1093 static int snp_set_vmsa(void *va, void *caa, int apic_id, bool make_vmsa)
1094 {
1095 	int ret;
1096 
1097 	if (snp_vmpl) {
1098 		struct svsm_call call = {};
1099 		unsigned long flags;
1100 
1101 		local_irq_save(flags);
1102 
1103 		call.caa = this_cpu_read(svsm_caa);
1104 		call.rcx = __pa(va);
1105 
1106 		if (make_vmsa) {
1107 			/* Protocol 0, Call ID 2 */
1108 			call.rax = SVSM_CORE_CALL(SVSM_CORE_CREATE_VCPU);
1109 			call.rdx = __pa(caa);
1110 			call.r8  = apic_id;
1111 		} else {
1112 			/* Protocol 0, Call ID 3 */
1113 			call.rax = SVSM_CORE_CALL(SVSM_CORE_DELETE_VCPU);
1114 		}
1115 
1116 		ret = svsm_perform_call_protocol(&call);
1117 
1118 		local_irq_restore(flags);
1119 	} else {
1120 		/*
1121 		 * If the kernel runs at VMPL0, it can change the VMSA
1122 		 * bit for a page using the RMPADJUST instruction.
1123 		 * However, for the instruction to succeed it must
1124 		 * target the permissions of a lesser privileged (higher
1125 		 * numbered) VMPL level, so use VMPL1.
1126 		 */
1127 		u64 attrs = 1;
1128 
1129 		if (make_vmsa)
1130 			attrs |= RMPADJUST_VMSA_PAGE_BIT;
1131 
1132 		ret = rmpadjust((unsigned long)va, RMP_PG_SIZE_4K, attrs);
1133 	}
1134 
1135 	return ret;
1136 }
1137 
1138 #define __ATTR_BASE		(SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK)
1139 #define INIT_CS_ATTRIBS		(__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK)
1140 #define INIT_DS_ATTRIBS		(__ATTR_BASE | SVM_SELECTOR_WRITE_MASK)
1141 
1142 #define INIT_LDTR_ATTRIBS	(SVM_SELECTOR_P_MASK | 2)
1143 #define INIT_TR_ATTRIBS		(SVM_SELECTOR_P_MASK | 3)
1144 
snp_alloc_vmsa_page(int cpu)1145 static void *snp_alloc_vmsa_page(int cpu)
1146 {
1147 	struct page *p;
1148 
1149 	/*
1150 	 * Allocate VMSA page to work around the SNP erratum where the CPU will
1151 	 * incorrectly signal an RMP violation #PF if a large page (2MB or 1GB)
1152 	 * collides with the RMP entry of VMSA page. The recommended workaround
1153 	 * is to not use a large page.
1154 	 *
1155 	 * Allocate an 8k page which is also 8k-aligned.
1156 	 */
1157 	p = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1);
1158 	if (!p)
1159 		return NULL;
1160 
1161 	split_page(p, 1);
1162 
1163 	/* Free the first 4k. This page may be 2M/1G aligned and cannot be used. */
1164 	__free_page(p);
1165 
1166 	return page_address(p + 1);
1167 }
1168 
snp_cleanup_vmsa(struct sev_es_save_area * vmsa,int apic_id)1169 static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa, int apic_id)
1170 {
1171 	int err;
1172 
1173 	err = snp_set_vmsa(vmsa, NULL, apic_id, false);
1174 	if (err)
1175 		pr_err("clear VMSA page failed (%u), leaking page\n", err);
1176 	else
1177 		free_page((unsigned long)vmsa);
1178 }
1179 
wakeup_cpu_via_vmgexit(u32 apic_id,unsigned long start_ip)1180 static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
1181 {
1182 	struct sev_es_save_area *cur_vmsa, *vmsa;
1183 	struct ghcb_state state;
1184 	struct svsm_ca *caa;
1185 	unsigned long flags;
1186 	struct ghcb *ghcb;
1187 	u8 sipi_vector;
1188 	int cpu, ret;
1189 	u64 cr4;
1190 
1191 	/*
1192 	 * The hypervisor SNP feature support check has happened earlier, just check
1193 	 * the AP_CREATION one here.
1194 	 */
1195 	if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION))
1196 		return -EOPNOTSUPP;
1197 
1198 	/*
1199 	 * Verify the desired start IP against the known trampoline start IP
1200 	 * to catch any future new trampolines that may be introduced that
1201 	 * would require a new protected guest entry point.
1202 	 */
1203 	if (WARN_ONCE(start_ip != real_mode_header->trampoline_start,
1204 		      "Unsupported SNP start_ip: %lx\n", start_ip))
1205 		return -EINVAL;
1206 
1207 	/* Override start_ip with known protected guest start IP */
1208 	start_ip = real_mode_header->sev_es_trampoline_start;
1209 
1210 	/* Find the logical CPU for the APIC ID */
1211 	for_each_present_cpu(cpu) {
1212 		if (arch_match_cpu_phys_id(cpu, apic_id))
1213 			break;
1214 	}
1215 	if (cpu >= nr_cpu_ids)
1216 		return -EINVAL;
1217 
1218 	cur_vmsa = per_cpu(sev_vmsa, cpu);
1219 
1220 	/*
1221 	 * A new VMSA is created each time because there is no guarantee that
1222 	 * the current VMSA is the kernels or that the vCPU is not running. If
1223 	 * an attempt was done to use the current VMSA with a running vCPU, a
1224 	 * #VMEXIT of that vCPU would wipe out all of the settings being done
1225 	 * here.
1226 	 */
1227 	vmsa = (struct sev_es_save_area *)snp_alloc_vmsa_page(cpu);
1228 	if (!vmsa)
1229 		return -ENOMEM;
1230 
1231 	/* If an SVSM is present, the SVSM per-CPU CAA will be !NULL */
1232 	caa = per_cpu(svsm_caa, cpu);
1233 
1234 	/* CR4 should maintain the MCE value */
1235 	cr4 = native_read_cr4() & X86_CR4_MCE;
1236 
1237 	/* Set the CS value based on the start_ip converted to a SIPI vector */
1238 	sipi_vector		= (start_ip >> 12);
1239 	vmsa->cs.base		= sipi_vector << 12;
1240 	vmsa->cs.limit		= AP_INIT_CS_LIMIT;
1241 	vmsa->cs.attrib		= INIT_CS_ATTRIBS;
1242 	vmsa->cs.selector	= sipi_vector << 8;
1243 
1244 	/* Set the RIP value based on start_ip */
1245 	vmsa->rip		= start_ip & 0xfff;
1246 
1247 	/* Set AP INIT defaults as documented in the APM */
1248 	vmsa->ds.limit		= AP_INIT_DS_LIMIT;
1249 	vmsa->ds.attrib		= INIT_DS_ATTRIBS;
1250 	vmsa->es		= vmsa->ds;
1251 	vmsa->fs		= vmsa->ds;
1252 	vmsa->gs		= vmsa->ds;
1253 	vmsa->ss		= vmsa->ds;
1254 
1255 	vmsa->gdtr.limit	= AP_INIT_GDTR_LIMIT;
1256 	vmsa->ldtr.limit	= AP_INIT_LDTR_LIMIT;
1257 	vmsa->ldtr.attrib	= INIT_LDTR_ATTRIBS;
1258 	vmsa->idtr.limit	= AP_INIT_IDTR_LIMIT;
1259 	vmsa->tr.limit		= AP_INIT_TR_LIMIT;
1260 	vmsa->tr.attrib		= INIT_TR_ATTRIBS;
1261 
1262 	vmsa->cr4		= cr4;
1263 	vmsa->cr0		= AP_INIT_CR0_DEFAULT;
1264 	vmsa->dr7		= DR7_RESET_VALUE;
1265 	vmsa->dr6		= AP_INIT_DR6_DEFAULT;
1266 	vmsa->rflags		= AP_INIT_RFLAGS_DEFAULT;
1267 	vmsa->g_pat		= AP_INIT_GPAT_DEFAULT;
1268 	vmsa->xcr0		= AP_INIT_XCR0_DEFAULT;
1269 	vmsa->mxcsr		= AP_INIT_MXCSR_DEFAULT;
1270 	vmsa->x87_ftw		= AP_INIT_X87_FTW_DEFAULT;
1271 	vmsa->x87_fcw		= AP_INIT_X87_FCW_DEFAULT;
1272 
1273 	/* SVME must be set. */
1274 	vmsa->efer		= EFER_SVME;
1275 
1276 	/*
1277 	 * Set the SNP-specific fields for this VMSA:
1278 	 *   VMPL level
1279 	 *   SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits)
1280 	 */
1281 	vmsa->vmpl		= snp_vmpl;
1282 	vmsa->sev_features	= sev_status >> 2;
1283 
1284 	/* Populate AP's TSC scale/offset to get accurate TSC values. */
1285 	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
1286 		vmsa->tsc_scale = snp_tsc_scale;
1287 		vmsa->tsc_offset = snp_tsc_offset;
1288 	}
1289 
1290 	/* Switch the page over to a VMSA page now that it is initialized */
1291 	ret = snp_set_vmsa(vmsa, caa, apic_id, true);
1292 	if (ret) {
1293 		pr_err("set VMSA page failed (%u)\n", ret);
1294 		free_page((unsigned long)vmsa);
1295 
1296 		return -EINVAL;
1297 	}
1298 
1299 	/* Issue VMGEXIT AP Creation NAE event */
1300 	local_irq_save(flags);
1301 
1302 	ghcb = __sev_get_ghcb(&state);
1303 
1304 	vc_ghcb_invalidate(ghcb);
1305 	ghcb_set_rax(ghcb, vmsa->sev_features);
1306 	ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_CREATION);
1307 	ghcb_set_sw_exit_info_1(ghcb,
1308 				((u64)apic_id << 32)	|
1309 				((u64)snp_vmpl << 16)	|
1310 				SVM_VMGEXIT_AP_CREATE);
1311 	ghcb_set_sw_exit_info_2(ghcb, __pa(vmsa));
1312 
1313 	sev_es_wr_ghcb_msr(__pa(ghcb));
1314 	VMGEXIT();
1315 
1316 	if (!ghcb_sw_exit_info_1_is_valid(ghcb) ||
1317 	    lower_32_bits(ghcb->save.sw_exit_info_1)) {
1318 		pr_err("SNP AP Creation error\n");
1319 		ret = -EINVAL;
1320 	}
1321 
1322 	__sev_put_ghcb(&state);
1323 
1324 	local_irq_restore(flags);
1325 
1326 	/* Perform cleanup if there was an error */
1327 	if (ret) {
1328 		snp_cleanup_vmsa(vmsa, apic_id);
1329 		vmsa = NULL;
1330 	}
1331 
1332 	/* Free up any previous VMSA page */
1333 	if (cur_vmsa)
1334 		snp_cleanup_vmsa(cur_vmsa, apic_id);
1335 
1336 	/* Record the current VMSA page */
1337 	per_cpu(sev_vmsa, cpu) = vmsa;
1338 
1339 	return ret;
1340 }
1341 
snp_set_wakeup_secondary_cpu(void)1342 void __init snp_set_wakeup_secondary_cpu(void)
1343 {
1344 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1345 		return;
1346 
1347 	/*
1348 	 * Always set this override if SNP is enabled. This makes it the
1349 	 * required method to start APs under SNP. If the hypervisor does
1350 	 * not support AP creation, then no APs will be started.
1351 	 */
1352 	apic_update_callback(wakeup_secondary_cpu, wakeup_cpu_via_vmgexit);
1353 }
1354 
sev_es_setup_ap_jump_table(struct real_mode_header * rmh)1355 int __init sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
1356 {
1357 	u16 startup_cs, startup_ip;
1358 	phys_addr_t jump_table_pa;
1359 	u64 jump_table_addr;
1360 	u16 __iomem *jump_table;
1361 
1362 	jump_table_addr = get_jump_table_addr();
1363 
1364 	/* On UP guests there is no jump table so this is not a failure */
1365 	if (!jump_table_addr)
1366 		return 0;
1367 
1368 	/* Check if AP Jump Table is page-aligned */
1369 	if (jump_table_addr & ~PAGE_MASK)
1370 		return -EINVAL;
1371 
1372 	jump_table_pa = jump_table_addr & PAGE_MASK;
1373 
1374 	startup_cs = (u16)(rmh->trampoline_start >> 4);
1375 	startup_ip = (u16)(rmh->sev_es_trampoline_start -
1376 			   rmh->trampoline_start);
1377 
1378 	jump_table = ioremap_encrypted(jump_table_pa, PAGE_SIZE);
1379 	if (!jump_table)
1380 		return -EIO;
1381 
1382 	writew(startup_ip, &jump_table[0]);
1383 	writew(startup_cs, &jump_table[1]);
1384 
1385 	iounmap(jump_table);
1386 
1387 	return 0;
1388 }
1389 
1390 /*
1391  * This is needed by the OVMF UEFI firmware which will use whatever it finds in
1392  * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
1393  * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
1394  */
sev_es_efi_map_ghcbs(pgd_t * pgd)1395 int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
1396 {
1397 	struct sev_es_runtime_data *data;
1398 	unsigned long address, pflags;
1399 	int cpu;
1400 	u64 pfn;
1401 
1402 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1403 		return 0;
1404 
1405 	pflags = _PAGE_NX | _PAGE_RW;
1406 
1407 	for_each_possible_cpu(cpu) {
1408 		data = per_cpu(runtime_data, cpu);
1409 
1410 		address = __pa(&data->ghcb_page);
1411 		pfn = address >> PAGE_SHIFT;
1412 
1413 		if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
1414 			return 1;
1415 	}
1416 
1417 	return 0;
1418 }
1419 
1420 /* Writes to the SVSM CAA MSR are ignored */
__vc_handle_msr_caa(struct pt_regs * regs,bool write)1421 static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
1422 {
1423 	if (write)
1424 		return ES_OK;
1425 
1426 	regs->ax = lower_32_bits(this_cpu_read(svsm_caa_pa));
1427 	regs->dx = upper_32_bits(this_cpu_read(svsm_caa_pa));
1428 
1429 	return ES_OK;
1430 }
1431 
1432 /*
1433  * TSC related accesses should not exit to the hypervisor when a guest is
1434  * executing with Secure TSC enabled, so special handling is required for
1435  * accesses of MSR_IA32_TSC and MSR_AMD64_GUEST_TSC_FREQ.
1436  */
__vc_handle_secure_tsc_msrs(struct pt_regs * regs,bool write)1437 static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
1438 {
1439 	u64 tsc;
1440 
1441 	/*
1442 	 * GUEST_TSC_FREQ should not be intercepted when Secure TSC is enabled.
1443 	 * Terminate the SNP guest when the interception is enabled.
1444 	 */
1445 	if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ)
1446 		return ES_VMM_ERROR;
1447 
1448 	/*
1449 	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
1450 	 *         to return undefined values, so ignore all writes.
1451 	 *
1452 	 * Reads: Reads of MSR_IA32_TSC should return the current TSC value, use
1453 	 *        the value returned by rdtsc_ordered().
1454 	 */
1455 	if (write) {
1456 		WARN_ONCE(1, "TSC MSR writes are verboten!\n");
1457 		return ES_OK;
1458 	}
1459 
1460 	tsc = rdtsc_ordered();
1461 	regs->ax = lower_32_bits(tsc);
1462 	regs->dx = upper_32_bits(tsc);
1463 
1464 	return ES_OK;
1465 }
1466 
vc_handle_msr(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1467 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1468 {
1469 	struct pt_regs *regs = ctxt->regs;
1470 	enum es_result ret;
1471 	bool write;
1472 
1473 	/* Is it a WRMSR? */
1474 	write = ctxt->insn.opcode.bytes[1] == 0x30;
1475 
1476 	switch (regs->cx) {
1477 	case MSR_SVSM_CAA:
1478 		return __vc_handle_msr_caa(regs, write);
1479 	case MSR_IA32_TSC:
1480 	case MSR_AMD64_GUEST_TSC_FREQ:
1481 		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
1482 			return __vc_handle_secure_tsc_msrs(regs, write);
1483 		else
1484 			break;
1485 	default:
1486 		break;
1487 	}
1488 
1489 	ghcb_set_rcx(ghcb, regs->cx);
1490 	if (write) {
1491 		ghcb_set_rax(ghcb, regs->ax);
1492 		ghcb_set_rdx(ghcb, regs->dx);
1493 	}
1494 
1495 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, write, 0);
1496 
1497 	if ((ret == ES_OK) && !write) {
1498 		regs->ax = ghcb->save.rax;
1499 		regs->dx = ghcb->save.rdx;
1500 	}
1501 
1502 	return ret;
1503 }
1504 
snp_register_per_cpu_ghcb(void)1505 static void snp_register_per_cpu_ghcb(void)
1506 {
1507 	struct sev_es_runtime_data *data;
1508 	struct ghcb *ghcb;
1509 
1510 	data = this_cpu_read(runtime_data);
1511 	ghcb = &data->ghcb_page;
1512 
1513 	snp_register_ghcb_early(__pa(ghcb));
1514 }
1515 
setup_ghcb(void)1516 void setup_ghcb(void)
1517 {
1518 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1519 		return;
1520 
1521 	/*
1522 	 * Check whether the runtime #VC exception handler is active. It uses
1523 	 * the per-CPU GHCB page which is set up by sev_es_init_vc_handling().
1524 	 *
1525 	 * If SNP is active, register the per-CPU GHCB page so that the runtime
1526 	 * exception handler can use it.
1527 	 */
1528 	if (initial_vc_handler == (unsigned long)kernel_exc_vmm_communication) {
1529 		if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1530 			snp_register_per_cpu_ghcb();
1531 
1532 		sev_cfg.ghcbs_initialized = true;
1533 
1534 		return;
1535 	}
1536 
1537 	/*
1538 	 * Make sure the hypervisor talks a supported protocol.
1539 	 * This gets called only in the BSP boot phase.
1540 	 */
1541 	if (!sev_es_negotiate_protocol())
1542 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
1543 
1544 	/*
1545 	 * Clear the boot_ghcb. The first exception comes in before the bss
1546 	 * section is cleared.
1547 	 */
1548 	memset(&boot_ghcb_page, 0, PAGE_SIZE);
1549 
1550 	/* Alright - Make the boot-ghcb public */
1551 	boot_ghcb = &boot_ghcb_page;
1552 
1553 	/* SNP guest requires that GHCB GPA must be registered. */
1554 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
1555 		snp_register_ghcb_early(__pa(&boot_ghcb_page));
1556 }
1557 
1558 #ifdef CONFIG_HOTPLUG_CPU
sev_es_ap_hlt_loop(void)1559 static void sev_es_ap_hlt_loop(void)
1560 {
1561 	struct ghcb_state state;
1562 	struct ghcb *ghcb;
1563 
1564 	ghcb = __sev_get_ghcb(&state);
1565 
1566 	while (true) {
1567 		vc_ghcb_invalidate(ghcb);
1568 		ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_HLT_LOOP);
1569 		ghcb_set_sw_exit_info_1(ghcb, 0);
1570 		ghcb_set_sw_exit_info_2(ghcb, 0);
1571 
1572 		sev_es_wr_ghcb_msr(__pa(ghcb));
1573 		VMGEXIT();
1574 
1575 		/* Wakeup signal? */
1576 		if (ghcb_sw_exit_info_2_is_valid(ghcb) &&
1577 		    ghcb->save.sw_exit_info_2)
1578 			break;
1579 	}
1580 
1581 	__sev_put_ghcb(&state);
1582 }
1583 
1584 /*
1585  * Play_dead handler when running under SEV-ES. This is needed because
1586  * the hypervisor can't deliver an SIPI request to restart the AP.
1587  * Instead the kernel has to issue a VMGEXIT to halt the VCPU until the
1588  * hypervisor wakes it up again.
1589  */
sev_es_play_dead(void)1590 static void sev_es_play_dead(void)
1591 {
1592 	play_dead_common();
1593 
1594 	/* IRQs now disabled */
1595 
1596 	sev_es_ap_hlt_loop();
1597 
1598 	/*
1599 	 * If we get here, the VCPU was woken up again. Jump to CPU
1600 	 * startup code to get it back online.
1601 	 */
1602 	soft_restart_cpu();
1603 }
1604 #else  /* CONFIG_HOTPLUG_CPU */
1605 #define sev_es_play_dead	native_play_dead
1606 #endif /* CONFIG_HOTPLUG_CPU */
1607 
1608 #ifdef CONFIG_SMP
sev_es_setup_play_dead(void)1609 static void __init sev_es_setup_play_dead(void)
1610 {
1611 	smp_ops.play_dead = sev_es_play_dead;
1612 }
1613 #else
sev_es_setup_play_dead(void)1614 static inline void sev_es_setup_play_dead(void) { }
1615 #endif
1616 
alloc_runtime_data(int cpu)1617 static void __init alloc_runtime_data(int cpu)
1618 {
1619 	struct sev_es_runtime_data *data;
1620 
1621 	data = memblock_alloc_node(sizeof(*data), PAGE_SIZE, cpu_to_node(cpu));
1622 	if (!data)
1623 		panic("Can't allocate SEV-ES runtime data");
1624 
1625 	per_cpu(runtime_data, cpu) = data;
1626 
1627 	if (snp_vmpl) {
1628 		struct svsm_ca *caa;
1629 
1630 		/* Allocate the SVSM CA page if an SVSM is present */
1631 		caa = memblock_alloc_or_panic(sizeof(*caa), PAGE_SIZE);
1632 
1633 		per_cpu(svsm_caa, cpu) = caa;
1634 		per_cpu(svsm_caa_pa, cpu) = __pa(caa);
1635 	}
1636 }
1637 
init_ghcb(int cpu)1638 static void __init init_ghcb(int cpu)
1639 {
1640 	struct sev_es_runtime_data *data;
1641 	int err;
1642 
1643 	data = per_cpu(runtime_data, cpu);
1644 
1645 	err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
1646 					 sizeof(data->ghcb_page));
1647 	if (err)
1648 		panic("Can't map GHCBs unencrypted");
1649 
1650 	memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
1651 
1652 	data->ghcb_active = false;
1653 	data->backup_ghcb_active = false;
1654 }
1655 
sev_es_init_vc_handling(void)1656 void __init sev_es_init_vc_handling(void)
1657 {
1658 	int cpu;
1659 
1660 	BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
1661 
1662 	if (!cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
1663 		return;
1664 
1665 	if (!sev_es_check_cpu_features())
1666 		panic("SEV-ES CPU Features missing");
1667 
1668 	/*
1669 	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
1670 	 * features.
1671 	 */
1672 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
1673 		sev_hv_features = get_hv_features();
1674 
1675 		if (!(sev_hv_features & GHCB_HV_FT_SNP))
1676 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
1677 	}
1678 
1679 	/* Initialize per-cpu GHCB pages */
1680 	for_each_possible_cpu(cpu) {
1681 		alloc_runtime_data(cpu);
1682 		init_ghcb(cpu);
1683 	}
1684 
1685 	/* If running under an SVSM, switch to the per-cpu CA */
1686 	if (snp_vmpl) {
1687 		struct svsm_call call = {};
1688 		unsigned long flags;
1689 		int ret;
1690 
1691 		local_irq_save(flags);
1692 
1693 		/*
1694 		 * SVSM_CORE_REMAP_CA call:
1695 		 *   RAX = 0 (Protocol=0, CallID=0)
1696 		 *   RCX = New CA GPA
1697 		 */
1698 		call.caa = svsm_get_caa();
1699 		call.rax = SVSM_CORE_CALL(SVSM_CORE_REMAP_CA);
1700 		call.rcx = this_cpu_read(svsm_caa_pa);
1701 		ret = svsm_perform_call_protocol(&call);
1702 		if (ret)
1703 			panic("Can't remap the SVSM CA, ret=%d, rax_out=0x%llx\n",
1704 			      ret, call.rax_out);
1705 
1706 		sev_cfg.use_cas = true;
1707 
1708 		local_irq_restore(flags);
1709 	}
1710 
1711 	sev_es_setup_play_dead();
1712 
1713 	/* Secondary CPUs use the runtime #VC handler */
1714 	initial_vc_handler = (unsigned long)kernel_exc_vmm_communication;
1715 }
1716 
vc_early_forward_exception(struct es_em_ctxt * ctxt)1717 static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
1718 {
1719 	int trapnr = ctxt->fi.vector;
1720 
1721 	if (trapnr == X86_TRAP_PF)
1722 		native_write_cr2(ctxt->fi.cr2);
1723 
1724 	ctxt->regs->orig_ax = ctxt->fi.error_code;
1725 	do_early_exception(ctxt->regs, trapnr);
1726 }
1727 
vc_insn_get_rm(struct es_em_ctxt * ctxt)1728 static long *vc_insn_get_rm(struct es_em_ctxt *ctxt)
1729 {
1730 	long *reg_array;
1731 	int offset;
1732 
1733 	reg_array = (long *)ctxt->regs;
1734 	offset    = insn_get_modrm_rm_off(&ctxt->insn, ctxt->regs);
1735 
1736 	if (offset < 0)
1737 		return NULL;
1738 
1739 	offset /= sizeof(long);
1740 
1741 	return reg_array + offset;
1742 }
vc_do_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned int bytes,bool read)1743 static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
1744 				 unsigned int bytes, bool read)
1745 {
1746 	u64 exit_code, exit_info_1, exit_info_2;
1747 	unsigned long ghcb_pa = __pa(ghcb);
1748 	enum es_result res;
1749 	phys_addr_t paddr;
1750 	void __user *ref;
1751 
1752 	ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
1753 	if (ref == (void __user *)-1L)
1754 		return ES_UNSUPPORTED;
1755 
1756 	exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
1757 
1758 	res = vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr);
1759 	if (res != ES_OK) {
1760 		if (res == ES_EXCEPTION && !read)
1761 			ctxt->fi.error_code |= X86_PF_WRITE;
1762 
1763 		return res;
1764 	}
1765 
1766 	exit_info_1 = paddr;
1767 	/* Can never be greater than 8 */
1768 	exit_info_2 = bytes;
1769 
1770 	ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
1771 
1772 	return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
1773 }
1774 
1775 /*
1776  * The MOVS instruction has two memory operands, which raises the
1777  * problem that it is not known whether the access to the source or the
1778  * destination caused the #VC exception (and hence whether an MMIO read
1779  * or write operation needs to be emulated).
1780  *
1781  * Instead of playing games with walking page-tables and trying to guess
1782  * whether the source or destination is an MMIO range, split the move
1783  * into two operations, a read and a write with only one memory operand.
1784  * This will cause a nested #VC exception on the MMIO address which can
1785  * then be handled.
1786  *
1787  * This implementation has the benefit that it also supports MOVS where
1788  * source _and_ destination are MMIO regions.
1789  *
1790  * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a
1791  * rare operation. If it turns out to be a performance problem the split
1792  * operations can be moved to memcpy_fromio() and memcpy_toio().
1793  */
vc_handle_mmio_movs(struct es_em_ctxt * ctxt,unsigned int bytes)1794 static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,
1795 					  unsigned int bytes)
1796 {
1797 	unsigned long ds_base, es_base;
1798 	unsigned char *src, *dst;
1799 	unsigned char buffer[8];
1800 	enum es_result ret;
1801 	bool rep;
1802 	int off;
1803 
1804 	ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS);
1805 	es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
1806 
1807 	if (ds_base == -1L || es_base == -1L) {
1808 		ctxt->fi.vector = X86_TRAP_GP;
1809 		ctxt->fi.error_code = 0;
1810 		return ES_EXCEPTION;
1811 	}
1812 
1813 	src = ds_base + (unsigned char *)ctxt->regs->si;
1814 	dst = es_base + (unsigned char *)ctxt->regs->di;
1815 
1816 	ret = vc_read_mem(ctxt, src, buffer, bytes);
1817 	if (ret != ES_OK)
1818 		return ret;
1819 
1820 	ret = vc_write_mem(ctxt, dst, buffer, bytes);
1821 	if (ret != ES_OK)
1822 		return ret;
1823 
1824 	if (ctxt->regs->flags & X86_EFLAGS_DF)
1825 		off = -bytes;
1826 	else
1827 		off =  bytes;
1828 
1829 	ctxt->regs->si += off;
1830 	ctxt->regs->di += off;
1831 
1832 	rep = insn_has_rep_prefix(&ctxt->insn);
1833 	if (rep)
1834 		ctxt->regs->cx -= 1;
1835 
1836 	if (!rep || ctxt->regs->cx == 0)
1837 		return ES_OK;
1838 	else
1839 		return ES_RETRY;
1840 }
1841 
vc_handle_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1842 static enum es_result vc_handle_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1843 {
1844 	struct insn *insn = &ctxt->insn;
1845 	enum insn_mmio_type mmio;
1846 	unsigned int bytes = 0;
1847 	enum es_result ret;
1848 	u8 sign_byte;
1849 	long *reg_data;
1850 
1851 	mmio = insn_decode_mmio(insn, &bytes);
1852 	if (mmio == INSN_MMIO_DECODE_FAILED)
1853 		return ES_DECODE_FAILED;
1854 
1855 	if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
1856 		reg_data = insn_get_modrm_reg_ptr(insn, ctxt->regs);
1857 		if (!reg_data)
1858 			return ES_DECODE_FAILED;
1859 	}
1860 
1861 	if (user_mode(ctxt->regs))
1862 		return ES_UNSUPPORTED;
1863 
1864 	switch (mmio) {
1865 	case INSN_MMIO_WRITE:
1866 		memcpy(ghcb->shared_buffer, reg_data, bytes);
1867 		ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1868 		break;
1869 	case INSN_MMIO_WRITE_IMM:
1870 		memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
1871 		ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1872 		break;
1873 	case INSN_MMIO_READ:
1874 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1875 		if (ret)
1876 			break;
1877 
1878 		/* Zero-extend for 32-bit operation */
1879 		if (bytes == 4)
1880 			*reg_data = 0;
1881 
1882 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1883 		break;
1884 	case INSN_MMIO_READ_ZERO_EXTEND:
1885 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1886 		if (ret)
1887 			break;
1888 
1889 		/* Zero extend based on operand size */
1890 		memset(reg_data, 0, insn->opnd_bytes);
1891 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1892 		break;
1893 	case INSN_MMIO_READ_SIGN_EXTEND:
1894 		ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1895 		if (ret)
1896 			break;
1897 
1898 		if (bytes == 1) {
1899 			u8 *val = (u8 *)ghcb->shared_buffer;
1900 
1901 			sign_byte = (*val & 0x80) ? 0xff : 0x00;
1902 		} else {
1903 			u16 *val = (u16 *)ghcb->shared_buffer;
1904 
1905 			sign_byte = (*val & 0x8000) ? 0xff : 0x00;
1906 		}
1907 
1908 		/* Sign extend based on operand size */
1909 		memset(reg_data, sign_byte, insn->opnd_bytes);
1910 		memcpy(reg_data, ghcb->shared_buffer, bytes);
1911 		break;
1912 	case INSN_MMIO_MOVS:
1913 		ret = vc_handle_mmio_movs(ctxt, bytes);
1914 		break;
1915 	default:
1916 		ret = ES_UNSUPPORTED;
1917 		break;
1918 	}
1919 
1920 	return ret;
1921 }
1922 
vc_handle_dr7_write(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1923 static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
1924 					  struct es_em_ctxt *ctxt)
1925 {
1926 	struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1927 	long val, *reg = vc_insn_get_rm(ctxt);
1928 	enum es_result ret;
1929 
1930 	if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1931 		return ES_VMM_ERROR;
1932 
1933 	if (!reg)
1934 		return ES_DECODE_FAILED;
1935 
1936 	val = *reg;
1937 
1938 	/* Upper 32 bits must be written as zeroes */
1939 	if (val >> 32) {
1940 		ctxt->fi.vector = X86_TRAP_GP;
1941 		ctxt->fi.error_code = 0;
1942 		return ES_EXCEPTION;
1943 	}
1944 
1945 	/* Clear out other reserved bits and set bit 10 */
1946 	val = (val & 0xffff23ffL) | BIT(10);
1947 
1948 	/* Early non-zero writes to DR7 are not supported */
1949 	if (!data && (val & ~DR7_RESET_VALUE))
1950 		return ES_UNSUPPORTED;
1951 
1952 	/* Using a value of 0 for ExitInfo1 means RAX holds the value */
1953 	ghcb_set_rax(ghcb, val);
1954 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
1955 	if (ret != ES_OK)
1956 		return ret;
1957 
1958 	if (data)
1959 		data->dr7 = val;
1960 
1961 	return ES_OK;
1962 }
1963 
vc_handle_dr7_read(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1964 static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
1965 					 struct es_em_ctxt *ctxt)
1966 {
1967 	struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1968 	long *reg = vc_insn_get_rm(ctxt);
1969 
1970 	if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
1971 		return ES_VMM_ERROR;
1972 
1973 	if (!reg)
1974 		return ES_DECODE_FAILED;
1975 
1976 	if (data)
1977 		*reg = data->dr7;
1978 	else
1979 		*reg = DR7_RESET_VALUE;
1980 
1981 	return ES_OK;
1982 }
1983 
vc_handle_wbinvd(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1984 static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
1985 				       struct es_em_ctxt *ctxt)
1986 {
1987 	return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
1988 }
1989 
vc_handle_rdpmc(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1990 static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1991 {
1992 	enum es_result ret;
1993 
1994 	ghcb_set_rcx(ghcb, ctxt->regs->cx);
1995 
1996 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
1997 	if (ret != ES_OK)
1998 		return ret;
1999 
2000 	if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
2001 		return ES_VMM_ERROR;
2002 
2003 	ctxt->regs->ax = ghcb->save.rax;
2004 	ctxt->regs->dx = ghcb->save.rdx;
2005 
2006 	return ES_OK;
2007 }
2008 
vc_handle_monitor(struct ghcb * ghcb,struct es_em_ctxt * ctxt)2009 static enum es_result vc_handle_monitor(struct ghcb *ghcb,
2010 					struct es_em_ctxt *ctxt)
2011 {
2012 	/*
2013 	 * Treat it as a NOP and do not leak a physical address to the
2014 	 * hypervisor.
2015 	 */
2016 	return ES_OK;
2017 }
2018 
vc_handle_mwait(struct ghcb * ghcb,struct es_em_ctxt * ctxt)2019 static enum es_result vc_handle_mwait(struct ghcb *ghcb,
2020 				      struct es_em_ctxt *ctxt)
2021 {
2022 	/* Treat the same as MONITOR/MONITORX */
2023 	return ES_OK;
2024 }
2025 
vc_handle_vmmcall(struct ghcb * ghcb,struct es_em_ctxt * ctxt)2026 static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
2027 					struct es_em_ctxt *ctxt)
2028 {
2029 	enum es_result ret;
2030 
2031 	ghcb_set_rax(ghcb, ctxt->regs->ax);
2032 	ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
2033 
2034 	if (x86_platform.hyper.sev_es_hcall_prepare)
2035 		x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
2036 
2037 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
2038 	if (ret != ES_OK)
2039 		return ret;
2040 
2041 	if (!ghcb_rax_is_valid(ghcb))
2042 		return ES_VMM_ERROR;
2043 
2044 	ctxt->regs->ax = ghcb->save.rax;
2045 
2046 	/*
2047 	 * Call sev_es_hcall_finish() after regs->ax is already set.
2048 	 * This allows the hypervisor handler to overwrite it again if
2049 	 * necessary.
2050 	 */
2051 	if (x86_platform.hyper.sev_es_hcall_finish &&
2052 	    !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
2053 		return ES_VMM_ERROR;
2054 
2055 	return ES_OK;
2056 }
2057 
vc_handle_trap_ac(struct ghcb * ghcb,struct es_em_ctxt * ctxt)2058 static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
2059 					struct es_em_ctxt *ctxt)
2060 {
2061 	/*
2062 	 * Calling ecx_alignment_check() directly does not work, because it
2063 	 * enables IRQs and the GHCB is active. Forward the exception and call
2064 	 * it later from vc_forward_exception().
2065 	 */
2066 	ctxt->fi.vector = X86_TRAP_AC;
2067 	ctxt->fi.error_code = 0;
2068 	return ES_EXCEPTION;
2069 }
2070 
vc_handle_exitcode(struct es_em_ctxt * ctxt,struct ghcb * ghcb,unsigned long exit_code)2071 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
2072 					 struct ghcb *ghcb,
2073 					 unsigned long exit_code)
2074 {
2075 	enum es_result result = vc_check_opcode_bytes(ctxt, exit_code);
2076 
2077 	if (result != ES_OK)
2078 		return result;
2079 
2080 	switch (exit_code) {
2081 	case SVM_EXIT_READ_DR7:
2082 		result = vc_handle_dr7_read(ghcb, ctxt);
2083 		break;
2084 	case SVM_EXIT_WRITE_DR7:
2085 		result = vc_handle_dr7_write(ghcb, ctxt);
2086 		break;
2087 	case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
2088 		result = vc_handle_trap_ac(ghcb, ctxt);
2089 		break;
2090 	case SVM_EXIT_RDTSC:
2091 	case SVM_EXIT_RDTSCP:
2092 		result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
2093 		break;
2094 	case SVM_EXIT_RDPMC:
2095 		result = vc_handle_rdpmc(ghcb, ctxt);
2096 		break;
2097 	case SVM_EXIT_INVD:
2098 		pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
2099 		result = ES_UNSUPPORTED;
2100 		break;
2101 	case SVM_EXIT_CPUID:
2102 		result = vc_handle_cpuid(ghcb, ctxt);
2103 		break;
2104 	case SVM_EXIT_IOIO:
2105 		result = vc_handle_ioio(ghcb, ctxt);
2106 		break;
2107 	case SVM_EXIT_MSR:
2108 		result = vc_handle_msr(ghcb, ctxt);
2109 		break;
2110 	case SVM_EXIT_VMMCALL:
2111 		result = vc_handle_vmmcall(ghcb, ctxt);
2112 		break;
2113 	case SVM_EXIT_WBINVD:
2114 		result = vc_handle_wbinvd(ghcb, ctxt);
2115 		break;
2116 	case SVM_EXIT_MONITOR:
2117 		result = vc_handle_monitor(ghcb, ctxt);
2118 		break;
2119 	case SVM_EXIT_MWAIT:
2120 		result = vc_handle_mwait(ghcb, ctxt);
2121 		break;
2122 	case SVM_EXIT_NPF:
2123 		result = vc_handle_mmio(ghcb, ctxt);
2124 		break;
2125 	default:
2126 		/*
2127 		 * Unexpected #VC exception
2128 		 */
2129 		result = ES_UNSUPPORTED;
2130 	}
2131 
2132 	return result;
2133 }
2134 
is_vc2_stack(unsigned long sp)2135 static __always_inline bool is_vc2_stack(unsigned long sp)
2136 {
2137 	return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
2138 }
2139 
vc_from_invalid_context(struct pt_regs * regs)2140 static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
2141 {
2142 	unsigned long sp, prev_sp;
2143 
2144 	sp      = (unsigned long)regs;
2145 	prev_sp = regs->sp;
2146 
2147 	/*
2148 	 * If the code was already executing on the VC2 stack when the #VC
2149 	 * happened, let it proceed to the normal handling routine. This way the
2150 	 * code executing on the VC2 stack can cause #VC exceptions to get handled.
2151 	 */
2152 	return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
2153 }
2154 
vc_raw_handle_exception(struct pt_regs * regs,unsigned long error_code)2155 static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
2156 {
2157 	struct ghcb_state state;
2158 	struct es_em_ctxt ctxt;
2159 	enum es_result result;
2160 	struct ghcb *ghcb;
2161 	bool ret = true;
2162 
2163 	ghcb = __sev_get_ghcb(&state);
2164 
2165 	vc_ghcb_invalidate(ghcb);
2166 	result = vc_init_em_ctxt(&ctxt, regs, error_code);
2167 
2168 	if (result == ES_OK)
2169 		result = vc_handle_exitcode(&ctxt, ghcb, error_code);
2170 
2171 	__sev_put_ghcb(&state);
2172 
2173 	/* Done - now check the result */
2174 	switch (result) {
2175 	case ES_OK:
2176 		vc_finish_insn(&ctxt);
2177 		break;
2178 	case ES_UNSUPPORTED:
2179 		pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
2180 				   error_code, regs->ip);
2181 		ret = false;
2182 		break;
2183 	case ES_VMM_ERROR:
2184 		pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
2185 				   error_code, regs->ip);
2186 		ret = false;
2187 		break;
2188 	case ES_DECODE_FAILED:
2189 		pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
2190 				   error_code, regs->ip);
2191 		ret = false;
2192 		break;
2193 	case ES_EXCEPTION:
2194 		vc_forward_exception(&ctxt);
2195 		break;
2196 	case ES_RETRY:
2197 		/* Nothing to do */
2198 		break;
2199 	default:
2200 		pr_emerg("Unknown result in %s():%d\n", __func__, result);
2201 		/*
2202 		 * Emulating the instruction which caused the #VC exception
2203 		 * failed - can't continue so print debug information
2204 		 */
2205 		BUG();
2206 	}
2207 
2208 	return ret;
2209 }
2210 
vc_is_db(unsigned long error_code)2211 static __always_inline bool vc_is_db(unsigned long error_code)
2212 {
2213 	return error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB;
2214 }
2215 
2216 /*
2217  * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode
2218  * and will panic when an error happens.
2219  */
DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)2220 DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
2221 {
2222 	irqentry_state_t irq_state;
2223 
2224 	/*
2225 	 * With the current implementation it is always possible to switch to a
2226 	 * safe stack because #VC exceptions only happen at known places, like
2227 	 * intercepted instructions or accesses to MMIO areas/IO ports. They can
2228 	 * also happen with code instrumentation when the hypervisor intercepts
2229 	 * #DB, but the critical paths are forbidden to be instrumented, so #DB
2230 	 * exceptions currently also only happen in safe places.
2231 	 *
2232 	 * But keep this here in case the noinstr annotations are violated due
2233 	 * to bug elsewhere.
2234 	 */
2235 	if (unlikely(vc_from_invalid_context(regs))) {
2236 		instrumentation_begin();
2237 		panic("Can't handle #VC exception from unsupported context\n");
2238 		instrumentation_end();
2239 	}
2240 
2241 	/*
2242 	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
2243 	 */
2244 	if (vc_is_db(error_code)) {
2245 		exc_debug(regs);
2246 		return;
2247 	}
2248 
2249 	irq_state = irqentry_nmi_enter(regs);
2250 
2251 	instrumentation_begin();
2252 
2253 	if (!vc_raw_handle_exception(regs, error_code)) {
2254 		/* Show some debug info */
2255 		show_regs(regs);
2256 
2257 		/* Ask hypervisor to sev_es_terminate */
2258 		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
2259 
2260 		/* If that fails and we get here - just panic */
2261 		panic("Returned from Terminate-Request to Hypervisor\n");
2262 	}
2263 
2264 	instrumentation_end();
2265 	irqentry_nmi_exit(regs, irq_state);
2266 }
2267 
2268 /*
2269  * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode
2270  * and will kill the current task with SIGBUS when an error happens.
2271  */
DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)2272 DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
2273 {
2274 	/*
2275 	 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
2276 	 */
2277 	if (vc_is_db(error_code)) {
2278 		noist_exc_debug(regs);
2279 		return;
2280 	}
2281 
2282 	irqentry_enter_from_user_mode(regs);
2283 	instrumentation_begin();
2284 
2285 	if (!vc_raw_handle_exception(regs, error_code)) {
2286 		/*
2287 		 * Do not kill the machine if user-space triggered the
2288 		 * exception. Send SIGBUS instead and let user-space deal with
2289 		 * it.
2290 		 */
2291 		force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
2292 	}
2293 
2294 	instrumentation_end();
2295 	irqentry_exit_to_user_mode(regs);
2296 }
2297 
handle_vc_boot_ghcb(struct pt_regs * regs)2298 bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
2299 {
2300 	unsigned long exit_code = regs->orig_ax;
2301 	struct es_em_ctxt ctxt;
2302 	enum es_result result;
2303 
2304 	vc_ghcb_invalidate(boot_ghcb);
2305 
2306 	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
2307 	if (result == ES_OK)
2308 		result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
2309 
2310 	/* Done - now check the result */
2311 	switch (result) {
2312 	case ES_OK:
2313 		vc_finish_insn(&ctxt);
2314 		break;
2315 	case ES_UNSUPPORTED:
2316 		early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
2317 				exit_code, regs->ip);
2318 		goto fail;
2319 	case ES_VMM_ERROR:
2320 		early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
2321 				exit_code, regs->ip);
2322 		goto fail;
2323 	case ES_DECODE_FAILED:
2324 		early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
2325 				exit_code, regs->ip);
2326 		goto fail;
2327 	case ES_EXCEPTION:
2328 		vc_early_forward_exception(&ctxt);
2329 		break;
2330 	case ES_RETRY:
2331 		/* Nothing to do */
2332 		break;
2333 	default:
2334 		BUG();
2335 	}
2336 
2337 	return true;
2338 
2339 fail:
2340 	show_regs(regs);
2341 
2342 	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
2343 }
2344 
2345 /*
2346  * Initial set up of SNP relies on information provided by the
2347  * Confidential Computing blob, which can be passed to the kernel
2348  * in the following ways, depending on how it is booted:
2349  *
2350  * - when booted via the boot/decompress kernel:
2351  *   - via boot_params
2352  *
2353  * - when booted directly by firmware/bootloader (e.g. CONFIG_PVH):
2354  *   - via a setup_data entry, as defined by the Linux Boot Protocol
2355  *
2356  * Scan for the blob in that order.
2357  */
find_cc_blob(struct boot_params * bp)2358 static __head struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
2359 {
2360 	struct cc_blob_sev_info *cc_info;
2361 
2362 	/* Boot kernel would have passed the CC blob via boot_params. */
2363 	if (bp->cc_blob_address) {
2364 		cc_info = (struct cc_blob_sev_info *)(unsigned long)bp->cc_blob_address;
2365 		goto found_cc_info;
2366 	}
2367 
2368 	/*
2369 	 * If kernel was booted directly, without the use of the
2370 	 * boot/decompression kernel, the CC blob may have been passed via
2371 	 * setup_data instead.
2372 	 */
2373 	cc_info = find_cc_blob_setup_data(bp);
2374 	if (!cc_info)
2375 		return NULL;
2376 
2377 found_cc_info:
2378 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
2379 		snp_abort();
2380 
2381 	return cc_info;
2382 }
2383 
svsm_setup(struct cc_blob_sev_info * cc_info)2384 static __head void svsm_setup(struct cc_blob_sev_info *cc_info)
2385 {
2386 	struct svsm_call call = {};
2387 	int ret;
2388 	u64 pa;
2389 
2390 	/*
2391 	 * Record the SVSM Calling Area address (CAA) if the guest is not
2392 	 * running at VMPL0. The CA will be used to communicate with the
2393 	 * SVSM to perform the SVSM services.
2394 	 */
2395 	if (!svsm_setup_ca(cc_info))
2396 		return;
2397 
2398 	/*
2399 	 * It is very early in the boot and the kernel is running identity
2400 	 * mapped but without having adjusted the pagetables to where the
2401 	 * kernel was loaded (physbase), so the get the CA address using
2402 	 * RIP-relative addressing.
2403 	 */
2404 	pa = (u64)&RIP_REL_REF(boot_svsm_ca_page);
2405 
2406 	/*
2407 	 * Switch over to the boot SVSM CA while the current CA is still
2408 	 * addressable. There is no GHCB at this point so use the MSR protocol.
2409 	 *
2410 	 * SVSM_CORE_REMAP_CA call:
2411 	 *   RAX = 0 (Protocol=0, CallID=0)
2412 	 *   RCX = New CA GPA
2413 	 */
2414 	call.caa = svsm_get_caa();
2415 	call.rax = SVSM_CORE_CALL(SVSM_CORE_REMAP_CA);
2416 	call.rcx = pa;
2417 	ret = svsm_perform_call_protocol(&call);
2418 	if (ret)
2419 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SVSM_CA_REMAP_FAIL);
2420 
2421 	RIP_REL_REF(boot_svsm_caa) = (struct svsm_ca *)pa;
2422 	RIP_REL_REF(boot_svsm_caa_pa) = pa;
2423 }
2424 
snp_init(struct boot_params * bp)2425 bool __head snp_init(struct boot_params *bp)
2426 {
2427 	struct cc_blob_sev_info *cc_info;
2428 
2429 	if (!bp)
2430 		return false;
2431 
2432 	cc_info = find_cc_blob(bp);
2433 	if (!cc_info)
2434 		return false;
2435 
2436 	if (cc_info->secrets_phys && cc_info->secrets_len == PAGE_SIZE)
2437 		secrets_pa = cc_info->secrets_phys;
2438 	else
2439 		return false;
2440 
2441 	setup_cpuid_table(cc_info);
2442 
2443 	svsm_setup(cc_info);
2444 
2445 	/*
2446 	 * The CC blob will be used later to access the secrets page. Cache
2447 	 * it here like the boot kernel does.
2448 	 */
2449 	bp->cc_blob_address = (u32)(unsigned long)cc_info;
2450 
2451 	return true;
2452 }
2453 
snp_abort(void)2454 void __head __noreturn snp_abort(void)
2455 {
2456 	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
2457 }
2458 
2459 /*
2460  * SEV-SNP guests should only execute dmi_setup() if EFI_CONFIG_TABLES are
2461  * enabled, as the alternative (fallback) logic for DMI probing in the legacy
2462  * ROM region can cause a crash since this region is not pre-validated.
2463  */
snp_dmi_setup(void)2464 void __init snp_dmi_setup(void)
2465 {
2466 	if (efi_enabled(EFI_CONFIG_TABLES))
2467 		dmi_setup();
2468 }
2469 
dump_cpuid_table(void)2470 static void dump_cpuid_table(void)
2471 {
2472 	const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
2473 	int i = 0;
2474 
2475 	pr_info("count=%d reserved=0x%x reserved2=0x%llx\n",
2476 		cpuid_table->count, cpuid_table->__reserved1, cpuid_table->__reserved2);
2477 
2478 	for (i = 0; i < SNP_CPUID_COUNT_MAX; i++) {
2479 		const struct snp_cpuid_fn *fn = &cpuid_table->fn[i];
2480 
2481 		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",
2482 			i, fn->eax_in, fn->ecx_in, fn->eax, fn->ebx, fn->ecx,
2483 			fn->edx, fn->xcr0_in, fn->xss_in, fn->__reserved);
2484 	}
2485 }
2486 
2487 /*
2488  * It is useful from an auditing/testing perspective to provide an easy way
2489  * for the guest owner to know that the CPUID table has been initialized as
2490  * expected, but that initialization happens too early in boot to print any
2491  * sort of indicator, and there's not really any other good place to do it,
2492  * so do it here.
2493  *
2494  * If running as an SNP guest, report the current VM privilege level (VMPL).
2495  */
report_snp_info(void)2496 static int __init report_snp_info(void)
2497 {
2498 	const struct snp_cpuid_table *cpuid_table = snp_cpuid_get_table();
2499 
2500 	if (cpuid_table->count) {
2501 		pr_info("Using SNP CPUID table, %d entries present.\n",
2502 			cpuid_table->count);
2503 
2504 		if (sev_cfg.debug)
2505 			dump_cpuid_table();
2506 	}
2507 
2508 	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2509 		pr_info("SNP running at VMPL%u.\n", snp_vmpl);
2510 
2511 	return 0;
2512 }
2513 arch_initcall(report_snp_info);
2514 
update_attest_input(struct svsm_call * call,struct svsm_attest_call * input)2515 static void update_attest_input(struct svsm_call *call, struct svsm_attest_call *input)
2516 {
2517 	/* If (new) lengths have been returned, propagate them up */
2518 	if (call->rcx_out != call->rcx)
2519 		input->manifest_buf.len = call->rcx_out;
2520 
2521 	if (call->rdx_out != call->rdx)
2522 		input->certificates_buf.len = call->rdx_out;
2523 
2524 	if (call->r8_out != call->r8)
2525 		input->report_buf.len = call->r8_out;
2526 }
2527 
snp_issue_svsm_attest_req(u64 call_id,struct svsm_call * call,struct svsm_attest_call * input)2528 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call,
2529 			      struct svsm_attest_call *input)
2530 {
2531 	struct svsm_attest_call *ac;
2532 	unsigned long flags;
2533 	u64 attest_call_pa;
2534 	int ret;
2535 
2536 	if (!snp_vmpl)
2537 		return -EINVAL;
2538 
2539 	local_irq_save(flags);
2540 
2541 	call->caa = svsm_get_caa();
2542 
2543 	ac = (struct svsm_attest_call *)call->caa->svsm_buffer;
2544 	attest_call_pa = svsm_get_caa_pa() + offsetof(struct svsm_ca, svsm_buffer);
2545 
2546 	*ac = *input;
2547 
2548 	/*
2549 	 * Set input registers for the request and set RDX and R8 to known
2550 	 * values in order to detect length values being returned in them.
2551 	 */
2552 	call->rax = call_id;
2553 	call->rcx = attest_call_pa;
2554 	call->rdx = -1;
2555 	call->r8 = -1;
2556 	ret = svsm_perform_call_protocol(call);
2557 	update_attest_input(call, input);
2558 
2559 	local_irq_restore(flags);
2560 
2561 	return ret;
2562 }
2563 EXPORT_SYMBOL_GPL(snp_issue_svsm_attest_req);
2564 
snp_issue_guest_request(struct snp_guest_req * req,struct snp_req_data * input,struct snp_guest_request_ioctl * rio)2565 static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
2566 				   struct snp_guest_request_ioctl *rio)
2567 {
2568 	struct ghcb_state state;
2569 	struct es_em_ctxt ctxt;
2570 	unsigned long flags;
2571 	struct ghcb *ghcb;
2572 	int ret;
2573 
2574 	rio->exitinfo2 = SEV_RET_NO_FW_CALL;
2575 
2576 	/*
2577 	 * __sev_get_ghcb() needs to run with IRQs disabled because it is using
2578 	 * a per-CPU GHCB.
2579 	 */
2580 	local_irq_save(flags);
2581 
2582 	ghcb = __sev_get_ghcb(&state);
2583 	if (!ghcb) {
2584 		ret = -EIO;
2585 		goto e_restore_irq;
2586 	}
2587 
2588 	vc_ghcb_invalidate(ghcb);
2589 
2590 	if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2591 		ghcb_set_rax(ghcb, input->data_gpa);
2592 		ghcb_set_rbx(ghcb, input->data_npages);
2593 	}
2594 
2595 	ret = sev_es_ghcb_hv_call(ghcb, &ctxt, req->exit_code, input->req_gpa, input->resp_gpa);
2596 	if (ret)
2597 		goto e_put;
2598 
2599 	rio->exitinfo2 = ghcb->save.sw_exit_info_2;
2600 	switch (rio->exitinfo2) {
2601 	case 0:
2602 		break;
2603 
2604 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_BUSY):
2605 		ret = -EAGAIN;
2606 		break;
2607 
2608 	case SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN):
2609 		/* Number of expected pages are returned in RBX */
2610 		if (req->exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) {
2611 			input->data_npages = ghcb_get_rbx(ghcb);
2612 			ret = -ENOSPC;
2613 			break;
2614 		}
2615 		fallthrough;
2616 	default:
2617 		ret = -EIO;
2618 		break;
2619 	}
2620 
2621 e_put:
2622 	__sev_put_ghcb(&state);
2623 e_restore_irq:
2624 	local_irq_restore(flags);
2625 
2626 	return ret;
2627 }
2628 
2629 static struct platform_device sev_guest_device = {
2630 	.name		= "sev-guest",
2631 	.id		= -1,
2632 };
2633 
snp_init_platform_device(void)2634 static int __init snp_init_platform_device(void)
2635 {
2636 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2637 		return -ENODEV;
2638 
2639 	if (platform_device_register(&sev_guest_device))
2640 		return -ENODEV;
2641 
2642 	pr_info("SNP guest platform device initialized.\n");
2643 	return 0;
2644 }
2645 device_initcall(snp_init_platform_device);
2646 
sev_show_status(void)2647 void sev_show_status(void)
2648 {
2649 	int i;
2650 
2651 	pr_info("Status: ");
2652 	for (i = 0; i < MSR_AMD64_SNP_RESV_BIT; i++) {
2653 		if (sev_status & BIT_ULL(i)) {
2654 			if (!sev_status_feat_names[i])
2655 				continue;
2656 
2657 			pr_cont("%s ", sev_status_feat_names[i]);
2658 		}
2659 	}
2660 	pr_cont("\n");
2661 }
2662 
snp_update_svsm_ca(void)2663 void __init snp_update_svsm_ca(void)
2664 {
2665 	if (!snp_vmpl)
2666 		return;
2667 
2668 	/* Update the CAA to a proper kernel address */
2669 	boot_svsm_caa = &boot_svsm_ca_page;
2670 }
2671 
2672 #ifdef CONFIG_SYSFS
vmpl_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)2673 static ssize_t vmpl_show(struct kobject *kobj,
2674 			 struct kobj_attribute *attr, char *buf)
2675 {
2676 	return sysfs_emit(buf, "%d\n", snp_vmpl);
2677 }
2678 
2679 static struct kobj_attribute vmpl_attr = __ATTR_RO(vmpl);
2680 
2681 static struct attribute *vmpl_attrs[] = {
2682 	&vmpl_attr.attr,
2683 	NULL
2684 };
2685 
2686 static struct attribute_group sev_attr_group = {
2687 	.attrs = vmpl_attrs,
2688 };
2689 
sev_sysfs_init(void)2690 static int __init sev_sysfs_init(void)
2691 {
2692 	struct kobject *sev_kobj;
2693 	struct device *dev_root;
2694 	int ret;
2695 
2696 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
2697 		return -ENODEV;
2698 
2699 	dev_root = bus_get_dev_root(&cpu_subsys);
2700 	if (!dev_root)
2701 		return -ENODEV;
2702 
2703 	sev_kobj = kobject_create_and_add("sev", &dev_root->kobj);
2704 	put_device(dev_root);
2705 
2706 	if (!sev_kobj)
2707 		return -ENOMEM;
2708 
2709 	ret = sysfs_create_group(sev_kobj, &sev_attr_group);
2710 	if (ret)
2711 		kobject_put(sev_kobj);
2712 
2713 	return ret;
2714 }
2715 arch_initcall(sev_sysfs_init);
2716 #endif // CONFIG_SYSFS
2717 
free_shared_pages(void * buf,size_t sz)2718 static void free_shared_pages(void *buf, size_t sz)
2719 {
2720 	unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
2721 	int ret;
2722 
2723 	if (!buf)
2724 		return;
2725 
2726 	ret = set_memory_encrypted((unsigned long)buf, npages);
2727 	if (ret) {
2728 		WARN_ONCE(ret, "failed to restore encryption mask (leak it)\n");
2729 		return;
2730 	}
2731 
2732 	__free_pages(virt_to_page(buf), get_order(sz));
2733 }
2734 
alloc_shared_pages(size_t sz)2735 static void *alloc_shared_pages(size_t sz)
2736 {
2737 	unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
2738 	struct page *page;
2739 	int ret;
2740 
2741 	page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz));
2742 	if (!page)
2743 		return NULL;
2744 
2745 	ret = set_memory_decrypted((unsigned long)page_address(page), npages);
2746 	if (ret) {
2747 		pr_err("failed to mark page shared, ret=%d\n", ret);
2748 		__free_pages(page, get_order(sz));
2749 		return NULL;
2750 	}
2751 
2752 	return page_address(page);
2753 }
2754 
get_vmpck(int id,struct snp_secrets_page * secrets,u32 ** seqno)2755 static u8 *get_vmpck(int id, struct snp_secrets_page *secrets, u32 **seqno)
2756 {
2757 	u8 *key = NULL;
2758 
2759 	switch (id) {
2760 	case 0:
2761 		*seqno = &secrets->os_area.msg_seqno_0;
2762 		key = secrets->vmpck0;
2763 		break;
2764 	case 1:
2765 		*seqno = &secrets->os_area.msg_seqno_1;
2766 		key = secrets->vmpck1;
2767 		break;
2768 	case 2:
2769 		*seqno = &secrets->os_area.msg_seqno_2;
2770 		key = secrets->vmpck2;
2771 		break;
2772 	case 3:
2773 		*seqno = &secrets->os_area.msg_seqno_3;
2774 		key = secrets->vmpck3;
2775 		break;
2776 	default:
2777 		break;
2778 	}
2779 
2780 	return key;
2781 }
2782 
snp_init_crypto(u8 * key,size_t keylen)2783 static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
2784 {
2785 	struct aesgcm_ctx *ctx;
2786 
2787 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
2788 	if (!ctx)
2789 		return NULL;
2790 
2791 	if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) {
2792 		pr_err("Crypto context initialization failed\n");
2793 		kfree(ctx);
2794 		return NULL;
2795 	}
2796 
2797 	return ctx;
2798 }
2799 
snp_msg_init(struct snp_msg_desc * mdesc,int vmpck_id)2800 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id)
2801 {
2802 	/* Adjust the default VMPCK key based on the executing VMPL level */
2803 	if (vmpck_id == -1)
2804 		vmpck_id = snp_vmpl;
2805 
2806 	mdesc->vmpck = get_vmpck(vmpck_id, mdesc->secrets, &mdesc->os_area_msg_seqno);
2807 	if (!mdesc->vmpck) {
2808 		pr_err("Invalid VMPCK%d communication key\n", vmpck_id);
2809 		return -EINVAL;
2810 	}
2811 
2812 	/* Verify that VMPCK is not zero. */
2813 	if (!memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
2814 		pr_err("Empty VMPCK%d communication key\n", vmpck_id);
2815 		return -EINVAL;
2816 	}
2817 
2818 	mdesc->vmpck_id = vmpck_id;
2819 
2820 	mdesc->ctx = snp_init_crypto(mdesc->vmpck, VMPCK_KEY_LEN);
2821 	if (!mdesc->ctx)
2822 		return -ENOMEM;
2823 
2824 	return 0;
2825 }
2826 EXPORT_SYMBOL_GPL(snp_msg_init);
2827 
snp_msg_alloc(void)2828 struct snp_msg_desc *snp_msg_alloc(void)
2829 {
2830 	struct snp_msg_desc *mdesc;
2831 	void __iomem *mem;
2832 
2833 	BUILD_BUG_ON(sizeof(struct snp_guest_msg) > PAGE_SIZE);
2834 
2835 	mdesc = kzalloc(sizeof(struct snp_msg_desc), GFP_KERNEL);
2836 	if (!mdesc)
2837 		return ERR_PTR(-ENOMEM);
2838 
2839 	mem = ioremap_encrypted(secrets_pa, PAGE_SIZE);
2840 	if (!mem)
2841 		goto e_free_mdesc;
2842 
2843 	mdesc->secrets = (__force struct snp_secrets_page *)mem;
2844 
2845 	/* Allocate the shared page used for the request and response message. */
2846 	mdesc->request = alloc_shared_pages(sizeof(struct snp_guest_msg));
2847 	if (!mdesc->request)
2848 		goto e_unmap;
2849 
2850 	mdesc->response = alloc_shared_pages(sizeof(struct snp_guest_msg));
2851 	if (!mdesc->response)
2852 		goto e_free_request;
2853 
2854 	return mdesc;
2855 
2856 e_free_request:
2857 	free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg));
2858 e_unmap:
2859 	iounmap(mem);
2860 e_free_mdesc:
2861 	kfree(mdesc);
2862 
2863 	return ERR_PTR(-ENOMEM);
2864 }
2865 EXPORT_SYMBOL_GPL(snp_msg_alloc);
2866 
snp_msg_free(struct snp_msg_desc * mdesc)2867 void snp_msg_free(struct snp_msg_desc *mdesc)
2868 {
2869 	if (!mdesc)
2870 		return;
2871 
2872 	kfree(mdesc->ctx);
2873 	free_shared_pages(mdesc->response, sizeof(struct snp_guest_msg));
2874 	free_shared_pages(mdesc->request, sizeof(struct snp_guest_msg));
2875 	iounmap((__force void __iomem *)mdesc->secrets);
2876 
2877 	memset(mdesc, 0, sizeof(*mdesc));
2878 	kfree(mdesc);
2879 }
2880 EXPORT_SYMBOL_GPL(snp_msg_free);
2881 
2882 /* Mutex to serialize the shared buffer access and command handling. */
2883 static DEFINE_MUTEX(snp_cmd_mutex);
2884 
2885 /*
2886  * If an error is received from the host or AMD Secure Processor (ASP) there
2887  * are two options. Either retry the exact same encrypted request or discontinue
2888  * using the VMPCK.
2889  *
2890  * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
2891  * encrypt the requests. The IV for this scheme is the sequence number. GCM
2892  * cannot tolerate IV reuse.
2893  *
2894  * The ASP FW v1.51 only increments the sequence numbers on a successful
2895  * guest<->ASP back and forth and only accepts messages at its exact sequence
2896  * number.
2897  *
2898  * So if the sequence number were to be reused the encryption scheme is
2899  * vulnerable. If the sequence number were incremented for a fresh IV the ASP
2900  * will reject the request.
2901  */
snp_disable_vmpck(struct snp_msg_desc * mdesc)2902 static void snp_disable_vmpck(struct snp_msg_desc *mdesc)
2903 {
2904 	pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n",
2905 		  mdesc->vmpck_id);
2906 	memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN);
2907 	mdesc->vmpck = NULL;
2908 }
2909 
__snp_get_msg_seqno(struct snp_msg_desc * mdesc)2910 static inline u64 __snp_get_msg_seqno(struct snp_msg_desc *mdesc)
2911 {
2912 	u64 count;
2913 
2914 	lockdep_assert_held(&snp_cmd_mutex);
2915 
2916 	/* Read the current message sequence counter from secrets pages */
2917 	count = *mdesc->os_area_msg_seqno;
2918 
2919 	return count + 1;
2920 }
2921 
2922 /* Return a non-zero on success */
snp_get_msg_seqno(struct snp_msg_desc * mdesc)2923 static u64 snp_get_msg_seqno(struct snp_msg_desc *mdesc)
2924 {
2925 	u64 count = __snp_get_msg_seqno(mdesc);
2926 
2927 	/*
2928 	 * The message sequence counter for the SNP guest request is a  64-bit
2929 	 * value but the version 2 of GHCB specification defines a 32-bit storage
2930 	 * for it. If the counter exceeds the 32-bit value then return zero.
2931 	 * The caller should check the return value, but if the caller happens to
2932 	 * not check the value and use it, then the firmware treats zero as an
2933 	 * invalid number and will fail the  message request.
2934 	 */
2935 	if (count >= UINT_MAX) {
2936 		pr_err("request message sequence counter overflow\n");
2937 		return 0;
2938 	}
2939 
2940 	return count;
2941 }
2942 
snp_inc_msg_seqno(struct snp_msg_desc * mdesc)2943 static void snp_inc_msg_seqno(struct snp_msg_desc *mdesc)
2944 {
2945 	/*
2946 	 * The counter is also incremented by the PSP, so increment it by 2
2947 	 * and save in secrets page.
2948 	 */
2949 	*mdesc->os_area_msg_seqno += 2;
2950 }
2951 
verify_and_dec_payload(struct snp_msg_desc * mdesc,struct snp_guest_req * req)2952 static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req)
2953 {
2954 	struct snp_guest_msg *resp_msg = &mdesc->secret_response;
2955 	struct snp_guest_msg *req_msg = &mdesc->secret_request;
2956 	struct snp_guest_msg_hdr *req_msg_hdr = &req_msg->hdr;
2957 	struct snp_guest_msg_hdr *resp_msg_hdr = &resp_msg->hdr;
2958 	struct aesgcm_ctx *ctx = mdesc->ctx;
2959 	u8 iv[GCM_AES_IV_SIZE] = {};
2960 
2961 	pr_debug("response [seqno %lld type %d version %d sz %d]\n",
2962 		 resp_msg_hdr->msg_seqno, resp_msg_hdr->msg_type, resp_msg_hdr->msg_version,
2963 		 resp_msg_hdr->msg_sz);
2964 
2965 	/* Copy response from shared memory to encrypted memory. */
2966 	memcpy(resp_msg, mdesc->response, sizeof(*resp_msg));
2967 
2968 	/* Verify that the sequence counter is incremented by 1 */
2969 	if (unlikely(resp_msg_hdr->msg_seqno != (req_msg_hdr->msg_seqno + 1)))
2970 		return -EBADMSG;
2971 
2972 	/* Verify response message type and version number. */
2973 	if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) ||
2974 	    resp_msg_hdr->msg_version != req_msg_hdr->msg_version)
2975 		return -EBADMSG;
2976 
2977 	/*
2978 	 * If the message size is greater than our buffer length then return
2979 	 * an error.
2980 	 */
2981 	if (unlikely((resp_msg_hdr->msg_sz + ctx->authsize) > req->resp_sz))
2982 		return -EBADMSG;
2983 
2984 	/* Decrypt the payload */
2985 	memcpy(iv, &resp_msg_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_msg_hdr->msg_seqno)));
2986 	if (!aesgcm_decrypt(ctx, req->resp_buf, resp_msg->payload, resp_msg_hdr->msg_sz,
2987 			    &resp_msg_hdr->algo, AAD_LEN, iv, resp_msg_hdr->authtag))
2988 		return -EBADMSG;
2989 
2990 	return 0;
2991 }
2992 
enc_payload(struct snp_msg_desc * mdesc,u64 seqno,struct snp_guest_req * req)2993 static int enc_payload(struct snp_msg_desc *mdesc, u64 seqno, struct snp_guest_req *req)
2994 {
2995 	struct snp_guest_msg *msg = &mdesc->secret_request;
2996 	struct snp_guest_msg_hdr *hdr = &msg->hdr;
2997 	struct aesgcm_ctx *ctx = mdesc->ctx;
2998 	u8 iv[GCM_AES_IV_SIZE] = {};
2999 
3000 	memset(msg, 0, sizeof(*msg));
3001 
3002 	hdr->algo = SNP_AEAD_AES_256_GCM;
3003 	hdr->hdr_version = MSG_HDR_VER;
3004 	hdr->hdr_sz = sizeof(*hdr);
3005 	hdr->msg_type = req->msg_type;
3006 	hdr->msg_version = req->msg_version;
3007 	hdr->msg_seqno = seqno;
3008 	hdr->msg_vmpck = req->vmpck_id;
3009 	hdr->msg_sz = req->req_sz;
3010 
3011 	/* Verify the sequence number is non-zero */
3012 	if (!hdr->msg_seqno)
3013 		return -ENOSR;
3014 
3015 	pr_debug("request [seqno %lld type %d version %d sz %d]\n",
3016 		 hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
3017 
3018 	if (WARN_ON((req->req_sz + ctx->authsize) > sizeof(msg->payload)))
3019 		return -EBADMSG;
3020 
3021 	memcpy(iv, &hdr->msg_seqno, min(sizeof(iv), sizeof(hdr->msg_seqno)));
3022 	aesgcm_encrypt(ctx, msg->payload, req->req_buf, req->req_sz, &hdr->algo,
3023 		       AAD_LEN, iv, hdr->authtag);
3024 
3025 	return 0;
3026 }
3027 
__handle_guest_request(struct snp_msg_desc * mdesc,struct snp_guest_req * req,struct snp_guest_request_ioctl * rio)3028 static int __handle_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
3029 				  struct snp_guest_request_ioctl *rio)
3030 {
3031 	unsigned long req_start = jiffies;
3032 	unsigned int override_npages = 0;
3033 	u64 override_err = 0;
3034 	int rc;
3035 
3036 retry_request:
3037 	/*
3038 	 * Call firmware to process the request. In this function the encrypted
3039 	 * message enters shared memory with the host. So after this call the
3040 	 * sequence number must be incremented or the VMPCK must be deleted to
3041 	 * prevent reuse of the IV.
3042 	 */
3043 	rc = snp_issue_guest_request(req, &req->input, rio);
3044 	switch (rc) {
3045 	case -ENOSPC:
3046 		/*
3047 		 * If the extended guest request fails due to having too
3048 		 * small of a certificate data buffer, retry the same
3049 		 * guest request without the extended data request in
3050 		 * order to increment the sequence number and thus avoid
3051 		 * IV reuse.
3052 		 */
3053 		override_npages = req->input.data_npages;
3054 		req->exit_code	= SVM_VMGEXIT_GUEST_REQUEST;
3055 
3056 		/*
3057 		 * Override the error to inform callers the given extended
3058 		 * request buffer size was too small and give the caller the
3059 		 * required buffer size.
3060 		 */
3061 		override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN);
3062 
3063 		/*
3064 		 * If this call to the firmware succeeds, the sequence number can
3065 		 * be incremented allowing for continued use of the VMPCK. If
3066 		 * there is an error reflected in the return value, this value
3067 		 * is checked further down and the result will be the deletion
3068 		 * of the VMPCK and the error code being propagated back to the
3069 		 * user as an ioctl() return code.
3070 		 */
3071 		goto retry_request;
3072 
3073 	/*
3074 	 * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been
3075 	 * throttled. Retry in the driver to avoid returning and reusing the
3076 	 * message sequence number on a different message.
3077 	 */
3078 	case -EAGAIN:
3079 		if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) {
3080 			rc = -ETIMEDOUT;
3081 			break;
3082 		}
3083 		schedule_timeout_killable(SNP_REQ_RETRY_DELAY);
3084 		goto retry_request;
3085 	}
3086 
3087 	/*
3088 	 * Increment the message sequence number. There is no harm in doing
3089 	 * this now because decryption uses the value stored in the response
3090 	 * structure and any failure will wipe the VMPCK, preventing further
3091 	 * use anyway.
3092 	 */
3093 	snp_inc_msg_seqno(mdesc);
3094 
3095 	if (override_err) {
3096 		rio->exitinfo2 = override_err;
3097 
3098 		/*
3099 		 * If an extended guest request was issued and the supplied certificate
3100 		 * buffer was not large enough, a standard guest request was issued to
3101 		 * prevent IV reuse. If the standard request was successful, return -EIO
3102 		 * back to the caller as would have originally been returned.
3103 		 */
3104 		if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))
3105 			rc = -EIO;
3106 	}
3107 
3108 	if (override_npages)
3109 		req->input.data_npages = override_npages;
3110 
3111 	return rc;
3112 }
3113 
snp_send_guest_request(struct snp_msg_desc * mdesc,struct snp_guest_req * req,struct snp_guest_request_ioctl * rio)3114 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
3115 			   struct snp_guest_request_ioctl *rio)
3116 {
3117 	u64 seqno;
3118 	int rc;
3119 
3120 	guard(mutex)(&snp_cmd_mutex);
3121 
3122 	/* Check if the VMPCK is not empty */
3123 	if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
3124 		pr_err_ratelimited("VMPCK is disabled\n");
3125 		return -ENOTTY;
3126 	}
3127 
3128 	/* Get message sequence and verify that its a non-zero */
3129 	seqno = snp_get_msg_seqno(mdesc);
3130 	if (!seqno)
3131 		return -EIO;
3132 
3133 	/* Clear shared memory's response for the host to populate. */
3134 	memset(mdesc->response, 0, sizeof(struct snp_guest_msg));
3135 
3136 	/* Encrypt the userspace provided payload in mdesc->secret_request. */
3137 	rc = enc_payload(mdesc, seqno, req);
3138 	if (rc)
3139 		return rc;
3140 
3141 	/*
3142 	 * Write the fully encrypted request to the shared unencrypted
3143 	 * request page.
3144 	 */
3145 	memcpy(mdesc->request, &mdesc->secret_request, sizeof(mdesc->secret_request));
3146 
3147 	/* Initialize the input address for guest request */
3148 	req->input.req_gpa = __pa(mdesc->request);
3149 	req->input.resp_gpa = __pa(mdesc->response);
3150 	req->input.data_gpa = req->certs_data ? __pa(req->certs_data) : 0;
3151 
3152 	rc = __handle_guest_request(mdesc, req, rio);
3153 	if (rc) {
3154 		if (rc == -EIO &&
3155 		    rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))
3156 			return rc;
3157 
3158 		pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n",
3159 			 rc, rio->exitinfo2);
3160 
3161 		snp_disable_vmpck(mdesc);
3162 		return rc;
3163 	}
3164 
3165 	rc = verify_and_dec_payload(mdesc, req);
3166 	if (rc) {
3167 		pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc);
3168 		snp_disable_vmpck(mdesc);
3169 		return rc;
3170 	}
3171 
3172 	return 0;
3173 }
3174 EXPORT_SYMBOL_GPL(snp_send_guest_request);
3175 
snp_get_tsc_info(void)3176 static int __init snp_get_tsc_info(void)
3177 {
3178 	struct snp_guest_request_ioctl *rio;
3179 	struct snp_tsc_info_resp *tsc_resp;
3180 	struct snp_tsc_info_req *tsc_req;
3181 	struct snp_msg_desc *mdesc;
3182 	struct snp_guest_req *req;
3183 	int rc = -ENOMEM;
3184 
3185 	tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL);
3186 	if (!tsc_req)
3187 		return rc;
3188 
3189 	/*
3190 	 * The intermediate response buffer is used while decrypting the
3191 	 * response payload. Make sure that it has enough space to cover
3192 	 * the authtag.
3193 	 */
3194 	tsc_resp = kzalloc(sizeof(*tsc_resp) + AUTHTAG_LEN, GFP_KERNEL);
3195 	if (!tsc_resp)
3196 		goto e_free_tsc_req;
3197 
3198 	req = kzalloc(sizeof(*req), GFP_KERNEL);
3199 	if (!req)
3200 		goto e_free_tsc_resp;
3201 
3202 	rio = kzalloc(sizeof(*rio), GFP_KERNEL);
3203 	if (!rio)
3204 		goto e_free_req;
3205 
3206 	mdesc = snp_msg_alloc();
3207 	if (IS_ERR_OR_NULL(mdesc))
3208 		goto e_free_rio;
3209 
3210 	rc = snp_msg_init(mdesc, snp_vmpl);
3211 	if (rc)
3212 		goto e_free_mdesc;
3213 
3214 	req->msg_version = MSG_HDR_VER;
3215 	req->msg_type = SNP_MSG_TSC_INFO_REQ;
3216 	req->vmpck_id = snp_vmpl;
3217 	req->req_buf = tsc_req;
3218 	req->req_sz = sizeof(*tsc_req);
3219 	req->resp_buf = (void *)tsc_resp;
3220 	req->resp_sz = sizeof(*tsc_resp) + AUTHTAG_LEN;
3221 	req->exit_code = SVM_VMGEXIT_GUEST_REQUEST;
3222 
3223 	rc = snp_send_guest_request(mdesc, req, rio);
3224 	if (rc)
3225 		goto e_request;
3226 
3227 	pr_debug("%s: response status 0x%x scale 0x%llx offset 0x%llx factor 0x%x\n",
3228 		 __func__, tsc_resp->status, tsc_resp->tsc_scale, tsc_resp->tsc_offset,
3229 		 tsc_resp->tsc_factor);
3230 
3231 	if (!tsc_resp->status) {
3232 		snp_tsc_scale = tsc_resp->tsc_scale;
3233 		snp_tsc_offset = tsc_resp->tsc_offset;
3234 	} else {
3235 		pr_err("Failed to get TSC info, response status 0x%x\n", tsc_resp->status);
3236 		rc = -EIO;
3237 	}
3238 
3239 e_request:
3240 	/* The response buffer contains sensitive data, explicitly clear it. */
3241 	memzero_explicit(tsc_resp, sizeof(*tsc_resp) + AUTHTAG_LEN);
3242 e_free_mdesc:
3243 	snp_msg_free(mdesc);
3244 e_free_rio:
3245 	kfree(rio);
3246 e_free_req:
3247 	kfree(req);
3248  e_free_tsc_resp:
3249 	kfree(tsc_resp);
3250 e_free_tsc_req:
3251 	kfree(tsc_req);
3252 
3253 	return rc;
3254 }
3255 
snp_secure_tsc_prepare(void)3256 void __init snp_secure_tsc_prepare(void)
3257 {
3258 	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
3259 		return;
3260 
3261 	if (snp_get_tsc_info()) {
3262 		pr_alert("Unable to retrieve Secure TSC info from ASP\n");
3263 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC);
3264 	}
3265 
3266 	pr_debug("SecureTSC enabled");
3267 }
3268 
securetsc_get_tsc_khz(void)3269 static unsigned long securetsc_get_tsc_khz(void)
3270 {
3271 	return snp_tsc_freq_khz;
3272 }
3273 
snp_secure_tsc_init(void)3274 void __init snp_secure_tsc_init(void)
3275 {
3276 	unsigned long long tsc_freq_mhz;
3277 
3278 	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
3279 		return;
3280 
3281 	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
3282 	rdmsrl(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz);
3283 	snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000);
3284 
3285 	x86_platform.calibrate_cpu = securetsc_get_tsc_khz;
3286 	x86_platform.calibrate_tsc = securetsc_get_tsc_khz;
3287 }
3288