linux_exec.c revision 1.95.8.2 1 /* $NetBSD: linux_exec.c,v 1.95.8.2 2008/01/09 01:51:08 matt Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1998, 2000, 2007 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.95.8.2 2008/01/09 01:51:08 matt 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/syscallargs.h>
56
57 #include <sys/ptrace.h> /* For proc_reparent() */
58
59 #include <uvm/uvm_extern.h>
60
61 #include <sys/cpu.h>
62 #include <machine/reg.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_util.h>
67 #include <compat/linux/common/linux_sched.h>
68 #include <compat/linux/common/linux_machdep.h>
69 #include <compat/linux/common/linux_exec.h>
70 #include <compat/linux/common/linux_futex.h>
71 #include <compat/linux/common/linux_ipc.h>
72 #include <compat/linux/common/linux_sem.h>
73
74 #include <compat/linux/linux_syscallargs.h>
75 #include <compat/linux/linux_syscall.h>
76 #include <compat/linux/common/linux_misc.h>
77 #include <compat/linux/common/linux_errno.h>
78 #include <compat/linux/common/linux_emuldata.h>
79
80 extern struct sysent linux_sysent[];
81 extern const char * const linux_syscallnames[];
82 extern char linux_sigcode[], linux_esigcode[];
83
84 static void linux_e_proc_exec(struct proc *, struct exec_package *);
85 static void linux_e_proc_fork(struct proc *, struct proc *, int);
86 static void linux_e_proc_exit(struct proc *);
87 static void linux_e_proc_init(struct proc *, struct proc *, int);
88
89 #ifdef LINUX_NPTL
90 void linux_userret(void);
91 #endif
92
93 /*
94 * Emulation switch.
95 */
96
97 struct uvm_object *emul_linux_object;
98
99 const struct emul emul_linux = {
100 "linux",
101 "/emul/linux",
102 #ifndef __HAVE_MINIMAL_EMUL
103 0,
104 (const int *)native_to_linux_errno,
105 LINUX_SYS_syscall,
106 LINUX_SYS_NSYSENT,
107 #endif
108 linux_sysent,
109 linux_syscallnames,
110 linux_sendsig,
111 linux_trapsignal,
112 NULL,
113 linux_sigcode,
114 linux_esigcode,
115 &emul_linux_object,
116 linux_setregs,
117 linux_e_proc_exec,
118 linux_e_proc_fork,
119 linux_e_proc_exit,
120 NULL,
121 NULL,
122 #ifdef __HAVE_SYSCALL_INTERN
123 linux_syscall_intern,
124 #else
125 #error Implement __HAVE_SYSCALL_INTERN for this platform
126 #endif
127 NULL,
128 NULL,
129
130 uvm_default_mapaddr,
131
132 linux_usertrap,
133 0,
134 NULL, /* e_startlwp */
135 };
136
137 static void
138 linux_e_proc_init(p, parent, forkflags)
139 struct proc *p, *parent;
140 int forkflags;
141 {
142 struct linux_emuldata *e = p->p_emuldata;
143 struct linux_emuldata_shared *s;
144 struct linux_emuldata *ep = NULL;
145
146 if (!e) {
147 /* allocate new Linux emuldata */
148 MALLOC(e, void *, sizeof(struct linux_emuldata),
149 M_EMULDATA, M_WAITOK);
150 } else {
151 e->s->refs--;
152 if (e->s->refs == 0)
153 FREE(e->s, M_EMULDATA);
154 }
155
156 memset(e, '\0', sizeof(struct linux_emuldata));
157
158 e->proc = p;
159
160 if (parent)
161 ep = parent->p_emuldata;
162
163 if (forkflags & FORK_SHAREVM) {
164 #ifdef DIAGNOSTIC
165 if (ep == NULL) {
166 killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
167 FREE(e, M_EMULDATA);
168 return;
169 }
170 #endif
171 s = ep->s;
172 s->refs++;
173 } else {
174 struct vmspace *vm;
175
176 MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
177 M_EMULDATA, M_WAITOK);
178 s->refs = 1;
179
180 /*
181 * Set the process idea of the break to the real value.
182 * For fork, we use parent's vmspace since our's
183 * is not setup at the time of this call and is going
184 * to be copy of parent's anyway. For exec, just
185 * use our own vmspace.
186 */
187 vm = (parent) ? parent->p_vmspace : p->p_vmspace;
188 s->p_break = (char *)vm->vm_daddr + ctob(vm->vm_dsize);
189
190 /*
191 * Linux threads are emulated as NetBSD processes (not lwp)
192 * We use native PID for Linux TID. The Linux TID is the
193 * PID of the first process in the group. It is stored
194 * here
195 */
196 s->group_pid = p->p_pid;
197
198 /*
199 * Initialize the list of threads in the group
200 */
201 LIST_INIT(&s->threads);
202
203 s->xstat = 0;
204 s->flags = 0;
205 }
206
207 e->s = s;
208
209 /*
210 * Add this thread in the group thread list
211 */
212 LIST_INSERT_HEAD(&s->threads, e, threads);
213
214 #ifdef LINUX_NPTL
215 if (ep != NULL) {
216 e->parent_tidptr = ep->parent_tidptr;
217 e->child_tidptr = ep->child_tidptr;
218 e->clone_flags = ep->clone_flags;
219 }
220 #endif /* LINUX_NPTL */
221
222 p->p_emuldata = e;
223 }
224
225 /*
226 * Allocate new per-process structures. Called when executing Linux
227 * process. We can reuse the old emuldata - if it's not null,
228 * the executed process is of same emulation as original forked one.
229 */
230 static void
231 linux_e_proc_exec(struct proc *p, struct exec_package *epp)
232 {
233 /* exec, use our vmspace */
234 linux_e_proc_init(p, NULL, 0);
235 }
236
237 /*
238 * Emulation per-process exit hook.
239 */
240 static void
241 linux_e_proc_exit(struct proc *p)
242 {
243 struct linux_emuldata *e = p->p_emuldata;
244
245 #ifdef LINUX_NPTL
246 linux_nptl_proc_exit(p);
247 #endif
248 /* Remove the thread for the group thread list */
249 LIST_REMOVE(e, threads);
250
251 /* free Linux emuldata and set the pointer to null */
252 e->s->refs--;
253 if (e->s->refs == 0)
254 FREE(e->s, M_EMULDATA);
255 FREE(e, M_EMULDATA);
256 p->p_emuldata = NULL;
257 }
258
259 /*
260 * Emulation fork hook.
261 */
262 static void
263 linux_e_proc_fork(p, parent, forkflags)
264 struct proc *p, *parent;
265 int forkflags;
266 {
267 /*
268 * The new process might share some vmspace-related stuff
269 * with parent, depending on fork flags (CLONE_VM et.al).
270 * Force allocation of new base emuldata, and share the
271 * VM-related parts only if necessary.
272 */
273 p->p_emuldata = NULL;
274 linux_e_proc_init(p, parent, forkflags);
275
276 #ifdef LINUX_NPTL
277 linux_nptl_proc_fork(p, parent, linux_userret);
278 #endif
279
280 return;
281 }
282
283 #ifdef LINUX_NPTL
284 void
285 linux_userret(void)
286 {
287 struct lwp *l = curlwp;
288 struct proc *p = l->l_proc;
289 struct linux_emuldata *led = p->p_emuldata;
290 int error;
291
292 /* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory */
293 if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) {
294 if ((error = copyout(&l->l_proc->p_pid,
295 led->child_tidptr, sizeof(l->l_proc->p_pid))) != 0)
296 printf("linux_userret: LINUX_CLONE_CHILD_SETTID "
297 "failed (led->child_tidptr = %p, p->p_pid = %d)\n",
298 led->child_tidptr, p->p_pid);
299 }
300
301 /* LINUX_CLONE_SETTLS: allocate a new TLS */
302 if (led->clone_flags & LINUX_CLONE_SETTLS) {
303 if (linux_set_newtls(l, linux_get_newtls(l)) != 0)
304 printf("linux_userret: linux_set_tls failed");
305 }
306
307 return;
308 }
309
310 void
311 linux_nptl_proc_exit(struct proc *p)
312 {
313 struct linux_emuldata *e = p->p_emuldata;
314
315 mutex_enter(&proclist_lock);
316
317 /*
318 * Check if we are a thread group leader victim of another
319 * thread doing exit_group(). If we are, change the exit code.
320 */
321 if ((e->s->group_pid == p->p_pid) &&
322 (e->s->flags & LINUX_LES_INEXITGROUP)) {
323 p->p_xstat = e->s->xstat;
324 }
325
326 /*
327 * Members of the thread groups others than the leader should
328 * exit quietely: no zombie stage, no signal. We do that by
329 * reparenting to init. init will collect us and nobody will
330 * notice what happened.
331 */
332 #ifdef DEBUG_LINUX
333 printf("%s:%d e->s->group_pid = %d, p->p_pid = %d, flags = 0x%x\n",
334 __func__, __LINE__, e->s->group_pid, p->p_pid, e->s->flags);
335 #endif
336 if (e->s->group_pid != p->p_pid) {
337 proc_reparent(p, initproc);
338 cv_broadcast(&initproc->p_waitcv);
339 }
340
341 mutex_exit(&proclist_lock);
342
343 /* Emulate LINUX_CLONE_CHILD_CLEARTID */
344 if (e->clear_tid != NULL) {
345 int error;
346 int null = 0;
347 struct linux_sys_futex_args cup;
348 register_t retval;
349
350 error = copyout(&null, e->clear_tid, sizeof(null));
351 #ifdef DEBUG_LINUX
352 if (error != 0)
353 printf("%s: cannot clear TID\n", __func__);
354 #endif
355
356 SCARG(&cup, uaddr) = e->clear_tid;
357 SCARG(&cup, op) = LINUX_FUTEX_WAKE;
358 SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */
359 SCARG(&cup, timeout) = NULL;
360 SCARG(&cup, uaddr2) = NULL;
361 SCARG(&cup, val3) = 0;
362 if ((error = linux_sys_futex(curlwp, &cup, &retval)) != 0)
363 printf("%s: linux_sys_futex failed\n", __func__);
364 }
365
366 return;
367 }
368
369 void
370 linux_nptl_proc_fork(p, parent, luserret)
371 struct proc *p;
372 struct proc *parent;
373 void (*luserret)(void);
374 {
375 #ifdef LINUX_NPTL
376 struct linux_emuldata *e;
377 #endif
378
379 e = p->p_emuldata;
380
381 /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
382 if (e->clone_flags & LINUX_CLONE_CHILD_CLEARTID)
383 e->clear_tid = e->child_tidptr;
384
385 /* LINUX_CLONE_PARENT_SETTID: set child's TID in parent's memory */
386 if (e->clone_flags & LINUX_CLONE_PARENT_SETTID) {
387 if (copyout_proc(parent, &p->p_pid,
388 e->parent_tidptr, sizeof(p->p_pid)) != 0)
389 printf("%s: LINUX_CLONE_PARENT_SETTID "
390 "failed (e->parent_tidptr = %p, "
391 "parent->p_pid = %d, p->p_pid = %d)\n",
392 __func__, e->parent_tidptr,
393 parent->p_pid, p->p_pid);
394 }
395
396 /*
397 * CLONE_CHILD_SETTID and LINUX_CLONE_SETTLS require child's VM
398 * setup to be completed, we postpone them until userret time.
399 */
400 if (e->clone_flags &
401 (LINUX_CLONE_CHILD_CLEARTID | LINUX_CLONE_SETTLS))
402 p->p_userret = luserret;
403
404 return;
405 }
406
407 void
408 linux_nptl_proc_init(struct proc *p, struct proc *parent)
409 {
410 struct linux_emuldata *e = p->p_emuldata;
411 struct linux_emuldata *ep;
412
413 if ((parent != NULL) && (parent->p_emuldata != NULL)) {
414 ep = parent->p_emuldata;
415
416 e->parent_tidptr = ep->parent_tidptr;
417 e->child_tidptr = ep->child_tidptr;
418 e->clone_flags = ep->clone_flags;
419 }
420
421 return;
422 }
423
424
425 #endif /* LINUX_NPTL */
426