diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2019-01-31 17:49:21 -0500 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2019-02-12 07:12:12 -0500 |
commit | 65ab26e397555d340290a99e6c736e291526ecc0 (patch) | |
tree | 89df27638efa9adbdad55d7adc3b8ba78300294a /tools | |
parent | d13937116f1e82bf508a6325111b322c30c85eb9 (diff) |
selftests: kvm: add selftest for releasing VM file descriptor while in L2
This adds a test for the previous bug.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/testing/selftests/kvm/.gitignore | 1 | ||||
-rw-r--r-- | tools/testing/selftests/kvm/Makefile | 1 | ||||
-rw-r--r-- | tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c | 95 |
3 files changed, 97 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 6210ba41c29e..2689d1ea6d7a 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore | |||
@@ -3,6 +3,7 @@ | |||
3 | /x86_64/platform_info_test | 3 | /x86_64/platform_info_test |
4 | /x86_64/set_sregs_test | 4 | /x86_64/set_sregs_test |
5 | /x86_64/sync_regs_test | 5 | /x86_64/sync_regs_test |
6 | /x86_64/vmx_close_while_nested_test | ||
6 | /x86_64/vmx_tsc_adjust_test | 7 | /x86_64/vmx_tsc_adjust_test |
7 | /x86_64/state_test | 8 | /x86_64/state_test |
8 | /dirty_log_test | 9 | /dirty_log_test |
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index f9a0e9938480..3c1f4bdf9000 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile | |||
@@ -16,6 +16,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test | |||
16 | TEST_GEN_PROGS_x86_64 += x86_64/state_test | 16 | TEST_GEN_PROGS_x86_64 += x86_64/state_test |
17 | TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test | 17 | TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test |
18 | TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid | 18 | TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid |
19 | TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test | ||
19 | TEST_GEN_PROGS_x86_64 += dirty_log_test | 20 | TEST_GEN_PROGS_x86_64 += dirty_log_test |
20 | TEST_GEN_PROGS_x86_64 += clear_dirty_log_test | 21 | TEST_GEN_PROGS_x86_64 += clear_dirty_log_test |
21 | 22 | ||
diff --git a/tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c b/tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c new file mode 100644 index 000000000000..6edec6fd790b --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/vmx_close_while_nested_test.c | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * vmx_close_while_nested | ||
3 | * | ||
4 | * Copyright (C) 2019, Red Hat, Inc. | ||
5 | * | ||
6 | * This work is licensed under the terms of the GNU GPL, version 2. | ||
7 | * | ||
8 | * Verify that nothing bad happens if a KVM user exits with open | ||
9 | * file descriptors while executing a nested guest. | ||
10 | */ | ||
11 | |||
12 | #include "test_util.h" | ||
13 | #include "kvm_util.h" | ||
14 | #include "processor.h" | ||
15 | #include "vmx.h" | ||
16 | |||
17 | #include <string.h> | ||
18 | #include <sys/ioctl.h> | ||
19 | |||
20 | #include "kselftest.h" | ||
21 | |||
22 | #define VCPU_ID 5 | ||
23 | |||
24 | enum { | ||
25 | PORT_L0_EXIT = 0x2000, | ||
26 | }; | ||
27 | |||
28 | /* The virtual machine object. */ | ||
29 | static struct kvm_vm *vm; | ||
30 | |||
31 | static void l2_guest_code(void) | ||
32 | { | ||
33 | /* Exit to L0 */ | ||
34 | asm volatile("inb %%dx, %%al" | ||
35 | : : [port] "d" (PORT_L0_EXIT) : "rax"); | ||
36 | } | ||
37 | |||
38 | static void l1_guest_code(struct vmx_pages *vmx_pages) | ||
39 | { | ||
40 | #define L2_GUEST_STACK_SIZE 64 | ||
41 | unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; | ||
42 | uint32_t control; | ||
43 | uintptr_t save_cr3; | ||
44 | |||
45 | GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages)); | ||
46 | GUEST_ASSERT(load_vmcs(vmx_pages)); | ||
47 | |||
48 | /* Prepare the VMCS for L2 execution. */ | ||
49 | prepare_vmcs(vmx_pages, l2_guest_code, | ||
50 | &l2_guest_stack[L2_GUEST_STACK_SIZE]); | ||
51 | |||
52 | GUEST_ASSERT(!vmlaunch()); | ||
53 | GUEST_ASSERT(0); | ||
54 | } | ||
55 | |||
56 | int main(int argc, char *argv[]) | ||
57 | { | ||
58 | struct vmx_pages *vmx_pages; | ||
59 | vm_vaddr_t vmx_pages_gva; | ||
60 | struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); | ||
61 | |||
62 | if (!(entry->ecx & CPUID_VMX)) { | ||
63 | fprintf(stderr, "nested VMX not enabled, skipping test\n"); | ||
64 | exit(KSFT_SKIP); | ||
65 | } | ||
66 | |||
67 | vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code); | ||
68 | vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); | ||
69 | |||
70 | /* Allocate VMX pages and shared descriptors (vmx_pages). */ | ||
71 | vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva); | ||
72 | vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva); | ||
73 | |||
74 | for (;;) { | ||
75 | volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID); | ||
76 | struct ucall uc; | ||
77 | |||
78 | vcpu_run(vm, VCPU_ID); | ||
79 | TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, | ||
80 | "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", | ||
81 | run->exit_reason, | ||
82 | exit_reason_str(run->exit_reason)); | ||
83 | |||
84 | if (run->io.port == PORT_L0_EXIT) | ||
85 | break; | ||
86 | |||
87 | switch (get_ucall(vm, VCPU_ID, &uc)) { | ||
88 | case UCALL_ABORT: | ||
89 | TEST_ASSERT(false, "%s", (const char *)uc.args[0]); | ||
90 | /* NOT REACHED */ | ||
91 | default: | ||
92 | TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd); | ||
93 | } | ||
94 | } | ||
95 | } | ||