Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.45.2.5
      1  1.45.2.5   nathanw /*	$NetBSD: linux_exec.c,v 1.45.2.5 2001/11/14 19:13:09 nathanw 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.45.2.5   nathanw 
     40  1.45.2.5   nathanw #include <sys/cdefs.h>
     41  1.45.2.5   nathanw __KERNEL_RCSID(0, "$NetBSD: linux_exec.c,v 1.45.2.5 2001/11/14 19:13:09 nathanw 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.13  christos #include <sys/syscallargs.h>
     56       1.1      fvdl 
     57       1.1      fvdl #include <machine/cpu.h>
     58       1.1      fvdl #include <machine/reg.h>
     59       1.1      fvdl 
     60      1.32  christos #include <compat/linux/common/linux_types.h>
     61      1.32  christos #include <compat/linux/common/linux_signal.h>
     62      1.32  christos #include <compat/linux/common/linux_util.h>
     63      1.32  christos #include <compat/linux/common/linux_exec.h>
     64      1.32  christos #include <compat/linux/common/linux_machdep.h>
     65      1.32  christos 
     66      1.32  christos #include <compat/linux/linux_syscallargs.h>
     67       1.4  christos #include <compat/linux/linux_syscall.h>
     68      1.38  jdolecek #include <compat/linux/common/linux_misc.h>
     69      1.38  jdolecek #include <compat/linux/common/linux_errno.h>
     70      1.39  jdolecek #include <compat/linux/common/linux_emuldata.h>
     71       1.8      fvdl 
     72      1.38  jdolecek extern struct sysent linux_sysent[];
     73      1.38  jdolecek extern const char * const linux_syscallnames[];
     74      1.38  jdolecek extern char linux_sigcode[], linux_esigcode[];
     75      1.44   mycroft #ifndef __HAVE_SYSCALL_INTERN
     76      1.43  jdolecek void syscall __P((void));
     77      1.44   mycroft #endif
     78       1.1      fvdl 
     79      1.39  jdolecek static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
     80      1.39  jdolecek static void linux_e_proc_fork __P((struct proc *, struct proc *));
     81      1.39  jdolecek static void linux_e_proc_exit __P((struct proc *));
     82  1.45.2.2   nathanw static void linux_e_proc_init __P((struct proc *, struct vmspace *));
     83      1.39  jdolecek 
     84       1.1      fvdl /*
     85       1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
     86       1.1      fvdl  * to the NetBSD execve().
     87       1.1      fvdl  */
     88       1.1      fvdl int
     89  1.45.2.1   nathanw linux_sys_execve(l, v, retval)
     90  1.45.2.1   nathanw 	struct lwp *l;
     91      1.11   thorpej 	void *v;
     92      1.11   thorpej 	register_t *retval;
     93      1.11   thorpej {
     94      1.12   mycroft 	struct linux_sys_execve_args /* {
     95      1.35  christos 		syscallarg(const char *) path;
     96       1.1      fvdl 		syscallarg(char **) argv;
     97       1.1      fvdl 		syscallarg(char **) envp;
     98      1.11   thorpej 	} */ *uap = v;
     99  1.45.2.1   nathanw 	struct proc *p = l->l_proc;
    100      1.16   mycroft 	struct sys_execve_args ap;
    101       1.1      fvdl 	caddr_t sg;
    102       1.1      fvdl 
    103       1.9  christos 	sg = stackgap_init(p->p_emul);
    104      1.40  jdolecek 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    105       1.1      fvdl 
    106      1.16   mycroft 	SCARG(&ap, path) = SCARG(uap, path);
    107      1.16   mycroft 	SCARG(&ap, argp) = SCARG(uap, argp);
    108      1.16   mycroft 	SCARG(&ap, envp) = SCARG(uap, envp);
    109      1.16   mycroft 
    110  1.45.2.1   nathanw 	return sys_execve(l, &ap, retval);
    111       1.1      fvdl }
    112      1.38  jdolecek 
    113      1.38  jdolecek /*
    114      1.38  jdolecek  * Emulation switch.
    115      1.38  jdolecek  */
    116      1.38  jdolecek const struct emul emul_linux = {
    117      1.38  jdolecek 	"linux",
    118      1.40  jdolecek 	"/emul/linux",
    119      1.44   mycroft #ifndef __HAVE_MINIMAL_EMUL
    120      1.44   mycroft 	0,
    121      1.45      manu 	(int*)native_to_linux_errno,
    122      1.38  jdolecek 	LINUX_SYS_syscall,
    123      1.38  jdolecek 	LINUX_SYS_MAXSYSCALL,
    124      1.44   mycroft #endif
    125      1.38  jdolecek 	linux_sysent,
    126      1.38  jdolecek 	linux_syscallnames,
    127      1.44   mycroft 	linux_sendsig,
    128  1.45.2.3   nathanw 	linux_trapsignal,
    129      1.38  jdolecek 	linux_sigcode,
    130      1.38  jdolecek 	linux_esigcode,
    131  1.45.2.4   nathanw 	linux_setregs,
    132      1.39  jdolecek 	linux_e_proc_exec,
    133      1.39  jdolecek 	linux_e_proc_fork,
    134      1.39  jdolecek 	linux_e_proc_exit,
    135      1.44   mycroft #ifdef __HAVE_SYSCALL_INTERN
    136      1.44   mycroft 	linux_syscall_intern,
    137      1.42  jdolecek #else
    138      1.43  jdolecek 	syscall,
    139      1.41  jdolecek #endif
    140      1.38  jdolecek };
    141      1.39  jdolecek 
    142      1.39  jdolecek static void
    143  1.45.2.2   nathanw linux_e_proc_init(p, vmspace)
    144      1.39  jdolecek 	struct proc *p;
    145  1.45.2.2   nathanw 	struct vmspace *vmspace;
    146      1.39  jdolecek {
    147      1.39  jdolecek 	if (!p->p_emuldata) {
    148      1.39  jdolecek 		/* allocate new Linux emuldata */
    149      1.39  jdolecek 		MALLOC(p->p_emuldata, void *, sizeof(struct linux_emuldata),
    150      1.39  jdolecek 			M_EMULDATA, M_WAITOK);
    151      1.39  jdolecek 	}
    152      1.39  jdolecek 
    153      1.39  jdolecek 	memset(p->p_emuldata, '\0', sizeof(struct linux_emuldata));
    154  1.45.2.2   nathanw 
    155  1.45.2.2   nathanw 	/* Set the process idea of the break to the real value */
    156  1.45.2.2   nathanw 	((struct linux_emuldata*)(p->p_emuldata))->p_break =
    157  1.45.2.2   nathanw 	    vmspace->vm_daddr + ctob(vmspace->vm_dsize);
    158  1.45.2.2   nathanw }
    159  1.45.2.2   nathanw 
    160  1.45.2.2   nathanw /*
    161  1.45.2.2   nathanw  * Allocate per-process structures. Called when executing Linux
    162  1.45.2.2   nathanw  * process. We can reuse the old emuldata - if it's not null,
    163  1.45.2.2   nathanw  * the executed process is of same emulation as original forked one.
    164  1.45.2.2   nathanw  */
    165  1.45.2.2   nathanw static void
    166  1.45.2.2   nathanw linux_e_proc_exec(p, epp)
    167  1.45.2.2   nathanw 	struct proc *p;
    168  1.45.2.2   nathanw 	struct exec_package *epp;
    169  1.45.2.2   nathanw {
    170  1.45.2.2   nathanw 	/* exec, use our vmspace */
    171  1.45.2.2   nathanw 	linux_e_proc_init(p, p->p_vmspace);
    172      1.39  jdolecek }
    173      1.39  jdolecek 
    174      1.39  jdolecek /*
    175      1.39  jdolecek  * Emulation per-process exit hook.
    176      1.39  jdolecek  */
    177      1.39  jdolecek static void
    178      1.39  jdolecek linux_e_proc_exit(p)
    179      1.39  jdolecek 	struct proc *p;
    180      1.39  jdolecek {
    181      1.39  jdolecek 	/* free Linux emuldata and set the pointer to null */
    182      1.39  jdolecek 	FREE(p->p_emuldata, M_EMULDATA);
    183      1.39  jdolecek 	p->p_emuldata = NULL;
    184      1.39  jdolecek }
    185      1.39  jdolecek 
    186      1.39  jdolecek /*
    187      1.39  jdolecek  * Emulation fork hook.
    188      1.39  jdolecek  */
    189      1.39  jdolecek static void
    190      1.39  jdolecek linux_e_proc_fork(p, parent)
    191      1.39  jdolecek 	struct proc *p, *parent;
    192      1.39  jdolecek {
    193      1.39  jdolecek 	/*
    194      1.39  jdolecek 	 * It could be desirable to copy some stuff from parent's
    195      1.39  jdolecek 	 * emuldata. We don't need anything like that for now.
    196      1.39  jdolecek 	 * So just allocate new emuldata for the new process.
    197      1.39  jdolecek 	 */
    198      1.39  jdolecek 	p->p_emuldata = NULL;
    199  1.45.2.2   nathanw 
    200  1.45.2.2   nathanw 	/* fork, use parent's vmspace (our vmspace may not be setup yet) */
    201  1.45.2.2   nathanw 	linux_e_proc_init(p, parent->p_vmspace);
    202      1.39  jdolecek }
    203