1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AMD Memory Encryption Support
4 *
5 * Copyright (C) 2019 SUSE
6 *
7 * Author: Joerg Roedel <jroedel@suse.de>
8 */
9
10 #define pr_fmt(fmt) "SEV: " fmt
11
12 #include <linux/sched/debug.h> /* For show_regs() */
13 #include <linux/cc_platform.h>
14 #include <linux/printk.h>
15 #include <linux/mm_types.h>
16 #include <linux/kernel.h>
17 #include <linux/mm.h>
18 #include <linux/io.h>
19 #include <linux/psp-sev.h>
20 #include <linux/efi.h>
21 #include <uapi/linux/sev-guest.h>
22
23 #include <asm/init.h>
24 #include <asm/stacktrace.h>
25 #include <asm/sev.h>
26 #include <asm/sev-internal.h>
27 #include <asm/insn-eval.h>
28 #include <asm/fpu/xcr.h>
29 #include <asm/processor.h>
30 #include <asm/setup.h>
31 #include <asm/traps.h>
32 #include <asm/svm.h>
33 #include <asm/smp.h>
34 #include <asm/cpu.h>
35 #include <asm/apic.h>
36 #include <asm/cpuid/api.h>
37
vc_slow_virt_to_phys(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned long vaddr,phys_addr_t * paddr)38 static enum es_result vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
39 unsigned long vaddr, phys_addr_t *paddr)
40 {
41 unsigned long va = (unsigned long)vaddr;
42 unsigned int level;
43 phys_addr_t pa;
44 pgd_t *pgd;
45 pte_t *pte;
46
47 pgd = __va(read_cr3_pa());
48 pgd = &pgd[pgd_index(va)];
49 pte = lookup_address_in_pgd(pgd, va, &level);
50 if (!pte) {
51 ctxt->fi.vector = X86_TRAP_PF;
52 ctxt->fi.cr2 = vaddr;
53 ctxt->fi.error_code = 0;
54
55 if (user_mode(ctxt->regs))
56 ctxt->fi.error_code |= X86_PF_USER;
57
58 return ES_EXCEPTION;
59 }
60
61 if (WARN_ON_ONCE(pte_val(*pte) & _PAGE_ENC))
62 /* Emulated MMIO to/from encrypted memory not supported */
63 return ES_UNSUPPORTED;
64
65 pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
66 pa |= va & ~page_level_mask(level);
67
68 *paddr = pa;
69
70 return ES_OK;
71 }
72
vc_ioio_check(struct es_em_ctxt * ctxt,u16 port,size_t size)73 static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
74 {
75 BUG_ON(size > 4);
76
77 if (user_mode(ctxt->regs)) {
78 struct thread_struct *t = ¤t->thread;
79 struct io_bitmap *iobm = t->io_bitmap;
80 size_t idx;
81
82 if (!iobm)
83 goto fault;
84
85 for (idx = port; idx < port + size; ++idx) {
86 if (test_bit(idx, iobm->bitmap))
87 goto fault;
88 }
89 }
90
91 return ES_OK;
92
93 fault:
94 ctxt->fi.vector = X86_TRAP_GP;
95 ctxt->fi.error_code = 0;
96
97 return ES_EXCEPTION;
98 }
99
vc_forward_exception(struct es_em_ctxt * ctxt)100 void vc_forward_exception(struct es_em_ctxt *ctxt)
101 {
102 long error_code = ctxt->fi.error_code;
103 int trapnr = ctxt->fi.vector;
104
105 ctxt->regs->orig_ax = ctxt->fi.error_code;
106
107 switch (trapnr) {
108 case X86_TRAP_GP:
109 exc_general_protection(ctxt->regs, error_code);
110 break;
111 case X86_TRAP_UD:
112 exc_invalid_op(ctxt->regs);
113 break;
114 case X86_TRAP_PF:
115 write_cr2(ctxt->fi.cr2);
116 exc_page_fault(ctxt->regs, error_code);
117 break;
118 case X86_TRAP_AC:
119 exc_alignment_check(ctxt->regs, error_code);
120 break;
121 default:
122 pr_emerg("Unsupported exception in #VC instruction emulation - can't continue\n");
123 BUG();
124 }
125 }
126
vc_fetch_insn_kernel(struct es_em_ctxt * ctxt,unsigned char * buffer)127 static int vc_fetch_insn_kernel(struct es_em_ctxt *ctxt,
128 unsigned char *buffer)
129 {
130 return copy_from_kernel_nofault(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
131 }
132
__vc_decode_user_insn(struct es_em_ctxt * ctxt)133 static enum es_result __vc_decode_user_insn(struct es_em_ctxt *ctxt)
134 {
135 char buffer[MAX_INSN_SIZE];
136 int insn_bytes;
137
138 insn_bytes = insn_fetch_from_user_inatomic(ctxt->regs, buffer);
139 if (insn_bytes == 0) {
140 /* Nothing could be copied */
141 ctxt->fi.vector = X86_TRAP_PF;
142 ctxt->fi.error_code = X86_PF_INSTR | X86_PF_USER;
143 ctxt->fi.cr2 = ctxt->regs->ip;
144 return ES_EXCEPTION;
145 } else if (insn_bytes == -EINVAL) {
146 /* Effective RIP could not be calculated */
147 ctxt->fi.vector = X86_TRAP_GP;
148 ctxt->fi.error_code = 0;
149 ctxt->fi.cr2 = 0;
150 return ES_EXCEPTION;
151 }
152
153 if (!insn_decode_from_regs(&ctxt->insn, ctxt->regs, buffer, insn_bytes))
154 return ES_DECODE_FAILED;
155
156 if (ctxt->insn.immediate.got)
157 return ES_OK;
158 else
159 return ES_DECODE_FAILED;
160 }
161
__vc_decode_kern_insn(struct es_em_ctxt * ctxt)162 static enum es_result __vc_decode_kern_insn(struct es_em_ctxt *ctxt)
163 {
164 char buffer[MAX_INSN_SIZE];
165 int res, ret;
166
167 res = vc_fetch_insn_kernel(ctxt, buffer);
168 if (res) {
169 ctxt->fi.vector = X86_TRAP_PF;
170 ctxt->fi.error_code = X86_PF_INSTR;
171 ctxt->fi.cr2 = ctxt->regs->ip;
172 return ES_EXCEPTION;
173 }
174
175 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
176 if (ret < 0)
177 return ES_DECODE_FAILED;
178 else
179 return ES_OK;
180 }
181
182 /*
183 * User instruction decoding is also required for the EFI runtime. Even though
184 * the EFI runtime is running in kernel mode, it uses special EFI virtual
185 * address mappings that require the use of efi_mm to properly address and
186 * decode.
187 */
vc_decode_insn(struct es_em_ctxt * ctxt)188 static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
189 {
190 if (user_mode(ctxt->regs) || mm_is_efi(current->active_mm))
191 return __vc_decode_user_insn(ctxt);
192 else
193 return __vc_decode_kern_insn(ctxt);
194 }
195
vc_write_mem(struct es_em_ctxt * ctxt,char * dst,char * buf,size_t size)196 static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
197 char *dst, char *buf, size_t size)
198 {
199 unsigned long error_code = X86_PF_PROT | X86_PF_WRITE;
200
201 /*
202 * This function uses __put_user() independent of whether kernel or user
203 * memory is accessed. This works fine because __put_user() does no
204 * sanity checks of the pointer being accessed. All that it does is
205 * to report when the access failed.
206 *
207 * Also, this function runs in atomic context, so __put_user() is not
208 * allowed to sleep. The page-fault handler detects that it is running
209 * in atomic context and will not try to take mmap_sem and handle the
210 * fault, so additional pagefault_enable()/disable() calls are not
211 * needed.
212 *
213 * The access can't be done via copy_to_user() here because
214 * vc_write_mem() must not use string instructions to access unsafe
215 * memory. The reason is that MOVS is emulated by the #VC handler by
216 * splitting the move up into a read and a write and taking a nested #VC
217 * exception on whatever of them is the MMIO access. Using string
218 * instructions here would cause infinite nesting.
219 */
220 switch (size) {
221 case 1: {
222 u8 d1;
223 u8 __user *target = (u8 __user *)dst;
224
225 memcpy(&d1, buf, 1);
226 if (__put_user(d1, target))
227 goto fault;
228 break;
229 }
230 case 2: {
231 u16 d2;
232 u16 __user *target = (u16 __user *)dst;
233
234 memcpy(&d2, buf, 2);
235 if (__put_user(d2, target))
236 goto fault;
237 break;
238 }
239 case 4: {
240 u32 d4;
241 u32 __user *target = (u32 __user *)dst;
242
243 memcpy(&d4, buf, 4);
244 if (__put_user(d4, target))
245 goto fault;
246 break;
247 }
248 case 8: {
249 u64 d8;
250 u64 __user *target = (u64 __user *)dst;
251
252 memcpy(&d8, buf, 8);
253 if (__put_user(d8, target))
254 goto fault;
255 break;
256 }
257 default:
258 WARN_ONCE(1, "%s: Invalid size: %zu\n", __func__, size);
259 return ES_UNSUPPORTED;
260 }
261
262 return ES_OK;
263
264 fault:
265 if (user_mode(ctxt->regs))
266 error_code |= X86_PF_USER;
267
268 ctxt->fi.vector = X86_TRAP_PF;
269 ctxt->fi.error_code = error_code;
270 ctxt->fi.cr2 = (unsigned long)dst;
271
272 return ES_EXCEPTION;
273 }
274
vc_read_mem(struct es_em_ctxt * ctxt,char * src,char * buf,size_t size)275 static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
276 char *src, char *buf, size_t size)
277 {
278 unsigned long error_code = X86_PF_PROT;
279
280 /*
281 * This function uses __get_user() independent of whether kernel or user
282 * memory is accessed. This works fine because __get_user() does no
283 * sanity checks of the pointer being accessed. All that it does is
284 * to report when the access failed.
285 *
286 * Also, this function runs in atomic context, so __get_user() is not
287 * allowed to sleep. The page-fault handler detects that it is running
288 * in atomic context and will not try to take mmap_sem and handle the
289 * fault, so additional pagefault_enable()/disable() calls are not
290 * needed.
291 *
292 * The access can't be done via copy_from_user() here because
293 * vc_read_mem() must not use string instructions to access unsafe
294 * memory. The reason is that MOVS is emulated by the #VC handler by
295 * splitting the move up into a read and a write and taking a nested #VC
296 * exception on whatever of them is the MMIO access. Using string
297 * instructions here would cause infinite nesting.
298 */
299 switch (size) {
300 case 1: {
301 u8 d1;
302 u8 __user *s = (u8 __user *)src;
303
304 if (__get_user(d1, s))
305 goto fault;
306 memcpy(buf, &d1, 1);
307 break;
308 }
309 case 2: {
310 u16 d2;
311 u16 __user *s = (u16 __user *)src;
312
313 if (__get_user(d2, s))
314 goto fault;
315 memcpy(buf, &d2, 2);
316 break;
317 }
318 case 4: {
319 u32 d4;
320 u32 __user *s = (u32 __user *)src;
321
322 if (__get_user(d4, s))
323 goto fault;
324 memcpy(buf, &d4, 4);
325 break;
326 }
327 case 8: {
328 u64 d8;
329 u64 __user *s = (u64 __user *)src;
330 if (__get_user(d8, s))
331 goto fault;
332 memcpy(buf, &d8, 8);
333 break;
334 }
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)src;
349
350 return ES_EXCEPTION;
351 }
352
353 #define sev_printk(fmt, ...) printk(fmt, ##__VA_ARGS__)
354 #define error(v)
355 #define has_cpuflag(f) boot_cpu_has(f)
356
357 #include "vc-shared.c"
358
359 /* Writes to the SVSM CAA MSR are ignored */
__vc_handle_msr_caa(struct pt_regs * regs,bool write)360 static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
361 {
362 if (write)
363 return ES_OK;
364
365 regs->ax = lower_32_bits(this_cpu_read(svsm_caa_pa));
366 regs->dx = upper_32_bits(this_cpu_read(svsm_caa_pa));
367
368 return ES_OK;
369 }
370
371 /*
372 * TSC related accesses should not exit to the hypervisor when a guest is
373 * executing with Secure TSC enabled, so special handling is required for
374 * accesses of MSR_IA32_TSC and MSR_AMD64_GUEST_TSC_FREQ.
375 */
__vc_handle_secure_tsc_msrs(struct es_em_ctxt * ctxt,bool write)376 static enum es_result __vc_handle_secure_tsc_msrs(struct es_em_ctxt *ctxt, bool write)
377 {
378 struct pt_regs *regs = ctxt->regs;
379 u64 tsc;
380
381 /*
382 * Writing to MSR_IA32_TSC can cause subsequent reads of the TSC to
383 * return undefined values, and GUEST_TSC_FREQ is read-only. Generate
384 * a #GP on all writes.
385 */
386 if (write) {
387 ctxt->fi.vector = X86_TRAP_GP;
388 ctxt->fi.error_code = 0;
389 return ES_EXCEPTION;
390 }
391
392 /*
393 * GUEST_TSC_FREQ read should not be intercepted when Secure TSC is
394 * enabled. Terminate the guest if a read is attempted.
395 */
396 if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ)
397 return ES_VMM_ERROR;
398
399 /* Reads of MSR_IA32_TSC should return the current TSC value. */
400 tsc = rdtsc_ordered();
401 regs->ax = lower_32_bits(tsc);
402 regs->dx = upper_32_bits(tsc);
403
404 return ES_OK;
405 }
406
sev_es_ghcb_handle_msr(struct ghcb * ghcb,struct es_em_ctxt * ctxt,bool write)407 enum es_result sev_es_ghcb_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt, bool write)
408 {
409 struct pt_regs *regs = ctxt->regs;
410 enum es_result ret;
411
412 switch (regs->cx) {
413 case MSR_SVSM_CAA:
414 return __vc_handle_msr_caa(regs, write);
415 case MSR_IA32_TSC:
416 case MSR_AMD64_GUEST_TSC_FREQ:
417 if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
418 return __vc_handle_secure_tsc_msrs(ctxt, write);
419 break;
420 case MSR_AMD64_SAVIC_CONTROL:
421 /*
422 * AMD64_SAVIC_CONTROL should not be intercepted when
423 * Secure AVIC is enabled. Terminate the Secure AVIC guest
424 * if the interception is enabled.
425 */
426 if (cc_platform_has(CC_ATTR_SNP_SECURE_AVIC))
427 return ES_VMM_ERROR;
428 break;
429 default:
430 break;
431 }
432
433 ghcb_set_rcx(ghcb, regs->cx);
434 if (write) {
435 ghcb_set_rax(ghcb, regs->ax);
436 ghcb_set_rdx(ghcb, regs->dx);
437 }
438
439 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, write, 0);
440
441 if ((ret == ES_OK) && !write) {
442 regs->ax = ghcb->save.rax;
443 regs->dx = ghcb->save.rdx;
444 }
445
446 return ret;
447 }
448
vc_handle_msr(struct ghcb * ghcb,struct es_em_ctxt * ctxt)449 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
450 {
451 return sev_es_ghcb_handle_msr(ghcb, ctxt, ctxt->insn.opcode.bytes[1] == 0x30);
452 }
453
vc_early_forward_exception(struct es_em_ctxt * ctxt)454 static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
455 {
456 int trapnr = ctxt->fi.vector;
457
458 if (trapnr == X86_TRAP_PF)
459 native_write_cr2(ctxt->fi.cr2);
460
461 ctxt->regs->orig_ax = ctxt->fi.error_code;
462 do_early_exception(ctxt->regs, trapnr);
463 }
464
vc_insn_get_rm(struct es_em_ctxt * ctxt)465 static long *vc_insn_get_rm(struct es_em_ctxt *ctxt)
466 {
467 long *reg_array;
468 int offset;
469
470 reg_array = (long *)ctxt->regs;
471 offset = insn_get_modrm_rm_off(&ctxt->insn, ctxt->regs);
472
473 if (offset < 0)
474 return NULL;
475
476 offset /= sizeof(long);
477
478 return reg_array + offset;
479 }
vc_do_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt,unsigned int bytes,bool read)480 static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
481 unsigned int bytes, bool read)
482 {
483 u64 exit_code, exit_info_1, exit_info_2;
484 unsigned long ghcb_pa = __pa(ghcb);
485 enum es_result res;
486 phys_addr_t paddr;
487 void __user *ref;
488
489 ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
490 if (ref == (void __user *)-1L)
491 return ES_UNSUPPORTED;
492
493 exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
494
495 res = vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr);
496 if (res != ES_OK) {
497 if (res == ES_EXCEPTION && !read)
498 ctxt->fi.error_code |= X86_PF_WRITE;
499
500 return res;
501 }
502
503 exit_info_1 = paddr;
504 /* Can never be greater than 8 */
505 exit_info_2 = bytes;
506
507 ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
508
509 return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
510 }
511
512 /*
513 * The MOVS instruction has two memory operands, which raises the
514 * problem that it is not known whether the access to the source or the
515 * destination caused the #VC exception (and hence whether an MMIO read
516 * or write operation needs to be emulated).
517 *
518 * Instead of playing games with walking page-tables and trying to guess
519 * whether the source or destination is an MMIO range, split the move
520 * into two operations, a read and a write with only one memory operand.
521 * This will cause a nested #VC exception on the MMIO address which can
522 * then be handled.
523 *
524 * This implementation has the benefit that it also supports MOVS where
525 * source _and_ destination are MMIO regions.
526 *
527 * It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a
528 * rare operation. If it turns out to be a performance problem the split
529 * operations can be moved to memcpy_fromio() and memcpy_toio().
530 */
vc_handle_mmio_movs(struct es_em_ctxt * ctxt,unsigned int bytes)531 static enum es_result vc_handle_mmio_movs(struct es_em_ctxt *ctxt,
532 unsigned int bytes)
533 {
534 unsigned long ds_base, es_base;
535 unsigned char *src, *dst;
536 unsigned char buffer[8];
537 enum es_result ret;
538 bool rep;
539 int off;
540
541 ds_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_DS);
542 es_base = insn_get_seg_base(ctxt->regs, INAT_SEG_REG_ES);
543
544 if (ds_base == -1L || es_base == -1L) {
545 ctxt->fi.vector = X86_TRAP_GP;
546 ctxt->fi.error_code = 0;
547 return ES_EXCEPTION;
548 }
549
550 src = ds_base + (unsigned char *)ctxt->regs->si;
551 dst = es_base + (unsigned char *)ctxt->regs->di;
552
553 ret = vc_read_mem(ctxt, src, buffer, bytes);
554 if (ret != ES_OK)
555 return ret;
556
557 ret = vc_write_mem(ctxt, dst, buffer, bytes);
558 if (ret != ES_OK)
559 return ret;
560
561 if (ctxt->regs->flags & X86_EFLAGS_DF)
562 off = -bytes;
563 else
564 off = bytes;
565
566 ctxt->regs->si += off;
567 ctxt->regs->di += off;
568
569 rep = insn_has_rep_prefix(&ctxt->insn);
570 if (rep)
571 ctxt->regs->cx -= 1;
572
573 if (!rep || ctxt->regs->cx == 0)
574 return ES_OK;
575 else
576 return ES_RETRY;
577 }
578
vc_handle_mmio(struct ghcb * ghcb,struct es_em_ctxt * ctxt)579 static enum es_result vc_handle_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
580 {
581 struct insn *insn = &ctxt->insn;
582 enum insn_mmio_type mmio;
583 unsigned int bytes = 0;
584 enum es_result ret;
585 u8 sign_byte;
586 long *reg_data;
587
588 mmio = insn_decode_mmio(insn, &bytes);
589 if (mmio == INSN_MMIO_DECODE_FAILED)
590 return ES_DECODE_FAILED;
591
592 if (mmio != INSN_MMIO_WRITE_IMM && mmio != INSN_MMIO_MOVS) {
593 reg_data = insn_get_modrm_reg_ptr(insn, ctxt->regs);
594 if (!reg_data)
595 return ES_DECODE_FAILED;
596 }
597
598 if (user_mode(ctxt->regs))
599 return ES_UNSUPPORTED;
600
601 switch (mmio) {
602 case INSN_MMIO_WRITE:
603 memcpy(ghcb->shared_buffer, reg_data, bytes);
604 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
605 break;
606 case INSN_MMIO_WRITE_IMM:
607 memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
608 ret = vc_do_mmio(ghcb, ctxt, bytes, false);
609 break;
610 case INSN_MMIO_READ:
611 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
612 if (ret)
613 break;
614
615 /* Zero-extend for 32-bit operation */
616 if (bytes == 4)
617 *reg_data = 0;
618
619 memcpy(reg_data, ghcb->shared_buffer, bytes);
620 break;
621 case INSN_MMIO_READ_ZERO_EXTEND:
622 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
623 if (ret)
624 break;
625
626 /* Zero extend based on operand size */
627 memset(reg_data, 0, insn->opnd_bytes);
628 memcpy(reg_data, ghcb->shared_buffer, bytes);
629 break;
630 case INSN_MMIO_READ_SIGN_EXTEND:
631 ret = vc_do_mmio(ghcb, ctxt, bytes, true);
632 if (ret)
633 break;
634
635 if (bytes == 1) {
636 u8 *val = (u8 *)ghcb->shared_buffer;
637
638 sign_byte = (*val & 0x80) ? 0xff : 0x00;
639 } else {
640 u16 *val = (u16 *)ghcb->shared_buffer;
641
642 sign_byte = (*val & 0x8000) ? 0xff : 0x00;
643 }
644
645 /* Sign extend based on operand size */
646 memset(reg_data, sign_byte, insn->opnd_bytes);
647 memcpy(reg_data, ghcb->shared_buffer, bytes);
648 break;
649 case INSN_MMIO_MOVS:
650 ret = vc_handle_mmio_movs(ctxt, bytes);
651 break;
652 default:
653 ret = ES_UNSUPPORTED;
654 break;
655 }
656
657 return ret;
658 }
659
vc_handle_dr7_write(struct ghcb * ghcb,struct es_em_ctxt * ctxt)660 static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
661 struct es_em_ctxt *ctxt)
662 {
663 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
664 long val, *reg = vc_insn_get_rm(ctxt);
665 enum es_result ret;
666
667 if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
668 return ES_VMM_ERROR;
669
670 if (!reg)
671 return ES_DECODE_FAILED;
672
673 val = *reg;
674
675 /* Upper 32 bits must be written as zeroes */
676 if (val >> 32) {
677 ctxt->fi.vector = X86_TRAP_GP;
678 ctxt->fi.error_code = 0;
679 return ES_EXCEPTION;
680 }
681
682 /* Clear out other reserved bits and set bit 10 */
683 val = (val & 0xffff23ffL) | BIT(10);
684
685 /* Early non-zero writes to DR7 are not supported */
686 if (!data && (val & ~DR7_RESET_VALUE))
687 return ES_UNSUPPORTED;
688
689 /* Using a value of 0 for ExitInfo1 means RAX holds the value */
690 ghcb_set_rax(ghcb, val);
691 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
692 if (ret != ES_OK)
693 return ret;
694
695 if (data)
696 data->dr7 = val;
697
698 return ES_OK;
699 }
700
vc_handle_dr7_read(struct ghcb * ghcb,struct es_em_ctxt * ctxt)701 static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
702 struct es_em_ctxt *ctxt)
703 {
704 struct sev_es_runtime_data *data = this_cpu_read(runtime_data);
705 long *reg = vc_insn_get_rm(ctxt);
706
707 if (sev_status & MSR_AMD64_SNP_DEBUG_SWAP)
708 return ES_VMM_ERROR;
709
710 if (!reg)
711 return ES_DECODE_FAILED;
712
713 if (data)
714 *reg = data->dr7;
715 else
716 *reg = DR7_RESET_VALUE;
717
718 return ES_OK;
719 }
720
vc_handle_wbinvd(struct ghcb * ghcb,struct es_em_ctxt * ctxt)721 static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
722 struct es_em_ctxt *ctxt)
723 {
724 return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
725 }
726
vc_handle_rdpmc(struct ghcb * ghcb,struct es_em_ctxt * ctxt)727 static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
728 {
729 enum es_result ret;
730
731 ghcb_set_rcx(ghcb, ctxt->regs->cx);
732
733 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
734 if (ret != ES_OK)
735 return ret;
736
737 if (!(ghcb_rax_is_valid(ghcb) && ghcb_rdx_is_valid(ghcb)))
738 return ES_VMM_ERROR;
739
740 ctxt->regs->ax = ghcb->save.rax;
741 ctxt->regs->dx = ghcb->save.rdx;
742
743 return ES_OK;
744 }
745
vc_handle_monitor(struct ghcb * ghcb,struct es_em_ctxt * ctxt)746 static enum es_result vc_handle_monitor(struct ghcb *ghcb,
747 struct es_em_ctxt *ctxt)
748 {
749 /*
750 * Treat it as a NOP and do not leak a physical address to the
751 * hypervisor.
752 */
753 return ES_OK;
754 }
755
vc_handle_mwait(struct ghcb * ghcb,struct es_em_ctxt * ctxt)756 static enum es_result vc_handle_mwait(struct ghcb *ghcb,
757 struct es_em_ctxt *ctxt)
758 {
759 /* Treat the same as MONITOR/MONITORX */
760 return ES_OK;
761 }
762
vc_handle_vmmcall(struct ghcb * ghcb,struct es_em_ctxt * ctxt)763 static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
764 struct es_em_ctxt *ctxt)
765 {
766 enum es_result ret;
767
768 ghcb_set_rax(ghcb, ctxt->regs->ax);
769 ghcb_set_cpl(ghcb, user_mode(ctxt->regs) ? 3 : 0);
770
771 if (x86_platform.hyper.sev_es_hcall_prepare)
772 x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
773
774 ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
775 if (ret != ES_OK)
776 return ret;
777
778 if (!ghcb_rax_is_valid(ghcb))
779 return ES_VMM_ERROR;
780
781 ctxt->regs->ax = ghcb->save.rax;
782
783 /*
784 * Call sev_es_hcall_finish() after regs->ax is already set.
785 * This allows the hypervisor handler to overwrite it again if
786 * necessary.
787 */
788 if (x86_platform.hyper.sev_es_hcall_finish &&
789 !x86_platform.hyper.sev_es_hcall_finish(ghcb, ctxt->regs))
790 return ES_VMM_ERROR;
791
792 return ES_OK;
793 }
794
vc_handle_trap_ac(struct ghcb * ghcb,struct es_em_ctxt * ctxt)795 static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,
796 struct es_em_ctxt *ctxt)
797 {
798 /*
799 * Calling ecx_alignment_check() directly does not work, because it
800 * enables IRQs and the GHCB is active. Forward the exception and call
801 * it later from vc_forward_exception().
802 */
803 ctxt->fi.vector = X86_TRAP_AC;
804 ctxt->fi.error_code = 0;
805 return ES_EXCEPTION;
806 }
807
vc_handle_exitcode(struct es_em_ctxt * ctxt,struct ghcb * ghcb,unsigned long exit_code)808 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
809 struct ghcb *ghcb,
810 unsigned long exit_code)
811 {
812 enum es_result result = vc_check_opcode_bytes(ctxt, exit_code);
813
814 if (result != ES_OK)
815 return result;
816
817 switch (exit_code) {
818 case SVM_EXIT_READ_DR7:
819 result = vc_handle_dr7_read(ghcb, ctxt);
820 break;
821 case SVM_EXIT_WRITE_DR7:
822 result = vc_handle_dr7_write(ghcb, ctxt);
823 break;
824 case SVM_EXIT_EXCP_BASE + X86_TRAP_AC:
825 result = vc_handle_trap_ac(ghcb, ctxt);
826 break;
827 case SVM_EXIT_RDTSC:
828 case SVM_EXIT_RDTSCP:
829 result = vc_handle_rdtsc(ghcb, ctxt, exit_code);
830 break;
831 case SVM_EXIT_RDPMC:
832 result = vc_handle_rdpmc(ghcb, ctxt);
833 break;
834 case SVM_EXIT_INVD:
835 pr_err_ratelimited("#VC exception for INVD??? Seriously???\n");
836 result = ES_UNSUPPORTED;
837 break;
838 case SVM_EXIT_CPUID:
839 result = vc_handle_cpuid(ghcb, ctxt);
840 break;
841 case SVM_EXIT_IOIO:
842 result = vc_handle_ioio(ghcb, ctxt);
843 break;
844 case SVM_EXIT_MSR:
845 result = vc_handle_msr(ghcb, ctxt);
846 break;
847 case SVM_EXIT_VMMCALL:
848 result = vc_handle_vmmcall(ghcb, ctxt);
849 break;
850 case SVM_EXIT_WBINVD:
851 result = vc_handle_wbinvd(ghcb, ctxt);
852 break;
853 case SVM_EXIT_MONITOR:
854 result = vc_handle_monitor(ghcb, ctxt);
855 break;
856 case SVM_EXIT_MWAIT:
857 result = vc_handle_mwait(ghcb, ctxt);
858 break;
859 case SVM_EXIT_NPF:
860 result = vc_handle_mmio(ghcb, ctxt);
861 break;
862 default:
863 /*
864 * Unexpected #VC exception
865 */
866 result = ES_UNSUPPORTED;
867 }
868
869 return result;
870 }
871
is_vc2_stack(unsigned long sp)872 static __always_inline bool is_vc2_stack(unsigned long sp)
873 {
874 return (sp >= __this_cpu_ist_bottom_va(VC2) && sp < __this_cpu_ist_top_va(VC2));
875 }
876
vc_from_invalid_context(struct pt_regs * regs)877 static __always_inline bool vc_from_invalid_context(struct pt_regs *regs)
878 {
879 unsigned long sp, prev_sp;
880
881 sp = (unsigned long)regs;
882 prev_sp = regs->sp;
883
884 /*
885 * If the code was already executing on the VC2 stack when the #VC
886 * happened, let it proceed to the normal handling routine. This way the
887 * code executing on the VC2 stack can cause #VC exceptions to get handled.
888 */
889 return is_vc2_stack(sp) && !is_vc2_stack(prev_sp);
890 }
891
vc_raw_handle_exception(struct pt_regs * regs,unsigned long error_code)892 static bool vc_raw_handle_exception(struct pt_regs *regs, unsigned long error_code)
893 {
894 struct ghcb_state state;
895 struct es_em_ctxt ctxt;
896 enum es_result result;
897 struct ghcb *ghcb;
898 bool ret = true;
899
900 ghcb = __sev_get_ghcb(&state);
901
902 vc_ghcb_invalidate(ghcb);
903 result = vc_init_em_ctxt(&ctxt, regs, error_code);
904
905 if (result == ES_OK)
906 result = vc_handle_exitcode(&ctxt, ghcb, error_code);
907
908 __sev_put_ghcb(&state);
909
910 /* Done - now check the result */
911 switch (result) {
912 case ES_OK:
913 vc_finish_insn(&ctxt);
914 break;
915 case ES_UNSUPPORTED:
916 pr_err_ratelimited("Unsupported exit-code 0x%02lx in #VC exception (IP: 0x%lx)\n",
917 error_code, regs->ip);
918 ret = false;
919 break;
920 case ES_VMM_ERROR:
921 pr_err_ratelimited("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
922 error_code, regs->ip);
923 ret = false;
924 break;
925 case ES_DECODE_FAILED:
926 pr_err_ratelimited("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
927 error_code, regs->ip);
928 ret = false;
929 break;
930 case ES_EXCEPTION:
931 vc_forward_exception(&ctxt);
932 break;
933 case ES_RETRY:
934 /* Nothing to do */
935 break;
936 default:
937 pr_emerg("Unknown result in %s():%d\n", __func__, result);
938 /*
939 * Emulating the instruction which caused the #VC exception
940 * failed - can't continue so print debug information
941 */
942 BUG();
943 }
944
945 return ret;
946 }
947
vc_is_db(unsigned long error_code)948 static __always_inline bool vc_is_db(unsigned long error_code)
949 {
950 return error_code == SVM_EXIT_EXCP_BASE + X86_TRAP_DB;
951 }
952
953 /*
954 * Runtime #VC exception handler when raised from kernel mode. Runs in NMI mode
955 * and will panic when an error happens.
956 */
DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)957 DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication)
958 {
959 irqentry_state_t irq_state;
960
961 /*
962 * With the current implementation it is always possible to switch to a
963 * safe stack because #VC exceptions only happen at known places, like
964 * intercepted instructions or accesses to MMIO areas/IO ports. They can
965 * also happen with code instrumentation when the hypervisor intercepts
966 * #DB, but the critical paths are forbidden to be instrumented, so #DB
967 * exceptions currently also only happen in safe places.
968 *
969 * But keep this here in case the noinstr annotations are violated due
970 * to bug elsewhere.
971 */
972 if (unlikely(vc_from_invalid_context(regs))) {
973 instrumentation_begin();
974 panic("Can't handle #VC exception from unsupported context\n");
975 instrumentation_end();
976 }
977
978 /*
979 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
980 */
981 if (vc_is_db(error_code)) {
982 exc_debug(regs);
983 return;
984 }
985
986 irq_state = irqentry_nmi_enter(regs);
987
988 instrumentation_begin();
989
990 if (!vc_raw_handle_exception(regs, error_code)) {
991 /* Show some debug info */
992 show_regs(regs);
993
994 /* Ask hypervisor to sev_es_terminate */
995 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
996
997 /* If that fails and we get here - just panic */
998 panic("Returned from Terminate-Request to Hypervisor\n");
999 }
1000
1001 instrumentation_end();
1002 irqentry_nmi_exit(regs, irq_state);
1003 }
1004
1005 /*
1006 * Runtime #VC exception handler when raised from user mode. Runs in IRQ mode
1007 * and will kill the current task with SIGBUS when an error happens.
1008 */
DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)1009 DEFINE_IDTENTRY_VC_USER(exc_vmm_communication)
1010 {
1011 /*
1012 * Handle #DB before calling into !noinstr code to avoid recursive #DB.
1013 */
1014 if (vc_is_db(error_code)) {
1015 noist_exc_debug(regs);
1016 return;
1017 }
1018
1019 irqentry_enter_from_user_mode(regs);
1020 instrumentation_begin();
1021
1022 if (!vc_raw_handle_exception(regs, error_code)) {
1023 /*
1024 * Do not kill the machine if user-space triggered the
1025 * exception. Send SIGBUS instead and let user-space deal with
1026 * it.
1027 */
1028 force_sig_fault(SIGBUS, BUS_OBJERR, (void __user *)0);
1029 }
1030
1031 instrumentation_end();
1032 irqentry_exit_to_user_mode(regs);
1033 }
1034
handle_vc_boot_ghcb(struct pt_regs * regs)1035 bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
1036 {
1037 unsigned long exit_code = regs->orig_ax;
1038 struct es_em_ctxt ctxt;
1039 enum es_result result;
1040
1041 vc_ghcb_invalidate(boot_ghcb);
1042
1043 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
1044 if (result == ES_OK)
1045 result = vc_handle_exitcode(&ctxt, boot_ghcb, exit_code);
1046
1047 /* Done - now check the result */
1048 switch (result) {
1049 case ES_OK:
1050 vc_finish_insn(&ctxt);
1051 break;
1052 case ES_UNSUPPORTED:
1053 early_printk("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n",
1054 exit_code, regs->ip);
1055 goto fail;
1056 case ES_VMM_ERROR:
1057 early_printk("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n",
1058 exit_code, regs->ip);
1059 goto fail;
1060 case ES_DECODE_FAILED:
1061 early_printk("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n",
1062 exit_code, regs->ip);
1063 goto fail;
1064 case ES_EXCEPTION:
1065 vc_early_forward_exception(&ctxt);
1066 break;
1067 case ES_RETRY:
1068 /* Nothing to do */
1069 break;
1070 default:
1071 BUG();
1072 }
1073
1074 return true;
1075
1076 fail:
1077 show_regs(regs);
1078
1079 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
1080 }
1081
1082