Module Name:    src
Committed By:   thorpej
Date:           Sat Nov  6 20:42:57 UTC 2021

Modified Files:
        src/sys/arch/aarch64/aarch64: netbsd32_machdep.c
        src/sys/arch/amd64/amd64: netbsd32_machdep.c netbsd32_machdep_16.c
        src/sys/arch/arm/include: netbsd32_machdep.h
        src/sys/arch/mips/mips: netbsd32_machdep.c netbsd32_machdep_16.c
        src/sys/arch/sparc64/sparc64: netbsd32_machdep.c netbsd32_machdep_16.c
        src/sys/compat/netbsd32: netbsd32.h netbsd32_exec.h netbsd32_signal.c

Log Message:
COMPAT_NETBSD32 is all about running the 32-bit flavor of native
binaries on a 64-bit platform[*], as such:
- Make the logic about which "sendsig" flavor to call MI (as it is in the
  native 64-bit environment) and follow the same rules as the native 32-bit
  environment.
- Make COMPAT_NETBSD32 x COMPAT_16 work the same as it would in the
  native 32-bit environment by providing a netbsd32_sendsig_sigcontext_16_hook,
  rather than overriding the entire sendsig logic with a netbsd32_sendsig_hook.
- In netbsd32___sigaction_sigtramp(), make sure the compat_netbsd32_16
  module is loaded if the trampoline version specifies a sigcontext style
  handler, otherwise return EINVAL so that libc can try again with siginfo
  style.

[*] ...except for arm32, which uses it to mean "run 32-bit OABI binaries
from the 32-bit EABI environment".  Doing it this way was arguably a mistake,
but we are stuck with it for now, so support it by providing a machine-
dependent override for netbsd32_sendsig() that also disables the corresponding
logic in netbsd32___sigaction_sigtramp().

Fixes PR kern/56487.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/aarch64/aarch64/netbsd32_machdep.c
cvs rdiff -u -r1.139 -r1.140 src/sys/arch/amd64/amd64/netbsd32_machdep.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/amd64/netbsd32_machdep_16.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/arm/include/netbsd32_machdep.h
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/mips/mips/netbsd32_machdep.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/mips/mips/netbsd32_machdep_16.c
cvs rdiff -u -r1.116 -r1.117 src/sys/arch/sparc64/sparc64/netbsd32_machdep.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c
cvs rdiff -u -r1.137 -r1.138 src/sys/compat/netbsd32/netbsd32.h
cvs rdiff -u -r1.36 -r1.37 src/sys/compat/netbsd32/netbsd32_exec.h
cvs rdiff -u -r1.52 -r1.53 src/sys/compat/netbsd32/netbsd32_signal.c

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/aarch64/aarch64/netbsd32_machdep.c
diff -u src/sys/arch/aarch64/aarch64/netbsd32_machdep.c:1.21 src/sys/arch/aarch64/aarch64/netbsd32_machdep.c:1.22
--- src/sys/arch/aarch64/aarch64/netbsd32_machdep.c:1.21	Mon Nov  1 05:07:15 2021
+++ src/sys/arch/aarch64/aarch64/netbsd32_machdep.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.21 2021/11/01 05:07:15 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.22 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2018 Ryo Shimizu <r...@nerv.org>
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.21 2021/11/01 05:07:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.22 2021/11/06 20:42:56 thorpej Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_compat_netbsd.h"
@@ -300,7 +300,7 @@ cpu_coredump32(struct lwp *l, struct cor
 	return error;
 }
 
-static void
+void
 netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
 {
 	struct lwp * const l = curlwp;
@@ -375,19 +375,6 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 }
 
 void
