linux_fork.c revision 289672
1/*-
2 * Copyright (c) 2004 Tim J. Robbins
3 * Copyright (c) 2002 Doug Rabson
4 * Copyright (c) 2000 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
16 *
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.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/compat/linux/linux_fork.c 289672 2015-10-21 01:41:22Z bdrewery $");
31
32#include "opt_compat.h"
33#include "opt_kdtrace.h"
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/imgact.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/proc.h>
41#include <sys/sched.h>
42#include <sys/sdt.h>
43#include <sys/sx.h>
44#include <sys/unistd.h>
45
46#ifdef COMPAT_LINUX32
47#include <machine/../linux32/linux.h>
48#include <machine/../linux32/linux32_proto.h>
49#else
50#include <machine/../linux/linux.h>
51#include <machine/../linux/linux_proto.h>
52#endif
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>
57
58/* DTrace init */
59LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
60
61/* Linuxulator-global DTrace probes */
62LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
63LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
64
65
66int
67linux_fork(struct thread *td, struct linux_fork_args *args)
68{
69	int error;
70	struct proc *p2;
71	struct thread *td2;
72
73#ifdef DEBUG
74	if (ldebug(fork))
75		printf(ARGS(fork, ""));
76#endif
77
78	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0))
79	    != 0)
80		return (error);
81
82	td->td_retval[0] = p2->p_pid;
83	td->td_retval[1] = 0;
84
85	error = linux_proc_init(td, td->td_retval[0], 0);
86	if (error)
87		return (error);
88
89	td2 = FIRST_THREAD_IN_PROC(p2);
90
91	/*
92	 * Make this runnable after we are finished with it.
93	 */
94	thread_lock(td2);
95	TD_SET_CAN_RUN(td2);
96	sched_add(td2, SRQ_BORING);
97	thread_unlock(td2);
98
99	return (0);
100}
101
102int
103linux_vfork(struct thread *td, struct linux_vfork_args *args)
104{
105	int error;
106	struct proc *p2;
107	struct thread *td2;
108
109#ifdef DEBUG
110	if (ldebug(vfork))
111		printf(ARGS(vfork, ""));
112#endif
113
114	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED,
115	    0, &p2, NULL, 0)) != 0)
116		return (error);
117
118   	td->td_retval[0] = p2->p_pid;
119
120	error = linux_proc_init(td, td->td_retval[0], 0);
121	if (error)
122		return (error);
123
124	td2 = FIRST_THREAD_IN_PROC(p2);
125
126	/*
127	 * Make this runnable after we are finished with it.
128	 */
129	thread_lock(td2);
130	TD_SET_CAN_RUN(td2);
131	sched_add(td2, SRQ_BORING);
132	thread_unlock(td2);
133
134	return (0);
135}
136
137int
138linux_clone(struct thread *td, struct linux_clone_args *args)
139{
140	int error, ff = RFPROC | RFSTOPPED;
141	struct proc *p2;
142	struct thread *td2;
143	int exit_signal;
144	struct linux_emuldata *em;
145
146#ifdef DEBUG
147	if (ldebug(clone)) {
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);
151	}
152#endif
153
154	exit_signal = args->flags & 0x000000ff;
155	if (LINUX_SIG_VALID(exit_signal)) {
156		if (exit_signal <= LINUX_SIGTBLSZ)
157			exit_signal =
158			    linux_to_bsd_signal[_SIG_IDX(exit_signal)];
159	} else if (exit_signal != 0)
160		return (EINVAL);
161
162	if (args->flags & LINUX_CLONE_VM)
163		ff |= RFMEM;
164	if (args->flags & LINUX_CLONE_SIGHAND)
165		ff |= RFSIGSHARE;
166	/*
167	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
168	 * and open files is independant.  In FreeBSD, its in one
169	 * structure but in reality it does not cause any problems
170	 * because both of these flags are usually set together.
171	 */
172	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
173		ff |= RFFDG;
174
175	/*
176	 * Attempt to detect when linux_clone(2) is used for creating
177	 * kernel threads. Unfortunately despite the existence of the
178	 * CLONE_THREAD flag, version of linuxthreads package used in
179	 * most popular distros as of beginning of 2005 doesn't make
180	 * any use of it. Therefore, this detection relies on
181	 * empirical observation that linuxthreads sets certain
182	 * combination of flags, so that we can make more or less
183	 * precise detection and notify the FreeBSD kernel that several
184	 * processes are in fact part of the same threading group, so
185	 * that special treatment is necessary for signal delivery
186	 * between those processes and fd locking.
187	 */
188	if ((args->flags & 0xffffff00) == LINUX_THREADING_FLAGS)
189		ff |= RFTHREAD;
190
191	if (args->flags & LINUX_CLONE_PARENT_SETTID)
192		if (args->parent_tidptr == NULL)
193			return (EINVAL);
194
195	if (args->flags & LINUX_CLONE_VFORK)
196		ff |= RFPPWAIT;
197
198	error = fork1(td, ff, 0, &p2, NULL, 0);
199	if (error)
200		return (error);
201
202	if (args->flags & (LINUX_CLONE_PARENT | LINUX_CLONE_THREAD)) {
203	   	sx_xlock(&proctree_lock);
204		PROC_LOCK(p2);
205		proc_reparent(p2, td->td_proc->p_pptr);
206		PROC_UNLOCK(p2);
207		sx_xunlock(&proctree_lock);
208	}
209
210	/* create the emuldata */
211	error = linux_proc_init(td, p2->p_pid, args->flags);
212	/* reference it - no need to check this */
213	em = em_find(p2, EMUL_DOLOCK);
214	KASSERT(em != NULL, ("clone: emuldata not found."));
215	/* and adjust it */
216
217	if (args->flags & LINUX_CLONE_THREAD) {
218#ifdef notyet
219	   	PROC_LOCK(p2);
220	   	p2->p_pgrp = td->td_proc->p_pgrp;
221	   	PROC_UNLOCK(p2);
222#endif
223		exit_signal = 0;
224	}
225
226	if (args->flags & LINUX_CLONE_CHILD_SETTID)
227		em->child_set_tid = args->child_tidptr;
228	else
229	   	em->child_set_tid = NULL;
230
231	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
232		em->child_clear_tid = args->child_tidptr;
233	else
234	   	em->child_clear_tid = NULL;
235
236	EMUL_UNLOCK(&emul_lock);
237
238	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
239		error = copyout(&p2->p_pid, args->parent_tidptr,
240		    sizeof(p2->p_pid));
241		if (error)
242			printf(LMSG("copyout failed!"));
243	}
244
245	PROC_LOCK(p2);
246	p2->p_sigparent = exit_signal;
247	PROC_UNLOCK(p2);
248	td2 = FIRST_THREAD_IN_PROC(p2);
249	/*
250	 * In a case of stack = NULL, we are supposed to COW calling process
251	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
252	 * intact.
253	 */
254	if (args->stack)
255		linux_set_upcall_kse(td2, PTROUT(args->stack));
256
257	if (args->flags & LINUX_CLONE_SETTLS)
258		linux_set_cloned_tls(td2, args->tls);
259
260#ifdef DEBUG
261	if (ldebug(clone))
262		printf(LMSG("clone: successful rfork to %d, "
263		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
264		    exit_signal);
265#endif
266	/*
267	 * Make this runnable after we are finished with it.
268	 */
269	thread_lock(td2);
270	TD_SET_CAN_RUN(td2);
271	sched_add(td2, SRQ_BORING);
272	thread_unlock(td2);
273
274	td->td_retval[0] = p2->p_pid;
275	td->td_retval[1] = 0;
276
277	return (0);
278}
279