xref: /linux/tools/testing/selftests/kvm/lib/kvm_util.c (revision ac84bac4062e7fc24f5e2c61c6a414b2a00a29ad)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tools/testing/selftests/kvm/lib/kvm_util.c
4  *
5  * Copyright (C) 2018, Google LLC.
6  */
7 
8 #include "test_util.h"
9 #include "kvm_util.h"
10 #include "kvm_util_internal.h"
11 #include "processor.h"
12 
13 #include <assert.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <linux/kernel.h>
18 
19 #define KVM_UTIL_PGS_PER_HUGEPG 512
20 #define KVM_UTIL_MIN_PFN	2
21 
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)
24 {
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);
29 }
30 
31 /*
32  * Capability
33  *
34  * Input Args:
35  *   cap - Capability
36  *
37  * Output Args: None
38  *
39  * Return:
40  *   On success, the Value corresponding to the capability (KVM_CAP_*)
41  *   specified by the value of cap.  On failure a TEST_ASSERT failure
42  *   is produced.
43  *
44  * Looks up and returns the value corresponding to the capability
45  * (KVM_CAP_*) given by cap.
46  */
47 int kvm_check_cap(long cap)
48 {
49 	int ret;
50 	int kvm_fd;
51 
52 	kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
53 	if (kvm_fd < 0)
54 		exit(KSFT_SKIP);
55 
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);
59 
60 	close(kvm_fd);
61 
62 	return ret;
63 }
64 
65 /* VM Enable Capability
66  *
67  * Input Args:
68  *   vm - Virtual Machine
69  *   cap - Capability
70  *
71  * Output Args: None
72  *
73  * Return: On success, 0. On failure a TEST_ASSERT failure is produced.
74  *
75  * Enables a capability (KVM_CAP_*) on the VM.
76  */
77 int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
78 {
79 	int ret;
80 
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);
84 
85 	return ret;
86 }
87 
88 static void vm_open(struct kvm_vm *vm, int perm)
89 {
90 	vm->kvm_fd = open(KVM_DEV_PATH, perm);
91 	if (vm->kvm_fd < 0)
92 		exit(KSFT_SKIP);
93 
94 	if (!kvm_check_cap(KVM_CAP_IMMEDIATE_EXIT)) {
95 		print_skip("immediate_exit not available");
96 		exit(KSFT_SKIP);
97 	}
98 
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);
102 }
103 
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",
112 };
113 _Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
114 	       "Missing new mode strings?");
115 
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;
121 };
122 
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 },
131 };
132 _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
133 	       "Missing new mode params?");
134 
135 /*
136  * VM Create
137  *
138  * Input Args:
139  *   mode - VM Mode (e.g. VM_MODE_P52V48_4K)
140  *   phy_pages - Physical memory pages
141  *   perm - permission
142  *
143  * Output Args: None
144  *
145  * Return:
146  *   Pointer to opaque structure that describes the created VM.
147  *
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).
153  */
154 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
155 {
156 	struct kvm_vm *vm;
157 
158 	pr_debug("%s: mode='%s' pages='%ld' perm='%d'\n", __func__,
159 		 vm_guest_mode_string(mode), phy_pages, perm);
160 
161 	vm = calloc(1, sizeof(*vm));
162 	TEST_ASSERT(vm != NULL, "Insufficient Memory");
163 
164 	vm->mode = mode;
165 	vm->type = 0;
166 
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;
171 
172 	/* Setup mode specific traits. */
173 	switch (vm->mode) {
174 	case VM_MODE_P52V48_4K:
175 		vm->pgtable_levels = 4;
176 		break;
177 	case VM_MODE_P52V48_64K:
178 		vm->pgtable_levels = 3;
179 		break;
180 	case VM_MODE_P48V48_4K:
181 		vm->pgtable_levels = 4;
182 		break;
183 	case VM_MODE_P48V48_64K:
184 		vm->pgtable_levels = 3;
185 		break;
186 	case VM_MODE_P40V48_4K:
187 		vm->pgtable_levels = 4;
188 		break;
189 	case VM_MODE_P40V48_64K:
190 		vm->pgtable_levels = 3;
191 		break;
192 	case VM_MODE_PXXV48_4K:
193 #ifdef __x86_64__
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",
198 			 vm->pa_bits);
199 		vm->pgtable_levels = 4;
200 #else
201 		TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
202 #endif
203 		break;
204 	default:
205 		TEST_FAIL("Unknown guest mode, mode: 0x%x", mode);
206 	}
207 
208 #ifdef __aarch64__
209 	if (vm->pa_bits != 40)
210 		vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
211 #endif
212 
213 	vm_open(vm, perm);
214 
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);
222 
223 	/* Limit physical addresses to PA-bits. */
224 	vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
225 
226 	/* Allocate and setup memory for guest. */
227 	vm->vpages_mapped = sparsebit_alloc();
228 	if (phy_pages != 0)
229 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
230 					    0, 0, phy_pages, 0);
231 
232 	return vm;
233 }
234 
235 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
236 {
237 	return _vm_create(mode, phy_pages, perm);
238 }
239 
240 /*
241  * VM Restart
242  *
243  * Input Args:
244  *   vm - VM that has been released before
245  *   perm - permission
246  *
247  * Output Args: None
248  *
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
251  * into the guest.
252  */
253 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
254 {
255 	struct userspace_mem_region *region;
256 
257 	vm_open(vmp, perm);
258 	if (vmp->has_irqchip)
259 		vm_create_irqchip(vmp);
260 
261 	for (region = vmp->userspace_mem_region_head; region;
262 		region = region->next) {
263 		int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, &region->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);
272 	}
273 }
274 
275 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
276 {
277 	struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
278 	int ret;
279 
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));
283 }
284 
285 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
286 			    uint64_t first_page, uint32_t num_pages)
287 {
288 	struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
289 		                            .first_page = first_page,
290 	                                    .num_pages = num_pages };
291 	int ret;
292 
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));
296 }
297 
298 /*
299  * Userspace Memory Region Find
300  *
301  * Input Args:
302  *   vm - Virtual Machine
303  *   start - Starting VM physical address
304  *   end - Ending VM physical address, inclusive.
305  *
306  * Output Args: None
307  *
308  * Return:
309  *   Pointer to overlapping region, NULL if no such region.
310  *
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
315  * region exists.
316  */
317 static struct userspace_mem_region *
318 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
319 {
320 	struct userspace_mem_region *region;
321 
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)
328 			return region;
329 	}
330 
331 	return NULL;
332 }
333 
334 /*
335  * KVM Userspace Memory Region Find
336  *
337  * Input Args:
338  *   vm - Virtual Machine
339  *   start - Starting VM physical address
340  *   end - Ending VM physical address, inclusive.
341  *
342  * Output Args: None
343  *
344  * Return:
345  *   Pointer to overlapping region, NULL if no such region.
346  *
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.
349  */
350 struct kvm_userspace_memory_region *
351 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
352 				 uint64_t end)
353 {
354 	struct userspace_mem_region *region;
355 
356 	region = userspace_mem_region_find(vm, start, end);
357 	if (!region)
358 		return NULL;
359 
360 	return &region->region;
361 }
362 
363 /*
364  * VCPU Find
365  *
366  * Input Args:
367  *   vm - Virtual Machine
368  *   vcpuid - VCPU ID
369  *
370  * Output Args: None
371  *
372  * Return:
373  *   Pointer to VCPU structure
374  *
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.
378  */
379 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
380 {
381 	struct vcpu *vcpup;
382 
383 	for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
384 		if (vcpup->id == vcpuid)
385 			return vcpup;
386 	}
387 
388 	return NULL;
389 }
390 
391 /*
392  * VM VCPU Remove
393  *
394  * Input Args:
395  *   vm - Virtual Machine
396  *   vcpuid - VCPU ID
397  *
398  * Output Args: None
399  *
400  * Return: None, TEST_ASSERT failures for all error conditions
401  *
402  * Within the VM specified by vm, removes the VCPU given by vcpuid.
403  */
404 static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
405 {
406 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
407 	int ret;
408 
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);
412 	close(vcpu->fd);
413 	TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
414 		"errno: %i", ret, errno);
415 
416 	if (vcpu->next)
417 		vcpu->next->prev = vcpu->prev;
418 	if (vcpu->prev)
419 		vcpu->prev->next = vcpu->next;
420 	else
421 		vm->vcpu_head = vcpu->next;
422 	free(vcpu);
423 }
424 
425 void kvm_vm_release(struct kvm_vm *vmp)
426 {
427 	int ret;
428 
429 	while (vmp->vcpu_head)
430 		vm_vcpu_rm(vmp, vmp->vcpu_head->id);
431 
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);
435 
436 	close(vmp->kvm_fd);
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);
439 }
440 
441 /*
442  * Destroys and frees the VM pointed to by vmp.
443  */
444 void kvm_vm_free(struct kvm_vm *vmp)
445 {
446 	int ret;
447 
448 	if (vmp == NULL)
449 		return;
450 
451 	/* Free userspace_mem_regions. */
452 	while (vmp->userspace_mem_region_head) {
453 		struct userspace_mem_region *region
454 			= vmp->userspace_mem_region_head;
455 
456 		region->region.memory_size = 0;
457 		ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
458 			&region->region);
459 		TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
460 			"rc: %i errno: %i", ret, errno);
461 
462 		vmp->userspace_mem_region_head = region->next;
463 		sparsebit_free(&region->unused_phy_pages);
464 		ret = munmap(region->mmap_start, region->mmap_size);
465 		TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
466 			    ret, errno);
467 
468 		free(region);
469 	}
470 
471 	/* Free sparsebit arrays. */
472 	sparsebit_free(&vmp->vpages_valid);
473 	sparsebit_free(&vmp->vpages_mapped);
474 
475 	kvm_vm_release(vmp);
476 
477 	/* Free the structure describing the VM. */
478 	free(vmp);
479 }
480 
481 /*
482  * Memory Compare, host virtual to guest virtual
483  *
484  * Input Args:
485  *   hva - Starting host virtual address
486  *   vm - Virtual Machine
487  *   gva - Starting guest virtual address
488  *   len - number of bytes to compare
489  *
490  * Output Args: None
491  *
492  * Input/Output Args: None
493  *
494  * Return:
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.
499  *
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.
503  */
504 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
505 {
506 	size_t amt;
507 
508 	/*
509 	 * Compare a batch of bytes until either a match is found
510 	 * or all the bytes have been compared.
511 	 */
512 	for (uintptr_t offset = 0; offset < len; offset += amt) {
513 		uintptr_t ptr1 = (uintptr_t)hva + offset;
514 
515 		/*
516 		 * Determine host address for guest virtual address
517 		 * at offset.
518 		 */
519 		uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
520 
521 		/*
522 		 * Determine amount to compare on this pass.
523 		 * Don't allow the comparsion to cross a page boundary.
524 		 */
525 		amt = len - offset;
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);
530 
531 		assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
532 		assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
533 
534 		/*
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.
538 		 */
539 		int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
540 		if (ret != 0)
541 			return ret;
542 	}
543 
544 	/*
545 	 * No mismatch found.  Let the caller know the two memory
546 	 * areas are equal.
547 	 */
548 	return 0;
549 }
550 
551 /*
552  * VM Userspace Memory Region Add
553  *
554  * Input Args:
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)
562  *
563  * Output Args: None
564  *
565  * Return: None
566  *
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.
572  */
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,
576 	uint32_t flags)
577 {
578 	int ret;
579 	struct userspace_mem_region *region;
580 	size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
581 	size_t alignment;
582 
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));
586 
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);
597 
598 	/*
599 	 * Confirm a mem region with an overlapping address doesn't
600 	 * already exist.
601 	 */
602 	region = (struct userspace_mem_region *) userspace_mem_region_find(
603 		vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
604 	if (region != NULL)
605 		TEST_FAIL("overlapping userspace_mem_region already "
606 			"exists\n"
607 			"  requested guest_paddr: 0x%lx npages: 0x%lx "
608 			"page_size: 0x%x\n"
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);
613 
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)
618 			break;
619 	}
620 	if (region != NULL)
621 		TEST_FAIL("A mem region with the requested slot "
622 			"already exists.\n"
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,
626 			region->region.slot,
627 			(uint64_t) region->region.guest_phys_addr,
628 			(uint64_t) region->region.memory_size);
629 
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;
634 
635 #ifdef __s390x__
636 	/* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
637 	alignment = 0x100000;
638 #else
639 	alignment = 1;
640 #endif
641 
642 	if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
643 		alignment = max(huge_page_size, alignment);
644 
645 	/* Add enough memory to align up if necessary */
646 	if (alignment > 1)
647 		region->mmap_size += alignment;
648 
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),
653 				  -1, 0);
654 	TEST_ASSERT(region->mmap_start != MAP_FAILED,
655 		    "test_malloc failed, mmap_start: %p errno: %i",
656 		    region->mmap_start, errno);
657 
658 	/* Align host address */
659 	region->host_mem = align(region->mmap_start, alignment);
660 
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"
666 			    "  addr: %p\n"
667 			    "  length: 0x%lx\n"
668 			    "  src_type: %x",
669 			    region->host_mem, npages * vm->page_size, src_type);
670 	}
671 
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, &region->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);
687 
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;
693 }
694 
695 /*
696  * Memslot to region
697  *
698  * Input Args:
699  *   vm - Virtual Machine
700  *   memslot - KVM memory slot ID
701  *
702  * Output Args: None
703  *
704  * Return:
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
708  *   memory slot ID).
709  */
710 struct userspace_mem_region *
711 memslot2region(struct kvm_vm *vm, uint32_t memslot)
712 {
713 	struct userspace_mem_region *region;
714 
715 	for (region = vm->userspace_mem_region_head; region;
716 		region = region->next) {
717 		if (region->region.slot == memslot)
718 			break;
719 	}
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");
726 	}
727 
728 	return region;
729 }
730 
731 /*
732  * VM Memory Region Flags Set
733  *
734  * Input Args:
735  *   vm - Virtual Machine
736  *   flags - Starting guest physical address
737  *
738  * Output Args: None
739  *
740  * Return: None
741  *
742  * Sets the flags of the memory region specified by the value of slot,
743  * to the values given by flags.
744  */
745 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
746 {
747 	int ret;
748 	struct userspace_mem_region *region;
749 
750 	region = memslot2region(vm, slot);
751 
752 	region->region.flags = flags;
753 
754 	ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
755 
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);
759 }
760 
761 /*
762  * VM Memory Region Move
763  *
764  * Input Args:
765  *   vm - Virtual Machine
766  *   slot - Slot of the memory region to move
767  *   new_gpa - Starting guest physical address
768  *
769  * Output Args: None
770  *
771  * Return: None
772  *
773  * Change the gpa of a memory region.
774  */
775 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
776 {
777 	struct userspace_mem_region *region;
778 	int ret;
779 
780 	region = memslot2region(vm, slot);
781 
782 	region->region.guest_phys_addr = new_gpa;
783 
784 	ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
785 
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);
789 }
790 
791 /*
792  * VCPU mmap Size
793  *
794  * Input Args: None
795  *
796  * Output Args: None
797  *
798  * Return:
799  *   Size of VCPU state
800  *
801  * Returns the size of the structure pointed to by the return value
802  * of vcpu_state().
803  */
804 static int vcpu_mmap_sz(void)
805 {
806 	int dev_fd, ret;
807 
808 	dev_fd = open(KVM_DEV_PATH, O_RDONLY);
809 	if (dev_fd < 0)
810 		exit(KSFT_SKIP);
811 
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);
816 
817 	close(dev_fd);
818 
819 	return ret;
820 }
821 
822 /*
823  * VM VCPU Add
824  *
825  * Input Args:
826  *   vm - Virtual Machine
827  *   vcpuid - VCPU ID
828  *
829  * Output Args: None
830  *
831  * Return: None
832  *
833  * Adds a virtual CPU to the VM specified by vm with the ID given by vcpuid.
834  * No additional VCPU setup is done.
835  */
836 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid)
837 {
838 	struct vcpu *vcpu;
839 
840 	/* Confirm a vcpu with the specified id doesn't already exist. */
841 	vcpu = vcpu_find(vm, vcpuid);
842 	if (vcpu != NULL)
843 		TEST_FAIL("vcpu with the specified id "
844 			"already exists,\n"
845 			"  requested vcpuid: %u\n"
846 			"  existing vcpuid: %u state: %p",
847 			vcpuid, vcpu->id, vcpu->state);
848 
849 	/* Allocate and initialize new vcpu structure. */
850 	vcpu = calloc(1, sizeof(*vcpu));
851 	TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
852 	vcpu->id = vcpuid;
853 	vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
854 	TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
855 		vcpu->fd, errno);
856 
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);
864 
865 	/* Add to linked-list of VCPUs. */
866 	if (vm->vcpu_head)
867 		vm->vcpu_head->prev = vcpu;
868 	vcpu->next = vm->vcpu_head;
869 	vm->vcpu_head = vcpu;
870 }
871 
872 /*
873  * VM Virtual Address Unused Gap
874  *
875  * Input Args:
876  *   vm - Virtual Machine
877  *   sz - Size (bytes)
878  *   vaddr_min - Minimum Virtual Address
879  *
880  * Output Args: None
881  *
882  * Return:
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.
886  *
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.
891  */
892 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
893 				      vm_vaddr_t vaddr_min)
894 {
895 	uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
896 
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)
900 		goto no_va_found;
901 
902 	/* Loop over section with enough valid virtual page indexes. */
903 	if (!sparsebit_is_set_num(vm->vpages_valid,
904 		pgidx_start, pages))
905 		pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
906 			pgidx_start, pages);
907 	do {
908 		/*
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
912 		 * possible.
913 		 */
914 		if (sparsebit_is_clear_num(vm->vpages_mapped,
915 			pgidx_start, pages))
916 			goto va_found;
917 		pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
918 			pgidx_start, pages);
919 		if (pgidx_start == 0)
920 			goto no_va_found;
921 
922 		/*
923 		 * If needed, adjust proposed starting virtual address,
924 		 * to next range of valid virtual addresses.
925 		 */
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)
931 				goto no_va_found;
932 		}
933 	} while (pgidx_start != 0);
934 
935 no_va_found:
936 	TEST_FAIL("No vaddr of specified pages available, pages: 0x%lx", pages);
937 
938 	/* NOT REACHED */
939 	return -1;
940 
941 va_found:
942 	TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
943 		pgidx_start, pages),
944 		"Unexpected, invalid virtual page index range,\n"
945 		"  pgidx_start: 0x%lx\n"
946 		"  pages: 0x%lx",
947 		pgidx_start, pages);
948 	TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
949 		pgidx_start, pages),
950 		"Unexpected, pages already mapped,\n"
951 		"  pgidx_start: 0x%lx\n"
952 		"  pages: 0x%lx",
953 		pgidx_start, pages);
954 
955 	return pgidx_start * vm->page_size;
956 }
957 
958 /*
959  * VM Virtual Address Allocate
960  *
961  * Input Args:
962  *   vm - Virtual Machine
963  *   sz - Size in bytes
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
967  *
968  * Output Args: None
969  *
970  * Return:
971  *   Starting guest virtual address
972  *
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
977  * a page.
978  */
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)
981 {
982 	uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
983 
984 	virt_pgd_alloc(vm, pgd_memslot);
985 
986 	/*
987 	 * Find an unused range of virtual page addresses of at least
988 	 * pages in length.
989 	 */
990 	vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
991 
992 	/* Map the virtual pages. */
993 	for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
994 		pages--, vaddr += vm->page_size) {
995 		vm_paddr_t paddr;
996 
997 		paddr = vm_phy_page_alloc(vm,
998 				KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
999 
1000 		virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1001 
1002 		sparsebit_set(vm->vpages_mapped,
1003 			vaddr >> vm->page_shift);
1004 	}
1005 
1006 	return vaddr_start;
1007 }
1008 
1009 /*
1010  * Map a range of VM virtual address to the VM's physical address
1011  *
1012  * Input Args:
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
1018  *
1019  * Output Args: None
1020  *
1021  * Return: None
1022  *
1023  * Within the VM given by @vm, creates a virtual translation for
1024  * @npages starting at @vaddr to the page range starting at @paddr.
1025  */
1026 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1027 	      unsigned int npages, uint32_t pgd_memslot)
1028 {
1029 	size_t page_size = vm->page_size;
1030 	size_t size = npages * page_size;
1031 
1032 	TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1033 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1034 
1035 	while (npages--) {
1036 		virt_pg_map(vm, vaddr, paddr, pgd_memslot);
1037 		vaddr += page_size;
1038 		paddr += page_size;
1039 	}
1040 }
1041 
1042 /*
1043  * Address VM Physical to Host Virtual
1044  *
1045  * Input Args:
1046  *   vm - Virtual Machine
1047  *   gpa - VM physical address
1048  *
1049  * Output Args: None
1050  *
1051  * Return:
1052  *   Equivalent host virtual address
1053  *
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.
1058  */
1059 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1060 {
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));
1069 	}
1070 
1071 	TEST_FAIL("No vm physical memory at 0x%lx", gpa);
1072 	return NULL;
1073 }
1074 
1075 /*
1076  * Address Host Virtual to VM Physical
1077  *
1078  * Input Args:
1079  *   vm - Virtual Machine
1080  *   hva - Host virtual address
1081  *
1082  * Output Args: None
1083  *
1084  * Return:
1085  *   Equivalent VM physical address
1086  *
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.
1091  */
1092 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1093 {
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));
1103 	}
1104 
1105 	TEST_FAIL("No mapping to a guest physical address, hva: %p", hva);
1106 	return -1;
1107 }
1108 
1109 /*
1110  * VM Create IRQ Chip
1111  *
1112  * Input Args:
1113  *   vm - Virtual Machine
1114  *
1115  * Output Args: None
1116  *
1117  * Return: None
1118  *
1119  * Creates an interrupt controller chip for the VM specified by vm.
1120  */
1121 void vm_create_irqchip(struct kvm_vm *vm)
1122 {
1123 	int ret;
1124 
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);
1128 
1129 	vm->has_irqchip = true;
1130 }
1131 
1132 /*
1133  * VM VCPU State
1134  *
1135  * Input Args:
1136  *   vm - Virtual Machine
1137  *   vcpuid - VCPU ID
1138  *
1139  * Output Args: None
1140  *
1141  * Return:
1142  *   Pointer to structure that describes the state of the VCPU.
1143  *
1144  * Locates and returns a pointer to a structure that describes the
1145  * state of the VCPU with the given vcpuid.
1146  */
1147 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1148 {
1149 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1150 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1151 
1152 	return vcpu->state;
1153 }
1154 
1155 /*
1156  * VM VCPU Run
1157  *
1158  * Input Args:
1159  *   vm - Virtual Machine
1160  *   vcpuid - VCPU ID
1161  *
1162  * Output Args: None
1163  *
1164  * Return: None
1165  *
1166  * Switch to executing the code for the VCPU given by vcpuid, within the VM
1167  * given by vm.
1168  */
1169 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1170 {
1171 	int ret = _vcpu_run(vm, vcpuid);
1172 	TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1173 		"rc: %i errno: %i", ret, errno);
1174 }
1175 
1176 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1177 {
1178 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1179 	int rc;
1180 
1181 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1182 	do {
1183 		rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1184 	} while (rc == -1 && errno == EINTR);
1185 	return rc;
1186 }
1187 
1188 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1189 {
1190 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1191 	int ret;
1192 
1193 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1194 
1195 	vcpu->state->immediate_exit = 1;
1196 	ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1197 	vcpu->state->immediate_exit = 0;
1198 
1199 	TEST_ASSERT(ret == -1 && errno == EINTR,
1200 		    "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1201 		    ret, errno);
1202 }
1203 
1204 /*
1205  * VM VCPU Set MP State
1206  *
1207  * Input Args:
1208  *   vm - Virtual Machine
1209  *   vcpuid - VCPU ID
1210  *   mp_state - mp_state to be set
1211  *
1212  * Output Args: None
1213  *
1214  * Return: None
1215  *
1216  * Sets the MP state of the VCPU given by vcpuid, to the state given
1217  * by mp_state.
1218  */
1219 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1220 		       struct kvm_mp_state *mp_state)
1221 {
1222 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1223 	int ret;
1224 
1225 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1226 
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);
1230 }
1231 
1232 /*
1233  * VM VCPU Regs Get
1234  *
1235  * Input Args:
1236  *   vm - Virtual Machine
1237  *   vcpuid - VCPU ID
1238  *
1239  * Output Args:
1240  *   regs - current state of VCPU regs
1241  *
1242  * Return: None
1243  *
1244  * Obtains the current register state for the VCPU specified by vcpuid
1245  * and stores it at the location given by regs.
1246  */
1247 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1248 {
1249 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1250 	int ret;
1251 
1252 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1253 
1254 	ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1255 	TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1256 		ret, errno);
1257 }
1258 
1259 /*
1260  * VM VCPU Regs Set
1261  *
1262  * Input Args:
1263  *   vm - Virtual Machine
1264  *   vcpuid - VCPU ID
1265  *   regs - Values to set VCPU regs to
1266  *
1267  * Output Args: None
1268  *
1269  * Return: None
1270  *
1271  * Sets the regs of the VCPU specified by vcpuid to the values
1272  * given by regs.
1273  */
1274 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1275 {
1276 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1277 	int ret;
1278 
1279 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1280 
1281 	ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1282 	TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1283 		ret, errno);
1284 }
1285 
1286 #ifdef __KVM_HAVE_VCPU_EVENTS
1287 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1288 		     struct kvm_vcpu_events *events)
1289 {
1290 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1291 	int ret;
1292 
1293 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1294 
1295 	ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1296 	TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1297 		ret, errno);
1298 }
1299 
1300 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1301 		     struct kvm_vcpu_events *events)
1302 {
1303 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1304 	int ret;
1305 
1306 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1307 
1308 	ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
1309 	TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1310 		ret, errno);
1311 }
1312 #endif
1313 
1314 #ifdef __x86_64__
1315 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1316 			   struct kvm_nested_state *state)
1317 {
1318 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1319 	int ret;
1320 
1321 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1322 
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",
1326 		ret, errno);
1327 }
1328 
1329 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1330 			  struct kvm_nested_state *state, bool ignore_error)
1331 {
1332 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1333 	int ret;
1334 
1335 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1336 
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",
1341 			ret, errno);
1342 	}
1343 
1344 	return ret;
1345 }
1346 #endif
1347 
1348 /*
1349  * VM VCPU System Regs Get
1350  *
1351  * Input Args:
1352  *   vm - Virtual Machine
1353  *   vcpuid - VCPU ID
1354  *
1355  * Output Args:
1356  *   sregs - current state of VCPU system regs
1357  *
1358  * Return: None
1359  *
1360  * Obtains the current system register state for the VCPU specified by
1361  * vcpuid and stores it at the location given by sregs.
1362  */
1363 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1364 {
1365 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1366 	int ret;
1367 
1368 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1369 
1370 	ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1371 	TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1372 		ret, errno);
1373 }
1374 
1375 /*
1376  * VM VCPU System Regs Set
1377  *
1378  * Input Args:
1379  *   vm - Virtual Machine
1380  *   vcpuid - VCPU ID
1381  *   sregs - Values to set VCPU system regs to
1382  *
1383  * Output Args: None
1384  *
1385  * Return: None
1386  *
1387  * Sets the system regs of the VCPU specified by vcpuid to the values
1388  * given by sregs.
1389  */
1390 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1391 {
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);
1395 }
1396 
1397 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1398 {
1399 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1400 
1401 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1402 
1403 	return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1404 }
1405 
1406 void vcpu_fpu_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1407 {
1408 	int ret;
1409 
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));
1413 }
1414 
1415 void vcpu_fpu_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_fpu *fpu)
1416 {
1417 	int ret;
1418 
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));
1422 }
1423 
1424 void vcpu_get_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1425 {
1426 	int ret;
1427 
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));
1431 }
1432 
1433 void vcpu_set_reg(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_one_reg *reg)
1434 {
1435 	int ret;
1436 
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));
1440 }
1441 
1442 /*
1443  * VCPU Ioctl
1444  *
1445  * Input Args:
1446  *   vm - Virtual Machine
1447  *   vcpuid - VCPU ID
1448  *   cmd - Ioctl number
1449  *   arg - Argument to pass to the ioctl
1450  *
1451  * Return: None
1452  *
1453  * Issues an arbitrary ioctl on a VCPU fd.
1454  */
1455 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1456 		unsigned long cmd, void *arg)
1457 {
1458 	int ret;
1459 
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));
1463 }
1464 
1465 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1466 		unsigned long cmd, void *arg)
1467 {
1468 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1469 	int ret;
1470 
1471 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1472 
1473 	ret = ioctl(vcpu->fd, cmd, arg);
1474 
1475 	return ret;
1476 }
1477 
1478 /*
1479  * VM Ioctl
1480  *
1481  * Input Args:
1482  *   vm - Virtual Machine
1483  *   cmd - Ioctl number
1484  *   arg - Argument to pass to the ioctl
1485  *
1486  * Return: None
1487  *
1488  * Issues an arbitrary ioctl on a VM fd.
1489  */
1490 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1491 {
1492 	int ret;
1493 
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));
1497 }
1498 
1499 /*
1500  * VM Dump
1501  *
1502  * Input Args:
1503  *   vm - Virtual Machine
1504  *   indent - Left margin indent amount
1505  *
1506  * Output Args:
1507  *   stream - Output FILE stream
1508  *
1509  * Return: None
1510  *
1511  * Dumps the current state of the VM given by vm, to the FILE stream
1512  * given by stream.
1513  */
1514 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1515 {
1516 	struct userspace_mem_region *region;
1517 	struct vcpu *vcpu;
1518 
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,
1529 			region->host_mem);
1530 		fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1531 		sparsebit_dump(stream, region->unused_phy_pages, 0);
1532 	}
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, "",
1536 		vm->pgd_created);
1537 	if (vm->pgd_created) {
1538 		fprintf(stream, "%*sVirtual Translation Tables:\n",
1539 			indent + 2, "");
1540 		virt_dump(stream, vm, indent + 4);
1541 	}
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);
1545 }
1546 
1547 /* Known KVM exit reasons */
1548 static struct exit_reason {
1549 	unsigned int reason;
1550 	const char *name;
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"},
1574 #endif
1575 };
1576 
1577 /*
1578  * Exit Reason String
1579  *
1580  * Input Args:
1581  *   exit_reason - Exit reason
1582  *
1583  * Output Args: None
1584  *
1585  * Return:
1586  *   Constant string pointer describing the exit reason.
1587  *
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.
1591  */
1592 const char *exit_reason_str(unsigned int exit_reason)
1593 {
1594 	unsigned int n1;
1595 
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;
1599 	}
1600 
1601 	return "Unknown";
1602 }
1603 
1604 /*
1605  * Physical Contiguous Page Allocator
1606  *
1607  * Input Args:
1608  *   vm - Virtual Machine
1609  *   num - number of pages
1610  *   paddr_min - Physical address minimum
1611  *   memslot - Memory region to allocate page from
1612  *
1613  * Output Args: None
1614  *
1615  * Return:
1616  *   Starting physical address
1617  *
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.
1622  */
1623 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1624 			      vm_paddr_t paddr_min, uint32_t memslot)
1625 {
1626 	struct userspace_mem_region *region;
1627 	sparsebit_idx_t pg, base;
1628 
1629 	TEST_ASSERT(num > 0, "Must allocate at least one page");
1630 
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);
1635 
1636 	region = memslot2region(vm, memslot);
1637 	base = pg = paddr_min >> vm->page_shift;
1638 
1639 	do {
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);
1643 				break;
1644 			}
1645 		}
1646 	} while (pg && pg != base + num);
1647 
1648 	if (pg == 0) {
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);
1654 		abort();
1655 	}
1656 
1657 	for (pg = base; pg < base + num; ++pg)
1658 		sparsebit_clear(region->unused_phy_pages, pg);
1659 
1660 	return base * vm->page_size;
1661 }
1662 
1663 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1664 			     uint32_t memslot)
1665 {
1666 	return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1667 }
1668 
1669 /*
1670  * Address Guest Virtual to Host Virtual
1671  *
1672  * Input Args:
1673  *   vm - Virtual Machine
1674  *   gva - VM virtual address
1675  *
1676  * Output Args: None
1677  *
1678  * Return:
1679  *   Equivalent host virtual address
1680  */
1681 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1682 {
1683 	return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1684 }
1685 
1686 /*
1687  * Is Unrestricted Guest
1688  *
1689  * Input Args:
1690  *   vm - Virtual Machine
1691  *
1692  * Output Args: None
1693  *
1694  * Return: True if the unrestricted guest is set to 'Y', otherwise return false.
1695  *
1696  * Check if the unrestricted guest flag is enabled.
1697  */
1698 bool vm_is_unrestricted_guest(struct kvm_vm *vm)
1699 {
1700 	char val = 'N';
1701 	size_t count;
1702 	FILE *f;
1703 
1704 	if (vm == NULL) {
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",
1708 			    errno);
1709 		fclose(f);
1710 	}
1711 
1712 	f = fopen("/sys/module/kvm_intel/parameters/unrestricted_guest", "r");
1713 	if (f) {
1714 		count = fread(&val, sizeof(char), 1, f);
1715 		TEST_ASSERT(count == 1, "Unable to read from param file.");
1716 		fclose(f);
1717 	}
1718 
1719 	return val == 'Y';
1720 }
1721 
1722 unsigned int vm_get_page_size(struct kvm_vm *vm)
1723 {
1724 	return vm->page_size;
1725 }
1726 
1727 unsigned int vm_get_page_shift(struct kvm_vm *vm)
1728 {
1729 	return vm->page_shift;
1730 }
1731 
1732 unsigned int vm_get_max_gfn(struct kvm_vm *vm)
1733 {
1734 	return vm->max_gfn;
1735 }
1736 
1737 static unsigned int vm_calc_num_pages(unsigned int num_pages,
1738 				      unsigned int page_shift,
1739 				      unsigned int new_page_shift,
1740 				      bool ceil)
1741 {
1742 	unsigned int n = 1 << (new_page_shift - page_shift);
1743 
1744 	if (page_shift >= new_page_shift)
1745 		return num_pages * (1 << (page_shift - new_page_shift));
1746 
1747 	return num_pages / n + !!(ceil && num_pages % n);
1748 }
1749 
1750 static inline int getpageshift(void)
1751 {
1752 	return __builtin_ffs(getpagesize()) - 1;
1753 }
1754 
1755 unsigned int
1756 vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
1757 {
1758 	return vm_calc_num_pages(num_guest_pages,
1759 				 vm_guest_mode_params[mode].page_shift,
1760 				 getpageshift(), true);
1761 }
1762 
1763 unsigned int
1764 vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
1765 {
1766 	return vm_calc_num_pages(num_host_pages, getpageshift(),
1767 				 vm_guest_mode_params[mode].page_shift, false);
1768 }
1769 
1770 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
1771 {
1772 	unsigned int n;
1773 	n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
1774 	return vm_adjust_num_guest_pages(mode, n);
1775 }
1776