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