1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018, Red Hat, Inc.
4 *
5 * Tests for Enlightened VMCS, including nested guest state.
6 */
7 #define _GNU_SOURCE /* for program_invocation_short_name */
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
13
14 #include "test_util.h"
15
16 #include "kvm_util.h"
17
18 #include "vmx.h"
19
20 #define VCPU_ID 5
21
22 static bool have_nested_state;
23
l2_guest_code(void)24 void l2_guest_code(void)
25 {
26 GUEST_SYNC(6);
27
28 GUEST_SYNC(7);
29
30 /* Done, exit to L1 and never come back. */
31 vmcall();
32 }
33
l1_guest_code(struct vmx_pages * vmx_pages)34 void l1_guest_code(struct vmx_pages *vmx_pages)
35 {
36 #define L2_GUEST_STACK_SIZE 64
37 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
38
39 enable_vp_assist(vmx_pages->vp_assist_gpa, vmx_pages->vp_assist);
40
41 GUEST_ASSERT(vmx_pages->vmcs_gpa);
42 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
43 GUEST_SYNC(3);
44 GUEST_ASSERT(load_vmcs(vmx_pages));
45 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
46
47 GUEST_SYNC(4);
48 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
49
50 prepare_vmcs(vmx_pages, l2_guest_code,
51 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
52
53 GUEST_SYNC(5);
54 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
55 GUEST_ASSERT(!vmlaunch());
56 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
57 GUEST_SYNC(8);
58 GUEST_ASSERT(!vmresume());
59 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
60 GUEST_SYNC(9);
61 }
62
guest_code(struct vmx_pages * vmx_pages)63 void guest_code(struct vmx_pages *vmx_pages)
64 {
65 GUEST_SYNC(1);
66 GUEST_SYNC(2);
67
68 if (vmx_pages)
69 l1_guest_code(vmx_pages);
70
71 GUEST_DONE();
72 }
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 struct vmx_pages *vmx_pages = NULL;
77 vm_vaddr_t vmx_pages_gva = 0;
78
79 struct kvm_regs regs1, regs2;
80 struct kvm_vm *vm;
81 struct kvm_run *run;
82 struct kvm_x86_state *state;
83 struct ucall uc;
84 int stage;
85 uint16_t evmcs_ver;
86 struct kvm_enable_cap enable_evmcs_cap = {
87 .cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
88 .args[0] = (unsigned long)&evmcs_ver
89 };
90
91 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
92
93 /* Create VM */
94 vm = vm_create_default(VCPU_ID, 0, guest_code);
95
96 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
97
98 if (!kvm_check_cap(KVM_CAP_NESTED_STATE) ||
99 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
100 printf("capabilities not available, skipping test\n");
101 exit(KSFT_SKIP);
102 }
103
104 vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
105
106 run = vcpu_state(vm, VCPU_ID);
107
108 vcpu_regs_get(vm, VCPU_ID, ®s1);
109
110 vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
111 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
112
113 for (stage = 1;; stage++) {
114 _vcpu_run(vm, VCPU_ID);
115 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
116 "Unexpected exit reason: %u (%s),\n",
117 run->exit_reason,
118 exit_reason_str(run->exit_reason));
119
120 memset(®s1, 0, sizeof(regs1));
121 vcpu_regs_get(vm, VCPU_ID, ®s1);
122 switch (get_ucall(vm, VCPU_ID, &uc)) {
123 case UCALL_ABORT:
124 TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
125 __FILE__, uc.args[1]);
126 /* NOT REACHED */
127 case UCALL_SYNC:
128 break;
129 case UCALL_DONE:
130 goto done;
131 default:
132 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
133 }
134
135 /* UCALL_SYNC is handled here. */
136 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
137 uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
138 stage, (ulong)uc.args[1]);
139
140 state = vcpu_save_state(vm, VCPU_ID);
141 kvm_vm_release(vm);
142
143 /* Restore state in a new VM. */
144 kvm_vm_restart(vm, O_RDWR);
145 vm_vcpu_add(vm, VCPU_ID, 0, 0);
146 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
147 vcpu_load_state(vm, VCPU_ID, state);
148 run = vcpu_state(vm, VCPU_ID);
149 free(state);
150
151 memset(®s2, 0, sizeof(regs2));
152 vcpu_regs_get(vm, VCPU_ID, ®s2);
153 TEST_ASSERT(!memcmp(®s1, ®s2, sizeof(regs2)),
154 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
155 (ulong) regs2.rdi, (ulong) regs2.rsi);
156 }
157
158 done:
159 kvm_vm_free(vm);
160 }
161