1 // SPDX-License-Identifier: GPL-2.0-only
11 #include "kselftest.h"
13 static void guest_ud_handler(struct ex_regs *regs)
15 /* Loop on the ud2 until guest state is made invalid. */
18 static void guest_code(void)
23 static void __run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
25 struct kvm_run *run = vcpu->run;
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);
35 static void run_vcpu_with_invalid_state(struct kvm_vcpu *vcpu)
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.
42 __run_vcpu_with_invalid_state(vcpu);
43 __run_vcpu_with_invalid_state(vcpu);
46 static void set_timer(void)
48 struct itimerval timer;
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);
56 static void set_or_clear_invalid_guest_state(struct kvm_vcpu *vcpu, bool set)
58 static struct kvm_sregs sregs;
61 vcpu_sregs_get(vcpu, &sregs);
62 sregs.tr.unusable = !!set;
63 vcpu_sregs_set(vcpu, &sregs);
66 static void set_invalid_guest_state(struct kvm_vcpu *vcpu)
68 set_or_clear_invalid_guest_state(vcpu, true);
71 static void clear_invalid_guest_state(struct kvm_vcpu *vcpu)
73 set_or_clear_invalid_guest_state(vcpu, false);
76 static struct kvm_vcpu *get_set_sigalrm_vcpu(struct kvm_vcpu *__vcpu)
78 static struct kvm_vcpu *vcpu = NULL;
85 static void sigalrm_handler(int sig)
87 struct kvm_vcpu *vcpu = get_set_sigalrm_vcpu(NULL);
88 struct kvm_vcpu_events events;
90 TEST_ASSERT(sig == SIGALRM, "Unexpected signal = %d", sig);
92 vcpu_events_get(vcpu, &events);
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.
99 if (events.exception.pending) {
100 set_invalid_guest_state(vcpu);
101 run_vcpu_with_invalid_state(vcpu);
107 int main(int argc, char *argv[])
109 struct kvm_vcpu *vcpu;
112 TEST_REQUIRE(host_cpu_is_intel);
113 TEST_REQUIRE(!vm_is_unrestricted_guest(NULL));
115 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
116 get_set_sigalrm_vcpu(vcpu);
118 vm_init_descriptor_tables(vm);
119 vcpu_init_descriptor_tables(vcpu);
121 vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);
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.
128 set_invalid_guest_state(vcpu);
129 run_vcpu_with_invalid_state(vcpu);
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
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));
144 run_vcpu_with_invalid_state(vcpu);