Commit 1422efd6 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Return created vcpu from vm_vcpu_add_default()

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: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 9931be3f
......@@ -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];
......
......@@ -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);
......
......@@ -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, ...)
......
......@@ -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, ...)
......
......@@ -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, ...)
......
......@@ -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;
}
/*
......
......@@ -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),
......
......@@ -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;
......
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