Home | History | Annotate | Line # | Download | only in common
linux32_exec.c revision 1.13.2.1
      1 /*	$NetBSD: linux32_exec.c,v 1.13.2.1 2008/05/10 23:48:56 wrstuden Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1994-2007 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,
      9  * Thor Lancelot Simon, and Emmanuel Dreyfus.
     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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: linux32_exec.c,v 1.13.2.1 2008/05/10 23:48:56 wrstuden Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/proc.h>
     40 #include <sys/malloc.h>
     41 #include <sys/namei.h>
     42 #include <sys/vnode.h>
     43 #include <sys/mount.h>
     44 #include <sys/exec.h>
     45 #include <sys/exec_elf.h>
     46 
     47 #include <sys/mman.h>
     48 #include <sys/sa.h>
     49 #include <sys/syscallargs.h>
     50 #include <sys/ptrace.h>		/* For proc_reparent() */
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <sys/cpu.h>
     55 #include <machine/reg.h>
     56 
     57 #include <compat/linux/common/linux_types.h>
     58 #include <compat/linux/common/linux_emuldata.h>
     59 
     60 #include <compat/linux32/common/linux32_exec.h>
     61 #include <compat/linux32/common/linux32_types.h>
     62 #include <compat/linux32/common/linux32_signal.h>
     63 #include <compat/linux32/common/linux32_machdep.h>
     64 
     65 #include <compat/linux32/linux32_syscallargs.h>
     66 #include <compat/linux32/linux32_syscall.h>
     67 
     68 extern char linux32_sigcode[1];
     69 extern char linux32_rt_sigcode[1];
     70 extern char linux32_esigcode[1];
     71 
     72 extern struct sysent linux32_sysent[];
     73 extern const char * const linux32_syscallnames[];
     74 
     75 static void linux32_e_proc_exec(struct proc *, struct exec_package *);
     76 static void linux32_e_proc_fork(struct proc *, struct proc *, int);
     77 static void linux32_e_proc_exit(struct proc *);
     78 static void linux32_e_proc_init(struct proc *, struct proc *, int);
     79 
     80 #ifdef LINUX32_NPTL
     81 void linux32_userret(void);
     82 void linux_nptl_proc_fork(struct proc *, struct proc *, void (*luserret)(void));
     83 void linux_nptl_proc_exit(struct proc *);
     84 void linux_nptl_proc_init(struct proc *, struct proc *);
     85 #endif
     86 
     87 /*
     88  * Emulation switch.
     89  */
     90 
     91 struct uvm_object *emul_linux32_object;
     92 
     93 const struct emul emul_linux32 = {
     94 	"linux32",
     95 	"/emul/linux32",
     96 #ifndef __HAVE_MINIMAL_EMUL
     97 	0,
     98 	NULL,
     99 	LINUX32_SYS_syscall,
    100 	LINUX32_SYS_NSYSENT,
    101 #endif
    102 	linux32_sysent,
    103 	linux32_syscallnames,
    104 	linux32_sendsig,
    105 	trapsignal,
    106 	NULL,
    107 	linux32_sigcode,
    108 	linux32_esigcode,
    109 	&emul_linux32_object,
    110 	linux32_setregs,
    111 	linux32_e_proc_exec,
    112 	linux32_e_proc_fork,
    113 	linux32_e_proc_exit,
    114 	NULL,
    115 	NULL,
    116 	linux32_syscall_intern,
    117 	NULL,
    118 	NULL,
    119 	netbsd32_vm_default_addr,
    120 };
    121 
    122 static void
    123 linux32_e_proc_init(p, parent, forkflags)
    124 	struct proc *p, *parent;
    125 	int forkflags;
    126 {
    127 	struct linux_emuldata *e = p->p_emuldata;
    128 	struct linux_emuldata_shared *s;
    129 	struct linux_emuldata *ep = NULL;
    130 
    131 	if (!e) {
    132 		/* allocate new Linux emuldata */
    133 		MALLOC(e, void *, sizeof(struct linux_emuldata),
    134 			M_EMULDATA, M_WAITOK);
    135 	} else  {
    136 		mutex_enter(proc_lock);
    137 		e->s->refs--;
    138 		if (e->s->refs == 0)
    139 			FREE(e->s, M_EMULDATA);
    140 		mutex_exit(proc_lock);
    141 	}
    142 
    143 	memset(e, '\0', sizeof(struct linux_emuldata));
    144 
    145 	e->proc = p;
    146 
    147 	if (parent)
    148 		ep = parent->p_emuldata;
    149 
    150 	if (forkflags & FORK_SHAREVM) {
    151 		mutex_enter(proc_lock);
    152 #ifdef DIAGNOSTIC
    153 		if (ep == NULL) {
    154 			killproc(p, "FORK_SHAREVM while emuldata is NULL\n");
    155 			mutex_exit(proc_lock);
    156 			return;
    157 		}
    158 #endif
    159 		s = ep->s;
    160 		s->refs++;
    161 	} else {
    162 		struct vmspace *vm;
    163 
    164 		MALLOC(s, void *, sizeof(struct linux_emuldata_shared),
    165 			M_EMULDATA, M_WAITOK);
    166 		s->refs = 1;
    167 
    168 		/*
    169 		 * Set the process idea of the break to the real value.
    170 		 * For fork, we use parent's vmspace since our's
    171 		 * is not setup at the time of this call and is going
    172 		 * to be copy of parent's anyway. For exec, just
    173 		 * use our own vmspace.
    174 		 */
    175 		vm = (parent) ? parent->p_vmspace : p->p_vmspace;
    176 		s->p_break = (char *)vm->vm_daddr + ctob(vm->vm_dsize);
    177 
    178 		/*
    179 		 * Linux threads are emulated as NetBSD processes (not lwp)
    180 		 * We use native PID for Linux TID. The Linux TID is the
    181 		 * PID of the first process in the group. It is stored
    182 		 * here
    183 		 */
    184 		s->group_pid = p->p_pid;
    185 
    186 		/*
    187 		 * Initialize the list of threads in the group
    188 		 */
    189 		LIST_INIT(&s->threads);
    190 
    191 		s->xstat = 0;
    192 		s->flags = 0;
    193 		mutex_enter(proc_lock);
    194 	}
    195 
    196 	e->s = s;
    197 
    198 	/*
    199 	 * Add this thread in the group thread list
    200 	 */
    201 	LIST_INSERT_HEAD(&s->threads, e, threads);
    202 	mutex_exit(proc_lock);
    203 
    204 #ifdef LINUX32_NPTL
    205 	linux_nptl_proc_init(p, parent);
    206 #endif /* LINUX32_NPTL */
    207 
    208 	p->p_emuldata = e;
    209 }
    210 
    211 /*
    212  * Allocate new per-process structures. Called when executing Linux
    213  * process. We can reuse the old emuldata - if it's not null,
    214  * the executed process is of same emulation as original forked one.
    215  */
    216 static void
    217 linux32_e_proc_exec(struct proc *p, struct exec_package *epp)
    218 {
    219 	/* exec, use our vmspace */
    220 	linux32_e_proc_init(p, NULL, 0);
    221 }
    222 
    223 /*
    224  * Emulation per-process exit hook.
    225  */
    226 static void
    227 linux32_e_proc_exit(struct proc *p)
    228 {
    229 	struct linux_emuldata *e = p->p_emuldata;
    230 
    231 #ifdef LINUX32_NPTL
    232 	linux_nptl_proc_exit(p);
    233 #endif /* LINUX32_NPTL */
    234 
    235 	/* Remove the thread for the group thread list */
    236 	mutex_enter(proc_lock);
    237 	LIST_REMOVE(e, threads);
    238 
    239 	/* free Linux emuldata and set the pointer to null */
    240 	e->s->refs--;
    241 	if (e->s->refs == 0)
    242 		FREE(e->s, M_EMULDATA);
    243 	p->p_emuldata = NULL;
    244 	mutex_exit(proc_lock);
    245 	FREE(e, M_EMULDATA);
    246 }
    247 
    248 /*
    249  * Emulation fork hook.
    250  */
    251 static void
    252 linux32_e_proc_fork(p, parent, forkflags)
    253 	struct proc *p, *parent;
    254 	int forkflags;
    255 {
    256 	/*
    257 	 * The new process might share some vmspace-related stuff
    258 	 * with parent, depending on fork flags (CLONE_VM et.al).
    259 	 * Force allocation of new base emuldata, and share the
    260 	 * VM-related parts only if necessary.
    261 	 */
    262 	p->p_emuldata = NULL;
    263 	linux32_e_proc_init(p, parent, forkflags);
    264 
    265 #ifdef LINUX32_NPTL
    266 	linux_nptl_proc_fork(p, parent, linux32_userret);
    267 #endif
    268 
    269 	return;
    270 }
    271 
    272 #ifdef LINUX32_NPTL
    273 void
    274 linux32_userret(void)
    275 {
    276 	struct lwp *l = curlwp;
    277 	struct proc *p = l->l_proc;
    278 	struct linux_emuldata *led = p->p_emuldata;
    279 	int error;
    280 
    281 	/* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory  */
    282 	if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) {
    283 		if ((error = copyout(&l->l_proc->p_pid,
    284 		    led->child_tidptr,  sizeof(l->l_proc->p_pid))) != 0)
    285 			printf("linux32_userret: LINUX_CLONE_CHILD_SETTID "
    286 			    "failed (led->child_tidptr = %p, p->p_pid = %d)\n",
    287 			    led->child_tidptr, p->p_pid);
    288 	}
    289 
    290 	/* LINUX_CLONE_SETTLS: allocate a new TLS */
    291 	if (led->clone_flags & LINUX_CLONE_SETTLS) {
    292 		if (linux32_set_newtls(l, linux32_get_newtls(l)) != 0)
    293 			printf("linux32_userret: linux32_set_tls failed");
    294 	}
    295 
    296 	return;
    297 }
    298 #endif /* LINUX32_NPTL */
    299