xref: /freebsd/sys/compat/linux/linux_fork.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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_emul.h>
60 #include <compat/linux/linux_futex.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 	struct fork_req fr;
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 	bzero(&fr, sizeof(fr));
78 	fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
79 	fr.fr_procp = &p2;
80 	if ((error = fork1(td, &fr)) != 0)
81 		return (error);
82 
83 	td2 = FIRST_THREAD_IN_PROC(p2);
84 
85 	linux_proc_init(td, td2, 0);
86 
87 	td->td_retval[0] = p2->p_pid;
88 
89 	/*
90 	 * Make this runnable after we are finished with it.
91 	 */
92 	thread_lock(td2);
93 	TD_SET_CAN_RUN(td2);
94 	sched_add(td2, SRQ_BORING);
95 	thread_unlock(td2);
96 
97 	return (0);
98 }
99 
100 int
101 linux_vfork(struct thread *td, struct linux_vfork_args *args)
102 {
103 	struct fork_req fr;
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 	bzero(&fr, sizeof(fr));
114 	fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
115 	fr.fr_procp = &p2;
116 	if ((error = fork1(td, &fr)) != 0)
117 		return (error);
118 
119 	td2 = FIRST_THREAD_IN_PROC(p2);
120 
121 	linux_proc_init(td, td2, 0);
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 	return (0);
134 }
135 
136 static int
137 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
138 {
139 	struct fork_req fr;
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 		exit_signal = linux_to_bsd_signal(exit_signal);
157 	} else if (exit_signal != 0)
158 		return (EINVAL);
159 
160 	if (args->flags & LINUX_CLONE_VM)
161 		ff |= RFMEM;
162 	if (args->flags & LINUX_CLONE_SIGHAND)
163 		ff |= RFSIGSHARE;
164 	/*
165 	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
166 	 * and open files is independant.  In FreeBSD, its in one
167 	 * structure but in reality it does not cause any problems
168 	 * because both of these flags are usually set together.
169 	 */
170 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
171 		ff |= RFFDG;
172 
173 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
174 		if (args->parent_tidptr == NULL)
175 			return (EINVAL);
176 
177 	if (args->flags & LINUX_CLONE_VFORK)
178 		ff |= RFPPWAIT;
179 
180 	bzero(&fr, sizeof(fr));
181 	fr.fr_flags = ff;
182 	fr.fr_procp = &p2;
183 	error = fork1(td, &fr);
184 	if (error)
185 		return (error);
186 
187 	td2 = FIRST_THREAD_IN_PROC(p2);
188 
189 	/* create the emuldata */
190 	linux_proc_init(td, td2, args->flags);
191 
192 	em = em_find(td2);
193 	KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
194 
195 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
196 		em->child_set_tid = args->child_tidptr;
197 	else
198 	   	em->child_set_tid = NULL;
199 
200 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
201 		em->child_clear_tid = args->child_tidptr;
202 	else
203 	   	em->child_clear_tid = NULL;
204 
205 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
206 		error = copyout(&p2->p_pid, args->parent_tidptr,
207 		    sizeof(p2->p_pid));
208 		if (error)
209 			printf(LMSG("copyout failed!"));
210 	}
211 
212 	PROC_LOCK(p2);
213 	p2->p_sigparent = exit_signal;
214 	PROC_UNLOCK(p2);
215 	/*
216 	 * In a case of stack = NULL, we are supposed to COW calling process
217 	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
218 	 * intact.
219 	 */
220 	linux_set_upcall_kse(td2, PTROUT(args->stack));
221 
222 	if (args->flags & LINUX_CLONE_SETTLS)
223 		linux_set_cloned_tls(td2, args->tls);
224 
225 #ifdef DEBUG
226 	if (ldebug(clone))
227 		printf(LMSG("clone: successful rfork to %d, "
228 		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
229 		    exit_signal);
230 #endif
231 
232 	/*
233 	 * Make this runnable after we are finished with it.
234 	 */
235 	thread_lock(td2);
236 	TD_SET_CAN_RUN(td2);
237 	sched_add(td2, SRQ_BORING);
238 	thread_unlock(td2);
239 
240 	td->td_retval[0] = p2->p_pid;
241 
242 	return (0);
243 }
244 
245 static int
246 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
247 {
248 	struct linux_emuldata *em;
249 	struct thread *newtd;
250 	struct proc *p;
251 	int error;
252 
253 #ifdef DEBUG
254 	if (ldebug(clone)) {
255 		printf(ARGS(clone, "thread: flags %x, stack %p, parent tid: %p, "
256 		    "child tid: %p"), (unsigned)args->flags,
257 		    args->stack, args->parent_tidptr, args->child_tidptr);
258 	}
259 #endif
260 
261 	LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
262 	    td->td_tid, (unsigned)args->flags,
263 	    args->parent_tidptr, args->child_tidptr);
264 
265 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
266 		if (args->parent_tidptr == NULL)
267 			return (EINVAL);
268 
269 	/* Threads should be created with own stack */
270 	if (args->stack == NULL)
271 		return (EINVAL);
272 
273 	p = td->td_proc;
274 
275 #ifdef RACCT
276 	if (racct_enable) {
277 		PROC_LOCK(p);
278 		error = racct_add(p, RACCT_NTHR, 1);
279 		PROC_UNLOCK(p);
280 		if (error != 0)
281 			return (EPROCLIM);
282 	}
283 #endif
284 
285 	/* Initialize our td */
286 	error = kern_thr_alloc(p, 0, &newtd);
287 	if (error)
288 		goto fail;
289 
290 	cpu_set_upcall(newtd, td);
291 
292 	bzero(&newtd->td_startzero,
293 	    __rangeof(struct thread, td_startzero, td_endzero));
294 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
295 	    __rangeof(struct thread, td_startcopy, td_endcopy));
296 
297 	newtd->td_proc = p;
298 	thread_cow_get(newtd, td);
299 
300 	/* create the emuldata */
301 	linux_proc_init(td, newtd, args->flags);
302 
303 	em = em_find(newtd);
304 	KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
305 
306 	if (args->flags & LINUX_CLONE_SETTLS)
307 		linux_set_cloned_tls(newtd, args->tls);
308 
309 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
310 		em->child_set_tid = args->child_tidptr;
311 	else
312 	   	em->child_set_tid = NULL;
313 
314 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
315 		em->child_clear_tid = args->child_tidptr;
316 	else
317 	   	em->child_clear_tid = NULL;
318 
319 	cpu_thread_clean(newtd);
320 
321 	linux_set_upcall_kse(newtd, PTROUT(args->stack));
322 
323 	PROC_LOCK(p);
324 	p->p_flag |= P_HADTHREADS;
325 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
326 
327 	if (args->flags & LINUX_CLONE_PARENT)
328 		thread_link(newtd, p->p_pptr);
329 	else
330 		thread_link(newtd, p);
331 
332 	thread_lock(td);
333 	/* let the scheduler know about these things. */
334 	sched_fork_thread(td, newtd);
335 	thread_unlock(td);
336 	if (P_SHOULDSTOP(p))
337 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
338 	PROC_UNLOCK(p);
339 
340 	tidhash_add(newtd);
341 
342 #ifdef DEBUG
343 	if (ldebug(clone))
344 		printf(ARGS(clone, "successful clone to %d, stack %p"),
345 		(int)newtd->td_tid, args->stack);
346 #endif
347 
348 	LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
349 	    td->td_tid, newtd->td_tid);
350 
351 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
352 		error = copyout(&newtd->td_tid, args->parent_tidptr,
353 		    sizeof(newtd->td_tid));
354 		if (error)
355 			printf(LMSG("clone_thread: copyout failed!"));
356 	}
357 
358 	/*
359 	 * Make this runnable after we are finished with it.
360 	 */
361 	thread_lock(newtd);
362 	TD_SET_CAN_RUN(newtd);
363 	sched_add(newtd, SRQ_BORING);
364 	thread_unlock(newtd);
365 
366 	td->td_retval[0] = newtd->td_tid;
367 
368 	return (0);
369 
370 fail:
371 #ifdef RACCT
372 	if (racct_enable) {
373 		PROC_LOCK(p);
374 		racct_sub(p, RACCT_NTHR, 1);
375 		PROC_UNLOCK(p);
376 	}
377 #endif
378 	return (error);
379 }
380 
381 int
382 linux_clone(struct thread *td, struct linux_clone_args *args)
383 {
384 
385 	if (args->flags & LINUX_CLONE_THREAD)
386 		return (linux_clone_thread(td, args));
387 	else
388 		return (linux_clone_proc(td, args));
389 }
390 
391 int
392 linux_exit(struct thread *td, struct linux_exit_args *args)
393 {
394 	struct linux_emuldata *em;
395 
396 	em = em_find(td);
397 	KASSERT(em != NULL, ("exit: emuldata not found.\n"));
398 
399 	LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
400 
401 	linux_thread_detach(td);
402 
403 	/*
404 	 * XXX. When the last two threads of a process
405 	 * exit via pthread_exit() try thr_exit() first.
406 	 */
407 	kern_thr_exit(td);
408 	exit1(td, args->rval, 0);
409 		/* NOTREACHED */
410 }
411 
412 int
413 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
414 {
415 	struct linux_emuldata *em;
416 
417 	em = em_find(td);
418 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
419 
420 	em->child_clear_tid = args->tidptr;
421 
422 	td->td_retval[0] = em->em_tid;
423 
424 	LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
425 	    em->em_tid, args->tidptr, td->td_retval[0]);
426 
427 	return (0);
428 }
429 
430 void
431 linux_thread_detach(struct thread *td)
432 {
433 	struct linux_sys_futex_args cup;
434 	struct linux_emuldata *em;
435 	int *child_clear_tid;
436 	int error;
437 
438 	em = em_find(td);
439 	KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
440 
441 	LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
442 
443 	release_futexes(td, em);
444 
445 	child_clear_tid = em->child_clear_tid;
446 
447 	if (child_clear_tid != NULL) {
448 
449 		LINUX_CTR2(thread_detach, "thread(%d) %p",
450 		    em->em_tid, child_clear_tid);
451 
452 		error = suword32(child_clear_tid, 0);
453 		if (error != 0)
454 			return;
455 
456 		cup.uaddr = child_clear_tid;
457 		cup.op = LINUX_FUTEX_WAKE;
458 		cup.val = 1;		/* wake one */
459 		cup.timeout = NULL;
460 		cup.uaddr2 = NULL;
461 		cup.val3 = 0;
462 		error = linux_sys_futex(td, &cup);
463 		/*
464 		 * this cannot happen at the moment and if this happens it
465 		 * probably means there is a user space bug
466 		 */
467 		if (error != 0)
468 			linux_msg(td, "futex stuff in thread_detach failed.");
469 	}
470 }
471