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 */
12 #include <linux/bitmap.h>
13 #include <linux/bitops.h>
15 #include "test_util.h"
17 #include "processor.h"
22 /* The memory slot index to track dirty pages */
23 #define TEST_MEM_SLOT_INDEX 1
24 #define TEST_MEM_SIZE 3
26 /* L1 guest test virtual memory offset */
27 #define GUEST_TEST_MEM 0xc0000000
29 /* L2 guest test virtual memory offset */
30 #define NESTED_TEST_MEM1 0xc0001000
31 #define NESTED_TEST_MEM2 0xc0002000
33 static void l2_guest_code(void)
35 *(volatile uint64_t *)NESTED_TEST_MEM1
;
36 *(volatile uint64_t *)NESTED_TEST_MEM1
= 1;
40 *(volatile uint64_t *)NESTED_TEST_MEM2
= 1;
42 *(volatile uint64_t *)NESTED_TEST_MEM2
= 1;
46 /* Exit to L1 and never come back. */
50 void l1_guest_code(struct vmx_pages
*vmx
)
52 #define L2_GUEST_STACK_SIZE 64
53 unsigned long l2_guest_stack
[L2_GUEST_STACK_SIZE
];
55 GUEST_ASSERT(vmx
->vmcs_gpa
);
56 GUEST_ASSERT(prepare_for_vmx_operation(vmx
));
57 GUEST_ASSERT(load_vmcs(vmx
));
59 prepare_vmcs(vmx
, l2_guest_code
,
60 &l2_guest_stack
[L2_GUEST_STACK_SIZE
]);
63 GUEST_ASSERT(!vmlaunch());
65 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
69 int main(int argc
, char *argv
[])
71 vm_vaddr_t vmx_pages_gva
= 0;
72 struct vmx_pages
*vmx
;
74 uint64_t *host_test_mem
;
81 nested_vmx_check_supported();
84 vm
= vm_create_default(VCPU_ID
, 0, l1_guest_code
);
85 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
86 vmx
= vcpu_alloc_vmx(vm
, &vmx_pages_gva
);
87 vcpu_args_set(vm
, VCPU_ID
, 1, vmx_pages_gva
);
88 run
= vcpu_state(vm
, VCPU_ID
);
90 /* Add an extra memory slot for testing dirty logging */
91 vm_userspace_mem_region_add(vm
, VM_MEM_SRC_ANONYMOUS
,
95 KVM_MEM_LOG_DIRTY_PAGES
);
98 * Add an identity map for GVA range [0xc0000000, 0xc0002000). This
99 * affects both L1 and L2. However...
101 virt_map(vm
, GUEST_TEST_MEM
, GUEST_TEST_MEM
,
102 TEST_MEM_SIZE
* 4096, 0);
105 * ... pages in the L2 GPA range [0xc0001000, 0xc0003000) will map to
108 * Note that prepare_eptp should be called only L1's GPA map is done,
109 * meaning after the last call to virt_map.
111 prepare_eptp(vmx
, vm
, 0);
112 nested_map_memslot(vmx
, vm
, 0, 0);
113 nested_map(vmx
, vm
, NESTED_TEST_MEM1
, GUEST_TEST_MEM
, 4096, 0);
114 nested_map(vmx
, vm
, NESTED_TEST_MEM2
, GUEST_TEST_MEM
, 4096, 0);
116 bmap
= bitmap_alloc(TEST_MEM_SIZE
);
117 host_test_mem
= addr_gpa2hva(vm
, GUEST_TEST_MEM
);
120 memset(host_test_mem
, 0xaa, TEST_MEM_SIZE
* 4096);
121 _vcpu_run(vm
, VCPU_ID
);
122 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
123 "Unexpected exit reason: %u (%s),\n",
125 exit_reason_str(run
->exit_reason
));
127 switch (get_ucall(vm
, VCPU_ID
, &uc
)) {
129 TEST_ASSERT(false, "%s at %s:%d", (const char *)uc
.args
[0],
130 __FILE__
, uc
.args
[1]);
134 * The nested guest wrote at offset 0x1000 in the memslot, but the
135 * dirty bitmap must be filled in according to L1 GPA, not L2.
137 kvm_vm_get_dirty_log(vm
, TEST_MEM_SLOT_INDEX
, bmap
);
139 TEST_ASSERT(test_bit(0, bmap
), "Page 0 incorrectly reported clean\n");
140 TEST_ASSERT(host_test_mem
[0] == 1, "Page 0 not written by guest\n");
142 TEST_ASSERT(!test_bit(0, bmap
), "Page 0 incorrectly reported dirty\n");
143 TEST_ASSERT(host_test_mem
[0] == 0xaaaaaaaaaaaaaaaaULL
, "Page 0 written by guest\n");
146 TEST_ASSERT(!test_bit(1, bmap
), "Page 1 incorrectly reported dirty\n");
147 TEST_ASSERT(host_test_mem
[4096 / 8] == 0xaaaaaaaaaaaaaaaaULL
, "Page 1 written by guest\n");
148 TEST_ASSERT(!test_bit(2, bmap
), "Page 2 incorrectly reported dirty\n");
149 TEST_ASSERT(host_test_mem
[8192 / 8] == 0xaaaaaaaaaaaaaaaaULL
, "Page 2 written by guest\n");
155 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc
.cmd
);