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