1 // SPDX-License-Identifier: GPL-2.0-only
2 #include "test_util.h"
3 #include "kvm_util.h"
4 #include "processor.h"
5 
6 #include <signal.h>
7 #include <string.h>
8 #include <sys/ioctl.h>
9 #include <sys/time.h>
10 
11 #include "kselftest.h"
12 
13 static void guest_ud_handler(struct ex_regs *regs)
14 {
15 	/* Loop on the ud2 until guest state is made invalid. */
16 }
17 
18 static void guest_code(void)
19 {
20 	asm volatile("ud2");
21 }
22 
23 static void __run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
24 {
25 	struct kvm_run *run = vcpu->run;
26 
27 	vcpu_run(vcpu);
28 
29 	TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR,
30 		    "Expected KVM_EXIT_INTERNAL_ERROR, got %d (%s)\n",
31 		    run->exit_reason, exit_reason_str(run->exit_reason));
32 	TEST_ASSERT(run->emulation_failure.suberror == KVM_INTERNAL_ERROR_EMULATION,
33 		    "Expected emulation failure, got %d\n",
34 		    run->emulation_failure.suberror);
35 }
36 
37 static void run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
38 {
39 	/*
40 	 * Always run twice to verify KVM handles the case where _KVM_ queues
41 	 * an exception with invalid state and then exits to userspace, i.e.
42 	 * that KVM doesn't explode if userspace ignores the initial error.
43 	 */
44 	__run_vcpu_with_invalid_state(vcpu);
45 	__run_vcpu_with_invalid_state(vcpu);
46 }
47 
48 static void set_timer(void)
49 {
50 	struct itimerval timer;
51 
52 	timer.it_value.tv_sec  = 0;
53 	timer.it_value.tv_usec = 200;
54 	timer.it_interval = timer.it_value;
55 	ASSERT_EQ(setitimer(ITIMER_REAL, &timer, NULL), 0);
56 }
57 
58 static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
59 {
60 	static struct kvm_sregs sregs;
61 
62 	if (!sregs.cr0)
63 		vcpu_sregs_get(vcpu, &sregs);
64 	sregs.tr.unusable = !!set;
65 	vcpu_sregs_set(vcpu, &sregs);
66 }
67 
68 static void set_invalid_guest_state(struct kvm_vcpu *vcpu)
69 {
70 	set_or_clear_invalid_guest_state(vcpu, true);
71 }
72 
73 static void clear_invalid_guest_state(struct kvm_vcpu *vcpu)
74 {
75 	set_or_clear_invalid_guest_state(vcpu, false);
76 }
77 
78 static struct kvm_vcpu *get_set_sigalrm_vcpu(struct kvm_vcpu *__vcpu)
79 {
80 	static struct kvm_vcpu *vcpu = NULL;
81 
82 	if (__vcpu)
83 		vcpu = __vcpu;
84 	return vcpu;
85 }
86 
87 static void sigalrm_handler(int sig)
88 {
89 	struct kvm_vcpu *vcpu = get_set_sigalrm_vcpu(NULL);
90 	struct kvm_vcpu_events events;
91 
92 	TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig);
93 
94 	vcpu_events_get(vcpu, &events);
95 
96 	/*
97 	 * If an exception is pending, attempt KVM_RUN with invalid guest,
98 	 * otherwise rearm the timer and keep doing so until the timer fires
99 	 * between KVM queueing an exception and re-entering the guest.
100 	 */
101 	if (events.exception.pending) {
102 		set_invalid_guest_state(vcpu);
103 		run_vcpu_with_invalid_state(vcpu);
104 	} else {
105 		set_timer();
106 	}
107 }
108 
109 int main(int argc, char *argv[])
110 {
111 	struct kvm_vcpu *vcpu;
112 	struct kvm_vm *vm;
113 
114 	TEST_REQUIRE(is_intel_cpu());
115 	TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
116 
117 	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
118 	get_set_sigalrm_vcpu(vcpu);
119 
120 	vm_init_descriptor_tables(vm);
121 	vcpu_init_descriptor_tables(vcpu);
122 
123 	vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
124 
125 	/*
126 	 * Stuff invalid guest state for L2 by making TR unusuable.  The next
127 	 * KVM_RUN should induce a TRIPLE_FAULT in L2 as KVM doesn't support
128 	 * emulating invalid guest state for L2.
129 	 */
130 	set_invalid_guest_state(vcpu);
131 	run_vcpu_with_invalid_state(vcpu);
132 
133 	/*
134 	 * Verify KVM also handles the case where userspace gains control while
135 	 * an exception is pending and stuffs invalid state.  Run with valid
136 	 * guest state and a timer firing every 200us, and attempt to enter the
137 	 * guest with invalid state when the handler interrupts KVM with an
138 	 * exception pending.
139 	 */
140 	clear_invalid_guest_state(vcpu);
141 	TEST_ASSERT(signal(SIGALRM, sigalrm_handler) != SIG_ERR,
142 		    "Failed to register SIGALRM handler, errno = %d (%s)",
143 		    errno, strerror(errno));
144 
145 	set_timer();
146 	run_vcpu_with_invalid_state(vcpu);
147 }
148