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 #define VCPU_ID 1
21
22 /* The memory slot index to track dirty pages */
23 #define TEST_MEM_SLOT_INDEX 1
24 #define TEST_MEM_SIZE 3
25
26 /* L1 guest test virtual memory offset */
27 #define GUEST_TEST_MEM 0xc0000000
28
29 /* L2 guest test virtual memory offset */
30 #define NESTED_TEST_MEM1 0xc0001000
31 #define NESTED_TEST_MEM2 0xc0002000
32
l2_guest_code(void)33 static void l2_guest_code(void)
34 {
35 *(volatile uint64_t *)NESTED_TEST_MEM1;
36 *(volatile uint64_t *)NESTED_TEST_MEM1 = 1;
37 GUEST_SYNC(true);
38 GUEST_SYNC(false);
39
40 *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
41 GUEST_SYNC(true);
42 *(volatile uint64_t *)NESTED_TEST_MEM2 = 1;
43 GUEST_SYNC(true);
44 GUEST_SYNC(false);
45
46 /* Exit to L1 and never come back. */
47 vmcall();
48 }
49
l1_guest_code(struct vmx_pages * vmx)50 void l1_guest_code(struct vmx_pages *vmx)
51 {
52 #define L2_GUEST_STACK_SIZE 64
53 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
54
55 GUEST_ASSERT(vmx->vmcs_gpa);
56 GUEST_ASSERT(prepare_for_vmx_operation(vmx));
57 GUEST_ASSERT(load_vmcs(vmx));
58
59 prepare_vmcs(vmx, l2_guest_code,
60 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
61
62 GUEST_SYNC(false);
63 GUEST_ASSERT(!vmlaunch());
64 GUEST_SYNC(false);
65 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
66 GUEST_DONE();
67 }
68
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 vm_vaddr_t vmx_pages_gva = 0;
72 struct vmx_pages *vmx;
73 unsigned long *bmap;
74 uint64_t *host_test_mem;
75
76 struct kvm_vm *vm;
77 struct kvm_run *run;
78 struct ucall uc;
79 bool done = false;
80
81 nested_vmx_check_supported();
82
83 /* Create VM */
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);
89
90 /* Add an extra memory slot for testing dirty logging */
91 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
92 GUEST_TEST_MEM,
93 TEST_MEM_SLOT_INDEX,
94 TEST_MEM_SIZE,
95 KVM_MEM_LOG_DIRTY_PAGES);
96
97 /*
98 * Add an identity map for GVA range [0xc0000000, 0xc0002000). This
99 * affects both L1 and L2. However...
100 */
101 virt_map(vm, GUEST_TEST_MEM, GUEST_TEST_MEM,
102 TEST_MEM_SIZE * 4096, 0);
103
104 /*
105 * ... pages in the L2 GPA range [0xc0001000, 0xc0003000) will map to
106 * 0xc0000000.
107 *
108 * Note that prepare_eptp should be called only L1's GPA map is done,
109 * meaning after the last call to virt_map.
110 */
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);
115
116 bmap = bitmap_alloc(TEST_MEM_SIZE);
117 host_test_mem = addr_gpa2hva(vm, GUEST_TEST_MEM);
118
119 while (!done) {
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",
124 run->exit_reason,
125 exit_reason_str(run->exit_reason));
126
127 switch (get_ucall(vm, VCPU_ID, &uc)) {
128 case UCALL_ABORT:
129 TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
130 __FILE__, uc.args[1]);
131 /* NOT REACHED */
132 case UCALL_SYNC:
133 /*
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.
136 */
137 kvm_vm_get_dirty_log(vm, TEST_MEM_SLOT_INDEX, bmap);
138 if (uc.args[1]) {
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");
141 } else {
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");
144 }
145
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");
150 break;
151 case UCALL_DONE:
152 done = true;
153 break;
154 default:
155 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
156 }
157 }
158 }
159