1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vmx_close_while_nested 4 * 5 * Copyright (C) 2019, Red Hat, Inc. 6 * 7 * Verify that nothing bad happens if a KVM user exits with open 8 * file descriptors while executing a nested guest. 9 */ 10 11 #include "test_util.h" 12 #include "kvm_util.h" 13 #include "processor.h" 14 #include "vmx.h" 15 16 #include <string.h> 17 #include <sys/ioctl.h> 18 19 #include "kselftest.h" 20 21 enum { 22 PORT_L0_EXIT = 0x2000, 23 }; 24 25 static void l2_guest_code(void) 26 { 27 /* Exit to L0 */ 28 asm volatile("inb %%dx, %%al" 29 : : [port] "d" (PORT_L0_EXIT) : "rax"); 30 } 31 32 static void l1_guest_code(struct vmx_pages *vmx_pages) 33 { 34 #define L2_GUEST_STACK_SIZE 64 35 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; 36 37 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages)); 38 GUEST_ASSERT(load_vmcs(vmx_pages)); 39 40 /* Prepare the VMCS for L2 execution. */ 41 prepare_vmcs(vmx_pages, l2_guest_code, 42 &l2_guest_stack[L2_GUEST_STACK_SIZE]); 43 44 GUEST_ASSERT(!vmlaunch()); 45 GUEST_ASSERT(0); 46 } 47 48 int main(int argc, char *argv[]) 49 { 50 vm_vaddr_t vmx_pages_gva; 51 struct kvm_vcpu *vcpu; 52 struct kvm_vm *vm; 53 54 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX)); 55 56 vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code); 57 58 /* Allocate VMX pages and shared descriptors (vmx_pages). */ 59 vcpu_alloc_vmx(vm, &vmx_pages_gva); 60 vcpu_args_set(vcpu, 1, vmx_pages_gva); 61 62 for (;;) { 63 volatile struct kvm_run *run = vcpu->run; 64 struct ucall uc; 65 66 vcpu_run(vcpu); 67 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, 68 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", 69 run->exit_reason, 70 exit_reason_str(run->exit_reason)); 71 72 if (run->io.port == PORT_L0_EXIT) 73 break; 74 75 switch (get_ucall(vcpu, &uc)) { 76 case UCALL_ABORT: 77 REPORT_GUEST_ASSERT(uc); 78 /* NOT REACHED */ 79 default: 80 TEST_FAIL("Unknown ucall %lu", uc.cmd); 81 } 82 } 83 } 84