1 // SPDX-License-Identifier: GPL-2.0
3 * KVM dirty page logging test
5 * Copyright (C) 2018, Red Hat, Inc.
8 #define _GNU_SOURCE /* for program_invocation_name */
15 #include <linux/bitmap.h>
16 #include <linux/bitops.h>
18 #include "test_util.h"
20 #include "processor.h"
26 /* The memory slot index to track dirty pages */
27 #define TEST_MEM_SLOT_INDEX 1
29 /* Default guest test memory offset, 1G */
30 #define DEFAULT_GUEST_TEST_MEM 0x40000000
32 /* How many pages to dirty for each guest loop */
33 #define TEST_PAGES_PER_LOOP 1024
35 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
36 #define TEST_HOST_LOOP_N 32UL
38 /* Interval for each host loop (ms) */
39 #define TEST_HOST_LOOP_INTERVAL 10UL
42 * Guest/Host shared variables. Ensure addr_gva2hva() and/or
43 * sync_global_to/from_guest() are used when accessing from
44 * the host. READ/WRITE_ONCE() should also be used with anything
47 static uint64_t host_page_size
;
48 static uint64_t guest_page_size
;
49 static uint64_t guest_num_pages
;
50 static uint64_t random_array
[TEST_PAGES_PER_LOOP
];
51 static uint64_t iteration
;
54 * Guest physical memory offset of the testing memory slot.
55 * This will be set to the topmost valid physical address minus
56 * the test memory size.
58 static uint64_t guest_test_phys_mem
;
61 * Guest virtual memory offset of the testing memory slot.
62 * Must not conflict with identity mapped test code.
64 static uint64_t guest_test_virt_mem
= DEFAULT_GUEST_TEST_MEM
;
67 * Continuously write to the first 8 bytes of a random pages within
68 * the testing memory region.
70 static void guest_code(void)
75 for (i
= 0; i
< TEST_PAGES_PER_LOOP
; i
++) {
76 uint64_t addr
= guest_test_virt_mem
;
77 addr
+= (READ_ONCE(random_array
[i
]) % guest_num_pages
)
79 addr
&= ~(host_page_size
- 1);
80 *(uint64_t *)addr
= READ_ONCE(iteration
);
83 /* Tell the host that we need more random numbers */
89 static bool host_quit
;
91 /* Points to the test VM memory region on which we track dirty logs */
92 static void *host_test_mem
;
93 static uint64_t host_num_pages
;
95 /* For statistics only */
96 static uint64_t host_dirty_count
;
97 static uint64_t host_clear_count
;
98 static uint64_t host_track_next_count
;
101 * We use this bitmap to track some pages that should have its dirty
102 * bit set in the _next_ iteration. For example, if we detected the
103 * page value changed to current iteration but at the same time the
104 * page bit is cleared in the latest bitmap, then the system must
105 * report that write in the next get dirty log call.
107 static unsigned long *host_bmap_track
;
109 static void generate_random_array(uint64_t *guest_array
, uint64_t size
)
113 for (i
= 0; i
< size
; i
++)
114 guest_array
[i
] = random();
117 static void *vcpu_worker(void *data
)
120 struct kvm_vm
*vm
= data
;
121 uint64_t *guest_array
;
122 uint64_t pages_count
= 0;
126 run
= vcpu_state(vm
, VCPU_ID
);
128 guest_array
= addr_gva2hva(vm
, (vm_vaddr_t
)random_array
);
129 generate_random_array(guest_array
, TEST_PAGES_PER_LOOP
);
131 while (!READ_ONCE(host_quit
)) {
132 /* Let the guest dirty the random pages */
133 ret
= _vcpu_run(vm
, VCPU_ID
);
134 TEST_ASSERT(ret
== 0, "vcpu_run failed: %d\n", ret
);
135 if (get_ucall(vm
, VCPU_ID
, &uc
) == UCALL_SYNC
) {
136 pages_count
+= TEST_PAGES_PER_LOOP
;
137 generate_random_array(guest_array
, TEST_PAGES_PER_LOOP
);
140 "Invalid guest sync status: "
142 exit_reason_str(run
->exit_reason
));
146 DEBUG("Dirtied %"PRIu64
" pages\n", pages_count
);
151 static void vm_dirty_log_verify(unsigned long *bmap
)
155 uint64_t step
= host_page_size
>= guest_page_size
? 1 :
156 guest_page_size
/ host_page_size
;
158 for (page
= 0; page
< host_num_pages
; page
+= step
) {
159 value_ptr
= host_test_mem
+ page
* host_page_size
;
161 /* If this is a special page that we were tracking... */
162 if (test_and_clear_bit(page
, host_bmap_track
)) {
163 host_track_next_count
++;
164 TEST_ASSERT(test_bit(page
, bmap
),
165 "Page %"PRIu64
" should have its dirty bit "
166 "set in this iteration but it is missing",
170 if (test_bit(page
, bmap
)) {
173 * If the bit is set, the value written onto
174 * the corresponding page should be either the
175 * previous iteration number or the current one.
177 TEST_ASSERT(*value_ptr
== iteration
||
178 *value_ptr
== iteration
- 1,
179 "Set page %"PRIu64
" value %"PRIu64
180 " incorrect (iteration=%"PRIu64
")",
181 page
, *value_ptr
, iteration
);
185 * If cleared, the value written can be any
186 * value smaller or equals to the iteration
187 * number. Note that the value can be exactly
188 * (iteration-1) if that write can happen
191 * (1) increase loop count to "iteration-1"
192 * (2) write to page P happens (with value
194 * (3) get dirty log for "iteration-1"; we'll
195 * see that page P bit is set (dirtied),
196 * and not set the bit in host_bmap_track
197 * (4) increase loop count to "iteration"
198 * (which is current iteration)
199 * (5) get dirty log for current iteration,
200 * we'll see that page P is cleared, with
201 * value "iteration-1".
203 TEST_ASSERT(*value_ptr
<= iteration
,
204 "Clear page %"PRIu64
" value %"PRIu64
205 " incorrect (iteration=%"PRIu64
")",
206 page
, *value_ptr
, iteration
);
207 if (*value_ptr
== iteration
) {
209 * This page is _just_ modified; it
210 * should report its dirtyness in the
213 set_bit(page
, host_bmap_track
);
219 static struct kvm_vm
*create_vm(enum vm_guest_mode mode
, uint32_t vcpuid
,
220 uint64_t extra_mem_pages
, void *guest_code
,
224 uint64_t extra_pg_pages
= extra_mem_pages
/ 512 * 2;
226 vm
= _vm_create(mode
, DEFAULT_GUEST_PHY_PAGES
+ extra_pg_pages
,
228 kvm_vm_elf_load(vm
, program_invocation_name
, 0, 0);
230 vm_create_irqchip(vm
);
232 vm_vcpu_add_default(vm
, vcpuid
, guest_code
);
236 static void run_test(enum vm_guest_mode mode
, unsigned long iterations
,
237 unsigned long interval
, uint64_t phys_offset
)
239 unsigned int guest_pa_bits
, guest_page_shift
;
240 pthread_t vcpu_thread
;
244 unsigned long type
= 0;
247 case VM_MODE_P52V48_4K
:
249 guest_page_shift
= 12;
251 case VM_MODE_P52V48_64K
:
253 guest_page_shift
= 16;
255 case VM_MODE_P48V48_4K
:
257 guest_page_shift
= 12;
259 case VM_MODE_P48V48_64K
:
261 guest_page_shift
= 16;
263 case VM_MODE_P40V48_4K
:
265 guest_page_shift
= 12;
267 case VM_MODE_P40V48_64K
:
269 guest_page_shift
= 16;
272 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode
);
275 DEBUG("Testing guest mode: %s\n", vm_guest_mode_string(mode
));
280 * The x86_64 kvm selftests framework currently only supports a
281 * single PML4 which restricts the number of physical address
282 * bits we can change to 39.
287 if (guest_pa_bits
!= 40)
288 type
= KVM_VM_TYPE_ARM_IPA_SIZE(guest_pa_bits
);
290 max_gfn
= (1ul << (guest_pa_bits
- guest_page_shift
)) - 1;
291 guest_page_size
= (1ul << guest_page_shift
);
293 * A little more than 1G of guest page sized pages. Cover the
294 * case where the size is not aligned to 64 pages.
296 guest_num_pages
= (1ul << (30 - guest_page_shift
)) + 3;
297 host_page_size
= getpagesize();
298 host_num_pages
= (guest_num_pages
* guest_page_size
) / host_page_size
+
299 !!((guest_num_pages
* guest_page_size
) % host_page_size
);
302 guest_test_phys_mem
= (max_gfn
- guest_num_pages
) * guest_page_size
;
303 guest_test_phys_mem
&= ~(host_page_size
- 1);
305 guest_test_phys_mem
= phys_offset
;
308 DEBUG("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem
);
310 bmap
= bitmap_alloc(host_num_pages
);
311 host_bmap_track
= bitmap_alloc(host_num_pages
);
313 vm
= create_vm(mode
, VCPU_ID
, guest_num_pages
, guest_code
, type
);
315 #ifdef USE_CLEAR_DIRTY_LOG
316 struct kvm_enable_cap cap
= {};
318 cap
.cap
= KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2
;
320 vm_enable_cap(vm
, &cap
);
323 /* Add an extra memory slot for testing dirty logging */
324 vm_userspace_mem_region_add(vm
, VM_MEM_SRC_ANONYMOUS
,
328 KVM_MEM_LOG_DIRTY_PAGES
);
330 /* Do mapping for the dirty track memory slot */
331 virt_map(vm
, guest_test_virt_mem
, guest_test_phys_mem
,
332 guest_num_pages
* guest_page_size
, 0);
334 /* Cache the HVA pointer of the region */
335 host_test_mem
= addr_gpa2hva(vm
, (vm_paddr_t
)guest_test_phys_mem
);
338 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
341 ucall_init(vm
, UCALL_MMIO
, NULL
);
344 /* Export the shared variables to the guest */
345 sync_global_to_guest(vm
, host_page_size
);
346 sync_global_to_guest(vm
, guest_page_size
);
347 sync_global_to_guest(vm
, guest_test_virt_mem
);
348 sync_global_to_guest(vm
, guest_num_pages
);
350 /* Start the iterations */
352 sync_global_to_guest(vm
, iteration
);
354 host_dirty_count
= 0;
355 host_clear_count
= 0;
356 host_track_next_count
= 0;
358 pthread_create(&vcpu_thread
, NULL
, vcpu_worker
, vm
);
360 while (iteration
< iterations
) {
361 /* Give the vcpu thread some time to dirty some pages */
362 usleep(interval
* 1000);
363 kvm_vm_get_dirty_log(vm
, TEST_MEM_SLOT_INDEX
, bmap
);
364 #ifdef USE_CLEAR_DIRTY_LOG
365 kvm_vm_clear_dirty_log(vm
, TEST_MEM_SLOT_INDEX
, bmap
, 0,
368 vm_dirty_log_verify(bmap
);
370 sync_global_to_guest(vm
, iteration
);
373 /* Tell the vcpu thread to quit */
375 pthread_join(vcpu_thread
, NULL
);
377 DEBUG("Total bits checked: dirty (%"PRIu64
"), clear (%"PRIu64
"), "
378 "track_next (%"PRIu64
")\n", host_dirty_count
, host_clear_count
,
379 host_track_next_count
);
382 free(host_bmap_track
);
387 struct vm_guest_mode_params
{
391 struct vm_guest_mode_params vm_guest_mode_params
[NUM_VM_MODES
];
393 #define vm_guest_mode_params_init(mode, supported, enabled) \
395 vm_guest_mode_params[mode] = (struct vm_guest_mode_params){ supported, enabled }; \
398 static void help(char *name
)
403 printf("usage: %s [-h] [-i iterations] [-I interval] "
404 "[-p offset] [-m mode]\n", name
);
406 printf(" -i: specify iteration counts (default: %"PRIu64
")\n",
408 printf(" -I: specify interval in ms (default: %"PRIu64
" ms)\n",
409 TEST_HOST_LOOP_INTERVAL
);
410 printf(" -p: specify guest physical test memory offset\n"
411 " Warning: a low offset can conflict with the loaded test code.\n");
412 printf(" -m: specify the guest mode ID to test "
413 "(default: test all supported modes)\n"
414 " This option may be used multiple times.\n"
415 " Guest mode IDs:\n");
416 for (i
= 0; i
< NUM_VM_MODES
; ++i
) {
417 printf(" %d: %s%s\n", i
, vm_guest_mode_string(i
),
418 vm_guest_mode_params
[i
].supported
? " (supported)" : "");
424 int main(int argc
, char *argv
[])
426 unsigned long iterations
= TEST_HOST_LOOP_N
;
427 unsigned long interval
= TEST_HOST_LOOP_INTERVAL
;
428 bool mode_selected
= false;
429 uint64_t phys_offset
= 0;
433 unsigned int host_ipa_limit
;
436 #ifdef USE_CLEAR_DIRTY_LOG
437 if (!kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2
)) {
438 fprintf(stderr
, "KVM_CLEAR_DIRTY_LOG not available, skipping tests\n");
444 vm_guest_mode_params_init(VM_MODE_P52V48_4K
, true, true);
447 vm_guest_mode_params_init(VM_MODE_P40V48_4K
, true, true);
448 vm_guest_mode_params_init(VM_MODE_P40V48_64K
, true, true);
450 host_ipa_limit
= kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE
);
451 if (host_ipa_limit
>= 52)
452 vm_guest_mode_params_init(VM_MODE_P52V48_64K
, true, true);
453 if (host_ipa_limit
>= 48) {
454 vm_guest_mode_params_init(VM_MODE_P48V48_4K
, true, true);
455 vm_guest_mode_params_init(VM_MODE_P48V48_64K
, true, true);
459 while ((opt
= getopt(argc
, argv
, "hi:I:p:m:")) != -1) {
462 iterations
= strtol(optarg
, NULL
, 10);
465 interval
= strtol(optarg
, NULL
, 10);
468 phys_offset
= strtoull(optarg
, NULL
, 0);
471 if (!mode_selected
) {
472 for (i
= 0; i
< NUM_VM_MODES
; ++i
)
473 vm_guest_mode_params
[i
].enabled
= false;
474 mode_selected
= true;
476 mode
= strtoul(optarg
, NULL
, 10);
477 TEST_ASSERT(mode
< NUM_VM_MODES
,
478 "Guest mode ID %d too big", mode
);
479 vm_guest_mode_params
[mode
].enabled
= true;
488 TEST_ASSERT(iterations
> 2, "Iterations must be greater than two");
489 TEST_ASSERT(interval
> 0, "Interval must be greater than zero");
491 DEBUG("Test iterations: %"PRIu64
", interval: %"PRIu64
" (ms)\n",
492 iterations
, interval
);
496 for (i
= 0; i
< NUM_VM_MODES
; ++i
) {
497 if (!vm_guest_mode_params
[i
].enabled
)
499 TEST_ASSERT(vm_guest_mode_params
[i
].supported
,
500 "Guest mode ID %d (%s) not supported.",
501 i
, vm_guest_mode_string(i
));
502 run_test(i
, iterations
, interval
, phys_offset
);