xref: /onnv-gate/usr/src/uts/common/os/fork.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/types.h>
34*0Sstevel@tonic-gate #include <sys/param.h>
35*0Sstevel@tonic-gate #include <sys/sysmacros.h>
36*0Sstevel@tonic-gate #include <sys/signal.h>
37*0Sstevel@tonic-gate #include <sys/cred.h>
38*0Sstevel@tonic-gate #include <sys/policy.h>
39*0Sstevel@tonic-gate #include <sys/user.h>
40*0Sstevel@tonic-gate #include <sys/systm.h>
41*0Sstevel@tonic-gate #include <sys/cpuvar.h>
42*0Sstevel@tonic-gate #include <sys/vfs.h>
43*0Sstevel@tonic-gate #include <sys/vnode.h>
44*0Sstevel@tonic-gate #include <sys/file.h>
45*0Sstevel@tonic-gate #include <sys/errno.h>
46*0Sstevel@tonic-gate #include <sys/time.h>
47*0Sstevel@tonic-gate #include <sys/proc.h>
48*0Sstevel@tonic-gate #include <sys/cmn_err.h>
49*0Sstevel@tonic-gate #include <sys/acct.h>
50*0Sstevel@tonic-gate #include <sys/tuneable.h>
51*0Sstevel@tonic-gate #include <sys/class.h>
52*0Sstevel@tonic-gate #include <sys/kmem.h>
53*0Sstevel@tonic-gate #include <sys/session.h>
54*0Sstevel@tonic-gate #include <sys/ucontext.h>
55*0Sstevel@tonic-gate #include <sys/stack.h>
56*0Sstevel@tonic-gate #include <sys/procfs.h>
57*0Sstevel@tonic-gate #include <sys/prsystm.h>
58*0Sstevel@tonic-gate #include <sys/vmsystm.h>
59*0Sstevel@tonic-gate #include <sys/vtrace.h>
60*0Sstevel@tonic-gate #include <sys/debug.h>
61*0Sstevel@tonic-gate #include <sys/shm_impl.h>
62*0Sstevel@tonic-gate #include <sys/door_data.h>
63*0Sstevel@tonic-gate #include <vm/as.h>
64*0Sstevel@tonic-gate #include <vm/rm.h>
65*0Sstevel@tonic-gate #include <c2/audit.h>
66*0Sstevel@tonic-gate #include <sys/var.h>
67*0Sstevel@tonic-gate #include <sys/schedctl.h>
68*0Sstevel@tonic-gate #include <sys/utrap.h>
69*0Sstevel@tonic-gate #include <sys/task.h>
70*0Sstevel@tonic-gate #include <sys/resource.h>
71*0Sstevel@tonic-gate #include <sys/cyclic.h>
72*0Sstevel@tonic-gate #include <sys/lgrp.h>
73*0Sstevel@tonic-gate #include <sys/rctl.h>
74*0Sstevel@tonic-gate #include <sys/contract_impl.h>
75*0Sstevel@tonic-gate #include <sys/contract/process_impl.h>
76*0Sstevel@tonic-gate #include <sys/list.h>
77*0Sstevel@tonic-gate #include <sys/dtrace.h>
78*0Sstevel@tonic-gate #include <sys/pool.h>
79*0Sstevel@tonic-gate #include <sys/zone.h>
80*0Sstevel@tonic-gate #include <sys/sdt.h>
81*0Sstevel@tonic-gate #include <sys/class.h>
82*0Sstevel@tonic-gate #include <sys/corectl.h>
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate static int64_t cfork(int, int);
85*0Sstevel@tonic-gate static int getproc(proc_t **, int);
86*0Sstevel@tonic-gate static void fork_fail(proc_t *);
87*0Sstevel@tonic-gate static void forklwp_fail(proc_t *);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate int fork_fail_pending;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate extern struct kmem_cache *process_cache;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate  * forkall system call.
95*0Sstevel@tonic-gate  */
96*0Sstevel@tonic-gate int64_t
97*0Sstevel@tonic-gate forkall(void)
98*0Sstevel@tonic-gate {
99*0Sstevel@tonic-gate 	return (cfork(0, 0));
100*0Sstevel@tonic-gate }
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * The parent is stopped until the child invokes relvm().
104*0Sstevel@tonic-gate  */
105*0Sstevel@tonic-gate int64_t
106*0Sstevel@tonic-gate vfork(void)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* so vfwait() will be called */
109*0Sstevel@tonic-gate 	return (cfork(1, 1));
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate  * fork1 system call
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate int64_t
116*0Sstevel@tonic-gate fork1(void)
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	return (cfork(0, 1));
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate /* ARGSUSED */
122*0Sstevel@tonic-gate static int64_t
123*0Sstevel@tonic-gate cfork(int isvfork, int isfork1)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
126*0Sstevel@tonic-gate 	struct as *as;
127*0Sstevel@tonic-gate 	proc_t *cp, **orphpp;
128*0Sstevel@tonic-gate 	klwp_t *clone;
129*0Sstevel@tonic-gate 	kthread_t *t;
130*0Sstevel@tonic-gate 	task_t *tk;
131*0Sstevel@tonic-gate 	rval_t	r;
132*0Sstevel@tonic-gate 	int error;
133*0Sstevel@tonic-gate 	int i;
134*0Sstevel@tonic-gate 	rctl_set_t *dup_set;
135*0Sstevel@tonic-gate 	rctl_alloc_gp_t *dup_gp;
136*0Sstevel@tonic-gate 	rctl_entity_p_t e;
137*0Sstevel@tonic-gate 	lwpdir_t *ldp;
138*0Sstevel@tonic-gate 	lwpent_t *lep;
139*0Sstevel@tonic-gate 	lwpent_t *clep;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 * fork is not supported for the /proc agent lwp.
143*0Sstevel@tonic-gate 	 */
144*0Sstevel@tonic-gate 	if (curthread == p->p_agenttp) {
145*0Sstevel@tonic-gate 		error = ENOTSUP;
146*0Sstevel@tonic-gate 		goto forkerr;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	if ((error = secpolicy_basic_fork(CRED())) != 0)
150*0Sstevel@tonic-gate 		goto forkerr;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/*
153*0Sstevel@tonic-gate 	 * If the calling lwp is doing a fork1() then the
154*0Sstevel@tonic-gate 	 * other lwps in this process are not duplicated and
155*0Sstevel@tonic-gate 	 * don't need to be held where their kernel stacks can be
156*0Sstevel@tonic-gate 	 * cloned.  If doing forkall(), the process is held with
157*0Sstevel@tonic-gate 	 * SHOLDFORK, so that the lwps are at a point where their
158*0Sstevel@tonic-gate 	 * stacks can be copied which is on entry or exit from
159*0Sstevel@tonic-gate 	 * the kernel.
160*0Sstevel@tonic-gate 	 */
161*0Sstevel@tonic-gate 	if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) {
162*0Sstevel@tonic-gate 		aston(curthread);
163*0Sstevel@tonic-gate 		error = EINTR;
164*0Sstevel@tonic-gate 		goto forkerr;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate #if defined(__sparc)
168*0Sstevel@tonic-gate 	/*
169*0Sstevel@tonic-gate 	 * Ensure that the user stack is fully constructed
170*0Sstevel@tonic-gate 	 * before creating the child process structure.
171*0Sstevel@tonic-gate 	 */
172*0Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
173*0Sstevel@tonic-gate #endif
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/*
176*0Sstevel@tonic-gate 	 * Prevent our resource set associations from being changed during fork.
177*0Sstevel@tonic-gate 	 */
178*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
179*0Sstevel@tonic-gate 	pool_barrier_enter();
180*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	/*
183*0Sstevel@tonic-gate 	 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes.
184*0Sstevel@tonic-gate 	 */
185*0Sstevel@tonic-gate 	if (getproc(&cp, 0) < 0) {
186*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
187*0Sstevel@tonic-gate 		pool_barrier_exit();
188*0Sstevel@tonic-gate 		continuelwps(p);
189*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
190*0Sstevel@tonic-gate 		error = EAGAIN;
191*0Sstevel@tonic-gate 		goto forkerr;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p);
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	/*
197*0Sstevel@tonic-gate 	 * Assign an address space to child
198*0Sstevel@tonic-gate 	 */
199*0Sstevel@tonic-gate 	if (isvfork) {
200*0Sstevel@tonic-gate 		/*
201*0Sstevel@tonic-gate 		 * Clear any watched areas and remember the
202*0Sstevel@tonic-gate 		 * watched pages for restoring in vfwait().
203*0Sstevel@tonic-gate 		 */
204*0Sstevel@tonic-gate 		as = p->p_as;
205*0Sstevel@tonic-gate 		if (avl_numnodes(&as->a_wpage) != 0) {
206*0Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
207*0Sstevel@tonic-gate 			as_clearwatch(as);
208*0Sstevel@tonic-gate 			p->p_wpage = as->a_wpage;
209*0Sstevel@tonic-gate 			avl_create(&as->a_wpage, wp_compare,
210*0Sstevel@tonic-gate 			    sizeof (struct watched_page),
211*0Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
212*0Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 		cp->p_as = as;
215*0Sstevel@tonic-gate 		cp->p_flag |= SVFORK;
216*0Sstevel@tonic-gate 	} else {
217*0Sstevel@tonic-gate 		/*
218*0Sstevel@tonic-gate 		 * We need to hold P_PR_LOCK until the address space has
219*0Sstevel@tonic-gate 		 * been duplicated and we've had a chance to remove from the
220*0Sstevel@tonic-gate 		 * child any DTrace probes that were in the parent. Holding
221*0Sstevel@tonic-gate 		 * P_PR_LOCK prevents any new probes from being added and any
222*0Sstevel@tonic-gate 		 * extant probes from being removed.
223*0Sstevel@tonic-gate 		 */
224*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
225*0Sstevel@tonic-gate 		sprlock_proc(p);
226*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		error = as_dup(p->p_as, &cp->p_as);
229*0Sstevel@tonic-gate 		if (error != 0) {
230*0Sstevel@tonic-gate 			fork_fail(cp);
231*0Sstevel@tonic-gate 			mutex_enter(&pidlock);
232*0Sstevel@tonic-gate 			orphpp = &p->p_orphan;
233*0Sstevel@tonic-gate 			while (*orphpp != cp)
234*0Sstevel@tonic-gate 				orphpp = &(*orphpp)->p_nextorph;
235*0Sstevel@tonic-gate 			*orphpp = cp->p_nextorph;
236*0Sstevel@tonic-gate 			ASSERT(p->p_child == cp);
237*0Sstevel@tonic-gate 			p->p_child = cp->p_sibling;
238*0Sstevel@tonic-gate 			if (p->p_child) {
239*0Sstevel@tonic-gate 				p->p_child->p_psibling = NULL;
240*0Sstevel@tonic-gate 			}
241*0Sstevel@tonic-gate 			mutex_enter(&cp->p_lock);
242*0Sstevel@tonic-gate 			tk = cp->p_task;
243*0Sstevel@tonic-gate 			task_detach(cp);
244*0Sstevel@tonic-gate 			ASSERT(cp->p_pool->pool_ref > 0);
245*0Sstevel@tonic-gate 			atomic_add_32(&cp->p_pool->pool_ref, -1);
246*0Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
247*0Sstevel@tonic-gate 			pid_exit(cp);
248*0Sstevel@tonic-gate 			mutex_exit(&pidlock);
249*0Sstevel@tonic-gate 			task_rele(tk);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
252*0Sstevel@tonic-gate 			pool_barrier_exit();
253*0Sstevel@tonic-gate 			continuelwps(p);
254*0Sstevel@tonic-gate 			sprunlock(p);
255*0Sstevel@tonic-gate 			/*
256*0Sstevel@tonic-gate 			 * Preserve ENOMEM error condition but
257*0Sstevel@tonic-gate 			 * map all others to EAGAIN.
258*0Sstevel@tonic-gate 			 */
259*0Sstevel@tonic-gate 			error = (error == ENOMEM) ? ENOMEM : EAGAIN;
260*0Sstevel@tonic-gate 			goto forkerr;
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 		/* Duplicate parent's shared memory */
263*0Sstevel@tonic-gate 		if (p->p_segacct)
264*0Sstevel@tonic-gate 			shmfork(p, cp);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 		if (p->p_dtrace_helpers != NULL) {
267*0Sstevel@tonic-gate 			ASSERT(dtrace_helpers_fork != NULL);
268*0Sstevel@tonic-gate 			(*dtrace_helpers_fork)(p, cp);
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 		/*
272*0Sstevel@tonic-gate 		 * Remove all DTrace tracepoints from the child process.
273*0Sstevel@tonic-gate 		 */
274*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
275*0Sstevel@tonic-gate 		if (p->p_dtrace_count > 0)
276*0Sstevel@tonic-gate 			dtrace_fasttrap_fork(p, cp);
277*0Sstevel@tonic-gate 		sprunlock(p);
278*0Sstevel@tonic-gate 	}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	/*
281*0Sstevel@tonic-gate 	 * Duplicate parent's resource controls.
282*0Sstevel@tonic-gate 	 */
283*0Sstevel@tonic-gate 	dup_set = rctl_set_create();
284*0Sstevel@tonic-gate 	for (;;) {
285*0Sstevel@tonic-gate 		dup_gp = rctl_set_dup_prealloc(p->p_rctls);
286*0Sstevel@tonic-gate 		mutex_enter(&p->p_rctls->rcs_lock);
287*0Sstevel@tonic-gate 		if (rctl_set_dup_ready(p->p_rctls, dup_gp))
288*0Sstevel@tonic-gate 			break;
289*0Sstevel@tonic-gate 		mutex_exit(&p->p_rctls->rcs_lock);
290*0Sstevel@tonic-gate 		rctl_prealloc_destroy(dup_gp);
291*0Sstevel@tonic-gate 	}
292*0Sstevel@tonic-gate 	e.rcep_p.proc = cp;
293*0Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
294*0Sstevel@tonic-gate 	cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp,
295*0Sstevel@tonic-gate 	    RCD_DUP | RCD_CALLBACK);
296*0Sstevel@tonic-gate 	mutex_exit(&p->p_rctls->rcs_lock);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	rctl_prealloc_destroy(dup_gp);
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	/*
301*0Sstevel@tonic-gate 	 * Allocate the child's lwp directory and lwpid hash table.
302*0Sstevel@tonic-gate 	 */
303*0Sstevel@tonic-gate 	if (isfork1)
304*0Sstevel@tonic-gate 		cp->p_lwpdir_sz = 2;
305*0Sstevel@tonic-gate 	else
306*0Sstevel@tonic-gate 		cp->p_lwpdir_sz = p->p_lwpdir_sz;
307*0Sstevel@tonic-gate 	cp->p_lwpdir = cp->p_lwpfree = ldp =
308*0Sstevel@tonic-gate 		kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP);
309*0Sstevel@tonic-gate 	for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++)
310*0Sstevel@tonic-gate 		ldp->ld_next = ldp + 1;
311*0Sstevel@tonic-gate 	cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2;
312*0Sstevel@tonic-gate 	cp->p_tidhash =
313*0Sstevel@tonic-gate 		kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP);
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	/*
316*0Sstevel@tonic-gate 	 * Duplicate parent's lwps.
317*0Sstevel@tonic-gate 	 * Mutual exclusion is not needed because the process is
318*0Sstevel@tonic-gate 	 * in the hold state and only the current lwp is running.
319*0Sstevel@tonic-gate 	 */
320*0Sstevel@tonic-gate 	klgrpset_clear(cp->p_lgrpset);
321*0Sstevel@tonic-gate 	if (isfork1) {
322*0Sstevel@tonic-gate 		clone = forklwp(ttolwp(curthread), cp, curthread->t_tid);
323*0Sstevel@tonic-gate 		if (clone == NULL)
324*0Sstevel@tonic-gate 			goto forklwperr;
325*0Sstevel@tonic-gate 		/*
326*0Sstevel@tonic-gate 		 * Inherit only the lwp_wait()able flag,
327*0Sstevel@tonic-gate 		 * Daemon threads should not call fork1(), but oh well...
328*0Sstevel@tonic-gate 		 */
329*0Sstevel@tonic-gate 		lwptot(clone)->t_proc_flag |=
330*0Sstevel@tonic-gate 			(curthread->t_proc_flag & TP_TWAIT);
331*0Sstevel@tonic-gate 	} else {
332*0Sstevel@tonic-gate 		/* this is forkall(), no one can be in lwp_wait() */
333*0Sstevel@tonic-gate 		ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0);
334*0Sstevel@tonic-gate 		/* for each entry in the parent's lwp directory... */
335*0Sstevel@tonic-gate 		for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) {
336*0Sstevel@tonic-gate 			klwp_t *clwp;
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) == NULL)
339*0Sstevel@tonic-gate 				continue;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 			if ((t = lep->le_thread) != NULL) {
342*0Sstevel@tonic-gate 				clwp = forklwp(ttolwp(t), cp, t->t_tid);
343*0Sstevel@tonic-gate 				if (clwp == NULL)
344*0Sstevel@tonic-gate 					goto forklwperr;
345*0Sstevel@tonic-gate 				/*
346*0Sstevel@tonic-gate 				 * Inherit lwp_wait()able and daemon flags.
347*0Sstevel@tonic-gate 				 */
348*0Sstevel@tonic-gate 				lwptot(clwp)->t_proc_flag |=
349*0Sstevel@tonic-gate 				    (t->t_proc_flag & (TP_TWAIT|TP_DAEMON));
350*0Sstevel@tonic-gate 				/*
351*0Sstevel@tonic-gate 				 * Keep track of the clone of curthread to
352*0Sstevel@tonic-gate 				 * post return values through lwp_setrval().
353*0Sstevel@tonic-gate 				 */
354*0Sstevel@tonic-gate 				if (t == curthread)
355*0Sstevel@tonic-gate 					clone = clwp;
356*0Sstevel@tonic-gate 			} else {
357*0Sstevel@tonic-gate 				/*
358*0Sstevel@tonic-gate 				 * Replicate zombie lwps in the child.
359*0Sstevel@tonic-gate 				 */
360*0Sstevel@tonic-gate 				clep = kmem_zalloc(sizeof (*clep), KM_SLEEP);
361*0Sstevel@tonic-gate 				clep->le_lwpid = lep->le_lwpid;
362*0Sstevel@tonic-gate 				clep->le_start = lep->le_start;
363*0Sstevel@tonic-gate 				lwp_hash_in(cp, clep);
364*0Sstevel@tonic-gate 			}
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 	}
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/*
369*0Sstevel@tonic-gate 	 * Put new process in the parent's process contract, or put it
370*0Sstevel@tonic-gate 	 * in a new one if there is an active process template.  Send a
371*0Sstevel@tonic-gate 	 * fork event (if requested) to whatever contract the child is
372*0Sstevel@tonic-gate 	 * a member of.  Fails if the parent has been SIGKILLed.
373*0Sstevel@tonic-gate 	 */
374*0Sstevel@tonic-gate 	if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL)
375*0Sstevel@tonic-gate 		goto forklwperr;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	/*
378*0Sstevel@tonic-gate 	 * No fork failures occur beyond this point.
379*0Sstevel@tonic-gate 	 */
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	cp->p_lwpid = p->p_lwpid;
382*0Sstevel@tonic-gate 	if (!isfork1) {
383*0Sstevel@tonic-gate 		cp->p_lwpdaemon = p->p_lwpdaemon;
384*0Sstevel@tonic-gate 		cp->p_zombcnt = p->p_zombcnt;
385*0Sstevel@tonic-gate 		/*
386*0Sstevel@tonic-gate 		 * If the parent's lwp ids have wrapped around, so have the
387*0Sstevel@tonic-gate 		 * child's.
388*0Sstevel@tonic-gate 		 */
389*0Sstevel@tonic-gate 		cp->p_flag |= p->p_flag & SLWPWRAP;
390*0Sstevel@tonic-gate 	}
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	corectl_path_hold(cp->p_corefile = p->p_corefile);
393*0Sstevel@tonic-gate 	corectl_content_hold(cp->p_content = p->p_content);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate #if defined(__x86)
396*0Sstevel@tonic-gate 	/*
397*0Sstevel@tonic-gate 	 * Get the right ldt descr for the child.
398*0Sstevel@tonic-gate 	 */
399*0Sstevel@tonic-gate 	(void) ldt_dup(p, cp);
400*0Sstevel@tonic-gate #endif
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate #ifdef __sparc
403*0Sstevel@tonic-gate 	utrap_dup(p, cp);
404*0Sstevel@tonic-gate #endif
405*0Sstevel@tonic-gate 	/*
406*0Sstevel@tonic-gate 	 * If the child process has been marked to stop on exit
407*0Sstevel@tonic-gate 	 * from this fork, arrange for all other lwps to stop in
408*0Sstevel@tonic-gate 	 * sympathy with the active lwp.
409*0Sstevel@tonic-gate 	 */
410*0Sstevel@tonic-gate 	if (PTOU(cp)->u_systrap &&
411*0Sstevel@tonic-gate 	    prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) {
412*0Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
413*0Sstevel@tonic-gate 		t = cp->p_tlist;
414*0Sstevel@tonic-gate 		do {
415*0Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
416*0Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
417*0Sstevel@tonic-gate 		} while ((t = t->t_forw) != cp->p_tlist);
418*0Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 	/*
421*0Sstevel@tonic-gate 	 * If the parent process has been marked to stop on exit
422*0Sstevel@tonic-gate 	 * from this fork, and its asynchronous-stop flag has not
423*0Sstevel@tonic-gate 	 * been set, arrange for all other lwps to stop before
424*0Sstevel@tonic-gate 	 * they return back to user level.
425*0Sstevel@tonic-gate 	 */
426*0Sstevel@tonic-gate 	if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap &&
427*0Sstevel@tonic-gate 	    prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) {
428*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
429*0Sstevel@tonic-gate 		t = p->p_tlist;
430*0Sstevel@tonic-gate 		do {
431*0Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
432*0Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
433*0Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
434*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
435*0Sstevel@tonic-gate 	}
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	/* set return values for child */
438*0Sstevel@tonic-gate 	lwp_setrval(clone, p->p_pid, 1);
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	/* set return values for parent */
441*0Sstevel@tonic-gate 	r.r_val1 = (int)cp->p_pid;
442*0Sstevel@tonic-gate 	r.r_val2 = 0;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	/*
445*0Sstevel@tonic-gate 	 * pool_barrier_exit() can now be called because the child process has:
446*0Sstevel@tonic-gate 	 * - all identifying features cloned or set (p_pid, p_task, p_pool)
447*0Sstevel@tonic-gate 	 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset)
448*0Sstevel@tonic-gate 	 * - any other fields set which are used in resource set binding.
449*0Sstevel@tonic-gate 	 */
450*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
451*0Sstevel@tonic-gate 	pool_barrier_exit();
452*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
453*0Sstevel@tonic-gate 
454*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
455*0Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 	/*
458*0Sstevel@tonic-gate 	 * Now that there are lwps and threads attached, add the new
459*0Sstevel@tonic-gate 	 * process to the process group.
460*0Sstevel@tonic-gate 	 */
461*0Sstevel@tonic-gate 	pgjoin(cp, p->p_pgidp);
462*0Sstevel@tonic-gate 	cp->p_stat = SRUN;
463*0Sstevel@tonic-gate 	/*
464*0Sstevel@tonic-gate 	 * We are now done with all the lwps in the child process.
465*0Sstevel@tonic-gate 	 */
466*0Sstevel@tonic-gate 	t = cp->p_tlist;
467*0Sstevel@tonic-gate 	do {
468*0Sstevel@tonic-gate 		/*
469*0Sstevel@tonic-gate 		 * Set the lwp_suspend()ed lwps running.
470*0Sstevel@tonic-gate 		 * They will suspend properly at syscall exit.
471*0Sstevel@tonic-gate 		 */
472*0Sstevel@tonic-gate 		if (t->t_proc_flag & TP_HOLDLWP)
473*0Sstevel@tonic-gate 			lwp_create_done(t);
474*0Sstevel@tonic-gate 		else {
475*0Sstevel@tonic-gate 			/* set TS_CREATE to allow continuelwps() to work */
476*0Sstevel@tonic-gate 			thread_lock(t);
477*0Sstevel@tonic-gate 			ASSERT(t->t_state == TS_STOPPED &&
478*0Sstevel@tonic-gate 			    !(t->t_schedflag & (TS_CREATE|TS_CSTART)));
479*0Sstevel@tonic-gate 			t->t_schedflag |= TS_CREATE;
480*0Sstevel@tonic-gate 			thread_unlock(t);
481*0Sstevel@tonic-gate 		}
482*0Sstevel@tonic-gate 	} while ((t = t->t_forw) != cp->p_tlist);
483*0Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	if (isvfork) {
486*0Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysvfork, 1);
487*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
488*0Sstevel@tonic-gate 		p->p_flag |= SVFWAIT;
489*0Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
490*0Sstevel@tonic-gate 		cv_broadcast(&pr_pid_cv[p->p_slot]);	/* inform /proc */
491*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
492*0Sstevel@tonic-gate 		/*
493*0Sstevel@tonic-gate 		 * Grab child's p_lock before dropping pidlock to ensure
494*0Sstevel@tonic-gate 		 * the process will not disappear before we set it running.
495*0Sstevel@tonic-gate 		 */
496*0Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
497*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
498*0Sstevel@tonic-gate 		sigdefault(cp);
499*0Sstevel@tonic-gate 		continuelwps(cp);
500*0Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
501*0Sstevel@tonic-gate 	} else {
502*0Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysfork, 1);
503*0Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
504*0Sstevel@tonic-gate 		/*
505*0Sstevel@tonic-gate 		 * It is CL_FORKRET's job to drop pidlock.
506*0Sstevel@tonic-gate 		 * If we do it here, the process could be set running
507*0Sstevel@tonic-gate 		 * and disappear before CL_FORKRET() is called.
508*0Sstevel@tonic-gate 		 */
509*0Sstevel@tonic-gate 		CL_FORKRET(curthread, cp->p_tlist);
510*0Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&pidlock));
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	return (r.r_vals);
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate forklwperr:
516*0Sstevel@tonic-gate 	if (isvfork) {
517*0Sstevel@tonic-gate 		if (avl_numnodes(&p->p_wpage) != 0) {
518*0Sstevel@tonic-gate 			/* restore watchpoints to parent */
519*0Sstevel@tonic-gate 			as = p->p_as;
520*0Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock,
521*0Sstevel@tonic-gate 				RW_WRITER);
522*0Sstevel@tonic-gate 			as->a_wpage = p->p_wpage;
523*0Sstevel@tonic-gate 			avl_create(&p->p_wpage, wp_compare,
524*0Sstevel@tonic-gate 			    sizeof (struct watched_page),
525*0Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
526*0Sstevel@tonic-gate 			as_setwatch(as);
527*0Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
528*0Sstevel@tonic-gate 		}
529*0Sstevel@tonic-gate 	} else {
530*0Sstevel@tonic-gate 		if (cp->p_segacct)
531*0Sstevel@tonic-gate 			shmexit(cp);
532*0Sstevel@tonic-gate 		as = cp->p_as;
533*0Sstevel@tonic-gate 		cp->p_as = &kas;
534*0Sstevel@tonic-gate 		as_free(as);
535*0Sstevel@tonic-gate 	}
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	if (cp->p_lwpdir) {
538*0Sstevel@tonic-gate 		for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++)
539*0Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) != NULL)
540*0Sstevel@tonic-gate 				kmem_free(lep, sizeof (*lep));
541*0Sstevel@tonic-gate 		kmem_free(cp->p_lwpdir,
542*0Sstevel@tonic-gate 		    cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir));
543*0Sstevel@tonic-gate 	}
544*0Sstevel@tonic-gate 	cp->p_lwpdir = NULL;
545*0Sstevel@tonic-gate 	cp->p_lwpfree = NULL;
546*0Sstevel@tonic-gate 	cp->p_lwpdir_sz = 0;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	if (cp->p_tidhash)
549*0Sstevel@tonic-gate 		kmem_free(cp->p_tidhash,
550*0Sstevel@tonic-gate 		    cp->p_tidhash_sz * sizeof (*cp->p_tidhash));
551*0Sstevel@tonic-gate 	cp->p_tidhash = NULL;
552*0Sstevel@tonic-gate 	cp->p_tidhash_sz = 0;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	forklwp_fail(cp);
555*0Sstevel@tonic-gate 	fork_fail(cp);
556*0Sstevel@tonic-gate 	rctl_set_free(cp->p_rctls);
557*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate 	/*
560*0Sstevel@tonic-gate 	 * Detach failed child from task.
561*0Sstevel@tonic-gate 	 */
562*0Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
563*0Sstevel@tonic-gate 	tk = cp->p_task;
564*0Sstevel@tonic-gate 	task_detach(cp);
565*0Sstevel@tonic-gate 	ASSERT(cp->p_pool->pool_ref > 0);
566*0Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, -1);
567*0Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	orphpp = &p->p_orphan;
570*0Sstevel@tonic-gate 	while (*orphpp != cp)
571*0Sstevel@tonic-gate 		orphpp = &(*orphpp)->p_nextorph;
572*0Sstevel@tonic-gate 	*orphpp = cp->p_nextorph;
573*0Sstevel@tonic-gate 	ASSERT(p->p_child == cp);
574*0Sstevel@tonic-gate 	p->p_child = cp->p_sibling;
575*0Sstevel@tonic-gate 	if (p->p_child) {
576*0Sstevel@tonic-gate 		p->p_child->p_psibling = NULL;
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	pid_exit(cp);
579*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	task_rele(tk);
582*0Sstevel@tonic-gate 
583*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
584*0Sstevel@tonic-gate 	pool_barrier_exit();
585*0Sstevel@tonic-gate 	continuelwps(p);
586*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
587*0Sstevel@tonic-gate 	error = EAGAIN;
588*0Sstevel@tonic-gate forkerr:
589*0Sstevel@tonic-gate 	return ((int64_t)set_errno(error));
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /*
593*0Sstevel@tonic-gate  * Free allocated resources from getproc() if a fork failed.
594*0Sstevel@tonic-gate  */
595*0Sstevel@tonic-gate static void
596*0Sstevel@tonic-gate fork_fail(proc_t *cp)
597*0Sstevel@tonic-gate {
598*0Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(cp);
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate 	fcnt_add(fip, -1);
601*0Sstevel@tonic-gate 	sigdelq(cp, NULL, 0);
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
604*0Sstevel@tonic-gate 	upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred));
605*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	/*
608*0Sstevel@tonic-gate 	 * single threaded, so no locking needed here
609*0Sstevel@tonic-gate 	 */
610*0Sstevel@tonic-gate 	crfree(cp->p_cred);
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	VN_RELE(u.u_cdir);
615*0Sstevel@tonic-gate 	if (u.u_rdir)
616*0Sstevel@tonic-gate 		VN_RELE(u.u_rdir);
617*0Sstevel@tonic-gate 	if (cp->p_exec)
618*0Sstevel@tonic-gate 		VN_RELE(cp->p_exec);
619*0Sstevel@tonic-gate 	if (cp->p_execdir)
620*0Sstevel@tonic-gate 		VN_RELE(cp->p_execdir);
621*0Sstevel@tonic-gate 	if (u.u_cwd)
622*0Sstevel@tonic-gate 		refstr_rele(u.u_cwd);
623*0Sstevel@tonic-gate }
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate /*
626*0Sstevel@tonic-gate  * Clean up the lwps already created for this child process.
627*0Sstevel@tonic-gate  * The fork failed while duplicating all the lwps of the parent
628*0Sstevel@tonic-gate  * and those lwps already created must be freed.
629*0Sstevel@tonic-gate  * This process is invisible to the rest of the system,
630*0Sstevel@tonic-gate  * so we don't need to hold p->p_lock to protect the list.
631*0Sstevel@tonic-gate  */
632*0Sstevel@tonic-gate static void
633*0Sstevel@tonic-gate forklwp_fail(proc_t *p)
634*0Sstevel@tonic-gate {
635*0Sstevel@tonic-gate 	kthread_t *t;
636*0Sstevel@tonic-gate 	task_t *tk;
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	while ((t = p->p_tlist) != NULL) {
639*0Sstevel@tonic-gate 		/*
640*0Sstevel@tonic-gate 		 * First remove the lwp from the process's p_tlist.
641*0Sstevel@tonic-gate 		 */
642*0Sstevel@tonic-gate 		if (t != t->t_forw)
643*0Sstevel@tonic-gate 			p->p_tlist = t->t_forw;
644*0Sstevel@tonic-gate 		else
645*0Sstevel@tonic-gate 			p->p_tlist = NULL;
646*0Sstevel@tonic-gate 		p->p_lwpcnt--;
647*0Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
648*0Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 		tk = p->p_task;
651*0Sstevel@tonic-gate 		mutex_enter(&p->p_zone->zone_nlwps_lock);
652*0Sstevel@tonic-gate 		tk->tk_nlwps--;
653*0Sstevel@tonic-gate 		tk->tk_proj->kpj_nlwps--;
654*0Sstevel@tonic-gate 		p->p_zone->zone_nlwps--;
655*0Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 		ASSERT(t->t_schedctl == NULL);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 		if (t->t_door != NULL) {
660*0Sstevel@tonic-gate 			kmem_free(t->t_door, sizeof (door_data_t));
661*0Sstevel@tonic-gate 			t->t_door = NULL;
662*0Sstevel@tonic-gate 		}
663*0Sstevel@tonic-gate 		lwp_ctmpl_clear(ttolwp(t));
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 		/*
666*0Sstevel@tonic-gate 		 * Remove the thread from the all threads list.
667*0Sstevel@tonic-gate 		 * We need to hold pidlock for this.
668*0Sstevel@tonic-gate 		 */
669*0Sstevel@tonic-gate 		mutex_enter(&pidlock);
670*0Sstevel@tonic-gate 		t->t_next->t_prev = t->t_prev;
671*0Sstevel@tonic-gate 		t->t_prev->t_next = t->t_next;
672*0Sstevel@tonic-gate 		CL_EXIT(t);	/* tell the scheduler that we're exiting */
673*0Sstevel@tonic-gate 		cv_broadcast(&t->t_joincv);	/* tell anyone in thread_join */
674*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate 		/*
677*0Sstevel@tonic-gate 		 * Let the lgroup load averages know that this thread isn't
678*0Sstevel@tonic-gate 		 * going to show up (i.e. un-do what was done on behalf of
679*0Sstevel@tonic-gate 		 * this thread by the earlier lgrp_move_thread()).
680*0Sstevel@tonic-gate 		 */
681*0Sstevel@tonic-gate 		kpreempt_disable();
682*0Sstevel@tonic-gate 		lgrp_move_thread(t, NULL, 1);
683*0Sstevel@tonic-gate 		kpreempt_enable();
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 		/*
686*0Sstevel@tonic-gate 		 * The thread was created TS_STOPPED.
687*0Sstevel@tonic-gate 		 * We change it to TS_FREE to avoid an
688*0Sstevel@tonic-gate 		 * ASSERT() panic in thread_free().
689*0Sstevel@tonic-gate 		 */
690*0Sstevel@tonic-gate 		t->t_state = TS_FREE;
691*0Sstevel@tonic-gate 		thread_rele(t);
692*0Sstevel@tonic-gate 		thread_free(t);
693*0Sstevel@tonic-gate 	}
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate extern struct as kas;
697*0Sstevel@tonic-gate 
698*0Sstevel@tonic-gate /*
699*0Sstevel@tonic-gate  * fork a kernel process.
700*0Sstevel@tonic-gate  */
701*0Sstevel@tonic-gate int
702*0Sstevel@tonic-gate newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct)
703*0Sstevel@tonic-gate {
704*0Sstevel@tonic-gate 	proc_t *p;
705*0Sstevel@tonic-gate 	struct user *up;
706*0Sstevel@tonic-gate 	klwp_t *lwp;
707*0Sstevel@tonic-gate 	cont_process_t *ctp = NULL;
708*0Sstevel@tonic-gate 	rctl_entity_p_t e;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate 	ASSERT(!(cid == syscid && ct != NULL));
711*0Sstevel@tonic-gate 	if (cid == syscid) {
712*0Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp;
713*0Sstevel@tonic-gate 		rctl_set_t *init_set;
714*0Sstevel@tonic-gate 
715*0Sstevel@tonic-gate 		if (getproc(&p, 1) < 0)
716*0Sstevel@tonic-gate 			return (EAGAIN);
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 		p->p_flag |= SNOWAIT;
719*0Sstevel@tonic-gate 		p->p_exec = NULL;
720*0Sstevel@tonic-gate 		p->p_execdir = NULL;
721*0Sstevel@tonic-gate 
722*0Sstevel@tonic-gate 		init_set = rctl_set_create();
723*0Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 		/*
726*0Sstevel@tonic-gate 		 * kernel processes do not inherit /proc tracing flags.
727*0Sstevel@tonic-gate 		 */
728*0Sstevel@tonic-gate 		sigemptyset(&p->p_sigmask);
729*0Sstevel@tonic-gate 		premptyset(&p->p_fltmask);
730*0Sstevel@tonic-gate 		up = PTOU(p);
731*0Sstevel@tonic-gate 		up->u_systrap = 0;
732*0Sstevel@tonic-gate 		premptyset(&(up->u_entrymask));
733*0Sstevel@tonic-gate 		premptyset(&(up->u_exitmask));
734*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
735*0Sstevel@tonic-gate 		e.rcep_p.proc = p;
736*0Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
737*0Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
738*0Sstevel@tonic-gate 		    init_gp);
739*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
742*0Sstevel@tonic-gate 	} else  {
743*0Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp, *default_gp;
744*0Sstevel@tonic-gate 		rctl_set_t *init_set;
745*0Sstevel@tonic-gate 		task_t *tk, *tk_old;
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 		if (getproc(&p, 0) < 0)
748*0Sstevel@tonic-gate 			return (EAGAIN);
749*0Sstevel@tonic-gate 		/*
750*0Sstevel@tonic-gate 		 * init creates a new task, distinct from the task
751*0Sstevel@tonic-gate 		 * containing kernel "processes".
752*0Sstevel@tonic-gate 		 */
753*0Sstevel@tonic-gate 		tk = task_create(0, p->p_zone);
754*0Sstevel@tonic-gate 		mutex_enter(&tk->tk_zone->zone_nlwps_lock);
755*0Sstevel@tonic-gate 		tk->tk_proj->kpj_ntasks++;
756*0Sstevel@tonic-gate 		mutex_exit(&tk->tk_zone->zone_nlwps_lock);
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 		default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS);
759*0Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
760*0Sstevel@tonic-gate 		init_set = rctl_set_create();
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 		mutex_enter(&pidlock);
763*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
764*0Sstevel@tonic-gate 		tk_old = p->p_task;	/* switch to new task */
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 		task_detach(p);
767*0Sstevel@tonic-gate 		task_begin(tk, p);
768*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 		e.rcep_p.proc = p;
771*0Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
772*0Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
773*0Sstevel@tonic-gate 		    init_gp);
774*0Sstevel@tonic-gate 		rctlproc_default_init(p, default_gp);
775*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 		task_rele(tk_old);
778*0Sstevel@tonic-gate 		rctl_prealloc_destroy(default_gp);
779*0Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	p->p_as = &kas;
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate #if defined(__x86)
785*0Sstevel@tonic-gate 	(void) ldt_dup(&p0, p);		/* Get the default ldt descr */
786*0Sstevel@tonic-gate #endif
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri,
789*0Sstevel@tonic-gate 	    &curthread->t_hold, cid, 1)) == NULL) {
790*0Sstevel@tonic-gate 		task_t *tk;
791*0Sstevel@tonic-gate 		fork_fail(p);
792*0Sstevel@tonic-gate 		mutex_enter(&pidlock);
793*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
794*0Sstevel@tonic-gate 		tk = p->p_task;
795*0Sstevel@tonic-gate 		task_detach(p);
796*0Sstevel@tonic-gate 		ASSERT(p->p_pool->pool_ref > 0);
797*0Sstevel@tonic-gate 		atomic_add_32(&p->p_pool->pool_ref, -1);
798*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
799*0Sstevel@tonic-gate 		pid_exit(p);
800*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
801*0Sstevel@tonic-gate 		task_rele(tk);
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 		return (EAGAIN);
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	if (cid != syscid) {
807*0Sstevel@tonic-gate 		ctp = contract_process_fork(sys_process_tmpl, p, curproc,
808*0Sstevel@tonic-gate 		    B_FALSE);
809*0Sstevel@tonic-gate 		ASSERT(ctp != NULL);
810*0Sstevel@tonic-gate 		if (ct != NULL)
811*0Sstevel@tonic-gate 			*ct = &ctp->conp_contract;
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	p->p_lwpid = 1;
815*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
816*0Sstevel@tonic-gate 	pgjoin(p, curproc->p_pgidp);
817*0Sstevel@tonic-gate 	p->p_stat = SRUN;
818*0Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
819*0Sstevel@tonic-gate 	lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP;
820*0Sstevel@tonic-gate 	lwp_create_done(lwptot(lwp));
821*0Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
822*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
823*0Sstevel@tonic-gate 	return (0);
824*0Sstevel@tonic-gate }
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate /*
827*0Sstevel@tonic-gate  * create a child proc struct.
828*0Sstevel@tonic-gate  */
829*0Sstevel@tonic-gate static int
830*0Sstevel@tonic-gate getproc(proc_t **cpp, int kernel)
831*0Sstevel@tonic-gate {
832*0Sstevel@tonic-gate 	proc_t		*pp, *cp;
833*0Sstevel@tonic-gate 	pid_t		newpid;
834*0Sstevel@tonic-gate 	struct user	*uarea;
835*0Sstevel@tonic-gate 	extern uint_t	nproc;
836*0Sstevel@tonic-gate 	struct cred	*cr;
837*0Sstevel@tonic-gate 	uid_t		ruid;
838*0Sstevel@tonic-gate 	zoneid_t	zoneid;
839*0Sstevel@tonic-gate 
840*0Sstevel@tonic-gate 	if (!page_mem_avail(tune.t_minarmem))
841*0Sstevel@tonic-gate 		return (-1);
842*0Sstevel@tonic-gate 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
843*0Sstevel@tonic-gate 		return (-1);	/* no point in starting new processes */
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	pp = curproc;
846*0Sstevel@tonic-gate 	cp = kmem_cache_alloc(process_cache, KM_SLEEP);
847*0Sstevel@tonic-gate 	bzero(cp, sizeof (proc_t));
848*0Sstevel@tonic-gate 
849*0Sstevel@tonic-gate 	/*
850*0Sstevel@tonic-gate 	 * Make proc entry for child process
851*0Sstevel@tonic-gate 	 */
852*0Sstevel@tonic-gate 	mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL);
853*0Sstevel@tonic-gate 	mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL);
854*0Sstevel@tonic-gate #if defined(__x86)
855*0Sstevel@tonic-gate 	mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL);
856*0Sstevel@tonic-gate #endif
857*0Sstevel@tonic-gate 	mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL);
858*0Sstevel@tonic-gate 	cp->p_stat = SIDL;
859*0Sstevel@tonic-gate 	cp->p_mstart = gethrtime();
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	if ((newpid = pid_assign(cp)) == -1) {
862*0Sstevel@tonic-gate 		if (nproc == v.v_proc) {
863*0Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, procovf, 1);
864*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "out of processes");
865*0Sstevel@tonic-gate 		}
866*0Sstevel@tonic-gate 		goto bad;
867*0Sstevel@tonic-gate 	}
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 	/*
870*0Sstevel@tonic-gate 	 * If not privileged make sure that this user hasn't exceeded
871*0Sstevel@tonic-gate 	 * v.v_maxup processes, and that users collectively haven't
872*0Sstevel@tonic-gate 	 * exceeded v.v_maxupttl processes.
873*0Sstevel@tonic-gate 	 */
874*0Sstevel@tonic-gate 	mutex_enter(&pidlock);
875*0Sstevel@tonic-gate 	ASSERT(nproc < v.v_proc);	/* otherwise how'd we get our pid? */
876*0Sstevel@tonic-gate 	cr = CRED();
877*0Sstevel@tonic-gate 	ruid = crgetruid(cr);
878*0Sstevel@tonic-gate 	zoneid = crgetzoneid(cr);
879*0Sstevel@tonic-gate 	if (nproc >= v.v_maxup && 	/* short-circuit; usually false */
880*0Sstevel@tonic-gate 	    (nproc >= v.v_maxupttl ||
881*0Sstevel@tonic-gate 	    upcount_get(ruid, zoneid) >= v.v_maxup) &&
882*0Sstevel@tonic-gate 	    secpolicy_newproc(cr) != 0) {
883*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
884*0Sstevel@tonic-gate 		zcmn_err(zoneid, CE_NOTE,
885*0Sstevel@tonic-gate 		    "out of per-user processes for uid %d", ruid);
886*0Sstevel@tonic-gate 		goto bad;
887*0Sstevel@tonic-gate 	}
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate 	/*
890*0Sstevel@tonic-gate 	 * Everything is cool, put the new proc on the active process list.
891*0Sstevel@tonic-gate 	 * It is already on the pid list and in /proc.
892*0Sstevel@tonic-gate 	 * Increment the per uid process count (upcount).
893*0Sstevel@tonic-gate 	 */
894*0Sstevel@tonic-gate 	nproc++;
895*0Sstevel@tonic-gate 	upcount_inc(ruid, zoneid);
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	cp->p_next = practive;
898*0Sstevel@tonic-gate 	practive->p_prev = cp;
899*0Sstevel@tonic-gate 	practive = cp;
900*0Sstevel@tonic-gate 
901*0Sstevel@tonic-gate 	cp->p_ignore = pp->p_ignore;
902*0Sstevel@tonic-gate 	cp->p_siginfo = pp->p_siginfo;
903*0Sstevel@tonic-gate 	cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD);
904*0Sstevel@tonic-gate 	cp->p_sessp = pp->p_sessp;
905*0Sstevel@tonic-gate 	SESS_HOLD(pp->p_sessp);
906*0Sstevel@tonic-gate 	cp->p_exec = pp->p_exec;
907*0Sstevel@tonic-gate 	cp->p_execdir = pp->p_execdir;
908*0Sstevel@tonic-gate 	cp->p_zone = pp->p_zone;
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 	cp->p_bssbase = pp->p_bssbase;
911*0Sstevel@tonic-gate 	cp->p_brkbase = pp->p_brkbase;
912*0Sstevel@tonic-gate 	cp->p_brksize = pp->p_brksize;
913*0Sstevel@tonic-gate 	cp->p_brkpageszc = pp->p_brkpageszc;
914*0Sstevel@tonic-gate 	cp->p_stksize = pp->p_stksize;
915*0Sstevel@tonic-gate 	cp->p_stkpageszc = pp->p_stkpageszc;
916*0Sstevel@tonic-gate 	cp->p_stkprot = pp->p_stkprot;
917*0Sstevel@tonic-gate 	cp->p_datprot = pp->p_datprot;
918*0Sstevel@tonic-gate 	cp->p_usrstack = pp->p_usrstack;
919*0Sstevel@tonic-gate 	cp->p_model = pp->p_model;
920*0Sstevel@tonic-gate 	cp->p_ppid = pp->p_pid;
921*0Sstevel@tonic-gate 	cp->p_ancpid = pp->p_pid;
922*0Sstevel@tonic-gate 	cp->p_portcnt = pp->p_portcnt;
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate 	/*
925*0Sstevel@tonic-gate 	 * Initialize watchpoint structures
926*0Sstevel@tonic-gate 	 */
927*0Sstevel@tonic-gate 	avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area),
928*0Sstevel@tonic-gate 	    offsetof(struct watched_area, wa_link));
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	/*
931*0Sstevel@tonic-gate 	 * Initialize immediate resource control values.
932*0Sstevel@tonic-gate 	 */
933*0Sstevel@tonic-gate 	cp->p_stk_ctl = pp->p_stk_ctl;
934*0Sstevel@tonic-gate 	cp->p_fsz_ctl = pp->p_fsz_ctl;
935*0Sstevel@tonic-gate 	cp->p_vmem_ctl = pp->p_vmem_ctl;
936*0Sstevel@tonic-gate 	cp->p_fno_ctl = pp->p_fno_ctl;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 	/*
939*0Sstevel@tonic-gate 	 * Link up to parent-child-sibling chain.  No need to lock
940*0Sstevel@tonic-gate 	 * in general since only a call to freeproc() (done by the
941*0Sstevel@tonic-gate 	 * same parent as newproc()) diddles with the child chain.
942*0Sstevel@tonic-gate 	 */
943*0Sstevel@tonic-gate 	cp->p_sibling = pp->p_child;
944*0Sstevel@tonic-gate 	if (pp->p_child)
945*0Sstevel@tonic-gate 		pp->p_child->p_psibling = cp;
946*0Sstevel@tonic-gate 
947*0Sstevel@tonic-gate 	cp->p_parent = pp;
948*0Sstevel@tonic-gate 	pp->p_child = cp;
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 	cp->p_child_ns = NULL;
951*0Sstevel@tonic-gate 	cp->p_sibling_ns = NULL;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 	cp->p_nextorph = pp->p_orphan;
954*0Sstevel@tonic-gate 	cp->p_nextofkin = pp;
955*0Sstevel@tonic-gate 	pp->p_orphan = cp;
956*0Sstevel@tonic-gate 
957*0Sstevel@tonic-gate 	/*
958*0Sstevel@tonic-gate 	 * Inherit profiling state; do not inherit REALPROF profiling state.
959*0Sstevel@tonic-gate 	 */
960*0Sstevel@tonic-gate 	cp->p_prof = pp->p_prof;
961*0Sstevel@tonic-gate 	cp->p_rprof_cyclic = CYCLIC_NONE;
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	/*
964*0Sstevel@tonic-gate 	 * Inherit pool pointer from the parent.  Kernel processes are
965*0Sstevel@tonic-gate 	 * always bound to the default pool.
966*0Sstevel@tonic-gate 	 */
967*0Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
968*0Sstevel@tonic-gate 	if (kernel) {
969*0Sstevel@tonic-gate 		cp->p_pool = pool_default;
970*0Sstevel@tonic-gate 		cp->p_flag |= SSYS;
971*0Sstevel@tonic-gate 	} else {
972*0Sstevel@tonic-gate 		cp->p_pool = pp->p_pool;
973*0Sstevel@tonic-gate 	}
974*0Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, 1);
975*0Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 	/*
978*0Sstevel@tonic-gate 	 * Add the child process to the current task.  Kernel processes
979*0Sstevel@tonic-gate 	 * are always attached to task0.
980*0Sstevel@tonic-gate 	 */
981*0Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
982*0Sstevel@tonic-gate 	if (kernel)
983*0Sstevel@tonic-gate 		task_attach(task0p, cp);
984*0Sstevel@tonic-gate 	else
985*0Sstevel@tonic-gate 		task_attach(pp->p_task, cp);
986*0Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
987*0Sstevel@tonic-gate 	mutex_exit(&pidlock);
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 	avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t),
990*0Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 	/*
993*0Sstevel@tonic-gate 	 * Duplicate any audit information kept in the process table
994*0Sstevel@tonic-gate 	 */
995*0Sstevel@tonic-gate #ifdef C2_AUDIT
996*0Sstevel@tonic-gate 	if (audit_active)	/* copy audit data to cp */
997*0Sstevel@tonic-gate 		audit_newproc(cp);
998*0Sstevel@tonic-gate #endif
999*0Sstevel@tonic-gate 
1000*0Sstevel@tonic-gate 	crhold(cp->p_cred = cr);
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 	/*
1003*0Sstevel@tonic-gate 	 * Bump up the counts on the file structures pointed at by the
1004*0Sstevel@tonic-gate 	 * parent's file table since the child will point at them too.
1005*0Sstevel@tonic-gate 	 */
1006*0Sstevel@tonic-gate 	fcnt_add(P_FINFO(pp), 1);
1007*0Sstevel@tonic-gate 
1008*0Sstevel@tonic-gate 	VN_HOLD(u.u_cdir);
1009*0Sstevel@tonic-gate 	if (u.u_rdir)
1010*0Sstevel@tonic-gate 		VN_HOLD(u.u_rdir);
1011*0Sstevel@tonic-gate 	if (u.u_cwd)
1012*0Sstevel@tonic-gate 		refstr_hold(u.u_cwd);
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	/*
1015*0Sstevel@tonic-gate 	 * copy the parent's uarea.
1016*0Sstevel@tonic-gate 	 */
1017*0Sstevel@tonic-gate 	uarea = PTOU(cp);
1018*0Sstevel@tonic-gate 	bcopy(PTOU(pp), uarea, sizeof (user_t));
1019*0Sstevel@tonic-gate 	flist_fork(P_FINFO(pp), P_FINFO(cp));
1020*0Sstevel@tonic-gate 
1021*0Sstevel@tonic-gate 	gethrestime(&uarea->u_start);
1022*0Sstevel@tonic-gate 	uarea->u_ticks = lbolt;
1023*0Sstevel@tonic-gate 	uarea->u_mem = rm_asrss(pp->p_as);
1024*0Sstevel@tonic-gate 	uarea->u_acflag = AFORK;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	/*
1027*0Sstevel@tonic-gate 	 * If inherit-on-fork, copy /proc tracing flags to child.
1028*0Sstevel@tonic-gate 	 */
1029*0Sstevel@tonic-gate 	if ((pp->p_proc_flag & P_PR_FORK) != 0) {
1030*0Sstevel@tonic-gate 		cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK);
1031*0Sstevel@tonic-gate 		cp->p_sigmask = pp->p_sigmask;
1032*0Sstevel@tonic-gate 		cp->p_fltmask = pp->p_fltmask;
1033*0Sstevel@tonic-gate 	} else {
1034*0Sstevel@tonic-gate 		sigemptyset(&cp->p_sigmask);
1035*0Sstevel@tonic-gate 		premptyset(&cp->p_fltmask);
1036*0Sstevel@tonic-gate 		uarea->u_systrap = 0;
1037*0Sstevel@tonic-gate 		premptyset(&uarea->u_entrymask);
1038*0Sstevel@tonic-gate 		premptyset(&uarea->u_exitmask);
1039*0Sstevel@tonic-gate 	}
1040*0Sstevel@tonic-gate 	/*
1041*0Sstevel@tonic-gate 	 * If microstate accounting is being inherited, mark child
1042*0Sstevel@tonic-gate 	 */
1043*0Sstevel@tonic-gate 	if ((pp->p_flag & SMSFORK) != 0)
1044*0Sstevel@tonic-gate 		cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT);
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	/*
1047*0Sstevel@tonic-gate 	 * Inherit fixalignment flag from the parent
1048*0Sstevel@tonic-gate 	 */
1049*0Sstevel@tonic-gate 	cp->p_fixalignment = pp->p_fixalignment;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 	if (cp->p_exec)
1052*0Sstevel@tonic-gate 		VN_HOLD(cp->p_exec);
1053*0Sstevel@tonic-gate 	if (cp->p_execdir)
1054*0Sstevel@tonic-gate 		VN_HOLD(cp->p_execdir);
1055*0Sstevel@tonic-gate 	*cpp = cp;
1056*0Sstevel@tonic-gate 	return (0);
1057*0Sstevel@tonic-gate 
1058*0Sstevel@tonic-gate bad:
1059*0Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&pidlock));
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	mutex_destroy(&cp->p_crlock);
1062*0Sstevel@tonic-gate 	mutex_destroy(&cp->p_pflock);
1063*0Sstevel@tonic-gate #if defined(__x86)
1064*0Sstevel@tonic-gate 	mutex_destroy(&cp->p_ldtlock);
1065*0Sstevel@tonic-gate #endif
1066*0Sstevel@tonic-gate 	if (newpid != -1) {
1067*0Sstevel@tonic-gate 		proc_entry_free(cp->p_pidp);
1068*0Sstevel@tonic-gate 		(void) pid_rele(cp->p_pidp);
1069*0Sstevel@tonic-gate 	}
1070*0Sstevel@tonic-gate 	kmem_cache_free(process_cache, cp);
1071*0Sstevel@tonic-gate 
1072*0Sstevel@tonic-gate 	/*
1073*0Sstevel@tonic-gate 	 * We most likely got into this situation because some process is
1074*0Sstevel@tonic-gate 	 * forking out of control.  As punishment, put it to sleep for a
1075*0Sstevel@tonic-gate 	 * bit so it can't eat the machine alive.  Sleep interval is chosen
1076*0Sstevel@tonic-gate 	 * to allow no more than one fork failure per cpu per clock tick
1077*0Sstevel@tonic-gate 	 * on average (yes, I just made this up).  This has two desirable
1078*0Sstevel@tonic-gate 	 * properties: (1) it sets a constant limit on the fork failure
1079*0Sstevel@tonic-gate 	 * rate, and (2) the busier the system is, the harsher the penalty
1080*0Sstevel@tonic-gate 	 * for abusing it becomes.
1081*0Sstevel@tonic-gate 	 */
1082*0Sstevel@tonic-gate 	INCR_COUNT(&fork_fail_pending, &pidlock);
1083*0Sstevel@tonic-gate 	delay(fork_fail_pending / ncpus + 1);
1084*0Sstevel@tonic-gate 	DECR_COUNT(&fork_fail_pending, &pidlock);
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate 	return (-1); /* out of memory or proc slots */
1087*0Sstevel@tonic-gate }
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate /*
1090*0Sstevel@tonic-gate  * Release virtual memory.
1091*0Sstevel@tonic-gate  * In the case of vfork(), the child was given exclusive access to its
1092*0Sstevel@tonic-gate  * parent's address space.  The parent is waiting in vfwait() for the
1093*0Sstevel@tonic-gate  * child to release its exclusive claim via relvm().
1094*0Sstevel@tonic-gate  */
1095*0Sstevel@tonic-gate void
1096*0Sstevel@tonic-gate relvm()
1097*0Sstevel@tonic-gate {
1098*0Sstevel@tonic-gate 	proc_t *p = curproc;
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate 	ASSERT((unsigned)p->p_lwpcnt <= 1);
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 	prrelvm();	/* inform /proc */
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate 	if (p->p_flag & SVFORK) {
1105*0Sstevel@tonic-gate 		proc_t *pp = p->p_parent;
1106*0Sstevel@tonic-gate 		/*
1107*0Sstevel@tonic-gate 		 * The child process is either exec'ing or exit'ing.
1108*0Sstevel@tonic-gate 		 * The child is now separated from the parent's address
1109*0Sstevel@tonic-gate 		 * space.  The parent process is made dispatchable.
1110*0Sstevel@tonic-gate 		 *
1111*0Sstevel@tonic-gate 		 * This is a delicate locking maneuver, involving
1112*0Sstevel@tonic-gate 		 * both the parent's p_lock and the child's p_lock.
1113*0Sstevel@tonic-gate 		 * As soon as the SVFORK flag is turned off, the
1114*0Sstevel@tonic-gate 		 * parent is free to run, but it must not run until
1115*0Sstevel@tonic-gate 		 * we wake it up using its p_cv because it might
1116*0Sstevel@tonic-gate 		 * exit and we would be referencing invalid memory.
1117*0Sstevel@tonic-gate 		 * Therefore, we hold the parent with its p_lock
1118*0Sstevel@tonic-gate 		 * while protecting our p_flags with our own p_lock.
1119*0Sstevel@tonic-gate 		 */
1120*0Sstevel@tonic-gate try_again:
1121*0Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* grab child's lock first */
1122*0Sstevel@tonic-gate 		prbarrier(p);		/* make sure /proc is blocked out */
1123*0Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 		/*
1126*0Sstevel@tonic-gate 		 * Check if parent is locked by /proc.
1127*0Sstevel@tonic-gate 		 */
1128*0Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_LOCK) {
1129*0Sstevel@tonic-gate 			/*
1130*0Sstevel@tonic-gate 			 * Delay until /proc is done with the parent.
1131*0Sstevel@tonic-gate 			 * We must drop our (the child's) p->p_lock, wait
1132*0Sstevel@tonic-gate 			 * via prbarrier() on the parent, then start over.
1133*0Sstevel@tonic-gate 			 */
1134*0Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1135*0Sstevel@tonic-gate 			prbarrier(pp);
1136*0Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
1137*0Sstevel@tonic-gate 			goto try_again;
1138*0Sstevel@tonic-gate 		}
1139*0Sstevel@tonic-gate 		p->p_flag &= ~SVFORK;
1140*0Sstevel@tonic-gate 		kpreempt_disable();
1141*0Sstevel@tonic-gate 		p->p_as = &kas;
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 		/*
1144*0Sstevel@tonic-gate 		 * notify hat of change in thread's address space
1145*0Sstevel@tonic-gate 		 */
1146*0Sstevel@tonic-gate 		hat_thread_exit(curthread);
1147*0Sstevel@tonic-gate 		kpreempt_enable();
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 		/*
1150*0Sstevel@tonic-gate 		 * child sizes are copied back to parent because
1151*0Sstevel@tonic-gate 		 * child may have grown.
1152*0Sstevel@tonic-gate 		 */
1153*0Sstevel@tonic-gate 		pp->p_brkbase = p->p_brkbase;
1154*0Sstevel@tonic-gate 		pp->p_brksize = p->p_brksize;
1155*0Sstevel@tonic-gate 		pp->p_stksize = p->p_stksize;
1156*0Sstevel@tonic-gate 		/*
1157*0Sstevel@tonic-gate 		 * The parent is no longer waiting for the vfork()d child.
1158*0Sstevel@tonic-gate 		 * Restore the parent's watched pages, if any.  This is
1159*0Sstevel@tonic-gate 		 * safe because we know the parent is not locked by /proc
1160*0Sstevel@tonic-gate 		 */
1161*0Sstevel@tonic-gate 		pp->p_flag &= ~SVFWAIT;
1162*0Sstevel@tonic-gate 		if (avl_numnodes(&pp->p_wpage) != 0) {
1163*0Sstevel@tonic-gate 			pp->p_as->a_wpage = pp->p_wpage;
1164*0Sstevel@tonic-gate 			avl_create(&pp->p_wpage, wp_compare,
1165*0Sstevel@tonic-gate 			    sizeof (struct watched_page),
1166*0Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
1167*0Sstevel@tonic-gate 		}
1168*0Sstevel@tonic-gate 		cv_signal(&pp->p_cv);
1169*0Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
1170*0Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1171*0Sstevel@tonic-gate 	} else {
1172*0Sstevel@tonic-gate 		if (p->p_as != &kas) {
1173*0Sstevel@tonic-gate 			struct as *as;
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate 			if (p->p_segacct)
1176*0Sstevel@tonic-gate 				shmexit(p);
1177*0Sstevel@tonic-gate 			/*
1178*0Sstevel@tonic-gate 			 * We grab p_lock for the benefit of /proc
1179*0Sstevel@tonic-gate 			 */
1180*0Sstevel@tonic-gate 			kpreempt_disable();
1181*0Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1182*0Sstevel@tonic-gate 			prbarrier(p);	/* make sure /proc is blocked out */
1183*0Sstevel@tonic-gate 			as = p->p_as;
1184*0Sstevel@tonic-gate 			p->p_as = &kas;
1185*0Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 			/*
1188*0Sstevel@tonic-gate 			 * notify hat of change in thread's address space
1189*0Sstevel@tonic-gate 			 */
1190*0Sstevel@tonic-gate 			hat_thread_exit(curthread);
1191*0Sstevel@tonic-gate 			kpreempt_enable();
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate 			as_free(as);
1194*0Sstevel@tonic-gate 		}
1195*0Sstevel@tonic-gate 	}
1196*0Sstevel@tonic-gate }
1197*0Sstevel@tonic-gate 
1198*0Sstevel@tonic-gate /*
1199*0Sstevel@tonic-gate  * Wait for child to exec or exit.
1200*0Sstevel@tonic-gate  * Called by parent of vfork'ed process.
1201*0Sstevel@tonic-gate  * See important comments in relvm(), above.
1202*0Sstevel@tonic-gate  */
1203*0Sstevel@tonic-gate void
1204*0Sstevel@tonic-gate vfwait(pid_t pid)
1205*0Sstevel@tonic-gate {
1206*0Sstevel@tonic-gate 	int signalled = 0;
1207*0Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
1208*0Sstevel@tonic-gate 	proc_t *cp;
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	/*
1211*0Sstevel@tonic-gate 	 * Wait for child to exec or exit.
1212*0Sstevel@tonic-gate 	 */
1213*0Sstevel@tonic-gate 	for (;;) {
1214*0Sstevel@tonic-gate 		mutex_enter(&pidlock);
1215*0Sstevel@tonic-gate 		cp = prfind(pid);
1216*0Sstevel@tonic-gate 		if (cp == NULL || cp->p_parent != pp) {
1217*0Sstevel@tonic-gate 			/*
1218*0Sstevel@tonic-gate 			 * Child has exit()ed.
1219*0Sstevel@tonic-gate 			 */
1220*0Sstevel@tonic-gate 			mutex_exit(&pidlock);
1221*0Sstevel@tonic-gate 			break;
1222*0Sstevel@tonic-gate 		}
1223*0Sstevel@tonic-gate 		/*
1224*0Sstevel@tonic-gate 		 * Grab the child's p_lock before releasing pidlock.
1225*0Sstevel@tonic-gate 		 * Otherwise, the child could exit and we would be
1226*0Sstevel@tonic-gate 		 * referencing invalid memory.
1227*0Sstevel@tonic-gate 		 */
1228*0Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
1229*0Sstevel@tonic-gate 		mutex_exit(&pidlock);
1230*0Sstevel@tonic-gate 		if (!(cp->p_flag & SVFORK)) {
1231*0Sstevel@tonic-gate 			/*
1232*0Sstevel@tonic-gate 			 * Child has exec()ed or is exit()ing.
1233*0Sstevel@tonic-gate 			 */
1234*0Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
1235*0Sstevel@tonic-gate 			break;
1236*0Sstevel@tonic-gate 		}
1237*0Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
1238*0Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
1239*0Sstevel@tonic-gate 		/*
1240*0Sstevel@tonic-gate 		 * We might be waked up spuriously from the cv_wait().
1241*0Sstevel@tonic-gate 		 * We have to do the whole operation over again to be
1242*0Sstevel@tonic-gate 		 * sure the child's SVFORK flag really is turned off.
1243*0Sstevel@tonic-gate 		 * We cannot make reference to the child because it can
1244*0Sstevel@tonic-gate 		 * exit before we return and we would be referencing
1245*0Sstevel@tonic-gate 		 * invalid memory.
1246*0Sstevel@tonic-gate 		 *
1247*0Sstevel@tonic-gate 		 * Because this is potentially a very long-term wait,
1248*0Sstevel@tonic-gate 		 * we call cv_wait_sig() (for its jobcontrol and /proc
1249*0Sstevel@tonic-gate 		 * side-effects) unless there is a current signal, in
1250*0Sstevel@tonic-gate 		 * which case we use cv_wait() because we cannot return
1251*0Sstevel@tonic-gate 		 * from this function until the child has released the
1252*0Sstevel@tonic-gate 		 * address space.  Calling cv_wait_sig() with a current
1253*0Sstevel@tonic-gate 		 * signal would lead to an indefinite loop here because
1254*0Sstevel@tonic-gate 		 * cv_wait_sig() returns immediately in this case.
1255*0Sstevel@tonic-gate 		 */
1256*0Sstevel@tonic-gate 		if (signalled)
1257*0Sstevel@tonic-gate 			cv_wait(&pp->p_cv, &pp->p_lock);
1258*0Sstevel@tonic-gate 		else
1259*0Sstevel@tonic-gate 			signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock);
1260*0Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
1261*0Sstevel@tonic-gate 	}
1262*0Sstevel@tonic-gate 
1263*0Sstevel@tonic-gate 	/* restore watchpoints to parent */
1264*0Sstevel@tonic-gate 	if (pr_watch_active(pp)) {
1265*0Sstevel@tonic-gate 		struct as *as = pp->p_as;
1266*0Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1267*0Sstevel@tonic-gate 		as_setwatch(as);
1268*0Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
1269*0Sstevel@tonic-gate 	}
1270*0Sstevel@tonic-gate 
1271*0Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
1272*0Sstevel@tonic-gate 	prbarrier(pp);	/* barrier against /proc locking */
1273*0Sstevel@tonic-gate 	continuelwps(pp);
1274*0Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
1275*0Sstevel@tonic-gate }
1276