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 || errno != ENOENT, "Cannot open %s: %s", path, strerror(errno));
31 TEST_ASSERT(fd >= 0, "Failed to open '%s'", path);
37 * Open KVM_DEV_PATH if available, otherwise exit the entire program.
40 * flags - The flags to pass when opening KVM_DEV_PATH.
43 * The opened file descriptor of /dev/kvm.
45 static int _open_kvm_dev_path_or_exit(int flags)
47 return open_path_or_exit(KVM_DEV_PATH, flags);
50 int open_kvm_dev_path_or_exit(void)
52 return _open_kvm_dev_path_or_exit(O_RDONLY);
55 static ssize_t get_module_param(const char *module_name, const char *param,
56 void *buffer, size_t buffer_size)
58 const int path_size = 128;
63 r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
65 TEST_ASSERT(r < path_size,
66 "Failed to construct sysfs path in %d bytes.", path_size);
68 fd = open_path_or_exit(path, O_RDONLY);
70 bytes_read = read(fd, buffer, buffer_size);
71 TEST_ASSERT(bytes_read > 0, "read(%s) returned %ld, wanted %ld bytes",
72 path, bytes_read, buffer_size);
75 TEST_ASSERT(!r, "close(%s) failed", path);
79 static int get_module_param_integer(const char *module_name, const char *param)
82 * 16 bytes to hold a 64-bit value (1 byte per char), 1 byte for the
83 * NUL char, and 1 byte because the kernel sucks and inserts a newline
86 char value[16 + 1 + 1];
89 memset(value, '\0', sizeof(value));
91 r = get_module_param(module_name, param, value, sizeof(value));
92 TEST_ASSERT(value[r - 1] == '\n',
93 "Expected trailing newline, got char '%c'", value[r - 1]);
96 * Squash the newline, otherwise atoi_paranoid() will complain about
97 * trailing non-NUL characters in the string.
100 return atoi_paranoid(value);
103 static bool get_module_param_bool(const char *module_name, const char *param)
108 r = get_module_param(module_name, param, &value, sizeof(value));
109 TEST_ASSERT_EQ(r, 1);
113 else if (value == 'N')
116 TEST_FAIL("Unrecognized value '%c' for boolean module param", value);
119 bool get_kvm_param_bool(const char *param)
121 return get_module_param_bool("kvm", param);
124 bool get_kvm_intel_param_bool(const char *param)
126 return get_module_param_bool("kvm_intel", param);
129 bool get_kvm_amd_param_bool(const char *param)
131 return get_module_param_bool("kvm_amd", param);
134 int get_kvm_param_integer(const char *param)
136 return get_module_param_integer("kvm", param);
139 int get_kvm_intel_param_integer(const char *param)
141 return get_module_param_integer("kvm_intel", param);
144 int get_kvm_amd_param_integer(const char *param)
146 return get_module_param_integer("kvm_amd", param);
158 * On success, the Value corresponding to the capability (KVM_CAP_*)
159 * specified by the value of cap. On failure a TEST_ASSERT failure
162 * Looks up and returns the value corresponding to the capability
163 * (KVM_CAP_*) given by cap.
165 unsigned int kvm_check_cap(long cap)
170 kvm_fd = open_kvm_dev_path_or_exit();
171 ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap);
172 TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret));
176 return (unsigned int)ret;
179 void vm_enable_dirty_ring(struct kvm_vm *vm, uint32_t ring_size)
181 if (vm_check_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL))
182 vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING_ACQ_REL, ring_size);
184 vm_enable_cap(vm, KVM_CAP_DIRTY_LOG_RING, ring_size);
185 vm->dirty_ring_size = ring_size;
188 static void vm_open(struct kvm_vm *vm)
190 vm->kvm_fd = _open_kvm_dev_path_or_exit(O_RDWR);
192 TEST_REQUIRE(kvm_has_cap(KVM_CAP_IMMEDIATE_EXIT));
194 vm->fd = __kvm_ioctl(vm->kvm_fd, KVM_CREATE_VM, (void *)vm->type);
195 TEST_ASSERT(vm->fd >= 0, KVM_IOCTL_ERROR(KVM_CREATE_VM, vm->fd));
198 const char *vm_guest_mode_string(uint32_t i)
200 static const char * const strings[] = {
201 [VM_MODE_P52V48_4K] = "PA-bits:52, VA-bits:48, 4K pages",
202 [VM_MODE_P52V48_16K] = "PA-bits:52, VA-bits:48, 16K pages",
203 [VM_MODE_P52V48_64K] = "PA-bits:52, VA-bits:48, 64K pages",
204 [VM_MODE_P48V48_4K] = "PA-bits:48, VA-bits:48, 4K pages",
205 [VM_MODE_P48V48_16K] = "PA-bits:48, VA-bits:48, 16K pages",
206 [VM_MODE_P48V48_64K] = "PA-bits:48, VA-bits:48, 64K pages",
207 [VM_MODE_P40V48_4K] = "PA-bits:40, VA-bits:48, 4K pages",
208 [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages",
209 [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages",
210 [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages",
211 [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages",
212 [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages",
213 [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages",
214 [VM_MODE_P36V48_16K] = "PA-bits:36, VA-bits:48, 16K pages",
215 [VM_MODE_P36V48_64K] = "PA-bits:36, VA-bits:48, 64K pages",
216 [VM_MODE_P36V47_16K] = "PA-bits:36, VA-bits:47, 16K pages",
218 _Static_assert(sizeof(strings)/sizeof(char *) == NUM_VM_MODES,
219 "Missing new mode strings?");
221 TEST_ASSERT(i < NUM_VM_MODES, "Guest mode ID %d too big", i);
226 const struct vm_guest_mode_params vm_guest_mode_params[] = {
227 [VM_MODE_P52V48_4K] = { 52, 48, 0x1000, 12 },
228 [VM_MODE_P52V48_16K] = { 52, 48, 0x4000, 14 },
229 [VM_MODE_P52V48_64K] = { 52, 48, 0x10000, 16 },
230 [VM_MODE_P48V48_4K] = { 48, 48, 0x1000, 12 },
231 [VM_MODE_P48V48_16K] = { 48, 48, 0x4000, 14 },
232 [VM_MODE_P48V48_64K] = { 48, 48, 0x10000, 16 },
233 [VM_MODE_P40V48_4K] = { 40, 48, 0x1000, 12 },
234 [VM_MODE_P40V48_16K] = { 40, 48, 0x4000, 14 },
235 [VM_MODE_P40V48_64K] = { 40, 48, 0x10000, 16 },
236 [VM_MODE_PXXV48_4K] = { 0, 0, 0x1000, 12 },
237 [VM_MODE_P47V64_4K] = { 47, 64, 0x1000, 12 },
238 [VM_MODE_P44V64_4K] = { 44, 64, 0x1000, 12 },
239 [VM_MODE_P36V48_4K] = { 36, 48, 0x1000, 12 },
240 [VM_MODE_P36V48_16K] = { 36, 48, 0x4000, 14 },
241 [VM_MODE_P36V48_64K] = { 36, 48, 0x10000, 16 },
242 [VM_MODE_P36V47_16K] = { 36, 47, 0x4000, 14 },
244 _Static_assert(sizeof(vm_guest_mode_params)/sizeof(struct vm_guest_mode_params) == NUM_VM_MODES,
245 "Missing new mode params?");
248 * Initializes vm->vpages_valid to match the canonical VA space of the
251 * The default implementation is valid for architectures which split the
252 * range addressed by a single page table into a low and high region
253 * based on the MSB of the VA. On architectures with this behavior
254 * the VA region spans [0, 2^(va_bits - 1)), [-(2^(va_bits - 1), -1].
256 __weak void vm_vaddr_populate_bitmap(struct kvm_vm *vm)
258 sparsebit_set_num(vm->vpages_valid,
259 0, (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
260 sparsebit_set_num(vm->vpages_valid,
261 (~((1ULL << (vm->va_bits - 1)) - 1)) >> vm->page_shift,
262 (1ULL << (vm->va_bits - 1)) >> vm->page_shift);
265 struct kvm_vm *____vm_create(struct vm_shape shape)
269 vm = calloc(1, sizeof(*vm));
270 TEST_ASSERT(vm != NULL, "Insufficient Memory");
272 INIT_LIST_HEAD(&vm->vcpus);
273 vm->regions.gpa_tree = RB_ROOT;
274 vm->regions.hva_tree = RB_ROOT;
275 hash_init(vm->regions.slot_hash);
277 vm->mode = shape.mode;
278 vm->type = shape.type;
279 vm->subtype = shape.subtype;
281 vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits;
282 vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;
283 vm->page_size = vm_guest_mode_params[vm->mode].page_size;
284 vm->page_shift = vm_guest_mode_params[vm->mode].page_shift;
286 /* Setup mode specific traits. */
288 case VM_MODE_P52V48_4K:
289 vm->pgtable_levels = 4;
291 case VM_MODE_P52V48_64K:
292 vm->pgtable_levels = 3;
294 case VM_MODE_P48V48_4K:
295 vm->pgtable_levels = 4;
297 case VM_MODE_P48V48_64K:
298 vm->pgtable_levels = 3;
300 case VM_MODE_P40V48_4K:
301 case VM_MODE_P36V48_4K:
302 vm->pgtable_levels = 4;
304 case VM_MODE_P40V48_64K:
305 case VM_MODE_P36V48_64K:
306 vm->pgtable_levels = 3;
308 case VM_MODE_P52V48_16K:
309 case VM_MODE_P48V48_16K:
310 case VM_MODE_P40V48_16K:
311 case VM_MODE_P36V48_16K:
312 vm->pgtable_levels = 4;
314 case VM_MODE_P36V47_16K:
315 vm->pgtable_levels = 3;
317 case VM_MODE_PXXV48_4K:
319 kvm_get_cpu_address_width(&vm->pa_bits, &vm->va_bits);
320 kvm_init_vm_address_properties(vm);
322 * Ignore KVM support for 5-level paging (vm->va_bits == 57),
323 * it doesn't take effect unless a CR4.LA57 is set, which it
324 * isn't for this mode (48-bit virtual address space).
326 TEST_ASSERT(vm->va_bits == 48 || vm->va_bits == 57,
327 "Linear address width (%d bits) not supported",
329 pr_debug("Guest physical address width detected: %d\n",
331 vm->pgtable_levels = 4;
334 TEST_FAIL("VM_MODE_PXXV48_4K not supported on non-x86 platforms");
337 case VM_MODE_P47V64_4K:
338 vm->pgtable_levels = 5;
340 case VM_MODE_P44V64_4K:
341 vm->pgtable_levels = 5;
344 TEST_FAIL("Unknown guest mode: 0x%x", vm->mode);
348 TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types");
349 if (vm->pa_bits != 40)
350 vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits);
355 /* Limit to VA-bit canonical virtual addresses. */
356 vm->vpages_valid = sparsebit_alloc();
357 vm_vaddr_populate_bitmap(vm);
359 /* Limit physical addresses to PA-bits. */
360 vm->max_gfn = vm_compute_max_gfn(vm);
362 /* Allocate and setup memory for guest. */
363 vm->vpages_mapped = sparsebit_alloc();
368 static uint64_t vm_nr_pages_required(enum vm_guest_mode mode,
369 uint32_t nr_runnable_vcpus,
370 uint64_t extra_mem_pages)
372 uint64_t page_size = vm_guest_mode_params[mode].page_size;
375 TEST_ASSERT(nr_runnable_vcpus,
376 "Use vm_create_barebones() for VMs that _never_ have vCPUs");
378 TEST_ASSERT(nr_runnable_vcpus <= kvm_check_cap(KVM_CAP_MAX_VCPUS),
379 "nr_vcpus = %d too large for host, max-vcpus = %d",
380 nr_runnable_vcpus, kvm_check_cap(KVM_CAP_MAX_VCPUS));
383 * Arbitrarily allocate 512 pages (2mb when page size is 4kb) for the
384 * test code and other per-VM assets that will be loaded into memslot0.
388 /* Account for the per-vCPU stacks on behalf of the test. */
389 nr_pages += nr_runnable_vcpus * DEFAULT_STACK_PGS;
392 * Account for the number of pages needed for the page tables. The
393 * maximum page table size for a memory region will be when the
394 * smallest page size is used. Considering each page contains x page
395 * table descriptors, the total extra size for page tables (for extra
396 * N pages) will be: N/x+N/x^2+N/x^3+... which is definitely smaller
399 nr_pages += (nr_pages + extra_mem_pages) / PTES_PER_MIN_PAGE * 2;
401 /* Account for the number of pages needed by ucall. */
402 nr_pages += ucall_nr_pages_required(page_size);
404 return vm_adjust_num_guest_pages(mode, nr_pages);
407 struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
408 uint64_t nr_extra_pages)
410 uint64_t nr_pages = vm_nr_pages_required(shape.mode, nr_runnable_vcpus,
412 struct userspace_mem_region *slot0;
416 pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__,
417 vm_guest_mode_string(shape.mode), shape.type, nr_pages);
419 vm = ____vm_create(shape);
421 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0);
422 for (i = 0; i < NR_MEM_REGIONS; i++)
425 kvm_vm_elf_load(vm, program_invocation_name);
428 * TODO: Add proper defines to protect the library's memslots, and then
429 * carve out memslot1 for the ucall MMIO address. KVM treats writes to
430 * read-only memslots as MMIO, and creating a read-only memslot for the
431 * MMIO region would prevent silently clobbering the MMIO region.
433 slot0 = memslot2region(vm, 0);
434 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
436 kvm_arch_vm_post_create(vm);
442 * VM Create with customized parameters
445 * mode - VM Mode (e.g. VM_MODE_P52V48_4K)
446 * nr_vcpus - VCPU count
447 * extra_mem_pages - Non-slot0 physical memory total size
448 * guest_code - Guest entry point
454 * Pointer to opaque structure that describes the created VM.
456 * Creates a VM with the mode specified by mode (e.g. VM_MODE_P52V48_4K).
457 * extra_mem_pages is only used to calculate the maximum page table size,
458 * no real memory allocation for non-slot0 memory in this function.
460 struct kvm_vm *__vm_create_with_vcpus(struct vm_shape shape, uint32_t nr_vcpus,
461 uint64_t extra_mem_pages,
462 void *guest_code, struct kvm_vcpu *vcpus[])
467 TEST_ASSERT(!nr_vcpus || vcpus, "Must provide vCPU array");
469 vm = __vm_create(shape, nr_vcpus, extra_mem_pages);
471 for (i = 0; i < nr_vcpus; ++i)
472 vcpus[i] = vm_vcpu_add(vm, i, guest_code);
477 struct kvm_vm *__vm_create_shape_with_one_vcpu(struct vm_shape shape,
478 struct kvm_vcpu **vcpu,
479 uint64_t extra_mem_pages,
482 struct kvm_vcpu *vcpus[1];
485 vm = __vm_create_with_vcpus(shape, 1, extra_mem_pages, guest_code, vcpus);
495 * vm - VM that has been released before
499 * Reopens the file descriptors associated to the VM and reinstates the
500 * global state, such as the irqchip and the memory regions that are mapped
503 void kvm_vm_restart(struct kvm_vm *vmp)
506 struct userspace_mem_region *region;
509 if (vmp->has_irqchip)
510 vm_create_irqchip(vmp);
512 hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) {
513 int ret = ioctl(vmp->fd, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
515 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
516 " rc: %i errno: %i\n"
517 " slot: %u flags: 0x%x\n"
518 " guest_phys_addr: 0x%llx size: 0x%llx",
519 ret, errno, region->region.slot,
520 region->region.flags,
521 region->region.guest_phys_addr,
522 region->region.memory_size);
526 __weak struct kvm_vcpu *vm_arch_vcpu_recreate(struct kvm_vm *vm,
529 return __vm_vcpu_add(vm, vcpu_id);
532 struct kvm_vcpu *vm_recreate_with_one_vcpu(struct kvm_vm *vm)
536 return vm_vcpu_recreate(vm, 0);
539 void kvm_pin_this_task_to_pcpu(uint32_t pcpu)
545 CPU_SET(pcpu, &mask);
546 r = sched_setaffinity(0, sizeof(mask), &mask);
547 TEST_ASSERT(!r, "sched_setaffinity() failed for pCPU '%u'.", pcpu);
550 static uint32_t parse_pcpu(const char *cpu_str, const cpu_set_t *allowed_mask)
552 uint32_t pcpu = atoi_non_negative("CPU number", cpu_str);
554 TEST_ASSERT(CPU_ISSET(pcpu, allowed_mask),
555 "Not allowed to run on pCPU '%d', check cgroups?", pcpu);
559 void kvm_print_vcpu_pinning_help(void)
561 const char *name = program_invocation_name;
563 printf(" -c: Pin tasks to physical CPUs. Takes a list of comma separated\n"
564 " values (target pCPU), one for each vCPU, plus an optional\n"
565 " entry for the main application task (specified via entry\n"
566 " <nr_vcpus + 1>). If used, entries must be provided for all\n"
567 " vCPUs, i.e. pinning vCPUs is all or nothing.\n\n"
568 " E.g. to create 3 vCPUs, pin vCPU0=>pCPU22, vCPU1=>pCPU23,\n"
569 " vCPU2=>pCPU24, and pin the application task to pCPU50:\n\n"
570 " %s -v 3 -c 22,23,24,50\n\n"
571 " To leave the application task unpinned, drop the final entry:\n\n"
572 " %s -v 3 -c 22,23,24\n\n"
573 " (default: no pinning)\n", name, name);
576 void kvm_parse_vcpu_pinning(const char *pcpus_string, uint32_t vcpu_to_pcpu[],
579 cpu_set_t allowed_mask;
580 char *cpu, *cpu_list;
584 cpu_list = strdup(pcpus_string);
585 TEST_ASSERT(cpu_list, "strdup() allocation failed.");
587 r = sched_getaffinity(0, sizeof(allowed_mask), &allowed_mask);
588 TEST_ASSERT(!r, "sched_getaffinity() failed");
590 cpu = strtok(cpu_list, delim);
592 /* 1. Get all pcpus for vcpus. */
593 for (i = 0; i < nr_vcpus; i++) {
594 TEST_ASSERT(cpu, "pCPU not provided for vCPU '%d'", i);
595 vcpu_to_pcpu[i] = parse_pcpu(cpu, &allowed_mask);
596 cpu = strtok(NULL, delim);
599 /* 2. Check if the main worker needs to be pinned. */
601 kvm_pin_this_task_to_pcpu(parse_pcpu(cpu, &allowed_mask));
602 cpu = strtok(NULL, delim);
605 TEST_ASSERT(!cpu, "pCPU list contains trailing garbage characters '%s'", cpu);
610 * Userspace Memory Region Find
613 * vm - Virtual Machine
614 * start - Starting VM physical address
615 * end - Ending VM physical address, inclusive.
620 * Pointer to overlapping region, NULL if no such region.
622 * Searches for a region with any physical memory that overlaps with
623 * any portion of the guest physical addresses from start to end
624 * inclusive. If multiple overlapping regions exist, a pointer to any
625 * of the regions is returned. Null is returned only when no overlapping
628 static struct userspace_mem_region *
629 userspace_mem_region_find(struct kvm_vm *vm, uint64_t start, uint64_t end)
631 struct rb_node *node;
633 for (node = vm->regions.gpa_tree.rb_node; node; ) {
634 struct userspace_mem_region *region =
635 container_of(node, struct userspace_mem_region, gpa_node);
636 uint64_t existing_start = region->region.guest_phys_addr;
637 uint64_t existing_end = region->region.guest_phys_addr
638 + region->region.memory_size - 1;
639 if (start <= existing_end && end >= existing_start)
642 if (start < existing_start)
643 node = node->rb_left;
645 node = node->rb_right;
651 __weak void vcpu_arch_free(struct kvm_vcpu *vcpu)
660 * vcpu - VCPU to remove
664 * Return: None, TEST_ASSERT failures for all error conditions
666 * Removes a vCPU from a VM and frees its resources.
668 static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
672 if (vcpu->dirty_gfns) {
673 ret = munmap(vcpu->dirty_gfns, vm->dirty_ring_size);
674 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
675 vcpu->dirty_gfns = NULL;
678 ret = munmap(vcpu->run, vcpu_mmap_sz());
679 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
681 ret = close(vcpu->fd);
682 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
684 list_del(&vcpu->list);
686 vcpu_arch_free(vcpu);
690 void kvm_vm_release(struct kvm_vm *vmp)
692 struct kvm_vcpu *vcpu, *tmp;
695 list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list)
696 vm_vcpu_rm(vmp, vcpu);
698 ret = close(vmp->fd);
699 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
701 ret = close(vmp->kvm_fd);
702 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("close()", ret));
705 static void __vm_mem_region_delete(struct kvm_vm *vm,
706 struct userspace_mem_region *region,
712 rb_erase(®ion->gpa_node, &vm->regions.gpa_tree);
713 rb_erase(®ion->hva_node, &vm->regions.hva_tree);
714 hash_del(®ion->slot_node);
717 region->region.memory_size = 0;
718 vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
720 sparsebit_free(®ion->unused_phy_pages);
721 sparsebit_free(®ion->protected_phy_pages);
722 ret = munmap(region->mmap_start, region->mmap_size);
723 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
724 if (region->fd >= 0) {
725 /* There's an extra map when using shared memory. */
726 ret = munmap(region->mmap_alias, region->mmap_size);
727 TEST_ASSERT(!ret, __KVM_SYSCALL_ERROR("munmap()", ret));
730 if (region->region.guest_memfd >= 0)
731 close(region->region.guest_memfd);
737 * Destroys and frees the VM pointed to by vmp.
739 void kvm_vm_free(struct kvm_vm *vmp)
742 struct hlist_node *node;
743 struct userspace_mem_region *region;
748 /* Free cached stats metadata and close FD */
750 free(vmp->stats_desc);
751 close(vmp->stats_fd);
754 /* Free userspace_mem_regions. */
755 hash_for_each_safe(vmp->regions.slot_hash, ctr, node, region, slot_node)
756 __vm_mem_region_delete(vmp, region, false);
758 /* Free sparsebit arrays. */
759 sparsebit_free(&vmp->vpages_valid);
760 sparsebit_free(&vmp->vpages_mapped);
764 /* Free the structure describing the VM. */
768 int kvm_memfd_alloc(size_t size, bool hugepages)
770 int memfd_flags = MFD_CLOEXEC;
774 memfd_flags |= MFD_HUGETLB;
776 fd = memfd_create("kvm_selftest", memfd_flags);
777 TEST_ASSERT(fd != -1, __KVM_SYSCALL_ERROR("memfd_create()", fd));
779 r = ftruncate(fd, size);
780 TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("ftruncate()", r));
782 r = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, size);
783 TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("fallocate()", r));
789 * Memory Compare, host virtual to guest virtual
792 * hva - Starting host virtual address
793 * vm - Virtual Machine
794 * gva - Starting guest virtual address
795 * len - number of bytes to compare
799 * Input/Output Args: None
802 * Returns 0 if the bytes starting at hva for a length of len
803 * are equal the guest virtual bytes starting at gva. Returns
804 * a value < 0, if bytes at hva are less than those at gva.
805 * Otherwise a value > 0 is returned.
807 * Compares the bytes starting at the host virtual address hva, for
808 * a length of len, to the guest bytes starting at the guest virtual
809 * address given by gva.
811 int kvm_memcmp_hva_gva(void *hva, struct kvm_vm *vm, vm_vaddr_t gva, size_t len)
816 * Compare a batch of bytes until either a match is found
817 * or all the bytes have been compared.
819 for (uintptr_t offset = 0; offset < len; offset += amt) {
820 uintptr_t ptr1 = (uintptr_t)hva + offset;
823 * Determine host address for guest virtual address
826 uintptr_t ptr2 = (uintptr_t)addr_gva2hva(vm, gva + offset);
829 * Determine amount to compare on this pass.
830 * Don't allow the comparsion to cross a page boundary.
833 if ((ptr1 >> vm->page_shift) != ((ptr1 + amt) >> vm->page_shift))
834 amt = vm->page_size - (ptr1 % vm->page_size);
835 if ((ptr2 >> vm->page_shift) != ((ptr2 + amt) >> vm->page_shift))
836 amt = vm->page_size - (ptr2 % vm->page_size);
838 assert((ptr1 >> vm->page_shift) == ((ptr1 + amt - 1) >> vm->page_shift));
839 assert((ptr2 >> vm->page_shift) == ((ptr2 + amt - 1) >> vm->page_shift));
842 * Perform the comparison. If there is a difference
843 * return that result to the caller, otherwise need
844 * to continue on looking for a mismatch.
846 int ret = memcmp((void *)ptr1, (void *)ptr2, amt);
852 * No mismatch found. Let the caller know the two memory
858 static void vm_userspace_mem_region_gpa_insert(struct rb_root *gpa_tree,
859 struct userspace_mem_region *region)
861 struct rb_node **cur, *parent;
863 for (cur = &gpa_tree->rb_node, parent = NULL; *cur; ) {
864 struct userspace_mem_region *cregion;
866 cregion = container_of(*cur, typeof(*cregion), gpa_node);
868 if (region->region.guest_phys_addr <
869 cregion->region.guest_phys_addr)
870 cur = &(*cur)->rb_left;
872 TEST_ASSERT(region->region.guest_phys_addr !=
873 cregion->region.guest_phys_addr,
874 "Duplicate GPA in region tree");
876 cur = &(*cur)->rb_right;
880 rb_link_node(®ion->gpa_node, parent, cur);
881 rb_insert_color(®ion->gpa_node, gpa_tree);
884 static void vm_userspace_mem_region_hva_insert(struct rb_root *hva_tree,
885 struct userspace_mem_region *region)
887 struct rb_node **cur, *parent;
889 for (cur = &hva_tree->rb_node, parent = NULL; *cur; ) {
890 struct userspace_mem_region *cregion;
892 cregion = container_of(*cur, typeof(*cregion), hva_node);
894 if (region->host_mem < cregion->host_mem)
895 cur = &(*cur)->rb_left;
897 TEST_ASSERT(region->host_mem !=
899 "Duplicate HVA in region tree");
901 cur = &(*cur)->rb_right;
905 rb_link_node(®ion->hva_node, parent, cur);
906 rb_insert_color(®ion->hva_node, hva_tree);
910 int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
911 uint64_t gpa, uint64_t size, void *hva)
913 struct kvm_userspace_memory_region region = {
916 .guest_phys_addr = gpa,
918 .userspace_addr = (uintptr_t)hva,
921 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION, ®ion);
924 void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
925 uint64_t gpa, uint64_t size, void *hva)
927 int ret = __vm_set_user_memory_region(vm, slot, flags, gpa, size, hva);
929 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION failed, errno = %d (%s)",
930 errno, strerror(errno));
933 int __vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
934 uint64_t gpa, uint64_t size, void *hva,
935 uint32_t guest_memfd, uint64_t guest_memfd_offset)
937 struct kvm_userspace_memory_region2 region = {
940 .guest_phys_addr = gpa,
942 .userspace_addr = (uintptr_t)hva,
943 .guest_memfd = guest_memfd,
944 .guest_memfd_offset = guest_memfd_offset,
947 return ioctl(vm->fd, KVM_SET_USER_MEMORY_REGION2, ®ion);
950 void vm_set_user_memory_region2(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
951 uint64_t gpa, uint64_t size, void *hva,
952 uint32_t guest_memfd, uint64_t guest_memfd_offset)
954 int ret = __vm_set_user_memory_region2(vm, slot, flags, gpa, size, hva,
955 guest_memfd, guest_memfd_offset);
957 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed, errno = %d (%s)",
958 errno, strerror(errno));
962 /* FIXME: This thing needs to be ripped apart and rewritten. */
963 void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type,
964 uint64_t guest_paddr, uint32_t slot, uint64_t npages,
965 uint32_t flags, int guest_memfd, uint64_t guest_memfd_offset)
968 struct userspace_mem_region *region;
969 size_t backing_src_pagesz = get_backing_src_pagesz(src_type);
970 size_t mem_size = npages * vm->page_size;
973 TEST_ASSERT(vm_adjust_num_guest_pages(vm->mode, npages) == npages,
974 "Number of guest pages is not compatible with the host. "
975 "Try npages=%d", vm_adjust_num_guest_pages(vm->mode, npages));
977 TEST_ASSERT((guest_paddr % vm->page_size) == 0, "Guest physical "
978 "address not on a page boundary.\n"
979 " guest_paddr: 0x%lx vm->page_size: 0x%x",
980 guest_paddr, vm->page_size);
981 TEST_ASSERT((((guest_paddr >> vm->page_shift) + npages) - 1)
982 <= vm->max_gfn, "Physical range beyond maximum "
983 "supported physical address,\n"
984 " guest_paddr: 0x%lx npages: 0x%lx\n"
985 " vm->max_gfn: 0x%lx vm->page_size: 0x%x",
986 guest_paddr, npages, vm->max_gfn, vm->page_size);
989 * Confirm a mem region with an overlapping address doesn't
992 region = (struct userspace_mem_region *) userspace_mem_region_find(
993 vm, guest_paddr, (guest_paddr + npages * vm->page_size) - 1);
995 TEST_FAIL("overlapping userspace_mem_region already "
997 " requested guest_paddr: 0x%lx npages: 0x%lx "
999 " existing guest_paddr: 0x%lx size: 0x%lx",
1000 guest_paddr, npages, vm->page_size,
1001 (uint64_t) region->region.guest_phys_addr,
1002 (uint64_t) region->region.memory_size);
1004 /* Confirm no region with the requested slot already exists. */
1005 hash_for_each_possible(vm->regions.slot_hash, region, slot_node,
1007 if (region->region.slot != slot)
1010 TEST_FAIL("A mem region with the requested slot "
1012 " requested slot: %u paddr: 0x%lx npages: 0x%lx\n"
1013 " existing slot: %u paddr: 0x%lx size: 0x%lx",
1014 slot, guest_paddr, npages,
1015 region->region.slot,
1016 (uint64_t) region->region.guest_phys_addr,
1017 (uint64_t) region->region.memory_size);
1020 /* Allocate and initialize new mem region structure. */
1021 region = calloc(1, sizeof(*region));
1022 TEST_ASSERT(region != NULL, "Insufficient Memory");
1023 region->mmap_size = mem_size;
1026 /* On s390x, the host address must be aligned to 1M (due to PGSTEs) */
1027 alignment = 0x100000;
1033 * When using THP mmap is not guaranteed to returned a hugepage aligned
1034 * address so we have to pad the mmap. Padding is not needed for HugeTLB
1035 * because mmap will always return an address aligned to the HugeTLB
1038 if (src_type == VM_MEM_SRC_ANONYMOUS_THP)
1039 alignment = max(backing_src_pagesz, alignment);
1041 TEST_ASSERT_EQ(guest_paddr, align_up(guest_paddr, backing_src_pagesz));
1043 /* Add enough memory to align up if necessary */
1045 region->mmap_size += alignment;
1048 if (backing_src_is_shared(src_type))
1049 region->fd = kvm_memfd_alloc(region->mmap_size,
1050 src_type == VM_MEM_SRC_SHARED_HUGETLB);
1052 region->mmap_start = mmap(NULL, region->mmap_size,
1053 PROT_READ | PROT_WRITE,
1054 vm_mem_backing_src_alias(src_type)->flag,
1056 TEST_ASSERT(region->mmap_start != MAP_FAILED,
1057 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1059 TEST_ASSERT(!is_backing_src_hugetlb(src_type) ||
1060 region->mmap_start == align_ptr_up(region->mmap_start, backing_src_pagesz),
1061 "mmap_start %p is not aligned to HugeTLB page size 0x%lx",
1062 region->mmap_start, backing_src_pagesz);
1064 /* Align host address */
1065 region->host_mem = align_ptr_up(region->mmap_start, alignment);
1067 /* As needed perform madvise */
1068 if ((src_type == VM_MEM_SRC_ANONYMOUS ||
1069 src_type == VM_MEM_SRC_ANONYMOUS_THP) && thp_configured()) {
1070 ret = madvise(region->host_mem, mem_size,
1071 src_type == VM_MEM_SRC_ANONYMOUS ? MADV_NOHUGEPAGE : MADV_HUGEPAGE);
1072 TEST_ASSERT(ret == 0, "madvise failed, addr: %p length: 0x%lx src_type: %s",
1073 region->host_mem, mem_size,
1074 vm_mem_backing_src_alias(src_type)->name);
1077 region->backing_src_type = src_type;
1079 if (flags & KVM_MEM_GUEST_MEMFD) {
1080 if (guest_memfd < 0) {
1081 uint32_t guest_memfd_flags = 0;
1082 TEST_ASSERT(!guest_memfd_offset,
1083 "Offset must be zero when creating new guest_memfd");
1084 guest_memfd = vm_create_guest_memfd(vm, mem_size, guest_memfd_flags);
1087 * Install a unique fd for each memslot so that the fd
1088 * can be closed when the region is deleted without
1089 * needing to track if the fd is owned by the framework
1092 guest_memfd = dup(guest_memfd);
1093 TEST_ASSERT(guest_memfd >= 0, __KVM_SYSCALL_ERROR("dup()", guest_memfd));
1096 region->region.guest_memfd = guest_memfd;
1097 region->region.guest_memfd_offset = guest_memfd_offset;
1099 region->region.guest_memfd = -1;
1102 region->unused_phy_pages = sparsebit_alloc();
1103 if (vm_arch_has_protected_memory(vm))
1104 region->protected_phy_pages = sparsebit_alloc();
1105 sparsebit_set_num(region->unused_phy_pages,
1106 guest_paddr >> vm->page_shift, npages);
1107 region->region.slot = slot;
1108 region->region.flags = flags;
1109 region->region.guest_phys_addr = guest_paddr;
1110 region->region.memory_size = npages * vm->page_size;
1111 region->region.userspace_addr = (uintptr_t) region->host_mem;
1112 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1113 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
1114 " rc: %i errno: %i\n"
1115 " slot: %u flags: 0x%x\n"
1116 " guest_phys_addr: 0x%lx size: 0x%lx guest_memfd: %d",
1117 ret, errno, slot, flags,
1118 guest_paddr, (uint64_t) region->region.memory_size,
1119 region->region.guest_memfd);
1121 /* Add to quick lookup data structures */
1122 vm_userspace_mem_region_gpa_insert(&vm->regions.gpa_tree, region);
1123 vm_userspace_mem_region_hva_insert(&vm->regions.hva_tree, region);
1124 hash_add(vm->regions.slot_hash, ®ion->slot_node, slot);
1126 /* If shared memory, create an alias. */
1127 if (region->fd >= 0) {
1128 region->mmap_alias = mmap(NULL, region->mmap_size,
1129 PROT_READ | PROT_WRITE,
1130 vm_mem_backing_src_alias(src_type)->flag,
1132 TEST_ASSERT(region->mmap_alias != MAP_FAILED,
1133 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1135 /* Align host alias address */
1136 region->host_alias = align_ptr_up(region->mmap_alias, alignment);
1140 void vm_userspace_mem_region_add(struct kvm_vm *vm,
1141 enum vm_mem_backing_src_type src_type,
1142 uint64_t guest_paddr, uint32_t slot,
1143 uint64_t npages, uint32_t flags)
1145 vm_mem_add(vm, src_type, guest_paddr, slot, npages, flags, -1, 0);
1152 * vm - Virtual Machine
1153 * memslot - KVM memory slot ID
1158 * Pointer to memory region structure that describe memory region
1159 * using kvm memory slot ID given by memslot. TEST_ASSERT failure
1160 * on error (e.g. currently no memory region using memslot as a KVM
1163 struct userspace_mem_region *
1164 memslot2region(struct kvm_vm *vm, uint32_t memslot)
1166 struct userspace_mem_region *region;
1168 hash_for_each_possible(vm->regions.slot_hash, region, slot_node,
1170 if (region->region.slot == memslot)
1173 fprintf(stderr, "No mem region with the requested slot found,\n"
1174 " requested slot: %u\n", memslot);
1175 fputs("---- vm dump ----\n", stderr);
1176 vm_dump(stderr, vm, 2);
1177 TEST_FAIL("Mem region not found");
1182 * VM Memory Region Flags Set
1185 * vm - Virtual Machine
1186 * flags - Starting guest physical address
1192 * Sets the flags of the memory region specified by the value of slot,
1193 * to the values given by flags.
1195 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags)
1198 struct userspace_mem_region *region;
1200 region = memslot2region(vm, slot);
1202 region->region.flags = flags;
1204 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1206 TEST_ASSERT(ret == 0, "KVM_SET_USER_MEMORY_REGION2 IOCTL failed,\n"
1207 " rc: %i errno: %i slot: %u flags: 0x%x",
1208 ret, errno, slot, flags);
1212 * VM Memory Region Move
1215 * vm - Virtual Machine
1216 * slot - Slot of the memory region to move
1217 * new_gpa - Starting guest physical address
1223 * Change the gpa of a memory region.
1225 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa)
1227 struct userspace_mem_region *region;
1230 region = memslot2region(vm, slot);
1232 region->region.guest_phys_addr = new_gpa;
1234 ret = __vm_ioctl(vm, KVM_SET_USER_MEMORY_REGION2, ®ion->region);
1236 TEST_ASSERT(!ret, "KVM_SET_USER_MEMORY_REGION2 failed\n"
1237 "ret: %i errno: %i slot: %u new_gpa: 0x%lx",
1238 ret, errno, slot, new_gpa);
1242 * VM Memory Region Delete
1245 * vm - Virtual Machine
1246 * slot - Slot of the memory region to delete
1252 * Delete a memory region.
1254 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot)
1256 __vm_mem_region_delete(vm, memslot2region(vm, slot), true);
1259 void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size,
1262 const int mode = FALLOC_FL_KEEP_SIZE | (punch_hole ? FALLOC_FL_PUNCH_HOLE : 0);
1263 struct userspace_mem_region *region;
1264 uint64_t end = base + size;
1269 for (gpa = base; gpa < end; gpa += len) {
1272 region = userspace_mem_region_find(vm, gpa, gpa);
1273 TEST_ASSERT(region && region->region.flags & KVM_MEM_GUEST_MEMFD,
1274 "Private memory region not found for GPA 0x%lx", gpa);
1276 offset = gpa - region->region.guest_phys_addr;
1277 fd_offset = region->region.guest_memfd_offset + offset;
1278 len = min_t(uint64_t, end - gpa, region->region.memory_size - offset);
1280 ret = fallocate(region->region.guest_memfd, mode, fd_offset, len);
1281 TEST_ASSERT(!ret, "fallocate() failed to %s at %lx (len = %lu), fd = %d, mode = %x, offset = %lx",
1282 punch_hole ? "punch hole" : "allocate", gpa, len,
1283 region->region.guest_memfd, mode, fd_offset);
1287 /* Returns the size of a vCPU's kvm_run structure. */
1288 static int vcpu_mmap_sz(void)
1292 dev_fd = open_kvm_dev_path_or_exit();
1294 ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
1295 TEST_ASSERT(ret >= sizeof(struct kvm_run),
1296 KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
1303 static bool vcpu_exists(struct kvm_vm *vm, uint32_t vcpu_id)
1305 struct kvm_vcpu *vcpu;
1307 list_for_each_entry(vcpu, &vm->vcpus, list) {
1308 if (vcpu->id == vcpu_id)
1316 * Adds a virtual CPU to the VM specified by vm with the ID given by vcpu_id.
1317 * No additional vCPU setup is done. Returns the vCPU.
1319 struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
1321 struct kvm_vcpu *vcpu;
1323 /* Confirm a vcpu with the specified id doesn't already exist. */
1324 TEST_ASSERT(!vcpu_exists(vm, vcpu_id), "vCPU%d already exists", vcpu_id);
1326 /* Allocate and initialize new vcpu structure. */
1327 vcpu = calloc(1, sizeof(*vcpu));
1328 TEST_ASSERT(vcpu != NULL, "Insufficient Memory");
1332 vcpu->fd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu_id);
1333 TEST_ASSERT_VM_VCPU_IOCTL(vcpu->fd >= 0, KVM_CREATE_VCPU, vcpu->fd, vm);
1335 TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size "
1336 "smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
1337 vcpu_mmap_sz(), sizeof(*vcpu->run));
1338 vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(),
1339 PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
1340 TEST_ASSERT(vcpu->run != MAP_FAILED,
1341 __KVM_SYSCALL_ERROR("mmap()", (int)(unsigned long)MAP_FAILED));
1343 /* Add to linked-list of VCPUs. */
1344 list_add(&vcpu->list, &vm->vcpus);
1350 * VM Virtual Address Unused Gap
1353 * vm - Virtual Machine
1355 * vaddr_min - Minimum Virtual Address
1360 * Lowest virtual address at or below vaddr_min, with at least
1361 * sz unused bytes. TEST_ASSERT failure if no area of at least
1362 * size sz is available.
1364 * Within the VM specified by vm, locates the lowest starting virtual
1365 * address >= vaddr_min, that has at least sz unallocated bytes. A
1366 * TEST_ASSERT failure occurs for invalid input or no area of at least
1367 * sz unallocated bytes >= vaddr_min is available.
1369 vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,
1370 vm_vaddr_t vaddr_min)
1372 uint64_t pages = (sz + vm->page_size - 1) >> vm->page_shift;
1374 /* Determine lowest permitted virtual page index. */
1375 uint64_t pgidx_start = (vaddr_min + vm->page_size - 1) >> vm->page_shift;
1376 if ((pgidx_start * vm->page_size) < vaddr_min)
1379 /* Loop over section with enough valid virtual page indexes. */
1380 if (!sparsebit_is_set_num(vm->vpages_valid,
1381 pgidx_start, pages))
1382 pgidx_start = sparsebit_next_set_num(vm->vpages_valid,
1383 pgidx_start, pages);
1386 * Are there enough unused virtual pages available at
1387 * the currently proposed starting virtual page index.
1388 * If not, adjust proposed starting index to next
1391 if (sparsebit_is_clear_num(vm->vpages_mapped,
1392 pgidx_start, pages))
1394 pgidx_start = sparsebit_next_clear_num(vm->vpages_mapped,
1395 pgidx_start, pages);
1396 if (pgidx_start == 0)
1400 * If needed, adjust proposed starting virtual address,
1401 * to next range of valid virtual addresses.
1403 if (!sparsebit_is_set_num(vm->vpages_valid,
1404 pgidx_start, pages)) {
1405 pgidx_start = sparsebit_next_set_num(
1406 vm->vpages_valid, pgidx_start, pages);
1407 if (pgidx_start == 0)
1410 } while (pgidx_start != 0);
1413 TEST_FAIL("No vaddr of specified pages available, pages: 0x%lx", pages);
1419 TEST_ASSERT(sparsebit_is_set_num(vm->vpages_valid,
1420 pgidx_start, pages),
1421 "Unexpected, invalid virtual page index range,\n"
1422 " pgidx_start: 0x%lx\n"
1424 pgidx_start, pages);
1425 TEST_ASSERT(sparsebit_is_clear_num(vm->vpages_mapped,
1426 pgidx_start, pages),
1427 "Unexpected, pages already mapped,\n"
1428 " pgidx_start: 0x%lx\n"
1430 pgidx_start, pages);
1432 return pgidx_start * vm->page_size;
1435 static vm_vaddr_t ____vm_vaddr_alloc(struct kvm_vm *vm, size_t sz,
1436 vm_vaddr_t vaddr_min,
1437 enum kvm_mem_region_type type,
1440 uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) != 0);
1443 vm_paddr_t paddr = __vm_phy_pages_alloc(vm, pages,
1444 KVM_UTIL_MIN_PFN * vm->page_size,
1445 vm->memslots[type], protected);
1448 * Find an unused range of virtual page addresses of at least
1451 vm_vaddr_t vaddr_start = vm_vaddr_unused_gap(vm, sz, vaddr_min);
1453 /* Map the virtual pages. */
1454 for (vm_vaddr_t vaddr = vaddr_start; pages > 0;
1455 pages--, vaddr += vm->page_size, paddr += vm->page_size) {
1457 virt_pg_map(vm, vaddr, paddr);
1459 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
1465 vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
1466 enum kvm_mem_region_type type)
1468 return ____vm_vaddr_alloc(vm, sz, vaddr_min, type,
1469 vm_arch_has_protected_memory(vm));
1472 vm_vaddr_t vm_vaddr_alloc_shared(struct kvm_vm *vm, size_t sz,
1473 vm_vaddr_t vaddr_min,
1474 enum kvm_mem_region_type type)
1476 return ____vm_vaddr_alloc(vm, sz, vaddr_min, type, false);
1480 * VM Virtual Address Allocate
1483 * vm - Virtual Machine
1484 * sz - Size in bytes
1485 * vaddr_min - Minimum starting virtual address
1490 * Starting guest virtual address
1492 * Allocates at least sz bytes within the virtual address space of the vm
1493 * given by vm. The allocated bytes are mapped to a virtual address >=
1494 * the address given by vaddr_min. Note that each allocation uses a
1495 * a unique set of pages, with the minimum real allocation being at least
1496 * a page. The allocated physical space comes from the TEST_DATA memory region.
1498 vm_vaddr_t vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min)
1500 return __vm_vaddr_alloc(vm, sz, vaddr_min, MEM_REGION_TEST_DATA);
1504 * VM Virtual Address Allocate Pages
1507 * vm - Virtual Machine
1512 * Starting guest virtual address
1514 * Allocates at least N system pages worth of bytes within the virtual address
1517 vm_vaddr_t vm_vaddr_alloc_pages(struct kvm_vm *vm, int nr_pages)
1519 return vm_vaddr_alloc(vm, nr_pages * getpagesize(), KVM_UTIL_MIN_VADDR);
1522 vm_vaddr_t __vm_vaddr_alloc_page(struct kvm_vm *vm, enum kvm_mem_region_type type)
1524 return __vm_vaddr_alloc(vm, getpagesize(), KVM_UTIL_MIN_VADDR, type);
1528 * VM Virtual Address Allocate Page
1531 * vm - Virtual Machine
1536 * Starting guest virtual address
1538 * Allocates at least one system page worth of bytes within the virtual address
1541 vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm)
1543 return vm_vaddr_alloc_pages(vm, 1);
1547 * Map a range of VM virtual address to the VM's physical address
1550 * vm - Virtual Machine
1551 * vaddr - Virtuall address to map
1552 * paddr - VM Physical Address
1553 * npages - The number of pages to map
1559 * Within the VM given by @vm, creates a virtual translation for
1560 * @npages starting at @vaddr to the page range starting at @paddr.
1562 void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,
1563 unsigned int npages)
1565 size_t page_size = vm->page_size;
1566 size_t size = npages * page_size;
1568 TEST_ASSERT(vaddr + size > vaddr, "Vaddr overflow");
1569 TEST_ASSERT(paddr + size > paddr, "Paddr overflow");
1572 virt_pg_map(vm, vaddr, paddr);
1573 sparsebit_set(vm->vpages_mapped, vaddr >> vm->page_shift);
1581 * Address VM Physical to Host Virtual
1584 * vm - Virtual Machine
1585 * gpa - VM physical address
1590 * Equivalent host virtual address
1592 * Locates the memory region containing the VM physical address given
1593 * by gpa, within the VM given by vm. When found, the host virtual
1594 * address providing the memory to the vm physical address is returned.
1595 * A TEST_ASSERT failure occurs if no region containing gpa exists.
1597 void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa)
1599 struct userspace_mem_region *region;
1601 gpa = vm_untag_gpa(vm, gpa);
1603 region = userspace_mem_region_find(vm, gpa, gpa);
1605 TEST_FAIL("No vm physical memory at 0x%lx", gpa);
1609 return (void *)((uintptr_t)region->host_mem
1610 + (gpa - region->region.guest_phys_addr));
1614 * Address Host Virtual to VM Physical
1617 * vm - Virtual Machine
1618 * hva - Host virtual address
1623 * Equivalent VM physical address
1625 * Locates the memory region containing the host virtual address given
1626 * by hva, within the VM given by vm. When found, the equivalent
1627 * VM physical address is returned. A TEST_ASSERT failure occurs if no
1628 * region containing hva exists.
1630 vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva)
1632 struct rb_node *node;
1634 for (node = vm->regions.hva_tree.rb_node; node; ) {
1635 struct userspace_mem_region *region =
1636 container_of(node, struct userspace_mem_region, hva_node);
1638 if (hva >= region->host_mem) {
1639 if (hva <= (region->host_mem
1640 + region->region.memory_size - 1))
1641 return (vm_paddr_t)((uintptr_t)
1642 region->region.guest_phys_addr
1643 + (hva - (uintptr_t)region->host_mem));
1645 node = node->rb_right;
1647 node = node->rb_left;
1650 TEST_FAIL("No mapping to a guest physical address, hva: %p", hva);
1655 * Address VM physical to Host Virtual *alias*.
1658 * vm - Virtual Machine
1659 * gpa - VM physical address
1664 * Equivalent address within the host virtual *alias* area, or NULL
1665 * (without failing the test) if the guest memory is not shared (so
1668 * Create a writable, shared virtual=>physical alias for the specific GPA.
1669 * The primary use case is to allow the host selftest to manipulate guest
1670 * memory without mapping said memory in the guest's address space. And, for
1671 * userfaultfd-based demand paging, to do so without triggering userfaults.
1673 void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)
1675 struct userspace_mem_region *region;
1678 region = userspace_mem_region_find(vm, gpa, gpa);
1682 if (!region->host_alias)
1685 offset = gpa - region->region.guest_phys_addr;
1686 return (void *) ((uintptr_t) region->host_alias + offset);
1689 /* Create an interrupt controller chip for the specified VM. */
1690 void vm_create_irqchip(struct kvm_vm *vm)
1692 vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
1694 vm->has_irqchip = true;
1697 int _vcpu_run(struct kvm_vcpu *vcpu)
1702 rc = __vcpu_run(vcpu);
1703 } while (rc == -1 && errno == EINTR);
1705 assert_on_unhandled_exception(vcpu);
1711 * Invoke KVM_RUN on a vCPU until KVM returns something other than -EINTR.
1712 * Assert if the KVM returns an error (other than -EINTR).
1714 void vcpu_run(struct kvm_vcpu *vcpu)
1716 int ret = _vcpu_run(vcpu);
1718 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_RUN, ret));
1721 void vcpu_run_complete_io(struct kvm_vcpu *vcpu)
1725 vcpu->run->immediate_exit = 1;
1726 ret = __vcpu_run(vcpu);
1727 vcpu->run->immediate_exit = 0;
1729 TEST_ASSERT(ret == -1 && errno == EINTR,
1730 "KVM_RUN IOCTL didn't exit immediately, rc: %i, errno: %i",
1735 * Get the list of guest registers which are supported for
1736 * KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls. Returns a kvm_reg_list pointer,
1737 * it is the caller's responsibility to free the list.
1739 struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu)
1741 struct kvm_reg_list reg_list_n = { .n = 0 }, *reg_list;
1744 ret = __vcpu_ioctl(vcpu, KVM_GET_REG_LIST, ®_list_n);
1745 TEST_ASSERT(ret == -1 && errno == E2BIG, "KVM_GET_REG_LIST n=0");
1747 reg_list = calloc(1, sizeof(*reg_list) + reg_list_n.n * sizeof(__u64));
1748 reg_list->n = reg_list_n.n;
1749 vcpu_ioctl(vcpu, KVM_GET_REG_LIST, reg_list);
1753 void *vcpu_map_dirty_ring(struct kvm_vcpu *vcpu)
1755 uint32_t page_size = getpagesize();
1756 uint32_t size = vcpu->vm->dirty_ring_size;
1758 TEST_ASSERT(size > 0, "Should enable dirty ring first");
1760 if (!vcpu->dirty_gfns) {
1763 addr = mmap(NULL, size, PROT_READ, MAP_PRIVATE, vcpu->fd,
1764 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1765 TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped private");
1767 addr = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_PRIVATE, vcpu->fd,
1768 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1769 TEST_ASSERT(addr == MAP_FAILED, "Dirty ring mapped exec");
1771 addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd,
1772 page_size * KVM_DIRTY_LOG_PAGE_OFFSET);
1773 TEST_ASSERT(addr != MAP_FAILED, "Dirty ring map failed");
1775 vcpu->dirty_gfns = addr;
1776 vcpu->dirty_gfns_count = size / sizeof(struct kvm_dirty_gfn);
1779 return vcpu->dirty_gfns;
1786 int __kvm_has_device_attr(int dev_fd, uint32_t group, uint64_t attr)
1788 struct kvm_device_attr attribute = {
1794 return ioctl(dev_fd, KVM_HAS_DEVICE_ATTR, &attribute);
1797 int __kvm_test_create_device(struct kvm_vm *vm, uint64_t type)
1799 struct kvm_create_device create_dev = {
1801 .flags = KVM_CREATE_DEVICE_TEST,
1804 return __vm_ioctl(vm, KVM_CREATE_DEVICE, &create_dev);
1807 int __kvm_create_device(struct kvm_vm *vm, uint64_t type)
1809 struct kvm_create_device create_dev = {
1816 err = __vm_ioctl(vm, KVM_CREATE_DEVICE, &create_dev);
1817 TEST_ASSERT(err <= 0, "KVM_CREATE_DEVICE shouldn't return a positive value");
1818 return err ? : create_dev.fd;
1821 int __kvm_device_attr_get(int dev_fd, uint32_t group, uint64_t attr, void *val)
1823 struct kvm_device_attr kvmattr = {
1827 .addr = (uintptr_t)val,
1830 return __kvm_ioctl(dev_fd, KVM_GET_DEVICE_ATTR, &kvmattr);
1833 int __kvm_device_attr_set(int dev_fd, uint32_t group, uint64_t attr, void *val)
1835 struct kvm_device_attr kvmattr = {
1839 .addr = (uintptr_t)val,
1842 return __kvm_ioctl(dev_fd, KVM_SET_DEVICE_ATTR, &kvmattr);
1846 * IRQ related functions.
1849 int _kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)
1851 struct kvm_irq_level irq_level = {
1856 return __vm_ioctl(vm, KVM_IRQ_LINE, &irq_level);
1859 void kvm_irq_line(struct kvm_vm *vm, uint32_t irq, int level)
1861 int ret = _kvm_irq_line(vm, irq, level);
1863 TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_IRQ_LINE, ret));
1866 struct kvm_irq_routing *kvm_gsi_routing_create(void)
1868 struct kvm_irq_routing *routing;
1871 size = sizeof(struct kvm_irq_routing);
1872 /* Allocate space for the max number of entries: this wastes 196 KBs. */
1873 size += KVM_MAX_IRQ_ROUTES * sizeof(struct kvm_irq_routing_entry);
1874 routing = calloc(1, size);
1880 void kvm_gsi_routing_irqchip_add(struct kvm_irq_routing *routing,
1881 uint32_t gsi, uint32_t pin)
1886 assert(routing->nr < KVM_MAX_IRQ_ROUTES);
1889 routing->entries[i].gsi = gsi;
1890 routing->entries[i].type = KVM_IRQ_ROUTING_IRQCHIP;
1891 routing->entries[i].flags = 0;
1892 routing->entries[i].u.irqchip.irqchip = 0;
1893 routing->entries[i].u.irqchip.pin = pin;
1897 int _kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)
1902 ret = __vm_ioctl(vm, KVM_SET_GSI_ROUTING, routing);
1908 void kvm_gsi_routing_write(struct kvm_vm *vm, struct kvm_irq_routing *routing)
1912 ret = _kvm_gsi_routing_write(vm, routing);
1913 TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_GSI_ROUTING, ret));
1920 * vm - Virtual Machine
1921 * indent - Left margin indent amount
1924 * stream - Output FILE stream
1928 * Dumps the current state of the VM given by vm, to the FILE stream
1931 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent)
1934 struct userspace_mem_region *region;
1935 struct kvm_vcpu *vcpu;
1937 fprintf(stream, "%*smode: 0x%x\n", indent, "", vm->mode);
1938 fprintf(stream, "%*sfd: %i\n", indent, "", vm->fd);
1939 fprintf(stream, "%*spage_size: 0x%x\n", indent, "", vm->page_size);
1940 fprintf(stream, "%*sMem Regions:\n", indent, "");
1941 hash_for_each(vm->regions.slot_hash, ctr, region, slot_node) {
1942 fprintf(stream, "%*sguest_phys: 0x%lx size: 0x%lx "
1943 "host_virt: %p\n", indent + 2, "",
1944 (uint64_t) region->region.guest_phys_addr,
1945 (uint64_t) region->region.memory_size,
1947 fprintf(stream, "%*sunused_phy_pages: ", indent + 2, "");
1948 sparsebit_dump(stream, region->unused_phy_pages, 0);
1949 if (region->protected_phy_pages) {
1950 fprintf(stream, "%*sprotected_phy_pages: ", indent + 2, "");
1951 sparsebit_dump(stream, region->protected_phy_pages, 0);
1954 fprintf(stream, "%*sMapped Virtual Pages:\n", indent, "");
1955 sparsebit_dump(stream, vm->vpages_mapped, indent + 2);
1956 fprintf(stream, "%*spgd_created: %u\n", indent, "",
1958 if (vm->pgd_created) {
1959 fprintf(stream, "%*sVirtual Translation Tables:\n",
1961 virt_dump(stream, vm, indent + 4);
1963 fprintf(stream, "%*sVCPUs:\n", indent, "");
1965 list_for_each_entry(vcpu, &vm->vcpus, list)
1966 vcpu_dump(stream, vcpu, indent + 2);
1969 #define KVM_EXIT_STRING(x) {KVM_EXIT_##x, #x}
1971 /* Known KVM exit reasons */
1972 static struct exit_reason {
1973 unsigned int reason;
1975 } exit_reasons_known[] = {
1976 KVM_EXIT_STRING(UNKNOWN),
1977 KVM_EXIT_STRING(EXCEPTION),
1978 KVM_EXIT_STRING(IO),
1979 KVM_EXIT_STRING(HYPERCALL),
1980 KVM_EXIT_STRING(DEBUG),
1981 KVM_EXIT_STRING(HLT),
1982 KVM_EXIT_STRING(MMIO),
1983 KVM_EXIT_STRING(IRQ_WINDOW_OPEN),
1984 KVM_EXIT_STRING(SHUTDOWN),
1985 KVM_EXIT_STRING(FAIL_ENTRY),
1986 KVM_EXIT_STRING(INTR),
1987 KVM_EXIT_STRING(SET_TPR),
1988 KVM_EXIT_STRING(TPR_ACCESS),
1989 KVM_EXIT_STRING(S390_SIEIC),
1990 KVM_EXIT_STRING(S390_RESET),
1991 KVM_EXIT_STRING(DCR),
1992 KVM_EXIT_STRING(NMI),
1993 KVM_EXIT_STRING(INTERNAL_ERROR),
1994 KVM_EXIT_STRING(OSI),
1995 KVM_EXIT_STRING(PAPR_HCALL),
1996 KVM_EXIT_STRING(S390_UCONTROL),
1997 KVM_EXIT_STRING(WATCHDOG),
1998 KVM_EXIT_STRING(S390_TSCH),
1999 KVM_EXIT_STRING(EPR),
2000 KVM_EXIT_STRING(SYSTEM_EVENT),
2001 KVM_EXIT_STRING(S390_STSI),
2002 KVM_EXIT_STRING(IOAPIC_EOI),
2003 KVM_EXIT_STRING(HYPERV),
2004 KVM_EXIT_STRING(ARM_NISV),
2005 KVM_EXIT_STRING(X86_RDMSR),
2006 KVM_EXIT_STRING(X86_WRMSR),
2007 KVM_EXIT_STRING(DIRTY_RING_FULL),
2008 KVM_EXIT_STRING(AP_RESET_HOLD),
2009 KVM_EXIT_STRING(X86_BUS_LOCK),
2010 KVM_EXIT_STRING(XEN),
2011 KVM_EXIT_STRING(RISCV_SBI),
2012 KVM_EXIT_STRING(RISCV_CSR),
2013 KVM_EXIT_STRING(NOTIFY),
2014 #ifdef KVM_EXIT_MEMORY_NOT_PRESENT
2015 KVM_EXIT_STRING(MEMORY_NOT_PRESENT),
2020 * Exit Reason String
2023 * exit_reason - Exit reason
2028 * Constant string pointer describing the exit reason.
2030 * Locates and returns a constant string that describes the KVM exit
2031 * reason given by exit_reason. If no such string is found, a constant
2032 * string of "Unknown" is returned.
2034 const char *exit_reason_str(unsigned int exit_reason)
2038 for (n1 = 0; n1 < ARRAY_SIZE(exit_reasons_known); n1++) {
2039 if (exit_reason == exit_reasons_known[n1].reason)
2040 return exit_reasons_known[n1].name;
2047 * Physical Contiguous Page Allocator
2050 * vm - Virtual Machine
2051 * num - number of pages
2052 * paddr_min - Physical address minimum
2053 * memslot - Memory region to allocate page from
2054 * protected - True if the pages will be used as protected/private memory
2059 * Starting physical address
2061 * Within the VM specified by vm, locates a range of available physical
2062 * pages at or above paddr_min. If found, the pages are marked as in use
2063 * and their base address is returned. A TEST_ASSERT failure occurs if
2064 * not enough pages are available at or above paddr_min.
2066 vm_paddr_t __vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
2067 vm_paddr_t paddr_min, uint32_t memslot,
2070 struct userspace_mem_region *region;
2071 sparsebit_idx_t pg, base;
2073 TEST_ASSERT(num > 0, "Must allocate at least one page");
2075 TEST_ASSERT((paddr_min % vm->page_size) == 0, "Min physical address "
2076 "not divisible by page size.\n"
2077 " paddr_min: 0x%lx page_size: 0x%x",
2078 paddr_min, vm->page_size);
2080 region = memslot2region(vm, memslot);
2081 TEST_ASSERT(!protected || region->protected_phy_pages,
2082 "Region doesn't support protected memory");
2084 base = pg = paddr_min >> vm->page_shift;
2086 for (; pg < base + num; ++pg) {
2087 if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
2088 base = pg = sparsebit_next_set(region->unused_phy_pages, pg);
2092 } while (pg && pg != base + num);
2095 fprintf(stderr, "No guest physical page available, "
2096 "paddr_min: 0x%lx page_size: 0x%x memslot: %u\n",
2097 paddr_min, vm->page_size, memslot);
2098 fputs("---- vm dump ----\n", stderr);
2099 vm_dump(stderr, vm, 2);
2103 for (pg = base; pg < base + num; ++pg) {
2104 sparsebit_clear(region->unused_phy_pages, pg);
2106 sparsebit_set(region->protected_phy_pages, pg);
2109 return base * vm->page_size;
2112 vm_paddr_t vm_phy_page_alloc(struct kvm_vm *vm, vm_paddr_t paddr_min,
2115 return vm_phy_pages_alloc(vm, 1, paddr_min, memslot);
2118 vm_paddr_t vm_alloc_page_table(struct kvm_vm *vm)
2120 return vm_phy_page_alloc(vm, KVM_GUEST_PAGE_TABLE_MIN_PADDR,
2121 vm->memslots[MEM_REGION_PT]);
2125 * Address Guest Virtual to Host Virtual
2128 * vm - Virtual Machine
2129 * gva - VM virtual address
2134 * Equivalent host virtual address
2136 void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
2138 return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
2141 unsigned long __weak vm_compute_max_gfn(struct kvm_vm *vm)
2143 return ((1ULL << vm->pa_bits) >> vm->page_shift) - 1;
2146 static unsigned int vm_calc_num_pages(unsigned int num_pages,
2147 unsigned int page_shift,
2148 unsigned int new_page_shift,
2151 unsigned int n = 1 << (new_page_shift - page_shift);
2153 if (page_shift >= new_page_shift)
2154 return num_pages * (1 << (page_shift - new_page_shift));
2156 return num_pages / n + !!(ceil && num_pages % n);
2159 static inline int getpageshift(void)
2161 return __builtin_ffs(getpagesize()) - 1;
2165 vm_num_host_pages(enum vm_guest_mode mode, unsigned int num_guest_pages)
2167 return vm_calc_num_pages(num_guest_pages,
2168 vm_guest_mode_params[mode].page_shift,
2169 getpageshift(), true);
2173 vm_num_guest_pages(enum vm_guest_mode mode, unsigned int num_host_pages)
2175 return vm_calc_num_pages(num_host_pages, getpageshift(),
2176 vm_guest_mode_params[mode].page_shift, false);
2179 unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size)
2182 n = DIV_ROUND_UP(size, vm_guest_mode_params[mode].page_size);
2183 return vm_adjust_num_guest_pages(mode, n);
2187 * Read binary stats descriptors
2190 * stats_fd - the file descriptor for the binary stats file from which to read
2191 * header - the binary stats metadata header corresponding to the given FD
2196 * A pointer to a newly allocated series of stat descriptors.
2197 * Caller is responsible for freeing the returned kvm_stats_desc.
2199 * Read the stats descriptors from the binary stats interface.
2201 struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
2202 struct kvm_stats_header *header)
2204 struct kvm_stats_desc *stats_desc;
2205 ssize_t desc_size, total_size, ret;
2207 desc_size = get_stats_descriptor_size(header);
2208 total_size = header->num_desc * desc_size;
2210 stats_desc = calloc(header->num_desc, desc_size);
2211 TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors");
2213 ret = pread(stats_fd, stats_desc, total_size, header->desc_offset);
2214 TEST_ASSERT(ret == total_size, "Read KVM stats descriptors");
2220 * Read stat data for a particular stat
2223 * stats_fd - the file descriptor for the binary stats file from which to read
2224 * header - the binary stats metadata header corresponding to the given FD
2225 * desc - the binary stat metadata for the particular stat to be read
2226 * max_elements - the maximum number of 8-byte values to read into data
2229 * data - the buffer into which stat data should be read
2231 * Read the data values of a specified stat from the binary stats interface.
2233 void read_stat_data(int stats_fd, struct kvm_stats_header *header,
2234 struct kvm_stats_desc *desc, uint64_t *data,
2235 size_t max_elements)
2237 size_t nr_elements = min_t(ssize_t, desc->size, max_elements);
2238 size_t size = nr_elements * sizeof(*data);
2241 TEST_ASSERT(desc->size, "No elements in stat '%s'", desc->name);
2242 TEST_ASSERT(max_elements, "Zero elements requested for stat '%s'", desc->name);
2244 ret = pread(stats_fd, data, size,
2245 header->data_offset + desc->offset);
2247 TEST_ASSERT(ret >= 0, "pread() failed on stat '%s', errno: %i (%s)",
2248 desc->name, errno, strerror(errno));
2249 TEST_ASSERT(ret == size,
2250 "pread() on stat '%s' read %ld bytes, wanted %lu bytes",
2251 desc->name, size, ret);
2255 * Read the data of the named stat
2258 * vm - the VM for which the stat should be read
2259 * stat_name - the name of the stat to read
2260 * max_elements - the maximum number of 8-byte values to read into data
2263 * data - the buffer into which stat data should be read
2265 * Read the data values of a specified stat from the binary stats interface.
2267 void __vm_get_stat(struct kvm_vm *vm, const char *stat_name, uint64_t *data,
2268 size_t max_elements)
2270 struct kvm_stats_desc *desc;
2274 if (!vm->stats_fd) {
2275 vm->stats_fd = vm_get_stats_fd(vm);
2276 read_stats_header(vm->stats_fd, &vm->stats_header);
2277 vm->stats_desc = read_stats_descriptors(vm->stats_fd,
2281 size_desc = get_stats_descriptor_size(&vm->stats_header);
2283 for (i = 0; i < vm->stats_header.num_desc; ++i) {
2284 desc = (void *)vm->stats_desc + (i * size_desc);
2286 if (strcmp(desc->name, stat_name))
2289 read_stat_data(vm->stats_fd, &vm->stats_header, desc,
2290 data, max_elements);
2296 __weak void kvm_arch_vm_post_create(struct kvm_vm *vm)
2300 __weak void kvm_selftest_arch_init(void)
2304 void __attribute((constructor)) kvm_selftest_init(void)
2306 /* Tell stdout not to buffer its content. */
2307 setbuf(stdout, NULL);
2309 kvm_selftest_arch_init();
2312 bool vm_is_gpa_protected(struct kvm_vm *vm, vm_paddr_t paddr)
2314 sparsebit_idx_t pg = 0;
2315 struct userspace_mem_region *region;
2317 if (!vm_arch_has_protected_memory(vm))
2320 region = userspace_mem_region_find(vm, paddr, paddr);
2321 TEST_ASSERT(region, "No vm physical memory at 0x%lx", paddr);
2323 pg = paddr >> vm->page_shift;
2324 return sparsebit_is_set(region->protected_phy_pages, pg);