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");
167 vm->pa_bits = vm_guest_mode_params[mode].pa_bits;
168 vm->va_bits = vm_guest_mode_params[mode].va_bits;
169 vm->page_size = vm_guest_mode_params[mode].page_size;
170 vm->page_shift = vm_guest_mode_params[mode].page_shift;
172 /* Setup mode specific traits. */
174 case VM_MODE_P52V48_4K:
175 vm->pgtable_levels = 4;
177 case VM_MODE_P52V48_64K:
178 vm->pgtable_levels = 3;
180 case VM_MODE_P48V48_4K:
181 vm->pgtable_levels = 4;
183 case VM_MODE_P48V48_64K:
184 vm->pgtable_levels = 3;
186 case VM_MODE_P40V48_4K:
187 vm->pgtable_levels = 4;
189 case VM_MODE_P40V48_64K:
190 vm->pgtable_levels = 3;
192 case VM_MODE_PXXV48_4K:
194 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
195 TEST_ASSERT(vm->va_bits == 48, "Linear address width "
196 "(%d bits) not supported", vm->va_bits);
197 pr_debug("Guest physical address width detected: %d\n",
199 vm->pgtable_levels = 4;
201 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
205 TEST_FAIL("Unknown guest mode, mode: 0x%x", mode);
209 if (vm->pa_bits != 40)
210 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
215 /* Limit to VA-bit canonical virtual addresses. */
216 vm->vpages_valid = sparsebit_alloc();
217 sparsebit_set_num(vm->vpages_valid,
218 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
219 sparsebit_set_num(vm->vpages_valid,
220 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
221 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
223 /* Limit physical addresses to PA-bits. */
224 vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
226 /* Allocate and setup memory for guest. */
227 vm->vpages_mapped = sparsebit_alloc();
229 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
235 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
237 return _vm_create(mode, phy_pages, perm);
244 * vm - VM that has been released before
249 * Reopens the file descriptors associated to the VM and reinstates the
250 * global state, such as the irqchip and the memory regions that are mapped
253 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
255 struct userspace_mem_region *region;
258 if (vmp->has_irqchip)
259 vm_create_irqchip(vmp);
261 for (region = vmp->userspace_mem_region_head; region;
262 region = region->next) {
263 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
264 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
265 " rc: %i errno: %i\n"
266 " slot: %u flags: 0x%x\n"
267 " guest_phys_addr: 0x%llx size: 0x%llx",
268 ret, errno, region->region.slot,
269 region->region.flags,
270 region->region.guest_phys_addr,
271 region->region.memory_size);
275 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
277 struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
280 ret = ioctl(vm->fd, KVM_GET_DIRTY_LOG, &args);
281 TEST_ASSERT(ret == 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
282 __func__, strerror(-ret));
285 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
286 uint64_t first_page, uint32_t num_pages)
288 struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
289 .first_page = first_page,
290 .num_pages = num_pages };
293 ret = ioctl(vm->fd, KVM_CLEAR_DIRTY_LOG, &args);
294 TEST_ASSERT(ret == 0, "%s: KVM_CLEAR_DIRTY_LOG failed: %s",
295 __func__, strerror(-ret));
299 * Userspace Memory Region Find
302 * vm - Virtual Machine
303 * start - Starting VM physical address
304 * end - Ending VM physical address, inclusive.
309 * Pointer to overlapping region, NULL if no such region.
311 * Searches for a region with any physical memory that overlaps with
312 * any portion of the guest physical addresses from start to end
313 * inclusive. If multiple overlapping regions exist, a pointer to any
314 * of the regions is returned. Null is returned only when no overlapping
317 static struct userspace_mem_region *
318 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
320 struct userspace_mem_region *region;
322 for (region = vm->userspace_mem_region_head; region;
323 region = region->next) {
324 uint64_t existing_start = region->region.guest_phys_addr;
325 uint64_t existing_end = region->region.guest_phys_addr
326 + region->region.memory_size - 1;
327 if (start <= existing_end && end >= existing_start)
335 * KVM Userspace Memory Region Find
338 * vm - Virtual Machine
339 * start - Starting VM physical address
340 * end - Ending VM physical address, inclusive.
345 * Pointer to overlapping region, NULL if no such region.
347 * Public interface to userspace_mem_region_find. Allows tests to look up
348 * the memslot datastructure for a given range of guest physical memory.
350 struct kvm_userspace_memory_region *
351 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
354 struct userspace_mem_region *region;
356 region = userspace_mem_region_find(vm, start, end);
360 return ®ion->region;
367 * vm - Virtual Machine
373 * Pointer to VCPU structure
375 * Locates a vcpu structure that describes the VCPU specified by vcpuid and
376 * returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
377 * for the specified vcpuid.
379 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
383 for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
384 if (vcpup->id == vcpuid)
395 * vm - Virtual Machine
400 * Return: None, TEST_ASSERT failures for all error conditions
402 * Within the VM specified by vm, removes the VCPU given by vcpuid.
404 static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
406 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
409 ret = munmap(vcpu->state, sizeof(*vcpu->state));
410 TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
411 "errno: %i", ret, errno);
413 TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
414 "errno: %i", ret, errno);
417 vcpu->next->prev = vcpu->prev;
419 vcpu->prev->next = vcpu->next;
421 vm->vcpu_head = vcpu->next;
425 void kvm_vm_release(struct kvm_vm *vmp)
429 while (vmp->vcpu_head)
430 vm_vcpu_rm(vmp, vmp->vcpu_head->id);
432 ret = close(vmp->fd);
433 TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
434 " vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
437 TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
438 " vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
442 * Destroys and frees the VM pointed to by vmp.
444 void kvm_vm_free(struct kvm_vm *vmp)
451 /* Free userspace_mem_regions. */
452 while (vmp->userspace_mem_region_head) {
453 struct userspace_mem_region *region
454 = vmp->userspace_mem_region_head;
456 region->region.memory_size = 0;
457 ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
459 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
460 "rc: %i errno: %i", ret, errno);
462 vmp->userspace_mem_region_head = region->next;
463 sparsebit_free(®ion->unused_phy_pages);
464 ret = munmap(region->mmap_start, region->mmap_size);
465 TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
471 /* Free sparsebit arrays. */
472 sparsebit_free(&vmp->vpages_valid);
473 sparsebit_free(&vmp->vpages_mapped);
477 /* Free the structure describing the VM. */
482 * Memory Compare, host virtual to guest virtual
485 * hva - Starting host virtual address
486 * vm - Virtual Machine
487 * gva - Starting guest virtual address
488 * len - number of bytes to compare
492 * Input/Output Args: None
495 * Returns 0 if the bytes starting at hva for a length of len
496 * are equal the guest virtual bytes starting at gva. Returns
497 * a value < 0, if bytes at hva are less than those at gva.
498 * Otherwise a value > 0 is returned.
500 * Compares the bytes starting at the host virtual address hva, for
501 * a length of len, to the guest bytes starting at the guest virtual
502 * address given by gva.
504 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
509 * Compare a batch of bytes until either a match is found
510 * or all the bytes have been compared.
512 for (uintptr_t offset = 0; offset < len; offset += amt) {
513 uintptr_t ptr1 = (uintptr_t)hva + offset;
516 * Determine host address for guest virtual address
519 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
522 * Determine amount to compare on this pass.
523 * Don't allow the comparsion to cross a page boundary.
526 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
527 amt = vm->page_size - (ptr1 % vm->page_size);
528 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
529 amt = vm->page_size - (ptr2 % vm->page_size);
531 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
532 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
535 * Perform the comparison. If there is a difference
536 * return that result to the caller, otherwise need
537 * to continue on looking for a mismatch.
539 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
545 * No mismatch found. Let the caller know the two memory
552 * VM Userspace Memory Region Add
555 * vm - Virtual Machine
556 * backing_src - Storage source for this region.
557 * NULL to use anonymous memory.
558 * guest_paddr - Starting guest physical address
559 * slot - KVM region slot
560 * npages - Number of physical pages
561 * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
567 * Allocates a memory area of the number of pages specified by npages
568 * and maps it to the VM specified by vm, at a starting physical address
569 * given by guest_paddr. The region is created with a KVM region slot
570 * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The
571 * region is created with the flags given by flags.
573 void vm_userspace_mem_region_add(struct kvm_vm *vm,
574 enum vm_mem_backing_src_type src_type,
575 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
579 struct userspace_mem_region *region;
580 size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
583 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
584 "Number of guest pages is not compatible with the host. "
585 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
587 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
588 "address not on a page boundary.\n"
589 " guest_paddr: 0x%lx vm->page_size: 0x%x",
590 guest_paddr, vm->page_size);
591 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
592 <= vm->max_gfn, "Physical range beyond maximum "
593 "supported physical address,\n"
594 " guest_paddr: 0x%lx npages: 0x%lx\n"
595 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
596 guest_paddr, npages, vm->max_gfn, vm->page_size);
599 * Confirm a mem region with an overlapping address doesn't
602 region = (struct userspace_mem_region *) userspace_mem_region_find(
603 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
605 TEST_FAIL("overlapping userspace_mem_region already "
607 " requested guest_paddr: 0x%lx npages: 0x%lx "
609 " existing guest_paddr: 0x%lx size: 0x%lx",
610 guest_paddr, npages, vm->page_size,
611 (uint64_t) region->region.guest_phys_addr,
612 (uint64_t) region->region.memory_size);
614 /* Confirm no region with the requested slot already exists. */
615 for (region = vm->userspace_mem_region_head; region;
616 region = region->next) {
617 if (region->region.slot == slot)
621 TEST_FAIL("A mem region with the requested slot "
623 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
624 " existing slot: %u paddr: 0x%lx size: 0x%lx",
625 slot, guest_paddr, npages,
627 (uint64_t) region->region.guest_phys_addr,
628 (uint64_t) region->region.memory_size);
630 /* Allocate and initialize new mem region structure. */
631 region = calloc(1, sizeof(*region));
632 TEST_ASSERT(region != NULL, "Insufficient Memory");
633 region->mmap_size = npages * vm->page_size;
636 /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
637 alignment = 0x100000;
642 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
643 alignment = max(huge_page_size, alignment);
645 /* Add enough memory to align up if necessary */
647 region->mmap_size += alignment;
649 region->mmap_start = mmap(NULL, region->mmap_size,
650 PROT_READ | PROT_WRITE,
651 MAP_PRIVATE | MAP_ANONYMOUS
652 | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
654 TEST_ASSERT(region->mmap_start != MAP_FAILED,
655 "test_malloc failed, mmap_start: %p errno: %i",
656 region->mmap_start, errno);
658 /* Align host address */
659 region->host_mem = align(region->mmap_start, alignment);
661 /* As needed perform madvise */
662 if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
663 ret = madvise(region->host_mem, npages * vm->page_size,
664 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
665 TEST_ASSERT(ret == 0, "madvise failed,\n"
669 region->host_mem, npages * vm->page_size, src_type);
672 region->unused_phy_pages = sparsebit_alloc();
673 sparsebit_set_num(region->unused_phy_pages,
674 guest_paddr >> vm->page_shift, npages);
675 region->region.slot = slot;
676 region->region.flags = flags;
677 region->region.guest_phys_addr = guest_paddr;
678 region->region.memory_size = npages * vm->page_size;
679 region->region.userspace_addr = (uintptr_t) region->host_mem;
680 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
681 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
682 " rc: %i errno: %i\n"
683 " slot: %u flags: 0x%x\n"
684 " guest_phys_addr: 0x%lx size: 0x%lx",
685 ret, errno, slot, flags,
686 guest_paddr, (uint64_t) region->region.memory_size);
688 /* Add to linked-list of memory regions. */
689 if (vm->userspace_mem_region_head)
690 vm->userspace_mem_region_head->prev = region;
691 region->next = vm->userspace_mem_region_head;
692 vm->userspace_mem_region_head = region;
699 * vm - Virtual Machine
700 * memslot - KVM memory slot ID
705 * Pointer to memory region structure that describe memory region
706 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
707 * on error (e.g. currently no memory region using memslot as a KVM
710 struct userspace_mem_region *
711 memslot2region(struct kvm_vm *vm, uint32_t memslot)
713 struct userspace_mem_region *region;
715 for (region = vm->userspace_mem_region_head; region;
716 region = region->next) {
717 if (region->region.slot == memslot)
720 if (region == NULL) {
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");
732 * VM Memory Region Flags Set
735 * vm - Virtual Machine
736 * flags - Starting guest physical address
742 * Sets the flags of the memory region specified by the value of slot,
743 * to the values given by flags.
745 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
748 struct userspace_mem_region *region;
750 region = memslot2region(vm, slot);
752 region->region.flags = flags;
754 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
756 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
757 " rc: %i errno: %i slot: %u flags: 0x%x",
758 ret, errno, slot, flags);
762 * VM Memory Region Move
765 * vm - Virtual Machine
766 * slot - Slot of the memory region to move
767 * new_gpa - Starting guest physical address
773 * Change the gpa of a memory region.
775 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
777 struct userspace_mem_region *region;
780 region = memslot2region(vm, slot);
782 region->region.guest_phys_addr = new_gpa;
784 ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion->region);
786 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION failed\n"
787 "ret: %i errno: %i slot: %u new_gpa: 0x%lx",
788 ret, errno, slot, new_gpa);
801 * Returns the size of the structure pointed to by the return value
804 static int vcpu_mmap_sz(void)
808 dev_fd = open(KVM_DEV_PATH, O_RDONLY);
812 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
813 TEST_ASSERT(ret >= sizeof(struct kvm_run),
814 "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
815 __func__, ret, errno);
826 * vm - Virtual Machine
833 * Adds a virtual CPU to the VM specified by vm with the ID given by vcpuid.
834 * No additional VCPU setup is done.
836 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
840 /* Confirm a vcpu with the specified id doesn't already exist. */
841 vcpu = vcpu_find(vm, vcpuid);
843 TEST_FAIL("vcpu with the specified id "
845 " requested vcpuid: %u\n"
846 " existing vcpuid: %u state: %p",
847 vcpuid, vcpu->id, vcpu->state);
849 /* Allocate and initialize new vcpu structure. */
850 vcpu = calloc(1, sizeof(*vcpu));
851 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
853 vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
854 TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
857 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
858 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
859 vcpu_mmap_sz(), sizeof(*vcpu->state));
860 vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
861 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
862 TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
863 "vcpu id: %u errno: %i", vcpuid, errno);
865 /* Add to linked-list of VCPUs. */
867 vm->vcpu_head->prev = vcpu;
868 vcpu->next = vm->vcpu_head;
869 vm->vcpu_head = vcpu;
873 * VM Virtual Address Unused Gap
876 * vm - Virtual Machine
878 * vaddr_min - Minimum Virtual Address
883 * Lowest virtual address at or below vaddr_min, with at least
884 * sz unused bytes. TEST_ASSERT failure if no area of at least
885 * size sz is available.
887 * Within the VM specified by vm, locates the lowest starting virtual
888 * address >= vaddr_min, that has at least sz unallocated bytes. A
889 * TEST_ASSERT failure occurs for invalid input or no area of at least
890 * sz unallocated bytes >= vaddr_min is available.
892 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
893 vm_vaddr_t vaddr_min)
895 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
897 /* Determine lowest permitted virtual page index. */
898 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
899 if ((pgidx_start * vm->page_size) < vaddr_min)
902 /* Loop over section with enough valid virtual page indexes. */
903 if (!sparsebit_is_set_num(vm->vpages_valid,
905 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
909 * Are there enough unused virtual pages available at
910 * the currently proposed starting virtual page index.
911 * If not, adjust proposed starting index to next
914 if (sparsebit_is_clear_num(vm->vpages_mapped,
917 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
919 if (pgidx_start == 0)
923 * If needed, adjust proposed starting virtual address,
924 * to next range of valid virtual addresses.
926 if (!sparsebit_is_set_num(vm->vpages_valid,
927 pgidx_start, pages)) {
928 pgidx_start = sparsebit_next_set_num(
929 vm->vpages_valid, pgidx_start, pages);
930 if (pgidx_start == 0)
933 } while (pgidx_start != 0);
936 TEST_FAIL("No vaddr of specified pages available, pages: 0x%lx", pages);
942 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
944 "Unexpected, invalid virtual page index range,\n"
945 " pgidx_start: 0x%lx\n"
948 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
950 "Unexpected, pages already mapped,\n"
951 " pgidx_start: 0x%lx\n"
955 return pgidx_start * vm->page_size;
959 * VM Virtual Address Allocate
962 * vm - Virtual Machine
964 * vaddr_min - Minimum starting virtual address
965 * data_memslot - Memory region slot for data pages
966 * pgd_memslot - Memory region slot for new virtual translation tables
971 * Starting guest virtual address
973 * Allocates at least sz bytes within the virtual address space of the vm
974 * given by vm. The allocated bytes are mapped to a virtual address >=
975 * the address given by vaddr_min. Note that each allocation uses a
976 * a unique set of pages, with the minimum real allocation being at least
979 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
980 uint32_t data_memslot, uint32_t pgd_memslot)
982 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
984 virt_pgd_alloc(vm, pgd_memslot);
987 * Find an unused range of virtual page addresses of at least
990 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
992 /* Map the virtual pages. */
993 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
994 pages--, vaddr += vm->page_size) {
997 paddr = vm_phy_page_alloc(vm,
998 KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
1000 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1002 sparsebit_set(vm->vpages_mapped,
1003 vaddr >> vm->page_shift);
1010 * Map a range of VM virtual address to the VM's physical address
1013 * vm - Virtual Machine
1014 * vaddr - Virtuall address to map
1015 * paddr - VM Physical Address
1016 * npages - The number of pages to map
1017 * pgd_memslot - Memory region slot for new virtual translation tables
1023 * Within the VM given by @vm, creates a virtual translation for
1024 * @npages starting at @vaddr to the page range starting at @paddr.
1026 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1027 unsigned int npages, uint32_t pgd_memslot)
1029 size_t page_size = vm->page_size;
1030 size_t size = npages * page_size;
1032 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1033 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1036 virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1043 * Address VM Physical to Host Virtual
1046 * vm - Virtual Machine
1047 * gpa - VM physical address
1052 * Equivalent host virtual address
1054 * Locates the memory region containing the VM physical address given
1055 * by gpa, within the VM given by vm. When found, the host virtual
1056 * address providing the memory to the vm physical address is returned.
1057 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1059 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1061 struct userspace_mem_region *region;
1062 for (region = vm->userspace_mem_region_head; region;
1063 region = region->next) {
1064 if ((gpa >= region->region.guest_phys_addr)
1065 && (gpa <= (region->region.guest_phys_addr
1066 + region->region.memory_size - 1)))
1067 return (void *) ((uintptr_t) region->host_mem
1068 + (gpa - region->region.guest_phys_addr));
1071 TEST_FAIL("No vm physical memory at 0x%lx", gpa);
1076 * Address Host Virtual to VM Physical
1079 * vm - Virtual Machine
1080 * hva - Host virtual address
1085 * Equivalent VM physical address
1087 * Locates the memory region containing the host virtual address given
1088 * by hva, within the VM given by vm. When found, the equivalent
1089 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1090 * region containing hva exists.
1092 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1094 struct userspace_mem_region *region;
1095 for (region = vm->userspace_mem_region_head; region;
1096 region = region->next) {
1097 if ((hva >= region->host_mem)
1098 && (hva <= (region->host_mem
1099 + region->region.memory_size - 1)))
1100 return (vm_paddr_t) ((uintptr_t)
1101 region->region.guest_phys_addr
1102 + (hva - (uintptr_t) region->host_mem));
1105 TEST_FAIL("No mapping to a guest physical address, hva: %p", hva);
1110 * VM Create IRQ Chip
1113 * vm - Virtual Machine
1119 * Creates an interrupt controller chip for the VM specified by vm.
1121 void vm_create_irqchip(struct kvm_vm *vm)
1125 ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
1126 TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1127 "rc: %i errno: %i", ret, errno);
1129 vm->has_irqchip = true;
1136 * vm - Virtual Machine
1142 * Pointer to structure that describes the state of the VCPU.
1144 * Locates and returns a pointer to a structure that describes the
1145 * state of the VCPU with the given vcpuid.
1147 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1149 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1150 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1159 * vm - Virtual Machine
1166 * Switch to executing the code for the VCPU given by vcpuid, within the VM
1169 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1171 int ret = _vcpu_run(vm, vcpuid);
1172 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1173 "rc: %i errno: %i", ret, errno);
1176 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1178 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1181 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1183 rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1184 } while (rc == -1 && errno == EINTR);
1188 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1190 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1193 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1195 vcpu->state->immediate_exit = 1;
1196 ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1197 vcpu->state->immediate_exit = 0;
1199 TEST_ASSERT(ret == -1 && errno == EINTR,
1200 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1204 void vcpu_set_guest_debug(struct kvm_vm *vm, uint32_t vcpuid,
1205 struct kvm_guest_debug *debug)
1207 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1208 int ret = ioctl(vcpu->fd, KVM_SET_GUEST_DEBUG, debug);
1210 TEST_ASSERT(ret == 0, "KVM_SET_GUEST_DEBUG failed: %d", ret);
1214 * VM VCPU Set MP State
1217 * vm - Virtual Machine
1219 * mp_state - mp_state to be set
1225 * Sets the MP state of the VCPU given by vcpuid, to the state given
1228 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1229 struct kvm_mp_state *mp_state)
1231 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1234 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1236 ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
1237 TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
1238 "rc: %i errno: %i", ret, errno);
1245 * vm - Virtual Machine
1249 * regs - current state of VCPU regs
1253 * Obtains the current register state for the VCPU specified by vcpuid
1254 * and stores it at the location given by regs.
1256 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1258 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1261 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1263 ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1264 TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1272 * vm - Virtual Machine
1274 * regs - Values to set VCPU regs to
1280 * Sets the regs of the VCPU specified by vcpuid to the values
1283 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1285 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1288 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1290 ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1291 TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1295 #ifdef __KVM_HAVE_VCPU_EVENTS
1296 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1297 struct kvm_vcpu_events *events)
1299 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1302 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1304 ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1305 TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1309 void vcpu_events_set(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_SET_VCPU_EVENTS, events);
1318 TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1324 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1325 struct kvm_nested_state *state)
1327 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1330 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1332 ret = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, state);
1333 TEST_ASSERT(ret == 0,
1334 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1338 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1339 struct kvm_nested_state *state, bool ignore_error)
1341 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1344 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1346 ret = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, state);
1347 if (!ignore_error) {
1348 TEST_ASSERT(ret == 0,
1349 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1358 * VM VCPU System Regs Get
1361 * vm - Virtual Machine
1365 * sregs - current state of VCPU system regs
1369 * Obtains the current system register state for the VCPU specified by
1370 * vcpuid and stores it at the location given by sregs.
1372 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1374 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1377 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1379 ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1380 TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1385 * VM VCPU System Regs Set
1388 * vm - Virtual Machine
1390 * sregs - Values to set VCPU system regs to
1396 * Sets the system regs of the VCPU specified by vcpuid to the values
1399 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1401 int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
1402 TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1403 "rc: %i errno: %i", ret, errno);
1406 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1408 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1410 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1412 return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1415 void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1419 ret = _vcpu_ioctl(vm, vcpuid, KVM_GET_FPU, fpu);
1420 TEST_ASSERT(ret == 0, "KVM_GET_FPU failed, rc: %i errno: %i (%s)",
1421 ret, errno, strerror(errno));
1424 void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1428 ret = _vcpu_ioctl(vm, vcpuid, KVM_SET_FPU, fpu);
1429 TEST_ASSERT(ret == 0, "KVM_SET_FPU failed, rc: %i errno: %i (%s)",
1430 ret, errno, strerror(errno));
1433 void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1437 ret = _vcpu_ioctl(vm, vcpuid, KVM_GET_ONE_REG, reg);
1438 TEST_ASSERT(ret == 0, "KVM_GET_ONE_REG failed, rc: %i errno: %i (%s)",
1439 ret, errno, strerror(errno));
1442 void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1446 ret = _vcpu_ioctl(vm, vcpuid, KVM_SET_ONE_REG, reg);
1447 TEST_ASSERT(ret == 0, "KVM_SET_ONE_REG failed, rc: %i errno: %i (%s)",
1448 ret, errno, strerror(errno));
1455 * vm - Virtual Machine
1457 * cmd - Ioctl number
1458 * arg - Argument to pass to the ioctl
1462 * Issues an arbitrary ioctl on a VCPU fd.
1464 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1465 unsigned long cmd, void *arg)
1469 ret = _vcpu_ioctl(vm, vcpuid, cmd, arg);
1470 TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1471 cmd, ret, errno, strerror(errno));
1474 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1475 unsigned long cmd, void *arg)
1477 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1480 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1482 ret = ioctl(vcpu->fd, cmd, arg);
1491 * vm - Virtual Machine
1492 * cmd - Ioctl number
1493 * arg - Argument to pass to the ioctl
1497 * Issues an arbitrary ioctl on a VM fd.
1499 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1503 ret = ioctl(vm->fd, cmd, arg);
1504 TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1505 cmd, ret, errno, strerror(errno));
1512 * vm - Virtual Machine
1513 * indent - Left margin indent amount
1516 * stream - Output FILE stream
1520 * Dumps the current state of the VM given by vm, to the FILE stream
1523 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1525 struct userspace_mem_region *region;
1528 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1529 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1530 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1531 fprintf(stream, "%*sMem Regions:\n", indent, "");
1532 for (region = vm->userspace_mem_region_head; region;
1533 region = region->next) {
1534 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1535 "host_virt: %p\n", indent + 2, "",
1536 (uint64_t) region->region.guest_phys_addr,
1537 (uint64_t) region->region.memory_size,
1539 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1540 sparsebit_dump(stream, region->unused_phy_pages, 0);
1542 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1543 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1544 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1546 if (vm->pgd_created) {
1547 fprintf(stream, "%*sVirtual Translation Tables:\n",
1549 virt_dump(stream, vm, indent + 4);
1551 fprintf(stream, "%*sVCPUs:\n", indent, "");
1552 for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
1553 vcpu_dump(stream, vm, vcpu->id, indent + 2);
1556 /* Known KVM exit reasons */
1557 static struct exit_reason {
1558 unsigned int reason;
1560 } exit_reasons_known[] = {
1561 {KVM_EXIT_UNKNOWN, "UNKNOWN"},
1562 {KVM_EXIT_EXCEPTION, "EXCEPTION"},
1563 {KVM_EXIT_IO, "IO"},
1564 {KVM_EXIT_HYPERCALL, "HYPERCALL"},
1565 {KVM_EXIT_DEBUG, "DEBUG"},
1566 {KVM_EXIT_HLT, "HLT"},
1567 {KVM_EXIT_MMIO, "MMIO"},
1568 {KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
1569 {KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
1570 {KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
1571 {KVM_EXIT_INTR, "INTR"},
1572 {KVM_EXIT_SET_TPR, "SET_TPR"},
1573 {KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
1574 {KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
1575 {KVM_EXIT_S390_RESET, "S390_RESET"},
1576 {KVM_EXIT_DCR, "DCR"},
1577 {KVM_EXIT_NMI, "NMI"},
1578 {KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
1579 {KVM_EXIT_OSI, "OSI"},
1580 {KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
1581 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1582 {KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
1587 * Exit Reason String
1590 * exit_reason - Exit reason
1595 * Constant string pointer describing the exit reason.
1597 * Locates and returns a constant string that describes the KVM exit
1598 * reason given by exit_reason. If no such string is found, a constant
1599 * string of "Unknown" is returned.
1601 const char *exit_reason_str(unsigned int exit_reason)
1605 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1606 if (exit_reason == exit_reasons_known[n1].reason)
1607 return exit_reasons_known[n1].name;
1614 * Physical Contiguous Page Allocator
1617 * vm - Virtual Machine
1618 * num - number of pages
1619 * paddr_min - Physical address minimum
1620 * memslot - Memory region to allocate page from
1625 * Starting physical address
1627 * Within the VM specified by vm, locates a range of available physical
1628 * pages at or above paddr_min. If found, the pages are marked as in use
1629 * and their base address is returned. A TEST_ASSERT failure occurs if
1630 * not enough pages are available at or above paddr_min.
1632 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1633 vm_paddr_t paddr_min, uint32_t memslot)
1635 struct userspace_mem_region *region;
1636 sparsebit_idx_t pg, base;
1638 TEST_ASSERT(num > 0, "Must allocate at least one page");
1640 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1641 "not divisible by page size.\n"
1642 " paddr_min: 0x%lx page_size: 0x%x",
1643 paddr_min, vm->page_size);
1645 region = memslot2region(vm, memslot);
1646 base = pg = paddr_min >> vm->page_shift;
1649 for (; pg < base + num; ++pg) {
1650 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
1651 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
1655 } while (pg && pg != base + num);
1658 fprintf(stderr, "No guest physical page available, "
1659 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1660 paddr_min, vm->page_size, memslot);
1661 fputs("---- vm dump ----\n", stderr);
1662 vm_dump(stderr, vm, 2);
1666 for (pg = base; pg < base + num; ++pg)
1667 sparsebit_clear(region->unused_phy_pages, pg);
1669 return base * vm->page_size;
1672 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1675 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1679 * Address Guest Virtual to Host Virtual
1682 * vm - Virtual Machine
1683 * gva - VM virtual address
1688 * Equivalent host virtual address
1690 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1692 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1696 * Is Unrestricted Guest
1699 * vm - Virtual Machine
1703 * Return: True if the unrestricted guest is set to 'Y', otherwise return false.
1705 * Check if the unrestricted guest flag is enabled.
1707 bool vm_is_unrestricted_guest(struct kvm_vm *vm)
1714 /* Ensure that the KVM vendor-specific module is loaded. */
1715 f = fopen(KVM_DEV_PATH, "r");
1716 TEST_ASSERT(f != NULL, "Error in opening KVM dev file: %d",
1721 f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1723 count = fread(&val, sizeof(char), 1, f);
1724 TEST_ASSERT(count == 1, "Unable to read from param file.");
1731 unsigned int vm_get_page_size(struct kvm_vm *vm)
1733 return vm->page_size;
1736 unsigned int vm_get_page_shift(struct kvm_vm *vm)
1738 return vm->page_shift;
1741 unsigned int vm_get_max_gfn(struct kvm_vm *vm)
1746 static unsigned int vm_calc_num_pages(unsigned int num_pages,
1747 unsigned int page_shift,
1748 unsigned int new_page_shift,
1751 unsigned int n = 1 << (new_page_shift - page_shift);
1753 if (page_shift >= new_page_shift)
1754 return num_pages * (1 << (page_shift - new_page_shift));
1756 return num_pages / n + !!(ceil && num_pages % n);
1759 static inline int getpageshift(void)
1761 return __builtin_ffs(getpagesize()) - 1;
1765 vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
1767 return vm_calc_num_pages(num_guest_pages,
1768 vm_guest_mode_params[mode].page_shift,
1769 getpageshift(), true);
1773 vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
1775 return vm_calc_num_pages(num_host_pages, getpageshift(),
1776 vm_guest_mode_params[mode].page_shift, false);
1779 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
1782 n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
1783 return vm_adjust_num_guest_pages(mode, n);