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",
1205 * VM VCPU Set MP State
1208 * vm - Virtual Machine
1210 * mp_state - mp_state to be set
1216 * Sets the MP state of the VCPU given by vcpuid, to the state given
1219 void vcpu_set_mp_state(struct kvm_vm
*vm
, uint32_t vcpuid
,
1220 struct kvm_mp_state
*mp_state
)
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_MP_STATE
, mp_state
);
1228 TEST_ASSERT(ret
== 0, "KVM_SET_MP_STATE IOCTL failed, "
1229 "rc: %i errno: %i", ret
, errno
);
1236 * vm - Virtual Machine
1240 * regs - current state of VCPU regs
1244 * Obtains the current register state for the VCPU specified by vcpuid
1245 * and stores it at the location given by regs.
1247 void vcpu_regs_get(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_regs
*regs
)
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_GET_REGS
, regs
);
1255 TEST_ASSERT(ret
== 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1263 * vm - Virtual Machine
1265 * regs - Values to set VCPU regs to
1271 * Sets the regs of the VCPU specified by vcpuid to the values
1274 void vcpu_regs_set(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_regs
*regs
)
1276 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1279 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1281 ret
= ioctl(vcpu
->fd
, KVM_SET_REGS
, regs
);
1282 TEST_ASSERT(ret
== 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1286 #ifdef __KVM_HAVE_VCPU_EVENTS
1287 void vcpu_events_get(struct kvm_vm
*vm
, uint32_t vcpuid
,
1288 struct kvm_vcpu_events
*events
)
1290 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1293 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1295 ret
= ioctl(vcpu
->fd
, KVM_GET_VCPU_EVENTS
, events
);
1296 TEST_ASSERT(ret
== 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1300 void vcpu_events_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
1301 struct kvm_vcpu_events
*events
)
1303 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1306 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1308 ret
= ioctl(vcpu
->fd
, KVM_SET_VCPU_EVENTS
, events
);
1309 TEST_ASSERT(ret
== 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1315 void vcpu_nested_state_get(struct kvm_vm
*vm
, uint32_t vcpuid
,
1316 struct kvm_nested_state
*state
)
1318 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1321 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1323 ret
= ioctl(vcpu
->fd
, KVM_GET_NESTED_STATE
, state
);
1324 TEST_ASSERT(ret
== 0,
1325 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1329 int vcpu_nested_state_set(struct kvm_vm
*vm
, uint32_t vcpuid
,
1330 struct kvm_nested_state
*state
, bool ignore_error
)
1332 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1335 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1337 ret
= ioctl(vcpu
->fd
, KVM_SET_NESTED_STATE
, state
);
1338 if (!ignore_error
) {
1339 TEST_ASSERT(ret
== 0,
1340 "KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1349 * VM VCPU System Regs Get
1352 * vm - Virtual Machine
1356 * sregs - current state of VCPU system regs
1360 * Obtains the current system register state for the VCPU specified by
1361 * vcpuid and stores it at the location given by sregs.
1363 void vcpu_sregs_get(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1365 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1368 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1370 ret
= ioctl(vcpu
->fd
, KVM_GET_SREGS
, sregs
);
1371 TEST_ASSERT(ret
== 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1376 * VM VCPU System Regs Set
1379 * vm - Virtual Machine
1381 * sregs - Values to set VCPU system regs to
1387 * Sets the system regs of the VCPU specified by vcpuid to the values
1390 void vcpu_sregs_set(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1392 int ret
= _vcpu_sregs_set(vm
, vcpuid
, sregs
);
1393 TEST_ASSERT(ret
== 0, "KVM_RUN IOCTL failed, "
1394 "rc: %i errno: %i", ret
, errno
);
1397 int _vcpu_sregs_set(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_sregs
*sregs
)
1399 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1401 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1403 return ioctl(vcpu
->fd
, KVM_SET_SREGS
, sregs
);
1406 void vcpu_fpu_get(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_fpu
*fpu
)
1410 ret
= _vcpu_ioctl(vm
, vcpuid
, KVM_GET_FPU
, fpu
);
1411 TEST_ASSERT(ret
== 0, "KVM_GET_FPU failed, rc: %i errno: %i (%s)",
1412 ret
, errno
, strerror(errno
));
1415 void vcpu_fpu_set(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_fpu
*fpu
)
1419 ret
= _vcpu_ioctl(vm
, vcpuid
, KVM_SET_FPU
, fpu
);
1420 TEST_ASSERT(ret
== 0, "KVM_SET_FPU failed, rc: %i errno: %i (%s)",
1421 ret
, errno
, strerror(errno
));
1424 void vcpu_get_reg(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_one_reg
*reg
)
1428 ret
= _vcpu_ioctl(vm
, vcpuid
, KVM_GET_ONE_REG
, reg
);
1429 TEST_ASSERT(ret
== 0, "KVM_GET_ONE_REG failed, rc: %i errno: %i (%s)",
1430 ret
, errno
, strerror(errno
));
1433 void vcpu_set_reg(struct kvm_vm
*vm
, uint32_t vcpuid
, struct kvm_one_reg
*reg
)
1437 ret
= _vcpu_ioctl(vm
, vcpuid
, KVM_SET_ONE_REG
, reg
);
1438 TEST_ASSERT(ret
== 0, "KVM_SET_ONE_REG failed, rc: %i errno: %i (%s)",
1439 ret
, errno
, strerror(errno
));
1446 * vm - Virtual Machine
1448 * cmd - Ioctl number
1449 * arg - Argument to pass to the ioctl
1453 * Issues an arbitrary ioctl on a VCPU fd.
1455 void vcpu_ioctl(struct kvm_vm
*vm
, uint32_t vcpuid
,
1456 unsigned long cmd
, void *arg
)
1460 ret
= _vcpu_ioctl(vm
, vcpuid
, cmd
, arg
);
1461 TEST_ASSERT(ret
== 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1462 cmd
, ret
, errno
, strerror(errno
));
1465 int _vcpu_ioctl(struct kvm_vm
*vm
, uint32_t vcpuid
,
1466 unsigned long cmd
, void *arg
)
1468 struct vcpu
*vcpu
= vcpu_find(vm
, vcpuid
);
1471 TEST_ASSERT(vcpu
!= NULL
, "vcpu not found, vcpuid: %u", vcpuid
);
1473 ret
= ioctl(vcpu
->fd
, cmd
, arg
);
1482 * vm - Virtual Machine
1483 * cmd - Ioctl number
1484 * arg - Argument to pass to the ioctl
1488 * Issues an arbitrary ioctl on a VM fd.
1490 void vm_ioctl(struct kvm_vm
*vm
, unsigned long cmd
, void *arg
)
1494 ret
= ioctl(vm
->fd
, cmd
, arg
);
1495 TEST_ASSERT(ret
== 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1496 cmd
, ret
, errno
, strerror(errno
));
1503 * vm - Virtual Machine
1504 * indent - Left margin indent amount
1507 * stream - Output FILE stream
1511 * Dumps the current state of the VM given by vm, to the FILE stream
1514 void vm_dump(FILE *stream
, struct kvm_vm
*vm
, uint8_t indent
)
1516 struct userspace_mem_region
*region
;
1519 fprintf(stream
, "%*smode: 0x%x\n", indent
, "", vm
->mode
);
1520 fprintf(stream
, "%*sfd: %i\n", indent
, "", vm
->fd
);
1521 fprintf(stream
, "%*spage_size: 0x%x\n", indent
, "", vm
->page_size
);
1522 fprintf(stream
, "%*sMem Regions:\n", indent
, "");
1523 for (region
= vm
->userspace_mem_region_head
; region
;
1524 region
= region
->next
) {
1525 fprintf(stream
, "%*sguest_phys: 0x%lx size: 0x%lx "
1526 "host_virt: %p\n", indent
+ 2, "",
1527 (uint64_t) region
->region
.guest_phys_addr
,
1528 (uint64_t) region
->region
.memory_size
,
1530 fprintf(stream
, "%*sunused_phy_pages: ", indent
+ 2, "");
1531 sparsebit_dump(stream
, region
->unused_phy_pages
, 0);
1533 fprintf(stream
, "%*sMapped Virtual Pages:\n", indent
, "");
1534 sparsebit_dump(stream
, vm
->vpages_mapped
, indent
+ 2);
1535 fprintf(stream
, "%*spgd_created: %u\n", indent
, "",
1537 if (vm
->pgd_created
) {
1538 fprintf(stream
, "%*sVirtual Translation Tables:\n",
1540 virt_dump(stream
, vm
, indent
+ 4);
1542 fprintf(stream
, "%*sVCPUs:\n", indent
, "");
1543 for (vcpu
= vm
->vcpu_head
; vcpu
; vcpu
= vcpu
->next
)
1544 vcpu_dump(stream
, vm
, vcpu
->id
, indent
+ 2);
1547 /* Known KVM exit reasons */
1548 static struct exit_reason
{
1549 unsigned int reason
;
1551 } exit_reasons_known
[] = {
1552 {KVM_EXIT_UNKNOWN
, "UNKNOWN"},
1553 {KVM_EXIT_EXCEPTION
, "EXCEPTION"},
1554 {KVM_EXIT_IO
, "IO"},
1555 {KVM_EXIT_HYPERCALL
, "HYPERCALL"},
1556 {KVM_EXIT_DEBUG
, "DEBUG"},
1557 {KVM_EXIT_HLT
, "HLT"},
1558 {KVM_EXIT_MMIO
, "MMIO"},
1559 {KVM_EXIT_IRQ_WINDOW_OPEN
, "IRQ_WINDOW_OPEN"},
1560 {KVM_EXIT_SHUTDOWN
, "SHUTDOWN"},
1561 {KVM_EXIT_FAIL_ENTRY
, "FAIL_ENTRY"},
1562 {KVM_EXIT_INTR
, "INTR"},
1563 {KVM_EXIT_SET_TPR
, "SET_TPR"},
1564 {KVM_EXIT_TPR_ACCESS
, "TPR_ACCESS"},
1565 {KVM_EXIT_S390_SIEIC
, "S390_SIEIC"},
1566 {KVM_EXIT_S390_RESET
, "S390_RESET"},
1567 {KVM_EXIT_DCR
, "DCR"},
1568 {KVM_EXIT_NMI
, "NMI"},
1569 {KVM_EXIT_INTERNAL_ERROR
, "INTERNAL_ERROR"},
1570 {KVM_EXIT_OSI
, "OSI"},
1571 {KVM_EXIT_PAPR_HCALL
, "PAPR_HCALL"},
1572 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1573 {KVM_EXIT_MEMORY_NOT_PRESENT
, "MEMORY_NOT_PRESENT"},
1578 * Exit Reason String
1581 * exit_reason - Exit reason
1586 * Constant string pointer describing the exit reason.
1588 * Locates and returns a constant string that describes the KVM exit
1589 * reason given by exit_reason. If no such string is found, a constant
1590 * string of "Unknown" is returned.
1592 const char *exit_reason_str(unsigned int exit_reason
)
1596 for (n1
= 0; n1
< ARRAY_SIZE(exit_reasons_known
); n1
++) {
1597 if (exit_reason
== exit_reasons_known
[n1
].reason
)
1598 return exit_reasons_known
[n1
].name
;
1605 * Physical Contiguous Page Allocator
1608 * vm - Virtual Machine
1609 * num - number of pages
1610 * paddr_min - Physical address minimum
1611 * memslot - Memory region to allocate page from
1616 * Starting physical address
1618 * Within the VM specified by vm, locates a range of available physical
1619 * pages at or above paddr_min. If found, the pages are marked as in use
1620 * and their base address is returned. A TEST_ASSERT failure occurs if
1621 * not enough pages are available at or above paddr_min.
1623 vm_paddr_t
vm_phy_pages_alloc(struct kvm_vm
*vm
, size_t num
,
1624 vm_paddr_t paddr_min
, uint32_t memslot
)
1626 struct userspace_mem_region
*region
;
1627 sparsebit_idx_t pg
, base
;
1629 TEST_ASSERT(num
> 0, "Must allocate at least one page");
1631 TEST_ASSERT((paddr_min
% vm
->page_size
) == 0, "Min physical address "
1632 "not divisible by page size.\n"
1633 " paddr_min: 0x%lx page_size: 0x%x",
1634 paddr_min
, vm
->page_size
);
1636 region
= memslot2region(vm
, memslot
);
1637 base
= pg
= paddr_min
>> vm
->page_shift
;
1640 for (; pg
< base
+ num
; ++pg
) {
1641 if (!sparsebit_is_set(region
->unused_phy_pages
, pg
)) {
1642 base
= pg
= sparsebit_next_set(region
->unused_phy_pages
, pg
);
1646 } while (pg
&& pg
!= base
+ num
);
1649 fprintf(stderr
, "No guest physical page available, "
1650 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1651 paddr_min
, vm
->page_size
, memslot
);
1652 fputs("---- vm dump ----\n", stderr
);
1653 vm_dump(stderr
, vm
, 2);
1657 for (pg
= base
; pg
< base
+ num
; ++pg
)
1658 sparsebit_clear(region
->unused_phy_pages
, pg
);
1660 return base
* vm
->page_size
;
1663 vm_paddr_t
vm_phy_page_alloc(struct kvm_vm
*vm
, vm_paddr_t paddr_min
,
1666 return vm_phy_pages_alloc(vm
, 1, paddr_min
, memslot
);
1670 * Address Guest Virtual to Host Virtual
1673 * vm - Virtual Machine
1674 * gva - VM virtual address
1679 * Equivalent host virtual address
1681 void *addr_gva2hva(struct kvm_vm
*vm
, vm_vaddr_t gva
)
1683 return addr_gpa2hva(vm
, addr_gva2gpa(vm
, gva
));
1687 * Is Unrestricted Guest
1690 * vm - Virtual Machine
1694 * Return: True if the unrestricted guest is set to 'Y', otherwise return false.
1696 * Check if the unrestricted guest flag is enabled.
1698 bool vm_is_unrestricted_guest(struct kvm_vm
*vm
)
1705 /* Ensure that the KVM vendor-specific module is loaded. */
1706 f
= fopen(KVM_DEV_PATH
, "r");
1707 TEST_ASSERT(f
!= NULL
, "Error in opening KVM dev file: %d",
1712 f
= fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1714 count
= fread(&val
, sizeof(char), 1, f
);
1715 TEST_ASSERT(count
== 1, "Unable to read from param file.");
1722 unsigned int vm_get_page_size(struct kvm_vm
*vm
)
1724 return vm
->page_size
;
1727 unsigned int vm_get_page_shift(struct kvm_vm
*vm
)
1729 return vm
->page_shift
;
1732 unsigned int vm_get_max_gfn(struct kvm_vm
*vm
)
1737 static unsigned int vm_calc_num_pages(unsigned int num_pages
,
1738 unsigned int page_shift
,
1739 unsigned int new_page_shift
,
1742 unsigned int n
= 1 << (new_page_shift
- page_shift
);
1744 if (page_shift
>= new_page_shift
)
1745 return num_pages
* (1 << (page_shift
- new_page_shift
));
1747 return num_pages
/ n
+ !!(ceil
&& num_pages
% n
);
1750 static inline int getpageshift(void)
1752 return __builtin_ffs(getpagesize()) - 1;
1756 vm_num_host_pages(enum vm_guest_mode mode
, unsigned int num_guest_pages
)
1758 return vm_calc_num_pages(num_guest_pages
,
1759 vm_guest_mode_params
[mode
].page_shift
,
1760 getpageshift(), true);
1764 vm_num_guest_pages(enum vm_guest_mode mode
, unsigned int num_host_pages
)
1766 return vm_calc_num_pages(num_host_pages
, getpageshift(),
1767 vm_guest_mode_params
[mode
].page_shift
, false);
1770 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode
, size_t size
)
1773 n
= DIV_ROUND_UP(size
, vm_guest_mode_params
[mode
].page_size
);
1774 return vm_adjust_num_guest_pages(mode
, n
);