]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: selftests: Return created vcpu from vm_vcpu_add_default()
authorSean Christopherson <seanjc@google.com>
Wed, 16 Feb 2022 17:37:11 +0000 (09:37 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sat, 11 Jun 2022 15:47:03 +0000 (11:47 -0400)
Return the created 'struct kvm_vcpu' object from vm_vcpu_add_default(),
which cleans up a few tests and will eventually allow removing vcpu_get()
entirely.

Opportunistically rename @vcpuid to @vcpu_id to follow preferred kernel
style.

Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
tools/testing/selftests/kvm/include/aarch64/processor.h
tools/testing/selftests/kvm/include/kvm_util_base.h
tools/testing/selftests/kvm/lib/aarch64/processor.c
tools/testing/selftests/kvm/lib/riscv/processor.c
tools/testing/selftests/kvm/lib/s390x/processor.c
tools/testing/selftests/kvm/lib/x86_64/processor.c
tools/testing/selftests/kvm/x86_64/pmu_event_filter_test.c
tools/testing/selftests/kvm/x86_64/tsc_scaling_sync.c

index 4d2d474b6874b69582ee4051abd76799693ef159..9dad391b4fecaecc8c23701476402c20e24acb10 100644 (file)
@@ -64,8 +64,9 @@ static inline void set_reg(struct kvm_vm *vm, uint32_t vcpuid, uint64_t id, uint
 }
 
 void aarch64_vcpu_setup(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_vcpu_init *init);
-void aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid,
-                             struct kvm_vcpu_init *init, void *guest_code);
+struct kvm_vcpu *aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpu_id,
+                                         struct kvm_vcpu_init *init,
+                                         void *guest_code);
 
 struct ex_regs {
        u64 regs[31];
index c7abe48d07cbf03c3485079f26de23f73f8b68c4..622b09ec23dda63039e02c7bf5f12449a15ca7ee 100644 (file)
@@ -656,12 +656,14 @@ static inline void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid,
  *   vcpuid - The id of the VCPU to add to the VM.
  *   guest_code - The vCPU's entry point
  */
-void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code);
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
+                                 void *guest_code);
 
-static inline void vm_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid,
-                                      void *guest_code)
+static inline struct kvm_vcpu *vm_vcpu_add_default(struct kvm_vm *vm,
+                                                  uint32_t vcpu_id,
+                                                  void *guest_code)
 {
-       vm_arch_vcpu_add(vm, vcpuid, guest_code);
+       return vm_arch_vcpu_add(vm, vcpu_id, guest_code);
 }
 
 void virt_arch_pgd_alloc(struct kvm_vm *vm);
index d14579176e522079a6dc82428b729c9ee54ee704..2b169b4ec29e5856b1bdea5e5be6aad7b961c22e 100644 (file)
@@ -314,25 +314,29 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t in
                indent, "", pstate, pc);
 }
 
-void aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpuid,
-                             struct kvm_vcpu_init *init, void *guest_code)
+struct kvm_vcpu *aarch64_vcpu_add_default(struct kvm_vm *vm, uint32_t vcpu_id,
+                                         struct kvm_vcpu_init *init,
+                                         void *guest_code)
 {
        size_t stack_size = vm->page_size == 4096 ?
                                        DEFAULT_STACK_PGS * vm->page_size :
                                        vm->page_size;
        uint64_t stack_vaddr = vm_vaddr_alloc(vm, stack_size,
                                              DEFAULT_ARM64_GUEST_STACK_VADDR_MIN);
+       struct kvm_vcpu *vcpu = vm_vcpu_add(vm, vcpu_id);
 
-       vm_vcpu_add(vm, vcpuid);
-       aarch64_vcpu_setup(vm, vcpuid, init);
+       aarch64_vcpu_setup(vm, vcpu_id, init);
 
-       set_reg(vm, vcpuid, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size);
-       set_reg(vm, vcpuid, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
+       set_reg(vm, vcpu_id, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size);
+       set_reg(vm, vcpu_id, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
+
+       return vcpu;
 }
 
-void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
+                                 void *guest_code)
 {
-       aarch64_vcpu_add_default(vm, vcpuid, NULL, guest_code);
+       return aarch64_vcpu_add_default(vm, vcpu_id, NULL, guest_code);
 }
 
 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
index d70d5a4c5ad6bea581b55d33a7c32dea78ad3e32..5946101144eb6d0ea8355811a77f3fed1860bc82 100644 (file)
@@ -274,7 +274,8 @@ static void __aligned(16) guest_unexp_trap(void)
                  0, 0, 0, 0, 0, 0);
 }
 
