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