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