xref: /linux/tools/testing/selftests/kvm/lib/kvm_util.c (revision af873fcecef567abf8a3468b06dd4e4aab46da6d)
1 /*
2  * tools/testing/selftests/kvm/lib/kvm_util.c
3  *
4  * Copyright (C) 2018, Google LLC.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  */
8 
9 #include "test_util.h"
10 #include "kvm_util.h"
11 #include "kvm_util_internal.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, unsigned long type)
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 		fprintf(stderr, "immediate_exit not available, skipping test\n");
96 		exit(KSFT_SKIP);
97 	}
98 
99 	vm->fd = ioctl(vm->kvm_fd, KVM_CREATE_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 };
112 _Static_assert(sizeof(vm_guest_mode_string)/sizeof(char *) == NUM_VM_MODES,
113 	       "Missing new mode strings?");
114 
115 /*
116  * VM Create
117  *
118  * Input Args:
119  *   mode - VM Mode (e.g. VM_MODE_P52V48_4K)
120  *   phy_pages - Physical memory pages
121  *   perm - permission
122  *
123  * Output Args: None
124  *
125  * Return:
126  *   Pointer to opaque structure that describes the created VM.
127  *
128  * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K).
129  * When phy_pages is non-zero, a memory region of phy_pages physical pages
130  * is created and mapped starting at guest physical address 0.  The file
131  * descriptor to control the created VM is created with the permissions
132  * given by perm (e.g. O_RDWR).
133  */
134 struct kvm_vm *_vm_create(enum vm_guest_mode mode, uint64_t phy_pages,
135 			  int perm, unsigned long type)
136 {
137 	struct kvm_vm *vm;
138 
139 	vm = calloc(1, sizeof(*vm));
140 	TEST_ASSERT(vm != NULL, "Insufficient Memory");
141 
142 	vm->mode = mode;
143 	vm->type = type;
144 	vm_open(vm, perm, type);
145 
146 	/* Setup mode specific traits. */
147 	switch (vm->mode) {
148 	case VM_MODE_P52V48_4K:
149 		vm->pgtable_levels = 4;
150 		vm->pa_bits = 52;
151 		vm->va_bits = 48;
152 		vm->page_size = 0x1000;
153 		vm->page_shift = 12;
154 		break;
155 	case VM_MODE_P52V48_64K:
156 		vm->pgtable_levels = 3;
157 		vm->pa_bits = 52;
158 		vm->va_bits = 48;
159 		vm->page_size = 0x10000;
160 		vm->page_shift = 16;
161 		break;
162 	case VM_MODE_P48V48_4K:
163 		vm->pgtable_levels = 4;
164 		vm->pa_bits = 48;
165 		vm->va_bits = 48;
166 		vm->page_size = 0x1000;
167 		vm->page_shift = 12;
168 		break;
169 	case VM_MODE_P48V48_64K:
170 		vm->pgtable_levels = 3;
171 		vm->pa_bits = 48;
172 		vm->va_bits = 48;
173 		vm->page_size = 0x10000;
174 		vm->page_shift = 16;
175 		break;
176 	case VM_MODE_P40V48_4K:
177 		vm->pgtable_levels = 4;
178 		vm->pa_bits = 40;
179 		vm->va_bits = 48;
180 		vm->page_size = 0x1000;
181 		vm->page_shift = 12;
182 		break;
183 	case VM_MODE_P40V48_64K:
184 		vm->pgtable_levels = 3;
185 		vm->pa_bits = 40;
186 		vm->va_bits = 48;
187 		vm->page_size = 0x10000;
188 		vm->page_shift = 16;
189 		break;
190 	default:
191 		TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
192 	}
193 
194 	/* Limit to VA-bit canonical virtual addresses. */
195 	vm->vpages_valid = sparsebit_alloc();
196 	sparsebit_set_num(vm->vpages_valid,
197 		0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
198 	sparsebit_set_num(vm->vpages_valid,
199 		(~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
200 		(1ULL << (vm->va_bits - 1)) >> vm->page_shift);
201 
202 	/* Limit physical addresses to PA-bits. */
203 	vm->max_gfn = ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
204 
205 	/* Allocate and setup memory for guest. */
206 	vm->vpages_mapped = sparsebit_alloc();
207 	if (phy_pages != 0)
208 		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
209 					    0, 0, phy_pages, 0);
210 
211 	return vm;
212 }
213 
214 struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
215 {
216 	return _vm_create(mode, phy_pages, perm, 0);
217 }
218 
219 /*
220  * VM Restart
221  *
222  * Input Args:
223  *   vm - VM that has been released before
224  *   perm - permission
225  *
226  * Output Args: None
227  *
228  * Reopens the file descriptors associated to the VM and reinstates the
229  * global state, such as the irqchip and the memory regions that are mapped
230  * into the guest.
231  */
232 void kvm_vm_restart(struct kvm_vm *vmp, int perm)
233 {
234 	struct userspace_mem_region *region;
235 
236 	vm_open(vmp, perm, vmp->type);
237 	if (vmp->has_irqchip)
238 		vm_create_irqchip(vmp);
239 
240 	for (region = vmp->userspace_mem_region_head; region;
241 		region = region->next) {
242 		int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
243 		TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
244 			    "  rc: %i errno: %i\n"
245 			    "  slot: %u flags: 0x%x\n"
246 			    "  guest_phys_addr: 0x%lx size: 0x%lx",
247 			    ret, errno, region->region.slot,
248 			    region->region.flags,
249 			    region->region.guest_phys_addr,
250 			    region->region.memory_size);
251 	}
252 }
253 
254 void kvm_vm_get_dirty_log(struct kvm_vm *vm, int slot, void *log)
255 {
256 	struct kvm_dirty_log args = { .dirty_bitmap = log, .slot = slot };
257 	int ret;
258 
259 	ret = ioctl(vm->fd, KVM_GET_DIRTY_LOG, &args);
260 	TEST_ASSERT(ret == 0, "%s: KVM_GET_DIRTY_LOG failed: %s",
261 		    strerror(-ret));
262 }
263 
264 void kvm_vm_clear_dirty_log(struct kvm_vm *vm, int slot, void *log,
265 			    uint64_t first_page, uint32_t num_pages)
266 {
267 	struct kvm_clear_dirty_log args = { .dirty_bitmap = log, .slot = slot,
268 		                            .first_page = first_page,
269 	                                    .num_pages = num_pages };
270 	int ret;
271 
272 	ret = ioctl(vm->fd, KVM_CLEAR_DIRTY_LOG, &args);
273 	TEST_ASSERT(ret == 0, "%s: KVM_CLEAR_DIRTY_LOG failed: %s",
274 		    strerror(-ret));
275 }
276 
277 /*
278  * Userspace Memory Region Find
279  *
280  * Input Args:
281  *   vm - Virtual Machine
282  *   start - Starting VM physical address
283  *   end - Ending VM physical address, inclusive.
284  *
285  * Output Args: None
286  *
287  * Return:
288  *   Pointer to overlapping region, NULL if no such region.
289  *
290  * Searches for a region with any physical memory that overlaps with
291  * any portion of the guest physical addresses from start to end
292  * inclusive.  If multiple overlapping regions exist, a pointer to any
293  * of the regions is returned.  Null is returned only when no overlapping
294  * region exists.
295  */
296 static struct userspace_mem_region *
297 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
298 {
299 	struct userspace_mem_region *region;
300 
301 	for (region = vm->userspace_mem_region_head; region;
302 		region = region->next) {
303 		uint64_t existing_start = region->region.guest_phys_addr;
304 		uint64_t existing_end = region->region.guest_phys_addr
305 			+ region->region.memory_size - 1;
306 		if (start <= existing_end && end >= existing_start)
307 			return region;
308 	}
309 
310 	return NULL;
311 }
312 
313 /*
314  * KVM Userspace Memory Region Find
315  *
316  * Input Args:
317  *   vm - Virtual Machine
318  *   start - Starting VM physical address
319  *   end - Ending VM physical address, inclusive.
320  *
321  * Output Args: None
322  *
323  * Return:
324  *   Pointer to overlapping region, NULL if no such region.
325  *
326  * Public interface to userspace_mem_region_find. Allows tests to look up
327  * the memslot datastructure for a given range of guest physical memory.
328  */
329 struct kvm_userspace_memory_region *
330 kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,
331 				 uint64_t end)
332 {
333 	struct userspace_mem_region *region;
334 
335 	region = userspace_mem_region_find(vm, start, end);
336 	if (!region)
337 		return NULL;
338 
339 	return &region->region;
340 }
341 
342 /*
343  * VCPU Find
344  *
345  * Input Args:
346  *   vm - Virtual Machine
347  *   vcpuid - VCPU ID
348  *
349  * Output Args: None
350  *
351  * Return:
352  *   Pointer to VCPU structure
353  *
354  * Locates a vcpu structure that describes the VCPU specified by vcpuid and
355  * returns a pointer to it.  Returns NULL if the VM doesn't contain a VCPU
356  * for the specified vcpuid.
357  */
358 struct vcpu *vcpu_find(struct kvm_vm *vm, uint32_t vcpuid)
359 {
360 	struct vcpu *vcpup;
361 
362 	for (vcpup = vm->vcpu_head; vcpup; vcpup = vcpup->next) {
363 		if (vcpup->id == vcpuid)
364 			return vcpup;
365 	}
366 
367 	return NULL;
368 }
369 
370 /*
371  * VM VCPU Remove
372  *
373  * Input Args:
374  *   vm - Virtual Machine
375  *   vcpuid - VCPU ID
376  *
377  * Output Args: None
378  *
379  * Return: None, TEST_ASSERT failures for all error conditions
380  *
381  * Within the VM specified by vm, removes the VCPU given by vcpuid.
382  */
383 static void vm_vcpu_rm(struct kvm_vm *vm, uint32_t vcpuid)
384 {
385 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
386 	int ret;
387 
388 	ret = munmap(vcpu->state, sizeof(*vcpu->state));
389 	TEST_ASSERT(ret == 0, "munmap of VCPU fd failed, rc: %i "
390 		"errno: %i", ret, errno);
391 	close(vcpu->fd);
392 	TEST_ASSERT(ret == 0, "Close of VCPU fd failed, rc: %i "
393 		"errno: %i", ret, errno);
394 
395 	if (vcpu->next)
396 		vcpu->next->prev = vcpu->prev;
397 	if (vcpu->prev)
398 		vcpu->prev->next = vcpu->next;
399 	else
400 		vm->vcpu_head = vcpu->next;
401 	free(vcpu);
402 }
403 
404 void kvm_vm_release(struct kvm_vm *vmp)
405 {
406 	int ret;
407 
408 	while (vmp->vcpu_head)
409 		vm_vcpu_rm(vmp, vmp->vcpu_head->id);
410 
411 	ret = close(vmp->fd);
412 	TEST_ASSERT(ret == 0, "Close of vm fd failed,\n"
413 		"  vmp->fd: %i rc: %i errno: %i", vmp->fd, ret, errno);
414 
415 	close(vmp->kvm_fd);
416 	TEST_ASSERT(ret == 0, "Close of /dev/kvm fd failed,\n"
417 		"  vmp->kvm_fd: %i rc: %i errno: %i", vmp->kvm_fd, ret, errno);
418 }
419 
420 /*
421  * Destroys and frees the VM pointed to by vmp.
422  */
423 void kvm_vm_free(struct kvm_vm *vmp)
424 {
425 	int ret;
426 
427 	if (vmp == NULL)
428 		return;
429 
430 	/* Free userspace_mem_regions. */
431 	while (vmp->userspace_mem_region_head) {
432 		struct userspace_mem_region *region
433 			= vmp->userspace_mem_region_head;
434 
435 		region->region.memory_size = 0;
436 		ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION,
437 			&region->region);
438 		TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed, "
439 			"rc: %i errno: %i", ret, errno);
440 
441 		vmp->userspace_mem_region_head = region->next;
442 		sparsebit_free(&region->unused_phy_pages);
443 		ret = munmap(region->mmap_start, region->mmap_size);
444 		TEST_ASSERT(ret == 0, "munmap failed, rc: %i errno: %i",
445 			    ret, errno);
446 
447 		free(region);
448 	}
449 
450 	/* Free sparsebit arrays. */
451 	sparsebit_free(&vmp->vpages_valid);
452 	sparsebit_free(&vmp->vpages_mapped);
453 
454 	kvm_vm_release(vmp);
455 
456 	/* Free the structure describing the VM. */
457 	free(vmp);
458 }
459 
460 /*
461  * Memory Compare, host virtual to guest virtual
462  *
463  * Input Args:
464  *   hva - Starting host virtual address
465  *   vm - Virtual Machine
466  *   gva - Starting guest virtual address
467  *   len - number of bytes to compare
468  *
469  * Output Args: None
470  *
471  * Input/Output Args: None
472  *
473  * Return:
474  *   Returns 0 if the bytes starting at hva for a length of len
475  *   are equal the guest virtual bytes starting at gva.  Returns
476  *   a value < 0, if bytes at hva are less than those at gva.
477  *   Otherwise a value > 0 is returned.
478  *
479  * Compares the bytes starting at the host virtual address hva, for
480  * a length of len, to the guest bytes starting at the guest virtual
481  * address given by gva.
482  */
483 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
484 {
485 	size_t amt;
486 
487 	/*
488 	 * Compare a batch of bytes until either a match is found
489 	 * or all the bytes have been compared.
490 	 */
491 	for (uintptr_t offset = 0; offset < len; offset += amt) {
492 		uintptr_t ptr1 = (uintptr_t)hva + offset;
493 
494 		/*
495 		 * Determine host address for guest virtual address
496 		 * at offset.
497 		 */
498 		uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
499 
500 		/*
501 		 * Determine amount to compare on this pass.
502 		 * Don't allow the comparsion to cross a page boundary.
503 		 */
504 		amt = len - offset;
505 		if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
506 			amt = vm->page_size - (ptr1 % vm->page_size);
507 		if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
508 			amt = vm->page_size - (ptr2 % vm->page_size);
509 
510 		assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
511 		assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
512 
513 		/*
514 		 * Perform the comparison.  If there is a difference
515 		 * return that result to the caller, otherwise need
516 		 * to continue on looking for a mismatch.
517 		 */
518 		int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
519 		if (ret != 0)
520 			return ret;
521 	}
522 
523 	/*
524 	 * No mismatch found.  Let the caller know the two memory
525 	 * areas are equal.
526 	 */
527 	return 0;
528 }
529 
530 /*
531  * VM Userspace Memory Region Add
532  *
533  * Input Args:
534  *   vm - Virtual Machine
535  *   backing_src - Storage source for this region.
536  *                 NULL to use anonymous memory.
537  *   guest_paddr - Starting guest physical address
538  *   slot - KVM region slot
539  *   npages - Number of physical pages
540  *   flags - KVM memory region flags (e.g. KVM_MEM_LOG_DIRTY_PAGES)
541  *
542  * Output Args: None
543  *
544  * Return: None
545  *
546  * Allocates a memory area of the number of pages specified by npages
547  * and maps it to the VM specified by vm, at a starting physical address
548  * given by guest_paddr.  The region is created with a KVM region slot
549  * given by slot, which must be unique and < KVM_MEM_SLOTS_NUM.  The
550  * region is created with the flags given by flags.
551  */
552 void vm_userspace_mem_region_add(struct kvm_vm *vm,
553 	enum vm_mem_backing_src_type src_type,
554 	uint64_t guest_paddr, uint32_t slot, uint64_t npages,
555 	uint32_t flags)
556 {
557 	int ret;
558 	struct userspace_mem_region *region;
559 	size_t huge_page_size = KVM_UTIL_PGS_PER_HUGEPG * vm->page_size;
560 
561 	TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
562 		"address not on a page boundary.\n"
563 		"  guest_paddr: 0x%lx vm->page_size: 0x%x",
564 		guest_paddr, vm->page_size);
565 	TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
566 		<= vm->max_gfn, "Physical range beyond maximum "
567 		"supported physical address,\n"
568 		"  guest_paddr: 0x%lx npages: 0x%lx\n"
569 		"  vm->max_gfn: 0x%lx vm->page_size: 0x%x",
570 		guest_paddr, npages, vm->max_gfn, vm->page_size);
571 
572 	/*
573 	 * Confirm a mem region with an overlapping address doesn't
574 	 * already exist.
575 	 */
576 	region = (struct userspace_mem_region *) userspace_mem_region_find(
577 		vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
578 	if (region != NULL)
579 		TEST_ASSERT(false, "overlapping userspace_mem_region already "
580 			"exists\n"
581 			"  requested guest_paddr: 0x%lx npages: 0x%lx "
582 			"page_size: 0x%x\n"
583 			"  existing guest_paddr: 0x%lx size: 0x%lx",
584 			guest_paddr, npages, vm->page_size,
585 			(uint64_t) region->region.guest_phys_addr,
586 			(uint64_t) region->region.memory_size);
587 
588 	/* Confirm no region with the requested slot already exists. */
589 	for (region = vm->userspace_mem_region_head; region;
590 		region = region->next) {
591 		if (region->region.slot == slot)
592 			break;
593 	}
594 	if (region != NULL)
595 		TEST_ASSERT(false, "A mem region with the requested slot "
596 			"already exists.\n"
597 			"  requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
598 			"  existing slot: %u paddr: 0x%lx size: 0x%lx",
599 			slot, guest_paddr, npages,
600 			region->region.slot,
601 			(uint64_t) region->region.guest_phys_addr,
602 			(uint64_t) region->region.memory_size);
603 
604 	/* Allocate and initialize new mem region structure. */
605 	region = calloc(1, sizeof(*region));
606 	TEST_ASSERT(region != NULL, "Insufficient Memory");
607 	region->mmap_size = npages * vm->page_size;
608 
609 	/* Enough memory to align up to a huge page. */
610 	if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
611 		region->mmap_size += huge_page_size;
612 	region->mmap_start = mmap(NULL, region->mmap_size,
613 				  PROT_READ | PROT_WRITE,
614 				  MAP_PRIVATE | MAP_ANONYMOUS
615 				  | (src_type == VM_MEM_SRC_ANONYMOUS_HUGETLB ? MAP_HUGETLB : 0),
616 				  -1, 0);
617 	TEST_ASSERT(region->mmap_start != MAP_FAILED,
618 		    "test_malloc failed, mmap_start: %p errno: %i",
619 		    region->mmap_start, errno);
620 
621 	/* Align THP allocation up to start of a huge page. */
622 	region->host_mem = align(region->mmap_start,
623 				 src_type == VM_MEM_SRC_ANONYMOUS_THP ?  huge_page_size : 1);
624 
625 	/* As needed perform madvise */
626 	if (src_type == VM_MEM_SRC_ANONYMOUS || src_type == VM_MEM_SRC_ANONYMOUS_THP) {
627 		ret = madvise(region->host_mem, npages * vm->page_size,
628 			     src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
629 		TEST_ASSERT(ret == 0, "madvise failed,\n"
630 			    "  addr: %p\n"
631 			    "  length: 0x%lx\n"
632 			    "  src_type: %x",
633 			    region->host_mem, npages * vm->page_size, src_type);
634 	}
635 
636 	region->unused_phy_pages = sparsebit_alloc();
637 	sparsebit_set_num(region->unused_phy_pages,
638 		guest_paddr >> vm->page_shift, npages);
639 	region->region.slot = slot;
640 	region->region.flags = flags;
641 	region->region.guest_phys_addr = guest_paddr;
642 	region->region.memory_size = npages * vm->page_size;
643 	region->region.userspace_addr = (uintptr_t) region->host_mem;
644 	ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
645 	TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
646 		"  rc: %i errno: %i\n"
647 		"  slot: %u flags: 0x%x\n"
648 		"  guest_phys_addr: 0x%lx size: 0x%lx",
649 		ret, errno, slot, flags,
650 		guest_paddr, (uint64_t) region->region.memory_size);
651 
652 	/* Add to linked-list of memory regions. */
653 	if (vm->userspace_mem_region_head)
654 		vm->userspace_mem_region_head->prev = region;
655 	region->next = vm->userspace_mem_region_head;
656 	vm->userspace_mem_region_head = region;
657 }
658 
659 /*
660  * Memslot to region
661  *
662  * Input Args:
663  *   vm - Virtual Machine
664  *   memslot - KVM memory slot ID
665  *
666  * Output Args: None
667  *
668  * Return:
669  *   Pointer to memory region structure that describe memory region
670  *   using kvm memory slot ID given by memslot.  TEST_ASSERT failure
671  *   on error (e.g. currently no memory region using memslot as a KVM
672  *   memory slot ID).
673  */
674 static struct userspace_mem_region *
675 memslot2region(struct kvm_vm *vm, uint32_t memslot)
676 {
677 	struct userspace_mem_region *region;
678 
679 	for (region = vm->userspace_mem_region_head; region;
680 		region = region->next) {
681 		if (region->region.slot == memslot)
682 			break;
683 	}
684 	if (region == NULL) {
685 		fprintf(stderr, "No mem region with the requested slot found,\n"
686 			"  requested slot: %u\n", memslot);
687 		fputs("---- vm dump ----\n", stderr);
688 		vm_dump(stderr, vm, 2);
689 		TEST_ASSERT(false, "Mem region not found");
690 	}
691 
692 	return region;
693 }
694 
695 /*
696  * VM Memory Region Flags Set
697  *
698  * Input Args:
699  *   vm - Virtual Machine
700  *   flags - Starting guest physical address
701  *
702  * Output Args: None
703  *
704  * Return: None
705  *
706  * Sets the flags of the memory region specified by the value of slot,
707  * to the values given by flags.
708  */
709 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
710 {
711 	int ret;
712 	struct userspace_mem_region *region;
713 
714 	region = memslot2region(vm, slot);
715 
716 	region->region.flags = flags;
717 
718 	ret = ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, &region->region);
719 
720 	TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION IOCTL failed,\n"
721 		"  rc: %i errno: %i slot: %u flags: 0x%x",
722 		ret, errno, slot, flags);
723 }
724 
725 /*
726  * VCPU mmap Size
727  *
728  * Input Args: None
729  *
730  * Output Args: None
731  *
732  * Return:
733  *   Size of VCPU state
734  *
735  * Returns the size of the structure pointed to by the return value
736  * of vcpu_state().
737  */
738 static int vcpu_mmap_sz(void)
739 {
740 	int dev_fd, ret;
741 
742 	dev_fd = open(KVM_DEV_PATH, O_RDONLY);
743 	if (dev_fd < 0)
744 		exit(KSFT_SKIP);
745 
746 	ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
747 	TEST_ASSERT(ret >= sizeof(struct kvm_run),
748 		"%s KVM_GET_VCPU_MMAP_SIZE ioctl failed, rc: %i errno: %i",
749 		__func__, ret, errno);
750 
751 	close(dev_fd);
752 
753 	return ret;
754 }
755 
756 /*
757  * VM VCPU Add
758  *
759  * Input Args:
760  *   vm - Virtual Machine
761  *   vcpuid - VCPU ID
762  *
763  * Output Args: None
764  *
765  * Return: None
766  *
767  * Creates and adds to the VM specified by vm and virtual CPU with
768  * the ID given by vcpuid.
769  */
770 void vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpuid, int pgd_memslot,
771 		 int gdt_memslot)
772 {
773 	struct vcpu *vcpu;
774 
775 	/* Confirm a vcpu with the specified id doesn't already exist. */
776 	vcpu = vcpu_find(vm, vcpuid);
777 	if (vcpu != NULL)
778 		TEST_ASSERT(false, "vcpu with the specified id "
779 			"already exists,\n"
780 			"  requested vcpuid: %u\n"
781 			"  existing vcpuid: %u state: %p",
782 			vcpuid, vcpu->id, vcpu->state);
783 
784 	/* Allocate and initialize new vcpu structure. */
785 	vcpu = calloc(1, sizeof(*vcpu));
786 	TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
787 	vcpu->id = vcpuid;
788 	vcpu->fd = ioctl(vm->fd, KVM_CREATE_VCPU, vcpuid);
789 	TEST_ASSERT(vcpu->fd >= 0, "KVM_CREATE_VCPU failed, rc: %i errno: %i",
790 		vcpu->fd, errno);
791 
792 	TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->state), "vcpu mmap size "
793 		"smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
794 		vcpu_mmap_sz(), sizeof(*vcpu->state));
795 	vcpu->state = (struct kvm_run *) mmap(NULL, sizeof(*vcpu->state),
796 		PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
797 	TEST_ASSERT(vcpu->state != MAP_FAILED, "mmap vcpu_state failed, "
798 		"vcpu id: %u errno: %i", vcpuid, errno);
799 
800 	/* Add to linked-list of VCPUs. */
801 	if (vm->vcpu_head)
802 		vm->vcpu_head->prev = vcpu;
803 	vcpu->next = vm->vcpu_head;
804 	vm->vcpu_head = vcpu;
805 
806 	vcpu_setup(vm, vcpuid, pgd_memslot, gdt_memslot);
807 }
808 
809 /*
810  * VM Virtual Address Unused Gap
811  *
812  * Input Args:
813  *   vm - Virtual Machine
814  *   sz - Size (bytes)
815  *   vaddr_min - Minimum Virtual Address
816  *
817  * Output Args: None
818  *
819  * Return:
820  *   Lowest virtual address at or below vaddr_min, with at least
821  *   sz unused bytes.  TEST_ASSERT failure if no area of at least
822  *   size sz is available.
823  *
824  * Within the VM specified by vm, locates the lowest starting virtual
825  * address >= vaddr_min, that has at least sz unallocated bytes.  A
826  * TEST_ASSERT failure occurs for invalid input or no area of at least
827  * sz unallocated bytes >= vaddr_min is available.
828  */
829 static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
830 				      vm_vaddr_t vaddr_min)
831 {
832 	uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
833 
834 	/* Determine lowest permitted virtual page index. */
835 	uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
836 	if ((pgidx_start * vm->page_size) < vaddr_min)
837 		goto no_va_found;
838 
839 	/* Loop over section with enough valid virtual page indexes. */
840 	if (!sparsebit_is_set_num(vm->vpages_valid,
841 		pgidx_start, pages))
842 		pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
843 			pgidx_start, pages);
844 	do {
845 		/*
846 		 * Are there enough unused virtual pages available at
847 		 * the currently proposed starting virtual page index.
848 		 * If not, adjust proposed starting index to next
849 		 * possible.
850 		 */
851 		if (sparsebit_is_clear_num(vm->vpages_mapped,
852 			pgidx_start, pages))
853 			goto va_found;
854 		pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
855 			pgidx_start, pages);
856 		if (pgidx_start == 0)
857 			goto no_va_found;
858 
859 		/*
860 		 * If needed, adjust proposed starting virtual address,
861 		 * to next range of valid virtual addresses.
862 		 */
863 		if (!sparsebit_is_set_num(vm->vpages_valid,
864 			pgidx_start, pages)) {
865 			pgidx_start = sparsebit_next_set_num(
866 				vm->vpages_valid, pgidx_start, pages);
867 			if (pgidx_start == 0)
868 				goto no_va_found;
869 		}
870 	} while (pgidx_start != 0);
871 
872 no_va_found:
873 	TEST_ASSERT(false, "No vaddr of specified pages available, "
874 		"pages: 0x%lx", pages);
875 
876 	/* NOT REACHED */
877 	return -1;
878 
879 va_found:
880 	TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
881 		pgidx_start, pages),
882 		"Unexpected, invalid virtual page index range,\n"
883 		"  pgidx_start: 0x%lx\n"
884 		"  pages: 0x%lx",
885 		pgidx_start, pages);
886 	TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
887 		pgidx_start, pages),
888 		"Unexpected, pages already mapped,\n"
889 		"  pgidx_start: 0x%lx\n"
890 		"  pages: 0x%lx",
891 		pgidx_start, pages);
892 
893 	return pgidx_start * vm->page_size;
894 }
895 
896 /*
897  * VM Virtual Address Allocate
898  *
899  * Input Args:
900  *   vm - Virtual Machine
901  *   sz - Size in bytes
902  *   vaddr_min - Minimum starting virtual address
903  *   data_memslot - Memory region slot for data pages
904  *   pgd_memslot - Memory region slot for new virtual translation tables
905  *
906  * Output Args: None
907  *
908  * Return:
909  *   Starting guest virtual address
910  *
911  * Allocates at least sz bytes within the virtual address space of the vm
912  * given by vm.  The allocated bytes are mapped to a virtual address >=
913  * the address given by vaddr_min.  Note that each allocation uses a
914  * a unique set of pages, with the minimum real allocation being at least
915  * a page.
916  */
917 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
918 			  uint32_t data_memslot, uint32_t pgd_memslot)
919 {
920 	uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
921 
922 	virt_pgd_alloc(vm, pgd_memslot);
923 
924 	/*
925 	 * Find an unused range of virtual page addresses of at least
926 	 * pages in length.
927 	 */
928 	vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
929 
930 	/* Map the virtual pages. */
931 	for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
932 		pages--, vaddr += vm->page_size) {
933 		vm_paddr_t paddr;
934 
935 		paddr = vm_phy_page_alloc(vm,
936 				KVM_UTIL_MIN_PFN * vm->page_size, data_memslot);
937 
938 		virt_pg_map(vm, vaddr, paddr, pgd_memslot);
939 
940 		sparsebit_set(vm->vpages_mapped,
941 			vaddr >> vm->page_shift);
942 	}
943 
944 	return vaddr_start;
945 }
946 
947 /*
948  * Map a range of VM virtual address to the VM's physical address
949  *
950  * Input Args:
951  *   vm - Virtual Machine
952  *   vaddr - Virtuall address to map
953  *   paddr - VM Physical Address
954  *   size - The size of the range to map
955  *   pgd_memslot - Memory region slot for new virtual translation tables
956  *
957  * Output Args: None
958  *
959  * Return: None
960  *
961  * Within the VM given by vm, creates a virtual translation for the
962  * page range starting at vaddr to the page range starting at paddr.
963  */
964 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
965 	      size_t size, uint32_t pgd_memslot)
966 {
967 	size_t page_size = vm->page_size;
968 	size_t npages = size / page_size;
969 
970 	TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
971 	TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
972 
973 	while (npages--) {
974 		virt_pg_map(vm, vaddr, paddr, pgd_memslot);
975 		vaddr += page_size;
976 		paddr += page_size;
977 	}
978 }
979 
980 /*
981  * Address VM Physical to Host Virtual
982  *
983  * Input Args:
984  *   vm - Virtual Machine
985  *   gpa - VM physical address
986  *
987  * Output Args: None
988  *
989  * Return:
990  *   Equivalent host virtual address
991  *
992  * Locates the memory region containing the VM physical address given
993  * by gpa, within the VM given by vm.  When found, the host virtual
994  * address providing the memory to the vm physical address is returned.
995  * A TEST_ASSERT failure occurs if no region containing gpa exists.
996  */
997 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
998 {
999 	struct userspace_mem_region *region;
1000 	for (region = vm->userspace_mem_region_head; region;
1001 	     region = region->next) {
1002 		if ((gpa >= region->region.guest_phys_addr)
1003 			&& (gpa <= (region->region.guest_phys_addr
1004 				+ region->region.memory_size - 1)))
1005 			return (void *) ((uintptr_t) region->host_mem
1006 				+ (gpa - region->region.guest_phys_addr));
1007 	}
1008 
1009 	TEST_ASSERT(false, "No vm physical memory at 0x%lx", gpa);
1010 	return NULL;
1011 }
1012 
1013 /*
1014  * Address Host Virtual to VM Physical
1015  *
1016  * Input Args:
1017  *   vm - Virtual Machine
1018  *   hva - Host virtual address
1019  *
1020  * Output Args: None
1021  *
1022  * Return:
1023  *   Equivalent VM physical address
1024  *
1025  * Locates the memory region containing the host virtual address given
1026  * by hva, within the VM given by vm.  When found, the equivalent
1027  * VM physical address is returned. A TEST_ASSERT failure occurs if no
1028  * region containing hva exists.
1029  */
1030 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1031 {
1032 	struct userspace_mem_region *region;
1033 	for (region = vm->userspace_mem_region_head; region;
1034 	     region = region->next) {
1035 		if ((hva >= region->host_mem)
1036 			&& (hva <= (region->host_mem
1037 				+ region->region.memory_size - 1)))
1038 			return (vm_paddr_t) ((uintptr_t)
1039 				region->region.guest_phys_addr
1040 				+ (hva - (uintptr_t) region->host_mem));
1041 	}
1042 
1043 	TEST_ASSERT(false, "No mapping to a guest physical address, "
1044 		"hva: %p", hva);
1045 	return -1;
1046 }
1047 
1048 /*
1049  * VM Create IRQ Chip
1050  *
1051  * Input Args:
1052  *   vm - Virtual Machine
1053  *
1054  * Output Args: None
1055  *
1056  * Return: None
1057  *
1058  * Creates an interrupt controller chip for the VM specified by vm.
1059  */
1060 void vm_create_irqchip(struct kvm_vm *vm)
1061 {
1062 	int ret;
1063 
1064 	ret = ioctl(vm->fd, KVM_CREATE_IRQCHIP, 0);
1065 	TEST_ASSERT(ret == 0, "KVM_CREATE_IRQCHIP IOCTL failed, "
1066 		"rc: %i errno: %i", ret, errno);
1067 
1068 	vm->has_irqchip = true;
1069 }
1070 
1071 /*
1072  * VM VCPU State
1073  *
1074  * Input Args:
1075  *   vm - Virtual Machine
1076  *   vcpuid - VCPU ID
1077  *
1078  * Output Args: None
1079  *
1080  * Return:
1081  *   Pointer to structure that describes the state of the VCPU.
1082  *
1083  * Locates and returns a pointer to a structure that describes the
1084  * state of the VCPU with the given vcpuid.
1085  */
1086 struct kvm_run *vcpu_state(struct kvm_vm *vm, uint32_t vcpuid)
1087 {
1088 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1089 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1090 
1091 	return vcpu->state;
1092 }
1093 
1094 /*
1095  * VM VCPU Run
1096  *
1097  * Input Args:
1098  *   vm - Virtual Machine
1099  *   vcpuid - VCPU ID
1100  *
1101  * Output Args: None
1102  *
1103  * Return: None
1104  *
1105  * Switch to executing the code for the VCPU given by vcpuid, within the VM
1106  * given by vm.
1107  */
1108 void vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1109 {
1110 	int ret = _vcpu_run(vm, vcpuid);
1111 	TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1112 		"rc: %i errno: %i", ret, errno);
1113 }
1114 
1115 int _vcpu_run(struct kvm_vm *vm, uint32_t vcpuid)
1116 {
1117 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1118 	int rc;
1119 
1120 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1121 	do {
1122 		rc = ioctl(vcpu->fd, KVM_RUN, NULL);
1123 	} while (rc == -1 && errno == EINTR);
1124 	return rc;
1125 }
1126 
1127 void vcpu_run_complete_io(struct kvm_vm *vm, uint32_t vcpuid)
1128 {
1129 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1130 	int ret;
1131 
1132 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1133 
1134 	vcpu->state->immediate_exit = 1;
1135 	ret = ioctl(vcpu->fd, KVM_RUN, NULL);
1136 	vcpu->state->immediate_exit = 0;
1137 
1138 	TEST_ASSERT(ret == -1 && errno == EINTR,
1139 		    "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1140 		    ret, errno);
1141 }
1142 
1143 /*
1144  * VM VCPU Set MP State
1145  *
1146  * Input Args:
1147  *   vm - Virtual Machine
1148  *   vcpuid - VCPU ID
1149  *   mp_state - mp_state to be set
1150  *
1151  * Output Args: None
1152  *
1153  * Return: None
1154  *
1155  * Sets the MP state of the VCPU given by vcpuid, to the state given
1156  * by mp_state.
1157  */
1158 void vcpu_set_mp_state(struct kvm_vm *vm, uint32_t vcpuid,
1159 		       struct kvm_mp_state *mp_state)
1160 {
1161 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1162 	int ret;
1163 
1164 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1165 
1166 	ret = ioctl(vcpu->fd, KVM_SET_MP_STATE, mp_state);
1167 	TEST_ASSERT(ret == 0, "KVM_SET_MP_STATE IOCTL failed, "
1168 		"rc: %i errno: %i", ret, errno);
1169 }
1170 
1171 /*
1172  * VM VCPU Regs Get
1173  *
1174  * Input Args:
1175  *   vm - Virtual Machine
1176  *   vcpuid - VCPU ID
1177  *
1178  * Output Args:
1179  *   regs - current state of VCPU regs
1180  *
1181  * Return: None
1182  *
1183  * Obtains the current register state for the VCPU specified by vcpuid
1184  * and stores it at the location given by regs.
1185  */
1186 void vcpu_regs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1187 {
1188 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1189 	int ret;
1190 
1191 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1192 
1193 	ret = ioctl(vcpu->fd, KVM_GET_REGS, regs);
1194 	TEST_ASSERT(ret == 0, "KVM_GET_REGS failed, rc: %i errno: %i",
1195 		ret, errno);
1196 }
1197 
1198 /*
1199  * VM VCPU Regs Set
1200  *
1201  * Input Args:
1202  *   vm - Virtual Machine
1203  *   vcpuid - VCPU ID
1204  *   regs - Values to set VCPU regs to
1205  *
1206  * Output Args: None
1207  *
1208  * Return: None
1209  *
1210  * Sets the regs of the VCPU specified by vcpuid to the values
1211  * given by regs.
1212  */
1213 void vcpu_regs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_regs *regs)
1214 {
1215 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1216 	int ret;
1217 
1218 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1219 
1220 	ret = ioctl(vcpu->fd, KVM_SET_REGS, regs);
1221 	TEST_ASSERT(ret == 0, "KVM_SET_REGS failed, rc: %i errno: %i",
1222 		ret, errno);
1223 }
1224 
1225 void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
1226 		     struct kvm_vcpu_events *events)
1227 {
1228 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1229 	int ret;
1230 
1231 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1232 
1233 	ret = ioctl(vcpu->fd, KVM_GET_VCPU_EVENTS, events);
1234 	TEST_ASSERT(ret == 0, "KVM_GET_VCPU_EVENTS, failed, rc: %i errno: %i",
1235 		ret, errno);
1236 }
1237 
1238 void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1239 		     struct kvm_vcpu_events *events)
1240 {
1241 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1242 	int ret;
1243 
1244 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1245 
1246 	ret = ioctl(vcpu->fd, KVM_SET_VCPU_EVENTS, events);
1247 	TEST_ASSERT(ret == 0, "KVM_SET_VCPU_EVENTS, failed, rc: %i errno: %i",
1248 		ret, errno);
1249 }
1250 
1251 #ifdef __x86_64__
1252 void vcpu_nested_state_get(struct kvm_vm *vm, uint32_t vcpuid,
1253 			   struct kvm_nested_state *state)
1254 {
1255 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1256 	int ret;
1257 
1258 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1259 
1260 	ret = ioctl(vcpu->fd, KVM_GET_NESTED_STATE, state);
1261 	TEST_ASSERT(ret == 0,
1262 		"KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1263 		ret, errno);
1264 }
1265 
1266 int vcpu_nested_state_set(struct kvm_vm *vm, uint32_t vcpuid,
1267 			  struct kvm_nested_state *state, bool ignore_error)
1268 {
1269 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1270 	int ret;
1271 
1272 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1273 
1274 	ret = ioctl(vcpu->fd, KVM_SET_NESTED_STATE, state);
1275 	if (!ignore_error) {
1276 		TEST_ASSERT(ret == 0,
1277 			"KVM_SET_NESTED_STATE failed, ret: %i errno: %i",
1278 			ret, errno);
1279 	}
1280 
1281 	return ret;
1282 }
1283 #endif
1284 
1285 /*
1286  * VM VCPU System Regs Get
1287  *
1288  * Input Args:
1289  *   vm - Virtual Machine
1290  *   vcpuid - VCPU ID
1291  *
1292  * Output Args:
1293  *   sregs - current state of VCPU system regs
1294  *
1295  * Return: None
1296  *
1297  * Obtains the current system register state for the VCPU specified by
1298  * vcpuid and stores it at the location given by sregs.
1299  */
1300 void vcpu_sregs_get(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1301 {
1302 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1303 	int ret;
1304 
1305 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1306 
1307 	ret = ioctl(vcpu->fd, KVM_GET_SREGS, sregs);
1308 	TEST_ASSERT(ret == 0, "KVM_GET_SREGS failed, rc: %i errno: %i",
1309 		ret, errno);
1310 }
1311 
1312 /*
1313  * VM VCPU System Regs Set
1314  *
1315  * Input Args:
1316  *   vm - Virtual Machine
1317  *   vcpuid - VCPU ID
1318  *   sregs - Values to set VCPU system regs to
1319  *
1320  * Output Args: None
1321  *
1322  * Return: None
1323  *
1324  * Sets the system regs of the VCPU specified by vcpuid to the values
1325  * given by sregs.
1326  */
1327 void vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1328 {
1329 	int ret = _vcpu_sregs_set(vm, vcpuid, sregs);
1330 	TEST_ASSERT(ret == 0, "KVM_RUN IOCTL failed, "
1331 		"rc: %i errno: %i", ret, errno);
1332 }
1333 
1334 int _vcpu_sregs_set(struct kvm_vm *vm, uint32_t vcpuid, struct kvm_sregs *sregs)
1335 {
1336 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1337 
1338 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1339 
1340 	return ioctl(vcpu->fd, KVM_SET_SREGS, sregs);
1341 }
1342 
1343 /*
1344  * VCPU Ioctl
1345  *
1346  * Input Args:
1347  *   vm - Virtual Machine
1348  *   vcpuid - VCPU ID
1349  *   cmd - Ioctl number
1350  *   arg - Argument to pass to the ioctl
1351  *
1352  * Return: None
1353  *
1354  * Issues an arbitrary ioctl on a VCPU fd.
1355  */
1356 void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1357 		unsigned long cmd, void *arg)
1358 {
1359 	int ret;
1360 
1361 	ret = _vcpu_ioctl(vm, vcpuid, cmd, arg);
1362 	TEST_ASSERT(ret == 0, "vcpu ioctl %lu failed, rc: %i errno: %i (%s)",
1363 		cmd, ret, errno, strerror(errno));
1364 }
1365 
1366 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid,
1367 		unsigned long cmd, void *arg)
1368 {
1369 	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1370 	int ret;
1371 
1372 	TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1373 
1374 	ret = ioctl(vcpu->fd, cmd, arg);
1375 
1376 	return ret;
1377 }
1378 
1379 /*
1380  * VM Ioctl
1381  *
1382  * Input Args:
1383  *   vm - Virtual Machine
1384  *   cmd - Ioctl number
1385  *   arg - Argument to pass to the ioctl
1386  *
1387  * Return: None
1388  *
1389  * Issues an arbitrary ioctl on a VM fd.
1390  */
1391 void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
1392 {
1393 	int ret;
1394 
1395 	ret = ioctl(vm->fd, cmd, arg);
1396 	TEST_ASSERT(ret == 0, "vm ioctl %lu failed, rc: %i errno: %i (%s)",
1397 		cmd, ret, errno, strerror(errno));
1398 }
1399 
1400 /*
1401  * VM Dump
1402  *
1403  * Input Args:
1404  *   vm - Virtual Machine
1405  *   indent - Left margin indent amount
1406  *
1407  * Output Args:
1408  *   stream - Output FILE stream
1409  *
1410  * Return: None
1411  *
1412  * Dumps the current state of the VM given by vm, to the FILE stream
1413  * given by stream.
1414  */
1415 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1416 {
1417 	struct userspace_mem_region *region;
1418 	struct vcpu *vcpu;
1419 
1420 	fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1421 	fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1422 	fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1423 	fprintf(stream, "%*sMem Regions:\n", indent, "");
1424 	for (region = vm->userspace_mem_region_head; region;
1425 		region = region->next) {
1426 		fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1427 			"host_virt: %p\n", indent + 2, "",
1428 			(uint64_t) region->region.guest_phys_addr,
1429 			(uint64_t) region->region.memory_size,
1430 			region->host_mem);
1431 		fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1432 		sparsebit_dump(stream, region->unused_phy_pages, 0);
1433 	}
1434 	fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1435 	sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1436 	fprintf(stream, "%*spgd_created: %u\n", indent, "",
1437 		vm->pgd_created);
1438 	if (vm->pgd_created) {
1439 		fprintf(stream, "%*sVirtual Translation Tables:\n",
1440 			indent + 2, "");
1441 		virt_dump(stream, vm, indent + 4);
1442 	}
1443 	fprintf(stream, "%*sVCPUs:\n", indent, "");
1444 	for (vcpu = vm->vcpu_head; vcpu; vcpu = vcpu->next)
1445 		vcpu_dump(stream, vm, vcpu->id, indent + 2);
1446 }
1447 
1448 /* Known KVM exit reasons */
1449 static struct exit_reason {
1450 	unsigned int reason;
1451 	const char *name;
1452 } exit_reasons_known[] = {
1453 	{KVM_EXIT_UNKNOWN, "UNKNOWN"},
1454 	{KVM_EXIT_EXCEPTION, "EXCEPTION"},
1455 	{KVM_EXIT_IO, "IO"},
1456 	{KVM_EXIT_HYPERCALL, "HYPERCALL"},
1457 	{KVM_EXIT_DEBUG, "DEBUG"},
1458 	{KVM_EXIT_HLT, "HLT"},
1459 	{KVM_EXIT_MMIO, "MMIO"},
1460 	{KVM_EXIT_IRQ_WINDOW_OPEN, "IRQ_WINDOW_OPEN"},
1461 	{KVM_EXIT_SHUTDOWN, "SHUTDOWN"},
1462 	{KVM_EXIT_FAIL_ENTRY, "FAIL_ENTRY"},
1463 	{KVM_EXIT_INTR, "INTR"},
1464 	{KVM_EXIT_SET_TPR, "SET_TPR"},
1465 	{KVM_EXIT_TPR_ACCESS, "TPR_ACCESS"},
1466 	{KVM_EXIT_S390_SIEIC, "S390_SIEIC"},
1467 	{KVM_EXIT_S390_RESET, "S390_RESET"},
1468 	{KVM_EXIT_DCR, "DCR"},
1469 	{KVM_EXIT_NMI, "NMI"},
1470 	{KVM_EXIT_INTERNAL_ERROR, "INTERNAL_ERROR"},
1471 	{KVM_EXIT_OSI, "OSI"},
1472 	{KVM_EXIT_PAPR_HCALL, "PAPR_HCALL"},
1473 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1474 	{KVM_EXIT_MEMORY_NOT_PRESENT, "MEMORY_NOT_PRESENT"},
1475 #endif
1476 };
1477 
1478 /*
1479  * Exit Reason String
1480  *
1481  * Input Args:
1482  *   exit_reason - Exit reason
1483  *
1484  * Output Args: None
1485  *
1486  * Return:
1487  *   Constant string pointer describing the exit reason.
1488  *
1489  * Locates and returns a constant string that describes the KVM exit
1490  * reason given by exit_reason.  If no such string is found, a constant
1491  * string of "Unknown" is returned.
1492  */
1493 const char *exit_reason_str(unsigned int exit_reason)
1494 {
1495 	unsigned int n1;
1496 
1497 	for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1498 		if (exit_reason == exit_reasons_known[n1].reason)
1499 			return exit_reasons_known[n1].name;
1500 	}
1501 
1502 	return "Unknown";
1503 }
1504 
1505 /*
1506  * Physical Contiguous Page Allocator
1507  *
1508  * Input Args:
1509  *   vm - Virtual Machine
1510  *   num - number of pages
1511  *   paddr_min - Physical address minimum
1512  *   memslot - Memory region to allocate page from
1513  *
1514  * Output Args: None
1515  *
1516  * Return:
1517  *   Starting physical address
1518  *
1519  * Within the VM specified by vm, locates a range of available physical
1520  * pages at or above paddr_min. If found, the pages are marked as in use
1521  * and their base address is returned. A TEST_ASSERT failure occurs if
1522  * not enough pages are available at or above paddr_min.
1523  */
1524 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1525 			      vm_paddr_t paddr_min, uint32_t memslot)
1526 {
1527 	struct userspace_mem_region *region;
1528 	sparsebit_idx_t pg, base;
1529 
1530 	TEST_ASSERT(num > 0, "Must allocate at least one page");
1531 
1532 	TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1533 		"not divisible by page size.\n"
1534 		"  paddr_min: 0x%lx page_size: 0x%x",
1535 		paddr_min, vm->page_size);
1536 
1537 	region = memslot2region(vm, memslot);
1538 	base = pg = paddr_min >> vm->page_shift;
1539 
1540 	do {
1541 		for (; pg < base + num; ++pg) {
1542 			if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
1543 				base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
1544 				break;
1545 			}
1546 		}
1547 	} while (pg && pg != base + num);
1548 
1549 	if (pg == 0) {
1550 		fprintf(stderr, "No guest physical page available, "
1551 			"paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
1552 			paddr_min, vm->page_size, memslot);
1553 		fputs("---- vm dump ----\n", stderr);
1554 		vm_dump(stderr, vm, 2);
1555 		abort();
1556 	}
1557 
1558 	for (pg = base; pg < base + num; ++pg)
1559 		sparsebit_clear(region->unused_phy_pages, pg);
1560 
1561 	return base * vm->page_size;
1562 }
1563 
1564 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
1565 			     uint32_t memslot)
1566 {
1567 	return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
1568 }
1569 
1570 /*
1571  * Address Guest Virtual to Host Virtual
1572  *
1573  * Input Args:
1574  *   vm - Virtual Machine
1575  *   gva - VM virtual address
1576  *
1577  * Output Args: None
1578  *
1579  * Return:
1580  *   Equivalent host virtual address
1581  */
1582 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
1583 {
1584 	return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
1585 }
1586