Merge branch 'kvm-amd-fixes' into HEAD
[linux-block.git] / tools / testing / selftests / kvm / lib / kvm_util.c
index 8a3523d4434fb3fa5887239bc9e70ee7c9992ff4..c9cede5c7d0de63480cf62bbbdeaeca7d083ed88 100644 (file)
@@ -161,6 +161,9 @@ struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
        vm = calloc(1, sizeof(*vm));
        TEST_ASSERT(vm != NULL, "Insufficient Memory");
 
+       INIT_LIST_HEAD(&vm->vcpus);
+       INIT_LIST_HEAD(&vm->userspace_mem_regions);
+
        vm->mode = mode;
        vm->type = 0;
 
@@ -258,8 +261,7 @@ void kvm_vm_restart(struct kvm_vm *vmp, int perm)
        if (vmp->has_irqchip)
                vm_create_irqchip(vmp);
 
-       for (region = vmp->userspace_mem_region_head; region;
-               region = region->next) {
+       list_for_each_entry(region, &vmp->userspace_mem_regions, list) {
                int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
                TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
                            "  rc: %i errno: %i\n"
@@ -319,8 +321,7 @@ userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
 {
        struct userspace_mem_region *region;
 
-       for (region = vm->userspace_mem_region_head; region;
-               region = region->next) {
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
                uint64_t existing_start = region->region.guest_phys_addr;
                uint64_t existing_end = region->region.guest_phys_addr
                        + region->region.memory_size - 1;
@@ -378,11 +379,11 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
  */
 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
 {
-       struct vcpu *vcpup;
+       struct vcpu *vcpu;
 
-       for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
-               if (vcpup->id == vcpuid)
-                       return vcpup;
+       list_for_each_entry(vcpu, &vm->vcpus, list) {
+               if (vcpu->id == vcpuid)
+                       return vcpu;
        }
 
        return NULL;
@@ -392,18 +393,16 @@ struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
  * VM VCPU Remove
  *
  * Input Args:
- *   vm - Virtual Machine
- *   vcpuid - VCPU ID
+ *   vcpu - VCPU to remove
  *
  * Output Args: None
  *
  * Return: None, TEST_ASSERT failures for all error conditions
  *
- * Within the VM specified by vm, removes the VCPU given by vcpuid.
+ * Removes a vCPU from a VM and frees its resources.
  */
-static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
+static void vm_vcpu_rm(struct vcpu *vcpu)
 {
-       struct vcpu *vcpu = vcpu_find(vm, vcpuid);
        int ret;
 
        ret = munmap(vcpu->state, sizeof(*vcpu->state));
@@ -413,21 +412,17 @@ static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
        TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
                "errno: %i", ret, errno);
 
-       if (vcpu->next)
-               vcpu->next->prev = vcpu->prev;
-       if (vcpu->prev)
-               vcpu->prev->next = vcpu->next;
-       else
-               vm->vcpu_head = vcpu->next;
+       list_del(&vcpu->list);
        free(vcpu);
 }
 
 void kvm_vm_release(struct kvm_vm *vmp)
 {
+       struct vcpu *vcpu, *tmp;
        int ret;
 
-       while (vmp->vcpu_head)
-               vm_vcpu_rm(vmp, vmp->vcpu_head->id);
+       list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list)
+               vm_vcpu_rm(vcpu);
 
        ret = close(vmp->fd);
        TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
@@ -438,35 +433,38 @@ void kvm_vm_release(struct kvm_vm *vmp)
                "  vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
 }
 
+static void __vm_mem_region_delete(struct kvm_vm *vm,
+                                  struct userspace_mem_region *region)
+{
+       int ret;
+
+       list_del(&region->list);
+
+       region->region.memory_size = 0;
+       ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
+       TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
+                   "rc: %i errno: %i", ret, errno);
+
+       sparsebit_free(&region->unused_phy_pages);
+       ret = munmap(region->mmap_start, region->mmap_size);
+       TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i", ret, errno);
+
+       free(region);
+}
+
 /*
  * Destroys and frees the VM pointed to by vmp.
  */
 void kvm_vm_free(struct kvm_vm *vmp)
 {
-       int ret;
+       struct userspace_mem_region *region, *tmp;
 
        if (vmp == NULL)
                return;
 
        /* Free userspace_mem_regions. */
-       while (vmp->userspace_mem_region_head) {
-               struct userspace_mem_region *region
-                       = vmp->userspace_mem_region_head;
-
-               region->region.memory_size = 0;
-               ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
-                       &region->region);
-               TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
-                       "rc: %i errno: %i", ret, errno);
-
-               vmp->userspace_mem_region_head = region->next;
-               sparsebit_free(&region->unused_phy_pages);
-               ret = munmap(region->mmap_start, region->mmap_size);
-               TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
-                           ret, errno);
-
-               free(region);
-       }
+       list_for_each_entry_safe(region, tmp, &vmp->userspace_mem_regions, list)
+               __vm_mem_region_delete(vmp, region);
 
        /* Free sparsebit arrays. */
        sparsebit_free(&vmp->vpages_valid);
@@ -612,12 +610,10 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                        (uint64_t) region->region.memory_size);
 
        /* Confirm no region with the requested slot already exists. */
