1/*
2 * entry_from_vm86.c - tests kernel entries from vm86 mode
3 * Copyright (c) 2014-2015 Andrew Lutomirski
4 *
5 * This exercises a few paths that need to special-case vm86 mode.
6 *
7 * GPL v2.
8 */
9
10#define _GNU_SOURCE
11
12#include <assert.h>
13#include <stdlib.h>
14#include <sys/syscall.h>
15#include <sys/signal.h>
16#include <sys/ucontext.h>
17#include <unistd.h>
18#include <stdio.h>
19#include <string.h>
20#include <inttypes.h>
21#include <sys/mman.h>
22#include <err.h>
23#include <stddef.h>
24#include <stdbool.h>
25#include <errno.h>
26#include <sys/vm86.h>
27
28static unsigned long load_addr = 0x10000;
29static int nerrs = 0;
30
31static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
32		       int flags)
33{
34	struct sigaction sa;
35	memset(&sa, 0, sizeof(sa));
36	sa.sa_sigaction = handler;
37	sa.sa_flags = SA_SIGINFO | flags;
38	sigemptyset(&sa.sa_mask);
39	if (sigaction(sig, &sa, 0))
40		err(1, "sigaction");
41}
42
43static void clearhandler(int sig)
44{
45	struct sigaction sa;
46	memset(&sa, 0, sizeof(sa));
47	sa.sa_handler = SIG_DFL;
48	sigemptyset(&sa.sa_mask);
49	if (sigaction(sig, &sa, 0))
50		err(1, "sigaction");
51}
52
53static sig_atomic_t got_signal;
54
55static void sighandler(int sig, siginfo_t *info, void *ctx_void)
56{
57	ucontext_t *ctx = (ucontext_t*)ctx_void;
58
59	if (ctx->uc_mcontext.gregs[REG_EFL] & X86_EFLAGS_VM ||
60	    (ctx->uc_mcontext.gregs[REG_CS] & 3) != 3) {
61		printf("[FAIL]\tSignal frame should not reflect vm86 mode\n");
62		nerrs++;
63	}
64
65	const char *signame;
66	if (sig == SIGSEGV)
67		signame = "SIGSEGV";
68	else if (sig == SIGILL)
69		signame = "SIGILL";
70	else
71		signame = "unexpected signal";
72
73	printf("[INFO]\t%s: FLAGS = 0x%lx, CS = 0x%hx\n", signame,
74	       (unsigned long)ctx->uc_mcontext.gregs[REG_EFL],
75	       (unsigned short)ctx->uc_mcontext.gregs[REG_CS]);
76
77	got_signal = 1;
78}
79
80asm (
81	".pushsection .rodata\n\t"
82	".type vmcode_bound, @object\n\t"
83	"vmcode:\n\t"
84	"vmcode_bound:\n\t"
85	".code16\n\t"
86	"bound %ax, (2048)\n\t"
87	"int3\n\t"
88	"vmcode_sysenter:\n\t"
89	"sysenter\n\t"
90	"vmcode_syscall:\n\t"
91	"syscall\n\t"
92	"vmcode_sti:\n\t"
93	"sti\n\t"
94	"vmcode_int3:\n\t"
95	"int3\n\t"
96	"vmcode_int80:\n\t"
97	"int $0x80\n\t"
98	".size vmcode, . - vmcode\n\t"
99	"end_vmcode:\n\t"
100	".code32\n\t"
101	".popsection"
102	);
103
104extern unsigned char vmcode[], end_vmcode[];
105extern unsigned char vmcode_bound[], vmcode_sysenter[], vmcode_syscall[],
106	vmcode_sti[], vmcode_int3[], vmcode_int80[];
107
108/* Returns false if the test was skipped. */
109static bool do_test(struct vm86plus_struct *v86, unsigned long eip,
110		    unsigned int rettype, unsigned int retarg,
111		    const char *text)
112{
113	long ret;
114
115	printf("[RUN]\t%s from vm86 mode\n", text);
116	v86->regs.eip = eip;
117	ret = vm86(VM86_ENTER, v86);
118
119	if (ret == -1 && (errno == ENOSYS || errno == EPERM)) {
120		printf("[SKIP]\tvm86 %s\n",
121		       errno == ENOSYS ? "not supported" : "not allowed");
122		return false;
123	}
124
125	if (VM86_TYPE(ret) == VM86_INTx) {
126		char trapname[32];
127		int trapno = VM86_ARG(ret);
128		if (trapno == 13)
129			strcpy(trapname, "GP");
130		else if (trapno == 5)
131			strcpy(trapname, "BR");
132		else if (trapno == 14)
133			strcpy(trapname, "PF");
134		else
135			sprintf(trapname, "%d", trapno);
136
137		printf("[INFO]\tExited vm86 mode due to #%s\n", trapname);
138	} else if (VM86_TYPE(ret) == VM86_UNKNOWN) {
139		printf("[INFO]\tExited vm86 mode due to unhandled GP fault\n");
140	} else if (VM86_TYPE(ret) == VM86_TRAP) {
141		printf("[INFO]\tExited vm86 mode due to a trap (arg=%ld)\n",
142		       VM86_ARG(ret));
143	} else if (VM86_TYPE(ret) == VM86_SIGNAL) {
144		printf("[INFO]\tExited vm86 mode due to a signal\n");
145	} else if (VM86_TYPE(ret) == VM86_STI) {
146		printf("[INFO]\tExited vm86 mode due to STI\n");
147	} else {
148		printf("[INFO]\tExited vm86 mode due to type %ld, arg %ld\n",
149		       VM86_TYPE(ret), VM86_ARG(ret));
150	}
151
152	if (rettype == -1 ||
153	    (VM86_TYPE(ret) == rettype && VM86_ARG(ret) == retarg)) {
154		printf("[OK]\tReturned correctly\n");
155	} else {
156		printf("[FAIL]\tIncorrect return reason\n");
157		nerrs++;
158	}
159
160	return true;
161}
162
163int main(void)
164{
165	struct vm86plus_struct v86;
166	unsigned char *addr = mmap((void *)load_addr, 4096,
167				   PROT_READ | PROT_WRITE | PROT_EXEC,
168				   MAP_ANONYMOUS | MAP_PRIVATE, -1,0);
169	if (addr != (unsigned char *)load_addr)
170		err(1, "mmap");
171
172	memcpy(addr, vmcode, end_vmcode - vmcode);
173	addr[2048] = 2;
174	addr[2050] = 3;
175
176	memset(&v86, 0, sizeof(v86));
177
178	v86.regs.cs = load_addr / 16;
179	v86.regs.ss = load_addr / 16;
180	v86.regs.ds = load_addr / 16;
181	v86.regs.es = load_addr / 16;
182
183	assert((v86.regs.cs & 3) == 0);	/* Looks like RPL = 0 */
184
185	/* #BR -- should deliver SIG??? */
186	do_test(&v86, vmcode_bound - vmcode, VM86_INTx, 5, "#BR");
187
188	/*
189	 * SYSENTER -- should cause #GP or #UD depending on CPU.
190	 * Expected return type -1 means that we shouldn't validate
191	 * the vm86 return value.  This will avoid problems on non-SEP
192	 * CPUs.
193	 */
194	sethandler(SIGILL, sighandler, 0);
195	do_test(&v86, vmcode_sysenter - vmcode, -1, 0, "SYSENTER");
196	clearhandler(SIGILL);
197
198	/*
199	 * SYSCALL would be a disaster in VM86 mode.  Fortunately,
200	 * there is no kernel that both enables SYSCALL and sets
201	 * EFER.SCE, so it's #UD on all systems.  But vm86 is
202	 * buggy (or has a "feature"), so the SIGILL will actually
203	 * be delivered.
204	 */
205	sethandler(SIGILL, sighandler, 0);
206	do_test(&v86, vmcode_syscall - vmcode, VM86_SIGNAL, 0, "SYSCALL");
207	clearhandler(SIGILL);
208
209	/* STI with VIP set */
210	v86.regs.eflags |= X86_EFLAGS_VIP;
211	v86.regs.eflags &= ~X86_EFLAGS_IF;
212	do_test(&v86, vmcode_sti - vmcode, VM86_STI, 0, "STI with VIP set");
213
214	/* INT3 -- should cause #BP */
215	do_test(&v86, vmcode_int3 - vmcode, VM86_TRAP, 3, "INT3");
216
217	/* INT80 -- should exit with "INTx 0x80" */
218	v86.regs.eax = (unsigned int)-1;
219	do_test(&v86, vmcode_int80 - vmcode, VM86_INTx, 0x80, "int80");
220
221	/* Execute a null pointer */
222	v86.regs.cs = 0;
223	v86.regs.ss = 0;
224	sethandler(SIGSEGV, sighandler, 0);
225	got_signal = 0;
226	if (do_test(&v86, 0, VM86_SIGNAL, 0, "Execute null pointer") &&
227	    !got_signal) {
228		printf("[FAIL]\tDid not receive SIGSEGV\n");
229		nerrs++;
230	}
231	clearhandler(SIGSEGV);
232
233	/* Make sure nothing explodes if we fork. */
234	if (fork() > 0)
235		return 0;
236
237	return (nerrs == 0 ? 0 : 1);
238}
239