Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.64.2.7
      1  1.64.2.7     skrll /*	$NetBSD: linux_exec.c,v 1.64.2.7 2005/11/10 14:01:06 skrll Exp $	*/
      2      1.29  christos 
      3      1.29  christos /*-
      4      1.44   mycroft  * Copyright (c) 1994, 1995, 1998, 2000 The NetBSD Foundation, Inc.
      5      1.29  christos  * All rights reserved.
      6      1.29  christos  *
      7      1.29  christos  * This code is derived from software contributed to The NetBSD Foundation
      8      1.33      fvdl  * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
      9      1.33      fvdl  * Thor Lancelot Simon.
     10      1.29  christos  *
     11      1.29  christos  * Redistribution and use in source and binary forms, with or without
     12      1.29  christos  * modification, are permitted provided that the following conditions
     13      1.29  christos  * are met:
     14      1.29  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.29  christos  *    notice, this list of conditions and the following disclaimer.
     16      1.29  christos  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.29  christos  *    notice, this list of conditions and the following disclaimer in the
     18      1.29  christos  *    documentation and/or other materials provided with the distribution.
     19      1.29  christos  * 3. All advertising materials mentioning features or use of this software
     20      1.29  christos  *    must display the following acknowledgement:
     21      1.29  christos  *        This product includes software developed by the NetBSD
     22      1.29  christos  *        Foundation, Inc. and its contributors.
     23      1.29  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.29  christos  *    contributors may be used to endorse or promote products derived
     25      1.29  christos  *    from this software without specific prior written permission.
     26      1.29  christos  *
     27      1.29  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.29  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.29  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.29  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.29  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.29  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.29  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.29  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.29  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.29  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.29  christos  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1      fvdl  */
     39      1.54     lukem 
     40      1.54     lukem #include <sys/cdefs.h>
     41  1.64.2.7     skrll __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.64.2.7 2005/11/10 14:01:06 skrll Exp $");
     42       1.1      fvdl 
     43       1.1      fvdl #include <sys/param.h>
     44       1.1      fvdl #include <sys/systm.h>
     45       1.1      fvdl #include <sys/kernel.h>
     46       1.1      fvdl #include <sys/proc.h>
     47       1.1      fvdl #include <sys/malloc.h>
     48       1.1      fvdl #include <sys/namei.h>
     49       1.1      fvdl #include <sys/vnode.h>
     50      1.13  christos #include <sys/mount.h>
     51      1.25  christos #include <sys/exec.h>
     52       1.8      fvdl #include <sys/exec_elf.h>
     53       1.1      fvdl 
     54       1.1      fvdl #include <sys/mman.h>
     55      1.61   thorpej #include <sys/sa.h>
     56      1.13  christos #include <sys/syscallargs.h>
     57       1.1      fvdl 
     58  1.64.2.6     skrll #include <uvm/uvm_extern.h>
     59  1.64.2.6     skrll 
     60       1.1      fvdl #include <machine/cpu.h>
     61       1.1      fvdl #include <machine/reg.h>
     62       1.1      fvdl 
     63      1.32  christos #include <compat/linux/common/linux_types.h>
     64      1.32  christos #include <compat/linux/common/linux_signal.h>
     65      1.32  christos #include <compat/linux/common/linux_util.h>
     66      1.32  christos #include <compat/linux/common/linux_exec.h>
     67      1.32  christos #include <compat/linux/common/linux_machdep.h>
     68  1.64.2.7     skrll #include <compat/linux/common/linux_futex.h>
     69      1.32  christos 
     70      1.32  christos #include <compat/linux/linux_syscallargs.h>
     71       1.4  christos #include <compat/linux/linux_syscall.h>
     72      1.38  jdolecek #include <compat/linux/common/linux_misc.h>
     73      1.38  jdolecek #include <compat/linux/common/linux_errno.h>
     74      1.39  jdolecek #include <compat/linux/common/linux_emuldata.h>
     75       1.8      fvdl 
     76      1.38  jdolecek extern struct sysent linux_sysent[];
     77      1.38  jdolecek extern const char * const linux_syscallnames[];
     78      1.38  jdolecek extern char linux_sigcode[], linux_esigcode[];
     79       1.1      fvdl 
     80      1.39  jdolecek static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
     81  1.64.2.3     skrll static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
     82      1.39  jdolecek static void linux_e_proc_exit __P((struct proc *));
     83  1.64.2.3     skrll static void linux_e_proc_init __P((struct proc *, struct proc *, int));
     84      1.39  jdolecek 
     85  1.64.2.7     skrll #ifdef LINUX_NPTL
     86  1.64.2.7     skrll static void linux_userret __P((struct lwp *, void *));
     87  1.64.2.7     skrll #endif
     88  1.64.2.7     skrll 
     89       1.1      fvdl /*
     90       1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
     91       1.1      fvdl  * to the NetBSD execve().
     92       1.1      fvdl  */
     93       1.1      fvdl int
     94      1.61   thorpej linux_sys_execve(l, v, retval)
     95      1.61   thorpej 	struct lwp *l;
     96      1.11   thorpej 	void *v;
     97      1.11   thorpej 	register_t *retval;
     98      1.11   thorpej {
     99      1.12   mycroft 	struct linux_sys_execve_args /* {
    100      1.35  christos 		syscallarg(const char *) path;
    101       1.1      fvdl 		syscallarg(char **) argv;
    102       1.1      fvdl 		syscallarg(char **) envp;
    103      1.11   thorpej 	} */ *uap = v;
    104      1.61   thorpej 	struct proc *p = l->l_proc;
    105      1.16   mycroft 	struct sys_execve_args ap;
    106       1.1      fvdl 	caddr_t sg;
    107       1.1      fvdl 
    108      1.57  christos 	sg = stackgap_init(p, 0);
    109  1.64.2.5     skrll 	CHECK_ALT_EXIST(l, &sg, SCARG(uap, path));
    110       1.1      fvdl 
    111      1.16   mycroft 	SCARG(&ap, path) = SCARG(uap, path);
    112      1.16   mycroft 	SCARG(&ap, argp) = SCARG(uap, argp);
    113      1.16   mycroft 	SCARG(&ap, envp) = SCARG(uap, envp);
    114      1.16   mycroft 
    115      1.61   thorpej 	return sys_execve(l, &ap, retval);
    116       1.1      fvdl }
    117      1.38  jdolecek 
    118      1.38  jdolecek /*
    119      1.38  jdolecek  * Emulation switch.
    120      1.38  jdolecek  */
    121  1.64.2.2     skrll 
    122  1.64.2.2     skrll struct uvm_object *emul_linux_object;
    123  1.64.2.2     skrll 
    124      1.38  jdolecek const struct emul emul_linux = {
    125      1.38  jdolecek 	"linux",
    126      1.40  jdolecek 	"/emul/linux",
    127      1.44   mycroft #ifndef __HAVE_MINIMAL_EMUL
    128      1.51      manu 	0,
    129  1.64.2.7     skrll 	(const int *)native_to_linux_errno,
    130      1.38  jdolecek 	LINUX_SYS_syscall,
    131      1.60  jdolecek 	LINUX_SYS_NSYSENT,
    132      1.44   mycroft #endif
    133      1.38  jdolecek 	linux_sysent,
    134      1.38  jdolecek 	linux_syscallnames,
    135      1.44   mycroft 	linux_sendsig,
    136      1.52  christos 	linux_trapsignal,
    137  1.64.2.2     skrll 	NULL,
    138      1.38  jdolecek 	linux_sigcode,
    139      1.38  jdolecek 	linux_esigcode,
    140  1.64.2.2     skrll 	&emul_linux_object,
    141      1.53  jdolecek 	linux_setregs,
    142      1.39  jdolecek 	linux_e_proc_exec,
    143      1.39  jdolecek 	linux_e_proc_fork,
    144      1.39  jdolecek 	linux_e_proc_exit,
    145  1.64.2.2     skrll 	NULL,
    146  1.64.2.2     skrll 	NULL,
    147      1.44   mycroft #ifdef __HAVE_SYSCALL_INTERN
    148      1.44   mycroft 	linux_syscall_intern,
    149      1.42  jdolecek #else
    150      1.62   thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
    151      1.41  jdolecek #endif
    152  1.64.2.2     skrll 	NULL,
    153      1.59      manu 	NULL,
    154  1.64.2.6     skrll 
    155  1.64.2.6     skrll 	uvm_default_mapaddr,
    156  1.64.2.7     skrll 
    157  1.64.2.7     skrll 	linux_usertrap,
    158      1.38  jdolecek };
    159      1.39  jdolecek 
    160      1.39  jdolecek static void
    161  1.64.2.3     skrll linux_e_proc_init(p, parent, forkflags)
    162  1.64.2.3     skrll 	struct proc *p, *parent;
    163  1.64.2.3     skrll 	int forkflags;
    164      1.39  jdolecek {
    165  1.64.2.3     skrll 	struct linux_emuldata *e = p->p_emuldata;
    166  1.64.2.3     skrll 	struct linux_emuldata_shared *s;
    167  1.64.2.7     skrll 	struct linux_emuldata *ep = NULL;
    168  1.64.2.3     skrll 
    169  1.64.2.3     skrll 	if (!e) {
    170      1.39  jdolecek 		/* allocate new Linux emuldata */
    171  1.64.2.3     skrll 		MALLOC(e, void *, sizeof(struct linux_emuldata),
    172  1.64.2.3     skrll 			M_EMULDATA, M_WAITOK);
    173  1.64.2.3     skrll 	} else  {
    174  1.64.2.3     skrll 		e->s->refs--;
    175  1.64.2.3     skrll 		if (e->s->refs == 0)
    176  1.64.2.3     skrll 			FREE(e->s, M_EMULDATA);
    177  1.64.2.3     skrll 	}
    178  1.64.2.3     skrll 
    179  1.64.2.3     skrll 	memset(e, '\0', sizeof(struct linux_emuldata));
    180  1.64.2.3     skrll 
    181  1.64.2.7     skrll 	if (parent)
    182  1.64.2.7     skrll 		ep = parent->p_emuldata;
    183  1.64.2.7     skrll 
    184  1.64.2.3     skrll 	if (forkflags & FORK_SHAREVM) {
    185  1.64.2.7     skrll #ifdef DIAGNOSTIC
    186  1.64.2.7     skrll 		if (ep == NULL) {
    187  1.64.2.7     skrll 			killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
    188  1.64.2.7     skrll 			return;
    189  1.64.2.7     skrll 		}
    190  1.64.2.7     skrll #endif
    191  1.64.2.7     skrll 		s = ep->s;
    192  1.64.2.3     skrll 		s->refs++;
    193  1.64.2.3     skrll 	} else {
    194  1.64.2.3     skrll 		struct vmspace *vm;
    195  1.64.2.3     skrll 
    196  1.64.2.3     skrll 		MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
    197      1.39  jdolecek 			M_EMULDATA, M_WAITOK);
    198  1.64.2.3     skrll 		s->refs = 1;
    199  1.64.2.3     skrll 
    200  1.64.2.3     skrll 		/*
    201  1.64.2.3     skrll 		 * Set the process idea of the break to the real value.
    202  1.64.2.3     skrll 		 * For fork, we use parent's vmspace since our's
    203  1.64.2.3     skrll 		 * is not setup at the time of this call and is going
    204  1.64.2.3     skrll 		 * to be copy of parent's anyway. For exec, just
    205  1.64.2.3     skrll 		 * use our own vmspace.
    206  1.64.2.3     skrll 		 */
    207  1.64.2.3     skrll 		vm = (parent) ? parent->p_vmspace : p->p_vmspace;
    208  1.64.2.3     skrll 		s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
    209  1.64.2.3     skrll 
    210  1.64.2.7     skrll 		/*
    211  1.64.2.7     skrll 		 * Linux threads are emulated as NetBSD processes (not lwp)
    212  1.64.2.7     skrll 		 * We use native PID for Linux TID. The Linux TID is the
    213  1.64.2.7     skrll 		 * PID of the first process in the group. It is stored
    214  1.64.2.7     skrll 		 * here
    215  1.64.2.7     skrll 		 */
    216  1.64.2.7     skrll 		s->group_pid = p->p_pid;
    217      1.39  jdolecek 	}
    218      1.39  jdolecek 
    219  1.64.2.3     skrll 	e->s = s;
    220  1.64.2.7     skrll 
    221  1.64.2.7     skrll #ifdef LINUX_NPTL
    222  1.64.2.7     skrll 	/*
    223  1.64.2.7     skrll 	 * initialize TID pointers. ep->child_clear_tid and
    224  1.64.2.7     skrll 	 * ep->child_set_tid will not be used beyond this point.
    225  1.64.2.7     skrll 	 */
    226  1.64.2.7     skrll 	e->child_clear_tid = NULL;
    227  1.64.2.7     skrll 	e->child_set_tid = NULL;
    228  1.64.2.7     skrll 	if (ep != NULL) {
    229  1.64.2.7     skrll 		e->clear_tid = ep->child_clear_tid;
    230  1.64.2.7     skrll 		e->set_tid = ep->child_set_tid;
    231  1.64.2.7     skrll 		e->set_tls = ep->set_tls;
    232  1.64.2.7     skrll 		ep->child_clear_tid = NULL;
    233  1.64.2.7     skrll 		ep->child_set_tid = NULL;
    234  1.64.2.7     skrll 		ep->set_tls = 0;
    235  1.64.2.7     skrll 	} else {
    236  1.64.2.7     skrll 		e->clear_tid = NULL;
    237  1.64.2.7     skrll 		e->set_tid = NULL;
    238  1.64.2.7     skrll 		e->set_tls = 0;
    239  1.64.2.7     skrll 	}
    240  1.64.2.7     skrll #endif /* LINUX_NPTL */
    241  1.64.2.7     skrll 
    242  1.64.2.3     skrll 	p->p_emuldata = e;
    243      1.48  jdolecek }
    244      1.48  jdolecek 
    245      1.48  jdolecek /*
    246  1.64.2.3     skrll  * Allocate new per-process structures. Called when executing Linux
    247      1.48  jdolecek  * process. We can reuse the old emuldata - if it's not null,
    248      1.48  jdolecek  * the executed process is of same emulation as original forked one.
    249      1.48  jdolecek  */
    250      1.48  jdolecek static void
    251      1.48  jdolecek linux_e_proc_exec(p, epp)
    252      1.48  jdolecek 	struct proc *p;
    253      1.48  jdolecek 	struct exec_package *epp;
    254      1.48  jdolecek {
    255      1.48  jdolecek 	/* exec, use our vmspace */
    256  1.64.2.3     skrll 	linux_e_proc_init(p, NULL, 0);
    257      1.39  jdolecek }
    258      1.39  jdolecek 
    259      1.39  jdolecek /*
    260      1.39  jdolecek  * Emulation per-process exit hook.
    261      1.39  jdolecek  */
    262      1.39  jdolecek static void
    263      1.39  jdolecek linux_e_proc_exit(p)
    264      1.39  jdolecek 	struct proc *p;
    265      1.39  jdolecek {
    266  1.64.2.3     skrll 	struct linux_emuldata *e = p->p_emuldata;
    267  1.64.2.3     skrll 
    268  1.64.2.7     skrll #ifdef LINUX_NPTL
    269  1.64.2.7     skrll 	/* Emulate LINUX_CLONE_CHILD_CLEARTID */
    270  1.64.2.7     skrll 	if (e->clear_tid != NULL) {
    271  1.64.2.7     skrll 		int error;
    272  1.64.2.7     skrll 		int null = 0;
    273  1.64.2.7     skrll 		struct linux_sys_futex_args cup;
    274  1.64.2.7     skrll 		register_t retval;
    275  1.64.2.7     skrll 		struct lwp *l;
    276  1.64.2.7     skrll 
    277  1.64.2.7     skrll 		if ((error = copyout(&null,
    278  1.64.2.7     skrll 		    e->clear_tid,
    279  1.64.2.7     skrll 		    sizeof(null))) != 0)
    280  1.64.2.7     skrll 			printf("linux_e_proc_exit: cannot clear TID\n");
    281  1.64.2.7     skrll 
    282  1.64.2.7     skrll 		l = proc_representative_lwp(p);
    283  1.64.2.7     skrll 		SCARG(&cup, uaddr) = e->clear_tid;
    284  1.64.2.7     skrll 		SCARG(&cup, op) = LINUX_FUTEX_WAKE;
    285  1.64.2.7     skrll 		SCARG(&cup, val) = -1; /* Awake everyone */
    286  1.64.2.7     skrll 		SCARG(&cup, timeout) = NULL;
    287  1.64.2.7     skrll 		SCARG(&cup, uaddr2) = NULL;
    288  1.64.2.7     skrll 		SCARG(&cup, val3) = 0;
    289  1.64.2.7     skrll 		if ((error = linux_sys_futex(l, &cup, &retval)) != 0)
    290  1.64.2.7     skrll 			printf("linux_e_proc_exit: linux_sys_futex failed\n");
    291  1.64.2.7     skrll 	}
    292  1.64.2.7     skrll #endif /* LINUX_NPTL */
    293  1.64.2.7     skrll 
    294      1.39  jdolecek 	/* free Linux emuldata and set the pointer to null */
    295  1.64.2.3     skrll 	e->s->refs--;
    296  1.64.2.3     skrll 	if (e->s->refs == 0)
    297  1.64.2.3     skrll 		FREE(e->s, M_EMULDATA);
    298  1.64.2.3     skrll 	FREE(e, M_EMULDATA);
    299      1.39  jdolecek 	p->p_emuldata = NULL;
    300      1.39  jdolecek }
    301      1.39  jdolecek 
    302      1.39  jdolecek /*
    303      1.39  jdolecek  * Emulation fork hook.
    304      1.39  jdolecek  */
    305      1.39  jdolecek static void
    306  1.64.2.3     skrll linux_e_proc_fork(p, parent, forkflags)
    307      1.39  jdolecek 	struct proc *p, *parent;
    308  1.64.2.3     skrll 	int forkflags;
    309      1.39  jdolecek {
    310  1.64.2.7     skrll #ifdef LINUX_NPTL
    311  1.64.2.7     skrll 	struct linux_emuldata *e;
    312  1.64.2.7     skrll #endif
    313  1.64.2.7     skrll 
    314      1.39  jdolecek 	/*
    315  1.64.2.3     skrll 	 * The new process might share some vmspace-related stuff
    316  1.64.2.3     skrll 	 * with parent, depending on fork flags (CLONE_VM et.al).
    317  1.64.2.3     skrll 	 * Force allocation of new base emuldata, and share the
    318  1.64.2.3     skrll 	 * VM-related parts only if necessary.
    319      1.39  jdolecek 	 */
    320      1.39  jdolecek 	p->p_emuldata = NULL;
    321  1.64.2.3     skrll 	linux_e_proc_init(p, parent, forkflags);
    322  1.64.2.7     skrll 
    323  1.64.2.7     skrll #ifdef LINUX_NPTL
    324  1.64.2.7     skrll 	/*
    325  1.64.2.7     skrll 	 * Emulate LINUX_CLONE_CHILD_SETTID and LINUX_CLONE_TLS:
    326  1.64.2.7     skrll 	 * This cannot be done right now because the child VM
    327  1.64.2.7     skrll 	 * is not set up. We will do it at userret time.
    328  1.64.2.7     skrll 	 */
    329  1.64.2.7     skrll 	e = p->p_emuldata;
    330  1.64.2.7     skrll 	if ((e->set_tid != NULL) || (e->set_tls != 0))
    331  1.64.2.7     skrll 		p->p_userret = (*linux_userret);
    332  1.64.2.7     skrll #endif
    333  1.64.2.7     skrll 
    334  1.64.2.7     skrll 	return;
    335  1.64.2.7     skrll }
    336  1.64.2.7     skrll 
    337  1.64.2.7     skrll #ifdef LINUX_NPTL
    338  1.64.2.7     skrll static void
    339  1.64.2.7     skrll linux_userret(l, arg)
    340  1.64.2.7     skrll 	struct lwp *l;
    341  1.64.2.7     skrll 	void *arg;
    342  1.64.2.7     skrll {
    343  1.64.2.7     skrll 	struct proc *p = l->l_proc;
    344  1.64.2.7     skrll 	struct linux_emuldata *led = p->p_emuldata;
    345  1.64.2.7     skrll 	int error;
    346  1.64.2.7     skrll 
    347  1.64.2.7     skrll 	p->p_userret = NULL;
    348  1.64.2.7     skrll 
    349  1.64.2.7     skrll 	/* Emulate LINUX_CLONE_CHILD_SETTID  */
    350  1.64.2.7     skrll 	if (led->set_tid != NULL) {
    351  1.64.2.7     skrll 		if ((error = copyout(&p->p_pid,
    352  1.64.2.7     skrll 		    led->set_tid, sizeof(p->p_pid))) != 0)
    353  1.64.2.7     skrll 			printf("linux_userret: cannot set TID\n");
    354  1.64.2.7     skrll 	}
    355  1.64.2.7     skrll 
    356  1.64.2.7     skrll 	/* Emulate LINUX_CLONE_NEWTLS */
    357  1.64.2.7     skrll 	if (led->set_tls != 0) {
    358  1.64.2.7     skrll 		if (linux_set_newtls(l, led->set_tls) != 0)
    359  1.64.2.7     skrll 			printf("linux_userret: cannot set TLS\n");
    360  1.64.2.7     skrll 	}
    361  1.64.2.7     skrll 
    362  1.64.2.7     skrll 	return;
    363      1.39  jdolecek }
    364  1.64.2.7     skrll #endif /* LINUX_NPTL */
    365