2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Tim J. Robbins
5 * Copyright (c) 2002 Doug Rabson
6 * Copyright (c) 2000 Marcel Moolenaar
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_compat.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
41 #include <sys/mutex.h>
43 #include <sys/ptrace.h>
44 #include <sys/racct.h>
45 #include <sys/sched.h>
46 #include <sys/syscallsubr.h>
48 #include <sys/umtxvar.h>
49 #include <sys/unistd.h>
54 #include <vm/vm_map.h>
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
63 #include <compat/linux/linux.h>
64 #include <compat/linux/linux_emul.h>
65 #include <compat/linux/linux_fork.h>
66 #include <compat/linux/linux_futex.h>
67 #include <compat/linux/linux_mib.h>
68 #include <compat/linux/linux_misc.h>
69 #include <compat/linux/linux_util.h>
71 #ifdef LINUX_LEGACY_SYSCALLS
73 linux_fork(struct thread *td, struct linux_fork_args *args)
80 bzero(&fr, sizeof(fr));
81 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
83 if ((error = fork1(td, &fr)) != 0)
86 td2 = FIRST_THREAD_IN_PROC(p2);
88 linux_proc_init(td, td2, false);
90 td->td_retval[0] = p2->p_pid;
93 * Make this runnable after we are finished with it.
97 sched_add(td2, SRQ_BORING);
103 linux_vfork(struct thread *td, struct linux_vfork_args *args)
110 bzero(&fr, sizeof(fr));
111 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
113 if ((error = fork1(td, &fr)) != 0)
116 td2 = FIRST_THREAD_IN_PROC(p2);
118 linux_proc_init(td, td2, false);
120 td->td_retval[0] = p2->p_pid;
123 * Make this runnable after we are finished with it.
127 sched_add(td2, SRQ_BORING);
134 linux_clone_proc(struct thread *td, struct l_clone_args *args)
141 struct linux_emuldata *em;
144 ff = RFPROC | RFSTOPPED;
145 if (LINUX_SIG_VALID(args->exit_signal)) {
146 exit_signal = linux_to_bsd_signal(args->exit_signal);
147 } else if (args->exit_signal != 0)
152 if (args->flags & LINUX_CLONE_VM)
154 if (args->flags & LINUX_CLONE_SIGHAND)
156 if ((args->flags & LINUX_CLONE_CLEAR_SIGHAND) != 0)
157 f2 |= FR2_DROPSIG_CAUGHT;
158 if (args->flags & LINUX_CLONE_FILES) {
159 if (!(args->flags & LINUX_CLONE_FS))
160 f2 |= FR2_SHARE_PATHS;
163 if (args->flags & LINUX_CLONE_FS)
164 f2 |= FR2_SHARE_PATHS;
167 if (args->flags & LINUX_CLONE_PARENT_SETTID)
168 if (args->parent_tid == NULL)
171 if (args->flags & LINUX_CLONE_VFORK)
174 bzero(&fr, sizeof(fr));
178 error = fork1(td, &fr);
182 td2 = FIRST_THREAD_IN_PROC(p2);
184 /* create the emuldata */
185 linux_proc_init(td, td2, false);
188 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
190 if (args->flags & LINUX_CLONE_CHILD_SETTID)
191 em->child_set_tid = args->child_tid;
193 em->child_set_tid = NULL;
195 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
196 em->child_clear_tid = args->child_tid;
198 em->child_clear_tid = NULL;
200 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
201 error = copyout(&p2->p_pid, args->parent_tid,
204 linux_msg(td, "copyout p_pid failed!");
208 p2->p_sigparent = exit_signal;
211 * In a case of stack = NULL, we are supposed to COW calling process
212 * stack. This is what normal fork() does, so we just keep tf_rsp arg
215 linux_set_upcall(td2, args->stack);
217 if (args->flags & LINUX_CLONE_SETTLS)
218 linux_set_cloned_tls(td2, PTRIN(args->tls));
221 * If CLONE_PARENT is set, then the parent of the new process will be
222 * the same as that of the calling process.
224 if (args->flags & LINUX_CLONE_PARENT) {
225 sx_xlock(&proctree_lock);
227 proc_reparent(p2, td->td_proc->p_pptr, true);
229 sx_xunlock(&proctree_lock);
233 * Make this runnable after we are finished with it.
237 sched_add(td2, SRQ_BORING);
239 td->td_retval[0] = p2->p_pid;
245 linux_clone_thread(struct thread *td, struct l_clone_args *args)
247 struct linux_emuldata *em;
248 struct thread *newtd;
252 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
253 td->td_tid, (unsigned)args->flags,
254 args->parent_tid, args->child_tid);
256 if ((args->flags & LINUX_CLONE_PARENT) != 0)
258 if (args->flags & LINUX_CLONE_PARENT_SETTID)
259 if (args->parent_tid == NULL)
262 /* Threads should be created with own stack */
263 if (PTRIN(args->stack) == NULL)
271 error = racct_add(p, RACCT_NTHR, 1);
278 /* Initialize our td */
279 error = kern_thr_alloc(p, 0, &newtd);
283 cpu_copy_thread(newtd, td);
285 bzero(&newtd->td_startzero,
286 __rangeof(struct thread, td_startzero, td_endzero));
287 bcopy(&td->td_startcopy, &newtd->td_startcopy,
288 __rangeof(struct thread, td_startcopy, td_endcopy));
291 thread_cow_get(newtd, td);
293 /* create the emuldata */
294 linux_proc_init(td, newtd, true);
297 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
299 if (args->flags & LINUX_CLONE_SETTLS)
300 linux_set_cloned_tls(newtd, PTRIN(args->tls));
302 if (args->flags & LINUX_CLONE_CHILD_SETTID)
303 em->child_set_tid = args->child_tid;
305 em->child_set_tid = NULL;
307 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
308 em->child_clear_tid = args->child_tid;
310 em->child_clear_tid = NULL;
312 cpu_thread_clean(newtd);
314 linux_set_upcall(newtd, args->stack);
317 p->p_flag |= P_HADTHREADS;
318 thread_link(newtd, p);
319 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
322 /* let the scheduler know about these things. */
323 sched_fork_thread(td, newtd);
326 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
328 if (p->p_ptevents & PTRACE_LWP)
329 newtd->td_dbgflags |= TDB_BORN;
334 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
335 td->td_tid, newtd->td_tid);
337 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
338 error = copyout(&newtd->td_tid, args->parent_tid,
339 sizeof(newtd->td_tid));
341 linux_msg(td, "clone_thread: copyout td_tid failed!");
345 * Make this runnable after we are finished with it.
348 TD_SET_CAN_RUN(newtd);
349 sched_add(newtd, SRQ_BORING);
351 td->td_retval[0] = newtd->td_tid;
359 racct_sub(p, RACCT_NTHR, 1);
367 linux_clone(struct thread *td, struct linux_clone_args *args)
369 struct l_clone_args ca = {
370 .flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL),
371 .child_tid = args->child_tidptr,
372 .parent_tid = args->parent_tidptr,
373 .exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL),
374 .stack = args->stack,
378 if (args->flags & LINUX_CLONE_THREAD)
379 return (linux_clone_thread(td, &ca));
381 return (linux_clone_proc(td, &ca));
386 linux_clone3_args_valid(struct l_user_clone_args *uca)
389 /* Verify that no unknown flags are passed along. */
390 if ((uca->flags & ~(LINUX_CLONE_LEGACY_FLAGS |
391 LINUX_CLONE_CLEAR_SIGHAND | LINUX_CLONE_INTO_CGROUP)) != 0)
393 if ((uca->flags & (LINUX_CLONE_DETACHED | LINUX_CSIGNAL)) != 0)
396 if ((uca->flags & (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND)) ==
397 (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND))
399 if ((uca->flags & (LINUX_CLONE_THREAD | LINUX_CLONE_PARENT)) != 0 &&
400 uca->exit_signal != 0)
403 /* We don't support set_tid, only validate input. */
404 if (uca->set_tid_size > LINUX_MAX_PID_NS_LEVEL)
406 if (uca->set_tid == 0 && uca->set_tid_size > 0)
408 if (uca->set_tid != 0 && uca->set_tid_size == 0)
411 if (uca->stack == 0 && uca->stack_size > 0)
413 if (uca->stack != 0 && uca->stack_size == 0)
416 /* Verify that higher 32bits of exit_signal are unset. */
417 if ((uca->exit_signal & ~(uint64_t)LINUX_CSIGNAL) != 0)
420 /* Verify that no unsupported flags are passed along. */
421 if ((uca->flags & LINUX_CLONE_NEWTIME) != 0) {
422 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_NEWTIME");
425 if ((uca->flags & LINUX_CLONE_INTO_CGROUP) != 0) {
426 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_INTO_CGROUP");
429 if (uca->set_tid != 0 || uca->set_tid_size != 0) {
430 LINUX_RATELIMIT_MSG("unsupported clone3 set_tid");
438 linux_clone3(struct thread *td, struct linux_clone3_args *args)
440 struct l_user_clone_args *uca;
441 struct l_clone_args *ca;
445 if (args->usize > PAGE_SIZE)
447 if (args->usize < LINUX_CLONE_ARGS_SIZE_VER0)
451 * usize can be less than size of struct clone_args, to avoid using
452 * of uninitialized data of struct clone_args, allocate at least
453 * sizeof(struct clone_args) storage and zero it.
455 size = max(args->usize, sizeof(*uca));
456 uca = malloc(size, M_LINUX, M_WAITOK | M_ZERO);
457 error = copyin(args->uargs, uca, args->usize);
460 error = linux_clone3_args_valid(uca);
463 ca = malloc(sizeof(*ca), M_LINUX, M_WAITOK | M_ZERO);
464 ca->flags = uca->flags;
465 ca->child_tid = PTRIN(uca->child_tid);
466 ca->parent_tid = PTRIN(uca->parent_tid);
467 ca->exit_signal = uca->exit_signal;
468 ca->stack = uca->stack + uca->stack_size;
469 ca->stack_size = uca->stack_size;
472 if ((ca->flags & LINUX_CLONE_THREAD) != 0)
473 error = linux_clone_thread(td, ca);
475 error = linux_clone_proc(td, ca);
483 linux_exit(struct thread *td, struct linux_exit_args *args)
485 struct linux_emuldata *em __diagused;
488 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
490 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
492 linux_thread_detach(td);
495 * XXX. When the last two threads of a process
496 * exit via pthread_exit() try thr_exit() first.
499 exit1(td, args->rval, 0);
504 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
506 struct linux_emuldata *em;
509 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
511 em->child_clear_tid = args->tidptr;
513 td->td_retval[0] = em->em_tid;
515 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
516 em->em_tid, args->tidptr, td->td_retval[0]);
522 linux_thread_detach(struct thread *td)
524 struct linux_emuldata *em;
525 int *child_clear_tid;
529 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
531 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
533 release_futexes(td, em);
535 child_clear_tid = em->child_clear_tid;
537 if (child_clear_tid != NULL) {
538 LINUX_CTR2(thread_detach, "thread(%d) %p",
539 em->em_tid, child_clear_tid);
541 error = suword32(child_clear_tid, 0);
545 error = futex_wake(td, child_clear_tid, 1, false);
547 * this cannot happen at the moment and if this happens it
548 * probably means there is a user space bug
551 linux_msg(td, "futex stuff in thread_detach failed.");
555 * Do not rely on the robust list which is maintained by userspace,
556 * cleanup remaining pi (if any) after release_futexes anyway.
558 umtx_thread_exit(td);