1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Test for s390x CMMA migration
4 *
5 * Copyright IBM Corp. 2023
6 *
7 * Authors:
8 * Nico Boehr <nrb@linux.ibm.com>
9 */
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/ioctl.h>
15
16 #include "test_util.h"
17 #include "kvm_util.h"
18 #include "kselftest.h"
19 #include "ucall_common.h"
20 #include "processor.h"
21
22 #define MAIN_PAGE_COUNT 512
23
24 #define TEST_DATA_PAGE_COUNT 512
25 #define TEST_DATA_MEMSLOT 1
26 #define TEST_DATA_START_GFN PAGE_SIZE
27
28 #define TEST_DATA_TWO_PAGE_COUNT 256
29 #define TEST_DATA_TWO_MEMSLOT 2
30 #define TEST_DATA_TWO_START_GFN (2 * PAGE_SIZE)
31
32 static char cmma_value_buf[MAIN_PAGE_COUNT + TEST_DATA_PAGE_COUNT];
33
34 /**
35 * Dirty CMMA attributes of exactly one page in the TEST_DATA memslot,
36 * so use_cmma goes on and the CMMA related ioctls do something.
37 * Touch the page at offset 1M inside TEST_DATA to make sure its page
38 * tables are allocated in the host.
39 */
guest_do_one_essa(void)40 static void guest_do_one_essa(void)
41 {
42 asm volatile(
43 /* load TEST_DATA_START_GFN into r1 */
44 " xgr 1,1\n"
45 " llilf 1,%[start_gfn]\n"
46 /* calculate the address from the gfn */
47 " sllg 1,1,12(0)\n"
48 /* set the first page in TEST_DATA memslot to STABLE */
49 " .insn rrf,0xb9ab0000,2,1,1,0\n"
50 " agfi 1,0x100000\n"
51 /* also touch the first page of the second MB of TEST_DATA */
52 " .insn rrf,0xb9ab0000,2,1,1,0\n"
53 /* hypercall */
54 " diag 0,0,0x501\n"
55 "0: j 0b"
56 :
57 : [start_gfn] "L"(TEST_DATA_START_GFN)
58 : "r1", "r2", "memory", "cc"
59 );
60 }
61
62 /**
63 * Touch CMMA attributes of all pages in TEST_DATA memslot. Set them to stable
64 * state.
65 */
guest_dirty_test_data(void)66 static void guest_dirty_test_data(void)
67 {
68 asm volatile(
69 /* r1 = TEST_DATA_START_GFN */
70 " xgr 1,1\n"
71 " llilf 1,%[start_gfn]\n"
72 /* r5 = TEST_DATA_PAGE_COUNT */
73 " lghi 5,%[page_count]\n"
74 /* r5 += r1 */
75 "2: agfr 5,1\n"
76 /* r2 = r1 << PAGE_SHIFT */
77 "1: sllg 2,1,12(0)\n"
78 /* essa(r4, r2, SET_STABLE) */
79 " .insn rrf,0xb9ab0000,4,2,1,0\n"
80 /* i++ */
81 " agfi 1,1\n"
82 /* if r1 < r5 goto 1 */
83 " cgrjl 1,5,1b\n"
84 /* hypercall */
85 " diag 0,0,0x501\n"
86 "0: j 0b"
87 :
88 : [start_gfn] "L"(TEST_DATA_START_GFN),
89 [page_count] "L"(TEST_DATA_PAGE_COUNT)
90 :
91 /* the counter in our loop over the pages */
92 "r1",
93 /* the calculated page physical address */
94 "r2",
95 /* ESSA output register */
96 "r4",
97 /* last page */
98 "r5",
99 "cc", "memory"
100 );
101 }
102
create_main_memslot(struct kvm_vm * vm)103 static void create_main_memslot(struct kvm_vm *vm)
104 {
105 int i;
106
107 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, MAIN_PAGE_COUNT, 0);
108 /* set the array of memslots to zero like __vm_create does */
109 for (i = 0; i < NR_MEM_REGIONS; i++)
110 vm->memslots[i] = 0;
111 }
112
create_test_memslot(struct kvm_vm * vm)113 static void create_test_memslot(struct kvm_vm *vm)
114 {
115 vm_userspace_mem_region_add(vm,
116 VM_MEM_SRC_ANONYMOUS,
117 TEST_DATA_START_GFN << vm->page_shift,
118 TEST_DATA_MEMSLOT,
119 TEST_DATA_PAGE_COUNT,
120 0
121 );
122 vm->memslots[MEM_REGION_TEST_DATA] = TEST_DATA_MEMSLOT;
123 }
124
create_memslots(struct kvm_vm * vm)125 static void create_memslots(struct kvm_vm *vm)
126 {
127 /*
128 * Our VM has the following memory layout:
129 * +------+---------------------------+
130 * | GFN | Memslot |
131 * +------+---------------------------+
132 * | 0 | |
133 * | ... | MAIN (Code, Stack, ...) |
134 * | 511 | |
135 * +------+---------------------------+
136 * | 4096 | |
137 * | ... | TEST_DATA |
138 * | 4607 | |
139 * +------+---------------------------+
140 */
141 create_main_memslot(vm);
142 create_test_memslot(vm);
143 }
144
finish_vm_setup(struct kvm_vm * vm)145 static void finish_vm_setup(struct kvm_vm *vm)
146 {
147 struct userspace_mem_region *slot0;
148
149 kvm_vm_elf_load(vm, program_invocation_name);
150
151 slot0 = memslot2region(vm, 0);
152 ucall_init(vm, slot0->region.guest_phys_addr + slot0->region.memory_size);
153
154 kvm_arch_vm_post_create(vm, 0);
155 }
156
create_vm_two_memslots(void)157 static struct kvm_vm *create_vm_two_memslots(void)
158 {
159 struct kvm_vm *vm;
160
161 vm = vm_create_barebones();
162
163 create_memslots(vm);
164
165 finish_vm_setup(vm);
166
167 return vm;
168 }
169
enable_cmma(struct kvm_vm * vm)170 static void enable_cmma(struct kvm_vm *vm)
171 {
172 int r;
173
174 r = __kvm_device_attr_set(vm->fd, KVM_S390_VM_MEM_CTRL, KVM_S390_VM_MEM_ENABLE_CMMA, NULL);
175 TEST_ASSERT(!r, "enabling cmma failed r=%d errno=%d", r, errno);
176 }
177
enable_dirty_tracking(struct kvm_vm * vm)178 static void enable_dirty_tracking(struct kvm_vm *vm)
179 {
180 vm_mem_region_set_flags(vm, 0, KVM_MEM_LOG_DIRTY_PAGES);
181 vm_mem_region_set_flags(vm, TEST_DATA_MEMSLOT, KVM_MEM_LOG_DIRTY_PAGES);
182 }
183
__enable_migration_mode(struct kvm_vm * vm)184 static int __enable_migration_mode(struct kvm_vm *vm)
185 {
186 return __kvm_device_attr_set(vm->fd,
187 KVM_S390_VM_MIGRATION,
188 KVM_S390_VM_MIGRATION_START,
189 NULL
190 );
191 }
192
enable_migration_mode(struct kvm_vm * vm)193 static void enable_migration_mode(struct kvm_vm *vm)
194 {
195 int r = __enable_migration_mode(vm);
196
197 TEST_ASSERT(!r, "enabling migration mode failed r=%d errno=%d", r, errno);
198 }
199
is_migration_mode_on(struct kvm_vm * vm)200 static bool is_migration_mode_on(struct kvm_vm *vm)
201 {
202 u64 out;
203 int r;
204
205 r = __kvm_device_attr_get(vm->fd,
206 KVM_S390_VM_MIGRATION,
207 KVM_S390_VM_MIGRATION_STATUS,
208 &out
209 );
210 TEST_ASSERT(!r, "getting migration mode status failed r=%d errno=%d", r, errno);
211 return out;
212 }
213
vm_get_cmma_bits(struct kvm_vm * vm,u64 flags,int * errno_out)214 static int vm_get_cmma_bits(struct kvm_vm *vm, u64 flags, int *errno_out)
215 {
216 struct kvm_s390_cmma_log args;
217 int rc;
218
219 errno = 0;
220
221 args = (struct kvm_s390_cmma_log){
222 .start_gfn = 0,
223 .count = sizeof(cmma_value_buf),
224 .flags = flags,
225 .values = (__u64)&cmma_value_buf[0]
226 };
227 rc = __vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
228
229 *errno_out = errno;
230 return rc;
231 }
232
test_get_cmma_basic(void)233 static void test_get_cmma_basic(void)
234 {
235 struct kvm_vm *vm = create_vm_two_memslots();
236 struct kvm_vcpu *vcpu;
237 int rc, errno_out;
238
239 /* GET_CMMA_BITS without CMMA enabled should fail */
240 rc = vm_get_cmma_bits(vm, 0, &errno_out);
241 TEST_ASSERT_EQ(rc, -1);
242 TEST_ASSERT_EQ(errno_out, ENXIO);
243
244 enable_cmma(vm);
245 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
246
247 vcpu_run(vcpu);
248
249 /* GET_CMMA_BITS without migration mode and without peeking should fail */
250 rc = vm_get_cmma_bits(vm, 0, &errno_out);
251 TEST_ASSERT_EQ(rc, -1);
252 TEST_ASSERT_EQ(errno_out, EINVAL);
253
254 /* GET_CMMA_BITS without migration mode and with peeking should work */
255 rc = vm_get_cmma_bits(vm, KVM_S390_CMMA_PEEK, &errno_out);
256 TEST_ASSERT_EQ(rc, 0);
257 TEST_ASSERT_EQ(errno_out, 0);
258
259 enable_dirty_tracking(vm);
260 enable_migration_mode(vm);
261
262 /* GET_CMMA_BITS with invalid flags */
263 rc = vm_get_cmma_bits(vm, 0xfeedc0fe, &errno_out);
264 TEST_ASSERT_EQ(rc, -1);
265 TEST_ASSERT_EQ(errno_out, EINVAL);
266
267 kvm_vm_free(vm);
268 }
269
assert_exit_was_hypercall(struct kvm_vcpu * vcpu)270 static void assert_exit_was_hypercall(struct kvm_vcpu *vcpu)
271 {
272 TEST_ASSERT_EQ(vcpu->run->exit_reason, 13);
273 TEST_ASSERT_EQ(vcpu->run->s390_sieic.icptcode, 4);
274 TEST_ASSERT_EQ(vcpu->run->s390_sieic.ipa, 0x8300);
275 TEST_ASSERT_EQ(vcpu->run->s390_sieic.ipb, 0x5010000);
276 }
277
test_migration_mode(void)278 static void test_migration_mode(void)
279 {
280 struct kvm_vm *vm = vm_create_barebones();
281 struct kvm_vcpu *vcpu;
282 u64 orig_psw;
283 int rc;
284
285 /* enabling migration mode on a VM without memory should fail */
286 rc = __enable_migration_mode(vm);
287 TEST_ASSERT_EQ(rc, -1);
288 TEST_ASSERT_EQ(errno, EINVAL);
289 TEST_ASSERT(!is_migration_mode_on(vm), "migration mode should still be off");
290 errno = 0;
291
292 create_memslots(vm);
293 finish_vm_setup(vm);
294
295 enable_cmma(vm);
296 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
297 orig_psw = vcpu->run->psw_addr;
298
299 /*
300 * Execute one essa instruction in the guest. Otherwise the guest will
301 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
302 */
303 vcpu_run(vcpu);
304 assert_exit_was_hypercall(vcpu);
305
306 /* migration mode when memslots have dirty tracking off should fail */
307 rc = __enable_migration_mode(vm);
308 TEST_ASSERT_EQ(rc, -1);
309 TEST_ASSERT_EQ(errno, EINVAL);
310 TEST_ASSERT(!is_migration_mode_on(vm), "migration mode should still be off");
311 errno = 0;
312
313 /* enable dirty tracking */
314 enable_dirty_tracking(vm);
315
316 /* enabling migration mode should work now */
317 rc = __enable_migration_mode(vm);
318 TEST_ASSERT_EQ(rc, 0);
319 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
320 errno = 0;
321
322 /* execute another ESSA instruction to see this goes fine */
323 vcpu->run->psw_addr = orig_psw;
324 vcpu_run(vcpu);
325 assert_exit_was_hypercall(vcpu);
326
327 /*
328 * With migration mode on, create a new memslot with dirty tracking off.
329 * This should turn off migration mode.
330 */
331 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
332 vm_userspace_mem_region_add(vm,
333 VM_MEM_SRC_ANONYMOUS,
334 TEST_DATA_TWO_START_GFN << vm->page_shift,
335 TEST_DATA_TWO_MEMSLOT,
336 TEST_DATA_TWO_PAGE_COUNT,
337 0
338 );
339 TEST_ASSERT(!is_migration_mode_on(vm),
340 "creating memslot without dirty tracking turns off migration mode"
341 );
342
343 /* ESSA instructions should still execute fine */
344 vcpu->run->psw_addr = orig_psw;
345 vcpu_run(vcpu);
346 assert_exit_was_hypercall(vcpu);
347
348 /*
349 * Turn on dirty tracking on the new memslot.
350 * It should be possible to turn migration mode back on again.
351 */
352 vm_mem_region_set_flags(vm, TEST_DATA_TWO_MEMSLOT, KVM_MEM_LOG_DIRTY_PAGES);
353 rc = __enable_migration_mode(vm);
354 TEST_ASSERT_EQ(rc, 0);
355 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
356 errno = 0;
357
358 /*
359 * Turn off dirty tracking again, this time with just a flag change.
360 * Again, migration mode should turn off.
361 */
362 TEST_ASSERT(is_migration_mode_on(vm), "migration mode should be on");
363 vm_mem_region_set_flags(vm, TEST_DATA_TWO_MEMSLOT, 0);
364 TEST_ASSERT(!is_migration_mode_on(vm),
365 "disabling dirty tracking should turn off migration mode"
366 );
367
368 /* ESSA instructions should still execute fine */
369 vcpu->run->psw_addr = orig_psw;
370 vcpu_run(vcpu);
371 assert_exit_was_hypercall(vcpu);
372
373 kvm_vm_free(vm);
374 }
375
376 /**
377 * Given a VM with the MAIN and TEST_DATA memslot, assert that both slots have
378 * CMMA attributes of all pages in both memslots and nothing more dirty.
379 * This has the useful side effect of ensuring nothing is CMMA dirty after this
380 * function.
381 */
assert_all_slots_cmma_dirty(struct kvm_vm * vm)382 static void assert_all_slots_cmma_dirty(struct kvm_vm *vm)
383 {
384 struct kvm_s390_cmma_log args;
385
386 /*
387 * First iteration - everything should be dirty.
388 * Start at the main memslot...
389 */
390 args = (struct kvm_s390_cmma_log){
391 .start_gfn = 0,
392 .count = sizeof(cmma_value_buf),
393 .flags = 0,
394 .values = (__u64)&cmma_value_buf[0]
395 };
396 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
397 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
398 TEST_ASSERT_EQ(args.count, MAIN_PAGE_COUNT);
399 TEST_ASSERT_EQ(args.remaining, TEST_DATA_PAGE_COUNT);
400 TEST_ASSERT_EQ(args.start_gfn, 0);
401
402 /* ...and then - after a hole - the TEST_DATA memslot should follow */
403 args = (struct kvm_s390_cmma_log){
404 .start_gfn = MAIN_PAGE_COUNT,
405 .count = sizeof(cmma_value_buf),
406 .flags = 0,
407 .values = (__u64)&cmma_value_buf[0]
408 };
409 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
410 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
411 TEST_ASSERT_EQ(args.count, TEST_DATA_PAGE_COUNT);
412 TEST_ASSERT_EQ(args.start_gfn, TEST_DATA_START_GFN);
413 TEST_ASSERT_EQ(args.remaining, 0);
414
415 /* ...and nothing else should be there */
416 args = (struct kvm_s390_cmma_log){
417 .start_gfn = TEST_DATA_START_GFN + TEST_DATA_PAGE_COUNT,
418 .count = sizeof(cmma_value_buf),
419 .flags = 0,
420 .values = (__u64)&cmma_value_buf[0]
421 };
422 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
423 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
424 TEST_ASSERT_EQ(args.count, 0);
425 TEST_ASSERT_EQ(args.start_gfn, 0);
426 TEST_ASSERT_EQ(args.remaining, 0);
427 }
428
429 /**
430 * Given a VM, assert no pages are CMMA dirty.
431 */
assert_no_pages_cmma_dirty(struct kvm_vm * vm)432 static void assert_no_pages_cmma_dirty(struct kvm_vm *vm)
433 {
434 struct kvm_s390_cmma_log args;
435
436 /* If we start from GFN 0 again, nothing should be dirty. */
437 args = (struct kvm_s390_cmma_log){
438 .start_gfn = 0,
439 .count = sizeof(cmma_value_buf),
440 .flags = 0,
441 .values = (__u64)&cmma_value_buf[0]
442 };
443 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
444 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, &args);
445 if (args.count || args.remaining || args.start_gfn)
446 TEST_FAIL("pages are still dirty start_gfn=0x%llx count=%u remaining=%llu",
447 args.start_gfn,
448 args.count,
449 args.remaining
450 );
451 }
452
test_get_initial_dirty(void)453 static void test_get_initial_dirty(void)
454 {
455 struct kvm_vm *vm = create_vm_two_memslots();
456 struct kvm_vcpu *vcpu;
457
458 enable_cmma(vm);
459 vcpu = vm_vcpu_add(vm, 1, guest_do_one_essa);
460
461 /*
462 * Execute one essa instruction in the guest. Otherwise the guest will
463 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
464 */
465 vcpu_run(vcpu);
466 assert_exit_was_hypercall(vcpu);
467
468 enable_dirty_tracking(vm);
469 enable_migration_mode(vm);
470
471 assert_all_slots_cmma_dirty(vm);
472
473 /* Start from the beginning again and make sure nothing else is dirty */
474 assert_no_pages_cmma_dirty(vm);
475
476 kvm_vm_free(vm);
477 }
478
query_cmma_range(struct kvm_vm * vm,u64 start_gfn,u64 gfn_count,struct kvm_s390_cmma_log * res_out)479 static void query_cmma_range(struct kvm_vm *vm,
480 u64 start_gfn, u64 gfn_count,
481 struct kvm_s390_cmma_log *res_out)
482 {
483 *res_out = (struct kvm_s390_cmma_log){
484 .start_gfn = start_gfn,
485 .count = gfn_count,
486 .flags = 0,
487 .values = (__u64)&cmma_value_buf[0]
488 };
489 memset(cmma_value_buf, 0xff, sizeof(cmma_value_buf));
490 vm_ioctl(vm, KVM_S390_GET_CMMA_BITS, res_out);
491 }
492
493 /**
494 * Assert the given cmma_log struct that was executed by query_cmma_range()
495 * indicates the first dirty gfn is at first_dirty_gfn and contains exactly
496 * dirty_gfn_count CMMA values.
497 */
assert_cmma_dirty(u64 first_dirty_gfn,u64 dirty_gfn_count,const struct kvm_s390_cmma_log * res)498 static void assert_cmma_dirty(u64 first_dirty_gfn,
499 u64 dirty_gfn_count,
500 const struct kvm_s390_cmma_log *res)
501 {
502 TEST_ASSERT_EQ(res->start_gfn, first_dirty_gfn);
503 TEST_ASSERT_EQ(res->count, dirty_gfn_count);
504 for (size_t i = 0; i < dirty_gfn_count; i++)
505 TEST_ASSERT_EQ(cmma_value_buf[0], 0x0); /* stable state */
506 TEST_ASSERT_EQ(cmma_value_buf[dirty_gfn_count], 0xff); /* not touched */
507 }
508
test_get_skip_holes(void)509 static void test_get_skip_holes(void)
510 {
511 size_t gfn_offset;
512 struct kvm_vm *vm = create_vm_two_memslots();
513 struct kvm_s390_cmma_log log;
514 struct kvm_vcpu *vcpu;
515 u64 orig_psw;
516
517 enable_cmma(vm);
518 vcpu = vm_vcpu_add(vm, 1, guest_dirty_test_data);
519
520 orig_psw = vcpu->run->psw_addr;
521
522 /*
523 * Execute some essa instructions in the guest. Otherwise the guest will
524 * not have use_cmm enabled and GET_CMMA_BITS will return no pages.
525 */
526 vcpu_run(vcpu);
527 assert_exit_was_hypercall(vcpu);
528
529 enable_dirty_tracking(vm);
530 enable_migration_mode(vm);
531
532 /* un-dirty all pages */
533 assert_all_slots_cmma_dirty(vm);
534
535 /* Then, dirty just the TEST_DATA memslot */
536 vcpu->run->psw_addr = orig_psw;
537 vcpu_run(vcpu);
538
539 gfn_offset = TEST_DATA_START_GFN;
540 /**
541 * Query CMMA attributes of one page, starting at page 0. Since the
542 * main memslot was not touched by the VM, this should yield the first
543 * page of the TEST_DATA memslot.
544 * The dirty bitmap should now look like this:
545 * 0: not dirty
546 * [0x1, 0x200): dirty
547 */
548 query_cmma_range(vm, 0, 1, &log);
549 assert_cmma_dirty(gfn_offset, 1, &log);
550 gfn_offset++;
551
552 /**
553 * Query CMMA attributes of 32 (0x20) pages past the end of the TEST_DATA
554 * memslot. This should wrap back to the beginning of the TEST_DATA
555 * memslot, page 1.
556 * The dirty bitmap should now look like this:
557 * [0, 0x21): not dirty
558 * [0x21, 0x200): dirty
559 */
560 query_cmma_range(vm, TEST_DATA_START_GFN + TEST_DATA_PAGE_COUNT, 0x20, &log);
561 assert_cmma_dirty(gfn_offset, 0x20, &log);
562 gfn_offset += 0x20;
563
564 /* Skip 32 pages */
565 gfn_offset += 0x20;
566
567 /**
568 * After skipping 32 pages, query the next 32 (0x20) pages.
569 * The dirty bitmap should now look like this:
570 * [0, 0x21): not dirty
571 * [0x21, 0x41): dirty
572 * [0x41, 0x61): not dirty
573 * [0x61, 0x200): dirty
574 */
575 query_cmma_range(vm, gfn_offset, 0x20, &log);
576 assert_cmma_dirty(gfn_offset, 0x20, &log);
577 gfn_offset += 0x20;
578
579 /**
580 * Query 1 page from the beginning of the TEST_DATA memslot. This should
581 * yield page 0x21.
582 * The dirty bitmap should now look like this:
583 * [0, 0x22): not dirty
584 * [0x22, 0x41): dirty
585 * [0x41, 0x61): not dirty
586 * [0x61, 0x200): dirty
587 */
588 query_cmma_range(vm, TEST_DATA_START_GFN, 1, &log);
589 assert_cmma_dirty(TEST_DATA_START_GFN + 0x21, 1, &log);
590 gfn_offset++;
591
592 /**
593 * Query 15 (0xF) pages from page 0x23 in TEST_DATA memslot.
594 * This should yield pages [0x23, 0x33).
595 * The dirty bitmap should now look like this:
596 * [0, 0x22): not dirty
597 * 0x22: dirty
598 * [0x23, 0x33): not dirty
599 * [0x33, 0x41): dirty
600 * [0x41, 0x61): not dirty
601 * [0x61, 0x200): dirty
602 */
603 gfn_offset = TEST_DATA_START_GFN + 0x23;
604 query_cmma_range(vm, gfn_offset, 15, &log);
605 assert_cmma_dirty(gfn_offset, 15, &log);
606
607 /**
608 * Query 17 (0x11) pages from page 0x22 in TEST_DATA memslot.
609 * This should yield page [0x22, 0x33)
610 * The dirty bitmap should now look like this:
611 * [0, 0x33): not dirty
612 * [0x33, 0x41): dirty
613 * [0x41, 0x61): not dirty
614 * [0x61, 0x200): dirty
615 */
616 gfn_offset = TEST_DATA_START_GFN + 0x22;
617 query_cmma_range(vm, gfn_offset, 17, &log);
618 assert_cmma_dirty(gfn_offset, 17, &log);
619
620 /**
621 * Query 25 (0x19) pages from page 0x40 in TEST_DATA memslot.
622 * This should yield page 0x40 and nothing more, since there are more
623 * than 16 non-dirty pages after page 0x40.
624 * The dirty bitmap should now look like this:
625 * [0, 0x33): not dirty
626 * [0x33, 0x40): dirty
627 * [0x40, 0x61): not dirty
628 * [0x61, 0x200): dirty
629 */
630 gfn_offset = TEST_DATA_START_GFN + 0x40;
631 query_cmma_range(vm, gfn_offset, 25, &log);
632 assert_cmma_dirty(gfn_offset, 1, &log);
633
634 /**
635 * Query pages [0x33, 0x40).
636 * The dirty bitmap should now look like this:
637 * [0, 0x61): not dirty
638 * [0x61, 0x200): dirty
639 */
640 gfn_offset = TEST_DATA_START_GFN + 0x33;
641 query_cmma_range(vm, gfn_offset, 0x40 - 0x33, &log);
642 assert_cmma_dirty(gfn_offset, 0x40 - 0x33, &log);
643
644 /**
645 * Query the remaining pages [0x61, 0x200).
646 */
647 gfn_offset = TEST_DATA_START_GFN;
648 query_cmma_range(vm, gfn_offset, TEST_DATA_PAGE_COUNT - 0x61, &log);
649 assert_cmma_dirty(TEST_DATA_START_GFN + 0x61, TEST_DATA_PAGE_COUNT - 0x61, &log);
650
651 assert_no_pages_cmma_dirty(vm);
652 }
653
654 struct testdef {
655 const char *name;
656 void (*test)(void);
657 } testlist[] = {
658 { "migration mode and dirty tracking", test_migration_mode },
659 { "GET_CMMA_BITS: basic calls", test_get_cmma_basic },
660 { "GET_CMMA_BITS: all pages are dirty initially", test_get_initial_dirty },
661 { "GET_CMMA_BITS: holes are skipped", test_get_skip_holes },
662 };
663
664 /**
665 * The kernel may support CMMA, but the machine may not (i.e. if running as
666 * guest-3).
667 *
668 * In this case, the CMMA capabilities are all there, but the CMMA-related
669 * ioctls fail. To find out whether the machine supports CMMA, create a
670 * temporary VM and then query the CMMA feature of the VM.
671 */
machine_has_cmma(void)672 static int machine_has_cmma(void)
673 {
674 struct kvm_vm *vm = vm_create_barebones();
675 int r;
676
677 r = !__kvm_has_device_attr(vm->fd, KVM_S390_VM_MEM_CTRL, KVM_S390_VM_MEM_ENABLE_CMMA);
678 kvm_vm_free(vm);
679
680 return r;
681 }
682
main(int argc,char * argv[])683 int main(int argc, char *argv[])
684 {
685 int idx;
686
687 TEST_REQUIRE(kvm_has_cap(KVM_CAP_SYNC_REGS));
688 TEST_REQUIRE(kvm_has_cap(KVM_CAP_S390_CMMA_MIGRATION));
689 TEST_REQUIRE(machine_has_cmma());
690
691 ksft_print_header();
692
693 ksft_set_plan(ARRAY_SIZE(testlist));
694
695 for (idx = 0; idx < ARRAY_SIZE(testlist); idx++) {
696 testlist[idx].test();
697 ksft_test_result_pass("%s\n", testlist[idx].name);
698 }
699
700 ksft_finished(); /* Print results and exit() accordingly */
701 }
702