]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: selftests: Convert vmx_exception_with_invalid_guest_state away from VCPU_ID
authorSean Christopherson <seanjc@google.com>
Wed, 16 Feb 2022 00:44:36 +0000 (16:44 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 11 Jun 2022 15:46:49 +0000 (11:46 -0400)
Convert vmx_exception_with_invalid_guest_state to use
vm_create_with_one_vcpu() and pass around a 'struct kvm_vcpu' object
instead of using a global VCPU_ID.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/x86_64/vmx_exception_with_invalid_guest_state.c

index 27a850f3d7ce4f47afec79900f03f4b2899a25cb..70b30583e50d82443d742f28abddaa47eb4aa841 100644 (file)
 
 #include "kselftest.h"
 
-#define VCPU_ID        0
-
-static struct kvm_vm *vm;
-
 static void guest_ud_handler(struct ex_regs *regs)
 {
        /* Loop on the ud2 until guest state is made invalid. */
@@ -24,11 +20,11 @@ static void guest_code(void)
        asm volatile("ud2");
 }
 
-static void __run_vcpu_with_invalid_state(void)
+static void __run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
 {
-       struct kvm_run *run = vcpu_state(vm, VCPU_ID);
+       struct kvm_run *run = vcpu->run;
 
-       vcpu_run(vm, VCPU_ID);
+       vcpu_run(vcpu->vm, vcpu->id);
 
        TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR,
                    "Expected KVM_EXIT_INTERNAL_ERROR, got %d (%s)\n",
@@ -38,15 +34,15 @@ static void __run_vcpu_with_invalid_state(void)
                    run->emulation_failure.suberror);
 }
 
-static void run_vcpu_with_invalid_state(void)
+static void run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
 {
        /*
         * Always run twice to verify KVM handles the case where _KVM_ queues
         * an exception with invalid state and then exits to userspace, i.e.
         * that KVM doesn't explode if userspace ignores the initial error.
         */
-       __run_vcpu_with_invalid_state();
-       __run_vcpu_with_invalid_state();
+       __run_vcpu_with_invalid_state(vcpu);
+       __run_vcpu_with_invalid_state(vcpu);
 }
 
 static void set_timer(void)
@@ -59,33 +55,43 @@ static void set_timer(void)
        ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
 }
 
-static void set_or_clear_invalid_guest_state(bool set)
+static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
 {
        static struct kvm_sregs sregs;
 
        if (!sregs.cr0)
-               vcpu_sregs_get(vm, VCPU_ID, &sregs);
+               vcpu_sregs_get(vcpu->vm, vcpu->id, &sregs);
        sregs.tr.unusable = !!set;
-       vcpu_sregs_set(vm, VCPU_ID, &sregs);
+       vcpu_sregs_set(vcpu->vm, vcpu->id, &sregs);
 }
 
-static void set_invalid_guest_state(void)
+static void set_invalid_guest_state(struct kvm_vcpu *vcpu)
 {
-       set_or_clear_invalid_guest_state(true);
+       set_or_clear_invalid_guest_state(vcpu, true);
 }
 
-static void clear_invalid_guest_state(void)
+static void clear_invalid_guest_state(struct kvm_vcpu *vcpu)
 {
-       set_or_clear_invalid_guest_state(false);
+       set_or_clear_invalid_guest_state(vcpu, false);
+}
+
+static struct kvm_vcpu *get_set_sigalrm_vcpu(struct kvm_vcpu *__vcpu)
+{
+       static struct kvm_vcpu *vcpu = NULL;
+
+       if (__vcpu)
+               vcpu = __vcpu;
+       return vcpu;
 }
 
 static void sigalrm_handler(int sig)
 {
+       struct kvm_vcpu *vcpu = get_set_sigalrm_vcpu(NULL);
        struct kvm_vcpu_events events;
 
        TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig);
 
-       vcpu_events_get(vm, VCPU_ID, &events);
+       vcpu_events_get(vcpu->vm, vcpu->id, &events);
 
        /*
         * If an exception is pending, attempt KVM_RUN with invalid guest,
@@ -93,8 +99,8 @@ static void sigalrm_handler(int sig)
         * between KVM queueing an exception and re-entering the guest.
         */
        if (events.exception.pending) {
-               set_invalid_guest_state();
-               run_vcpu_with_invalid_state();
+               set_invalid_guest_state(vcpu);
+               run_vcpu_with_invalid_state(vcpu);
        } else {
                set_timer();
        }
@@ -102,15 +108,19 @@ static void sigalrm_handler(int sig)
 
 int main(int argc, char *argv[])
 {
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm;
+
        if (!is_intel_cpu() || vm_is_unrestricted_guest(NULL)) {
                print_skip("Must be run with kvm_intel.unrestricted_guest=0");
                exit(KSFT_SKIP);
        }
 
-       vm = vm_create_default(VCPU_ID, 0, (void *)guest_code);
+       vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+       get_set_sigalrm_vcpu(vcpu);
 
        vm_init_descriptor_tables(vm);
-       vcpu_init_descriptor_tables(vm, VCPU_ID);
+       vcpu_init_descriptor_tables(vm, vcpu->id);
 
        vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
 
@@ -119,8 +129,8 @@ int main(int argc, char *argv[])
         * KVM_RUN should induce a TRIPLE_FAULT in L2 as KVM doesn't support
         * emulating invalid guest state for L2.
         */
-       set_invalid_guest_state();
-       run_vcpu_with_invalid_state();
+       set_invalid_guest_state(vcpu);
+       run_vcpu_with_invalid_state(vcpu);
 
        /*
         * Verify KVM also handles the case where userspace gains control while
@@ -129,11 +139,11 @@ int main(int argc, char *argv[])
         * guest with invalid state when the handler interrupts KVM with an
         * exception pending.
         */
-       clear_invalid_guest_state();
+       clear_invalid_guest_state(vcpu);
        TEST_ASSERT(signal(SIGALRM, sigalrm_handler) != SIG_ERR,
                    "Failed to register SIGALRM handler, errno = %d (%s)",
                    errno, strerror(errno));
 
        set_timer();
-       run_vcpu_with_invalid_state();
+       run_vcpu_with_invalid_state(vcpu);
 }