1 /* $NetBSD: linux32_exec.c,v 1.2 2006/06/25 16:15:40 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 1994-2006 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 * 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: linux32_exec.c,v 1.2 2006/06/25 16:15:40 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_emuldata.h> 65 66 #include <compat/linux32/common/linux32_exec.h> 67 #include <compat/linux32/common/linux32_types.h> 68 #include <compat/linux32/common/linux32_signal.h> 69 #include <compat/linux32/common/linux32_machdep.h> 70 71 #include <compat/linux32/linux32_syscallargs.h> 72 #include <compat/linux32/linux32_syscall.h> 73 74 extern char linux32_sigcode[1]; 75 extern char linux32_rt_sigcode[1]; 76 extern char linux32_esigcode[1]; 77 78 extern struct sysent linux32_sysent[]; 79 extern const char * const linux32_syscallnames[]; 80 81 static void linux32_e_proc_exec __P((struct proc *, struct exec_package *)); 82 static void linux32_e_proc_fork __P((struct proc *, struct proc *, int)); 83 static void linux32_e_proc_exit __P((struct proc *)); 84 static void linux32_e_proc_init __P((struct proc *, struct proc *, int)); 85 86 #ifdef LINUX32_NPTL 87 static void linux32_userret __P((struct lwp *, void *)); 88 #endif 89 90 /* 91 * Emulation switch. 92 */ 93 94 struct uvm_object *emul_linux32_object; 95 96 const struct emul emul_linux32 = { 97 "linux32", 98 "/emul/linux32", 99 #ifndef __HAVE_MINIMAL_EMUL 100 0, 101 NULL, 102 LINUX32_SYS_syscall, 103 LINUX32_SYS_NSYSENT, 104 #endif 105 linux32_sysent, 106 linux32_syscallnames, 107 linux32_sendsig, 108 trapsignal, 109 NULL, 110 linux32_sigcode, 111 linux32_esigcode, 112 &emul_linux32_object, 113 linux32_setregs, 114 linux32_e_proc_exec, 115 linux32_e_proc_fork, 116 linux32_e_proc_exit, 117 NULL, 118 NULL, 119 linux32_syscall_intern, 120 NULL, 121 NULL, 122 netbsd32_vm_default_addr, 123 }; 124 125 static void 126 linux32_e_proc_init(p, parent, forkflags) 127 struct proc *p, *parent; 128 int forkflags; 129 { 130 struct linux_emuldata *e = p->p_emuldata; 131 struct linux_emuldata_shared *s; 132 struct linux_emuldata *ep = NULL; 133 134 if (!e) { 135 /* allocate new Linux emuldata */ 136 MALLOC(e, void *, sizeof(struct linux_emuldata), 137 M_EMULDATA, M_WAITOK); 138 } else { 139 e->s->refs--; 140 if (e->s->refs == 0) 141 FREE(e->s, M_EMULDATA); 142 } 143 144 memset(e, '\0', sizeof(struct linux_emuldata)); 145 146 e->proc = p; 147 148 if (parent) 149 ep = parent->p_emuldata; 150 151 if (forkflags & FORK_SHAREVM) { 152 #ifdef DIAGNOSTIC 153 if (ep == NULL) { 154 killproc(p, "FORK_SHAREVM while emuldata is NULL\n"); 155 return; 156 } 157 #endif 158 s = ep->s; 159 s->refs++; 160 } else { 161 struct vmspace *vm; 162 163 MALLOC(s, void *, sizeof(struct linux_emuldata_shared), 164 M_EMULDATA, M_WAITOK); 165 s->refs = 1; 166 167 /* 168 * Set the process idea of the break to the real value. 169 * For fork, we use parent's vmspace since our's 170 * is not setup at the time of this call and is going 171 * to be copy of parent's anyway. For exec, just 172 * use our own vmspace. 173 */ 174 vm = (parent) ? parent->p_vmspace : p->p_vmspace; 175 s->p_break = vm->vm_daddr + ctob(vm->vm_dsize); 176 177 /* 178 * Linux threads are emulated as NetBSD processes (not lwp) 179 * We use native PID for Linux TID. The Linux TID is the 180 * PID of the first process in the group. It is stored 181 * here 182 */ 183 s->group_pid = p->p_pid; 184 185 /* 186 * Initialize the list of threads in the group 187 */ 188 LIST_INIT(&s->threads); 189 } 190 191 e->s = s; 192 193 /* 194 * Add this thread in the group thread list 195 */ 196 LIST_INSERT_HEAD(&s->threads, e, threads); 197 198 #ifdef LINUX32_NPTL 199 /* 200 * initialize TID pointers. ep->child_clear_tid and 201 * ep->child_set_tid will not be used beyond this point. 202 */ 203 e->child_clear_tid = NULL; 204 e->child_set_tid = NULL; 205 if (ep != NULL) { 206 e->clear_tid = ep->child_clear_tid; 207 e->set_tid = ep->child_set_tid; 208 e->set_tls = ep->set_tls; 209 ep->child_clear_tid = NULL; 210 ep->child_set_tid = NULL; 211 ep->set_tls = 0; 212 } else { 213 e->clear_tid = NULL; 214 e->set_tid = NULL; 215 e->set_tls = 0; 216 } 217 #endif /* LINUX32_NPTL */ 218 219 p->p_emuldata = e; 220 } 221 222 /* 223 * Allocate new per-process structures. Called when executing Linux 224 * process. We can reuse the old emuldata - if it's not null, 225 * the executed process is of same emulation as original forked one. 226 */ 227 static void 228 linux32_e_proc_exec(p, epp) 229 struct proc *p; 230 struct exec_package *epp; 231 { 232 /* exec, use our vmspace */ 233 linux32_e_proc_init(p, NULL, 0); 234 } 235 236 /* 237 * Emulation per-process exit hook. 238 */ 239 static void 240 linux32_e_proc_exit(p) 241 struct proc *p; 242 { 243 struct linux_emuldata *e = p->p_emuldata; 244 245 #ifdef LINUX32_NPTL 246 /* Emulate LINUX_CLONE_CHILD_CLEARTID */ 247 if (e->clear_tid != NULL) { 248 int error; 249 int null = 0; 250 struct linux32_sys_futex_args cup; 251 register_t retval; 252 struct lwp *l; 253 254 error = copyout(&null, e->clear_tid, sizeof(null)); 255 #ifdef DEBUG_LINUX 256 if (error != 0) 257 printf("linux32_e_proc_exit: cannot clear TID\n"); 258 #endif 259 260 l = proc_representative_lwp(p); 261 SCARG(&cup, uaddr) = e->clear_tid; 262 SCARG(&cup, op) = LINUX_FUTEX_WAKE; 263 SCARG(&cup, val) = 0x7fffffff; /* Awake everyone */ 264 SCARG(&cup, timeout) = NULL; 265 SCARG(&cup, uaddr2) = NULL; 266 SCARG(&cup, val3) = 0; 267 if ((error = linux32_sys_futex(l, &cup, &retval)) != 0) 268 printf("linux32_e_proc_exit: linux_sys_futex failed\n"); 269 } 270 #endif /* LINUX32_NPTL */ 271 272 /* Remove the thread for the group thread list */ 273 LIST_REMOVE(e, threads); 274 275 /* free Linux emuldata and set the pointer to null */ 276 e->s->refs--; 277 if (e->s->refs == 0) 278 FREE(e->s, M_EMULDATA); 279 FREE(e, M_EMULDATA); 280 p->p_emuldata = NULL; 281 } 282 283 /* 284 * Emulation fork hook. 285 */ 286 static void 287 linux32_e_proc_fork(p, parent, forkflags) 288 struct proc *p, *parent; 289 int forkflags; 290 { 291 #ifdef LINUX32_NPTL 292 struct linux_emuldata *e; 293 #endif 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 linux32_e_proc_init(p, parent, forkflags); 303 304 #ifdef LINUX32_NPTL 305 /* 306 * Emulate LINUX_CLONE_CHILD_SETTID and LINUX_CLONE_TLS: 307 * This cannot be done right now because the child VM 308 * is not set up. We will do it at userret time. 309 */ 310 e = p->p_emuldata; 311 if ((e->set_tid != NULL) || (e->set_tls != 0)) 312 p->p_userret = (*linux32_userret); 313 #endif 314 315 return; 316 } 317 318 #ifdef LINUX32_NPTL 319 static void 320 linux32_userret(l, arg) 321 struct lwp *l; 322 void *arg; 323 { 324 struct proc *p = l->l_proc; 325 struct linux_emuldata *led = p->p_emuldata; 326 int error; 327 328 p->p_userret = NULL; 329 330 /* Emulate LINUX_CLONE_CHILD_SETTID */ 331 if (led->set_tid != NULL) { 332 if ((error = copyout(&p->p_pid, 333 led->set_tid, sizeof(p->p_pid))) != 0) 334 printf("linux32_userret: cannot set TID\n"); 335 } 336 337 /* Emulate LINUX_CLONE_NEWTLS */ 338 if (led->set_tls != 0) { 339 if (linux32_set_newtls(l, led->set_tls) != 0) 340 printf("linux32_userret: cannot set TLS\n"); 341 } 342 343 return; 344 } 345 #endif /* LINUX32_NPTL */ 346