linux_emul.c revision 167157
1131087Smarcel/*- 2131087Smarcel * Copyright (c) 2006 Roman Divacky 3138215Smarcel * All rights reserved. 4138215Smarcel * 5131087Smarcel * Redistribution and use in source and binary forms, with or without 6136910Sru * modification, are permitted provided that the following conditions 7137440Smarcel * are met: 8137440Smarcel * 1. Redistributions of source code must retain the above copyright 9137440Smarcel * notice, this list of conditions and the following disclaimer 10137440Smarcel * in this position and unchanged. 11137440Smarcel * 2. Redistributions in binary form must reproduce the above copyright 12137440Smarcel * notice, this list of conditions and the following disclaimer in the 13137440Smarcel * documentation and/or other materials provided with the distribution. 14137440Smarcel * 3. The name of the author may not be used to endorse or promote products 15137440Smarcel * derived from this software without specific prior written permission 16137440Smarcel * 17137440Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18137440Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19137440Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20137440Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21138215Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22137440Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23137440Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24138215Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25138215Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26137440Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27137440Smarcel */ 28137440Smarcel 29137440Smarcel#include <sys/cdefs.h> 30137440Smarcel__FBSDID("$FreeBSD: head/sys/compat/linux/linux_emul.c 167157 2007-03-02 00:08:47Z jkim $"); 31137440Smarcel 32137440Smarcel#include "opt_compat.h" 33137440Smarcel 34137440Smarcel#include <sys/param.h> 35137440Smarcel#include <sys/systm.h> 36137440Smarcel#include <sys/imgact.h> 37137440Smarcel#include <sys/kernel.h> 38137440Smarcel#include <sys/lock.h> 39137440Smarcel#include <sys/malloc.h> 40138383Smarcel#include <sys/mutex.h> 41138383Smarcel#include <sys/sx.h> 42137440Smarcel#include <sys/proc.h> 43137440Smarcel#include <sys/syscallsubr.h> 44137440Smarcel#include <sys/sysproto.h> 45137440Smarcel#include <sys/unistd.h> 46137440Smarcel 47137440Smarcel#include <compat/linux/linux_emul.h> 48137440Smarcel#include <compat/linux/linux_futex.h> 49134154Sdavidxu 50131087Smarcel#ifdef COMPAT_LINUX32 51141911Sobrien#include <machine/../linux32/linux.h> 52141911Sobrien#include <machine/../linux32/linux32_proto.h> 53141911Sobrien#else 54141911Sobrien#include <machine/../linux/linux.h> 55141911Sobrien#include <machine/../linux/linux_proto.h> 56138380Smarcel#endif 57138215Smarcel 58138215Smarcelstruct sx emul_shared_lock; 59138215Smarcelstruct sx emul_lock; 60134154Sdavidxu 61134154Sdavidxu/* this returns locked reference to the emuldata entry (if found) */ 62138215Smarcelstruct linux_emuldata * 63138215Smarcelem_find(struct proc *p, int locked) 64131087Smarcel{ 65131087Smarcel struct linux_emuldata *em; 66131087Smarcel 67131087Smarcel if (locked == EMUL_DOLOCK) 68131087Smarcel EMUL_LOCK(&emul_lock); 69131087Smarcel 70131087Smarcel em = p->p_emuldata; 71131087Smarcel 72131087Smarcel if (em == NULL && locked == EMUL_DOLOCK) 73 EMUL_UNLOCK(&emul_lock); 74 75 return (em); 76} 77 78int 79linux_proc_init(struct thread *td, pid_t child, int flags) 80{ 81 struct linux_emuldata *em, *p_em; 82 struct proc *p; 83 84 if (child != 0) { 85 /* non-exec call */ 86 em = malloc(sizeof *em, M_LINUX, M_WAITOK | M_ZERO); 87 em->pid = child; 88 em->pdeath_signal = 0; 89 if (flags & LINUX_CLONE_THREAD) { 90 /* handled later in the code */ 91 } else { 92 struct linux_emuldata_shared *s; 93 94 s = malloc(sizeof *s, M_LINUX, M_WAITOK | M_ZERO); 95 s->refs = 1; 96 s->group_pid = child; 97 98 LIST_INIT(&s->threads); 99 em->shared = s; 100 } 101 } else { 102 /* lookup the old one */ 103 em = em_find(td->td_proc, EMUL_DOLOCK); 104 KASSERT(em != NULL, ("proc_init: emuldata not found in exec case.\n")); 105 } 106 107 em->child_clear_tid = NULL; 108 em->child_set_tid = NULL; 109 110 /* 111 * allocate the shared struct only in clone()/fork cases in the case 112 * of clone() td = calling proc and child = pid of the newly created 113 * proc 114 */ 115 if (child != 0) { 116 if (flags & LINUX_CLONE_THREAD) { 117 /* lookup the parent */ 118 /* 119 * we dont have to lock the p_em because 120 * its waiting for us in linux_clone so 121 * there is no chance of it changing the 122 * p_em->shared address 123 */ 124 p_em = em_find(td->td_proc, EMUL_DONTLOCK); 125 KASSERT(p_em != NULL, ("proc_init: parent emuldata not found for CLONE_THREAD\n")); 126 em->shared = p_em->shared; 127 EMUL_SHARED_WLOCK(&emul_shared_lock); 128 em->shared->refs++; 129 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 130 } else { 131 /* 132 * handled earlier to avoid malloc(M_WAITOK) with 133 * rwlock held 134 */ 135 } 136 } 137 if (child != 0) { 138 EMUL_SHARED_WLOCK(&emul_shared_lock); 139 LIST_INSERT_HEAD(&em->shared->threads, em, threads); 140 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 141 142 p = pfind(child); 143 KASSERT(p != NULL, ("process not found in proc_init\n")); 144 p->p_emuldata = em; 145 PROC_UNLOCK(p); 146 } else 147 EMUL_UNLOCK(&emul_lock); 148 149 return (0); 150} 151 152void 153linux_proc_exit(void *arg __unused, struct proc *p) 154{ 155 struct linux_emuldata *em; 156 int error; 157 struct thread *td = FIRST_THREAD_IN_PROC(p); 158 int *child_clear_tid; 159 struct proc *q, *nq; 160 161 if (__predict_true(p->p_sysent != &elf_linux_sysvec)) 162 return; 163 164 /* find the emuldata */ 165 em = em_find(p, EMUL_DOLOCK); 166 167 KASSERT(em != NULL, ("proc_exit: emuldata not found.\n")); 168 169 /* reparent all procs that are not a thread leader to initproc */ 170 if (em->shared->group_pid != p->p_pid) { 171 child_clear_tid = em->child_clear_tid; 172 EMUL_UNLOCK(&emul_lock); 173 sx_xlock(&proctree_lock); 174 wakeup(initproc); 175 PROC_LOCK(p); 176 proc_reparent(p, initproc); 177 p->p_sigparent = SIGCHLD; 178 PROC_UNLOCK(p); 179 sx_xunlock(&proctree_lock); 180 } else { 181 child_clear_tid = em->child_clear_tid; 182 EMUL_UNLOCK(&emul_lock); 183 } 184 185 EMUL_SHARED_WLOCK(&emul_shared_lock); 186 LIST_REMOVE(em, threads); 187 188 em->shared->refs--; 189 if (em->shared->refs == 0) { 190 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 191 free(em->shared, M_LINUX); 192 } else 193 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 194 195 if (child_clear_tid != NULL) { 196 struct linux_sys_futex_args cup; 197 int null = 0; 198 199 error = copyout(&null, child_clear_tid, sizeof(null)); 200 if (error) { 201 free(em, M_LINUX); 202 return; 203 } 204 205 /* futexes stuff */ 206 cup.uaddr = child_clear_tid; 207 cup.op = LINUX_FUTEX_WAKE; 208 cup.val = 0x7fffffff; /* Awake everyone */ 209 cup.timeout = NULL; 210 cup.uaddr2 = NULL; 211 cup.val3 = 0; 212 error = linux_sys_futex(FIRST_THREAD_IN_PROC(p), &cup); 213 /* 214 * this cannot happen at the moment and if this happens it 215 * probably means there is a user space bug 216 */ 217 if (error) 218 printf(LMSG("futex stuff in proc_exit failed.\n")); 219 } 220 221 /* clean the stuff up */ 222 free(em, M_LINUX); 223 224 /* this is a little weird but rewritten from exit1() */ 225 sx_xlock(&proctree_lock); 226 q = LIST_FIRST(&p->p_children); 227 for (; q != NULL; q = nq) { 228 nq = LIST_NEXT(q, p_sibling); 229 if (q->p_flag & P_WEXIT) 230 continue; 231 if (__predict_false(q->p_sysent != &elf_linux_sysvec)) 232 continue; 233 em = em_find(q, EMUL_DOLOCK); 234 KASSERT(em != NULL, ("linux_reparent: emuldata not found: %i\n", q->p_pid)); 235 if (em->pdeath_signal != 0) { 236 PROC_LOCK(q); 237 psignal(q, em->pdeath_signal); 238 PROC_UNLOCK(q); 239 } 240 EMUL_UNLOCK(&emul_lock); 241 } 242 sx_xunlock(&proctree_lock); 243} 244 245/* 246 * This is used in a case of transition from FreeBSD binary execing to linux binary 247 * in this case we create linux emuldata proc entry with the pid of the currently running 248 * process. 249 */ 250void 251linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp) 252{ 253 if (__predict_false(imgp->sysent == &elf_linux_sysvec 254 && p->p_sysent != &elf_linux_sysvec)) 255 linux_proc_init(FIRST_THREAD_IN_PROC(p), p->p_pid, 0); 256 if (__predict_false(imgp->sysent != &elf_linux_sysvec 257 && p->p_sysent == &elf_linux_sysvec)) { 258 struct linux_emuldata *em; 259 260 /* 261 * XXX:There's a race because here we assign p->p_emuldata NULL 262 * but the process is still counted as linux one for a short 263 * time so some other process might reference it and try to 264 * access its p->p_emuldata and panicing on a NULL reference. 265 */ 266 em = em_find(p, EMUL_DONTLOCK); 267 268 KASSERT(em != NULL, ("proc_exec: emuldata not found.\n")); 269 270 EMUL_SHARED_WLOCK(&emul_shared_lock); 271 LIST_REMOVE(em, threads); 272 273 PROC_LOCK(p); 274 p->p_emuldata = NULL; 275 PROC_UNLOCK(p); 276 277 em->shared->refs--; 278 if (em->shared->refs == 0) { 279 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 280 free(em->shared, M_LINUX); 281 } else 282 EMUL_SHARED_WUNLOCK(&emul_shared_lock); 283 284 free(em, M_LINUX); 285 } 286} 287 288void 289linux_schedtail(void *arg __unused, struct proc *p) 290{ 291 struct linux_emuldata *em; 292 int error = 0; 293 int *child_set_tid; 294 295 if (__predict_true(p->p_sysent != &elf_linux_sysvec)) 296 return; 297 298 /* find the emuldata */ 299 em = em_find(p, EMUL_DOLOCK); 300 301 KASSERT(em != NULL, ("linux_schedtail: emuldata not found.\n")); 302 child_set_tid = em->child_set_tid; 303 EMUL_UNLOCK(&emul_lock); 304 305 if (child_set_tid != NULL) 306 error = copyout(&p->p_pid, (int *)child_set_tid, 307 sizeof(p->p_pid)); 308 309 return; 310} 311 312int 313linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args) 314{ 315 struct linux_emuldata *em; 316 317#ifdef DEBUG 318 if (ldebug(set_tid_address)) 319 printf(ARGS(set_tid_address, "%p"), args->tidptr); 320#endif 321 322 /* find the emuldata */ 323 em = em_find(td->td_proc, EMUL_DOLOCK); 324 325 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n")); 326 327 em->child_clear_tid = args->tidptr; 328 td->td_retval[0] = td->td_proc->p_pid; 329 330 EMUL_UNLOCK(&emul_lock); 331 return 0; 332} 333