Lines Matching +full:memory +full:- +full:region
1 // SPDX-License-Identifier: GPL-2.0-only
43 * flags - The flags to pass when opening KVM_DEV_PATH.
85 * 16 bytes to hold a 64-bit value (1 byte per char), 1 byte for the in get_module_param_integer()
95 TEST_ASSERT(value[r - 1] == '\n', in get_module_param_integer()
96 "Expected trailing newline, got char '%c'", value[r - 1]); in get_module_param_integer()
100 * trailing non-NUL characters in the string. in get_module_param_integer()
102 value[r - 1] = '\0'; in get_module_param_integer()
156 * cap - Capability
188 vm->dirty_ring_size = ring_size; in vm_enable_dirty_ring()
193 vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR); in vm_open()
197 vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type); in vm_open()
198 TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd)); in vm_open()
204 [VM_MODE_P52V48_4K] = "PA-bits:52, VA-bits:48, 4K pages", in vm_guest_mode_string()
205 [VM_MODE_P52V48_16K] = "PA-bits:52, VA-bits:48, 16K pages", in vm_guest_mode_string()
206 [VM_MODE_P52V48_64K] = "PA-bits:52, VA-bits:48, 64K pages", in vm_guest_mode_string()
207 [VM_MODE_P48V48_4K] = "PA-bits:48, VA-bits:48, 4K pages", in vm_guest_mode_string()
208 [VM_MODE_P48V48_16K] = "PA-bits:48, VA-bits:48, 16K pages", in vm_guest_mode_string()
209 [VM_MODE_P48V48_64K] = "PA-bits:48, VA-bits:48, 64K pages", in vm_guest_mode_string()
210 [VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages", in vm_guest_mode_string()
211 [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages", in vm_guest_mode_string()
212 [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages", in vm_guest_mode_string()
213 [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages", in vm_guest_mode_string()
214 [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages", in vm_guest_mode_string()
215 [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages", in vm_guest_mode_string()
216 [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages", in vm_guest_mode_string()
217 [VM_MODE_P36V48_16K] = "PA-bits:36, VA-bits:48, 16K pages", in vm_guest_mode_string()
218 [VM_MODE_P36V48_64K] = "PA-bits:36, VA-bits:48, 64K pages", in vm_guest_mode_string()
219 [VM_MODE_P36V47_16K] = "PA-bits:36, VA-bits:47, 16K pages", in vm_guest_mode_string()
251 * Initializes vm->vpages_valid to match the canonical VA space of the
255 * range addressed by a single page table into a low and high region
257 * the VA region spans [0, 2^(va_bits - 1)), [-(2^(va_bits - 1), -1].
261 sparsebit_set_num(vm->vpages_valid, in vm_vaddr_populate_bitmap()
262 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift); in vm_vaddr_populate_bitmap()
263 sparsebit_set_num(vm->vpages_valid, in vm_vaddr_populate_bitmap()
264 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift, in vm_vaddr_populate_bitmap()
265 (1ULL << (vm->va_bits - 1)) >> vm->page_shift); in vm_vaddr_populate_bitmap()
273 TEST_ASSERT(vm != NULL, "Insufficient Memory"); in ____vm_create()
275 INIT_LIST_HEAD(&vm->vcpus); in ____vm_create()
276 vm->regions.gpa_tree = RB_ROOT; in ____vm_create()
277 vm->regions.hva_tree = RB_ROOT; in ____vm_create()
278 hash_init(vm->regions.slot_hash); in ____vm_create()
280 vm->mode = shape.mode; in ____vm_create()
281 vm->type = shape.type; in ____vm_create()
283 vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; in ____vm_create()
284 vm->va_bits = vm_guest_mode_params[vm->mode].va_bits; in ____vm_create()
285 vm->page_size = vm_guest_mode_params[vm->mode].page_size; in ____vm_create()
286 vm->page_shift = vm_guest_mode_params[vm->mode].page_shift; in ____vm_create()
289 switch (vm->mode) { in ____vm_create()
291 vm->pgtable_levels = 4; in ____vm_create()
294 vm->pgtable_levels = 3; in ____vm_create()
297 vm->pgtable_levels = 4; in ____vm_create()
300 vm->pgtable_levels = 3; in ____vm_create()
304 vm->pgtable_levels = 4; in ____vm_create()
308 vm->pgtable_levels = 3; in ____vm_create()
314 vm->pgtable_levels = 4; in ____vm_create()
317 vm->pgtable_levels = 3; in ____vm_create()
321 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits); in ____vm_create()
324 * Ignore KVM support for 5-level paging (vm->va_bits == 57), in ____vm_create()
326 * isn't for this mode (48-bit virtual address space). in ____vm_create()
328 TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57, in ____vm_create()
330 vm->va_bits); in ____vm_create()
332 vm->pa_bits); in ____vm_create()
333 vm->pgtable_levels = 4; in ____vm_create()
334 vm->va_bits = 48; in ____vm_create()
336 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms"); in ____vm_create()
340 vm->pgtable_levels = 5; in ____vm_create()
343 vm->pgtable_levels = 5; in ____vm_create()
346 TEST_FAIL("Unknown guest mode: 0x%x", vm->mode); in ____vm_create()
350 TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types"); in ____vm_create()
351 if (vm->pa_bits != 40) in ____vm_create()
352 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); in ____vm_create()
357 /* Limit to VA-bit canonical virtual addresses. */ in ____vm_create()
358 vm->vpages_valid = sparsebit_alloc(); in ____vm_create()
361 /* Limit physical addresses to PA-bits. */ in ____vm_create()
362 vm->max_gfn = vm_compute_max_gfn(vm); in ____vm_create()
364 /* Allocate and setup memory for guest. */ in ____vm_create()
365 vm->vpages_mapped = sparsebit_alloc(); in ____vm_create()
381 "nr_vcpus = %d too large for host, max-vcpus = %d", in vm_nr_pages_required()
386 * test code and other per-VM assets that will be loaded into memslot0. in vm_nr_pages_required()
390 /* Account for the per-vCPU stacks on behalf of the test. */ in vm_nr_pages_required()
395 * maximum page table size for a memory region will be when the in vm_nr_pages_required()
425 vm->memslots[i] = 0; in __vm_create()
432 * read-only memslots as MMIO, and creating a read-only memslot for the in __vm_create()
433 * MMIO region would prevent silently clobbering the MMIO region. in __vm_create()
436 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size); in __vm_create()
454 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
455 * nr_vcpus - VCPU count
456 * extra_mem_pages - Non-slot0 physical memory total size
457 * guest_code - Guest entry point
458 * vcpuids - VCPU IDs
467 * no real memory allocation for non-slot0 memory in this function.
504 * vm - VM that has been released before
509 * global state, such as the irqchip and the memory regions that are mapped
515 struct userspace_mem_region *region; in kvm_vm_restart() local
518 if (vmp->has_irqchip) in kvm_vm_restart()
521 hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) { in kvm_vm_restart()
522 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION2, ®ion->region); in kvm_vm_restart()
528 ret, errno, region->region.slot, in kvm_vm_restart()
529 region->region.flags, in kvm_vm_restart()
530 region->region.guest_phys_addr, in kvm_vm_restart()
531 region->region.memory_size); in kvm_vm_restart()
572 printf(" -c: Pin tasks to physical CPUs. Takes a list of comma separated\n" in kvm_print_vcpu_pinning_help()
579 " %s -v 3 -c 22,23,24,50\n\n" in kvm_print_vcpu_pinning_help()
581 " %s -v 3 -c 22,23,24\n\n" in kvm_print_vcpu_pinning_help()
619 * Userspace Memory Region Find
622 * vm - Virtual Machine
623 * start - Starting VM physical address
624 * end - Ending VM physical address, inclusive.
629 * Pointer to overlapping region, NULL if no such region.
631 * Searches for a region with any physical memory that overlaps with
635 * region exists.
642 for (node = vm->regions.gpa_tree.rb_node; node; ) { in userspace_mem_region_find()
643 struct userspace_mem_region *region = in userspace_mem_region_find() local
645 uint64_t existing_start = region->region.guest_phys_addr; in userspace_mem_region_find()
646 uint64_t existing_end = region->region.guest_phys_addr in userspace_mem_region_find()
647 + region->region.memory_size - 1; in userspace_mem_region_find()
649 return region; in userspace_mem_region_find()
652 node = node->rb_left; in userspace_mem_region_find()
654 node = node->rb_right; in userspace_mem_region_find()
669 * vcpu - VCPU to remove
681 if (vcpu->dirty_gfns) { in vm_vcpu_rm()
682 ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size); in vm_vcpu_rm()
684 vcpu->dirty_gfns = NULL; in vm_vcpu_rm()
687 ret = munmap(vcpu->run, vcpu_mmap_sz()); in vm_vcpu_rm()
690 ret = close(vcpu->fd); in vm_vcpu_rm()
693 list_del(&vcpu->list); in vm_vcpu_rm()
704 list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list) in kvm_vm_release()
707 ret = close(vmp->fd); in kvm_vm_release()
710 ret = close(vmp->kvm_fd); in kvm_vm_release()
715 struct userspace_mem_region *region) in __vm_mem_region_delete() argument
719 rb_erase(®ion->gpa_node, &vm->regions.gpa_tree); in __vm_mem_region_delete()
720 rb_erase(®ion->hva_node, &vm->regions.hva_tree); in __vm_mem_region_delete()
721 hash_del(®ion->slot_node); in __vm_mem_region_delete()
723 region->region.memory_size = 0; in __vm_mem_region_delete()
724 vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); in __vm_mem_region_delete()
726 sparsebit_free(®ion->unused_phy_pages); in __vm_mem_region_delete()
727 sparsebit_free(®ion->protected_phy_pages); in __vm_mem_region_delete()
728 ret = munmap(region->mmap_start, region->mmap_size); in __vm_mem_region_delete()
730 if (region->fd >= 0) { in __vm_mem_region_delete()
731 /* There's an extra map when using shared memory. */ in __vm_mem_region_delete()
732 ret = munmap(region->mmap_alias, region->mmap_size); in __vm_mem_region_delete()
734 close(region->fd); in __vm_mem_region_delete()
736 if (region->region.guest_memfd >= 0) in __vm_mem_region_delete()
737 close(region->region.guest_memfd); in __vm_mem_region_delete()
739 free(region); in __vm_mem_region_delete()
749 struct userspace_mem_region *region; in kvm_vm_free() local
755 if (vmp->stats_fd) { in kvm_vm_free()
756 free(vmp->stats_desc); in kvm_vm_free()
757 close(vmp->stats_fd); in kvm_vm_free()
761 hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node) in kvm_vm_free()
762 __vm_mem_region_delete(vmp, region); in kvm_vm_free()
765 sparsebit_free(&vmp->vpages_valid); in kvm_vm_free()
766 sparsebit_free(&vmp->vpages_mapped); in kvm_vm_free()
783 TEST_ASSERT(fd != -1, __KVM_SYSCALL_ERROR("memfd_create()", fd)); in kvm_memfd_alloc()
795 struct userspace_mem_region *region) in vm_userspace_mem_region_gpa_insert() argument
799 for (cur = &gpa_tree->rb_node, parent = NULL; *cur; ) { in vm_userspace_mem_region_gpa_insert()
804 if (region->region.guest_phys_addr < in vm_userspace_mem_region_gpa_insert()
805 cregion->region.guest_phys_addr) in vm_userspace_mem_region_gpa_insert()
806 cur = &(*cur)->rb_left; in vm_userspace_mem_region_gpa_insert()
808 TEST_ASSERT(region->region.guest_phys_addr != in vm_userspace_mem_region_gpa_insert()
809 cregion->region.guest_phys_addr, in vm_userspace_mem_region_gpa_insert()
810 "Duplicate GPA in region tree"); in vm_userspace_mem_region_gpa_insert()
812 cur = &(*cur)->rb_right; in vm_userspace_mem_region_gpa_insert()
816 rb_link_node(®ion->gpa_node, parent, cur); in vm_userspace_mem_region_gpa_insert()
817 rb_insert_color(®ion->gpa_node, gpa_tree); in vm_userspace_mem_region_gpa_insert()
821 struct userspace_mem_region *region) in vm_userspace_mem_region_hva_insert() argument
825 for (cur = &hva_tree->rb_node, parent = NULL; *cur; ) { in vm_userspace_mem_region_hva_insert()
830 if (region->host_mem < cregion->host_mem) in vm_userspace_mem_region_hva_insert()
831 cur = &(*cur)->rb_left; in vm_userspace_mem_region_hva_insert()
833 TEST_ASSERT(region->host_mem != in vm_userspace_mem_region_hva_insert()
834 cregion->host_mem, in vm_userspace_mem_region_hva_insert()
835 "Duplicate HVA in region tree"); in vm_userspace_mem_region_hva_insert()
837 cur = &(*cur)->rb_right; in vm_userspace_mem_region_hva_insert()
841 rb_link_node(®ion->hva_node, parent, cur); in vm_userspace_mem_region_hva_insert()
842 rb_insert_color(®ion->hva_node, hva_tree); in vm_userspace_mem_region_hva_insert()
849 struct kvm_userspace_memory_region region = { in __vm_set_user_memory_region() local
857 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion); in __vm_set_user_memory_region()
877 struct kvm_userspace_memory_region2 region = { in __vm_set_user_memory_region2() local
889 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION2, ®ion); in __vm_set_user_memory_region2()
910 struct userspace_mem_region *region; in vm_mem_add() local
912 size_t mem_size = npages * vm->page_size; in vm_mem_add()
917 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages, in vm_mem_add()
919 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages)); in vm_mem_add()
921 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical " in vm_mem_add()
923 " guest_paddr: 0x%lx vm->page_size: 0x%x", in vm_mem_add()
924 guest_paddr, vm->page_size); in vm_mem_add()
925 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1) in vm_mem_add()
926 <= vm->max_gfn, "Physical range beyond maximum " in vm_mem_add()
929 " vm->max_gfn: 0x%lx vm->page_size: 0x%x", in vm_mem_add()
930 guest_paddr, npages, vm->max_gfn, vm->page_size); in vm_mem_add()
933 * Confirm a mem region with an overlapping address doesn't in vm_mem_add()
936 region = (struct userspace_mem_region *) userspace_mem_region_find( in vm_mem_add()
937 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1); in vm_mem_add()
938 if (region != NULL) in vm_mem_add()
944 guest_paddr, npages, vm->page_size, in vm_mem_add()
945 (uint64_t) region->region.guest_phys_addr, in vm_mem_add()
946 (uint64_t) region->region.memory_size); in vm_mem_add()
948 /* Confirm no region with the requested slot already exists. */ in vm_mem_add()
949 hash_for_each_possible(vm->regions.slot_hash, region, slot_node, in vm_mem_add()
951 if (region->region.slot != slot) in vm_mem_add()
954 TEST_FAIL("A mem region with the requested slot " in vm_mem_add()
959 region->region.slot, in vm_mem_add()
960 (uint64_t) region->region.guest_phys_addr, in vm_mem_add()
961 (uint64_t) region->region.memory_size); in vm_mem_add()
964 /* Allocate and initialize new mem region structure. */ in vm_mem_add()
965 region = calloc(1, sizeof(*region)); in vm_mem_add()
966 TEST_ASSERT(region != NULL, "Insufficient Memory"); in vm_mem_add()
967 region->mmap_size = mem_size; in vm_mem_add()
987 /* Add enough memory to align up if necessary */ in vm_mem_add()
989 region->mmap_size += alignment; in vm_mem_add()
991 region->fd = -1; in vm_mem_add()
993 region->fd = kvm_memfd_alloc(region->mmap_size, in vm_mem_add()
996 region->mmap_start = mmap(NULL, region->mmap_size, in vm_mem_add()
998 vm_mem_backing_src_alias(src_type)->flag, in vm_mem_add()
999 region->fd, 0); in vm_mem_add()
1000 TEST_ASSERT(region->mmap_start != MAP_FAILED, in vm_mem_add()
1004 region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz), in vm_mem_add()
1006 region->mmap_start, backing_src_pagesz); in vm_mem_add()
1009 region->host_mem = align_ptr_up(region->mmap_start, alignment); in vm_mem_add()
1014 ret = madvise(region->host_mem, mem_size, in vm_mem_add()
1017 region->host_mem, mem_size, in vm_mem_add()
1018 vm_mem_backing_src_alias(src_type)->name); in vm_mem_add()
1021 region->backing_src_type = src_type; in vm_mem_add()
1032 * can be closed when the region is deleted without in vm_mem_add()
1040 region->region.guest_memfd = guest_memfd; in vm_mem_add()
1041 region->region.guest_memfd_offset = guest_memfd_offset; in vm_mem_add()
1043 region->region.guest_memfd = -1; in vm_mem_add()
1046 region->unused_phy_pages = sparsebit_alloc(); in vm_mem_add()
1048 region->protected_phy_pages = sparsebit_alloc(); in vm_mem_add()
1049 sparsebit_set_num(region->unused_phy_pages, in vm_mem_add()
1050 guest_paddr >> vm->page_shift, npages); in vm_mem_add()
1051 region->region.slot = slot; in vm_mem_add()
1052 region->region.flags = flags; in vm_mem_add()
1053 region->region.guest_phys_addr = guest_paddr; in vm_mem_add()
1054 region->region.memory_size = npages * vm->page_size; in vm_mem_add()
1055 region->region.userspace_addr = (uintptr_t) region->host_mem; in vm_mem_add()
1056 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); in vm_mem_add()
1062 guest_paddr, (uint64_t) region->region.memory_size, in vm_mem_add()
1063 region->region.guest_memfd); in vm_mem_add()
1066 vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region); in vm_mem_add()
1067 vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region); in vm_mem_add()
1068 hash_add(vm->regions.slot_hash, ®ion->slot_node, slot); in vm_mem_add()
1070 /* If shared memory, create an alias. */ in vm_mem_add()
1071 if (region->fd >= 0) { in vm_mem_add()
1072 region->mmap_alias = mmap(NULL, region->mmap_size, in vm_mem_add()
1074 vm_mem_backing_src_alias(src_type)->flag, in vm_mem_add()
1075 region->fd, 0); in vm_mem_add()
1076 TEST_ASSERT(region->mmap_alias != MAP_FAILED, in vm_mem_add()
1080 region->host_alias = align_ptr_up(region->mmap_alias, alignment); in vm_mem_add()
1089 vm_mem_add(vm, src_type, guest_paddr, slot, npages, flags, -1, 0); in vm_userspace_mem_region_add()
1093 * Memslot to region
1096 * vm - Virtual Machine
1097 * memslot - KVM memory slot ID
1102 * Pointer to memory region structure that describe memory region
1103 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
1104 * on error (e.g. currently no memory region using memslot as a KVM
1105 * memory slot ID).
1110 struct userspace_mem_region *region; in memslot2region() local
1112 hash_for_each_possible(vm->regions.slot_hash, region, slot_node, in memslot2region()
1114 if (region->region.slot == memslot) in memslot2region()
1115 return region; in memslot2region()
1117 fprintf(stderr, "No mem region with the requested slot found,\n" in memslot2region()
1119 fputs("---- vm dump ----\n", stderr); in memslot2region()
1121 TEST_FAIL("Mem region not found"); in memslot2region()
1126 * VM Memory Region Flags Set
1129 * vm - Virtual Machine
1130 * flags - Starting guest physical address
1136 * Sets the flags of the memory region specified by the value of slot,
1142 struct userspace_mem_region *region; in vm_mem_region_set_flags() local
1144 region = memslot2region(vm, slot); in vm_mem_region_set_flags()
1146 region->region.flags = flags; in vm_mem_region_set_flags()
1148 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); in vm_mem_region_set_flags()
1156 * VM Memory Region Move
1159 * vm - Virtual Machine
1160 * slot - Slot of the memory region to move
1161 * new_gpa - Starting guest physical address
1167 * Change the gpa of a memory region.
1171 struct userspace_mem_region *region; in vm_mem_region_move() local
1174 region = memslot2region(vm, slot); in vm_mem_region_move()
1176 region->region.guest_phys_addr = new_gpa; in vm_mem_region_move()
1178 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region); in vm_mem_region_move()
1186 * VM Memory Region Delete
1189 * vm - Virtual Machine
1190 * slot - Slot of the memory region to delete
1196 * Delete a memory region.
1207 struct userspace_mem_region *region; in vm_guest_mem_fallocate() local
1216 region = userspace_mem_region_find(vm, gpa, gpa); in vm_guest_mem_fallocate()
1217 TEST_ASSERT(region && region->region.flags & KVM_MEM_GUEST_MEMFD, in vm_guest_mem_fallocate()
1218 "Private memory region not found for GPA 0x%lx", gpa); in vm_guest_mem_fallocate()
1220 offset = gpa - region->region.guest_phys_addr; in vm_guest_mem_fallocate()
1221 fd_offset = region->region.guest_memfd_offset + offset; in vm_guest_mem_fallocate()
1222 len = min_t(uint64_t, end - gpa, region->region.memory_size - offset); in vm_guest_mem_fallocate()
1224 ret = fallocate(region->region.guest_memfd, mode, fd_offset, len); in vm_guest_mem_fallocate()
1227 region->region.guest_memfd, mode, fd_offset); in vm_guest_mem_fallocate()
1251 list_for_each_entry(vcpu, &vm->vcpus, list) { in vcpu_exists()
1252 if (vcpu->id == vcpu_id) in vcpu_exists()
1272 TEST_ASSERT(vcpu != NULL, "Insufficient Memory"); in __vm_vcpu_add()
1274 vcpu->vm = vm; in __vm_vcpu_add()
1275 vcpu->id = vcpu_id; in __vm_vcpu_add()
1276 vcpu->fd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id); in __vm_vcpu_add()
1277 TEST_ASSERT_VM_VCPU_IOCTL(vcpu->fd >= 0, KVM_CREATE_VCPU, vcpu->fd, vm); in __vm_vcpu_add()
1279 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size " in __vm_vcpu_add()
1281 vcpu_mmap_sz(), sizeof(*vcpu->run)); in __vm_vcpu_add()
1282 vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(), in __vm_vcpu_add()
1283 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0); in __vm_vcpu_add()
1284 TEST_ASSERT(vcpu->run != MAP_FAILED, in __vm_vcpu_add()
1287 /* Add to linked-list of VCPUs. */ in __vm_vcpu_add()
1288 list_add(&vcpu->list, &vm->vcpus); in __vm_vcpu_add()
1297 * vm - Virtual Machine
1298 * sz - Size (bytes)
1299 * vaddr_min - Minimum Virtual Address
1316 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift; in vm_vaddr_unused_gap()
1319 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift; in vm_vaddr_unused_gap()
1320 if ((pgidx_start * vm->page_size) < vaddr_min) in vm_vaddr_unused_gap()
1324 if (!sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1326 pgidx_start = sparsebit_next_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1335 if (sparsebit_is_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1338 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1347 if (!sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1350 vm->vpages_valid, pgidx_start, pages); in vm_vaddr_unused_gap()
1360 return -1; in vm_vaddr_unused_gap()
1363 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid, in vm_vaddr_unused_gap()
1369 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped, in vm_vaddr_unused_gap()
1376 return pgidx_start * vm->page_size; in vm_vaddr_unused_gap()
1384 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0); in ____vm_vaddr_alloc()
1388 KVM_UTIL_MIN_PFN * vm->page_size, in ____vm_vaddr_alloc()
1389 vm->memslots[type], protected); in ____vm_vaddr_alloc()
1399 pages--, vaddr += vm->page_size, paddr += vm->page_size) { in ____vm_vaddr_alloc()
1403 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); in ____vm_vaddr_alloc()
1427 * vm - Virtual Machine
1428 * sz - Size in bytes
1429 * vaddr_min - Minimum starting virtual address
1440 * a page. The allocated physical space comes from the TEST_DATA memory region.
1451 * vm - Virtual Machine
1475 * vm - Virtual Machine
1494 * vm - Virtual Machine
1495 * vaddr - Virtuall address to map
1496 * paddr - VM Physical Address
1497 * npages - The number of pages to map
1509 size_t page_size = vm->page_size; in virt_map()
1515 while (npages--) { in virt_map()
1517 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift); in virt_map()
1528 * vm - Virtual Machine
1529 * gpa - VM physical address
1536 * Locates the memory region containing the VM physical address given
1538 * address providing the memory to the vm physical address is returned.
1539 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1543 struct userspace_mem_region *region; in addr_gpa2hva() local
1547 region = userspace_mem_region_find(vm, gpa, gpa); in addr_gpa2hva()
1548 if (!region) { in addr_gpa2hva()
1549 TEST_FAIL("No vm physical memory at 0x%lx", gpa); in addr_gpa2hva()
1553 return (void *)((uintptr_t)region->host_mem in addr_gpa2hva()
1554 + (gpa - region->region.guest_phys_addr)); in addr_gpa2hva()
1561 * vm - Virtual Machine
1562 * hva - Host virtual address
1569 * Locates the memory region containing the host virtual address given
1572 * region containing hva exists.
1578 for (node = vm->regions.hva_tree.rb_node; node; ) { in addr_hva2gpa()
1579 struct userspace_mem_region *region = in addr_hva2gpa() local
1582 if (hva >= region->host_mem) { in addr_hva2gpa()
1583 if (hva <= (region->host_mem in addr_hva2gpa()
1584 + region->region.memory_size - 1)) in addr_hva2gpa()
1586 region->region.guest_phys_addr in addr_hva2gpa()
1587 + (hva - (uintptr_t)region->host_mem)); in addr_hva2gpa()
1589 node = node->rb_right; in addr_hva2gpa()
1591 node = node->rb_left; in addr_hva2gpa()
1595 return -1; in addr_hva2gpa()
1602 * vm - Virtual Machine
1603 * gpa - VM physical address
1609 * (without failing the test) if the guest memory is not shared (so
1614 * memory without mapping said memory in the guest's address space. And, for
1615 * userfaultfd-based demand paging, to do so without triggering userfaults.
1619 struct userspace_mem_region *region; in addr_gpa2alias() local
1622 region = userspace_mem_region_find(vm, gpa, gpa); in addr_gpa2alias()
1623 if (!region) in addr_gpa2alias()
1626 if (!region->host_alias) in addr_gpa2alias()
1629 offset = gpa - region->region.guest_phys_addr; in addr_gpa2alias()
1630 return (void *) ((uintptr_t) region->host_alias + offset); in addr_gpa2alias()
1638 vm->has_irqchip = true; in vm_create_irqchip()
1647 } while (rc == -1 && errno == EINTR); in _vcpu_run()
1655 * Invoke KVM_RUN on a vCPU until KVM returns something other than -EINTR.
1656 * Assert if the KVM returns an error (other than -EINTR).
1669 vcpu->run->immediate_exit = 1; in vcpu_run_complete_io()
1671 vcpu->run->immediate_exit = 0; in vcpu_run_complete_io()
1673 TEST_ASSERT(ret == -1 && errno == EINTR, in vcpu_run_complete_io()
1689 TEST_ASSERT(ret == -1 && errno == E2BIG, "KVM_GET_REG_LIST n=0"); in vcpu_get_reg_list()
1692 reg_list->n = reg_list_n.n; in vcpu_get_reg_list()
1700 uint32_t size = vcpu->vm->dirty_ring_size; in vcpu_map_dirty_ring()
1704 if (!vcpu->dirty_gfns) { in vcpu_map_dirty_ring()
1707 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, vcpu->fd, in vcpu_map_dirty_ring()
1711 addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, vcpu->fd, in vcpu_map_dirty_ring()
1715 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, in vcpu_map_dirty_ring()
1719 vcpu->dirty_gfns = addr; in vcpu_map_dirty_ring()
1720 vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn); in vcpu_map_dirty_ring()
1723 return vcpu->dirty_gfns; in vcpu_map_dirty_ring()
1755 .fd = -1, in __kvm_create_device()
1830 assert(routing->nr < KVM_MAX_IRQ_ROUTES); in kvm_gsi_routing_irqchip_add()
1832 i = routing->nr; in kvm_gsi_routing_irqchip_add()
1833 routing->entries[i].gsi = gsi; in kvm_gsi_routing_irqchip_add()
1834 routing->entries[i].type = KVM_IRQ_ROUTING_IRQCHIP; in kvm_gsi_routing_irqchip_add()
1835 routing->entries[i].flags = 0; in kvm_gsi_routing_irqchip_add()
1836 routing->entries[i].u.irqchip.irqchip = 0; in kvm_gsi_routing_irqchip_add()
1837 routing->entries[i].u.irqchip.pin = pin; in kvm_gsi_routing_irqchip_add()
1838 routing->nr++; in kvm_gsi_routing_irqchip_add()
1864 * vm - Virtual Machine
1865 * indent - Left margin indent amount
1868 * stream - Output FILE stream
1878 struct userspace_mem_region *region; in vm_dump() local
1881 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode); in vm_dump()
1882 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd); in vm_dump()
1883 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size); in vm_dump()
1885 hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) { in vm_dump()
1888 (uint64_t) region->region.guest_phys_addr, in vm_dump()
1889 (uint64_t) region->region.memory_size, in vm_dump()
1890 region->host_mem); in vm_dump()
1892 sparsebit_dump(stream, region->unused_phy_pages, 0); in vm_dump()
1893 if (region->protected_phy_pages) { in vm_dump()
1895 sparsebit_dump(stream, region->protected_phy_pages, 0); in vm_dump()
1899 sparsebit_dump(stream, vm->vpages_mapped, indent + 2); in vm_dump()
1901 vm->pgd_created); in vm_dump()
1902 if (vm->pgd_created) { in vm_dump()
1909 list_for_each_entry(vcpu, &vm->vcpus, list) in vm_dump()
1967 * exit_reason - Exit reason
1994 * vm - Virtual Machine
1995 * num - number of pages
1996 * paddr_min - Physical address minimum
1997 * memslot - Memory region to allocate page from
1998 * protected - True if the pages will be used as protected/private memory
2014 struct userspace_mem_region *region; in __vm_phy_pages_alloc() local
2019 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address " in __vm_phy_pages_alloc()
2022 paddr_min, vm->page_size); in __vm_phy_pages_alloc()
2024 region = memslot2region(vm, memslot); in __vm_phy_pages_alloc()
2025 TEST_ASSERT(!protected || region->protected_phy_pages, in __vm_phy_pages_alloc()
2026 "Region doesn't support protected memory"); in __vm_phy_pages_alloc()
2028 base = pg = paddr_min >> vm->page_shift; in __vm_phy_pages_alloc()
2031 if (!sparsebit_is_set(region->unused_phy_pages, pg)) { in __vm_phy_pages_alloc()
2032 base = pg = sparsebit_next_set(region->unused_phy_pages, pg); in __vm_phy_pages_alloc()
2041 paddr_min, vm->page_size, memslot); in __vm_phy_pages_alloc()
2042 fputs("---- vm dump ----\n", stderr); in __vm_phy_pages_alloc()
2048 sparsebit_clear(region->unused_phy_pages, pg); in __vm_phy_pages_alloc()
2050 sparsebit_set(region->protected_phy_pages, pg); in __vm_phy_pages_alloc()
2053 return base * vm->page_size; in __vm_phy_pages_alloc()
2065 vm->memslots[MEM_REGION_PT]); in vm_alloc_page_table()
2072 * vm - Virtual Machine
2073 * gva - VM virtual address
2087 return ((1ULL << vm->pa_bits) >> vm->page_shift) - 1; in vm_compute_max_gfn()
2095 unsigned int n = 1 << (new_page_shift - page_shift); in vm_calc_num_pages()
2098 return num_pages * (1 << (page_shift - new_page_shift)); in vm_calc_num_pages()
2105 return __builtin_ffs(getpagesize()) - 1; in getpageshift()
2134 * stats_fd - the file descriptor for the binary stats file from which to read
2135 * header - the binary stats metadata header corresponding to the given FD
2152 total_size = header->num_desc * desc_size; in read_stats_descriptors()
2154 stats_desc = calloc(header->num_desc, desc_size); in read_stats_descriptors()
2155 TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); in read_stats_descriptors()
2157 ret = pread(stats_fd, stats_desc, total_size, header->desc_offset); in read_stats_descriptors()
2167 * stats_fd - the file descriptor for the binary stats file from which to read
2168 * header - the binary stats metadata header corresponding to the given FD
2169 * desc - the binary stat metadata for the particular stat to be read
2170 * max_elements - the maximum number of 8-byte values to read into data
2173 * data - the buffer into which stat data should be read
2181 size_t nr_elements = min_t(ssize_t, desc->size, max_elements); in read_stat_data()
2185 TEST_ASSERT(desc->size, "No elements in stat '%s'", desc->name); in read_stat_data()
2186 TEST_ASSERT(max_elements, "Zero elements requested for stat '%s'", desc->name); in read_stat_data()
2189 header->data_offset + desc->offset); in read_stat_data()
2192 desc->name, errno, strerror(errno)); in read_stat_data()
2195 desc->name, size, ret); in read_stat_data()
2202 * vm - the VM for which the stat should be read
2203 * stat_name - the name of the stat to read
2204 * max_elements - the maximum number of 8-byte values to read into data
2207 * data - the buffer into which stat data should be read
2218 if (!vm->stats_fd) { in __vm_get_stat()
2219 vm->stats_fd = vm_get_stats_fd(vm); in __vm_get_stat()
2220 read_stats_header(vm->stats_fd, &vm->stats_header); in __vm_get_stat()
2221 vm->stats_desc = read_stats_descriptors(vm->stats_fd, in __vm_get_stat()
2222 &vm->stats_header); in __vm_get_stat()
2225 size_desc = get_stats_descriptor_size(&vm->stats_header); in __vm_get_stat()
2227 for (i = 0; i < vm->stats_header.num_desc; ++i) { in __vm_get_stat()
2228 desc = (void *)vm->stats_desc + (i * size_desc); in __vm_get_stat()
2230 if (strcmp(desc->name, stat_name)) in __vm_get_stat()
2233 read_stat_data(vm->stats_fd, &vm->stats_header, desc, in __vm_get_stat()
2262 struct userspace_mem_region *region; in vm_is_gpa_protected() local
2267 region = userspace_mem_region_find(vm, paddr, paddr); in vm_is_gpa_protected()
2268 TEST_ASSERT(region, "No vm physical memory at 0x%lx", paddr); in vm_is_gpa_protected()
2270 pg = paddr >> vm->page_shift; in vm_is_gpa_protected()
2271 return sparsebit_is_set(region->protected_phy_pages, pg); in vm_is_gpa_protected()