2 * vmx_set_nested_state_test
4 * Copyright (C) 2019, Google LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2.
8 * This test verifies the integrity of calling the ioctl KVM_SET_NESTED_STATE.
11 #include "test_util.h"
13 #include "processor.h"
17 #include <linux/kvm.h>
19 #include <sys/ioctl.h>
23 * Mirror of VMCS12_REVISION in arch/x86/kvm/vmx/vmcs12.h. If that value
24 * changes this should be updated.
26 #define VMCS12_REVISION 0x11e57ed0
29 void test_nested_state(struct kvm_vm *vm, struct kvm_nested_state *state)
31 volatile struct kvm_run *run;
33 vcpu_nested_state_set(vm, VCPU_ID, state, false);
34 run = vcpu_state(vm, VCPU_ID);
35 vcpu_run(vm, VCPU_ID);
36 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
37 "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
39 exit_reason_str(run->exit_reason));
42 void test_nested_state_expect_errno(struct kvm_vm *vm,
43 struct kvm_nested_state *state,
46 volatile struct kvm_run *run;
49 rv = vcpu_nested_state_set(vm, VCPU_ID, state, true);
50 TEST_ASSERT(rv == -1 && errno == expected_errno,
51 "Expected %s (%d) from vcpu_nested_state_set but got rv: %i errno: %s (%d)",
52 strerror(expected_errno), expected_errno, rv, strerror(errno),
54 run = vcpu_state(vm, VCPU_ID);
55 vcpu_run(vm, VCPU_ID);
56 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
57 "Got exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s),\n",
59 exit_reason_str(run->exit_reason));
62 void test_nested_state_expect_einval(struct kvm_vm *vm,
63 struct kvm_nested_state *state)
65 test_nested_state_expect_errno(vm, state, EINVAL);
68 void test_nested_state_expect_efault(struct kvm_vm *vm,
69 struct kvm_nested_state *state)
71 test_nested_state_expect_errno(vm, state, EFAULT);
74 void set_revision_id_for_vmcs12(struct kvm_nested_state *state,
77 /* Set revision_id in vmcs12 to vmcs12_revision. */
78 memcpy(state->data, &vmcs12_revision, sizeof(u32));
81 void set_default_state(struct kvm_nested_state *state)
83 memset(state, 0, sizeof(*state));
84 state->flags = KVM_STATE_NESTED_RUN_PENDING |
85 KVM_STATE_NESTED_GUEST_MODE;
87 state->size = sizeof(*state);
90 void set_default_vmx_state(struct kvm_nested_state *state, int size)
92 memset(state, 0, size);
93 state->flags = KVM_STATE_NESTED_GUEST_MODE |
94 KVM_STATE_NESTED_RUN_PENDING |
95 KVM_STATE_NESTED_EVMCS;
98 state->vmx.vmxon_pa = 0x1000;
99 state->vmx.vmcs_pa = 0x2000;
100 state->vmx.smm.flags = 0;
101 set_revision_id_for_vmcs12(state, VMCS12_REVISION);
104 void test_vmx_nested_state(struct kvm_vm *vm)
106 /* Add a page for VMCS12. */
107 const int state_sz = sizeof(struct kvm_nested_state) + getpagesize();
108 struct kvm_nested_state *state =
109 (struct kvm_nested_state *)malloc(state_sz);
111 /* The format must be set to 0. 0 for VMX, 1 for SVM. */
112 set_default_vmx_state(state, state_sz);
114 test_nested_state_expect_einval(vm, state);
117 * We cannot virtualize anything if the guest does not have VMX
120 set_default_vmx_state(state, state_sz);
121 test_nested_state_expect_einval(vm, state);
124 * We cannot virtualize anything if the guest does not have VMX
125 * enabled. We expect KVM_SET_NESTED_STATE to return 0 if vmxon_pa
128 set_default_vmx_state(state, state_sz);
129 state->vmx.vmxon_pa = -1ull;
130 test_nested_state(vm, state);
132 /* Enable VMX in the guest CPUID. */
133 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
135 /* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */
136 set_default_vmx_state(state, state_sz);
137 state->vmx.vmxon_pa = -1ull;
138 state->vmx.smm.flags = 1;
139 test_nested_state_expect_einval(vm, state);
141 /* It is invalid to have vmxon_pa == -1ull and vmcs_pa != -1ull. */
142 set_default_vmx_state(state, state_sz);
143 state->vmx.vmxon_pa = -1ull;
144 state->vmx.vmcs_pa = 0;
145 test_nested_state_expect_einval(vm, state);
148 * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
149 * setting the nested state.
151 set_default_vmx_state(state, state_sz);
152 state->vmx.vmxon_pa = -1ull;
153 state->vmx.vmcs_pa = -1ull;
154 test_nested_state(vm, state);
156 /* It is invalid to have vmxon_pa set to a non-page aligned address. */
157 set_default_vmx_state(state, state_sz);
158 state->vmx.vmxon_pa = 1;
159 test_nested_state_expect_einval(vm, state);
162 * It is invalid to have KVM_STATE_NESTED_SMM_GUEST_MODE and
163 * KVM_STATE_NESTED_GUEST_MODE set together.
165 set_default_vmx_state(state, state_sz);
166 state->flags = KVM_STATE_NESTED_GUEST_MODE |
167 KVM_STATE_NESTED_RUN_PENDING;
168 state->vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
169 test_nested_state_expect_einval(vm, state);
172 * It is invalid to have any of the SMM flags set besides:
173 * KVM_STATE_NESTED_SMM_GUEST_MODE
174 * KVM_STATE_NESTED_SMM_VMXON
176 set_default_vmx_state(state, state_sz);
177 state->vmx.smm.flags = ~(KVM_STATE_NESTED_SMM_GUEST_MODE |
178 KVM_STATE_NESTED_SMM_VMXON);
179 test_nested_state_expect_einval(vm, state);
181 /* Outside SMM, SMM flags must be zero. */
182 set_default_vmx_state(state, state_sz);
184 state->vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
185 test_nested_state_expect_einval(vm, state);
187 /* Size must be large enough to fit kvm_nested_state and vmcs12. */
188 set_default_vmx_state(state, state_sz);
189 state->size = sizeof(*state);
190 test_nested_state(vm, state);
192 /* vmxon_pa cannot be the same address as vmcs_pa. */
193 set_default_vmx_state(state, state_sz);
194 state->vmx.vmxon_pa = 0;
195 state->vmx.vmcs_pa = 0;
196 test_nested_state_expect_einval(vm, state);
198 /* The revision id for vmcs12 must be VMCS12_REVISION. */
199 set_default_vmx_state(state, state_sz);
200 set_revision_id_for_vmcs12(state, 0);
201 test_nested_state_expect_einval(vm, state);
204 * Test that if we leave nesting the state reflects that when we get
207 set_default_vmx_state(state, state_sz);
208 state->vmx.vmxon_pa = -1ull;
209 state->vmx.vmcs_pa = -1ull;
211 test_nested_state(vm, state);
212 vcpu_nested_state_get(vm, VCPU_ID, state);
213 TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
214 "Size must be between %d and %d. The size returned was %d.",
215 sizeof(*state), state_sz, state->size);
216 TEST_ASSERT(state->vmx.vmxon_pa == -1ull, "vmxon_pa must be -1ull.");
217 TEST_ASSERT(state->vmx.vmcs_pa == -1ull, "vmcs_pa must be -1ull.");
222 int main(int argc, char *argv[])
225 struct kvm_nested_state state;
226 struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
228 if (!kvm_check_cap(KVM_CAP_NESTED_STATE)) {
229 printf("KVM_CAP_NESTED_STATE not available, skipping test\n");
234 * AMD currently does not implement set_nested_state, so for now we
237 if (!(entry->ecx & CPUID_VMX)) {
238 fprintf(stderr, "nested VMX not enabled, skipping test\n");
242 vm = vm_create_default(VCPU_ID, 0, 0);
244 /* Passing a NULL kvm_nested_state causes a EFAULT. */
245 test_nested_state_expect_efault(vm, NULL);
247 /* 'size' cannot be smaller than sizeof(kvm_nested_state). */
248 set_default_state(&state);
250 test_nested_state_expect_einval(vm, &state);
253 * Setting the flags 0xf fails the flags check. The only flags that
255 * KVM_STATE_NESTED_GUEST_MODE
256 * KVM_STATE_NESTED_RUN_PENDING
257 * KVM_STATE_NESTED_EVMCS
259 set_default_state(&state);
261 test_nested_state_expect_einval(vm, &state);
264 * If KVM_STATE_NESTED_RUN_PENDING is set then
265 * KVM_STATE_NESTED_GUEST_MODE has to be set as well.
267 set_default_state(&state);
268 state.flags = KVM_STATE_NESTED_RUN_PENDING;
269 test_nested_state_expect_einval(vm, &state);
272 * TODO: When SVM support is added for KVM_SET_NESTED_STATE
273 * add tests here to support it like VMX.
275 if (entry->ecx & CPUID_VMX)
276 test_vmx_nested_state(vm);