-void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
+                                 void *guest_code)
 {
        int r;
        size_t stack_size = vm->page_size == 4096 ?
@@ -284,9 +285,10 @@ void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
                                        DEFAULT_RISCV_GUEST_STACK_VADDR_MIN);
        unsigned long current_gp = 0;
        struct kvm_mp_state mps;
+       struct kvm_vcpu *vcpu;
 
-       vm_vcpu_add(vm, vcpuid);
-       riscv_vcpu_mmu_setup(vm, vcpuid);
+       vcpu = vm_vcpu_add(vm, vcpu_id);
+       riscv_vcpu_mmu_setup(vm, vcpu_id);
 
        /*
         * With SBI HSM support in KVM RISC-V, all secondary VCPUs are
@@ -294,23 +296,25 @@ void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
         * are powered-on using KVM_SET_MP_STATE ioctl().
         */
        mps.mp_state = KVM_MP_STATE_RUNNABLE;
-       r = __vcpu_ioctl(vm, vcpuid, KVM_SET_MP_STATE, &mps);
+       r = __vcpu_ioctl(vm, vcpu_id, KVM_SET_MP_STATE, &mps);
        TEST_ASSERT(!r, "IOCTL KVM_SET_MP_STATE failed (error %d)", r);
 
        /* Setup global pointer of guest to be same as the host */
        asm volatile (
                "add %0, gp, zero" : "=r" (current_gp) : : "memory");
-       set_reg(vm, vcpuid, RISCV_CORE_REG(regs.gp), current_gp);
+       set_reg(vm, vcpu_id, RISCV_CORE_REG(regs.gp), current_gp);
 
        /* Setup stack pointer and program counter of guest */
-       set_reg(vm, vcpuid, RISCV_CORE_REG(regs.sp),
+       set_reg(vm, vcpu_id, RISCV_CORE_REG(regs.sp),
                stack_vaddr + stack_size);
-       set_reg(vm, vcpuid, RISCV_CORE_REG(regs.pc),
+       set_reg(vm, vcpu_id, RISCV_CORE_REG(regs.pc),
                (unsigned long)guest_code);
 
        /* Setup default exception vector of guest */
-       set_reg(vm, vcpuid, RISCV_CSR_REG(stvec),
+       set_reg(vm, vcpu_id, RISCV_CSR_REG(stvec),
                (unsigned long)guest_unexp_trap);
+
+       return vcpu;
 }
 
 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
index c2fe56a3fb747db3c61529f725070dc01e5ceff6..cf759844b226b4a0dede08aa2b356f069150d4e7 100644 (file)
@@ -154,12 +154,14 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
        virt_dump_region(stream, vm, indent, vm->pgd);
 }
 
-void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
+                                 void *guest_code)
 {
        size_t stack_size =  DEFAULT_STACK_PGS * getpagesize();
        uint64_t stack_vaddr;
        struct kvm_regs regs;
        struct kvm_sregs sregs;
+       struct kvm_vcpu *vcpu;
        struct kvm_run *run;
 
        TEST_ASSERT(vm->page_size == 4096, "Unsupported page size: 0x%x",
@@ -168,21 +170,23 @@ void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
        stack_vaddr = vm_vaddr_alloc(vm, stack_size,
                                     DEFAULT_GUEST_STACK_VADDR_MIN);
 
-       vm_vcpu_add(vm, vcpuid);
+       vcpu = vm_vcpu_add(vm, vcpu_id);
 
        /* Setup guest registers */
-       vcpu_regs_get(vm, vcpuid, &regs);
+       vcpu_regs_get(vm, vcpu_id, &regs);
        regs.gprs[15] = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize()) - 160;
-       vcpu_regs_set(vm, vcpuid, &regs);
+       vcpu_regs_set(vm, vcpu_id, &regs);
 
-       vcpu_sregs_get(vm, vcpuid, &sregs);
+       vcpu_sregs_get(vm, vcpu_id, &sregs);
        sregs.crs[0] |= 0x00040000;             /* Enable floating point regs */
        sregs.crs[1] = vm->pgd | 0xf;           /* Primary region table */
-       vcpu_sregs_set(vm, vcpuid, &sregs);
+       vcpu_sregs_set(vm, vcpu_id, &sregs);
 
-       run = vcpu_state(vm, vcpuid);
+       run = vcpu_state(vm, vcpu_id);
        run->psw_mask = 0x0400000180000000ULL;  /* DAT enabled + 64 bit mode */
        run->psw_addr = (uintptr_t)guest_code;
+
+       return vcpu;
 }
 
 void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
