Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.70.2.1
      1  1.70.2.1      tron /*	$NetBSD: linux_exec.c,v 1.70.2.1 2004/08/15 13:59:18 tron 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.70.2.1      tron __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.70.2.1 2004/08/15 13:59:18 tron 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.1      fvdl #include <machine/cpu.h>
     59       1.1      fvdl #include <machine/reg.h>
     60       1.1      fvdl 
     61      1.32  christos #include <compat/linux/common/linux_types.h>
     62      1.32  christos #include <compat/linux/common/linux_signal.h>
     63      1.32  christos #include <compat/linux/common/linux_util.h>
     64      1.32  christos #include <compat/linux/common/linux_exec.h>
     65      1.32  christos #include <compat/linux/common/linux_machdep.h>
     66      1.32  christos 
     67      1.32  christos #include <compat/linux/linux_syscallargs.h>
     68       1.4  christos #include <compat/linux/linux_syscall.h>
     69      1.38  jdolecek #include <compat/linux/common/linux_misc.h>
     70      1.38  jdolecek #include <compat/linux/common/linux_errno.h>
     71      1.39  jdolecek #include <compat/linux/common/linux_emuldata.h>
     72       1.8      fvdl 
     73      1.38  jdolecek extern struct sysent linux_sysent[];
     74      1.38  jdolecek extern const char * const linux_syscallnames[];
     75      1.38  jdolecek extern char linux_sigcode[], linux_esigcode[];
     76       1.1      fvdl 
     77      1.39  jdolecek static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
     78  1.70.2.1      tron static void linux_e_proc_fork __P((struct proc *, struct proc *, int));
     79      1.39  jdolecek static void linux_e_proc_exit __P((struct proc *));
     80  1.70.2.1      tron static void linux_e_proc_init __P((struct proc *, struct proc *, int));
     81      1.39  jdolecek 
     82       1.1      fvdl /*
     83       1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
     84       1.1      fvdl  * to the NetBSD execve().
     85       1.1      fvdl  */
     86       1.1      fvdl int
     87      1.61   thorpej linux_sys_execve(l, v, retval)
     88      1.61   thorpej 	struct lwp *l;
     89      1.11   thorpej 	void *v;
     90      1.11   thorpej 	register_t *retval;
     91      1.11   thorpej {
     92      1.12   mycroft 	struct linux_sys_execve_args /* {
     93      1.35  christos 		syscallarg(const char *) path;
     94       1.1      fvdl 		syscallarg(char **) argv;
     95       1.1      fvdl 		syscallarg(char **) envp;
     96      1.11   thorpej 	} */ *uap = v;
     97      1.61   thorpej 	struct proc *p = l->l_proc;
     98      1.16   mycroft 	struct sys_execve_args ap;
     99       1.1      fvdl 	caddr_t sg;
    100       1.1      fvdl 
    101      1.57  christos 	sg = stackgap_init(p, 0);
    102      1.64      fvdl 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    103       1.1      fvdl 
    104      1.16   mycroft 	SCARG(&ap, path) = SCARG(uap, path);
    105      1.16   mycroft 	SCARG(&ap, argp) = SCARG(uap, argp);
    106      1.16   mycroft 	SCARG(&ap, envp) = SCARG(uap, envp);
    107      1.16   mycroft 
    108      1.61   thorpej 	return sys_execve(l, &ap, retval);
    109       1.1      fvdl }
    110      1.38  jdolecek 
    111      1.38  jdolecek /*
    112      1.38  jdolecek  * Emulation switch.
    113      1.38  jdolecek  */
    114      1.65       chs 
    115      1.65       chs struct uvm_object *emul_linux_object;
    116      1.65       chs 
    117      1.38  jdolecek const struct emul emul_linux = {
    118      1.38  jdolecek 	"linux",
    119      1.40  jdolecek 	"/emul/linux",
    120      1.44   mycroft #ifndef __HAVE_MINIMAL_EMUL
    121      1.51      manu 	0,
    122      1.45      manu 	(int*)native_to_linux_errno,
    123      1.38  jdolecek 	LINUX_SYS_syscall,
    124      1.60  jdolecek 	LINUX_SYS_NSYSENT,
    125      1.44   mycroft #endif
    126      1.38  jdolecek 	linux_sysent,
    127      1.38  jdolecek 	linux_syscallnames,
    128      1.44   mycroft 	linux_sendsig,
    129      1.52  christos 	linux_trapsignal,
    130      1.70      fvdl 	NULL,
    131      1.38  jdolecek 	linux_sigcode,
    132      1.38  jdolecek 	linux_esigcode,
    133      1.65       chs 	&emul_linux_object,
    134      1.53  jdolecek 	linux_setregs,
    135      1.39  jdolecek 	linux_e_proc_exec,
    136      1.39  jdolecek 	linux_e_proc_fork,
    137      1.39  jdolecek 	linux_e_proc_exit,
    138      1.69      manu 	NULL,
    139      1.69      manu 	NULL,
    140      1.44   mycroft #ifdef __HAVE_SYSCALL_INTERN
    141      1.44   mycroft 	linux_syscall_intern,
    142      1.42  jdolecek #else
    143      1.62   thorpej #error Implement __HAVE_SYSCALL_INTERN for this platform
    144      1.41  jdolecek #endif
    145      1.67    atatat 	NULL,
    146      1.59      manu 	NULL,
    147      1.38  jdolecek };
    148      1.39  jdolecek 
    149      1.39  jdolecek static void
    150  1.70.2.1      tron linux_e_proc_init(p, parent, forkflags)
    151  1.70.2.1      tron 	struct proc *p, *parent;
    152  1.70.2.1      tron 	int forkflags;
    153      1.39  jdolecek {
    154  1.70.2.1      tron 	struct linux_emuldata *e = p->p_emuldata;
    155  1.70.2.1      tron 	struct linux_emuldata_shared *s;
    156  1.70.2.1      tron 
    157  1.70.2.1      tron 	if (!e) {
    158      1.39  jdolecek 		/* allocate new Linux emuldata */
    159  1.70.2.1      tron 		MALLOC(e, void *, sizeof(struct linux_emuldata),
    160  1.70.2.1      tron 			M_EMULDATA, M_WAITOK);
    161  1.70.2.1      tron 	} else  {
    162  1.70.2.1      tron 		e->s->refs++;
    163  1.70.2.1      tron 		if (e->s->refs == 0)
    164  1.70.2.1      tron 			FREE(e->s, M_EMULDATA);
    165  1.70.2.1      tron 	}
    166  1.70.2.1      tron 
    167  1.70.2.1      tron 	memset(e, '\0', sizeof(struct linux_emuldata));
    168  1.70.2.1      tron 
    169  1.70.2.1      tron 	if (forkflags & CLONE_VM) {
    170  1.70.2.1      tron 		struct linux_emuldata *e2 = parent->p_emuldata;
    171  1.70.2.1      tron 		s = e2->s;
    172  1.70.2.1      tron 		s->refs++;
    173  1.70.2.1      tron 	} else {
    174  1.70.2.1      tron 		struct vmspace *vm;
    175  1.70.2.1      tron 
    176  1.70.2.1      tron 		MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
    177      1.39  jdolecek 			M_EMULDATA, M_WAITOK);
    178  1.70.2.1      tron 		s->refs = 1;
    179  1.70.2.1      tron 
    180  1.70.2.1      tron 		/*
    181  1.70.2.1      tron 		 * Set the process idea of the break to the real value.
    182  1.70.2.1      tron 		 * For fork, we use parent's vmspace since our's
    183  1.70.2.1      tron 		 * is not setup at the time of this call and is going
    184  1.70.2.1      tron 		 * to be copy of parent's anyway. For exec, just
    185  1.70.2.1      tron 		 * use our own vmspace.
    186  1.70.2.1      tron 		 */
    187  1.70.2.1      tron 		vm = (parent) ? parent->p_vmspace : p->p_vmspace;
    188  1.70.2.1      tron 		s->p_break = vm->vm_daddr + ctob(vm->vm_dsize);
    189  1.70.2.1      tron 
    190      1.39  jdolecek 	}
    191      1.39  jdolecek 
    192  1.70.2.1      tron 	e->s = s;
    193  1.70.2.1      tron 	p->p_emuldata = e;
    194      1.48  jdolecek }
    195      1.48  jdolecek 
    196      1.48  jdolecek /*
    197  1.70.2.1      tron  * Allocate new per-process structures. Called when executing Linux
    198      1.48  jdolecek  * process. We can reuse the old emuldata - if it's not null,
    199      1.48  jdolecek  * the executed process is of same emulation as original forked one.
    200      1.48  jdolecek  */
    201      1.48  jdolecek static void
    202      1.48  jdolecek linux_e_proc_exec(p, epp)
    203      1.48  jdolecek 	struct proc *p;
    204      1.48  jdolecek 	struct exec_package *epp;
    205      1.48  jdolecek {
    206      1.48  jdolecek 	/* exec, use our vmspace */
    207  1.70.2.1      tron 	linux_e_proc_init(p, NULL, 0);
    208      1.39  jdolecek }
    209      1.39  jdolecek 
    210      1.39  jdolecek /*
    211      1.39  jdolecek  * Emulation per-process exit hook.
    212      1.39  jdolecek  */
    213      1.39  jdolecek static void
    214      1.39  jdolecek linux_e_proc_exit(p)
    215      1.39  jdolecek 	struct proc *p;
    216      1.39  jdolecek {
    217  1.70.2.1      tron 	struct linux_emuldata *e = p->p_emuldata;
    218  1.70.2.1      tron 
    219      1.39  jdolecek 	/* free Linux emuldata and set the pointer to null */
    220  1.70.2.1      tron 	e->s->refs--;
    221  1.70.2.1      tron 	if (e->s->refs == 0)
    222  1.70.2.1      tron 		FREE(e->s, M_EMULDATA);
    223  1.70.2.1      tron 	FREE(e, M_EMULDATA);
    224      1.39  jdolecek 	p->p_emuldata = NULL;
    225      1.39  jdolecek }
    226      1.39  jdolecek 
    227      1.39  jdolecek /*
    228      1.39  jdolecek  * Emulation fork hook.
    229      1.39  jdolecek  */
    230      1.39  jdolecek static void
    231  1.70.2.1      tron linux_e_proc_fork(p, parent, forkflags)
    232      1.39  jdolecek 	struct proc *p, *parent;
    233  1.70.2.1      tron 	int forkflags;
    234      1.39  jdolecek {
    235      1.39  jdolecek 	/*
    236  1.70.2.1      tron 	 * The new process might share some vmspace-related stuff
    237  1.70.2.1      tron 	 * with parent, depending on fork flags (CLONE_VM et.al).
    238  1.70.2.1      tron 	 * Force allocation of new base emuldata, and share the
    239  1.70.2.1      tron 	 * VM-related parts only if necessary.
    240      1.39  jdolecek 	 */
    241      1.39  jdolecek 	p->p_emuldata = NULL;
    242  1.70.2.1      tron 	linux_e_proc_init(p, parent, forkflags);
    243      1.39  jdolecek }
    244