2 * SPDX-License-Identifier: BSD-2-Clause
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 <sys/param.h>
37 #include <sys/mutex.h>
39 #include <sys/ptrace.h>
40 #include <sys/racct.h>
41 #include <sys/sched.h>
42 #include <sys/syscallsubr.h>
44 #include <sys/umtxvar.h>
45 #include <sys/unistd.h>
49 #include <vm/vm_map.h>
52 #include <machine/../linux32/linux.h>
53 #include <machine/../linux32/linux32_proto.h>
55 #include <machine/../linux/linux.h>
56 #include <machine/../linux/linux_proto.h>
58 #include <compat/linux/linux.h>
59 #include <compat/linux/linux_emul.h>
60 #include <compat/linux/linux_fork.h>
61 #include <compat/linux/linux_futex.h>
62 #include <compat/linux/linux_mib.h>
63 #include <compat/linux/linux_misc.h>
64 #include <compat/linux/linux_util.h>
66 #ifdef LINUX_LEGACY_SYSCALLS
68 linux_fork(struct thread *td, struct linux_fork_args *args)
75 bzero(&fr, sizeof(fr));
76 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
78 if ((error = fork1(td, &fr)) != 0)
81 td2 = FIRST_THREAD_IN_PROC(p2);
83 linux_proc_init(td, td2, false);
85 td->td_retval[0] = p2->p_pid;
88 * Make this runnable after we are finished with it.
92 sched_add(td2, SRQ_BORING);
98 linux_vfork(struct thread *td, struct linux_vfork_args *args)
105 bzero(&fr, sizeof(fr));
106 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
108 if ((error = fork1(td, &fr)) != 0)
111 td2 = FIRST_THREAD_IN_PROC(p2);
113 linux_proc_init(td, td2, false);
115 td->td_retval[0] = p2->p_pid;
118 * Make this runnable after we are finished with it.
122 sched_add(td2, SRQ_BORING);
129 linux_clone_proc(struct thread *td, struct l_clone_args *args)
136 struct linux_emuldata *em;
139 ff = RFPROC | RFSTOPPED;
140 if (LINUX_SIG_VALID(args->exit_signal)) {
141 exit_signal = linux_to_bsd_signal(args->exit_signal);
142 } else if (args->exit_signal != 0)
147 if (args->flags & LINUX_CLONE_VM)
149 if (args->flags & LINUX_CLONE_SIGHAND)
151 if ((args->flags & LINUX_CLONE_CLEAR_SIGHAND) != 0)
152 f2 |= FR2_DROPSIG_CAUGHT;
153 if (args->flags & LINUX_CLONE_FILES) {
154 if (!(args->flags & LINUX_CLONE_FS))
155 f2 |= FR2_SHARE_PATHS;
158 if (args->flags & LINUX_CLONE_FS)
159 f2 |= FR2_SHARE_PATHS;
162 if (args->flags & LINUX_CLONE_PARENT_SETTID)
163 if (args->parent_tid == NULL)
166 if (args->flags & LINUX_CLONE_VFORK)
169 bzero(&fr, sizeof(fr));
173 error = fork1(td, &fr);
177 td2 = FIRST_THREAD_IN_PROC(p2);
179 /* create the emuldata */
180 linux_proc_init(td, td2, false);
183 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
185 if (args->flags & LINUX_CLONE_CHILD_SETTID)
186 em->child_set_tid = args->child_tid;
188 em->child_set_tid = NULL;
190 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
191 em->child_clear_tid = args->child_tid;
193 em->child_clear_tid = NULL;
195 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
196 error = copyout(&p2->p_pid, args->parent_tid,
199 linux_msg(td, "copyout p_pid failed!");
203 p2->p_sigparent = exit_signal;
206 * In a case of stack = NULL, we are supposed to COW calling process
207 * stack. This is what normal fork() does, so we just keep tf_rsp arg
210 linux_set_upcall(td2, args->stack);
212 if (args->flags & LINUX_CLONE_SETTLS)
213 linux_set_cloned_tls(td2, PTRIN(args->tls));
216 * If CLONE_PARENT is set, then the parent of the new process will be
217 * the same as that of the calling process.
219 if (args->flags & LINUX_CLONE_PARENT) {
220 sx_xlock(&proctree_lock);
222 proc_reparent(p2, td->td_proc->p_pptr, true);
224 sx_xunlock(&proctree_lock);
228 * Make this runnable after we are finished with it.
232 sched_add(td2, SRQ_BORING);
234 td->td_retval[0] = p2->p_pid;
240 linux_clone_thread(struct thread *td, struct l_clone_args *args)
242 struct linux_emuldata *em;
243 struct thread *newtd;
247 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
248 td->td_tid, (unsigned)args->flags,
249 args->parent_tid, args->child_tid);
251 if ((args->flags & LINUX_CLONE_PARENT) != 0)
253 if (args->flags & LINUX_CLONE_PARENT_SETTID)
254 if (args->parent_tid == NULL)
257 /* Threads should be created with own stack */
258 if (PTRIN(args->stack) == NULL)
266 error = racct_add(p, RACCT_NTHR, 1);
273 /* Initialize our td */
274 error = kern_thr_alloc(p, 0, &newtd);
278 bzero(&newtd->td_startzero,
279 __rangeof(struct thread, td_startzero, td_endzero));
280 bcopy(&td->td_startcopy, &newtd->td_startcopy,
281 __rangeof(struct thread, td_startcopy, td_endcopy));
284 thread_cow_get(newtd, td);
286 cpu_copy_thread(newtd, td);
288 /* create the emuldata */
289 linux_proc_init(td, newtd, true);
292 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
294 if (args->flags & LINUX_CLONE_SETTLS)
295 linux_set_cloned_tls(newtd, PTRIN(args->tls));
297 if (args->flags & LINUX_CLONE_CHILD_SETTID)
298 em->child_set_tid = args->child_tid;
300 em->child_set_tid = NULL;
302 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
303 em->child_clear_tid = args->child_tid;
305 em->child_clear_tid = NULL;
307 cpu_thread_clean(newtd);
309 linux_set_upcall(newtd, args->stack);
312 p->p_flag |= P_HADTHREADS;
313 thread_link(newtd, p);
314 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
317 /* let the scheduler know about these things. */
318 sched_fork_thread(td, newtd);
321 ast_sched(newtd, TDA_SUSPEND);
323 if (p->p_ptevents & PTRACE_LWP)
324 newtd->td_dbgflags |= TDB_BORN;
329 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
330 td->td_tid, newtd->td_tid);
332 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
333 error = copyout(&newtd->td_tid, args->parent_tid,
334 sizeof(newtd->td_tid));
336 linux_msg(td, "clone_thread: copyout td_tid failed!");
340 * Make this runnable after we are finished with it.
343 TD_SET_CAN_RUN(newtd);
344 sched_add(newtd, SRQ_BORING);
346 td->td_retval[0] = newtd->td_tid;
354 racct_sub(p, RACCT_NTHR, 1);
362 linux_clone(struct thread *td, struct linux_clone_args *args)
364 struct l_clone_args ca = {
365 .flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL),
366 .child_tid = args->child_tidptr,
367 .parent_tid = args->parent_tidptr,
368 .exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL),
369 .stack = args->stack,
373 if (args->flags & LINUX_CLONE_THREAD)
374 return (linux_clone_thread(td, &ca));
376 return (linux_clone_proc(td, &ca));
381 linux_clone3_args_valid(struct l_user_clone_args *uca)
384 /* Verify that no unknown flags are passed along. */
385 if ((uca->flags & ~(LINUX_CLONE_LEGACY_FLAGS |
386 LINUX_CLONE_CLEAR_SIGHAND | LINUX_CLONE_INTO_CGROUP)) != 0)
388 if ((uca->flags & (LINUX_CLONE_DETACHED | LINUX_CSIGNAL)) != 0)
391 if ((uca->flags & (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND)) ==
392 (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND))
394 if ((uca->flags & (LINUX_CLONE_THREAD | LINUX_CLONE_PARENT)) != 0 &&
395 uca->exit_signal != 0)
398 /* We don't support set_tid, only validate input. */
399 if (uca->set_tid_size > LINUX_MAX_PID_NS_LEVEL)
401 if (uca->set_tid == 0 && uca->set_tid_size > 0)
403 if (uca->set_tid != 0 && uca->set_tid_size == 0)
406 if (uca->stack == 0 && uca->stack_size > 0)
408 if (uca->stack != 0 && uca->stack_size == 0)
411 /* Verify that higher 32bits of exit_signal are unset. */
412 if ((uca->exit_signal & ~(uint64_t)LINUX_CSIGNAL) != 0)
415 /* Verify that no unsupported flags are passed along. */
416 if ((uca->flags & LINUX_CLONE_NEWTIME) != 0) {
417 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_NEWTIME");
420 if ((uca->flags & LINUX_CLONE_INTO_CGROUP) != 0) {
421 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_INTO_CGROUP");
424 if (uca->set_tid != 0 || uca->set_tid_size != 0) {
425 LINUX_RATELIMIT_MSG("unsupported clone3 set_tid");
433 linux_clone3(struct thread *td, struct linux_clone3_args *args)
435 struct l_user_clone_args *uca;
436 struct l_clone_args *ca;
440 if (args->usize > PAGE_SIZE)
442 if (args->usize < LINUX_CLONE_ARGS_SIZE_VER0)
446 * usize can be less than size of struct clone_args, to avoid using
447 * of uninitialized data of struct clone_args, allocate at least
448 * sizeof(struct clone_args) storage and zero it.
450 size = max(args->usize, sizeof(*uca));
451 uca = malloc(size, M_LINUX, M_WAITOK | M_ZERO);
452 error = copyin(args->uargs, uca, args->usize);
455 error = linux_clone3_args_valid(uca);
458 ca = malloc(sizeof(*ca), M_LINUX, M_WAITOK | M_ZERO);
459 ca->flags = uca->flags;
460 ca->child_tid = PTRIN(uca->child_tid);
461 ca->parent_tid = PTRIN(uca->parent_tid);
462 ca->exit_signal = uca->exit_signal;
463 ca->stack = uca->stack + uca->stack_size;
464 ca->stack_size = uca->stack_size;
467 if ((ca->flags & LINUX_CLONE_THREAD) != 0)
468 error = linux_clone_thread(td, ca);
470 error = linux_clone_proc(td, ca);
478 linux_exit(struct thread *td, struct linux_exit_args *args)
480 struct linux_emuldata *em __diagused;
483 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
485 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
487 linux_thread_detach(td);
490 * XXX. When the last two threads of a process
491 * exit via pthread_exit() try thr_exit() first.
494 exit1(td, args->rval, 0);
499 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
501 struct linux_emuldata *em;
504 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
506 em->child_clear_tid = args->tidptr;
508 td->td_retval[0] = em->em_tid;
510 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
511 em->em_tid, args->tidptr, td->td_retval[0]);
517 linux_thread_detach(struct thread *td)
519 struct linux_emuldata *em;
520 int *child_clear_tid;
524 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
526 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
528 release_futexes(td, em);
530 child_clear_tid = em->child_clear_tid;
532 if (child_clear_tid != NULL) {
533 LINUX_CTR2(thread_detach, "thread(%d) %p",
534 em->em_tid, child_clear_tid);
536 error = suword32(child_clear_tid, 0);
540 error = futex_wake(td, child_clear_tid, 1, false);
542 * this cannot happen at the moment and if this happens it
543 * probably means there is a user space bug
546 linux_msg(td, "futex stuff in thread_detach failed.");
550 * Do not rely on the robust list which is maintained by userspace,
551 * cleanup remaining pi (if any) after release_futexes anyway.
553 umtx_thread_exit(td);