2 * tools/testing/selftests/kvm/lib/kvm_util.c
4 * Copyright (C) 2018, Google LLC.
6 * This work is licensed under the terms of the GNU GPL, version 2.
11 #include "kvm_util_internal.h"
15 #include <sys/types.h>
17 #include <linux/kernel.h>
19 #define KVM_DEV_PATH "/dev/kvm"
21 #define KVM_UTIL_PGS_PER_HUGEPG 512
22 #define KVM_UTIL_MIN_PADDR 0x2000
24 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
25 static void *align(void *x
, size_t size
)
27 size_t mask
= size
- 1;
28 TEST_ASSERT(size
!= 0 && !(size
& (size
- 1)),
29 "size not a power of 2: %lu", size
);
30 return (void *) (((size_t) x
+ mask
) & ~mask
);
41 * On success, the Value corresponding to the capability (KVM_CAP_*)
42 * specified by the value of cap. On failure a TEST_ASSERT failure
45 * Looks up and returns the value corresponding to the capability
46 * (KVM_CAP_*) given by cap.
48 int kvm_check_cap(long cap
)
53 kvm_fd
= open(KVM_DEV_PATH
, O_RDONLY
);
57 ret
= ioctl(kvm_fd
, KVM_CHECK_EXTENSION
, cap
);
58 TEST_ASSERT(ret
!= -1, "KVM_CHECK_EXTENSION IOCTL failed,\n"
59 " rc: %i errno: %i", ret
, errno
);
66 /* VM Enable Capability
69 * vm - Virtual Machine
74 * Return: On success, 0. On failure a TEST_ASSERT failure is produced.
76 * Enables a capability (KVM_CAP_*) on the VM.
78 int vm_enable_cap(struct kvm_vm
*vm
, struct kvm_enable_cap
*cap
)
82 ret
= ioctl(vm
->fd
, KVM_ENABLE_CAP
, cap
);
83 TEST_ASSERT(ret
== 0, "KVM_ENABLE_CAP IOCTL failed,\n"
84 " rc: %i errno: %i", ret
, errno
);
89 static void vm_open(struct kvm_vm
*vm
, int perm
)
91 vm
->kvm_fd
= open(KVM_DEV_PATH
, perm
);
96 vm
->fd
= ioctl(vm
->kvm_fd
, KVM_CREATE_VM
, NULL
);
97 TEST_ASSERT(vm
->fd
>= 0, "KVM_CREATE_VM ioctl failed, "
98 "rc: %i errno: %i", vm
->fd
, errno
);
104 * mode - VM Mode (e.g. VM_MODE_FLAT48PG)
105 * phy_pages - Physical memory pages
111 * Pointer to opaque structure that describes the created VM.
113 * Creates a VM with the mode specified by mode (e.g. VM_MODE_FLAT48PG).
114 * When phy_pages is non-zero, a memory region of phy_pages physical pages
115 * is created and mapped starting at guest physical address 0. The file
116 * descriptor to control the created VM is created with the permissions
117 * given by perm (e.g. O_RDWR).
119 struct kvm_vm
*vm_create(enum vm_guest_mode mode
, uint64_t phy_pages
, int perm
)
124 /* Allocate memory. */
125 vm
= calloc(1, sizeof(*vm
));
126 TEST_ASSERT(vm
!= NULL
, "Insufficient Memory");
131 /* Setup mode specific traits. */
133 case VM_MODE_FLAT48PG
:
134 vm
->page_size
= 0x1000;
137 /* Limit to 48-bit canonical virtual addresses. */
138 vm
->vpages_valid
= sparsebit_alloc();
139 sparsebit_set_num(vm
->vpages_valid
,
140 0, (1ULL << (48 - 1)) >> vm
->page_shift
);
141 sparsebit_set_num(vm
->vpages_valid
,
142 (~((1ULL << (48 - 1)) - 1)) >> vm
->page_shift
,
143 (1ULL << (48 - 1)) >> vm
->page_shift
);
145 /* Limit physical addresses to 52-bits. */
146 vm
->max_gfn
= ((1ULL << 52) >> vm
->page_shift
) - 1;
150 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode
);
153 /* Allocate and setup memory for guest. */
154 vm
->vpages_mapped
= sparsebit_alloc();
156 vm_userspace_mem_region_add(vm
, VM_MEM_SRC_ANONYMOUS
,
165 * vm - VM that has been released before
170 * Reopens the file descriptors associated to the VM and reinstates the
171 * global state, such as the irqchip and the memory regions that are mapped
174 void kvm_vm_restart(struct kvm_vm
*vmp
, int perm
)
176 struct userspace_mem_region
*region
;
179 if (vmp
->has_irqchip
)
180 vm_create_irqchip(vmp
);
182 for (region
= vmp
->userspace_mem_region_head
; region
;
183 region
= region
->next
) {
184 int ret
= ioctl(vmp
->fd
, KVM_SET_USER_MEMORY_REGION
, ®ion
->region
);
185 TEST_ASSERT(ret
== 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
186 " rc: %i errno: %i\n"
187 " slot: %u flags: 0x%x\n"
188 " guest_phys_addr: 0x%lx size: 0x%lx",
189 ret
, errno
, region
->region
.slot
, region
->region
.flags
,
190 region
->region
.guest_phys_addr
,
191 region
->region
.memory_size
);
195 void kvm_vm_get_dirty_log(struct kvm_vm
*vm
, int slot
, void *log
)
197 struct kvm_dirty_log args
= { .dirty_bitmap
= log
, .slot
= slot
};
200 ret
= ioctl(vm
->fd
, KVM_GET_DIRTY_LOG
, &args
);
201 TEST_ASSERT(ret
== 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
205 /* Userspace Memory Region Find
208 * vm - Virtual Machine
209 * start - Starting VM physical address
210 * end - Ending VM physical address, inclusive.
215 * Pointer to overlapping region, NULL if no such region.
217 * Searches for a region with any physical memory that overlaps with
218 * any portion of the guest physical addresses from start to end
219 * inclusive. If multiple overlapping regions exist, a pointer to any
220 * of the regions is returned. Null is returned only when no overlapping
223 static struct userspace_mem_region
*userspace_mem_region_find(
224 struct kvm_vm
*vm
, uint64_t start
, uint64_t end
)
226 struct userspace_mem_region
*region
;
228 for (region
= vm
->userspace_mem_region_head
; region
;
229 region
= region
->next
) {
230 uint64_t existing_start
= region
->region
.guest_phys_addr
;
231 uint64_t existing_end
= region
->region
.guest_phys_addr
232 + region
->region
.memory_size
- 1;
233 if (start
<= existing_end
&& end
>= existing_start
)
240 /* KVM Userspace Memory Region Find
243 * vm - Virtual Machine
244 * start - Starting VM physical address
245 * end - Ending VM physical address, inclusive.
250 * Pointer to overlapping region, NULL if no such region.
252 * Public interface to userspace_mem_region_find. Allows tests to look up
253 * the memslot datastructure for a given range of guest physical memory.
255 struct kvm_userspace_memory_region
*
256 kvm_userspace_memory_region_find(struct kvm_vm
*vm
, uint64_t start
,
259 struct userspace_mem_region
*region
;
261 region
= userspace_mem_region_find(vm
, start
, end
);
265 return ®ion
->region
;
271 * vm - Virtual Machine
277 * Pointer to VCPU structure
279 * Locates a vcpu structure that describes the VCPU specified by vcpuid and
280 * returns a pointer to it. Returns NULL if the VM doesn't contain a VCPU
281 * for the specified vcpuid.
283 struct vcpu
*vcpu_find(struct kvm_vm
*vm
,
288 for (vcpup
= vm
->vcpu_head
; vcpup
; vcpup
= vcpup
->next
) {
289 if (vcpup
->id
== vcpuid
)
299 * vm - Virtual Machine
304 * Return: None, TEST_ASSERT failures for all error conditions
306 * Within the VM specified by vm, removes the VCPU given by vcpuid.
308 static void vm_vcpu_rm(struct kvm_vm
*vm
, uint32_t vcpuid
)
310 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
313 ret
= munmap(vcpu
->state
, sizeof(*vcpu
->state
));
314 TEST_ASSERT(ret
== 0, "munmap of VCPU fd failed, rc: %i "
315 "errno: %i", ret
, errno
);
317 TEST_ASSERT(ret
== 0, "Close of VCPU fd failed, rc: %i "
318 "errno: %i", ret
, errno
);
321 vcpu
->next
->prev
= vcpu
->prev
;
323 vcpu
->prev
->next
= vcpu
->next
;
325 vm
->vcpu_head
= vcpu
->next
;
329 void kvm_vm_release(struct kvm_vm
*vmp
)
334 while (vmp
->vcpu_head
)
335 vm_vcpu_rm(vmp
, vmp
->vcpu_head
->id
);
337 /* Close file descriptor for the VM. */
338 ret
= close(vmp
->fd
);
339 TEST_ASSERT(ret
== 0, "Close of vm fd failed,\n"
340 " vmp->fd: %i rc: %i errno: %i", vmp
->fd
, ret
, errno
);
343 TEST_ASSERT(ret
== 0, "Close of /dev/kvm fd failed,\n"
344 " vmp->kvm_fd: %i rc: %i errno: %i", vmp
->kvm_fd
, ret
, errno
);
347 /* Destroys and frees the VM pointed to by vmp.
349 void kvm_vm_free(struct kvm_vm
*vmp
)
356 /* Free userspace_mem_regions. */
357 while (vmp
->userspace_mem_region_head
) {
358 struct userspace_mem_region
*region
359 = vmp
->userspace_mem_region_head
;
361 region
->region
.memory_size
= 0;
362 ret
= ioctl(vmp
->fd
, KVM_SET_USER_MEMORY_REGION
,
364 TEST_ASSERT(ret
== 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
365 "rc: %i errno: %i", ret
, errno
);
367 vmp
->userspace_mem_region_head
= region
->next
;
368 sparsebit_free(®ion
->unused_phy_pages
);
369 ret
= munmap(region
->mmap_start
, region
->mmap_size
);
370 TEST_ASSERT(ret
== 0, "munmap failed, rc: %i errno: %i",
376 /* Free sparsebit arrays. */
377 sparsebit_free(&vmp
->vpages_valid
);
378 sparsebit_free(&vmp
->vpages_mapped
);
382 /* Free the structure describing the VM. */
386 /* Memory Compare, host virtual to guest virtual
389 * hva - Starting host virtual address
390 * vm - Virtual Machine
391 * gva - Starting guest virtual address
392 * len - number of bytes to compare
396 * Input/Output Args: None
399 * Returns 0 if the bytes starting at hva for a length of len
400 * are equal the guest virtual bytes starting at gva. Returns
401 * a value < 0, if bytes at hva are less than those at gva.
402 * Otherwise a value > 0 is returned.
404 * Compares the bytes starting at the host virtual address hva, for
405 * a length of len, to the guest bytes starting at the guest virtual
406 * address given by gva.
408 int kvm_memcmp_hva_gva(void *hva
,
409 struct kvm_vm
*vm
, vm_vaddr_t gva
, size_t len
)
413 /* Compare a batch of bytes until either a match is found
414 * or all the bytes have been compared.
416 for (uintptr_t offset
= 0; offset
< len
; offset
+= amt
) {
417 uintptr_t ptr1
= (uintptr_t)hva
+ offset
;
419 /* Determine host address for guest virtual address
422 uintptr_t ptr2
= (uintptr_t)addr_gva2hva(vm
, gva
+ offset
);
424 /* Determine amount to compare on this pass.
425 * Don't allow the comparsion to cross a page boundary.
428 if ((ptr1
>> vm
->page_shift
) != ((ptr1
+ amt
) >> vm
->page_shift
))
429 amt
= vm
->page_size
- (ptr1
% vm
->page_size
);
430 if ((ptr2
>> vm
->page_shift
) != ((ptr2
+ amt
) >> vm
->page_shift
))
431 amt
= vm
->page_size
- (ptr2
% vm
->page_size
);
433 assert((ptr1
>> vm
->page_shift
) == ((ptr1
+ amt
- 1) >> vm
->page_shift
));
434 assert((ptr2
>> vm
->page_shift
) == ((ptr2
+ amt
- 1) >> vm
->page_shift
));
436 /* Perform the comparison. If there is a difference
437 * return that result to the caller, otherwise need
438 * to continue on looking for a mismatch.
440 int ret
= memcmp((void *)ptr1
, (void *)ptr2
, amt
);
445 /* No mismatch found. Let the caller know the two memory
451 /* Allocate an instance of struct kvm_cpuid2
457 * Return: A pointer to the allocated struct. The caller is responsible
458 * for freeing this struct.
460 * Since kvm_cpuid2 uses a 0-length array to allow a the size of the
461 * array to be decided at allocation time, allocation is slightly
462 * complicated. This function uses a reasonable default length for
463 * the array and performs the appropriate allocation.
465 static struct kvm_cpuid2
*allocate_kvm_cpuid2(void)
467 struct kvm_cpuid2
*cpuid
;
471 size
= sizeof(*cpuid
);
472 size
+= nent
* sizeof(struct kvm_cpuid_entry2
);
473 cpuid
= malloc(size
);
484 /* KVM Supported CPUID Get
490 * Return: The supported KVM CPUID
492 * Get the guest CPUID supported by KVM.
494 struct kvm_cpuid2
*kvm_get_supported_cpuid(void)
496 static struct kvm_cpuid2
*cpuid
;
503 cpuid
= allocate_kvm_cpuid2();
504 kvm_fd
= open(KVM_DEV_PATH
, O_RDONLY
);
508 ret
= ioctl(kvm_fd
, KVM_GET_SUPPORTED_CPUID
, cpuid
);
509 TEST_ASSERT(ret
== 0, "KVM_GET_SUPPORTED_CPUID failed %d %d\n",
516 /* Locate a cpuid entry.
520 * function: The function of the cpuid entry to find.
524 * Return: A pointer to the cpuid entry. Never returns NULL.
526 struct kvm_cpuid_entry2
*
527 kvm_get_supported_cpuid_index(uint32_t function
, uint32_t index
)
529 struct kvm_cpuid2
*cpuid
;
530 struct kvm_cpuid_entry2
*entry
= NULL
;
533 cpuid
= kvm_get_supported_cpuid();
534 for (i
= 0; i
< cpuid
->nent
; i
++) {
535 if (cpuid
->entries
[i
].function
== function
&&
536 cpuid
->entries
[i
].index
== index
) {
537 entry
= &cpuid
->entries
[i
];
542 TEST_ASSERT(entry
, "Guest CPUID entry not found: (EAX=%x, ECX=%x).",
547 /* VM Userspace Memory Region Add
550 * vm - Virtual Machine
551 * backing_src - Storage source for this region.
552 * NULL to use anonymous memory.
553 * guest_paddr - Starting guest physical address
554 * slot - KVM region slot
555 * npages - Number of physical pages
556 * flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
562 * Allocates a memory area of the number of pages specified by npages
563 * and maps it to the VM specified by vm, at a starting physical address
564 * given by guest_paddr. The region is created with a KVM region slot
565 * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM. The
566 * region is created with the flags given by flags.
568 void vm_userspace_mem_region_add(struct kvm_vm
*vm
,
569 enum vm_mem_backing_src_type src_type
,
570 uint64_t guest_paddr
, uint32_t slot
, uint64_t npages
,
574 unsigned long pmem_size
= 0;
575 struct userspace_mem_region
*region
;
576 size_t huge_page_size
= KVM_UTIL_PGS_PER_HUGEPG
* vm
->page_size
;
578 TEST_ASSERT((guest_paddr
% vm
->page_size
) == 0, "Guest physical "
579 "address not on a page boundary.\n"
580 " guest_paddr: 0x%lx vm->page_size: 0x%x",
581 guest_paddr
, vm
->page_size
);
582 TEST_ASSERT((((guest_paddr
>> vm
->page_shift
) + npages
) - 1)
583 <= vm
->max_gfn
, "Physical range beyond maximum "
584 "supported physical address,\n"
585 " guest_paddr: 0x%lx npages: 0x%lx\n"
586 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
587 guest_paddr
, npages
, vm
->max_gfn
, vm
->page_size
);
589 /* Confirm a mem region with an overlapping address doesn't
592 region
= (struct userspace_mem_region
*) userspace_mem_region_find(
593 vm
, guest_paddr
, guest_paddr
+ npages
* vm
->page_size
);
595 TEST_ASSERT(false, "overlapping userspace_mem_region already "
597 " requested guest_paddr: 0x%lx npages: 0x%lx "
599 " existing guest_paddr: 0x%lx size: 0x%lx",
600 guest_paddr
, npages
, vm
->page_size
,
601 (uint64_t) region
->region
.guest_phys_addr
,
602 (uint64_t) region
->region
.memory_size
);
604 /* Confirm no region with the requested slot already exists. */
605 for (region
= vm
->userspace_mem_region_head
; region
;
606 region
= region
->next
) {
607 if (region
->region
.slot
== slot
)
609 if ((guest_paddr
<= (region
->region
.guest_phys_addr
610 + region
->region
.memory_size
))
611 && ((guest_paddr
+ npages
* vm
->page_size
)
612 >= region
->region
.guest_phys_addr
))
616 TEST_ASSERT(false, "A mem region with the requested slot "
617 "or overlapping physical memory range already exists.\n"
618 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
619 " existing slot: %u paddr: 0x%lx size: 0x%lx",
620 slot
, guest_paddr
, npages
,
622 (uint64_t) region
->region
.guest_phys_addr
,
623 (uint64_t) region
->region
.memory_size
);
625 /* Allocate and initialize new mem region structure. */
626 region
= calloc(1, sizeof(*region
));
627 TEST_ASSERT(region
!= NULL
, "Insufficient Memory");
628 region
->mmap_size
= npages
* vm
->page_size
;
630 /* Enough memory to align up to a huge page. */
631 if (src_type
== VM_MEM_SRC_ANONYMOUS_THP
)
632 region
->mmap_size
+= huge_page_size
;
633 region
->mmap_start
= mmap(NULL
, region
->mmap_size
,
634 PROT_READ
| PROT_WRITE
,
635 MAP_PRIVATE
| MAP_ANONYMOUS
636 | (src_type
== VM_MEM_SRC_ANONYMOUS_HUGETLB
? MAP_HUGETLB
: 0),
638 TEST_ASSERT(region
->mmap_start
!= MAP_FAILED
,
639 "test_malloc failed, mmap_start: %p errno: %i",
640 region
->mmap_start
, errno
);
642 /* Align THP allocation up to start of a huge page. */
643 region
->host_mem
= align(region
->mmap_start
,
644 src_type
== VM_MEM_SRC_ANONYMOUS_THP
? huge_page_size
: 1);
646 /* As needed perform madvise */
647 if (src_type
== VM_MEM_SRC_ANONYMOUS
|| src_type
== VM_MEM_SRC_ANONYMOUS_THP
) {
648 ret
= madvise(region
->host_mem
, npages
* vm
->page_size
,
649 src_type
== VM_MEM_SRC_ANONYMOUS
? MADV_NOHUGEPAGE
: MADV_HUGEPAGE
);
650 TEST_ASSERT(ret
== 0, "madvise failed,\n"
654 region
->host_mem
, npages
* vm
->page_size
, src_type
);
657 region
->unused_phy_pages
= sparsebit_alloc();
658 sparsebit_set_num(region
->unused_phy_pages
,
659 guest_paddr
>> vm
->page_shift
, npages
);
660 region
->region
.slot
= slot
;
661 region
->region
.flags
= flags
;
662 region
->region
.guest_phys_addr
= guest_paddr
;
663 region
->region
.memory_size
= npages
* vm
->page_size
;
664 region
->region
.userspace_addr
= (uintptr_t) region
->host_mem
;
665 ret
= ioctl(vm
->fd
, KVM_SET_USER_MEMORY_REGION
, ®ion
->region
);
666 TEST_ASSERT(ret
== 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
667 " rc: %i errno: %i\n"
668 " slot: %u flags: 0x%x\n"
669 " guest_phys_addr: 0x%lx size: 0x%lx",
670 ret
, errno
, slot
, flags
,
671 guest_paddr
, (uint64_t) region
->region
.memory_size
);
673 /* Add to linked-list of memory regions. */
674 if (vm
->userspace_mem_region_head
)
675 vm
->userspace_mem_region_head
->prev
= region
;
676 region
->next
= vm
->userspace_mem_region_head
;
677 vm
->userspace_mem_region_head
= region
;
683 * vm - Virtual Machine
684 * memslot - KVM memory slot ID
689 * Pointer to memory region structure that describe memory region
690 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
691 * on error (e.g. currently no memory region using memslot as a KVM
694 static struct userspace_mem_region
*memslot2region(struct kvm_vm
*vm
,
697 struct userspace_mem_region
*region
;
699 for (region
= vm
->userspace_mem_region_head
; region
;
700 region
= region
->next
) {
701 if (region
->region
.slot
== memslot
)
704 if (region
== NULL
) {
705 fprintf(stderr
, "No mem region with the requested slot found,\n"
706 " requested slot: %u\n", memslot
);
707 fputs("---- vm dump ----\n", stderr
);
708 vm_dump(stderr
, vm
, 2);
709 TEST_ASSERT(false, "Mem region not found");
715 /* VM Memory Region Flags Set
718 * vm - Virtual Machine
719 * flags - Starting guest physical address
725 * Sets the flags of the memory region specified by the value of slot,
726 * to the values given by flags.
728 void vm_mem_region_set_flags(struct kvm_vm
*vm
, uint32_t slot
, uint32_t flags
)
731 struct userspace_mem_region
*region
;
733 /* Locate memory region. */
734 region
= memslot2region(vm
, slot
);
736 region
->region
.flags
= flags
;
738 ret
= ioctl(vm
->fd
, KVM_SET_USER_MEMORY_REGION
, ®ion
->region
);
740 TEST_ASSERT(ret
== 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
741 " rc: %i errno: %i slot: %u flags: 0x%x",
742 ret
, errno
, slot
, flags
);
754 * Returns the size of the structure pointed to by the return value
757 static int vcpu_mmap_sz(void)
761 dev_fd
= open(KVM_DEV_PATH
, O_RDONLY
);
765 ret
= ioctl(dev_fd
, KVM_GET_VCPU_MMAP_SIZE
, NULL
);
766 TEST_ASSERT(ret
>= sizeof(struct kvm_run
),
767 "%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
768 __func__
, ret
, errno
);
778 * vm - Virtual Machine
785 * Creates and adds to the VM specified by vm and virtual CPU with
786 * the ID given by vcpuid.
788 void vm_vcpu_add(struct kvm_vm
*vm
, uint32_t vcpuid
, int pgd_memslot
, int gdt_memslot
)
792 /* Confirm a vcpu with the specified id doesn't already exist. */
793 vcpu
= vcpu_find(vm
, vcpuid
);
795 TEST_ASSERT(false, "vcpu with the specified id "
797 " requested vcpuid: %u\n"
798 " existing vcpuid: %u state: %p",
799 vcpuid
, vcpu
->id
, vcpu
->state
);
801 /* Allocate and initialize new vcpu structure. */
802 vcpu
= calloc(1, sizeof(*vcpu
));
803 TEST_ASSERT(vcpu
!= NULL
, "Insufficient Memory");
805 vcpu
->fd
= ioctl(vm
->fd
, KVM_CREATE_VCPU
, vcpuid
);
806 TEST_ASSERT(vcpu
->fd
>= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
809 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu
->state
), "vcpu mmap size "
810 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
811 vcpu_mmap_sz(), sizeof(*vcpu
->state
));
812 vcpu
->state
= (struct kvm_run
*) mmap(NULL
, sizeof(*vcpu
->state
),
813 PROT_READ
| PROT_WRITE
, MAP_SHARED
, vcpu
->fd
, 0);
814 TEST_ASSERT(vcpu
->state
!= MAP_FAILED
, "mmap vcpu_state failed, "
815 "vcpu id: %u errno: %i", vcpuid
, errno
);
817 /* Add to linked-list of VCPUs. */
819 vm
->vcpu_head
->prev
= vcpu
;
820 vcpu
->next
= vm
->vcpu_head
;
821 vm
->vcpu_head
= vcpu
;
823 vcpu_setup(vm
, vcpuid
, pgd_memslot
, gdt_memslot
);
826 /* VM Virtual Address Unused Gap
829 * vm - Virtual Machine
831 * vaddr_min - Minimum Virtual Address
836 * Lowest virtual address at or below vaddr_min, with at least
837 * sz unused bytes. TEST_ASSERT failure if no area of at least
838 * size sz is available.
840 * Within the VM specified by vm, locates the lowest starting virtual
841 * address >= vaddr_min, that has at least sz unallocated bytes. A
842 * TEST_ASSERT failure occurs for invalid input or no area of at least
843 * sz unallocated bytes >= vaddr_min is available.
845 static vm_vaddr_t
vm_vaddr_unused_gap(struct kvm_vm
*vm
, size_t sz
,
846 vm_vaddr_t vaddr_min
)
848 uint64_t pages
= (sz
+ vm
->page_size
- 1) >> vm
->page_shift
;
850 /* Determine lowest permitted virtual page index. */
851 uint64_t pgidx_start
= (vaddr_min
+ vm
->page_size
- 1) >> vm
->page_shift
;
852 if ((pgidx_start
* vm
->page_size
) < vaddr_min
)
855 /* Loop over section with enough valid virtual page indexes. */
856 if (!sparsebit_is_set_num(vm
->vpages_valid
,
858 pgidx_start
= sparsebit_next_set_num(vm
->vpages_valid
,
862 * Are there enough unused virtual pages available at
863 * the currently proposed starting virtual page index.
864 * If not, adjust proposed starting index to next
867 if (sparsebit_is_clear_num(vm
->vpages_mapped
,
870 pgidx_start
= sparsebit_next_clear_num(vm
->vpages_mapped
,
872 if (pgidx_start
== 0)
876 * If needed, adjust proposed starting virtual address,
877 * to next range of valid virtual addresses.
879 if (!sparsebit_is_set_num(vm
->vpages_valid
,
880 pgidx_start
, pages
)) {
881 pgidx_start
= sparsebit_next_set_num(
882 vm
->vpages_valid
, pgidx_start
, pages
);
883 if (pgidx_start
== 0)
886 } while (pgidx_start
!= 0);
889 TEST_ASSERT(false, "No vaddr of specified pages available, "
890 "pages: 0x%lx", pages
);
896 TEST_ASSERT(sparsebit_is_set_num(vm
->vpages_valid
,
898 "Unexpected, invalid virtual page index range,\n"
899 " pgidx_start: 0x%lx\n"
902 TEST_ASSERT(sparsebit_is_clear_num(vm
->vpages_mapped
,
904 "Unexpected, pages already mapped,\n"
905 " pgidx_start: 0x%lx\n"
909 return pgidx_start
* vm
->page_size
;
912 /* VM Virtual Address Allocate
915 * vm - Virtual Machine
917 * vaddr_min - Minimum starting virtual address
918 * data_memslot - Memory region slot for data pages
919 * pgd_memslot - Memory region slot for new virtual translation tables
924 * Starting guest virtual address
926 * Allocates at least sz bytes within the virtual address space of the vm
927 * given by vm. The allocated bytes are mapped to a virtual address >=
928 * the address given by vaddr_min. Note that each allocation uses a
929 * a unique set of pages, with the minimum real allocation being at least
932 vm_vaddr_t
vm_vaddr_alloc(struct kvm_vm
*vm
, size_t sz
, vm_vaddr_t vaddr_min
,
933 uint32_t data_memslot
, uint32_t pgd_memslot
)
935 uint64_t pages
= (sz
>> vm
->page_shift
) + ((sz
% vm
->page_size
) != 0);
937 virt_pgd_alloc(vm
, pgd_memslot
);
939 /* Find an unused range of virtual page addresses of at least
942 vm_vaddr_t vaddr_start
= vm_vaddr_unused_gap(vm
, sz
, vaddr_min
);
944 /* Map the virtual pages. */
945 for (vm_vaddr_t vaddr
= vaddr_start
; pages
> 0;
946 pages
--, vaddr
+= vm
->page_size
) {
949 paddr
= vm_phy_page_alloc(vm
, KVM_UTIL_MIN_PADDR
, data_memslot
);
951 virt_pg_map(vm
, vaddr
, paddr
, pgd_memslot
);
953 sparsebit_set(vm
->vpages_mapped
,
954 vaddr
>> vm
->page_shift
);
961 * Map a range of VM virtual address to the VM's physical address
964 * vm - Virtual Machine
965 * vaddr - Virtuall address to map
966 * paddr - VM Physical Address
967 * size - The size of the range to map
968 * pgd_memslot - Memory region slot for new virtual translation tables
974 * Within the VM given by vm, creates a virtual translation for the
975 * page range starting at vaddr to the page range starting at paddr.
977 void virt_map(struct kvm_vm
*vm
, uint64_t vaddr
, uint64_t paddr
,
978 size_t size
, uint32_t pgd_memslot
)
980 size_t page_size
= vm
->page_size
;
981 size_t npages
= size
/ page_size
;
983 TEST_ASSERT(vaddr
+ size
> vaddr
, "Vaddr overflow");
984 TEST_ASSERT(paddr
+ size
> paddr
, "Paddr overflow");
987 virt_pg_map(vm
, vaddr
, paddr
, pgd_memslot
);
993 /* Address VM Physical to Host Virtual
996 * vm - Virtual Machine
997 * gpa - VM physical address
1002 * Equivalent host virtual address
1004 * Locates the memory region containing the VM physical address given
1005 * by gpa, within the VM given by vm. When found, the host virtual
1006 * address providing the memory to the vm physical address is returned.
1007 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1009 void *addr_gpa2hva(struct kvm_vm
*vm
, vm_paddr_t gpa
)
1011 struct userspace_mem_region
*region
;
1012 for (region
= vm
->userspace_mem_region_head
; region
;
1013 region
= region
->next
) {
1014 if ((gpa
>= region
->region
.guest_phys_addr
)
1015 && (gpa
<= (region
->region
.guest_phys_addr
1016 + region
->region
.memory_size
- 1)))
1017 return (void *) ((uintptr_t) region
->host_mem
1018 + (gpa
- region
->region
.guest_phys_addr
));
1021 TEST_ASSERT(false, "No vm physical memory at 0x%lx", gpa
);
1025 /* Address Host Virtual to VM Physical
1028 * vm - Virtual Machine
1029 * hva - Host virtual address
1034 * Equivalent VM physical address
1036 * Locates the memory region containing the host virtual address given
1037 * by hva, within the VM given by vm. When found, the equivalent
1038 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1039 * region containing hva exists.
1041 vm_paddr_t
addr_hva2gpa(struct kvm_vm
*vm
, void *hva
)
1043 struct userspace_mem_region
*region
;
1044 for (region
= vm
->userspace_mem_region_head
; region
;
1045 region
= region
->next
) {
1046 if ((hva
>= region
->host_mem
)
1047 && (hva
<= (region
->host_mem
1048 + region
->region
.memory_size
- 1)))
1049 return (vm_paddr_t
) ((uintptr_t)
1050 region
->region
.guest_phys_addr
1051 + (hva
- (uintptr_t) region
->host_mem
));
1054 TEST_ASSERT(false, "No mapping to a guest physical address, "
1059 /* VM Create IRQ Chip
1062 * vm - Virtual Machine
1068 * Creates an interrupt controller chip for the VM specified by vm.
1070 void vm_create_irqchip(struct kvm_vm
*vm
)
1074 ret
= ioctl(vm
->fd
, KVM_CREATE_IRQCHIP
, 0);
1075 TEST_ASSERT(ret
== 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1076 "rc: %i errno: %i", ret
, errno
);
1078 vm
->has_irqchip
= true;
1084 * vm - Virtual Machine
1090 * Pointer to structure that describes the state of the VCPU.
1092 * Locates and returns a pointer to a structure that describes the
1093 * state of the VCPU with the given vcpuid.
1095 struct kvm_run
*vcpu_state(struct kvm_vm
*vm
, uint32_t vcpuid
)
1097 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1098 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1106 * vm - Virtual Machine
1113 * Switch to executing the code for the VCPU given by vcpuid, within the VM
1116 void vcpu_run(struct kvm_vm
*vm
, uint32_t vcpuid
)
1118 int ret
= _vcpu_run(vm
, vcpuid
);
1119 TEST_ASSERT(ret
== 0, "KVM_RUN IOCTL failed, "
1120 "rc: %i errno: %i", ret
, errno
);
1123 int _vcpu_run(struct kvm_vm
*vm
, uint32_t vcpuid
)
1125 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1128 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1130 rc
= ioctl(vcpu
->fd
, KVM_RUN
, NULL
);
1131 } while (rc
== -1 && errno
== EINTR
);
1135 /* VM VCPU Set MP State
1138 * vm - Virtual Machine
1140 * mp_state - mp_state to be set
1146 * Sets the MP state of the VCPU given by vcpuid, to the state given
1149 void vcpu_set_mp_state(struct kvm_vm
*vm
, uint32_t vcpuid
,
1150 struct kvm_mp_state
*mp_state
)
1152 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1155 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1157 ret
= ioctl(vcpu
->fd
, KVM_SET_MP_STATE
, mp_state
);
1158 TEST_ASSERT(ret
== 0, "KVM_SET_MP_STATE IOCTL failed, "
1159 "rc: %i errno: %i", ret
, errno
);
1165 * vm - Virtual Machine
1169 * regs - current state of VCPU regs
1173 * Obtains the current register state for the VCPU specified by vcpuid
1174 * and stores it at the location given by regs.
1176 void vcpu_regs_get(struct kvm_vm
*vm
,
1177 uint32_t vcpuid
, struct kvm_regs
*regs
)
1179 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1182 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1185 ret
= ioctl(vcpu
->fd
, KVM_GET_REGS
, regs
);
1186 TEST_ASSERT(ret
== 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1193 * vm - Virtual Machine
1195 * regs - Values to set VCPU regs to
1201 * Sets the regs of the VCPU specified by vcpuid to the values
1204 void vcpu_regs_set(struct kvm_vm
*vm
,
1205 uint32_t vcpuid
, struct kvm_regs
*regs
)
1207 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1210 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1213 ret
= ioctl(vcpu
->fd
, KVM_SET_REGS
, regs
);
1214 TEST_ASSERT(ret
== 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1218 void vcpu_events_get(struct kvm_vm
*vm
, uint32_t vcpuid
,
1219 struct kvm_vcpu_events
*events
)
1221 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1224 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1227 ret
= ioctl(vcpu
->fd
, KVM_GET_VCPU_EVENTS
, events
);
1228 TEST_ASSERT(ret
== 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1232 void vcpu_events_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
1233 struct kvm_vcpu_events
*events
)
1235 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1238 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1241 ret
= ioctl(vcpu
->fd
, KVM_SET_VCPU_EVENTS
, events
);
1242 TEST_ASSERT(ret
== 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1249 * vm - Virtual Machine
1251 * msr_index - Index of MSR
1255 * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
1257 * Get value of MSR for VCPU.
1259 uint64_t vcpu_get_msr(struct kvm_vm
*vm
, uint32_t vcpuid
, uint64_t msr_index
)
1261 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1263 struct kvm_msrs header
;
1264 struct kvm_msr_entry entry
;
1268 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1269 buffer
.header
.nmsrs
= 1;
1270 buffer
.entry
.index
= msr_index
;
1271 r
= ioctl(vcpu
->fd
, KVM_GET_MSRS
, &buffer
.header
);
1272 TEST_ASSERT(r
== 1, "KVM_GET_MSRS IOCTL failed,\n"
1273 " rc: %i errno: %i", r
, errno
);
1275 return buffer
.entry
.data
;
1281 * vm - Virtual Machine
1283 * msr_index - Index of MSR
1284 * msr_value - New value of MSR
1288 * Return: On success, nothing. On failure a TEST_ASSERT is produced.
1290 * Set value of MSR for VCPU.
1292 void vcpu_set_msr(struct kvm_vm
*vm
, uint32_t vcpuid
, uint64_t msr_index
,
1295 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1297 struct kvm_msrs header
;
1298 struct kvm_msr_entry entry
;
1302 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1303 memset(&buffer
, 0, sizeof(buffer
));
1304 buffer
.header
.nmsrs
= 1;
1305 buffer
.entry
.index
= msr_index
;
1306 buffer
.entry
.data
= msr_value
;
1307 r
= ioctl(vcpu
->fd
, KVM_SET_MSRS
, &buffer
.header
);
1308 TEST_ASSERT(r
== 1, "KVM_SET_MSRS IOCTL failed,\n"
1309 " rc: %i errno: %i", r
, errno
);
1315 * vm - Virtual Machine
1317 * num - number of arguments
1318 * ... - arguments, each of type uint64_t
1324 * Sets the first num function input arguments to the values
1325 * given as variable args. Each of the variable args is expected to
1326 * be of type uint64_t.
1328 void vcpu_args_set(struct kvm_vm
*vm
, uint32_t vcpuid
, unsigned int num
, ...)
1331 struct kvm_regs regs
;
1333 TEST_ASSERT(num
>= 1 && num
<= 6, "Unsupported number of args,\n"
1338 vcpu_regs_get(vm
, vcpuid
, ®s
);
1341 regs
.rdi
= va_arg(ap
, uint64_t);
1344 regs
.rsi
= va_arg(ap
, uint64_t);
1347 regs
.rdx
= va_arg(ap
, uint64_t);
1350 regs
.rcx
= va_arg(ap
, uint64_t);
1353 regs
.r8
= va_arg(ap
, uint64_t);
1356 regs
.r9
= va_arg(ap
, uint64_t);
1358 vcpu_regs_set(vm
, vcpuid
, ®s
);
1362 /* VM VCPU System Regs Get
1365 * vm - Virtual Machine
1369 * sregs - current state of VCPU system regs
1373 * Obtains the current system register state for the VCPU specified by
1374 * vcpuid and stores it at the location given by sregs.
1376 void vcpu_sregs_get(struct kvm_vm
*vm
,
1377 uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1379 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1382 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1386 ret
= ioctl(vcpu
->fd
, KVM_GET_SREGS
, sregs
);
1387 TEST_ASSERT(ret
== 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1391 /* VM VCPU System Regs Set
1394 * vm - Virtual Machine
1396 * sregs - Values to set VCPU system regs to
1402 * Sets the system regs of the VCPU specified by vcpuid to the values
1405 void vcpu_sregs_set(struct kvm_vm
*vm
,
1406 uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1408 int ret
= _vcpu_sregs_set(vm
, vcpuid
, sregs
);
1409 TEST_ASSERT(ret
== 0, "KVM_RUN IOCTL failed, "
1410 "rc: %i errno: %i", ret
, errno
);
1413 int _vcpu_sregs_set(struct kvm_vm
*vm
,
1414 uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1416 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1419 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1422 return ioctl(vcpu
->fd
, KVM_SET_SREGS
, sregs
);
1428 * vm - Virtual Machine
1430 * cmd - Ioctl number
1431 * arg - Argument to pass to the ioctl
1435 * Issues an arbitrary ioctl on a VCPU fd.
1437 void vcpu_ioctl(struct kvm_vm
*vm
,
1438 uint32_t vcpuid
, unsigned long cmd
, void *arg
)
1440 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1443 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1445 ret
= ioctl(vcpu
->fd
, cmd
, arg
);
1446 TEST_ASSERT(ret
== 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1447 cmd
, ret
, errno
, strerror(errno
));
1453 * vm - Virtual Machine
1454 * cmd - Ioctl number
1455 * arg - Argument to pass to the ioctl
1459 * Issues an arbitrary ioctl on a VM fd.
1461 void vm_ioctl(struct kvm_vm
*vm
, unsigned long cmd
, void *arg
)
1465 ret
= ioctl(vm
->fd
, cmd
, arg
);
1466 TEST_ASSERT(ret
== 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1467 cmd
, ret
, errno
, strerror(errno
));
1473 * vm - Virtual Machine
1474 * indent - Left margin indent amount
1477 * stream - Output FILE stream
1481 * Dumps the current state of the VM given by vm, to the FILE stream
1484 void vm_dump(FILE *stream
, struct kvm_vm
*vm
, uint8_t indent
)
1486 struct userspace_mem_region
*region
;
1489 fprintf(stream
, "%*smode: 0x%x\n", indent
, "", vm
->mode
);
1490 fprintf(stream
, "%*sfd: %i\n", indent
, "", vm
->fd
);
1491 fprintf(stream
, "%*spage_size: 0x%x\n", indent
, "", vm
->page_size
);
1492 fprintf(stream
, "%*sMem Regions:\n", indent
, "");
1493 for (region
= vm
->userspace_mem_region_head
; region
;
1494 region
= region
->next
) {
1495 fprintf(stream
, "%*sguest_phys: 0x%lx size: 0x%lx "
1496 "host_virt: %p\n", indent
+ 2, "",
1497 (uint64_t) region
->region
.guest_phys_addr
,
1498 (uint64_t) region
->region
.memory_size
,
1500 fprintf(stream
, "%*sunused_phy_pages: ", indent
+ 2, "");
1501 sparsebit_dump(stream
, region
->unused_phy_pages
, 0);
1503 fprintf(stream
, "%*sMapped Virtual Pages:\n", indent
, "");
1504 sparsebit_dump(stream
, vm
->vpages_mapped
, indent
+ 2);
1505 fprintf(stream
, "%*spgd_created: %u\n", indent
, "",
1507 if (vm
->pgd_created
) {
1508 fprintf(stream
, "%*sVirtual Translation Tables:\n",
1510 virt_dump(stream
, vm
, indent
+ 4);
1512 fprintf(stream
, "%*sVCPUs:\n", indent
, "");
1513 for (vcpu
= vm
->vcpu_head
; vcpu
; vcpu
= vcpu
->next
)
1514 vcpu_dump(stream
, vm
, vcpu
->id
, indent
+ 2);
1520 * vm - Virtual Machine
1522 * indent - Left margin indent amount
1525 * stream - Output FILE stream
1529 * Dumps the current state of the VCPU specified by vcpuid, within the VM
1530 * given by vm, to the FILE stream given by stream.
1532 void vcpu_dump(FILE *stream
, struct kvm_vm
*vm
,
1533 uint32_t vcpuid
, uint8_t indent
)
1535 struct kvm_regs regs
;
1536 struct kvm_sregs sregs
;
1538 fprintf(stream
, "%*scpuid: %u\n", indent
, "", vcpuid
);
1540 fprintf(stream
, "%*sregs:\n", indent
+ 2, "");
1541 vcpu_regs_get(vm
, vcpuid
, ®s
);
1542 regs_dump(stream
, ®s
, indent
+ 4);
1544 fprintf(stream
, "%*ssregs:\n", indent
+ 2, "");
1545 vcpu_sregs_get(vm
, vcpuid
, &sregs
);
1546 sregs_dump(stream
, &sregs
, indent
+ 4);
1549 /* Known KVM exit reasons */
1550 static struct exit_reason
{
1551 unsigned int reason
;
1553 } exit_reasons_known
[] = {
1554 {KVM_EXIT_UNKNOWN
, "UNKNOWN"},
1555 {KVM_EXIT_EXCEPTION
, "EXCEPTION"},
1556 {KVM_EXIT_IO
, "IO"},
1557 {KVM_EXIT_HYPERCALL
, "HYPERCALL"},
1558 {KVM_EXIT_DEBUG
, "DEBUG"},
1559 {KVM_EXIT_HLT
, "HLT"},
1560 {KVM_EXIT_MMIO
, "MMIO"},
1561 {KVM_EXIT_IRQ_WINDOW_OPEN
, "IRQ_WINDOW_OPEN"},
1562 {KVM_EXIT_SHUTDOWN
, "SHUTDOWN"},
1563 {KVM_EXIT_FAIL_ENTRY
, "FAIL_ENTRY"},
1564 {KVM_EXIT_INTR
, "INTR"},
1565 {KVM_EXIT_SET_TPR
, "SET_TPR"},
1566 {KVM_EXIT_TPR_ACCESS
, "TPR_ACCESS"},
1567 {KVM_EXIT_S390_SIEIC
, "S390_SIEIC"},
1568 {KVM_EXIT_S390_RESET
, "S390_RESET"},
1569 {KVM_EXIT_DCR
, "DCR"},
1570 {KVM_EXIT_NMI
, "NMI"},
1571 {KVM_EXIT_INTERNAL_ERROR
, "INTERNAL_ERROR"},
1572 {KVM_EXIT_OSI
, "OSI"},
1573 {KVM_EXIT_PAPR_HCALL
, "PAPR_HCALL"},
1574 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1575 {KVM_EXIT_MEMORY_NOT_PRESENT
, "MEMORY_NOT_PRESENT"},
1579 /* Exit Reason String
1582 * exit_reason - Exit reason
1587 * Constant string pointer describing the exit reason.
1589 * Locates and returns a constant string that describes the KVM exit
1590 * reason given by exit_reason. If no such string is found, a constant
1591 * string of "Unknown" is returned.
1593 const char *exit_reason_str(unsigned int exit_reason
)
1597 for (n1
= 0; n1
< ARRAY_SIZE(exit_reasons_known
); n1
++) {
1598 if (exit_reason
== exit_reasons_known
[n1
].reason
)
1599 return exit_reasons_known
[n1
].name
;
1605 /* Physical Page Allocate
1608 * vm - Virtual Machine
1609 * paddr_min - Physical address minimum
1610 * memslot - Memory region to allocate page from
1615 * Starting physical address
1617 * Within the VM specified by vm, locates an available physical page
1618 * at or above paddr_min. If found, the page is marked as in use
1619 * and its address is returned. A TEST_ASSERT failure occurs if no
1620 * page is available at or above paddr_min.
1622 vm_paddr_t
vm_phy_page_alloc(struct kvm_vm
*vm
,
1623 vm_paddr_t paddr_min
, uint32_t memslot
)
1625 struct userspace_mem_region
*region
;
1628 TEST_ASSERT((paddr_min
% vm
->page_size
) == 0, "Min physical address "
1629 "not divisible by page size.\n"
1630 " paddr_min: 0x%lx page_size: 0x%x",
1631 paddr_min
, vm
->page_size
);
1633 /* Locate memory region. */
1634 region
= memslot2region(vm
, memslot
);
1636 /* Locate next available physical page at or above paddr_min. */
1637 pg
= paddr_min
>> vm
->page_shift
;
1639 if (!sparsebit_is_set(region
->unused_phy_pages
, pg
)) {
1640 pg
= sparsebit_next_set(region
->unused_phy_pages
, pg
);
1642 fprintf(stderr
, "No guest physical page available, "
1643 "paddr_min: 0x%lx page_size: 0x%x memslot: %u",
1644 paddr_min
, vm
->page_size
, memslot
);
1645 fputs("---- vm dump ----\n", stderr
);
1646 vm_dump(stderr
, vm
, 2);
1651 /* Specify page as in use and return its address. */
1652 sparsebit_clear(region
->unused_phy_pages
, pg
);
1654 return pg
* vm
->page_size
;
1657 /* Address Guest Virtual to Host Virtual
1660 * vm - Virtual Machine
1661 * gva - VM virtual address
1666 * Equivalent host virtual address
1668 void *addr_gva2hva(struct kvm_vm
*vm
, vm_vaddr_t gva
)
1670 return addr_gpa2hva(vm
, addr_gva2gpa(vm
, gva
));
1673 void guest_args_read(struct kvm_vm
*vm
, uint32_t vcpu_id
,
1674 struct guest_args
*args
)
1676 struct kvm_run
*run
= vcpu_state(vm
, vcpu_id
);
1677 struct kvm_regs regs
;
1679 memset(®s
, 0, sizeof(regs
));
1680 vcpu_regs_get(vm
, vcpu_id
, ®s
);
1682 args
->port
= run
->io
.port
;
1683 args
->arg0
= regs
.rdi
;
1684 args
->arg1
= regs
.rsi
;