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