1 /* $NetBSD: linux_exec.c,v 1.77 2005/06/22 15:10:51 manu 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.77 2005/06/22 15:10:51 manu 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 static void linux_userret_settid __P((struct lwp *, void *)); 85 86 /* 87 * Execve(2). Just check the alternate emulation path, and pass it on 88 * to the NetBSD execve(). 89 */ 90 int 91 linux_sys_execve(l, v, retval) 92 struct lwp *l; 93 void *v; 94 register_t *retval; 95 { 96 struct linux_sys_execve_args /* { 97 syscallarg(const char *) path; 98 syscallarg(char **) argv; 99 syscallarg(char **) envp; 100 } */ *uap = v; 101 struct proc *p = l->l_proc; 102 struct sys_execve_args ap; 103 caddr_t sg; 104 105 sg = stackgap_init(p, 0); 106 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path)); 107 108 SCARG(&ap, path) = SCARG(uap, path); 109 SCARG(&ap, argp) = SCARG(uap, argp); 110 SCARG(&ap, envp) = SCARG(uap, envp); 111 112 return sys_execve(l, &ap, retval); 113 } 114 115 /* 116 * Emulation switch. 117 */ 118 119 struct uvm_object *emul_linux_object; 120 121 const struct emul emul_linux = { 122 "linux", 123 "/emul/linux", 124 #ifndef __HAVE_MINIMAL_EMUL 125 0, 126 (const int *)native_to_linux_errno, 127 LINUX_SYS_syscall, 128 LINUX_SYS_NSYSENT, 129 #endif 130 linux_sysent, 131 linux_syscallnames, 132 linux_sendsig, 133 linux_trapsignal, 134 NULL, 135 linux_sigcode, 136 linux_esigcode, 137 &emul_linux_object, 138 linux_setregs, 139 linux_e_proc_exec, 140 linux_e_proc_fork, 141 linux_e_proc_exit, 142 NULL, 143 NULL, 144 #ifdef __HAVE_SYSCALL_INTERN 145 linux_syscall_intern, 146 #else 147 #error Implement __HAVE_SYSCALL_INTERN for this platform 148 #endif 149 NULL, 150 NULL, 151 152 uvm_default_mapaddr, 153 154 linux_usertrap, 155 }; 156 157 static void 158 linux_e_proc_init(p, parent, forkflags) 159 struct proc *p, *parent; 160 int forkflags; 161 { 162 struct linux_emuldata *e = p->p_emuldata; 163 struct linux_emuldata_shared *s; 164 struct linux_emuldata *ep = NULL; 165 166 if (!e) { 167 /* allocate new Linux emuldata */ 168 MALLOC(e, void *, sizeof(struct linux_emuldata), 169 M_EMULDATA, M_WAITOK); 170 } else { 171 e->s->refs--; 172 if (e->s->refs == 0) 173 FREE(e->s, M_EMULDATA); 174 } 175 176 memset(e, '\0', sizeof(struct linux_emuldata)); 177 178 if (parent) 179 ep = parent->p_emuldata; 180 181 if (forkflags & FORK_SHAREVM) { 182 #ifdef DIAGNOSTIC 183 if (ep == NULL) { 184 killproc(p, "FORK_SHAREVM while emuldata is NULL\n"); 185 return; 186 } 187 #endif 188 s = ep->s; 189 s->refs++; 190 } else { 191 struct vmspace *vm; 192 193 MALLOC(s, void *, sizeof(struct linux_emuldata_shared), 194 M_EMULDATA, M_WAITOK); 195 s->refs = 1; 196 197 /* 198 * Set the process idea of the break to the real value. 199 * For fork, we use parent's vmspace since our's 200 * is not setup at the time of this call and is going 201 * to be copy of parent's anyway. For exec, just 202 * use our own vmspace. 203 */ 204 vm = (parent) ? parent->p_vmspace : p->p_vmspace; 205 s->p_break = vm->vm_daddr + ctob(vm->vm_dsize); 206 207 /* 208 * Linux threads are emulated as NetBSD processes (not lwp) 209 * We use native PID for Linux TID. The Linux TID is the 210 * PID of the first process in the group. It is stored 211 * here 212 */ 213 s->group_pid = p->p_pid; 214 } 215 216 e->s = s; 217 218 /* 219 * initialize TID pointers. ep->child_clear_tid and 220 * ep->child_set_tid will not be used beyond this point. 221 */ 222 e->child_clear_tid = NULL; 223 e->child_set_tid = NULL; 224 if (ep != NULL) { 225 e->clear_tid = ep->child_clear_tid; 226 e->set_tid = ep->child_set_tid; 227 ep->child_clear_tid = NULL; 228 ep->child_set_tid = NULL; 229 } else { 230 e->clear_tid = NULL; 231 e->set_tid = NULL; 232 } 233 234 p->p_emuldata = e; 235 } 236 237 /* 238 * Allocate new per-process structures. Called when executing Linux 239 * process. We can reuse the old emuldata - if it's not null, 240 * the executed process is of same emulation as original forked one. 241 */ 242 static void 243 linux_e_proc_exec(p, epp) 244 struct proc *p; 245 struct exec_package *epp; 246 { 247 /* exec, use our vmspace */ 248 linux_e_proc_init(p, NULL, 0); 249 } 250 251 /* 252 * Emulation per-process exit hook. 253 */ 254 static void 255 linux_e_proc_exit(p) 256 struct proc *p; 257 { 258 struct linux_emuldata *e = p->p_emuldata; 259 260 /* Emulate LINUX_CLONE_CHILD_CLEARTID */ 261 if (e->clear_tid != NULL) { 262 int error; 263 int null = 0; 264 265 if ((error = copyout(&null, 266 e->clear_tid, 267 sizeof(null))) != 0) 268 printf("linux_e_proc_exit: cannot clear TID\n"); 269 270 #ifdef notyet /* Not yet implemented */ 271 if ((error = linux_sys_futex(e->clear_tid, 272 LINUX_FUTEX_WAKE, 1, NULL, NULL, 0)) != 0) 273 printf("linux_e_proc_exit: linux_sys_futex failed\n"); 274 #endif 275 } 276 277 /* free Linux emuldata and set the pointer to null */ 278 e->s->refs--; 279 if (e->s->refs == 0) 280 FREE(e->s, M_EMULDATA); 281 FREE(e, M_EMULDATA); 282 p->p_emuldata = NULL; 283 } 284 285 /* 286 * Emulation fork hook. 287 */ 288 static void 289 linux_e_proc_fork(p, parent, forkflags) 290 struct proc *p, *parent; 291 int forkflags; 292 { 293 struct linux_emuldata *e; 294 295 /* 296 * The new process might share some vmspace-related stuff 297 * with parent, depending on fork flags (CLONE_VM et.al). 298 * Force allocation of new base emuldata, and share the 299 * VM-related parts only if necessary. 300 */ 301 p->p_emuldata = NULL; 302 linux_e_proc_init(p, parent, forkflags); 303 304 /* 305 * Emulate LINUX_CLONE_CHILD_SETTID: This cannot be done 306 * right now because the child VM is not set up. We will 307 * do it at userret time. 308 */ 309 e = p->p_emuldata; 310 if (e->set_tid != NULL) 311 p->p_userret = (*linux_userret_settid); 312 313 return; 314 } 315 316 static void 317 linux_userret_settid(l, arg) 318 struct lwp *l; 319 void *arg; 320 { 321 struct proc *p = l->l_proc; 322 struct linux_emuldata *led = p->p_emuldata; 323 int error; 324 325 p->p_userret = NULL; 326 327 /* Emulate LINUX_CLONE_CHILD_SETTID */ 328 if (led->set_tid != NULL) { 329 if ((error = copyout(&p->p_pid, 330 led->set_tid, sizeof(p->p_pid))) != 0) 331 printf("linux_userret_settid: cannot set TID\n"); 332 } 333 334 return; 335 } 336