xref: /freebsd/sys/compat/linux/linux_fork.c (revision ec513841b3ac59ce581ffb5eb859926555f6834b)
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/ktr.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/racct.h>
42 #include <sys/sched.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/wait.h>
47 
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50 #include <vm/vm_map.h>
51 
52 #ifdef COMPAT_LINUX32
53 #include <machine/../linux32/linux.h>
54 #include <machine/../linux32/linux32_proto.h>
55 #else
56 #include <machine/../linux/linux.h>
57 #include <machine/../linux/linux_proto.h>
58 #endif
59 #include <compat/linux/linux_signal.h>
60 #include <compat/linux/linux_emul.h>
61 #include <compat/linux/linux_misc.h>
62 #include <compat/linux/linux_util.h>
63 
64 int
65 linux_fork(struct thread *td, struct linux_fork_args *args)
66 {
67 	int error;
68 	struct proc *p2;
69 	struct thread *td2;
70 
71 #ifdef DEBUG
72 	if (ldebug(fork))
73 		printf(ARGS(fork, ""));
74 #endif
75 
76 	if ((error = fork1(td, RFFDG | RFPROC | RFSTOPPED, 0, &p2, NULL, 0))
77 	    != 0)
78 		return (error);
79 
80 	td2 = FIRST_THREAD_IN_PROC(p2);
81 
82 	linux_proc_init(td, td2, 0);
83 
84 	td->td_retval[0] = p2->p_pid;
85 
86 	/*
87 	 * Make this runnable after we are finished with it.
88 	 */
89 	thread_lock(td2);
90 	TD_SET_CAN_RUN(td2);
91 	sched_add(td2, SRQ_BORING);
92 	thread_unlock(td2);
93 
94 	return (0);
95 }
96 
97 int
98 linux_vfork(struct thread *td, struct linux_vfork_args *args)
99 {
100 	int error;
101 	struct proc *p2;
102 	struct thread *td2;
103 
104 #ifdef DEBUG
105 	if (ldebug(vfork))
106 		printf(ARGS(vfork, ""));
107 #endif
108 
109 	/* Exclude RFPPWAIT */
110 	if ((error = fork1(td, RFFDG | RFPROC | RFMEM | RFSTOPPED, 0, &p2,
111 	    NULL, 0)) != 0)
112 		return (error);
113 
114 
115 	td2 = FIRST_THREAD_IN_PROC(p2);
116 
117 	linux_proc_init(td, td2, 0);
118 
119 	PROC_LOCK(p2);
120 	p2->p_flag |= P_PPWAIT;
121 	PROC_UNLOCK(p2);
122 
123    	td->td_retval[0] = p2->p_pid;
124 
125 	/*
126 	 * Make this runnable after we are finished with it.
127 	 */
128 	thread_lock(td2);
129 	TD_SET_CAN_RUN(td2);
130 	sched_add(td2, SRQ_BORING);
131 	thread_unlock(td2);
132 
133 	/* wait for the children to exit, ie. emulate vfork */
134 	PROC_LOCK(p2);
135 	while (p2->p_flag & P_PPWAIT)
136 		cv_wait(&p2->p_pwait, &p2->p_mtx);
137 	PROC_UNLOCK(p2);
138 
139 	return (0);
140 }
141 
142 static int
143 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
144 {
145 	int error, ff = RFPROC | RFSTOPPED;
146 	struct proc *p2;
147 	struct thread *td2;
148 	int exit_signal;
149 	struct linux_emuldata *em;
150 
151 #ifdef DEBUG
152 	if (ldebug(clone)) {
153 		printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
154 		    "child tid: %p"), (unsigned)args->flags,
155 		    args->stack, args->parent_tidptr, args->child_tidptr);
156 	}
157 #endif
158 
159 	exit_signal = args->flags & 0x000000ff;
160 	if (LINUX_SIG_VALID(exit_signal)) {
161 		if (exit_signal <= LINUX_SIGTBLSZ)
162 			exit_signal =
163 			    linux_to_bsd_signal[_SIG_IDX(exit_signal)];
164 	} else if (exit_signal != 0)
165 		return (EINVAL);
166 
167 	if (args->flags & LINUX_CLONE_VM)
168 		ff |= RFMEM;
169 	if (args->flags & LINUX_CLONE_SIGHAND)
170 		ff |= RFSIGSHARE;
171 	/*
172 	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
173 	 * and open files is independant.  In FreeBSD, its in one
174 	 * structure but in reality it does not cause any problems
175 	 * because both of these flags are usually set together.
176 	 */
177 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
178 		ff |= RFFDG;
179 
180 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
181 		if (args->parent_tidptr == NULL)
182 			return (EINVAL);
183 
184 	error = fork1(td, ff, 0, &p2, NULL, 0);
185 	if (error)
186 		return (error);
187 
188 	td2 = FIRST_THREAD_IN_PROC(p2);
189 
190 	/* create the emuldata */
191 	linux_proc_init(td, td2, args->flags);
192 
193 	em = em_find(td2);
194 	KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
195 
196 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
197 		em->child_set_tid = args->child_tidptr;
198 	else
199 	   	em->child_set_tid = NULL;
200 
201 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
202 		em->child_clear_tid = args->child_tidptr;
203 	else
204 	   	em->child_clear_tid = NULL;
205 
206 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
207 		error = copyout(&p2->p_pid, args->parent_tidptr,
208 		    sizeof(p2->p_pid));
209 		if (error)
210 			printf(LMSG("copyout failed!"));
211 	}
212 
213 	PROC_LOCK(p2);
214 	p2->p_sigparent = exit_signal;
215 	PROC_UNLOCK(p2);
216 	/*
217 	 * In a case of stack = NULL, we are supposed to COW calling process
218 	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
219 	 * intact.
220 	 */
221 	linux_set_upcall_kse(td2, PTROUT(args->stack));
222 
223 	if (args->flags & LINUX_CLONE_SETTLS)
224 		linux_set_cloned_tls(td2, args->tls);
225 
226 #ifdef DEBUG
227 	if (ldebug(clone))
228 		printf(LMSG("clone: successful rfork to %d, "
229 		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
230 		    exit_signal);
231 #endif
232 
233 	if (args->flags & LINUX_CLONE_VFORK) {
234 	   	PROC_LOCK(p2);
235 	   	p2->p_flag |= P_PPWAIT;
236 	   	PROC_UNLOCK(p2);
237 	}
238 
239 	/*
240 	 * Make this runnable after we are finished with it.
241 	 */
242 	thread_lock(td2);
243 	TD_SET_CAN_RUN(td2);
244 	sched_add(td2, SRQ_BORING);
245 	thread_unlock(td2);
246 
247 	td->td_retval[0] = p2->p_pid;
248 
249 	if (args->flags & LINUX_CLONE_VFORK) {
250 		/* wait for the children to exit, ie. emulate vfork */
251 		PROC_LOCK(p2);
252 		while (p2->p_flag & P_PPWAIT)
253 			cv_wait(&p2->p_pwait, &p2->p_mtx);
254 		PROC_UNLOCK(p2);
255 	}
256 
257 	return (0);
258 }
259 
260 static int
261 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
262 {
263 	struct linux_emuldata *em;
264 	struct thread *newtd;
265 	struct proc *p;
266 	int error;
267 
268 #ifdef DEBUG
269 	if (ldebug(clone)) {
270 		printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
271 		    "child tid: %p"), (unsigned)args->flags,
272 		    args->stack, args->parent_tidptr, args->child_tidptr);
273 	}
274 #endif
275 
276 	LINUX_CTR4(clone, "thread(%d) flags %x ptid %p ctid %p",
277 	    td->td_tid, (unsigned)args->flags,
278 	    args->parent_tidptr, args->child_tidptr);
279 
280 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
281 		if (args->parent_tidptr == NULL)
282 			return (EINVAL);
283 
284 	/* Threads should be created with own stack */
285 	if (args->stack == NULL)
286 		return (EINVAL);
287 
288 	p = td->td_proc;
289 
290 	/* Initialize our td */
291 	error = kern_thr_alloc(p, 0, &newtd);
292 	if (error)
293 		return (error);
294 
295 	cpu_set_upcall(newtd, td);
296 
297 	bzero(&newtd->td_startzero,
298 	    __rangeof(struct thread, td_startzero, td_endzero));
299 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
300 	    __rangeof(struct thread, td_startcopy, td_endcopy));
301 
302 	newtd->td_proc = p;
303 	newtd->td_ucred = crhold(td->td_ucred);
304 
305 	/* create the emuldata */
306 	linux_proc_init(td, newtd, args->flags);
307 
308 	em = em_find(newtd);
309 	KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
310 
311 	if (args->flags & LINUX_CLONE_SETTLS)
312 		linux_set_cloned_tls(newtd, args->tls);
313 
314 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
315 		em->child_set_tid = args->child_tidptr;
316 	else
317 	   	em->child_set_tid = NULL;
318 
319 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
320 		em->child_clear_tid = args->child_tidptr;
321 	else
322 	   	em->child_clear_tid = NULL;
323 
324 	cpu_thread_clean(newtd);
325 
326 	linux_set_upcall_kse(newtd, PTROUT(args->stack));
327 
328 	PROC_LOCK(p);
329 	p->p_flag |= P_HADTHREADS;
330 	newtd->td_sigmask = td->td_sigmask;
331 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
332 
333 	if (args->flags & LINUX_CLONE_PARENT)
334 		thread_link(newtd, p->p_pptr);
335 	else
336 		thread_link(newtd, p);
337 
338 	thread_lock(td);
339 	/* let the scheduler know about these things. */
340 	sched_fork_thread(td, newtd);
341 	thread_unlock(td);
342 	if (P_SHOULDSTOP(p))
343 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
344 	PROC_UNLOCK(p);
345 
346 	tidhash_add(newtd);
347 
348 #ifdef DEBUG
349 	if (ldebug(clone))
350 		printf(ARGS(clone, "successful clone to %d, stack %p"),
351 		(int)newtd->td_tid, args->stack);
352 #endif
353 
354 	LINUX_CTR2(clone, "thread(%d) successful clone to %d",
355 	    td->td_tid, newtd->td_tid);
356 
357 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
358 		error = copyout(&newtd->td_tid, args->parent_tidptr,
359 		    sizeof(newtd->td_tid));
360 		if (error)
361 			printf(LMSG("clone_thread: copyout failed!"));
362 	}
363 
364 	/*
365 	 * Make this runnable after we are finished with it.
366 	 */
367 	thread_lock(newtd);
368 	TD_SET_CAN_RUN(newtd);
369 	sched_add(newtd, SRQ_BORING);
370 	thread_unlock(newtd);
371 
372 	td->td_retval[0] = newtd->td_tid;
373 
374 	return (0);
375 }
376 
377 int
378 linux_clone(struct thread *td, struct linux_clone_args *args)
379 {
380 
381 	if (args->flags & LINUX_CLONE_THREAD)
382 		return (linux_clone_thread(td, args));
383 	else
384 		return (linux_clone_proc(td, args));
385 }
386 
387 int
388 linux_exit(struct thread *td, struct linux_exit_args *args)
389 {
390 	struct linux_emuldata *em;
391 
392 	em = em_find(td);
393 	KASSERT(em != NULL, ("exit: emuldata not found.\n"));
394 
395 	LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
396 
397 	linux_thread_detach(td);
398 
399 	/*
400 	 * XXX. When the last two threads of a process
401 	 * exit via pthread_exit() try thr_exit() first.
402 	 */
403 	kern_thr_exit(td);
404 	exit1(td, W_EXITCODE(args->rval, 0));
405 		/* NOTREACHED */
406 }
407