1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer 12 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_compat.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/imgact.h> 37 #include <sys/ktr.h> 38 #include <sys/lock.h> 39 #include <sys/mutex.h> 40 #include <sys/proc.h> 41 #include <sys/racct.h> 42 #include <sys/sched.h> 43 #include <sys/syscallsubr.h> 44 #include <sys/sx.h> 45 #include <sys/unistd.h> 46 #include <sys/wait.h> 47 48 #include <vm/vm.h> 49 #include <vm/pmap.h> 50 #include <vm/vm_map.h> 51 52 #ifdef COMPAT_LINUX32 53 #include <machine/../linux32/linux.h> 54 #include <machine/../linux32/linux32_proto.h> 55 #else 56 #include <machine/../linux/linux.h> 57 #include <machine/../linux/linux_proto.h> 58 #endif 59 #include <compat/linux/linux_emul.h> 60 #include <compat/linux/linux_futex.h> 61 #include <compat/linux/linux_misc.h> 62 #include <compat/linux/linux_util.h> 63 64 int 65 linux_fork(struct thread *td, struct linux_fork_args *args) 66 { 67 int error; 68 struct proc *p2; 69 struct thread *td2; 70 71 #ifdef DEBUG 72 if (ldebug(fork)) 73 printf(ARGS(fork, "")); 74 #endif 75 76 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0)) 77 != 0) 78 return (error); 79 80 td2 = FIRST_THREAD_IN_PROC(p2); 81 82 linux_proc_init(td, td2, 0); 83 84 td->td_retval[0] = p2->p_pid; 85 86 /* 87 * Make this runnable after we are finished with it. 88 */ 89 thread_lock(td2); 90 TD_SET_CAN_RUN(td2); 91 sched_add(td2, SRQ_BORING); 92 thread_unlock(td2); 93 94 return (0); 95 } 96 97 int 98 linux_vfork(struct thread *td, struct linux_vfork_args *args) 99 { 100 int error; 101 struct proc *p2; 102 struct thread *td2; 103 104 #ifdef DEBUG 105 if (ldebug(vfork)) 106 printf(ARGS(vfork, "")); 107 #endif 108 109 /* Exclude RFPPWAIT */ 110 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2, 111 NULL, 0)) != 0) 112 return (error); 113 114 115 td2 = FIRST_THREAD_IN_PROC(p2); 116 117 linux_proc_init(td, td2, 0); 118 119 PROC_LOCK(p2); 120 p2->p_flag |= P_PPWAIT; 121 PROC_UNLOCK(p2); 122 123 td->td_retval[0] = p2->p_pid; 124 125 /* 126 * Make this runnable after we are finished with it. 127 */ 128 thread_lock(td2); 129 TD_SET_CAN_RUN(td2); 130 sched_add(td2, SRQ_BORING); 131 thread_unlock(td2); 132 133 /* wait for the children to exit, ie. emulate vfork */ 134 PROC_LOCK(p2); 135 while (p2->p_flag & P_PPWAIT) 136 cv_wait(&p2->p_pwait, &p2->p_mtx); 137 PROC_UNLOCK(p2); 138 139 return (0); 140 } 141 142 static int 143 linux_clone_proc(struct thread *td, struct linux_clone_args *args) 144 { 145 int error, ff = RFPROC | RFSTOPPED; 146 struct proc *p2; 147 struct thread *td2; 148 int exit_signal; 149 struct linux_emuldata *em; 150 151 #ifdef DEBUG 152 if (ldebug(clone)) { 153 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, " 154 "child tid: %p"), (unsigned)args->flags, 155 args->stack, args->parent_tidptr, args->child_tidptr); 156 } 157 #endif 158 159 exit_signal = args->flags & 0x000000ff; 160 if (LINUX_SIG_VALID(exit_signal)) { 161 exit_signal = linux_to_bsd_signal(exit_signal); 162 } else if (exit_signal != 0) 163 return (EINVAL); 164 165 if (args->flags & LINUX_CLONE_VM) 166 ff |= RFMEM; 167 if (args->flags & LINUX_CLONE_SIGHAND) 168 ff |= RFSIGSHARE; 169 /* 170 * XXX: In Linux, sharing of fs info (chroot/cwd/umask) 171 * and open files is independant. In FreeBSD, its in one 172 * structure but in reality it does not cause any problems 173 * because both of these flags are usually set together. 174 */ 175 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS))) 176 ff |= RFFDG; 177 178 if (args->flags & LINUX_CLONE_PARENT_SETTID) 179 if (args->parent_tidptr == NULL) 180 return (EINVAL); 181 182 error = fork1(td, ff, 0, &p2, NULL, 0); 183 if (error) 184 return (error); 185 186 td2 = FIRST_THREAD_IN_PROC(p2); 187 188 /* create the emuldata */ 189 linux_proc_init(td, td2, args->flags); 190 191 em = em_find(td2); 192 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n")); 193 194 if (args->flags & LINUX_CLONE_CHILD_SETTID) 195 em->child_set_tid = args->child_tidptr; 196 else 197 em->child_set_tid = NULL; 198 199 if (args->flags & LINUX_CLONE_CHILD_CLEARTID) 200 em->child_clear_tid = args->child_tidptr; 201 else 202 em->child_clear_tid = NULL; 203 204 if (args->flags & LINUX_CLONE_PARENT_SETTID) { 205 error = copyout(&p2->p_pid, args->parent_tidptr, 206 sizeof(p2->p_pid)); 207 if (error) 208 printf(LMSG("copyout failed!")); 209 } 210 211 PROC_LOCK(p2); 212 p2->p_sigparent = exit_signal; 213 PROC_UNLOCK(p2); 214 /* 215 * In a case of stack = NULL, we are supposed to COW calling process 216 * stack. This is what normal fork() does, so we just keep tf_rsp arg 217 * intact. 218 */ 219 linux_set_upcall_kse(td2, PTROUT(args->stack)); 220 221 if (args->flags & LINUX_CLONE_SETTLS) 222 linux_set_cloned_tls(td2, args->tls); 223 224 #ifdef DEBUG 225 if (ldebug(clone)) 226 printf(LMSG("clone: successful rfork to %d, " 227 "stack %p sig = %d"), (int)p2->p_pid, args->stack, 228 exit_signal); 229 #endif 230 231 if (args->flags & LINUX_CLONE_VFORK) { 232 PROC_LOCK(p2); 233 p2->p_flag |= P_PPWAIT; 234 PROC_UNLOCK(p2); 235 } 236 237 /* 238 * Make this runnable after we are finished with it. 239 */ 240 thread_lock(td2); 241 TD_SET_CAN_RUN(td2); 242 sched_add(td2, SRQ_BORING); 243 thread_unlock(td2); 244 245 td->td_retval[0] = p2->p_pid; 246 247 if (args->flags & LINUX_CLONE_VFORK) { 248 /* wait for the children to exit, ie. emulate vfork */ 249 PROC_LOCK(p2); 250 while (p2->p_flag & P_PPWAIT) 251 cv_wait(&p2->p_pwait, &p2->p_mtx); 252 PROC_UNLOCK(p2); 253 } 254 255 return (0); 256 } 257 258 static int 259 linux_clone_thread(struct thread *td, struct linux_clone_args *args) 260 { 261 struct linux_emuldata *em; 262 struct thread *newtd; 263 struct proc *p; 264 int error; 265 266 #ifdef DEBUG 267 if (ldebug(clone)) { 268 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, " 269 "child tid: %p"), (unsigned)args->flags, 270 args->stack, args->parent_tidptr, args->child_tidptr); 271 } 272 #endif 273 274 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p", 275 td->td_tid, (unsigned)args->flags, 276 args->parent_tidptr, args->child_tidptr); 277 278 if (args->flags & LINUX_CLONE_PARENT_SETTID) 279 if (args->parent_tidptr == NULL) 280 return (EINVAL); 281 282 /* Threads should be created with own stack */ 283 if (args->stack == NULL) 284 return (EINVAL); 285 286 p = td->td_proc; 287 288 /* Initialize our td */ 289 error = kern_thr_alloc(p, 0, &newtd); 290 if (error) 291 return (error); 292 293 cpu_set_upcall(newtd, td); 294 295 bzero(&newtd->td_startzero, 296 __rangeof(struct thread, td_startzero, td_endzero)); 297 bcopy(&td->td_startcopy, &newtd->td_startcopy, 298 __rangeof(struct thread, td_startcopy, td_endcopy)); 299 300 newtd->td_proc = p; 301 thread_cow_get(newtd, td); 302 303 /* create the emuldata */ 304 linux_proc_init(td, newtd, args->flags); 305 306 em = em_find(newtd); 307 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n")); 308 309 if (args->flags & LINUX_CLONE_SETTLS) 310 linux_set_cloned_tls(newtd, args->tls); 311 312 if (args->flags & LINUX_CLONE_CHILD_SETTID) 313 em->child_set_tid = args->child_tidptr; 314 else 315 em->child_set_tid = NULL; 316 317 if (args->flags & LINUX_CLONE_CHILD_CLEARTID) 318 em->child_clear_tid = args->child_tidptr; 319 else 320 em->child_clear_tid = NULL; 321 322 cpu_thread_clean(newtd); 323 324 linux_set_upcall_kse(newtd, PTROUT(args->stack)); 325 326 PROC_LOCK(p); 327 p->p_flag |= P_HADTHREADS; 328 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name)); 329 330 if (args->flags & LINUX_CLONE_PARENT) 331 thread_link(newtd, p->p_pptr); 332 else 333 thread_link(newtd, p); 334 335 thread_lock(td); 336 /* let the scheduler know about these things. */ 337 sched_fork_thread(td, newtd); 338 thread_unlock(td); 339 if (P_SHOULDSTOP(p)) 340 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK; 341 PROC_UNLOCK(p); 342 343 tidhash_add(newtd); 344 345 #ifdef DEBUG 346 if (ldebug(clone)) 347 printf(ARGS(clone, "successful clone to %d, stack %p"), 348 (int)newtd->td_tid, args->stack); 349 #endif 350 351 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d", 352 td->td_tid, newtd->td_tid); 353 354 if (args->flags & LINUX_CLONE_PARENT_SETTID) { 355 error = copyout(&newtd->td_tid, args->parent_tidptr, 356 sizeof(newtd->td_tid)); 357 if (error) 358 printf(LMSG("clone_thread: copyout failed!")); 359 } 360 361 /* 362 * Make this runnable after we are finished with it. 363 */ 364 thread_lock(newtd); 365 TD_SET_CAN_RUN(newtd); 366 sched_add(newtd, SRQ_BORING); 367 thread_unlock(newtd); 368 369 td->td_retval[0] = newtd->td_tid; 370 371 return (0); 372 } 373 374 int 375 linux_clone(struct thread *td, struct linux_clone_args *args) 376 { 377 378 if (args->flags & LINUX_CLONE_THREAD) 379 return (linux_clone_thread(td, args)); 380 else 381 return (linux_clone_proc(td, args)); 382 } 383 384 int 385 linux_exit(struct thread *td, struct linux_exit_args *args) 386 { 387 struct linux_emuldata *em; 388 389 em = em_find(td); 390 KASSERT(em != NULL, ("exit: emuldata not found.\n")); 391 392 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval); 393 394 linux_thread_detach(td); 395 396 /* 397 * XXX. When the last two threads of a process 398 * exit via pthread_exit() try thr_exit() first. 399 */ 400 kern_thr_exit(td); 401 exit1(td, args->rval, 0); 402 /* NOTREACHED */ 403 } 404 405 int 406 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args) 407 { 408 struct linux_emuldata *em; 409 410 em = em_find(td); 411 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n")); 412 413 em->child_clear_tid = args->tidptr; 414 415 td->td_retval[0] = em->em_tid; 416 417 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d", 418 em->em_tid, args->tidptr, td->td_retval[0]); 419 420 return (0); 421 } 422 423 void 424 linux_thread_detach(struct thread *td) 425 { 426 struct linux_sys_futex_args cup; 427 struct linux_emuldata *em; 428 int *child_clear_tid; 429 int error; 430 431 em = em_find(td); 432 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n")); 433 434 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid); 435 436 release_futexes(td, em); 437 438 child_clear_tid = em->child_clear_tid; 439 440 if (child_clear_tid != NULL) { 441 442 LINUX_CTR2(thread_detach, "thread(%d) %p", 443 em->em_tid, child_clear_tid); 444 445 error = suword32(child_clear_tid, 0); 446 if (error != 0) 447 return; 448 449 cup.uaddr = child_clear_tid; 450 cup.op = LINUX_FUTEX_WAKE; 451 cup.val = 1; /* wake one */ 452 cup.timeout = NULL; 453 cup.uaddr2 = NULL; 454 cup.val3 = 0; 455 error = linux_sys_futex(td, &cup); 456 /* 457 * this cannot happen at the moment and if this happens it 458 * probably means there is a user space bug 459 */ 460 if (error != 0) 461 linux_msg(td, "futex stuff in thread_detach failed."); 462 } 463 } 464