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 <jroedel@suse.de>
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/mem_encrypt.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
22 #include <asm/cpu_entry_area.h>
23 #include <asm/stacktrace.h>
24 #include <asm/sev.h>
25 #include <asm/insn-eval.h>
26 #include <asm/fpu/internal.h>
27 #include <asm/processor.h>
28 #include <asm/realmode.h>
29 #include <asm/traps.h>
30 #include <asm/svm.h>
31 #include <asm/smp.h>
32 #include <asm/cpu.h>
33
34 #define DR7_RESET_VALUE 0x400
35
36 /* For early boot hypervisor communication in SEV-ES enabled guests */
37 static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
38
39 /*
40 * Needs to be in the .data section because we need it NULL before bss is
41 * cleared
42 */
43 static struct ghcb __initdata *boot_ghcb;
44
45 /* #VC handler runtime per-CPU data */
46 struct sev_es_runtime_data {
47 struct ghcb ghcb_page;
48
49 /*
50 * Reserve one page per CPU as backup storage for the unencrypted GHCB.
51 * It is needed when an NMI happens while the #VC handler uses the real
52 * GHCB, and the NMI handler itself is causing another #VC exception. In
53 * that case the GHCB content of the first handler needs to be backed up
54 * and restored.
55 */
56 struct ghcb backup_ghcb;
57
58 /*
59 * Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions.
60 * There is no need for it to be atomic, because nothing is written to
61 * the GHCB between the read and the write of ghcb_active. So it is safe
62 * to use it when a nested #VC exception happens before the write.
63 *
64 * This is necessary for example in the #VC->NMI->#VC case when the NMI
65 * happens while the first #VC handler uses the GHCB. When the NMI code
66 * raises a second #VC handler it might overwrite the contents of the
67 * GHCB written by the first handler. To avoid this the content of the
68 * GHCB is saved and restored when the GHCB is detected to be in use
69 * already.
70 */
71 bool ghcb_active;
72 bool backup_ghcb_active;
73
74 /*
75 * Cached DR7 value - write it on DR7 writes and return it on reads.
76 * That value will never make it to the real hardware DR7 as debugging
77 * is currently unsupported in SEV-ES guests.
78 */
79 unsigned long dr7;
80 };
81
82 struct ghcb_state {
83 struct ghcb *ghcb;
84 };
85
86 static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
87 DEFINE_STATIC_KEY_FALSE(sev_es_enable_key);
88
89 /* Needed in vc_early_forward_exception */
90 void do_early_exception(struct pt_regs *regs, int trapnr);
91
on_vc_stack(struct pt_regs * regs)92 static __always_inline bool on_vc_stack(struct pt_regs *regs)
93 {
94 unsigned long sp = regs->sp;
95
96 /* User-mode RSP is not trusted */
97 if (user_mode(regs))
98 return false;
99
100 /* SYSCALL gap still has user-mode RSP */
101 if (ip_within_syscall_gap(regs))
102 return false;
103
104 return ((sp >= __this_cpu_ist_bottom_va(VC)) && (sp < __this_cpu_ist_top_va(VC)));
105 }
106
107 /*
108 * This function handles the case when an NMI is raised in the #VC
109 * exception handler entry code, before the #VC handler has switched off
110 * its IST stack. In this case, the IST entry for #VC must be adjusted,
111 * so that any nested #VC exception will not overwrite the stack
112 * contents of the interrupted #VC handler.
113 *
114 * The IST entry is adjusted unconditionally so that it can be also be
115 * unconditionally adjusted back in __sev_es_ist_exit(). Otherwise a
116 * nested sev_es_ist_exit() call may adjust back the IST entry too
117 * early.
118 *
119 * The __sev_es_ist_enter() and __sev_es_ist_exit() functions always run
120 * on the NMI IST stack, as they are only called from NMI handling code
121 * right now.
122 */
__sev_es_ist_enter(struct pt_regs * regs)123 void noinstr __sev_es_ist_enter(struct pt_regs *regs)
124 {
125 unsigned long old_ist, new_ist;
126
127 /* Read old IST entry */
128 new_ist = old_ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
129
130 /*
131 * If NMI happened while on the #VC IST stack, set the new IST
132 * value below regs->sp, so that the interrupted stack frame is
133 * not overwritten by subsequent #VC exceptions.
134 */
135 if (on_vc_stack(regs))
136 new_ist = regs->sp;
137
138 /*
139 * Reserve additional 8 bytes and store old IST value so this
140 * adjustment can be unrolled in __sev_es_ist_exit().
141 */
142 new_ist -= sizeof(old_ist);
143 *(unsigned long *)new_ist = old_ist;
144
145 /* Set new IST entry */
146 this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], new_ist);
147 }
148
__sev_es_ist_exit(void)149 void noinstr __sev_es_ist_exit(void)
150 {
151 unsigned long ist;
152
153 /* Read IST entry */
154 ist = __this_cpu_read(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC]);
155
156 if (WARN_ON(ist == __this_cpu_ist_top_va(VC)))
157 return;
158
159 /* Read back old IST entry and write it to the TSS */
160 this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist);
161 }
162
163 /*
164 * Nothing shall interrupt this code path while holding the per-CPU
165 * GHCB. The backup GHCB is only for NMIs interrupting this path.
166 *
167 * Callers must disable local interrupts around it.
168 */
__sev_get_ghcb(struct ghcb_state * state)169 static noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state)
170 {
171 struct sev_es_runtime_data *data;
172 struct ghcb *ghcb;
173
174 WARN_ON(!irqs_disabled());
175
176 data = this_cpu_read(runtime_data);
177 ghcb = &data->ghcb_page;
178
179 if (unlikely(data->ghcb_active)) {
180 /* GHCB is already in use - save its contents */
181
182 if (unlikely(data->backup_ghcb_active)) {
183 /*
184 * Backup-GHCB is also already in use. There is no way
185 * to continue here so just kill the machine. To make
186 * panic() work, mark GHCBs inactive so that messages
187 * can be printed out.
188 */
189 data->ghcb_active = false;
190 data->backup_ghcb_active = false;
191
192 instrumentation_begin();
193 panic("Unable to handle #VC exception! GHCB and Backup GHCB are already in use");
194 instrumentation_end();
195 }
196
197 /* Mark backup_ghcb active before writing to it */
198 data->backup_ghcb_active = true;
199
200 state->ghcb = &data->backup_ghcb;
201
202 /* Backup GHCB content */
203 *state->ghcb = *ghcb;
204 } else {
205 state->ghcb = NULL;
206 data->ghcb_active = true;
207 }
208
209 return ghcb;
210 }
211
212 /* Needed in vc_early_forward_exception */
213 void do_early_exception(struct pt_regs *regs, int trapnr);
214
sev_es_rd_ghcb_msr(void)215 static inline u64 sev_es_rd_ghcb_msr(void)
216 {
217 return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
218 }
219
sev_es_wr_ghcb_msr(u64 val)220 static __always_inline void sev_es_wr_ghcb_msr(u64 val)
221 {
222 u32 low, high;
223
224 low = (u32)(val);
225 high = (u32)(val >> 32);
226
227 native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
228 }
229
vc_fetch_insn_kernel(struct es_em_ctxt * ctxt,unsigned char * buffer)230 static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
231 unsigned char *buffer)
232 {
233 return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
234 }
235
__vc_decode_user_insn(struct es_em_ctxt * ctxt)236 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
237 {
238 char buffer[MAX_INSN_SIZE];
239 int insn_bytes;
240
241 insn_bytes = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
242 if (insn_bytes == 0) {
243 /* Nothing could be copied */
244 ctxt->fi.vector = X86_TRAP_PF;
245 ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
246 ctxt->fi.cr2 = ctxt->regs->ip;
247 return ES_EXCEPTION;
248 } else if (insn_bytes == -EINVAL) {
249 /* Effective RIP could not be calculated */
250 ctxt->fi.vector = X86_TRAP_GP;
251 ctxt->fi.error_code = 0;
252 ctxt->fi.cr2 = 0;
253 return ES_EXCEPTION;
254 }
255
256 if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, insn_bytes))
257 return ES_DECODE_FAILED;
258
259 if (ctxt->insn.immediate.got)
260 return ES_OK;
261 else
262 return ES_DECODE_FAILED;
263 }
264
__vc_decode_kern_insn(struct es_em_ctxt * ctxt)265 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
266 {
267 char buffer[MAX_INSN_SIZE];
268 int res, ret;
269
270 res = vc_fetch_insn_kernel(ctxt, buffer);
271 if (res) {
272 ctxt->fi.vector = X86_TRAP_PF;
273 ctxt->fi.error_code = X86_PF_INSTR;
274 ctxt->fi.cr2 = ctxt->regs->ip;
275 return ES_EXCEPTION;
276 }
277
278 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
279 if (ret < 0)
280 return ES_DECODE_FAILED;
281 else
282 return ES_OK;
283 }
284
vc_decode_insn(struct es_em_ctxt * ctxt)285 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
286 {
287 if (user_mode(ctxt->regs))
288 return __vc_decode_user_insn(ctxt);
289 else
290 return __vc_decode_kern_insn(ctxt);
291 }
292
vc_write_mem(struct es_em_ctxt * ctxt,char * dst,char * buf,size_t size)293 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
294 char *dst, char *buf, size_t size)
295 {
296 unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
297
298 /*
299 * This function uses __put_user() independent of whether kernel or user
300 * memory is accessed. This works fine because __put_user() does no
301 * sanity checks of the pointer being accessed. All that it does is
302 * to report when the access failed.
303 *
304 * Also, this function runs in atomic context, so __put_user() is not
305 * allowed to sleep. The page-fault handler detects that it is running
306 * in atomic context and will not try to take mmap_sem and handle the
307 * fault, so additional pagefault_enable()/disable() calls are not
308 * needed.
309 *
310 * The access can't be done via copy_to_user() here because
311 * vc_write_mem() must not use string instructions to access unsafe
312 * memory. The reason is that MOVS is emulated by the #VC handler by
313 * splitting the move up into a read and a write and taking a nested #VC
314 * exception on whatever of them is the MMIO access. Using string
315 * instructions here would cause infinite nesting.
316 */
317 switch (size) {
318 case 1: {
319 u8 d1;
320 u8 __user *target = (u8 __user *)dst;
321
322 memcpy(&d1, buf, 1);
323 if (__put_user(d1, target))
324 goto fault;
325 break;
326 }
327 case 2: {
328 u16 d2;
329 u16 __user *target = (u16 __user *)dst;
330
331 memcpy(&d2, buf, 2);
332 if (__put_user(d2, target))
333 goto fault;
334 break;
335 }
336 case 4: {
337 u32 d4;
338 u32 __user *target = (u32 __user *)dst;
339
340 memcpy(&d4, buf, 4);
341 if (__put_user(d4, target))
342 goto fault;
343 break;
344 }
345 case 8: {
346 u64 d8;
347 u64 __user *target = (u64 __user *)dst;
348
349 memcpy(&d8, buf, 8);
350 if (__put_user(d8, target))
351 goto fault;
352 break;
353 }
354 default:
355 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
356 return ES_UNSUPPORTED;
357 }
358
359 return ES_OK;
360
361 fault:
362 if (user_mode(ctxt->regs))
363 error_code |= X86_PF_USER;
364
365 ctxt->fi.vector = X86_TRAP_PF;
366 ctxt->fi.error_code = error_code;
367 ctxt->fi.cr2 = (unsigned long)dst;
368
369 return ES_EXCEPTION;
370 }
371
vc_read_mem(struct es_em_ctxt * ctxt,char * src,char * buf,size_t size)372 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
373 char *src, char *buf, size_t size)
374 {
375 unsigned long error_code = X86_PF_PROT;
376
377 /*
378 * This function uses __get_user() independent of whether kernel or user
379 * memory is accessed. This works fine because __get_user() does no
380 * sanity checks of the pointer being accessed. All that it does is
381 * to report when the access failed.
382 *
383 * Also, this function runs in atomic context, so __get_user() is not
384 * allowed to sleep. The page-fault handler detects that it is running
385 * in atomic context and will not try to take mmap_sem and handle the
386 * fault, so additional pagefault_enable()/disable() calls are not
387 * needed.
388 *
389 * The access can't be done via copy_from_user() here because
390 * vc_read_mem() must not use string instructions to access unsafe
391 * memory. The reason is that MOVS is emulated by the #VC handler by
392 * splitting the move up into a read and a write and taking a nested #VC
393 * exception on whatever of them is the MMIO access. Using string
394 * instructions here would cause infinite nesting.
395 */
396 switch (size) {
397 case 1: {
398 u8 d1;
399 u8 __user *s = (u8 __user *)src;
400
401 if (__get_user(d1, s))
402 goto fault;
403 memcpy(buf, &d1, 1);
404 break;
405 }
406 case 2: {
407 u16 d2;
408 u16 __user *s = (u16 __user *)src;
409
410 if (__get_user(d2, s))
411 goto fault;
412 memcpy(buf, &d2, 2);
413 break;
414 }
415 case 4: {
416 u32 d4;
417 u32 __user *s = (u32 __user *)src;
418
419 if (__get_user(d4, s))
420 goto fault;
421 memcpy(buf, &d4, 4);
422 break;
423 }
424 case 8: {
425 u64 d8;
426 u64 __user *s = (u64 __user *)src;
427 if (__get_user(d8, s))
428 goto fault;
429 memcpy(buf, &d8, 8);
430 break;
431 }
432 default:
433 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
434 return ES_UNSUPPORTED;
435 }
436
437 return ES_OK;
438
439 fault:
440 if (user_mode(ctxt->regs))
441 error_code |= X86_PF_USER;
442
443 ctxt->fi.vector = X86_TRAP_PF;
444 ctxt->fi.error_code = error_code;
445 ctxt->fi.cr2 = (unsigned long)src;
446
447 return ES_EXCEPTION;
448 }
449
vc_slow_virt_to_phys(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned long vaddr,phys_addr_t * paddr)450 static enum es_result vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
451 unsigned long vaddr, phys_addr_t *paddr)
452 {
453 unsigned long va = (unsigned long)vaddr;
454 unsigned int level;
455 phys_addr_t pa;
456 pgd_t *pgd;
457 pte_t *pte;
458
459 pgd = __va(read_cr3_pa());
460 pgd = &pgd[pgd_index(va)];
461 pte = lookup_address_in_pgd(pgd, va, &level);
462 if (!pte) {
463 ctxt->fi.vector = X86_TRAP_PF;
464 ctxt->fi.cr2 = vaddr;
465 ctxt->fi.error_code = 0;
466
467 if (user_mode(ctxt->regs))
468 ctxt->fi.error_code |= X86_PF_USER;
469
470 return ES_EXCEPTION;
471 }
472
473 if (WARN_ON_ONCE(pte_val(*pte) & _PAGE_ENC))
474 /* Emulated MMIO to/from encrypted memory not supported */
475 return ES_UNSUPPORTED;
476
477 pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
478 pa |= va & ~page_level_mask(level);
479
480 *paddr = pa;
481
482 return ES_OK;
483 }
484
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)485 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
486 {
487 BUG_ON(size > 4);
488
489 if (user_mode(ctxt->regs)) {
490 struct thread_struct *t = ¤t->thread;
491 struct io_bitmap *iobm = t->io_bitmap;
492 size_t idx;
493
494 if (!iobm)
495 goto fault;
496
497 for (idx = port; idx < port + size; ++idx) {
498 if (test_bit(idx, iobm->bitmap))
499 goto fault;
500 }
501 }
502
503 return ES_OK;
504
505 fault:
506 ctxt->fi.vector = X86_TRAP_GP;
507 ctxt->fi.error_code = 0;
508
509 return ES_EXCEPTION;
510 }
511
512 /* Include code shared with pre-decompression boot stage */
513 #include "sev-shared.c"
514
__sev_put_ghcb(struct ghcb_state * state)515 static noinstr void __sev_put_ghcb(struct ghcb_state *state)
516 {
517 struct sev_es_runtime_data *data;
518 struct ghcb *ghcb;
519
520 WARN_ON(!irqs_disabled());
521
522 data = this_cpu_read(runtime_data);
523 ghcb = &data->ghcb_page;
524
525 if (state->ghcb) {
526 /* Restore GHCB from Backup */
527 *ghcb = *state->ghcb;
528 data->backup_ghcb_active = false;
529 state->ghcb = NULL;
530 } else {
531 /*
532 * Invalidate the GHCB so a VMGEXIT instruction issued
533 * from userspace won't appear to be valid.
534 */
535 vc_ghcb_invalidate(ghcb);
536 data->ghcb_active = false;
537 }
538 }
539
__sev_es_nmi_complete(void)540 void noinstr __sev_es_nmi_complete(void)
541 {
542 struct ghcb_state state;
543 struct ghcb *ghcb;
544
545 ghcb = __sev_get_ghcb(&state);
546
547 vc_ghcb_invalidate(ghcb);
548 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_NMI_COMPLETE);
549 ghcb_set_sw_exit_info_1(ghcb, 0);
550 ghcb_set_sw_exit_info_2(ghcb, 0);
551
552 sev_es_wr_ghcb_msr(__pa_nodebug(ghcb));
553 VMGEXIT();
554
555 __sev_put_ghcb(&state);
556 }
557
get_jump_table_addr(void)558 static u64 get_jump_table_addr(void)
559 {
560 struct ghcb_state state;
561 unsigned long flags;
562 struct ghcb *ghcb;
563 u64 ret = 0;
564
565 local_irq_save(flags);
566
567 ghcb = __sev_get_ghcb(&state);
568
569 vc_ghcb_invalidate(ghcb);
570 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_JUMP_TABLE);
571 ghcb_set_sw_exit_info_1(ghcb, SVM_VMGEXIT_GET_AP_JUMP_TABLE);
572 ghcb_set_sw_exit_info_2(ghcb, 0);
573
574 sev_es_wr_ghcb_msr(__pa(ghcb));
575 VMGEXIT();
576
577 if (ghcb_sw_exit_info_1_is_valid(ghcb) &&
578 ghcb_sw_exit_info_2_is_valid(ghcb))
579 ret = ghcb->save.sw_exit_info_2;
580
581 __sev_put_ghcb(&state);
582
583 local_irq_restore(flags);
584
585 return ret;
586 }
587
sev_es_setup_ap_jump_table(struct real_mode_header * rmh)588 int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
589 {
590 u16 startup_cs, startup_ip;
591 phys_addr_t jump_table_pa;
592 u64 jump_table_addr;
593 u16 __iomem *jump_table;
594
595 jump_table_addr = get_jump_table_addr();
596
597 /* On UP guests there is no jump table so this is not a failure */
598 if (!jump_table_addr)
599 return 0;
600
601 /* Check if AP Jump Table is page-aligned */
602 if (jump_table_addr & ~PAGE_MASK)
603 return -EINVAL;
604
605 jump_table_pa = jump_table_addr & PAGE_MASK;
606
607 startup_cs = (u16)(rmh->trampoline_start >> 4);
608 startup_ip = (u16)(rmh->sev_es_trampoline_start -
609 rmh->trampoline_start);
610
611 jump_table = ioremap_encrypted(jump_table_pa, PAGE_SIZE);
612 if (!jump_table)
613 return -EIO;
614
615 writew(startup_ip, &jump_table[0]);
616 writew(startup_cs, &jump_table[1]);
617
618 iounmap(jump_table);
619
620 return 0;
621 }
622
623 /*
624 * This is needed by the OVMF UEFI firmware which will use whatever it finds in
625 * the GHCB MSR as its GHCB to talk to the hypervisor. So make sure the per-cpu
626 * runtime GHCBs used by the kernel are also mapped in the EFI page-table.
627 */
sev_es_efi_map_ghcbs(pgd_t * pgd)628 int __init sev_es_efi_map_ghcbs(pgd_t *pgd)
629 {
630 struct sev_es_runtime_data *data;
631 unsigned long address, pflags;
632 int cpu;
633 u64 pfn;
634
635 if (!sev_es_active())
636 return 0;
637
638 pflags = _PAGE_NX | _PAGE_RW;
639
640 for_each_possible_cpu(cpu) {
641 data = per_cpu(runtime_data, cpu);
642
643 address = __pa(&data->ghcb_page);
644 pfn = address >> PAGE_SHIFT;
645
646 if (kernel_map_pages_in_pgd(pgd, pfn, address, 1, pflags))
647 return 1;
648 }
649
650 return 0;
651 }
652
vc_handle_msr(struct ghcb * ghcb,struct es_em_ctxt * ctxt)653 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
654 {
655 struct pt_regs *regs = ctxt->regs;
656 enum es_result ret;
657 u64 exit_info_1;
658
659 /* Is it a WRMSR? */
660 exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;
661
662 ghcb_set_rcx(ghcb, regs->cx);
663 if (exit_info_1) {
664 ghcb_set_rax(ghcb, regs->ax);
665 ghcb_set_rdx(ghcb, regs->dx);
666 }
667
668 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, exit_info_1, 0);
669
670 if ((ret == ES_OK) && (!exit_info_1)) {
671 regs->ax = ghcb->save.rax;
672 regs->dx = ghcb->save.rdx;
673 }
674
675 return ret;
676 }
677
678 /*
679 * This function runs on the first #VC exception after the kernel
680 * switched to virtual addresses.
681 */
sev_es_setup_ghcb(void)682 static bool __init sev_es_setup_ghcb(void)
683 {
684 /* First make sure the hypervisor talks a supported protocol. */
685 if (!sev_es_negotiate_protocol())
686 return false;
687
688 /*
689 * Clear the boot_ghcb. The first exception comes in before the bss
690 * section is cleared.
691 */
692 memset(&boot_ghcb_page, 0, PAGE_SIZE);
693
694 /* Alright - Make the boot-ghcb public */
695 boot_ghcb = &boot_ghcb_page;
696
697 return true;
698 }
699
700 #ifdef CONFIG_HOTPLUG_CPU
sev_es_ap_hlt_loop(void)701 static void sev_es_ap_hlt_loop(void)
702 {
703 struct ghcb_state state;
704 struct ghcb *ghcb;
705
706 ghcb = __sev_get_ghcb(&state);
707
708 while (true) {
709 vc_ghcb_invalidate(ghcb);
710 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_HLT_LOOP);
711 ghcb_set_sw_exit_info_1(ghcb, 0);
712 ghcb_set_sw_exit_info_2(ghcb, 0);
713
714 sev_es_wr_ghcb_msr(__pa(ghcb));
715 VMGEXIT();
716
717 /* Wakeup signal? */
718 if (ghcb_sw_exit_info_2_is_valid(ghcb) &&
719 ghcb->save.sw_exit_info_2)
720 break;
721 }
722
723 __sev_put_ghcb(&state);
724 }
725
726 /*
727 * Play_dead handler when running under SEV-ES. This is needed because
728 * the hypervisor can't deliver an SIPI request to restart the AP.
729 * Instead the kernel has to issue a VMGEXIT to halt the VCPU until the
730 * hypervisor wakes it up again.
731 */
sev_es_play_dead(void)732 static void sev_es_play_dead(void)
733 {
734 play_dead_common();
735
736 /* IRQs now disabled */
737
738 sev_es_ap_hlt_loop();
739
740 /*
741 * If we get here, the VCPU was woken up again. Jump to CPU
742 * startup code to get it back online.
743 */
744 start_cpu0();
745 }
746 #else /* CONFIG_HOTPLUG_CPU */
747 #define sev_es_play_dead native_play_dead
748 #endif /* CONFIG_HOTPLUG_CPU */
749
750 #ifdef CONFIG_SMP
sev_es_setup_play_dead(void)751 static void __init sev_es_setup_play_dead(void)
752 {
753 smp_ops.play_dead = sev_es_play_dead;
754 }
755 #else
sev_es_setup_play_dead(void)756 static inline void sev_es_setup_play_dead(void) { }
757 #endif
758
alloc_runtime_data(int cpu)759 static void __init alloc_runtime_data(int cpu)
760 {
761 struct sev_es_runtime_data *data;
762
763 data = memblock_alloc(sizeof(*data), PAGE_SIZE);
764 if (!data)
765 panic("Can't allocate SEV-ES runtime data");
766
767 per_cpu(runtime_data, cpu) = data;
768 }
769
init_ghcb(int cpu)770 static void __init init_ghcb(int cpu)
771 {
772 struct sev_es_runtime_data *data;
773 int err;
774
775 data = per_cpu(runtime_data, cpu);
776
777 err = early_set_memory_decrypted((unsigned long)&data->ghcb_page,
778 sizeof(data->ghcb_page));
779 if (err)
780 panic("Can't map GHCBs unencrypted");
781
782 memset(&data->ghcb_page, 0, sizeof(data->ghcb_page));
783
784 data->ghcb_active = false;
785 data->backup_ghcb_active = false;
786 }
787
sev_es_init_vc_handling(void)788 void __init sev_es_init_vc_handling(void)
789 {
790 int cpu;
791
792 BUILD_BUG_ON(offsetof(struct sev_es_runtime_data, ghcb_page) % PAGE_SIZE);
793
794 if (!sev_es_active())
795 return;
796
797 if (!sev_es_check_cpu_features())
798 panic("SEV-ES CPU Features missing");
799
800 /* Enable SEV-ES special handling */
801 static_branch_enable(&sev_es_enable_key);
802
803 /* Initialize per-cpu GHCB pages */
804 for_each_possible_cpu(cpu) {
805 alloc_runtime_data(cpu);
806 init_ghcb(cpu);
807 }
808
809 sev_es_setup_play_dead();
810
811 /* Secondary CPUs use the runtime #VC handler */
812 initial_vc_handler = (unsigned long)kernel_exc_vmm_communication;
813 }
814
vc_early_forward_exception(struct es_em_ctxt * ctxt)815 static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
816 {
817 int trapnr = ctxt->fi.vector;
818
819 if (trapnr == X86_TRAP_PF)
820 native_write_cr2(ctxt->fi.cr2);
821
822 ctxt->regs->orig_ax = ctxt->fi.error_code;
823 do_early_exception(ctxt->regs, trapnr);
824 }
825
vc_insn_get_reg(struct es_em_ctxt * ctxt)826 static long *vc_insn_get_reg(struct es_em_ctxt *ctxt)
827 {
828 long *reg_array;
829 int offset;
830
831 reg_array = (long *)ctxt->regs;
832 offset = insn_get_modrm_reg_off(&ctxt->insn, ctxt->regs);
833
834 if (offset < 0)
835 return NULL;
836
837 offset /= sizeof(long);
838
839 return reg_array + offset;
840 }
841
vc_insn_get_rm(struct es_em_ctxt * ctxt)842 static long *vc_insn_get_rm(struct es_em_ctxt *ctxt)
843 {
844 long *reg_array;
845 int offset;
846
847 reg_array = (long *)ctxt->regs;
848 offset = insn_get_modrm_rm_off(&ctxt->insn, ctxt->regs);
849
850 if (offset < 0)
851 return NULL;
852
853 offset /= sizeof(long);
854
855 return reg_array + offset;
856 }
vc_do_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned int bytes,bool read)857 static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
858 unsigned int bytes, bool read)
859 {
860 u64 exit_code, exit_info_1, exit_info_2;
861 unsigned long ghcb_pa = __pa(ghcb);
862 enum es_result res;
863 phys_addr_t paddr;
864 void __user *ref;
865
866 ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
867 if (ref == (void __user *)-1L)
868 return ES_UNSUPPORTED;
869
870 exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
871
872 res = vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr);
873 if (res != ES_OK) {
874 if (res == ES_EXCEPTION && !read)
875 ctxt->fi.error_code |= X86_PF_WRITE;
876
877 return res;
878 }
879
880 exit_info_1 = paddr;
881 /* Can never be greater than 8 */
882 exit_info_2 = bytes;
883
884 ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
885
886 return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
887 }
888
vc_handle_mmio_twobyte_ops(struct ghcb * ghcb,struct es_em_ctxt * ctxt)889 static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb,
890 struct es_em_ctxt *ctxt)
891 {
892 struct insn *insn = &ctxt->insn;
893 unsigned int bytes = 0;
894 enum es_result ret;
895 int sign_byte;
896 long *reg_data;
897
898 switch (insn->opcode.bytes[1]) {
899 /* MMIO Read w/ zero-extension */
900 case 0xb6:
901 bytes = 1;
902 fallthrough;
903 case 0xb7:
904 if (!bytes)
905 bytes = 2;
906
907 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
908 if (ret)
909 break;
910
911 /* Zero extend based on operand size */
912 reg_data = vc_insn_get_reg(ctxt);
913 if (!reg_data)
914 return ES_DECODE_FAILED;
915
916 memset(reg_data, 0, insn->opnd_bytes);
917
918 memcpy(reg_data, ghcb->shared_buffer, bytes);
919 break;
920
921 /* MMIO Read w/ sign-extension */
922 case 0xbe:
923 bytes = 1;
924 fallthrough;
925 case 0xbf:
926 if (!bytes)
927 bytes = 2;
928
929 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
930 if (ret)
931 break;
932
933 /* Sign extend based on operand size */
934 reg_data = vc_insn_get_reg(ctxt);
935 if (!reg_data)
936 return ES_DECODE_FAILED;
937
938 if (bytes == 1) {
939 u8 *val = (u8 *)ghcb->shared_buffer;
940
941 sign_byte = (*val & 0x80) ? 0xff : 0x00;
942 } else {
943 u16 *val = (u16 *)ghcb->shared_buffer;
944
945 sign_byte = (*val & 0x8000) ? 0xff : 0x00;
946 }
947 memset(reg_data, sign_byte, insn->opnd_bytes);
948
949 memcpy(reg_data, ghcb->shared_buffer, bytes);
950 break;
951
952 default:
953 ret = ES_UNSUPPORTED;
954 }
955
956 return ret;
957 }
958
959 /*
960 * The MOVS instruction has two memory operands, which raises the
961 * problem that it is not known whether the access to the source or the
962 * destination caused the #VC exception (and hence whether an MMIO read
963 * or write operation needs to be emulated).
964 *
965 * Instead of playing games with walking page-tables and trying to guess
966 * whether the source or destination is an MMIO range, split the move
967 * into two operations, a read and a write with only one memory operand.
968 * This will cause a nested #VC exception on the MMIO address which can
969 * then be handled.
970 *
971 * This implementation has the benefit that it also supports MOVS where
972 * source _and_ destination are MMIO regions.
973 *
974 * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a
975 * rare operation. If it turns out to be a performance problem the split
976 * operations can be moved to memcpy_fromio() and memcpy_toio().
977 */
vc_handle_mmio_movs(struct es_em_ctxt * ctxt,unsigned int bytes)978 static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,
979 unsigned int bytes)
980 {
981 unsigned long ds_base, es_base;
982 unsigned char *src, *dst;
983 unsigned char buffer[8];
984 enum es_result ret;
985 bool rep;
986 int off;
987
988 ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS);
989 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
990
991 if (ds_base == -1L || es_base == -1L) {
992 ctxt->fi.vector = X86_TRAP_GP;
993 ctxt->fi.error_code = 0;
994 return ES_EXCEPTION;
995 }
996
997 src = ds_base + (unsigned char *)ctxt->regs->si;
998 dst = es_base + (unsigned char *)ctxt->regs->di;
999
1000 ret = vc_read_mem(ctxt, src, buffer, bytes);
1001 if (ret != ES_OK)
1002 return ret;
1003
1004 ret = vc_write_mem(ctxt, dst, buffer, bytes);
1005 if (ret != ES_OK)
1006 return ret;
1007
1008 if (ctxt->regs->flags & X86_EFLAGS_DF)
1009 off = -bytes;
1010 else
1011 off = bytes;
1012
1013 ctxt->regs->si += off;
1014 ctxt->regs->di += off;
1015
1016 rep = insn_has_rep_prefix(&ctxt->insn);
1017 if (rep)
1018 ctxt->regs->cx -= 1;
1019
1020 if (!rep || ctxt->regs->cx == 0)
1021 return ES_OK;
1022 else
1023 return ES_RETRY;
1024 }
1025
vc_handle_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1026 static enum es_result vc_handle_mmio(struct ghcb *ghcb,
1027 struct es_em_ctxt *ctxt)
1028 {
1029 struct insn *insn = &ctxt->insn;
1030 unsigned int bytes = 0;
1031 enum es_result ret;
1032 long *reg_data;
1033
1034 if (user_mode(ctxt->regs))
1035 return ES_UNSUPPORTED;
1036
1037 switch (insn->opcode.bytes[0]) {
1038 /* MMIO Write */
1039 case 0x88:
1040 bytes = 1;
1041 fallthrough;
1042 case 0x89:
1043 if (!bytes)
1044 bytes = insn->opnd_bytes;
1045
1046 reg_data = vc_insn_get_reg(ctxt);
1047 if (!reg_data)
1048 return ES_DECODE_FAILED;
1049
1050 memcpy(ghcb->shared_buffer, reg_data, bytes);
1051
1052 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1053 break;
1054
1055 case 0xc6:
1056 bytes = 1;
1057 fallthrough;
1058 case 0xc7:
1059 if (!bytes)
1060 bytes = insn->opnd_bytes;
1061
1062 memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
1063
1064 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
1065 break;
1066
1067 /* MMIO Read */
1068 case 0x8a:
1069 bytes = 1;
1070 fallthrough;
1071 case 0x8b:
1072 if (!bytes)
1073 bytes = insn->opnd_bytes;
1074
1075 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
1076 if (ret)
1077 break;
1078
1079 reg_data = vc_insn_get_reg(ctxt);
1080 if (!reg_data)
1081 return ES_DECODE_FAILED;
1082
1083 /* Zero-extend for 32-bit operation */
1084 if (bytes == 4)
1085 *reg_data = 0;
1086
1087 memcpy(reg_data, ghcb->shared_buffer, bytes);
1088 break;
1089
1090 /* MOVS instruction */
1091 case 0xa4:
1092 bytes = 1;
1093 fallthrough;
1094 case 0xa5:
1095 if (!bytes)
1096 bytes = insn->opnd_bytes;
1097
1098 ret = vc_handle_mmio_movs(ctxt, bytes);
1099 break;
1100 /* Two-Byte Opcodes */
1101 case 0x0f:
1102 ret = vc_handle_mmio_twobyte_ops(ghcb, ctxt);
1103 break;
1104 default:
1105 ret = ES_UNSUPPORTED;
1106 }
1107
1108 return ret;
1109 }
1110
vc_handle_dr7_write(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1111 static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
1112 struct es_em_ctxt *ctxt)
1113 {
1114 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1115 long val, *reg = vc_insn_get_rm(ctxt);
1116 enum es_result ret;
1117
1118 if (!reg)
1119 return ES_DECODE_FAILED;
1120
1121 val = *reg;
1122
1123 /* Upper 32 bits must be written as zeroes */
1124 if (val >> 32) {
1125 ctxt->fi.vector = X86_TRAP_GP;
1126 ctxt->fi.error_code = 0;
1127 return ES_EXCEPTION;
1128 }
1129
1130 /* Clear out other reserved bits and set bit 10 */
1131 val = (val & 0xffff23ffL) | BIT(10);
1132
1133 /* Early non-zero writes to DR7 are not supported */
1134 if (!data && (val & ~DR7_RESET_VALUE))
1135 return ES_UNSUPPORTED;
1136
1137 /* Using a value of 0 for ExitInfo1 means RAX holds the value */
1138 ghcb_set_rax(ghcb, val);
1139 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
1140 if (ret != ES_OK)
1141 return ret;
1142
1143 if (data)
1144 data->dr7 = val;
1145
1146 return ES_OK;
1147 }
1148
vc_handle_dr7_read(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1149 static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
1150 struct es_em_ctxt *ctxt)
1151 {
1152 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
1153 long *reg = vc_insn_get_rm(ctxt);
1154
1155 if (!reg)
1156 return ES_DECODE_FAILED;
1157
1158 if (data)
1159 *reg = data->dr7;
1160 else
1161 *reg = DR7_RESET_VALUE;
1162
1163 return ES_OK;
1164 }
1165
vc_handle_wbinvd(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1166 static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
1167 struct es_em_ctxt *ctxt)
1168 {
1169 return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
1170 }
1171
vc_handle_rdpmc(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1172 static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
1173 {
1174 enum es_result ret;
1175
1176 ghcb_set_rcx(ghcb, ctxt->regs->cx);
1177
1178 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
1179 if (ret != ES_OK)
1180 return ret;
1181
1182 if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
1183 return ES_VMM_ERROR;
1184
1185 ctxt->regs->ax = ghcb->save.rax;
1186 ctxt->regs->dx = ghcb->save.rdx;
1187
1188 return ES_OK;
1189 }
1190
vc_handle_monitor(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1191 static enum es_result vc_handle_monitor(struct ghcb *ghcb,
1192 struct es_em_ctxt *ctxt)
1193 {
1194 /*
1195 * Treat it as a NOP and do not leak a physical address to the
1196 * hypervisor.
1197 */
1198 return ES_OK;
1199 }
1200
vc_handle_mwait(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1201 static enum es_result vc_handle_mwait(struct ghcb *ghcb,
1202 struct es_em_ctxt *ctxt)
1203 {
1204 /* Treat the same as MONITOR/MONITORX */
1205 return ES_OK;
1206 }
1207
vc_handle_vmmcall(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1208 static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
1209 struct es_em_ctxt *ctxt)
1210 {
1211 enum es_result ret;
1212
1213 ghcb_set_rax(ghcb, ctxt->regs->ax);
1214 ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
1215
1216 if (x86_platform.hyper.sev_es_hcall_prepare)
1217 x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
1218
1219 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
1220 if (ret != ES_OK)
1221 return ret;
1222
1223 if (!ghcb_rax_is_valid(ghcb))
1224 return ES_VMM_ERROR;
1225
1226 ctxt->regs->ax = ghcb->save.rax;
1227
1228 /*
1229 * Call sev_es_hcall_finish() after regs->ax is already set.
1230 * This allows the hypervisor handler to overwrite it again if
1231 * necessary.
1232 */
1233 if (x86_platform.hyper.sev_es_hcall_finish &&
1234 !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
1235 return ES_VMM_ERROR;
1236
1237 return ES_OK;
1238 }
1239
vc_handle_trap_ac(struct ghcb * ghcb,struct es_em_ctxt * ctxt)1240 static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
1241 struct es_em_ctxt *ctxt)
1242 {
1243 /*
1244 * Calling ecx_alignment_check() directly does not work, because it
1245 * enables IRQs and the GHCB is active. Forward the exception and call
1246 * it later from vc_forward_exception().
1247 */
1248 ctxt->fi.vector = X86_TRAP_AC;
1249 ctxt->fi.error_code = 0;
1250 return ES_EXCEPTION;
1251 }
1252
vc_handle_exitcode(struct es_em_ctxt * ctxt,struct ghcb * ghcb,unsigned long exit_code)1253 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
1254 struct ghcb *ghcb,
1255 unsigned long exit_code)
1256 {
1257 enum es_result result;
1258
1259 switch (exit_code) {
1260 case SVM_EXIT_READ_DR7:
1261 result = vc_handle_dr7_read(ghcb, ctxt);
1262 break;
1263 case SVM_EXIT_WRITE_DR7:
1264 result = vc_handle_dr7_write(ghcb, ctxt);
1265 break;
1266 case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
1267 result = vc_handle_trap_ac(ghcb, ctxt);
1268 break;
1269 case SVM_EXIT_RDTSC:
1270 case SVM_EXIT_RDTSCP:
1271 result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
1272 break;
1273 case SVM_EXIT_RDPMC:
1274 result = vc_handle_rdpmc(ghcb, ctxt);
1275 break;
1276 case SVM_EXIT_INVD:
1277 pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
1278 result = ES_UNSUPPORTED;
1279 break;
1280 case SVM_EXIT_CPUID:
1281 result = vc_handle_cpuid(ghcb, ctxt);
1282 break;
1283 case SVM_EXIT_IOIO:
1284 result = vc_handle_ioio(ghcb, ctxt);
1285 break;
1286 case SVM_EXIT_MSR:
1287 result = vc_handle_msr(ghcb, ctxt);
1288 break;
1289 case SVM_EXIT_VMMCALL:
1290 result = vc_handle_vmmcall(ghcb, ctxt);
1291 break;
1292 case SVM_EXIT_WBINVD:
1293 result = vc_handle_wbinvd(ghcb, ctxt);
1294 break;
1295 case SVM_EXIT_MONITOR:
1296 result = vc_handle_monitor(ghcb, ctxt);
1297 break;
1298 case SVM_EXIT_MWAIT:
1299 result = vc_handle_mwait(ghcb, ctxt);
1300 break;
1301 case SVM_EXIT_NPF:
1302 result = vc_handle_mmio(ghcb, ctxt);
1303 break;
1304 default:
1305 /*
1306 * Unexpected #VC exception
1307 */
1308 result = ES_UNSUPPORTED;
1309 }
1310
1311 return result;
1312 }
1313
vc_forward_exception(struct es_em_ctxt * ctxt)1314 static __always_inline void vc_forward_exception(struct es_em_ctxt *ctxt)
1315 {
1316 long error_code = ctxt->fi.error_code;
1317 int trapnr = ctxt->fi.vector;
1318
1319 ctxt->regs->orig_ax = ctxt->fi.error_code;
1320
1321 switch (trapnr) {
1322 case X86_TRAP_GP:
1323 exc_general_protection(ctxt->regs, error_code);
1324 break;
1325 case X86_TRAP_UD:
1326 exc_invalid_op(ctxt->regs);
1327 break;
1328 case X86_TRAP_PF:
1329 write_cr2(ctxt->fi.cr2);
1330 exc_page_fault(ctxt->regs, error_code);
1331 break;
1332 case X86_TRAP_AC:
1333 exc_alignment_check(ctxt->regs, error_code);
1334 break;
1335 default:
1336 pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
1337 BUG();
1338 }
1339 }
1340
on_vc_fallback_stack(struct pt_regs * regs)1341 static __always_inline bool on_vc_fallback_stack(struct pt_regs *regs)
1342 {
1343 unsigned long sp = (unsigned long)regs;
1344
1345 return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
1346 }
1347
vc_raw_handle_exception(struct pt_regs * regs,unsigned long error_code)1348 static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
1349 {
1350 struct ghcb_state state;
1351 struct es_em_ctxt ctxt;
1352 enum es_result result;
1353 struct ghcb *ghcb;
1354 bool ret = true;
1355
1356 ghcb = __sev_get_ghcb(&state);
1357
1358 vc_ghcb_invalidate(ghcb);
1359 result = vc_init_em_ctxt(&ctxt, regs, error_code);
1360
1361 if (result == ES_OK)
1362 result = vc_handle_exitcode(&ctxt, ghcb, error_code);
1363
1364 __sev_put_ghcb(&state);
1365
1366 /* Done - now check the result */
1367 switch (result) {
1368 case ES_OK:
1369 vc_finish_insn(&ctxt);
1370 break;
1371 case ES_UNSUPPORTED:
1372 pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
1373 error_code, regs->ip);
1374 ret = false;
1375 break;
1376 case ES_VMM_ERROR:
1377 pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
1378 error_code, regs->ip);
1379 ret = false;
1380 break;
1381 case ES_DECODE_FAILED:
1382 pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
1383 error_code, regs->ip);
1384 ret = false;
1385 break;
1386 case ES_EXCEPTION:
1387 vc_forward_exception(&ctxt);
1388 break;
1389 case ES_RETRY:
1390 /* Nothing to do */
1391 break;
1392 default:
1393 pr_emerg("Unknown result in %s():%d\n", __func__, result);
1394 /*
1395 * Emulating the instruction which caused the #VC exception
1396 * failed - can't continue so print debug information
1397 */
1398 BUG();
1399 }
1400
1401 return ret;
1402 }
1403
vc_is_db(unsigned long error_code)1404 static __always_inline bool vc_is_db(unsigned long error_code)
1405 {
1406 return error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB;
1407 }
1408
1409 /*
1410 * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode
1411 * and will panic when an error happens.
1412 */
DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)1413 DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
1414 {
1415 irqentry_state_t irq_state;
1416
1417 /*
1418 * With the current implementation it is always possible to switch to a
1419 * safe stack because #VC exceptions only happen at known places, like
1420 * intercepted instructions or accesses to MMIO areas/IO ports. They can
1421 * also happen with code instrumentation when the hypervisor intercepts
1422 * #DB, but the critical paths are forbidden to be instrumented, so #DB
1423 * exceptions currently also only happen in safe places.
1424 *
1425 * But keep this here in case the noinstr annotations are violated due
1426 * to bug elsewhere.
1427 */
1428 if (unlikely(on_vc_fallback_stack(regs))) {
1429 instrumentation_begin();
1430 panic("Can't handle #VC exception from unsupported context\n");
1431 instrumentation_end();
1432 }
1433
1434 /*
1435 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
1436 */
1437 if (vc_is_db(error_code)) {
1438 exc_debug(regs);
1439 return;
1440 }
1441
1442 irq_state = irqentry_nmi_enter(regs);
1443
1444 instrumentation_begin();
1445
1446 if (!vc_raw_handle_exception(regs, error_code)) {
1447 /* Show some debug info */
1448 show_regs(regs);
1449
1450 /* Ask hypervisor to sev_es_terminate */
1451 sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST);
1452
1453 /* If that fails and we get here - just panic */
1454 panic("Returned from Terminate-Request to Hypervisor\n");
1455 }
1456
1457 instrumentation_end();
1458 irqentry_nmi_exit(regs, irq_state);
1459 }
1460
1461 /*
1462 * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode
1463 * and will kill the current task with SIGBUS when an error happens.
1464 */
DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)1465 DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
1466 {
1467 /*
1468 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
1469 */
1470 if (vc_is_db(error_code)) {
1471 noist_exc_debug(regs);
1472 return;
1473 }
1474
1475 irqentry_enter_from_user_mode(regs);
1476 instrumentation_begin();
1477
1478 if (!vc_raw_handle_exception(regs, error_code)) {
1479 /*
1480 * Do not kill the machine if user-space triggered the
1481 * exception. Send SIGBUS instead and let user-space deal with
1482 * it.
1483 */
1484 force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
1485 }
1486
1487 instrumentation_end();
1488 irqentry_exit_to_user_mode(regs);
1489 }
1490
handle_vc_boot_ghcb(struct pt_regs * regs)1491 bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
1492 {
1493 unsigned long exit_code = regs->orig_ax;
1494 struct es_em_ctxt ctxt;
1495 enum es_result result;
1496
1497 /* Do initial setup or terminate the guest */
1498 if (unlikely(boot_ghcb == NULL && !sev_es_setup_ghcb()))
1499 sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST);
1500
1501 vc_ghcb_invalidate(boot_ghcb);
1502
1503 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
1504 if (result == ES_OK)
1505 result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
1506
1507 /* Done - now check the result */
1508 switch (result) {
1509 case ES_OK:
1510 vc_finish_insn(&ctxt);
1511 break;
1512 case ES_UNSUPPORTED:
1513 early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
1514 exit_code, regs->ip);
1515 goto fail;
1516 case ES_VMM_ERROR:
1517 early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
1518 exit_code, regs->ip);
1519 goto fail;
1520 case ES_DECODE_FAILED:
1521 early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
1522 exit_code, regs->ip);
1523 goto fail;
1524 case ES_EXCEPTION:
1525 vc_early_forward_exception(&ctxt);
1526 break;
1527 case ES_RETRY:
1528 /* Nothing to do */
1529 break;
1530 default:
1531 BUG();
1532 }
1533
1534 return true;
1535
1536 fail:
1537 show_regs(regs);
1538
1539 while (true)
1540 halt();
1541 }
1542