-       for (region = vm->userspace_mem_region_head; region;
-               region = region->next) {
-               if (region->region.slot == slot)
-                       break;
-       }
-       if (region != NULL)
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
+               if (region->region.slot != slot)
+                       continue;
+
                TEST_FAIL("A mem region with the requested slot "
                        "already exists.\n"
                        "  requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
@@ -626,6 +622,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                        region->region.slot,
                        (uint64_t) region->region.guest_phys_addr,
                        (uint64_t) region->region.memory_size);
+       }
 
        /* Allocate and initialize new mem region structure. */
        region = calloc(1, sizeof(*region));
@@ -686,10 +683,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm,
                guest_paddr, (uint64_t) region->region.memory_size);
 
        /* Add to linked-list of memory regions. */
-       if (vm->userspace_mem_region_head)
-               vm->userspace_mem_region_head->prev = region;
-       region->next = vm->userspace_mem_region_head;
-       vm->userspace_mem_region_head = region;
+       list_add(&region->list, &vm->userspace_mem_regions);
 }
 
 /*
@@ -712,20 +706,17 @@ memslot2region(struct kvm_vm *vm, uint32_t memslot)
 {
        struct userspace_mem_region *region;
 
-       for (region = vm->userspace_mem_region_head; region;
-               region = region->next) {
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
                if (region->region.slot == memslot)
-                       break;
-       }
-       if (region == NULL) {
-               fprintf(stderr, "No mem region with the requested slot found,\n"
-                       "  requested slot: %u\n", memslot);
-               fputs("---- vm dump ----\n", stderr);
-               vm_dump(stderr, vm, 2);
-               TEST_FAIL("Mem region not found");
+                       return region;
        }
 
-       return region;
+       fprintf(stderr, "No mem region with the requested slot found,\n"
+               "  requested slot: %u\n", memslot);
+       fputs("---- vm dump ----\n", stderr);
+       vm_dump(stderr, vm, 2);
+       TEST_FAIL("Mem region not found");
+       return NULL;
 }
 
 /*
@@ -788,6 +779,24 @@ void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
                    ret, errno, slot, new_gpa);
 }
 
+/*
+ * VM Memory Region Delete
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   slot - Slot of the memory region to delete
+ *
+ * Output Args: None
+ *
+ * Return: None
+ *
+ * Delete a memory region.
+ */
+void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)
+{
+       __vm_mem_region_delete(vm, memslot2region(vm, slot));
+}
+
 /*
  * VCPU mmap Size
  *
@@ -863,10 +872,7 @@ void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
                "vcpu id: %u errno: %i", vcpuid, errno);
 
        /* Add to linked-list of VCPUs. */
-       if (vm->vcpu_head)
-               vm->vcpu_head->prev = vcpu;
-       vcpu->next = vm->vcpu_head;
-       vm->vcpu_head = vcpu;
+       list_add(&vcpu->list, &vm->vcpus);
 }
 
 /*
@@ -1059,8 +1065,8 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
 {
        struct userspace_mem_region *region;
-       for (region = vm->userspace_mem_region_head; region;
-            region = region->next) {
+
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
                if ((gpa >= region->region.guest_phys_addr)
                        && (gpa <= (region->region.guest_phys_addr
                                + region->region.memory_size - 1)))
@@ -1092,8 +1098,8 @@ void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
 {
        struct userspace_mem_region *region;
-       for (region = vm->userspace_mem_region_head; region;
-            region = region->next) {
+
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
                if ((hva >= region->host_mem)
                        && (hva <= (region->host_mem
                                + region->region.memory_size - 1)))
@@ -1201,6 +1207,15 @@ void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
                    ret, errno);
 }
 
+void vcpu_set_guest_debug(struct kvm_vm *vm, uint32_t vcpuid,
+                         struct kvm_guest_debug *debug)
+{
+       struct vcpu *vcpu = vcpu_find(vm, vcpuid);
+       int ret = ioctl(vcpu->fd, KVM_SET_GUEST_DEBUG, debug);
+
+       TEST_ASSERT(ret == 0, "KVM_SET_GUEST_DEBUG failed: %d", ret);
+}
+
 /*
  * VM VCPU Set MP State
  *
@@ -1520,8 +1535,7 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
        fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
        fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
        fprintf(stream, "%*sMem Regions:\n", indent, "");
-       for (region = vm->userspace_mem_region_head; region;
-               region = region->next) {
+       list_for_each_entry(region, &vm->userspace_mem_regions, list) {
                fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
                        "host_virt: %p\n", indent + 2, "",
                        (uint64_t) region->region.guest_phys_addr,
@@ -1540,7 +1554,7 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
                virt_dump(stream, vm, indent + 4);
        }
        fprintf(stream, "%*sVCPUs:\n", indent, "");
-       for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
+       list_for_each_entry(vcpu, &vm->vcpus, list)
                vcpu_dump(stream, vm, vcpu->id, indent + 2);
 }
 
@@ -1734,6 +1748,11 @@ unsigned int vm_get_max_gfn(struct kvm_vm *vm)
        return vm->max_gfn;
 }
 
+int vm_get_fd(struct kvm_vm *vm)
+{
+       return vm->fd;
+}
+
 static unsigned int vm_calc_num_pages(unsigned int num_pages,
                                      unsigned int page_shift,
                                      unsigned int new_page_shift,