Skip to content

Commit 8ef1926

Browse files
committed
KVM: selftests: Move setting a vCPU's entry point to a dedicated API
Extract the code to set a vCPU's entry point out of vm_arch_vcpu_add() and into a new API, vcpu_arch_set_entry_point(). Providing a separate API will allow creating a KVM selftests hardness that can handle tests that use different entry points for sub-tests, whereas *requiring* the entry point to be specified at vCPU creation makes it difficult to create a generic harness, e.g. the boilerplate setup/teardown can't easily create and destroy the VM and vCPUs. Signed-off-by: Thomas Huth <thuth@redhat.com> Link: https://lore.kernel.org/r/20240208204844.119326-4-thuth@redhat.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 221d654 commit 8ef1926

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

tools/testing/selftests/kvm/include/kvm_util_base.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,15 +969,18 @@ static inline void vcpu_dump(FILE *stream, struct kvm_vcpu *vcpu,
969969
* Input Args:
970970
* vm - Virtual Machine
971971
* vcpu_id - The id of the VCPU to add to the VM.
972-
* guest_code - The vCPU's entry point
973972
*/
974-
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
975-
void *guest_code);
973+
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id);
974+
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code);
976975

977976
static inline struct kvm_vcpu *vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
978977
void *guest_code)
979978
{
980-
return vm_arch_vcpu_add(vm, vcpu_id, guest_code);
979+
struct kvm_vcpu *vcpu = vm_arch_vcpu_add(vm, vcpu_id);
980+
981+
vcpu_arch_set_entry_point(vcpu, guest_code);
982+
983+
return vcpu;
981984
}
982985

983986
/* Re-create a vCPU after restarting a VM, e.g. for state save/restore tests. */

tools/testing/selftests/kvm/lib/aarch64/processor.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,13 @@ void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu, uint8_t indent)
365365
indent, "", pstate, pc);
366366
}
367367

368-
struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
369-
struct kvm_vcpu_init *init, void *guest_code)
368+
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
369+
{
370+
vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
371+
}
372+
373+
static struct kvm_vcpu *__aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
374+
struct kvm_vcpu_init *init)
370375
{
371376
size_t stack_size;
372377
uint64_t stack_vaddr;
@@ -381,15 +386,21 @@ struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
381386
aarch64_vcpu_setup(vcpu, init);
382387

383388
vcpu_set_reg(vcpu, ARM64_CORE_REG(sp_el1), stack_vaddr + stack_size);
384-
vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (uint64_t)guest_code);
389+
}
390+
391+
struct kvm_vcpu *aarch64_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
392+
struct kvm_vcpu_init *init, void *guest_code)
393+
{
394+
struct kvm_vcpu *vcpu = __aarch64_vcpu_add(vm, vcpu_id, init);
395+
396+
vcpu_arch_set_entry_point(vcpu, guest_code);
385397

386398
return vcpu;
387399
}
388400

389-
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
390-
void *guest_code)
401+
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
391402
{
392-
return aarch64_vcpu_add(vm, vcpu_id, NULL, guest_code);
403+
return __aarch64_vcpu_add(vm, vcpu_id, NULL);
393404
}
394405

395406
void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...)

tools/testing/selftests/kvm/lib/riscv/processor.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,12 @@ static void __aligned(16) guest_unexp_trap(void)
277277
0, 0, 0, 0, 0, 0);
278278
}
279279

280-
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
281-
void *guest_code)
280+
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
281+
{
282+
vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), (unsigned long)guest_code);
283+
}
284+
285+
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
282286
{
283287
int r;
284288
size_t stack_size;
@@ -312,7 +316,6 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
312316

313317
/* Setup stack pointer and program counter of guest */
314318
vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.sp), stack_vaddr + stack_size);
315-
vcpu_set_reg(vcpu, RISCV_CORE_REG(regs.pc), (unsigned long)guest_code);
316319

317320
/* Setup default exception vector of guest */
318321
vcpu_set_reg(vcpu, RISCV_GENERAL_CSR_REG(stvec), (unsigned long)guest_unexp_trap);

tools/testing/selftests/kvm/lib/s390x/processor.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,18 @@ void virt_arch_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
155155
virt_dump_region(stream, vm, indent, vm->pgd);
156156
}
157157

158-
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
159-
void *guest_code)
158+
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
159+
{
160+
vcpu->run->psw_addr = (uintptr_t)guest_code;
161+
}
162+
163+
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
160164
{
161165
size_t stack_size = DEFAULT_STACK_PGS * getpagesize();
162166
uint64_t stack_vaddr;
163167
struct kvm_regs regs;
164168
struct kvm_sregs sregs;
165169
struct kvm_vcpu *vcpu;
166-
struct kvm_run *run;
167170

168171
TEST_ASSERT(vm->page_size == 4096, "Unsupported page size: 0x%x",
169172
vm->page_size);
@@ -184,9 +187,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
184187
sregs.crs[1] = vm->pgd | 0xf; /* Primary region table */
185188
vcpu_sregs_set(vcpu, &sregs);
186189

187-
run = vcpu->run;
188-
run->psw_mask = 0x0400000180000000ULL; /* DAT enabled + 64 bit mode */
189-
run->psw_addr = (uintptr_t)guest_code;
190+
vcpu->run->psw_mask = 0x0400000180000000ULL; /* DAT enabled + 64 bit mode */
190191

191192
return vcpu;
192193
}

tools/testing/selftests/kvm/lib/x86_64/processor.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,16 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
562562
sync_global_to_guest(vm, host_cpu_is_amd);
563563
}
564564

565-
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
566-
void *guest_code)
565+
void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)
566+
{
567+
struct kvm_regs regs;
568+
569+
vcpu_regs_get(vcpu, &regs);
570+
regs.rip = (unsigned long) guest_code;
571+
vcpu_regs_set(vcpu, &regs);
572+
}
573+
574+
struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
567575
{
568576
struct kvm_mp_state mp_state;
569577
struct kvm_regs regs;
@@ -597,7 +605,6 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
597605
vcpu_regs_get(vcpu, &regs);
598606
regs.rflags = regs.rflags | 0x2;
599607
regs.rsp = stack_vaddr;
600-
regs.rip = (unsigned long) guest_code;
601608
vcpu_regs_set(vcpu, &regs);
602609

603610
/* Setup the MP state */

0 commit comments

Comments
 (0)