Skip to content

Commit 51ee7d6

Browse files
tlendackysuryasaimadhu
authored andcommitted
x86/sev-es: Handle MMIO events
Add a handler for #VC exceptions caused by MMIO intercepts. These intercepts come along as nested page faults on pages with reserved bits set. Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com> [ jroedel@suse.de: Adapt to VC handling framework ] Co-developed-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Joerg Roedel <jroedel@suse.de> Signed-off-by: Borislav Petkov <bp@suse.de> Link: https://lkml.kernel.org/r/20200907131613.12703-50-joro@8bytes.org
1 parent 5e3427a commit 51ee7d6

2 files changed

Lines changed: 227 additions & 0 deletions

File tree

‎arch/x86/include/uapi/asm/svm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401
8282
#define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402
8383

84+
/* SEV-ES software-defined VMGEXIT events */
85+
#define SVM_VMGEXIT_MMIO_READ 0x80000001
86+
#define SVM_VMGEXIT_MMIO_WRITE 0x80000002
87+
#define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff
88+
8489
#define SVM_EXIT_ERR -1
8590

8691
#define SVM_EXIT_REASONS \

‎arch/x86/kernel/sev-es.c‎

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,37 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
359359
return ES_EXCEPTION;
360360
}
361361

362+
static bool vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
363+
unsigned long vaddr, phys_addr_t *paddr)
364+
{
365+
unsigned long va = (unsigned long)vaddr;
366+
unsigned int level;
367+
phys_addr_t pa;
368+
pgd_t *pgd;
369+
pte_t *pte;
370+
371+
pgd = __va(read_cr3_pa());
372+
pgd = &pgd[pgd_index(va)];
373+
pte = lookup_address_in_pgd(pgd, va, &level);
374+
if (!pte) {
375+
ctxt->fi.vector = X86_TRAP_PF;
376+
ctxt->fi.cr2 = vaddr;
377+
ctxt->fi.error_code = 0;
378+
379+
if (user_mode(ctxt->regs))
380+
ctxt->fi.error_code |= X86_PF_USER;
381+
382+
return false;
383+
}
384+
385+
pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
386+
pa |= va & ~page_level_mask(level);
387+
388+
*paddr = pa;
389+
390+
return true;
391+
}
392+
362393
/* Include code shared with pre-decompression boot stage */
363394
#include "sev-es-shared.c"
364395

@@ -447,6 +478,194 @@ static void __init vc_early_forward_exception(struct es_em_ctxt *ctxt)
447478
do_early_exception(ctxt->regs, trapnr);
448479
}
449480

