xref: /linux/tools/testing/selftests/kvm/x86/sync_regs_test.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Test for x86 KVM_CAP_SYNC_REGS
4  *
5  * Copyright (C) 2018, Google LLC.
6  *
7  * Verifies expected behavior of x86 KVM_CAP_SYNC_REGS functionality,
8  * including requesting an invalid register set, updates to/from values
9  * in kvm_run.s.regs when kvm_valid_regs and kvm_dirty_regs are toggled.
10  */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/ioctl.h>
16 #include <pthread.h>
17 
18 #include "kvm_test_harness.h"
19 #include "test_util.h"
20 #include "kvm_util.h"
21 #include "processor.h"
22 
23 #define UCALL_PIO_PORT ((u16)0x1000)
24 
25 struct ucall uc_none = {
26 	.cmd = UCALL_NONE,
27 };
28 
29 /*
30  * ucall is embedded here to protect against compiler reshuffling registers
31  * before calling a function. In this test we only need to get KVM_EXIT_IO
32  * vmexit and preserve RBX, no additional information is needed.
33  */
34 void guest_code(void)
35 {
36 	asm volatile("1: in %[port], %%al\n"
37 		     "add $0x1, %%rbx\n"
38 		     "jmp 1b"
39 		     : : [port] "d" (UCALL_PIO_PORT), "D" (&uc_none)
40 		     : "rax", "rbx");
41 }
42 
43 KVM_ONE_VCPU_TEST_SUITE(sync_regs_test);
44 
45 static void compare_regs(struct kvm_regs *left, struct kvm_regs *right)
46 {
47 #define REG_COMPARE(reg) \
48 	TEST_ASSERT(left->reg == right->reg, \
49 		    "Register " #reg \
50 		    " values did not match: 0x%llx, 0x%llx", \
51 		    left->reg, right->reg)
52 	REG_COMPARE(rax);
53 	REG_COMPARE(rbx);
54 	REG_COMPARE(rcx);
55 	REG_COMPARE(rdx);
56 	REG_COMPARE(rsi);
57 	REG_COMPARE(rdi);
58 	REG_COMPARE(rsp);
59 	REG_COMPARE(rbp);
60 	REG_COMPARE(r8);
61 	REG_COMPARE(r9);
62 	REG_COMPARE(r10);
63 	REG_COMPARE(r11);
64 	REG_COMPARE(r12);
65 	REG_COMPARE(r13);
66 	REG_COMPARE(r14);
67 	REG_COMPARE(r15);
68 	REG_COMPARE(rip);
69 	REG_COMPARE(rflags);
70 #undef REG_COMPARE
71 }
72 
73 static void compare_sregs(struct kvm_sregs *left, struct kvm_sregs *right)
74 {
75 }
76 
77 static void compare_vcpu_events(struct kvm_vcpu_events *left,
78 				struct kvm_vcpu_events *right)
79 {
80 }
81 
82 #define TEST_SYNC_FIELDS   (KVM_SYNC_X86_REGS|KVM_SYNC_X86_SREGS|KVM_SYNC_X86_EVENTS)
83 #define INVALID_SYNC_FIELD 0x80000000
84 
85 /*
86  * Set an exception as pending *and* injected while KVM is processing events.
87  * KVM is supposed to ignore/drop pending exceptions if userspace is also
88  * requesting that an exception be injected.
89  */
90 static void *race_events_inj_pen(void *arg)
91 {
92 	struct kvm_run *run = (struct kvm_run *)arg;
93 	struct kvm_vcpu_events *events = &run->s.regs.events;
94 
95 	WRITE_ONCE(events->exception.nr, UD_VECTOR);
96 
97 	for (;;) {
98 		WRITE_ONCE(run->kvm_dirty_regs, KVM_SYNC_X86_EVENTS);
99 		WRITE_ONCE(events->flags, 0);
100 		WRITE_ONCE(events->exception.injected, 1);
101 		WRITE_ONCE(events->exception.pending, 1);
102 
103 		pthread_testcancel();
104 	}
105 
106 	return NULL;
107 }
108 
109 /*
110  * Set an invalid exception vector while KVM is processing events.  KVM is
111  * supposed to reject any vector >= 32, as well as NMIs (vector 2).
112  */
113 static void *race_events_exc(void *arg)
114 {
115 	struct kvm_run *run = (struct kvm_run *)arg;
116 	struct kvm_vcpu_events *events = &run->s.regs.events;
117 
118 	for (;;) {
119 		WRITE_ONCE(run->kvm_dirty_regs, KVM_SYNC_X86_EVENTS);
120 		WRITE_ONCE(events->flags, 0);
121 		WRITE_ONCE(events->exception.nr, UD_VECTOR);
122 		WRITE_ONCE(events->exception.pending, 1);
123 		WRITE_ONCE(events->exception.nr, 255);
124 
125 		pthread_testcancel();
126 	}
127 
128 	return NULL;
129 }
130 
131 /*
132  * Toggle CR4.PAE while KVM is processing SREGS, EFER.LME=1 with CR4.PAE=0 is
133  * illegal, and KVM's MMU heavily relies on vCPU state being valid.
134  */
135 static noinline void *race_sregs_cr4(void *arg)
136 {
137 	struct kvm_run *run = (struct kvm_run *)arg;
138 	__u64 *cr4 = &run->s.regs.sregs.cr4;
139 	__u64 pae_enabled = *cr4;
140 	__u64 pae_disabled = *cr4 & ~X86_CR4_PAE;
141 
142 	for (;;) {
143 		WRITE_ONCE(run->kvm_dirty_regs, KVM_SYNC_X86_SREGS);
144 		WRITE_ONCE(*cr4, pae_enabled);
145 		asm volatile(".rept 512\n\t"
146 			     "nop\n\t"
147 			     ".endr");
148 		WRITE_ONCE(*cr4, pae_disabled);
149 
150 		pthread_testcancel();
151 	}
152 
153 	return NULL;
154 }
155 
156 static void race_sync_regs(struct kvm_vcpu *vcpu, void *racer)
157 {
158 	const time_t TIMEOUT = 2; /* seconds, roughly */
159 	struct kvm_x86_state *state;
160 	struct kvm_translation tr;
161 	struct kvm_run *run;
162 	pthread_t thread;
163 	time_t t;
164 
165 	run = vcpu->run;
166 
167 	run->kvm_valid_regs = KVM_SYNC_X86_SREGS;
168 	vcpu_run(vcpu);
169 	run->kvm_valid_regs = 0;
170 
171 	/* Save state *before* spawning the thread that mucks with vCPU state. */
172 	state = vcpu_save_state(vcpu);
173 
174 	/*
175 	 * Selftests run 64-bit guests by default, both EFER.LME and CR4.PAE
176 	 * should already be set in guest state.
177 	 */
178 	TEST_ASSERT((run->s.regs.sregs.cr4 & X86_CR4_PAE) &&
179 		    (run->s.regs.sregs.efer & EFER_LME),
180 		    "vCPU should be in long mode, CR4.PAE=%d, EFER.LME=%d",
181 		    !!(run->s.regs.sregs.cr4 & X86_CR4_PAE),
182 		    !!(run->s.regs.sregs.efer & EFER_LME));
183 
184 	kvm_pthread_create(&thread, NULL, racer, (void *)run);
185 
186 	for (t = time(NULL) + TIMEOUT; time(NULL) < t;) {
187 		/*
188 		 * Reload known good state if the vCPU triple faults, e.g. due
189 		 * to the unhandled #GPs being injected.  VMX preserves state
190 		 * on shutdown, but SVM synthesizes an INIT as the VMCB state
191 		 * is architecturally undefined on triple fault.
192 		 */
193 		if (!__vcpu_run(vcpu) && run->exit_reason == KVM_EXIT_SHUTDOWN)
194 			vcpu_load_state(vcpu, state);
195 
196 		if (racer == race_sregs_cr4) {
197 			tr = (struct kvm_translation) { .linear_address = 0 };
198 			__vcpu_ioctl(vcpu, KVM_TRANSLATE, &tr);
199 		}
200 	}
201 
202 	kvm_pthread_cancel_join(thread);
203 
204 	kvm_x86_state_cleanup(state);
205 }
206 
207 KVM_ONE_VCPU_TEST(sync_regs_test, read_invalid, guest_code)
208 {
209 	struct kvm_run *run = vcpu->run;
210 	int rv;
211 
212 	/* Request reading invalid register set from VCPU. */
213 	run->kvm_valid_regs = INVALID_SYNC_FIELD;
214 	rv = _vcpu_run(vcpu);
215 	TEST_ASSERT(rv < 0 && errno == EINVAL,
216 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d",
217 		    rv);
218 	run->kvm_valid_regs = 0;
219 
220 	run->kvm_valid_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
221 	rv = _vcpu_run(vcpu);
222 	TEST_ASSERT(rv < 0 && errno == EINVAL,
223 		    "Invalid kvm_valid_regs did not cause expected KVM_RUN error: %d",
224 		    rv);
225 	run->kvm_valid_regs = 0;
226 }
227 
228 KVM_ONE_VCPU_TEST(sync_regs_test, set_invalid, guest_code)
229 {
230 	struct kvm_run *run = vcpu->run;
231 	int rv;
232 
233 	/* Request setting invalid register set into VCPU. */
234 	run->kvm_dirty_regs = INVALID_SYNC_FIELD;
235 	rv = _vcpu_run(vcpu);
236 	TEST_ASSERT(rv < 0 && errno == EINVAL,
237 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d",
238 		    rv);
239 	run->kvm_dirty_regs = 0;
240 
241 	run->kvm_dirty_regs = INVALID_SYNC_FIELD | TEST_SYNC_FIELDS;
242 	rv = _vcpu_run(vcpu);
243 	TEST_ASSERT(rv < 0 && errno == EINVAL,
244 		    "Invalid kvm_dirty_regs did not cause expected KVM_RUN error: %d",
245 		    rv);
246 	run->kvm_dirty_regs = 0;
247 }
248 
249 KVM_ONE_VCPU_TEST(sync_regs_test, req_and_verify_all_valid, guest_code)
250 {
251 	struct kvm_run *run = vcpu->run;
252 	struct kvm_vcpu_events events;
253 	struct kvm_sregs sregs;
254 	struct kvm_regs regs;
255 
256 	/* Request and verify all valid register sets. */
257 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
258 	vcpu_run(vcpu);
259 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
260 
261 	vcpu_regs_get(vcpu, &regs);
262 	compare_regs(&regs, &run->s.regs.regs);
263 
264 	vcpu_sregs_get(vcpu, &sregs);
265 	compare_sregs(&sregs, &run->s.regs.sregs);
266 
267 	vcpu_events_get(vcpu, &events);
268 	compare_vcpu_events(&events, &run->s.regs.events);
269 }
270 
271 KVM_ONE_VCPU_TEST(sync_regs_test, set_and_verify_various, guest_code)
272 {
273 	struct kvm_run *run = vcpu->run;
274 	struct kvm_vcpu_events events;
275 	struct kvm_sregs sregs;
276 	struct kvm_regs regs;
277 
278 	/* Run once to get register set */
279 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
280 	vcpu_run(vcpu);
281 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
282 
283 	/* Set and verify various register values. */
284 	run->s.regs.regs.rbx = 0xBAD1DEA;
285 	run->s.regs.sregs.apic_base = 1 << 11;
286 	/* TODO run->s.regs.events.XYZ = ABC; */
287 
288 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
289 	run->kvm_dirty_regs = KVM_SYNC_X86_REGS | KVM_SYNC_X86_SREGS;
290 	vcpu_run(vcpu);
291 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
292 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBAD1DEA + 1,
293 		    "rbx sync regs value incorrect 0x%llx.",
294 		    run->s.regs.regs.rbx);
295 	TEST_ASSERT(run->s.regs.sregs.apic_base == 1 << 11,
296 		    "apic_base sync regs value incorrect 0x%llx.",
297 		    run->s.regs.sregs.apic_base);
298 
299 	vcpu_regs_get(vcpu, &regs);
300 	compare_regs(&regs, &run->s.regs.regs);
301 
302 	vcpu_sregs_get(vcpu, &sregs);
303 	compare_sregs(&sregs, &run->s.regs.sregs);
304 
305 	vcpu_events_get(vcpu, &events);
306 	compare_vcpu_events(&events, &run->s.regs.events);
307 }
308 
309 KVM_ONE_VCPU_TEST(sync_regs_test, clear_kvm_dirty_regs_bits, guest_code)
310 {
311 	struct kvm_run *run = vcpu->run;
312 
313 	/* Clear kvm_dirty_regs bits, verify new s.regs values are
314 	 * overwritten with existing guest values.
315 	 */
316 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
317 	run->kvm_dirty_regs = 0;
318 	run->s.regs.regs.rbx = 0xDEADBEEF;
319 	vcpu_run(vcpu);
320 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
321 	TEST_ASSERT(run->s.regs.regs.rbx != 0xDEADBEEF,
322 		    "rbx sync regs value incorrect 0x%llx.",
323 		    run->s.regs.regs.rbx);
324 }
325 
326 KVM_ONE_VCPU_TEST(sync_regs_test, clear_kvm_valid_and_dirty_regs, guest_code)
327 {
328 	struct kvm_run *run = vcpu->run;
329 	struct kvm_regs regs;
330 
331 	/* Run once to get register set */
332 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
333 	vcpu_run(vcpu);
334 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
335 
336 	/* Clear kvm_valid_regs bits and kvm_dirty_bits.
337 	 * Verify s.regs values are not overwritten with existing guest values
338 	 * and that guest values are not overwritten with kvm_sync_regs values.
339 	 */
340 	run->kvm_valid_regs = 0;
341 	run->kvm_dirty_regs = 0;
342 	run->s.regs.regs.rbx = 0xAAAA;
343 	vcpu_regs_get(vcpu, &regs);
344 	regs.rbx = 0xBAC0;
345 	vcpu_regs_set(vcpu, &regs);
346 	vcpu_run(vcpu);
347 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
348 	TEST_ASSERT(run->s.regs.regs.rbx == 0xAAAA,
349 		    "rbx sync regs value incorrect 0x%llx.",
350 		    run->s.regs.regs.rbx);
351 	vcpu_regs_get(vcpu, &regs);
352 	TEST_ASSERT(regs.rbx == 0xBAC0 + 1,
353 		    "rbx guest value incorrect 0x%llx.",
354 		    regs.rbx);
355 }
356 
357 KVM_ONE_VCPU_TEST(sync_regs_test, clear_kvm_valid_regs_bits, guest_code)
358 {
359 	struct kvm_run *run = vcpu->run;
360 	struct kvm_regs regs;
361 
362 	/* Run once to get register set */
363 	run->kvm_valid_regs = TEST_SYNC_FIELDS;
364 	vcpu_run(vcpu);
365 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
366 
367 	/* Clear kvm_valid_regs bits. Verify s.regs values are not overwritten
368 	 * with existing guest values but that guest values are overwritten
369 	 * with kvm_sync_regs values.
370 	 */
371 	run->kvm_valid_regs = 0;
372 	run->kvm_dirty_regs = TEST_SYNC_FIELDS;
373 	run->s.regs.regs.rbx = 0xBBBB;
374 	vcpu_run(vcpu);
375 	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
376 	TEST_ASSERT(run->s.regs.regs.rbx == 0xBBBB,
377 		    "rbx sync regs value incorrect 0x%llx.",
378 		    run->s.regs.regs.rbx);
379 	vcpu_regs_get(vcpu, &regs);
380 	TEST_ASSERT(regs.rbx == 0xBBBB + 1,
381 		    "rbx guest value incorrect 0x%llx.",
382 		    regs.rbx);
383 }
384 
385 KVM_ONE_VCPU_TEST(sync_regs_test, race_cr4, guest_code)
386 {
387 	race_sync_regs(vcpu, race_sregs_cr4);
388 }
389 
390 KVM_ONE_VCPU_TEST(sync_regs_test, race_exc, guest_code)
391 {
392 	race_sync_regs(vcpu, race_events_exc);
393 }
394 
395 KVM_ONE_VCPU_TEST(sync_regs_test, race_inj_pen, guest_code)
396 {
397 	race_sync_regs(vcpu, race_events_inj_pen);
398 }
399 
400 int main(int argc, char *argv[])
401 {
402 	int cap;
403 
404 	cap = kvm_check_cap(KVM_CAP_SYNC_REGS);
405 	TEST_REQUIRE((cap & TEST_SYNC_FIELDS) == TEST_SYNC_FIELDS);
406 	TEST_REQUIRE(!(cap & INVALID_SYNC_FIELD));
407 
408 	return test_harness_run(argc, argv);
409 }
410