index 1bc406b6317dada06829e88f4e7e9d3c3e4a3efb..bafa8ec5456908ed5dcb3ffe4d9315eb01838fb6 100644 (file)
@@ -632,29 +632,33 @@ void vm_xsave_req_perm(int bit)
                    bitmask);
 }
 
-void vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, void *guest_code)
+struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
+                                 void *guest_code)
 {
        struct kvm_mp_state mp_state;
        struct kvm_regs regs;
        vm_vaddr_t stack_vaddr;
+       struct kvm_vcpu *vcpu;
+
        stack_vaddr = vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(),
                                     DEFAULT_GUEST_STACK_VADDR_MIN);
 
-       /* Create VCPU */
-       vm_vcpu_add(vm, vcpuid);
-       vcpu_set_cpuid(vm, vcpuid, kvm_get_supported_cpuid());
-       vcpu_setup(vm, vcpuid);
+       vcpu = vm_vcpu_add(vm, vcpu_id);
+       vcpu_set_cpuid(vm, vcpu_id, kvm_get_supported_cpuid());
+       vcpu_setup(vm, vcpu_id);
 
        /* Setup guest general purpose registers */
-       vcpu_regs_get(vm, vcpuid, &regs);
+       vcpu_regs_get(vm, vcpu_id, &regs);
        regs.rflags = regs.rflags | 0x2;
        regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize());
        regs.rip = (unsigned long) guest_code;
-       vcpu_regs_set(vm, vcpuid, &regs);
+       vcpu_regs_set(vm, vcpu_id, &regs);
 
        /* Setup the MP state */
        mp_state.mp_state = 0;
-       vcpu_mp_state_set(vm, vcpuid, &mp_state);
+       vcpu_mp_state_set(vm, vcpu_id, &mp_state);
+
+       return vcpu;
 }
 
 /*
index d2c571f2052146f9b9535ef284b3c7b2a3c3d8b4..2faa43336131d99d14d4dbb0eab8df9b6590133d 100644 (file)
@@ -369,10 +369,8 @@ static void test_pmu_config_disable(void (*guest_code)(void))
 
        vm_enable_cap(vm, KVM_CAP_PMU_CAPABILITY, KVM_PMU_CAP_DISABLE);
 
-       vm_vcpu_add_default(vm, 0, guest_code);
+       vcpu = vm_vcpu_add_default(vm, 0, guest_code);
        vm_init_descriptor_tables(vm);
-
-       vcpu = vcpu_get(vm, 0);
        vcpu_init_descriptor_tables(vm, vcpu->id);
 
        TEST_ASSERT(!sanity_check_pmu(vcpu),
index b7cd5c47fc537fcfd443c8fd63df5b6d9e4698c8..ea70ca2e63c3c7c6d5489740aceef84fc61cec30 100644 (file)
@@ -54,8 +54,7 @@ static void *run_vcpu(void *_cpu_nr)
        /* The kernel is fine, but vm_vcpu_add_default() needs locking */
        pthread_spin_lock(&create_lock);
 
-       vm_vcpu_add_default(vm, vcpu_id, guest_code);
-       vcpu = vcpu_get(vm, vcpu_id);
+       vcpu = vm_vcpu_add_default(vm, vcpu_id, guest_code);
 
        if (!first_cpu_done) {
                first_cpu_done = true;