1 // SPDX-License-Identifier: GPL-2.0-only
3 * tools/testing/selftests/kvm/lib/kvm_util.c
5 * Copyright (C) 2018, Google LLC.
8 #define _GNU_SOURCE /* for program_invocation_name */
11 #include "processor.h"
16 #include <sys/types.h>
19 #include <linux/kernel.h>
21 #define KVM_UTIL_MIN_PFN 2
23 static int vcpu_mmap_sz(void);
25 int open_path_or_exit(const char *path, int flags)
29 fd = open(path, flags);
30 __TEST_REQUIRE(fd >= 0, "%s not available (errno: %d)", path, errno);
36 * Open KVM_DEV_PATH if available, otherwise exit the entire program.
39 * flags - The flags to pass when opening KVM_DEV_PATH.
42 * The opened file descriptor of /dev/kvm.
44 static int _open_kvm_dev_path_or_exit(int flags)
46 return open_path_or_exit(KVM_DEV_PATH, flags);
49 int open_kvm_dev_path_or_exit(void)
51 return _open_kvm_dev_path_or_exit(O_RDONLY);
54 static bool get_module_param_bool(const char *module_name, const char *param)
56 const int path_size = 128;
62 r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
64 TEST_ASSERT(r < path_size,
65 "Failed to construct sysfs path in %d bytes.", path_size);
67 fd = open_path_or_exit(path, O_RDONLY);
69 r = read(fd, &value, 1);
70 TEST_ASSERT(r == 1, "read(%s) failed", path);
73 TEST_ASSERT(!r, "close(%s) failed", path);
77 else if (value == 'N')
80 TEST_FAIL("Unrecognized value '%c' for boolean module param", value);
83 bool get_kvm_param_bool(const char *param)
85 return get_module_param_bool("kvm", param);
88 bool get_kvm_intel_param_bool(const char *param)
90 return get_module_param_bool("kvm_intel", param);
93 bool get_kvm_amd_param_bool(const char *param)
95 return get_module_param_bool("kvm_amd", param);
107 * On success, the Value corresponding to the capability (KVM_CAP_*)
108 * specified by the value of cap. On failure a TEST_ASSERT failure
111 * Looks up and returns the value corresponding to the capability
112 * (KVM_CAP_*) given by cap.
114 unsigned int kvm_check_cap(long cap)
119 kvm_fd = open_kvm_dev_path_or_exit();
120 ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap);
121 TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret));
125 return (unsigned int)ret;
128 void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size)
130 if (vm_check_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL))
131 vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL, ring_size);
133 vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING, ring_size);
134 vm->dirty_ring_size = ring_size;
137 static void vm_open(struct kvm_vm *vm)
139 vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR);
141 TEST_REQUIRE(kvm_has_cap(KVM_CAP_IMMEDIATE_EXIT));
143 vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
144 TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
147 const char *vm_guest_mode_string(uint32_t i)
149 static const char * const strings[] = {
150 [VM_MODE_P52V48_4K] = "PA-bits:52, VA-bits:48, 4K pages",
151 [VM_MODE_P52V48_16K] = "PA-bits:52, VA-bits:48, 16K pages",
152 [VM_MODE_P52V48_64K] = "PA-bits:52, VA-bits:48, 64K pages",
153 [VM_MODE_P48V48_4K] = "PA-bits:48, VA-bits:48, 4K pages",
154 [VM_MODE_P48V48_16K] = "PA-bits:48, VA-bits:48, 16K pages",
155 [VM_MODE_P48V48_64K] = "PA-bits:48, VA-bits:48, 64K pages",
156 [VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages",
157 [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages",
158 [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages",
159 [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages",
160 [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages",
161 [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages",
162 [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages",
163 [VM_MODE_P36V48_16K] = "PA-bits:36, VA-bits:48, 16K pages",
164 [VM_MODE_P36V48_64K] = "PA-bits:36, VA-bits:48, 64K pages",
165 [VM_MODE_P36V47_16K] = "PA-bits:36, VA-bits:47, 16K pages",
167 _Static_assert(sizeof(strings)/sizeof(char *) == NUM_VM_MODES,
168 "Missing new mode strings?");
170 TEST_ASSERT(i < NUM_VM_MODES, "Guest mode ID %d too big", i);
175 const struct vm_guest_mode_params vm_guest_mode_params[] = {
176 [VM_MODE_P52V48_4K] = { 52, 48, 0x1000, 12 },
177 [VM_MODE_P52V48_16K] = { 52, 48, 0x4000, 14 },
178 [VM_MODE_P52V48_64K] = { 52, 48, 0x10000, 16 },
179 [VM_MODE_P48V48_4K] = { 48, 48, 0x1000, 12 },
180 [VM_MODE_P48V48_16K] = { 48, 48, 0x4000, 14 },
181 [VM_MODE_P48V48_64K] = { 48, 48, 0x10000, 16 },
182 [VM_MODE_P40V48_4K] = { 40, 48, 0x1000, 12 },
183 [VM_MODE_P40V48_16K] = { 40, 48, 0x4000, 14 },
184 [VM_MODE_P40V48_64K] = { 40, 48, 0x10000, 16 },
185 [VM_MODE_PXXV48_4K] = { 0, 0, 0x1000, 12 },
186 [VM_MODE_P47V64_4K] = { 47, 64, 0x1000, 12 },
187 [VM_MODE_P44V64_4K] = { 44, 64, 0x1000, 12 },
188 [VM_MODE_P36V48_4K] = { 36, 48, 0x1000, 12 },
189 [VM_MODE_P36V48_16K] = { 36, 48, 0x4000, 14 },
190 [VM_MODE_P36V48_64K] = { 36, 48, 0x10000, 16 },
191 [VM_MODE_P36V47_16K] = { 36, 47, 0x4000, 14 },
193 _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
194 "Missing new mode params?");
197 * Initializes vm->vpages_valid to match the canonical VA space of the
200 * The default implementation is valid for architectures which split the
201 * range addressed by a single page table into a low and high region
202 * based on the MSB of the VA. On architectures with this behavior
203 * the VA region spans [0, 2^(va_bits - 1)), [-(2^(va_bits - 1), -1].
205 __weak void vm_vaddr_populate_bitmap(struct kvm_vm *vm)
207 sparsebit_set_num(vm->vpages_valid,
208 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
209 sparsebit_set_num(vm->vpages_valid,
210 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
211 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
214 struct kvm_vm *____vm_create(struct vm_shape shape)
218 vm = calloc(1, sizeof(*vm));
219 TEST_ASSERT(vm != NULL, "Insufficient Memory");
221 INIT_LIST_HEAD(&vm->vcpus);
222 vm->regions.gpa_tree = RB_ROOT;
223 vm->regions.hva_tree = RB_ROOT;
224 hash_init(vm->regions.slot_hash);
226 vm->mode = shape.mode;
227 vm->type = shape.type;
229 vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits;
230 vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;
231 vm->page_size = vm_guest_mode_params[vm->mode].page_size;
232 vm->page_shift = vm_guest_mode_params[vm->mode].page_shift;
234 /* Setup mode specific traits. */
236 case VM_MODE_P52V48_4K:
237 vm->pgtable_levels = 4;
239 case VM_MODE_P52V48_64K:
240 vm->pgtable_levels = 3;
242 case VM_MODE_P48V48_4K:
243 vm->pgtable_levels = 4;
245 case VM_MODE_P48V48_64K:
246 vm->pgtable_levels = 3;
248 case VM_MODE_P40V48_4K:
249 case VM_MODE_P36V48_4K:
250 vm->pgtable_levels = 4;
252 case VM_MODE_P40V48_64K:
253 case VM_MODE_P36V48_64K:
254 vm->pgtable_levels = 3;
256 case VM_MODE_P52V48_16K:
257 case VM_MODE_P48V48_16K:
258 case VM_MODE_P40V48_16K:
259 case VM_MODE_P36V48_16K:
260 vm->pgtable_levels = 4;
262 case VM_MODE_P36V47_16K:
263 vm->pgtable_levels = 3;
265 case VM_MODE_PXXV48_4K:
267 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
269 * Ignore KVM support for 5-level paging (vm->va_bits == 57),
270 * it doesn't take effect unless a CR4.LA57 is set, which it
271 * isn't for this mode (48-bit virtual address space).
273 TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57,
274 "Linear address width (%d bits) not supported",
276 pr_debug("Guest physical address width detected: %d\n",
278 vm->pgtable_levels = 4;
281 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
284 case VM_MODE_P47V64_4K:
285 vm->pgtable_levels = 5;
287 case VM_MODE_P44V64_4K:
288 vm->pgtable_levels = 5;
291 TEST_FAIL("Unknown guest mode: 0x%x", vm->mode);
295 TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
296 if (vm->pa_bits != 40)
297 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
302 /* Limit to VA-bit canonical virtual addresses. */
303 vm->vpages_valid = sparsebit_alloc();
304 vm_vaddr_populate_bitmap(vm);
306 /* Limit physical addresses to PA-bits. */
307 vm->max_gfn = vm_compute_max_gfn(vm);
309 /* Allocate and setup memory for guest. */
310 vm->vpages_mapped = sparsebit_alloc();
315 static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
316 uint32_t nr_runnable_vcpus,
317 uint64_t extra_mem_pages)
319 uint64_t page_size = vm_guest_mode_params[mode].page_size;
322 TEST_ASSERT(nr_runnable_vcpus,
323 "Use vm_create_barebones() for VMs that _never_ have vCPUs\n");
325 TEST_ASSERT(nr_runnable_vcpus <= kvm_check_cap(KVM_CAP_MAX_VCPUS),
326 "nr_vcpus = %d too large for host, max-vcpus = %d",
327 nr_runnable_vcpus, kvm_check_cap(KVM_CAP_MAX_VCPUS));
330 * Arbitrarily allocate 512 pages (2mb when page size is 4kb) for the
331 * test code and other per-VM assets that will be loaded into memslot0.
335 /* Account for the per-vCPU stacks on behalf of the test. */
336 nr_pages += nr_runnable_vcpus * DEFAULT_STACK_PGS;
339 * Account for the number of pages needed for the page tables. The
340 * maximum page table size for a memory region will be when the
341 * smallest page size is used. Considering each page contains x page
342 * table descriptors, the total extra size for page tables (for extra
343 * N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller
346 nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2;
348 /* Account for the number of pages needed by ucall. */
349 nr_pages += ucall_nr_pages_required(page_size);
351 return vm_adjust_num_guest_pages(mode, nr_pages);
354 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
355 uint64_t nr_extra_pages)
357 uint64_t nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus,
359 struct userspace_mem_region *slot0;
363 pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__,
364 vm_guest_mode_string(shape.mode), shape.type, nr_pages);
366 vm = ____vm_create(shape);
368 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0);
369 for (i = 0; i < NR_MEM_REGIONS; i++)
372 kvm_vm_elf_load(vm, program_invocation_name);
375 * TODO: Add proper defines to protect the library's memslots, and then
376 * carve out memslot1 for the ucall MMIO address. KVM treats writes to
377 * read-only memslots as MMIO, and creating a read-only memslot for the
378 * MMIO region would prevent silently clobbering the MMIO region.
380 slot0 = memslot2region(vm, 0);
381 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
383 kvm_arch_vm_post_create(vm);
389 * VM Create with customized parameters
392 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
393 * nr_vcpus - VCPU count
394 * extra_mem_pages - Non-slot0 physical memory total size
395 * guest_code - Guest entry point
401 * Pointer to opaque structure that describes the created VM.
403 * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K).
404 * extra_mem_pages is only used to calculate the maximum page table size,
405 * no real memory allocation for non-slot0 memory in this function.
407 struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus,
408 uint64_t extra_mem_pages,
409 void *guest_code, struct kvm_vcpu *vcpus[])
414 TEST_ASSERT(!nr_vcpus || vcpus, "Must provide vCPU array");
416 vm = __vm_create(shape, nr_vcpus, extra_mem_pages);
418 for (i = 0; i < nr_vcpus; ++i)
419 vcpus[i] = vm_vcpu_add(vm, i, guest_code);
424 struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape,
425 struct kvm_vcpu **vcpu,
426 uint64_t extra_mem_pages,
429 struct kvm_vcpu *vcpus[1];
432 vm = __vm_create_with_vcpus(shape, 1, extra_mem_pages, guest_code, vcpus);
442 * vm - VM that has been released before
446 * Reopens the file descriptors associated to the VM and reinstates the
447 * global state, such as the irqchip and the memory regions that are mapped
450 void kvm_vm_restart(struct kvm_vm *vmp)
453 struct userspace_mem_region *region;
456 if (vmp->has_irqchip)
457 vm_create_irqchip(vmp);
459 hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) {
460 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
462 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
463 " rc: %i errno: %i\n"
464 " slot: %u flags: 0x%x\n"
465 " guest_phys_addr: 0x%llx size: 0x%llx",
466 ret, errno, region->region.slot,
467 region->region.flags,
468 region->region.guest_phys_addr,
469 region->region.memory_size);
473 __weak struct kvm_vcpu *vm_arch_vcpu_recreate(struct kvm_vm *vm,
476 return __vm_vcpu_add(vm, vcpu_id);
479 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm)
483 return vm_vcpu_recreate(vm, 0);
486 void kvm_pin_this_task_to_pcpu(uint32_t pcpu)
492 CPU_SET(pcpu, &mask);
493 r = sched_setaffinity(0, sizeof(mask), &mask);
494 TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.\n", pcpu);
497 static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_mask)
499 uint32_t pcpu = atoi_non_negative("CPU number", cpu_str);
501 TEST_ASSERT(CPU_ISSET(pcpu, allowed_mask),
502 "Not allowed to run on pCPU '%d', check cgroups?\n", pcpu);
506 void kvm_print_vcpu_pinning_help(void)
508 const char *name = program_invocation_name;
510 printf(" -c: Pin tasks to physical CPUs. Takes a list of comma separated\n"
511 " values (target pCPU), one for each vCPU, plus an optional\n"
512 " entry for the main application task (specified via entry\n"
513 " <nr_vcpus + 1>). If used, entries must be provided for all\n"
514 " vCPUs, i.e. pinning vCPUs is all or nothing.\n\n"
515 " E.g. to create 3 vCPUs, pin vCPU0=>pCPU22, vCPU1=>pCPU23,\n"
516 " vCPU2=>pCPU24, and pin the application task to pCPU50:\n\n"
517 " %s -v 3 -c 22,23,24,50\n\n"
518 " To leave the application task unpinned, drop the final entry:\n\n"
519 " %s -v 3 -c 22,23,24\n\n"
520 " (default: no pinning)\n", name, name);
523 void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
526 cpu_set_t allowed_mask;
527 char *cpu, *cpu_list;
531 cpu_list = strdup(pcpus_string);
532 TEST_ASSERT(cpu_list, "strdup() allocation failed.\n");
534 r = sched_getaffinity(0, sizeof(allowed_mask), &allowed_mask);
535 TEST_ASSERT(!r, "sched_getaffinity() failed");
537 cpu = strtok(cpu_list, delim);
539 /* 1. Get all pcpus for vcpus. */
540 for (i = 0; i < nr_vcpus; i++) {
541 TEST_ASSERT(cpu, "pCPU not provided for vCPU '%d'\n", i);
542 vcpu_to_pcpu[i] = parse_pcpu(cpu, &allowed_mask);
543 cpu = strtok(NULL, delim);
546 /* 2. Check if the main worker needs to be pinned. */
548 kvm_pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask));
549 cpu = strtok(NULL, delim);
552 TEST_ASSERT(!cpu, "pCPU list contains trailing garbage characters '%s'", cpu);
557 * Userspace Memory Region Find
560 * vm - Virtual Machine
561 * start - Starting VM physical address
562 * end - Ending VM physical address, inclusive.
567 * Pointer to overlapping region, NULL if no such region.
569 * Searches for a region with any physical memory that overlaps with
570 * any portion of the guest physical addresses from start to end
571 * inclusive. If multiple overlapping regions exist, a pointer to any
572 * of the regions is returned. Null is returned only when no overlapping
575 static struct userspace_mem_region *
576 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
578 struct rb_node *node;
580 for (node = vm->regions.gpa_tree.rb_node; node; ) {
581 struct userspace_mem_region *region =
582 container_of(node, struct userspace_mem_region, gpa_node);
583 uint64_t existing_start = region->region.guest_phys_addr;
584 uint64_t existing_end = region->region.guest_phys_addr
585 + region->region.memory_size - 1;
586 if (start <= existing_end && end >= existing_start)
589 if (start < existing_start)
590 node = node->rb_left;
592 node = node->rb_right;
598 __weak void vcpu_arch_free(struct kvm_vcpu *vcpu)
607 * vcpu - VCPU to remove
611 * Return: None, TEST_ASSERT failures for all error conditions
613 * Removes a vCPU from a VM and frees its resources.
615 static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
619 if (vcpu->dirty_gfns) {
620 ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size);
621 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
622 vcpu->dirty_gfns = NULL;
625 ret = munmap(vcpu->run, vcpu_mmap_sz());
626 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
628 ret = close(vcpu->fd);
629 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
631 list_del(&vcpu->list);
633 vcpu_arch_free(vcpu);
637 void kvm_vm_release(struct kvm_vm *vmp)
639 struct kvm_vcpu *vcpu, *tmp;
642 list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list)
643 vm_vcpu_rm(vmp, vcpu);
645 ret = close(vmp->fd);
646 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
648 ret = close(vmp->kvm_fd);
649 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
652 static void __vm_mem_region_delete(struct kvm_vm *vm,
653 struct userspace_mem_region *region,
659 rb_erase(®ion->gpa_node, &vm->regions.gpa_tree);
660 rb_erase(®ion->hva_node, &vm->regions.hva_tree);
661 hash_del(®ion->slot_node);
664 region->region.memory_size = 0;
665 vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
667 sparsebit_free(®ion->unused_phy_pages);
668 ret = munmap(region->mmap_start, region->mmap_size);
669 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
670 if (region->fd >= 0) {
671 /* There's an extra map when using shared memory. */
672 ret = munmap(region->mmap_alias, region->mmap_size);
673 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
676 if (region->region.guest_memfd >= 0)
677 close(region->region.guest_memfd);
683 * Destroys and frees the VM pointed to by vmp.
685 void kvm_vm_free(struct kvm_vm *vmp)
688 struct hlist_node *node;
689 struct userspace_mem_region *region;
694 /* Free cached stats metadata and close FD */
696 free(vmp->stats_desc);
697 close(vmp->stats_fd);
700 /* Free userspace_mem_regions. */
701 hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)
702 __vm_mem_region_delete(vmp, region, false);
704 /* Free sparsebit arrays. */
705 sparsebit_free(&vmp->vpages_valid);
706 sparsebit_free(&vmp->vpages_mapped);
710 /* Free the structure describing the VM. */
714 int kvm_memfd_alloc(size_t size, bool hugepages)
716 int memfd_flags = MFD_CLOEXEC;
720 memfd_flags |= MFD_HUGETLB;
722 fd = memfd_create("kvm_selftest", memfd_flags);
723 TEST_ASSERT(fd != -1, __KVM_SYSCALL_ERROR("memfd_create()", fd));
725 r = ftruncate(fd, size);
726 TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("ftruncate()", r));
728 r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, size);
729 TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("fallocate()", r));
735 * Memory Compare, host virtual to guest virtual
738 * hva - Starting host virtual address
739 * vm - Virtual Machine
740 * gva - Starting guest virtual address
741 * len - number of bytes to compare
745 * Input/Output Args: None
748 * Returns 0 if the bytes starting at hva for a length of len
749 * are equal the guest virtual bytes starting at gva. Returns
750 * a value < 0, if bytes at hva are less than those at gva.
751 * Otherwise a value > 0 is returned.
753 * Compares the bytes starting at the host virtual address hva, for
754 * a length of len, to the guest bytes starting at the guest virtual
755 * address given by gva.
757 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
762 * Compare a batch of bytes until either a match is found
763 * or all the bytes have been compared.
765 for (uintptr_t offset = 0; offset < len; offset += amt) {
766 uintptr_t ptr1 = (uintptr_t)hva + offset;
769 * Determine host address for guest virtual address
772 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
775 * Determine amount to compare on this pass.
776 * Don't allow the comparsion to cross a page boundary.
779 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
780 amt = vm->page_size - (ptr1 % vm->page_size);
781 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
782 amt = vm->page_size - (ptr2 % vm->page_size);
784 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
785 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
788 * Perform the comparison. If there is a difference
789 * return that result to the caller, otherwise need
790 * to continue on looking for a mismatch.
792 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
798 * No mismatch found. Let the caller know the two memory
804 static void vm_userspace_mem_region_gpa_insert(struct rb_root *gpa_tree,
805 struct userspace_mem_region *region)
807 struct rb_node **cur, *parent;
809 for (cur = &gpa_tree->rb_node, parent = NULL; *cur; ) {
810 struct userspace_mem_region *cregion;
812 cregion = container_of(*cur, typeof(*cregion), gpa_node);
814 if (region->region.guest_phys_addr <
815 cregion->region.guest_phys_addr)
816 cur = &(*cur)->rb_left;
818 TEST_ASSERT(region->region.guest_phys_addr !=
819 cregion->region.guest_phys_addr,
820 "Duplicate GPA in region tree");
822 cur = &(*cur)->rb_right;
826 rb_link_node(®ion->gpa_node, parent, cur);
827 rb_insert_color(®ion->gpa_node, gpa_tree);
830 static void vm_userspace_mem_region_hva_insert(struct rb_root *hva_tree,
831 struct userspace_mem_region *region)
833 struct rb_node **cur, *parent;
835 for (cur = &hva_tree->rb_node, parent = NULL; *cur; ) {
836 struct userspace_mem_region *cregion;
838 cregion = container_of(*cur, typeof(*cregion), hva_node);
840 if (region->host_mem < cregion->host_mem)
841 cur = &(*cur)->rb_left;
843 TEST_ASSERT(region->host_mem !=
845 "Duplicate HVA in region tree");
847 cur = &(*cur)->rb_right;
851 rb_link_node(®ion->hva_node, parent, cur);
852 rb_insert_color(®ion->hva_node, hva_tree);
856 int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
857 uint64_t gpa, uint64_t size, void *hva)
859 struct kvm_userspace_memory_region region = {
862 .guest_phys_addr = gpa,
864 .userspace_addr = (uintptr_t)hva,
867 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion);
870 void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
871 uint64_t gpa, uint64_t size, void *hva)
873 int ret = __vm_set_user_memory_region(vm, slot, flags, gpa, size, hva);
875 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION failed, errno = %d (%s)",
876 errno, strerror(errno));
879 int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
880 uint64_t gpa, uint64_t size, void *hva,
881 uint32_t guest_memfd, uint64_t guest_memfd_offset)
883 struct kvm_userspace_memory_region2 region = {
886 .guest_phys_addr = gpa,
888 .userspace_addr = (uintptr_t)hva,
889 .guest_memfd = guest_memfd,
890 .guest_memfd_offset = guest_memfd_offset,
893 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION2, ®ion);
896 void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
897 uint64_t gpa, uint64_t size, void *hva,
898 uint32_t guest_memfd, uint64_t guest_memfd_offset)
900 int ret = __vm_set_user_memory_region2(vm, slot, flags, gpa, size, hva,
901 guest_memfd, guest_memfd_offset);
903 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed, errno = %d (%s)",
904 errno, strerror(errno));
908 /* FIXME: This thing needs to be ripped apart and rewritten. */
909 void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
910 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
911 uint32_t flags, int guest_memfd, uint64_t guest_memfd_offset)
914 struct userspace_mem_region *region;
915 size_t backing_src_pagesz = get_backing_src_pagesz(src_type);
916 size_t mem_size = npages * vm->page_size;
919 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
920 "Number of guest pages is not compatible with the host. "
921 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
923 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
924 "address not on a page boundary.\n"
925 " guest_paddr: 0x%lx vm->page_size: 0x%x",
926 guest_paddr, vm->page_size);
927 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
928 <= vm->max_gfn, "Physical range beyond maximum "
929 "supported physical address,\n"
930 " guest_paddr: 0x%lx npages: 0x%lx\n"
931 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
932 guest_paddr, npages, vm->max_gfn, vm->page_size);
935 * Confirm a mem region with an overlapping address doesn't
938 region = (struct userspace_mem_region *) userspace_mem_region_find(
939 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
941 TEST_FAIL("overlapping userspace_mem_region already "
943 " requested guest_paddr: 0x%lx npages: 0x%lx "
945 " existing guest_paddr: 0x%lx size: 0x%lx",
946 guest_paddr, npages, vm->page_size,
947 (uint64_t) region->region.guest_phys_addr,
948 (uint64_t) region->region.memory_size);
950 /* Confirm no region with the requested slot already exists. */
951 hash_for_each_possible(vm->regions.slot_hash, region, slot_node,
953 if (region->region.slot != slot)
956 TEST_FAIL("A mem region with the requested slot "
958 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
959 " existing slot: %u paddr: 0x%lx size: 0x%lx",
960 slot, guest_paddr, npages,
962 (uint64_t) region->region.guest_phys_addr,
963 (uint64_t) region->region.memory_size);
966 /* Allocate and initialize new mem region structure. */
967 region = calloc(1, sizeof(*region));
968 TEST_ASSERT(region != NULL, "Insufficient Memory");
969 region->mmap_size = mem_size;
972 /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
973 alignment = 0x100000;
979 * When using THP mmap is not guaranteed to returned a hugepage aligned
980 * address so we have to pad the mmap. Padding is not needed for HugeTLB
981 * because mmap will always return an address aligned to the HugeTLB
984 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
985 alignment = max(backing_src_pagesz, alignment);
987 TEST_ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
989 /* Add enough memory to align up if necessary */
991 region->mmap_size += alignment;
994 if (backing_src_is_shared(src_type))
995 region->fd = kvm_memfd_alloc(region->mmap_size,
996 src_type == VM_MEM_SRC_SHARED_HUGETLB);
998 region->mmap_start = mmap(NULL, region->mmap_size,
999 PROT_READ | PROT_WRITE,
1000 vm_mem_backing_src_alias(src_type)->flag,
1002 TEST_ASSERT(region->mmap_start != MAP_FAILED,
1003 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1005 TEST_ASSERT(!is_backing_src_hugetlb(src_type) ||
1006 region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz),
1007 "mmap_start %p is not aligned to HugeTLB page size 0x%lx",
1008 region->mmap_start, backing_src_pagesz);
1010 /* Align host address */
1011 region->host_mem = align_ptr_up(region->mmap_start, alignment);
1013 /* As needed perform madvise */
1014 if ((src_type == VM_MEM_SRC_ANONYMOUS ||
1015 src_type == VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) {
1016 ret = madvise(region->host_mem, mem_size,
1017 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
1018 TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %s",
1019 region->host_mem, mem_size,
1020 vm_mem_backing_src_alias(src_type)->name);
1023 region->backing_src_type = src_type;
1025 if (flags & KVM_MEM_GUEST_MEMFD) {
1026 if (guest_memfd < 0) {
1027 uint32_t guest_memfd_flags = 0;
1028 TEST_ASSERT(!guest_memfd_offset,
1029 "Offset must be zero when creating new guest_memfd");
1030 guest_memfd = vm_create_guest_memfd(vm, mem_size, guest_memfd_flags);
1033 * Install a unique fd for each memslot so that the fd
1034 * can be closed when the region is deleted without
1035 * needing to track if the fd is owned by the framework
1038 guest_memfd = dup(guest_memfd);
1039 TEST_ASSERT(guest_memfd >= 0, __KVM_SYSCALL_ERROR("dup()", guest_memfd));
1042 region->region.guest_memfd = guest_memfd;
1043 region->region.guest_memfd_offset = guest_memfd_offset;
1045 region->region.guest_memfd = -1;
1048 region->unused_phy_pages = sparsebit_alloc();
1049 sparsebit_set_num(region->unused_phy_pages,
1050 guest_paddr >> vm->page_shift, npages);
1051 region->region.slot = slot;
1052 region->region.flags = flags;
1053 region->region.guest_phys_addr = guest_paddr;
1054 region->region.memory_size = npages * vm->page_size;
1055 region->region.userspace_addr = (uintptr_t) region->host_mem;
1056 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1057 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
1058 " rc: %i errno: %i\n"
1059 " slot: %u flags: 0x%x\n"
1060 " guest_phys_addr: 0x%lx size: 0x%lx guest_memfd: %d\n",
1061 ret, errno, slot, flags,
1062 guest_paddr, (uint64_t) region->region.memory_size,
1063 region->region.guest_memfd);
1065 /* Add to quick lookup data structures */
1066 vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region);
1067 vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region);
1068 hash_add(vm->regions.slot_hash, ®ion->slot_node, slot);
1070 /* If shared memory, create an alias. */
1071 if (region->fd >= 0) {
1072 region->mmap_alias = mmap(NULL, region->mmap_size,
1073 PROT_READ | PROT_WRITE,
1074 vm_mem_backing_src_alias(src_type)->flag,
1076 TEST_ASSERT(region->mmap_alias != MAP_FAILED,
1077 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1079 /* Align host alias address */
1080 region->host_alias = align_ptr_up(region->mmap_alias, alignment);
1084 void vm_userspace_mem_region_add(struct kvm_vm *vm,
1085 enum vm_mem_backing_src_type src_type,
1086 uint64_t guest_paddr, uint32_t slot,
1087 uint64_t npages, uint32_t flags)
1089 vm_mem_add(vm, src_type, guest_paddr, slot, npages, flags, -1, 0);
1096 * vm - Virtual Machine
1097 * memslot - KVM memory slot ID
1102 * Pointer to memory region structure that describe memory region
1103 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
1104 * on error (e.g. currently no memory region using memslot as a KVM
1107 struct userspace_mem_region *
1108 memslot2region(struct kvm_vm *vm, uint32_t memslot)
1110 struct userspace_mem_region *region;
1112 hash_for_each_possible(vm->regions.slot_hash, region, slot_node,
1114 if (region->region.slot == memslot)
1117 fprintf(stderr, "No mem region with the requested slot found,\n"
1118 " requested slot: %u\n", memslot);
1119 fputs("---- vm dump ----\n", stderr);
1120 vm_dump(stderr, vm, 2);
1121 TEST_FAIL("Mem region not found");
1126 * VM Memory Region Flags Set
1129 * vm - Virtual Machine
1130 * flags - Starting guest physical address
1136 * Sets the flags of the memory region specified by the value of slot,
1137 * to the values given by flags.
1139 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
1142 struct userspace_mem_region *region;
1144 region = memslot2region(vm, slot);
1146 region->region.flags = flags;
1148 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1150 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
1151 " rc: %i errno: %i slot: %u flags: 0x%x",
1152 ret, errno, slot, flags);
1156 * VM Memory Region Move
1159 * vm - Virtual Machine
1160 * slot - Slot of the memory region to move
1161 * new_gpa - Starting guest physical address
1167 * Change the gpa of a memory region.
1169 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
1171 struct userspace_mem_region *region;
1174 region = memslot2region(vm, slot);
1176 region->region.guest_phys_addr = new_gpa;
1178 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1180 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed\n"
1181 "ret: %i errno: %i slot: %u new_gpa: 0x%lx",
1182 ret, errno, slot, new_gpa);
1186 * VM Memory Region Delete
1189 * vm - Virtual Machine
1190 * slot - Slot of the memory region to delete
1196 * Delete a memory region.
1198 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)
1200 __vm_mem_region_delete(vm, memslot2region(vm, slot), true);
1203 void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size,
1206 const int mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0);
1207 struct userspace_mem_region *region;
1208 uint64_t end = base + size;
1213 for (gpa = base; gpa < end; gpa += len) {
1216 region = userspace_mem_region_find(vm, gpa, gpa);
1217 TEST_ASSERT(region && region->region.flags & KVM_MEM_GUEST_MEMFD,
1218 "Private memory region not found for GPA 0x%lx", gpa);
1220 offset = gpa - region->region.guest_phys_addr;
1221 fd_offset = region->region.guest_memfd_offset + offset;
1222 len = min_t(uint64_t, end - gpa, region->region.memory_size - offset);
1224 ret = fallocate(region->region.guest_memfd, mode, fd_offset, len);
1225 TEST_ASSERT(!ret, "fallocate() failed to %s at %lx (len = %lu), fd = %d, mode = %x, offset = %lx\n",
1226 punch_hole ? "punch hole" : "allocate", gpa, len,
1227 region->region.guest_memfd, mode, fd_offset);
1231 /* Returns the size of a vCPU's kvm_run structure. */
1232 static int vcpu_mmap_sz(void)
1236 dev_fd = open_kvm_dev_path_or_exit();
1238 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
1239 TEST_ASSERT(ret >= sizeof(struct kvm_run),
1240 KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
1247 static bool vcpu_exists(struct kvm_vm *vm, uint32_t vcpu_id)
1249 struct kvm_vcpu *vcpu;
1251 list_for_each_entry(vcpu, &vm->vcpus, list) {
1252 if (vcpu->id == vcpu_id)
1260 * Adds a virtual CPU to the VM specified by vm with the ID given by vcpu_id.
1261 * No additional vCPU setup is done. Returns the vCPU.
1263 struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
1265 struct kvm_vcpu *vcpu;
1267 /* Confirm a vcpu with the specified id doesn't already exist. */
1268 TEST_ASSERT(!vcpu_exists(vm, vcpu_id), "vCPU%d already exists\n", vcpu_id);
1270 /* Allocate and initialize new vcpu structure. */
1271 vcpu = calloc(1, sizeof(*vcpu));
1272 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
1276 vcpu->fd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id);
1277 TEST_ASSERT_VM_VCPU_IOCTL(vcpu->fd >= 0, KVM_CREATE_VCPU, vcpu->fd, vm);
1279 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size "
1280 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
1281 vcpu_mmap_sz(), sizeof(*vcpu->run));
1282 vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(),
1283 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
1284 TEST_ASSERT(vcpu->run != MAP_FAILED,
1285 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1287 /* Add to linked-list of VCPUs. */
1288 list_add(&vcpu->list, &vm->vcpus);
1294 * VM Virtual Address Unused Gap
1297 * vm - Virtual Machine
1299 * vaddr_min - Minimum Virtual Address
1304 * Lowest virtual address at or below vaddr_min, with at least
1305 * sz unused bytes. TEST_ASSERT failure if no area of at least
1306 * size sz is available.
1308 * Within the VM specified by vm, locates the lowest starting virtual
1309 * address >= vaddr_min, that has at least sz unallocated bytes. A
1310 * TEST_ASSERT failure occurs for invalid input or no area of at least
1311 * sz unallocated bytes >= vaddr_min is available.
1313 vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
1314 vm_vaddr_t vaddr_min)
1316 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
1318 /* Determine lowest permitted virtual page index. */
1319 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
1320 if ((pgidx_start * vm->page_size) < vaddr_min)
1323 /* Loop over section with enough valid virtual page indexes. */
1324 if (!sparsebit_is_set_num(vm->vpages_valid,
1325 pgidx_start, pages))
1326 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
1327 pgidx_start, pages);
1330 * Are there enough unused virtual pages available at
1331 * the currently proposed starting virtual page index.
1332 * If not, adjust proposed starting index to next
1335 if (sparsebit_is_clear_num(vm->vpages_mapped,
1336 pgidx_start, pages))
1338 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
1339 pgidx_start, pages);
1340 if (pgidx_start == 0)
1344 * If needed, adjust proposed starting virtual address,
1345 * to next range of valid virtual addresses.
1347 if (!sparsebit_is_set_num(vm->vpages_valid,
1348 pgidx_start, pages)) {
1349 pgidx_start = sparsebit_next_set_num(
1350 vm->vpages_valid, pgidx_start, pages);
1351 if (pgidx_start == 0)
1354 } while (pgidx_start != 0);
1357 TEST_FAIL("No vaddr of specified pages available, pages: 0x%lx", pages);
1363 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
1364 pgidx_start, pages),
1365 "Unexpected, invalid virtual page index range,\n"
1366 " pgidx_start: 0x%lx\n"
1368 pgidx_start, pages);
1369 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
1370 pgidx_start, pages),
1371 "Unexpected, pages already mapped,\n"
1372 " pgidx_start: 0x%lx\n"
1374 pgidx_start, pages);
1376 return pgidx_start * vm->page_size;
1379 vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
1380 enum kvm_mem_region_type type)
1382 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
1385 vm_paddr_t paddr = vm_phy_pages_alloc(vm, pages,
1386 KVM_UTIL_MIN_PFN * vm->page_size,
1387 vm->memslots[type]);
1390 * Find an unused range of virtual page addresses of at least
1393 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
1395 /* Map the virtual pages. */
1396 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
1397 pages--, vaddr += vm->page_size, paddr += vm->page_size) {
1399 virt_pg_map(vm, vaddr, paddr);
1401 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
1408 * VM Virtual Address Allocate
1411 * vm - Virtual Machine
1412 * sz - Size in bytes
1413 * vaddr_min - Minimum starting virtual address
1418 * Starting guest virtual address
1420 * Allocates at least sz bytes within the virtual address space of the vm
1421 * given by vm. The allocated bytes are mapped to a virtual address >=
1422 * the address given by vaddr_min. Note that each allocation uses a
1423 * a unique set of pages, with the minimum real allocation being at least
1424 * a page. The allocated physical space comes from the TEST_DATA memory region.
1426 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
1428 return __vm_vaddr_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA);
1432 * VM Virtual Address Allocate Pages
1435 * vm - Virtual Machine
1440 * Starting guest virtual address
1442 * Allocates at least N system pages worth of bytes within the virtual address
1445 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages)
1447 return vm_vaddr_alloc(vm, nr_pages * getpagesize(), KVM_UTIL_MIN_VADDR);
1450 vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type)
1452 return __vm_vaddr_alloc(vm, getpagesize(), KVM_UTIL_MIN_VADDR, type);
1456 * VM Virtual Address Allocate Page
1459 * vm - Virtual Machine
1464 * Starting guest virtual address
1466 * Allocates at least one system page worth of bytes within the virtual address
1469 vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm)
1471 return vm_vaddr_alloc_pages(vm, 1);
1475 * Map a range of VM virtual address to the VM's physical address
1478 * vm - Virtual Machine
1479 * vaddr - Virtuall address to map
1480 * paddr - VM Physical Address
1481 * npages - The number of pages to map
1487 * Within the VM given by @vm, creates a virtual translation for
1488 * @npages starting at @vaddr to the page range starting at @paddr.
1490 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1491 unsigned int npages)
1493 size_t page_size = vm->page_size;
1494 size_t size = npages * page_size;
1496 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1497 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1500 virt_pg_map(vm, vaddr, paddr);
1501 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
1509 * Address VM Physical to Host Virtual
1512 * vm - Virtual Machine
1513 * gpa - VM physical address
1518 * Equivalent host virtual address
1520 * Locates the memory region containing the VM physical address given
1521 * by gpa, within the VM given by vm. When found, the host virtual
1522 * address providing the memory to the vm physical address is returned.
1523 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1525 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1527 struct userspace_mem_region *region;
1529 region = userspace_mem_region_find(vm, gpa, gpa);
1531 TEST_FAIL("No vm physical memory at 0x%lx", gpa);
1535 return (void *)((uintptr_t)region->host_mem
1536 + (gpa - region->region.guest_phys_addr));
1540 * Address Host Virtual to VM Physical
1543 * vm - Virtual Machine
1544 * hva - Host virtual address
1549 * Equivalent VM physical address
1551 * Locates the memory region containing the host virtual address given
1552 * by hva, within the VM given by vm. When found, the equivalent
1553 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1554 * region containing hva exists.
1556 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1558 struct rb_node *node;
1560 for (node = vm->regions.hva_tree.rb_node; node; ) {
1561 struct userspace_mem_region *region =
1562 container_of(node, struct userspace_mem_region, hva_node);
1564 if (hva >= region->host_mem) {
1565 if (hva <= (region->host_mem
1566 + region->region.memory_size - 1))
1567 return (vm_paddr_t)((uintptr_t)
1568 region->region.guest_phys_addr
1569 + (hva - (uintptr_t)region->host_mem));
1571 node = node->rb_right;
1573 node = node->rb_left;
1576 TEST_FAIL("No mapping to a guest physical address, hva: %p", hva);
1581 * Address VM physical to Host Virtual *alias*.
1584 * vm - Virtual Machine
1585 * gpa - VM physical address
1590 * Equivalent address within the host virtual *alias* area, or NULL
1591 * (without failing the test) if the guest memory is not shared (so
1594 * Create a writable, shared virtual=>physical alias for the specific GPA.
1595 * The primary use case is to allow the host selftest to manipulate guest
1596 * memory without mapping said memory in the guest's address space. And, for
1597 * userfaultfd-based demand paging, to do so without triggering userfaults.
1599 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)
1601 struct userspace_mem_region *region;
1604 region = userspace_mem_region_find(vm, gpa, gpa);
1608 if (!region->host_alias)
1611 offset = gpa - region->region.guest_phys_addr;
1612 return (void *) ((uintptr_t) region->host_alias + offset);
1615 /* Create an interrupt controller chip for the specified VM. */
1616 void vm_create_irqchip(struct kvm_vm *vm)
1618 vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
1620 vm->has_irqchip = true;
1623 int _vcpu_run(struct kvm_vcpu *vcpu)
1628 rc = __vcpu_run(vcpu);
1629 } while (rc == -1 && errno == EINTR);
1631 assert_on_unhandled_exception(vcpu);
1637 * Invoke KVM_RUN on a vCPU until KVM returns something other than -EINTR.
1638 * Assert if the KVM returns an error (other than -EINTR).
1640 void vcpu_run(struct kvm_vcpu *vcpu)
1642 int ret = _vcpu_run(vcpu);
1644 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_RUN, ret));
1647 void vcpu_run_complete_io(struct kvm_vcpu *vcpu)
1651 vcpu->run->immediate_exit = 1;
1652 ret = __vcpu_run(vcpu);
1653 vcpu->run->immediate_exit = 0;
1655 TEST_ASSERT(ret == -1 && errno == EINTR,
1656 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1661 * Get the list of guest registers which are supported for
1662 * KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls. Returns a kvm_reg_list pointer,
1663 * it is the caller's responsibility to free the list.
1665 struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu)
1667 struct kvm_reg_list reg_list_n = { .n = 0 }, *reg_list;
1670 ret = __vcpu_ioctl(vcpu, KVM_GET_REG_LIST, ®_list_n);
1671 TEST_ASSERT(ret == -1 && errno == E2BIG, "KVM_GET_REG_LIST n=0");
1673 reg_list = calloc(1, sizeof(*reg_list) + reg_list_n.n * sizeof(__u64));
1674 reg_list->n = reg_list_n.n;
1675 vcpu_ioctl(vcpu, KVM_GET_REG_LIST, reg_list);
1679 void *vcpu_map_dirty_ring(struct kvm_vcpu *vcpu)
1681 uint32_t page_size = getpagesize();
1682 uint32_t size = vcpu->vm->dirty_ring_size;
1684 TEST_ASSERT(size > 0, "Should enable dirty ring first");
1686 if (!vcpu->dirty_gfns) {
1689 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, vcpu->fd,
1690 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1691 TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped private");
1693 addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, vcpu->fd,
1694 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1695 TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped exec");
1697 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd,
1698 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1699 TEST_ASSERT(addr != MAP_FAILED, "Dirty ring map failed");
1701 vcpu->dirty_gfns = addr;
1702 vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn);
1705 return vcpu->dirty_gfns;
1712 int __kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr)
1714 struct kvm_device_attr attribute = {
1720 return ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute);
1723 int __kvm_test_create_device(struct kvm_vm *vm, uint64_t type)
1725 struct kvm_create_device create_dev = {
1727 .flags = KVM_CREATE_DEVICE_TEST,
1730 return __vm_ioctl(vm, KVM_CREATE_DEVICE, &create_dev);
1733 int __kvm_create_device(struct kvm_vm *vm, uint64_t type)
1735 struct kvm_create_device create_dev = {
1742 err = __vm_ioctl(vm, KVM_CREATE_DEVICE, &create_dev);
1743 TEST_ASSERT(err <= 0, "KVM_CREATE_DEVICE shouldn't return a positive value");
1744 return err ? : create_dev.fd;
1747 int __kvm_device_attr_get(int dev_fd, uint32_t group, uint64_t attr, void *val)
1749 struct kvm_device_attr kvmattr = {
1753 .addr = (uintptr_t)val,
1756 return __kvm_ioctl(dev_fd, KVM_GET_DEVICE_ATTR, &kvmattr);
1759 int __kvm_device_attr_set(int dev_fd, uint32_t group, uint64_t attr, void *val)
1761 struct kvm_device_attr kvmattr = {
1765 .addr = (uintptr_t)val,
1768 return __kvm_ioctl(dev_fd, KVM_SET_DEVICE_ATTR, &kvmattr);
1772 * IRQ related functions.
1775 int _kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)
1777 struct kvm_irq_level irq_level = {
1782 return __vm_ioctl(vm, KVM_IRQ_LINE, &irq_level);
1785 void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)
1787 int ret = _kvm_irq_line(vm, irq, level);
1789 TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_IRQ_LINE, ret));
1792 struct kvm_irq_routing *kvm_gsi_routing_create(void)
1794 struct kvm_irq_routing *routing;
1797 size = sizeof(struct kvm_irq_routing);
1798 /* Allocate space for the max number of entries: this wastes 196 KBs. */
1799 size += KVM_MAX_IRQ_ROUTES * sizeof(struct kvm_irq_routing_entry);
1800 routing = calloc(1, size);
1806 void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing,
1807 uint32_t gsi, uint32_t pin)
1812 assert(routing->nr < KVM_MAX_IRQ_ROUTES);
1815 routing->entries[i].gsi = gsi;
1816 routing->entries[i].type = KVM_IRQ_ROUTING_IRQCHIP;
1817 routing->entries[i].flags = 0;
1818 routing->entries[i].u.irqchip.irqchip = 0;
1819 routing->entries[i].u.irqchip.pin = pin;
1823 int _kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)
1828 ret = __vm_ioctl(vm, KVM_SET_GSI_ROUTING, routing);
1834 void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)
1838 ret = _kvm_gsi_routing_write(vm, routing);
1839 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_GSI_ROUTING, ret));
1846 * vm - Virtual Machine
1847 * indent - Left margin indent amount
1850 * stream - Output FILE stream
1854 * Dumps the current state of the VM given by vm, to the FILE stream
1857 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1860 struct userspace_mem_region *region;
1861 struct kvm_vcpu *vcpu;
1863 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1864 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1865 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1866 fprintf(stream, "%*sMem Regions:\n", indent, "");
1867 hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) {
1868 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1869 "host_virt: %p\n", indent + 2, "",
1870 (uint64_t) region->region.guest_phys_addr,
1871 (uint64_t) region->region.memory_size,
1873 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1874 sparsebit_dump(stream, region->unused_phy_pages, 0);
1876 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1877 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1878 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1880 if (vm->pgd_created) {
1881 fprintf(stream, "%*sVirtual Translation Tables:\n",
1883 virt_dump(stream, vm, indent + 4);
1885 fprintf(stream, "%*sVCPUs:\n", indent, "");
1887 list_for_each_entry(vcpu, &vm->vcpus, list)
1888 vcpu_dump(stream, vcpu, indent + 2);
1891 #define KVM_EXIT_STRING(x) {KVM_EXIT_##x, #x}
1893 /* Known KVM exit reasons */
1894 static struct exit_reason {
1895 unsigned int reason;
1897 } exit_reasons_known[] = {
1898 KVM_EXIT_STRING(UNKNOWN),
1899 KVM_EXIT_STRING(EXCEPTION),
1900 KVM_EXIT_STRING(IO),
1901 KVM_EXIT_STRING(HYPERCALL),
1902 KVM_EXIT_STRING(DEBUG),
1903 KVM_EXIT_STRING(HLT),
1904 KVM_EXIT_STRING(MMIO),
1905 KVM_EXIT_STRING(IRQ_WINDOW_OPEN),
1906 KVM_EXIT_STRING(SHUTDOWN),
1907 KVM_EXIT_STRING(FAIL_ENTRY),
1908 KVM_EXIT_STRING(INTR),
1909 KVM_EXIT_STRING(SET_TPR),
1910 KVM_EXIT_STRING(TPR_ACCESS),
1911 KVM_EXIT_STRING(S390_SIEIC),
1912 KVM_EXIT_STRING(S390_RESET),
1913 KVM_EXIT_STRING(DCR),
1914 KVM_EXIT_STRING(NMI),
1915 KVM_EXIT_STRING(INTERNAL_ERROR),
1916 KVM_EXIT_STRING(OSI),
1917 KVM_EXIT_STRING(PAPR_HCALL),
1918 KVM_EXIT_STRING(S390_UCONTROL),
1919 KVM_EXIT_STRING(WATCHDOG),
1920 KVM_EXIT_STRING(S390_TSCH),
1921 KVM_EXIT_STRING(EPR),
1922 KVM_EXIT_STRING(SYSTEM_EVENT),
1923 KVM_EXIT_STRING(S390_STSI),
1924 KVM_EXIT_STRING(IOAPIC_EOI),
1925 KVM_EXIT_STRING(HYPERV),
1926 KVM_EXIT_STRING(ARM_NISV),
1927 KVM_EXIT_STRING(X86_RDMSR),
1928 KVM_EXIT_STRING(X86_WRMSR),
1929 KVM_EXIT_STRING(DIRTY_RING_FULL),
1930 KVM_EXIT_STRING(AP_RESET_HOLD),
1931 KVM_EXIT_STRING(X86_BUS_LOCK),
1932 KVM_EXIT_STRING(XEN),
1933 KVM_EXIT_STRING(RISCV_SBI),
1934 KVM_EXIT_STRING(RISCV_CSR),
1935 KVM_EXIT_STRING(NOTIFY),
1936 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
1937 KVM_EXIT_STRING(MEMORY_NOT_PRESENT),
1942 * Exit Reason String
1945 * exit_reason - Exit reason
1950 * Constant string pointer describing the exit reason.
1952 * Locates and returns a constant string that describes the KVM exit
1953 * reason given by exit_reason. If no such string is found, a constant
1954 * string of "Unknown" is returned.
1956 const char *exit_reason_str(unsigned int exit_reason)
1960 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
1961 if (exit_reason == exit_reasons_known[n1].reason)
1962 return exit_reasons_known[n1].name;
1969 * Physical Contiguous Page Allocator
1972 * vm - Virtual Machine
1973 * num - number of pages
1974 * paddr_min - Physical address minimum
1975 * memslot - Memory region to allocate page from
1980 * Starting physical address
1982 * Within the VM specified by vm, locates a range of available physical
1983 * pages at or above paddr_min. If found, the pages are marked as in use
1984 * and their base address is returned. A TEST_ASSERT failure occurs if
1985 * not enough pages are available at or above paddr_min.
1987 vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
1988 vm_paddr_t paddr_min, uint32_t memslot)
1990 struct userspace_mem_region *region;
1991 sparsebit_idx_t pg, base;
1993 TEST_ASSERT(num > 0, "Must allocate at least one page");
1995 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
1996 "not divisible by page size.\n"
1997 " paddr_min: 0x%lx page_size: 0x%x",
1998 paddr_min, vm->page_size);
2000 region = memslot2region(vm, memslot);
2001 base = pg = paddr_min >> vm->page_shift;
2004 for (; pg < base + num; ++pg) {
2005 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
2006 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
2010 } while (pg && pg != base + num);
2013 fprintf(stderr, "No guest physical page available, "
2014 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
2015 paddr_min, vm->page_size, memslot);
2016 fputs("---- vm dump ----\n", stderr);
2017 vm_dump(stderr, vm, 2);
2021 for (pg = base; pg < base + num; ++pg)
2022 sparsebit_clear(region->unused_phy_pages, pg);
2024 return base * vm->page_size;
2027 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
2030 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
2033 vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm)
2035 return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR,
2036 vm->memslots[MEM_REGION_PT]);
2040 * Address Guest Virtual to Host Virtual
2043 * vm - Virtual Machine
2044 * gva - VM virtual address
2049 * Equivalent host virtual address
2051 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
2053 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
2056 unsigned long __weak vm_compute_max_gfn(struct kvm_vm *vm)
2058 return ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
2061 static unsigned int vm_calc_num_pages(unsigned int num_pages,
2062 unsigned int page_shift,
2063 unsigned int new_page_shift,
2066 unsigned int n = 1 << (new_page_shift - page_shift);
2068 if (page_shift >= new_page_shift)
2069 return num_pages * (1 << (page_shift - new_page_shift));
2071 return num_pages / n + !!(ceil && num_pages % n);
2074 static inline int getpageshift(void)
2076 return __builtin_ffs(getpagesize()) - 1;
2080 vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
2082 return vm_calc_num_pages(num_guest_pages,
2083 vm_guest_mode_params[mode].page_shift,
2084 getpageshift(), true);
2088 vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
2090 return vm_calc_num_pages(num_host_pages, getpageshift(),
2091 vm_guest_mode_params[mode].page_shift, false);
2094 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
2097 n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
2098 return vm_adjust_num_guest_pages(mode, n);
2102 * Read binary stats descriptors
2105 * stats_fd - the file descriptor for the binary stats file from which to read
2106 * header - the binary stats metadata header corresponding to the given FD
2111 * A pointer to a newly allocated series of stat descriptors.
2112 * Caller is responsible for freeing the returned kvm_stats_desc.
2114 * Read the stats descriptors from the binary stats interface.
2116 struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
2117 struct kvm_stats_header *header)
2119 struct kvm_stats_desc *stats_desc;
2120 ssize_t desc_size, total_size, ret;
2122 desc_size = get_stats_descriptor_size(header);
2123 total_size = header->num_desc * desc_size;
2125 stats_desc = calloc(header->num_desc, desc_size);
2126 TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
2128 ret = pread(stats_fd, stats_desc, total_size, header->desc_offset);
2129 TEST_ASSERT(ret == total_size, "Read KVM stats descriptors");
2135 * Read stat data for a particular stat
2138 * stats_fd - the file descriptor for the binary stats file from which to read
2139 * header - the binary stats metadata header corresponding to the given FD
2140 * desc - the binary stat metadata for the particular stat to be read
2141 * max_elements - the maximum number of 8-byte values to read into data
2144 * data - the buffer into which stat data should be read
2146 * Read the data values of a specified stat from the binary stats interface.
2148 void read_stat_data(int stats_fd, struct kvm_stats_header *header,
2149 struct kvm_stats_desc *desc, uint64_t *data,
2150 size_t max_elements)
2152 size_t nr_elements = min_t(ssize_t, desc->size, max_elements);
2153 size_t size = nr_elements * sizeof(*data);
2156 TEST_ASSERT(desc->size, "No elements in stat '%s'", desc->name);
2157 TEST_ASSERT(max_elements, "Zero elements requested for stat '%s'", desc->name);
2159 ret = pread(stats_fd, data, size,
2160 header->data_offset + desc->offset);
2162 TEST_ASSERT(ret >= 0, "pread() failed on stat '%s', errno: %i (%s)",
2163 desc->name, errno, strerror(errno));
2164 TEST_ASSERT(ret == size,
2165 "pread() on stat '%s' read %ld bytes, wanted %lu bytes",
2166 desc->name, size, ret);
2170 * Read the data of the named stat
2173 * vm - the VM for which the stat should be read
2174 * stat_name - the name of the stat to read
2175 * max_elements - the maximum number of 8-byte values to read into data
2178 * data - the buffer into which stat data should be read
2180 * Read the data values of a specified stat from the binary stats interface.
2182 void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
2183 size_t max_elements)
2185 struct kvm_stats_desc *desc;
2189 if (!vm->stats_fd) {
2190 vm->stats_fd = vm_get_stats_fd(vm);
2191 read_stats_header(vm->stats_fd, &vm->stats_header);
2192 vm->stats_desc = read_stats_descriptors(vm->stats_fd,
2196 size_desc = get_stats_descriptor_size(&vm->stats_header);
2198 for (i = 0; i < vm->stats_header.num_desc; ++i) {
2199 desc = (void *)vm->stats_desc + (i * size_desc);
2201 if (strcmp(desc->name, stat_name))
2204 read_stat_data(vm->stats_fd, &vm->stats_header, desc,
2205 data, max_elements);
2211 __weak void kvm_arch_vm_post_create(struct kvm_vm *vm)
2215 __weak void kvm_selftest_arch_init(void)
2219 void __attribute((constructor)) kvm_selftest_init(void)
2221 /* Tell stdout not to buffer its content. */
2222 setbuf(stdout, NULL);
2224 kvm_selftest_arch_init();