1 /*
2  * Test for x86 KVM_CAP_SYNC_REGS
3  *
4  * Copyright (C) 2018, Google LLC.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  *
8  * Verifies expected behavior of x86 KVM_CAP_SYNC_REGS functionality,
9  * including requesting an invalid register set, updates to/from values
10  * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled.
11  */
12 
13 #define _GNU_SOURCE /* for program_invocation_short_name */
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/ioctl.h>
19 
20 #include "test_util.h"
21 #include "kvm_util.h"
22 #include "processor.h"
23 
24 #define VCPU_ID 5
25 
26 void guest_code(void)
27 {
28 	for (;;) {
29 		GUEST_SYNC(0);
30 		asm volatile ("inc %r11");
31 	}
32 }
33 
34 static void compare_regs(struct kvm_regs *left, struct kvm_regs *right)
35 {
36 #define REG_COMPARE(reg) \
37 	TEST_ASSERT(left->reg == right->reg, \
38 		    "Register " #reg \
39 		    " values did not match: 0x%llx, 0x%llx\n", \
40 		    left->reg, right->reg)
41 	REG_COMPARE(rax);
42 	REG_COMPARE(rbx);
43 	REG_COMPARE(rcx);
44 	REG_COMPARE(rdx);
45 	REG_COMPARE(rsi);
46 	REG_COMPARE(rdi);
47 	REG_COMPARE(rsp);
48 	REG_COMPARE(rbp);
49 	REG_COMPARE(r8);
50 	REG_COMPARE(r9);
51 	REG_COMPARE(r10);
52 	REG_COMPARE(r11);
53 	REG_COMPARE(r12);
54 	REG_COMPARE(r13);
55 	REG_COMPARE(r14);
56 	REG_COMPARE(r15);
57 	REG_COMPARE(rip);
58 	REG_COMPARE(rflags);
59 #undef REG_COMPARE
60 }
61 
62 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right)
63 {
64 }
65 
66 static void compare_vcpu_events(struct kvm_vcpu_events *left,
67 				struct kvm_vcpu_events *right)
68 {
69 }
70 
71 #define TEST_SYNC_FIELDS   (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
72 #define INVALID_SYNC_FIELD 0x80000000
73 
74 int main(int argc, char *argv[])
75 {
76 	struct kvm_vm *vm;
77 	struct kvm_run *run;
78 	struct kvm_regs regs;
79 	struct kvm_sregs sregs;
80 	struct kvm_vcpu_events events;
81 	int rv, cap;
82 
83 	/* Tell stdout not to buffer its content */
84 	setbuf(stdout, NULL);
85 
86 	cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
87 	if ((cap & TEST_SYNC_FIELDS) != TEST_SYNC_FIELDS) {
88 		fprintf(stderr, "KVM_CAP_SYNC_REGS not supported, skipping test\n");
89 		exit(KSFT_SKIP);
90 	}
91 	if ((cap & INVALID_SYNC_FIELD) != 0) {
92 		fprintf(stderr, "The \"invalid\" field is not invalid, skipping test\n");
93 		exit(KSFT_SKIP);
94 	}
95 
96 	/* Create VM */
97 	vm = vm_create_default(VCPU_ID, 0, guest_code);
98 
99 	run = vcpu_state(vm, VCPU_ID);
100 
101 	/* Request reading invalid register set from VCPU. */
102 	run->kvm_valid_regs = INVALID_SYNC_FIELD;
103 	rv = _vcpu_run(vm, VCPU_ID);
104 	TEST_ASSERT(rv < 0 && errno == EINVAL,
105 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
106 		    rv);
107 	vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
108 
109 	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
110 	rv = _vcpu_run(vm, VCPU_ID);
111 	TEST_ASSERT(rv < 0 && errno == EINVAL,
112 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d\n",
113 		    rv);
114 	vcpu_state(vm, VCPU_ID)->kvm_valid_regs = 0;
115 
116 	/* Request setting invalid register set into VCPU. */
117 	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
118 	rv = _vcpu_run(vm, VCPU_ID);
119 	TEST_ASSERT(rv < 0 && errno == EINVAL,
120 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
121 		    rv);
122 	vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
123 
124 	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
125 	rv = _vcpu_run(vm, VCPU_ID);
126 	TEST_ASSERT(rv < 0 && errno == EINVAL,
127 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d\n",
128 		    rv);
129 	vcpu_state(vm, VCPU_ID)->kvm_dirty_regs = 0;
130 
131 	/* Request and verify all valid register sets. */
132 	/* TODO: BUILD TIME CHECK: TEST_ASSERT(KVM_SYNC_X86_NUM_FIELDS != 3); */
133 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
134 	rv = _vcpu_run(vm, VCPU_ID);
135 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
136 		    "Unexpected exit reason: %u (%s),\n",
137 		    run->exit_reason,
138 		    exit_reason_str(run->exit_reason));
139 
140 	vcpu_regs_get(vm, VCPU_ID, &regs);
141 	compare_regs(&regs, &run->s.regs.regs);
142 
143 	vcpu_sregs_get(vm, VCPU_ID, &sregs);
144 	compare_sregs(&sregs, &run->s.regs.sregs);
145 
146 	vcpu_events_get(vm, VCPU_ID, &events);
147 	compare_vcpu_events(&events, &run->s.regs.events);
148 
149 	/* Set and verify various register values. */
150 	run->s.regs.regs.r11 = 0xBAD1DEA;
151 	run->s.regs.sregs.apic_base = 1 << 11;
152 	/* TODO run->s.regs.events.XYZ = ABC; */
153 
154 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
155 	run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
156 	rv = _vcpu_run(vm, VCPU_ID);
157 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
158 		    "Unexpected exit reason: %u (%s),\n",
159 		    run->exit_reason,
160 		    exit_reason_str(run->exit_reason));
161 	TEST_ASSERT(run->s.regs.regs.r11 == 0xBAD1DEA + 1,
162 		    "r11 sync regs value incorrect 0x%llx.",
163 		    run->s.regs.regs.r11);
164 	TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11,
165 		    "apic_base sync regs value incorrect 0x%llx.",
166 		    run->s.regs.sregs.apic_base);
167 
168 	vcpu_regs_get(vm, VCPU_ID, &regs);
169 	compare_regs(&regs, &run->s.regs.regs);
170 
171 	vcpu_sregs_get(vm, VCPU_ID, &sregs);
172 	compare_sregs(&sregs, &run->s.regs.sregs);
173 
174 	vcpu_events_get(vm, VCPU_ID, &events);
175 	compare_vcpu_events(&events, &run->s.regs.events);
176 
177 	/* Clear kvm_dirty_regs bits, verify new s.regs values are
178 	 * overwritten with existing guest values.
179 	 */
180 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
181 	run->kvm_dirty_regs = 0;
182 	run->s.regs.regs.r11 = 0xDEADBEEF;
183 	rv = _vcpu_run(vm, VCPU_ID);
184 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
185 		    "Unexpected exit reason: %u (%s),\n",
186 		    run->exit_reason,
187 		    exit_reason_str(run->exit_reason));
188 	TEST_ASSERT(run->s.regs.regs.r11 != 0xDEADBEEF,
189 		    "r11 sync regs value incorrect 0x%llx.",
190 		    run->s.regs.regs.r11);
191 
192 	/* Clear kvm_valid_regs bits and kvm_dirty_bits.
193 	 * Verify s.regs values are not overwritten with existing guest values
194 	 * and that guest values are not overwritten with kvm_sync_regs values.
195 	 */
196 	run->kvm_valid_regs = 0;
197 	run->kvm_dirty_regs = 0;
198 	run->s.regs.regs.r11 = 0xAAAA;
199 	regs.r11 = 0xBAC0;
200 	vcpu_regs_set(vm, VCPU_ID, &regs);
201 	rv = _vcpu_run(vm, VCPU_ID);
202 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
203 		    "Unexpected exit reason: %u (%s),\n",
204 		    run->exit_reason,
205 		    exit_reason_str(run->exit_reason));
206 	TEST_ASSERT(run->s.regs.regs.r11 == 0xAAAA,
207 		    "r11 sync regs value incorrect 0x%llx.",
208 		    run->s.regs.regs.r11);
209 	vcpu_regs_get(vm, VCPU_ID, &regs);
210 	TEST_ASSERT(regs.r11 == 0xBAC0 + 1,
211 		    "r11 guest value incorrect 0x%llx.",
212 		    regs.r11);
213 
214 	/* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten
215 	 * with existing guest values but that guest values are overwritten
216 	 * with kvm_sync_regs values.
217 	 */
218 	run->kvm_valid_regs = 0;
219 	run->kvm_dirty_regs = TEST_SYNC_FIELDS;
220 	run->s.regs.regs.r11 = 0xBBBB;
221 	rv = _vcpu_run(vm, VCPU_ID);
222 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
223 		    "Unexpected exit reason: %u (%s),\n",
224 		    run->exit_reason,
225 		    exit_reason_str(run->exit_reason));
226 	TEST_ASSERT(run->s.regs.regs.r11 == 0xBBBB,
227 		    "r11 sync regs value incorrect 0x%llx.",
228 		    run->s.regs.regs.r11);
229 	vcpu_regs_get(vm, VCPU_ID, &regs);
230 	TEST_ASSERT(regs.r11 == 0xBBBB + 1,
231 		    "r11 guest value incorrect 0x%llx.",
232 		    regs.r11);
233 
234 	kvm_vm_free(vm);
235 
236 	return 0;
237 }
238