1 /*        $NetBSD: netbsd32_compat_16.c,v 1.10 2025/02/28 18:35:44 wiz Exp $    */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_16.c,v 1.10 2025/02/28 18:35:44 wiz Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/dirent.h>
38 #include <sys/exec.h>
39 #include <sys/proc.h>
40 #include <sys/lwp.h>
41 #include <sys/syscallargs.h>
42 #include <sys/syscallvar.h>
43 
44 #include <compat/netbsd32/netbsd32.h>
45 #include <compat/netbsd32/netbsd32_syscall.h>
46 #include <compat/netbsd32/netbsd32_syscallargs.h>
47 #include <compat/netbsd32/netbsd32_conv.h>
48 
49 struct uvm_object *emul_netbsd32_object;
50 
51 #if defined(__amd64__) || defined(__arm__) || defined(__mips__) || defined(__sparc64__)
52 #define __HAVE_MD_NETBSD32_SIGRETURN14
53 #endif
54 
55 #if defined(__amd64__) || defined(__arm__) || defined(__mips__) || defined(__powerpc__) || defined(__riscv__)
56 #define __HAVE_MD_NETBSD32_SIGCODE
57 #endif
58 
59 
60 #ifdef __HAVE_MD_NETBSD32_SIGRETURN14
61 static const struct syscall_package netbsd32_kern_sig_16_syscalls[] = {
62         /* compat_16_netbsd32___sigreturn14 is in MD code! */
63         { NETBSD32_SYS_compat_16_netbsd32___sigreturn14, 0,
64             (sy_call_t *)compat_16_netbsd32___sigreturn14 },
65         { 0, 0, NULL }
66 };
67 #endif
68 
69 static int
compat_netbsd32_16_init(void)70 compat_netbsd32_16_init(void)
71 {
72 #if defined(__HAVE_MD_NETBSD32_SIGRETURN14) || defined(__HAVE_MD_NETBSD32_SIGCODE)
73           int error;
74 #endif
75 
76 #ifdef __HAVE_MD_NETBSD32_SIGRETURN14
77           error = syscall_establish(&emul_netbsd32,
78               netbsd32_kern_sig_16_syscalls);
79           if (error)
80                     return error;
81 #endif
82 
83 #ifdef __HAVE_MD_NETBSD32_SIGCODE
84           rw_enter(&exec_lock, RW_WRITER);
85           emul_netbsd32.e_sigcode = netbsd32_sigcode;
86           emul_netbsd32.e_esigcode = netbsd32_esigcode;
87           emul_netbsd32.e_sigobject = &emul_netbsd32_object;
88           error = exec_sigcode_alloc(&emul_netbsd);
89           if (error) {
90                     emul_netbsd32.e_sigcode = NULL;
91                     emul_netbsd32.e_esigcode = NULL;
92                     emul_netbsd32.e_sigobject = NULL;
93           }
94           rw_exit(&exec_lock);
95           if (error)
96                     return error;
97           netbsd32_machdep_md_16_init();
98 #endif
99           return 0;
100 }
101 
102 static int
compat_netbsd32_16_fini(void)103 compat_netbsd32_16_fini(void)
104 {
105 #if defined(__HAVE_MD_NETBSD32_SIGRETURN14)
106           proc_t *p;
107           int error;
108 #endif
109 
110 #ifdef __HAVE_MD_NETBSD32_SIGRETURN14
111           error = syscall_disestablish(&emul_netbsd32,
112               netbsd32_kern_sig_16_syscalls);
113           if (error)
114                     return error;
115           /*
116            * Ensure sendsig_sigcontext() is not being used.
117            * module_lock prevents the flag being set on any
118            * further processes while we are here.  See
119            * sigaction1() for the opposing half.
120            */
121           mutex_enter(&proc_lock);
122           PROCLIST_FOREACH(p, &allproc) {
123                     if ((p->p_lflag & PL_SIGCOMPAT) != 0) {
124                               break;
125                     }
126           }
127           mutex_exit(&proc_lock);
128           if (p != NULL) {
129                     syscall_establish(&emul_netbsd32,
130                         netbsd32_kern_sig_16_syscalls);
131                     return EBUSY;
132           }
133 #endif
134 
135 #ifdef __HAVE_MD_NETBSD32_SIGCODE
136           rw_enter(&exec_lock, RW_WRITER);
137           exec_sigcode_free(&emul_netbsd);
138           emul_netbsd32.e_sigcode = NULL;
139           emul_netbsd32.e_esigcode = NULL;
140           emul_netbsd32.e_sigobject = NULL;
141           rw_exit(&exec_lock);
142           netbsd32_machdep_md_16_fini();
143 #endif
144           return 0;
145 }
146 
147 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_16, "compat_netbsd32_20,compat_16");
148 
149 static int
compat_netbsd32_16_modcmd(modcmd_t cmd,void * arg)150 compat_netbsd32_16_modcmd(modcmd_t cmd, void *arg)
151 {
152           switch (cmd) {
153           case MODULE_CMD_INIT:
154                     return compat_netbsd32_16_init();
155 
156           case MODULE_CMD_FINI:
157                     return compat_netbsd32_16_fini();
158 
159           default:
160                     return ENOTTY;
161           }
162 }
163