Module Name:    src
Committed By:   pgoyette
Date:           Fri Sep 14 05:37:08 UTC 2018

Modified Files:
        src/sys/arch/amd64/amd64 [pgoyette-compat]: netbsd32_machdep.c
        src/sys/compat/netbsd32 [pgoyette-compat]: netbsd32_compat_16.c
            netbsd32_mod.c
        src/sys/modules/compat_netbsd32 [pgoyette-compat]: Makefile
        src/sys/modules/compat_netbsd32_13 [pgoyette-compat]: Makefile
        src/sys/modules/compat_netbsd32_16 [pgoyette-compat]: Makefile
Added Files:
        src/sys/arch/amd64/amd64 [pgoyette-compat]: netbsd32_machdep_13.c
            netbsd32_machdep_16.c

Log Message:
Work in progress - get the arch-specific netbsd32_machdep.c code to
build as a module.

XXX Doesn't work when the code is built-in to the kernel.


To generate a diff of this commit:
cvs rdiff -u -r1.115.2.1 -r1.115.2.2 \
    src/sys/arch/amd64/amd64/netbsd32_machdep.c
cvs rdiff -u -r0 -r1.1.2.1 src/sys/arch/amd64/amd64/netbsd32_machdep_13.c \
    src/sys/arch/amd64/amd64/netbsd32_machdep_16.c
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/compat/netbsd32/netbsd32_compat_16.c
cvs rdiff -u -r1.13.16.9 -r1.13.16.10 src/sys/compat/netbsd32/netbsd32_mod.c
cvs rdiff -u -r1.20.12.10 -r1.20.12.11 \
    src/sys/modules/compat_netbsd32/Makefile
cvs rdiff -u -r1.1.2.3 -r1.1.2.4 src/sys/modules/compat_netbsd32_13/Makefile
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 src/sys/modules/compat_netbsd32_16/Makefile

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

Modified files:

