xref: /freebsd/tools/test/vm86/vm86_test.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 /* $Id: vm86_test.c,v 1.10 2018/05/12 11:35:58 kostik Exp kostik $ */
34 
35 #include <sys/param.h>
36 #include <sys/mman.h>
37 #include <sys/ucontext.h>
38 
39 #include <machine/cpufunc.h>
40 #include <machine/psl.h>
41 #include <machine/sysarch.h>
42 #include <machine/vm86.h>
43 
44 #include <err.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 static u_int gs;
51 
52 static void
53 sig_handler(int signo, siginfo_t *si __unused, void *ucp)
54 {
55 	ucontext_t *uc;
56 	mcontext_t *mc;
57 
58 	uc = ucp;
59 	mc = &uc->uc_mcontext;
60 
61 	/*
62 	 * Reload pointer to the TLS base, so that malloc inside
63 	 * printf() works.
64 	 */
65 	load_gs(gs);
66 
67 	printf("sig %d %%eax %#x %%ecx %#x %%eip %#x\n", signo,
68 	    mc->mc_eax, mc->mc_ecx, mc->mc_eip);
69 	exit(0);
70 }
71 
72 extern char vm86_code_start[], vm86_code_end[];
73 
74 int
75 main(void)
76 {
77 	ucontext_t uc;
78 	struct sigaction sa;
79 	struct vm86_init_args va;
80 	stack_t ssa;
81 	char *vm86_code;
82 
83 	gs = rgs();
84 
85 	memset(&ssa, 0, sizeof(ssa));
86 	ssa.ss_size = PAGE_SIZE * 128;
87 	ssa.ss_sp = mmap(NULL, ssa.ss_size, PROT_READ | PROT_WRITE |
88 	    PROT_EXEC, MAP_ANON, -1, 0);
89 	if (ssa.ss_sp == MAP_FAILED)
90 		err(1, "mmap sigstack");
91 	if (sigaltstack(&ssa, NULL) == -1)
92 		err(1, "sigaltstack");
93 
94 	memset(&sa, 0, sizeof(sa));
95 	sa.sa_sigaction = sig_handler;
96 	sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
97 	if (sigaction(SIGBUS, &sa, NULL) == -1)
98 		err(1, "sigaction SIGBUS");
99 	if (sigaction(SIGSEGV, &sa, NULL) == -1)
100 		err(1, "sigaction SIGSEGV");
101 	if (sigaction(SIGILL, &sa, NULL) == -1)
102 		err(1, "sigaction SIGILL");
103 
104 	vm86_code = mmap((void *)0x10000, PAGE_SIZE, PROT_READ | PROT_WRITE |
105 	    PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0);
106 	if (vm86_code == MAP_FAILED)
107 		err(1, "mmap");
108 	memcpy(vm86_code, vm86_code_start, vm86_code_end - vm86_code_start);
109 
110 	memset(&va, 0, sizeof(va));
111 	if (i386_vm86(VM86_INIT, &va) == -1)
112 		err(1, "VM86_INIT");
113 
114 	memset(&uc, 0, sizeof(uc));
115 	uc.uc_mcontext.mc_ecx = 0x2345;
116 	uc.uc_mcontext.mc_eflags = PSL_VM | PSL_USER;
117 	uc.uc_mcontext.mc_cs = uc.uc_mcontext.mc_ds = uc.uc_mcontext.mc_es =
118 	    uc.uc_mcontext.mc_ss = (uintptr_t)vm86_code >> 4;
119 	uc.uc_mcontext.mc_esp = 0xfffe;
120 	sigreturn(&uc);
121 }
122