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 <linux/bitmap.h>
13 #include <linux/bitops.h>
14
15 #include "test_util.h"
16 #include "kvm_util.h"
17 #include "processor.h"
18 #include "vmx.h"
19
20 /* The memory slot index to track dirty pages */
21 #define TEST_MEM_SLOT_INDEX 1
22 #define TEST_MEM_PAGES 3
23
24 /* L1 guest test virtual memory offset */
25 #define GUEST_TEST_MEM 0xc0000000
26
27 /* L2 guest test virtual memory offset */
28 #define NESTED_TEST_MEM1 0xc0001000
29 #define NESTED_TEST_MEM2 0xc0002000
30
l2_guest_code(void)31 static void l2_guest_code(void)
32 {
33 *(volatile uint64_t *)NESTED_TEST_MEM1;
34 *(volatile uint64_t *)NESTED_TEST_MEM1 = 1;
35 GUEST_SYNC(true);
36 GUEST_SYNC(false);
37
38 *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
39 GUEST_SYNC(true);
40 *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
41 GUEST_SYNC(true);
42 GUEST_SYNC(false);
43
44 /* Exit to L1 and never come back. */
45 vmcall();
46 }
47
l1_guest_code(struct vmx_pages * vmx)48 void l1_guest_code(struct vmx_pages *vmx)
49 {
50 #define L2_GUEST_STACK_SIZE 64
51 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
52
53 GUEST_ASSERT(vmx->vmcs_gpa);
54 GUEST_ASSERT(prepare_for_vmx_operation(vmx));
55 GUEST_ASSERT(load_vmcs(vmx));
56
57 prepare_vmcs(vmx, l2_guest_code,
58 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
59
60 GUEST_SYNC(false);
61 GUEST_ASSERT(!vmlaunch());
62 GUEST_SYNC(false);
63 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
64 GUEST_DONE();
65 }
66
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69 vm_vaddr_t vmx_pages_gva = 0;
70 struct vmx_pages *vmx;
71 unsigned long *bmap;
72 uint64_t *host_test_mem;
73
74 struct kvm_vcpu *vcpu;
75 struct kvm_vm *vm;
76 struct kvm_run *run;
77 struct ucall uc;
78 bool done = false;
79
80 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
81
82 /* Create VM */
83 vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
84 vmx = vcpu_alloc_vmx(vm, &vmx_pages_gva);
85 vcpu_args_set(vcpu, 1, vmx_pages_gva);
86 run = vcpu->run;
87
88 /* Add an extra memory slot for testing dirty logging */
89 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
90 GUEST_TEST_MEM,
91 TEST_MEM_SLOT_INDEX,
92 TEST_MEM_PAGES,
93 KVM_MEM_LOG_DIRTY_PAGES);
94
95 /*
96 * Add an identity map for GVA range [0xc0000000, 0xc0002000). This
97 * affects both L1 and L2. However...
98 */
99 virt_map(vm, GUEST_TEST_MEM, GUEST_TEST_MEM, TEST_MEM_PAGES);
100
101 /*
102 * ... pages in the L2 GPA range [0xc0001000, 0xc0003000) will map to
103 * 0xc0000000.
104 *
105 * Note that prepare_eptp should be called only L1's GPA map is done,
106 * meaning after the last call to virt_map.
107 */
108 prepare_eptp(vmx, vm, 0);
109 nested_map_memslot(vmx, vm, 0);
110 nested_map(vmx, vm, NESTED_TEST_MEM1, GUEST_TEST_MEM, 4096);
111 nested_map(vmx, vm, NESTED_TEST_MEM2, GUEST_TEST_MEM, 4096);
112
113 bmap = bitmap_zalloc(TEST_MEM_PAGES);
114 host_test_mem = addr_gpa2hva(vm, GUEST_TEST_MEM);
115
116 while (!done) {
117 memset(host_test_mem, 0xaa, TEST_MEM_PAGES * 4096);
118 vcpu_run(vcpu);
119 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
120 "Unexpected exit reason: %u (%s),\n",
121 run->exit_reason,
122 exit_reason_str(run->exit_reason));
123
124 switch (get_ucall(vcpu, &uc)) {
125 case UCALL_ABORT:
126 REPORT_GUEST_ASSERT(uc);
127 /* NOT REACHED */
128 case UCALL_SYNC:
129 /*
130 * The nested guest wrote at offset 0x1000 in the memslot, but the
131 * dirty bitmap must be filled in according to L1 GPA, not L2.
132 */
133 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
134 if (uc.args[1]) {
135 TEST_ASSERT(test_bit(0, bmap), "Page 0 incorrectly reported clean\n");
136 TEST_ASSERT(host_test_mem[0] == 1, "Page 0 not written by guest\n");
137 } else {
138 TEST_ASSERT(!test_bit(0, bmap), "Page 0 incorrectly reported dirty\n");
139 TEST_ASSERT(host_test_mem[0] == 0xaaaaaaaaaaaaaaaaULL, "Page 0 written by guest\n");
140 }
141
142 TEST_ASSERT(!test_bit(1, bmap), "Page 1 incorrectly reported dirty\n");
143 TEST_ASSERT(host_test_mem[4096 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 1 written by guest\n");
144 TEST_ASSERT(!test_bit(2, bmap), "Page 2 incorrectly reported dirty\n");
145 TEST_ASSERT(host_test_mem[8192 / 8] == 0xaaaaaaaaaaaaaaaaULL, "Page 2 written by guest\n");
146 break;
147 case UCALL_DONE:
148 done = true;
149 break;
150 default:
151 TEST_FAIL("Unknown ucall %lu", uc.cmd);
152 }
153 }
154 }
155