KVM: selftests: Convert smm_test away from VCPU_ID
authorSean Christopherson <seanjc@google.com>
Tue, 15 Feb 2022 23:51:19 +0000 (15:51 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 11 Jun 2022 15:46:42 +0000 (11:46 -0400)
Convert smm_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==1.  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: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/x86_64/smm_test.c

index dd2c152..36165b7 100644 (file)
@@ -19,8 +19,6 @@
 #include "vmx.h"
 #include "svm_util.h"
 
-#define VCPU_ID              1
-
 #define SMRAM_SIZE 65536
 #define SMRAM_MEMSLOT ((1 << 16) | 1)
 #define SMRAM_PAGES (SMRAM_SIZE / PAGE_SIZE)
@@ -116,22 +114,23 @@ static void guest_code(void *arg)
        sync_with_host(DONE);
 }
 
-void inject_smi(struct kvm_vm *vm)
+void inject_smi(struct kvm_vcpu *vcpu)
 {
        struct kvm_vcpu_events events;
 
-       vcpu_events_get(vm, VCPU_ID, &events);
+       vcpu_events_get(vcpu->vm, vcpu->id, &events);
 
        events.smi.pending = 1;
        events.flags |= KVM_VCPUEVENT_VALID_SMM;
 
-       vcpu_events_set(vm, VCPU_ID, &events);
+       vcpu_events_set(vcpu->vm, vcpu->id, &events);
 }
 
 int main(int argc, char *argv[])
 {
        vm_vaddr_t nested_gva = 0;
 
+       struct kvm_vcpu *vcpu;
        struct kvm_regs regs;
        struct kvm_vm *vm;
        struct kvm_run *run;
@@ -139,9 +138,9 @@ int main(int argc, char *argv[])
        int stage, stage_reported;
 
        /* 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;
 
        vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, SMRAM_GPA,
                                    SMRAM_MEMSLOT, SMRAM_PAGES, 0);
@@ -152,7 +151,7 @@ int main(int argc, char *argv[])
        memcpy(addr_gpa2hva(vm, SMRAM_GPA) + 0x8000, smi_handler,
               sizeof(smi_handler));
 
-       vcpu_set_msr(vm, VCPU_ID, MSR_IA32_SMBASE, SMRAM_GPA);
+       vcpu_set_msr(vm, vcpu->id, MSR_IA32_SMBASE, SMRAM_GPA);
 
        if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
                if (nested_svm_supported())
@@ -164,17 +163,17 @@ int main(int argc, char *argv[])
        if (!nested_gva)
                pr_info("will skip SMM test with VMX enabled\n");
 
-       vcpu_args_set(vm, VCPU_ID, 1, nested_gva);
+       vcpu_args_set(vm, vcpu->id, 1, nested_gva);
 
        for (stage = 1;; stage++) {
-               _vcpu_run(vm, VCPU_ID);
+               vcpu_run(vm, vcpu->id);
                TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
                            "Stage %d: unexpected exit reason: %u (%s),\n",
                            stage, run->exit_reason,
                            exit_reason_str(run->exit_reason));
 
                memset(&regs, 0, sizeof(regs));
-               vcpu_regs_get(vm, VCPU_ID, &regs);
+               vcpu_regs_get(vm, vcpu->id, &regs);
 
                stage_reported = regs.rax & 0xff;
 
@@ -191,7 +190,7 @@ int main(int argc, char *argv[])
                 * return from it. Do not perform save/restore while in SMM yet.
                 */
                if (stage == 8) {
-                       inject_smi(vm);
+                       inject_smi(vcpu);
                        continue;
                }
 
@@ -200,15 +199,15 @@ int main(int argc, char *argv[])
                 * during L2 execution.
                 */
                if (stage == 10)
-                       inject_smi(vm);
+                       inject_smi(vcpu);
 
-               state = vcpu_save_state(vm, VCPU_ID);
+               state = vcpu_save_state(vm, vcpu->id);
                kvm_vm_release(vm);
-               kvm_vm_restart(vm);
-               vm_vcpu_add(vm, VCPU_ID);
-               vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
-               vcpu_load_state(vm, VCPU_ID, state);
-               run = vcpu_state(vm, VCPU_ID);
+
+               vcpu = vm_recreate_with_one_vcpu(vm);
+               vcpu_set_cpuid(vm, vcpu->id, kvm_get_supported_cpuid());
+               vcpu_load_state(vm, vcpu->id, state);
+               run = vcpu->run;
                kvm_x86_state_cleanup(state);
        }