From owner-svn-src-head@freebsd.org Sat May 12 11:53:51 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 343DBFD67D5; Sat, 12 May 2018 11:53:51 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id D77F16F798; Sat, 12 May 2018 11:53:50 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B9B5B1E01E; Sat, 12 May 2018 11:53:50 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w4CBroW6019348; Sat, 12 May 2018 11:53:50 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w4CBro4x019346; Sat, 12 May 2018 11:53:50 GMT (envelope-from kib@FreeBSD.org) Message-Id: <201805121153.w4CBro4x019346@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Sat, 12 May 2018 11:53:50 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r333534 - head/tools/test/vm86 X-SVN-Group: head X-SVN-Commit-Author: kib X-SVN-Commit-Paths: head/tools/test/vm86 X-SVN-Commit-Revision: 333534 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 12 May 2018 11:53:51 -0000 Author: kib Date: Sat May 12 11:53:49 2018 New Revision: 333534 URL: https://svnweb.freebsd.org/changeset/base/333534 Log: Add a test for vm86(2), simple to use and diagnose. MFC after: 1 week Sponsored by: The FreeBSD Foundation Added: head/tools/test/vm86/ head/tools/test/vm86/Makefile (contents, props changed) head/tools/test/vm86/vm86_test.c (contents, props changed) head/tools/test/vm86/vm86_test_asm.s (contents, props changed) Added: head/tools/test/vm86/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/test/vm86/Makefile Sat May 12 11:53:49 2018 (r333534) @@ -0,0 +1,7 @@ +# $FreeBSD$ + +all: vm86_test + +vm86_test: vm86_test_asm.s vm86_test.c + $(CC) -Wall -Wextra -g -m32 -O -o vm86_test vm86_test_asm.s vm86_test.c + Added: head/tools/test/vm86/vm86_test.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/test/vm86/vm86_test.c Sat May 12 11:53:49 2018 (r333534) @@ -0,0 +1,122 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2018 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); +/* $Id: vm86_test.c,v 1.10 2018/05/12 11:35:58 kostik Exp kostik $ */ + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +static u_int gs; + +static void +sig_handler(int signo, siginfo_t *si __unused, void *ucp) +{ + ucontext_t *uc; + mcontext_t *mc; + + uc = ucp; + mc = &uc->uc_mcontext; + + /* + * Reload pointer to the TLS base, so that malloc inside + * printf() works. + */ + load_gs(gs); + + printf("sig %d %%eax %#x %%ecx %#x %%eip %#x\n", signo, + mc->mc_eax, mc->mc_ecx, mc->mc_eip); + exit(0); +} + +extern char vm86_code_start[], vm86_code_end[]; + +int +main(void) +{ + ucontext_t uc; + struct sigaction sa; + struct vm86_init_args va; + stack_t ssa; + char *vm86_code; + + gs = rgs(); + + memset(&ssa, 0, sizeof(ssa)); + ssa.ss_size = PAGE_SIZE * 128; + ssa.ss_sp = mmap(NULL, ssa.ss_size, PROT_READ | PROT_WRITE | + PROT_EXEC, MAP_ANON, -1, 0); + if (ssa.ss_sp == MAP_FAILED) + err(1, "mmap sigstack"); + if (sigaltstack(&ssa, NULL) == -1) + err(1, "sigaltstack"); + + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = sig_handler; + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; + if (sigaction(SIGBUS, &sa, NULL) == -1) + err(1, "sigaction SIGBUS"); + if (sigaction(SIGSEGV, &sa, NULL) == -1) + err(1, "sigaction SIGSEGV"); + if (sigaction(SIGILL, &sa, NULL) == -1) + err(1, "sigaction SIGILL"); + + vm86_code = mmap((void *)0x10000, PAGE_SIZE, PROT_READ | PROT_WRITE | + PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0); + if (vm86_code == MAP_FAILED) + err(1, "mmap"); + memcpy(vm86_code, vm86_code_start, vm86_code_end - vm86_code_start); + + memset(&va, 0, sizeof(va)); + if (i386_vm86(VM86_INIT, &va) == -1) + err(1, "VM86_INIT"); + + memset(&uc, 0, sizeof(uc)); + uc.uc_mcontext.mc_ecx = 0x2345; + uc.uc_mcontext.mc_eflags = PSL_VM | PSL_USER; + uc.uc_mcontext.mc_cs = uc.uc_mcontext.mc_ds = uc.uc_mcontext.mc_es = + uc.uc_mcontext.mc_ss = (uintptr_t)vm86_code >> 4; + uc.uc_mcontext.mc_esp = 0xfffe; + sigreturn(&uc); +} Added: head/tools/test/vm86/vm86_test_asm.s ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/test/vm86/vm86_test_asm.s Sat May 12 11:53:49 2018 (r333534) @@ -0,0 +1,42 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2018 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Konstantin Belousov + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + * $Id: vm86_test_asm.s,v 1.2 2018/05/12 11:39:00 kostik Exp kostik $ + */ + + .text + .code16 + .globl vm86_code_start, vm86_code_end +vm86_code_start: +1: inc %ax + loop 1b + ud2 +vm86_code_end: