Skip to content

Commit 62e41f6

Browse files
iorlov-devsean-jc
authored andcommitted
KVM: selftests: Add test case for MMIO during vectoring on x86
Extend the 'set_memory_region_test' with an x86-only test case which covers emulated MMIO during event vectoring error handling. The test case 1) Sets an IDT descriptor base to point to an MMIO address 2) Generates a #GP in the guest 3) Verifies userspace gets the correct exit reason, suberror code, and GPA in internal.data[3] Opportunistically add a definition for a non-canonical address to processor.h so that the source of the #GP is somewhat self-documenting, and so that future tests don't have to reinvent the wheel. Signed-off-by: Ivan Orlov <iorlov@amazon.com> Link: https://lore.kernel.org/r/20241217181458.68690-8-iorlov@amazon.com [sean: massage changelog] Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 4e9427a commit 62e41f6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tools/testing/selftests/kvm/include/x86/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ extern uint64_t guest_tsc_khz;
2727
#define MAX_NR_CPUID_ENTRIES 100
2828
#endif
2929

30+
#define NONCANONICAL 0xaaaaaaaaaaaaaaaaull
31+
3032
/* Forced emulation prefix, used to invoke the emulator unconditionally. */
3133
#define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
3234

tools/testing/selftests/kvm/set_memory_region_test.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,56 @@ static void test_add_overlapping_private_memory_regions(void)
553553
close(memfd);
554554
kvm_vm_free(vm);
555555
}
556+
557+
static void guest_code_mmio_during_vectoring(void)
558+
{
559+
const struct desc_ptr idt_desc = {
560+
.address = MEM_REGION_GPA,
561+
.size = 0xFFF,
562+
};
563+
564+
set_idt(&idt_desc);
565+
566+
/* Generate a #GP by dereferencing a non-canonical address */
567+
*((uint8_t *)NONCANONICAL) = 0x1;
568+
569+
GUEST_ASSERT(0);
570+
}
571+
572+
/*
573+
* This test points the IDT descriptor base to an MMIO address. It should cause
574+
* a KVM internal error when an event occurs in the guest.
575+
*/
576+
static void test_mmio_during_vectoring(void)
577+
{
578+
struct kvm_vcpu *vcpu;
579+
struct kvm_run *run;
580+
struct kvm_vm *vm;
581+
u64 expected_gpa;
582+
583+
pr_info("Testing MMIO during vectoring error handling\n");
584+
585+
vm = vm_create_with_one_vcpu(&vcpu, guest_code_mmio_during_vectoring);
586+
virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 1);
587+
588+
run = vcpu->run;
589+
590+
vcpu_run(vcpu);
591+
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
592+
TEST_ASSERT(run->internal.suberror == KVM_INTERNAL_ERROR_DELIVERY_EV,
593+
"Unexpected suberror = %d", vcpu->run->internal.suberror);
594+
TEST_ASSERT(run->internal.ndata != 4, "Unexpected internal error data array size = %d",
595+
run->internal.ndata);
596+
597+
/* The reported GPA should be IDT base + offset of the GP vector */
598+
expected_gpa = MEM_REGION_GPA + GP_VECTOR * sizeof(struct idt_entry);
599+
600+
TEST_ASSERT(run->internal.data[3] == expected_gpa,
601+
"Unexpected GPA = %llx (expected %lx)",
602+
vcpu->run->internal.data[3], expected_gpa);
603+
604+
kvm_vm_free(vm);
605+
}
556606
#endif
557607

558608
int main(int argc, char *argv[])
@@ -568,6 +618,7 @@ int main(int argc, char *argv[])
568618
* KVM_RUN fails with ENOEXEC or EFAULT.
569619
*/
570620
test_zero_memory_regions();
621+
test_mmio_during_vectoring();
571622
#endif
572623

573624
test_invalid_memory_region_flags();

0 commit comments

Comments
 (0)