This source file includes following definitions.
- guest_code
- generate_random_array
- vcpu_worker
- vm_dirty_log_verify
- create_vm
- run_test
- help
- main
1
2
3
4
5
6
7
8 #define _GNU_SOURCE
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 VCPU_ID 1
23
24
25 #define TEST_MEM_SLOT_INDEX 1
26
27
28 #define DEFAULT_GUEST_TEST_MEM 0xc0000000
29
30
31 #define TEST_PAGES_PER_LOOP 1024
32
33
34 #define TEST_HOST_LOOP_N 32UL
35
36
37 #define TEST_HOST_LOOP_INTERVAL 10UL
38
39
40 #if defined(__s390x__)
41 # define BITOP_LE_SWIZZLE ((BITS_PER_LONG-1) & ~0x7)
42 # define test_bit_le(nr, addr) \
43 test_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
44 # define set_bit_le(nr, addr) \
45 set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
46 # define clear_bit_le(nr, addr) \
47 clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
48 # define test_and_set_bit_le(nr, addr) \
49 test_and_set_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
50 # define test_and_clear_bit_le(nr, addr) \
51 test_and_clear_bit((nr) ^ BITOP_LE_SWIZZLE, addr)
52 #else
53 # define test_bit_le test_bit
54 # define set_bit_le set_bit
55 # define clear_bit_le clear_bit
56 # define test_and_set_bit_le test_and_set_bit
57 # define test_and_clear_bit_le test_and_clear_bit
58 #endif
59
60
61
62
63
64
65
66 static uint64_t host_page_size;
67 static uint64_t guest_page_size;
68 static uint64_t guest_num_pages;
69 static uint64_t random_array[TEST_PAGES_PER_LOOP];
70 static uint64_t iteration;
71
72
73
74
75
76
77 static uint64_t guest_test_phys_mem;
78
79
80
81
82
83 static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
84
85
86
87
88
89 static void guest_code(void)
90 {
91 uint64_t addr;
92 int i;
93
94
95
96
97
98
99
100 for (i = 0; i < guest_num_pages; i++) {
101 addr = guest_test_virt_mem + i * guest_page_size;
102 *(uint64_t *)addr = READ_ONCE(iteration);
103 }
104
105 while (true) {
106 for (i = 0; i < TEST_PAGES_PER_LOOP; i++) {
107 addr = guest_test_virt_mem;
108 addr += (READ_ONCE(random_array[i]) % guest_num_pages)
109 * guest_page_size;
110 addr &= ~(host_page_size - 1);
111 *(uint64_t *)addr = READ_ONCE(iteration);
112 }
113
114
115 GUEST_SYNC(1);
116 }
117 }
118
119
120 static bool host_quit;
121
122
123 static void *host_test_mem;
124 static uint64_t host_num_pages;
125
126
127 static uint64_t host_dirty_count;
128 static uint64_t host_clear_count;
129 static uint64_t host_track_next_count;
130
131
132
133
134
135
136
137
138 static unsigned long *host_bmap_track;
139
140 static void generate_random_array(uint64_t *guest_array, uint64_t size)
141 {
142 uint64_t i;
143
144 for (i = 0; i < size; i++)
145 guest_array[i] = random();
146 }
147
148 static void *vcpu_worker(void *data)
149 {
150 int ret;
151 struct kvm_vm *vm = data;
152 uint64_t *guest_array;
153 uint64_t pages_count = 0;
154 struct kvm_run *run;
155
156 run = vcpu_state(vm, VCPU_ID);
157
158 guest_array = addr_gva2hva(vm, (vm_vaddr_t)random_array);
159 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
160
161 while (!READ_ONCE(host_quit)) {
162
163 ret = _vcpu_run(vm, VCPU_ID);
164 TEST_ASSERT(ret == 0, "vcpu_run failed: %d\n", ret);
165 if (get_ucall(vm, VCPU_ID, NULL) == UCALL_SYNC) {
166 pages_count += TEST_PAGES_PER_LOOP;
167 generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
168 } else {
169 TEST_ASSERT(false,
170 "Invalid guest sync status: "
171 "exit_reason=%s\n",
172 exit_reason_str(run->exit_reason));
173 }
174 }
175
176 DEBUG("Dirtied %"PRIu64" pages\n", pages_count);
177
178 return NULL;
179 }
180
181 static void vm_dirty_log_verify(unsigned long *bmap)
182 {
183 uint64_t page;
184 uint64_t *value_ptr;
185 uint64_t step = host_page_size >= guest_page_size ? 1 :
186 guest_page_size / host_page_size;
187
188 for (page = 0; page < host_num_pages; page += step) {
189 value_ptr = host_test_mem + page * host_page_size;
190
191
192 if (test_and_clear_bit_le(page, host_bmap_track)) {
193 host_track_next_count++;
194 TEST_ASSERT(test_bit_le(page, bmap),
195 "Page %"PRIu64" should have its dirty bit "
196 "set in this iteration but it is missing",
197 page);
198 }
199
200 if (test_bit_le(page, bmap)) {
201 host_dirty_count++;
202
203
204
205
206
207 TEST_ASSERT(*value_ptr == iteration ||
208 *value_ptr == iteration - 1,
209 "Set page %"PRIu64" value %"PRIu64
210 " incorrect (iteration=%"PRIu64")",
211 page, *value_ptr, iteration);
212 } else {
213 host_clear_count++;
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 TEST_ASSERT(*value_ptr <= iteration,
234 "Clear page %"PRIu64" value %"PRIu64
235 " incorrect (iteration=%"PRIu64")",
236 page, *value_ptr, iteration);
237 if (*value_ptr == iteration) {
238
239
240
241
242
243 set_bit_le(page, host_bmap_track);
244 }
245 }
246 }
247 }
248
249 static struct kvm_vm *create_vm(enum vm_guest_mode mode, uint32_t vcpuid,
250 uint64_t extra_mem_pages, void *guest_code)
251 {
252 struct kvm_vm *vm;
253 uint64_t extra_pg_pages = extra_mem_pages / 512 * 2;
254
255 vm = _vm_create(mode, DEFAULT_GUEST_PHY_PAGES + extra_pg_pages, O_RDWR);
256 kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
257 #ifdef __x86_64__
258 vm_create_irqchip(vm);
259 #endif
260 vm_vcpu_add_default(vm, vcpuid, guest_code);
261 return vm;
262 }
263
264 #define DIRTY_MEM_BITS 30
265 #define PAGE_SHIFT_4K 12
266
267 static void run_test(enum vm_guest_mode mode, unsigned long iterations,
268 unsigned long interval, uint64_t phys_offset)
269 {
270 pthread_t vcpu_thread;
271 struct kvm_vm *vm;
272 unsigned long *bmap;
273
274
275
276
277
278
279
280
281
282 vm = create_vm(mode, VCPU_ID,
283 2ul << (DIRTY_MEM_BITS - PAGE_SHIFT_4K),
284 guest_code);
285
286 guest_page_size = vm_get_page_size(vm);
287
288
289
290
291 guest_num_pages = (1ul << (DIRTY_MEM_BITS -
292 vm_get_page_shift(vm))) + 16;
293 #ifdef __s390x__
294
295 guest_num_pages = (guest_num_pages + 0xff) & ~0xffUL;
296 #endif
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);
300
301 if (!phys_offset) {
302 guest_test_phys_mem = (vm_get_max_gfn(vm) -
303 guest_num_pages) * guest_page_size;
304 guest_test_phys_mem &= ~(host_page_size - 1);
305 } else {
306 guest_test_phys_mem = phys_offset;
307 }
308
309 #ifdef __s390x__
310
311 guest_test_phys_mem &= ~((1 << 20) - 1);
312 #endif
313
314 DEBUG("guest physical test memory offset: 0x%lx\n", guest_test_phys_mem);
315
316 bmap = bitmap_alloc(host_num_pages);
317 host_bmap_track = bitmap_alloc(host_num_pages);
318
319 #ifdef USE_CLEAR_DIRTY_LOG
320 struct kvm_enable_cap cap = {};
321
322 cap.cap = KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2;
323 cap.args[0] = 1;
324 vm_enable_cap(vm, &cap);
325 #endif
326
327
328 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
329 guest_test_phys_mem,
330 TEST_MEM_SLOT_INDEX,
331 guest_num_pages,
332 KVM_MEM_LOG_DIRTY_PAGES);
333
334
335 virt_map(vm, guest_test_virt_mem, guest_test_phys_mem,
336 guest_num_pages * guest_page_size, 0);
337
338
339 host_test_mem = addr_gpa2hva(vm, (vm_paddr_t)guest_test_phys_mem);
340
341 #ifdef __x86_64__
342 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
343 #endif
344 #ifdef __aarch64__
345 ucall_init(vm, NULL);
346 #endif
347
348
349 sync_global_to_guest(vm, host_page_size);
350 sync_global_to_guest(vm, guest_page_size);
351 sync_global_to_guest(vm, guest_test_virt_mem);
352 sync_global_to_guest(vm, guest_num_pages);
353
354
355 iteration = 1;
356 sync_global_to_guest(vm, iteration);
357 host_quit = false;
358 host_dirty_count = 0;
359 host_clear_count = 0;
360 host_track_next_count = 0;
361
362 pthread_create(&vcpu_thread, NULL, vcpu_worker, vm);
363
364 while (iteration < iterations) {
365
366 usleep(interval * 1000);
367 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
368 #ifdef USE_CLEAR_DIRTY_LOG
369 kvm_vm_clear_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap, 0,
370 host_num_pages);
371 #endif
372 vm_dirty_log_verify(bmap);
373 iteration++;
374 sync_global_to_guest(vm, iteration);
375 }
376
377
378 host_quit = true;
379 pthread_join(vcpu_thread, NULL);
380
381 DEBUG("Total bits checked: dirty (%"PRIu64"), clear (%"PRIu64"), "
382 "track_next (%"PRIu64")\n", host_dirty_count, host_clear_count,
383 host_track_next_count);
384
385 free(bmap);
386 free(host_bmap_track);
387 ucall_uninit(vm);
388 kvm_vm_free(vm);
389 }
390
391 struct vm_guest_mode_params {
392 bool supported;
393 bool enabled;
394 };
395 struct vm_guest_mode_params vm_guest_mode_params[NUM_VM_MODES];
396
397 #define vm_guest_mode_params_init(mode, supported, enabled) \
398 ({ \
399 vm_guest_mode_params[mode] = (struct vm_guest_mode_params){ supported, enabled }; \
400 })
401
402 static void help(char *name)
403 {
404 int i;
405
406 puts("");
407 printf("usage: %s [-h] [-i iterations] [-I interval] "
408 "[-p offset] [-m mode]\n", name);
409 puts("");
410 printf(" -i: specify iteration counts (default: %"PRIu64")\n",
411 TEST_HOST_LOOP_N);
412 printf(" -I: specify interval in ms (default: %"PRIu64" ms)\n",
413 TEST_HOST_LOOP_INTERVAL);
414 printf(" -p: specify guest physical test memory offset\n"
415 " Warning: a low offset can conflict with the loaded test code.\n");
416 printf(" -m: specify the guest mode ID to test "
417 "(default: test all supported modes)\n"
418 " This option may be used multiple times.\n"
419 " Guest mode IDs:\n");
420 for (i = 0; i < NUM_VM_MODES; ++i) {
421 printf(" %d: %s%s\n", i, vm_guest_mode_string(i),
422 vm_guest_mode_params[i].supported ? " (supported)" : "");
423 }
424 puts("");
425 exit(0);
426 }
427
428 int main(int argc, char *argv[])
429 {
430 unsigned long iterations = TEST_HOST_LOOP_N;
431 unsigned long interval = TEST_HOST_LOOP_INTERVAL;
432 bool mode_selected = false;
433 uint64_t phys_offset = 0;
434 unsigned int mode;
435 int opt, i;
436 #ifdef __aarch64__
437 unsigned int host_ipa_limit;
438 #endif
439
440 #ifdef USE_CLEAR_DIRTY_LOG
441 if (!kvm_check_cap(KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2)) {
442 fprintf(stderr, "KVM_CLEAR_DIRTY_LOG not available, skipping tests\n");
443 exit(KSFT_SKIP);
444 }
445 #endif
446
447 #ifdef __x86_64__
448 vm_guest_mode_params_init(VM_MODE_PXXV48_4K, true, true);
449 #endif
450 #ifdef __aarch64__
451 vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
452 vm_guest_mode_params_init(VM_MODE_P40V48_64K, true, true);
453
454 host_ipa_limit = kvm_check_cap(KVM_CAP_ARM_VM_IPA_SIZE);
455 if (host_ipa_limit >= 52)
456 vm_guest_mode_params_init(VM_MODE_P52V48_64K, true, true);
457 if (host_ipa_limit >= 48) {
458 vm_guest_mode_params_init(VM_MODE_P48V48_4K, true, true);
459 vm_guest_mode_params_init(VM_MODE_P48V48_64K, true, true);
460 }
461 #endif
462 #ifdef __s390x__
463 vm_guest_mode_params_init(VM_MODE_P40V48_4K, true, true);
464 #endif
465
466 while ((opt = getopt(argc, argv, "hi:I:p:m:")) != -1) {
467 switch (opt) {
468 case 'i':
469 iterations = strtol(optarg, NULL, 10);
470 break;
471 case 'I':
472 interval = strtol(optarg, NULL, 10);
473 break;
474 case 'p':
475 phys_offset = strtoull(optarg, NULL, 0);
476 break;
477 case 'm':
478 if (!mode_selected) {
479 for (i = 0; i < NUM_VM_MODES; ++i)
480 vm_guest_mode_params[i].enabled = false;
481 mode_selected = true;
482 }
483 mode = strtoul(optarg, NULL, 10);
484 TEST_ASSERT(mode < NUM_VM_MODES,
485 "Guest mode ID %d too big", mode);
486 vm_guest_mode_params[mode].enabled = true;
487 break;
488 case 'h':
489 default:
490 help(argv[0]);
491 break;
492 }
493 }
494
495 TEST_ASSERT(iterations > 2, "Iterations must be greater than two");
496 TEST_ASSERT(interval > 0, "Interval must be greater than zero");
497
498 DEBUG("Test iterations: %"PRIu64", interval: %"PRIu64" (ms)\n",
499 iterations, interval);
500
501 srandom(time(0));
502
503 for (i = 0; i < NUM_VM_MODES; ++i) {
504 if (!vm_guest_mode_params[i].enabled)
505 continue;
506 TEST_ASSERT(vm_guest_mode_params[i].supported,
507 "Guest mode ID %d (%s) not supported.",
508 i, vm_guest_mode_string(i));
509 run_test(i, iterations, interval, phys_offset);
510 }
511
512 return 0;
513 }