Lines Matching +full:memory +full:- +full:region
1 // SPDX-License-Identifier: GPL-2.0-only
39 * flags - The flags to pass when opening KVM_DEV_PATH.
102 * cap - Capability
134 vm->dirty_ring_size = ring_size; in vm_enable_dirty_ring()
139 vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR); in vm_open()
143 vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type); in vm_open()
144 TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd)); in vm_open()
150 [VM_MODE_P52V48_4K] = "PA-bits:52, VA-bits:48, 4K pages", in vm_guest_mode_string()
151 [VM_MODE_P52V48_64K] = "PA-bits:52, VA-bits:48, 64K pages", in vm_guest_mode_string()
152 [VM_MODE_P48V48_4K] = "PA-bits:48, VA-bits:48, 4K pages", in vm_guest_mode_string()
153 [VM_MODE_P48V48_16K] = "PA-bits:48, VA-bits:48, 16K pages", in vm_guest_mode_string()
154 [VM_MODE_P48V48_64K] = "PA-bits:48, VA-bits:48, 64K pages", in vm_guest_mode_string()
155 [VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages", in vm_guest_mode_string()
156 [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages", in vm_guest_mode_string()
157 [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages", in vm_guest_mode_string()
158 [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages", in vm_guest_mode_string()
159 [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages", in vm_guest_mode_string()
160 [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages", in vm_guest_mode_string()
161 [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages", in vm_guest_mode_string()
162 [VM_MODE_P36V48_16K] = "PA-bits:36, VA-bits:48, 16K pages", in vm_guest_mode_string()
163 [VM_MODE_P36V48_64K] = "PA-bits:36, VA-bits:48, 64K pages", in vm_guest_mode_string()
164 [VM_MODE_P36V47_16K] = "PA-bits:36, VA-bits:47, 16K pages", in vm_guest_mode_string()
195 * Initializes vm->vpages_valid to match the canonical VA space of the
199 * range addressed by a single page table into a low and high region
201 * the VA region spans [0, 2^(va_bits - 1)), [-(2^(va_bits - 1), -1].
205 sparsebit_set_num(vm->vpages_valid, in vm_vaddr_populate_bitmap()
206 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift); in vm_vaddr_populate_bitmap()
207 sparsebit_set_num(vm->vpages_valid, in vm_vaddr_populate_bitmap()
208 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift, in vm_vaddr_populate_bitmap()
209 (1ULL << (vm->va_bits - 1)) >> vm->page_shift); in vm_vaddr_populate_bitmap()
217 TEST_ASSERT(vm != NULL, "Insufficient Memory"); in ____vm_create()
219 INIT_LIST_HEAD(&vm->vcpus); in ____vm_create()
220 vm->regions.gpa_tree = RB_ROOT; in ____vm_create()
221 vm->regions.hva_tree = RB_ROOT; in ____vm_create()
222 hash_init(vm->regions.slot_hash); in ____vm_create()
224 vm->mode = mode; in ____vm_create()
225 vm->type = 0; in ____vm_create()
227 vm->pa_bits = vm_guest_mode_params[mode].pa_bits; in ____vm_create()
228 vm->va_bits = vm_guest_mode_params[mode].va_bits; in ____vm_create()
229 vm->page_size = vm_guest_mode_params[mode].page_size; in ____vm_create()
230 vm->page_shift = vm_guest_mode_params[mode].page_shift; in ____vm_create()
233 switch (vm->mode) { in ____vm_create()
235 vm->pgtable_levels = 4; in ____vm_create()
238 vm->pgtable_levels = 3; in ____vm_create()
241 vm->pgtable_levels = 4; in ____vm_create()
244 vm->pgtable_levels = 3; in ____vm_create()
248 vm->pgtable_levels = 4; in ____vm_create()
252 vm->pgtable_levels = 3; in ____vm_create()
257 vm->pgtable_levels = 4; in ____vm_create()
260 vm->pgtable_levels = 3; in ____vm_create()
264 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits); in ____vm_create()
266 * Ignore KVM support for 5-level paging (vm->va_bits == 57), in ____vm_create()
270 TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57, in ____vm_create()
272 vm->va_bits); in ____vm_create()
274 vm->pa_bits); in ____vm_create()
275 vm->pgtable_levels = 4; in ____vm_create()
276 vm->va_bits = 48; in ____vm_create()
278 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms"); in ____vm_create()
282 vm->pgtable_levels = 5; in ____vm_create()
285 vm->pgtable_levels = 5; in ____vm_create()
292 if (vm->pa_bits != 40) in ____vm_create()
293 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); in ____vm_create()
298 /* Limit to VA-bit canonical virtual addresses. */ in ____vm_create()
299 vm->vpages_valid = sparsebit_alloc(); in ____vm_create()
302 /* Limit physical addresses to PA-bits. */ in ____vm_create()
303 vm->max_gfn = vm_compute_max_gfn(vm); in ____vm_create()
305 /* Allocate and setup memory for guest. */ in ____vm_create()
306 vm->vpages_mapped = sparsebit_alloc(); in ____vm_create()
322 "nr_vcpus = %d too large for host, max-vcpus = %d", in vm_nr_pages_required()
327 * test code and other per-VM assets that will be loaded into memslot0. in vm_nr_pages_required()
331 /* Account for the per-vCPU stacks on behalf of the test. */ in vm_nr_pages_required()
336 * maximum page table size for a memory region will be when the in vm_nr_pages_required()
366 vm->memslots[i] = 0; in __vm_create()
373 * read-only memslots as MMIO, and creating a read-only memslot for the in __vm_create()
374 * MMIO region would prevent silently clobbering the MMIO region. in __vm_create()
377 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size); in __vm_create()
388 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
389 * nr_vcpus - VCPU count
390 * extra_mem_pages - Non-slot0 physical memory total size
391 * guest_code - Guest entry point
392 * vcpuids - VCPU IDs
401 * no real memory allocation for non-slot0 memory in this function.
438 * vm - VM that has been released before
443 * global state, such as the irqchip and the memory regions that are mapped
449 struct userspace_mem_region *region; in kvm_vm_restart() local
452 if (vmp->has_irqchip) in kvm_vm_restart()
455 hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) { in kvm_vm_restart()
456 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region); in kvm_vm_restart()
461 ret, errno, region->region.slot, in kvm_vm_restart()
462 region->region.flags, in kvm_vm_restart()
463 region->region.guest_phys_addr, in kvm_vm_restart()
464 region->region.memory_size); in kvm_vm_restart()
505 printf(" -c: Pin tasks to physical CPUs. Takes a list of comma separated\n" in kvm_print_vcpu_pinning_help()
512 " %s -v 3 -c 22,23,24,50\n\n" in kvm_print_vcpu_pinning_help()
514 " %s -v 3 -c 22,23,24\n\n" in kvm_print_vcpu_pinning_help()
552 * Userspace Memory Region Find
555 * vm - Virtual Machine
556 * start - Starting VM physical address
557 * end - Ending VM physical address, inclusive.
562 * Pointer to overlapping region, NULL if no such region.
564 * Searches for a region with any physical memory that overlaps with
568 * region exists.
575 for (node = vm->regions.gpa_tree.rb_node; node; ) { in userspace_mem_region_find()
576 struct userspace_mem_region *region = in userspace_mem_region_find() local
578 uint64_t existing_start = region->region.guest_phys_addr; in userspace_mem_region_find()
579 uint64_t existing_end = region->region.guest_phys_addr in userspace_mem_region_find()
580 + region->region.memory_size - 1; in userspace_mem_region_find()
582 return region; in userspace_mem_region_find()
585 node = node->rb_left; in userspace_mem_region_find()
587 node = node->rb_right; in userspace_mem_region_find()
594 * KVM Userspace Memory Region Find
597 * vm - Virtual Machine
598 * start - Starting VM physical address
599 * end - Ending VM physical address, inclusive.
604 * Pointer to overlapping region, NULL if no such region.
607 * the memslot datastructure for a given range of guest physical memory.
613 struct userspace_mem_region *region; in kvm_userspace_memory_region_find() local
615 region = userspace_mem_region_find(vm, start, end); in kvm_userspace_memory_region_find()
616 if (!region) in kvm_userspace_memory_region_find()
619 return ®ion->region; in kvm_userspace_memory_region_find()
631 * vcpu - VCPU to remove
643 if (vcpu->dirty_gfns) { in vm_vcpu_rm()
644 ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size); in vm_vcpu_rm()
646 vcpu->dirty_gfns = NULL; in vm_vcpu_rm()
649 ret = munmap(vcpu->run, vcpu_mmap_sz()); in vm_vcpu_rm()
652 ret = close(vcpu->fd); in vm_vcpu_rm()
655 list_del(&vcpu->list); in vm_vcpu_rm()
666 list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list) in kvm_vm_release()
669 ret = close(vmp->fd); in kvm_vm_release()
672 ret = close(vmp->kvm_fd); in kvm_vm_release()
677 struct userspace_mem_region *region, in __vm_mem_region_delete() argument
683 rb_erase(®ion->gpa_node, &vm->regions.gpa_tree); in __vm_mem_region_delete()
684 rb_erase(®ion->hva_node, &vm->regions.hva_tree); in __vm_mem_region_delete()
685 hash_del(®ion->slot_node); in __vm_mem_region_delete()
688 region->region.memory_size = 0; in __vm_mem_region_delete()
689 vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); in __vm_mem_region_delete()
691 sparsebit_free(®ion->unused_phy_pages); in __vm_mem_region_delete()
692 ret = munmap(region->mmap_start, region->mmap_size); in __vm_mem_region_delete()
694 if (region->fd >= 0) { in __vm_mem_region_delete()
695 /* There's an extra map when using shared memory. */ in __vm_mem_region_delete()
696 ret = munmap(region->mmap_alias, region->mmap_size); in __vm_mem_region_delete()
698 close(region->fd); in __vm_mem_region_delete()
701 free(region); in __vm_mem_region_delete()
711 struct userspace_mem_region *region; in kvm_vm_free() local
717 if (vmp->stats_fd) { in kvm_vm_free()
718 free(vmp->stats_desc); in kvm_vm_free()
719 close(vmp->stats_fd); in kvm_vm_free()
723 hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node) in kvm_vm_free()
724 __vm_mem_region_delete(vmp, region, false); in kvm_vm_free()
727 sparsebit_free(&vmp->vpages_valid); in kvm_vm_free()
728 sparsebit_free(&vmp->vpages_mapped); in kvm_vm_free()
745 TEST_ASSERT(fd != -1, __KVM_SYSCALL_ERROR("memfd_create()", fd)); in kvm_memfd_alloc()
757 * Memory Compare, host virtual to guest virtual
760 * hva - Starting host virtual address
761 * vm - Virtual Machine
762 * gva - Starting guest virtual address
763 * len - number of bytes to compare
800 amt = len - offset; in kvm_memcmp_hva_gva()
801 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift)) in kvm_memcmp_hva_gva()
802 amt = vm->page_size - (ptr1 % vm->page_size); in kvm_memcmp_hva_gva()
803 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift)) in kvm_memcmp_hva_gva()
804 amt = vm->page_size - (ptr2 % vm->page_size); in kvm_memcmp_hva_gva()
806 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift)); in kvm_memcmp_hva_gva()
807 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift)); in kvm_memcmp_hva_gva()
820 * No mismatch found. Let the caller know the two memory in kvm_memcmp_hva_gva()
827 struct userspace_mem_region *region) in vm_userspace_mem_region_gpa_insert() argument
831 for (cur = &gpa_tree->rb_node, parent = NULL; *cur; ) { in vm_userspace_mem_region_gpa_insert()
836 if (region->region.guest_phys_addr < in vm_userspace_mem_region_gpa_insert()
837 cregion->region.guest_phys_addr) in vm_userspace_mem_region_gpa_insert()
838 cur = &(*cur)->rb_left; in vm_userspace_mem_region_gpa_insert()
840 TEST_ASSERT(region->region.guest_phys_addr != in vm_userspace_mem_region_gpa_insert()
841 cregion->region.guest_phys_addr, in vm_userspace_mem_region_gpa_insert()
842 "Duplicate GPA in region tree"); in vm_userspace_mem_region_gpa_insert()
844 cur = &(*cur)->rb_right; in vm_userspace_mem_region_gpa_insert()
848 rb_link_node(®ion->gpa_node, parent, cur); in vm_userspace_mem_region_gpa_insert()
849 rb_insert_color(®ion->gpa_node, gpa_tree); in vm_userspace_mem_region_gpa_insert()
853 struct userspace_mem_region *region) in vm_userspace_mem_region_hva_insert() argument
857 for (cur = &hva_tree->rb_node, parent = NULL; *cur; ) { in vm_userspace_mem_region_hva_insert()
862 if (region->host_mem < cregion->host_mem) in vm_userspace_mem_region_hva_insert()
863 cur = &(*cur)->rb_left; in vm_userspace_mem_region_hva_insert()
865 TEST_ASSERT(region->host_mem != in vm_userspace_mem_region_hva_insert()
866 cregion->host_mem, in vm_userspace_mem_region_hva_insert()
867 "Duplicate HVA in region tree"); in vm_userspace_mem_region_hva_insert()
869 cur = &(*cur)->rb_right; in vm_userspace_mem_region_hva_insert()
873 rb_link_node(®ion->hva_node, parent, cur); in vm_userspace_mem_region_hva_insert()
874 rb_insert_color(®ion->hva_node, hva_tree); in vm_userspace_mem_region_hva_insert()
881 struct kvm_userspace_memory_region region = { in __vm_set_user_memory_region() local
889 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion); in __vm_set_user_memory_region()
902 * VM Userspace Memory Region Add
905 * vm - Virtual Machine
906 * src_type - Storage source for this region.
907 * NULL to use anonymous memory.
908 * guest_paddr - Starting guest physical address
909 * slot - KVM region slot
910 * npages - Number of physical pages
911 * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
917 * Allocates a memory area of the number of pages specified by npages
919 * given by guest_paddr. The region is created with a KVM region slot
921 * region is created with the flags given by flags.
929 struct userspace_mem_region *region; in vm_userspace_mem_region_add() local
933 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages, in vm_userspace_mem_region_add()
935 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages)); in vm_userspace_mem_region_add()
937 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical " in vm_userspace_mem_region_add()
939 " guest_paddr: 0x%lx vm->page_size: 0x%x", in vm_userspace_mem_region_add()
940 guest_paddr, vm->page_size); in vm_userspace_mem_region_add()
941 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1) in vm_userspace_mem_region_add()
942 <= vm->max_gfn, "Physical range beyond maximum " in vm_userspace_mem_region_add()
945 " vm->max_gfn: 0x%lx vm->page_size: 0x%x", in vm_userspace_mem_region_add()
946 guest_paddr, npages, vm->max_gfn, vm->page_size); in vm_userspace_mem_region_add()
949 * Confirm a mem region with an overlapping address doesn't in vm_userspace_mem_region_add()
952 region = (struct userspace_mem_region *) userspace_mem_region_find( in vm_userspace_mem_region_add()
953 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1); in vm_userspace_mem_region_add()
954 if (region != NULL) in vm_userspace_mem_region_add()
960 guest_paddr, npages, vm->page_size, in vm_userspace_mem_region_add()
961 (uint64_t) region->region.guest_phys_addr, in vm_userspace_mem_region_add()
962 (uint64_t) region->region.memory_size); in vm_userspace_mem_region_add()
964 /* Confirm no region with the requested slot already exists. */ in vm_userspace_mem_region_add()
965 hash_for_each_possible(vm->regions.slot_hash, region, slot_node, in vm_userspace_mem_region_add()
967 if (region->region.slot != slot) in vm_userspace_mem_region_add()
970 TEST_FAIL("A mem region with the requested slot " in vm_userspace_mem_region_add()
975 region->region.slot, in vm_userspace_mem_region_add()
976 (uint64_t) region->region.guest_phys_addr, in vm_userspace_mem_region_add()
977 (uint64_t) region->region.memory_size); in vm_userspace_mem_region_add()
980 /* Allocate and initialize new mem region structure. */ in vm_userspace_mem_region_add()
981 region = calloc(1, sizeof(*region)); in vm_userspace_mem_region_add()
982 TEST_ASSERT(region != NULL, "Insufficient Memory"); in vm_userspace_mem_region_add()
983 region->mmap_size = npages * vm->page_size; in vm_userspace_mem_region_add()
1003 /* Add enough memory to align up if necessary */ in vm_userspace_mem_region_add()
1005 region->mmap_size += alignment; in vm_userspace_mem_region_add()
1007 region->fd = -1; in vm_userspace_mem_region_add()
1009 region->fd = kvm_memfd_alloc(region->mmap_size, in vm_userspace_mem_region_add()
1012 region->mmap_start = mmap(NULL, region->mmap_size, in vm_userspace_mem_region_add()
1014 vm_mem_backing_src_alias(src_type)->flag, in vm_userspace_mem_region_add()
1015 region->fd, 0); in vm_userspace_mem_region_add()
1016 TEST_ASSERT(region->mmap_start != MAP_FAILED, in vm_userspace_mem_region_add()
1020 region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz), in vm_userspace_mem_region_add()
1022 region->mmap_start, backing_src_pagesz); in vm_userspace_mem_region_add()
1025 region->host_mem = align_ptr_up(region->mmap_start, alignment); in vm_userspace_mem_region_add()
1030 ret = madvise(region->host_mem, npages * vm->page_size, in vm_userspace_mem_region_add()
1033 region->host_mem, npages * vm->page_size, in vm_userspace_mem_region_add()
1034 vm_mem_backing_src_alias(src_type)->name); in vm_userspace_mem_region_add()
1037 region->backing_src_type = src_type; in vm_userspace_mem_region_add()
1038 region->unused_phy_pages = sparsebit_alloc(); in vm_userspace_mem_region_add()
1039 sparsebit_set_num(region->unused_phy_pages, in vm_userspace_mem_region_add()
1040 guest_paddr >> vm->page_shift, npages); in vm_userspace_mem_region_add()
1041 region->region.slot = slot; in vm_userspace_mem_region_add()
1042 region->region.flags = flags; in vm_userspace_mem_region_add()
1043 region->region.guest_phys_addr = guest_paddr; in vm_userspace_mem_region_add()
1044 region->region.memory_size = npages * vm->page_size; in vm_userspace_mem_region_add()
1045 region->region.userspace_addr = (uintptr_t) region->host_mem; in vm_userspace_mem_region_add()
1046 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); in vm_userspace_mem_region_add()
1052 guest_paddr, (uint64_t) region->region.memory_size); in vm_userspace_mem_region_add()
1055 vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region); in vm_userspace_mem_region_add()
1056 vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region); in vm_userspace_mem_region_add()
1057 hash_add(vm->regions.slot_hash, ®ion->slot_node, slot); in vm_userspace_mem_region_add()
1059 /* If shared memory, create an alias. */ in vm_userspace_mem_region_add()
1060 if (region->fd >= 0) { in vm_userspace_mem_region_add()
1061 region->mmap_alias = mmap(NULL, region->mmap_size, in vm_userspace_mem_region_add()
1063 vm_mem_backing_src_alias(src_type)->flag, in vm_userspace_mem_region_add()
1064 region->fd, 0); in vm_userspace_mem_region_add()
1065 TEST_ASSERT(region->mmap_alias != MAP_FAILED, in vm_userspace_mem_region_add()
1069 region->host_alias = align_ptr_up(region->mmap_alias, alignment); in vm_userspace_mem_region_add()
1074 * Memslot to region
1077 * vm - Virtual Machine
1078 * memslot - KVM memory slot ID
1083 * Pointer to memory region structure that describe memory region
1084 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
1085 * on error (e.g. currently no memory region using memslot as a KVM
1086 * memory slot ID).
1091 struct userspace_mem_region *region; in memslot2region() local
1093 hash_for_each_possible(vm->regions.slot_hash, region, slot_node, in memslot2region()
1095 if (region->region.slot == memslot) in memslot2region()
1096 return region; in memslot2region()
1098 fprintf(stderr, "No mem region with the requested slot found,\n" in memslot2region()
1100 fputs("---- vm dump ----\n", stderr); in memslot2region()
1102 TEST_FAIL("Mem region not found"); in memslot2region()
1107 * VM Memory Region Flags Set
1110 * vm - Virtual Machine
1111 * flags - Starting guest physical address
1117 * Sets the flags of the memory region specified by the value of slot,
1123 struct userspace_mem_region *region; in vm_mem_region_set_flags() local
1125 region = memslot2region(vm, slot); in vm_mem_region_set_flags()
1127 region->region.flags = flags; in vm_mem_region_set_flags()
1129 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); in vm_mem_region_set_flags()
1137 * VM Memory Region Move
1140 * vm - Virtual Machine
1141 * slot - Slot of the memory region to move
1142 * new_gpa - Starting guest physical address
1148 * Change the gpa of a memory region.
1152 struct userspace_mem_region *region; in vm_mem_region_move() local
1155 region = memslot2region(vm, slot); in vm_mem_region_move()
1157 region->region.guest_phys_addr = new_gpa; in vm_mem_region_move()
1159 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION, ®ion->region); in vm_mem_region_move()
1167 * VM Memory Region Delete
1170 * vm - Virtual Machine
1171 * slot - Slot of the memory region to delete
1177 * Delete a memory region.
1204 list_for_each_entry(vcpu, &vm->vcpus, list) { in vcpu_exists()
1205 if (vcpu->id == vcpu_id) in vcpu_exists()
1225 TEST_ASSERT(vcpu != NULL, "Insufficient Memory"); in __vm_vcpu_add()
1227 vcpu->vm = vm; in __vm_vcpu_add()
1228 vcpu->id = vcpu_id; in __vm_vcpu_add()
1229 vcpu->fd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id); in __vm_vcpu_add()
1230 TEST_ASSERT(vcpu->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VCPU, vcpu->fd)); in __vm_vcpu_add()
1232 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size " in __vm_vcpu_add()
1234 vcpu_mmap_sz(), sizeof(*vcpu->run)); in __vm_vcpu_add()
1235 vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(), in __vm_vcpu_add()
1236 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0); in __vm_vcpu_add()
1237 TEST_ASSERT(vcpu->run != MAP_FAILED, in __vm_vcpu_add()
1240 /* Add to linked-list of VCPUs. */ in __vm_vcpu_add()
1241 list_add(&vcpu->list, &vm->vcpus); in __vm_vcpu_add()
1250 * vm - Virtual Machine
1251 * sz - Size (bytes)
1252 * vaddr_min - Minimum Virtual Address
1269 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift; in vm_vaddr_unused_gap()
1272 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift; in vm_vaddr_unused_gap()
1273 if ((pgidx_start * vm->page_size) < vaddr_min) in vm_vaddr_unused_gap()
1277 if (!sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1279 pgidx_start = sparsebit_next_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1288 if (sparsebit_is_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1291 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1300 if (!sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1303 vm->vpages_valid, pgidx_start, pages); in vm_vaddr_unused_gap()
1313 return -1; in vm_vaddr_unused_gap()
1316 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1322 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1329 return pgidx_start * vm->page_size; in vm_vaddr_unused_gap()
1335 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); in __vm_vaddr_alloc()
1339 KVM_UTIL_MIN_PFN * vm->page_size, in __vm_vaddr_alloc()
1340 vm->memslots[type]); in __vm_vaddr_alloc()
1350 pages--, vaddr += vm->page_size, paddr += vm->page_size) { in __vm_vaddr_alloc()
1354 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); in __vm_vaddr_alloc()
1364 * vm - Virtual Machine
1365 * sz - Size in bytes
1366 * vaddr_min - Minimum starting virtual address
1377 * a page. The allocated physical space comes from the TEST_DATA memory region.
1388 * vm - Virtual Machine
1412 * vm - Virtual Machine
1431 * vm - Virtual Machine
1432 * vaddr - Virtuall address to map
1433 * paddr - VM Physical Address
1434 * npages - The number of pages to map
1446 size_t page_size = vm->page_size; in virt_map()
1452 while (npages--) { in virt_map()
1454 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); in virt_map()
1465 * vm - Virtual Machine
1466 * gpa - VM physical address
1473 * Locates the memory region containing the VM physical address given
1475 * address providing the memory to the vm physical address is returned.
1476 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1480 struct userspace_mem_region *region; in addr_gpa2hva() local
1482 region = userspace_mem_region_find(vm, gpa, gpa); in addr_gpa2hva()
1483 if (!region) { in addr_gpa2hva()
1484 TEST_FAIL("No vm physical memory at 0x%lx", gpa); in addr_gpa2hva()
1488 return (void *)((uintptr_t)region->host_mem in addr_gpa2hva()
1489 + (gpa - region->region.guest_phys_addr)); in addr_gpa2hva()
1496 * vm - Virtual Machine
1497 * hva - Host virtual address
1504 * Locates the memory region containing the host virtual address given
1507 * region containing hva exists.
1513 for (node = vm->regions.hva_tree.rb_node; node; ) { in addr_hva2gpa()
1514 struct userspace_mem_region *region = in addr_hva2gpa() local
1517 if (hva >= region->host_mem) { in addr_hva2gpa()
1518 if (hva <= (region->host_mem in addr_hva2gpa()
1519 + region->region.memory_size - 1)) in addr_hva2gpa()
1521 region->region.guest_phys_addr in addr_hva2gpa()
1522 + (hva - (uintptr_t)region->host_mem)); in addr_hva2gpa()
1524 node = node->rb_right; in addr_hva2gpa()
1526 node = node->rb_left; in addr_hva2gpa()
1530 return -1; in addr_hva2gpa()
1537 * vm - Virtual Machine
1538 * gpa - VM physical address
1544 * (without failing the test) if the guest memory is not shared (so
1549 * memory without mapping said memory in the guest's address space. And, for
1550 * userfaultfd-based demand paging, to do so without triggering userfaults.
1554 struct userspace_mem_region *region; in addr_gpa2alias() local
1557 region = userspace_mem_region_find(vm, gpa, gpa); in addr_gpa2alias()
1558 if (!region) in addr_gpa2alias()
1561 if (!region->host_alias) in addr_gpa2alias()
1564 offset = gpa - region->region.guest_phys_addr; in addr_gpa2alias()
1565 return (void *) ((uintptr_t) region->host_alias + offset); in addr_gpa2alias()
1573 vm->has_irqchip = true; in vm_create_irqchip()
1582 } while (rc == -1 && errno == EINTR); in _vcpu_run()
1590 * Invoke KVM_RUN on a vCPU until KVM returns something other than -EINTR.
1591 * Assert if the KVM returns an error (other than -EINTR).
1604 vcpu->run->immediate_exit = 1; in vcpu_run_complete_io()
1606 vcpu->run->immediate_exit = 0; in vcpu_run_complete_io()
1608 TEST_ASSERT(ret == -1 && errno == EINTR, in vcpu_run_complete_io()
1624 TEST_ASSERT(ret == -1 && errno == E2BIG, "KVM_GET_REG_LIST n=0"); in vcpu_get_reg_list()
1627 reg_list->n = reg_list_n.n; in vcpu_get_reg_list()
1635 uint32_t size = vcpu->vm->dirty_ring_size; in vcpu_map_dirty_ring()
1639 if (!vcpu->dirty_gfns) { in vcpu_map_dirty_ring()
1642 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, vcpu->fd, in vcpu_map_dirty_ring()
1646 addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, vcpu->fd, in vcpu_map_dirty_ring()
1650 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, in vcpu_map_dirty_ring()
1654 vcpu->dirty_gfns = addr; in vcpu_map_dirty_ring()
1655 vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn); in vcpu_map_dirty_ring()
1658 return vcpu->dirty_gfns; in vcpu_map_dirty_ring()
1690 .fd = -1, in __kvm_create_device()
1765 assert(routing->nr < KVM_MAX_IRQ_ROUTES); in kvm_gsi_routing_irqchip_add()
1767 i = routing->nr; in kvm_gsi_routing_irqchip_add()
1768 routing->entries[i].gsi = gsi; in kvm_gsi_routing_irqchip_add()
1769 routing->entries[i].type = KVM_IRQ_ROUTING_IRQCHIP; in kvm_gsi_routing_irqchip_add()
1770 routing->entries[i].flags = 0; in kvm_gsi_routing_irqchip_add()
1771 routing->entries[i].u.irqchip.irqchip = 0; in kvm_gsi_routing_irqchip_add()
1772 routing->entries[i].u.irqchip.pin = pin; in kvm_gsi_routing_irqchip_add()
1773 routing->nr++; in kvm_gsi_routing_irqchip_add()
1799 * vm - Virtual Machine
1800 * indent - Left margin indent amount
1803 * stream - Output FILE stream
1813 struct userspace_mem_region *region; in vm_dump() local
1816 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode); in vm_dump()
1817 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd); in vm_dump()
1818 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size); in vm_dump()
1820 hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { in vm_dump()
1823 (uint64_t) region->region.guest_phys_addr, in vm_dump()
1824 (uint64_t) region->region.memory_size, in vm_dump()
1825 region->host_mem); in vm_dump()
1827 sparsebit_dump(stream, region->unused_phy_pages, 0); in vm_dump()
1830 sparsebit_dump(stream, vm->vpages_mapped, indent + 2); in vm_dump()
1832 vm->pgd_created); in vm_dump()
1833 if (vm->pgd_created) { in vm_dump()
1840 list_for_each_entry(vcpu, &vm->vcpus, list) in vm_dump()
1898 * exit_reason - Exit reason
1925 * vm - Virtual Machine
1926 * num - number of pages
1927 * paddr_min - Physical address minimum
1928 * memslot - Memory region to allocate page from
1943 struct userspace_mem_region *region; in vm_phy_pages_alloc() local
1948 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address " in vm_phy_pages_alloc()
1951 paddr_min, vm->page_size); in vm_phy_pages_alloc()
1953 region = memslot2region(vm, memslot); in vm_phy_pages_alloc()
1954 base = pg = paddr_min >> vm->page_shift; in vm_phy_pages_alloc()
1958 if (!sparsebit_is_set(region->unused_phy_pages, pg)) { in vm_phy_pages_alloc()
1959 base = pg = sparsebit_next_set(region->unused_phy_pages, pg); in vm_phy_pages_alloc()
1968 paddr_min, vm->page_size, memslot); in vm_phy_pages_alloc()
1969 fputs("---- vm dump ----\n", stderr); in vm_phy_pages_alloc()
1975 sparsebit_clear(region->unused_phy_pages, pg); in vm_phy_pages_alloc()
1977 return base * vm->page_size; in vm_phy_pages_alloc()
1989 vm->memslots[MEM_REGION_PT]); in vm_alloc_page_table()
1996 * vm - Virtual Machine
1997 * gva - VM virtual address
2011 return ((1ULL << vm->pa_bits) >> vm->page_shift) - 1; in vm_compute_max_gfn()
2019 unsigned int n = 1 << (new_page_shift - page_shift); in vm_calc_num_pages()
2022 return num_pages * (1 << (page_shift - new_page_shift)); in vm_calc_num_pages()
2029 return __builtin_ffs(getpagesize()) - 1; in getpageshift()
2058 * stats_fd - the file descriptor for the binary stats file from which to read
2059 * header - the binary stats metadata header corresponding to the given FD
2076 total_size = header->num_desc * desc_size; in read_stats_descriptors()
2078 stats_desc = calloc(header->num_desc, desc_size); in read_stats_descriptors()
2079 TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); in read_stats_descriptors()
2081 ret = pread(stats_fd, stats_desc, total_size, header->desc_offset); in read_stats_descriptors()
2091 * stats_fd - the file descriptor for the binary stats file from which to read
2092 * header - the binary stats metadata header corresponding to the given FD
2093 * desc - the binary stat metadata for the particular stat to be read
2094 * max_elements - the maximum number of 8-byte values to read into data
2097 * data - the buffer into which stat data should be read
2105 size_t nr_elements = min_t(ssize_t, desc->size, max_elements); in read_stat_data()
2109 TEST_ASSERT(desc->size, "No elements in stat '%s'", desc->name); in read_stat_data()
2110 TEST_ASSERT(max_elements, "Zero elements requested for stat '%s'", desc->name); in read_stat_data()
2113 header->data_offset + desc->offset); in read_stat_data()
2116 desc->name, errno, strerror(errno)); in read_stat_data()
2119 desc->name, size, ret); in read_stat_data()
2126 * vm - the VM for which the stat should be read
2127 * stat_name - the name of the stat to read
2128 * max_elements - the maximum number of 8-byte values to read into data
2131 * data - the buffer into which stat data should be read
2142 if (!vm->stats_fd) { in __vm_get_stat()
2143 vm->stats_fd = vm_get_stats_fd(vm); in __vm_get_stat()
2144 read_stats_header(vm->stats_fd, &vm->stats_header); in __vm_get_stat()
2145 vm->stats_desc = read_stats_descriptors(vm->stats_fd, in __vm_get_stat()
2146 &vm->stats_header); in __vm_get_stat()
2149 size_desc = get_stats_descriptor_size(&vm->stats_header); in __vm_get_stat()
2151 for (i = 0; i < vm->stats_header.num_desc; ++i) { in __vm_get_stat()
2152 desc = (void *)vm->stats_desc + (i * size_desc); in __vm_get_stat()
2154 if (strcmp(desc->name, stat_name)) in __vm_get_stat()
2157 read_stat_data(vm->stats_fd, &vm->stats_header, desc, in __vm_get_stat()