xref: /freebsd/sys/kern/kern_fork.c (revision 6236e71bfe7a77e1f4b1908c727cc52e4c2888cc)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40db6a20e2SGarrett Wollman #include "opt_ktrace.h"
418a945d10SKonstantin Belousov #include "opt_kstack_pages.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
4675b8b3b2SJohn Baldwin #include <sys/eventhandler.h>
47cfb5f768SJonathan Anderson #include <sys/fcntl.h>
48df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
490304c731SJamie Gritton #include <sys/jail.h>
50df8bae1dSRodney W. Grimes #include <sys/kernel.h>
5170fca427SJohn Baldwin #include <sys/kthread.h>
52c76e95c3SPeter Wemm #include <sys/sysctl.h>
5319284646SJohn Baldwin #include <sys/lock.h>
54df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5535e0e5b3SJohn Baldwin #include <sys/mutex.h>
56acd3428bSRobert Watson #include <sys/priv.h>
57df8bae1dSRodney W. Grimes #include <sys/proc.h>
58cfb5f768SJonathan Anderson #include <sys/procdesc.h>
599ccba881SMatthew N. Dodd #include <sys/pioctl.h>
60097055e2SEdward Tomasz Napierala #include <sys/racct.h>
61df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
62b43179fbSJeff Roberson #include <sys/sched.h>
63a7b124c3SJohn Baldwin #include <sys/syscall.h>
6470fca427SJohn Baldwin #include <sys/vmmeter.h>
65df8bae1dSRodney W. Grimes #include <sys/vnode.h>
66df8bae1dSRodney W. Grimes #include <sys/acct.h>
670384fff8SJason Evans #include <sys/ktr.h>
68df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
69b71fec07SBruce Evans #include <sys/unistd.h>
705d217f17SJohn Birrell #include <sys/sdt.h>
7157934cd3SJohn Baldwin #include <sys/sx.h>
72e5d81ef1SDmitry Chagin #include <sys/sysent.h>
736004362eSDavid Schultz #include <sys/signalvar.h>
74df8bae1dSRodney W. Grimes 
75fcf7f27aSRobert Watson #include <security/audit/audit.h>
76aed55708SRobert Watson #include <security/mac/mac_framework.h>
77fcf7f27aSRobert Watson 
78d93f860cSPoul-Henning Kamp #include <vm/vm.h>
79dabee6feSPeter Wemm #include <vm/pmap.h>
80dabee6feSPeter Wemm #include <vm/vm_map.h>
81efeaf95aSDavid Greenman #include <vm/vm_extern.h>
82c897b813SJeff Roberson #include <vm/uma.h>
836520495aSAdrian Chadd #include <vm/vm_domain.h>
84d93f860cSPoul-Henning Kamp 
855d217f17SJohn Birrell #ifdef KDTRACE_HOOKS
865d217f17SJohn Birrell #include <sys/dtrace_bsd.h>
875d217f17SJohn Birrell dtrace_fork_func_t	dtrace_fasttrap_fork;
885d217f17SJohn Birrell #endif
895d217f17SJohn Birrell 
905d217f17SJohn Birrell SDT_PROVIDER_DECLARE(proc);
91d9fae5abSAndriy Gapon SDT_PROBE_DEFINE3(proc, kernel, , create, "struct proc *",
927b77e1feSMark Johnston     "struct proc *", "int");
9388c5ea45SJulian Elischer 
94d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
95ad7507e2SSteven Wallace struct fork_args {
96ad7507e2SSteven Wallace 	int     dummy;
97ad7507e2SSteven Wallace };
98d2d3e875SBruce Evans #endif
99ad7507e2SSteven Wallace 
100df8bae1dSRodney W. Grimes /* ARGSUSED */
10126f9a767SRodney W. Grimes int
1028451d0ddSKip Macy sys_fork(struct thread *td, struct fork_args *uap)
103df8bae1dSRodney W. Grimes {
104df8abd0bSPeter Wemm 	int error;
105df8abd0bSPeter Wemm 	struct proc *p2;
106be67169aSBruce Evans 
107367a13f9SEd Schouten 	error = fork1(td, RFFDG | RFPROC, 0, &p2, NULL, 0, NULL);
108df8abd0bSPeter Wemm 	if (error == 0) {
109b40ce416SJulian Elischer 		td->td_retval[0] = p2->p_pid;
110b40ce416SJulian Elischer 		td->td_retval[1] = 0;
111df8abd0bSPeter Wemm 	}
11270fca427SJohn Baldwin 	return (error);
113df8bae1dSRodney W. Grimes }
114df8bae1dSRodney W. Grimes 
115cfb5f768SJonathan Anderson /* ARGUSED */
116cfb5f768SJonathan Anderson int
1178451d0ddSKip Macy sys_pdfork(td, uap)
118cfb5f768SJonathan Anderson 	struct thread *td;
119cfb5f768SJonathan Anderson 	struct pdfork_args *uap;
120cfb5f768SJonathan Anderson {
121cfb5f768SJonathan Anderson 	int error, fd;
122cfb5f768SJonathan Anderson 	struct proc *p2;
123cfb5f768SJonathan Anderson 
124cfb5f768SJonathan Anderson 	/*
125cfb5f768SJonathan Anderson 	 * It is necessary to return fd by reference because 0 is a valid file
126cfb5f768SJonathan Anderson 	 * descriptor number, and the child needs to be able to distinguish
127cfb5f768SJonathan Anderson 	 * itself from the parent using the return value.
128cfb5f768SJonathan Anderson 	 */
129cfb5f768SJonathan Anderson 	error = fork1(td, RFFDG | RFPROC | RFPROCDESC, 0, &p2,
130367a13f9SEd Schouten 	    &fd, uap->flags, NULL);
131cfb5f768SJonathan Anderson 	if (error == 0) {
132cfb5f768SJonathan Anderson 		td->td_retval[0] = p2->p_pid;
133cfb5f768SJonathan Anderson 		td->td_retval[1] = 0;
134cfb5f768SJonathan Anderson 		error = copyout(&fd, uap->fdp, sizeof(fd));
135cfb5f768SJonathan Anderson 	}
136cfb5f768SJonathan Anderson 	return (error);
137cfb5f768SJonathan Anderson }
138cfb5f768SJonathan Anderson 
139df8bae1dSRodney W. Grimes /* ARGSUSED */
14026f9a767SRodney W. Grimes int
1418451d0ddSKip Macy sys_vfork(struct thread *td, struct vfork_args *uap)
142df8bae1dSRodney W. Grimes {
14350d6e424SKip Macy 	int error, flags;
144df8abd0bSPeter Wemm 	struct proc *p2;
145be67169aSBruce Evans 
14650d6e424SKip Macy 	flags = RFFDG | RFPROC | RFPPWAIT | RFMEM;
147367a13f9SEd Schouten 	error = fork1(td, flags, 0, &p2, NULL, 0, NULL);
148df8abd0bSPeter Wemm 	if (error == 0) {
149b40ce416SJulian Elischer 		td->td_retval[0] = p2->p_pid;
150b40ce416SJulian Elischer 		td->td_retval[1] = 0;
151df8abd0bSPeter Wemm 	}
15270fca427SJohn Baldwin 	return (error);
153df8bae1dSRodney W. Grimes }
154df8bae1dSRodney W. Grimes 
155dabee6feSPeter Wemm int
1568451d0ddSKip Macy sys_rfork(struct thread *td, struct rfork_args *uap)
157dabee6feSPeter Wemm {
158df8abd0bSPeter Wemm 	struct proc *p2;
159c8564ad4SBruce Evans 	int error;
160be67169aSBruce Evans 
161c8564ad4SBruce Evans 	/* Don't allow kernel-only flags. */
162885ccc61SJohn Baldwin 	if ((uap->flags & RFKERNELONLY) != 0)
163885ccc61SJohn Baldwin 		return (EINVAL);
164c8564ad4SBruce Evans 
16514961ba7SRobert Watson 	AUDIT_ARG_FFLAGS(uap->flags);
166367a13f9SEd Schouten 	error = fork1(td, uap->flags, 0, &p2, NULL, 0, NULL);
167df8abd0bSPeter Wemm 	if (error == 0) {
168b40ce416SJulian Elischer 		td->td_retval[0] = p2 ? p2->p_pid : 0;
169b40ce416SJulian Elischer 		td->td_retval[1] = 0;
170df8abd0bSPeter Wemm 	}
17170fca427SJohn Baldwin 	return (error);
172dabee6feSPeter Wemm }
173dabee6feSPeter Wemm 
174df8bae1dSRodney W. Grimes int	nprocs = 1;		/* process 0 */
1758f7e4eb5SDag-Erling Smørgrav int	lastpid = 0;
1768f7e4eb5SDag-Erling Smørgrav SYSCTL_INT(_kern, OID_AUTO, lastpid, CTLFLAG_RD, &lastpid, 0,
177d941d475SRobert Watson     "Last used PID");
178df8bae1dSRodney W. Grimes 
179bb6a234eSPeter Wemm /*
1808f7e4eb5SDag-Erling Smørgrav  * Random component to lastpid generation.  We mix in a random factor to make
181bb6a234eSPeter Wemm  * it a little harder to predict.  We sanity check the modulus value to avoid
182bb6a234eSPeter Wemm  * doing it in critical paths.  Don't let it be too small or we pointlessly
183bb6a234eSPeter Wemm  * waste randomness entropy, and don't let it be impossibly large.  Using a
184bb6a234eSPeter Wemm  * modulus that is too big causes a LOT more process table scans and slows
185bb6a234eSPeter Wemm  * down fork processing as the pidchecked caching is defeated.
186bb6a234eSPeter Wemm  */
187ee3fd601SDan Moschuk static int randompid = 0;
188bb6a234eSPeter Wemm 
189bb6a234eSPeter Wemm static int
19082d9ae4eSPoul-Henning Kamp sysctl_kern_randompid(SYSCTL_HANDLER_ARGS)
191bb6a234eSPeter Wemm {
192bb6a234eSPeter Wemm 	int error, pid;
193bb6a234eSPeter Wemm 
19447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, sizeof(int));
19547934cefSDon Lewis 	if (error != 0)
19647934cefSDon Lewis 		return(error);
1973fc755c1SJohn Baldwin 	sx_xlock(&allproc_lock);
198bb6a234eSPeter Wemm 	pid = randompid;
199bb6a234eSPeter Wemm 	error = sysctl_handle_int(oidp, &pid, 0, req);
2003fc755c1SJohn Baldwin 	if (error == 0 && req->newptr != NULL) {
20102c6fc21SKonstantin Belousov 		if (pid < 0 || pid > pid_max - 100)	/* out of range */
20202c6fc21SKonstantin Belousov 			pid = pid_max - 100;
203bb6a234eSPeter Wemm 		else if (pid < 2)			/* NOP */
204bb6a234eSPeter Wemm 			pid = 0;
205bb6a234eSPeter Wemm 		else if (pid < 100)			/* Make it reasonable */
206bb6a234eSPeter Wemm 			pid = 100;
207bb6a234eSPeter Wemm 		randompid = pid;
2083fc755c1SJohn Baldwin 	}
2093fc755c1SJohn Baldwin 	sx_xunlock(&allproc_lock);
210bb6a234eSPeter Wemm 	return (error);
211bb6a234eSPeter Wemm }
212bb6a234eSPeter Wemm 
213bb6a234eSPeter Wemm SYSCTL_PROC(_kern, OID_AUTO, randompid, CTLTYPE_INT|CTLFLAG_RW,
214bb6a234eSPeter Wemm     0, 0, sysctl_kern_randompid, "I", "Random PID modulus");
215ee3fd601SDan Moschuk 
2161d845e86SEdward Tomasz Napierala static int
217afd01097SEdward Tomasz Napierala fork_findpid(int flags)
218afd01097SEdward Tomasz Napierala {
219afd01097SEdward Tomasz Napierala 	struct proc *p;
220afd01097SEdward Tomasz Napierala 	int trypid;
221afd01097SEdward Tomasz Napierala 	static int pidchecked = 0;
222afd01097SEdward Tomasz Napierala 
2233e73ff1eSEdward Tomasz Napierala 	/*
2243e73ff1eSEdward Tomasz Napierala 	 * Requires allproc_lock in order to iterate over the list
2253e73ff1eSEdward Tomasz Napierala 	 * of processes, and proctree_lock to access p_pgrp.
2263e73ff1eSEdward Tomasz Napierala 	 */
2273e73ff1eSEdward Tomasz Napierala 	sx_assert(&allproc_lock, SX_LOCKED);
2283e73ff1eSEdward Tomasz Napierala 	sx_assert(&proctree_lock, SX_LOCKED);
229afd01097SEdward Tomasz Napierala 
230afd01097SEdward Tomasz Napierala 	/*
231afd01097SEdward Tomasz Napierala 	 * Find an unused process ID.  We remember a range of unused IDs
232afd01097SEdward Tomasz Napierala 	 * ready to use (from lastpid+1 through pidchecked-1).
233afd01097SEdward Tomasz Napierala 	 *
234afd01097SEdward Tomasz Napierala 	 * If RFHIGHPID is set (used during system boot), do not allocate
235afd01097SEdward Tomasz Napierala 	 * low-numbered pids.
236afd01097SEdward Tomasz Napierala 	 */
237afd01097SEdward Tomasz Napierala 	trypid = lastpid + 1;
238afd01097SEdward Tomasz Napierala 	if (flags & RFHIGHPID) {
239afd01097SEdward Tomasz Napierala 		if (trypid < 10)
240afd01097SEdward Tomasz Napierala 			trypid = 10;
241afd01097SEdward Tomasz Napierala 	} else {
242afd01097SEdward Tomasz Napierala 		if (randompid)
243afd01097SEdward Tomasz Napierala 			trypid += arc4random() % randompid;
244afd01097SEdward Tomasz Napierala 	}
245afd01097SEdward Tomasz Napierala retry:
246afd01097SEdward Tomasz Napierala 	/*
247afd01097SEdward Tomasz Napierala 	 * If the process ID prototype has wrapped around,
248afd01097SEdward Tomasz Napierala 	 * restart somewhat above 0, as the low-numbered procs
249afd01097SEdward Tomasz Napierala 	 * tend to include daemons that don't exit.
250afd01097SEdward Tomasz Napierala 	 */
25102c6fc21SKonstantin Belousov 	if (trypid >= pid_max) {
25202c6fc21SKonstantin Belousov 		trypid = trypid % pid_max;
253afd01097SEdward Tomasz Napierala 		if (trypid < 100)
254afd01097SEdward Tomasz Napierala 			trypid += 100;
255afd01097SEdward Tomasz Napierala 		pidchecked = 0;
256afd01097SEdward Tomasz Napierala 	}
257afd01097SEdward Tomasz Napierala 	if (trypid >= pidchecked) {
258afd01097SEdward Tomasz Napierala 		int doingzomb = 0;
259afd01097SEdward Tomasz Napierala 
260afd01097SEdward Tomasz Napierala 		pidchecked = PID_MAX;
261afd01097SEdward Tomasz Napierala 		/*
262afd01097SEdward Tomasz Napierala 		 * Scan the active and zombie procs to check whether this pid
263afd01097SEdward Tomasz Napierala 		 * is in use.  Remember the lowest pid that's greater
264afd01097SEdward Tomasz Napierala 		 * than trypid, so we can avoid checking for a while.
265237623b0SKonstantin Belousov 		 *
266237623b0SKonstantin Belousov 		 * Avoid reuse of the process group id, session id or
267237623b0SKonstantin Belousov 		 * the reaper subtree id.  Note that for process group
268237623b0SKonstantin Belousov 		 * and sessions, the amount of reserved pids is
269237623b0SKonstantin Belousov 		 * limited by process limit.  For the subtree ids, the
270237623b0SKonstantin Belousov 		 * id is kept reserved only while there is a
271237623b0SKonstantin Belousov 		 * non-reaped process in the subtree, so amount of
272237623b0SKonstantin Belousov 		 * reserved pids is limited by process limit times
273237623b0SKonstantin Belousov 		 * two.
274afd01097SEdward Tomasz Napierala 		 */
275afd01097SEdward Tomasz Napierala 		p = LIST_FIRST(&allproc);
276afd01097SEdward Tomasz Napierala again:
277afd01097SEdward Tomasz Napierala 		for (; p != NULL; p = LIST_NEXT(p, p_list)) {
278afd01097SEdward Tomasz Napierala 			while (p->p_pid == trypid ||
279237623b0SKonstantin Belousov 			    p->p_reapsubtree == trypid ||
280afd01097SEdward Tomasz Napierala 			    (p->p_pgrp != NULL &&
281afd01097SEdward Tomasz Napierala 			    (p->p_pgrp->pg_id == trypid ||
282afd01097SEdward Tomasz Napierala 			    (p->p_session != NULL &&
283afd01097SEdward Tomasz Napierala 			    p->p_session->s_sid == trypid)))) {
284afd01097SEdward Tomasz Napierala 				trypid++;
285afd01097SEdward Tomasz Napierala 				if (trypid >= pidchecked)
286afd01097SEdward Tomasz Napierala 					goto retry;
287afd01097SEdward Tomasz Napierala 			}
288afd01097SEdward Tomasz Napierala 			if (p->p_pid > trypid && pidchecked > p->p_pid)
289afd01097SEdward Tomasz Napierala 				pidchecked = p->p_pid;
290afd01097SEdward Tomasz Napierala 			if (p->p_pgrp != NULL) {
291afd01097SEdward Tomasz Napierala 				if (p->p_pgrp->pg_id > trypid &&
292afd01097SEdward Tomasz Napierala 				    pidchecked > p->p_pgrp->pg_id)
293afd01097SEdward Tomasz Napierala 					pidchecked = p->p_pgrp->pg_id;
294afd01097SEdward Tomasz Napierala 				if (p->p_session != NULL &&
295afd01097SEdward Tomasz Napierala 				    p->p_session->s_sid > trypid &&
296afd01097SEdward Tomasz Napierala 				    pidchecked > p->p_session->s_sid)
297afd01097SEdward Tomasz Napierala 					pidchecked = p->p_session->s_sid;
298afd01097SEdward Tomasz Napierala 			}
299afd01097SEdward Tomasz Napierala 		}
300afd01097SEdward Tomasz Napierala 		if (!doingzomb) {
301afd01097SEdward Tomasz Napierala 			doingzomb = 1;
302afd01097SEdward Tomasz Napierala 			p = LIST_FIRST(&zombproc);
303afd01097SEdward Tomasz Napierala 			goto again;
304afd01097SEdward Tomasz Napierala 		}
305afd01097SEdward Tomasz Napierala 	}
306afd01097SEdward Tomasz Napierala 
307afd01097SEdward Tomasz Napierala 	/*
308afd01097SEdward Tomasz Napierala 	 * RFHIGHPID does not mess with the lastpid counter during boot.
309afd01097SEdward Tomasz Napierala 	 */
310afd01097SEdward Tomasz Napierala 	if (flags & RFHIGHPID)
311afd01097SEdward Tomasz Napierala 		pidchecked = 0;
312afd01097SEdward Tomasz Napierala 	else
313afd01097SEdward Tomasz Napierala 		lastpid = trypid;
314afd01097SEdward Tomasz Napierala 
315afd01097SEdward Tomasz Napierala 	return (trypid);
316afd01097SEdward Tomasz Napierala }
317afd01097SEdward Tomasz Napierala 
318afd01097SEdward Tomasz Napierala static int
3193e73ff1eSEdward Tomasz Napierala fork_norfproc(struct thread *td, int flags)
3201d845e86SEdward Tomasz Napierala {
3211d845e86SEdward Tomasz Napierala 	int error;
3221d845e86SEdward Tomasz Napierala 	struct proc *p1;
3231d845e86SEdward Tomasz Napierala 
324087bfb0eSEdward Tomasz Napierala 	KASSERT((flags & RFPROC) == 0,
325087bfb0eSEdward Tomasz Napierala 	    ("fork_norfproc called with RFPROC set"));
3261d845e86SEdward Tomasz Napierala 	p1 = td->td_proc;
3271d845e86SEdward Tomasz Napierala 
3281d845e86SEdward Tomasz Napierala 	if (((p1->p_flag & (P_HADTHREADS|P_SYSTEM)) == P_HADTHREADS) &&
3291d845e86SEdward Tomasz Napierala 	    (flags & (RFCFDG | RFFDG))) {
3301d845e86SEdward Tomasz Napierala 		PROC_LOCK(p1);
3316ddcc233SKonstantin Belousov 		if (thread_single(p1, SINGLE_BOUNDARY)) {
3321d845e86SEdward Tomasz Napierala 			PROC_UNLOCK(p1);
3331d845e86SEdward Tomasz Napierala 			return (ERESTART);
3341d845e86SEdward Tomasz Napierala 		}
3351d845e86SEdward Tomasz Napierala 		PROC_UNLOCK(p1);
3361d845e86SEdward Tomasz Napierala 	}
3371d845e86SEdward Tomasz Napierala 
3381d845e86SEdward Tomasz Napierala 	error = vm_forkproc(td, NULL, NULL, NULL, flags);
3391d845e86SEdward Tomasz Napierala 	if (error)
3401d845e86SEdward Tomasz Napierala 		goto fail;
3411d845e86SEdward Tomasz Napierala 
3421d845e86SEdward Tomasz Napierala 	/*
3431d845e86SEdward Tomasz Napierala 	 * Close all file descriptors.
3441d845e86SEdward Tomasz Napierala 	 */
3451d845e86SEdward Tomasz Napierala 	if (flags & RFCFDG) {
3461d845e86SEdward Tomasz Napierala 		struct filedesc *fdtmp;
347eb48fbd9SMateusz Guzik 		fdtmp = fdinit(td->td_proc->p_fd, false);
3482609222aSPawel Jakub Dawidek 		fdescfree(td);
3491d845e86SEdward Tomasz Napierala 		p1->p_fd = fdtmp;
3501d845e86SEdward Tomasz Napierala 	}
3511d845e86SEdward Tomasz Napierala 
3521d845e86SEdward Tomasz Napierala 	/*
3531d845e86SEdward Tomasz Napierala 	 * Unshare file descriptors (from parent).
3541d845e86SEdward Tomasz Napierala 	 */
3551d845e86SEdward Tomasz Napierala 	if (flags & RFFDG)
356b9d32c36SMateusz Guzik 		fdunshare(td);
3571d845e86SEdward Tomasz Napierala 
3581d845e86SEdward Tomasz Napierala fail:
3591d845e86SEdward Tomasz Napierala 	if (((p1->p_flag & (P_HADTHREADS|P_SYSTEM)) == P_HADTHREADS) &&
3601d845e86SEdward Tomasz Napierala 	    (flags & (RFCFDG | RFFDG))) {
3611d845e86SEdward Tomasz Napierala 		PROC_LOCK(p1);
3626ddcc233SKonstantin Belousov 		thread_single_end(p1, SINGLE_BOUNDARY);
3631d845e86SEdward Tomasz Napierala 		PROC_UNLOCK(p1);
3641d845e86SEdward Tomasz Napierala 	}
3651d845e86SEdward Tomasz Napierala 	return (error);
3661d845e86SEdward Tomasz Napierala }
3671d845e86SEdward Tomasz Napierala 
368afd01097SEdward Tomasz Napierala static void
369afd01097SEdward Tomasz Napierala do_fork(struct thread *td, int flags, struct proc *p2, struct thread *td2,
370cfb5f768SJonathan Anderson     struct vmspace *vm2, int pdflags)
371df8bae1dSRodney W. Grimes {
372afd01097SEdward Tomasz Napierala 	struct proc *p1, *pptr;
3736fa39a73SKonstantin Belousov 	int p2_held, trypid;
3745641ae5dSJohn Baldwin 	struct filedesc *fd;
375ad05d580STor Egge 	struct filedesc_to_leader *fdtol;
3763fc755c1SJohn Baldwin 	struct sigacts *newsigacts;
3775856e12eSJohn Dyson 
378afd01097SEdward Tomasz Napierala 	sx_assert(&proctree_lock, SX_SLOCKED);
379afd01097SEdward Tomasz Napierala 	sx_assert(&allproc_lock, SX_XLOCKED);
380df8bae1dSRodney W. Grimes 
3816fa39a73SKonstantin Belousov 	p2_held = 0;
38270fca427SJohn Baldwin 	p1 = td->td_proc;
38370fca427SJohn Baldwin 
384df8bae1dSRodney W. Grimes 	/*
385ef5dc8a9SJohn Dyson 	 * Increment the nprocs resource before blocking can occur.  There
386ef5dc8a9SJohn Dyson 	 * are hard-limits as to the number of processes that can run.
387ef5dc8a9SJohn Dyson 	 */
388ef5dc8a9SJohn Dyson 	nprocs++;
389ef5dc8a9SJohn Dyson 
390afd01097SEdward Tomasz Napierala 	trypid = fork_findpid(flags);
391df8bae1dSRodney W. Grimes 
3925ce2f678SJohn Baldwin 	sx_sunlock(&proctree_lock);
393df8bae1dSRodney W. Grimes 
394e602ba25SJulian Elischer 	p2->p_state = PRS_NEW;		/* protect against others */
395553629ebSJake Burkholder 	p2->p_pid = trypid;
39614961ba7SRobert Watson 	AUDIT_ARG_PID(p2->p_pid);
397553629ebSJake Burkholder 	LIST_INSERT_HEAD(&allproc, p2, p_list);
3986ddcc233SKonstantin Belousov 	allproc_gen++;
399553629ebSJake Burkholder 	LIST_INSERT_HEAD(PIDHASH(p2->p_pid), p2, p_hash);
400cf7d9a8cSDavid Xu 	tidhash_add(td2);
4011ad9ee86SXin LI 	PROC_LOCK(p2);
4021ad9ee86SXin LI 	PROC_LOCK(p1);
4031ad9ee86SXin LI 
4041005a129SJohn Baldwin 	sx_xunlock(&allproc_lock);
405553629ebSJake Burkholder 
4061ad9ee86SXin LI 	bcopy(&p1->p_startcopy, &p2->p_startcopy,
4071ad9ee86SXin LI 	    __rangeof(struct proc, p_startcopy, p_endcopy));
4088b4a2800SKonstantin Belousov 	pargs_hold(p2->p_args);
4096520495aSAdrian Chadd 
4101ad9ee86SXin LI 	PROC_UNLOCK(p1);
4111ad9ee86SXin LI 
4121ad9ee86SXin LI 	bzero(&p2->p_startzero,
4131ad9ee86SXin LI 	    __rangeof(struct proc, p_startzero, p_endzero));
4141ad9ee86SXin LI 
4150304c731SJamie Gritton 	/* Tell the prison that we exist. */
416413628a7SBjoern A. Zeeb 	prison_proc_hold(p2->p_ucred->cr_prison);
417413628a7SBjoern A. Zeeb 
4181ad9ee86SXin LI 	PROC_UNLOCK(p2);
4191ad9ee86SXin LI 
4200384fff8SJason Evans 	/*
4213fc755c1SJohn Baldwin 	 * Malloc things while we don't hold any locks.
4223fc755c1SJohn Baldwin 	 */
42390af4afaSJohn Baldwin 	if (flags & RFSIGSHARE)
4243fc755c1SJohn Baldwin 		newsigacts = NULL;
42590af4afaSJohn Baldwin 	else
42690af4afaSJohn Baldwin 		newsigacts = sigacts_alloc();
4273fc755c1SJohn Baldwin 
4283fc755c1SJohn Baldwin 	/*
4293fc755c1SJohn Baldwin 	 * Copy filedesc.
4303fc755c1SJohn Baldwin 	 */
431ad05d580STor Egge 	if (flags & RFCFDG) {
432eb48fbd9SMateusz Guzik 		fd = fdinit(p1->p_fd, false);
433ad05d580STor Egge 		fdtol = NULL;
434ad05d580STor Egge 	} else if (flags & RFFDG) {
435598b7ec8SPoul-Henning Kamp 		fd = fdcopy(p1->p_fd);
436ad05d580STor Egge 		fdtol = NULL;
437ad05d580STor Egge 	} else {
438c7f1c11bSAlfred Perlstein 		fd = fdshare(p1->p_fd);
439ad05d580STor Egge 		if (p1->p_fdtol == NULL)
4403e73ff1eSEdward Tomasz Napierala 			p1->p_fdtol = filedesc_to_leader_alloc(NULL, NULL,
441ad05d580STor Egge 			    p1->p_leader);
442ad05d580STor Egge 		if ((flags & RFTHREAD) != 0) {
443ad05d580STor Egge 			/*
4443e73ff1eSEdward Tomasz Napierala 			 * Shared file descriptor table, and shared
4453e73ff1eSEdward Tomasz Napierala 			 * process leaders.
446ad05d580STor Egge 			 */
447ad05d580STor Egge 			fdtol = p1->p_fdtol;
4485e3f7694SRobert Watson 			FILEDESC_XLOCK(p1->p_fd);
449ad05d580STor Egge 			fdtol->fdl_refcount++;
4505e3f7694SRobert Watson 			FILEDESC_XUNLOCK(p1->p_fd);
451ad05d580STor Egge 		} else {
452ad05d580STor Egge 			/*
4533e73ff1eSEdward Tomasz Napierala 			 * Shared file descriptor table, and different
4543e73ff1eSEdward Tomasz Napierala 			 * process leaders.
455ad05d580STor Egge 			 */
456ad05d580STor Egge 			fdtol = filedesc_to_leader_alloc(p1->p_fdtol,
4573e73ff1eSEdward Tomasz Napierala 			    p1->p_fd, p2);
458ad05d580STor Egge 		}
459ad05d580STor Egge 	}
4603fc755c1SJohn Baldwin 	/*
461df8bae1dSRodney W. Grimes 	 * Make a proc table entry for the new process.
462df8bae1dSRodney W. Grimes 	 * Start by zeroing the section of proc that is zero-initialized,
463df8bae1dSRodney W. Grimes 	 * then copy the section that is copied directly from the parent.
464df8bae1dSRodney W. Grimes 	 */
465316ec49aSScott Long 
4667d447c95SJohn Baldwin 	PROC_LOCK(p2);
4677d447c95SJohn Baldwin 	PROC_LOCK(p1);
4687d447c95SJohn Baldwin 
469079b7badSJulian Elischer 	bzero(&td2->td_startzero,
4706db36923SDavid Schultz 	    __rangeof(struct thread, td_startzero, td_endzero));
471079b7badSJulian Elischer 
472079b7badSJulian Elischer 	bcopy(&td->td_startcopy, &td2->td_startcopy,
4736db36923SDavid Schultz 	    __rangeof(struct thread, td_startcopy, td_endcopy));
474df8bae1dSRodney W. Grimes 
4754b9322aeSJulian Elischer 	bcopy(&p2->p_comm, &td2->td_name, sizeof(td2->td_name));
476a30ec4b9SDavid Xu 	td2->td_sigstk = td->td_sigstk;
477b61ce5b0SJeff Roberson 	td2->td_flags = TDF_INMEM;
478acbe332aSDavid Xu 	td2->td_lend_user_pri = PRI_MAX;
479a30ec4b9SDavid Xu 
48021ca7b57SMarko Zec #ifdef VIMAGE
48121ca7b57SMarko Zec 	td2->td_vnet = NULL;
48221ca7b57SMarko Zec 	td2->td_vnet_lpush = NULL;
48321ca7b57SMarko Zec #endif
48421ca7b57SMarko Zec 
485df8bae1dSRodney W. Grimes 	/*
48622d19207SJohn Baldwin 	 * Allow the scheduler to initialize the child.
48722d19207SJohn Baldwin 	 */
48822d19207SJohn Baldwin 	thread_lock(td);
48922d19207SJohn Baldwin 	sched_fork(td, td2);
49022d19207SJohn Baldwin 	thread_unlock(td);
49122d19207SJohn Baldwin 
49222d19207SJohn Baldwin 	/*
493df8bae1dSRodney W. Grimes 	 * Duplicate sub-structures as needed.
494df8bae1dSRodney W. Grimes 	 * Increase reference counts on shared objects.
495df8bae1dSRodney W. Grimes 	 */
496b61ce5b0SJeff Roberson 	p2->p_flag = P_INMEM;
497677258f7SKonstantin Belousov 	p2->p_flag2 = p1->p_flag2 & (P2_NOTRACE | P2_NOTRACE_EXEC);
49854b0e65fSJeff Roberson 	p2->p_swtick = ticks;
4999752f794SJohn Baldwin 	if (p1->p_flag & P_PROFIL)
5009752f794SJohn Baldwin 		startprofclock(p2);
501b9df5231SPoul-Henning Kamp 
5026520495aSAdrian Chadd 	/*
5036520495aSAdrian Chadd 	 * Whilst the proc lock is held, copy the VM domain data out
5046520495aSAdrian Chadd 	 * using the VM domain method.
5056520495aSAdrian Chadd 	 */
5066520495aSAdrian Chadd 	vm_domain_policy_init(&p2->p_vm_dom_policy);
5076520495aSAdrian Chadd 	vm_domain_policy_localcopy(&p2->p_vm_dom_policy,
5086520495aSAdrian Chadd 	    &p1->p_vm_dom_policy);
5096520495aSAdrian Chadd 
5106626c604SJulian Elischer 	if (flags & RFSIGSHARE) {
51190af4afaSJohn Baldwin 		p2->p_sigacts = sigacts_hold(p1->p_sigacts);
5126626c604SJulian Elischer 	} else {
51390af4afaSJohn Baldwin 		sigacts_copy(newsigacts, p1->p_sigacts);
51490af4afaSJohn Baldwin 		p2->p_sigacts = newsigacts;
5156626c604SJulian Elischer 	}
516f49d8202SKonstantin Belousov 
517f49d8202SKonstantin Belousov 	if (flags & RFTSIGZMB)
518f49d8202SKonstantin Belousov 	        p2->p_sigparent = RFTSIGNUM(flags);
519f49d8202SKonstantin Belousov 	else if (flags & RFLINUXTHPN)
5206626c604SJulian Elischer 	        p2->p_sigparent = SIGUSR1;
5214ac9ae70SJulian Elischer 	else
5224ac9ae70SJulian Elischer 	        p2->p_sigparent = SIGCHLD;
52388c5ea45SJulian Elischer 
524df8bae1dSRodney W. Grimes 	p2->p_textvp = p1->p_textvp;
5255641ae5dSJohn Baldwin 	p2->p_fd = fd;
526ad05d580STor Egge 	p2->p_fdtol = fdtol;
527dabee6feSPeter Wemm 
52855648840SJohn Baldwin 	if (p1->p_flag2 & P2_INHERIT_PROTECTED) {
52955648840SJohn Baldwin 		p2->p_flag |= P_PROTECTED;
53055648840SJohn Baldwin 		p2->p_flag2 |= P2_INHERIT_PROTECTED;
53155648840SJohn Baldwin 	}
53255648840SJohn Baldwin 
533df8bae1dSRodney W. Grimes 	/*
534c8564ad4SBruce Evans 	 * p_limit is copy-on-write.  Bump its refcount.
535df8bae1dSRodney W. Grimes 	 */
5361c4bcd05SJeff Roberson 	lim_fork(p1, p2);
5378b059651SDavid Schultz 
5384ea6a9a2SMateusz Guzik 	thread_cow_get_proc(td2, p2);
5394ea6a9a2SMateusz Guzik 
5408b059651SDavid Schultz 	pstats_fork(p1->p_stats, p2->p_stats);
5418b059651SDavid Schultz 
542299bc736SDavid Schultz 	PROC_UNLOCK(p1);
543cda5aba4SDavid Schultz 	PROC_UNLOCK(p2);
544df8bae1dSRodney W. Grimes 
5453e73ff1eSEdward Tomasz Napierala 	/* Bump references to the text vnode (for procfs). */
546a69d88afSPeter Wemm 	if (p2->p_textvp)
547a69d88afSPeter Wemm 		vref(p2->p_textvp);
548a69d88afSPeter Wemm 
549c6544064SJohn Baldwin 	/*
550c8564ad4SBruce Evans 	 * Set up linkage for kernel based threading.
551c6544064SJohn Baldwin 	 */
552c6544064SJohn Baldwin 	if ((flags & RFTHREAD) != 0) {
553c6544064SJohn Baldwin 		mtx_lock(&ppeers_lock);
554c6544064SJohn Baldwin 		p2->p_peers = p1->p_peers;
555c6544064SJohn Baldwin 		p1->p_peers = p2;
556c6544064SJohn Baldwin 		p2->p_leader = p1->p_leader;
557c6544064SJohn Baldwin 		mtx_unlock(&ppeers_lock);
558c6544064SJohn Baldwin 		PROC_LOCK(p1->p_leader);
559c6544064SJohn Baldwin 		if ((p1->p_leader->p_flag & P_WEXIT) != 0) {
560c6544064SJohn Baldwin 			PROC_UNLOCK(p1->p_leader);
561c6544064SJohn Baldwin 			/*
562c6544064SJohn Baldwin 			 * The task leader is exiting, so process p1 is
563c6544064SJohn Baldwin 			 * going to be killed shortly.  Since p1 obviously
564c6544064SJohn Baldwin 			 * isn't dead yet, we know that the leader is either
565c6544064SJohn Baldwin 			 * sending SIGKILL's to all the processes in this
566c6544064SJohn Baldwin 			 * task or is sleeping waiting for all the peers to
567c6544064SJohn Baldwin 			 * exit.  We let p1 complete the fork, but we need
568c6544064SJohn Baldwin 			 * to go ahead and kill the new process p2 since
569c6544064SJohn Baldwin 			 * the task leader may not get a chance to send
570c6544064SJohn Baldwin 			 * SIGKILL to it.  We leave it on the list so that
571c6544064SJohn Baldwin 			 * the task leader will wait for this new process
572c6544064SJohn Baldwin 			 * to commit suicide.
573c6544064SJohn Baldwin 			 */
574c6544064SJohn Baldwin 			PROC_LOCK(p2);
5758451d0ddSKip Macy 			kern_psignal(p2, SIGKILL);
576c6544064SJohn Baldwin 			PROC_UNLOCK(p2);
577293d2d22SRobert Watson 		} else
578293d2d22SRobert Watson 			PROC_UNLOCK(p1->p_leader);
579c6544064SJohn Baldwin 	} else {
580c6544064SJohn Baldwin 		p2->p_peers = NULL;
581c6544064SJohn Baldwin 		p2->p_leader = p2;
582c6544064SJohn Baldwin 	}
583c6544064SJohn Baldwin 
5843fc755c1SJohn Baldwin 	sx_xlock(&proctree_lock);
5853fc755c1SJohn Baldwin 	PGRP_LOCK(p1->p_pgrp);
5863fc755c1SJohn Baldwin 	PROC_LOCK(p2);
5873fc755c1SJohn Baldwin 	PROC_LOCK(p1);
5883fc755c1SJohn Baldwin 
58970e534e7SDavid Greenman 	/*
5909752f794SJohn Baldwin 	 * Preserve some more flags in subprocess.  P_PROFIL has already
591be67169aSBruce Evans 	 * been preserved.
59270e534e7SDavid Greenman 	 */
593a30ec4b9SDavid Xu 	p2->p_flag |= p1->p_flag & P_SUGID;
594a30ec4b9SDavid Xu 	td2->td_pflags |= td->td_pflags & TDP_ALTSTACK;
595f591779bSSeigo Tanimura 	SESS_LOCK(p1->p_session);
596df8bae1dSRodney W. Grimes 	if (p1->p_session->s_ttyvp != NULL && p1->p_flag & P_CONTROLT)
597df8bae1dSRodney W. Grimes 		p2->p_flag |= P_CONTROLT;
598f591779bSSeigo Tanimura 	SESS_UNLOCK(p1->p_session);
5990e3eb7eeSSujal Patel 	if (flags & RFPPWAIT)
600df8bae1dSRodney W. Grimes 		p2->p_flag |= P_PPWAIT;
601be67169aSBruce Evans 
6025cded904SOlivier Houchard 	p2->p_pgrp = p1->p_pgrp;
603b75356e1SJeffrey Hsu 	LIST_INSERT_AFTER(p1, p2, p_pglist);
6042a60b9b9SSeigo Tanimura 	PGRP_UNLOCK(p1->p_pgrp);
605b75356e1SJeffrey Hsu 	LIST_INIT(&p2->p_children);
606dcd43281SKonstantin Belousov 	LIST_INIT(&p2->p_orphans);
607b75356e1SJeffrey Hsu 
608f7e50ea7SKonstantin Belousov 	callout_init_mtx(&p2->p_itcallout, &p2->p_mtx, 0);
6094f559836SJake Burkholder 
610df8bae1dSRodney W. Grimes 	/*
611df95311aSMatthew N. Dodd 	 * If PF_FORK is set, the child process inherits the
612df95311aSMatthew N. Dodd 	 * procfs ioctl flags from its parent.
613df95311aSMatthew N. Dodd 	 */
614df95311aSMatthew N. Dodd 	if (p1->p_pfsflags & PF_FORK) {
615df95311aSMatthew N. Dodd 		p2->p_stops = p1->p_stops;
616df95311aSMatthew N. Dodd 		p2->p_pfsflags = p1->p_pfsflags;
617df95311aSMatthew N. Dodd 	}
618df95311aSMatthew N. Dodd 
619df95311aSMatthew N. Dodd 	/*
620df8bae1dSRodney W. Grimes 	 * This begins the section where we must prevent the parent
621cda5aba4SDavid Schultz 	 * from being swapped.
622df8bae1dSRodney W. Grimes 	 */
623cda5aba4SDavid Schultz 	_PHOLD(p1);
62457934cd3SJohn Baldwin 	PROC_UNLOCK(p1);
6250d2afceeSDavid Greenman 
626df8bae1dSRodney W. Grimes 	/*
6273fc755c1SJohn Baldwin 	 * Attach the new process to its parent.
6283fc755c1SJohn Baldwin 	 *
6293fc755c1SJohn Baldwin 	 * If RFNOWAIT is set, the newly created process becomes a child
6303fc755c1SJohn Baldwin 	 * of init.  This effectively disassociates the child from the
6313fc755c1SJohn Baldwin 	 * parent.
6323fc755c1SJohn Baldwin 	 */
633237623b0SKonstantin Belousov 	if ((flags & RFNOWAIT) != 0) {
634237623b0SKonstantin Belousov 		pptr = p1->p_reaper;
635237623b0SKonstantin Belousov 		p2->p_reaper = pptr;
636237623b0SKonstantin Belousov 	} else {
637237623b0SKonstantin Belousov 		p2->p_reaper = (p1->p_treeflag & P_TREE_REAPER) != 0 ?
638237623b0SKonstantin Belousov 		    p1 : p1->p_reaper;
6393fc755c1SJohn Baldwin 		pptr = p1;
640237623b0SKonstantin Belousov 	}
6413fc755c1SJohn Baldwin 	p2->p_pptr = pptr;
6423fc755c1SJohn Baldwin 	LIST_INSERT_HEAD(&pptr->p_children, p2, p_sibling);
643237623b0SKonstantin Belousov 	LIST_INIT(&p2->p_reaplist);
644237623b0SKonstantin Belousov 	LIST_INSERT_HEAD(&p2->p_reaper->p_reaplist, p2, p_reapsibling);
645237623b0SKonstantin Belousov 	if (p2->p_reaper == p1)
646237623b0SKonstantin Belousov 		p2->p_reapsubtree = p2->p_pid;
6473fc755c1SJohn Baldwin 	sx_xunlock(&proctree_lock);
6483fc755c1SJohn Baldwin 
649bb0e8070SJohn Baldwin 	/* Inform accounting that we have forked. */
650bb0e8070SJohn Baldwin 	p2->p_acflag = AFORK;
651bb0e8070SJohn Baldwin 	PROC_UNLOCK(p2);
652bb0e8070SJohn Baldwin 
6537705d4b2SDmitry Chagin #ifdef KTRACE
6547705d4b2SDmitry Chagin 	ktrprocfork(p1, p2);
6557705d4b2SDmitry Chagin #endif
6567705d4b2SDmitry Chagin 
6573fc755c1SJohn Baldwin 	/*
658a2a1c95cSPeter Wemm 	 * Finish creating the child process.  It will return via a different
659a2a1c95cSPeter Wemm 	 * execution path later.  (ie: directly into user mode)
660dabee6feSPeter Wemm 	 */
66189b57fcfSKonstantin Belousov 	vm_forkproc(td, p2, td2, vm2, flags);
662df8bae1dSRodney W. Grimes 
6635d22597fSHajimu UMEMOTO 	if (flags == (RFFDG | RFPROC)) {
664393a081dSAttilio Rao 		PCPU_INC(cnt.v_forks);
665393a081dSAttilio Rao 		PCPU_ADD(cnt.v_forkpages, p2->p_vmspace->vm_dsize +
66694ddc707SAlan Cox 		    p2->p_vmspace->vm_ssize);
6675d22597fSHajimu UMEMOTO 	} else if (flags == (RFFDG | RFPROC | RFPPWAIT | RFMEM)) {
668393a081dSAttilio Rao 		PCPU_INC(cnt.v_vforks);
669393a081dSAttilio Rao 		PCPU_ADD(cnt.v_vforkpages, p2->p_vmspace->vm_dsize +
67094ddc707SAlan Cox 		    p2->p_vmspace->vm_ssize);
6715d22597fSHajimu UMEMOTO 	} else if (p1 == &proc0) {
672393a081dSAttilio Rao 		PCPU_INC(cnt.v_kthreads);
673393a081dSAttilio Rao 		PCPU_ADD(cnt.v_kthreadpages, p2->p_vmspace->vm_dsize +
67494ddc707SAlan Cox 		    p2->p_vmspace->vm_ssize);
6755d22597fSHajimu UMEMOTO 	} else {
676393a081dSAttilio Rao 		PCPU_INC(cnt.v_rforks);
677393a081dSAttilio Rao 		PCPU_ADD(cnt.v_rforkpages, p2->p_vmspace->vm_dsize +
67894ddc707SAlan Cox 		    p2->p_vmspace->vm_ssize);
6795d22597fSHajimu UMEMOTO 	}
6805d22597fSHajimu UMEMOTO 
681cfb5f768SJonathan Anderson 	/*
682cfb5f768SJonathan Anderson 	 * Associate the process descriptor with the process before anything
683cfb5f768SJonathan Anderson 	 * can happen that might cause that process to need the descriptor.
684cfb5f768SJonathan Anderson 	 * However, don't do this until after fork(2) can no longer fail.
685cfb5f768SJonathan Anderson 	 */
686cfb5f768SJonathan Anderson 	if (flags & RFPROCDESC)
687cfb5f768SJonathan Anderson 		procdesc_new(p2, pdflags);
688cfb5f768SJonathan Anderson 
689df8bae1dSRodney W. Grimes 	/*
690e9189611SPeter Wemm 	 * Both processes are set up, now check if any loadable modules want
691e0d898b4SJulian Elischer 	 * to adjust anything.
692fed06968SJulian Elischer 	 */
69375b8b3b2SJohn Baldwin 	EVENTHANDLER_INVOKE(process_fork, p1, p2, flags);
694fed06968SJulian Elischer 
695fed06968SJulian Elischer 	/*
6964c3558aaSJohn Baldwin 	 * Set the child start time and mark the process as being complete.
6974c3558aaSJohn Baldwin 	 */
6988e6fa660SJohn Baldwin 	PROC_LOCK(p2);
6998e6fa660SJohn Baldwin 	PROC_LOCK(p1);
7004c3558aaSJohn Baldwin 	microuptime(&p2->p_stats->p_start);
70111bda9b8SJeff Roberson 	PROC_SLOCK(p2);
7024c3558aaSJohn Baldwin 	p2->p_state = PRS_NORMAL;
70311bda9b8SJeff Roberson 	PROC_SUNLOCK(p2);
7046fa39a73SKonstantin Belousov 
705d3555b6fSRui Paulo #ifdef KDTRACE_HOOKS
706d3555b6fSRui Paulo 	/*
7077159310fSMark Johnston 	 * Tell the DTrace fasttrap provider about the new process so that any
7087159310fSMark Johnston 	 * tracepoints inherited from the parent can be removed. We have to do
7097159310fSMark Johnston 	 * this only after p_state is PRS_NORMAL since the fasttrap module will
7107159310fSMark Johnston 	 * use pfind() later on.
711d3555b6fSRui Paulo 	 */
7127159310fSMark Johnston 	if ((flags & RFMEM) == 0 && dtrace_fasttrap_fork)
713d3555b6fSRui Paulo 		dtrace_fasttrap_fork(p1, p2);
714d3555b6fSRui Paulo #endif
7156fa39a73SKonstantin Belousov 	if ((p1->p_flag & (P_TRACED | P_FOLLOWFORK)) == (P_TRACED |
7166fa39a73SKonstantin Belousov 	    P_FOLLOWFORK)) {
7174c3558aaSJohn Baldwin 		/*
7186fa39a73SKonstantin Belousov 		 * Arrange for debugger to receive the fork event.
7196fa39a73SKonstantin Belousov 		 *
7206fa39a73SKonstantin Belousov 		 * We can report PL_FLAG_FORKED regardless of
7216fa39a73SKonstantin Belousov 		 * P_FOLLOWFORK settings, but it does not make a sense
7226fa39a73SKonstantin Belousov 		 * for runaway child.
723df8bae1dSRodney W. Grimes 		 */
7246fa39a73SKonstantin Belousov 		td->td_dbgflags |= TDB_FORK;
7256fa39a73SKonstantin Belousov 		td->td_dbg_forked = p2->p_pid;
7266fa39a73SKonstantin Belousov 		td2->td_dbgflags |= TDB_STOPATFORK;
7276fa39a73SKonstantin Belousov 		_PHOLD(p2);
7286fa39a73SKonstantin Belousov 		p2_held = 1;
7296fa39a73SKonstantin Belousov 	}
7301d7ca9bbSKonstantin Belousov 	if (flags & RFPPWAIT) {
7311d7ca9bbSKonstantin Belousov 		td->td_pflags |= TDP_RFPPWAIT;
7321d7ca9bbSKonstantin Belousov 		td->td_rfppwait_p = p2;
7331d7ca9bbSKonstantin Belousov 	}
7348e6fa660SJohn Baldwin 	PROC_UNLOCK(p2);
7350384fff8SJason Evans 	if ((flags & RFSTOPPED) == 0) {
7366fa39a73SKonstantin Belousov 		/*
7376fa39a73SKonstantin Belousov 		 * If RFSTOPPED not requested, make child runnable and
7386fa39a73SKonstantin Belousov 		 * add to run queue.
7396fa39a73SKonstantin Belousov 		 */
74011bda9b8SJeff Roberson 		thread_lock(td2);
74171fad9fdSJulian Elischer 		TD_SET_CAN_RUN(td2);
742f0393f06SJeff Roberson 		sched_add(td2, SRQ_BORING);
74311bda9b8SJeff Roberson 		thread_unlock(td2);
7440384fff8SJason Evans 	}
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes 	/*
747df8bae1dSRodney W. Grimes 	 * Now can be swapped.
748df8bae1dSRodney W. Grimes 	 */
74957934cd3SJohn Baldwin 	_PRELE(p1);
7507054ee4eSKonstantin Belousov 	PROC_UNLOCK(p1);
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	/*
75370fca427SJohn Baldwin 	 * Tell any interested parties about the new process.
754cb679c38SJonathan Lemon 	 */
7557054ee4eSKonstantin Belousov 	knote_fork(&p1->p_klist, p2->p_pid);
7565d217f17SJohn Birrell 	SDT_PROBE(proc, kernel, , create, p2, p1, flags, 0, 0);
7575d217f17SJohn Birrell 
758cb679c38SJonathan Lemon 	/*
7596fa39a73SKonstantin Belousov 	 * Wait until debugger is attached to child.
7606fa39a73SKonstantin Belousov 	 */
7616fa39a73SKonstantin Belousov 	PROC_LOCK(p2);
7626fa39a73SKonstantin Belousov 	while ((td2->td_dbgflags & TDB_STOPATFORK) != 0)
7636fa39a73SKonstantin Belousov 		cv_wait(&p2->p_dbgwait, &p2->p_mtx);
7646fa39a73SKonstantin Belousov 	if (p2_held)
7656fa39a73SKonstantin Belousov 		_PRELE(p2);
76657934cd3SJohn Baldwin 	PROC_UNLOCK(p2);
767afd01097SEdward Tomasz Napierala }
768afd01097SEdward Tomasz Napierala 
769afd01097SEdward Tomasz Napierala int
770cfb5f768SJonathan Anderson fork1(struct thread *td, int flags, int pages, struct proc **procp,
771367a13f9SEd Schouten     int *procdescp, int pdflags, struct filecaps *fcaps)
772afd01097SEdward Tomasz Napierala {
773afd01097SEdward Tomasz Napierala 	struct proc *p1;
774afd01097SEdward Tomasz Napierala 	struct proc *newproc;
775afd01097SEdward Tomasz Napierala 	int ok;
776afd01097SEdward Tomasz Napierala 	struct thread *td2;
777afd01097SEdward Tomasz Napierala 	struct vmspace *vm2;
778afd01097SEdward Tomasz Napierala 	vm_ooffset_t mem_charged;
779afd01097SEdward Tomasz Napierala 	int error;
780afd01097SEdward Tomasz Napierala 	static int curfail;
781afd01097SEdward Tomasz Napierala 	static struct timeval lastfail;
782cfb5f768SJonathan Anderson 	struct file *fp_procdesc = NULL;
783afd01097SEdward Tomasz Napierala 
784f49d8202SKonstantin Belousov 	/* Check for the undefined or unimplemented flags. */
785f49d8202SKonstantin Belousov 	if ((flags & ~(RFFLAGS | RFTSIGFLAGS(RFTSIGMASK))) != 0)
786f49d8202SKonstantin Belousov 		return (EINVAL);
787f49d8202SKonstantin Belousov 
788f49d8202SKonstantin Belousov 	/* Signal value requires RFTSIGZMB. */
789f49d8202SKonstantin Belousov 	if ((flags & RFTSIGFLAGS(RFTSIGMASK)) != 0 && (flags & RFTSIGZMB) == 0)
790f49d8202SKonstantin Belousov 		return (EINVAL);
791f49d8202SKonstantin Belousov 
792afd01097SEdward Tomasz Napierala 	/* Can't copy and clear. */
793afd01097SEdward Tomasz Napierala 	if ((flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG))
794afd01097SEdward Tomasz Napierala 		return (EINVAL);
795afd01097SEdward Tomasz Napierala 
796f49d8202SKonstantin Belousov 	/* Check the validity of the signal number. */
797f49d8202SKonstantin Belousov 	if ((flags & RFTSIGZMB) != 0 && (u_int)RFTSIGNUM(flags) > _SIG_MAXSIG)
798f49d8202SKonstantin Belousov 		return (EINVAL);
799f49d8202SKonstantin Belousov 
800cfb5f768SJonathan Anderson 	if ((flags & RFPROCDESC) != 0) {
801cfb5f768SJonathan Anderson 		/* Can't not create a process yet get a process descriptor. */
802cfb5f768SJonathan Anderson 		if ((flags & RFPROC) == 0)
803cfb5f768SJonathan Anderson 			return (EINVAL);
804cfb5f768SJonathan Anderson 
805cfb5f768SJonathan Anderson 		/* Must provide a place to put a procdesc if creating one. */
806cfb5f768SJonathan Anderson 		if (procdescp == NULL)
807cfb5f768SJonathan Anderson 			return (EINVAL);
808cfb5f768SJonathan Anderson 	}
809cfb5f768SJonathan Anderson 
810afd01097SEdward Tomasz Napierala 	p1 = td->td_proc;
811afd01097SEdward Tomasz Napierala 
812afd01097SEdward Tomasz Napierala 	/*
813afd01097SEdward Tomasz Napierala 	 * Here we don't create a new process, but we divorce
814afd01097SEdward Tomasz Napierala 	 * certain parts of a process from itself.
815afd01097SEdward Tomasz Napierala 	 */
8163e73ff1eSEdward Tomasz Napierala 	if ((flags & RFPROC) == 0) {
8173e73ff1eSEdward Tomasz Napierala 		*procp = NULL;
8183e73ff1eSEdward Tomasz Napierala 		return (fork_norfproc(td, flags));
8193e73ff1eSEdward Tomasz Napierala 	}
820afd01097SEdward Tomasz Napierala 
821cfb5f768SJonathan Anderson 	/*
822cfb5f768SJonathan Anderson 	 * If required, create a process descriptor in the parent first; we
823cfb5f768SJonathan Anderson 	 * will abandon it if something goes wrong. We don't finit() until
824cfb5f768SJonathan Anderson 	 * later.
825cfb5f768SJonathan Anderson 	 */
826cfb5f768SJonathan Anderson 	if (flags & RFPROCDESC) {
827*6236e71bSEd Schouten 		error = falloc_caps(td, &fp_procdesc, procdescp, 0, fcaps);
828b38520f0SEdward Tomasz Napierala 		if (error != 0)
829cfb5f768SJonathan Anderson 			return (error);
830cfb5f768SJonathan Anderson 	}
831cfb5f768SJonathan Anderson 
832afd01097SEdward Tomasz Napierala 	mem_charged = 0;
833afd01097SEdward Tomasz Napierala 	vm2 = NULL;
834afd01097SEdward Tomasz Napierala 	if (pages == 0)
835afd01097SEdward Tomasz Napierala 		pages = KSTACK_PAGES;
836afd01097SEdward Tomasz Napierala 	/* Allocate new proc. */
837afd01097SEdward Tomasz Napierala 	newproc = uma_zalloc(proc_zone, M_WAITOK);
838afd01097SEdward Tomasz Napierala 	td2 = FIRST_THREAD_IN_PROC(newproc);
839afd01097SEdward Tomasz Napierala 	if (td2 == NULL) {
840afd01097SEdward Tomasz Napierala 		td2 = thread_alloc(pages);
841afd01097SEdward Tomasz Napierala 		if (td2 == NULL) {
842afd01097SEdward Tomasz Napierala 			error = ENOMEM;
84312cec311SMateusz Guzik 			goto fail2;
844afd01097SEdward Tomasz Napierala 		}
845afd01097SEdward Tomasz Napierala 		proc_linkup(newproc, td2);
846afd01097SEdward Tomasz Napierala 	} else {
847afd01097SEdward Tomasz Napierala 		if (td2->td_kstack == 0 || td2->td_kstack_pages != pages) {
848afd01097SEdward Tomasz Napierala 			if (td2->td_kstack != 0)
849afd01097SEdward Tomasz Napierala 				vm_thread_dispose(td2);
850afd01097SEdward Tomasz Napierala 			if (!thread_alloc_stack(td2, pages)) {
851afd01097SEdward Tomasz Napierala 				error = ENOMEM;
85212cec311SMateusz Guzik 				goto fail2;
853afd01097SEdward Tomasz Napierala 			}
854afd01097SEdward Tomasz Napierala 		}
855afd01097SEdward Tomasz Napierala 	}
856afd01097SEdward Tomasz Napierala 
857afd01097SEdward Tomasz Napierala 	if ((flags & RFMEM) == 0) {
858afd01097SEdward Tomasz Napierala 		vm2 = vmspace_fork(p1->p_vmspace, &mem_charged);
859afd01097SEdward Tomasz Napierala 		if (vm2 == NULL) {
860afd01097SEdward Tomasz Napierala 			error = ENOMEM;
86112cec311SMateusz Guzik 			goto fail2;
862afd01097SEdward Tomasz Napierala 		}
863afd01097SEdward Tomasz Napierala 		if (!swap_reserve(mem_charged)) {
864afd01097SEdward Tomasz Napierala 			/*
865afd01097SEdward Tomasz Napierala 			 * The swap reservation failed. The accounting
866afd01097SEdward Tomasz Napierala 			 * from the entries of the copied vm2 will be
867afd01097SEdward Tomasz Napierala 			 * substracted in vmspace_free(), so force the
868afd01097SEdward Tomasz Napierala 			 * reservation there.
869afd01097SEdward Tomasz Napierala 			 */
870afd01097SEdward Tomasz Napierala 			swap_reserve_force(mem_charged);
871afd01097SEdward Tomasz Napierala 			error = ENOMEM;
87212cec311SMateusz Guzik 			goto fail2;
873afd01097SEdward Tomasz Napierala 		}
874afd01097SEdward Tomasz Napierala 	} else
875afd01097SEdward Tomasz Napierala 		vm2 = NULL;
876afd01097SEdward Tomasz Napierala 
877097055e2SEdward Tomasz Napierala 	/*
878097055e2SEdward Tomasz Napierala 	 * XXX: This is ugly; when we copy resource usage, we need to bump
879097055e2SEdward Tomasz Napierala 	 *      per-cred resource counters.
880097055e2SEdward Tomasz Napierala 	 */
881ffb34484SMateusz Guzik 	proc_set_cred_init(newproc, crhold(td->td_ucred));
882097055e2SEdward Tomasz Napierala 
883097055e2SEdward Tomasz Napierala 	/*
884097055e2SEdward Tomasz Napierala 	 * Initialize resource accounting for the child process.
885097055e2SEdward Tomasz Napierala 	 */
886097055e2SEdward Tomasz Napierala 	error = racct_proc_fork(p1, newproc);
887097055e2SEdward Tomasz Napierala 	if (error != 0) {
888097055e2SEdward Tomasz Napierala 		error = EAGAIN;
889097055e2SEdward Tomasz Napierala 		goto fail1;
890097055e2SEdward Tomasz Napierala 	}
891097055e2SEdward Tomasz Napierala 
8921dbf9dccSEdward Tomasz Napierala #ifdef MAC
8931dbf9dccSEdward Tomasz Napierala 	mac_proc_init(newproc);
8941dbf9dccSEdward Tomasz Napierala #endif
8951dbf9dccSEdward Tomasz Napierala 	knlist_init_mtx(&newproc->p_klist, &newproc->p_mtx);
8961dbf9dccSEdward Tomasz Napierala 	STAILQ_INIT(&newproc->p_ktr);
8971dbf9dccSEdward Tomasz Napierala 
898afd01097SEdward Tomasz Napierala 	/* We have to lock the process tree while we look for a pid. */
899afd01097SEdward Tomasz Napierala 	sx_slock(&proctree_lock);
900afd01097SEdward Tomasz Napierala 
901afd01097SEdward Tomasz Napierala 	/*
902afd01097SEdward Tomasz Napierala 	 * Although process entries are dynamically created, we still keep
903afd01097SEdward Tomasz Napierala 	 * a global limit on the maximum number we will create.  Don't allow
904afd01097SEdward Tomasz Napierala 	 * a nonprivileged user to use the last ten processes; don't let root
905afd01097SEdward Tomasz Napierala 	 * exceed the limit. The variable nprocs is the current number of
906afd01097SEdward Tomasz Napierala 	 * processes, maxproc is the limit.
907afd01097SEdward Tomasz Napierala 	 */
908afd01097SEdward Tomasz Napierala 	sx_xlock(&allproc_lock);
909afd01097SEdward Tomasz Napierala 	if ((nprocs >= maxproc - 10 && priv_check_cred(td->td_ucred,
910afd01097SEdward Tomasz Napierala 	    PRIV_MAXPROC, 0) != 0) || nprocs >= maxproc) {
911afd01097SEdward Tomasz Napierala 		error = EAGAIN;
912afd01097SEdward Tomasz Napierala 		goto fail;
913afd01097SEdward Tomasz Napierala 	}
914afd01097SEdward Tomasz Napierala 
91558c77a9dSEdward Tomasz Napierala 	/*
916afd01097SEdward Tomasz Napierala 	 * Increment the count of procs running with this uid. Don't allow
917afd01097SEdward Tomasz Napierala 	 * a nonprivileged user to exceed their current limit.
918afd01097SEdward Tomasz Napierala 	 *
919afd01097SEdward Tomasz Napierala 	 * XXXRW: Can we avoid privilege here if it's not needed?
920afd01097SEdward Tomasz Napierala 	 */
921afd01097SEdward Tomasz Napierala 	error = priv_check_cred(td->td_ucred, PRIV_PROC_LIMIT, 0);
922afd01097SEdward Tomasz Napierala 	if (error == 0)
923afd01097SEdward Tomasz Napierala 		ok = chgproccnt(td->td_ucred->cr_ruidinfo, 1, 0);
924afd01097SEdward Tomasz Napierala 	else {
925afd01097SEdward Tomasz Napierala 		ok = chgproccnt(td->td_ucred->cr_ruidinfo, 1,
926f6f6d240SMateusz Guzik 		    lim_cur(td, RLIMIT_NPROC));
927afd01097SEdward Tomasz Napierala 	}
928afd01097SEdward Tomasz Napierala 	if (ok) {
929cfb5f768SJonathan Anderson 		do_fork(td, flags, newproc, td2, vm2, pdflags);
930afd01097SEdward Tomasz Napierala 
93149539972SJulian Elischer 		/*
932df8abd0bSPeter Wemm 		 * Return child proc pointer to parent.
933df8bae1dSRodney W. Grimes 		 */
934afd01097SEdward Tomasz Napierala 		*procp = newproc;
9350a7007b9SPawel Jakub Dawidek 		if (flags & RFPROCDESC) {
936cfb5f768SJonathan Anderson 			procdesc_finit(newproc->p_procdesc, fp_procdesc);
9370a7007b9SPawel Jakub Dawidek 			fdrop(fp_procdesc, td);
9380a7007b9SPawel Jakub Dawidek 		}
93972a401d9SEdward Tomasz Napierala 		racct_proc_fork_done(newproc);
940df8bae1dSRodney W. Grimes 		return (0);
941afd01097SEdward Tomasz Napierala 	}
942afd01097SEdward Tomasz Napierala 
943afd01097SEdward Tomasz Napierala 	error = EAGAIN;
944c6544064SJohn Baldwin fail:
9455ce2f678SJohn Baldwin 	sx_sunlock(&proctree_lock);
946b083ea51SMike Silbersack 	if (ppsratecheck(&lastfail, &curfail, 1))
947a208417cSJaakko Heinonen 		printf("maxproc limit exceeded by uid %u (pid %d); see tuning(7) and login.conf(5)\n",
948a208417cSJaakko Heinonen 		    td->td_ucred->cr_ruid, p1->p_pid);
949c6544064SJohn Baldwin 	sx_xunlock(&allproc_lock);
9506bea667fSRobert Watson #ifdef MAC
95130d239bcSRobert Watson 	mac_proc_destroy(newproc);
9526bea667fSRobert Watson #endif
9531dbf9dccSEdward Tomasz Napierala 	racct_proc_exit(newproc);
954ab27d5d8SEdward Tomasz Napierala fail1:
955edf1796dSMateusz Guzik 	crfree(newproc->p_ucred);
956edf1796dSMateusz Guzik 	newproc->p_ucred = NULL;
95712cec311SMateusz Guzik fail2:
95869aa768aSKonstantin Belousov 	if (vm2 != NULL)
95969aa768aSKonstantin Belousov 		vmspace_free(vm2);
960c6544064SJohn Baldwin 	uma_zfree(proc_zone, newproc);
961de265498SPawel Jakub Dawidek 	if ((flags & RFPROCDESC) != 0 && fp_procdesc != NULL) {
96290f54cbfSMateusz Guzik 		fdclose(td, fp_procdesc, *procdescp);
963cfb5f768SJonathan Anderson 		fdrop(fp_procdesc, td);
9640a7007b9SPawel Jakub Dawidek 	}
96584d37a46SJohn Baldwin 	pause("fork", hz / 2);
966c6544064SJohn Baldwin 	return (error);
967df8bae1dSRodney W. Grimes }
968fed06968SJulian Elischer 
969e0d898b4SJulian Elischer /*
970a7b124c3SJohn Baldwin  * Handle the return of a child process from fork1().  This function
971a7b124c3SJohn Baldwin  * is called from the MD fork_trampoline() entry point.
972a7b124c3SJohn Baldwin  */
973a7b124c3SJohn Baldwin void
9741d845e86SEdward Tomasz Napierala fork_exit(void (*callout)(void *, struct trapframe *), void *arg,
9751d845e86SEdward Tomasz Napierala     struct trapframe *frame)
976a7b124c3SJohn Baldwin {
977696058c3SJulian Elischer 	struct proc *p;
97870fca427SJohn Baldwin 	struct thread *td;
979fe54587fSJeff Roberson 	struct thread *dtd;
98070fca427SJohn Baldwin 
9810047b9a9SBosko Milekic 	td = curthread;
9820047b9a9SBosko Milekic 	p = td->td_proc;
9830047b9a9SBosko Milekic 	KASSERT(p->p_state == PRS_NORMAL, ("executing process is still new"));
9840047b9a9SBosko Milekic 
9856617724cSJeff Roberson 	CTR4(KTR_PROC, "fork_exit: new thread %p (td_sched %p, pid %d, %s)",
986e01eafefSJulian Elischer 		td, td->td_sched, p->p_pid, td->td_name);
9870047b9a9SBosko Milekic 
98811bda9b8SJeff Roberson 	sched_fork_exit(td);
989a7b124c3SJohn Baldwin 	/*
990fe54587fSJeff Roberson 	* Processes normally resume in mi_switch() after being
991fe54587fSJeff Roberson 	* cpu_switch()'ed to, but when children start up they arrive here
992fe54587fSJeff Roberson 	* instead, so we must do much the same things as mi_switch() would.
993fe54587fSJeff Roberson 	*/
994fe54587fSJeff Roberson 	if ((dtd = PCPU_GET(deadthread))) {
995fe54587fSJeff Roberson 		PCPU_SET(deadthread, NULL);
996fe54587fSJeff Roberson 		thread_stash(dtd);
997fe54587fSJeff Roberson 	}
998fe54587fSJeff Roberson 	thread_unlock(td);
999fe54587fSJeff Roberson 
1000fe54587fSJeff Roberson 	/*
1001a7b124c3SJohn Baldwin 	 * cpu_set_fork_handler intercepts this function call to
1002a7b124c3SJohn Baldwin 	 * have this call a non-return function to stay in kernel mode.
1003a7b124c3SJohn Baldwin 	 * initproc has its own fork handler, but it does return.
1004a7b124c3SJohn Baldwin 	 */
10055813dc03SJohn Baldwin 	KASSERT(callout != NULL, ("NULL callout in fork_exit"));
10068865286bSJohn Baldwin 	callout(arg, frame);
1007a7b124c3SJohn Baldwin 
1008a7b124c3SJohn Baldwin 	/*
1009a7b124c3SJohn Baldwin 	 * Check if a kernel thread misbehaved and returned from its main
1010a7b124c3SJohn Baldwin 	 * function.
1011a7b124c3SJohn Baldwin 	 */
1012a7b124c3SJohn Baldwin 	if (p->p_flag & P_KTHREAD) {
1013a7b124c3SJohn Baldwin 		printf("Kernel thread \"%s\" (pid %d) exited prematurely.\n",
1014e01eafefSJulian Elischer 		    td->td_name, p->p_pid);
10153745c395SJulian Elischer 		kproc_exit(0);
1016a7b124c3SJohn Baldwin 	}
1017a7b124c3SJohn Baldwin 	mtx_assert(&Giant, MA_NOTOWNED);
1018993182e5SAlexander Leidinger 
1019e5d81ef1SDmitry Chagin 	if (p->p_sysent->sv_schedtail != NULL)
1020e5d81ef1SDmitry Chagin 		(p->p_sysent->sv_schedtail)(td);
1021a7b124c3SJohn Baldwin }
1022a7b124c3SJohn Baldwin 
1023a7b124c3SJohn Baldwin /*
1024a7b124c3SJohn Baldwin  * Simplified back end of syscall(), used when returning from fork()
1025a7b124c3SJohn Baldwin  * directly into user mode.  Giant is not held on entry, and must not
1026a7b124c3SJohn Baldwin  * be held on return.  This function is passed in to fork_exit() as the
1027a7b124c3SJohn Baldwin  * first parameter and is called when returning to a new userland process.
1028a7b124c3SJohn Baldwin  */
1029a7b124c3SJohn Baldwin void
10301d845e86SEdward Tomasz Napierala fork_return(struct thread *td, struct trapframe *frame)
1031a7b124c3SJohn Baldwin {
10326fa39a73SKonstantin Belousov 	struct proc *p, *dbg;
10336fa39a73SKonstantin Belousov 
10346fa39a73SKonstantin Belousov 	if (td->td_dbgflags & TDB_STOPATFORK) {
10356fa39a73SKonstantin Belousov 		p = td->td_proc;
10366fa39a73SKonstantin Belousov 		sx_xlock(&proctree_lock);
10376fa39a73SKonstantin Belousov 		PROC_LOCK(p);
10386fa39a73SKonstantin Belousov 		if ((p->p_pptr->p_flag & (P_TRACED | P_FOLLOWFORK)) ==
10396fa39a73SKonstantin Belousov 		    (P_TRACED | P_FOLLOWFORK)) {
10406fa39a73SKonstantin Belousov 			/*
10416fa39a73SKonstantin Belousov 			 * If debugger still wants auto-attach for the
10426fa39a73SKonstantin Belousov 			 * parent's children, do it now.
10436fa39a73SKonstantin Belousov 			 */
10446fa39a73SKonstantin Belousov 			dbg = p->p_pptr->p_pptr;
10456fa39a73SKonstantin Belousov 			p->p_flag |= P_TRACED;
10466fa39a73SKonstantin Belousov 			p->p_oppid = p->p_pptr->p_pid;
1047515b7a0bSJohn Baldwin 			CTR2(KTR_PTRACE,
1048515b7a0bSJohn Baldwin 		    "fork_return: attaching to new child pid %d: oppid %d",
1049515b7a0bSJohn Baldwin 			    p->p_pid, p->p_oppid);
10506fa39a73SKonstantin Belousov 			proc_reparent(p, dbg);
10516fa39a73SKonstantin Belousov 			sx_xunlock(&proctree_lock);
1052db327339SKonstantin Belousov 			td->td_dbgflags |= TDB_CHILD;
10536fa39a73SKonstantin Belousov 			ptracestop(td, SIGSTOP);
1054db327339SKonstantin Belousov 			td->td_dbgflags &= ~TDB_CHILD;
10556fa39a73SKonstantin Belousov 		} else {
10566fa39a73SKonstantin Belousov 			/*
10576fa39a73SKonstantin Belousov 			 * ... otherwise clear the request.
10586fa39a73SKonstantin Belousov 			 */
10596fa39a73SKonstantin Belousov 			sx_xunlock(&proctree_lock);
10606fa39a73SKonstantin Belousov 			td->td_dbgflags &= ~TDB_STOPATFORK;
10616fa39a73SKonstantin Belousov 			cv_broadcast(&p->p_dbgwait);
10626fa39a73SKonstantin Belousov 		}
10636fa39a73SKonstantin Belousov 		PROC_UNLOCK(p);
10646fa39a73SKonstantin Belousov 	}
1065a7b124c3SJohn Baldwin 
1066eb2da9a5SPoul-Henning Kamp 	userret(td, frame);
10676fa39a73SKonstantin Belousov 
1068a7b124c3SJohn Baldwin #ifdef KTRACE
1069af300f23SJohn Baldwin 	if (KTRPOINT(td, KTR_SYSRET))
1070af300f23SJohn Baldwin 		ktrsysret(SYS_fork, 0, 0);
1071a7b124c3SJohn Baldwin #endif
1072a7b124c3SJohn Baldwin }
1073