netbsd32_mod.c revision 1.13.18.2 1 /* $NetBSD: netbsd32_mod.c,v 1.13.18.2 2020/04/08 14:08:01 martin 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.13.18.2 2020/04/08 14:08:01 martin 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_sysctl.h>
80 #include <compat/netbsd32/netbsd32_kern_proc.h>
81 #include <compat/netbsd32/netbsd32_exec.h>
82
83 #define ELF32_AUXSIZE (howmany(ELF_AUX_ENTRIES * sizeof(Aux32Info), \
84 sizeof(Elf32_Addr)) + MAXPATHLEN + ALIGN(1))
85
86 struct compat32_80_modctl_hook_t compat32_80_modctl_hook;
87
88 # define DEPS1 "ksem,compat_util"
89
90 #if defined(EXEC_ELF32)
91 # define DEPS2 ",exec_elf32"
92 #else
93 # define DEPS2 ""
94 #endif
95
96 MODULE(MODULE_CLASS_EXEC, compat_netbsd32, DEPS1 DEPS2);
97
98 static struct execsw netbsd32_execsw[] = {
99 #ifdef EXEC_AOUT
100 {
101 .es_hdrsz = sizeof(struct netbsd32_exec),
102 .es_makecmds = exec_netbsd32_makecmds,
103 .u = {
104 .elf_probe_func = NULL,
105 },
106 .es_emul = &emul_netbsd32,
107 .es_prio = EXECSW_PRIO_FIRST,
108 .es_arglen = 0,
109 .es_copyargs = netbsd32_copyargs,
110 .es_setregs = NULL,
111 .es_coredump = coredump_netbsd32,
112 .es_setup_stack = exec_setup_stack,
113 },
114 #endif
115 #ifdef EXEC_ELF32
116 {
117 .es_hdrsz = sizeof (Elf32_Ehdr),
118 .es_makecmds = exec_elf32_makecmds,
119 .u = {
120 .elf_probe_func = netbsd32_elf32_probe,
121 },
122 .es_emul = &emul_netbsd32,
123 .es_prio = EXECSW_PRIO_ANY,
124 .es_arglen = ELF32_AUXSIZE,
125 .es_copyargs = netbsd32_elf32_copyargs,
126 .es_setregs = NULL,
127 .es_coredump = coredump_elf32,
128 .es_setup_stack = exec_setup_stack,
129 },
130 #endif
131 };
132
133 #if defined(__amd64__)
134
135 /* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */
136
137 static int
138 amd64_oosyscall_handle(struct proc *p, struct trapframe *frame)
139 {
140
141 static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
142 const size_t sz = sizeof(lcall);
143 char tmp[sizeof(lcall) /* Avoids VLA */];
144
145 /* Check for the oosyscall lcall instruction. */
146 if (p->p_emul == &emul_netbsd32 &&
147 frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz &&
148 copyin((void *)frame->tf_rip, tmp, sz) == 0 &&
149 memcmp(tmp, lcall, sz) == 0) {
150
151 /* Advance past the lcall and save instruction size. */
152 frame->tf_rip += sz;
153 frame->tf_err = sz;
154 return 0;
155 } else
156 return EPASSTHROUGH;
157 }
158 #endif /* defined(__amd64__) */
159
160 static int
161 compat_netbsd32_modcmd(modcmd_t cmd, void *arg)
162 {
163 int error;
164
165 switch (cmd) {
166 case MODULE_CMD_INIT:
167 error = exec_add(netbsd32_execsw,
168 __arraycount(netbsd32_execsw));
169 if (error == 0) {
170 netbsd32_machdep_md_init();
171 netbsd32_kern_proc_32_init();
172 #if defined(__amd64__)
173 MODULE_HOOK_SET(amd64_oosyscall_hook,
174 amd64_oosyscall_handle);
175 #endif /* defined(__amd64__) */
176 }
177 return error;
178
179 case MODULE_CMD_FINI:
180 #if defined(__amd64__)
181 MODULE_HOOK_UNSET(amd64_oosyscall_hook);
182 #endif /* defined(__amd64__) */
183 netbsd32_machdep_md_fini();
184 netbsd32_sysctl_fini();
185 netbsd32_kern_proc_32_fini();
186
187 error = exec_remove(netbsd32_execsw,
188 __arraycount(netbsd32_execsw));
189 if (error) {
190 netbsd32_kern_proc_32_init();
191 netbsd32_machdep_md_init();
192 #if defined(__amd64__)
193 MODULE_HOOK_SET(amd64_oosyscall_hook,
194 amd64_oosyscall_handle);
195 #endif /* defined(__amd64__) */
196 }
197 return error;
198
199 default:
200 return ENOTTY;
201 }
202 }
203