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