1 // SPDX-License-Identifier: GPL-2.0
11 #include <linux/bitmap.h>
12 #include <linux/bitops.h>
13 #include <linux/atomic.h>
14 #include <linux/sizes.h>
17 #include "test_util.h"
18 #include "guest_modes.h"
19 #include "processor.h"
21 static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
25 for (gpa = start_gpa; gpa < end_gpa; gpa += stride)
26 *((volatile uint64_t *)gpa) = gpa;
32 struct kvm_vcpu *vcpu;
38 static atomic_t rendezvous;
40 static void rendezvous_with_boss(void)
42 int orig = atomic_read(&rendezvous);
45 atomic_dec_and_test(&rendezvous);
46 while (atomic_read(&rendezvous) > 0)
49 atomic_inc(&rendezvous);
50 while (atomic_read(&rendezvous) < 0)
55 static void run_vcpu(struct kvm_vcpu *vcpu)
58 ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE);
61 static void *vcpu_worker(void *data)
63 struct vcpu_info *info = data;
64 struct kvm_vcpu *vcpu = info->vcpu;
65 struct kvm_vm *vm = vcpu->vm;
66 struct kvm_sregs sregs;
69 vcpu_args_set(vcpu, 3, info->start_gpa, info->end_gpa, vm->page_size);
71 /* Snapshot regs before the first run. */
72 vcpu_regs_get(vcpu, ®s);
73 rendezvous_with_boss();
76 rendezvous_with_boss();
77 vcpu_regs_set(vcpu, ®s);
78 vcpu_sregs_get(vcpu, &sregs);
80 /* Toggle CR0.WP to trigger a MMU context reset. */
81 sregs.cr0 ^= X86_CR0_WP;
83 vcpu_sregs_set(vcpu, &sregs);
84 rendezvous_with_boss();
87 rendezvous_with_boss();
92 static pthread_t *spawn_workers(struct kvm_vm *vm, struct kvm_vcpu **vcpus,
93 uint64_t start_gpa, uint64_t end_gpa)
95 struct vcpu_info *info;
96 uint64_t gpa, nr_bytes;
100 threads = malloc(nr_vcpus * sizeof(*threads));
101 TEST_ASSERT(threads, "Failed to allocate vCPU threads");
103 info = malloc(nr_vcpus * sizeof(*info));
104 TEST_ASSERT(info, "Failed to allocate vCPU gpa ranges");
106 nr_bytes = ((end_gpa - start_gpa) / nr_vcpus) &
107 ~((uint64_t)vm->page_size - 1);
108 TEST_ASSERT(nr_bytes, "C'mon, no way you have %d CPUs", nr_vcpus);
110 for (i = 0, gpa = start_gpa; i < nr_vcpus; i++, gpa += nr_bytes) {
111 info[i].vcpu = vcpus[i];
112 info[i].start_gpa = gpa;
113 info[i].end_gpa = gpa + nr_bytes;
114 pthread_create(&threads[i], NULL, vcpu_worker, &info[i]);
119 static void rendezvous_with_vcpus(struct timespec *time, const char *name)
123 pr_info("Waiting for vCPUs to finish %s...\n", name);
125 rendezvoused = atomic_read(&rendezvous);
126 for (i = 0; abs(rendezvoused) != 1; i++) {
129 pr_info("\r%d vCPUs haven't rendezvoused...",
130 abs(rendezvoused) - 1);
131 rendezvoused = atomic_read(&rendezvous);
134 clock_gettime(CLOCK_MONOTONIC, time);
136 /* Release the vCPUs after getting the time of the previous action. */
137 pr_info("\rAll vCPUs finished %s, releasing...\n", name);
138 if (rendezvoused > 0)
139 atomic_set(&rendezvous, -nr_vcpus - 1);
141 atomic_set(&rendezvous, nr_vcpus + 1);
144 static void calc_default_nr_vcpus(void)
146 cpu_set_t possible_mask;
149 r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask);
150 TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)",
151 errno, strerror(errno));
153 nr_vcpus = CPU_COUNT(&possible_mask) * 3/4;
154 TEST_ASSERT(nr_vcpus > 0, "Uh, no CPUs?");
157 int main(int argc, char *argv[])
160 * Skip the first 4gb and slot0. slot0 maps <1gb and is used to back
161 * the guest's code, stack, and page tables. Because selftests creates
162 * an IRQCHIP, a.k.a. a local APIC, KVM creates an internal memslot
163 * just below the 4gb boundary. This test could create memory at
164 * 1gb-3gb,but it's simpler to skip straight to 4gb.
166 const uint64_t start_gpa = SZ_4G;
167 const int first_slot = 1;
169 struct timespec time_start, time_run1, time_reset, time_run2;
170 uint64_t max_gpa, gpa, slot_size, max_mem, i;
171 int max_slots, slot, opt, fd;
172 bool hugepages = false;
173 struct kvm_vcpu **vcpus;
179 * Default to 2gb so that maxing out systems with MAXPHADDR=46, which
180 * are quite common for x86, requires changing only max_mem (KVM allows
181 * 32k memslots, 32k * 2gb == ~64tb of guest memory).
185 max_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
186 TEST_ASSERT(max_slots > first_slot, "KVM is broken");
188 /* All KVM MMUs should be able to survive a 128gb guest. */
189 max_mem = 128ull * SZ_1G;
191 calc_default_nr_vcpus();
193 while ((opt = getopt(argc, argv, "c:h:m:s:H")) != -1) {
196 nr_vcpus = atoi_positive("Number of vCPUs", optarg);
199 max_mem = 1ull * atoi_positive("Memory size", optarg) * SZ_1G;
202 slot_size = 1ull * atoi_positive("Slot size", optarg) * SZ_1G;
209 printf("usage: %s [-c nr_vcpus] [-m max_mem_in_gb] [-s slot_size_in_gb] [-H]\n", argv[0]);
214 vcpus = malloc(nr_vcpus * sizeof(*vcpus));
215 TEST_ASSERT(vcpus, "Failed to allocate vCPU array");
217 vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
219 max_gpa = vm->max_gfn << vm->page_shift;
220 TEST_ASSERT(max_gpa > (4 * slot_size), "MAXPHYADDR <4gb ");
222 fd = kvm_memfd_alloc(slot_size, hugepages);
223 mem = mmap(NULL, slot_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
224 TEST_ASSERT(mem != MAP_FAILED, "mmap() failed");
226 TEST_ASSERT(!madvise(mem, slot_size, MADV_NOHUGEPAGE), "madvise() failed");
228 /* Pre-fault the memory to avoid taking mmap_sem on guest page faults. */
229 for (i = 0; i < slot_size; i += vm->page_size)
230 ((uint8_t *)mem)[i] = 0xaa;
233 for (slot = first_slot; slot < max_slots; slot++) {
234 gpa = start_gpa + ((slot - first_slot) * slot_size);
235 if (gpa + slot_size > max_gpa)
238 if ((gpa - start_gpa) >= max_mem)
241 vm_set_user_memory_region(vm, slot, 0, gpa, slot_size, mem);
244 /* Identity map memory in the guest using 1gb pages. */
245 for (i = 0; i < slot_size; i += SZ_1G)
246 __virt_pg_map(vm, gpa + i, gpa + i, PG_LEVEL_1G);
248 for (i = 0; i < slot_size; i += vm->page_size)
249 virt_pg_map(vm, gpa + i, gpa + i);
253 atomic_set(&rendezvous, nr_vcpus + 1);
254 threads = spawn_workers(vm, vcpus, start_gpa, gpa);
259 pr_info("Running with %lugb of guest memory and %u vCPUs\n",
260 (gpa - start_gpa) / SZ_1G, nr_vcpus);
262 rendezvous_with_vcpus(&time_start, "spawning");
263 rendezvous_with_vcpus(&time_run1, "run 1");
264 rendezvous_with_vcpus(&time_reset, "reset");
265 rendezvous_with_vcpus(&time_run2, "run 2");
267 time_run2 = timespec_sub(time_run2, time_reset);
268 time_reset = timespec_sub(time_reset, time_run1);
269 time_run1 = timespec_sub(time_run1, time_start);
271 pr_info("run1 = %ld.%.9lds, reset = %ld.%.9lds, run2 = %ld.%.9lds\n",
272 time_run1.tv_sec, time_run1.tv_nsec,
273 time_reset.tv_sec, time_reset.tv_nsec,
274 time_run2.tv_sec, time_run2.tv_nsec);
277 * Delete even numbered slots (arbitrary) and unmap the first half of
278 * the backing (also arbitrary) to verify KVM correctly drops all
279 * references to the removed regions.
281 for (slot = (slot - 1) & ~1ull; slot >= first_slot; slot -= 2)
282 vm_set_user_memory_region(vm, slot, 0, 0, 0, NULL);
284 munmap(mem, slot_size / 2);
286 /* Sanity check that the vCPUs actually ran. */
287 for (i = 0; i < nr_vcpus; i++)
288 pthread_join(threads[i], NULL);
291 * Deliberately exit without deleting the remaining memslots or closing
292 * kvm_fd to test cleanup via mmu_notifier.release.