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