1 /* $NetBSD: linux_exec.c,v 1.74 2005/03/26 17:10:43 christos 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.74 2005/03/26 17:10:43 christos 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 (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 153 static void 154 linux_e_proc_init(p, parent, forkflags) 155 struct proc *p, *parent; 156 int forkflags; 157 { 158 struct linux_emuldata *e = p->p_emuldata; 159 struct linux_emuldata_shared *s; 160 161 if (!e) { 162 /* allocate new Linux emuldata */ 163 MALLOC(e, void *, sizeof(struct linux_emuldata), 164 M_EMULDATA, M_WAITOK); 165 } else { 166 e->s->refs--; 167 if (e->s->refs == 0) 168 FREE(e->s, M_EMULDATA); 169 } 170 171 memset(e, '\0', sizeof(struct linux_emuldata)); 172 173 if (forkflags & FORK_SHAREVM) { 174 struct linux_emuldata *e2 = parent->p_emuldata; 175 s = e2->s; 176 s->refs++; 177 } else { 178 struct vmspace *vm; 179 180 MALLOC(s, void *, sizeof(struct linux_emuldata_shared), 181 M_EMULDATA, M_WAITOK); 182 s->refs = 1; 183 184 /* 185 * Set the process idea of the break to the real value. 186 * For fork, we use parent's vmspace since our's 187 * is not setup at the time of this call and is going 188 * to be copy of parent's anyway. For exec, just 189 * use our own vmspace. 190 */ 191 vm = (parent) ? parent->p_vmspace : p->p_vmspace; 192 s->p_break = vm->vm_daddr + ctob(vm->vm_dsize); 193 194 } 195 196 e->s = s; 197 p->p_emuldata = e; 198 } 199 200 /* 201 * Allocate new per-process structures. Called when executing Linux 202 * process. We can reuse the old emuldata - if it's not null, 203 * the executed process is of same emulation as original forked one. 204 */ 205 static void 206 linux_e_proc_exec(p, epp) 207 struct proc *p; 208 struct exec_package *epp; 209 { 210 /* exec, use our vmspace */ 211 linux_e_proc_init(p, NULL, 0); 212 } 213 214 /* 215 * Emulation per-process exit hook. 216 */ 217 static void 218 linux_e_proc_exit(p) 219 struct proc *p; 220 { 221 struct linux_emuldata *e = p->p_emuldata; 222 223 /* free Linux emuldata and set the pointer to null */ 224 e->s->refs--; 225 if (e->s->refs == 0) 226 FREE(e->s, M_EMULDATA); 227 FREE(e, M_EMULDATA); 228 p->p_emuldata = NULL; 229 } 230 231 /* 232 * Emulation fork hook. 233 */ 234 static void 235 linux_e_proc_fork(p, parent, forkflags) 236 struct proc *p, *parent; 237 int forkflags; 238 { 239 /* 240 * The new process might share some vmspace-related stuff 241 * with parent, depending on fork flags (CLONE_VM et.al). 242 * Force allocation of new base emuldata, and share the 243 * VM-related parts only if necessary. 244 */ 245 p->p_emuldata = NULL; 246 linux_e_proc_init(p, parent, forkflags); 247 } 248