xref: /freebsd/sys/compat/linux/linux_fork.c (revision 2ed3236082a4473c1da8f72c1ebc071a7b54321f)
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/ptrace.h>
44 #include <sys/racct.h>
45 #include <sys/sched.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sx.h>
48 #include <sys/umtx.h>
49 #include <sys/unistd.h>
50 #include <sys/wait.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 #include <vm/vm_map.h>
55 
56 #ifdef COMPAT_LINUX32
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
59 #else
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
62 #endif
63 #include <compat/linux/linux_emul.h>
64 #include <compat/linux/linux_futex.h>
65 #include <compat/linux/linux_misc.h>
66 #include <compat/linux/linux_util.h>
67 
68 #ifdef LINUX_LEGACY_SYSCALLS
69 int
70 linux_fork(struct thread *td, struct linux_fork_args *args)
71 {
72 	struct fork_req fr;
73 	int error;
74 	struct proc *p2;
75 	struct thread *td2;
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 
96 	return (0);
97 }
98 
99 int
100 linux_vfork(struct thread *td, struct linux_vfork_args *args)
101 {
102 	struct fork_req fr;
103 	int error;
104 	struct proc *p2;
105 	struct thread *td2;
106 
107 	bzero(&fr, sizeof(fr));
108 	fr.fr_flags = RFFDG | RFPROC | RFMEM | RFPPWAIT | RFSTOPPED;
109 	fr.fr_procp = &p2;
110 	if ((error = fork1(td, &fr)) != 0)
111 		return (error);
112 
113 	td2 = FIRST_THREAD_IN_PROC(p2);
114 
115 	linux_proc_init(td, td2, 0);
116 
117 	td->td_retval[0] = p2->p_pid;
118 
119 	/*
120 	 * Make this runnable after we are finished with it.
121 	 */
122 	thread_lock(td2);
123 	TD_SET_CAN_RUN(td2);
124 	sched_add(td2, SRQ_BORING);
125 
126 	return (0);
127 }
128 #endif
129 
130 static int
131 linux_clone_proc(struct thread *td, struct linux_clone_args *args)
132 {
133 	struct fork_req fr;
134 	int error, ff = RFPROC | RFSTOPPED;
135 	struct proc *p2;
136 	struct thread *td2;
137 	int exit_signal;
138 	struct linux_emuldata *em;
139 
140 	exit_signal = args->flags & 0x000000ff;
141 	if (LINUX_SIG_VALID(exit_signal)) {
142 		exit_signal = linux_to_bsd_signal(exit_signal);
143 	} else if (exit_signal != 0)
144 		return (EINVAL);
145 
146 	if (args->flags & LINUX_CLONE_VM)
147 		ff |= RFMEM;
148 	if (args->flags & LINUX_CLONE_SIGHAND)
149 		ff |= RFSIGSHARE;
150 	/*
151 	 * XXX: In Linux, sharing of fs info (chroot/cwd/umask)
152 	 * and open files is independent.  In FreeBSD, its in one
153 	 * structure but in reality it does not cause any problems
154 	 * because both of these flags are usually set together.
155 	 */
156 	if (!(args->flags & (LINUX_CLONE_FILES | LINUX_CLONE_FS)))
157 		ff |= RFFDG;
158 
159 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
160 		if (args->parent_tidptr == NULL)
161 			return (EINVAL);
162 
163 	if (args->flags & LINUX_CLONE_VFORK)
164 		ff |= RFPPWAIT;
165 
166 	bzero(&fr, sizeof(fr));
167 	fr.fr_flags = ff;
168 	fr.fr_procp = &p2;
169 	error = fork1(td, &fr);
170 	if (error)
171 		return (error);
172 
173 	td2 = FIRST_THREAD_IN_PROC(p2);
174 
175 	/* create the emuldata */
176 	linux_proc_init(td, td2, args->flags);
177 
178 	em = em_find(td2);
179 	KASSERT(em != NULL, ("clone_proc: emuldata not found.\n"));
180 
181 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
182 		em->child_set_tid = args->child_tidptr;
183 	else
184 		em->child_set_tid = NULL;
185 
186 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
187 		em->child_clear_tid = args->child_tidptr;
188 	else
189 		em->child_clear_tid = NULL;
190 
191 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
192 		error = copyout(&p2->p_pid, args->parent_tidptr,
193 		    sizeof(p2->p_pid));
194 		if (error)
195 			linux_msg(td, "copyout p_pid failed!");
196 	}
197 
198 	PROC_LOCK(p2);
199 	p2->p_sigparent = exit_signal;
200 	PROC_UNLOCK(p2);
201 	/*
202 	 * In a case of stack = NULL, we are supposed to COW calling process
203 	 * stack. This is what normal fork() does, so we just keep tf_rsp arg
204 	 * intact.
205 	 */
206 	linux_set_upcall_kse(td2, PTROUT(args->stack));
207 
208 	if (args->flags & LINUX_CLONE_SETTLS)
209 		linux_set_cloned_tls(td2, args->tls);
210 
211 	/*
212 	 * If CLONE_PARENT is set, then the parent of the new process will be
213 	 * the same as that of the calling process.
214 	 */
215 	if (args->flags & LINUX_CLONE_PARENT) {
216 		sx_xlock(&proctree_lock);
217 		PROC_LOCK(p2);
218 		proc_reparent(p2, td->td_proc->p_pptr, true);
219 		PROC_UNLOCK(p2);
220 		sx_xunlock(&proctree_lock);
221 	}
222 
223 	/*
224 	 * Make this runnable after we are finished with it.
225 	 */
226 	thread_lock(td2);
227 	TD_SET_CAN_RUN(td2);
228 	sched_add(td2, SRQ_BORING);
229 
230 	td->td_retval[0] = p2->p_pid;
231 
232 	return (0);
233 }
234 
235 static int
236 linux_clone_thread(struct thread *td, struct linux_clone_args *args)
237 {
238 	struct linux_emuldata *em;
239 	struct thread *newtd;
240 	struct proc *p;
241 	int error;
242 
243 	LINUX_CTR4(clone_thread, "thread(%d) flags %x ptid %p ctid %p",
244 	    td->td_tid, (unsigned)args->flags,
245 	    args->parent_tidptr, args->child_tidptr);
246 
247 	if ((args->flags & LINUX_CLONE_PARENT) != 0)
248 		return (EINVAL);
249 	if (args->flags & LINUX_CLONE_PARENT_SETTID)
250 		if (args->parent_tidptr == NULL)
251 			return (EINVAL);
252 
253 	/* Threads should be created with own stack */
254 	if (args->stack == NULL)
255 		return (EINVAL);
256 
257 	p = td->td_proc;
258 
259 #ifdef RACCT
260 	if (racct_enable) {
261 		PROC_LOCK(p);
262 		error = racct_add(p, RACCT_NTHR, 1);
263 		PROC_UNLOCK(p);
264 		if (error != 0)
265 			return (EPROCLIM);
266 	}
267 #endif
268 
269 	/* Initialize our td */
270 	error = kern_thr_alloc(p, 0, &newtd);
271 	if (error)
272 		goto fail;
273 
274 	cpu_copy_thread(newtd, td);
275 
276 	bzero(&newtd->td_startzero,
277 	    __rangeof(struct thread, td_startzero, td_endzero));
278 	bcopy(&td->td_startcopy, &newtd->td_startcopy,
279 	    __rangeof(struct thread, td_startcopy, td_endcopy));
280 
281 	newtd->td_proc = p;
282 	thread_cow_get(newtd, td);
283 
284 	/* create the emuldata */
285 	linux_proc_init(td, newtd, args->flags);
286 
287 	em = em_find(newtd);
288 	KASSERT(em != NULL, ("clone_thread: emuldata not found.\n"));
289 
290 	if (args->flags & LINUX_CLONE_SETTLS)
291 		linux_set_cloned_tls(newtd, args->tls);
292 
293 	if (args->flags & LINUX_CLONE_CHILD_SETTID)
294 		em->child_set_tid = args->child_tidptr;
295 	else
296 		em->child_set_tid = NULL;
297 
298 	if (args->flags & LINUX_CLONE_CHILD_CLEARTID)
299 		em->child_clear_tid = args->child_tidptr;
300 	else
301 		em->child_clear_tid = NULL;
302 
303 	cpu_thread_clean(newtd);
304 
305 	linux_set_upcall_kse(newtd, PTROUT(args->stack));
306 
307 	PROC_LOCK(p);
308 	p->p_flag |= P_HADTHREADS;
309 	thread_link(newtd, p);
310 	bcopy(p->p_comm, newtd->td_name, sizeof(newtd->td_name));
311 
312 	thread_lock(td);
313 	/* let the scheduler know about these things. */
314 	sched_fork_thread(td, newtd);
315 	thread_unlock(td);
316 	if (P_SHOULDSTOP(p))
317 		newtd->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
318 
319 	if (p->p_ptevents & PTRACE_LWP)
320 		newtd->td_dbgflags |= TDB_BORN;
321 	PROC_UNLOCK(p);
322 
323 	tidhash_add(newtd);
324 
325 	LINUX_CTR2(clone_thread, "thread(%d) successful clone to %d",
326 	    td->td_tid, newtd->td_tid);
327 
328 	if (args->flags & LINUX_CLONE_PARENT_SETTID) {
329 		error = copyout(&newtd->td_tid, args->parent_tidptr,
330 		    sizeof(newtd->td_tid));
331 		if (error)
332 			linux_msg(td, "clone_thread: copyout td_tid failed!");
333 	}
334 
335 	/*
336 	 * Make this runnable after we are finished with it.
337 	 */
338 	thread_lock(newtd);
339 	TD_SET_CAN_RUN(newtd);
340 	sched_add(newtd, SRQ_BORING);
341 
342 	td->td_retval[0] = newtd->td_tid;
343 
344 	return (0);
345 
346 fail:
347 #ifdef RACCT
348 	if (racct_enable) {
349 		PROC_LOCK(p);
350 		racct_sub(p, RACCT_NTHR, 1);
351 		PROC_UNLOCK(p);
352 	}
353 #endif
354 	return (error);
355 }
356 
357 int
358 linux_clone(struct thread *td, struct linux_clone_args *args)
359 {
360 
361 	if (args->flags & LINUX_CLONE_THREAD)
362 		return (linux_clone_thread(td, args));
363 	else
364 		return (linux_clone_proc(td, args));
365 }
366 
367 int
368 linux_exit(struct thread *td, struct linux_exit_args *args)
369 {
370 	struct linux_emuldata *em;
371 
372 	em = em_find(td);
373 	KASSERT(em != NULL, ("exit: emuldata not found.\n"));
374 
375 	LINUX_CTR2(exit, "thread(%d) (%d)", em->em_tid, args->rval);
376 
377 	umtx_thread_exit(td);
378 
379 	linux_thread_detach(td);
380 
381 	/*
382 	 * XXX. When the last two threads of a process
383 	 * exit via pthread_exit() try thr_exit() first.
384 	 */
385 	kern_thr_exit(td);
386 	exit1(td, args->rval, 0);
387 		/* NOTREACHED */
388 }
389 
390 int
391 linux_set_tid_address(struct thread *td, struct linux_set_tid_address_args *args)
392 {
393 	struct linux_emuldata *em;
394 
395 	em = em_find(td);
396 	KASSERT(em != NULL, ("set_tid_address: emuldata not found.\n"));
397 
398 	em->child_clear_tid = args->tidptr;
399 
400 	td->td_retval[0] = em->em_tid;
401 
402 	LINUX_CTR3(set_tid_address, "tidptr(%d) %p, returns %d",
403 	    em->em_tid, args->tidptr, td->td_retval[0]);
404 
405 	return (0);
406 }
407 
408 void
409 linux_thread_detach(struct thread *td)
410 {
411 	struct linux_sys_futex_args cup;
412 	struct linux_emuldata *em;
413 	int *child_clear_tid;
414 	int error;
415 
416 	em = em_find(td);
417 	KASSERT(em != NULL, ("thread_detach: emuldata not found.\n"));
418 
419 	LINUX_CTR1(thread_detach, "thread(%d)", em->em_tid);
420 
421 	release_futexes(td, em);
422 
423 	child_clear_tid = em->child_clear_tid;
424 
425 	if (child_clear_tid != NULL) {
426 		LINUX_CTR2(thread_detach, "thread(%d) %p",
427 		    em->em_tid, child_clear_tid);
428 
429 		error = suword32(child_clear_tid, 0);
430 		if (error != 0)
431 			return;
432 
433 		cup.uaddr = child_clear_tid;
434 		cup.op = LINUX_FUTEX_WAKE;
435 		cup.val = 1;		/* wake one */
436 		cup.timeout = NULL;
437 		cup.uaddr2 = NULL;
438 		cup.val3 = 0;
439 		error = linux_sys_futex(td, &cup);
440 		/*
441 		 * this cannot happen at the moment and if this happens it
442 		 * probably means there is a user space bug
443 		 */
444 		if (error != 0)
445 			linux_msg(td, "futex stuff in thread_detach failed.");
446 	}
447 }
448