2 * Copyright (c) 2004 Tim J. Robbins
3 * Copyright (c) 2002 Doug Rabson
4 * Copyright (c) 2000 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/imgact.h>
39 #include <sys/mutex.h>
41 #include <sys/racct.h>
42 #include <sys/sched.h>
43 #include <sys/syscallsubr.h>
46 #include <sys/unistd.h>
51 #include <vm/vm_map.h>
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
57 #include <machine/../linux/linux.h>
58 #include <machine/../linux/linux_proto.h>
60 #include <compat/linux/linux_emul.h>
61 #include <compat/linux/linux_futex.h>
62 #include <compat/linux/linux_misc.h>
63 #include <compat/linux/linux_util.h>
66 linux_fork(struct thread *td, struct linux_fork_args *args)
75 printf(ARGS(fork, ""));
78 bzero(&fr, sizeof(fr));
79 fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
81 if ((error = fork1(td, &fr)) != 0)
84 td2 = FIRST_THREAD_IN_PROC(p2);
86 linux_proc_init(td, td2, 0);
88 td->td_retval[0] = p2->p_pid;
91 * Make this runnable after we are finished with it.
95 sched_add(td2, SRQ_BORING);
102 linux_vfork(struct thread *td, struct linux_vfork_args *args)
111 printf(ARGS(vfork, ""));
114 bzero(&fr, sizeof(fr));
115 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
117 if ((error = fork1(td, &fr)) != 0)
120 td2 = FIRST_THREAD_IN_PROC(p2);
122 linux_proc_init(td, td2, 0);
124 td->td_retval[0] = p2->p_pid;
127 * Make this runnable after we are finished with it.
131 sched_add(td2, SRQ_BORING);
138 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
141 int error, ff = RFPROC | RFSTOPPED;
145 struct linux_emuldata *em;
149 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
150 "child tid: %p"), (unsigned)args->flags,
151 args->stack, args->parent_tidptr, args->child_tidptr);
155 exit_signal = args->flags & 0x000000ff;
156 if (LINUX_SIG_VALID(exit_signal)) {
157 exit_signal = linux_to_bsd_signal(exit_signal);
158 } else if (exit_signal != 0)
161 if (args->flags & LINUX_CLONE_VM)
163 if (args->flags & LINUX_CLONE_SIGHAND)
166 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
167 * and open files is independent. In FreeBSD, its in one
168 * structure but in reality it does not cause any problems
169 * because both of these flags are usually set together.
171 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
174 if (args->flags & LINUX_CLONE_PARENT_SETTID)
175 if (args->parent_tidptr == NULL)
178 if (args->flags & LINUX_CLONE_VFORK)
181 bzero(&fr, sizeof(fr));
184 error = fork1(td, &fr);
188 td2 = FIRST_THREAD_IN_PROC(p2);
190 /* create the emuldata */
191 linux_proc_init(td, td2, args->flags);
194 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
196 if (args->flags & LINUX_CLONE_CHILD_SETTID)
197 em->child_set_tid = args->child_tidptr;
199 em->child_set_tid = NULL;
201 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
202 em->child_clear_tid = args->child_tidptr;
204 em->child_clear_tid = NULL;
206 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
207 error = copyout(&p2->p_pid, args->parent_tidptr,
210 printf(LMSG("copyout failed!"));
214 p2->p_sigparent = exit_signal;
217 * In a case of stack = NULL, we are supposed to COW calling process
218 * stack. This is what normal fork() does, so we just keep tf_rsp arg
221 linux_set_upcall_kse(td2, PTROUT(args->stack));
223 if (args->flags & LINUX_CLONE_SETTLS)
224 linux_set_cloned_tls(td2, args->tls);
227 * If CLONE_PARENT is set, then the parent of the new process will be
228 * the same as that of the calling process.
230 if (args->flags & LINUX_CLONE_PARENT) {
231 sx_xlock(&proctree_lock);
233 proc_reparent(p2, td->td_proc->p_pptr);
235 sx_xunlock(&proctree_lock);
240 printf(LMSG("clone: successful rfork to %d, "
241 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
246 * Make this runnable after we are finished with it.
250 sched_add(td2, SRQ_BORING);
253 td->td_retval[0] = p2->p_pid;
259 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
261 struct linux_emuldata *em;
262 struct thread *newtd;
268 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
269 "child tid: %p"), (unsigned)args->flags,
270 args->stack, args->parent_tidptr, args->child_tidptr);
274 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
275 td->td_tid, (unsigned)args->flags,
276 args->parent_tidptr, args->child_tidptr);
278 if (args->flags & LINUX_CLONE_PARENT_SETTID)
279 if (args->parent_tidptr == NULL)
282 /* Threads should be created with own stack */
283 if (args->stack == NULL)
291 error = racct_add(p, RACCT_NTHR, 1);
298 /* Initialize our td */
299 error = kern_thr_alloc(p, 0, &newtd);
303 cpu_set_upcall(newtd, td);
305 bzero(&newtd->td_startzero,
306 __rangeof(struct thread, td_startzero, td_endzero));
307 bcopy(&td->td_startcopy, &newtd->td_startcopy,
308 __rangeof(struct thread, td_startcopy, td_endcopy));
311 thread_cow_get(newtd, td);
313 /* create the emuldata */
314 linux_proc_init(td, newtd, args->flags);
317 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
319 if (args->flags & LINUX_CLONE_SETTLS)
320 linux_set_cloned_tls(newtd, args->tls);
322 if (args->flags & LINUX_CLONE_CHILD_SETTID)
323 em->child_set_tid = args->child_tidptr;
325 em->child_set_tid = NULL;
327 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
328 em->child_clear_tid = args->child_tidptr;
330 em->child_clear_tid = NULL;
332 cpu_thread_clean(newtd);
334 linux_set_upcall_kse(newtd, PTROUT(args->stack));
337 p->p_flag |= P_HADTHREADS;
338 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
340 if (args->flags & LINUX_CLONE_PARENT)
341 thread_link(newtd, p->p_pptr);
343 thread_link(newtd, p);
346 /* let the scheduler know about these things. */
347 sched_fork_thread(td, newtd);
350 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
357 printf(ARGS(clone, "successful clone to %d, stack %p"),
358 (int)newtd->td_tid, args->stack);
361 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
362 td->td_tid, newtd->td_tid);
364 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
365 error = copyout(&newtd->td_tid, args->parent_tidptr,
366 sizeof(newtd->td_tid));
368 printf(LMSG("clone_thread: copyout failed!"));
372 * Make this runnable after we are finished with it.
375 TD_SET_CAN_RUN(newtd);
376 sched_add(newtd, SRQ_BORING);
377 thread_unlock(newtd);
379 td->td_retval[0] = newtd->td_tid;
387 racct_sub(p, RACCT_NTHR, 1);
395 linux_clone(struct thread *td, struct linux_clone_args *args)
398 if (args->flags & LINUX_CLONE_THREAD)
399 return (linux_clone_thread(td, args));
401 return (linux_clone_proc(td, args));
405 linux_exit(struct thread *td, struct linux_exit_args *args)
407 struct linux_emuldata *em;
410 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
412 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
414 umtx_thread_exit(td);
416 linux_thread_detach(td);
419 * XXX. When the last two threads of a process
420 * exit via pthread_exit() try thr_exit() first.
423 exit1(td, args->rval, 0);
428 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
430 struct linux_emuldata *em;
433 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
435 em->child_clear_tid = args->tidptr;
437 td->td_retval[0] = em->em_tid;
439 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
440 em->em_tid, args->tidptr, td->td_retval[0]);
446 linux_thread_detach(struct thread *td)
448 struct linux_sys_futex_args cup;
449 struct linux_emuldata *em;
450 int *child_clear_tid;
454 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
456 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
458 release_futexes(td, em);
460 child_clear_tid = em->child_clear_tid;
462 if (child_clear_tid != NULL) {
464 LINUX_CTR2(thread_detach, "thread(%d) %p",
465 em->em_tid, child_clear_tid);
467 error = suword32(child_clear_tid, 0);
471 cup.uaddr = child_clear_tid;
472 cup.op = LINUX_FUTEX_WAKE;
473 cup.val = 1; /* wake one */
477 error = linux_sys_futex(td, &cup);
479 * this cannot happen at the moment and if this happens it
480 * probably means there is a user space bug
483 linux_msg(td, "futex stuff in thread_detach failed.");