linux_exec.c revision 1.78 1 /* $NetBSD: linux_exec.c,v 1.78 2005/11/04 16:54:11 manu Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1998, 2000 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, Frank van der Linden, Eric Haszlakiewicz and
9 * Thor Lancelot Simon.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.78 2005/11/04 16:54:11 manu Exp $");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/malloc.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/exec.h>
52 #include <sys/exec_elf.h>
53
54 #include <sys/mman.h>
55 #include <sys/sa.h>
56 #include <sys/syscallargs.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <machine/cpu.h>
61 #include <machine/reg.h>
62
63 #include <compat/linux/common/linux_types.h>
64 #include <compat/linux/common/linux_signal.h>
65 #include <compat/linux/common/linux_util.h>
66 #include <compat/linux/common/linux_exec.h>
67 #include <compat/linux/common/linux_machdep.h>
68 #include <compat/linux/common/linux_futex.h>
69
70 #include <compat/linux/linux_syscallargs.h>
71 #include <compat/linux/linux_syscall.h>
72 #include <compat/linux/common/linux_misc.h>
73 #include <compat/linux/common/linux_errno.h>
74 #include <compat/linux/common/linux_emuldata.h>
75
76 extern struct sysent linux_sysent[];
77 extern const char * const linux_syscallnames[];
78 extern char linux_sigcode[], linux_esigcode[];
79
80 static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
81 static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
82 static void linux_e_proc_exit __P((struct proc *));
83 static void linux_e_proc_init __P((struct proc *, struct proc *, int));
84
85 static void linux_userret_settid __P((struct lwp *, void *));
86
87 /*
88 * Execve(2). Just check the alternate emulation path, and pass it on
89 * to the NetBSD execve().
90 */
91 int
92 linux_sys_execve(l, v, retval)
93 struct lwp *l;
94 void *v;
95 register_t *retval;
96 {
97 struct linux_sys_execve_args /* {
98 syscallarg(const char *) path;
99 syscallarg(char **) argv;
100 syscallarg(char **) envp;
101 } */ *uap = v;
102 struct proc *p = l->l_proc;
103 struct sys_execve_args ap;
104 caddr_t sg;
105
106 sg = stackgap_init(p, 0);
107 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
108
109 SCARG(&ap, path) = SCARG(uap, path);
110 SCARG(&ap, argp) = SCARG(uap, argp);
111 SCARG(&ap, envp) = SCARG(uap, envp);
112
113 return sys_execve(l, &ap, retval);
114 }
115
116 /*
117 * Emulation switch.
118 */
119
120 struct uvm_object *emul_linux_object;
121
122 const struct emul emul_linux = {
123 "linux",
124 "/emul/linux",
125 #ifndef __HAVE_MINIMAL_EMUL
126 0,
127 (const int *)native_to_linux_errno,
128 LINUX_SYS_syscall,
129 LINUX_SYS_NSYSENT,
130 #endif
131 linux_sysent,
132 linux_syscallnames,
133 linux_sendsig,
134 linux_trapsignal,
135 NULL,
136 linux_sigcode,
137 linux_esigcode,
138 &emul_linux_object,
139 linux_setregs,
140 linux_e_proc_exec,
141 linux_e_proc_fork,
142 linux_e_proc_exit,
143 NULL,
144 NULL,
145 #ifdef __HAVE_SYSCALL_INTERN
146 linux_syscall_intern,
147 #else
148 #error Implement __HAVE_SYSCALL_INTERN for this platform
149 #endif
150 NULL,
151 NULL,
152
153 uvm_default_mapaddr,
154
155 linux_usertrap,
156 };
157
158 static void
159 linux_e_proc_init(p, parent, forkflags)
160 struct proc *p, *parent;
161 int forkflags;
162 {
163 struct linux_emuldata *e = p->p_emuldata;
164 struct linux_emuldata_shared *s;
165 struct linux_emuldata *ep = NULL;
166
167 if (!e) {
168 /* allocate new Linux emuldata */
169 MALLOC(e, void *, sizeof(struct linux_emuldata),
170 M_EMULDATA, M_WAITOK);
171 } else {
172 e->s->refs--;
173 if (e->s->refs == 0)
174 FREE(e->s, M_EMULDATA);
175 }
176
177 memset(e, '\0', sizeof(struct linux_emuldata));
178
179 if (parent)
180 ep = parent->p_emuldata;
181
182 if (forkflags & FORK_SHAREVM) {
183 #ifdef DIAGNOSTIC
184 if (ep == NULL) {
185 killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
186 return;
187 }
188 #endif
189 s = ep->s;
190 s->refs++;
191 } else {
192 struct vmspace *vm;
193
194 MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
195 M_EMULDATA, M_WAITOK);
196 s->refs = 1;
197
198 /*
199 * Set the process idea of the break to the real value.
200 * For fork, we use parent's vmspace since our's
201 * is not setup at the time of this call and is going
202 * to be copy of parent's anyway. For exec, just
203 * use our own vmspace.
204 */
205 vm = (parent) ? parent->p_vmspace : p->p_vmspace;
206 s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
207
208 /*
209 * Linux threads are emulated as NetBSD processes (not lwp)
210 * We use native PID for Linux TID. The Linux TID is the
211 * PID of the first process in the group. It is stored
212 * here
213 */
214 s->group_pid = p->p_pid;
215 }
216
217 e->s = s;
218
219 /*
220 * initialize TID pointers. ep->child_clear_tid and
221 * ep->child_set_tid will not be used beyond this point.
222 */
223 e->child_clear_tid = NULL;
224 e->child_set_tid = NULL;
225 if (ep != NULL) {
226 e->clear_tid = ep->child_clear_tid;
227 e->set_tid = ep->child_set_tid;
228 ep->child_clear_tid = NULL;
229 ep->child_set_tid = NULL;
230 } else {
231 e->clear_tid = NULL;
232 e->set_tid = NULL;
233 }
234
235 p->p_emuldata = e;
236 }
237
238 /*
239 * Allocate new per-process structures. Called when executing Linux
240 * process. We can reuse the old emuldata - if it's not null,
241 * the executed process is of same emulation as original forked one.
242 */
243 static void
244 linux_e_proc_exec(p, epp)
245 struct proc *p;
246 struct exec_package *epp;
247 {
248 /* exec, use our vmspace */
249 linux_e_proc_init(p, NULL, 0);
250 }
251
252 /*
253 * Emulation per-process exit hook.
254 */
255 static void
256 linux_e_proc_exit(p)
257 struct proc *p;
258 {
259 struct linux_emuldata *e = p->p_emuldata;
260
261 /* Emulate LINUX_CLONE_CHILD_CLEARTID */
262 if (e->clear_tid != NULL) {
263 int error;
264 int null = 0;
265 struct linux_sys_futex_args cup;
266 register_t retval;
267 struct lwp *l;
268
269 if ((error = copyout(&null,
270 e->clear_tid,
271 sizeof(null))) != 0)
272 printf("linux_e_proc_exit: cannot clear TID\n");
273
274 l = proc_representative_lwp(p);
275 SCARG(&cup, uaddr) = e->clear_tid;
276 SCARG(&cup, op) = LINUX_FUTEX_WAKE;
277 SCARG(&cup, val) = 0;
278 SCARG(&cup, timeout) = NULL;
279 SCARG(&cup, uaddr2) = NULL;
280 SCARG(&cup, val3) = 0;
281 if ((error = linux_sys_futex(l, &cup, &retval)) != 0)
282 printf("linux_e_proc_exit: linux_sys_futex failed\n");
283 }
284
285 /* free Linux emuldata and set the pointer to null */
286 e->s->refs--;
287 if (e->s->refs == 0)
288 FREE(e->s, M_EMULDATA);
289 FREE(e, M_EMULDATA);
290 p->p_emuldata = NULL;
291 }
292
293 /*
294 * Emulation fork hook.
295 */
296 static void
297 linux_e_proc_fork(p, parent, forkflags)
298 struct proc *p, *parent;
299 int forkflags;
300 {
301 struct linux_emuldata *e;
302
303 /*
304 * The new process might share some vmspace-related stuff
305 * with parent, depending on fork flags (CLONE_VM et.al).
306 * Force allocation of new base emuldata, and share the
307 * VM-related parts only if necessary.
308 */
309 p->p_emuldata = NULL;
310 linux_e_proc_init(p, parent, forkflags);
311
312 /*
313 * Emulate LINUX_CLONE_CHILD_SETTID: This cannot be done
314 * right now because the child VM is not set up. We will
315 * do it at userret time.
316 */
317 e = p->p_emuldata;
318 if (e->set_tid != NULL)
319 p->p_userret = (*linux_userret_settid);
320
321 return;
322 }
323
324 static void
325 linux_userret_settid(l, arg)
326 struct lwp *l;
327 void *arg;
328 {
329 struct proc *p = l->l_proc;
330 struct linux_emuldata *led = p->p_emuldata;
331 int error;
332
333 p->p_userret = NULL;
334
335 /* Emulate LINUX_CLONE_CHILD_SETTID */
336 if (led->set_tid != NULL) {
337 if ((error = copyout(&p->p_pid,
338 led->set_tid, sizeof(p->p_pid))) != 0)
339 printf("linux_userret_settid: cannot set TID\n");
340 }
341
342 return;
343 }
344