Lines Matching full:region

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].
395 * maximum page table size for a memory region will be when the in vm_nr_pages_required()
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()
515 struct userspace_mem_region *region; in kvm_vm_restart() local
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, &region->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()
619 * Userspace Memory Region Find
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.
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()
715 struct userspace_mem_region *region) in __vm_mem_region_delete() argument
719 rb_erase(&region->gpa_node, &vm->regions.gpa_tree); in __vm_mem_region_delete()
720 rb_erase(&region->hva_node, &vm->regions.hva_tree); in __vm_mem_region_delete()
721 hash_del(&region->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, &region->region); in __vm_mem_region_delete()
726 sparsebit_free(&region->unused_phy_pages); in __vm_mem_region_delete()
727 sparsebit_free(&region->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()
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
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()
795 struct userspace_mem_region *region) in vm_userspace_mem_region_gpa_insert() argument
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()
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()
816 rb_link_node(&region->gpa_node, parent, cur); in vm_userspace_mem_region_gpa_insert()
817 rb_insert_color(&region->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
830 if (region->host_mem < cregion->host_mem) in vm_userspace_mem_region_hva_insert()
833 TEST_ASSERT(region->host_mem != in vm_userspace_mem_region_hva_insert()
835 "Duplicate HVA in region tree"); in vm_userspace_mem_region_hva_insert()
841 rb_link_node(&region->hva_node, parent, cur); in vm_userspace_mem_region_hva_insert()
842 rb_insert_color(&region->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, &region); 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, &region); in __vm_set_user_memory_region2()
910 struct userspace_mem_region *region; in vm_mem_add() local
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()
938 if (region != NULL) 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()
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()
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()
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()
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, &region->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, &region->slot_node, slot); 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()
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()
1093 * Memslot to region
1102 * Pointer to memory region structure that describe memory region
1104 * on error (e.g. currently no memory region using memslot as a KVM
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()
1121 TEST_FAIL("Mem region not found"); in memslot2region()
1126 * VM Memory Region Flags Set
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, &region->region); in vm_mem_region_set_flags()
1156 * VM Memory Region Move
1160 * slot - Slot of the memory region to move
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, &region->region); in vm_mem_region_move()
1186 * VM Memory Region Delete
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()
1440 * a page. The allocated physical space comes from the TEST_DATA memory region.
1536 * Locates the memory region containing the VM physical address given
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()
1553 return (void *)((uintptr_t)region->host_mem in addr_gpa2hva()
1554 + (gpa - region->region.guest_phys_addr)); in addr_gpa2hva()
1569 * Locates the memory region containing the host virtual address given
1572 * region containing hva exists.
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()
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()
1878 struct userspace_mem_region *region; in vm_dump() local
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()
1997 * memslot - Memory region to allocate page from
2014 struct userspace_mem_region *region; in __vm_phy_pages_alloc() local
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()
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()
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()
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()
2271 return sparsebit_is_set(region->protected_phy_pages, pg); in vm_is_gpa_protected()