Index: src/sys/arch/amd64/amd64/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.115.2.1 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.115.2.2
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.115.2.1	Sat Jul 28 04:37:26 2018
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Fri Sep 14 05:37:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.115.2.1 2018/07/28 04:37:26 pgoyette Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.115.2.2 2018/09/14 05:37:08 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.115.2.1 2018/07/28 04:37:26 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.115.2.2 2018/09/14 05:37:08 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -101,7 +101,12 @@ static int x86_64_set_mtrr32(struct lwp 
 #define x86_64_set_mtrr32(x, y, z)	ENOSYS
 #endif
 
-static int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
+int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
+
+void  netbsd32_buildcontext(struct lwp *l, struct trapframe *tf, void *fp,
+    sig_t catcher, int onstack);
+
+void netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
 
 #ifdef EXEC_AOUT
 /*
@@ -160,7 +165,7 @@ netbsd32_setregs(struct lwp *l, struct e
 	tf->tf_ss = LSEL(LUDATA32_SEL, SEL_UPL);
 }
 
-static void
+void
 netbsd32_buildcontext(struct lwp *l, struct trapframe *tf, void *fp,
     sig_t catcher, int onstack)
 {
@@ -197,96 +202,7 @@ netbsd32_buildcontext(struct lwp *l, str
 	}
 }
 
-#ifdef COMPAT_16
-static void
-netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-	struct lwp *l = curlwp;
-	struct proc *p = l->l_proc;
-	struct trapframe *tf;
-	int sig = ksi->ksi_signo;
-	sig_t catcher = SIGACTION(p, sig).sa_handler;
-	struct netbsd32_sigframe_sigcontext *fp, frame;
-	int onstack, error;
-	struct sigacts *ps = p->p_sigacts;
-
-	tf = l->l_md.md_regs;
-
-	/* Do we need to jump onto the signal stack? */
-	onstack =
-	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
-	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
-
-	/* Allocate space for the signal handler context. */
-	if (onstack)
-		fp = (struct netbsd32_sigframe_sigcontext *)
-		    ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
-	else
-		fp = (struct netbsd32_sigframe_sigcontext *)tf->tf_rsp;
-	fp--;
-
-	/* Build stack frame for signal trampoline. */
-	switch (ps->sa_sigdesc[sig].sd_vers) {
-	case 0:
-		frame.sf_ra = (uint32_t)(u_long)p->p_sigctx.ps_sigcode;
-		break;
-	case 1:
-		frame.sf_ra = (uint32_t)(u_long)ps->sa_sigdesc[sig].sd_tramp;
-		break;
-	default:
-		/* Don't know what trampoline version; kill it. */
-		sigexit(l, SIGILL);
-	}
-	frame.sf_signum = sig;
-	frame.sf_code = ksi->ksi_trap;
-	frame.sf_scp = (uint32_t)(u_long)&fp->sf_sc;
-
-	frame.sf_sc.sc_ds = tf->tf_ds & 0xFFFF;
-	frame.sf_sc.sc_es = tf->tf_es & 0xFFFF;
-	frame.sf_sc.sc_fs = tf->tf_fs & 0xFFFF;
-	frame.sf_sc.sc_gs = tf->tf_gs & 0xFFFF;
-
-	frame.sf_sc.sc_eflags = tf->tf_rflags;
-	frame.sf_sc.sc_edi = tf->tf_rdi;
-	frame.sf_sc.sc_esi = tf->tf_rsi;
-	frame.sf_sc.sc_ebp = tf->tf_rbp;
-	frame.sf_sc.sc_ebx = tf->tf_rbx;
-	frame.sf_sc.sc_edx = tf->tf_rdx;
-	frame.sf_sc.sc_ecx = tf->tf_rcx;
-	frame.sf_sc.sc_eax = tf->tf_rax;
-	frame.sf_sc.sc_eip = tf->tf_rip;
-	frame.sf_sc.sc_cs = tf->tf_cs & 0xFFFF;
-	frame.sf_sc.sc_esp = tf->tf_rsp;
-	frame.sf_sc.sc_ss = tf->tf_ss & 0xFFFF;
-	frame.sf_sc.sc_trapno = tf->tf_trapno;
-	frame.sf_sc.sc_err = tf->tf_err;
-
-	/* Save signal stack. */
-	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
-
-	/* Save signal mask. */
-	frame.sf_sc.sc_mask = *mask;
-
-	sendsig_reset(l, sig);
-
-	mutex_exit(p->p_lock);
-	error = copyout(&frame, fp, sizeof(frame));
-	mutex_enter(p->p_lock);
-
-	if (error != 0) {
-		/*
-		 * Process has trashed its stack; give it an illegal
-		 * instruction to halt it in its tracks.
-		 */
-		sigexit(l, SIGILL);
-		/* NOTREACHED */
-	}
-
-	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
-}
-#endif
-
-static void
+void
 netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
 {
 	struct lwp *l = curlwp;
@@ -354,15 +270,13 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
 }
 
+void (*vec_netbsd32_sendsig)(const ksiginfo_t *, const sigset_t *);
+
 void
 netbsd32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
 {
-#ifdef COMPAT_16
-	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2)
-		netbsd32_sendsig_sigcontext(ksi, mask);
-	else
-#endif
-		netbsd32_sendsig_siginfo(ksi, mask);
+
+	(*vec_netbsd32_sendsig)(ksi, mask);
 }
 
 int
@@ -1023,7 +937,7 @@ startlwp32(void *arg)
  * These functions perform the needed checks.
  */
 
-static int
+int
 check_sigcontext32(struct lwp *l, const struct netbsd32_sigcontext *scp)
 {
 	struct pmap *pmap = l->l_proc->p_vmspace->vm_map.pmap;
@@ -1111,66 +1025,20 @@ netbsd32_vm_default_addr(struct proc *p,
 		return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, sz);
 }
 
-#ifdef COMPAT_13
+int netbsd32_amd64_init(int);
+int netbsd32_amd64_fini(int);
+
 int
-compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
+netbsd32_amd64_init(int misc)
 {
-	/* {
-		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
-	} */
-	struct proc *p = l->l_proc;
-	struct netbsd32_sigcontext13 *scp, context;
-	struct trapframe *tf;
-	sigset_t mask;
-	int error;
 
-	/*
-	 * The trampoline code hands us the context.
-	 * It is unsafe to keep track of it ourselves, in the event that a
-	 * program jumps out of a signal handler.
-	 */
-	scp = (struct netbsd32_sigcontext13 *)NETBSD32PTR64(SCARG(uap, sigcntxp));
-	if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
-		return (EFAULT);
-
-	/* Restore register context. */
-	tf = l->l_md.md_regs;
-
-	/*
-	 * Check for security violations.
-	 */
-	error = check_sigcontext32(l, (const struct netbsd32_sigcontext *)&context);
-	if (error != 0)
-		return error;
-
-	tf->tf_gs = context.sc_gs & 0xFFFF;
-	tf->tf_fs = context.sc_fs & 0xFFFF;		
-	tf->tf_es = context.sc_es & 0xFFFF;
-	tf->tf_ds = context.sc_ds & 0xFFFF;
-	tf->tf_rflags = context.sc_eflags;
-	tf->tf_rdi = context.sc_edi;
-	tf->tf_rsi = context.sc_esi;
-	tf->tf_rbp = context.sc_ebp;
-	tf->tf_rbx = context.sc_ebx;
-	tf->tf_rdx = context.sc_edx;
-	tf->tf_rcx = context.sc_ecx;
-	tf->tf_rax = context.sc_eax;
-	tf->tf_rip = context.sc_eip;
-	tf->tf_cs = context.sc_cs & 0xFFFF;
-	tf->tf_rsp = context.sc_esp;
-	tf->tf_ss = context.sc_ss & 0xFFFF;
+	vec_netbsd32_sendsig = netbsd32_sendsig_siginfo;
+	return 0;
+}
 
-	mutex_enter(p->p_lock);
-	/* Restore signal stack. */
-	if (context.sc_onstack & SS_ONSTACK)
-		l->l_sigstk.ss_flags |= SS_ONSTACK;
-	else
-		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
-	/* Restore signal mask. */
-	native_sigset13_to_sigset((sigset13_t *)&context.sc_mask, &mask);
-	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
-	mutex_exit(p->p_lock);
+int
+netbsd32_amd64_fini(int misc)
+{
 
-	return (EJUSTRETURN);
+	return 0;
 }
-#endif

