1
2
3
4
5
6
7
8
9
10#include "test_util.h"
11#include "kvm_util.h"
12#include "processor.h"
13#include "vmx.h"
14
15#include <errno.h>
16#include <linux/kvm.h>
17#include <string.h>
18#include <sys/ioctl.h>
19#include <unistd.h>
20
21
22
23
24
25#define VMCS12_REVISION 0x11e57ed0
26#define VCPU_ID 5
27
28void test_nested_state(struct kvm_vm *vm, struct kvm_nested_state *state)
29{
30 volatile struct kvm_run *run;
31
32 vcpu_nested_state_set(vm, VCPU_ID, state, false);
33 run = vcpu_state(vm, VCPU_ID);
34 vcpu_run(vm, VCPU_ID);
35 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
36 "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
37 run->exit_reason,
38 exit_reason_str(run->exit_reason));
39}
40
41void test_nested_state_expect_errno(struct kvm_vm *vm,
42 struct kvm_nested_state *state,
43 int expected_errno)
44{
45 volatile struct kvm_run *run;
46 int rv;
47
48 rv = vcpu_nested_state_set(vm, VCPU_ID, state, true);
49 TEST_ASSERT(rv == -1 && errno == expected_errno,
50 "Expected %s (%d) from vcpu_nested_state_set but got rv: %i errno: %s (%d)",
51 strerror(expected_errno), expected_errno, rv, strerror(errno),
52 errno);
53 run = vcpu_state(vm, VCPU_ID);
54 vcpu_run(vm, VCPU_ID);
55 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
56 "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
57 run->exit_reason,
58 exit_reason_str(run->exit_reason));
59}
60
61void test_nested_state_expect_einval(struct kvm_vm *vm,
62 struct kvm_nested_state *state)
63{
64 test_nested_state_expect_errno(vm, state, EINVAL);
65}
66
67void test_nested_state_expect_efault(struct kvm_vm *vm,
68 struct kvm_nested_state *state)
69{
70 test_nested_state_expect_errno(vm, state, EFAULT);
71}
72
73void set_revision_id_for_vmcs12(struct kvm_nested_state *state,
74 u32 vmcs12_revision)
75{
76
77 memcpy(&state->data, &vmcs12_revision, sizeof(u32));
78}
79
80void set_default_state(struct kvm_nested_state *state)
81{
82 memset(state, 0, sizeof(*state));
83 state->flags = KVM_STATE_NESTED_RUN_PENDING |
84 KVM_STATE_NESTED_GUEST_MODE;
85 state->format = 0;
86 state->size = sizeof(*state);
87}
88
89void set_default_vmx_state(struct kvm_nested_state *state, int size)
90{
91 memset(state, 0, size);
92 state->flags = KVM_STATE_NESTED_GUEST_MODE |
93 KVM_STATE_NESTED_RUN_PENDING |
94 KVM_STATE_NESTED_EVMCS;
95 state->format = 0;
96 state->size = size;
97 state->hdr.vmx.vmxon_pa = 0x1000;
98 state->hdr.vmx.vmcs12_pa = 0x2000;
99 state->hdr.vmx.smm.flags = 0;
100 set_revision_id_for_vmcs12(state, VMCS12_REVISION);
101}
102
103void test_vmx_nested_state(struct kvm_vm *vm)
104{
105
106 const int state_sz = sizeof(struct kvm_nested_state) + getpagesize();
107 struct kvm_nested_state *state =
108 (struct kvm_nested_state *)malloc(state_sz);
109
110
111 set_default_vmx_state(state, state_sz);
112 state->format = 1;
113 test_nested_state_expect_einval(vm, state);
114
115
116
117
118
119 set_default_vmx_state(state, state_sz);
120 test_nested_state_expect_einval(vm, state);
121
122
123
124
125
126
127 set_default_vmx_state(state, state_sz);
128 state->hdr.vmx.vmxon_pa = -1ull;
129 test_nested_state_expect_einval(vm, state);
130
131 state->hdr.vmx.vmcs12_pa = -1ull;
132 state->flags = KVM_STATE_NESTED_EVMCS;
133 test_nested_state_expect_einval(vm, state);
134
135 state->flags = 0;
136 test_nested_state(vm, state);
137
138
139 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
140
141
142
143
144
145 set_default_vmx_state(state, state_sz);
146 state->hdr.vmx.vmxon_pa = -1ull;
147 state->hdr.vmx.vmcs12_pa = -1ull;
148 test_nested_state_expect_einval(vm, state);
149
150 state->flags = KVM_STATE_NESTED_EVMCS;
151 test_nested_state(vm, state);
152
153
154 state->hdr.vmx.smm.flags = 1;
155 test_nested_state_expect_einval(vm, state);
156
157
158 set_default_vmx_state(state, state_sz);
159 state->hdr.vmx.vmxon_pa = -1ull;
160 state->flags = 0;
161 test_nested_state_expect_einval(vm, state);
162
163
164 set_default_vmx_state(state, state_sz);
165 state->hdr.vmx.vmxon_pa = 1;
166 test_nested_state_expect_einval(vm, state);
167
168
169
170
171
172 set_default_vmx_state(state, state_sz);
173 state->flags = KVM_STATE_NESTED_GUEST_MODE |
174 KVM_STATE_NESTED_RUN_PENDING;
175 state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
176 test_nested_state_expect_einval(vm, state);
177
178
179
180
181
182
183 set_default_vmx_state(state, state_sz);
184 state->hdr.vmx.smm.flags = ~(KVM_STATE_NESTED_SMM_GUEST_MODE |
185 KVM_STATE_NESTED_SMM_VMXON);
186 test_nested_state_expect_einval(vm, state);
187
188
189 set_default_vmx_state(state, state_sz);
190 state->flags = 0;
191 state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
192 test_nested_state_expect_einval(vm, state);
193
194
195 set_default_vmx_state(state, state_sz);
196 state->size = sizeof(*state);
197 test_nested_state(vm, state);
198
199
200 set_default_vmx_state(state, state_sz);
201 state->hdr.vmx.vmxon_pa = 0;
202 state->hdr.vmx.vmcs12_pa = 0;
203 test_nested_state_expect_einval(vm, state);
204
205
206 set_default_vmx_state(state, state_sz);
207 set_revision_id_for_vmcs12(state, 0);
208 test_nested_state_expect_einval(vm, state);
209
210
211
212
213
214 set_default_vmx_state(state, state_sz);
215 state->hdr.vmx.vmxon_pa = -1ull;
216 state->hdr.vmx.vmcs12_pa = -1ull;
217 state->flags = 0;
218 test_nested_state(vm, state);
219 vcpu_nested_state_get(vm, VCPU_ID, state);
220 TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
221 "Size must be between %d and %d. The size returned was %d.",
222 sizeof(*state), state_sz, state->size);
223 TEST_ASSERT(state->hdr.vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
224 TEST_ASSERT(state->hdr.vmx.vmcs12_pa == -1ull, "vmcs_pa must be -1ull.");
225
226 free(state);
227}
228
229int main(int argc, char *argv[])
230{
231 struct kvm_vm *vm;
232 struct kvm_nested_state state;
233 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
234
235 if (!kvm_check_cap(KVM_CAP_NESTED_STATE)) {
236 printf("KVM_CAP_NESTED_STATE not available, skipping test\n");
237 exit(KSFT_SKIP);
238 }
239
240
241
242
243
244 if (!(entry->ecx & CPUID_VMX)) {
245 fprintf(stderr, "nested VMX not enabled, skipping test\n");
246 exit(KSFT_SKIP);
247 }
248
249 vm = vm_create_default(VCPU_ID, 0, 0);
250
251
252 test_nested_state_expect_efault(vm, NULL);
253
254
255 set_default_state(&state);
256 state.size = 0;
257 test_nested_state_expect_einval(vm, &state);
258
259
260
261
262
263
264
265
266 set_default_state(&state);
267 state.flags = 0xf;
268 test_nested_state_expect_einval(vm, &state);
269
270
271
272
273
274 set_default_state(&state);
275 state.flags = KVM_STATE_NESTED_RUN_PENDING;
276 test_nested_state_expect_einval(vm, &state);
277
278
279
280
281
282 if (entry->ecx & CPUID_VMX)
283 test_vmx_nested_state(vm);
284
285 kvm_vm_free(vm);
286 return 0;
287}
288