xref: /freebsd/sys/compat/linux/linux_fork.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2002 Doug Rabson
6  * Copyright (c) 2000 Marcel Moolenaar
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "opt_compat.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/racct.h>
44 #include <sys/sched.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/sx.h>
47 #include <sys/umtx.h>
48 #include <sys/unistd.h>
49 #include <sys/wait.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54 
55 #ifdef COMPAT_LINUX32
56 #include <machine/../linux32/linux.h>
57 #include <machine/../linux32/linux32_proto.h>
58 #else
59 #include <machine/../linux/linux.h>
60 #include <machine/../linux/linux_proto.h>
61 #endif
62 #include <compat/linux/linux_emul.h>
63 #include <compat/linux/linux_futex.h>
64 #include <compat/linux/linux_misc.h>
65 #include <compat/linux/linux_util.h>
66 
67 int
68 linux_fork(struct thread *td, struct linux_fork_args *args)
69 {
70 	struct fork_req fr;
71 	int error;
72 	struct proc *p2;
73 	struct thread *td2;
74 
75 #ifdef DEBUG
76 	if (ldebug(fork))
77 		printf(ARGS(fork, ""));
78 #endif
79 
80 	bzero(&fr, sizeof(fr));
81 	fr.fr_flags = RFFDG | RFPROC | RFSTOPPED;
82 	fr.fr_procp = &p2;
83 	if ((error = fork1(td, &fr)) != 0)
84 		return (error);
85 
86 	td2 = FIRST_THREAD_IN_PROC(p2);
87 
88 	linux_proc_init(td, td2, 0);
89 
90 	td->td_retval[0] = p2->p_pid;
91 
92 	/*
93 	 * Make this runnable after we are finished with it.
94 	 */
95 	thread_lock(td2);
96 	TD_SET_CAN_RUN(td2);
97 	sched_add(td2, SRQ_BORING);
98 	thread_unlock(td2);
99 
100 	return (0);
101 }
102 
103 int
104 linux_vfork(struct thread *td, struct linux_vfork_args *args)
105 {
106 	struct fork_req fr;
107 	int error;
108 	struct proc *p2;
109 	struct thread *td2;
110 
111 #ifdef DEBUG
112 	if (ldebug(vfork))
113 		printf(ARGS(vfork, ""));
114 #endif
115 
116 	bzero(&fr, sizeof(fr));
117 	fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
118 	fr.fr_procp = &p2;
119 	if ((error = fork1(td, &fr)) != 0)
120 		return (error);
121 
122 	td2 = FIRST_THREAD_IN_PROC(p2);
123 
124 	linux_proc_init(td, td2, 0);
125 
126 	td->td_retval[0] = p2->p_pid;
127 
128 	/*
129 	 * Make this runnable after we are finished with it.
130 	 */
131 	thread_lock(td2);
132 	TD_SET_CAN_RUN(td2);
133 	sched_add(td2, SRQ_BORING);
134 	thread_unlock(td2);
135 
136 	return (0);
137 }
138 
139 static int
140 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
141 {
142 	struct fork_req fr;
143 	int error, ff = RFPROC | RFSTOPPED;
144 	struct proc *p2;
145 	struct thread *td2;
146 	int exit_signal;
147 	struct linux_emuldata *em;
148 
149 #ifdef DEBUG
150 	if (ldebug(clone)) {
151 		printf(ARGS(clone, "flags %x, stack %p, parent tid: %p, "
152 		    "child tid: %p"), (unsigned)args->flags,
153 		    args->stack, args->parent_tidptr, args->child_tidptr);
154 	}
155 #endif
156 
157 	exit_signal = args->flags & 0x000000ff;
158 	if (LINUX_SIG_VALID(exit_signal)) {
159 		exit_signal = linux_to_bsd_signal(exit_signal);
160 	} else if (exit_signal != 0)
161 		return (EINVAL);
162 
163 	if (args->flags & LINUX_CLONE_VM)
164 		ff |= RFMEM;
165 	if (args->flags & LINUX_CLONE_SIGHAND)
166 		ff |= RFSIGSHARE;
167 	/*
168 	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
169 	 * and open files is independent.  In FreeBSD, its in one
170 	 * structure but in reality it does not cause any problems
171 	 * because both of these flags are usually set together.
172 	 */
173 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
174 		ff |= RFFDG;
175 
176 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
177 		if (args->parent_tidptr == NULL)
178 			return (EINVAL);
179 
180 	if (args->flags & LINUX_CLONE_VFORK)
181 		ff |= RFPPWAIT;
182 
183 	bzero(&fr, sizeof(fr));
184 	fr.fr_flags = ff;
185 	fr.fr_procp = &p2;
186 	error = fork1(td, &fr);
187 	if (error)
188 		return (error);
189 
190 	td2 = FIRST_THREAD_IN_PROC(p2);
191 
192 	/* create the emuldata */
193 	linux_proc_init(td, td2, args->flags);
194 
195 	em = em_find(td2);
196 	KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
197 
198 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
199 		em->child_set_tid = args->child_tidptr;
200 	else
201 		em->child_set_tid = NULL;
202 
203 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
204 		em->child_clear_tid = args->child_tidptr;
205 	else
206 		em->child_clear_tid = NULL;
207 
208 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
209 		error = copyout(&p2->p_pid, args->parent_tidptr,
210 		    sizeof(p2->p_pid));
211 		if (error)
212 			printf(LMSG("copyout failed!"));
213 	}
214 
215 	PROC_LOCK(p2);
216 	p2->p_sigparent = exit_signal;
217 	PROC_UNLOCK(p2);
218 	/*
219 	 * In a case of stack = NULL, we are supposed to COW calling process
220 	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
221 	 * intact.
222 	 */
223 	linux_set_upcall_kse(td2, PTROUT(args->stack));
224 
225 	if (args->flags & LINUX_CLONE_SETTLS)
226 		linux_set_cloned_tls(td2, args->tls);
227 
228 	/*
229 	 * If CLONE_PARENT is set, then the parent of the new process will be
230 	 * the same as that of the calling process.
231 	 */
232 	if (args->flags & LINUX_CLONE_PARENT) {
233 		sx_xlock(&proctree_lock);
234 		PROC_LOCK(p2);
235 		proc_reparent(p2, td->td_proc->p_pptr);
236 		PROC_UNLOCK(p2);
237 		sx_xunlock(&proctree_lock);
238 	}
239 
240 #ifdef DEBUG
241 	if (ldebug(clone))
242 		printf(LMSG("clone: successful rfork to %d, "
243 		    "stack %p sig = %d"), (int)p2->p_pid, args->stack,
244 		    exit_signal);
245 #endif
246 
247 	/*
248 	 * Make this runnable after we are finished with it.
249 	 */
250 	thread_lock(td2);
251 	TD_SET_CAN_RUN(td2);
252 	sched_add(td2, SRQ_BORING);
253 	thread_unlock(td2);
254 
255 	td->td_retval[0] = p2->p_pid;
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, "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 #ifdef RACCT
291 	if (racct_enable) {
292 		PROC_LOCK(p);
293 		error = racct_add(p, RACCT_NTHR, 1);
294 		PROC_UNLOCK(p);
295 		if (error != 0)
296 			return (EPROCLIM);
297 	}
298 #endif
299 
300 	/* Initialize our td */
301 	error = kern_thr_alloc(p, 0, &newtd);
302 	if (error)
303 		goto fail;
304 
305 	cpu_copy_thread(newtd, td);
306 
307 	bzero(&newtd->td_startzero,
308 	    __rangeof(struct thread, td_startzero, td_endzero));
309 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
310 	    __rangeof(struct thread, td_startcopy, td_endcopy));
311 
312 	newtd->td_proc = p;
313 	thread_cow_get(newtd, td);
314 
315 	/* create the emuldata */
316 	linux_proc_init(td, newtd, args->flags);
317 
318 	em = em_find(newtd);
319 	KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
320 
321 	if (args->flags & LINUX_CLONE_SETTLS)
322 		linux_set_cloned_tls(newtd, args->tls);
323 
324 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
325 		em->child_set_tid = args->child_tidptr;
326 	else
327 		em->child_set_tid = NULL;
328 
329 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
330 		em->child_clear_tid = args->child_tidptr;
331 	else
332 		em->child_clear_tid = NULL;
333 
334 	cpu_thread_clean(newtd);
335 
336 	linux_set_upcall_kse(newtd, PTROUT(args->stack));
337 
338 	PROC_LOCK(p);
339 	p->p_flag |= P_HADTHREADS;
340 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
341 
342 	if (args->flags & LINUX_CLONE_PARENT)
343 		thread_link(newtd, p->p_pptr);
344 	else
345 		thread_link(newtd, p);
346 
347 	thread_lock(td);
348 	/* let the scheduler know about these things. */
349 	sched_fork_thread(td, newtd);
350 	thread_unlock(td);
351 	if (P_SHOULDSTOP(p))
352 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
353 	PROC_UNLOCK(p);
354 
355 	tidhash_add(newtd);
356 
357 #ifdef DEBUG
358 	if (ldebug(clone))
359 		printf(ARGS(clone, "successful clone to %d, stack %p"),
360 		(int)newtd->td_tid, args->stack);
361 #endif
362 
363 	LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
364 	    td->td_tid, newtd->td_tid);
365 
366 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
367 		error = copyout(&newtd->td_tid, args->parent_tidptr,
368 		    sizeof(newtd->td_tid));
369 		if (error)
370 			printf(LMSG("clone_thread: copyout failed!"));
371 	}
372 
373 	/*
374 	 * Make this runnable after we are finished with it.
375 	 */
376 	thread_lock(newtd);
377 	TD_SET_CAN_RUN(newtd);
378 	sched_add(newtd, SRQ_BORING);
379 	thread_unlock(newtd);
380 
381 	td->td_retval[0] = newtd->td_tid;
382 
383 	return (0);
384 
385 fail:
386 #ifdef RACCT
387 	if (racct_enable) {
388 		PROC_LOCK(p);
389 		racct_sub(p, RACCT_NTHR, 1);
390 		PROC_UNLOCK(p);
391 	}
392 #endif
393 	return (error);
394 }
395 
396 int
397 linux_clone(struct thread *td, struct linux_clone_args *args)
398 {
399 
400 	if (args->flags & LINUX_CLONE_THREAD)
401 		return (linux_clone_thread(td, args));
402 	else
403 		return (linux_clone_proc(td, args));
404 }
405 
406 int
407 linux_exit(struct thread *td, struct linux_exit_args *args)
408 {
409 	struct linux_emuldata *em;
410 
411 	em = em_find(td);
412 	KASSERT(em != NULL, ("exit: emuldata not found.\n"));
413 
414 	LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
415 
416 	umtx_thread_exit(td);
417 
418 	linux_thread_detach(td);
419 
420 	/*
421 	 * XXX. When the last two threads of a process
422 	 * exit via pthread_exit() try thr_exit() first.
423 	 */
424 	kern_thr_exit(td);
425 	exit1(td, args->rval, 0);
426 		/* NOTREACHED */
427 }
428 
429 int
430 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
431 {
432 	struct linux_emuldata *em;
433 
434 	em = em_find(td);
435 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
436 
437 	em->child_clear_tid = args->tidptr;
438 
439 	td->td_retval[0] = em->em_tid;
440 
441 	LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
442 	    em->em_tid, args->tidptr, td->td_retval[0]);
443 
444 	return (0);
445 }
446 
447 void
448 linux_thread_detach(struct thread *td)
449 {
450 	struct linux_sys_futex_args cup;
451 	struct linux_emuldata *em;
452 	int *child_clear_tid;
453 	int error;
454 
455 	em = em_find(td);
456 	KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
457 
458 	LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
459 
460 	release_futexes(td, em);
461 
462 	child_clear_tid = em->child_clear_tid;
463 
464 	if (child_clear_tid != NULL) {
465 
466 		LINUX_CTR2(thread_detach, "thread(%d) %p",
467 		    em->em_tid, child_clear_tid);
468 
469 		error = suword32(child_clear_tid, 0);
470 		if (error != 0)
471 			return;
472 
473 		cup.uaddr = child_clear_tid;
474 		cup.op = LINUX_FUTEX_WAKE;
475 		cup.val = 1;		/* wake one */
476 		cup.timeout = NULL;
477 		cup.uaddr2 = NULL;
478 		cup.val3 = 0;
479 		error = linux_sys_futex(td, &cup);
480 		/*
481 		 * this cannot happen at the moment and if this happens it
482 		 * probably means there is a user space bug
483 		 */
484 		if (error != 0)
485 			linux_msg(td, "futex stuff in thread_detach failed.");
486 	}
487 }
488