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/racct.h>
44 #include <sys/sched.h>
45 #include <sys/syscallsubr.h>
48 #include <sys/unistd.h>
53 #include <vm/vm_map.h>
56 #include <machine/../linux32/linux.h>
57 #include <machine/../linux32/linux32_proto.h>
59 #include <machine/../linux/linux.h>
60 #include <machine/../linux/linux_proto.h>
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>
68 linux_fork(struct thread *td, struct linux_fork_args *args)
77 printf(ARGS(fork, ""));
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, 0);
90 td->td_retval[0] = p2->p_pid;
93 * Make this runnable after we are finished with it.
97 sched_add(td2, SRQ_BORING);
104 linux_vfork(struct thread *td, struct linux_vfork_args *args)
113 printf(ARGS(vfork, ""));
116 bzero(&fr, sizeof(fr));
117 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
119 if ((error = fork1(td, &fr)) != 0)
122 td2 = FIRST_THREAD_IN_PROC(p2);
124 linux_proc_init(td, td2, 0);
126 td->td_retval[0] = p2->p_pid;
129 * Make this runnable after we are finished with it.
133 sched_add(td2, SRQ_BORING);
140 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
143 int error, ff = RFPROC | RFSTOPPED;
147 struct linux_emuldata *em;
151 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
152 "child tid: %p"), (unsigned)args->flags,
153 args->stack, args->parent_tidptr, args->child_tidptr);
157 exit_signal = args->flags & 0x000000ff;
158 if (LINUX_SIG_VALID(exit_signal)) {
159 exit_signal = linux_to_bsd_signal(exit_signal);
160 } else if (exit_signal != 0)
163 if (args->flags & LINUX_CLONE_VM)
165 if (args->flags & LINUX_CLONE_SIGHAND)
168 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
169 * and open files is independent. In FreeBSD, its in one
170 * structure but in reality it does not cause any problems
171 * because both of these flags are usually set together.
173 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
176 if (args->flags & LINUX_CLONE_PARENT_SETTID)
177 if (args->parent_tidptr == NULL)
180 if (args->flags & LINUX_CLONE_VFORK)
183 bzero(&fr, sizeof(fr));
186 error = fork1(td, &fr);
190 td2 = FIRST_THREAD_IN_PROC(p2);
192 /* create the emuldata */
193 linux_proc_init(td, td2, args->flags);
196 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
198 if (args->flags & LINUX_CLONE_CHILD_SETTID)
199 em->child_set_tid = args->child_tidptr;
201 em->child_set_tid = NULL;
203 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
204 em->child_clear_tid = args->child_tidptr;
206 em->child_clear_tid = NULL;
208 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
209 error = copyout(&p2->p_pid, args->parent_tidptr,
212 printf(LMSG("copyout failed!"));
216 p2->p_sigparent = exit_signal;
219 * In a case of stack = NULL, we are supposed to COW calling process
220 * stack. This is what normal fork() does, so we just keep tf_rsp arg
223 linux_set_upcall_kse(td2, PTROUT(args->stack));
225 if (args->flags & LINUX_CLONE_SETTLS)
226 linux_set_cloned_tls(td2, args->tls);
229 * If CLONE_PARENT is set, then the parent of the new process will be
230 * the same as that of the calling process.
232 if (args->flags & LINUX_CLONE_PARENT) {
233 sx_xlock(&proctree_lock);
235 proc_reparent(p2, td->td_proc->p_pptr);
237 sx_xunlock(&proctree_lock);
242 printf(LMSG("clone: successful rfork to %d, "
243 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
248 * Make this runnable after we are finished with it.
252 sched_add(td2, SRQ_BORING);
255 td->td_retval[0] = p2->p_pid;
261 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
263 struct linux_emuldata *em;
264 struct thread *newtd;
270 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
271 "child tid: %p"), (unsigned)args->flags,
272 args->stack, args->parent_tidptr, args->child_tidptr);
276 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
277 td->td_tid, (unsigned)args->flags,
278 args->parent_tidptr, args->child_tidptr);
280 if (args->flags & LINUX_CLONE_PARENT_SETTID)
281 if (args->parent_tidptr == NULL)
284 /* Threads should be created with own stack */
285 if (args->stack == NULL)
293 error = racct_add(p, RACCT_NTHR, 1);
300 /* Initialize our td */
301 error = kern_thr_alloc(p, 0, &newtd);
305 cpu_copy_thread(newtd, td);
307 bzero(&newtd->td_startzero,
308 __rangeof(struct thread, td_startzero, td_endzero));
309 bcopy(&td->td_startcopy, &newtd->td_startcopy,
310 __rangeof(struct thread, td_startcopy, td_endcopy));
313 thread_cow_get(newtd, td);
315 /* create the emuldata */
316 linux_proc_init(td, newtd, args->flags);
319 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
321 if (args->flags & LINUX_CLONE_SETTLS)
322 linux_set_cloned_tls(newtd, args->tls);
324 if (args->flags & LINUX_CLONE_CHILD_SETTID)
325 em->child_set_tid = args->child_tidptr;
327 em->child_set_tid = NULL;
329 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
330 em->child_clear_tid = args->child_tidptr;
332 em->child_clear_tid = NULL;
334 cpu_thread_clean(newtd);
336 linux_set_upcall_kse(newtd, PTROUT(args->stack));
339 p->p_flag |= P_HADTHREADS;
340 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
342 if (args->flags & LINUX_CLONE_PARENT)
343 thread_link(newtd, p->p_pptr);
345 thread_link(newtd, p);
348 /* let the scheduler know about these things. */
349 sched_fork_thread(td, newtd);
352 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
359 printf(ARGS(clone, "successful clone to %d, stack %p"),
360 (int)newtd->td_tid, args->stack);
363 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
364 td->td_tid, newtd->td_tid);
366 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
367 error = copyout(&newtd->td_tid, args->parent_tidptr,
368 sizeof(newtd->td_tid));
370 printf(LMSG("clone_thread: copyout failed!"));
374 * Make this runnable after we are finished with it.
377 TD_SET_CAN_RUN(newtd);
378 sched_add(newtd, SRQ_BORING);
379 thread_unlock(newtd);
381 td->td_retval[0] = newtd->td_tid;
389 racct_sub(p, RACCT_NTHR, 1);
397 linux_clone(struct thread *td, struct linux_clone_args *args)
400 if (args->flags & LINUX_CLONE_THREAD)
401 return (linux_clone_thread(td, args));
403 return (linux_clone_proc(td, args));
407 linux_exit(struct thread *td, struct linux_exit_args *args)
409 struct linux_emuldata *em;
412 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
414 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
416 umtx_thread_exit(td);
418 linux_thread_detach(td);
421 * XXX. When the last two threads of a process
422 * exit via pthread_exit() try thr_exit() first.
425 exit1(td, args->rval, 0);
430 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
432 struct linux_emuldata *em;
435 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
437 em->child_clear_tid = args->tidptr;
439 td->td_retval[0] = em->em_tid;
441 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
442 em->em_tid, args->tidptr, td->td_retval[0]);
448 linux_thread_detach(struct thread *td)
450 struct linux_sys_futex_args cup;
451 struct linux_emuldata *em;
452 int *child_clear_tid;
456 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
458 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
460 release_futexes(td, em);
462 child_clear_tid = em->child_clear_tid;
464 if (child_clear_tid != NULL) {
466 LINUX_CTR2(thread_detach, "thread(%d) %p",
467 em->em_tid, child_clear_tid);
469 error = suword32(child_clear_tid, 0);
473 cup.uaddr = child_clear_tid;
474 cup.op = LINUX_FUTEX_WAKE;
475 cup.val = 1; /* wake one */
479 error = linux_sys_futex(td, &cup);
481 * this cannot happen at the moment and if this happens it
482 * probably means there is a user space bug
485 linux_msg(td, "futex stuff in thread_detach failed.");