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>
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_emul.h>
64 #include <compat/linux/linux_futex.h>
65 #include <compat/linux/linux_misc.h>
66 #include <compat/linux/linux_util.h>
68 #ifdef LINUX_LEGACY_SYSCALLS
70 linux_fork(struct thread *td, struct linux_fork_args *args)
77 bzero(&fr, sizeof(fr));
78 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
80 if ((error = fork1(td, &fr)) != 0)
83 td2 = FIRST_THREAD_IN_PROC(p2);
85 linux_proc_init(td, td2, 0);
87 td->td_retval[0] = p2->p_pid;
90 * Make this runnable after we are finished with it.
94 sched_add(td2, SRQ_BORING);
100 linux_vfork(struct thread *td, struct linux_vfork_args *args)
107 bzero(&fr, sizeof(fr));
108 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
110 if ((error = fork1(td, &fr)) != 0)
113 td2 = FIRST_THREAD_IN_PROC(p2);
115 linux_proc_init(td, td2, 0);
117 td->td_retval[0] = p2->p_pid;
120 * Make this runnable after we are finished with it.
124 sched_add(td2, SRQ_BORING);
131 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
134 int error, ff = RFPROC | RFSTOPPED;
138 struct linux_emuldata *em;
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)
146 if (args->flags & LINUX_CLONE_VM)
148 if (args->flags & LINUX_CLONE_SIGHAND)
151 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
152 * and open files is independent. In FreeBSD, its in one
153 * structure but in reality it does not cause any problems
154 * because both of these flags are usually set together.
156 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
159 if (args->flags & LINUX_CLONE_PARENT_SETTID)
160 if (args->parent_tidptr == NULL)
163 if (args->flags & LINUX_CLONE_VFORK)
166 bzero(&fr, sizeof(fr));
169 error = fork1(td, &fr);
173 td2 = FIRST_THREAD_IN_PROC(p2);
175 /* create the emuldata */
176 linux_proc_init(td, td2, args->flags);
179 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
181 if (args->flags & LINUX_CLONE_CHILD_SETTID)
182 em->child_set_tid = args->child_tidptr;
184 em->child_set_tid = NULL;
186 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
187 em->child_clear_tid = args->child_tidptr;
189 em->child_clear_tid = NULL;
191 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
192 error = copyout(&p2->p_pid, args->parent_tidptr,
195 linux_msg(td, "copyout p_pid failed!");
199 p2->p_sigparent = exit_signal;
202 * In a case of stack = NULL, we are supposed to COW calling process
203 * stack. This is what normal fork() does, so we just keep tf_rsp arg
206 linux_set_upcall_kse(td2, PTROUT(args->stack));
208 if (args->flags & LINUX_CLONE_SETTLS)
209 linux_set_cloned_tls(td2, args->tls);
212 * If CLONE_PARENT is set, then the parent of the new process will be
213 * the same as that of the calling process.
215 if (args->flags & LINUX_CLONE_PARENT) {
216 sx_xlock(&proctree_lock);
218 proc_reparent(p2, td->td_proc->p_pptr, true);
220 sx_xunlock(&proctree_lock);
224 * Make this runnable after we are finished with it.
228 sched_add(td2, SRQ_BORING);
230 td->td_retval[0] = p2->p_pid;
236 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
238 struct linux_emuldata *em;
239 struct thread *newtd;
243 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
244 td->td_tid, (unsigned)args->flags,
245 args->parent_tidptr, args->child_tidptr);
247 if (args->flags & LINUX_CLONE_PARENT_SETTID)
248 if (args->parent_tidptr == NULL)
251 /* Threads should be created with own stack */
252 if (args->stack == NULL)
260 error = racct_add(p, RACCT_NTHR, 1);
267 /* Initialize our td */
268 error = kern_thr_alloc(p, 0, &newtd);
272 cpu_copy_thread(newtd, td);
274 bzero(&newtd->td_startzero,
275 __rangeof(struct thread, td_startzero, td_endzero));
276 bcopy(&td->td_startcopy, &newtd->td_startcopy,
277 __rangeof(struct thread, td_startcopy, td_endcopy));
280 thread_cow_get(newtd, td);
282 /* create the emuldata */
283 linux_proc_init(td, newtd, args->flags);
286 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
288 if (args->flags & LINUX_CLONE_SETTLS)
289 linux_set_cloned_tls(newtd, args->tls);
291 if (args->flags & LINUX_CLONE_CHILD_SETTID)
292 em->child_set_tid = args->child_tidptr;
294 em->child_set_tid = NULL;
296 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
297 em->child_clear_tid = args->child_tidptr;
299 em->child_clear_tid = NULL;
301 cpu_thread_clean(newtd);
303 linux_set_upcall_kse(newtd, PTROUT(args->stack));
306 p->p_flag |= P_HADTHREADS;
307 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
309 if (args->flags & LINUX_CLONE_PARENT)
310 thread_link(newtd, p->p_pptr);
312 thread_link(newtd, p);
315 /* let the scheduler know about these things. */
316 sched_fork_thread(td, newtd);
319 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
321 if (p->p_ptevents & PTRACE_LWP)
322 newtd->td_dbgflags |= TDB_BORN;
327 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
328 td->td_tid, newtd->td_tid);
330 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
331 error = copyout(&newtd->td_tid, args->parent_tidptr,
332 sizeof(newtd->td_tid));
334 linux_msg(td, "clone_thread: copyout td_tid failed!");
338 * Make this runnable after we are finished with it.
341 TD_SET_CAN_RUN(newtd);
342 sched_add(newtd, SRQ_BORING);
344 td->td_retval[0] = newtd->td_tid;
352 racct_sub(p, RACCT_NTHR, 1);
360 linux_clone(struct thread *td, struct linux_clone_args *args)
363 if (args->flags & LINUX_CLONE_THREAD)
364 return (linux_clone_thread(td, args));
366 return (linux_clone_proc(td, args));
370 linux_exit(struct thread *td, struct linux_exit_args *args)
372 struct linux_emuldata *em;
375 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
377 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
379 umtx_thread_exit(td);
381 linux_thread_detach(td);
384 * XXX. When the last two threads of a process
385 * exit via pthread_exit() try thr_exit() first.
388 exit1(td, args->rval, 0);
393 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
395 struct linux_emuldata *em;
398 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
400 em->child_clear_tid = args->tidptr;
402 td->td_retval[0] = em->em_tid;
404 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
405 em->em_tid, args->tidptr, td->td_retval[0]);
411 linux_thread_detach(struct thread *td)
413 struct linux_sys_futex_args cup;
414 struct linux_emuldata *em;
415 int *child_clear_tid;
419 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
421 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
423 release_futexes(td, em);
425 child_clear_tid = em->child_clear_tid;
427 if (child_clear_tid != NULL) {
429 LINUX_CTR2(thread_detach, "thread(%d) %p",
430 em->em_tid, child_clear_tid);
432 error = suword32(child_clear_tid, 0);
436 cup.uaddr = child_clear_tid;
437 cup.op = LINUX_FUTEX_WAKE;
438 cup.val = 1; /* wake one */
442 error = linux_sys_futex(td, &cup);
444 * this cannot happen at the moment and if this happens it
445 * probably means there is a user space bug
448 linux_msg(td, "futex stuff in thread_detach failed.");