1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <sys/ioctl.h>
14 #include <linux/compiler.h>
16 #include <test_util.h>
18 #include <processor.h>
23 * s390x needs at least 1MB alignment, and the x86_64 MOVE/DELETE tests need a
24 * 2MB sized and aligned region so that the initial region corresponds to
25 * exactly one large page.
27 #define MEM_REGION_SIZE 0x200000
31 * Somewhat arbitrary location and slot, intended to not overlap anything.
33 #define MEM_REGION_GPA 0xc0000000
34 #define MEM_REGION_SLOT 10
36 static const uint64_t MMIO_VAL = 0xbeefull;
38 extern const uint64_t final_rip_start;
39 extern const uint64_t final_rip_end;
41 static sem_t vcpu_ready;
43 static inline uint64_t guest_spin_on_val(uint64_t spin_val)
48 val = READ_ONCE(*((uint64_t *)MEM_REGION_GPA));
49 } while (val == spin_val);
55 static void *vcpu_worker(void *data)
57 struct kvm_vm *vm = data;
63 * Loop until the guest is done. Re-enter the guest on all MMIO exits,
64 * which will occur if the guest attempts to access a memslot after it
65 * has been deleted or while it is being moved .
67 run = vcpu_state(vm, VCPU_ID);
70 vcpu_run(vm, VCPU_ID);
72 if (run->exit_reason == KVM_EXIT_IO) {
73 cmd = get_ucall(vm, VCPU_ID, &uc);
74 if (cmd != UCALL_SYNC)
77 sem_post(&vcpu_ready);
81 if (run->exit_reason != KVM_EXIT_MMIO)
84 TEST_ASSERT(!run->mmio.is_write, "Unexpected exit mmio write");
85 TEST_ASSERT(run->mmio.len == 8,
86 "Unexpected exit mmio size = %u", run->mmio.len);
88 TEST_ASSERT(run->mmio.phys_addr == MEM_REGION_GPA,
89 "Unexpected exit mmio address = 0x%llx",
91 memcpy(run->mmio.data, &MMIO_VAL, 8);
94 if (run->exit_reason == KVM_EXIT_IO && cmd == UCALL_ABORT)
95 TEST_FAIL("%s at %s:%ld, val = %lu", (const char *)uc.args[0],
96 __FILE__, uc.args[1], uc.args[2]);
101 static void wait_for_vcpu(void)
105 TEST_ASSERT(!clock_gettime(CLOCK_REALTIME, &ts),
106 "clock_gettime() failed: %d\n", errno);
109 TEST_ASSERT(!sem_timedwait(&vcpu_ready, &ts),
110 "sem_timedwait() failed: %d\n", errno);
112 /* Wait for the vCPU thread to reenter the guest. */
116 static struct kvm_vm *spawn_vm(pthread_t *vcpu_thread, void *guest_code)
122 vm = vm_create_default(VCPU_ID, 0, guest_code);
124 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
125 MEM_REGION_GPA, MEM_REGION_SLOT,
126 MEM_REGION_SIZE / getpagesize(), 0);
129 * Allocate and map two pages so that the GPA accessed by guest_code()
130 * stays valid across the memslot move.
132 gpa = vm_phy_pages_alloc(vm, 2, MEM_REGION_GPA, MEM_REGION_SLOT);
133 TEST_ASSERT(gpa == MEM_REGION_GPA, "Failed vm_phy_pages_alloc\n");
135 virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 2, 0);
137 /* Ditto for the host mapping so that both pages can be zeroed. */
138 hva = addr_gpa2hva(vm, MEM_REGION_GPA);
139 memset(hva, 0, 2 * 4096);
141 pthread_create(vcpu_thread, NULL, vcpu_worker, vm);
143 /* Ensure the guest thread is spun up. */
150 static void guest_code_move_memory_region(void)
157 * Spin until the memory region is moved to a misaligned address. This
158 * may or may not trigger MMIO, as the window where the memslot is
159 * invalid is quite small.
161 val = guest_spin_on_val(0);
162 GUEST_ASSERT_1(val == 1 || val == MMIO_VAL, val);
164 /* Spin until the memory region is realigned. */
165 val = guest_spin_on_val(MMIO_VAL);
166 GUEST_ASSERT_1(val == 1, val);
171 static void test_move_memory_region(void)
173 pthread_t vcpu_thread;
177 vm = spawn_vm(&vcpu_thread, guest_code_move_memory_region);
179 hva = addr_gpa2hva(vm, MEM_REGION_GPA);
182 * Shift the region's base GPA. The guest should not see "2" as the
183 * hva->gpa translation is misaligned, i.e. the guest is accessing a
184 * different host pfn.
186 vm_mem_region_move(vm, MEM_REGION_SLOT, MEM_REGION_GPA - 4096);
190 * The guest _might_ see an invalid memslot and trigger MMIO, but it's
191 * a tiny window. Spin and defer the sync until the memslot is
192 * restored and guest behavior is once again deterministic.
197 * Note, value in memory needs to be changed *before* restoring the
198 * memslot, else the guest could race the update and see "2".
202 /* Restore the original base, the guest should see "1". */
203 vm_mem_region_move(vm, MEM_REGION_SLOT, MEM_REGION_GPA);
205 /* Defered sync from when the memslot was misaligned (above). */
208 pthread_join(vcpu_thread, NULL);
213 static void guest_code_delete_memory_region(void)
219 /* Spin until the memory region is deleted. */
220 val = guest_spin_on_val(0);
221 GUEST_ASSERT_1(val == MMIO_VAL, val);
223 /* Spin until the memory region is recreated. */
224 val = guest_spin_on_val(MMIO_VAL);
225 GUEST_ASSERT_1(val == 0, val);
227 /* Spin until the memory region is deleted. */
228 val = guest_spin_on_val(0);
229 GUEST_ASSERT_1(val == MMIO_VAL, val);
232 ".pushsection .rodata\n\t"
233 ".global final_rip_start\n\t"
234 "final_rip_start: .quad 1b\n\t"
237 /* Spin indefinitely (until the code memslot is deleted). */
238 guest_spin_on_val(MMIO_VAL);
241 ".pushsection .rodata\n\t"
242 ".global final_rip_end\n\t"
243 "final_rip_end: .quad 1b\n\t"
246 GUEST_ASSERT_1(0, 0);
249 static void test_delete_memory_region(void)
251 pthread_t vcpu_thread;
252 struct kvm_regs regs;
256 vm = spawn_vm(&vcpu_thread, guest_code_delete_memory_region);
258 /* Delete the memory region, the guest should not die. */
259 vm_mem_region_delete(vm, MEM_REGION_SLOT);
262 /* Recreate the memory region. The guest should see "0". */
263 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS_THP,
264 MEM_REGION_GPA, MEM_REGION_SLOT,
265 MEM_REGION_SIZE / getpagesize(), 0);
268 /* Delete the region again so that there's only one memslot left. */
269 vm_mem_region_delete(vm, MEM_REGION_SLOT);
273 * Delete the primary memslot. This should cause an emulation error or
274 * shutdown due to the page tables getting nuked.
276 vm_mem_region_delete(vm, 0);
278 pthread_join(vcpu_thread, NULL);
280 run = vcpu_state(vm, VCPU_ID);
282 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN ||
283 run->exit_reason == KVM_EXIT_INTERNAL_ERROR,
284 "Unexpected exit reason = %d", run->exit_reason);
286 vcpu_regs_get(vm, VCPU_ID, ®s);
289 * On AMD, after KVM_EXIT_SHUTDOWN the VMCB has been reinitialized already,
290 * so the instruction pointer would point to the reset vector.
292 if (run->exit_reason == KVM_EXIT_INTERNAL_ERROR)
293 TEST_ASSERT(regs.rip >= final_rip_start &&
294 regs.rip < final_rip_end,
295 "Bad rip, expected 0x%lx - 0x%lx, got 0x%llx\n",
296 final_rip_start, final_rip_end, regs.rip);
301 static void test_zero_memory_regions(void)
306 pr_info("Testing KVM_RUN with zero added memory regions\n");
308 vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
309 vm_vcpu_add(vm, VCPU_ID);
311 TEST_ASSERT(!ioctl(vm_get_fd(vm), KVM_SET_NR_MMU_PAGES, 64),
312 "KVM_SET_NR_MMU_PAGES failed, errno = %d\n", errno);
313 vcpu_run(vm, VCPU_ID);
315 run = vcpu_state(vm, VCPU_ID);
316 TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR,
317 "Unexpected exit_reason = %u\n", run->exit_reason);
321 #endif /* __x86_64__ */
324 * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any
325 * tentative to add further slots should fail.
327 static void test_add_max_memory_regions(void)
331 uint32_t max_mem_slots;
333 uint64_t guest_addr = 0x0;
334 uint64_t mem_reg_npages;
337 max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS);
338 TEST_ASSERT(max_mem_slots > 0,
339 "KVM_CAP_NR_MEMSLOTS should be greater than 0");
340 pr_info("Allowed number of memory slots: %i\n", max_mem_slots);
342 vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
344 mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, MEM_REGION_SIZE);
346 /* Check it can be added memory slots up to the maximum allowed */
347 pr_info("Adding slots 0..%i, each memory region with %dK size\n",
348 (max_mem_slots - 1), MEM_REGION_SIZE >> 10);
349 for (slot = 0; slot < max_mem_slots; slot++) {
350 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
351 guest_addr, slot, mem_reg_npages,
353 guest_addr += MEM_REGION_SIZE;
356 /* Check it cannot be added memory slots beyond the limit */
357 mem = mmap(NULL, MEM_REGION_SIZE, PROT_READ | PROT_WRITE,
358 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
359 TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
361 ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION,
362 &(struct kvm_userspace_memory_region) {slot, 0, guest_addr,
363 MEM_REGION_SIZE, (uint64_t) mem});
364 TEST_ASSERT(ret == -1 && errno == EINVAL,
365 "Adding one more memory slot should fail with EINVAL");
367 munmap(mem, MEM_REGION_SIZE);
371 int main(int argc, char *argv[])
377 /* Tell stdout not to buffer its content */
378 setbuf(stdout, NULL);
382 * FIXME: the zero-memslot test fails on aarch64 and s390x because
383 * KVM_RUN fails with ENOEXEC or EFAULT.
385 test_zero_memory_regions();
388 test_add_max_memory_regions();
392 loops = atoi(argv[1]);
396 pr_info("Testing MOVE of in-use region, %d loops\n", loops);
397 for (i = 0; i < loops; i++)
398 test_move_memory_region();
400 pr_info("Testing DELETE of in-use region, %d loops\n", loops);
401 for (i = 0; i < loops; i++)
402 test_delete_memory_region();