Home | History | Annotate | Line # | Download | only in common
linux_exec.c revision 1.35.8.3
      1  1.35.8.2    bouyer /*	$NetBSD: linux_exec.c,v 1.35.8.3 2000/12/08 09:08:26 bouyer Exp $	*/
      2      1.29  christos 
      3      1.29  christos /*-
      4      1.33      fvdl  * Copyright (c) 1994, 1995, 1998 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.1      fvdl 
     40       1.1      fvdl #include <sys/param.h>
     41       1.1      fvdl #include <sys/systm.h>
     42       1.1      fvdl #include <sys/kernel.h>
     43       1.1      fvdl #include <sys/proc.h>
     44       1.1      fvdl #include <sys/malloc.h>
     45       1.1      fvdl #include <sys/namei.h>
     46       1.1      fvdl #include <sys/vnode.h>
     47      1.13  christos #include <sys/mount.h>
     48      1.25  christos #include <sys/exec.h>
     49       1.8      fvdl #include <sys/exec_elf.h>
     50       1.1      fvdl 
     51       1.1      fvdl #include <sys/mman.h>
     52      1.13  christos #include <sys/syscallargs.h>
     53       1.1      fvdl 
     54       1.1      fvdl #include <machine/cpu.h>
     55       1.1      fvdl #include <machine/reg.h>
     56       1.1      fvdl 
     57      1.32  christos #include <compat/linux/common/linux_types.h>
     58      1.32  christos #include <compat/linux/common/linux_signal.h>
     59      1.32  christos #include <compat/linux/common/linux_util.h>
     60      1.32  christos #include <compat/linux/common/linux_exec.h>
     61      1.32  christos #include <compat/linux/common/linux_machdep.h>
     62      1.32  christos 
     63      1.32  christos #include <compat/linux/linux_syscallargs.h>
     64       1.4  christos #include <compat/linux/linux_syscall.h>
     65  1.35.8.2    bouyer #include <compat/linux/common/linux_misc.h>
     66  1.35.8.2    bouyer #include <compat/linux/common/linux_errno.h>
     67  1.35.8.2    bouyer #include <compat/linux/common/linux_emuldata.h>
     68       1.4  christos 
     69  1.35.8.2    bouyer extern struct sysent linux_sysent[];
     70  1.35.8.2    bouyer extern const char * const linux_syscallnames[];
     71  1.35.8.2    bouyer extern char linux_sigcode[], linux_esigcode[];
     72  1.35.8.2    bouyer 
     73  1.35.8.2    bouyer static void linux_e_proc_exec __P((struct proc *, struct exec_package *));
     74  1.35.8.2    bouyer static void linux_e_proc_fork __P((struct proc *, struct proc *));
     75  1.35.8.2    bouyer static void linux_e_proc_exit __P((struct proc *));
     76  1.35.8.2    bouyer 
     77       1.1      fvdl /*
     78       1.1      fvdl  * Execve(2). Just check the alternate emulation path, and pass it on
     79       1.1      fvdl  * to the NetBSD execve().
     80       1.1      fvdl  */
     81       1.1      fvdl int
     82      1.12   mycroft linux_sys_execve(p, v, retval)
     83       1.1      fvdl 	struct proc *p;
     84      1.11   thorpej 	void *v;
     85      1.11   thorpej 	register_t *retval;
     86      1.11   thorpej {
     87      1.12   mycroft 	struct linux_sys_execve_args /* {
     88      1.35  christos 		syscallarg(const char *) path;
     89       1.1      fvdl 		syscallarg(char **) argv;
     90       1.1      fvdl 		syscallarg(char **) envp;
     91      1.11   thorpej 	} */ *uap = v;
     92      1.16   mycroft 	struct sys_execve_args ap;
     93       1.1      fvdl 	caddr_t sg;
     94       1.1      fvdl 
     95       1.9  christos 	sg = stackgap_init(p->p_emul);
     96  1.35.8.3    bouyer 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
     97       1.1      fvdl 
     98      1.16   mycroft 	SCARG(&ap, path) = SCARG(uap, path);
     99      1.16   mycroft 	SCARG(&ap, argp) = SCARG(uap, argp);
    100      1.16   mycroft 	SCARG(&ap, envp) = SCARG(uap, envp);
    101      1.16   mycroft 
    102      1.16   mycroft 	return sys_execve(p, &ap, retval);
    103  1.35.8.2    bouyer }
    104  1.35.8.2    bouyer 
    105  1.35.8.2    bouyer /*
    106  1.35.8.2    bouyer  * Emulation switch.
    107  1.35.8.2    bouyer  */
    108  1.35.8.2    bouyer const struct emul emul_linux = {
    109  1.35.8.2    bouyer 	"linux",
    110  1.35.8.3    bouyer 	"/emul/linux",
    111  1.35.8.2    bouyer 	native_to_linux_errno,
    112  1.35.8.2    bouyer 	linux_sendsig,
    113  1.35.8.2    bouyer 	LINUX_SYS_syscall,
    114  1.35.8.2    bouyer 	LINUX_SYS_MAXSYSCALL,
    115  1.35.8.2    bouyer 	linux_sysent,
    116  1.35.8.2    bouyer 	linux_syscallnames,
    117  1.35.8.2    bouyer 	linux_sigcode,
    118  1.35.8.2    bouyer 	linux_esigcode,
    119  1.35.8.2    bouyer 	linux_e_proc_exec,
    120  1.35.8.2    bouyer 	linux_e_proc_fork,
    121  1.35.8.2    bouyer 	linux_e_proc_exit,
    122  1.35.8.3    bouyer #ifdef LINUX_MACHDEP_PASS_PPID_AND_EUID
    123  1.35.8.3    bouyer 	EMUL_GETPID_PASS_PPID|EMUL_GETID_PASS_EID,
    124  1.35.8.3    bouyer #else
    125  1.35.8.3    bouyer 	0,
    126  1.35.8.3    bouyer #endif
    127  1.35.8.3    bouyer #ifdef LINUX_MACHDEP_HAS_SEPARATED_SYSCALL
    128  1.35.8.3    bouyer 	linux_syscall,
    129  1.35.8.3    bouyer #else
    130  1.35.8.3    bouyer 	NULL,
    131  1.35.8.3    bouyer #endif
    132  1.35.8.2    bouyer };
    133  1.35.8.2    bouyer 
    134  1.35.8.2    bouyer /*
    135  1.35.8.2    bouyer  * Allocate per-process structures. Called when executing Linux
    136  1.35.8.2    bouyer  * process. We can re-used the old emuldata - if it's not null,
    137  1.35.8.2    bouyer  * the executed process is of same emulation as original forked one.
    138  1.35.8.2    bouyer  */
    139  1.35.8.2    bouyer static void
    140  1.35.8.2    bouyer linux_e_proc_exec(p, epp)
    141  1.35.8.2    bouyer 	struct proc *p;
    142  1.35.8.2    bouyer 	struct exec_package *epp;
    143  1.35.8.2    bouyer {
    144  1.35.8.2    bouyer 	if (!p->p_emuldata) {
    145  1.35.8.2    bouyer 		/* allocate new Linux emuldata */
    146  1.35.8.2    bouyer 		MALLOC(p->p_emuldata, void *, sizeof(struct linux_emuldata),
    147  1.35.8.2    bouyer 			M_EMULDATA, M_WAITOK);
    148  1.35.8.2    bouyer 	}
    149  1.35.8.2    bouyer 
    150  1.35.8.2    bouyer 	memset(p->p_emuldata, '\0', sizeof(struct linux_emuldata));
    151  1.35.8.2    bouyer }
    152  1.35.8.2    bouyer 
    153  1.35.8.2    bouyer /*
    154  1.35.8.2    bouyer  * Emulation per-process exit hook.
    155  1.35.8.2    bouyer  */
    156  1.35.8.2    bouyer static void
    157  1.35.8.2    bouyer linux_e_proc_exit(p)
    158  1.35.8.2    bouyer 	struct proc *p;
    159  1.35.8.2    bouyer {
    160  1.35.8.2    bouyer 	/* free Linux emuldata and set the pointer to null */
    161  1.35.8.2    bouyer 	FREE(p->p_emuldata, M_EMULDATA);
    162  1.35.8.2    bouyer 	p->p_emuldata = NULL;
    163  1.35.8.2    bouyer }
    164  1.35.8.2    bouyer 
    165  1.35.8.2    bouyer /*
    166  1.35.8.2    bouyer  * Emulation fork hook.
    167  1.35.8.2    bouyer  */
    168  1.35.8.2    bouyer static void
    169  1.35.8.2    bouyer linux_e_proc_fork(p, parent)
    170  1.35.8.2    bouyer 	struct proc *p, *parent;
    171  1.35.8.2    bouyer {
    172  1.35.8.2    bouyer 	/*
    173  1.35.8.2    bouyer 	 * It could be desirable to copy some stuff from parent's
    174  1.35.8.2    bouyer 	 * emuldata. We don't need anything like that for now.
    175  1.35.8.2    bouyer 	 * So just allocate new emuldata for the new process.
    176  1.35.8.2    bouyer 	 */
    177  1.35.8.2    bouyer 	p->p_emuldata = NULL;
    178  1.35.8.2    bouyer 	linux_e_proc_exec(p, NULL);
    179       1.1      fvdl }
    180