Module Name:    src
Committed By:   jmcneill
Date:           Thu Jan  5 13:26:51 UTC 2012

Added Files:
        src/sys/arch/usermode/modules/syscallemu: Makefile syscallemu.c
            syscallemu.h syscallemu_i386.c

Log Message:
Add 'syscallemu' module, required by usermode kernel.

This module registers a syscall that takes a user_start and user_end address.
When a process calls SYS_syscallemu, the start and end address are recorded
and the syscall installs a filter on p->p_md.md_syscall. The filter then
uses the specified start and end addresses to determine where the syscall
should be routed (either host by calling the original md_syscall function,
or process by posting SIGILL).


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/usermode/modules/syscallemu/Makefile \
    src/sys/arch/usermode/modules/syscallemu/syscallemu.c \
    src/sys/arch/usermode/modules/syscallemu/syscallemu.h \
    src/sys/arch/usermode/modules/syscallemu/syscallemu_i386.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Added files:

Index: src/sys/arch/usermode/modules/syscallemu/Makefile
diff -u /dev/null src/sys/arch/usermode/modules/syscallemu/Makefile:1.1
--- /dev/null	Thu Jan  5 13:26:51 2012
+++ src/sys/arch/usermode/modules/syscallemu/Makefile	Thu Jan  5 13:26:51 2012
@@ -0,0 +1,16 @@
+#	$NetBSD: Makefile,v 1.1 2012/01/05 13:26:51 jmcneill Exp $
+
+NETBSDSRCDIR?=	../../../../..
+
+S?=	${NETBSDSRCDIR}/sys
+M?=	${S}/modules
+
+.include "${M}/Makefile.inc"
+.include "${M}/Makefile.assym"
+
+KMOD=	syscallemu
+
+SRCS=	syscallemu.c
+SRCS+=	syscallemu_${MACHINE_ARCH}.c
+
+.include <bsd.kmodule.mk>
Index: src/sys/arch/usermode/modules/syscallemu/syscallemu.c
diff -u /dev/null src/sys/arch/usermode/modules/syscallemu/syscallemu.c:1.1
--- /dev/null	Thu Jan  5 13:26:51 2012
+++ src/sys/arch/usermode/modules/syscallemu/syscallemu.c	Thu Jan  5 13:26:51 2012
@@ -0,0 +1,187 @@
+/* $NetBSD: syscallemu.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2012 Jared D. McNeill <[email protected]>
+ * All rights reserved.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
+__KERNEL_RCSID(0, "$NetBSD: syscallemu.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+#include <sys/module.h>
+#include <sys/atomic.h>
+#include <sys/syscallvar.h>
+
+#include "syscallemu.h"
+
+#if !defined(__HAVE_SYSCALL_INTERN)
+#error syscallemu requires __HAVE_SYSCALL_INTERN
+#endif
+
+static specificdata_key_t syscallemu_data_key;
+static unsigned int syscallemu_refcnt;
+
+static const struct syscall_package syscallemu_syscalls[] = {
+	{ SYS_syscallemu,	0, (sy_call_t *)sys_syscallemu	},
+	{ 0, 0, NULL },
+};
+
+struct syscallemu_data *
+syscallemu_getsce(struct proc *p)
+{
+	return proc_getspecific(p, syscallemu_data_key);
+}
+
+void
+syscallemu_setsce(struct proc *p, struct syscallemu_data *sce)
+{
+	proc_setspecific(p, syscallemu_data_key, sce);
+}
+
+/*
+ * specificdata destructor
+ */
+static void
+syscallemu_dtor(void *priv)
+{
+	struct syscallemu_data *sce = priv;
+
+	kmem_free(sce, sizeof(*sce));
+	atomic_dec_uint(&syscallemu_refcnt);
+}
+
+/*
+ * Allocate private storage for the syscallemu parameters and stash it
+ * in process specificdata. This can only be called once per process.
+ *
+ * Returns EINVAL if the specified start address falls after the end.
+ * Returns EACCESS if syscallemu has already been configured for this process.
+ */
+int
+sys_syscallemu(lwp_t *l, const struct sys_syscallemu_args *uap,
+    register_t *retval)
+{
+	/* {
+		syscallarg(uintptr_t) user_start;
+		syscallarg(uintptr_t) user_end;
+	} */
+	vaddr_t user_start = (vaddr_t)SCARG(uap, user_start);
+	vaddr_t user_end = (vaddr_t)SCARG(uap, user_end);
+	struct syscallemu_data *sce;
+	struct proc *p = l->l_proc;
+
+	if (syscallemu_getsce(p) != NULL)
+		return EACCES;
+	if (user_start >= user_end)
+		return EINVAL;
+
+	sce = kmem_alloc(sizeof(*sce), KM_SLEEP);
+	sce->sce_user_start = user_start;
+	sce->sce_user_end = user_end;
+	sce->sce_md_syscall = md_syscallemu(p);
+	KASSERT(sce->sce_md_syscall != NULL);
+
+	atomic_inc_uint(&syscallemu_refcnt);
+	syscallemu_setsce(p, sce);
+
+#ifdef DEBUG
+	printf("syscallemu: enabled for pid %d\n", p->p_pid);
+#endif
+
+	return 0;
+}
+
+/*
+ * Initialize the syscallemu module
+ */
+static int
+syscallemu_init(void)
+{
+	int error;
+
+	syscallemu_refcnt = 0;
+
+	/* XXX workaround for kern/45781 */
+	if (emul_netbsd.e_sysent[SYS_syscallemu].sy_call == sys_nosys) {
+		printf("syscallemu: applying workaround for kern/45781\n");
+		emul_netbsd.e_sysent[SYS_syscallemu].sy_call = sys_nomodule;
+	}
+	emul_netbsd.e_sysent[SYS_syscallemu].sy_narg =
+	    sizeof(struct sys_syscallemu_args) / sizeof(register_t);
+	emul_netbsd.e_sysent[SYS_syscallemu].sy_argsize =
+	    sizeof(struct sys_syscallemu_args);
+
+	error = proc_specific_key_create(&syscallemu_data_key, syscallemu_dtor);
+	if (error) {
+		printf("syscallemu: couldn't create proc specific key (%d)\n",
+		    error);
+		return error;
+	}
+
+	error = syscall_establish(NULL, syscallemu_syscalls);
+	if (error) {
+		printf("syscallemu: couldn't establish syscalls\n");
+		proc_specific_key_delete(syscallemu_data_key);
+		return ENXIO;
+	}
+
+	return 0;
+}
+
+/*
+ * Finalize the syscallemu module
+ */
+static int
+syscallemu_fini(void)
+{
+	if (syscallemu_refcnt > 0)
+		return EBUSY;
+
+	syscall_disestablish(NULL, syscallemu_syscalls);
+	proc_specific_key_delete(syscallemu_data_key);
+	return 0;
+}
+
+/*
+ * Module glue
+ */
+MODULE(MODULE_CLASS_MISC, syscallemu, NULL);
+
+static int
+syscallemu_modcmd(modcmd_t cmd, void *arg)
+{
+	switch (cmd) {
+	case MODULE_CMD_INIT:
+		return syscallemu_init();	
+	case MODULE_CMD_FINI:
+		return syscallemu_fini();
+	case MODULE_CMD_AUTOUNLOAD:
+		return EBUSY;
+	default:
+		return ENOTTY;
+	}
+}
Index: src/sys/arch/usermode/modules/syscallemu/syscallemu.h
diff -u /dev/null src/sys/arch/usermode/modules/syscallemu/syscallemu.h:1.1
--- /dev/null	Thu Jan  5 13:26:51 2012
+++ src/sys/arch/usermode/modules/syscallemu/syscallemu.h	Thu Jan  5 13:26:51 2012
@@ -0,0 +1,77 @@
+/* $NetBSD: syscallemu.h,v 1.1 2012/01/05 13:26:51 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2012 Jared D. McNeill <[email protected]>
+ * All rights reserved.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#ifndef _HAVE_SYSCALLEMU_H
+#define _HAVE_SYSCALLEMU_H
+
+#include <sys/cdefs.h>
+#include <sys/syscall.h>
+#include <sys/syscallargs.h>
+
+struct proc;
+
+/*
+ * Process specific syscallemu configuration
+ */
+struct syscallemu_data { 
+	vaddr_t		sce_user_start;
+	vaddr_t		sce_user_end;
+	void		*sce_md_syscall;
+};
+
+/*
+ * Get process specific syscallemu data
+ */
+struct syscallemu_data *	syscallemu_getsce(struct proc *);
+
+/*
+ * Set process specific syscallemu data
+ */
+void	syscallemu_setsce(struct proc *, struct syscallemu_data *);
+
+/*
+ * MD specific syscallemu syscall setup (in syscallemu_${MACHINE_ARCH}.c)
+ */
+void *	md_syscallemu(struct proc *);
+
+/* syscall: "syscallemu" ret: "int" args: "uintptr_t" "uintptr_t" */
+#define	SYS_syscallemu		511
+
+struct sys_syscallemu_args {
+	syscallarg(uintptr_t)	user_start;
+	syscallarg(uintptr_t)	user_end;
+};
+check_syscall_args(sys_syscallemu);
+
+int	sys_syscallemu(struct lwp *, const struct sys_syscallemu_args *,
+		       register_t *);
+
+__CTASSERT(SYS_syscallemu >= SYS_MAXSYSCALL);
+__CTASSERT(SYS_syscallemu < SYS_NSYSENT);
+
+#endif /* !_HAVE_SYSCALLEMU_H */
Index: src/sys/arch/usermode/modules/syscallemu/syscallemu_i386.c
diff -u /dev/null src/sys/arch/usermode/modules/syscallemu/syscallemu_i386.c:1.1
--- /dev/null	Thu Jan  5 13:26:51 2012
+++ src/sys/arch/usermode/modules/syscallemu/syscallemu_i386.c	Thu Jan  5 13:26:51 2012
@@ -0,0 +1,107 @@
+/* $NetBSD: syscallemu_i386.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2011 Reinoud Zandijk <[email protected]>
+ * Copyright (c) 2012 Jared D. McNeill <[email protected]>
+ * All rights reserved.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
+__KERNEL_RCSID(0, "$NetBSD: syscallemu_i386.c,v 1.1 2012/01/05 13:26:51 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/proc.h>
+#include <sys/systm.h>
+
+#include <machine/userret.h>
+
+#include "syscallemu.h"
+
+/*
+ * If syscallemu specific data is present for the process, verify that the
+ * caller is allowed to execute system calls. If not, deliver a SIGILL to
+ * the process. When syscallemu specific data is not present, simply defer
+ * to the original syscall handler.
+ */
+static void
+i386_syscall_emu(struct trapframe *frame)
+{
+	void (*md_syscall)(struct trapframe *) = NULL;
+	struct syscallemu_data *sce;
+	register_t rip_call;
+	struct proc *p;
+	ksiginfo_t ksi;
+	lwp_t *l;
+
+	l = curlwp;
+	p = l->l_proc;
+
+	rip_call = X86_TF_RIP(frame) - frame->tf_err;
+
+	/* Determine if we need to emulate the system call */
+	sce = syscallemu_getsce(p);
+	if (sce) {
+		if ((rip_call >= sce->sce_user_start &&
+		     rip_call < sce->sce_user_end) ||
+		    (rip_call + frame->tf_err >= sce->sce_user_start &&
+		     rip_call + frame->tf_err < sce->sce_user_end)) {
+			md_syscall = NULL;
+		} else {
+			md_syscall = sce->sce_md_syscall;
+		}
+	} else {
+		md_syscall = p->p_md.md_syscall;
+	}
+
+	if (md_syscall == NULL) {
+		/* If emulating, deliver SIGILL to process */
+		X86_TF_RIP(frame) = rip_call;
+		KSI_INIT_TRAP(&ksi);
+		ksi.ksi_signo = SIGILL;
+		ksi.ksi_code = ILL_ILLTRP;
+		ksi.ksi_addr = (void *)X86_TF_RIP(frame);
+		ksi.ksi_trap = 0;
+		trapsignal(l, &ksi);
+		userret(l);
+	} else {
+		/* Not emulating, so treat as a normal syscall */
+		KASSERT(md_syscall != NULL);
+		md_syscall(frame);
+	}
+}
+
+/*
+ * Set p_md.md_syscall to our syscall filter, and return a pointer to the
+ * original syscall handler.
+ */
+void *
+md_syscallemu(struct proc *p)
+{
+	void *osyscall;
+
+	osyscall = p->p_md.md_syscall;
+	p->p_md.md_syscall = i386_syscall_emu;
+
+	return osyscall;
+}

Reply via email to