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;
57 nested_vmx_check_supported();
59 vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
60 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
62 /* Allocate VMX pages and shared descriptors (vmx_pages). */
63 vcpu_alloc_vmx(vm, &vmx_pages_gva);
64 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
67 volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
70 vcpu_run(vm, VCPU_ID);
71 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
72 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
74 exit_reason_str(run->exit_reason));
76 if (run->io.port == PORT_L0_EXIT)
79 switch (get_ucall(vm, VCPU_ID, &uc)) {
81 TEST_ASSERT(false, "%s", (const char *)uc.args[0]);
84 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);