@@ -31,6 +31,11 @@ static uint64_t *guest_test_memory = (uint64_t *)TEST_GVA;
31
31
#define CMD_SKIP_TEST (1ULL << 1)
32
32
#define CMD_HOLE_PT (1ULL << 2)
33
33
#define CMD_HOLE_DATA (1ULL << 3)
34
+ #define CMD_CHECK_WRITE_IN_DIRTY_LOG (1ULL << 4)
35
+ #define CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG (1ULL << 5)
36
+ #define CMD_CHECK_NO_WRITE_IN_DIRTY_LOG (1ULL << 6)
37
+ #define CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG (1ULL << 7)
38
+ #define CMD_SET_PTE_AF (1ULL << 8)
34
39
35
40
#define PREPARE_FN_NR 10
36
41
#define CHECK_FN_NR 10
@@ -213,6 +218,21 @@ static void guest_check_pte_af(void)
213
218
GUEST_ASSERT_EQ (* ((uint64_t * )TEST_PTE_GVA ) & PTE_AF , PTE_AF );
214
219
}
215
220
221
+ static void guest_check_write_in_dirty_log (void )
222
+ {
223
+ GUEST_SYNC (CMD_CHECK_WRITE_IN_DIRTY_LOG );
224
+ }
225
+
226
+ static void guest_check_no_write_in_dirty_log (void )
227
+ {
228
+ GUEST_SYNC (CMD_CHECK_NO_WRITE_IN_DIRTY_LOG );
229
+ }
230
+
231
+ static void guest_check_s1ptw_wr_in_dirty_log (void )
232
+ {
233
+ GUEST_SYNC (CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG );
234
+ }
235
+
216
236
static void guest_exec (void )
217
237
{
218
238
int (* code )(void ) = (int (* )(void ))TEST_EXEC_GVA ;
@@ -395,6 +415,22 @@ static bool punch_hole_in_backing_store(struct kvm_vm *vm,
395
415
return true;
396
416
}
397
417
418
+ static bool check_write_in_dirty_log (struct kvm_vm * vm ,
419
+ struct userspace_mem_region * region ,
420
+ uint64_t host_pg_nr )
421
+ {
422
+ unsigned long * bmap ;
423
+ bool first_page_dirty ;
424
+ uint64_t size = region -> region .memory_size ;
425
+
426
+ /* getpage_size() is not always equal to vm->page_size */
427
+ bmap = bitmap_zalloc (size / getpagesize ());
428
+ kvm_vm_get_dirty_log (vm , region -> region .slot , bmap );
429
+ first_page_dirty = test_bit (host_pg_nr , bmap );
430
+ free (bmap );
431
+ return first_page_dirty ;
432
+ }
433
+
398
434
/* Returns true to continue the test, and false if it should be skipped. */
399
435
static bool handle_cmd (struct kvm_vm * vm , int cmd )
400
436
{
@@ -411,6 +447,18 @@ static bool handle_cmd(struct kvm_vm *vm, int cmd)
411
447
continue_test = punch_hole_in_backing_store (vm , pt_region );
412
448
if (cmd & CMD_HOLE_DATA )
413
449
continue_test = punch_hole_in_backing_store (vm , data_region );
450
+ if (cmd & CMD_CHECK_WRITE_IN_DIRTY_LOG )
451
+ TEST_ASSERT (check_write_in_dirty_log (vm , data_region , 0 ),
452
+ "Missing write in dirty log" );
453
+ if (cmd & CMD_CHECK_S1PTW_WR_IN_DIRTY_LOG )
454
+ TEST_ASSERT (check_write_in_dirty_log (vm , pt_region , 0 ),
455
+ "Missing s1ptw write in dirty log" );
456
+ if (cmd & CMD_CHECK_NO_WRITE_IN_DIRTY_LOG )
457
+ TEST_ASSERT (!check_write_in_dirty_log (vm , data_region , 0 ),
458
+ "Unexpected write in dirty log" );
459
+ if (cmd & CMD_CHECK_NO_S1PTW_WR_IN_DIRTY_LOG )
460
+ TEST_ASSERT (!check_write_in_dirty_log (vm , pt_region , 0 ),
461
+ "Unexpected s1ptw write in dirty log" );
414
462
415
463
return continue_test ;
416
464
}
@@ -673,6 +721,19 @@ static void help(char *name)
673
721
.expected_events = { .uffd_faults = _uffd_faults, }, \
674
722
}
675
723
724
+ #define TEST_DIRTY_LOG (_access , _with_af , _test_check ) \
725
+ { \
726
+ .name = SCAT3(dirty_log, _access, _with_af), \
727
+ .data_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \
728
+ .pt_memslot_flags = KVM_MEM_LOG_DIRTY_PAGES, \
729
+ .guest_prepare = { _PREPARE(_with_af), \
730
+ _PREPARE(_access) }, \
731
+ .guest_test = _access, \
732
+ .guest_test_check = { _CHECK(_with_af), _test_check, \
733
+ guest_check_s1ptw_wr_in_dirty_log}, \
734
+ .expected_events = { 0 }, \
735
+ }
736
+
676
737
static struct test_desc tests [] = {
677
738
678
739
/* Check that HW is setting the Access Flag (AF) (sanity checks). */
@@ -732,6 +793,21 @@ static struct test_desc tests[] = {
732
793
TEST_UFFD (guest_exec , with_af , CMD_HOLE_DATA | CMD_HOLE_PT ,
733
794
uffd_data_read_handler , uffd_pt_write_handler , 2 ),
734
795
796
+ /*
797
+ * Try accesses when the data and PT memory regions are both
798
+ * tracked for dirty logging.
799
+ */
800
+ TEST_DIRTY_LOG (guest_read64 , with_af , guest_check_no_write_in_dirty_log ),
801
+ /* no_af should also lead to a PT write. */
802
+ TEST_DIRTY_LOG (guest_read64 , no_af , guest_check_no_write_in_dirty_log ),
803
+ TEST_DIRTY_LOG (guest_ld_preidx , with_af , guest_check_no_write_in_dirty_log ),
804
+ TEST_DIRTY_LOG (guest_at , no_af , guest_check_no_write_in_dirty_log ),
805
+ TEST_DIRTY_LOG (guest_exec , with_af , guest_check_no_write_in_dirty_log ),
806
+ TEST_DIRTY_LOG (guest_write64 , with_af , guest_check_write_in_dirty_log ),
807
+ TEST_DIRTY_LOG (guest_cas , with_af , guest_check_write_in_dirty_log ),
808
+ TEST_DIRTY_LOG (guest_dc_zva , with_af , guest_check_write_in_dirty_log ),
809
+ TEST_DIRTY_LOG (guest_st_preidx , with_af , guest_check_write_in_dirty_log ),
810
+
735
811
{ 0 }
736
812
};
737
813
0 commit comments