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>
45 #include <sys/unistd.h>
50 #include <vm/vm_map.h>
53 #include <machine/../linux32/linux.h>
54 #include <machine/../linux32/linux32_proto.h>
56 #include <machine/../linux/linux.h>
57 #include <machine/../linux/linux_proto.h>
59 #include <compat/linux/linux_emul.h>
60 #include <compat/linux/linux_futex.h>
61 #include <compat/linux/linux_misc.h>
62 #include <compat/linux/linux_util.h>
65 linux_fork(struct thread *td, struct linux_fork_args *args)
74 printf(ARGS(fork, ""));
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);
101 linux_vfork(struct thread *td, struct linux_vfork_args *args)
110 printf(ARGS(vfork, ""));
113 bzero(&fr, sizeof(fr));
114 fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
116 if ((error = fork1(td, &fr)) != 0)
119 td2 = FIRST_THREAD_IN_PROC(p2);
121 linux_proc_init(td, td2, 0);
123 td->td_retval[0] = p2->p_pid;
126 * Make this runnable after we are finished with it.
130 sched_add(td2, SRQ_BORING);
137 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
140 int error, ff = RFPROC | RFSTOPPED;
144 struct linux_emuldata *em;
148 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
149 "child tid: %p"), (unsigned)args->flags,
150 args->stack, args->parent_tidptr, args->child_tidptr);
154 exit_signal = args->flags & 0x000000ff;
155 if (LINUX_SIG_VALID(exit_signal)) {
156 exit_signal = linux_to_bsd_signal(exit_signal);
157 } else if (exit_signal != 0)
160 if (args->flags & LINUX_CLONE_VM)
162 if (args->flags & LINUX_CLONE_SIGHAND)
165 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
166 * and open files is independant. In FreeBSD, its in one
167 * structure but in reality it does not cause any problems
168 * because both of these flags are usually set together.
170 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
173 if (args->flags & LINUX_CLONE_PARENT_SETTID)
174 if (args->parent_tidptr == NULL)
177 if (args->flags & LINUX_CLONE_VFORK)
180 bzero(&fr, sizeof(fr));
183 error = fork1(td, &fr);
187 td2 = FIRST_THREAD_IN_PROC(p2);
189 /* create the emuldata */
190 linux_proc_init(td, td2, args->flags);
193 KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
195 if (args->flags & LINUX_CLONE_CHILD_SETTID)
196 em->child_set_tid = args->child_tidptr;
198 em->child_set_tid = NULL;
200 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
201 em->child_clear_tid = args->child_tidptr;
203 em->child_clear_tid = NULL;
205 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
206 error = copyout(&p2->p_pid, args->parent_tidptr,
209 printf(LMSG("copyout failed!"));
213 p2->p_sigparent = exit_signal;
216 * In a case of stack = NULL, we are supposed to COW calling process
217 * stack. This is what normal fork() does, so we just keep tf_rsp arg
220 linux_set_upcall_kse(td2, PTROUT(args->stack));
222 if (args->flags & LINUX_CLONE_SETTLS)
223 linux_set_cloned_tls(td2, args->tls);
226 * If CLONE_PARENT is set, then the parent of the new process will be
227 * the same as that of the calling process.
229 if (args->flags & LINUX_CLONE_PARENT) {
230 sx_xlock(&proctree_lock);
232 proc_reparent(p2, td->td_proc->p_pptr);
234 sx_xunlock(&proctree_lock);
239 printf(LMSG("clone: successful rfork to %d, "
240 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
245 * Make this runnable after we are finished with it.
249 sched_add(td2, SRQ_BORING);
252 td->td_retval[0] = p2->p_pid;
258 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
260 struct linux_emuldata *em;
261 struct thread *newtd;
267 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
268 "child tid: %p"), (unsigned)args->flags,
269 args->stack, args->parent_tidptr, args->child_tidptr);
273 LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
274 td->td_tid, (unsigned)args->flags,
275 args->parent_tidptr, args->child_tidptr);
277 if (args->flags & LINUX_CLONE_PARENT_SETTID)
278 if (args->parent_tidptr == NULL)
281 /* Threads should be created with own stack */
282 if (args->stack == NULL)
290 error = racct_add(p, RACCT_NTHR, 1);
297 /* Initialize our td */
298 error = kern_thr_alloc(p, 0, &newtd);
302 cpu_set_upcall(newtd, td);
304 bzero(&newtd->td_startzero,
305 __rangeof(struct thread, td_startzero, td_endzero));
306 bcopy(&td->td_startcopy, &newtd->td_startcopy,
307 __rangeof(struct thread, td_startcopy, td_endcopy));
310 thread_cow_get(newtd, td);
312 /* create the emuldata */
313 linux_proc_init(td, newtd, args->flags);
316 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
318 if (args->flags & LINUX_CLONE_SETTLS)
319 linux_set_cloned_tls(newtd, args->tls);
321 if (args->flags & LINUX_CLONE_CHILD_SETTID)
322 em->child_set_tid = args->child_tidptr;
324 em->child_set_tid = NULL;
326 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
327 em->child_clear_tid = args->child_tidptr;
329 em->child_clear_tid = NULL;
331 cpu_thread_clean(newtd);
333 linux_set_upcall_kse(newtd, PTROUT(args->stack));
336 p->p_flag |= P_HADTHREADS;
337 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
339 if (args->flags & LINUX_CLONE_PARENT)
340 thread_link(newtd, p->p_pptr);
342 thread_link(newtd, p);
345 /* let the scheduler know about these things. */
346 sched_fork_thread(td, newtd);
349 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
356 printf(ARGS(clone, "successful clone to %d, stack %p"),
357 (int)newtd->td_tid, args->stack);
360 LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
361 td->td_tid, newtd->td_tid);
363 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
364 error = copyout(&newtd->td_tid, args->parent_tidptr,
365 sizeof(newtd->td_tid));
367 printf(LMSG("clone_thread: copyout failed!"));
371 * Make this runnable after we are finished with it.
374 TD_SET_CAN_RUN(newtd);
375 sched_add(newtd, SRQ_BORING);
376 thread_unlock(newtd);
378 td->td_retval[0] = newtd->td_tid;
386 racct_sub(p, RACCT_NTHR, 1);
394 linux_clone(struct thread *td, struct linux_clone_args *args)
397 if (args->flags & LINUX_CLONE_THREAD)
398 return (linux_clone_thread(td, args));
400 return (linux_clone_proc(td, args));
404 linux_exit(struct thread *td, struct linux_exit_args *args)
406 struct linux_emuldata *em;
409 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
411 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
413 linux_thread_detach(td);
416 * XXX. When the last two threads of a process
417 * exit via pthread_exit() try thr_exit() first.
420 exit1(td, args->rval, 0);
425 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
427 struct linux_emuldata *em;
430 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
432 em->child_clear_tid = args->tidptr;
434 td->td_retval[0] = em->em_tid;
436 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
437 em->em_tid, args->tidptr, td->td_retval[0]);
443 linux_thread_detach(struct thread *td)
445 struct linux_sys_futex_args cup;
446 struct linux_emuldata *em;
447 int *child_clear_tid;
451 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
453 LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
455 release_futexes(td, em);
457 child_clear_tid = em->child_clear_tid;
459 if (child_clear_tid != NULL) {
461 LINUX_CTR2(thread_detach, "thread(%d) %p",
462 em->em_tid, child_clear_tid);
464 error = suword32(child_clear_tid, 0);
468 cup.uaddr = child_clear_tid;
469 cup.op = LINUX_FUTEX_WAKE;
470 cup.val = 1; /* wake one */
474 error = linux_sys_futex(td, &cup);
476 * this cannot happen at the moment and if this happens it
477 * probably means there is a user space bug
480 linux_msg(td, "futex stuff in thread_detach failed.");