1 // SPDX-License-Identifier: GPL-2.0-only
3 * vmx_close_while_nested
5 * Copyright (C) 2019, Red Hat, Inc.
7 * Verify that nothing bad happens if a KVM user exits with open
8 * file descriptors while executing a nested guest.
11 #include "test_util.h"
13 #include "processor.h"
17 #include <sys/ioctl.h>
19 #include "kselftest.h"
24 PORT_L0_EXIT = 0x2000,
27 /* The virtual machine object. */
28 static struct kvm_vm *vm;
30 static void l2_guest_code(void)
33 asm volatile("inb %%dx, %%al"
34 : : [port] "d" (PORT_L0_EXIT) : "rax");
37 static void l1_guest_code(struct vmx_pages *vmx_pages)
39 #define L2_GUEST_STACK_SIZE 64
40 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
42 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
43 GUEST_ASSERT(load_vmcs(vmx_pages));
45 /* Prepare the VMCS for L2 execution. */
46 prepare_vmcs(vmx_pages, l2_guest_code,
47 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
49 GUEST_ASSERT(!vmlaunch());
53 int main(int argc, char *argv[])
55 vm_vaddr_t vmx_pages_gva;
56 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
58 if (!(entry->ecx & CPUID_VMX)) {
59 fprintf(stderr, "nested VMX not enabled, skipping test\n");
63 vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
64 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
66 /* Allocate VMX pages and shared descriptors (vmx_pages). */
67 vcpu_alloc_vmx(vm, &vmx_pages_gva);
68 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
71 volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
74 vcpu_run(vm, VCPU_ID);
75 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
76 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
78 exit_reason_str(run->exit_reason));
80 if (run->io.port == PORT_L0_EXIT)
83 switch (get_ucall(vm, VCPU_ID, &uc)) {
85 TEST_ASSERT(false, "%s", (const char *)uc.args[0]);
88 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);