xref: /netbsd-src/sys/compat/netbsd32/netbsd32_compat_16.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /*	$NetBSD: netbsd32_compat_16.c,v 1.6 2024/08/18 18:42:57 riastradh 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.6 2024/08/18 18:42:57 riastradh 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 static const struct syscall_package netbsd32_kern_sig_16_syscalls[] = {
52         /* compat_16_netbs32___sigreturn14 is in MD code! */
53         { NETBSD32_SYS_compat_16_netbsd32___sigreturn14, 0,
54             (sy_call_t *)compat_16_netbsd32___sigreturn14 },
55         { 0, 0, NULL }
56 };
57 
58 static int
59 compat_netbsd32_16_init(void)
60 {
61 	int error;
62 
63 	error = syscall_establish(&emul_netbsd32,
64 	    netbsd32_kern_sig_16_syscalls);
65 	if (error)
66 		return error;
67 
68 	rw_enter(&exec_lock, RW_WRITER);
69 	emul_netbsd32.e_sigcode = netbsd32_sigcode;
70 	emul_netbsd32.e_esigcode = netbsd32_esigcode;
71 	emul_netbsd32.e_sigobject = &emul_netbsd32_object;
72 	error = exec_sigcode_alloc(&emul_netbsd);
73 	if (error) {
74 		emul_netbsd32.e_sigcode = NULL;
75 		emul_netbsd32.e_esigcode = NULL;
76 		emul_netbsd32.e_sigobject = NULL;
77 	}
78 	rw_exit(&exec_lock);
79 	if (error)
80 		return error;
81 	netbsd32_machdep_md_16_init();
82 	return 0;
83 }
84 
85 static int
86 compat_netbsd32_16_fini(void)
87 {
88 	proc_t *p;
89 	int error;
90 
91 	error = syscall_disestablish(&emul_netbsd32,
92 	    netbsd32_kern_sig_16_syscalls);
93 	if (error)
94 		return error;
95 	/*
96 	 * Ensure sendsig_sigcontext() is not being used.
97 	 * module_lock prevents the flag being set on any
98 	 * further processes while we are here.  See
99 	 * sigaction1() for the opposing half.
100 	 */
101 	mutex_enter(&proc_lock);
102 	PROCLIST_FOREACH(p, &allproc) {
103 		if ((p->p_lflag & PL_SIGCOMPAT) != 0) {
104 			break;
105 		}
106 	}
107 	mutex_exit(&proc_lock);
108 	if (p != NULL) {
109 		syscall_establish(&emul_netbsd32,
110 		    netbsd32_kern_sig_16_syscalls);
111 		return EBUSY;
112 	}
113 
114 	rw_enter(&exec_lock, RW_WRITER);
115 	exec_sigcode_free(&emul_netbsd);
116 	emul_netbsd32.e_sigcode = NULL;
117 	emul_netbsd32.e_esigcode = NULL;
118 	emul_netbsd32.e_sigobject = NULL;
119 	rw_exit(&exec_lock);
120 	netbsd32_machdep_md_16_fini();
121 	return 0;
122 }
123 
124 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_16, "compat_netbsd32_20,compat_16");
125 
126 static int
127 compat_netbsd32_16_modcmd(modcmd_t cmd, void *arg)
128 {
129 	switch (cmd) {
130 	case MODULE_CMD_INIT:
131 		return compat_netbsd32_16_init();
132 
133 	case MODULE_CMD_FINI:
134 		return compat_netbsd32_16_fini();
135 
136 	default:
137 		return ENOTTY;
138 	}
139 }
140