-netbsd32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-#ifdef COMPAT_16
-#error non EABI generation binaries are not supported
-	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers <
-	    __SIGTRAMP_SIGINFO_VERSION)
-		netbsd32_sendsig_sigcontext(ksi, mask);
-	else
-#endif
-		netbsd32_sendsig_siginfo(ksi, mask);
-}
-
-void
 startlwp32(void *arg)
 {
 	ucontext32_t *uc = arg;

Index: src/sys/arch/amd64/amd64/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.139 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.140
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.139	Mon Nov  1 05:07:15 2021
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.139 2021/11/01 05:07:15 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.140 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.139 2021/11/01 05:07:15 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.140 2021/11/06 20:42:56 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -109,7 +109,6 @@ static int x86_64_set_mtrr32(struct lwp 
 int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
 void netbsd32_buildcontext(struct lwp *, struct trapframe *, void *,
     sig_t, int);
-int netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
 
 #ifdef EXEC_AOUT
 /*
@@ -206,7 +205,7 @@ netbsd32_buildcontext(struct lwp *l, str
 	}
 }
 
-int
+void
 netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
 {
 	struct lwp *l = curlwp;
@@ -274,18 +273,6 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 	}
 
 	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
-
-	return 0;
-}
-
-struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
-
-void
-netbsd32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-
-	MODULE_HOOK_CALL_VOID(netbsd32_sendsig_hook, (ksi, mask),
-	    netbsd32_sendsig_siginfo(ksi, mask));
 }
 
 /*

Index: src/sys/arch/amd64/amd64/netbsd32_machdep_16.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep_16.c:1.6 src/sys/arch/amd64/amd64/netbsd32_machdep_16.c:1.7
--- src/sys/arch/amd64/amd64/netbsd32_machdep_16.c:1.6	Wed Oct 27 04:14:59 2021
+++ src/sys/arch/amd64/amd64/netbsd32_machdep_16.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/10/27 04:14:59 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep_16.c,v 1.7 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/10/27 04:14:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.7 2021/11/06 20:42:56 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -79,14 +79,8 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mac
 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 *);
-
-extern struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
-
 static void
 netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
 {
@@ -176,16 +170,6 @@ netbsd32_sendsig_sigcontext(const ksigin
 	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 <
-	    __SIGTRAMP_SIGINFO_VERSION)
-		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,
@@ -251,12 +235,13 @@ void
 netbsd32_machdep_md_16_init(void)
 {
 
-	MODULE_HOOK_SET(netbsd32_sendsig_hook, netbsd32_sendsig_16);
+	MODULE_HOOK_SET(netbsd32_sendsig_sigcontext_16_hook,
+	    netbsd32_sendsig_sigcontext);
 }
 
 void
 netbsd32_machdep_md_16_fini(void)
 {
 
-	MODULE_HOOK_UNSET(netbsd32_sendsig_hook);
+	MODULE_HOOK_UNSET(netbsd32_sendsig_sigcontext_16_hook);
 }

Index: src/sys/arch/arm/include/netbsd32_machdep.h
diff -u src/sys/arch/arm/include/netbsd32_machdep.h:1.3 src/sys/arch/arm/include/netbsd32_machdep.h:1.4
--- src/sys/arch/arm/include/netbsd32_machdep.h:1.3	Wed Oct  6 05:33:15 2021
+++ src/sys/arch/arm/include/netbsd32_machdep.h	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.h,v 1.3 2021/10/06 05:33:15 skrll Exp $	*/
+/*	$NetBSD: netbsd32_machdep.h,v 1.4 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -34,6 +34,10 @@
 
 #include <sys/types.h>
 
+#ifdef __aarch64__
+#error You have wandered from the path of righteousness.
+#endif
+
 #define	NETBSD32_POINTER_TYPE			uint32_t
 typedef struct { NETBSD32_POINTER_TYPE i32; }	netbsd32_pointer_t;
 
@@ -55,6 +59,12 @@ typedef netbsd32_pointer_t			netbsd32_si
 #define	netbsd32_esigcode			esigcode
 
 /*
+ * Using the entirety of COMPAT_NETBSD32 to support the OABI was
+ * a gigantic mistake, but here we are.
+ */
+#define	__HAVE_MD_NETBSD32_SENDSIG
+
+/*
  * Note: syscall_intern and setregs do not care about COMPAT_NETBSD32.
  */
 #define	netbsd32_syscall_intern			syscall_intern

Index: src/sys/arch/mips/mips/netbsd32_machdep.c
diff -u src/sys/arch/mips/mips/netbsd32_machdep.c:1.22 src/sys/arch/mips/mips/netbsd32_machdep.c:1.23
--- src/sys/arch/mips/mips/netbsd32_machdep.c:1.22	Wed Oct 27 04:15:00 2021
+++ src/sys/arch/mips/mips/netbsd32_machdep.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.22 2021/10/27 04:15:00 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.23 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.22 2021/10/27 04:15:00 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.23 2021/11/06 20:42:56 thorpej Exp $");
 
 #include "opt_compat_netbsd.h"
 
@@ -78,8 +78,6 @@ netbsd32_cpu_upcall
 netbsd32_vm_default_addr
 #endif
 
-int netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
-
 struct sigframe_siginfo32 {
 	siginfo32_t sf_si;
 	ucontext32_t sf_uc;
@@ -88,7 +86,7 @@ struct sigframe_siginfo32 {
 /*
  * Send a signal to process.
  */
-int
+void
 netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
 {
 	struct lwp * const l = curlwp;
@@ -160,8 +158,6 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 	/* Remember that we're now on the signal stack. */
 	if (onstack)
 		l->l_sigstk.ss_flags |= SS_ONSTACK;
-
-	return 0;
 }
 
 int
@@ -310,16 +306,6 @@ cpu_coredump32(struct lwp *l, struct cor
 	return error;
 }
 
-struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
- 
-void
-netbsd32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-
-	MODULE_HOOK_CALL_VOID(netbsd32_sendsig_hook, (ksi, mask),  
-	    netbsd32_sendsig_siginfo(ksi, mask));
-}
-
 static const char *
 netbsd32_machine32(void)
 {

Index: src/sys/arch/mips/mips/netbsd32_machdep_16.c
diff -u src/sys/arch/mips/mips/netbsd32_machdep_16.c:1.6 src/sys/arch/mips/mips/netbsd32_machdep_16.c:1.7
--- src/sys/arch/mips/mips/netbsd32_machdep_16.c:1.6	Wed Oct 27 04:15:00 2021
+++ src/sys/arch/mips/mips/netbsd32_machdep_16.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/10/27 04:15:00 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep_16.c,v 1.7 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/10/27 04:15:00 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.7 2021/11/06 20:42:56 thorpej Exp $");
 
 #include "opt_compat_netbsd.h"
 
@@ -66,12 +66,7 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mac
 
 #include <uvm/uvm_extern.h>
 
-void netbsd32_sendsig_16(const ksiginfo_t *, const sigset_t *);
-
 void sendsig_context(const ksiginfo_t *, const sigset_t *);
-int netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
-
-extern struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
 
 int
 compat_16_netbsd32___sigreturn14(struct lwp *l,
@@ -85,26 +80,17 @@ compat_16_netbsd32___sigreturn14(struct 
 	return compat_16_sys___sigreturn14(l, &ua, retval);
 }
 
-void
-netbsd32_sendsig_16(const ksiginfo_t *ksi, const sigset_t *mask)
-{               
-	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers <
-	    __SIGTRAMP_SIGINFO_VERSION)
-		sendsig_sigcontext(ksi, mask);
-	else    
-		netbsd32_sendsig_siginfo(ksi, mask);
-}       
-
 void    
 netbsd32_machdep_md_16_init(void)
 {       
                 
-	MODULE_HOOK_SET(netbsd32_sendsig_hook, netbsd32_sendsig_16); 
+	MODULE_HOOK_SET(netbsd32_sendsig_sigcontext_16_hook,
+	    sendsig_sigcontext); 
 }               
                 
 void            
 netbsd32_machdep_md_16_fini(void)
 {       
 
-	MODULE_HOOK_UNSET(netbsd32_sendsig_hook);
+	MODULE_HOOK_UNSET(netbsd32_sendsig_sigcontext_16_hook);
 }

Index: src/sys/arch/sparc64/sparc64/netbsd32_machdep.c
diff -u src/sys/arch/sparc64/sparc64/netbsd32_machdep.c:1.116 src/sys/arch/sparc64/sparc64/netbsd32_machdep.c:1.117
--- src/sys/arch/sparc64/sparc64/netbsd32_machdep.c:1.116	Wed Oct 27 04:15:00 2021
+++ src/sys/arch/sparc64/sparc64/netbsd32_machdep.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.116 2021/10/27 04:15:00 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.117 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.116 2021/10/27 04:15:00 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.117 2021/11/06 20:42:56 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -89,8 +89,6 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mac
 const char	machine32[] = "sparc";	
 const char	machine_arch32[] = "sparc";	
 
-int netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
-
 #if NFIRM_EVENTS > 0
 static int ev_out32(struct firm_event *, int, struct uio *);
 #endif
@@ -158,7 +156,7 @@ struct sparc32_sigframe_siginfo {
 	ucontext32_t sf_uc;
 };
 
-int
+void
 netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
 {
 	struct lwp *l = curlwp;
@@ -256,18 +254,6 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 	/* Remember that we're now on the signal stack. */
 	if (onstack)
 		l->l_sigstk.ss_flags |= SS_ONSTACK;
-
-	return 0;
-}
-
-struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
-        
-void
-netbsd32_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-
-	MODULE_HOOK_CALL_VOID(netbsd32_sendsig_hook, (ksi, mask),
-	    netbsd32_sendsig_siginfo(ksi, mask));
 }
 
 #undef DEBUG

Index: src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c
diff -u src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c:1.5 src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c:1.6
--- src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c:1.5	Wed Oct 27 04:15:00 2021
+++ src/sys/arch/sparc64/sparc64/netbsd32_machdep_16.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep_16.c,v 1.5 2021/10/27 04:15:00 thorpej Exp $	*/
+/*	$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.5 2021/10/27 04:15:00 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.6 2021/11/06 20:42:56 thorpej Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -83,10 +83,6 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mac
 #include <machine/netbsd32_machdep.h>
 #include <machine/userret.h>
 
-void netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
-
-void netbsd32_sendsig_16(const ksiginfo_t *, const sigset_t *);
-
 /*
  * NB: since this is a 32-bit address world, sf_scp and sf_sc
  *	can't be a pointer since those are 64-bits wide.
@@ -99,8 +95,6 @@ struct sparc32_sigframe {
 	struct	netbsd32_sigcontext sf_sc;	/* actual sigcontext */
 };
 
-extern struct netbsd32_sendsig_hook_t netbsd32_sendsig_hook;
-
 #undef DEBUG
 #ifdef DEBUG
 extern int sigdebug;
@@ -245,16 +239,6 @@ struct sparc32_sigframe_siginfo {
 	ucontext32_t sf_uc;
 };
 
-void
-netbsd32_sendsig_16(const ksiginfo_t *ksi, const sigset_t *mask)
-{
-	if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers <
-	    __SIGTRAMP_SIGINFO_VERSION)
-		netbsd32_sendsig_sigcontext(ksi, mask);
-	else
-		netbsd32_sendsig_siginfo(ksi, mask);
-}
-
 #undef DEBUG
 
 /*
@@ -353,12 +337,13 @@ void
 netbsd32_machdep_md_16_init(void)
 {
 
-	MODULE_HOOK_SET(netbsd32_sendsig_hook, netbsd32_sendsig_16);
+	MODULE_HOOK_SET(netbsd32_sendsig_sigcontext_16_hook,
+	    netbsd32_sendsig_sigcontext);
 }
  
 void    
 netbsd32_machdep_md_16_fini(void)  
 {
         
-	MODULE_HOOK_UNSET(netbsd32_sendsig_hook);
+	MODULE_HOOK_UNSET(netbsd32_sendsig_sigcontext_16_hook);
 }

Index: src/sys/compat/netbsd32/netbsd32.h
diff -u src/sys/compat/netbsd32/netbsd32.h:1.137 src/sys/compat/netbsd32/netbsd32.h:1.138
--- src/sys/compat/netbsd32/netbsd32.h:1.137	Tue Jan 19 03:41:22 2021
+++ src/sys/compat/netbsd32/netbsd32.h	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32.h,v 1.137 2021/01/19 03:41:22 simonb Exp $	*/
+/*	$NetBSD: netbsd32.h,v 1.138 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001, 2008, 2015 Matthew R. Green
@@ -1250,8 +1250,10 @@ struct iovec *netbsd32_get_iov(struct ne
 SYSCTL_SETUP_PROTO(netbsd32_sysctl_emul_setup);
 #endif /* SYSCTL_SETUP_PROTO */
 
-MODULE_HOOK(netbsd32_sendsig_hook, void,
-    (const ksiginfo_t *, const sigset_t *));
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+MODULE_HOOK(netbsd32_sendsig_sigcontext_16_hook, void,
+    (const struct ksiginfo *, const sigset_t *));
+#endif
 
 extern struct sysent netbsd32_sysent[];
 extern const uint32_t netbsd32_sysent_nomodbits[]; 

Index: src/sys/compat/netbsd32/netbsd32_exec.h
diff -u src/sys/compat/netbsd32/netbsd32_exec.h:1.36 src/sys/compat/netbsd32/netbsd32_exec.h:1.37
--- src/sys/compat/netbsd32/netbsd32_exec.h:1.36	Tue Jan 19 02:40:07 2021
+++ src/sys/compat/netbsd32/netbsd32_exec.h	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_exec.h,v 1.36 2021/01/19 02:40:07 simonb Exp $	*/
+/*	$NetBSD: netbsd32_exec.h,v 1.37 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -73,6 +73,7 @@ static __inline int netbsd32_copyargs(st
 void netbsd32_setregs(struct lwp *, struct exec_package *, vaddr_t stack);
 int netbsd32_sigreturn(struct proc *, void *, register_t *);
 void netbsd32_sendsig(const ksiginfo_t *, const sigset_t *);
+void netbsd32_sendsig_siginfo(const ksiginfo_t *, const sigset_t *);
 
 extern char netbsd32_esigcode[], netbsd32_sigcode[];
 

Index: src/sys/compat/netbsd32/netbsd32_signal.c
diff -u src/sys/compat/netbsd32/netbsd32_signal.c:1.52 src/sys/compat/netbsd32/netbsd32_signal.c:1.53
--- src/sys/compat/netbsd32/netbsd32_signal.c:1.52	Tue Sep  7 11:43:05 2021
+++ src/sys/compat/netbsd32/netbsd32_signal.c	Sat Nov  6 20:42:56 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_signal.c,v 1.52 2021/09/07 11:43:05 riastradh Exp $	*/
+/*	$NetBSD: netbsd32_signal.c,v 1.53 2021/11/06 20:42:56 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.52 2021/09/07 11:43:05 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_signal.c,v 1.53 2021/11/06 20:42:56 thorpej Exp $");
 
 #if defined(_KERNEL_OPT) 
 #include "opt_ktrace.h"
@@ -43,11 +43,14 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_sig
 #include <sys/proc.h>
 #include <sys/wait.h>
 #include <sys/dirent.h>
+#include <sys/module.h>
+#include <sys/exec.h>
 
 #include <uvm/uvm_extern.h>
 
 #include <compat/netbsd32/netbsd32.h>
 #include <compat/netbsd32/netbsd32_conv.h>
+#include <compat/netbsd32/netbsd32_exec.h>
 #include <compat/netbsd32/netbsd32_syscallargs.h>
 
 #include <compat/sys/signal.h>
@@ -160,7 +163,7 @@ netbsd32___sigaction_sigtramp(struct lwp
 	} */
 	struct netbsd32_sigaction sa32;
 	struct sigaction nsa, osa;
-	int error;
+	int error, vers;
 
 	if (SCARG_P32(uap, nsa)) {
 		error = copyin(SCARG_P32(uap, nsa), &sa32, sizeof(sa32));
@@ -170,10 +173,53 @@ netbsd32___sigaction_sigtramp(struct lwp
 		nsa.sa_mask = sa32.netbsd32_sa_mask;
 		nsa.sa_flags = sa32.netbsd32_sa_flags;
 	}
+	vers = SCARG(uap, vers);
+#ifndef __HAVE_MD_NETBSD32_SENDSIG	/* XXX paying for yesterday's sins */
+	if (vers < __SIGTRAMP_SIGINFO_VERSION_MIN) {
+		/*
+		 * sigaction1() doesn't enforce sigcontext-ness for
+		 * __SIGTRAMP_SIGCODE_VERSION because it might be
+		 * a foreign emulation.  However, we know these are
+		 * native NetBSD 32-bit binaries, so we do.
+		 */
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+		struct proc *p = l->l_proc;
+		bool sigcontext_valid = false;
+
+		/*
+		 * We need to ensure the compat_netbsd32_16 module
+		 * is loaded, because sigaction1() gives a free pass
+		 * to processes marked PK_32 (it can't be sure which
+		 * 32-bit compat module is needed).
+		 */
+		if ((p->p_lflag & PL_SIGCOMPAT) == 0) {
+			kernconfig_lock();
+			(void)module_autoload("compat_netbsd32_16",
+			    MODULE_CLASS_ANY);
+			if (netbsd32_sendsig_sigcontext_16_hook.hooked) {
+				sigcontext_valid = true;
+			}
+			mutex_enter(&proc_lock);
+			/*
+			 * Prevent unload of compat module while
+			 * this process remains.
+			 */
+			p->p_lflag |= PL_SIGCOMPAT;
+			mutex_exit(&proc_lock);
+			kernconfig_unlock();
+		}
+		if (!sigcontext_valid) {
+			return EINVAL;
+		}
+#else /* ! __HAVE_STRUCT_SIGCONTEXT */
+		return EINVAL;
+#endif /* __HAVE_STRUCT_SIGCONTEXT */
+	}
+#endif /* __HAVE_MD_NETBSD32_SENDSIG */
 	error = sigaction1(l, SCARG(uap, signum),
 	    SCARG_P32(uap, nsa) ? &nsa : 0,
 	    SCARG_P32(uap, osa) ? &osa : 0,
-	    SCARG_P32(uap, tramp), SCARG(uap, vers));
+	    SCARG_P32(uap, tramp), vers);
 	if (error)
 		return error;
 	if (SCARG_P32(uap, osa)) {
@@ -188,6 +234,43 @@ netbsd32___sigaction_sigtramp(struct lwp
 	return 0;
 }
 
+#ifndef __HAVE_MD_NETBSD32_SENDSIG	/* XXX paying for yesterday's sins */
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+struct netbsd32_sendsig_sigcontext_16_hook_t netbsd32_sendsig_sigcontext_16_hook;
+#endif
+
+void
+netbsd32_sendsig(const struct ksiginfo *ksi, const sigset_t *mask)
+{
+	struct sigacts *sa;
+	int sig;
+
+	sig = ksi->ksi_signo;
+	sa = curproc->p_sigacts;
+
+	switch (sa->sa_sigdesc[sig].sd_vers) {
+#ifdef __HAVE_STRUCT_SIGCONTEXT
+	case __SIGTRAMP_SIGCODE_VERSION:
+	case __SIGTRAMP_SIGCONTEXT_VERSION_MIN ...
+	     __SIGTRAMP_SIGCONTEXT_VERSION_MAX:
+		/* Compat for 1.6 and earlier. */
+		MODULE_HOOK_CALL_VOID(netbsd32_sendsig_sigcontext_16_hook,
+		    (ksi, mask), break);
+		return;
+#endif /* __HAVE_STRUCT_SIGCONTEXT */
+	case __SIGTRAMP_SIGINFO_VERSION_MIN ...
+	     __SIGTRAMP_SIGINFO_VERSION_MAX:
+		netbsd32_sendsig_siginfo(ksi, mask);
+		return;
+	default:
+		break;
+	}
+
+	printf("%s: bad version %d\n", __func__, sa->sa_sigdesc[sig].sd_vers);
+	sigexit(curlwp, SIGILL);
+}
+#endif /* __HAVE_MD_NETBSD32_SENDSIG */
+
 void
 netbsd32_ksi32_to_ksi(struct _ksiginfo *si, const struct __ksiginfo32 *si32)
 {

Reply via email to