481+
static long *vc_insn_get_reg(struct es_em_ctxt *ctxt)
482+
{
483+
long *reg_array;
484+
int offset;
485+
486+
reg_array = (long *)ctxt->regs;
487+
offset = insn_get_modrm_reg_off(&ctxt->insn, ctxt->regs);
488+
489+
if (offset < 0)
490+
return NULL;
491+
492+
offset /= sizeof(long);
493+
494+
return reg_array + offset;
495+
}
496+
497+
static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
498+
unsigned int bytes, bool read)
499+
{
500+
u64 exit_code, exit_info_1, exit_info_2;
501+
unsigned long ghcb_pa = __pa(ghcb);
502+
phys_addr_t paddr;
503+
void __user *ref;
504+
505+
ref = insn_get_addr_ref(&ctxt->insn, ctxt->regs);
506+
if (ref == (void __user *)-1L)
507+
return ES_UNSUPPORTED;
508+
509+
exit_code = read ? SVM_VMGEXIT_MMIO_READ : SVM_VMGEXIT_MMIO_WRITE;
510+
511+
if (!vc_slow_virt_to_phys(ghcb, ctxt, (unsigned long)ref, &paddr)) {
512+
if (!read)
513+
ctxt->fi.error_code |= X86_PF_WRITE;
514+
515+
return ES_EXCEPTION;
516+
}
517+
518+
exit_info_1 = paddr;
519+
/* Can never be greater than 8 */
520+
exit_info_2 = bytes;
521+
522+
ghcb->save.sw_scratch = ghcb_pa + offsetof(struct ghcb, shared_buffer);
523+
524+
return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
525+
}
526+
527+
static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb,
528+
struct es_em_ctxt *ctxt)
529+
{
530+
struct insn *insn = &ctxt->insn;
531+
unsigned int bytes = 0;
532+
enum es_result ret;
533+
int sign_byte;
534+
long *reg_data;
535+
536+
switch (insn->opcode.bytes[1]) {
537+
/* MMIO Read w/ zero-extension */
538+
case 0xb6:
539+
bytes = 1;
540+
fallthrough;
541+
case 0xb7:
542+
if (!bytes)
543+
bytes = 2;
544+
545+
ret = vc_do_mmio(ghcb, ctxt, bytes, true);
546+
if (ret)
547+
break;
548+
549+
/* Zero extend based on operand size */
550+
reg_data = vc_insn_get_reg(ctxt);
551+
if (!reg_data)
552+
return ES_DECODE_FAILED;
553+
554+
memset(reg_data, 0, insn->opnd_bytes);
555+
556+
memcpy(reg_data, ghcb->shared_buffer, bytes);
557+
break;
558+
559+
/* MMIO Read w/ sign-extension */
560+
case 0xbe:
561+
bytes = 1;
562+
fallthrough;
563+
case 0xbf:
564+
if (!bytes)
565+
bytes = 2;
566+
567+
ret = vc_do_mmio(ghcb, ctxt, bytes, true);
568+
if (ret)
569+
break;
570+
571+
/* Sign extend based on operand size */
572+
reg_data = vc_insn_get_reg(ctxt);
573+
if (!reg_data)
574+
return ES_DECODE_FAILED;
575+
576+
if (bytes == 1) {
577+
u8 *val = (u8 *)ghcb->shared_buffer;
578+
579+
sign_byte = (*val & 0x80) ? 0xff : 0x00;
580+
} else {
581+
u16 *val = (u16 *)ghcb->shared_buffer;
582+
583+
sign_byte = (*val & 0x8000) ? 0xff : 0x00;
584+
}
585+
memset(reg_data, sign_byte, insn->opnd_bytes);
586+
587+
memcpy(reg_data, ghcb->shared_buffer, bytes);
588+
break;
589+
590+
default:
591+
ret = ES_UNSUPPORTED;
592+
}
593+
594+
return ret;
595+
}
596+
597+
static enum es_result vc_handle_mmio(struct ghcb *ghcb,
598+
struct es_em_ctxt *ctxt)
599+
{
600+
struct insn *insn = &ctxt->insn;
601+
unsigned int bytes = 0;
602+
enum es_result ret;
603+
long *reg_data;
604+
605+
switch (insn->opcode.bytes[0]) {
606+
/* MMIO Write */
607+
case 0x88:
608+
bytes = 1;
609+
fallthrough;
610+
case 0x89:
611+
if (!bytes)
612+
bytes = insn->opnd_bytes;
613+
614+
reg_data = vc_insn_get_reg(ctxt);
615+
if (!reg_data)
616+
return ES_DECODE_FAILED;
617+
618+
memcpy(ghcb->shared_buffer, reg_data, bytes);
619+
620+
ret = vc_do_mmio(ghcb, ctxt, bytes, false);
621+
break;
622+
623+
case 0xc6:
624+
bytes = 1;
625+
fallthrough;
626+
case 0xc7:
627+
if (!bytes)
628+
bytes = insn->opnd_bytes;
629+
630+
memcpy(ghcb->shared_buffer, insn->immediate1.bytes, bytes);
631+
632+
ret = vc_do_mmio(ghcb, ctxt, bytes, false);
633+
break;
634+
635+
/* MMIO Read */
636+
case 0x8a:
637+
bytes = 1;
638+
fallthrough;
639+
case 0x8b:
640+
if (!bytes)
641+
bytes = insn->opnd_bytes;
642+
643+
ret = vc_do_mmio(ghcb, ctxt, bytes, true);
644+
if (ret)
645+
break;
646+
647+
reg_data = vc_insn_get_reg(ctxt);
648+
if (!reg_data)
649+
return ES_DECODE_FAILED;
650+
651+
/* Zero-extend for 32-bit operation */
652+
if (bytes == 4)
653+
*reg_data = 0;
654+
655+
memcpy(reg_data, ghcb->shared_buffer, bytes);
656+
break;
657+
658+
/* Two-Byte Opcodes */
659+
case 0x0f:
660+
ret = vc_handle_mmio_twobyte_ops(ghcb, ctxt);
661+
break;
662+
default:
663+
ret = ES_UNSUPPORTED;
664+
}
665+
666+
return ret;
667+
}
668+
450669
static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
451670
struct ghcb *ghcb,
452671
unsigned long exit_code)
@@ -460,6 +679,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
460679
case SVM_EXIT_IOIO:
461680
result = vc_handle_ioio(ghcb, ctxt);
462681
break;
682+
case SVM_EXIT_NPF:
683+
result = vc_handle_mmio(ghcb, ctxt);
684+
break;
463685
default:
464686
/*
465687
* Unexpected #VC exception

0 commit comments

Comments
 (0)