Index: src/sys/compat/netbsd32/netbsd32_compat_16.c
diff -u src/sys/compat/netbsd32/netbsd32_compat_16.c:1.1.2.1 src/sys/compat/netbsd32/netbsd32_compat_16.c:1.1.2.2
--- src/sys/compat/netbsd32/netbsd32_compat_16.c:1.1.2.1	Wed Sep 12 21:52:17 2018
+++ src/sys/compat/netbsd32/netbsd32_compat_16.c	Fri Sep 14 05:37:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_compat_16.c,v 1.1.2.1 2018/09/12 21:52:17 pgoyette Exp $	*/
+/*	$NetBSD: netbsd32_compat_16.c,v 1.1.2.2 2018/09/14 05:37:08 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_16.c,v 1.1.2.1 2018/09/12 21:52:17 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_16.c,v 1.1.2.2 2018/09/14 05:37:08 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -54,24 +54,36 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_com
 extern char netbsd32_sigcode[], netbsd32_esigcode[];
 struct uvm_object *emul_netbsd32_object;
 
+#ifdef MACHDEP_INIT_PREF
+int __CONCAT(MACHDEP_INIT_PREF,_init)(int);
+int __CONCAT(MACHDEP_INIT_PREF,_fini)(int);
+#endif
+
 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_16, "compat_netbsd32,compat_16");
 
 static int
 compat_netbsd32_16_modcmd(modcmd_t cmd, void *arg)
 {
+int error = 0;
 
 	switch (cmd) {
 	case MODULE_CMD_INIT:
 		emul_netbsd32.e_sigcode = netbsd32_sigcode;
         	emul_netbsd32.e_esigcode = netbsd32_esigcode;
         	emul_netbsd32.e_sigobject = &emul_netbsd32_object;
-		return 0;
+#ifdef MACHDEP_INIT_PREF
+		error = __CONCAT(MACHDEP_INIT_PREF,_init)(0);
+#endif
+		return error;
 
 	case MODULE_CMD_FINI:
 		emul_netbsd32.e_sigcode = NULL;
         	emul_netbsd32.e_esigcode = NULL;
         	emul_netbsd32.e_sigobject = NULL;
-		return 0;
+#ifdef MACHDEP_INIT_PREF
+		error = __CONCAT(MACHDEP_INIT_PREF,_fini)(0);
+#endif
+		return error;
 
 	default:
 		return ENOTTY;

Index: src/sys/compat/netbsd32/netbsd32_mod.c
diff -u src/sys/compat/netbsd32/netbsd32_mod.c:1.13.16.9 src/sys/compat/netbsd32/netbsd32_mod.c:1.13.16.10
--- src/sys/compat/netbsd32/netbsd32_mod.c:1.13.16.9	Fri Sep 14 00:47:48 2018
+++ src/sys/compat/netbsd32/netbsd32_mod.c	Fri Sep 14 05:37:08 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_mod.c,v 1.13.16.9 2018/09/14 00:47:48 pgoyette Exp $	*/
+/*	$NetBSD: netbsd32_mod.c,v 1.13.16.10 2018/09/14 05:37:08 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.13.16.9 2018/09/14 00:47:48 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.13.16.10 2018/09/14 05:37:08 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_execfmt.h"
@@ -102,6 +102,11 @@ static struct execsw netbsd32_execsw[] =
 #endif
 };
 
+#ifdef MACHDEP_INIT_PREF
+int __CONCAT(MACHDEP_INIT_PREF,_init)(int);
+int __CONCAT(MACHDEP_INIT_PREF,_fini)(int);
+#endif
+
 static int
 compat_netbsd32_modcmd(modcmd_t cmd, void *arg)
 {
@@ -113,6 +118,10 @@ compat_netbsd32_modcmd(modcmd_t cmd, voi
 		error = exec_add(netbsd32_execsw,
 		    __arraycount(netbsd32_execsw));
 		vec_compat32_80_modctl = compat32_80_modctl_compat_stub;
+#ifdef MACHDEP_INIT_PREF
+		if (error == 0)
+			error = __CONCAT(MACHDEP_INIT_PREF,_init)(0);
+#endif
 		if (error != 0) {
 			vec_compat32_80_modctl = (void *)enosys;
 			netbsd32_sysctl_fini();
@@ -120,12 +129,20 @@ compat_netbsd32_modcmd(modcmd_t cmd, voi
 		return error;
 
 	case MODULE_CMD_FINI:
+#ifdef MACHDEP_INIT_PREF
+		error = __CONCAT(MACHDEP_INIT_PREF,_fini)(0);
+		if (error)
+			return error;
+#endif
 		vec_compat32_80_modctl = (void *)enosys;
 		error = exec_remove(netbsd32_execsw,
 		    __arraycount(netbsd32_execsw));
-		if (error)
+		if (error) {
 			vec_compat32_80_modctl = compat32_80_modctl_compat_stub;
-		else
+#ifdef MACHDEP_INIT_PREF
+			(void)__CONCAT(MACHDEP_INIT_PREF,_init)(0);
+#endif
+		} else
 			netbsd32_sysctl_fini();
 		return error;
 

Index: src/sys/modules/compat_netbsd32/Makefile
diff -u src/sys/modules/compat_netbsd32/Makefile:1.20.12.10 src/sys/modules/compat_netbsd32/Makefile:1.20.12.11
--- src/sys/modules/compat_netbsd32/Makefile:1.20.12.10	Thu Sep 13 22:10:04 2018
+++ src/sys/modules/compat_netbsd32/Makefile	Fri Sep 14 05:37:08 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.20.12.10 2018/09/13 22:10:04 pgoyette Exp $
+#	$NetBSD: Makefile,v 1.20.12.11 2018/09/14 05:37:08 pgoyette Exp $
 
 .include "../Makefile.inc"
 .include "../Makefile.assym"
@@ -43,8 +43,9 @@ SRCS+=	netbsd32_exec_aout.c
 
 .if ${MACHINE_ARCH} == "x86_64"
 .PATH:	${S}/arch/amd64/amd64
-CPPFLAGS+=	-DCOMPAT_13 -DCOMPAT_16
-SRCS+=	netbsd32_machdep.c netbsd32_sigcode.S netbsd32_syscall.c
+CPPFLAGS+=	-DMTRR
+CPPFLAGS+=	-DMACHDEP_INIT_PREF=netbsd32_amd64
+SRCS+=	netbsd32_machdep.c netbsd32_syscall.c
 .endif
 
 .if ${MACHINE_ARCH} == "sparc64"

Index: src/sys/modules/compat_netbsd32_13/Makefile
diff -u src/sys/modules/compat_netbsd32_13/Makefile:1.1.2.3 src/sys/modules/compat_netbsd32_13/Makefile:1.1.2.4
--- src/sys/modules/compat_netbsd32_13/Makefile:1.1.2.3	Tue Sep 11 06:26:59 2018
+++ src/sys/modules/compat_netbsd32_13/Makefile	Fri Sep 14 05:37:08 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.1.2.3 2018/09/11 06:26:59 pgoyette Exp $
+#	$NetBSD: Makefile,v 1.1.2.4 2018/09/14 05:37:08 pgoyette Exp $
 
 .include "../Makefile.inc"
 
@@ -12,4 +12,9 @@ CPPFLAGS+=	-DCOMPAT_40 -DCOMPAT_50 -DCOM
 
 SRCS+=	netbsd32_compat_13.c
 
+.if ${MACHINE_ARCH} == "x86_64"
+.PATH:  ${S}/arch/amd64/amd64
+SRCS+=  netbsd32_machdep_13.c
+.endif
+
 .include <bsd.kmodule.mk>

Index: src/sys/modules/compat_netbsd32_16/Makefile
diff -u src/sys/modules/compat_netbsd32_16/Makefile:1.1.2.1 src/sys/modules/compat_netbsd32_16/Makefile:1.1.2.2
--- src/sys/modules/compat_netbsd32_16/Makefile:1.1.2.1	Wed Sep 12 01:26:40 2018
+++ src/sys/modules/compat_netbsd32_16/Makefile	Fri Sep 14 05:37:08 2018
@@ -1,6 +1,7 @@
-#	$NetBSD: Makefile,v 1.1.2.1 2018/09/12 01:26:40 pgoyette Exp $
+#	$NetBSD: Makefile,v 1.1.2.2 2018/09/14 05:37:08 pgoyette Exp $
 
 .include "../Makefile.inc"
+.include "../Makefile.assym"
 
 .PATH:	${S}/compat/netbsd32
 
@@ -12,4 +13,10 @@ CPPFLAGS+=	-DCOMPAT_40 -DCOMPAT_50 -DCOM
 
 SRCS+=	netbsd32_compat_16.c
 
+.if ${MACHINE_ARCH} == "x86_64"
+.PATH:  ${S}/arch/amd64/amd64
+CPPFLAGS+=	-DMACHDEP_INIT_PREF=netbsd32_amd64_16
+SRCS+=  netbsd32_machdep_16.c netbsd32_sigcode.S
+.endif
+
 .include <bsd.kmodule.mk>

Added files:

Index: src/sys/arch/amd64/amd64/netbsd32_machdep_13.c
diff -u /dev/null src/sys/arch/amd64/amd64/netbsd32_machdep_13.c:1.1.2.1
--- /dev/null	Fri Sep 14 05:37:08 2018
+++ src/sys/arch/amd64/amd64/netbsd32_machdep_13.c	Fri Sep 14 05:37:08 2018
@@ -0,0 +1,146 @@
+/*	$NetBSD: netbsd32_machdep_13.c,v 1.1.2.1 2018/09/14 05:37:08 pgoyette Exp $	*/
+
+/*
+ * Copyright (c) 2001 Wasabi Systems, Inc.
+ * All rights reserved.
+ *
+ * Written by Frank van der Linden for Wasabi Systems, Inc.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed for the NetBSD Project by
+ *      Wasabi Systems, Inc.
+ * 4. The name of Wasabi Systems, Inc. may not be used to endorse
+ *    or promote products derived from this software without specific prior
+ *    written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
+ * 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: netbsd32_machdep_13.c,v 1.1.2.1 2018/09/14 05:37:08 pgoyette Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_compat_netbsd.h"
+#include "opt_coredump.h"
+#include "opt_execfmt.h"
+#include "opt_user_ldt.h"
+#include "opt_mtrr.h"
+#endif
+
+#include <sys/param.h>
+#include <sys/exec.h>
+#include <sys/exec_aout.h>
+#include <sys/kmem.h>
+#include <sys/malloc.h>
+#include <sys/proc.h>
+#include <sys/signalvar.h>
+#include <sys/systm.h>
+#include <sys/core.h>
+#include <sys/mount.h>
+#include <sys/buf.h>
+#include <sys/vnode.h>
+#include <sys/ras.h>
+#include <sys/ptrace.h>
+#include <sys/kauth.h>
+
+#include <x86/fpu.h>
+#include <x86/dbregs.h>
+#include <machine/frame.h>
+#include <machine/reg.h>
+#include <machine/vmparam.h>
+#ifdef MTRR
+#include <machine/mtrr.h>
+#endif
+#include <machine/netbsd32_machdep.h>
+#include <machine/sysarch.h>
+#include <machine/userret.h>
+
+#include <compat/netbsd32/netbsd32.h>
+#include <compat/netbsd32/netbsd32_exec.h>
+#include <compat/netbsd32/netbsd32_syscallargs.h>
+
+#include <compat/sys/signal.h>
+#include <compat/sys/signalvar.h>
+
+int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
+
+int
+compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
+{
+	/* {
+		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
+	} */
+	struct proc *p = l->l_proc;
+	struct netbsd32_sigcontext13 *scp, context;
+	struct trapframe *tf;
+	sigset_t mask;
+	int error;
+
+	/*
+	 * The trampoline code hands us the context.
+	 * It is unsafe to keep track of it ourselves, in the event that a
+	 * program jumps out of a signal handler.
+	 */
+	scp = (struct netbsd32_sigcontext13 *)NETBSD32PTR64(SCARG(uap, sigcntxp));
+	if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
+		return (EFAULT);
+
+	/* Restore register context. */
+	tf = l->l_md.md_regs;
+
+	/*
+	 * Check for security violations.
+	 */
+	error = check_sigcontext32(l, (const struct netbsd32_sigcontext *)&context);
+	if (error != 0)
+		return error;
+
+	tf->tf_gs = context.sc_gs & 0xFFFF;
+	tf->tf_fs = context.sc_fs & 0xFFFF;		
+	tf->tf_es = context.sc_es & 0xFFFF;
+	tf->tf_ds = context.sc_ds & 0xFFFF;
+	tf->tf_rflags = context.sc_eflags;
+	tf->tf_rdi = context.sc_edi;
+	tf->tf_rsi = context.sc_esi;
+	tf->tf_rbp = context.sc_ebp;
+	tf->tf_rbx = context.sc_ebx;
+	tf->tf_rdx = context.sc_edx;
+	tf->tf_rcx = context.sc_ecx;
+	tf->tf_rax = context.sc_eax;
+	tf->tf_rip = context.sc_eip;
+	tf->tf_cs = context.sc_cs & 0xFFFF;
+	tf->tf_rsp = context.sc_esp;
+	tf->tf_ss = context.sc_ss & 0xFFFF;
+
+	mutex_enter(p->p_lock);
+	/* Restore signal stack. */
+	if (context.sc_onstack & SS_ONSTACK)
+		l->l_sigstk.ss_flags |= SS_ONSTACK;
+	else
+		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
+	/* Restore signal mask. */
+	native_sigset13_to_sigset((sigset13_t *)&context.sc_mask, &mask);
+	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
+	mutex_exit(p->p_lock);
+
+	return (EJUSTRETURN);
+}
Index: src/sys/arch/amd64/amd64/netbsd32_machdep_16.c
diff -u /dev/null src/sys/arch/amd64/amd64/netbsd32_machdep_16.c:1.1.2.1
--- /dev/null	Fri Sep 14 05:37:08 2018
+++ src/sys/arch/amd64/amd64/netbsd32_machdep_16.c	Fri Sep 14 05:37:08 2018
@@ -0,0 +1,262 @@
+/*	$NetBSD: netbsd32_machdep_16.c,v 1.1.2.1 2018/09/14 05:37:08 pgoyette Exp $	*/
+
+/*
+ * Copyright (c) 2001 Wasabi Systems, Inc.
+ * All rights reserved.
+ *
+ * Written by Frank van der Linden for Wasabi Systems, Inc.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *      This product includes software developed for the NetBSD Project by
+ *      Wasabi Systems, Inc.
+ * 4. The name of Wasabi Systems, Inc. may not be used to endorse
+ *    or promote products derived from this software without specific prior
+ *    written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
+ * 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: netbsd32_machdep_16.c,v 1.1.2.1 2018/09/14 05:37:08 pgoyette Exp $");
+
+#ifdef _KERNEL_OPT
+#include "opt_compat_netbsd.h"
+#include "opt_coredump.h"
+#include "opt_execfmt.h"
+#endif
+
+#include <sys/param.h>
+#include <sys/exec.h>
+#include <sys/exec_aout.h>
+#include <sys/kmem.h>
+#include <sys/malloc.h>
+#include <sys/proc.h>
+#include <sys/signalvar.h>
+#include <sys/systm.h>
+#include <sys/core.h>
+#include <sys/mount.h>
+#include <sys/buf.h>
+#include <sys/vnode.h>
+#include <sys/ras.h>
+#include <sys/ptrace.h>
+#include <sys/kauth.h>
+
+#include <x86/fpu.h>
+#include <x86/dbregs.h>
+#include <machine/frame.h>
+#include <machine/reg.h>
+#include <machine/vmparam.h>
+#include <machine/netbsd32_machdep.h>
+#include <machine/sysarch.h>
+#include <machine/userret.h>
+
+#include <compat/netbsd32/netbsd32.h>
+#include <compat/netbsd32/netbsd32_exec.h>
+#include <compat/netbsd32/netbsd32_syscallargs.h>
+
+#include <compat/sys/signal.h>
+#include <compat/sys/signalvar.h>
+
+void netbsd32_buildcontext(struct lwp *, struct trapframe *, void *,
+    sig_t, int);
+
+void netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
+
+int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
+
+void netbsd32_sendsig_16(const ksiginfo_t *, const sigset_t *);
+
+static void
+netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
+{
+	struct lwp *l = curlwp;
+	struct proc *p = l->l_proc;
+	struct trapframe *tf;
+	int sig = ksi->ksi_signo;
+	sig_t catcher = SIGACTION(p, sig).sa_handler;
+	struct netbsd32_sigframe_sigcontext *fp, frame;
+	int onstack, error;
+	struct sigacts *ps = p->p_sigacts;
+
+	tf = l->l_md.md_regs;
+
+	/* Do we need to jump onto the signal stack? */
+	onstack =
+	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
+	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
+
+	/* Allocate space for the signal handler context. */
+	if (onstack)
+		fp = (struct netbsd32_sigframe_sigcontext *)
+		    ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
+	else
+		fp = (struct netbsd32_sigframe_sigcontext *)tf->tf_rsp;
+	fp--;
+
+	/* Build stack frame for signal trampoline. */
+	switch (ps->sa_sigdesc[sig].sd_vers) {
+	case 0:
+		frame.sf_ra = (uint32_t)(u_long)p->p_sigctx.ps_sigcode;
+		break;
+	case 1:
+		frame.sf_ra = (uint32_t)(u_long)ps->sa_sigdesc[sig].sd_tramp;
+		break;
+	default:
+		/* Don't know what trampoline version; kill it. */
+		sigexit(l, SIGILL);
+	}
+	frame.sf_signum = sig;
+	frame.sf_code = ksi->ksi_trap;
+	frame.sf_scp = (uint32_t)(u_long)&fp->sf_sc;
+
+	frame.sf_sc.sc_ds = tf->tf_ds & 0xFFFF;
+	frame.sf_sc.sc_es = tf->tf_es & 0xFFFF;
+	frame.sf_sc.sc_fs = tf->tf_fs & 0xFFFF;
+	frame.sf_sc.sc_gs = tf->tf_gs & 0xFFFF;
+
+	frame.sf_sc.sc_eflags = tf->tf_rflags;
+	frame.sf_sc.sc_edi = tf->tf_rdi;
+	frame.sf_sc.sc_esi = tf->tf_rsi;
+	frame.sf_sc.sc_ebp = tf->tf_rbp;
+	frame.sf_sc.sc_ebx = tf->tf_rbx;
+	frame.sf_sc.sc_edx = tf->tf_rdx;
+	frame.sf_sc.sc_ecx = tf->tf_rcx;
+	frame.sf_sc.sc_eax = tf->tf_rax;
+	frame.sf_sc.sc_eip = tf->tf_rip;
+	frame.sf_sc.sc_cs = tf->tf_cs & 0xFFFF;
+	frame.sf_sc.sc_esp = tf->tf_rsp;
+	frame.sf_sc.sc_ss = tf->tf_ss & 0xFFFF;
+	frame.sf_sc.sc_trapno = tf->tf_trapno;
+	frame.sf_sc.sc_err = tf->tf_err;
+
+	/* Save signal stack. */
+	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
+
+	/* Save signal mask. */
+	frame.sf_sc.sc_mask = *mask;
+
+	sendsig_reset(l, sig);
+
+	mutex_exit(p->p_lock);
+	error = copyout(&frame, fp, sizeof(frame));
+	mutex_enter(p->p_lock);
+
+	if (error != 0) {
+		/*
+		 * Process has trashed its stack; give it an illegal
+		 * instruction to halt it in its tracks.
+		 */
+		sigexit(l, SIGILL);
+		/* NOTREACHED */
+	}
+
+	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
+}
+
+void
+netbsd32_sendsig_16(const ksiginfo_t *ksi, const sigset_t *mask)
+{
+	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2)
+		netbsd32_sendsig_sigcontext(ksi, mask);
+	else
+		netbsd32_sendsig_siginfo(ksi, mask);
+}
+
+int
+compat_16_netbsd32___sigreturn14(struct lwp *l, const struct compat_16_netbsd32___sigreturn14_args *uap, register_t *retval)
+{
+	/* {
+		syscallarg(netbsd32_sigcontextp_t) sigcntxp;
+	} */
+	struct netbsd32_sigcontext *scp, context;
+	struct proc *p = l->l_proc;
+	struct trapframe *tf;
+	int error;
+
+	/*
+	 * The trampoline code hands us the context.
+	 * It is unsafe to keep track of it ourselves, in the event that a
+	 * program jumps out of a signal handler.
+	 */
+	scp = NETBSD32PTR64(SCARG(uap, sigcntxp));
+	if (copyin(scp, &context, sizeof(*scp)) != 0)
+		return (EFAULT);
+
+	/*
+	 * Check for security violations.
+	 */
+	error = check_sigcontext32(l, &context);
+	if (error != 0)
+		return error;
+
+	/* Restore register context. */
+	tf = l->l_md.md_regs;
+	tf->tf_ds = context.sc_ds & 0xFFFF;
+	tf->tf_es = context.sc_es & 0xFFFF;
+	cpu_fsgs_reload(l, context.sc_fs, context.sc_gs);
+	tf->tf_rflags = context.sc_eflags;
+	tf->tf_rdi = context.sc_edi;
+	tf->tf_rsi = context.sc_esi;
+	tf->tf_rbp = context.sc_ebp;
+	tf->tf_rbx = context.sc_ebx;
+	tf->tf_rdx = context.sc_edx;
+	tf->tf_rcx = context.sc_ecx;
+	tf->tf_rax = context.sc_eax;
+
+	tf->tf_rip = context.sc_eip;
+	tf->tf_cs = context.sc_cs & 0xFFFF;
+	tf->tf_rsp = context.sc_esp;
+	tf->tf_ss = context.sc_ss & 0xFFFF;
+
+	mutex_enter(p->p_lock);
+	/* Restore signal stack. */
+	if (context.sc_onstack & SS_ONSTACK)
+		l->l_sigstk.ss_flags |= SS_ONSTACK;
+	else
+		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
+	/* Restore signal mask. */
+	(void) sigprocmask1(l, SIG_SETMASK, &context.sc_mask, 0);
+	mutex_exit(p->p_lock);
+
+	return (EJUSTRETURN);
+}
+
+extern void (*vec_netbsd32_sendsig)(const ksiginfo_t *, const sigset_t *);
+
+int netbsd32_amd64_16_init(int);
+int netbsd32_amd64_16_fini(int);
+
+int
+netbsd32_amd64_16_init(int misc)
+{
+
+	vec_netbsd32_sendsig = netbsd32_sendsig_16;
+	return 0;
+}
+
+int
+netbsd32_amd64_16_fini(int misc)
+{
+
+	vec_netbsd32_sendsig = netbsd32_sendsig_siginfo;
+	return 0;
+}

Reply via email to