Commit 47a7c924 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: selftests: Add vcpu_get() to retrieve and assert on vCPU existence

Add vcpu_get() to wrap vcpu_find() and deduplicate a pile of code that
asserts the requested vCPU exists.
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 21c6ee2b
...@@ -561,23 +561,7 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, ...@@ -561,23 +561,7 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
return &region->region; return &region->region;
} }
/* static struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
* VCPU Find
*
* Input Args:
* vm - Virtual Machine
* vcpuid - VCPU ID
*
* Output Args: None
*
* Return:
* Pointer to VCPU structure
*
* Locates a vcpu structure that describes the VCPU specified by vcpuid and
* returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
* for the specified vcpuid.
*/
struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
{ {
struct vcpu *vcpu; struct vcpu *vcpu;
...@@ -589,6 +573,14 @@ struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid) ...@@ -589,6 +573,14 @@ struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
return NULL; return NULL;
} }
struct vcpu *vcpu_get(struct kvm_vm *vm, uint32_t vcpuid)
{
struct vcpu *vcpu = vcpu_find(vm, vcpuid);
TEST_ASSERT(vcpu, "vCPU %d does not exist", vcpuid);
return vcpu;
}
/* /*
* VM VCPU Remove * VM VCPU Remove
* *
...@@ -1568,8 +1560,7 @@ void vm_create_irqchip(struct kvm_vm *vm) ...@@ -1568,8 +1560,7 @@ void vm_create_irqchip(struct kvm_vm *vm)
*/ */
struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid) struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
return vcpu->state; return vcpu->state;
} }
...@@ -1610,11 +1601,9 @@ int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid) ...@@ -1610,11 +1601,9 @@ int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid) void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
int ret; int ret;
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
vcpu->state->immediate_exit = 1; vcpu->state->immediate_exit = 1;
ret = __vcpu_run(vm, vcpuid); ret = __vcpu_run(vm, vcpuid);
vcpu->state->immediate_exit = 0; vcpu->state->immediate_exit = 0;
...@@ -1656,14 +1645,9 @@ struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vm *vm, uint32_t vcpuid) ...@@ -1656,14 +1645,9 @@ struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vm *vm, uint32_t vcpuid)
int __vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, int __vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
unsigned long cmd, void *arg) unsigned long cmd, void *arg)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
int ret;
TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
ret = ioctl(vcpu->fd, cmd, arg); return ioctl(vcpu->fd, cmd, arg);
return ret;
} }
void _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long cmd, void _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long cmd,
...@@ -1676,15 +1660,11 @@ void _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long cmd, ...@@ -1676,15 +1660,11 @@ void _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long cmd,
void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid) void *vcpu_map_dirty_ring(struct kvm_vm *vm, uint32_t vcpuid)
{ {
struct vcpu *vcpu; struct vcpu *vcpu = vcpu_get(vm, vcpuid);
uint32_t size = vm->dirty_ring_size; uint32_t size = vm->dirty_ring_size;
TEST_ASSERT(size > 0, "Should enable dirty ring first"); TEST_ASSERT(size > 0, "Should enable dirty ring first");
vcpu = vcpu_find(vm, vcpuid);
TEST_ASSERT(vcpu, "Cannot find vcpu %u", vcpuid);
if (!vcpu->dirty_gfns) { if (!vcpu->dirty_gfns) {
void *addr; void *addr;
...@@ -1840,9 +1820,7 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, ...@@ -1840,9 +1820,7 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr,
int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
uint64_t attr) uint64_t attr)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid);
return _kvm_device_check_attr(vcpu->fd, group, attr); return _kvm_device_check_attr(vcpu->fd, group, attr);
} }
...@@ -1859,9 +1837,7 @@ int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, ...@@ -1859,9 +1837,7 @@ int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group, int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
uint64_t attr, void *val, bool write) uint64_t attr, void *val, bool write)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid);
return _kvm_device_access(vcpu->fd, group, attr, val, write); return _kvm_device_access(vcpu->fd, group, attr, val, write);
} }
......
...@@ -69,7 +69,7 @@ struct kvm_vm { ...@@ -69,7 +69,7 @@ struct kvm_vm {
uint32_t dirty_ring_size; uint32_t dirty_ring_size;
}; };
struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid); struct vcpu *vcpu_get(struct kvm_vm *vm, uint32_t vcpuid);
/* /*
* Virtual Translation Tables Dump * Virtual Translation Tables Dump
......
...@@ -208,10 +208,7 @@ void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...) ...@@ -208,10 +208,7 @@ void vcpu_args_set(struct kvm_vm *vm, uint32_t vcpuid, unsigned int num, ...)
void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t indent) void vcpu_dump(FILE *stream, struct kvm_vm *vm, uint32_t vcpuid, uint8_t indent)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
if (!vcpu)
return;
fprintf(stream, "%*spstate: psw: 0x%.16llx:0x%.16llx\n", fprintf(stream, "%*spstate: psw: 0x%.16llx:0x%.16llx\n",
indent, "", vcpu->state->psw_mask, vcpu->state->psw_addr); indent, "", vcpu->state->psw_mask, vcpu->state->psw_addr);
......
...@@ -995,7 +995,7 @@ static int vcpu_save_xsave_state(struct kvm_vm *vm, struct vcpu *vcpu, ...@@ -995,7 +995,7 @@ static int vcpu_save_xsave_state(struct kvm_vm *vm, struct vcpu *vcpu,
struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid) struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
struct kvm_msr_list *list; struct kvm_msr_list *list;
struct kvm_x86_state *state; struct kvm_x86_state *state;
int nmsrs, r, i; int nmsrs, r, i;
...@@ -1078,7 +1078,7 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid) ...@@ -1078,7 +1078,7 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *state) void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_x86_state *state)
{ {
struct vcpu *vcpu = vcpu_find(vm, vcpuid); struct vcpu *vcpu = vcpu_get(vm, vcpuid);
int r; int r;
r = ioctl(vcpu->fd, KVM_SET_SREGS, &state->sregs); r = ioctl(vcpu->fd, KVM_SET_SREGS, &state->sregs);
......
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