xref: /freebsd/tools/test/vm86/vm86_test.c (revision 190cef3d52236565eb22e18b33e9e865ec634aa3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
34 /* $Id: vm86_test.c,v 1.10 2018/05/12 11:35:58 kostik Exp kostik $ */
35 
36 #include <sys/param.h>
37 #include <sys/mman.h>
38 #include <sys/ucontext.h>
39 
40 #include <machine/cpufunc.h>
41 #include <machine/psl.h>
42 #include <machine/sysarch.h>
43 #include <machine/vm86.h>
44 
45 #include <err.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 
51 static u_int gs;
52 
53 static void
54 sig_handler(int signo, siginfo_t *si __unused, void *ucp)
55 {
56 	ucontext_t *uc;
57 	mcontext_t *mc;
58 
59 	uc = ucp;
60 	mc = &uc->uc_mcontext;
61 
62 	/*
63 	 * Reload pointer to the TLS base, so that malloc inside
64 	 * printf() works.
65 	 */
66 	load_gs(gs);
67 
68 	printf("sig %d %%eax %#x %%ecx %#x %%eip %#x\n", signo,
69 	    mc->mc_eax, mc->mc_ecx, mc->mc_eip);
70 	exit(0);
71 }
72 
73 extern char vm86_code_start[], vm86_code_end[];
74 
75 int
76 main(void)
77 {
78 	ucontext_t uc;
79 	struct sigaction sa;
80 	struct vm86_init_args va;
81 	stack_t ssa;
82 	char *vm86_code;
83 
84 	gs = rgs();
85 
86 	memset(&ssa, 0, sizeof(ssa));
87 	ssa.ss_size = PAGE_SIZE * 128;
88 	ssa.ss_sp = mmap(NULL, ssa.ss_size, PROT_READ | PROT_WRITE |
89 	    PROT_EXEC, MAP_ANON, -1, 0);
90 	if (ssa.ss_sp == MAP_FAILED)
91 		err(1, "mmap sigstack");
92 	if (sigaltstack(&ssa, NULL) == -1)
93 		err(1, "sigaltstack");
94 
95 	memset(&sa, 0, sizeof(sa));
96 	sa.sa_sigaction = sig_handler;
97 	sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
98 	if (sigaction(SIGBUS, &sa, NULL) == -1)
99 		err(1, "sigaction SIGBUS");
100 	if (sigaction(SIGSEGV, &sa, NULL) == -1)
101 		err(1, "sigaction SIGSEGV");
102 	if (sigaction(SIGILL, &sa, NULL) == -1)
103 		err(1, "sigaction SIGILL");
104 
105 	vm86_code = mmap((void *)0x10000, PAGE_SIZE, PROT_READ | PROT_WRITE |
106 	    PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0);
107 	if (vm86_code == MAP_FAILED)
108 		err(1, "mmap");
109 	memcpy(vm86_code, vm86_code_start, vm86_code_end - vm86_code_start);
110 
111 	memset(&va, 0, sizeof(va));
112 	if (i386_vm86(VM86_INIT, &va) == -1)
113 		err(1, "VM86_INIT");
114 
115 	memset(&uc, 0, sizeof(uc));
116 	uc.uc_mcontext.mc_ecx = 0x2345;
117 	uc.uc_mcontext.mc_eflags = PSL_VM | PSL_USER;
118 	uc.uc_mcontext.mc_cs = uc.uc_mcontext.mc_ds = uc.uc_mcontext.mc_es =
119 	    uc.uc_mcontext.mc_ss = (uintptr_t)vm86_code >> 4;
120 	uc.uc_mcontext.mc_esp = 0xfffe;
121 	sigreturn(&uc);
122 }
123