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"
33 #include "opt_kdtrace.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
40 #include <sys/mutex.h>
42 #include <sys/racct.h>
43 #include <sys/sched.h>
44 #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_signal.h>
61 #include <compat/linux/linux_emul.h>
62 #include <compat/linux/linux_futex.h>
63 #include <compat/linux/linux_misc.h>
64 #include <compat/linux/linux_util.h>
67 linux_fork(struct thread *td, struct linux_fork_args *args)
75 printf(ARGS(fork, ""));
78 if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0))
82 td2 = FIRST_THREAD_IN_PROC(p2);
84 linux_proc_init(td, td2, 0);
86 td->td_retval[0] = p2->p_pid;
89 * Make this runnable after we are finished with it.
93 sched_add(td2, SRQ_BORING);
100 linux_vfork(struct thread *td, struct linux_vfork_args *args)
108 printf(ARGS(vfork, ""));
111 /* Exclude RFPPWAIT */
112 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2,
117 td2 = FIRST_THREAD_IN_PROC(p2);
119 linux_proc_init(td, td2, 0);
122 p2->p_flag |= P_PPWAIT;
125 td->td_retval[0] = p2->p_pid;
128 * Make this runnable after we are finished with it.
132 sched_add(td2, SRQ_BORING);
135 /* wait for the children to exit, ie. emulate vfork */
137 while (p2->p_flag & P_PPWAIT)
138 cv_wait(&p2->p_pwait, &p2->p_mtx);
145 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
147 int error, ff = RFPROC | RFSTOPPED;
151 struct linux_emuldata *em;
155 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
156 "child tid: %p"), (unsigned)args->flags,
157 args->stack, args->parent_tidptr, args->child_tidptr);
161 exit_signal = args->flags & 0x000000ff;
162 if (LINUX_SIG_VALID(exit_signal)) {
163 if (exit_signal <= LINUX_SIGTBLSZ)
165 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
166 } else if (exit_signal != 0)
169 if (args->flags & LINUX_CLONE_VM)
171 if (args->flags & LINUX_CLONE_SIGHAND)
174 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
175 * and open files is independant. In FreeBSD, its in one
176 * structure but in reality it does not cause any problems
177 * because both of these flags are usually set together.
179 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
182 if (args->flags & LINUX_CLONE_PARENT_SETTID)
183 if (args->parent_tidptr == NULL)
186 error = fork1(td, ff, 0, &p2, NULL, 0);
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);
230 printf(LMSG("clone: successful rfork to %d, "
231 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
235 if (args->flags & LINUX_CLONE_VFORK) {
237 p2->p_flag |= P_PPWAIT;
242 * Make this runnable after we are finished with it.
246 sched_add(td2, SRQ_BORING);
249 td->td_retval[0] = p2->p_pid;
251 if (args->flags & LINUX_CLONE_VFORK) {
252 /* wait for the children to exit, ie. emulate vfork */
254 while (p2->p_flag & P_PPWAIT)
255 cv_wait(&p2->p_pwait, &p2->p_mtx);
263 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
265 struct linux_emuldata *em;
266 struct thread *newtd;
272 printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
273 "child tid: %p"), (unsigned)args->flags,
274 args->stack, args->parent_tidptr, args->child_tidptr);
278 LINUX_CTR4(clone, "thread(%d) flags %x ptid %p ctid %p",
279 td->td_tid, (unsigned)args->flags,
280 args->parent_tidptr, args->child_tidptr);
282 if (args->flags & LINUX_CLONE_PARENT_SETTID)
283 if (args->parent_tidptr == NULL)
286 /* Threads should be created with own stack */
287 if (args->stack == NULL)
292 /* Initialize our td */
293 error = kern_thr_alloc(p, 0, &newtd);
297 cpu_set_upcall(newtd, td);
299 bzero(&newtd->td_startzero,
300 __rangeof(struct thread, td_startzero, td_endzero));
301 bcopy(&td->td_startcopy, &newtd->td_startcopy,
302 __rangeof(struct thread, td_startcopy, td_endcopy));
305 newtd->td_ucred = crhold(td->td_ucred);
307 /* create the emuldata */
308 linux_proc_init(td, newtd, args->flags);
311 KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
313 if (args->flags & LINUX_CLONE_SETTLS)
314 linux_set_cloned_tls(newtd, args->tls);
316 if (args->flags & LINUX_CLONE_CHILD_SETTID)
317 em->child_set_tid = args->child_tidptr;
319 em->child_set_tid = NULL;
321 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
322 em->child_clear_tid = args->child_tidptr;
324 em->child_clear_tid = NULL;
326 cpu_thread_clean(newtd);
328 linux_set_upcall_kse(newtd, PTROUT(args->stack));
331 p->p_flag |= P_HADTHREADS;
332 newtd->td_sigmask = td->td_sigmask;
333 bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
335 if (args->flags & LINUX_CLONE_PARENT)
336 thread_link(newtd, p->p_pptr);
338 thread_link(newtd, p);
341 /* let the scheduler know about these things. */
342 sched_fork_thread(td, newtd);
345 newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
352 printf(ARGS(clone, "successful clone to %d, stack %p"),
353 (int)newtd->td_tid, args->stack);
356 LINUX_CTR2(clone, "thread(%d) successful clone to %d",
357 td->td_tid, newtd->td_tid);
359 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
360 error = copyout(&newtd->td_tid, args->parent_tidptr,
361 sizeof(newtd->td_tid));
363 printf(LMSG("clone_thread: copyout failed!"));
367 * Make this runnable after we are finished with it.
370 TD_SET_CAN_RUN(newtd);
371 sched_add(newtd, SRQ_BORING);
372 thread_unlock(newtd);
374 td->td_retval[0] = newtd->td_tid;
380 linux_clone(struct thread *td, struct linux_clone_args *args)
383 if (args->flags & LINUX_CLONE_THREAD)
384 return (linux_clone_thread(td, args));
386 return (linux_clone_proc(td, args));
390 linux_exit(struct thread *td, struct linux_exit_args *args)
392 struct linux_emuldata *em;
395 KASSERT(em != NULL, ("exit: emuldata not found.\n"));
397 LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
399 linux_thread_detach(td);
402 * XXX. When the last two threads of a process
403 * exit via pthread_exit() try thr_exit() first.
406 exit1(td, W_EXITCODE(args->rval, 0));
411 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
413 struct linux_emuldata *em;
416 KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
418 em->child_clear_tid = args->tidptr;
420 td->td_retval[0] = em->em_tid;
422 LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
423 em->em_tid, args->tidptr, td->td_retval[0]);
429 linux_thread_detach(struct thread *td)
431 struct linux_sys_futex_args cup;
432 struct linux_emuldata *em;
433 int *child_clear_tid;
437 KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
439 LINUX_CTR1(exit, "thread detach(%d)", em->em_tid);
441 release_futexes(td, em);
443 child_clear_tid = em->child_clear_tid;
445 if (child_clear_tid != NULL) {
447 LINUX_CTR2(exit, "thread detach(%d) %p",
448 em->em_tid, child_clear_tid);
450 error = suword32(child_clear_tid, 0);
454 cup.uaddr = child_clear_tid;
455 cup.op = LINUX_FUTEX_WAKE;
456 cup.val = 1; /* wake one */
460 error = linux_sys_futex(td, &cup);
462 * this cannot happen at the moment and if this happens it
463 * probably means there is a user space bug
466 linux_msg(td, "futex stuff in thread_detach failed.");