xref: /onnv-gate/usr/src/uts/common/os/fork.c (revision 13045:76bc2e54d54b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51880Sahl  * Common Development and Distribution License (the "License").
61880Sahl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21875Sraf 
220Sstevel@tonic-gate /*
2312725SMenno.Lageman@Sun.COM  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/policy.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/cpuvar.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/proc.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/acct.h>
460Sstevel@tonic-gate #include <sys/tuneable.h>
470Sstevel@tonic-gate #include <sys/class.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/session.h>
500Sstevel@tonic-gate #include <sys/ucontext.h>
510Sstevel@tonic-gate #include <sys/stack.h>
520Sstevel@tonic-gate #include <sys/procfs.h>
530Sstevel@tonic-gate #include <sys/prsystm.h>
540Sstevel@tonic-gate #include <sys/vmsystm.h>
550Sstevel@tonic-gate #include <sys/vtrace.h>
560Sstevel@tonic-gate #include <sys/debug.h>
570Sstevel@tonic-gate #include <sys/shm_impl.h>
580Sstevel@tonic-gate #include <sys/door_data.h>
590Sstevel@tonic-gate #include <vm/as.h>
600Sstevel@tonic-gate #include <vm/rm.h>
610Sstevel@tonic-gate #include <c2/audit.h>
620Sstevel@tonic-gate #include <sys/var.h>
630Sstevel@tonic-gate #include <sys/schedctl.h>
640Sstevel@tonic-gate #include <sys/utrap.h>
650Sstevel@tonic-gate #include <sys/task.h>
660Sstevel@tonic-gate #include <sys/resource.h>
670Sstevel@tonic-gate #include <sys/cyclic.h>
680Sstevel@tonic-gate #include <sys/lgrp.h>
690Sstevel@tonic-gate #include <sys/rctl.h>
700Sstevel@tonic-gate #include <sys/contract_impl.h>
710Sstevel@tonic-gate #include <sys/contract/process_impl.h>
720Sstevel@tonic-gate #include <sys/list.h>
730Sstevel@tonic-gate #include <sys/dtrace.h>
740Sstevel@tonic-gate #include <sys/pool.h>
750Sstevel@tonic-gate #include <sys/zone.h>
760Sstevel@tonic-gate #include <sys/sdt.h>
770Sstevel@tonic-gate #include <sys/class.h>
780Sstevel@tonic-gate #include <sys/corectl.h>
792712Snn35248 #include <sys/brand.h>
803235Sraf #include <sys/fork.h>
810Sstevel@tonic-gate 
823235Sraf static int64_t cfork(int, int, int);
8311173SJonathan.Adams@Sun.COM static int getproc(proc_t **, pid_t, uint_t);
8411173SJonathan.Adams@Sun.COM #define	GETPROC_USER	0x0
8511173SJonathan.Adams@Sun.COM #define	GETPROC_KERNEL	0x1
8611173SJonathan.Adams@Sun.COM 
870Sstevel@tonic-gate static void fork_fail(proc_t *);
880Sstevel@tonic-gate static void forklwp_fail(proc_t *);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int fork_fail_pending;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate extern struct kmem_cache *process_cache;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
9511798SRoger.Faulkner@Sun.COM  * The vfork() system call trap is no longer invoked by libc.
9611798SRoger.Faulkner@Sun.COM  * It is retained only for the benefit of applications running
9711798SRoger.Faulkner@Sun.COM  * within a solaris10 branded zone.  It should be eliminated
9811798SRoger.Faulkner@Sun.COM  * when we no longer support solaris10 branded zones.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate int64_t
vfork(void)1010Sstevel@tonic-gate vfork(void)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* so vfwait() will be called */
1043235Sraf 	return (cfork(1, 1, 0));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
10811798SRoger.Faulkner@Sun.COM  * forksys system call - forkx, forkallx, vforkx.  This is the
10911798SRoger.Faulkner@Sun.COM  * interface invoked by libc for fork1(), forkall(), and vfork()
1103235Sraf  */
1113235Sraf int64_t
forksys(int subcode,int flags)1123235Sraf forksys(int subcode, int flags)
1133235Sraf {
1143235Sraf 	switch (subcode) {
1153235Sraf 	case 0:
1163235Sraf 		return (cfork(0, 1, flags));	/* forkx(flags) */
1173235Sraf 	case 1:
1183235Sraf 		return (cfork(0, 0, flags));	/* forkallx(flags) */
1193235Sraf 	case 2:
1203235Sraf 		curthread->t_post_sys = 1;	/* so vfwait() will be called */
1213235Sraf 		return (cfork(1, 1, flags));	/* vforkx(flags) */
1223235Sraf 	default:
1233235Sraf 		return ((int64_t)set_errno(EINVAL));
1243235Sraf 	}
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /* ARGSUSED */
1280Sstevel@tonic-gate static int64_t
cfork(int isvfork,int isfork1,int flags)1293235Sraf cfork(int isvfork, int isfork1, int flags)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1320Sstevel@tonic-gate 	struct as *as;
1330Sstevel@tonic-gate 	proc_t *cp, **orphpp;
1340Sstevel@tonic-gate 	klwp_t *clone;
1350Sstevel@tonic-gate 	kthread_t *t;
1360Sstevel@tonic-gate 	task_t *tk;
1370Sstevel@tonic-gate 	rval_t	r;
1380Sstevel@tonic-gate 	int error;
1390Sstevel@tonic-gate 	int i;
1400Sstevel@tonic-gate 	rctl_set_t *dup_set;
1410Sstevel@tonic-gate 	rctl_alloc_gp_t *dup_gp;
1420Sstevel@tonic-gate 	rctl_entity_p_t e;
1430Sstevel@tonic-gate 	lwpdir_t *ldp;
1440Sstevel@tonic-gate 	lwpent_t *lep;
1450Sstevel@tonic-gate 	lwpent_t *clep;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1483235Sraf 	 * Allow only these two flags.
1493235Sraf 	 */
1503235Sraf 	if ((flags & ~(FORK_NOSIGCHLD | FORK_WAITPID)) != 0) {
1513235Sraf 		error = EINVAL;
1523235Sraf 		goto forkerr;
1533235Sraf 	}
1543235Sraf 
1553235Sraf 	/*
1560Sstevel@tonic-gate 	 * fork is not supported for the /proc agent lwp.
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (curthread == p->p_agenttp) {
1590Sstevel@tonic-gate 		error = ENOTSUP;
1600Sstevel@tonic-gate 		goto forkerr;
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if ((error = secpolicy_basic_fork(CRED())) != 0)
1640Sstevel@tonic-gate 		goto forkerr;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * If the calling lwp is doing a fork1() then the
1680Sstevel@tonic-gate 	 * other lwps in this process are not duplicated and
1690Sstevel@tonic-gate 	 * don't need to be held where their kernel stacks can be
1700Sstevel@tonic-gate 	 * cloned.  If doing forkall(), the process is held with
1710Sstevel@tonic-gate 	 * SHOLDFORK, so that the lwps are at a point where their
1720Sstevel@tonic-gate 	 * stacks can be copied which is on entry or exit from
1730Sstevel@tonic-gate 	 * the kernel.
1740Sstevel@tonic-gate 	 */
1750Sstevel@tonic-gate 	if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) {
1760Sstevel@tonic-gate 		aston(curthread);
1770Sstevel@tonic-gate 		error = EINTR;
1780Sstevel@tonic-gate 		goto forkerr;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1811091Sraf #if defined(__sparc)
1821091Sraf 	/*
1831091Sraf 	 * Ensure that the user stack is fully constructed
1841091Sraf 	 * before creating the child process structure.
1851091Sraf 	 */
1861091Sraf 	(void) flush_user_windows_to_stack(NULL);
1871091Sraf #endif
1881091Sraf 
1891091Sraf 	mutex_enter(&p->p_lock);
190875Sraf 	/*
191875Sraf 	 * If this is vfork(), cancel any suspend request we might
192875Sraf 	 * have gotten from some other thread via lwp_suspend().
193875Sraf 	 * Otherwise we could end up with a deadlock on return
194875Sraf 	 * from the vfork() in both the parent and the child.
195875Sraf 	 */
196875Sraf 	if (isvfork)
197875Sraf 		curthread->t_proc_flag &= ~TP_HOLDLWP;
1980Sstevel@tonic-gate 	/*
1990Sstevel@tonic-gate 	 * Prevent our resource set associations from being changed during fork.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	pool_barrier_enter();
2020Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes.
2060Sstevel@tonic-gate 	 */
20711173SJonathan.Adams@Sun.COM 	if (getproc(&cp, 0, GETPROC_USER) < 0) {
2080Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2090Sstevel@tonic-gate 		pool_barrier_exit();
2100Sstevel@tonic-gate 		continuelwps(p);
2110Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2120Sstevel@tonic-gate 		error = EAGAIN;
2130Sstevel@tonic-gate 		goto forkerr;
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Assign an address space to child
2200Sstevel@tonic-gate 	 */
2210Sstevel@tonic-gate 	if (isvfork) {
2220Sstevel@tonic-gate 		/*
2230Sstevel@tonic-gate 		 * Clear any watched areas and remember the
2240Sstevel@tonic-gate 		 * watched pages for restoring in vfwait().
2250Sstevel@tonic-gate 		 */
2260Sstevel@tonic-gate 		as = p->p_as;
2270Sstevel@tonic-gate 		if (avl_numnodes(&as->a_wpage) != 0) {
2280Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2290Sstevel@tonic-gate 			as_clearwatch(as);
2300Sstevel@tonic-gate 			p->p_wpage = as->a_wpage;
2310Sstevel@tonic-gate 			avl_create(&as->a_wpage, wp_compare,
2320Sstevel@tonic-gate 			    sizeof (struct watched_page),
2330Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
2340Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		cp->p_as = as;
2370Sstevel@tonic-gate 		cp->p_flag |= SVFORK;
23811661SVamsi.Krishna@Sun.COM 
23911661SVamsi.Krishna@Sun.COM 		/*
24011661SVamsi.Krishna@Sun.COM 		 * Use the parent's shm segment list information for
24111661SVamsi.Krishna@Sun.COM 		 * the child as it uses its address space till it execs.
24211661SVamsi.Krishna@Sun.COM 		 */
24311661SVamsi.Krishna@Sun.COM 		cp->p_segacct = p->p_segacct;
2440Sstevel@tonic-gate 	} else {
2450Sstevel@tonic-gate 		/*
2460Sstevel@tonic-gate 		 * We need to hold P_PR_LOCK until the address space has
2470Sstevel@tonic-gate 		 * been duplicated and we've had a chance to remove from the
2480Sstevel@tonic-gate 		 * child any DTrace probes that were in the parent. Holding
2490Sstevel@tonic-gate 		 * P_PR_LOCK prevents any new probes from being added and any
2500Sstevel@tonic-gate 		 * extant probes from being removed.
2510Sstevel@tonic-gate 		 */
2520Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2530Sstevel@tonic-gate 		sprlock_proc(p);
2541880Sahl 		p->p_flag |= SFORKING;
2550Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2560Sstevel@tonic-gate 
2579121SVamsi.Krishna@Sun.COM 		error = as_dup(p->p_as, cp);
2580Sstevel@tonic-gate 		if (error != 0) {
2594202Sethindra 			mutex_enter(&p->p_lock);
2604202Sethindra 			sprunlock(p);
2610Sstevel@tonic-gate 			fork_fail(cp);
2620Sstevel@tonic-gate 			mutex_enter(&pidlock);
2630Sstevel@tonic-gate 			orphpp = &p->p_orphan;
2640Sstevel@tonic-gate 			while (*orphpp != cp)
2650Sstevel@tonic-gate 				orphpp = &(*orphpp)->p_nextorph;
2660Sstevel@tonic-gate 			*orphpp = cp->p_nextorph;
2672241Shuah 			if (p->p_child == cp)
2682241Shuah 				p->p_child = cp->p_sibling;
2692241Shuah 			if (cp->p_sibling)
2702241Shuah 				cp->p_sibling->p_psibling = cp->p_psibling;
2712241Shuah 			if (cp->p_psibling)
2722241Shuah 				cp->p_psibling->p_sibling = cp->p_sibling;
2730Sstevel@tonic-gate 			mutex_enter(&cp->p_lock);
2740Sstevel@tonic-gate 			tk = cp->p_task;
2750Sstevel@tonic-gate 			task_detach(cp);
2760Sstevel@tonic-gate 			ASSERT(cp->p_pool->pool_ref > 0);
2770Sstevel@tonic-gate 			atomic_add_32(&cp->p_pool->pool_ref, -1);
2780Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
27912834SMenno.Lageman@Sun.COM 			pid_exit(cp, tk);
2800Sstevel@tonic-gate 			mutex_exit(&pidlock);
2810Sstevel@tonic-gate 			task_rele(tk);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2841880Sahl 			p->p_flag &= ~SFORKING;
2850Sstevel@tonic-gate 			pool_barrier_exit();
2860Sstevel@tonic-gate 			continuelwps(p);
2874202Sethindra 			mutex_exit(&p->p_lock);
2880Sstevel@tonic-gate 			/*
2890Sstevel@tonic-gate 			 * Preserve ENOMEM error condition but
2900Sstevel@tonic-gate 			 * map all others to EAGAIN.
2910Sstevel@tonic-gate 			 */
2920Sstevel@tonic-gate 			error = (error == ENOMEM) ? ENOMEM : EAGAIN;
2930Sstevel@tonic-gate 			goto forkerr;
2940Sstevel@tonic-gate 		}
2952768Ssl108498 
2961880Sahl 		/*
2971880Sahl 		 * Remove all DTrace tracepoints from the child process. We
2981880Sahl 		 * need to do this _before_ duplicating USDT providers since
2991880Sahl 		 * any associated probes may be immediately enabled.
3001880Sahl 		 */
3011880Sahl 		if (p->p_dtrace_count > 0)
3021880Sahl 			dtrace_fasttrap_fork(p, cp);
3031880Sahl 
30410720SEthindra.Ramamurthy@Sun.COM 		mutex_enter(&p->p_lock);
30510720SEthindra.Ramamurthy@Sun.COM 		sprunlock(p);
30610720SEthindra.Ramamurthy@Sun.COM 
30710720SEthindra.Ramamurthy@Sun.COM 		/* Duplicate parent's shared memory */
30810720SEthindra.Ramamurthy@Sun.COM 		if (p->p_segacct)
30910720SEthindra.Ramamurthy@Sun.COM 			shmfork(p, cp);
31010720SEthindra.Ramamurthy@Sun.COM 
3111880Sahl 		/*
3121880Sahl 		 * Duplicate any helper actions and providers. The SFORKING
3131880Sahl 		 * we set above informs the code to enable USDT probes that
3141880Sahl 		 * sprlock() may fail because the child is being forked.
3151880Sahl 		 */
3160Sstevel@tonic-gate 		if (p->p_dtrace_helpers != NULL) {
3170Sstevel@tonic-gate 			ASSERT(dtrace_helpers_fork != NULL);
3180Sstevel@tonic-gate 			(*dtrace_helpers_fork)(p, cp);
31910720SEthindra.Ramamurthy@Sun.COM 		}
3204273Sahl 
32110720SEthindra.Ramamurthy@Sun.COM 		mutex_enter(&p->p_lock);
32210720SEthindra.Ramamurthy@Sun.COM 		p->p_flag &= ~SFORKING;
32310720SEthindra.Ramamurthy@Sun.COM 		mutex_exit(&p->p_lock);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	/*
3270Sstevel@tonic-gate 	 * Duplicate parent's resource controls.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	dup_set = rctl_set_create();
3300Sstevel@tonic-gate 	for (;;) {
3310Sstevel@tonic-gate 		dup_gp = rctl_set_dup_prealloc(p->p_rctls);
3320Sstevel@tonic-gate 		mutex_enter(&p->p_rctls->rcs_lock);
3330Sstevel@tonic-gate 		if (rctl_set_dup_ready(p->p_rctls, dup_gp))
3340Sstevel@tonic-gate 			break;
3350Sstevel@tonic-gate 		mutex_exit(&p->p_rctls->rcs_lock);
3360Sstevel@tonic-gate 		rctl_prealloc_destroy(dup_gp);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 	e.rcep_p.proc = cp;
3390Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
3400Sstevel@tonic-gate 	cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp,
3410Sstevel@tonic-gate 	    RCD_DUP | RCD_CALLBACK);
3420Sstevel@tonic-gate 	mutex_exit(&p->p_rctls->rcs_lock);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	rctl_prealloc_destroy(dup_gp);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	/*
3470Sstevel@tonic-gate 	 * Allocate the child's lwp directory and lwpid hash table.
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	if (isfork1)
3500Sstevel@tonic-gate 		cp->p_lwpdir_sz = 2;
3510Sstevel@tonic-gate 	else
3520Sstevel@tonic-gate 		cp->p_lwpdir_sz = p->p_lwpdir_sz;
3530Sstevel@tonic-gate 	cp->p_lwpdir = cp->p_lwpfree = ldp =
3545753Sgww 	    kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP);
3550Sstevel@tonic-gate 	for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++)
3560Sstevel@tonic-gate 		ldp->ld_next = ldp + 1;
3570Sstevel@tonic-gate 	cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2;
3580Sstevel@tonic-gate 	cp->p_tidhash =
3599393SRoger.Faulkner@Sun.COM 	    kmem_zalloc(cp->p_tidhash_sz * sizeof (tidhash_t), KM_SLEEP);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	/*
3620Sstevel@tonic-gate 	 * Duplicate parent's lwps.
3630Sstevel@tonic-gate 	 * Mutual exclusion is not needed because the process is
3640Sstevel@tonic-gate 	 * in the hold state and only the current lwp is running.
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	klgrpset_clear(cp->p_lgrpset);
3670Sstevel@tonic-gate 	if (isfork1) {
3680Sstevel@tonic-gate 		clone = forklwp(ttolwp(curthread), cp, curthread->t_tid);
3690Sstevel@tonic-gate 		if (clone == NULL)
3700Sstevel@tonic-gate 			goto forklwperr;
3710Sstevel@tonic-gate 		/*
3720Sstevel@tonic-gate 		 * Inherit only the lwp_wait()able flag,
3730Sstevel@tonic-gate 		 * Daemon threads should not call fork1(), but oh well...
3740Sstevel@tonic-gate 		 */
3750Sstevel@tonic-gate 		lwptot(clone)->t_proc_flag |=
3765753Sgww 		    (curthread->t_proc_flag & TP_TWAIT);
3770Sstevel@tonic-gate 	} else {
3780Sstevel@tonic-gate 		/* this is forkall(), no one can be in lwp_wait() */
3790Sstevel@tonic-gate 		ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0);
3800Sstevel@tonic-gate 		/* for each entry in the parent's lwp directory... */
3810Sstevel@tonic-gate 		for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) {
3820Sstevel@tonic-gate 			klwp_t *clwp;
383769Sraf 			kthread_t *ct;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) == NULL)
3860Sstevel@tonic-gate 				continue;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 			if ((t = lep->le_thread) != NULL) {
3890Sstevel@tonic-gate 				clwp = forklwp(ttolwp(t), cp, t->t_tid);
3900Sstevel@tonic-gate 				if (clwp == NULL)
3910Sstevel@tonic-gate 					goto forklwperr;
392769Sraf 				ct = lwptot(clwp);
3930Sstevel@tonic-gate 				/*
3940Sstevel@tonic-gate 				 * Inherit lwp_wait()able and daemon flags.
3950Sstevel@tonic-gate 				 */
396769Sraf 				ct->t_proc_flag |=
3970Sstevel@tonic-gate 				    (t->t_proc_flag & (TP_TWAIT|TP_DAEMON));
3980Sstevel@tonic-gate 				/*
3990Sstevel@tonic-gate 				 * Keep track of the clone of curthread to
4000Sstevel@tonic-gate 				 * post return values through lwp_setrval().
401769Sraf 				 * Mark other threads for special treatment
402769Sraf 				 * by lwp_rtt() / post_syscall().
4030Sstevel@tonic-gate 				 */
4040Sstevel@tonic-gate 				if (t == curthread)
4050Sstevel@tonic-gate 					clone = clwp;
406769Sraf 				else
407769Sraf 					ct->t_flag |= T_FORKALL;
4080Sstevel@tonic-gate 			} else {
4090Sstevel@tonic-gate 				/*
4100Sstevel@tonic-gate 				 * Replicate zombie lwps in the child.
4110Sstevel@tonic-gate 				 */
4120Sstevel@tonic-gate 				clep = kmem_zalloc(sizeof (*clep), KM_SLEEP);
4130Sstevel@tonic-gate 				clep->le_lwpid = lep->le_lwpid;
4140Sstevel@tonic-gate 				clep->le_start = lep->le_start;
4159393SRoger.Faulkner@Sun.COM 				lwp_hash_in(cp, clep,
4169393SRoger.Faulkner@Sun.COM 				    cp->p_tidhash, cp->p_tidhash_sz, 0);
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4220Sstevel@tonic-gate 	 * Put new process in the parent's process contract, or put it
4230Sstevel@tonic-gate 	 * in a new one if there is an active process template.  Send a
4240Sstevel@tonic-gate 	 * fork event (if requested) to whatever contract the child is
4250Sstevel@tonic-gate 	 * a member of.  Fails if the parent has been SIGKILLed.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL)
4280Sstevel@tonic-gate 		goto forklwperr;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * No fork failures occur beyond this point.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	cp->p_lwpid = p->p_lwpid;
4350Sstevel@tonic-gate 	if (!isfork1) {
4360Sstevel@tonic-gate 		cp->p_lwpdaemon = p->p_lwpdaemon;
4370Sstevel@tonic-gate 		cp->p_zombcnt = p->p_zombcnt;
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 * If the parent's lwp ids have wrapped around, so have the
4400Sstevel@tonic-gate 		 * child's.
4410Sstevel@tonic-gate 		 */
4420Sstevel@tonic-gate 		cp->p_flag |= p->p_flag & SLWPWRAP;
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
4452556Skk112340 	mutex_enter(&p->p_lock);
4460Sstevel@tonic-gate 	corectl_path_hold(cp->p_corefile = p->p_corefile);
4470Sstevel@tonic-gate 	corectl_content_hold(cp->p_content = p->p_content);
4482556Skk112340 	mutex_exit(&p->p_lock);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4511217Srab 	 * Duplicate process context ops, if any.
4520Sstevel@tonic-gate 	 */
4531217Srab 	if (p->p_pctx)
4541217Srab 		forkpctx(p, cp);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate #ifdef __sparc
4570Sstevel@tonic-gate 	utrap_dup(p, cp);
4580Sstevel@tonic-gate #endif
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * If the child process has been marked to stop on exit
4610Sstevel@tonic-gate 	 * from this fork, arrange for all other lwps to stop in
4620Sstevel@tonic-gate 	 * sympathy with the active lwp.
4630Sstevel@tonic-gate 	 */
4640Sstevel@tonic-gate 	if (PTOU(cp)->u_systrap &&
4650Sstevel@tonic-gate 	    prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) {
4660Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
4670Sstevel@tonic-gate 		t = cp->p_tlist;
4680Sstevel@tonic-gate 		do {
4690Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
4700Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
4710Sstevel@tonic-gate 		} while ((t = t->t_forw) != cp->p_tlist);
4720Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * If the parent process has been marked to stop on exit
4760Sstevel@tonic-gate 	 * from this fork, and its asynchronous-stop flag has not
4770Sstevel@tonic-gate 	 * been set, arrange for all other lwps to stop before
4780Sstevel@tonic-gate 	 * they return back to user level.
4790Sstevel@tonic-gate 	 */
4800Sstevel@tonic-gate 	if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap &&
4810Sstevel@tonic-gate 	    prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) {
4820Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4830Sstevel@tonic-gate 		t = p->p_tlist;
4840Sstevel@tonic-gate 		do {
4850Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
4860Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
4870Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
4880Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4912712Snn35248 	if (PROC_IS_BRANDED(p))
4922712Snn35248 		BROP(p)->b_lwp_setrval(clone, p->p_pid, 1);
4932712Snn35248 	else
4942712Snn35248 		lwp_setrval(clone, p->p_pid, 1);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/* set return values for parent */
4970Sstevel@tonic-gate 	r.r_val1 = (int)cp->p_pid;
4980Sstevel@tonic-gate 	r.r_val2 = 0;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * pool_barrier_exit() can now be called because the child process has:
5020Sstevel@tonic-gate 	 * - all identifying features cloned or set (p_pid, p_task, p_pool)
5030Sstevel@tonic-gate 	 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset)
5040Sstevel@tonic-gate 	 * - any other fields set which are used in resource set binding.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
5070Sstevel@tonic-gate 	pool_barrier_exit();
5080Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	mutex_enter(&pidlock);
5110Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/*
5143235Sraf 	 * Set flags telling the child what (not) to do on exit.
5153235Sraf 	 */
5163235Sraf 	if (flags & FORK_NOSIGCHLD)
5173235Sraf 		cp->p_pidflag |= CLDNOSIGCHLD;
5183235Sraf 	if (flags & FORK_WAITPID)
5193235Sraf 		cp->p_pidflag |= CLDWAITPID;
5203235Sraf 
5213235Sraf 	/*
5220Sstevel@tonic-gate 	 * Now that there are lwps and threads attached, add the new
5230Sstevel@tonic-gate 	 * process to the process group.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate 	pgjoin(cp, p->p_pgidp);
5260Sstevel@tonic-gate 	cp->p_stat = SRUN;
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * We are now done with all the lwps in the child process.
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	t = cp->p_tlist;
5310Sstevel@tonic-gate 	do {
5320Sstevel@tonic-gate 		/*
5330Sstevel@tonic-gate 		 * Set the lwp_suspend()ed lwps running.
5340Sstevel@tonic-gate 		 * They will suspend properly at syscall exit.
5350Sstevel@tonic-gate 		 */
5360Sstevel@tonic-gate 		if (t->t_proc_flag & TP_HOLDLWP)
5370Sstevel@tonic-gate 			lwp_create_done(t);
5380Sstevel@tonic-gate 		else {
5390Sstevel@tonic-gate 			/* set TS_CREATE to allow continuelwps() to work */
5400Sstevel@tonic-gate 			thread_lock(t);
5410Sstevel@tonic-gate 			ASSERT(t->t_state == TS_STOPPED &&
5420Sstevel@tonic-gate 			    !(t->t_schedflag & (TS_CREATE|TS_CSTART)));
5430Sstevel@tonic-gate 			t->t_schedflag |= TS_CREATE;
5440Sstevel@tonic-gate 			thread_unlock(t);
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	} while ((t = t->t_forw) != cp->p_tlist);
5470Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (isvfork) {
5500Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysvfork, 1);
5510Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5523828Sraf 		p->p_flag |= SVFWAIT;
5533828Sraf 		curthread->t_flag |= T_VFPARENT;
5540Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
5550Sstevel@tonic-gate 		cv_broadcast(&pr_pid_cv[p->p_slot]);	/* inform /proc */
5560Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5570Sstevel@tonic-gate 		/*
5580Sstevel@tonic-gate 		 * Grab child's p_lock before dropping pidlock to ensure
5590Sstevel@tonic-gate 		 * the process will not disappear before we set it running.
5600Sstevel@tonic-gate 		 */
5610Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
5620Sstevel@tonic-gate 		mutex_exit(&pidlock);
5630Sstevel@tonic-gate 		sigdefault(cp);
5640Sstevel@tonic-gate 		continuelwps(cp);
5650Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
5660Sstevel@tonic-gate 	} else {
5670Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysfork, 1);
5680Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
5690Sstevel@tonic-gate 		/*
5700Sstevel@tonic-gate 		 * It is CL_FORKRET's job to drop pidlock.
5710Sstevel@tonic-gate 		 * If we do it here, the process could be set running
5720Sstevel@tonic-gate 		 * and disappear before CL_FORKRET() is called.
5730Sstevel@tonic-gate 		 */
5740Sstevel@tonic-gate 		CL_FORKRET(curthread, cp->p_tlist);
5756247Sraf 		schedctl_set_cidpri(curthread);
5760Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&pidlock));
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (r.r_vals);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate forklwperr:
5820Sstevel@tonic-gate 	if (isvfork) {
5830Sstevel@tonic-gate 		if (avl_numnodes(&p->p_wpage) != 0) {
5840Sstevel@tonic-gate 			/* restore watchpoints to parent */
5850Sstevel@tonic-gate 			as = p->p_as;
5866247Sraf 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
5870Sstevel@tonic-gate 			as->a_wpage = p->p_wpage;
5880Sstevel@tonic-gate 			avl_create(&p->p_wpage, wp_compare,
5890Sstevel@tonic-gate 			    sizeof (struct watched_page),
5900Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
5910Sstevel@tonic-gate 			as_setwatch(as);
5920Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 	} else {
5950Sstevel@tonic-gate 		if (cp->p_segacct)
5960Sstevel@tonic-gate 			shmexit(cp);
5970Sstevel@tonic-gate 		as = cp->p_as;
5980Sstevel@tonic-gate 		cp->p_as = &kas;
5990Sstevel@tonic-gate 		as_free(as);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	if (cp->p_lwpdir) {
6030Sstevel@tonic-gate 		for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++)
6040Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) != NULL)
6050Sstevel@tonic-gate 				kmem_free(lep, sizeof (*lep));
6060Sstevel@tonic-gate 		kmem_free(cp->p_lwpdir,
6070Sstevel@tonic-gate 		    cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir));
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	cp->p_lwpdir = NULL;
6100Sstevel@tonic-gate 	cp->p_lwpfree = NULL;
6110Sstevel@tonic-gate 	cp->p_lwpdir_sz = 0;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (cp->p_tidhash)
6140Sstevel@tonic-gate 		kmem_free(cp->p_tidhash,
6150Sstevel@tonic-gate 		    cp->p_tidhash_sz * sizeof (*cp->p_tidhash));
6160Sstevel@tonic-gate 	cp->p_tidhash = NULL;
6170Sstevel@tonic-gate 	cp->p_tidhash_sz = 0;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	forklwp_fail(cp);
6200Sstevel@tonic-gate 	fork_fail(cp);
6210Sstevel@tonic-gate 	rctl_set_free(cp->p_rctls);
6220Sstevel@tonic-gate 	mutex_enter(&pidlock);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/*
6250Sstevel@tonic-gate 	 * Detach failed child from task.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
6280Sstevel@tonic-gate 	tk = cp->p_task;
6290Sstevel@tonic-gate 	task_detach(cp);
6300Sstevel@tonic-gate 	ASSERT(cp->p_pool->pool_ref > 0);
6310Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, -1);
6320Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	orphpp = &p->p_orphan;
6350Sstevel@tonic-gate 	while (*orphpp != cp)
6360Sstevel@tonic-gate 		orphpp = &(*orphpp)->p_nextorph;
6370Sstevel@tonic-gate 	*orphpp = cp->p_nextorph;
6382241Shuah 	if (p->p_child == cp)
6392241Shuah 		p->p_child = cp->p_sibling;
6402241Shuah 	if (cp->p_sibling)
6412241Shuah 		cp->p_sibling->p_psibling = cp->p_psibling;
6422241Shuah 	if (cp->p_psibling)
6432241Shuah 		cp->p_psibling->p_sibling = cp->p_sibling;
64412834SMenno.Lageman@Sun.COM 	pid_exit(cp, tk);
6450Sstevel@tonic-gate 	mutex_exit(&pidlock);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	task_rele(tk);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6500Sstevel@tonic-gate 	pool_barrier_exit();
6510Sstevel@tonic-gate 	continuelwps(p);
6520Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6530Sstevel@tonic-gate 	error = EAGAIN;
6540Sstevel@tonic-gate forkerr:
6550Sstevel@tonic-gate 	return ((int64_t)set_errno(error));
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * Free allocated resources from getproc() if a fork failed.
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate static void
fork_fail(proc_t * cp)6620Sstevel@tonic-gate fork_fail(proc_t *cp)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(cp);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	fcnt_add(fip, -1);
6670Sstevel@tonic-gate 	sigdelq(cp, NULL, 0);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	mutex_enter(&pidlock);
6700Sstevel@tonic-gate 	upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred));
6710Sstevel@tonic-gate 	mutex_exit(&pidlock);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * single threaded, so no locking needed here
6750Sstevel@tonic-gate 	 */
6760Sstevel@tonic-gate 	crfree(cp->p_cred);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
6790Sstevel@tonic-gate 
6803446Smrj 	VN_RELE(PTOU(curproc)->u_cdir);
6813446Smrj 	if (PTOU(curproc)->u_rdir)
6823446Smrj 		VN_RELE(PTOU(curproc)->u_rdir);
6830Sstevel@tonic-gate 	if (cp->p_exec)
6840Sstevel@tonic-gate 		VN_RELE(cp->p_exec);
6850Sstevel@tonic-gate 	if (cp->p_execdir)
6860Sstevel@tonic-gate 		VN_RELE(cp->p_execdir);
6873446Smrj 	if (PTOU(curproc)->u_cwd)
6883446Smrj 		refstr_rele(PTOU(curproc)->u_cwd);
689*13045SVamsi.Krishna@Sun.COM 	if (PROC_IS_BRANDED(cp)) {
690*13045SVamsi.Krishna@Sun.COM 		brand_clearbrand(cp, B_TRUE);
691*13045SVamsi.Krishna@Sun.COM 	}
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6950Sstevel@tonic-gate  * Clean up the lwps already created for this child process.
6960Sstevel@tonic-gate  * The fork failed while duplicating all the lwps of the parent
6970Sstevel@tonic-gate  * and those lwps already created must be freed.
6980Sstevel@tonic-gate  * This process is invisible to the rest of the system,
6990Sstevel@tonic-gate  * so we don't need to hold p->p_lock to protect the list.
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate static void
forklwp_fail(proc_t * p)7020Sstevel@tonic-gate forklwp_fail(proc_t *p)
7030Sstevel@tonic-gate {
7040Sstevel@tonic-gate 	kthread_t *t;
7050Sstevel@tonic-gate 	task_t *tk;
706*13045SVamsi.Krishna@Sun.COM 	int branded = 0;
707*13045SVamsi.Krishna@Sun.COM 
708*13045SVamsi.Krishna@Sun.COM 	if (PROC_IS_BRANDED(p))
709*13045SVamsi.Krishna@Sun.COM 		branded = 1;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	while ((t = p->p_tlist) != NULL) {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * First remove the lwp from the process's p_tlist.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		if (t != t->t_forw)
7160Sstevel@tonic-gate 			p->p_tlist = t->t_forw;
7170Sstevel@tonic-gate 		else
7180Sstevel@tonic-gate 			p->p_tlist = NULL;
7190Sstevel@tonic-gate 		p->p_lwpcnt--;
7200Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
7210Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		tk = p->p_task;
7240Sstevel@tonic-gate 		mutex_enter(&p->p_zone->zone_nlwps_lock);
7250Sstevel@tonic-gate 		tk->tk_nlwps--;
7260Sstevel@tonic-gate 		tk->tk_proj->kpj_nlwps--;
7270Sstevel@tonic-gate 		p->p_zone->zone_nlwps--;
7280Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 		ASSERT(t->t_schedctl == NULL);
7310Sstevel@tonic-gate 
732*13045SVamsi.Krishna@Sun.COM 		if (branded)
733*13045SVamsi.Krishna@Sun.COM 			BROP(p)->b_freelwp(ttolwp(t));
734*13045SVamsi.Krishna@Sun.COM 
7350Sstevel@tonic-gate 		if (t->t_door != NULL) {
7360Sstevel@tonic-gate 			kmem_free(t->t_door, sizeof (door_data_t));
7370Sstevel@tonic-gate 			t->t_door = NULL;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		lwp_ctmpl_clear(ttolwp(t));
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		/*
7420Sstevel@tonic-gate 		 * Remove the thread from the all threads list.
7430Sstevel@tonic-gate 		 * We need to hold pidlock for this.
7440Sstevel@tonic-gate 		 */
7450Sstevel@tonic-gate 		mutex_enter(&pidlock);
7460Sstevel@tonic-gate 		t->t_next->t_prev = t->t_prev;
7470Sstevel@tonic-gate 		t->t_prev->t_next = t->t_next;
7480Sstevel@tonic-gate 		CL_EXIT(t);	/* tell the scheduler that we're exiting */
7490Sstevel@tonic-gate 		cv_broadcast(&t->t_joincv);	/* tell anyone in thread_join */
7500Sstevel@tonic-gate 		mutex_exit(&pidlock);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		/*
7530Sstevel@tonic-gate 		 * Let the lgroup load averages know that this thread isn't
7540Sstevel@tonic-gate 		 * going to show up (i.e. un-do what was done on behalf of
7550Sstevel@tonic-gate 		 * this thread by the earlier lgrp_move_thread()).
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		kpreempt_disable();
7580Sstevel@tonic-gate 		lgrp_move_thread(t, NULL, 1);
7590Sstevel@tonic-gate 		kpreempt_enable();
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		/*
7620Sstevel@tonic-gate 		 * The thread was created TS_STOPPED.
7630Sstevel@tonic-gate 		 * We change it to TS_FREE to avoid an
7640Sstevel@tonic-gate 		 * ASSERT() panic in thread_free().
7650Sstevel@tonic-gate 		 */
7660Sstevel@tonic-gate 		t->t_state = TS_FREE;
7670Sstevel@tonic-gate 		thread_rele(t);
7680Sstevel@tonic-gate 		thread_free(t);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate extern struct as kas;
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * fork a kernel process.
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate int
newproc(void (* pc)(),caddr_t arg,id_t cid,int pri,struct contract ** ct,pid_t pid)77811173SJonathan.Adams@Sun.COM newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct,
77911173SJonathan.Adams@Sun.COM     pid_t pid)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	proc_t *p;
7820Sstevel@tonic-gate 	struct user *up;
78311173SJonathan.Adams@Sun.COM 	kthread_t *t;
7840Sstevel@tonic-gate 	cont_process_t *ctp = NULL;
7850Sstevel@tonic-gate 	rctl_entity_p_t e;
7860Sstevel@tonic-gate 
78711173SJonathan.Adams@Sun.COM 	ASSERT(cid != sysdccid);
78811173SJonathan.Adams@Sun.COM 	ASSERT(cid != syscid || ct == NULL);
78911173SJonathan.Adams@Sun.COM 	if (CLASS_KERNEL(cid)) {
7900Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp;
7910Sstevel@tonic-gate 		rctl_set_t *init_set;
7920Sstevel@tonic-gate 
79311173SJonathan.Adams@Sun.COM 		ASSERT(pid != 1);
79411173SJonathan.Adams@Sun.COM 
79511173SJonathan.Adams@Sun.COM 		if (getproc(&p, pid, GETPROC_KERNEL) < 0)
7960Sstevel@tonic-gate 			return (EAGAIN);
7970Sstevel@tonic-gate 
79810381SPramod.Batni@Sun.COM 		/*
79910381SPramod.Batni@Sun.COM 		 * Release the hold on the p_exec and p_execdir, these
80010381SPramod.Batni@Sun.COM 		 * were acquired in getproc()
80110381SPramod.Batni@Sun.COM 		 */
80210381SPramod.Batni@Sun.COM 		if (p->p_execdir != NULL)
80310381SPramod.Batni@Sun.COM 			VN_RELE(p->p_execdir);
80410381SPramod.Batni@Sun.COM 		if (p->p_exec != NULL)
80510381SPramod.Batni@Sun.COM 			VN_RELE(p->p_exec);
8060Sstevel@tonic-gate 		p->p_flag |= SNOWAIT;
8070Sstevel@tonic-gate 		p->p_exec = NULL;
8080Sstevel@tonic-gate 		p->p_execdir = NULL;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		init_set = rctl_set_create();
8110Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		/*
8140Sstevel@tonic-gate 		 * kernel processes do not inherit /proc tracing flags.
8150Sstevel@tonic-gate 		 */
8160Sstevel@tonic-gate 		sigemptyset(&p->p_sigmask);
8170Sstevel@tonic-gate 		premptyset(&p->p_fltmask);
8180Sstevel@tonic-gate 		up = PTOU(p);
8190Sstevel@tonic-gate 		up->u_systrap = 0;
8200Sstevel@tonic-gate 		premptyset(&(up->u_entrymask));
8210Sstevel@tonic-gate 		premptyset(&(up->u_exitmask));
8220Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8230Sstevel@tonic-gate 		e.rcep_p.proc = p;
8240Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
8250Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
8260Sstevel@tonic-gate 		    init_gp);
8270Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
83011173SJonathan.Adams@Sun.COM 
83111173SJonathan.Adams@Sun.COM 		t = lwp_kernel_create(p, pc, arg, TS_STOPPED, pri);
83211173SJonathan.Adams@Sun.COM 	} else {
8330Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp, *default_gp;
8340Sstevel@tonic-gate 		rctl_set_t *init_set;
8350Sstevel@tonic-gate 		task_t *tk, *tk_old;
83611173SJonathan.Adams@Sun.COM 		klwp_t *lwp;
8370Sstevel@tonic-gate 
83811173SJonathan.Adams@Sun.COM 		if (getproc(&p, pid, GETPROC_USER) < 0)
8390Sstevel@tonic-gate 			return (EAGAIN);
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * init creates a new task, distinct from the task
8420Sstevel@tonic-gate 		 * containing kernel "processes".
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		tk = task_create(0, p->p_zone);
8450Sstevel@tonic-gate 		mutex_enter(&tk->tk_zone->zone_nlwps_lock);
8460Sstevel@tonic-gate 		tk->tk_proj->kpj_ntasks++;
84712725SMenno.Lageman@Sun.COM 		tk->tk_nprocs++;
8480Sstevel@tonic-gate 		mutex_exit(&tk->tk_zone->zone_nlwps_lock);
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 		default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS);
8510Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
8520Sstevel@tonic-gate 		init_set = rctl_set_create();
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		mutex_enter(&pidlock);
8550Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8560Sstevel@tonic-gate 		tk_old = p->p_task;	/* switch to new task */
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		task_detach(p);
8590Sstevel@tonic-gate 		task_begin(tk, p);
8600Sstevel@tonic-gate 		mutex_exit(&pidlock);
8610Sstevel@tonic-gate 
86212725SMenno.Lageman@Sun.COM 		mutex_enter(&tk_old->tk_zone->zone_nlwps_lock);
86312725SMenno.Lageman@Sun.COM 		tk_old->tk_nprocs--;
86412725SMenno.Lageman@Sun.COM 		mutex_exit(&tk_old->tk_zone->zone_nlwps_lock);
86512725SMenno.Lageman@Sun.COM 
8660Sstevel@tonic-gate 		e.rcep_p.proc = p;
8670Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
8680Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
8690Sstevel@tonic-gate 		    init_gp);
8700Sstevel@tonic-gate 		rctlproc_default_init(p, default_gp);
8710Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		task_rele(tk_old);
8740Sstevel@tonic-gate 		rctl_prealloc_destroy(default_gp);
8750Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
8760Sstevel@tonic-gate 
87711173SJonathan.Adams@Sun.COM 		if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri,
87811173SJonathan.Adams@Sun.COM 		    &curthread->t_hold, cid, 1)) == NULL) {
87911173SJonathan.Adams@Sun.COM 			task_t *tk;
88011173SJonathan.Adams@Sun.COM 			fork_fail(p);
88111173SJonathan.Adams@Sun.COM 			mutex_enter(&pidlock);
88211173SJonathan.Adams@Sun.COM 			mutex_enter(&p->p_lock);
88311173SJonathan.Adams@Sun.COM 			tk = p->p_task;
88411173SJonathan.Adams@Sun.COM 			task_detach(p);
88511173SJonathan.Adams@Sun.COM 			ASSERT(p->p_pool->pool_ref > 0);
88611173SJonathan.Adams@Sun.COM 			atomic_add_32(&p->p_pool->pool_ref, -1);
88711173SJonathan.Adams@Sun.COM 			mutex_exit(&p->p_lock);
88812834SMenno.Lageman@Sun.COM 			pid_exit(p, tk);
88911173SJonathan.Adams@Sun.COM 			mutex_exit(&pidlock);
89011173SJonathan.Adams@Sun.COM 			task_rele(tk);
8910Sstevel@tonic-gate 
89211173SJonathan.Adams@Sun.COM 			return (EAGAIN);
89311173SJonathan.Adams@Sun.COM 		}
89411173SJonathan.Adams@Sun.COM 		t = lwptot(lwp);
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		ctp = contract_process_fork(sys_process_tmpl, p, curproc,
8970Sstevel@tonic-gate 		    B_FALSE);
8980Sstevel@tonic-gate 		ASSERT(ctp != NULL);
8990Sstevel@tonic-gate 		if (ct != NULL)
9000Sstevel@tonic-gate 			*ct = &ctp->conp_contract;
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 
90311173SJonathan.Adams@Sun.COM 	ASSERT3U(t->t_tid, ==, 1);
9040Sstevel@tonic-gate 	p->p_lwpid = 1;
9050Sstevel@tonic-gate 	mutex_enter(&pidlock);
90611173SJonathan.Adams@Sun.COM 	pgjoin(p, p->p_parent->p_pgidp);
9070Sstevel@tonic-gate 	p->p_stat = SRUN;
9080Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
90911173SJonathan.Adams@Sun.COM 	t->t_proc_flag &= ~TP_HOLDLWP;
91011173SJonathan.Adams@Sun.COM 	lwp_create_done(t);
9110Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9120Sstevel@tonic-gate 	mutex_exit(&pidlock);
9130Sstevel@tonic-gate 	return (0);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate /*
9170Sstevel@tonic-gate  * create a child proc struct.
9180Sstevel@tonic-gate  */
9190Sstevel@tonic-gate static int
getproc(proc_t ** cpp,pid_t pid,uint_t flags)92011173SJonathan.Adams@Sun.COM getproc(proc_t **cpp, pid_t pid, uint_t flags)
9210Sstevel@tonic-gate {
9220Sstevel@tonic-gate 	proc_t		*pp, *cp;
9230Sstevel@tonic-gate 	pid_t		newpid;
9240Sstevel@tonic-gate 	struct user	*uarea;
9250Sstevel@tonic-gate 	extern uint_t	nproc;
9260Sstevel@tonic-gate 	struct cred	*cr;
9270Sstevel@tonic-gate 	uid_t		ruid;
9280Sstevel@tonic-gate 	zoneid_t	zoneid;
92912725SMenno.Lageman@Sun.COM 	task_t		*task;
93012725SMenno.Lageman@Sun.COM 	kproject_t	*proj;
93112725SMenno.Lageman@Sun.COM 	zone_t		*zone;
93212725SMenno.Lageman@Sun.COM 	int		rctlfail = 0;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (!page_mem_avail(tune.t_minarmem))
9350Sstevel@tonic-gate 		return (-1);
9360Sstevel@tonic-gate 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
9370Sstevel@tonic-gate 		return (-1);	/* no point in starting new processes */
9380Sstevel@tonic-gate 
93911173SJonathan.Adams@Sun.COM 	pp = (flags & GETPROC_KERNEL) ? &p0 : curproc;
94012725SMenno.Lageman@Sun.COM 	task = pp->p_task;
94112725SMenno.Lageman@Sun.COM 	proj = task->tk_proj;
94212725SMenno.Lageman@Sun.COM 	zone = pp->p_zone;
94312725SMenno.Lageman@Sun.COM 
94412725SMenno.Lageman@Sun.COM 	mutex_enter(&pp->p_lock);
94512725SMenno.Lageman@Sun.COM 	mutex_enter(&zone->zone_nlwps_lock);
94612725SMenno.Lageman@Sun.COM 	if (proj != proj0p) {
94712725SMenno.Lageman@Sun.COM 		if (task->tk_nprocs >= task->tk_nprocs_ctl)
94812725SMenno.Lageman@Sun.COM 			if (rctl_test(rc_task_nprocs, task->tk_rctls,
94912725SMenno.Lageman@Sun.COM 			    pp, 1, 0) & RCT_DENY)
95012725SMenno.Lageman@Sun.COM 				rctlfail = 1;
95112725SMenno.Lageman@Sun.COM 
95212725SMenno.Lageman@Sun.COM 		if (proj->kpj_nprocs >= proj->kpj_nprocs_ctl)
95312725SMenno.Lageman@Sun.COM 			if (rctl_test(rc_project_nprocs, proj->kpj_rctls,
95412725SMenno.Lageman@Sun.COM 			    pp, 1, 0) & RCT_DENY)
95512725SMenno.Lageman@Sun.COM 				rctlfail = 1;
95612725SMenno.Lageman@Sun.COM 
95712725SMenno.Lageman@Sun.COM 		if (zone->zone_nprocs >= zone->zone_nprocs_ctl)
95812725SMenno.Lageman@Sun.COM 			if (rctl_test(rc_zone_nprocs, zone->zone_rctls,
95912725SMenno.Lageman@Sun.COM 			    pp, 1, 0) & RCT_DENY)
96012725SMenno.Lageman@Sun.COM 				rctlfail = 1;
96112725SMenno.Lageman@Sun.COM 
96212725SMenno.Lageman@Sun.COM 		if (rctlfail) {
96312725SMenno.Lageman@Sun.COM 			mutex_exit(&zone->zone_nlwps_lock);
96412725SMenno.Lageman@Sun.COM 			mutex_exit(&pp->p_lock);
96512725SMenno.Lageman@Sun.COM 			goto punish;
96612725SMenno.Lageman@Sun.COM 		}
96712725SMenno.Lageman@Sun.COM 	}
96812725SMenno.Lageman@Sun.COM 	task->tk_nprocs++;
96912725SMenno.Lageman@Sun.COM 	proj->kpj_nprocs++;
97012725SMenno.Lageman@Sun.COM 	zone->zone_nprocs++;
97112725SMenno.Lageman@Sun.COM 	mutex_exit(&zone->zone_nlwps_lock);
97212725SMenno.Lageman@Sun.COM 	mutex_exit(&pp->p_lock);
97312725SMenno.Lageman@Sun.COM 
9740Sstevel@tonic-gate 	cp = kmem_cache_alloc(process_cache, KM_SLEEP);
9750Sstevel@tonic-gate 	bzero(cp, sizeof (proc_t));
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/*
9780Sstevel@tonic-gate 	 * Make proc entry for child process
9790Sstevel@tonic-gate 	 */
9802712Snn35248 	mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL);
9810Sstevel@tonic-gate 	mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL);
9820Sstevel@tonic-gate 	mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL);
9830Sstevel@tonic-gate #if defined(__x86)
9840Sstevel@tonic-gate 	mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL);
9850Sstevel@tonic-gate #endif
9860Sstevel@tonic-gate 	mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL);
9870Sstevel@tonic-gate 	cp->p_stat = SIDL;
9880Sstevel@tonic-gate 	cp->p_mstart = gethrtime();
98911173SJonathan.Adams@Sun.COM 	cp->p_as = &kas;
9902899Sgjelinek 	/*
9912899Sgjelinek 	 * p_zone must be set before we call pid_allocate since the process
9922899Sgjelinek 	 * will be visible after that and code such as prfind_zone will
9932899Sgjelinek 	 * look at the p_zone field.
9942899Sgjelinek 	 */
9952899Sgjelinek 	cp->p_zone = pp->p_zone;
9964426Saguzovsk 	cp->p_t1_lgrpid = LGRP_NONE;
9974426Saguzovsk 	cp->p_tr_lgrpid = LGRP_NONE;
9980Sstevel@tonic-gate 
99911173SJonathan.Adams@Sun.COM 	if ((newpid = pid_allocate(cp, pid, PID_ALLOC_PROC)) == -1) {
10000Sstevel@tonic-gate 		if (nproc == v.v_proc) {
10010Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, procovf, 1);
10020Sstevel@tonic-gate 			cmn_err(CE_WARN, "out of processes");
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 		goto bad;
10050Sstevel@tonic-gate 	}
10060Sstevel@tonic-gate 
100711736SDonghai.Qiao@Sun.COM 	mutex_enter(&pp->p_lock);
100811736SDonghai.Qiao@Sun.COM 	cp->p_exec = pp->p_exec;
100911736SDonghai.Qiao@Sun.COM 	cp->p_execdir = pp->p_execdir;
101011736SDonghai.Qiao@Sun.COM 	mutex_exit(&pp->p_lock);
101111736SDonghai.Qiao@Sun.COM 
101211736SDonghai.Qiao@Sun.COM 	if (cp->p_exec) {
101311736SDonghai.Qiao@Sun.COM 		VN_HOLD(cp->p_exec);
101411736SDonghai.Qiao@Sun.COM 		/*
101511736SDonghai.Qiao@Sun.COM 		 * Each VOP_OPEN() must be paired with a corresponding
101611736SDonghai.Qiao@Sun.COM 		 * VOP_CLOSE(). In this case, the executable will be
101711736SDonghai.Qiao@Sun.COM 		 * closed for the child in either proc_exit() or gexec().
101811736SDonghai.Qiao@Sun.COM 		 */
101911736SDonghai.Qiao@Sun.COM 		if (VOP_OPEN(&cp->p_exec, FREAD, CRED(), NULL) != 0) {
102011736SDonghai.Qiao@Sun.COM 			VN_RELE(cp->p_exec);
102111736SDonghai.Qiao@Sun.COM 			cp->p_exec = NULLVP;
102211736SDonghai.Qiao@Sun.COM 			cp->p_execdir = NULLVP;
102311736SDonghai.Qiao@Sun.COM 			goto bad;
102411736SDonghai.Qiao@Sun.COM 		}
102511736SDonghai.Qiao@Sun.COM 	}
102611736SDonghai.Qiao@Sun.COM 	if (cp->p_execdir)
102711736SDonghai.Qiao@Sun.COM 		VN_HOLD(cp->p_execdir);
102811736SDonghai.Qiao@Sun.COM 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	 * If not privileged make sure that this user hasn't exceeded
10310Sstevel@tonic-gate 	 * v.v_maxup processes, and that users collectively haven't
10320Sstevel@tonic-gate 	 * exceeded v.v_maxupttl processes.
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	mutex_enter(&pidlock);
10350Sstevel@tonic-gate 	ASSERT(nproc < v.v_proc);	/* otherwise how'd we get our pid? */
10360Sstevel@tonic-gate 	cr = CRED();
10370Sstevel@tonic-gate 	ruid = crgetruid(cr);
10380Sstevel@tonic-gate 	zoneid = crgetzoneid(cr);
10390Sstevel@tonic-gate 	if (nproc >= v.v_maxup && 	/* short-circuit; usually false */
10400Sstevel@tonic-gate 	    (nproc >= v.v_maxupttl ||
10410Sstevel@tonic-gate 	    upcount_get(ruid, zoneid) >= v.v_maxup) &&
10420Sstevel@tonic-gate 	    secpolicy_newproc(cr) != 0) {
10430Sstevel@tonic-gate 		mutex_exit(&pidlock);
10440Sstevel@tonic-gate 		zcmn_err(zoneid, CE_NOTE,
10450Sstevel@tonic-gate 		    "out of per-user processes for uid %d", ruid);
10460Sstevel@tonic-gate 		goto bad;
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/*
10500Sstevel@tonic-gate 	 * Everything is cool, put the new proc on the active process list.
10510Sstevel@tonic-gate 	 * It is already on the pid list and in /proc.
10520Sstevel@tonic-gate 	 * Increment the per uid process count (upcount).
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	nproc++;
10550Sstevel@tonic-gate 	upcount_inc(ruid, zoneid);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	cp->p_next = practive;
10580Sstevel@tonic-gate 	practive->p_prev = cp;
10590Sstevel@tonic-gate 	practive = cp;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	cp->p_ignore = pp->p_ignore;
10620Sstevel@tonic-gate 	cp->p_siginfo = pp->p_siginfo;
10630Sstevel@tonic-gate 	cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD);
10640Sstevel@tonic-gate 	cp->p_sessp = pp->p_sessp;
10652712Snn35248 	sess_hold(pp);
10662712Snn35248 	cp->p_brand = pp->p_brand;
10672712Snn35248 	if (PROC_IS_BRANDED(pp))
10682712Snn35248 		BROP(pp)->b_copy_procdata(cp, pp);
10690Sstevel@tonic-gate 	cp->p_bssbase = pp->p_bssbase;
10700Sstevel@tonic-gate 	cp->p_brkbase = pp->p_brkbase;
10710Sstevel@tonic-gate 	cp->p_brksize = pp->p_brksize;
10720Sstevel@tonic-gate 	cp->p_brkpageszc = pp->p_brkpageszc;
10730Sstevel@tonic-gate 	cp->p_stksize = pp->p_stksize;
10740Sstevel@tonic-gate 	cp->p_stkpageszc = pp->p_stkpageszc;
10750Sstevel@tonic-gate 	cp->p_stkprot = pp->p_stkprot;
10760Sstevel@tonic-gate 	cp->p_datprot = pp->p_datprot;
10770Sstevel@tonic-gate 	cp->p_usrstack = pp->p_usrstack;
10780Sstevel@tonic-gate 	cp->p_model = pp->p_model;
10790Sstevel@tonic-gate 	cp->p_ppid = pp->p_pid;
10800Sstevel@tonic-gate 	cp->p_ancpid = pp->p_pid;
10810Sstevel@tonic-gate 	cp->p_portcnt = pp->p_portcnt;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	/*
10840Sstevel@tonic-gate 	 * Initialize watchpoint structures
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area),
10870Sstevel@tonic-gate 	    offsetof(struct watched_area, wa_link));
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * Initialize immediate resource control values.
10910Sstevel@tonic-gate 	 */
10920Sstevel@tonic-gate 	cp->p_stk_ctl = pp->p_stk_ctl;
10930Sstevel@tonic-gate 	cp->p_fsz_ctl = pp->p_fsz_ctl;
10940Sstevel@tonic-gate 	cp->p_vmem_ctl = pp->p_vmem_ctl;
10950Sstevel@tonic-gate 	cp->p_fno_ctl = pp->p_fno_ctl;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * Link up to parent-child-sibling chain.  No need to lock
10990Sstevel@tonic-gate 	 * in general since only a call to freeproc() (done by the
11000Sstevel@tonic-gate 	 * same parent as newproc()) diddles with the child chain.
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	cp->p_sibling = pp->p_child;
11030Sstevel@tonic-gate 	if (pp->p_child)
11040Sstevel@tonic-gate 		pp->p_child->p_psibling = cp;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	cp->p_parent = pp;
11070Sstevel@tonic-gate 	pp->p_child = cp;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	cp->p_child_ns = NULL;
11100Sstevel@tonic-gate 	cp->p_sibling_ns = NULL;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	cp->p_nextorph = pp->p_orphan;
11130Sstevel@tonic-gate 	cp->p_nextofkin = pp;
11140Sstevel@tonic-gate 	pp->p_orphan = cp;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Inherit profiling state; do not inherit REALPROF profiling state.
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	cp->p_prof = pp->p_prof;
11200Sstevel@tonic-gate 	cp->p_rprof_cyclic = CYCLIC_NONE;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	/*
11230Sstevel@tonic-gate 	 * Inherit pool pointer from the parent.  Kernel processes are
11240Sstevel@tonic-gate 	 * always bound to the default pool.
11250Sstevel@tonic-gate 	 */
11260Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
112711173SJonathan.Adams@Sun.COM 	if (flags & GETPROC_KERNEL) {
11280Sstevel@tonic-gate 		cp->p_pool = pool_default;
11290Sstevel@tonic-gate 		cp->p_flag |= SSYS;
11300Sstevel@tonic-gate 	} else {
11310Sstevel@tonic-gate 		cp->p_pool = pp->p_pool;
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, 1);
11340Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/*
11370Sstevel@tonic-gate 	 * Add the child process to the current task.  Kernel processes
11380Sstevel@tonic-gate 	 * are always attached to task0.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
114111173SJonathan.Adams@Sun.COM 	if (flags & GETPROC_KERNEL)
11420Sstevel@tonic-gate 		task_attach(task0p, cp);
11430Sstevel@tonic-gate 	else
11440Sstevel@tonic-gate 		task_attach(pp->p_task, cp);
11450Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
11460Sstevel@tonic-gate 	mutex_exit(&pidlock);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t),
11490Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * Duplicate any audit information kept in the process table
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	if (audit_active)	/* copy audit data to cp */
11550Sstevel@tonic-gate 		audit_newproc(cp);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	crhold(cp->p_cred = cr);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/*
11600Sstevel@tonic-gate 	 * Bump up the counts on the file structures pointed at by the
11610Sstevel@tonic-gate 	 * parent's file table since the child will point at them too.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	fcnt_add(P_FINFO(pp), 1);
11640Sstevel@tonic-gate 
116511173SJonathan.Adams@Sun.COM 	if (PTOU(pp)->u_cdir) {
116611173SJonathan.Adams@Sun.COM 		VN_HOLD(PTOU(pp)->u_cdir);
116711173SJonathan.Adams@Sun.COM 	} else {
116811173SJonathan.Adams@Sun.COM 		ASSERT(pp == &p0);
116911173SJonathan.Adams@Sun.COM 		/*
117011173SJonathan.Adams@Sun.COM 		 * We must be at or before vfs_mountroot(); it will take care of
117111173SJonathan.Adams@Sun.COM 		 * assigning our current directory.
117211173SJonathan.Adams@Sun.COM 		 */
117311173SJonathan.Adams@Sun.COM 	}
11743446Smrj 	if (PTOU(pp)->u_rdir)
11753446Smrj 		VN_HOLD(PTOU(pp)->u_rdir);
11763446Smrj 	if (PTOU(pp)->u_cwd)
11773446Smrj 		refstr_hold(PTOU(pp)->u_cwd);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/*
11800Sstevel@tonic-gate 	 * copy the parent's uarea.
11810Sstevel@tonic-gate 	 */
11820Sstevel@tonic-gate 	uarea = PTOU(cp);
11833446Smrj 	bcopy(PTOU(pp), uarea, sizeof (*uarea));
11840Sstevel@tonic-gate 	flist_fork(P_FINFO(pp), P_FINFO(cp));
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 	gethrestime(&uarea->u_start);
118711066Srafael.vanoni@sun.com 	uarea->u_ticks = ddi_get_lbolt();
11880Sstevel@tonic-gate 	uarea->u_mem = rm_asrss(pp->p_as);
11890Sstevel@tonic-gate 	uarea->u_acflag = AFORK;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * If inherit-on-fork, copy /proc tracing flags to child.
11930Sstevel@tonic-gate 	 */
11940Sstevel@tonic-gate 	if ((pp->p_proc_flag & P_PR_FORK) != 0) {
11950Sstevel@tonic-gate 		cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK);
11960Sstevel@tonic-gate 		cp->p_sigmask = pp->p_sigmask;
11970Sstevel@tonic-gate 		cp->p_fltmask = pp->p_fltmask;
11980Sstevel@tonic-gate 	} else {
11990Sstevel@tonic-gate 		sigemptyset(&cp->p_sigmask);
12000Sstevel@tonic-gate 		premptyset(&cp->p_fltmask);
12010Sstevel@tonic-gate 		uarea->u_systrap = 0;
12020Sstevel@tonic-gate 		premptyset(&uarea->u_entrymask);
12030Sstevel@tonic-gate 		premptyset(&uarea->u_exitmask);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	/*
12060Sstevel@tonic-gate 	 * If microstate accounting is being inherited, mark child
12070Sstevel@tonic-gate 	 */
12080Sstevel@tonic-gate 	if ((pp->p_flag & SMSFORK) != 0)
12090Sstevel@tonic-gate 		cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	/*
12120Sstevel@tonic-gate 	 * Inherit fixalignment flag from the parent
12130Sstevel@tonic-gate 	 */
12140Sstevel@tonic-gate 	cp->p_fixalignment = pp->p_fixalignment;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	*cpp = cp;
12170Sstevel@tonic-gate 	return (0);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate bad:
12200Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&pidlock));
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	mutex_destroy(&cp->p_crlock);
12230Sstevel@tonic-gate 	mutex_destroy(&cp->p_pflock);
12240Sstevel@tonic-gate #if defined(__x86)
12250Sstevel@tonic-gate 	mutex_destroy(&cp->p_ldtlock);
12260Sstevel@tonic-gate #endif
12270Sstevel@tonic-gate 	if (newpid != -1) {
12280Sstevel@tonic-gate 		proc_entry_free(cp->p_pidp);
12290Sstevel@tonic-gate 		(void) pid_rele(cp->p_pidp);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	kmem_cache_free(process_cache, cp);
12320Sstevel@tonic-gate 
123312725SMenno.Lageman@Sun.COM 	mutex_enter(&zone->zone_nlwps_lock);
123412725SMenno.Lageman@Sun.COM 	task->tk_nprocs--;
123512725SMenno.Lageman@Sun.COM 	proj->kpj_nprocs--;
123612725SMenno.Lageman@Sun.COM 	zone->zone_nprocs--;
123712725SMenno.Lageman@Sun.COM 	mutex_exit(&zone->zone_nlwps_lock);
123812725SMenno.Lageman@Sun.COM 
123912725SMenno.Lageman@Sun.COM punish:
12400Sstevel@tonic-gate 	/*
12410Sstevel@tonic-gate 	 * We most likely got into this situation because some process is
12420Sstevel@tonic-gate 	 * forking out of control.  As punishment, put it to sleep for a
12430Sstevel@tonic-gate 	 * bit so it can't eat the machine alive.  Sleep interval is chosen
12440Sstevel@tonic-gate 	 * to allow no more than one fork failure per cpu per clock tick
12450Sstevel@tonic-gate 	 * on average (yes, I just made this up).  This has two desirable
12460Sstevel@tonic-gate 	 * properties: (1) it sets a constant limit on the fork failure
12470Sstevel@tonic-gate 	 * rate, and (2) the busier the system is, the harsher the penalty
12480Sstevel@tonic-gate 	 * for abusing it becomes.
12490Sstevel@tonic-gate 	 */
12500Sstevel@tonic-gate 	INCR_COUNT(&fork_fail_pending, &pidlock);
12510Sstevel@tonic-gate 	delay(fork_fail_pending / ncpus + 1);
12520Sstevel@tonic-gate 	DECR_COUNT(&fork_fail_pending, &pidlock);
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	return (-1); /* out of memory or proc slots */
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate /*
12580Sstevel@tonic-gate  * Release virtual memory.
12590Sstevel@tonic-gate  * In the case of vfork(), the child was given exclusive access to its
12600Sstevel@tonic-gate  * parent's address space.  The parent is waiting in vfwait() for the
12610Sstevel@tonic-gate  * child to release its exclusive claim via relvm().
12620Sstevel@tonic-gate  */
12630Sstevel@tonic-gate void
relvm()12640Sstevel@tonic-gate relvm()
12650Sstevel@tonic-gate {
12660Sstevel@tonic-gate 	proc_t *p = curproc;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	ASSERT((unsigned)p->p_lwpcnt <= 1);
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	prrelvm();	/* inform /proc */
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	if (p->p_flag & SVFORK) {
12730Sstevel@tonic-gate 		proc_t *pp = p->p_parent;
12740Sstevel@tonic-gate 		/*
12750Sstevel@tonic-gate 		 * The child process is either exec'ing or exit'ing.
12760Sstevel@tonic-gate 		 * The child is now separated from the parent's address
12770Sstevel@tonic-gate 		 * space.  The parent process is made dispatchable.
12780Sstevel@tonic-gate 		 *
12790Sstevel@tonic-gate 		 * This is a delicate locking maneuver, involving
12800Sstevel@tonic-gate 		 * both the parent's p_lock and the child's p_lock.
12810Sstevel@tonic-gate 		 * As soon as the SVFORK flag is turned off, the
12820Sstevel@tonic-gate 		 * parent is free to run, but it must not run until
12830Sstevel@tonic-gate 		 * we wake it up using its p_cv because it might
12840Sstevel@tonic-gate 		 * exit and we would be referencing invalid memory.
12850Sstevel@tonic-gate 		 * Therefore, we hold the parent with its p_lock
12860Sstevel@tonic-gate 		 * while protecting our p_flags with our own p_lock.
12870Sstevel@tonic-gate 		 */
12880Sstevel@tonic-gate try_again:
12890Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* grab child's lock first */
12900Sstevel@tonic-gate 		prbarrier(p);		/* make sure /proc is blocked out */
12910Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 		/*
12940Sstevel@tonic-gate 		 * Check if parent is locked by /proc.
12950Sstevel@tonic-gate 		 */
12960Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_LOCK) {
12970Sstevel@tonic-gate 			/*
12980Sstevel@tonic-gate 			 * Delay until /proc is done with the parent.
12990Sstevel@tonic-gate 			 * We must drop our (the child's) p->p_lock, wait
13000Sstevel@tonic-gate 			 * via prbarrier() on the parent, then start over.
13010Sstevel@tonic-gate 			 */
13020Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
13030Sstevel@tonic-gate 			prbarrier(pp);
13040Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
13050Sstevel@tonic-gate 			goto try_again;
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 		p->p_flag &= ~SVFORK;
13080Sstevel@tonic-gate 		kpreempt_disable();
13090Sstevel@tonic-gate 		p->p_as = &kas;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 		/*
13120Sstevel@tonic-gate 		 * notify hat of change in thread's address space
13130Sstevel@tonic-gate 		 */
13140Sstevel@tonic-gate 		hat_thread_exit(curthread);
13150Sstevel@tonic-gate 		kpreempt_enable();
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 		/*
13180Sstevel@tonic-gate 		 * child sizes are copied back to parent because
13190Sstevel@tonic-gate 		 * child may have grown.
13200Sstevel@tonic-gate 		 */
13210Sstevel@tonic-gate 		pp->p_brkbase = p->p_brkbase;
13220Sstevel@tonic-gate 		pp->p_brksize = p->p_brksize;
13230Sstevel@tonic-gate 		pp->p_stksize = p->p_stksize;
132411661SVamsi.Krishna@Sun.COM 
132511661SVamsi.Krishna@Sun.COM 		/*
132611661SVamsi.Krishna@Sun.COM 		 * Copy back the shm accounting information
132711661SVamsi.Krishna@Sun.COM 		 * to the parent process.
132811661SVamsi.Krishna@Sun.COM 		 */
132911661SVamsi.Krishna@Sun.COM 		pp->p_segacct = p->p_segacct;
133011661SVamsi.Krishna@Sun.COM 		p->p_segacct = NULL;
133111661SVamsi.Krishna@Sun.COM 
13320Sstevel@tonic-gate 		/*
13330Sstevel@tonic-gate 		 * The parent is no longer waiting for the vfork()d child.
13340Sstevel@tonic-gate 		 * Restore the parent's watched pages, if any.  This is
13350Sstevel@tonic-gate 		 * safe because we know the parent is not locked by /proc
13360Sstevel@tonic-gate 		 */
13370Sstevel@tonic-gate 		pp->p_flag &= ~SVFWAIT;
13380Sstevel@tonic-gate 		if (avl_numnodes(&pp->p_wpage) != 0) {
13390Sstevel@tonic-gate 			pp->p_as->a_wpage = pp->p_wpage;
13400Sstevel@tonic-gate 			avl_create(&pp->p_wpage, wp_compare,
13410Sstevel@tonic-gate 			    sizeof (struct watched_page),
13420Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 		cv_signal(&pp->p_cv);
13450Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
13460Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13470Sstevel@tonic-gate 	} else {
13480Sstevel@tonic-gate 		if (p->p_as != &kas) {
13490Sstevel@tonic-gate 			struct as *as;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 			if (p->p_segacct)
13520Sstevel@tonic-gate 				shmexit(p);
13532712Snn35248 
13540Sstevel@tonic-gate 			/*
13550Sstevel@tonic-gate 			 * We grab p_lock for the benefit of /proc
13560Sstevel@tonic-gate 			 */
13570Sstevel@tonic-gate 			kpreempt_disable();
13580Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
13590Sstevel@tonic-gate 			prbarrier(p);	/* make sure /proc is blocked out */
13600Sstevel@tonic-gate 			as = p->p_as;
13610Sstevel@tonic-gate 			p->p_as = &kas;
13620Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 			/*
13650Sstevel@tonic-gate 			 * notify hat of change in thread's address space
13660Sstevel@tonic-gate 			 */
13670Sstevel@tonic-gate 			hat_thread_exit(curthread);
13680Sstevel@tonic-gate 			kpreempt_enable();
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 			as_free(as);
13714426Saguzovsk 			p->p_tr_lgrpid = LGRP_NONE;
13720Sstevel@tonic-gate 		}
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate  * Wait for child to exec or exit.
13780Sstevel@tonic-gate  * Called by parent of vfork'ed process.
13790Sstevel@tonic-gate  * See important comments in relvm(), above.
13800Sstevel@tonic-gate  */
13810Sstevel@tonic-gate void
vfwait(pid_t pid)13820Sstevel@tonic-gate vfwait(pid_t pid)
13830Sstevel@tonic-gate {
13840Sstevel@tonic-gate 	int signalled = 0;
13850Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
13860Sstevel@tonic-gate 	proc_t *cp;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	/*
13890Sstevel@tonic-gate 	 * Wait for child to exec or exit.
13900Sstevel@tonic-gate 	 */
13910Sstevel@tonic-gate 	for (;;) {
13920Sstevel@tonic-gate 		mutex_enter(&pidlock);
13930Sstevel@tonic-gate 		cp = prfind(pid);
13940Sstevel@tonic-gate 		if (cp == NULL || cp->p_parent != pp) {
13950Sstevel@tonic-gate 			/*
13960Sstevel@tonic-gate 			 * Child has exit()ed.
13970Sstevel@tonic-gate 			 */
13980Sstevel@tonic-gate 			mutex_exit(&pidlock);
13990Sstevel@tonic-gate 			break;
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 		/*
14020Sstevel@tonic-gate 		 * Grab the child's p_lock before releasing pidlock.
14030Sstevel@tonic-gate 		 * Otherwise, the child could exit and we would be
14040Sstevel@tonic-gate 		 * referencing invalid memory.
14050Sstevel@tonic-gate 		 */
14060Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
14070Sstevel@tonic-gate 		mutex_exit(&pidlock);
14080Sstevel@tonic-gate 		if (!(cp->p_flag & SVFORK)) {
14090Sstevel@tonic-gate 			/*
14100Sstevel@tonic-gate 			 * Child has exec()ed or is exit()ing.
14110Sstevel@tonic-gate 			 */
14120Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
14130Sstevel@tonic-gate 			break;
14140Sstevel@tonic-gate 		}
14150Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
14160Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
14170Sstevel@tonic-gate 		/*
14180Sstevel@tonic-gate 		 * We might be waked up spuriously from the cv_wait().
14190Sstevel@tonic-gate 		 * We have to do the whole operation over again to be
14200Sstevel@tonic-gate 		 * sure the child's SVFORK flag really is turned off.
14210Sstevel@tonic-gate 		 * We cannot make reference to the child because it can
14220Sstevel@tonic-gate 		 * exit before we return and we would be referencing
14230Sstevel@tonic-gate 		 * invalid memory.
14240Sstevel@tonic-gate 		 *
14250Sstevel@tonic-gate 		 * Because this is potentially a very long-term wait,
14260Sstevel@tonic-gate 		 * we call cv_wait_sig() (for its jobcontrol and /proc
14270Sstevel@tonic-gate 		 * side-effects) unless there is a current signal, in
14280Sstevel@tonic-gate 		 * which case we use cv_wait() because we cannot return
14290Sstevel@tonic-gate 		 * from this function until the child has released the
14300Sstevel@tonic-gate 		 * address space.  Calling cv_wait_sig() with a current
14310Sstevel@tonic-gate 		 * signal would lead to an indefinite loop here because
14320Sstevel@tonic-gate 		 * cv_wait_sig() returns immediately in this case.
14330Sstevel@tonic-gate 		 */
14340Sstevel@tonic-gate 		if (signalled)
14350Sstevel@tonic-gate 			cv_wait(&pp->p_cv, &pp->p_lock);
14360Sstevel@tonic-gate 		else
14370Sstevel@tonic-gate 			signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock);
14380Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
14390Sstevel@tonic-gate 	}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	/* restore watchpoints to parent */
14420Sstevel@tonic-gate 	if (pr_watch_active(pp)) {
14430Sstevel@tonic-gate 		struct as *as = pp->p_as;
14440Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
14450Sstevel@tonic-gate 		as_setwatch(as);
14460Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
14470Sstevel@tonic-gate 	}
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
14500Sstevel@tonic-gate 	prbarrier(pp);	/* barrier against /proc locking */
14510Sstevel@tonic-gate 	continuelwps(pp);
14520Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
14530Sstevel@tonic-gate }
1454