Commit 4bc87470 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Convert vmx_set_nested_state_test away from VCPU_ID

Convert vmx_set_nested_state_test to use vm_create_with_one_vcpu() and
pass around a 'struct kvm_vcpu' object instead of using a global VCPU_ID.
Note, this is a "functional" change in the sense that the test now
creates a vCPU with vcpu_id==0 instead of vcpu_id==5.  The non-zero
VCPU_ID was 100% arbitrary and added little to no validation coverage.
If testing non-zero vCPU IDs is desirable for generic tests, that can be
done in the future by tweaking the VM creation helpers.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent d8b5b5d1
...@@ -23,38 +23,37 @@ ...@@ -23,38 +23,37 @@
* changes this should be updated. * changes this should be updated.
*/ */
#define VMCS12_REVISION 0x11e57ed0 #define VMCS12_REVISION 0x11e57ed0
#define VCPU_ID 5
bool have_evmcs; bool have_evmcs;
void test_nested_state(struct kvm_vm *vm, struct kvm_nested_state *state) void test_nested_state(struct kvm_vcpu *vcpu, struct kvm_nested_state *state)
{ {
vcpu_nested_state_set(vm, VCPU_ID, state); vcpu_nested_state_set(vcpu->vm, vcpu->id, state);
} }
void test_nested_state_expect_errno(struct kvm_vm *vm, void test_nested_state_expect_errno(struct kvm_vcpu *vcpu,
struct kvm_nested_state *state, struct kvm_nested_state *state,
int expected_errno) int expected_errno)
{ {
int rv; int rv;
rv = __vcpu_nested_state_set(vm, VCPU_ID, state); rv = __vcpu_nested_state_set(vcpu->vm, vcpu->id, state);
TEST_ASSERT(rv == -1 && errno == expected_errno, TEST_ASSERT(rv == -1 && errno == expected_errno,
"Expected %s (%d) from vcpu_nested_state_set but got rv: %i errno: %s (%d)", "Expected %s (%d) from vcpu_nested_state_set but got rv: %i errno: %s (%d)",
strerror(expected_errno), expected_errno, rv, strerror(errno), strerror(expected_errno), expected_errno, rv, strerror(errno),
errno); errno);
} }
void test_nested_state_expect_einval(struct kvm_vm *vm, void test_nested_state_expect_einval(struct kvm_vcpu *vcpu,
struct kvm_nested_state *state) struct kvm_nested_state *state)
{ {
test_nested_state_expect_errno(vm, state, EINVAL); test_nested_state_expect_errno(vcpu, state, EINVAL);
} }
void test_nested_state_expect_efault(struct kvm_vm *vm, void test_nested_state_expect_efault(struct kvm_vcpu *vcpu,
struct kvm_nested_state *state) struct kvm_nested_state *state)
{ {
test_nested_state_expect_errno(vm, state, EFAULT); test_nested_state_expect_errno(vcpu, state, EFAULT);
} }
void set_revision_id_for_vmcs12(struct kvm_nested_state *state, void set_revision_id_for_vmcs12(struct kvm_nested_state *state,
...@@ -86,7 +85,7 @@ void set_default_vmx_state(struct kvm_nested_state *state, int size) ...@@ -86,7 +85,7 @@ void set_default_vmx_state(struct kvm_nested_state *state, int size)
set_revision_id_for_vmcs12(state, VMCS12_REVISION); set_revision_id_for_vmcs12(state, VMCS12_REVISION);
} }
void test_vmx_nested_state(struct kvm_vm *vm) void test_vmx_nested_state(struct kvm_vcpu *vcpu)
{ {
/* Add a page for VMCS12. */ /* Add a page for VMCS12. */
const int state_sz = sizeof(struct kvm_nested_state) + getpagesize(); const int state_sz = sizeof(struct kvm_nested_state) + getpagesize();
...@@ -96,14 +95,14 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -96,14 +95,14 @@ void test_vmx_nested_state(struct kvm_vm *vm)
/* The format must be set to 0. 0 for VMX, 1 for SVM. */ /* The format must be set to 0. 0 for VMX, 1 for SVM. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->format = 1; state->format = 1;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* We cannot virtualize anything if the guest does not have VMX * We cannot virtualize anything if the guest does not have VMX
* enabled. * enabled.
*/ */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* We cannot virtualize anything if the guest does not have VMX * We cannot virtualize anything if the guest does not have VMX
...@@ -112,17 +111,17 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -112,17 +111,17 @@ void test_vmx_nested_state(struct kvm_vm *vm)
*/ */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.vmxon_pa = -1ull; state->hdr.vmx.vmxon_pa = -1ull;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
state->hdr.vmx.vmcs12_pa = -1ull; state->hdr.vmx.vmcs12_pa = -1ull;
state->flags = KVM_STATE_NESTED_EVMCS; state->flags = KVM_STATE_NESTED_EVMCS;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
state->flags = 0; state->flags = 0;
test_nested_state(vm, state); test_nested_state(vcpu, state);
/* Enable VMX in the guest CPUID. */ /* Enable VMX in the guest CPUID. */
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); vcpu_set_cpuid(vcpu->vm, vcpu->id, kvm_get_supported_cpuid());
/* /*
* Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without
...@@ -133,34 +132,34 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -133,34 +132,34 @@ void test_vmx_nested_state(struct kvm_vm *vm)
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.vmxon_pa = -1ull; state->hdr.vmx.vmxon_pa = -1ull;
state->hdr.vmx.vmcs12_pa = -1ull; state->hdr.vmx.vmcs12_pa = -1ull;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
state->flags &= KVM_STATE_NESTED_EVMCS; state->flags &= KVM_STATE_NESTED_EVMCS;
if (have_evmcs) { if (have_evmcs) {
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
vcpu_enable_evmcs(vm, VCPU_ID); vcpu_enable_evmcs(vcpu->vm, vcpu->id);
} }
test_nested_state(vm, state); test_nested_state(vcpu, state);
/* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */ /* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */
state->hdr.vmx.smm.flags = 1; state->hdr.vmx.smm.flags = 1;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* Invalid flags are rejected. */ /* Invalid flags are rejected. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.flags = ~0; state->hdr.vmx.flags = ~0;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* It is invalid to have vmxon_pa == -1ull and vmcs_pa != -1ull. */ /* It is invalid to have vmxon_pa == -1ull and vmcs_pa != -1ull. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.vmxon_pa = -1ull; state->hdr.vmx.vmxon_pa = -1ull;
state->flags = 0; state->flags = 0;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* It is invalid to have vmxon_pa set to a non-page aligned address. */ /* It is invalid to have vmxon_pa set to a non-page aligned address. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.vmxon_pa = 1; state->hdr.vmx.vmxon_pa = 1;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* It is invalid to have KVM_STATE_NESTED_SMM_GUEST_MODE and * It is invalid to have KVM_STATE_NESTED_SMM_GUEST_MODE and
...@@ -170,7 +169,7 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -170,7 +169,7 @@ void test_vmx_nested_state(struct kvm_vm *vm)
state->flags = KVM_STATE_NESTED_GUEST_MODE | state->flags = KVM_STATE_NESTED_GUEST_MODE |
KVM_STATE_NESTED_RUN_PENDING; KVM_STATE_NESTED_RUN_PENDING;
state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE; state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* It is invalid to have any of the SMM flags set besides: * It is invalid to have any of the SMM flags set besides:
...@@ -180,13 +179,13 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -180,13 +179,13 @@ void test_vmx_nested_state(struct kvm_vm *vm)
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.smm.flags = ~(KVM_STATE_NESTED_SMM_GUEST_MODE | state->hdr.vmx.smm.flags = ~(KVM_STATE_NESTED_SMM_GUEST_MODE |
KVM_STATE_NESTED_SMM_VMXON); KVM_STATE_NESTED_SMM_VMXON);
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* Outside SMM, SMM flags must be zero. */ /* Outside SMM, SMM flags must be zero. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->flags = 0; state->flags = 0;
state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE; state->hdr.vmx.smm.flags = KVM_STATE_NESTED_SMM_GUEST_MODE;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* Size must be large enough to fit kvm_nested_state and vmcs12 * Size must be large enough to fit kvm_nested_state and vmcs12
...@@ -195,13 +194,13 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -195,13 +194,13 @@ void test_vmx_nested_state(struct kvm_vm *vm)
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->size = sizeof(*state); state->size = sizeof(*state);
state->flags = 0; state->flags = 0;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->size = sizeof(*state); state->size = sizeof(*state);
state->flags = 0; state->flags = 0;
state->hdr.vmx.vmcs12_pa = -1; state->hdr.vmx.vmcs12_pa = -1;
test_nested_state(vm, state); test_nested_state(vcpu, state);
/* /*
* KVM_SET_NESTED_STATE succeeds with invalid VMCS * KVM_SET_NESTED_STATE succeeds with invalid VMCS
...@@ -209,7 +208,7 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -209,7 +208,7 @@ void test_vmx_nested_state(struct kvm_vm *vm)
*/ */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->flags = 0; state->flags = 0;
test_nested_state(vm, state); test_nested_state(vcpu, state);
/* Invalid flags are rejected, even if no VMCS loaded. */ /* Invalid flags are rejected, even if no VMCS loaded. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
...@@ -217,13 +216,13 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -217,13 +216,13 @@ void test_vmx_nested_state(struct kvm_vm *vm)
state->flags = 0; state->flags = 0;
state->hdr.vmx.vmcs12_pa = -1; state->hdr.vmx.vmcs12_pa = -1;
state->hdr.vmx.flags = ~0; state->hdr.vmx.flags = ~0;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* vmxon_pa cannot be the same address as vmcs_pa. */ /* vmxon_pa cannot be the same address as vmcs_pa. */
set_default_vmx_state(state, state_sz); set_default_vmx_state(state, state_sz);
state->hdr.vmx.vmxon_pa = 0; state->hdr.vmx.vmxon_pa = 0;
state->hdr.vmx.vmcs12_pa = 0; state->hdr.vmx.vmcs12_pa = 0;
test_nested_state_expect_einval(vm, state); test_nested_state_expect_einval(vcpu, state);
/* /*
* Test that if we leave nesting the state reflects that when we get * Test that if we leave nesting the state reflects that when we get
...@@ -233,8 +232,8 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -233,8 +232,8 @@ void test_vmx_nested_state(struct kvm_vm *vm)
state->hdr.vmx.vmxon_pa = -1ull; state->hdr.vmx.vmxon_pa = -1ull;
state->hdr.vmx.vmcs12_pa = -1ull; state->hdr.vmx.vmcs12_pa = -1ull;
state->flags = 0; state->flags = 0;
test_nested_state(vm, state); test_nested_state(vcpu, state);
vcpu_nested_state_get(vm, VCPU_ID, state); vcpu_nested_state_get(vcpu->vm, vcpu->id, state);
TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz, TEST_ASSERT(state->size >= sizeof(*state) && state->size <= state_sz,
"Size must be between %ld and %d. The size returned was %d.", "Size must be between %ld and %d. The size returned was %d.",
sizeof(*state), state_sz, state->size); sizeof(*state), state_sz, state->size);
...@@ -244,7 +243,7 @@ void test_vmx_nested_state(struct kvm_vm *vm) ...@@ -244,7 +243,7 @@ void test_vmx_nested_state(struct kvm_vm *vm)
free(state); free(state);
} }
void disable_vmx(struct kvm_vm *vm) void disable_vmx(struct kvm_vcpu *vcpu)
{ {
struct kvm_cpuid2 *cpuid = kvm_get_supported_cpuid(); struct kvm_cpuid2 *cpuid = kvm_get_supported_cpuid();
int i; int i;
...@@ -256,7 +255,7 @@ void disable_vmx(struct kvm_vm *vm) ...@@ -256,7 +255,7 @@ void disable_vmx(struct kvm_vm *vm)
TEST_ASSERT(i != cpuid->nent, "CPUID function 1 not found"); TEST_ASSERT(i != cpuid->nent, "CPUID function 1 not found");
cpuid->entries[i].ecx &= ~CPUID_VMX; cpuid->entries[i].ecx &= ~CPUID_VMX;
vcpu_set_cpuid(vm, VCPU_ID, cpuid); vcpu_set_cpuid(vcpu->vm, vcpu->id, cpuid);
cpuid->entries[i].ecx |= CPUID_VMX; cpuid->entries[i].ecx |= CPUID_VMX;
} }
...@@ -264,6 +263,7 @@ int main(int argc, char *argv[]) ...@@ -264,6 +263,7 @@ int main(int argc, char *argv[])
{ {
struct kvm_vm *vm; struct kvm_vm *vm;
struct kvm_nested_state state; struct kvm_nested_state state;
struct kvm_vcpu *vcpu;
have_evmcs = kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS); have_evmcs = kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS);
...@@ -278,20 +278,20 @@ int main(int argc, char *argv[]) ...@@ -278,20 +278,20 @@ int main(int argc, char *argv[])
*/ */
nested_vmx_check_supported(); nested_vmx_check_supported();
vm = vm_create_default(VCPU_ID, 0, 0); vm = vm_create_with_one_vcpu(&vcpu, NULL);
/* /*
* First run tests with VMX disabled to check error handling. * First run tests with VMX disabled to check error handling.
*/ */
disable_vmx(vm); disable_vmx(vcpu);
/* Passing a NULL kvm_nested_state causes a EFAULT. */ /* Passing a NULL kvm_nested_state causes a EFAULT. */
test_nested_state_expect_efault(vm, NULL); test_nested_state_expect_efault(vcpu, NULL);
/* 'size' cannot be smaller than sizeof(kvm_nested_state). */ /* 'size' cannot be smaller than sizeof(kvm_nested_state). */
set_default_state(&state); set_default_state(&state);
state.size = 0; state.size = 0;
test_nested_state_expect_einval(vm, &state); test_nested_state_expect_einval(vcpu, &state);
/* /*
* Setting the flags 0xf fails the flags check. The only flags that * Setting the flags 0xf fails the flags check. The only flags that
...@@ -302,7 +302,7 @@ int main(int argc, char *argv[]) ...@@ -302,7 +302,7 @@ int main(int argc, char *argv[])
*/ */
set_default_state(&state); set_default_state(&state);
state.flags = 0xf; state.flags = 0xf;
test_nested_state_expect_einval(vm, &state); test_nested_state_expect_einval(vcpu, &state);
/* /*
* If KVM_STATE_NESTED_RUN_PENDING is set then * If KVM_STATE_NESTED_RUN_PENDING is set then
...@@ -310,9 +310,9 @@ int main(int argc, char *argv[]) ...@@ -310,9 +310,9 @@ int main(int argc, char *argv[])
*/ */
set_default_state(&state); set_default_state(&state);
state.flags = KVM_STATE_NESTED_RUN_PENDING; state.flags = KVM_STATE_NESTED_RUN_PENDING;
test_nested_state_expect_einval(vm, &state); test_nested_state_expect_einval(vcpu, &state);
test_vmx_nested_state(vm); test_vmx_nested_state(vcpu);
kvm_vm_free(vm); kvm_vm_free(vm);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment