xref: /linux/tools/testing/selftests/kvm/x86/vmx_apic_access_test.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vmx_apic_access_test
4  *
5  * Copyright (C) 2020, Google LLC.
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2.
8  *
9  * The first subtest simply checks to see that an L2 guest can be
10  * launched with a valid APIC-access address that is backed by a
11  * page of L1 physical memory.
12  *
13  * The second subtest sets the APIC-access address to a (valid) L1
14  * physical address that is not backed by memory. KVM can't handle
15  * this situation, so resuming L2 should result in a KVM exit for
16  * internal error (emulation). This is not an architectural
17  * requirement. It is just a shortcoming of KVM. The internal error
18  * is unfortunate, but it's better than what used to happen!
19  */
20 
21 #include "test_util.h"
22 #include "kvm_util.h"
23 #include "processor.h"
24 #include "vmx.h"
25 
26 #include <string.h>
27 #include <sys/ioctl.h>
28 
29 #include "kselftest.h"
30 
31 static void l2_guest_code(void)
32 {
33 	/* Exit to L1 */
34 	__asm__ __volatile__("vmcall");
35 }
36 
37 static void l1_guest_code(struct vmx_pages *vmx_pages, unsigned long high_gpa)
38 {
39 	u32 control;
40 
41 	GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
42 	GUEST_ASSERT(load_vmcs(vmx_pages));
43 
44 	/* Prepare the VMCS for L2 execution. */
45 	prepare_vmcs(vmx_pages, l2_guest_code);
46 	control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
47 	control |= CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
48 	vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
49 	control = vmreadz(SECONDARY_VM_EXEC_CONTROL);
50 	control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
51 	vmwrite(SECONDARY_VM_EXEC_CONTROL, control);
52 	vmwrite(APIC_ACCESS_ADDR, vmx_pages->apic_access_gpa);
53 
54 	/* Try to launch L2 with the memory-backed APIC-access address. */
55 	GUEST_SYNC(vmreadz(APIC_ACCESS_ADDR));
56 	GUEST_ASSERT(!vmlaunch());
57 	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
58 
59 	vmwrite(APIC_ACCESS_ADDR, high_gpa);
60 
61 	/* Try to resume L2 with the unbacked APIC-access address. */
62 	GUEST_SYNC(vmreadz(APIC_ACCESS_ADDR));
63 	GUEST_ASSERT(!vmresume());
64 	GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
65 
66 	GUEST_DONE();
67 }
68 
69 int main(int argc, char *argv[])
70 {
71 	unsigned long apic_access_addr = ~0ul;
72 	gva_t vmx_pages_gva;
73 	unsigned long high_gpa;
74 	struct vmx_pages *vmx;
75 	bool done = false;
76 
77 	struct kvm_vcpu *vcpu;
78 	struct kvm_vm *vm;
79 
80 	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
81 
82 	vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
83 
84 	high_gpa = (vm->max_gfn - 1) << vm->page_shift;
85 
86 	vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
87 	prepare_virtualize_apic_accesses(vmx, vm);
88 	vcpu_args_set(vcpu, 2, vmx_pages_gva, high_gpa);
89 
90 	while (!done) {
91 		volatile struct kvm_run *run = vcpu->run;
92 		struct ucall uc;
93 
94 		vcpu_run(vcpu);
95 		if (apic_access_addr == high_gpa) {
96 			TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
97 			TEST_ASSERT(run->internal.suberror ==
98 				    KVM_INTERNAL_ERROR_EMULATION,
99 				    "Got internal suberror other than KVM_INTERNAL_ERROR_EMULATION: %u",
100 				    run->internal.suberror);
101 			break;
102 		}
103 		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
104 
105 		switch (get_ucall(vcpu, &uc)) {
106 		case UCALL_ABORT:
107 			REPORT_GUEST_ASSERT(uc);
108 			/* NOT REACHED */
109 		case UCALL_SYNC:
110 			apic_access_addr = uc.args[1];
111 			break;
112 		case UCALL_DONE:
113 			done = true;
114 			break;
115 		default:
116 			TEST_ASSERT(false, "Unknown ucall %lu", uc.cmd);
117 		}
118 	}
119 	kvm_vm_free(vm);
120 	return 0;
121 }
122