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>
39 #include <sys/mutex.h>
41 #include <sys/sched.h>
44 #include <sys/unistd.h>
47 #include <machine/../linux32/linux.h>
48 #include <machine/../linux32/linux32_proto.h>
50 #include <machine/../linux/linux.h>
51 #include <machine/../linux/linux_proto.h>
53 #include <compat/linux/linux_dtrace.h>
54 #include <compat/linux/linux_signal.h>
55 #include <compat/linux/linux_emul.h>
56 #include <compat/linux/linux_misc.h>
59 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
61 /* Linuxulator-global DTrace probes */
62 LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
63 LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
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 td->td_retval[0] = p2->p_pid;
85 error = linux_proc_init(td, td->td_retval[0], 0);
89 td2 = FIRST_THREAD_IN_PROC(p2);
92 * Make this runnable after we are finished with it.
96 sched_add(td2, SRQ_BORING);
103 linux_vfork(struct thread *td, struct linux_vfork_args *args)
111 printf(ARGS(vfork, ""));
114 /* Exclude RFPPWAIT */
115 if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2,
119 td->td_retval[0] = p2->p_pid;
121 error = linux_proc_init(td, td->td_retval[0], 0);
126 p2->p_flag |= P_PPWAIT;
129 td2 = FIRST_THREAD_IN_PROC(p2);
132 * Make this runnable after we are finished with it.
136 sched_add(td2, SRQ_BORING);
139 /* wait for the children to exit, ie. emulate vfork */
141 while (p2->p_flag & P_PPWAIT)
142 cv_wait(&p2->p_pwait, &p2->p_mtx);
149 linux_clone(struct thread *td, struct linux_clone_args *args)
151 int error, ff = RFPROC | RFSTOPPED;
155 struct linux_emuldata *em;
159 printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
160 "child tid: %p"), (unsigned)args->flags,
161 args->stack, args->parent_tidptr, args->child_tidptr);
165 exit_signal = args->flags & 0x000000ff;
166 if (LINUX_SIG_VALID(exit_signal)) {
167 if (exit_signal <= LINUX_SIGTBLSZ)
169 linux_to_bsd_signal[_SIG_IDX(exit_signal)];
170 } else if (exit_signal != 0)
173 if (args->flags & LINUX_CLONE_VM)
175 if (args->flags & LINUX_CLONE_SIGHAND)
178 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
179 * and open files is independant. In FreeBSD, its in one
180 * structure but in reality it does not cause any problems
181 * because both of these flags are usually set together.
183 if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
187 * Attempt to detect when linux_clone(2) is used for creating
188 * kernel threads. Unfortunately despite the existence of the
189 * CLONE_THREAD flag, version of linuxthreads package used in
190 * most popular distros as of beginning of 2005 doesn't make
191 * any use of it. Therefore, this detection relies on
192 * empirical observation that linuxthreads sets certain
193 * combination of flags, so that we can make more or less
194 * precise detection and notify the FreeBSD kernel that several
195 * processes are in fact part of the same threading group, so
196 * that special treatment is necessary for signal delivery
197 * between those processes and fd locking.
199 if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
202 if (args->flags & LINUX_CLONE_PARENT_SETTID)
203 if (args->parent_tidptr == NULL)
206 error = fork1(td, ff, 0, &p2, NULL, 0);
210 if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
211 sx_xlock(&proctree_lock);
213 proc_reparent(p2, td->td_proc->p_pptr);
215 sx_xunlock(&proctree_lock);
218 /* create the emuldata */
219 error = linux_proc_init(td, p2->p_pid, args->flags);
220 /* reference it - no need to check this */
221 em = em_find(p2, EMUL_DOLOCK);
222 KASSERT(em != NULL, ("clone: emuldata not found."));
225 if (args->flags & LINUX_CLONE_THREAD) {
228 p2->p_pgrp = td->td_proc->p_pgrp;
234 if (args->flags & LINUX_CLONE_CHILD_SETTID)
235 em->child_set_tid = args->child_tidptr;
237 em->child_set_tid = NULL;
239 if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
240 em->child_clear_tid = args->child_tidptr;
242 em->child_clear_tid = NULL;
244 EMUL_UNLOCK(&emul_lock);
246 if (args->flags & LINUX_CLONE_PARENT_SETTID) {
247 error = copyout(&p2->p_pid, args->parent_tidptr,
250 printf(LMSG("copyout failed!"));
254 p2->p_sigparent = exit_signal;
256 td2 = FIRST_THREAD_IN_PROC(p2);
258 * In a case of stack = NULL, we are supposed to COW calling process
259 * stack. This is what normal fork() does, so we just keep tf_rsp arg
263 linux_set_upcall_kse(td2, PTROUT(args->stack));
265 if (args->flags & LINUX_CLONE_SETTLS)
266 linux_set_cloned_tls(td2, args->tls);
270 printf(LMSG("clone: successful rfork to %d, "
271 "stack %p sig = %d"), (int)p2->p_pid, args->stack,
274 if (args->flags & LINUX_CLONE_VFORK) {
276 p2->p_flag |= P_PPWAIT;
281 * Make this runnable after we are finished with it.
285 sched_add(td2, SRQ_BORING);
288 td->td_retval[0] = p2->p_pid;
289 td->td_retval[1] = 0;
291 if (args->flags & LINUX_CLONE_VFORK) {
292 /* wait for the children to exit, ie. emulate vfork */
294 while (p2->p_flag & P_PPWAIT)
295 cv_wait(&p2->p_pwait, &p2->p_mtx);