kvm: selftests: dirty_log_test: also test 64K pages on aarch64
[linux-2.6-microblaze.git] / tools / testing / selftests / kvm / dirty_log_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KVM dirty page logging test
4  *
5  * Copyright (C) 2018, Red Hat, Inc.
6  */
7
8 #define _GNU_SOURCE /* for program_invocation_name */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <time.h>
14 #include <pthread.h>
15 #include <linux/bitmap.h>
16 #include <linux/bitops.h>
17
18 #include "test_util.h"
19 #include "kvm_util.h"
20 #include "processor.h"
21
22 #define DEBUG printf
23
24 #define VCPU_ID                         1
25
26 /* The memory slot index to track dirty pages */
27 #define TEST_MEM_SLOT_INDEX             1
28
29 /*
30  * GPA offset of the testing memory slot. Must be bigger than the
31  * default vm mem slot, which is DEFAULT_GUEST_PHY_PAGES.
32  */
33 #define TEST_MEM_OFFSET                 (1ul << 30) /* 1G */
34
35 /* How many pages to dirty for each guest loop */
36 #define TEST_PAGES_PER_LOOP             1024
37
38 /* How many host loops to run (one KVM_GET_DIRTY_LOG for each loop) */
39 #define TEST_HOST_LOOP_N                32
40
41 /* Interval for each host loop (ms) */
42 #define TEST_HOST_LOOP_INTERVAL         10
43
44 /*
45  * Guest/Host shared variables. Ensure addr_gva2hva() and/or
46  * sync_global_to/from_guest() are used when accessing from
47  * the host. READ/WRITE_ONCE() should also be used with anything
48  * that may change.
49  */
50 static uint64_t host_page_size;
51 static uint64_t guest_page_size;
52 static uint64_t guest_num_pages;
53 static uint64_t random_array[TEST_PAGES_PER_LOOP];
54 static uint64_t iteration;
55
56 /*
57  * Continuously write to the first 8 bytes of a random pages within
58  * the testing memory region.
59  */
60 static void guest_code(void)
61 {
62         int i;
63
64         while (true) {
65                 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
66                         uint64_t addr = TEST_MEM_OFFSET;
67                         addr += (READ_ONCE(random_array[i]) % guest_num_pages)
68                                 * guest_page_size;
69                         addr &= ~(host_page_size - 1);
70                         *(uint64_t *)addr = READ_ONCE(iteration);
71                 }
72
73                 /* Tell the host that we need more random numbers */
74                 GUEST_SYNC(1);
75         }
76 }
77
78 /* Host variables */
79 static bool host_quit;
80
81 /* Points to the test VM memory region on which we track dirty logs */
82 static void *host_test_mem;
83 static uint64_t host_num_pages;
84
85 /* For statistics only */
86 static uint64_t host_dirty_count;
87 static uint64_t host_clear_count;
88 static uint64_t host_track_next_count;
89
90 /*
91  * We use this bitmap to track some pages that should have its dirty
92  * bit set in the _next_ iteration.  For example, if we detected the
93  * page value changed to current iteration but at the same time the
94  * page bit is cleared in the latest bitmap, then the system must
95  * report that write in the next get dirty log call.
96  */
97 static unsigned long *host_bmap_track;
98
99 static void generate_random_array(uint64_t *guest_array, uint64_t size)
100 {
101         uint64_t i;
102
103         for (i = 0; i < size; i++)
104                 guest_array[i] = random();
105 }
106
107 static void *vcpu_worker(void *data)
108 {
109         int ret;
110         struct kvm_vm *vm = data;
111         uint64_t *guest_array;
112         uint64_t pages_count = 0;
113         struct kvm_run *run;
114         struct ucall uc;
115
116         run = vcpu_state(vm, VCPU_ID);
117
118         guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
119         generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
120
121         while (!READ_ONCE(host_quit)) {
122                 /* Let the guest dirty the random pages */
123                 ret = _vcpu_run(vm, VCPU_ID);
124                 if (get_ucall(vm, VCPU_ID, &uc) == UCALL_SYNC) {
125                         pages_count += TEST_PAGES_PER_LOOP;
126                         generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
127                 } else {
128                         TEST_ASSERT(false,
129                                     "Invalid guest sync status: "
130                                     "exit_reason=%s\n",
131                                     exit_reason_str(run->exit_reason));
132                 }
133         }
134
135         DEBUG("Dirtied %"PRIu64" pages\n", pages_count);
136
137         return NULL;
138 }
139
140 static void vm_dirty_log_verify(unsigned long *bmap)
141 {
142         uint64_t page;
143         uint64_t *value_ptr;
144         uint64_t step = host_page_size >= guest_page_size ? 1 :
145                                 guest_page_size / host_page_size;
146
147         for (page = 0; page < host_num_pages; page += step) {
148                 value_ptr = host_test_mem + page * host_page_size;
149
150                 /* If this is a special page that we were tracking... */
151                 if (test_and_clear_bit(page, host_bmap_track)) {
152                         host_track_next_count++;
153                         TEST_ASSERT(test_bit(page, bmap),
154                                     "Page %"PRIu64" should have its dirty bit "
155                                     "set in this iteration but it is missing",
156                                     page);
157                 }
158
159                 if (test_bit(page, bmap)) {
160                         host_dirty_count++;
161                         /*
162                          * If the bit is set, the value written onto
163                          * the corresponding page should be either the
164                          * previous iteration number or the current one.
165                          */
166                         TEST_ASSERT(*value_ptr == iteration ||
167                                     *value_ptr == iteration - 1,
168                                     "Set page %"PRIu64" value %"PRIu64
169                                     " incorrect (iteration=%"PRIu64")",
170                                     page, *value_ptr, iteration);
171                 } else {
172                         host_clear_count++;
173                         /*
174                          * If cleared, the value written can be any
175                          * value smaller or equals to the iteration
176                          * number.  Note that the value can be exactly
177                          * (iteration-1) if that write can happen
178                          * like this:
179                          *
180                          * (1) increase loop count to "iteration-1"
181                          * (2) write to page P happens (with value
182                          *     "iteration-1")
183                          * (3) get dirty log for "iteration-1"; we'll
184                          *     see that page P bit is set (dirtied),
185                          *     and not set the bit in host_bmap_track
186                          * (4) increase loop count to "iteration"
187                          *     (which is current iteration)
188                          * (5) get dirty log for current iteration,
189                          *     we'll see that page P is cleared, with
190                          *     value "iteration-1".
191                          */
192                         TEST_ASSERT(*value_ptr <= iteration,
193                                     "Clear page %"PRIu64" value %"PRIu64
194                                     " incorrect (iteration=%"PRIu64")",
195                                     page, *value_ptr, iteration);
196                         if (*value_ptr == iteration) {
197                                 /*
198                                  * This page is _just_ modified; it
199                                  * should report its dirtyness in the
200                                  * next run
201                                  */
202                                 set_bit(page, host_bmap_track);
203                         }
204                 }
205         }
206 }
207
208 static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
209                                 uint64_t extra_mem_pages, void *guest_code)
210 {
211         struct kvm_vm *vm;
212         uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
213
214         vm = vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
215         kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
216 #ifdef __x86_64__
217         vm_create_irqchip(vm);
218 #endif
219         vm_vcpu_add_default(vm, vcpuid, guest_code);
220         return vm;
221 }
222
223 static void run_test(enum vm_guest_mode mode, unsigned long iterations,
224                      unsigned long interval)
225 {
226         unsigned int guest_page_shift;
227         pthread_t vcpu_thread;
228         struct kvm_vm *vm;
229         unsigned long *bmap;
230
231         switch (mode) {
232         case VM_MODE_P52V48_4K:
233                 guest_page_shift = 12;
234                 break;
235         case VM_MODE_P52V48_64K:
236                 guest_page_shift = 16;
237                 break;
238         default:
239                 TEST_ASSERT(false, "Unknown guest mode, mode: 0x%x", mode);
240         }
241
242         DEBUG("Testing guest mode: %s\n", vm_guest_mode_string(mode));
243
244         guest_page_size = (1ul << guest_page_shift);
245         /* 1G of guest page sized pages */
246         guest_num_pages = (1ul << (30 - guest_page_shift));
247         host_page_size = getpagesize();
248         host_num_pages = (guest_num_pages * guest_page_size) / host_page_size +
249                          !!((guest_num_pages * guest_page_size) % host_page_size);
250
251         bmap = bitmap_alloc(host_num_pages);
252         host_bmap_track = bitmap_alloc(host_num_pages);
253
254         vm = create_vm(mode, VCPU_ID, guest_num_pages, guest_code);
255
256         /* Add an extra memory slot for testing dirty logging */
257         vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
258                                     TEST_MEM_OFFSET,
259                                     TEST_MEM_SLOT_INDEX,
260                                     guest_num_pages,
261                                     KVM_MEM_LOG_DIRTY_PAGES);
262
263         /* Do 1:1 mapping for the dirty track memory slot */
264         virt_map(vm, TEST_MEM_OFFSET, TEST_MEM_OFFSET,
265                  guest_num_pages * guest_page_size, 0);
266
267         /* Cache the HVA pointer of the region */
268         host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)TEST_MEM_OFFSET);
269
270 #ifdef __x86_64__
271         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
272 #endif
273 #ifdef __aarch64__
274         ucall_init(vm, UCALL_MMIO, NULL);
275 #endif
276
277         /* Export the shared variables to the guest */
278         sync_global_to_guest(vm, host_page_size);
279         sync_global_to_guest(vm, guest_page_size);
280         sync_global_to_guest(vm, guest_num_pages);
281
282         /* Start the iterations */
283         iteration = 1;
284         sync_global_to_guest(vm, iteration);
285         host_quit = false;
286         host_dirty_count = 0;
287         host_clear_count = 0;
288         host_track_next_count = 0;
289
290         pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
291
292         while (iteration < iterations) {
293                 /* Give the vcpu thread some time to dirty some pages */
294                 usleep(interval * 1000);
295                 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
296                 vm_dirty_log_verify(bmap);
297                 iteration++;
298                 sync_global_to_guest(vm, iteration);
299         }
300
301         /* Tell the vcpu thread to quit */
302         host_quit = true;
303         pthread_join(vcpu_thread, NULL);
304
305         DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
306               "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
307               host_track_next_count);
308
309         free(bmap);
310         free(host_bmap_track);
311         ucall_uninit(vm);
312         kvm_vm_free(vm);
313 }
314
315 static struct vm_guest_modes {
316         enum vm_guest_mode mode;
317         bool supported;
318         bool enabled;
319 } vm_guest_modes[NUM_VM_MODES] = {
320         { VM_MODE_P52V48_4K,    1, 1, },
321 #ifdef __aarch64__
322         { VM_MODE_P52V48_64K,   1, 1, },
323 #else
324         { VM_MODE_P52V48_64K,   0, 0, },
325 #endif
326 };
327
328 static void help(char *name)
329 {
330         int i;
331
332         puts("");
333         printf("usage: %s [-h] [-i iterations] [-I interval] [-m mode]\n", name);
334         puts("");
335         printf(" -i: specify iteration counts (default: %"PRIu64")\n",
336                TEST_HOST_LOOP_N);
337         printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
338                TEST_HOST_LOOP_INTERVAL);
339         printf(" -m: specify the guest mode ID to test "
340                "(default: test all supported modes)\n"
341                "     This option may be used multiple times.\n"
342                "     Guest mode IDs:\n");
343         for (i = 0; i < NUM_VM_MODES; ++i) {
344                 printf("         %d:    %s%s\n",
345                        vm_guest_modes[i].mode,
346                        vm_guest_mode_string(vm_guest_modes[i].mode),
347                        vm_guest_modes[i].supported ? " (supported)" : "");
348         }
349         puts("");
350         exit(0);
351 }
352
353 int main(int argc, char *argv[])
354 {
355         unsigned long iterations = TEST_HOST_LOOP_N;
356         unsigned long interval = TEST_HOST_LOOP_INTERVAL;
357         bool mode_selected = false;
358         unsigned int mode;
359         int opt, i;
360
361         while ((opt = getopt(argc, argv, "hi:I:m:")) != -1) {
362                 switch (opt) {
363                 case 'i':
364                         iterations = strtol(optarg, NULL, 10);
365                         break;
366                 case 'I':
367                         interval = strtol(optarg, NULL, 10);
368                         break;
369                 case 'm':
370                         if (!mode_selected) {
371                                 for (i = 0; i < NUM_VM_MODES; ++i)
372                                         vm_guest_modes[i].enabled = 0;
373                                 mode_selected = true;
374                         }
375                         mode = strtoul(optarg, NULL, 10);
376                         TEST_ASSERT(mode < NUM_VM_MODES,
377                                     "Guest mode ID %d too big", mode);
378                         vm_guest_modes[mode].enabled = 1;
379                         break;
380                 case 'h':
381                 default:
382                         help(argv[0]);
383                         break;
384                 }
385         }
386
387         TEST_ASSERT(iterations > 2, "Iterations must be greater than two");
388         TEST_ASSERT(interval > 0, "Interval must be greater than zero");
389
390         DEBUG("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
391               iterations, interval);
392
393         srandom(time(0));
394
395         for (i = 0; i < NUM_VM_MODES; ++i) {
396                 if (!vm_guest_modes[i].enabled)
397                         continue;
398                 TEST_ASSERT(vm_guest_modes[i].supported,
399                             "Guest mode ID %d (%s) not supported.",
400                             vm_guest_modes[i].mode,
401                             vm_guest_mode_string(vm_guest_modes[i].mode));
402                 run_test(vm_guest_modes[i].mode, iterations, interval);
403         }
404
405         return 0;
406 }