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