Commit 90b13cdd authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Convert state_test away from VCPU_ID

Convert state_test to use vm_create_with_one_vcpu() and
vm_recreate_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.

Opportunistically use vcpu_run() instead of _vcpu_run(), the test expects
KVM_RUN to succeed.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 20092699
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "vmx.h" #include "vmx.h"
#include "svm_util.h" #include "svm_util.h"
#define VCPU_ID 5
#define L2_GUEST_STACK_SIZE 256 #define L2_GUEST_STACK_SIZE 256
void svm_l2_guest_code(void) void svm_l2_guest_code(void)
...@@ -157,6 +156,7 @@ int main(int argc, char *argv[]) ...@@ -157,6 +156,7 @@ int main(int argc, char *argv[])
vm_vaddr_t nested_gva = 0; vm_vaddr_t nested_gva = 0;
struct kvm_regs regs1, regs2; struct kvm_regs regs1, regs2;
struct kvm_vcpu *vcpu;
struct kvm_vm *vm; struct kvm_vm *vm;
struct kvm_run *run; struct kvm_run *run;
struct kvm_x86_state *state; struct kvm_x86_state *state;
...@@ -164,10 +164,10 @@ int main(int argc, char *argv[]) ...@@ -164,10 +164,10 @@ int main(int argc, char *argv[])
int stage; int stage;
/* Create VM */ /* Create VM */
vm = vm_create_default(VCPU_ID, 0, guest_code); vm = vm_create_with_one_vcpu(&vcpu, guest_code);
run = vcpu_state(vm, VCPU_ID); run = vcpu->run;
vcpu_regs_get(vm, VCPU_ID, &regs1); vcpu_regs_get(vm, vcpu->id, &regs1);
if (kvm_check_cap(KVM_CAP_NESTED_STATE)) { if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
if (nested_svm_supported()) if (nested_svm_supported())
...@@ -179,16 +179,16 @@ int main(int argc, char *argv[]) ...@@ -179,16 +179,16 @@ int main(int argc, char *argv[])
if (!nested_gva) if (!nested_gva)
pr_info("will skip nested state checks\n"); pr_info("will skip nested state checks\n");
vcpu_args_set(vm, VCPU_ID, 1, nested_gva); vcpu_args_set(vm, vcpu->id, 1, nested_gva);
for (stage = 1;; stage++) { for (stage = 1;; stage++) {
_vcpu_run(vm, VCPU_ID); vcpu_run(vm, vcpu->id);
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
"Stage %d: unexpected exit reason: %u (%s),\n", "Stage %d: unexpected exit reason: %u (%s),\n",
stage, run->exit_reason, stage, run->exit_reason,
exit_reason_str(run->exit_reason)); exit_reason_str(run->exit_reason));
switch (get_ucall(vm, VCPU_ID, &uc)) { switch (get_ucall(vm, vcpu->id, &uc)) {
case UCALL_ABORT: case UCALL_ABORT:
TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0],
__FILE__, uc.args[1]); __FILE__, uc.args[1]);
...@@ -206,22 +206,21 @@ int main(int argc, char *argv[]) ...@@ -206,22 +206,21 @@ int main(int argc, char *argv[])
uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx", uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
stage, (ulong)uc.args[1]); stage, (ulong)uc.args[1]);
state = vcpu_save_state(vm, VCPU_ID); state = vcpu_save_state(vm, vcpu->id);
memset(&regs1, 0, sizeof(regs1)); memset(&regs1, 0, sizeof(regs1));
vcpu_regs_get(vm, VCPU_ID, &regs1); vcpu_regs_get(vm, vcpu->id, &regs1);
kvm_vm_release(vm); kvm_vm_release(vm);
/* Restore state in a new VM. */ /* Restore state in a new VM. */
kvm_vm_restart(vm); vcpu = vm_recreate_with_one_vcpu(vm);
vm_vcpu_add(vm, VCPU_ID); vcpu_set_cpuid(vm, vcpu->id, kvm_get_supported_cpuid());
vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); vcpu_load_state(vm, vcpu->id, state);
vcpu_load_state(vm, VCPU_ID, state); run = vcpu->run;
run = vcpu_state(vm, VCPU_ID);
kvm_x86_state_cleanup(state); kvm_x86_state_cleanup(state);
memset(&regs2, 0, sizeof(regs2)); memset(&regs2, 0, sizeof(regs2));
vcpu_regs_get(vm, VCPU_ID, &regs2); vcpu_regs_get(vm, vcpu->id, &regs2);
TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)), TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
"Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx", "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
(ulong) regs2.rdi, (ulong) regs2.rsi); (ulong) regs2.rdi, (ulong) regs2.rsi);
......
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