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"
14 #include <sys/types.h>
16 #include <linux/kernel.h>
18 #define KVM_UTIL_PGS_PER_HUGEPG 512
19 #define KVM_UTIL_MIN_PFN 2
21 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
22 static void *align(void *x, size_t size)
24 size_t mask = size - 1;
25 TEST_ASSERT(size != 0 && !(size & (size - 1)),
26 "size not a power of 2: %lu", size);
27 return (void *) (((size_t) x + mask) & ~mask);
39 * On success, the Value corresponding to the capability (KVM_CAP_*)
40 * specified by the value of cap. On failure a TEST_ASSERT failure
43 * Looks up and returns the value corresponding to the capability
44 * (KVM_CAP_*) given by cap.
46 int kvm_check_cap(long cap)
51 kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
55 ret = ioctl(kvm_fd, KVM_CHECK_EXTENSION, cap);
56 TEST_ASSERT(ret != -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
57 " rc: %i errno: %i", ret, errno);
64 /* VM Enable Capability
67 * vm - Virtual Machine
72 * Return: On success, 0. On failure a TEST_ASSERT failure is produced.
74 * Enables a capability (KVM_CAP_*) on the VM.
76 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
80 ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
81 TEST_ASSERT(ret == 0, "KVM_ENABLE_CAP IOCTL failed,\n"
82 " rc: %i errno: %i", ret, errno);
87 static void vm_open(struct kvm_vm *vm, int perm, unsigned long type)
89 vm->kvm_fd = open(KVM_DEV_PATH, perm);
93 if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) {
94 fprintf(stderr, "immediate_exit not available, skipping test\n");
98 vm->fd = ioctl(vm->kvm_fd, KVM_CREATE_VM, type);
99 TEST_ASSERT(vm->fd >= 0, "KVM_CREATE_VM ioctl failed, "
100 "rc: %i errno: %i", vm->fd, errno);
103 const char * const vm_guest_mode_string[] = {
104 "PA-bits:52, VA-bits:48, 4K pages",
105 "PA-bits:52, VA-bits:48, 64K pages",
106 "PA-bits:48, VA-bits:48, 4K pages",
107 "PA-bits:48, VA-bits:48, 64K pages",
108 "PA-bits:40, VA-bits:48, 4K pages",
109 "PA-bits:40, VA-bits:48, 64K pages",
111 _Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
112 "Missing new mode strings?");
118 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
119 * phy_pages - Physical memory pages
125 * Pointer to opaque structure that describes the created VM.
127 * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K).
128 * When phy_pages is non-zero, a memory region of phy_pages physical pages
129 * is created and mapped starting at guest physical address 0. The file
130 * descriptor to control the created VM is created with the permissions
131 * given by perm (e.g. O_RDWR).
133 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages,
134 int perm, unsigned long type)
138 vm = calloc(1, sizeof(*vm));
139 TEST_ASSERT(vm != NULL, "Insufficient Memory");
143 vm_open(vm, perm, type);
145 /* Setup mode specific traits. */
147 case VM_MODE_P52V48_4K:
148 vm->pgtable_levels = 4;
151 vm->page_size = 0x1000;
154 case VM_MODE_P52V48_64K:
155 vm->pgtable_levels = 3;
158 vm->page_size = 0x10000;
161 case VM_MODE_P48V48_4K:
162 vm->pgtable_levels = 4;
165 vm->page_size = 0x1000;
168 case VM_MODE_P48V48_64K:
169 vm->pgtable_levels = 3;
172 vm->page_size = 0x10000;
175 case VM_MODE_P40V48_4K:
176 vm->pgtable_levels = 4;
179 vm->page_size = 0x1000;
182 case VM_MODE_P40V48_64K:
183 vm->pgtable_levels = 3;
186 vm->page_size = 0x10000;
190 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
193 /* Limit to VA-bit canonical virtual addresses. */
194 vm->vpages_valid = sparsebit_alloc();
195 sparsebit_set_num(vm->vpages_valid,
196 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
197 sparsebit_set_num(vm->vpages_valid,
198 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
199 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
201 /* Limit physical addresses to PA-bits. */
202 vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
204 /* Allocate and setup memory for guest. */
205 vm->vpages_mapped = sparsebit_alloc();
207 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
213 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
215 return _vm_create(mode, phy_pages, perm, 0);
222 * vm - VM that has been released before
227 * Reopens the file descriptors associated to the VM and reinstates the
228 * global state, such as the irqchip and the memory regions that are mapped
231 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
233 struct userspace_mem_region *region;
235 vm_open(vmp, perm, vmp->type);
236 if (vmp->has_irqchip)
237 vm_create_irqchip(vmp);
239 for (region = vmp->userspace_mem_region_head; region;
240 region = region->next) {
241 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
242 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
243 " rc: %i errno: %i\n"
244 " slot: %u flags: 0x%x\n"
245 " guest_phys_addr: 0x%lx size: 0x%lx",
246 ret, errno, region->region.slot,
247 region->region.flags,
248 region->region.guest_phys_addr,
249 region->region.memory_size);
253 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
255 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
258 ret = ioctl(vm->fd, KVM_GET_DIRTY_LOG, &args);
259 TEST_ASSERT(ret == 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
263 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
264 uint64_t first_page, uint32_t num_pages)
266 struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
267 .first_page = first_page,
268 .num_pages = num_pages };
271 ret = ioctl(vm->fd, KVM_CLEAR_DIRTY_LOG, &args);
272 TEST_ASSERT(ret == 0, "%s: KVM_CLEAR_DIRTY_LOG failed: %s",
277 * Userspace Memory Region Find
280 * vm - Virtual Machine
281 * start - Starting VM physical address
282 * end - Ending VM physical address, inclusive.
287 * Pointer to overlapping region, NULL if no such region.
289 * Searches for a region with any physical memory that overlaps with
290 * any portion of the guest physical addresses from start to end
291 * inclusive. If multiple overlapping regions exist, a pointer to any
292 * of the regions is returned. Null is returned only when no overlapping
295 static struct userspace_mem_region *
296 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
298 struct userspace_mem_region *region;
300 for (region = vm->userspace_mem_region_head; region;
301 region = region->next) {
302 uint64_t existing_start = region->region.guest_phys_addr;
303 uint64_t existing_end = region->region.guest_phys_addr
304 + region->region.memory_size - 1;
305 if (start <= existing_end && end >= existing_start)
313 * KVM Userspace Memory Region Find
316 * vm - Virtual Machine
317 * start - Starting VM physical address
318 * end - Ending VM physical address, inclusive.
323 * Pointer to overlapping region, NULL if no such region.
325 * Public interface to userspace_mem_region_find. Allows tests to look up
326 * the memslot datastructure for a given range of guest physical memory.
328 struct kvm_userspace_memory_region *
329 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
332 struct userspace_mem_region *region;
334 region = userspace_mem_region_find(vm, start, end);
338 return ®ion->region;
345 * vm - Virtual Machine
351 * Pointer to VCPU structure
353 * Locates a vcpu structure that describes the VCPU specified by vcpuid and
354 * returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
355 * for the specified vcpuid.
357 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
361 for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
362 if (vcpup->id == vcpuid)
373 * vm - Virtual Machine
378 * Return: None, TEST_ASSERT failures for all error conditions
380 * Within the VM specified by vm, removes the VCPU given by vcpuid.
382 static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
384 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
387 ret = munmap(vcpu->state, sizeof(*vcpu->state));
388 TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
389 "errno: %i", ret, errno);
391 TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
392 "errno: %i", ret, errno);
395 vcpu->next->prev = vcpu->prev;
397 vcpu->prev->next = vcpu->next;
399 vm->vcpu_head = vcpu->next;
403 void kvm_vm_release(struct kvm_vm *vmp)
407 while (vmp->vcpu_head)
408 vm_vcpu_rm(vmp, vmp->vcpu_head->id);
410 ret = close(vmp->fd);
411 TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
412 " vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
415 TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
416 " vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
420 * Destroys and frees the VM pointed to by vmp.
422 void kvm_vm_free(struct kvm_vm *vmp)
429 /* Free userspace_mem_regions. */
430 while (vmp->userspace_mem_region_head) {
431 struct userspace_mem_region *region
432 = vmp->userspace_mem_region_head;
434 region->region.memory_size = 0;
435 ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
437 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
438 "rc: %i errno: %i", ret, errno);
440 vmp->userspace_mem_region_head = region->next;
441 sparsebit_free(®ion->unused_phy_pages);
442 ret = munmap(region->mmap_start, region->mmap_size);
443 TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
449 /* Free sparsebit arrays. */
450 sparsebit_free(&vmp->vpages_valid);
451 sparsebit_free(&vmp->vpages_mapped);
455 /* Free the structure describing the VM. */
460 * Memory Compare, host virtual to guest virtual
463 * hva - Starting host virtual address
464 * vm - Virtual Machine
465 * gva - Starting guest virtual address
466 * len - number of bytes to compare
470 * Input/Output Args: None
473 * Returns 0 if the bytes starting at hva for a length of len
474 * are equal the guest virtual bytes starting at gva. Returns
475 * a value < 0, if bytes at hva are less than those at gva.
476 * Otherwise a value > 0 is returned.
478 * Compares the bytes starting at the host virtual address hva, for
479 * a length of len, to the guest bytes starting at the guest virtual
480 * address given by gva.
482 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
487 * Compare a batch of bytes until either a match is found
488 * or all the bytes have been compared.
490 for (uintptr_t offset = 0; offset < len; offset += amt) {
491 uintptr_t ptr1 = (uintptr_t)hva + offset;
494 * Determine host address for guest virtual address
497 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
500 * Determine amount to compare on this pass.
501 * Don't allow the comparsion to cross a page boundary.
504 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
505 amt = vm->page_size - (ptr1 % vm->page_size);
506 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
507 amt = vm->page_size - (ptr2 % vm->page_size);
509 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
510 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
513 * Perform the comparison. If there is a difference
514 * return that result to the caller, otherwise need
515 * to continue on looking for a mismatch.
517 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
523 * No mismatch found. Let the caller know the two memory
530 * VM Userspace Memory Region Add
533 * vm - Virtual Machine
534 * backing_src - Storage source for this region.
535 * NULL to use anonymous memory.
536 * guest_paddr - Starting guest physical address
537 * slot - KVM region slot
538 * npages - Number of physical pages
539 * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
545 * Allocates a memory area of the number of pages specified by npages
546 * and maps it to the VM specified by vm, at a starting physical address
547 * given by guest_paddr. The region is created with a KVM region slot
548 * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The
549 * region is created with the flags given by flags.
551 void vm_userspace_mem_region_add(struct kvm_vm *vm,
552 enum vm_mem_backing_src_type src_type,
553 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
557 struct userspace_mem_region *region;
558 size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
561 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
562 "address not on a page boundary.\n"
563 " guest_paddr: 0x%lx vm->page_size: 0x%x",
564 guest_paddr, vm->page_size);
565 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
566 <= vm->max_gfn, "Physical range beyond maximum "
567 "supported physical address,\n"
568 " guest_paddr: 0x%lx npages: 0x%lx\n"
569 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
570 guest_paddr, npages, vm->max_gfn, vm->page_size);
573 * Confirm a mem region with an overlapping address doesn't
576 region = (struct userspace_mem_region *) userspace_mem_region_find(
577 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
579 TEST_ASSERT(false, "overlapping userspace_mem_region already "
581 " requested guest_paddr: 0x%lx npages: 0x%lx "
583 " existing guest_paddr: 0x%lx size: 0x%lx",
584 guest_paddr, npages, vm->page_size,
585 (uint64_t) region->region.guest_phys_addr,
586 (uint64_t) region->region.memory_size);
588 /* Confirm no region with the requested slot already exists. */
589 for (region = vm->userspace_mem_region_head; region;
590 region = region->next) {
591 if (region->region.slot == slot)
595 TEST_ASSERT(false, "A mem region with the requested slot "
597 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
598 " existing slot: %u paddr: 0x%lx size: 0x%lx",
599 slot, guest_paddr, npages,
601 (uint64_t) region->region.guest_phys_addr,
602 (uint64_t) region->region.memory_size);
604 /* Allocate and initialize new mem region structure. */
605 region = calloc(1, sizeof(*region));
606 TEST_ASSERT(region != NULL, "Insufficient Memory");
607 region->mmap_size = npages * vm->page_size;
610 /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
611 alignment = 0x100000;
616 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
617 alignment = max(huge_page_size, alignment);
619 /* Add enough memory to align up if necessary */
621 region->mmap_size += alignment;
623 region->mmap_start = mmap(NULL, region->mmap_size,
624 PROT_READ | PROT_WRITE,
625 MAP_PRIVATE | MAP_ANONYMOUS
626 | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
628 TEST_ASSERT(region->mmap_start != MAP_FAILED,
629 "test_malloc failed, mmap_start: %p errno: %i",
630 region->mmap_start, errno);
632 /* Align host address */
633 region->host_mem = align(region->mmap_start, alignment);
635 /* As needed perform madvise */
636 if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
637 ret = madvise(region->host_mem, npages * vm->page_size,
638 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
639 TEST_ASSERT(ret == 0, "madvise failed,\n"
643 region->host_mem, npages * vm->page_size, src_type);
646 region->unused_phy_pages = sparsebit_alloc();
647 sparsebit_set_num(region->unused_phy_pages,
648 guest_paddr >> vm->page_shift, npages);
649 region->region.slot = slot;
650 region->region.flags = flags;
651 region->region.guest_phys_addr = guest_paddr;
652 region->region.memory_size = npages * vm->page_size;
653 region->region.userspace_addr = (uintptr_t) region->host_mem;
654 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
655 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
656 " rc: %i errno: %i\n"
657 " slot: %u flags: 0x%x\n"
658 " guest_phys_addr: 0x%lx size: 0x%lx",
659 ret, errno, slot, flags,
660 guest_paddr, (uint64_t) region->region.memory_size);
662 /* Add to linked-list of memory regions. */
663 if (vm->userspace_mem_region_head)
664 vm->userspace_mem_region_head->prev = region;
665 region->next = vm->userspace_mem_region_head;
666 vm->userspace_mem_region_head = region;
673 * vm - Virtual Machine
674 * memslot - KVM memory slot ID
679 * Pointer to memory region structure that describe memory region
680 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
681 * on error (e.g. currently no memory region using memslot as a KVM
684 static struct userspace_mem_region *
685 memslot2region(struct kvm_vm *vm, uint32_t memslot)
687 struct userspace_mem_region *region;
689 for (region = vm->userspace_mem_region_head; region;
690 region = region->next) {
691 if (region->region.slot == memslot)
694 if (region == NULL) {
695 fprintf(stderr, "No mem region with the requested slot found,\n"
696 " requested slot: %u\n", memslot);
697 fputs("---- vm dump ----\n", stderr);
698 vm_dump(stderr, vm, 2);
699 TEST_ASSERT(false, "Mem region not found");
706 * VM Memory Region Flags Set
709 * vm - Virtual Machine
710 * flags - Starting guest physical address
716 * Sets the flags of the memory region specified by the value of slot,
717 * to the values given by flags.
719 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
722 struct userspace_mem_region *region;
724 region = memslot2region(vm, slot);
726 region->region.flags = flags;
728 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
730 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
731 " rc: %i errno: %i slot: %u flags: 0x%x",
732 ret, errno, slot, flags);
745 * Returns the size of the structure pointed to by the return value
748 static int vcpu_mmap_sz(void)
752 dev_fd = open(KVM_DEV_PATH, O_RDONLY);
756 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
757 TEST_ASSERT(ret >= sizeof(struct kvm_run),
758 "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
759 __func__, ret, errno);
770 * vm - Virtual Machine
777 * Adds a virtual CPU to the VM specified by vm with the ID given by vcpuid.
778 * No additional VCPU setup is done.
780 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
784 /* Confirm a vcpu with the specified id doesn't already exist. */
785 vcpu = vcpu_find(vm, vcpuid);
787 TEST_ASSERT(false, "vcpu with the specified id "
789 " requested vcpuid: %u\n"
790 " existing vcpuid: %u state: %p",
791 vcpuid, vcpu->id, vcpu->state);
793 /* Allocate and initialize new vcpu structure. */
794 vcpu = calloc(1, sizeof(*vcpu));
795 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
797 vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
798 TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
801 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
802 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
803 vcpu_mmap_sz(), sizeof(*vcpu->state));
804 vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
805 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
806 TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
807 "vcpu id: %u errno: %i", vcpuid, errno);
809 /* Add to linked-list of VCPUs. */
811 vm->vcpu_head->prev = vcpu;
812 vcpu->next = vm->vcpu_head;
813 vm->vcpu_head = vcpu;
817 * VM Virtual Address Unused Gap
820 * vm - Virtual Machine
822 * vaddr_min - Minimum Virtual Address
827 * Lowest virtual address at or below vaddr_min, with at least
828 * sz unused bytes. TEST_ASSERT failure if no area of at least
829 * size sz is available.
831 * Within the VM specified by vm, locates the lowest starting virtual
832 * address >= vaddr_min, that has at least sz unallocated bytes. A
833 * TEST_ASSERT failure occurs for invalid input or no area of at least
834 * sz unallocated bytes >= vaddr_min is available.
836 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
837 vm_vaddr_t vaddr_min)
839 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
841 /* Determine lowest permitted virtual page index. */
842 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
843 if ((pgidx_start * vm->page_size) < vaddr_min)
846 /* Loop over section with enough valid virtual page indexes. */
847 if (!sparsebit_is_set_num(vm->vpages_valid,
849 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
853 * Are there enough unused virtual pages available at
854 * the currently proposed starting virtual page index.
855 * If not, adjust proposed starting index to next
858 if (sparsebit_is_clear_num(vm->vpages_mapped,
861 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
863 if (pgidx_start == 0)
867 * If needed, adjust proposed starting virtual address,
868 * to next range of valid virtual addresses.
870 if (!sparsebit_is_set_num(vm->vpages_valid,
871 pgidx_start, pages)) {
872 pgidx_start = sparsebit_next_set_num(
873 vm->vpages_valid, pgidx_start, pages);
874 if (pgidx_start == 0)
877 } while (pgidx_start != 0);
880 TEST_ASSERT(false, "No vaddr of specified pages available, "
881 "pages: 0x%lx", pages);
887 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
889 "Unexpected, invalid virtual page index range,\n"
890 " pgidx_start: 0x%lx\n"
893 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
895 "Unexpected, pages already mapped,\n"
896 " pgidx_start: 0x%lx\n"
900 return pgidx_start * vm->page_size;
904 * VM Virtual Address Allocate
907 * vm - Virtual Machine
909 * vaddr_min - Minimum starting virtual address
910 * data_memslot - Memory region slot for data pages
911 * pgd_memslot - Memory region slot for new virtual translation tables
916 * Starting guest virtual address
918 * Allocates at least sz bytes within the virtual address space of the vm
919 * given by vm. The allocated bytes are mapped to a virtual address >=
920 * the address given by vaddr_min. Note that each allocation uses a
921 * a unique set of pages, with the minimum real allocation being at least
924 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
925 uint32_t data_memslot, uint32_t pgd_memslot)
927 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
929 virt_pgd_alloc(vm, pgd_memslot);
932 * Find an unused range of virtual page addresses of at least
935 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
937 /* Map the virtual pages. */
938 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
939 pages--, vaddr += vm->page_size) {
942 paddr = vm_phy_page_alloc(vm,
943 KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
945 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
947 sparsebit_set(vm->vpages_mapped,
948 vaddr >> vm->page_shift);
955 * Map a range of VM virtual address to the VM's physical address
958 * vm - Virtual Machine
959 * vaddr - Virtuall address to map
960 * paddr - VM Physical Address
961 * size - The size of the range to map
962 * pgd_memslot - Memory region slot for new virtual translation tables
968 * Within the VM given by vm, creates a virtual translation for the
969 * page range starting at vaddr to the page range starting at paddr.
971 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
972 size_t size, uint32_t pgd_memslot)
974 size_t page_size = vm->page_size;
975 size_t npages = size / page_size;
977 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
978 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
981 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
988 * Address VM Physical to Host Virtual
991 * vm - Virtual Machine
992 * gpa - VM physical address
997 * Equivalent host virtual address
999 * Locates the memory region containing the VM physical address given
1000 * by gpa, within the VM given by vm. When found, the host virtual
1001 * address providing the memory to the vm physical address is returned.
1002 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1004 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1006 struct userspace_mem_region *region;
1007 for (region = vm->userspace_mem_region_head; region;
1008 region = region->next) {
1009 if ((gpa >= region->region.guest_phys_addr)
1010 && (gpa <= (region->region.guest_phys_addr
1011 + region->region.memory_size - 1)))
1012 return (void *) ((uintptr_t) region->host_mem
1013 + (gpa - region->region.guest_phys_addr));
1016 TEST_ASSERT(false, "No vm physical memory at 0x%lx", gpa);
1021 * Address Host Virtual to VM Physical
1024 * vm - Virtual Machine
1025 * hva - Host virtual address
1030 * Equivalent VM physical address
1032 * Locates the memory region containing the host virtual address given
1033 * by hva, within the VM given by vm. When found, the equivalent
1034 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1035 * region containing hva exists.
1037 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1039 struct userspace_mem_region *region;
1040 for (region = vm->userspace_mem_region_head; region;
1041 region = region->next) {
1042 if ((hva >= region->host_mem)
1043 && (hva <= (region->host_mem
1044 + region->region.memory_size - 1)))
1045 return (vm_paddr_t) ((uintptr_t)
1046 region->region.guest_phys_addr
1047 + (hva - (uintptr_t) region->host_mem));
1050 TEST_ASSERT(false, "No mapping to a guest physical address, "
1056 * VM Create IRQ Chip
1059 * vm - Virtual Machine
1065 * Creates an interrupt controller chip for the VM specified by vm.
1067 void vm_create_irqchip(struct kvm_vm *vm)
1071 ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
1072 TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1073 "rc: %i errno: %i", ret, errno);
1075 vm->has_irqchip = true;
1082 * vm - Virtual Machine
1088 * Pointer to structure that describes the state of the VCPU.
1090 * Locates and returns a pointer to a structure that describes the
1091 * state of the VCPU with the given vcpuid.
1093 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1095 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1096 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1105 * vm - Virtual Machine
1112 * Switch to executing the code for the VCPU given by vcpuid, within the VM
1115 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1117 int ret = _vcpu_run(vm, vcpuid);
1118 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1119 "rc: %i errno: %i", ret, errno);
1122 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1124 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1127 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1129 rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1130 } while (rc == -1 && errno == EINTR);
1134 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1136 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1139 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1141 vcpu->state->immediate_exit = 1;
1142 ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1143 vcpu->state->immediate_exit = 0;
1145 TEST_ASSERT(ret == -1 && errno == EINTR,
1146 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1151 * VM VCPU Set MP State
1154 * vm - Virtual Machine
1156 * mp_state - mp_state to be set
1162 * Sets the MP state of the VCPU given by vcpuid, to the state given
1165 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1166 struct kvm_mp_state *mp_state)
1168 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1171 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1173 ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
1174 TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
1175 "rc: %i errno: %i", ret, errno);
1182 * vm - Virtual Machine
1186 * regs - current state of VCPU regs
1190 * Obtains the current register state for the VCPU specified by vcpuid
1191 * and stores it at the location given by regs.
1193 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1195 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1198 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1200 ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1201 TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1209 * vm - Virtual Machine
1211 * regs - Values to set VCPU regs to
1217 * Sets the regs of the VCPU specified by vcpuid to the values
1220 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1222 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1225 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1227 ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1228 TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1232 #ifdef __KVM_HAVE_VCPU_EVENTS
1233 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1234 struct kvm_vcpu_events *events)
1236 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1239 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1241 ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1242 TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1246 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1247 struct kvm_vcpu_events *events)
1249 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1252 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1254 ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
1255 TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1261 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1262 struct kvm_nested_state *state)
1264 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1267 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1269 ret = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, state);
1270 TEST_ASSERT(ret == 0,
1271 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1275 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1276 struct kvm_nested_state *state, bool ignore_error)
1278 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1281 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1283 ret = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, state);
1284 if (!ignore_error) {
1285 TEST_ASSERT(ret == 0,
1286 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1295 * VM VCPU System Regs Get
1298 * vm - Virtual Machine
1302 * sregs - current state of VCPU system regs
1306 * Obtains the current system register state for the VCPU specified by
1307 * vcpuid and stores it at the location given by sregs.
1309 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1311 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1314 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1316 ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1317 TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1322 * VM VCPU System Regs Set
1325 * vm - Virtual Machine
1327 * sregs - Values to set VCPU system regs to
1333 * Sets the system regs of the VCPU specified by vcpuid to the values
1336 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1338 int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
1339 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1340 "rc: %i errno: %i", ret, errno);
1343 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1345 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1347 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1349 return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1356 * vm - Virtual Machine
1358 * cmd - Ioctl number
1359 * arg - Argument to pass to the ioctl
1363 * Issues an arbitrary ioctl on a VCPU fd.
1365 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1366 unsigned long cmd, void *arg)
1370 ret = _vcpu_ioctl(vm, vcpuid, cmd, arg);
1371 TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1372 cmd, ret, errno, strerror(errno));
1375 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1376 unsigned long cmd, void *arg)
1378 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1381 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1383 ret = ioctl(vcpu->fd, cmd, arg);
1392 * vm - Virtual Machine
1393 * cmd - Ioctl number
1394 * arg - Argument to pass to the ioctl
1398 * Issues an arbitrary ioctl on a VM fd.
1400 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1404 ret = ioctl(vm->fd, cmd, arg);
1405 TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1406 cmd, ret, errno, strerror(errno));
1413 * vm - Virtual Machine
1414 * indent - Left margin indent amount
1417 * stream - Output FILE stream
1421 * Dumps the current state of the VM given by vm, to the FILE stream
1424 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1426 struct userspace_mem_region *region;
1429 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1430 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1431 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1432 fprintf(stream, "%*sMem Regions:\n", indent, "");
1433 for (region = vm->userspace_mem_region_head; region;
1434 region = region->next) {
1435 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1436 "host_virt: %p\n", indent + 2, "",
1437 (uint64_t) region->region.guest_phys_addr,
1438 (uint64_t) region->region.memory_size,
1440 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1441 sparsebit_dump(stream, region->unused_phy_pages, 0);
1443 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1444 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1445 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1447 if (vm->pgd_created) {
1448 fprintf(stream, "%*sVirtual Translation Tables:\n",
1450 virt_dump(stream, vm, indent + 4);
1452 fprintf(stream, "%*sVCPUs:\n", indent, "");
1453 for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
1454 vcpu_dump(stream, vm, vcpu->id, indent + 2);
1457 /* Known KVM exit reasons */
1458 static struct exit_reason {
1459 unsigned int reason;
1461 } exit_reasons_known[] = {
1462 {KVM_EXIT_UNKNOWN, "UNKNOWN"},
1463 {KVM_EXIT_EXCEPTION, "EXCEPTION"},
1464 {KVM_EXIT_IO, "IO"},
1465 {KVM_EXIT_HYPERCALL, "HYPERCALL"},
1466 {KVM_EXIT_DEBUG, "DEBUG"},
1467 {KVM_EXIT_HLT, "HLT"},
1468 {KVM_EXIT_MMIO, "MMIO"},
1469 {KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
1470 {KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
1471 {KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
1472 {KVM_EXIT_INTR, "INTR"},
1473 {KVM_EXIT_SET_TPR, "SET_TPR"},
1474 {KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
1475 {KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
1476 {KVM_EXIT_S390_RESET, "S390_RESET"},
1477 {KVM_EXIT_DCR, "DCR"},
1478 {KVM_EXIT_NMI, "NMI"},
1479 {KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
1480 {KVM_EXIT_OSI, "OSI"},
1481 {KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
1482 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1483 {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
1488 * Exit Reason String
1491 * exit_reason - Exit reason
1496 * Constant string pointer describing the exit reason.
1498 * Locates and returns a constant string that describes the KVM exit
1499 * reason given by exit_reason. If no such string is found, a constant
1500 * string of "Unknown" is returned.
1502 const char *exit_reason_str(unsigned int exit_reason)
1506 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1507 if (exit_reason == exit_reasons_known[n1].reason)
1508 return exit_reasons_known[n1].name;
1515 * Physical Contiguous Page Allocator
1518 * vm - Virtual Machine
1519 * num - number of pages
1520 * paddr_min - Physical address minimum
1521 * memslot - Memory region to allocate page from
1526 * Starting physical address
1528 * Within the VM specified by vm, locates a range of available physical
1529 * pages at or above paddr_min. If found, the pages are marked as in use
1530 * and their base address is returned. A TEST_ASSERT failure occurs if
1531 * not enough pages are available at or above paddr_min.
1533 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1534 vm_paddr_t paddr_min, uint32_t memslot)
1536 struct userspace_mem_region *region;
1537 sparsebit_idx_t pg, base;
1539 TEST_ASSERT(num > 0, "Must allocate at least one page");
1541 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1542 "not divisible by page size.\n"
1543 " paddr_min: 0x%lx page_size: 0x%x",
1544 paddr_min, vm->page_size);
1546 region = memslot2region(vm, memslot);
1547 base = pg = paddr_min >> vm->page_shift;
1550 for (; pg < base + num; ++pg) {
1551 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
1552 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
1556 } while (pg && pg != base + num);
1559 fprintf(stderr, "No guest physical page available, "
1560 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1561 paddr_min, vm->page_size, memslot);
1562 fputs("---- vm dump ----\n", stderr);
1563 vm_dump(stderr, vm, 2);
1567 for (pg = base; pg < base + num; ++pg)
1568 sparsebit_clear(region->unused_phy_pages, pg);
1570 return base * vm->page_size;
1573 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1576 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1580 * Address Guest Virtual to Host Virtual
1583 * vm - Virtual Machine
1584 * gva - VM virtual address
1589 * Equivalent host virtual address
1591 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1593 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1597 * Is Unrestricted Guest
1600 * vm - Virtual Machine
1604 * Return: True if the unrestricted guest is set to 'Y', otherwise return false.
1606 * Check if the unrestricted guest flag is enabled.
1608 bool vm_is_unrestricted_guest(struct kvm_vm *vm)
1615 /* Ensure that the KVM vendor-specific module is loaded. */
1616 f = fopen(KVM_DEV_PATH, "r");
1617 TEST_ASSERT(f != NULL, "Error in opening KVM dev file: %d",
1622 f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1624 count = fread(&val, sizeof(char), 1, f);
1625 TEST_ASSERT(count == 1, "Unable to read from param file.");