Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 May 2018 11:53:50 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r333534 - head/tools/test/vm86
Message-ID:  <201805121153.w4CBro4x019346@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <kib@FreeBSD.org>
+ * 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 <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+/* $Id: vm86_test.c,v 1.10 2018/05/12 11:35:58 kostik Exp kostik $ */
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/ucontext.h>
+
+#include <machine/cpufunc.h>
+#include <machine/psl.h>
+#include <machine/sysarch.h>
+#include <machine/vm86.h>
+
+#include <err.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 <kib@FreeBSD.org>
+ * 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:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201805121153.w4CBro4x019346>