2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 The FreeBSD Foundation
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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 $ */
36 #include <sys/param.h>
38 #include <sys/ucontext.h>
40 #include <machine/cpufunc.h>
41 #include <machine/psl.h>
42 #include <machine/sysarch.h>
43 #include <machine/vm86.h>
54 sig_handler(int signo, siginfo_t *si __unused, void *ucp)
60 mc = &uc->uc_mcontext;
63 * Reload pointer to the TLS base, so that malloc inside
68 printf("sig %d %%eax %#x %%ecx %#x %%eip %#x\n", signo,
69 mc->mc_eax, mc->mc_ecx, mc->mc_eip);
73 extern char vm86_code_start[], vm86_code_end[];
80 struct vm86_init_args va;
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");
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");
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)
109 memcpy(vm86_code, vm86_code_start, vm86_code_end - vm86_code_start);
111 memset(&va, 0, sizeof(va));
112 if (i386_vm86(VM86_INIT, &va) == -1)
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;