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