1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/kvm_util.c
5 * Copyright (C) 2018, Google LLC.
10 #include "kvm_util_internal.h"
11 #include "processor.h"
15 #include <sys/types.h>
17 #include <linux/kernel.h>
19 #define KVM_UTIL_PGS_PER_HUGEPG 512
20 #define KVM_UTIL_MIN_PFN 2
22 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
23 static void *align(void *x, size_t size)
25 size_t mask = size - 1;
26 TEST_ASSERT(size != 0 && !(size & (size - 1)),
27 "size not a power of 2: %lu", size);
28 return (void *) (((size_t) x + mask) & ~mask);
40 * On success, the Value corresponding to the capability (KVM_CAP_*)
41 * specified by the value of cap. On failure a TEST_ASSERT failure
44 * Looks up and returns the value corresponding to the capability
45 * (KVM_CAP_*) given by cap.
47 int kvm_check_cap(long cap)
52 kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
56 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
57 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
58 " rc: %i errno: %i", ret, errno);
65 /* VM Enable Capability
68 * vm - Virtual Machine
73 * Return: On success, 0. On failure a TEST_ASSERT failure is produced.
75 * Enables a capability (KVM_CAP_*) on the VM.
77 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
81 ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
82 TEST_ASSERT(ret == 0, "KVM_ENABLE_CAP IOCTL failed,\n"
83 " rc: %i errno: %i", ret, errno);
88 static void vm_open(struct kvm_vm *vm, int perm)
90 vm->kvm_fd = open(KVM_DEV_PATH, perm);
94 if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) {
95 print_skip("immediate_exit not available");
99 vm->fd = ioctl(vm->kvm_fd, KVM_CREATE_VM, vm->type);
100 TEST_ASSERT(vm->fd >= 0, "KVM_CREATE_VM ioctl failed, "
101 "rc: %i errno: %i", vm->fd, errno);
104 const char * const vm_guest_mode_string[] = {
105 "PA-bits:52, VA-bits:48, 4K pages",
106 "PA-bits:52, VA-bits:48, 64K pages",
107 "PA-bits:48, VA-bits:48, 4K pages",
108 "PA-bits:48, VA-bits:48, 64K pages",
109 "PA-bits:40, VA-bits:48, 4K pages",
110 "PA-bits:40, VA-bits:48, 64K pages",
111 "PA-bits:ANY, VA-bits:48, 4K pages",
113 _Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
114 "Missing new mode strings?");
116 struct vm_guest_mode_params {
117 unsigned int pa_bits;
118 unsigned int va_bits;
119 unsigned int page_size;
120 unsigned int page_shift;
123 static const struct vm_guest_mode_params vm_guest_mode_params[] = {
124 { 52, 48, 0x1000, 12 },
125 { 52, 48, 0x10000, 16 },
126 { 48, 48, 0x1000, 12 },
127 { 48, 48, 0x10000, 16 },
128 { 40, 48, 0x1000, 12 },
129 { 40, 48, 0x10000, 16 },
130 { 0, 0, 0x1000, 12 },
132 _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
133 "Missing new mode params?");
139 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
140 * phy_pages - Physical memory pages
146 * Pointer to opaque structure that describes the created VM.
148 * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K).
149 * When phy_pages is non-zero, a memory region of phy_pages physical pages
150 * is created and mapped starting at guest physical address 0. The file
151 * descriptor to control the created VM is created with the permissions
152 * given by perm (e.g. O_RDWR).
154 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
158 pr_debug("%s: mode='%s' pages='%ld' perm='%d'\n", __func__,
159 vm_guest_mode_string(mode), phy_pages, perm);
161 vm = calloc(1, sizeof(*vm));
162 TEST_ASSERT(vm != NULL, "Insufficient Memory");
164 INIT_LIST_HEAD(&vm->vcpus);
165 INIT_LIST_HEAD(&vm->userspace_mem_regions);
170 vm->pa_bits = vm_guest_mode_params[mode].pa_bits;
171 vm->va_bits = vm_guest_mode_params[mode].va_bits;
172 vm->page_size = vm_guest_mode_params[mode].page_size;
173 vm->page_shift = vm_guest_mode_params[mode].page_shift;
175 /* Setup mode specific traits. */
177 case VM_MODE_P52V48_4K:
178 vm->pgtable_levels = 4;
180 case VM_MODE_P52V48_64K:
181 vm->pgtable_levels = 3;
183 case VM_MODE_P48V48_4K:
184 vm->pgtable_levels = 4;
186 case VM_MODE_P48V48_64K:
187 vm->pgtable_levels = 3;
189 case VM_MODE_P40V48_4K:
190 vm->pgtable_levels = 4;
192 case VM_MODE_P40V48_64K:
193 vm->pgtable_levels = 3;
195 case VM_MODE_PXXV48_4K:
197 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
199 * Ignore KVM support for 5-level paging (vm->va_bits == 57),
200 * it doesn't take effect unless a CR4.LA57 is set, which it
201 * isn't for this VM_MODE.
203 TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57,
204 "Linear address width (%d bits) not supported",
206 pr_debug("Guest physical address width detected: %d\n",
208 vm->pgtable_levels = 4;
211 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
215 TEST_FAIL("Unknown guest mode, mode: 0x%x", mode);
219 if (vm->pa_bits != 40)
220 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
225 /* Limit to VA-bit canonical virtual addresses. */
226 vm->vpages_valid = sparsebit_alloc();
227 sparsebit_set_num(vm->vpages_valid,
228 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
229 sparsebit_set_num(vm->vpages_valid,
230 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
231 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
233 /* Limit physical addresses to PA-bits. */
234 vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
236 /* Allocate and setup memory for guest. */
237 vm->vpages_mapped = sparsebit_alloc();
239 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
245 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
247 return _vm_create(mode, phy_pages, perm);
254 * vm - VM that has been released before
259 * Reopens the file descriptors associated to the VM and reinstates the
260 * global state, such as the irqchip and the memory regions that are mapped
263 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
265 struct userspace_mem_region *region;
268 if (vmp->has_irqchip)
269 vm_create_irqchip(vmp);
271 list_for_each_entry(region, &vmp->userspace_mem_regions, list) {
272 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
273 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
274 " rc: %i errno: %i\n"
275 " slot: %u flags: 0x%x\n"
276 " guest_phys_addr: 0x%llx size: 0x%llx",
277 ret, errno, region->region.slot,
278 region->region.flags,
279 region->region.guest_phys_addr,
280 region->region.memory_size);
284 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
286 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
289 ret = ioctl(vm->fd, KVM_GET_DIRTY_LOG, &args);
290 TEST_ASSERT(ret == 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
291 __func__, strerror(-ret));
294 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
295 uint64_t first_page, uint32_t num_pages)
297 struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
298 .first_page = first_page,
299 .num_pages = num_pages };
302 ret = ioctl(vm->fd, KVM_CLEAR_DIRTY_LOG, &args);
303 TEST_ASSERT(ret == 0, "%s: KVM_CLEAR_DIRTY_LOG failed: %s",
304 __func__, strerror(-ret));
308 * Userspace Memory Region Find
311 * vm - Virtual Machine
312 * start - Starting VM physical address
313 * end - Ending VM physical address, inclusive.
318 * Pointer to overlapping region, NULL if no such region.
320 * Searches for a region with any physical memory that overlaps with
321 * any portion of the guest physical addresses from start to end
322 * inclusive. If multiple overlapping regions exist, a pointer to any
323 * of the regions is returned. Null is returned only when no overlapping
326 static struct userspace_mem_region *
327 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
329 struct userspace_mem_region *region;
331 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
332 uint64_t existing_start = region->region.guest_phys_addr;
333 uint64_t existing_end = region->region.guest_phys_addr
334 + region->region.memory_size - 1;
335 if (start <= existing_end && end >= existing_start)
343 * KVM Userspace Memory Region Find
346 * vm - Virtual Machine
347 * start - Starting VM physical address
348 * end - Ending VM physical address, inclusive.
353 * Pointer to overlapping region, NULL if no such region.
355 * Public interface to userspace_mem_region_find. Allows tests to look up
356 * the memslot datastructure for a given range of guest physical memory.
358 struct kvm_userspace_memory_region *
359 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
362 struct userspace_mem_region *region;
364 region = userspace_mem_region_find(vm, start, end);
368 return ®ion->region;
375 * vm - Virtual Machine
381 * Pointer to VCPU structure
383 * Locates a vcpu structure that describes the VCPU specified by vcpuid and
384 * returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
385 * for the specified vcpuid.
387 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
391 list_for_each_entry(vcpu, &vm->vcpus, list) {
392 if (vcpu->id == vcpuid)
403 * vcpu - VCPU to remove
407 * Return: None, TEST_ASSERT failures for all error conditions
409 * Removes a vCPU from a VM and frees its resources.
411 static void vm_vcpu_rm(struct vcpu *vcpu)
415 ret = munmap(vcpu->state, sizeof(*vcpu->state));
416 TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
417 "errno: %i", ret, errno);
419 TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
420 "errno: %i", ret, errno);
422 list_del(&vcpu->list);
426 void kvm_vm_release(struct kvm_vm *vmp)
428 struct vcpu *vcpu, *tmp;
431 list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list)
434 ret = close(vmp->fd);
435 TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
436 " vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
439 TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
440 " vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
443 static void __vm_mem_region_delete(struct kvm_vm *vm,
444 struct userspace_mem_region *region)
448 list_del(®ion->list);
450 region->region.memory_size = 0;
451 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
452 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
453 "rc: %i errno: %i", ret, errno);
455 sparsebit_free(®ion->unused_phy_pages);
456 ret = munmap(region->mmap_start, region->mmap_size);
457 TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i", ret, errno);
463 * Destroys and frees the VM pointed to by vmp.
465 void kvm_vm_free(struct kvm_vm *vmp)
467 struct userspace_mem_region *region, *tmp;
472 /* Free userspace_mem_regions. */
473 list_for_each_entry_safe(region, tmp, &vmp->userspace_mem_regions, list)
474 __vm_mem_region_delete(vmp, region);
476 /* Free sparsebit arrays. */
477 sparsebit_free(&vmp->vpages_valid);
478 sparsebit_free(&vmp->vpages_mapped);
482 /* Free the structure describing the VM. */
487 * Memory Compare, host virtual to guest virtual
490 * hva - Starting host virtual address
491 * vm - Virtual Machine
492 * gva - Starting guest virtual address
493 * len - number of bytes to compare
497 * Input/Output Args: None
500 * Returns 0 if the bytes starting at hva for a length of len
501 * are equal the guest virtual bytes starting at gva. Returns
502 * a value < 0, if bytes at hva are less than those at gva.
503 * Otherwise a value > 0 is returned.
505 * Compares the bytes starting at the host virtual address hva, for
506 * a length of len, to the guest bytes starting at the guest virtual
507 * address given by gva.
509 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
514 * Compare a batch of bytes until either a match is found
515 * or all the bytes have been compared.
517 for (uintptr_t offset = 0; offset < len; offset += amt) {
518 uintptr_t ptr1 = (uintptr_t)hva + offset;
521 * Determine host address for guest virtual address
524 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
527 * Determine amount to compare on this pass.
528 * Don't allow the comparsion to cross a page boundary.
531 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
532 amt = vm->page_size - (ptr1 % vm->page_size);
533 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
534 amt = vm->page_size - (ptr2 % vm->page_size);
536 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
537 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
540 * Perform the comparison. If there is a difference
541 * return that result to the caller, otherwise need
542 * to continue on looking for a mismatch.
544 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
550 * No mismatch found. Let the caller know the two memory
557 * VM Userspace Memory Region Add
560 * vm - Virtual Machine
561 * backing_src - Storage source for this region.
562 * NULL to use anonymous memory.
563 * guest_paddr - Starting guest physical address
564 * slot - KVM region slot
565 * npages - Number of physical pages
566 * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
572 * Allocates a memory area of the number of pages specified by npages
573 * and maps it to the VM specified by vm, at a starting physical address
574 * given by guest_paddr. The region is created with a KVM region slot
575 * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The
576 * region is created with the flags given by flags.
578 void vm_userspace_mem_region_add(struct kvm_vm *vm,
579 enum vm_mem_backing_src_type src_type,
580 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
584 struct userspace_mem_region *region;
585 size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
588 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
589 "Number of guest pages is not compatible with the host. "
590 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
592 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
593 "address not on a page boundary.\n"
594 " guest_paddr: 0x%lx vm->page_size: 0x%x",
595 guest_paddr, vm->page_size);
596 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
597 <= vm->max_gfn, "Physical range beyond maximum "
598 "supported physical address,\n"
599 " guest_paddr: 0x%lx npages: 0x%lx\n"
600 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
601 guest_paddr, npages, vm->max_gfn, vm->page_size);
604 * Confirm a mem region with an overlapping address doesn't
607 region = (struct userspace_mem_region *) userspace_mem_region_find(
608 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
610 TEST_FAIL("overlapping userspace_mem_region already "
612 " requested guest_paddr: 0x%lx npages: 0x%lx "
614 " existing guest_paddr: 0x%lx size: 0x%lx",
615 guest_paddr, npages, vm->page_size,
616 (uint64_t) region->region.guest_phys_addr,
617 (uint64_t) region->region.memory_size);
619 /* Confirm no region with the requested slot already exists. */
620 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
621 if (region->region.slot != slot)
624 TEST_FAIL("A mem region with the requested slot "
626 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
627 " existing slot: %u paddr: 0x%lx size: 0x%lx",
628 slot, guest_paddr, npages,
630 (uint64_t) region->region.guest_phys_addr,
631 (uint64_t) region->region.memory_size);
634 /* Allocate and initialize new mem region structure. */
635 region = calloc(1, sizeof(*region));
636 TEST_ASSERT(region != NULL, "Insufficient Memory");
637 region->mmap_size = npages * vm->page_size;
640 /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
641 alignment = 0x100000;
646 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
647 alignment = max(huge_page_size, alignment);
649 /* Add enough memory to align up if necessary */
651 region->mmap_size += alignment;
653 region->mmap_start = mmap(NULL, region->mmap_size,
654 PROT_READ | PROT_WRITE,
655 MAP_PRIVATE | MAP_ANONYMOUS
656 | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
658 TEST_ASSERT(region->mmap_start != MAP_FAILED,
659 "test_malloc failed, mmap_start: %p errno: %i",
660 region->mmap_start, errno);
662 /* Align host address */
663 region->host_mem = align(region->mmap_start, alignment);
665 /* As needed perform madvise */
666 if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
667 ret = madvise(region->host_mem, npages * vm->page_size,
668 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
669 TEST_ASSERT(ret == 0, "madvise failed,\n"
673 region->host_mem, npages * vm->page_size, src_type);
676 region->unused_phy_pages = sparsebit_alloc();
677 sparsebit_set_num(region->unused_phy_pages,
678 guest_paddr >> vm->page_shift, npages);
679 region->region.slot = slot;
680 region->region.flags = flags;
681 region->region.guest_phys_addr = guest_paddr;
682 region->region.memory_size = npages * vm->page_size;
683 region->region.userspace_addr = (uintptr_t) region->host_mem;
684 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
685 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
686 " rc: %i errno: %i\n"
687 " slot: %u flags: 0x%x\n"
688 " guest_phys_addr: 0x%lx size: 0x%lx",
689 ret, errno, slot, flags,
690 guest_paddr, (uint64_t) region->region.memory_size);
692 /* Add to linked-list of memory regions. */
693 list_add(®ion->list, &vm->userspace_mem_regions);
700 * vm - Virtual Machine
701 * memslot - KVM memory slot ID
706 * Pointer to memory region structure that describe memory region
707 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
708 * on error (e.g. currently no memory region using memslot as a KVM
711 struct userspace_mem_region *
712 memslot2region(struct kvm_vm *vm, uint32_t memslot)
714 struct userspace_mem_region *region;
716 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
717 if (region->region.slot == memslot)
721 fprintf(stderr, "No mem region with the requested slot found,\n"
722 " requested slot: %u\n", memslot);
723 fputs("---- vm dump ----\n", stderr);
724 vm_dump(stderr, vm, 2);
725 TEST_FAIL("Mem region not found");
730 * VM Memory Region Flags Set
733 * vm - Virtual Machine
734 * flags - Starting guest physical address
740 * Sets the flags of the memory region specified by the value of slot,
741 * to the values given by flags.
743 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
746 struct userspace_mem_region *region;
748 region = memslot2region(vm, slot);
750 region->region.flags = flags;
752 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
754 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
755 " rc: %i errno: %i slot: %u flags: 0x%x",
756 ret, errno, slot, flags);
760 * VM Memory Region Move
763 * vm - Virtual Machine
764 * slot - Slot of the memory region to move
765 * new_gpa - Starting guest physical address
771 * Change the gpa of a memory region.
773 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
775 struct userspace_mem_region *region;
778 region = memslot2region(vm, slot);
780 region->region.guest_phys_addr = new_gpa;
782 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
784 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION failed\n"
785 "ret: %i errno: %i slot: %u new_gpa: 0x%lx",
786 ret, errno, slot, new_gpa);
790 * VM Memory Region Delete
793 * vm - Virtual Machine
794 * slot - Slot of the memory region to delete
800 * Delete a memory region.
802 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)
804 __vm_mem_region_delete(vm, memslot2region(vm, slot));
817 * Returns the size of the structure pointed to by the return value
820 static int vcpu_mmap_sz(void)
824 dev_fd = open(KVM_DEV_PATH, O_RDONLY);
828 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
829 TEST_ASSERT(ret >= sizeof(struct kvm_run),
830 "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
831 __func__, ret, errno);
842 * vm - Virtual Machine
849 * Adds a virtual CPU to the VM specified by vm with the ID given by vcpuid.
850 * No additional VCPU setup is done.
852 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
856 /* Confirm a vcpu with the specified id doesn't already exist. */
857 vcpu = vcpu_find(vm, vcpuid);
859 TEST_FAIL("vcpu with the specified id "
861 " requested vcpuid: %u\n"
862 " existing vcpuid: %u state: %p",
863 vcpuid, vcpu->id, vcpu->state);
865 /* Allocate and initialize new vcpu structure. */
866 vcpu = calloc(1, sizeof(*vcpu));
867 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
869 vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
870 TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
873 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
874 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
875 vcpu_mmap_sz(), sizeof(*vcpu->state));
876 vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
877 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
878 TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
879 "vcpu id: %u errno: %i", vcpuid, errno);
881 /* Add to linked-list of VCPUs. */
882 list_add(&vcpu->list, &vm->vcpus);
886 * VM Virtual Address Unused Gap
889 * vm - Virtual Machine
891 * vaddr_min - Minimum Virtual Address
896 * Lowest virtual address at or below vaddr_min, with at least
897 * sz unused bytes. TEST_ASSERT failure if no area of at least
898 * size sz is available.
900 * Within the VM specified by vm, locates the lowest starting virtual
901 * address >= vaddr_min, that has at least sz unallocated bytes. A
902 * TEST_ASSERT failure occurs for invalid input or no area of at least
903 * sz unallocated bytes >= vaddr_min is available.
905 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
906 vm_vaddr_t vaddr_min)
908 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
910 /* Determine lowest permitted virtual page index. */
911 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
912 if ((pgidx_start * vm->page_size) < vaddr_min)
915 /* Loop over section with enough valid virtual page indexes. */
916 if (!sparsebit_is_set_num(vm->vpages_valid,
918 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
922 * Are there enough unused virtual pages available at
923 * the currently proposed starting virtual page index.
924 * If not, adjust proposed starting index to next
927 if (sparsebit_is_clear_num(vm->vpages_mapped,
930 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
932 if (pgidx_start == 0)
936 * If needed, adjust proposed starting virtual address,
937 * to next range of valid virtual addresses.
939 if (!sparsebit_is_set_num(vm->vpages_valid,
940 pgidx_start, pages)) {
941 pgidx_start = sparsebit_next_set_num(
942 vm->vpages_valid, pgidx_start, pages);
943 if (pgidx_start == 0)
946 } while (pgidx_start != 0);
949 TEST_FAIL("No vaddr of specified pages available, pages: 0x%lx", pages);
955 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
957 "Unexpected, invalid virtual page index range,\n"
958 " pgidx_start: 0x%lx\n"
961 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
963 "Unexpected, pages already mapped,\n"
964 " pgidx_start: 0x%lx\n"
968 return pgidx_start * vm->page_size;
972 * VM Virtual Address Allocate
975 * vm - Virtual Machine
977 * vaddr_min - Minimum starting virtual address
978 * data_memslot - Memory region slot for data pages
979 * pgd_memslot - Memory region slot for new virtual translation tables
984 * Starting guest virtual address
986 * Allocates at least sz bytes within the virtual address space of the vm
987 * given by vm. The allocated bytes are mapped to a virtual address >=
988 * the address given by vaddr_min. Note that each allocation uses a
989 * a unique set of pages, with the minimum real allocation being at least
992 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
993 uint32_t data_memslot, uint32_t pgd_memslot)
995 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
997 virt_pgd_alloc(vm, pgd_memslot);
1000 * Find an unused range of virtual page addresses of at least
1003 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
1005 /* Map the virtual pages. */
1006 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
1007 pages--, vaddr += vm->page_size) {
1010 paddr = vm_phy_page_alloc(vm,
1011 KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
1013 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1015 sparsebit_set(vm->vpages_mapped,
1016 vaddr >> vm->page_shift);
1023 * Map a range of VM virtual address to the VM's physical address
1026 * vm - Virtual Machine
1027 * vaddr - Virtuall address to map
1028 * paddr - VM Physical Address
1029 * npages - The number of pages to map
1030 * pgd_memslot - Memory region slot for new virtual translation tables
1036 * Within the VM given by @vm, creates a virtual translation for
1037 * @npages starting at @vaddr to the page range starting at @paddr.
1039 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1040 unsigned int npages, uint32_t pgd_memslot)
1042 size_t page_size = vm->page_size;
1043 size_t size = npages * page_size;
1045 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1046 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1049 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1056 * Address VM Physical to Host Virtual
1059 * vm - Virtual Machine
1060 * gpa - VM physical address
1065 * Equivalent host virtual address
1067 * Locates the memory region containing the VM physical address given
1068 * by gpa, within the VM given by vm. When found, the host virtual
1069 * address providing the memory to the vm physical address is returned.
1070 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1072 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1074 struct userspace_mem_region *region;
1076 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
1077 if ((gpa >= region->region.guest_phys_addr)
1078 && (gpa <= (region->region.guest_phys_addr
1079 + region->region.memory_size - 1)))
1080 return (void *) ((uintptr_t) region->host_mem
1081 + (gpa - region->region.guest_phys_addr));
1084 TEST_FAIL("No vm physical memory at 0x%lx", gpa);
1089 * Address Host Virtual to VM Physical
1092 * vm - Virtual Machine
1093 * hva - Host virtual address
1098 * Equivalent VM physical address
1100 * Locates the memory region containing the host virtual address given
1101 * by hva, within the VM given by vm. When found, the equivalent
1102 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1103 * region containing hva exists.
1105 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1107 struct userspace_mem_region *region;
1109 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
1110 if ((hva >= region->host_mem)
1111 && (hva <= (region->host_mem
1112 + region->region.memory_size - 1)))
1113 return (vm_paddr_t) ((uintptr_t)
1114 region->region.guest_phys_addr
1115 + (hva - (uintptr_t) region->host_mem));
1118 TEST_FAIL("No mapping to a guest physical address, hva: %p", hva);
1123 * VM Create IRQ Chip
1126 * vm - Virtual Machine
1132 * Creates an interrupt controller chip for the VM specified by vm.
1134 void vm_create_irqchip(struct kvm_vm *vm)
1138 ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
1139 TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1140 "rc: %i errno: %i", ret, errno);
1142 vm->has_irqchip = true;
1149 * vm - Virtual Machine
1155 * Pointer to structure that describes the state of the VCPU.
1157 * Locates and returns a pointer to a structure that describes the
1158 * state of the VCPU with the given vcpuid.
1160 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1162 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1163 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1172 * vm - Virtual Machine
1179 * Switch to executing the code for the VCPU given by vcpuid, within the VM
1182 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1184 int ret = _vcpu_run(vm, vcpuid);
1185 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1186 "rc: %i errno: %i", ret, errno);
1189 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1191 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1194 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1196 rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1197 } while (rc == -1 && errno == EINTR);
1201 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1203 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1206 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1208 vcpu->state->immediate_exit = 1;
1209 ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1210 vcpu->state->immediate_exit = 0;
1212 TEST_ASSERT(ret == -1 && errno == EINTR,
1213 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1217 void vcpu_set_guest_debug(struct kvm_vm *vm, uint32_t vcpuid,
1218 struct kvm_guest_debug *debug)
1220 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1221 int ret = ioctl(vcpu->fd, KVM_SET_GUEST_DEBUG, debug);
1223 TEST_ASSERT(ret == 0, "KVM_SET_GUEST_DEBUG failed: %d", ret);
1227 * VM VCPU Set MP State
1230 * vm - Virtual Machine
1232 * mp_state - mp_state to be set
1238 * Sets the MP state of the VCPU given by vcpuid, to the state given
1241 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1242 struct kvm_mp_state *mp_state)
1244 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1247 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1249 ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
1250 TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
1251 "rc: %i errno: %i", ret, errno);
1258 * vm - Virtual Machine
1262 * regs - current state of VCPU regs
1266 * Obtains the current register state for the VCPU specified by vcpuid
1267 * and stores it at the location given by regs.
1269 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1271 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1274 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1276 ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1277 TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1285 * vm - Virtual Machine
1287 * regs - Values to set VCPU regs to
1293 * Sets the regs of the VCPU specified by vcpuid to the values
1296 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1298 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1301 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1303 ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1304 TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1308 #ifdef __KVM_HAVE_VCPU_EVENTS
1309 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1310 struct kvm_vcpu_events *events)
1312 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1315 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1317 ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1318 TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1322 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1323 struct kvm_vcpu_events *events)
1325 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1328 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1330 ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
1331 TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1337 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1338 struct kvm_nested_state *state)
1340 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1343 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1345 ret = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, state);
1346 TEST_ASSERT(ret == 0,
1347 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1351 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1352 struct kvm_nested_state *state, bool ignore_error)
1354 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1357 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1359 ret = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, state);
1360 if (!ignore_error) {
1361 TEST_ASSERT(ret == 0,
1362 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1371 * VM VCPU System Regs Get
1374 * vm - Virtual Machine
1378 * sregs - current state of VCPU system regs
1382 * Obtains the current system register state for the VCPU specified by
1383 * vcpuid and stores it at the location given by sregs.
1385 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1387 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1390 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1392 ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1393 TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1398 * VM VCPU System Regs Set
1401 * vm - Virtual Machine
1403 * sregs - Values to set VCPU system regs to
1409 * Sets the system regs of the VCPU specified by vcpuid to the values
1412 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1414 int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
1415 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1416 "rc: %i errno: %i", ret, errno);
1419 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1421 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1423 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1425 return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1428 void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1432 ret = _vcpu_ioctl(vm, vcpuid, KVM_GET_FPU, fpu);
1433 TEST_ASSERT(ret == 0, "KVM_GET_FPU failed, rc: %i errno: %i (%s)",
1434 ret, errno, strerror(errno));
1437 void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1441 ret = _vcpu_ioctl(vm, vcpuid, KVM_SET_FPU, fpu);
1442 TEST_ASSERT(ret == 0, "KVM_SET_FPU failed, rc: %i errno: %i (%s)",
1443 ret, errno, strerror(errno));
1446 void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1450 ret = _vcpu_ioctl(vm, vcpuid, KVM_GET_ONE_REG, reg);
1451 TEST_ASSERT(ret == 0, "KVM_GET_ONE_REG failed, rc: %i errno: %i (%s)",
1452 ret, errno, strerror(errno));
1455 void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1459 ret = _vcpu_ioctl(vm, vcpuid, KVM_SET_ONE_REG, reg);
1460 TEST_ASSERT(ret == 0, "KVM_SET_ONE_REG failed, rc: %i errno: %i (%s)",
1461 ret, errno, strerror(errno));
1468 * vm - Virtual Machine
1470 * cmd - Ioctl number
1471 * arg - Argument to pass to the ioctl
1475 * Issues an arbitrary ioctl on a VCPU fd.
1477 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1478 unsigned long cmd, void *arg)
1482 ret = _vcpu_ioctl(vm, vcpuid, cmd, arg);
1483 TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1484 cmd, ret, errno, strerror(errno));
1487 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1488 unsigned long cmd, void *arg)
1490 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1493 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1495 ret = ioctl(vcpu->fd, cmd, arg);
1504 * vm - Virtual Machine
1505 * cmd - Ioctl number
1506 * arg - Argument to pass to the ioctl
1510 * Issues an arbitrary ioctl on a VM fd.
1512 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1516 ret = ioctl(vm->fd, cmd, arg);
1517 TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1518 cmd, ret, errno, strerror(errno));
1525 * vm - Virtual Machine
1526 * indent - Left margin indent amount
1529 * stream - Output FILE stream
1533 * Dumps the current state of the VM given by vm, to the FILE stream
1536 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1538 struct userspace_mem_region *region;
1541 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1542 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1543 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1544 fprintf(stream, "%*sMem Regions:\n", indent, "");
1545 list_for_each_entry(region, &vm->userspace_mem_regions, list) {
1546 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1547 "host_virt: %p\n", indent + 2, "",
1548 (uint64_t) region->region.guest_phys_addr,
1549 (uint64_t) region->region.memory_size,
1551 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1552 sparsebit_dump(stream, region->unused_phy_pages, 0);
1554 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1555 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1556 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1558 if (vm->pgd_created) {
1559 fprintf(stream, "%*sVirtual Translation Tables:\n",
1561 virt_dump(stream, vm, indent + 4);
1563 fprintf(stream, "%*sVCPUs:\n", indent, "");
1564 list_for_each_entry(vcpu, &vm->vcpus, list)
1565 vcpu_dump(stream, vm, vcpu->id, indent + 2);
1568 /* Known KVM exit reasons */
1569 static struct exit_reason {
1570 unsigned int reason;
1572 } exit_reasons_known[] = {
1573 {KVM_EXIT_UNKNOWN, "UNKNOWN"},
1574 {KVM_EXIT_EXCEPTION, "EXCEPTION"},
1575 {KVM_EXIT_IO, "IO"},
1576 {KVM_EXIT_HYPERCALL, "HYPERCALL"},
1577 {KVM_EXIT_DEBUG, "DEBUG"},
1578 {KVM_EXIT_HLT, "HLT"},
1579 {KVM_EXIT_MMIO, "MMIO"},
1580 {KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
1581 {KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
1582 {KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
1583 {KVM_EXIT_INTR, "INTR"},
1584 {KVM_EXIT_SET_TPR, "SET_TPR"},
1585 {KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
1586 {KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
1587 {KVM_EXIT_S390_RESET, "S390_RESET"},
1588 {KVM_EXIT_DCR, "DCR"},
1589 {KVM_EXIT_NMI, "NMI"},
1590 {KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
1591 {KVM_EXIT_OSI, "OSI"},
1592 {KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
1593 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1594 {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
1599 * Exit Reason String
1602 * exit_reason - Exit reason
1607 * Constant string pointer describing the exit reason.
1609 * Locates and returns a constant string that describes the KVM exit
1610 * reason given by exit_reason. If no such string is found, a constant
1611 * string of "Unknown" is returned.
1613 const char *exit_reason_str(unsigned int exit_reason)
1617 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1618 if (exit_reason == exit_reasons_known[n1].reason)
1619 return exit_reasons_known[n1].name;
1626 * Physical Contiguous Page Allocator
1629 * vm - Virtual Machine
1630 * num - number of pages
1631 * paddr_min - Physical address minimum
1632 * memslot - Memory region to allocate page from
1637 * Starting physical address
1639 * Within the VM specified by vm, locates a range of available physical
1640 * pages at or above paddr_min. If found, the pages are marked as in use
1641 * and their base address is returned. A TEST_ASSERT failure occurs if
1642 * not enough pages are available at or above paddr_min.
1644 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1645 vm_paddr_t paddr_min, uint32_t memslot)
1647 struct userspace_mem_region *region;
1648 sparsebit_idx_t pg, base;
1650 TEST_ASSERT(num > 0, "Must allocate at least one page");
1652 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1653 "not divisible by page size.\n"
1654 " paddr_min: 0x%lx page_size: 0x%x",
1655 paddr_min, vm->page_size);
1657 region = memslot2region(vm, memslot);
1658 base = pg = paddr_min >> vm->page_shift;
1661 for (; pg < base + num; ++pg) {
1662 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
1663 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
1667 } while (pg && pg != base + num);
1670 fprintf(stderr, "No guest physical page available, "
1671 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1672 paddr_min, vm->page_size, memslot);
1673 fputs("---- vm dump ----\n", stderr);
1674 vm_dump(stderr, vm, 2);
1678 for (pg = base; pg < base + num; ++pg)
1679 sparsebit_clear(region->unused_phy_pages, pg);
1681 return base * vm->page_size;
1684 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1687 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1691 * Address Guest Virtual to Host Virtual
1694 * vm - Virtual Machine
1695 * gva - VM virtual address
1700 * Equivalent host virtual address
1702 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1704 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1708 * Is Unrestricted Guest
1711 * vm - Virtual Machine
1715 * Return: True if the unrestricted guest is set to 'Y', otherwise return false.
1717 * Check if the unrestricted guest flag is enabled.
1719 bool vm_is_unrestricted_guest(struct kvm_vm *vm)
1726 /* Ensure that the KVM vendor-specific module is loaded. */
1727 f = fopen(KVM_DEV_PATH, "r");
1728 TEST_ASSERT(f != NULL, "Error in opening KVM dev file: %d",
1733 f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1735 count = fread(&val, sizeof(char), 1, f);
1736 TEST_ASSERT(count == 1, "Unable to read from param file.");
1743 unsigned int vm_get_page_size(struct kvm_vm *vm)
1745 return vm->page_size;
1748 unsigned int vm_get_page_shift(struct kvm_vm *vm)
1750 return vm->page_shift;
1753 unsigned int vm_get_max_gfn(struct kvm_vm *vm)
1758 int vm_get_fd(struct kvm_vm *vm)
1763 static unsigned int vm_calc_num_pages(unsigned int num_pages,
1764 unsigned int page_shift,
1765 unsigned int new_page_shift,
1768 unsigned int n = 1 << (new_page_shift - page_shift);
1770 if (page_shift >= new_page_shift)
1771 return num_pages * (1 << (page_shift - new_page_shift));
1773 return num_pages / n + !!(ceil && num_pages % n);
1776 static inline int getpageshift(void)
1778 return __builtin_ffs(getpagesize()) - 1;
1782 vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
1784 return vm_calc_num_pages(num_guest_pages,
1785 vm_guest_mode_params[mode].page_shift,
1786 getpageshift(), true);
1790 vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
1792 return vm_calc_num_pages(num_host_pages, getpageshift(),
1793 vm_guest_mode_params[mode].page_shift, false);
1796 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
1799 n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
1800 return vm_adjust_num_guest_pages(mode, n);