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