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