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