1 /* $NetBSD: linux32_exec.c,v 1.3 2006/08/23 19:49:09 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.3 2006/08/23 19:49:09 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 #include <sys/ptrace.h> /* For proc_reparent() */ 58 59 #include <uvm/uvm_extern.h> 60 61 #include <machine/cpu.h> 62 #include <machine/reg.h> 63 64 #include <compat/linux/common/linux_types.h> 65 #include <compat/linux/common/linux_emuldata.h> 66 67 #include <compat/linux32/common/linux32_exec.h> 68 #include <compat/linux32/common/linux32_types.h> 69 #include <compat/linux32/common/linux32_signal.h> 70 #include <compat/linux32/common/linux32_machdep.h> 71 72 #include <compat/linux32/linux32_syscallargs.h> 73 #include <compat/linux32/linux32_syscall.h> 74 75 extern char linux32_sigcode[1]; 76 extern char linux32_rt_sigcode[1]; 77 extern char linux32_esigcode[1]; 78 79 extern struct sysent linux32_sysent[]; 80 extern const char * const linux32_syscallnames[]; 81 82 static void linux32_e_proc_exec __P((struct proc *, struct exec_package *)); 83 static void linux32_e_proc_fork __P((struct proc *, struct proc *, int)); 84 static void linux32_e_proc_exit __P((struct proc *)); 85 static void linux32_e_proc_init __P((struct proc *, struct proc *, int)); 86 87 #ifdef LINUX32_NPTL 88 void linux32_userret __P((struct lwp *, void *)); 89 void linux_nptl_proc_fork __P((struct proc *, struct proc *, 90 void (luserret)(struct lwp *, void *))); 91 void linux_nptl_proc_exit __P((struct proc *)); 92 void linux_nptl_proc_init __P((struct proc *, struct proc *)); 93 #endif 94 95 /* 96 * Emulation switch. 97 */ 98 99 struct uvm_object *emul_linux32_object; 100 101 const struct emul emul_linux32 = { 102 "linux32", 103 "/emul/linux32", 104 #ifndef __HAVE_MINIMAL_EMUL 105 0, 106 NULL, 107 LINUX32_SYS_syscall, 108 LINUX32_SYS_NSYSENT, 109 #endif 110 linux32_sysent, 111 linux32_syscallnames, 112 linux32_sendsig, 113 trapsignal, 114 NULL, 115 linux32_sigcode, 116 linux32_esigcode, 117 &emul_linux32_object, 118 linux32_setregs, 119 linux32_e_proc_exec, 120 linux32_e_proc_fork, 121 linux32_e_proc_exit, 122 NULL, 123 NULL, 124 linux32_syscall_intern, 125 NULL, 126 NULL, 127 netbsd32_vm_default_addr, 128 }; 129 130 static void 131 linux32_e_proc_init(p, parent, forkflags) 132 struct proc *p, *parent; 133 int forkflags; 134 { 135 struct linux_emuldata *e = p->p_emuldata; 136 struct linux_emuldata_shared *s; 137 struct linux_emuldata *ep = NULL; 138 139 if (!e) { 140 /* allocate new Linux emuldata */ 141 MALLOC(e, void *, sizeof(struct linux_emuldata), 142 M_EMULDATA, M_WAITOK); 143 } else { 144 e->s->refs--; 145 if (e->s->refs == 0) 146 FREE(e->s, M_EMULDATA); 147 } 148 149 memset(e, '\0', sizeof(struct linux_emuldata)); 150 151 e->proc = p; 152 153 if (parent) 154 ep = parent->p_emuldata; 155 156 if (forkflags & FORK_SHAREVM) { 157 #ifdef DIAGNOSTIC 158 if (ep == NULL) { 159 killproc(p, "FORK_SHAREVM while emuldata is NULL\n"); 160 return; 161 } 162 #endif 163 s = ep->s; 164 s->refs++; 165 } else { 166 struct vmspace *vm; 167 168 MALLOC(s, void *, sizeof(struct linux_emuldata_shared), 169 M_EMULDATA, M_WAITOK); 170 s->refs = 1; 171 172 /* 173 * Set the process idea of the break to the real value. 174 * For fork, we use parent's vmspace since our's 175 * is not setup at the time of this call and is going 176 * to be copy of parent's anyway. For exec, just 177 * use our own vmspace. 178 */ 179 vm = (parent) ? parent->p_vmspace : p->p_vmspace; 180 s->p_break = vm->vm_daddr + ctob(vm->vm_dsize); 181 182 /* 183 * Linux threads are emulated as NetBSD processes (not lwp) 184 * We use native PID for Linux TID. The Linux TID is the 185 * PID of the first process in the group. It is stored 186 * here 187 */ 188 s->group_pid = p->p_pid; 189 190 /* 191 * Initialize the list of threads in the group 192 */ 193 LIST_INIT(&s->threads); 194 195 s->xstat = 0; 196 s->flags = 0; 197 } 198 199 e->s = s; 200 201 /* 202 * Add this thread in the group thread list 203 */ 204 LIST_INSERT_HEAD(&s->threads, e, threads); 205 206 #ifdef LINUX32_NPTL 207 linux_nptl_proc_init(p, parent); 208 #endif /* LINUX32_NPTL */ 209 210 p->p_emuldata = e; 211 } 212 213 /* 214 * Allocate new per-process structures. Called when executing Linux 215 * process. We can reuse the old emuldata - if it's not null, 216 * the executed process is of same emulation as original forked one. 217 */ 218 static void 219 linux32_e_proc_exec(p, epp) 220 struct proc *p; 221 struct exec_package *epp; 222 { 223 /* exec, use our vmspace */ 224 linux32_e_proc_init(p, NULL, 0); 225 } 226 227 /* 228 * Emulation per-process exit hook. 229 */ 230 static void 231 linux32_e_proc_exit(p) 232 struct proc *p; 233 { 234 struct linux_emuldata *e = p->p_emuldata; 235 236 #ifdef LINUX32_NPTL 237 linux_nptl_proc_exit(p); 238 #endif /* LINUX32_NPTL */ 239 240 /* Remove the thread for the group thread list */ 241 LIST_REMOVE(e, threads); 242 243 /* free Linux emuldata and set the pointer to null */ 244 e->s->refs--; 245 if (e->s->refs == 0) 246 FREE(e->s, M_EMULDATA); 247 FREE(e, M_EMULDATA); 248 p->p_emuldata = NULL; 249 } 250 251 /* 252 * Emulation fork hook. 253 */ 254 static void 255 linux32_e_proc_fork(p, parent, forkflags) 256 struct proc *p, *parent; 257 int forkflags; 258 { 259 /* 260 * The new process might share some vmspace-related stuff 261 * with parent, depending on fork flags (CLONE_VM et.al). 262 * Force allocation of new base emuldata, and share the 263 * VM-related parts only if necessary. 264 */ 265 p->p_emuldata = NULL; 266 linux32_e_proc_init(p, parent, forkflags); 267 268 #ifdef LINUX32_NPTL 269 linux_nptl_proc_fork(p, parent, (*linux32_userret)); 270 #endif 271 272 return; 273 } 274 275 #ifdef LINUX32_NPTL 276 void 277 linux32_userret(l, arg) 278 struct lwp *l; 279 void *arg; 280 { 281 struct proc *p = l->l_proc; 282 struct linux_emuldata *led = p->p_emuldata; 283 int error; 284 285 p->p_userret = NULL; 286 287 /* LINUX_CLONE_CHILD_SETTID: copy child's TID to child's memory */ 288 if (led->clone_flags & LINUX_CLONE_CHILD_SETTID) { 289 if ((error = copyout(&l->l_proc->p_pid, 290 led->child_tidptr, sizeof(l->l_proc->p_pid))) != 0) 291 printf("linux32_userret: LINUX_CLONE_CHILD_SETTID " 292 "failed (led->child_tidptr = %p, p->p_pid = %d)\n", 293 led->child_tidptr, p->p_pid); 294 } 295 296 /* LINUX_CLONE_SETTLS: allocate a new TLS */ 297 if (led->clone_flags & LINUX_CLONE_SETTLS) { 298 if (linux32_set_newtls(l, linux32_get_newtls(l)) != 0) 299 printf("linux32_userret: linux32_set_tls failed"); 300 } 301 302 return; 303 } 304 #endif /* LINUX32_NPTL */ 305