1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020, Google LLC.
4  *
5  * Tests for exiting into userspace on registered MSRs
6  */
7 
8 #define _GNU_SOURCE /* for program_invocation_short_name */
9 #include <sys/ioctl.h>
10 
11 #include "test_util.h"
12 #include "kvm_util.h"
13 #include "vmx.h"
14 
15 /* Forced emulation prefix, used to invoke the emulator unconditionally. */
16 #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
17 #define KVM_FEP_LENGTH 5
18 static int fep_available = 1;
19 
20 #define MSR_NON_EXISTENT 0x474f4f00
21 
22 static u64 deny_bits = 0;
23 struct kvm_msr_filter filter_allow = {
24 	.flags = KVM_MSR_FILTER_DEFAULT_ALLOW,
25 	.ranges = {
26 		{
27 			.flags = KVM_MSR_FILTER_READ |
28 				 KVM_MSR_FILTER_WRITE,
29 			.nmsrs = 1,
30 			/* Test an MSR the kernel knows about. */
31 			.base = MSR_IA32_XSS,
32 			.bitmap = (uint8_t*)&deny_bits,
33 		}, {
34 			.flags = KVM_MSR_FILTER_READ |
35 				 KVM_MSR_FILTER_WRITE,
36 			.nmsrs = 1,
37 			/* Test an MSR the kernel doesn't know about. */
38 			.base = MSR_IA32_FLUSH_CMD,
39 			.bitmap = (uint8_t*)&deny_bits,
40 		}, {
41 			.flags = KVM_MSR_FILTER_READ |
42 				 KVM_MSR_FILTER_WRITE,
43 			.nmsrs = 1,
44 			/* Test a fabricated MSR that no one knows about. */
45 			.base = MSR_NON_EXISTENT,
46 			.bitmap = (uint8_t*)&deny_bits,
47 		},
48 	},
49 };
50 
51 struct kvm_msr_filter filter_fs = {
52 	.flags = KVM_MSR_FILTER_DEFAULT_ALLOW,
53 	.ranges = {
54 		{
55 			.flags = KVM_MSR_FILTER_READ,
56 			.nmsrs = 1,
57 			.base = MSR_FS_BASE,
58 			.bitmap = (uint8_t*)&deny_bits,
59 		},
60 	},
61 };
62 
63 struct kvm_msr_filter filter_gs = {
64 	.flags = KVM_MSR_FILTER_DEFAULT_ALLOW,
65 	.ranges = {
66 		{
67 			.flags = KVM_MSR_FILTER_READ,
68 			.nmsrs = 1,
69 			.base = MSR_GS_BASE,
70 			.bitmap = (uint8_t*)&deny_bits,
71 		},
72 	},
73 };
74 
75 static uint64_t msr_non_existent_data;
76 static int guest_exception_count;
77 static u32 msr_reads, msr_writes;
78 
79 static u8 bitmap_00000000[KVM_MSR_FILTER_MAX_BITMAP_SIZE];
80 static u8 bitmap_00000000_write[KVM_MSR_FILTER_MAX_BITMAP_SIZE];
81 static u8 bitmap_40000000[KVM_MSR_FILTER_MAX_BITMAP_SIZE];
82 static u8 bitmap_c0000000[KVM_MSR_FILTER_MAX_BITMAP_SIZE];
83 static u8 bitmap_c0000000_read[KVM_MSR_FILTER_MAX_BITMAP_SIZE];
84 static u8 bitmap_deadbeef[1] = { 0x1 };
85 
86 static void deny_msr(uint8_t *bitmap, u32 msr)
87 {
88 	u32 idx = msr & (KVM_MSR_FILTER_MAX_BITMAP_SIZE - 1);
89 
90 	bitmap[idx / 8] &= ~(1 << (idx % 8));
91 }
92 
93 static void prepare_bitmaps(void)
94 {
95 	memset(bitmap_00000000, 0xff, sizeof(bitmap_00000000));
96 	memset(bitmap_00000000_write, 0xff, sizeof(bitmap_00000000_write));
97 	memset(bitmap_40000000, 0xff, sizeof(bitmap_40000000));
98 	memset(bitmap_c0000000, 0xff, sizeof(bitmap_c0000000));
99 	memset(bitmap_c0000000_read, 0xff, sizeof(bitmap_c0000000_read));
100 
101 	deny_msr(bitmap_00000000_write, MSR_IA32_POWER_CTL);
102 	deny_msr(bitmap_c0000000_read, MSR_SYSCALL_MASK);
103 	deny_msr(bitmap_c0000000_read, MSR_GS_BASE);
104 }
105 
106 struct kvm_msr_filter filter_deny = {
107 	.flags = KVM_MSR_FILTER_DEFAULT_DENY,
108 	.ranges = {
109 		{
110 			.flags = KVM_MSR_FILTER_READ,
111 			.base = 0x00000000,
112 			.nmsrs = KVM_MSR_FILTER_MAX_BITMAP_SIZE * BITS_PER_BYTE,
113 			.bitmap = bitmap_00000000,
114 		}, {
115 			.flags = KVM_MSR_FILTER_WRITE,
116 			.base = 0x00000000,
117 			.nmsrs = KVM_MSR_FILTER_MAX_BITMAP_SIZE * BITS_PER_BYTE,
118 			.bitmap = bitmap_00000000_write,
119 		}, {
120 			.flags = KVM_MSR_FILTER_READ | KVM_MSR_FILTER_WRITE,
121 			.base = 0x40000000,
122 			.nmsrs = KVM_MSR_FILTER_MAX_BITMAP_SIZE * BITS_PER_BYTE,
123 			.bitmap = bitmap_40000000,
124 		}, {
125 			.flags = KVM_MSR_FILTER_READ,
126 			.base = 0xc0000000,
127 			.nmsrs = KVM_MSR_FILTER_MAX_BITMAP_SIZE * BITS_PER_BYTE,
128 			.bitmap = bitmap_c0000000_read,
129 		}, {
130 			.flags = KVM_MSR_FILTER_WRITE,
131 			.base = 0xc0000000,
132 			.nmsrs = KVM_MSR_FILTER_MAX_BITMAP_SIZE * BITS_PER_BYTE,
133 			.bitmap = bitmap_c0000000,
134 		}, {
135 			.flags = KVM_MSR_FILTER_WRITE | KVM_MSR_FILTER_READ,
136 			.base = 0xdeadbeef,
137 			.nmsrs = 1,
138 			.bitmap = bitmap_deadbeef,
139 		},
140 	},
141 };
142 
143 struct kvm_msr_filter no_filter_deny = {
144 	.flags = KVM_MSR_FILTER_DEFAULT_ALLOW,
145 };
146 
147 /*
148  * Note: Force test_rdmsr() to not be inlined to prevent the labels,
149  * rdmsr_start and rdmsr_end, from being defined multiple times.
150  */
151 static noinline uint64_t test_rdmsr(uint32_t msr)
152 {
153 	uint32_t a, d;
154 
155 	guest_exception_count = 0;
156 
157 	__asm__ __volatile__("rdmsr_start: rdmsr; rdmsr_end:" :
158 			"=a"(a), "=d"(d) : "c"(msr) : "memory");
159 
160 	return a | ((uint64_t) d << 32);
161 }
162 
163 /*
164  * Note: Force test_wrmsr() to not be inlined to prevent the labels,
165  * wrmsr_start and wrmsr_end, from being defined multiple times.
166  */
167 static noinline void test_wrmsr(uint32_t msr, uint64_t value)
168 {
169 	uint32_t a = value;
170 	uint32_t d = value >> 32;
171 
172 	guest_exception_count = 0;
173 
174 	__asm__ __volatile__("wrmsr_start: wrmsr; wrmsr_end:" ::
175 			"a"(a), "d"(d), "c"(msr) : "memory");
176 }
177 
178 extern char rdmsr_start, rdmsr_end;
179 extern char wrmsr_start, wrmsr_end;
180 
181 /*
182  * Note: Force test_em_rdmsr() to not be inlined to prevent the labels,
183  * rdmsr_start and rdmsr_end, from being defined multiple times.
184  */
185 static noinline uint64_t test_em_rdmsr(uint32_t msr)
186 {
187 	uint32_t a, d;
188 
189 	guest_exception_count = 0;
190 
191 	__asm__ __volatile__(KVM_FEP "em_rdmsr_start: rdmsr; em_rdmsr_end:" :
192 			"=a"(a), "=d"(d) : "c"(msr) : "memory");
193 
194 	return a | ((uint64_t) d << 32);
195 }
196 
197 /*
198  * Note: Force test_em_wrmsr() to not be inlined to prevent the labels,
199  * wrmsr_start and wrmsr_end, from being defined multiple times.
200  */
201 static noinline void test_em_wrmsr(uint32_t msr, uint64_t value)
202 {
203 	uint32_t a = value;
204 	uint32_t d = value >> 32;
205 
206 	guest_exception_count = 0;
207 
208 	__asm__ __volatile__(KVM_FEP "em_wrmsr_start: wrmsr; em_wrmsr_end:" ::
209 			"a"(a), "d"(d), "c"(msr) : "memory");
210 }
211 
212 extern char em_rdmsr_start, em_rdmsr_end;
213 extern char em_wrmsr_start, em_wrmsr_end;
214 
215 static void guest_code_filter_allow(void)
216 {
217 	uint64_t data;
218 
219 	/*
220 	 * Test userspace intercepting rdmsr / wrmsr for MSR_IA32_XSS.
221 	 *
222 	 * A GP is thrown if anything other than 0 is written to
223 	 * MSR_IA32_XSS.
224 	 */
225 	data = test_rdmsr(MSR_IA32_XSS);
226 	GUEST_ASSERT(data == 0);
227 	GUEST_ASSERT(guest_exception_count == 0);
228 
229 	test_wrmsr(MSR_IA32_XSS, 0);
230 	GUEST_ASSERT(guest_exception_count == 0);
231 
232 	test_wrmsr(MSR_IA32_XSS, 1);
233 	GUEST_ASSERT(guest_exception_count == 1);
234 
235 	/*
236 	 * Test userspace intercepting rdmsr / wrmsr for MSR_IA32_FLUSH_CMD.
237 	 *
238 	 * A GP is thrown if MSR_IA32_FLUSH_CMD is read
239 	 * from or if a value other than 1 is written to it.
240 	 */
241 	test_rdmsr(MSR_IA32_FLUSH_CMD);
242 	GUEST_ASSERT(guest_exception_count == 1);
243 
244 	test_wrmsr(MSR_IA32_FLUSH_CMD, 0);
245 	GUEST_ASSERT(guest_exception_count == 1);
246 
247 	test_wrmsr(MSR_IA32_FLUSH_CMD, 1);
248 	GUEST_ASSERT(guest_exception_count == 0);
249 
250 	/*
251 	 * Test userspace intercepting rdmsr / wrmsr for MSR_NON_EXISTENT.
252 	 *
253 	 * Test that a fabricated MSR can pass through the kernel
254 	 * and be handled in userspace.
255 	 */
256 	test_wrmsr(MSR_NON_EXISTENT, 2);
257 	GUEST_ASSERT(guest_exception_count == 0);
258 
259 	data = test_rdmsr(MSR_NON_EXISTENT);
260 	GUEST_ASSERT(data == 2);
261 	GUEST_ASSERT(guest_exception_count == 0);
262 
263 	/*
264 	 * Test to see if the instruction emulator is available (ie: the module
265 	 * parameter 'kvm.force_emulation_prefix=1' is set).  This instruction
266 	 * will #UD if it isn't available.
267 	 */
268 	__asm__ __volatile__(KVM_FEP "nop");
269 
270 	if (fep_available) {
271 		/* Let userspace know we aren't done. */
272 		GUEST_SYNC(0);
273 
274 		/*
275 		 * Now run the same tests with the instruction emulator.
276 		 */
277 		data = test_em_rdmsr(MSR_IA32_XSS);
278 		GUEST_ASSERT(data == 0);
279 		GUEST_ASSERT(guest_exception_count == 0);
280 		test_em_wrmsr(MSR_IA32_XSS, 0);
281 		GUEST_ASSERT(guest_exception_count == 0);
282 		test_em_wrmsr(MSR_IA32_XSS, 1);
283 		GUEST_ASSERT(guest_exception_count == 1);
284 
285 		test_em_rdmsr(MSR_IA32_FLUSH_CMD);
286 		GUEST_ASSERT(guest_exception_count == 1);
287 		test_em_wrmsr(MSR_IA32_FLUSH_CMD, 0);
288 		GUEST_ASSERT(guest_exception_count == 1);
289 		test_em_wrmsr(MSR_IA32_FLUSH_CMD, 1);
290 		GUEST_ASSERT(guest_exception_count == 0);
291 
292 		test_em_wrmsr(MSR_NON_EXISTENT, 2);
293 		GUEST_ASSERT(guest_exception_count == 0);
294 		data = test_em_rdmsr(MSR_NON_EXISTENT);
295 		GUEST_ASSERT(data == 2);
296 		GUEST_ASSERT(guest_exception_count == 0);
297 	}
298 
299 	GUEST_DONE();
300 }
301 
302 static void guest_msr_calls(bool trapped)
303 {
304 	/* This goes into the in-kernel emulation */
305 	wrmsr(MSR_SYSCALL_MASK, 0);
306 
307 	if (trapped) {
308 		/* This goes into user space emulation */
309 		GUEST_ASSERT(rdmsr(MSR_SYSCALL_MASK) == MSR_SYSCALL_MASK);
310 		GUEST_ASSERT(rdmsr(MSR_GS_BASE) == MSR_GS_BASE);
311 	} else {
312 		GUEST_ASSERT(rdmsr(MSR_SYSCALL_MASK) != MSR_SYSCALL_MASK);
313 		GUEST_ASSERT(rdmsr(MSR_GS_BASE) != MSR_GS_BASE);
314 	}
315 
316 	/* If trapped == true, this goes into user space emulation */
317 	wrmsr(MSR_IA32_POWER_CTL, 0x1234);
318 
319 	/* This goes into the in-kernel emulation */
320 	rdmsr(MSR_IA32_POWER_CTL);
321 
322 	/* Invalid MSR, should always be handled by user space exit */
323 	GUEST_ASSERT(rdmsr(0xdeadbeef) == 0xdeadbeef);
324 	wrmsr(0xdeadbeef, 0x1234);
325 }
326 
327 static void guest_code_filter_deny(void)
328 {
329 	guest_msr_calls(true);
330 
331 	/*
332 	 * Disable msr filtering, so that the kernel
333 	 * handles everything in the next round
334 	 */
335 	GUEST_SYNC(0);
336 
337 	guest_msr_calls(false);
338 
339 	GUEST_DONE();
340 }
341 
342 static void guest_code_permission_bitmap(void)
343 {
344 	uint64_t data;
345 
346 	data = test_rdmsr(MSR_FS_BASE);
347 	GUEST_ASSERT(data == MSR_FS_BASE);
348 	data = test_rdmsr(MSR_GS_BASE);
349 	GUEST_ASSERT(data != MSR_GS_BASE);
350 
351 	/* Let userspace know to switch the filter */
352 	GUEST_SYNC(0);
353 
354 	data = test_rdmsr(MSR_FS_BASE);
355 	GUEST_ASSERT(data != MSR_FS_BASE);
356 	data = test_rdmsr(MSR_GS_BASE);
357 	GUEST_ASSERT(data == MSR_GS_BASE);
358 
359 	GUEST_DONE();
360 }
361 
362 static void __guest_gp_handler(struct ex_regs *regs,
363 			       char *r_start, char *r_end,
364 			       char *w_start, char *w_end)
365 {
366 	if (regs->rip == (uintptr_t)r_start) {
367 		regs->rip = (uintptr_t)r_end;
368 		regs->rax = 0;
369 		regs->rdx = 0;
370 	} else if (regs->rip == (uintptr_t)w_start) {
371 		regs->rip = (uintptr_t)w_end;
372 	} else {
373 		GUEST_ASSERT(!"RIP is at an unknown location!");
374 	}
375 
376 	++guest_exception_count;
377 }
378 
379 static void guest_gp_handler(struct ex_regs *regs)
380 {
381 	__guest_gp_handler(regs, &rdmsr_start, &rdmsr_end,
382 			   &wrmsr_start, &wrmsr_end);
383 }
384 
385 static void guest_fep_gp_handler(struct ex_regs *regs)
386 {
387 	__guest_gp_handler(regs, &em_rdmsr_start, &em_rdmsr_end,
388 			   &em_wrmsr_start, &em_wrmsr_end);
389 }
390 
391 static void guest_ud_handler(struct ex_regs *regs)
392 {
393 	fep_available = 0;
394 	regs->rip += KVM_FEP_LENGTH;
395 }
396 
397 static void check_for_guest_assert(struct kvm_vcpu *vcpu)
398 {
399 	struct ucall uc;
400 
401 	if (vcpu->run->exit_reason == KVM_EXIT_IO &&
402 	    get_ucall(vcpu, &uc) == UCALL_ABORT) {
403 		REPORT_GUEST_ASSERT(uc);
404 	}
405 }
406 
407 static void process_rdmsr(struct kvm_vcpu *vcpu, uint32_t msr_index)
408 {
409 	struct kvm_run *run = vcpu->run;
410 
411 	check_for_guest_assert(vcpu);
412 
413 	TEST_ASSERT(run->exit_reason == KVM_EXIT_X86_RDMSR,
414 		    "Unexpected exit reason: %u (%s),\n",
415 		    run->exit_reason,
416 		    exit_reason_str(run->exit_reason));
417 	TEST_ASSERT(run->msr.index == msr_index,
418 			"Unexpected msr (0x%04x), expected 0x%04x",
419 			run->msr.index, msr_index);
420 
421 	switch (run->msr.index) {
422 	case MSR_IA32_XSS:
423 		run->msr.data = 0;
424 		break;
425 	case MSR_IA32_FLUSH_CMD:
426 		run->msr.error = 1;
427 		break;
428 	case MSR_NON_EXISTENT:
429 		run->msr.data = msr_non_existent_data;
430 		break;
431 	case MSR_FS_BASE:
432 		run->msr.data = MSR_FS_BASE;
433 		break;
434 	case MSR_GS_BASE:
435 		run->msr.data = MSR_GS_BASE;
436 		break;
437 	default:
438 		TEST_ASSERT(false, "Unexpected MSR: 0x%04x", run->msr.index);
439 	}
440 }
441 
442 static void process_wrmsr(struct kvm_vcpu *vcpu, uint32_t msr_index)
443 {
444 	struct kvm_run *run = vcpu->run;
445 
446 	check_for_guest_assert(vcpu);
447 
448 	TEST_ASSERT(run->exit_reason == KVM_EXIT_X86_WRMSR,
449 		    "Unexpected exit reason: %u (%s),\n",
450 		    run->exit_reason,
451 		    exit_reason_str(run->exit_reason));
452 	TEST_ASSERT(run->msr.index == msr_index,
453 			"Unexpected msr (0x%04x), expected 0x%04x",
454 			run->msr.index, msr_index);
455 
456 	switch (run->msr.index) {
457 	case MSR_IA32_XSS:
458 		if (run->msr.data != 0)
459 			run->msr.error = 1;
460 		break;
461 	case MSR_IA32_FLUSH_CMD:
462 		if (run->msr.data != 1)
463 			run->msr.error = 1;
464 		break;
465 	case MSR_NON_EXISTENT:
466 		msr_non_existent_data = run->msr.data;
467 		break;
468 	default:
469 		TEST_ASSERT(false, "Unexpected MSR: 0x%04x", run->msr.index);
470 	}
471 }
472 
473 static void process_ucall_done(struct kvm_vcpu *vcpu)
474 {
475 	struct kvm_run *run = vcpu->run;
476 	struct ucall uc;
477 
478 	check_for_guest_assert(vcpu);
479 
480 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
481 		    "Unexpected exit reason: %u (%s)",
482 		    run->exit_reason,
483 		    exit_reason_str(run->exit_reason));
484 
485 	TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_DONE,
486 		    "Unexpected ucall command: %lu, expected UCALL_DONE (%d)",
487 		    uc.cmd, UCALL_DONE);
488 }
489 
490 static uint64_t process_ucall(struct kvm_vcpu *vcpu)
491 {
492 	struct kvm_run *run = vcpu->run;
493 	struct ucall uc = {};
494 
495 	check_for_guest_assert(vcpu);
496 
497 	TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
498 		    "Unexpected exit reason: %u (%s)",
499 		    run->exit_reason,
500 		    exit_reason_str(run->exit_reason));
501 
502 	switch (get_ucall(vcpu, &uc)) {
503 	case UCALL_SYNC:
504 		break;
505 	case UCALL_ABORT:
506 		check_for_guest_assert(vcpu);
507 		break;
508 	case UCALL_DONE:
509 		process_ucall_done(vcpu);
510 		break;
511 	default:
512 		TEST_ASSERT(false, "Unexpected ucall");
513 	}
514 
515 	return uc.cmd;
516 }
517 
518 static void run_guest_then_process_rdmsr(struct kvm_vcpu *vcpu,
519 					 uint32_t msr_index)
520 {
521 	vcpu_run(vcpu);
522 	process_rdmsr(vcpu, msr_index);
523 }
524 
525 static void run_guest_then_process_wrmsr(struct kvm_vcpu *vcpu,
526 					 uint32_t msr_index)
527 {
528 	vcpu_run(vcpu);
529 	process_wrmsr(vcpu, msr_index);
530 }
531 
532 static uint64_t run_guest_then_process_ucall(struct kvm_vcpu *vcpu)
533 {
534 	vcpu_run(vcpu);
535 	return process_ucall(vcpu);
536 }
537 
538 static void run_guest_then_process_ucall_done(struct kvm_vcpu *vcpu)
539 {
540 	vcpu_run(vcpu);
541 	process_ucall_done(vcpu);
542 }
543 
544 static void test_msr_filter_allow(void)
545 {
546 	struct kvm_vcpu *vcpu;
547 	struct kvm_vm *vm;
548 	int rc;
549 
550 	vm = vm_create_with_one_vcpu(&vcpu, guest_code_filter_allow);
551 
552 	rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
553 	TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
554 	vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER);
555 
556 	rc = kvm_check_cap(KVM_CAP_X86_MSR_FILTER);
557 	TEST_ASSERT(rc, "KVM_CAP_X86_MSR_FILTER is available");
558 
559 	vm_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter_allow);
560 
561 	vm_init_descriptor_tables(vm);
562 	vcpu_init_descriptor_tables(vcpu);
563 
564 	vm_install_exception_handler(vm, GP_VECTOR, guest_gp_handler);
565 
566 	/* Process guest code userspace exits. */
567 	run_guest_then_process_rdmsr(vcpu, MSR_IA32_XSS);
568 	run_guest_then_process_wrmsr(vcpu, MSR_IA32_XSS);
569 	run_guest_then_process_wrmsr(vcpu, MSR_IA32_XSS);
570 
571 	run_guest_then_process_rdmsr(vcpu, MSR_IA32_FLUSH_CMD);
572 	run_guest_then_process_wrmsr(vcpu, MSR_IA32_FLUSH_CMD);
573 	run_guest_then_process_wrmsr(vcpu, MSR_IA32_FLUSH_CMD);
574 
575 	run_guest_then_process_wrmsr(vcpu, MSR_NON_EXISTENT);
576 	run_guest_then_process_rdmsr(vcpu, MSR_NON_EXISTENT);
577 
578 	vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
579 	vcpu_run(vcpu);
580 	vm_install_exception_handler(vm, UD_VECTOR, NULL);
581 
582 	if (process_ucall(vcpu) != UCALL_DONE) {
583 		vm_install_exception_handler(vm, GP_VECTOR, guest_fep_gp_handler);
584 
585 		/* Process emulated rdmsr and wrmsr instructions. */
586 		run_guest_then_process_rdmsr(vcpu, MSR_IA32_XSS);
587 		run_guest_then_process_wrmsr(vcpu, MSR_IA32_XSS);
588 		run_guest_then_process_wrmsr(vcpu, MSR_IA32_XSS);
589 
590 		run_guest_then_process_rdmsr(vcpu, MSR_IA32_FLUSH_CMD);
591 		run_guest_then_process_wrmsr(vcpu, MSR_IA32_FLUSH_CMD);
592 		run_guest_then_process_wrmsr(vcpu, MSR_IA32_FLUSH_CMD);
593 
594 		run_guest_then_process_wrmsr(vcpu, MSR_NON_EXISTENT);
595 		run_guest_then_process_rdmsr(vcpu, MSR_NON_EXISTENT);
596 
597 		/* Confirm the guest completed without issues. */
598 		run_guest_then_process_ucall_done(vcpu);
599 	} else {
600 		printf("To run the instruction emulated tests set the module parameter 'kvm.force_emulation_prefix=1'\n");
601 	}
602 
603 	kvm_vm_free(vm);
604 }
605 
606 static int handle_ucall(struct kvm_vcpu *vcpu)
607 {
608 	struct ucall uc;
609 
610 	switch (get_ucall(vcpu, &uc)) {
611 	case UCALL_ABORT:
612 		REPORT_GUEST_ASSERT(uc);
613 		break;
614 	case UCALL_SYNC:
615 		vm_ioctl(vcpu->vm, KVM_X86_SET_MSR_FILTER, &no_filter_deny);
616 		break;
617 	case UCALL_DONE:
618 		return 1;
619 	default:
620 		TEST_FAIL("Unknown ucall %lu", uc.cmd);
621 	}
622 
623 	return 0;
624 }
625 
626 static void handle_rdmsr(struct kvm_run *run)
627 {
628 	run->msr.data = run->msr.index;
629 	msr_reads++;
630 
631 	if (run->msr.index == MSR_SYSCALL_MASK ||
632 	    run->msr.index == MSR_GS_BASE) {
633 		TEST_ASSERT(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER,
634 			    "MSR read trap w/o access fault");
635 	}
636 
637 	if (run->msr.index == 0xdeadbeef) {
638 		TEST_ASSERT(run->msr.reason == KVM_MSR_EXIT_REASON_UNKNOWN,
639 			    "MSR deadbeef read trap w/o inval fault");
640 	}
641 }
642 
643 static void handle_wrmsr(struct kvm_run *run)
644 {
645 	/* ignore */
646 	msr_writes++;
647 
648 	if (run->msr.index == MSR_IA32_POWER_CTL) {
649 		TEST_ASSERT(run->msr.data == 0x1234,
650 			    "MSR data for MSR_IA32_POWER_CTL incorrect");
651 		TEST_ASSERT(run->msr.reason == KVM_MSR_EXIT_REASON_FILTER,
652 			    "MSR_IA32_POWER_CTL trap w/o access fault");
653 	}
654 
655 	if (run->msr.index == 0xdeadbeef) {
656 		TEST_ASSERT(run->msr.data == 0x1234,
657 			    "MSR data for deadbeef incorrect");
658 		TEST_ASSERT(run->msr.reason == KVM_MSR_EXIT_REASON_UNKNOWN,
659 			    "deadbeef trap w/o inval fault");
660 	}
661 }
662 
663 static void test_msr_filter_deny(void)
664 {
665 	struct kvm_vcpu *vcpu;
666 	struct kvm_vm *vm;
667 	struct kvm_run *run;
668 	int rc;
669 
670 	vm = vm_create_with_one_vcpu(&vcpu, guest_code_filter_deny);
671 	run = vcpu->run;
672 
673 	rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
674 	TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
675 	vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_INVAL |
676 						      KVM_MSR_EXIT_REASON_UNKNOWN |
677 						      KVM_MSR_EXIT_REASON_FILTER);
678 
679 	rc = kvm_check_cap(KVM_CAP_X86_MSR_FILTER);
680 	TEST_ASSERT(rc, "KVM_CAP_X86_MSR_FILTER is available");
681 
682 	prepare_bitmaps();
683 	vm_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter_deny);
684 
685 	while (1) {
686 		vcpu_run(vcpu);
687 
688 		switch (run->exit_reason) {
689 		case KVM_EXIT_X86_RDMSR:
690 			handle_rdmsr(run);
691 			break;
692 		case KVM_EXIT_X86_WRMSR:
693 			handle_wrmsr(run);
694 			break;
695 		case KVM_EXIT_IO:
696 			if (handle_ucall(vcpu))
697 				goto done;
698 			break;
699 		}
700 
701 	}
702 
703 done:
704 	TEST_ASSERT(msr_reads == 4, "Handled 4 rdmsr in user space");
705 	TEST_ASSERT(msr_writes == 3, "Handled 3 wrmsr in user space");
706 
707 	kvm_vm_free(vm);
708 }
709 
710 static void test_msr_permission_bitmap(void)
711 {
712 	struct kvm_vcpu *vcpu;
713 	struct kvm_vm *vm;
714 	int rc;
715 
716 	vm = vm_create_with_one_vcpu(&vcpu, guest_code_permission_bitmap);
717 
718 	rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
719 	TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
720 	vm_enable_cap(vm, KVM_CAP_X86_USER_SPACE_MSR, KVM_MSR_EXIT_REASON_FILTER);
721 
722 	rc = kvm_check_cap(KVM_CAP_X86_MSR_FILTER);
723 	TEST_ASSERT(rc, "KVM_CAP_X86_MSR_FILTER is available");
724 
725 	vm_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter_fs);
726 	run_guest_then_process_rdmsr(vcpu, MSR_FS_BASE);
727 	TEST_ASSERT(run_guest_then_process_ucall(vcpu) == UCALL_SYNC,
728 		    "Expected ucall state to be UCALL_SYNC.");
729 	vm_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter_gs);
730 	run_guest_then_process_rdmsr(vcpu, MSR_GS_BASE);
731 	run_guest_then_process_ucall_done(vcpu);
732 
733 	kvm_vm_free(vm);
734 }
735 
736 #define test_user_exit_msr_ioctl(vm, cmd, arg, flag, valid_mask)	\
737 ({									\
738 	int r = __vm_ioctl(vm, cmd, arg);				\
739 									\
740 	if (flag & valid_mask)						\
741 		TEST_ASSERT(!r, __KVM_IOCTL_ERROR(#cmd, r));		\
742 	else								\
743 		TEST_ASSERT(r == -1 && errno == EINVAL,			\
744 			    "Wanted EINVAL for %s with flag = 0x%llx, got  rc: %i errno: %i (%s)", \
745 			    #cmd, flag, r, errno,  strerror(errno));	\
746 })
747 
748 static void run_user_space_msr_flag_test(struct kvm_vm *vm)
749 {
750 	struct kvm_enable_cap cap = { .cap = KVM_CAP_X86_USER_SPACE_MSR };
751 	int nflags = sizeof(cap.args[0]) * BITS_PER_BYTE;
752 	int rc;
753 	int i;
754 
755 	rc = kvm_check_cap(KVM_CAP_X86_USER_SPACE_MSR);
756 	TEST_ASSERT(rc, "KVM_CAP_X86_USER_SPACE_MSR is available");
757 
758 	for (i = 0; i < nflags; i++) {
759 		cap.args[0] = BIT_ULL(i);
760 		test_user_exit_msr_ioctl(vm, KVM_ENABLE_CAP, &cap,
761 			   BIT_ULL(i), KVM_MSR_EXIT_REASON_VALID_MASK);
762 	}
763 }
764 
765 static void run_msr_filter_flag_test(struct kvm_vm *vm)
766 {
767 	u64 deny_bits = 0;
768 	struct kvm_msr_filter filter = {
769 		.flags = KVM_MSR_FILTER_DEFAULT_ALLOW,
770 		.ranges = {
771 			{
772 				.flags = KVM_MSR_FILTER_READ,
773 				.nmsrs = 1,
774 				.base = 0,
775 				.bitmap = (uint8_t *)&deny_bits,
776 			},
777 		},
778 	};
779 	int nflags;
780 	int rc;
781 	int i;
782 
783 	rc = kvm_check_cap(KVM_CAP_X86_MSR_FILTER);
784 	TEST_ASSERT(rc, "KVM_CAP_X86_MSR_FILTER is available");
785 
786 	nflags = sizeof(filter.flags) * BITS_PER_BYTE;
787 	for (i = 0; i < nflags; i++) {
788 		filter.flags = BIT_ULL(i);
789 		test_user_exit_msr_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter,
790 			   BIT_ULL(i), KVM_MSR_FILTER_VALID_MASK);
791 	}
792 
793 	filter.flags = KVM_MSR_FILTER_DEFAULT_ALLOW;
794 	nflags = sizeof(filter.ranges[0].flags) * BITS_PER_BYTE;
795 	for (i = 0; i < nflags; i++) {
796 		filter.ranges[0].flags = BIT_ULL(i);
797 		test_user_exit_msr_ioctl(vm, KVM_X86_SET_MSR_FILTER, &filter,
798 			   BIT_ULL(i), KVM_MSR_FILTER_RANGE_VALID_MASK);
799 	}
800 }
801 
802 /* Test that attempts to write to the unused bits in a flag fails. */
803 static void test_user_exit_msr_flags(void)
804 {
805 	struct kvm_vcpu *vcpu;
806 	struct kvm_vm *vm;
807 
808 	vm = vm_create_with_one_vcpu(&vcpu, NULL);
809 
810 	/* Test flags for KVM_CAP_X86_USER_SPACE_MSR. */
811 	run_user_space_msr_flag_test(vm);
812 
813 	/* Test flags and range flags for KVM_X86_SET_MSR_FILTER. */
814 	run_msr_filter_flag_test(vm);
815 
816 	kvm_vm_free(vm);
817 }
818 
819 int main(int argc, char *argv[])
820 {
821 	test_msr_filter_allow();
822 
823 	test_msr_filter_deny();
824 
825 	test_msr_permission_bitmap();
826 
827 	test_user_exit_msr_flags();
828 
829 	return 0;
830 }
831