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