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/param.h>
34 #include <sys/mutex.h>
36 #include <sys/ptrace.h>
37 #include <sys/racct.h>
38 #include <sys/sched.h>
39 #include <sys/syscallsubr.h>
41 #include <sys/umtxvar.h>
42 #include <sys/unistd.h>
46 #include <vm/vm_map.h>
49 #include <machine/../linux32/linux.h>
50 #include <machine/../linux32/linux32_proto.h>
52 #include <machine/../linux/linux.h>
53 #include <machine/../linux/linux_proto.h>
55 #include <compat/linux/linux.h>
56 #include <compat/linux/linux_emul.h>
57 #include <compat/linux/linux_fork.h>
58 #include <compat/linux/linux_futex.h>
59 #include <compat/linux/linux_mib.h>
60 #include <compat/linux/linux_misc.h>
61 #include <compat/linux/linux_util.h>
63 #ifdef LINUX_LEGACY_SYSCALLS
65 linux_fork(struct thread *td, struct linux_fork_args *args)
72 bzero(&fr, sizeof(fr));
73 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
75 if ((error = fork1(td, &fr)) != 0)
78 td2 = FIRST_THREAD_IN_PROC(p2);
80 linux_proc_init(td, td2, false);
82 td->td_retval[0] = p2->p_pid;
85 * Make this runnable after we are finished with it.
89 sched_add(td2, SRQ_BORING);
95 linux_vfork(struct thread *td, struct linux_vfork_args *args)
102 bzero(&fr, sizeof(fr));
103 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
105 if ((error = fork1(td, &fr)) != 0)
108 td2 = FIRST_THREAD_IN_PROC(p2);
110 linux_proc_init(td, td2, false);
112 td->td_retval[0] = p2->p_pid;
115 * Make this runnable after we are finished with it.
119 sched_add(td2, SRQ_BORING);
126 linux_clone_proc(struct thread *td, struct l_clone_args *args)
133 struct linux_emuldata *em;
136 ff = RFPROC | RFSTOPPED;
137 if (LINUX_SIG_VALID(args->exit_signal)) {
138 exit_signal = linux_to_bsd_signal(args->exit_signal);
139 } else if (args->exit_signal != 0)
144 if (args->flags & LINUX_CLONE_VM)
146 if (args->flags & LINUX_CLONE_SIGHAND)
148 if ((args->flags & LINUX_CLONE_CLEAR_SIGHAND) != 0)
149 f2 |= FR2_DROPSIG_CAUGHT;
150 if (args->flags & LINUX_CLONE_FILES) {
151 if (!(args->flags & LINUX_CLONE_FS))
152 f2 |= FR2_SHARE_PATHS;
155 if (args->flags & LINUX_CLONE_FS)
156 f2 |= FR2_SHARE_PATHS;
159 if (args->flags & LINUX_CLONE_PARENT_SETTID)
160 if (args->parent_tid == NULL)
163 if (args->flags & LINUX_CLONE_VFORK)
166 bzero(&fr, sizeof(fr));
170 error = fork1(td, &fr);
174 td2 = FIRST_THREAD_IN_PROC(p2);
176 /* create the emuldata */
177 linux_proc_init(td, td2, false);
180 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
182 if (args->flags & LINUX_CLONE_CHILD_SETTID)
183 em->child_set_tid = args->child_tid;
185 em->child_set_tid = NULL;
187 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
188 em->child_clear_tid = args->child_tid;
190 em->child_clear_tid = NULL;
192 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
193 error = copyout(&p2->p_pid, args->parent_tid,
196 linux_msg(td, "copyout p_pid failed!");
200 p2->p_sigparent = exit_signal;
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
207 linux_set_upcall(td2, args->stack);
209 if (args->flags & LINUX_CLONE_SETTLS)
210 linux_set_cloned_tls(td2, PTRIN(args->tls));
213 * If CLONE_PARENT is set, then the parent of the new process will be
214 * the same as that of the calling process.
216 if (args->flags & LINUX_CLONE_PARENT) {
217 sx_xlock(&proctree_lock);
219 proc_reparent(p2, td->td_proc->p_pptr, true);
221 sx_xunlock(&proctree_lock);
225 * Make this runnable after we are finished with it.
229 sched_add(td2, SRQ_BORING);
231 td->td_retval[0] = p2->p_pid;
237 linux_clone_thread(struct thread *td, struct l_clone_args *args)
239 struct linux_emuldata *em;
240 struct thread *newtd;
244 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
245 td->td_tid, (unsigned)args->flags,
246 args->parent_tid, args->child_tid);
248 if ((args->flags & LINUX_CLONE_PARENT) != 0)
250 if (args->flags & LINUX_CLONE_PARENT_SETTID)
251 if (args->parent_tid == NULL)
254 /* Threads should be created with own stack */
255 if (PTRIN(args->stack) == NULL)
263 error = racct_add(p, RACCT_NTHR, 1);
270 /* Initialize our td */
271 error = kern_thr_alloc(p, 0, &newtd);
275 bzero(&newtd->td_startzero,
276 __rangeof(struct thread, td_startzero, td_endzero));
277 bcopy(&td->td_startcopy, &newtd->td_startcopy,
278 __rangeof(struct thread, td_startcopy, td_endcopy));
281 thread_cow_get(newtd, td);
283 cpu_copy_thread(newtd, td);
285 /* create the emuldata */
286 linux_proc_init(td, newtd, true);
289 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
291 if (args->flags & LINUX_CLONE_SETTLS)
292 linux_set_cloned_tls(newtd, PTRIN(args->tls));
294 if (args->flags & LINUX_CLONE_CHILD_SETTID)
295 em->child_set_tid = args->child_tid;
297 em->child_set_tid = NULL;
299 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
300 em->child_clear_tid = args->child_tid;
302 em->child_clear_tid = NULL;
304 cpu_thread_clean(newtd);
306 linux_set_upcall(newtd, args->stack);
309 p->p_flag |= P_HADTHREADS;
310 thread_link(newtd, p);
311 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
314 /* let the scheduler know about these things. */
315 sched_fork_thread(td, newtd);
318 ast_sched(newtd, TDA_SUSPEND);
320 if (p->p_ptevents & PTRACE_LWP)
321 newtd->td_dbgflags |= TDB_BORN;
326 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
327 td->td_tid, newtd->td_tid);
329 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
330 error = copyout(&newtd->td_tid, args->parent_tid,
331 sizeof(newtd->td_tid));
333 linux_msg(td, "clone_thread: copyout td_tid failed!");
337 * Make this runnable after we are finished with it.
340 TD_SET_CAN_RUN(newtd);
341 sched_add(newtd, SRQ_BORING);
343 td->td_retval[0] = newtd->td_tid;
351 racct_sub(p, RACCT_NTHR, 1);
359 linux_clone(struct thread *td, struct linux_clone_args *args)
361 struct l_clone_args ca = {
362 .flags = (lower_32_bits(args->flags) & ~LINUX_CSIGNAL),
363 .child_tid = args->child_tidptr,
364 .parent_tid = args->parent_tidptr,
365 .exit_signal = (lower_32_bits(args->flags) & LINUX_CSIGNAL),
366 .stack = args->stack,
370 if (args->flags & LINUX_CLONE_THREAD)
371 return (linux_clone_thread(td, &ca));
373 return (linux_clone_proc(td, &ca));
378 linux_clone3_args_valid(struct l_user_clone_args *uca)
381 /* Verify that no unknown flags are passed along. */
382 if ((uca->flags & ~(LINUX_CLONE_LEGACY_FLAGS |
383 LINUX_CLONE_CLEAR_SIGHAND | LINUX_CLONE_INTO_CGROUP)) != 0)
385 if ((uca->flags & (LINUX_CLONE_DETACHED | LINUX_CSIGNAL)) != 0)
388 if ((uca->flags & (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND)) ==
389 (LINUX_CLONE_SIGHAND | LINUX_CLONE_CLEAR_SIGHAND))
391 if ((uca->flags & (LINUX_CLONE_THREAD | LINUX_CLONE_PARENT)) != 0 &&
392 uca->exit_signal != 0)
395 /* We don't support set_tid, only validate input. */
396 if (uca->set_tid_size > LINUX_MAX_PID_NS_LEVEL)
398 if (uca->set_tid == 0 && uca->set_tid_size > 0)
400 if (uca->set_tid != 0 && uca->set_tid_size == 0)
403 if (uca->stack == 0 && uca->stack_size > 0)
405 if (uca->stack != 0 && uca->stack_size == 0)
408 /* Verify that higher 32bits of exit_signal are unset. */
409 if ((uca->exit_signal & ~(uint64_t)LINUX_CSIGNAL) != 0)
412 /* Verify that no unsupported flags are passed along. */
413 if ((uca->flags & LINUX_CLONE_NEWTIME) != 0) {
414 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_NEWTIME");
417 if ((uca->flags & LINUX_CLONE_INTO_CGROUP) != 0) {
418 LINUX_RATELIMIT_MSG("unsupported clone3 option CLONE_INTO_CGROUP");
421 if (uca->set_tid != 0 || uca->set_tid_size != 0) {
422 LINUX_RATELIMIT_MSG("unsupported clone3 set_tid");
430 linux_clone3(struct thread *td, struct linux_clone3_args *args)
432 struct l_user_clone_args *uca;
433 struct l_clone_args *ca;
437 if (args->usize > PAGE_SIZE)
439 if (args->usize < LINUX_CLONE_ARGS_SIZE_VER0)
443 * usize can be less than size of struct clone_args, to avoid using
444 * of uninitialized data of struct clone_args, allocate at least
445 * sizeof(struct clone_args) storage and zero it.
447 size = max(args->usize, sizeof(*uca));
448 uca = malloc(size, M_LINUX, M_WAITOK | M_ZERO);
449 error = copyin(args->uargs, uca, args->usize);
452 error = linux_clone3_args_valid(uca);
455 ca = malloc(sizeof(*ca), M_LINUX, M_WAITOK | M_ZERO);
456 ca->flags = uca->flags;
457 ca->child_tid = PTRIN(uca->child_tid);
458 ca->parent_tid = PTRIN(uca->parent_tid);
459 ca->exit_signal = uca->exit_signal;
460 ca->stack = uca->stack + uca->stack_size;
461 ca->stack_size = uca->stack_size;
464 if ((ca->flags & LINUX_CLONE_THREAD) != 0)
465 error = linux_clone_thread(td, ca);
467 error = linux_clone_proc(td, ca);
475 linux_exit(struct thread *td, struct linux_exit_args *args)
477 struct linux_emuldata *em __diagused;
480 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
482 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
484 linux_thread_detach(td);
487 * XXX. When the last two threads of a process
488 * exit via pthread_exit() try thr_exit() first.
491 exit1(td, args->rval, 0);
496 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
498 struct linux_emuldata *em;
501 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
503 em->child_clear_tid = args->tidptr;
505 td->td_retval[0] = em->em_tid;
507 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
508 em->em_tid, args->tidptr, td->td_retval[0]);
514 linux_thread_detach(struct thread *td)
516 struct linux_emuldata *em;
517 int *child_clear_tid;
521 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
523 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
525 release_futexes(td, em);
527 child_clear_tid = em->child_clear_tid;
529 if (child_clear_tid != NULL) {
530 LINUX_CTR2(thread_detach, "thread(%d) %p",
531 em->em_tid, child_clear_tid);
533 error = suword32(child_clear_tid, 0);
537 error = futex_wake(td, child_clear_tid, 1, false);
539 * this cannot happen at the moment and if this happens it
540 * probably means there is a user space bug
543 linux_msg(td, "futex stuff in thread_detach failed.");
547 * Do not rely on the robust list which is maintained by userspace,
548 * cleanup remaining pi (if any) after release_futexes anyway.
550 umtx_thread_exit(td);