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