/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2018 The FreeBSD Foundation4*5* This software was developed by Konstantin Belousov <[email protected]>6* under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <sys/cdefs.h>31/* $Id: vm86_test.c,v 1.10 2018/05/12 11:35:58 kostik Exp kostik $ */3233#include <sys/param.h>34#include <sys/mman.h>35#include <sys/ucontext.h>3637#include <machine/cpufunc.h>38#include <machine/psl.h>39#include <machine/sysarch.h>40#include <machine/vm86.h>4142#include <err.h>43#include <signal.h>44#include <stdio.h>45#include <stdlib.h>46#include <string.h>4748static u_int gs;4950static void51sig_handler(int signo, siginfo_t *si __unused, void *ucp)52{53ucontext_t *uc;54mcontext_t *mc;5556uc = ucp;57mc = &uc->uc_mcontext;5859/*60* Reload pointer to the TLS base, so that malloc inside61* printf() works.62*/63load_gs(gs);6465printf("sig %d %%eax %#x %%ecx %#x %%eip %#x\n", signo,66mc->mc_eax, mc->mc_ecx, mc->mc_eip);67exit(0);68}6970extern char vm86_code_start[], vm86_code_end[];7172int73main(void)74{75ucontext_t uc;76struct sigaction sa;77struct vm86_init_args va;78stack_t ssa;79char *vm86_code;8081gs = rgs();8283memset(&ssa, 0, sizeof(ssa));84ssa.ss_size = PAGE_SIZE * 128;85ssa.ss_sp = mmap(NULL, ssa.ss_size, PROT_READ | PROT_WRITE |86PROT_EXEC, MAP_ANON, -1, 0);87if (ssa.ss_sp == MAP_FAILED)88err(1, "mmap sigstack");89if (sigaltstack(&ssa, NULL) == -1)90err(1, "sigaltstack");9192memset(&sa, 0, sizeof(sa));93sa.sa_sigaction = sig_handler;94sa.sa_flags = SA_SIGINFO | SA_ONSTACK;95if (sigaction(SIGBUS, &sa, NULL) == -1)96err(1, "sigaction SIGBUS");97if (sigaction(SIGSEGV, &sa, NULL) == -1)98err(1, "sigaction SIGSEGV");99if (sigaction(SIGILL, &sa, NULL) == -1)100err(1, "sigaction SIGILL");101102vm86_code = mmap((void *)0x10000, PAGE_SIZE, PROT_READ | PROT_WRITE |103PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0);104if (vm86_code == MAP_FAILED)105err(1, "mmap");106memcpy(vm86_code, vm86_code_start, vm86_code_end - vm86_code_start);107108memset(&va, 0, sizeof(va));109if (i386_vm86(VM86_INIT, &va) == -1)110err(1, "VM86_INIT");111112memset(&uc, 0, sizeof(uc));113uc.uc_mcontext.mc_ecx = 0x2345;114uc.uc_mcontext.mc_eflags = PSL_VM | PSL_USER;115uc.uc_mcontext.mc_cs = uc.uc_mcontext.mc_ds = uc.uc_mcontext.mc_es =116uc.uc_mcontext.mc_ss = (uintptr_t)vm86_code >> 4;117uc.uc_mcontext.mc_esp = 0xfffe;118sigreturn(&uc);119}120121122