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