xref: /netbsd-src/sys/compat/netbsd32/netbsd32_mod.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /*	$NetBSD: netbsd32_mod.c,v 1.21 2020/03/15 14:15:12 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software developed for The NetBSD Foundation
8  * by Andrew Doran.
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 
32 /*
33  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Charles M. Hannum, and by Maxime Villard.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58  * POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.21 2020/03/15 14:15:12 pgoyette Exp $");
63 
64 #ifdef _KERNEL_OPT
65 #include "opt_execfmt.h"
66 #endif
67 
68 #ifndef ELFSIZE
69 #define ELFSIZE ARCH_ELFSIZE
70 #endif
71 
72 #include <sys/param.h>
73 #include <sys/module.h>
74 #include <sys/exec.h>
75 #include <sys/exec_elf.h>
76 #include <sys/module_hook.h>
77 #include <sys/compat_stub.h>
78 
79 #include <compat/netbsd32/netbsd32_kern_proc.h>
80 #include <compat/netbsd32/netbsd32_exec.h>
81 
82 #define ELF32_AUXSIZE (howmany(ELF_AUX_ENTRIES * sizeof(Aux32Info), \
83     sizeof(Elf32_Addr)) + MAXPATHLEN + ALIGN(1))
84 
85 struct compat32_80_modctl_hook_t compat32_80_modctl_hook;
86 
87 # define	DEPS1	"ksem,compat_util"
88 
89 #if defined(EXEC_ELF32)
90 # define	DEPS2	",exec_elf32"
91 #else
92 # define	DEPS2	""
93 #endif
94 
95 MODULE(MODULE_CLASS_EXEC, compat_netbsd32, DEPS1 DEPS2);
96 
97 static struct execsw netbsd32_execsw[] = {
98 #ifdef EXEC_AOUT
99 	{
100 		.es_hdrsz = sizeof(struct netbsd32_exec),
101 		.es_makecmds = exec_netbsd32_makecmds,
102 		.u = {
103 			.elf_probe_func = NULL,
104 		},
105 		.es_emul = &emul_netbsd32,
106 		.es_prio = EXECSW_PRIO_FIRST,
107 		.es_arglen = 0,
108 		.es_copyargs = netbsd32_copyargs,
109 		.es_setregs = NULL,
110 		.es_coredump = coredump_netbsd32,
111 		.es_setup_stack = exec_setup_stack,
112 	},
113 #endif
114 #ifdef EXEC_ELF32
115 	{
116 		.es_hdrsz = sizeof (Elf32_Ehdr),
117 		.es_makecmds = exec_elf32_makecmds,
118 		.u = {
119 			.elf_probe_func = netbsd32_elf32_probe,
120 		},
121 		.es_emul = &emul_netbsd32,
122 		.es_prio = EXECSW_PRIO_ANY,
123 		.es_arglen = ELF32_AUXSIZE,
124 		.es_copyargs = netbsd32_elf32_copyargs,
125 		.es_setregs = NULL,
126 		.es_coredump = coredump_elf32,
127 		.es_setup_stack = exec_setup_stack,
128 	},
129 #endif
130 };
131 
132 #if defined(__amd64__)
133 
134 /* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */
135 
136 static int
137 amd64_oosyscall_handle(struct proc *p, struct trapframe *frame)
138 {
139 
140 	static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
141 	const size_t sz = sizeof(lcall);
142 	char tmp[sizeof(lcall) /* Avoids VLA */];
143 
144 	/* Check for the oosyscall lcall instruction. */
145 	if (p->p_emul == &emul_netbsd32 &&
146 	    frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz &&
147 	    copyin((void *)frame->tf_rip, tmp, sz) == 0 &&
148 	    memcmp(tmp, lcall, sz) == 0) {
149 
150 		/* Advance past the lcall and save instruction size. */
151 		frame->tf_rip += sz;
152 		frame->tf_err = sz;
153 		return 0;
154 	} else
155 		return EPASSTHROUGH;
156 }
157 #endif /* defined(__amd64__) */
158 
159 static int
160 compat_netbsd32_modcmd(modcmd_t cmd, void *arg)
161 {
162 	int error;
163 
164 	switch (cmd) {
165 	case MODULE_CMD_INIT:
166 		error = exec_add(netbsd32_execsw,
167 		    __arraycount(netbsd32_execsw));
168 		if (error == 0) {
169 			netbsd32_machdep_md_init();
170 			netbsd32_kern_proc_32_init();
171 #if defined(__amd64__)
172 			MODULE_HOOK_SET(amd64_oosyscall_hook,
173 			    amd64_oosyscall_handle);
174 #endif /* defined(__amd64__) */
175 		}
176 		return error;
177 
178 	case MODULE_CMD_FINI:
179 #if defined(__amd64__)
180 		MODULE_HOOK_UNSET(amd64_oosyscall_hook);
181 #endif /* defined(__amd64__) */
182 		netbsd32_machdep_md_fini();
183 		netbsd32_kern_proc_32_fini();
184 
185 		error = exec_remove(netbsd32_execsw,
186 		    __arraycount(netbsd32_execsw));
187 		if (error) {
188 			netbsd32_kern_proc_32_init();
189 			netbsd32_machdep_md_init();
190 #if defined(__amd64__)
191 			MODULE_HOOK_SET(amd64_oosyscall_hook,
192 			    amd64_oosyscall_handle);
193 #endif /* defined(__amd64__) */
194 		}
195 		return error;
196 
197 	default:
198 		return ENOTTY;
199 	}
200 }
201