xref: /onnv-gate/usr/src/uts/common/os/fork.c (revision 2241:592fbc504a44)
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 /*
231217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/param.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/signal.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/policy.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/systm.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/vfs.h>
430Sstevel@tonic-gate #include <sys/vnode.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/errno.h>
460Sstevel@tonic-gate #include <sys/time.h>
470Sstevel@tonic-gate #include <sys/proc.h>
480Sstevel@tonic-gate #include <sys/cmn_err.h>
490Sstevel@tonic-gate #include <sys/acct.h>
500Sstevel@tonic-gate #include <sys/tuneable.h>
510Sstevel@tonic-gate #include <sys/class.h>
520Sstevel@tonic-gate #include <sys/kmem.h>
530Sstevel@tonic-gate #include <sys/session.h>
540Sstevel@tonic-gate #include <sys/ucontext.h>
550Sstevel@tonic-gate #include <sys/stack.h>
560Sstevel@tonic-gate #include <sys/procfs.h>
570Sstevel@tonic-gate #include <sys/prsystm.h>
580Sstevel@tonic-gate #include <sys/vmsystm.h>
590Sstevel@tonic-gate #include <sys/vtrace.h>
600Sstevel@tonic-gate #include <sys/debug.h>
610Sstevel@tonic-gate #include <sys/shm_impl.h>
620Sstevel@tonic-gate #include <sys/door_data.h>
630Sstevel@tonic-gate #include <vm/as.h>
640Sstevel@tonic-gate #include <vm/rm.h>
650Sstevel@tonic-gate #include <c2/audit.h>
660Sstevel@tonic-gate #include <sys/var.h>
670Sstevel@tonic-gate #include <sys/schedctl.h>
680Sstevel@tonic-gate #include <sys/utrap.h>
690Sstevel@tonic-gate #include <sys/task.h>
700Sstevel@tonic-gate #include <sys/resource.h>
710Sstevel@tonic-gate #include <sys/cyclic.h>
720Sstevel@tonic-gate #include <sys/lgrp.h>
730Sstevel@tonic-gate #include <sys/rctl.h>
740Sstevel@tonic-gate #include <sys/contract_impl.h>
750Sstevel@tonic-gate #include <sys/contract/process_impl.h>
760Sstevel@tonic-gate #include <sys/list.h>
770Sstevel@tonic-gate #include <sys/dtrace.h>
780Sstevel@tonic-gate #include <sys/pool.h>
790Sstevel@tonic-gate #include <sys/zone.h>
800Sstevel@tonic-gate #include <sys/sdt.h>
810Sstevel@tonic-gate #include <sys/class.h>
820Sstevel@tonic-gate #include <sys/corectl.h>
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int64_t cfork(int, int);
850Sstevel@tonic-gate static int getproc(proc_t **, int);
860Sstevel@tonic-gate static void fork_fail(proc_t *);
870Sstevel@tonic-gate static void forklwp_fail(proc_t *);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate int fork_fail_pending;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate extern struct kmem_cache *process_cache;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * forkall system call.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate int64_t
970Sstevel@tonic-gate forkall(void)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	return (cfork(0, 0));
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate  * The parent is stopped until the child invokes relvm().
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate int64_t
1060Sstevel@tonic-gate vfork(void)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	curthread->t_post_sys = 1;	/* so vfwait() will be called */
1090Sstevel@tonic-gate 	return (cfork(1, 1));
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * fork1 system call
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate int64_t
1160Sstevel@tonic-gate fork1(void)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	return (cfork(0, 1));
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /* ARGSUSED */
1220Sstevel@tonic-gate static int64_t
1230Sstevel@tonic-gate cfork(int isvfork, int isfork1)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
1260Sstevel@tonic-gate 	struct as *as;
1270Sstevel@tonic-gate 	proc_t *cp, **orphpp;
1280Sstevel@tonic-gate 	klwp_t *clone;
1290Sstevel@tonic-gate 	kthread_t *t;
1300Sstevel@tonic-gate 	task_t *tk;
1310Sstevel@tonic-gate 	rval_t	r;
1320Sstevel@tonic-gate 	int error;
1330Sstevel@tonic-gate 	int i;
1340Sstevel@tonic-gate 	rctl_set_t *dup_set;
1350Sstevel@tonic-gate 	rctl_alloc_gp_t *dup_gp;
1360Sstevel@tonic-gate 	rctl_entity_p_t e;
1370Sstevel@tonic-gate 	lwpdir_t *ldp;
1380Sstevel@tonic-gate 	lwpent_t *lep;
1390Sstevel@tonic-gate 	lwpent_t *clep;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	/*
1420Sstevel@tonic-gate 	 * fork is not supported for the /proc agent lwp.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	if (curthread == p->p_agenttp) {
1450Sstevel@tonic-gate 		error = ENOTSUP;
1460Sstevel@tonic-gate 		goto forkerr;
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if ((error = secpolicy_basic_fork(CRED())) != 0)
1500Sstevel@tonic-gate 		goto forkerr;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * If the calling lwp is doing a fork1() then the
1540Sstevel@tonic-gate 	 * other lwps in this process are not duplicated and
1550Sstevel@tonic-gate 	 * don't need to be held where their kernel stacks can be
1560Sstevel@tonic-gate 	 * cloned.  If doing forkall(), the process is held with
1570Sstevel@tonic-gate 	 * SHOLDFORK, so that the lwps are at a point where their
1580Sstevel@tonic-gate 	 * stacks can be copied which is on entry or exit from
1590Sstevel@tonic-gate 	 * the kernel.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) {
1620Sstevel@tonic-gate 		aston(curthread);
1630Sstevel@tonic-gate 		error = EINTR;
1640Sstevel@tonic-gate 		goto forkerr;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1671091Sraf #if defined(__sparc)
1681091Sraf 	/*
1691091Sraf 	 * Ensure that the user stack is fully constructed
1701091Sraf 	 * before creating the child process structure.
1711091Sraf 	 */
1721091Sraf 	(void) flush_user_windows_to_stack(NULL);
1731091Sraf #endif
1741091Sraf 
1751091Sraf 	mutex_enter(&p->p_lock);
176875Sraf 	/*
177875Sraf 	 * If this is vfork(), cancel any suspend request we might
178875Sraf 	 * have gotten from some other thread via lwp_suspend().
179875Sraf 	 * Otherwise we could end up with a deadlock on return
180875Sraf 	 * from the vfork() in both the parent and the child.
181875Sraf 	 */
182875Sraf 	if (isvfork)
183875Sraf 		curthread->t_proc_flag &= ~TP_HOLDLWP;
1840Sstevel@tonic-gate 	/*
1850Sstevel@tonic-gate 	 * Prevent our resource set associations from being changed during fork.
1860Sstevel@tonic-gate 	 */
1870Sstevel@tonic-gate 	pool_barrier_enter();
1880Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	if (getproc(&cp, 0) < 0) {
1940Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1950Sstevel@tonic-gate 		pool_barrier_exit();
1960Sstevel@tonic-gate 		continuelwps(p);
1970Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1980Sstevel@tonic-gate 		error = EAGAIN;
1990Sstevel@tonic-gate 		goto forkerr;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * Assign an address space to child
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	if (isvfork) {
2080Sstevel@tonic-gate 		/*
2090Sstevel@tonic-gate 		 * Clear any watched areas and remember the
2100Sstevel@tonic-gate 		 * watched pages for restoring in vfwait().
2110Sstevel@tonic-gate 		 */
2120Sstevel@tonic-gate 		as = p->p_as;
2130Sstevel@tonic-gate 		if (avl_numnodes(&as->a_wpage) != 0) {
2140Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2150Sstevel@tonic-gate 			as_clearwatch(as);
2160Sstevel@tonic-gate 			p->p_wpage = as->a_wpage;
2170Sstevel@tonic-gate 			avl_create(&as->a_wpage, wp_compare,
2180Sstevel@tonic-gate 			    sizeof (struct watched_page),
2190Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
2200Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 		cp->p_as = as;
2230Sstevel@tonic-gate 		cp->p_flag |= SVFORK;
2240Sstevel@tonic-gate 	} else {
2250Sstevel@tonic-gate 		/*
2260Sstevel@tonic-gate 		 * We need to hold P_PR_LOCK until the address space has
2270Sstevel@tonic-gate 		 * been duplicated and we've had a chance to remove from the
2280Sstevel@tonic-gate 		 * child any DTrace probes that were in the parent. Holding
2290Sstevel@tonic-gate 		 * P_PR_LOCK prevents any new probes from being added and any
2300Sstevel@tonic-gate 		 * extant probes from being removed.
2310Sstevel@tonic-gate 		 */
2320Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2330Sstevel@tonic-gate 		sprlock_proc(p);
2341880Sahl 		p->p_flag |= SFORKING;
2350Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		error = as_dup(p->p_as, &cp->p_as);
2380Sstevel@tonic-gate 		if (error != 0) {
2390Sstevel@tonic-gate 			fork_fail(cp);
2400Sstevel@tonic-gate 			mutex_enter(&pidlock);
2410Sstevel@tonic-gate 			orphpp = &p->p_orphan;
2420Sstevel@tonic-gate 			while (*orphpp != cp)
2430Sstevel@tonic-gate 				orphpp = &(*orphpp)->p_nextorph;
2440Sstevel@tonic-gate 			*orphpp = cp->p_nextorph;
245*2241Shuah 			if (p->p_child == cp)
246*2241Shuah 				p->p_child = cp->p_sibling;
247*2241Shuah 			if (cp->p_sibling)
248*2241Shuah 				cp->p_sibling->p_psibling = cp->p_psibling;
249*2241Shuah 			if (cp->p_psibling)
250*2241Shuah 				cp->p_psibling->p_sibling = cp->p_sibling;
2510Sstevel@tonic-gate 			mutex_enter(&cp->p_lock);
2520Sstevel@tonic-gate 			tk = cp->p_task;
2530Sstevel@tonic-gate 			task_detach(cp);
2540Sstevel@tonic-gate 			ASSERT(cp->p_pool->pool_ref > 0);
2550Sstevel@tonic-gate 			atomic_add_32(&cp->p_pool->pool_ref, -1);
2560Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
2570Sstevel@tonic-gate 			pid_exit(cp);
2580Sstevel@tonic-gate 			mutex_exit(&pidlock);
2590Sstevel@tonic-gate 			task_rele(tk);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
2621880Sahl 			p->p_flag &= ~SFORKING;
2630Sstevel@tonic-gate 			pool_barrier_exit();
2640Sstevel@tonic-gate 			continuelwps(p);
2650Sstevel@tonic-gate 			sprunlock(p);
2660Sstevel@tonic-gate 			/*
2670Sstevel@tonic-gate 			 * Preserve ENOMEM error condition but
2680Sstevel@tonic-gate 			 * map all others to EAGAIN.
2690Sstevel@tonic-gate 			 */
2700Sstevel@tonic-gate 			error = (error == ENOMEM) ? ENOMEM : EAGAIN;
2710Sstevel@tonic-gate 			goto forkerr;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		/* Duplicate parent's shared memory */
2740Sstevel@tonic-gate 		if (p->p_segacct)
2750Sstevel@tonic-gate 			shmfork(p, cp);
2760Sstevel@tonic-gate 
2771880Sahl 		/*
2781880Sahl 		 * Remove all DTrace tracepoints from the child process. We
2791880Sahl 		 * need to do this _before_ duplicating USDT providers since
2801880Sahl 		 * any associated probes may be immediately enabled.
2811880Sahl 		 */
2821880Sahl 		if (p->p_dtrace_count > 0)
2831880Sahl 			dtrace_fasttrap_fork(p, cp);
2841880Sahl 
2851880Sahl 		/*
2861880Sahl 		 * Duplicate any helper actions and providers. The SFORKING
2871880Sahl 		 * we set above informs the code to enable USDT probes that
2881880Sahl 		 * sprlock() may fail because the child is being forked.
2891880Sahl 		 */
2900Sstevel@tonic-gate 		if (p->p_dtrace_helpers != NULL) {
2910Sstevel@tonic-gate 			ASSERT(dtrace_helpers_fork != NULL);
2920Sstevel@tonic-gate 			(*dtrace_helpers_fork)(p, cp);
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2961880Sahl 		p->p_flag &= ~SFORKING;
2970Sstevel@tonic-gate 		sprunlock(p);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * Duplicate parent's resource controls.
3020Sstevel@tonic-gate 	 */
3030Sstevel@tonic-gate 	dup_set = rctl_set_create();
3040Sstevel@tonic-gate 	for (;;) {
3050Sstevel@tonic-gate 		dup_gp = rctl_set_dup_prealloc(p->p_rctls);
3060Sstevel@tonic-gate 		mutex_enter(&p->p_rctls->rcs_lock);
3070Sstevel@tonic-gate 		if (rctl_set_dup_ready(p->p_rctls, dup_gp))
3080Sstevel@tonic-gate 			break;
3090Sstevel@tonic-gate 		mutex_exit(&p->p_rctls->rcs_lock);
3100Sstevel@tonic-gate 		rctl_prealloc_destroy(dup_gp);
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate 	e.rcep_p.proc = cp;
3130Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
3140Sstevel@tonic-gate 	cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp,
3150Sstevel@tonic-gate 	    RCD_DUP | RCD_CALLBACK);
3160Sstevel@tonic-gate 	mutex_exit(&p->p_rctls->rcs_lock);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	rctl_prealloc_destroy(dup_gp);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Allocate the child's lwp directory and lwpid hash table.
3220Sstevel@tonic-gate 	 */
3230Sstevel@tonic-gate 	if (isfork1)
3240Sstevel@tonic-gate 		cp->p_lwpdir_sz = 2;
3250Sstevel@tonic-gate 	else
3260Sstevel@tonic-gate 		cp->p_lwpdir_sz = p->p_lwpdir_sz;
3270Sstevel@tonic-gate 	cp->p_lwpdir = cp->p_lwpfree = ldp =
3280Sstevel@tonic-gate 		kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP);
3290Sstevel@tonic-gate 	for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++)
3300Sstevel@tonic-gate 		ldp->ld_next = ldp + 1;
3310Sstevel@tonic-gate 	cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2;
3320Sstevel@tonic-gate 	cp->p_tidhash =
3330Sstevel@tonic-gate 		kmem_zalloc(cp->p_tidhash_sz * sizeof (lwpdir_t *), KM_SLEEP);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Duplicate parent's lwps.
3370Sstevel@tonic-gate 	 * Mutual exclusion is not needed because the process is
3380Sstevel@tonic-gate 	 * in the hold state and only the current lwp is running.
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	klgrpset_clear(cp->p_lgrpset);
3410Sstevel@tonic-gate 	if (isfork1) {
3420Sstevel@tonic-gate 		clone = forklwp(ttolwp(curthread), cp, curthread->t_tid);
3430Sstevel@tonic-gate 		if (clone == NULL)
3440Sstevel@tonic-gate 			goto forklwperr;
3450Sstevel@tonic-gate 		/*
3460Sstevel@tonic-gate 		 * Inherit only the lwp_wait()able flag,
3470Sstevel@tonic-gate 		 * Daemon threads should not call fork1(), but oh well...
3480Sstevel@tonic-gate 		 */
3490Sstevel@tonic-gate 		lwptot(clone)->t_proc_flag |=
3500Sstevel@tonic-gate 			(curthread->t_proc_flag & TP_TWAIT);
3510Sstevel@tonic-gate 	} else {
3520Sstevel@tonic-gate 		/* this is forkall(), no one can be in lwp_wait() */
3530Sstevel@tonic-gate 		ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0);
3540Sstevel@tonic-gate 		/* for each entry in the parent's lwp directory... */
3550Sstevel@tonic-gate 		for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) {
3560Sstevel@tonic-gate 			klwp_t *clwp;
357769Sraf 			kthread_t *ct;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) == NULL)
3600Sstevel@tonic-gate 				continue;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 			if ((t = lep->le_thread) != NULL) {
3630Sstevel@tonic-gate 				clwp = forklwp(ttolwp(t), cp, t->t_tid);
3640Sstevel@tonic-gate 				if (clwp == NULL)
3650Sstevel@tonic-gate 					goto forklwperr;
366769Sraf 				ct = lwptot(clwp);
3670Sstevel@tonic-gate 				/*
3680Sstevel@tonic-gate 				 * Inherit lwp_wait()able and daemon flags.
3690Sstevel@tonic-gate 				 */
370769Sraf 				ct->t_proc_flag |=
3710Sstevel@tonic-gate 				    (t->t_proc_flag & (TP_TWAIT|TP_DAEMON));
3720Sstevel@tonic-gate 				/*
3730Sstevel@tonic-gate 				 * Keep track of the clone of curthread to
3740Sstevel@tonic-gate 				 * post return values through lwp_setrval().
375769Sraf 				 * Mark other threads for special treatment
376769Sraf 				 * by lwp_rtt() / post_syscall().
3770Sstevel@tonic-gate 				 */
3780Sstevel@tonic-gate 				if (t == curthread)
3790Sstevel@tonic-gate 					clone = clwp;
380769Sraf 				else
381769Sraf 					ct->t_flag |= T_FORKALL;
3820Sstevel@tonic-gate 			} else {
3830Sstevel@tonic-gate 				/*
3840Sstevel@tonic-gate 				 * Replicate zombie lwps in the child.
3850Sstevel@tonic-gate 				 */
3860Sstevel@tonic-gate 				clep = kmem_zalloc(sizeof (*clep), KM_SLEEP);
3870Sstevel@tonic-gate 				clep->le_lwpid = lep->le_lwpid;
3880Sstevel@tonic-gate 				clep->le_start = lep->le_start;
3890Sstevel@tonic-gate 				lwp_hash_in(cp, clep);
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * Put new process in the parent's process contract, or put it
3960Sstevel@tonic-gate 	 * in a new one if there is an active process template.  Send a
3970Sstevel@tonic-gate 	 * fork event (if requested) to whatever contract the child is
3980Sstevel@tonic-gate 	 * a member of.  Fails if the parent has been SIGKILLed.
3990Sstevel@tonic-gate 	 */
4000Sstevel@tonic-gate 	if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL)
4010Sstevel@tonic-gate 		goto forklwperr;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	/*
4040Sstevel@tonic-gate 	 * No fork failures occur beyond this point.
4050Sstevel@tonic-gate 	 */
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	cp->p_lwpid = p->p_lwpid;
4080Sstevel@tonic-gate 	if (!isfork1) {
4090Sstevel@tonic-gate 		cp->p_lwpdaemon = p->p_lwpdaemon;
4100Sstevel@tonic-gate 		cp->p_zombcnt = p->p_zombcnt;
4110Sstevel@tonic-gate 		/*
4120Sstevel@tonic-gate 		 * If the parent's lwp ids have wrapped around, so have the
4130Sstevel@tonic-gate 		 * child's.
4140Sstevel@tonic-gate 		 */
4150Sstevel@tonic-gate 		cp->p_flag |= p->p_flag & SLWPWRAP;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	corectl_path_hold(cp->p_corefile = p->p_corefile);
4190Sstevel@tonic-gate 	corectl_content_hold(cp->p_content = p->p_content);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	/*
4221217Srab 	 * Duplicate process context ops, if any.
4230Sstevel@tonic-gate 	 */
4241217Srab 	if (p->p_pctx)
4251217Srab 		forkpctx(p, cp);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate #ifdef __sparc
4280Sstevel@tonic-gate 	utrap_dup(p, cp);
4290Sstevel@tonic-gate #endif
4300Sstevel@tonic-gate 	/*
4310Sstevel@tonic-gate 	 * If the child process has been marked to stop on exit
4320Sstevel@tonic-gate 	 * from this fork, arrange for all other lwps to stop in
4330Sstevel@tonic-gate 	 * sympathy with the active lwp.
4340Sstevel@tonic-gate 	 */
4350Sstevel@tonic-gate 	if (PTOU(cp)->u_systrap &&
4360Sstevel@tonic-gate 	    prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) {
4370Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
4380Sstevel@tonic-gate 		t = cp->p_tlist;
4390Sstevel@tonic-gate 		do {
4400Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
4410Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
4420Sstevel@tonic-gate 		} while ((t = t->t_forw) != cp->p_tlist);
4430Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * If the parent process has been marked to stop on exit
4470Sstevel@tonic-gate 	 * from this fork, and its asynchronous-stop flag has not
4480Sstevel@tonic-gate 	 * been set, arrange for all other lwps to stop before
4490Sstevel@tonic-gate 	 * they return back to user level.
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap &&
4520Sstevel@tonic-gate 	    prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) {
4530Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
4540Sstevel@tonic-gate 		t = p->p_tlist;
4550Sstevel@tonic-gate 		do {
4560Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
4570Sstevel@tonic-gate 			aston(t);	/* so TP_PRSTOP will be seen */
4580Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
4590Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/* set return values for child */
4630Sstevel@tonic-gate 	lwp_setrval(clone, p->p_pid, 1);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/* set return values for parent */
4660Sstevel@tonic-gate 	r.r_val1 = (int)cp->p_pid;
4670Sstevel@tonic-gate 	r.r_val2 = 0;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * pool_barrier_exit() can now be called because the child process has:
4710Sstevel@tonic-gate 	 * - all identifying features cloned or set (p_pid, p_task, p_pool)
4720Sstevel@tonic-gate 	 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset)
4730Sstevel@tonic-gate 	 * - any other fields set which are used in resource set binding.
4740Sstevel@tonic-gate 	 */
4750Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4760Sstevel@tonic-gate 	pool_barrier_exit();
4770Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	mutex_enter(&pidlock);
4800Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	/*
4830Sstevel@tonic-gate 	 * Now that there are lwps and threads attached, add the new
4840Sstevel@tonic-gate 	 * process to the process group.
4850Sstevel@tonic-gate 	 */
4860Sstevel@tonic-gate 	pgjoin(cp, p->p_pgidp);
4870Sstevel@tonic-gate 	cp->p_stat = SRUN;
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * We are now done with all the lwps in the child process.
4900Sstevel@tonic-gate 	 */
4910Sstevel@tonic-gate 	t = cp->p_tlist;
4920Sstevel@tonic-gate 	do {
4930Sstevel@tonic-gate 		/*
4940Sstevel@tonic-gate 		 * Set the lwp_suspend()ed lwps running.
4950Sstevel@tonic-gate 		 * They will suspend properly at syscall exit.
4960Sstevel@tonic-gate 		 */
4970Sstevel@tonic-gate 		if (t->t_proc_flag & TP_HOLDLWP)
4980Sstevel@tonic-gate 			lwp_create_done(t);
4990Sstevel@tonic-gate 		else {
5000Sstevel@tonic-gate 			/* set TS_CREATE to allow continuelwps() to work */
5010Sstevel@tonic-gate 			thread_lock(t);
5020Sstevel@tonic-gate 			ASSERT(t->t_state == TS_STOPPED &&
5030Sstevel@tonic-gate 			    !(t->t_schedflag & (TS_CREATE|TS_CSTART)));
5040Sstevel@tonic-gate 			t->t_schedflag |= TS_CREATE;
5050Sstevel@tonic-gate 			thread_unlock(t);
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 	} while ((t = t->t_forw) != cp->p_tlist);
5080Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (isvfork) {
5110Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysvfork, 1);
5120Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
5130Sstevel@tonic-gate 		p->p_flag |= SVFWAIT;
5140Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
5150Sstevel@tonic-gate 		cv_broadcast(&pr_pid_cv[p->p_slot]);	/* inform /proc */
5160Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5170Sstevel@tonic-gate 		/*
5180Sstevel@tonic-gate 		 * Grab child's p_lock before dropping pidlock to ensure
5190Sstevel@tonic-gate 		 * the process will not disappear before we set it running.
5200Sstevel@tonic-gate 		 */
5210Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
5220Sstevel@tonic-gate 		mutex_exit(&pidlock);
5230Sstevel@tonic-gate 		sigdefault(cp);
5240Sstevel@tonic-gate 		continuelwps(cp);
5250Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
5260Sstevel@tonic-gate 	} else {
5270Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, sysfork, 1);
5280Sstevel@tonic-gate 		DTRACE_PROC1(create, proc_t *, cp);
5290Sstevel@tonic-gate 		/*
5300Sstevel@tonic-gate 		 * It is CL_FORKRET's job to drop pidlock.
5310Sstevel@tonic-gate 		 * If we do it here, the process could be set running
5320Sstevel@tonic-gate 		 * and disappear before CL_FORKRET() is called.
5330Sstevel@tonic-gate 		 */
5340Sstevel@tonic-gate 		CL_FORKRET(curthread, cp->p_tlist);
5350Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&pidlock));
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	return (r.r_vals);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate forklwperr:
5410Sstevel@tonic-gate 	if (isvfork) {
5420Sstevel@tonic-gate 		if (avl_numnodes(&p->p_wpage) != 0) {
5430Sstevel@tonic-gate 			/* restore watchpoints to parent */
5440Sstevel@tonic-gate 			as = p->p_as;
5450Sstevel@tonic-gate 			AS_LOCK_ENTER(as, &as->a_lock,
5460Sstevel@tonic-gate 				RW_WRITER);
5470Sstevel@tonic-gate 			as->a_wpage = p->p_wpage;
5480Sstevel@tonic-gate 			avl_create(&p->p_wpage, wp_compare,
5490Sstevel@tonic-gate 			    sizeof (struct watched_page),
5500Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
5510Sstevel@tonic-gate 			as_setwatch(as);
5520Sstevel@tonic-gate 			AS_LOCK_EXIT(as, &as->a_lock);
5530Sstevel@tonic-gate 		}
5540Sstevel@tonic-gate 	} else {
5550Sstevel@tonic-gate 		if (cp->p_segacct)
5560Sstevel@tonic-gate 			shmexit(cp);
5570Sstevel@tonic-gate 		as = cp->p_as;
5580Sstevel@tonic-gate 		cp->p_as = &kas;
5590Sstevel@tonic-gate 		as_free(as);
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	if (cp->p_lwpdir) {
5630Sstevel@tonic-gate 		for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++)
5640Sstevel@tonic-gate 			if ((lep = ldp->ld_entry) != NULL)
5650Sstevel@tonic-gate 				kmem_free(lep, sizeof (*lep));
5660Sstevel@tonic-gate 		kmem_free(cp->p_lwpdir,
5670Sstevel@tonic-gate 		    cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir));
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	cp->p_lwpdir = NULL;
5700Sstevel@tonic-gate 	cp->p_lwpfree = NULL;
5710Sstevel@tonic-gate 	cp->p_lwpdir_sz = 0;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (cp->p_tidhash)
5740Sstevel@tonic-gate 		kmem_free(cp->p_tidhash,
5750Sstevel@tonic-gate 		    cp->p_tidhash_sz * sizeof (*cp->p_tidhash));
5760Sstevel@tonic-gate 	cp->p_tidhash = NULL;
5770Sstevel@tonic-gate 	cp->p_tidhash_sz = 0;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	forklwp_fail(cp);
5800Sstevel@tonic-gate 	fork_fail(cp);
5810Sstevel@tonic-gate 	rctl_set_free(cp->p_rctls);
5820Sstevel@tonic-gate 	mutex_enter(&pidlock);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	/*
5850Sstevel@tonic-gate 	 * Detach failed child from task.
5860Sstevel@tonic-gate 	 */
5870Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
5880Sstevel@tonic-gate 	tk = cp->p_task;
5890Sstevel@tonic-gate 	task_detach(cp);
5900Sstevel@tonic-gate 	ASSERT(cp->p_pool->pool_ref > 0);
5910Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, -1);
5920Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	orphpp = &p->p_orphan;
5950Sstevel@tonic-gate 	while (*orphpp != cp)
5960Sstevel@tonic-gate 		orphpp = &(*orphpp)->p_nextorph;
5970Sstevel@tonic-gate 	*orphpp = cp->p_nextorph;
598*2241Shuah 	if (p->p_child == cp)
599*2241Shuah 		p->p_child = cp->p_sibling;
600*2241Shuah 	if (cp->p_sibling)
601*2241Shuah 		cp->p_sibling->p_psibling = cp->p_psibling;
602*2241Shuah 	if (cp->p_psibling)
603*2241Shuah 		cp->p_psibling->p_sibling = cp->p_sibling;
6040Sstevel@tonic-gate 	pid_exit(cp);
6050Sstevel@tonic-gate 	mutex_exit(&pidlock);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	task_rele(tk);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
6100Sstevel@tonic-gate 	pool_barrier_exit();
6110Sstevel@tonic-gate 	continuelwps(p);
6120Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6130Sstevel@tonic-gate 	error = EAGAIN;
6140Sstevel@tonic-gate forkerr:
6150Sstevel@tonic-gate 	return ((int64_t)set_errno(error));
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate /*
6190Sstevel@tonic-gate  * Free allocated resources from getproc() if a fork failed.
6200Sstevel@tonic-gate  */
6210Sstevel@tonic-gate static void
6220Sstevel@tonic-gate fork_fail(proc_t *cp)
6230Sstevel@tonic-gate {
6240Sstevel@tonic-gate 	uf_info_t *fip = P_FINFO(cp);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	fcnt_add(fip, -1);
6270Sstevel@tonic-gate 	sigdelq(cp, NULL, 0);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	mutex_enter(&pidlock);
6300Sstevel@tonic-gate 	upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred));
6310Sstevel@tonic-gate 	mutex_exit(&pidlock);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/*
6340Sstevel@tonic-gate 	 * single threaded, so no locking needed here
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 	crfree(cp->p_cred);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	VN_RELE(u.u_cdir);
6410Sstevel@tonic-gate 	if (u.u_rdir)
6420Sstevel@tonic-gate 		VN_RELE(u.u_rdir);
6430Sstevel@tonic-gate 	if (cp->p_exec)
6440Sstevel@tonic-gate 		VN_RELE(cp->p_exec);
6450Sstevel@tonic-gate 	if (cp->p_execdir)
6460Sstevel@tonic-gate 		VN_RELE(cp->p_execdir);
6470Sstevel@tonic-gate 	if (u.u_cwd)
6480Sstevel@tonic-gate 		refstr_rele(u.u_cwd);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate  * Clean up the lwps already created for this child process.
6530Sstevel@tonic-gate  * The fork failed while duplicating all the lwps of the parent
6540Sstevel@tonic-gate  * and those lwps already created must be freed.
6550Sstevel@tonic-gate  * This process is invisible to the rest of the system,
6560Sstevel@tonic-gate  * so we don't need to hold p->p_lock to protect the list.
6570Sstevel@tonic-gate  */
6580Sstevel@tonic-gate static void
6590Sstevel@tonic-gate forklwp_fail(proc_t *p)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	kthread_t *t;
6620Sstevel@tonic-gate 	task_t *tk;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	while ((t = p->p_tlist) != NULL) {
6650Sstevel@tonic-gate 		/*
6660Sstevel@tonic-gate 		 * First remove the lwp from the process's p_tlist.
6670Sstevel@tonic-gate 		 */
6680Sstevel@tonic-gate 		if (t != t->t_forw)
6690Sstevel@tonic-gate 			p->p_tlist = t->t_forw;
6700Sstevel@tonic-gate 		else
6710Sstevel@tonic-gate 			p->p_tlist = NULL;
6720Sstevel@tonic-gate 		p->p_lwpcnt--;
6730Sstevel@tonic-gate 		t->t_forw->t_back = t->t_back;
6740Sstevel@tonic-gate 		t->t_back->t_forw = t->t_forw;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		tk = p->p_task;
6770Sstevel@tonic-gate 		mutex_enter(&p->p_zone->zone_nlwps_lock);
6780Sstevel@tonic-gate 		tk->tk_nlwps--;
6790Sstevel@tonic-gate 		tk->tk_proj->kpj_nlwps--;
6800Sstevel@tonic-gate 		p->p_zone->zone_nlwps--;
6810Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		ASSERT(t->t_schedctl == NULL);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if (t->t_door != NULL) {
6860Sstevel@tonic-gate 			kmem_free(t->t_door, sizeof (door_data_t));
6870Sstevel@tonic-gate 			t->t_door = NULL;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 		lwp_ctmpl_clear(ttolwp(t));
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		/*
6920Sstevel@tonic-gate 		 * Remove the thread from the all threads list.
6930Sstevel@tonic-gate 		 * We need to hold pidlock for this.
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		mutex_enter(&pidlock);
6960Sstevel@tonic-gate 		t->t_next->t_prev = t->t_prev;
6970Sstevel@tonic-gate 		t->t_prev->t_next = t->t_next;
6980Sstevel@tonic-gate 		CL_EXIT(t);	/* tell the scheduler that we're exiting */
6990Sstevel@tonic-gate 		cv_broadcast(&t->t_joincv);	/* tell anyone in thread_join */
7000Sstevel@tonic-gate 		mutex_exit(&pidlock);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		/*
7030Sstevel@tonic-gate 		 * Let the lgroup load averages know that this thread isn't
7040Sstevel@tonic-gate 		 * going to show up (i.e. un-do what was done on behalf of
7050Sstevel@tonic-gate 		 * this thread by the earlier lgrp_move_thread()).
7060Sstevel@tonic-gate 		 */
7070Sstevel@tonic-gate 		kpreempt_disable();
7080Sstevel@tonic-gate 		lgrp_move_thread(t, NULL, 1);
7090Sstevel@tonic-gate 		kpreempt_enable();
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * The thread was created TS_STOPPED.
7130Sstevel@tonic-gate 		 * We change it to TS_FREE to avoid an
7140Sstevel@tonic-gate 		 * ASSERT() panic in thread_free().
7150Sstevel@tonic-gate 		 */
7160Sstevel@tonic-gate 		t->t_state = TS_FREE;
7170Sstevel@tonic-gate 		thread_rele(t);
7180Sstevel@tonic-gate 		thread_free(t);
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate extern struct as kas;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * fork a kernel process.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate int
7280Sstevel@tonic-gate newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	proc_t *p;
7310Sstevel@tonic-gate 	struct user *up;
7320Sstevel@tonic-gate 	klwp_t *lwp;
7330Sstevel@tonic-gate 	cont_process_t *ctp = NULL;
7340Sstevel@tonic-gate 	rctl_entity_p_t e;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	ASSERT(!(cid == syscid && ct != NULL));
7370Sstevel@tonic-gate 	if (cid == syscid) {
7380Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp;
7390Sstevel@tonic-gate 		rctl_set_t *init_set;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		if (getproc(&p, 1) < 0)
7420Sstevel@tonic-gate 			return (EAGAIN);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		p->p_flag |= SNOWAIT;
7450Sstevel@tonic-gate 		p->p_exec = NULL;
7460Sstevel@tonic-gate 		p->p_execdir = NULL;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		init_set = rctl_set_create();
7490Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 		/*
7520Sstevel@tonic-gate 		 * kernel processes do not inherit /proc tracing flags.
7530Sstevel@tonic-gate 		 */
7540Sstevel@tonic-gate 		sigemptyset(&p->p_sigmask);
7550Sstevel@tonic-gate 		premptyset(&p->p_fltmask);
7560Sstevel@tonic-gate 		up = PTOU(p);
7570Sstevel@tonic-gate 		up->u_systrap = 0;
7580Sstevel@tonic-gate 		premptyset(&(up->u_entrymask));
7590Sstevel@tonic-gate 		premptyset(&(up->u_exitmask));
7600Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7610Sstevel@tonic-gate 		e.rcep_p.proc = p;
7620Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
7630Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
7640Sstevel@tonic-gate 		    init_gp);
7650Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
7680Sstevel@tonic-gate 	} else  {
7690Sstevel@tonic-gate 		rctl_alloc_gp_t *init_gp, *default_gp;
7700Sstevel@tonic-gate 		rctl_set_t *init_set;
7710Sstevel@tonic-gate 		task_t *tk, *tk_old;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		if (getproc(&p, 0) < 0)
7740Sstevel@tonic-gate 			return (EAGAIN);
7750Sstevel@tonic-gate 		/*
7760Sstevel@tonic-gate 		 * init creates a new task, distinct from the task
7770Sstevel@tonic-gate 		 * containing kernel "processes".
7780Sstevel@tonic-gate 		 */
7790Sstevel@tonic-gate 		tk = task_create(0, p->p_zone);
7800Sstevel@tonic-gate 		mutex_enter(&tk->tk_zone->zone_nlwps_lock);
7810Sstevel@tonic-gate 		tk->tk_proj->kpj_ntasks++;
7820Sstevel@tonic-gate 		mutex_exit(&tk->tk_zone->zone_nlwps_lock);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS);
7850Sstevel@tonic-gate 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
7860Sstevel@tonic-gate 		init_set = rctl_set_create();
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 		mutex_enter(&pidlock);
7890Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7900Sstevel@tonic-gate 		tk_old = p->p_task;	/* switch to new task */
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 		task_detach(p);
7930Sstevel@tonic-gate 		task_begin(tk, p);
7940Sstevel@tonic-gate 		mutex_exit(&pidlock);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		e.rcep_p.proc = p;
7970Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROCESS;
7980Sstevel@tonic-gate 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
7990Sstevel@tonic-gate 		    init_gp);
8000Sstevel@tonic-gate 		rctlproc_default_init(p, default_gp);
8010Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		task_rele(tk_old);
8040Sstevel@tonic-gate 		rctl_prealloc_destroy(default_gp);
8050Sstevel@tonic-gate 		rctl_prealloc_destroy(init_gp);
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	p->p_as = &kas;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri,
8110Sstevel@tonic-gate 	    &curthread->t_hold, cid, 1)) == NULL) {
8120Sstevel@tonic-gate 		task_t *tk;
8130Sstevel@tonic-gate 		fork_fail(p);
8140Sstevel@tonic-gate 		mutex_enter(&pidlock);
8150Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
8160Sstevel@tonic-gate 		tk = p->p_task;
8170Sstevel@tonic-gate 		task_detach(p);
8180Sstevel@tonic-gate 		ASSERT(p->p_pool->pool_ref > 0);
8190Sstevel@tonic-gate 		atomic_add_32(&p->p_pool->pool_ref, -1);
8200Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
8210Sstevel@tonic-gate 		pid_exit(p);
8220Sstevel@tonic-gate 		mutex_exit(&pidlock);
8230Sstevel@tonic-gate 		task_rele(tk);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		return (EAGAIN);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (cid != syscid) {
8290Sstevel@tonic-gate 		ctp = contract_process_fork(sys_process_tmpl, p, curproc,
8300Sstevel@tonic-gate 		    B_FALSE);
8310Sstevel@tonic-gate 		ASSERT(ctp != NULL);
8320Sstevel@tonic-gate 		if (ct != NULL)
8330Sstevel@tonic-gate 			*ct = &ctp->conp_contract;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	p->p_lwpid = 1;
8370Sstevel@tonic-gate 	mutex_enter(&pidlock);
8380Sstevel@tonic-gate 	pgjoin(p, curproc->p_pgidp);
8390Sstevel@tonic-gate 	p->p_stat = SRUN;
8400Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
8410Sstevel@tonic-gate 	lwptot(lwp)->t_proc_flag &= ~TP_HOLDLWP;
8420Sstevel@tonic-gate 	lwp_create_done(lwptot(lwp));
8430Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8440Sstevel@tonic-gate 	mutex_exit(&pidlock);
8450Sstevel@tonic-gate 	return (0);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate  * create a child proc struct.
8500Sstevel@tonic-gate  */
8510Sstevel@tonic-gate static int
8520Sstevel@tonic-gate getproc(proc_t **cpp, int kernel)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	proc_t		*pp, *cp;
8550Sstevel@tonic-gate 	pid_t		newpid;
8560Sstevel@tonic-gate 	struct user	*uarea;
8570Sstevel@tonic-gate 	extern uint_t	nproc;
8580Sstevel@tonic-gate 	struct cred	*cr;
8590Sstevel@tonic-gate 	uid_t		ruid;
8600Sstevel@tonic-gate 	zoneid_t	zoneid;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (!page_mem_avail(tune.t_minarmem))
8630Sstevel@tonic-gate 		return (-1);
8640Sstevel@tonic-gate 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
8650Sstevel@tonic-gate 		return (-1);	/* no point in starting new processes */
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	pp = curproc;
8680Sstevel@tonic-gate 	cp = kmem_cache_alloc(process_cache, KM_SLEEP);
8690Sstevel@tonic-gate 	bzero(cp, sizeof (proc_t));
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Make proc entry for child process
8730Sstevel@tonic-gate 	 */
8740Sstevel@tonic-gate 	mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL);
8750Sstevel@tonic-gate 	mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL);
8760Sstevel@tonic-gate #if defined(__x86)
8770Sstevel@tonic-gate 	mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL);
8780Sstevel@tonic-gate #endif
8790Sstevel@tonic-gate 	mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL);
8800Sstevel@tonic-gate 	cp->p_stat = SIDL;
8810Sstevel@tonic-gate 	cp->p_mstart = gethrtime();
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((newpid = pid_assign(cp)) == -1) {
8840Sstevel@tonic-gate 		if (nproc == v.v_proc) {
8850Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, procovf, 1);
8860Sstevel@tonic-gate 			cmn_err(CE_WARN, "out of processes");
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 		goto bad;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * If not privileged make sure that this user hasn't exceeded
8930Sstevel@tonic-gate 	 * v.v_maxup processes, and that users collectively haven't
8940Sstevel@tonic-gate 	 * exceeded v.v_maxupttl processes.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	mutex_enter(&pidlock);
8970Sstevel@tonic-gate 	ASSERT(nproc < v.v_proc);	/* otherwise how'd we get our pid? */
8980Sstevel@tonic-gate 	cr = CRED();
8990Sstevel@tonic-gate 	ruid = crgetruid(cr);
9000Sstevel@tonic-gate 	zoneid = crgetzoneid(cr);
9010Sstevel@tonic-gate 	if (nproc >= v.v_maxup && 	/* short-circuit; usually false */
9020Sstevel@tonic-gate 	    (nproc >= v.v_maxupttl ||
9030Sstevel@tonic-gate 	    upcount_get(ruid, zoneid) >= v.v_maxup) &&
9040Sstevel@tonic-gate 	    secpolicy_newproc(cr) != 0) {
9050Sstevel@tonic-gate 		mutex_exit(&pidlock);
9060Sstevel@tonic-gate 		zcmn_err(zoneid, CE_NOTE,
9070Sstevel@tonic-gate 		    "out of per-user processes for uid %d", ruid);
9080Sstevel@tonic-gate 		goto bad;
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/*
9120Sstevel@tonic-gate 	 * Everything is cool, put the new proc on the active process list.
9130Sstevel@tonic-gate 	 * It is already on the pid list and in /proc.
9140Sstevel@tonic-gate 	 * Increment the per uid process count (upcount).
9150Sstevel@tonic-gate 	 */
9160Sstevel@tonic-gate 	nproc++;
9170Sstevel@tonic-gate 	upcount_inc(ruid, zoneid);
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	cp->p_next = practive;
9200Sstevel@tonic-gate 	practive->p_prev = cp;
9210Sstevel@tonic-gate 	practive = cp;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	cp->p_ignore = pp->p_ignore;
9240Sstevel@tonic-gate 	cp->p_siginfo = pp->p_siginfo;
9250Sstevel@tonic-gate 	cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD);
9260Sstevel@tonic-gate 	cp->p_sessp = pp->p_sessp;
9270Sstevel@tonic-gate 	SESS_HOLD(pp->p_sessp);
9280Sstevel@tonic-gate 	cp->p_exec = pp->p_exec;
9290Sstevel@tonic-gate 	cp->p_execdir = pp->p_execdir;
9300Sstevel@tonic-gate 	cp->p_zone = pp->p_zone;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	cp->p_bssbase = pp->p_bssbase;
9330Sstevel@tonic-gate 	cp->p_brkbase = pp->p_brkbase;
9340Sstevel@tonic-gate 	cp->p_brksize = pp->p_brksize;
9350Sstevel@tonic-gate 	cp->p_brkpageszc = pp->p_brkpageszc;
9360Sstevel@tonic-gate 	cp->p_stksize = pp->p_stksize;
9370Sstevel@tonic-gate 	cp->p_stkpageszc = pp->p_stkpageszc;
9380Sstevel@tonic-gate 	cp->p_stkprot = pp->p_stkprot;
9390Sstevel@tonic-gate 	cp->p_datprot = pp->p_datprot;
9400Sstevel@tonic-gate 	cp->p_usrstack = pp->p_usrstack;
9410Sstevel@tonic-gate 	cp->p_model = pp->p_model;
9420Sstevel@tonic-gate 	cp->p_ppid = pp->p_pid;
9430Sstevel@tonic-gate 	cp->p_ancpid = pp->p_pid;
9440Sstevel@tonic-gate 	cp->p_portcnt = pp->p_portcnt;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * Initialize watchpoint structures
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area),
9500Sstevel@tonic-gate 	    offsetof(struct watched_area, wa_link));
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * Initialize immediate resource control values.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	cp->p_stk_ctl = pp->p_stk_ctl;
9560Sstevel@tonic-gate 	cp->p_fsz_ctl = pp->p_fsz_ctl;
9570Sstevel@tonic-gate 	cp->p_vmem_ctl = pp->p_vmem_ctl;
9580Sstevel@tonic-gate 	cp->p_fno_ctl = pp->p_fno_ctl;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/*
9610Sstevel@tonic-gate 	 * Link up to parent-child-sibling chain.  No need to lock
9620Sstevel@tonic-gate 	 * in general since only a call to freeproc() (done by the
9630Sstevel@tonic-gate 	 * same parent as newproc()) diddles with the child chain.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	cp->p_sibling = pp->p_child;
9660Sstevel@tonic-gate 	if (pp->p_child)
9670Sstevel@tonic-gate 		pp->p_child->p_psibling = cp;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	cp->p_parent = pp;
9700Sstevel@tonic-gate 	pp->p_child = cp;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	cp->p_child_ns = NULL;
9730Sstevel@tonic-gate 	cp->p_sibling_ns = NULL;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	cp->p_nextorph = pp->p_orphan;
9760Sstevel@tonic-gate 	cp->p_nextofkin = pp;
9770Sstevel@tonic-gate 	pp->p_orphan = cp;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/*
9800Sstevel@tonic-gate 	 * Inherit profiling state; do not inherit REALPROF profiling state.
9810Sstevel@tonic-gate 	 */
9820Sstevel@tonic-gate 	cp->p_prof = pp->p_prof;
9830Sstevel@tonic-gate 	cp->p_rprof_cyclic = CYCLIC_NONE;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	/*
9860Sstevel@tonic-gate 	 * Inherit pool pointer from the parent.  Kernel processes are
9870Sstevel@tonic-gate 	 * always bound to the default pool.
9880Sstevel@tonic-gate 	 */
9890Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
9900Sstevel@tonic-gate 	if (kernel) {
9910Sstevel@tonic-gate 		cp->p_pool = pool_default;
9920Sstevel@tonic-gate 		cp->p_flag |= SSYS;
9930Sstevel@tonic-gate 	} else {
9940Sstevel@tonic-gate 		cp->p_pool = pp->p_pool;
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	atomic_add_32(&cp->p_pool->pool_ref, 1);
9970Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/*
10000Sstevel@tonic-gate 	 * Add the child process to the current task.  Kernel processes
10010Sstevel@tonic-gate 	 * are always attached to task0.
10020Sstevel@tonic-gate 	 */
10030Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
10040Sstevel@tonic-gate 	if (kernel)
10050Sstevel@tonic-gate 		task_attach(task0p, cp);
10060Sstevel@tonic-gate 	else
10070Sstevel@tonic-gate 		task_attach(pp->p_task, cp);
10080Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
10090Sstevel@tonic-gate 	mutex_exit(&pidlock);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t),
10120Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * Duplicate any audit information kept in the process table
10160Sstevel@tonic-gate 	 */
10170Sstevel@tonic-gate #ifdef C2_AUDIT
10180Sstevel@tonic-gate 	if (audit_active)	/* copy audit data to cp */
10190Sstevel@tonic-gate 		audit_newproc(cp);
10200Sstevel@tonic-gate #endif
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	crhold(cp->p_cred = cr);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/*
10250Sstevel@tonic-gate 	 * Bump up the counts on the file structures pointed at by the
10260Sstevel@tonic-gate 	 * parent's file table since the child will point at them too.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	fcnt_add(P_FINFO(pp), 1);
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	VN_HOLD(u.u_cdir);
10310Sstevel@tonic-gate 	if (u.u_rdir)
10320Sstevel@tonic-gate 		VN_HOLD(u.u_rdir);
10330Sstevel@tonic-gate 	if (u.u_cwd)
10340Sstevel@tonic-gate 		refstr_hold(u.u_cwd);
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
10370Sstevel@tonic-gate 	 * copy the parent's uarea.
10380Sstevel@tonic-gate 	 */
10390Sstevel@tonic-gate 	uarea = PTOU(cp);
10400Sstevel@tonic-gate 	bcopy(PTOU(pp), uarea, sizeof (user_t));
10410Sstevel@tonic-gate 	flist_fork(P_FINFO(pp), P_FINFO(cp));
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	gethrestime(&uarea->u_start);
10440Sstevel@tonic-gate 	uarea->u_ticks = lbolt;
10450Sstevel@tonic-gate 	uarea->u_mem = rm_asrss(pp->p_as);
10460Sstevel@tonic-gate 	uarea->u_acflag = AFORK;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	/*
10490Sstevel@tonic-gate 	 * If inherit-on-fork, copy /proc tracing flags to child.
10500Sstevel@tonic-gate 	 */
10510Sstevel@tonic-gate 	if ((pp->p_proc_flag & P_PR_FORK) != 0) {
10520Sstevel@tonic-gate 		cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK);
10530Sstevel@tonic-gate 		cp->p_sigmask = pp->p_sigmask;
10540Sstevel@tonic-gate 		cp->p_fltmask = pp->p_fltmask;
10550Sstevel@tonic-gate 	} else {
10560Sstevel@tonic-gate 		sigemptyset(&cp->p_sigmask);
10570Sstevel@tonic-gate 		premptyset(&cp->p_fltmask);
10580Sstevel@tonic-gate 		uarea->u_systrap = 0;
10590Sstevel@tonic-gate 		premptyset(&uarea->u_entrymask);
10600Sstevel@tonic-gate 		premptyset(&uarea->u_exitmask);
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 	/*
10630Sstevel@tonic-gate 	 * If microstate accounting is being inherited, mark child
10640Sstevel@tonic-gate 	 */
10650Sstevel@tonic-gate 	if ((pp->p_flag & SMSFORK) != 0)
10660Sstevel@tonic-gate 		cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	/*
10690Sstevel@tonic-gate 	 * Inherit fixalignment flag from the parent
10700Sstevel@tonic-gate 	 */
10710Sstevel@tonic-gate 	cp->p_fixalignment = pp->p_fixalignment;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	if (cp->p_exec)
10740Sstevel@tonic-gate 		VN_HOLD(cp->p_exec);
10750Sstevel@tonic-gate 	if (cp->p_execdir)
10760Sstevel@tonic-gate 		VN_HOLD(cp->p_execdir);
10770Sstevel@tonic-gate 	*cpp = cp;
10780Sstevel@tonic-gate 	return (0);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate bad:
10810Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&pidlock));
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	mutex_destroy(&cp->p_crlock);
10840Sstevel@tonic-gate 	mutex_destroy(&cp->p_pflock);
10850Sstevel@tonic-gate #if defined(__x86)
10860Sstevel@tonic-gate 	mutex_destroy(&cp->p_ldtlock);
10870Sstevel@tonic-gate #endif
10880Sstevel@tonic-gate 	if (newpid != -1) {
10890Sstevel@tonic-gate 		proc_entry_free(cp->p_pidp);
10900Sstevel@tonic-gate 		(void) pid_rele(cp->p_pidp);
10910Sstevel@tonic-gate 	}
10920Sstevel@tonic-gate 	kmem_cache_free(process_cache, cp);
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	/*
10950Sstevel@tonic-gate 	 * We most likely got into this situation because some process is
10960Sstevel@tonic-gate 	 * forking out of control.  As punishment, put it to sleep for a
10970Sstevel@tonic-gate 	 * bit so it can't eat the machine alive.  Sleep interval is chosen
10980Sstevel@tonic-gate 	 * to allow no more than one fork failure per cpu per clock tick
10990Sstevel@tonic-gate 	 * on average (yes, I just made this up).  This has two desirable
11000Sstevel@tonic-gate 	 * properties: (1) it sets a constant limit on the fork failure
11010Sstevel@tonic-gate 	 * rate, and (2) the busier the system is, the harsher the penalty
11020Sstevel@tonic-gate 	 * for abusing it becomes.
11030Sstevel@tonic-gate 	 */
11040Sstevel@tonic-gate 	INCR_COUNT(&fork_fail_pending, &pidlock);
11050Sstevel@tonic-gate 	delay(fork_fail_pending / ncpus + 1);
11060Sstevel@tonic-gate 	DECR_COUNT(&fork_fail_pending, &pidlock);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	return (-1); /* out of memory or proc slots */
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /*
11120Sstevel@tonic-gate  * Release virtual memory.
11130Sstevel@tonic-gate  * In the case of vfork(), the child was given exclusive access to its
11140Sstevel@tonic-gate  * parent's address space.  The parent is waiting in vfwait() for the
11150Sstevel@tonic-gate  * child to release its exclusive claim via relvm().
11160Sstevel@tonic-gate  */
11170Sstevel@tonic-gate void
11180Sstevel@tonic-gate relvm()
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	proc_t *p = curproc;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	ASSERT((unsigned)p->p_lwpcnt <= 1);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	prrelvm();	/* inform /proc */
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	if (p->p_flag & SVFORK) {
11270Sstevel@tonic-gate 		proc_t *pp = p->p_parent;
11280Sstevel@tonic-gate 		/*
11290Sstevel@tonic-gate 		 * The child process is either exec'ing or exit'ing.
11300Sstevel@tonic-gate 		 * The child is now separated from the parent's address
11310Sstevel@tonic-gate 		 * space.  The parent process is made dispatchable.
11320Sstevel@tonic-gate 		 *
11330Sstevel@tonic-gate 		 * This is a delicate locking maneuver, involving
11340Sstevel@tonic-gate 		 * both the parent's p_lock and the child's p_lock.
11350Sstevel@tonic-gate 		 * As soon as the SVFORK flag is turned off, the
11360Sstevel@tonic-gate 		 * parent is free to run, but it must not run until
11370Sstevel@tonic-gate 		 * we wake it up using its p_cv because it might
11380Sstevel@tonic-gate 		 * exit and we would be referencing invalid memory.
11390Sstevel@tonic-gate 		 * Therefore, we hold the parent with its p_lock
11400Sstevel@tonic-gate 		 * while protecting our p_flags with our own p_lock.
11410Sstevel@tonic-gate 		 */
11420Sstevel@tonic-gate try_again:
11430Sstevel@tonic-gate 		mutex_enter(&p->p_lock);	/* grab child's lock first */
11440Sstevel@tonic-gate 		prbarrier(p);		/* make sure /proc is blocked out */
11450Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		/*
11480Sstevel@tonic-gate 		 * Check if parent is locked by /proc.
11490Sstevel@tonic-gate 		 */
11500Sstevel@tonic-gate 		if (pp->p_proc_flag & P_PR_LOCK) {
11510Sstevel@tonic-gate 			/*
11520Sstevel@tonic-gate 			 * Delay until /proc is done with the parent.
11530Sstevel@tonic-gate 			 * We must drop our (the child's) p->p_lock, wait
11540Sstevel@tonic-gate 			 * via prbarrier() on the parent, then start over.
11550Sstevel@tonic-gate 			 */
11560Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
11570Sstevel@tonic-gate 			prbarrier(pp);
11580Sstevel@tonic-gate 			mutex_exit(&pp->p_lock);
11590Sstevel@tonic-gate 			goto try_again;
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 		p->p_flag &= ~SVFORK;
11620Sstevel@tonic-gate 		kpreempt_disable();
11630Sstevel@tonic-gate 		p->p_as = &kas;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		/*
11660Sstevel@tonic-gate 		 * notify hat of change in thread's address space
11670Sstevel@tonic-gate 		 */
11680Sstevel@tonic-gate 		hat_thread_exit(curthread);
11690Sstevel@tonic-gate 		kpreempt_enable();
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		/*
11720Sstevel@tonic-gate 		 * child sizes are copied back to parent because
11730Sstevel@tonic-gate 		 * child may have grown.
11740Sstevel@tonic-gate 		 */
11750Sstevel@tonic-gate 		pp->p_brkbase = p->p_brkbase;
11760Sstevel@tonic-gate 		pp->p_brksize = p->p_brksize;
11770Sstevel@tonic-gate 		pp->p_stksize = p->p_stksize;
11780Sstevel@tonic-gate 		/*
11790Sstevel@tonic-gate 		 * The parent is no longer waiting for the vfork()d child.
11800Sstevel@tonic-gate 		 * Restore the parent's watched pages, if any.  This is
11810Sstevel@tonic-gate 		 * safe because we know the parent is not locked by /proc
11820Sstevel@tonic-gate 		 */
11830Sstevel@tonic-gate 		pp->p_flag &= ~SVFWAIT;
11840Sstevel@tonic-gate 		if (avl_numnodes(&pp->p_wpage) != 0) {
11850Sstevel@tonic-gate 			pp->p_as->a_wpage = pp->p_wpage;
11860Sstevel@tonic-gate 			avl_create(&pp->p_wpage, wp_compare,
11870Sstevel@tonic-gate 			    sizeof (struct watched_page),
11880Sstevel@tonic-gate 			    offsetof(struct watched_page, wp_link));
11890Sstevel@tonic-gate 		}
11900Sstevel@tonic-gate 		cv_signal(&pp->p_cv);
11910Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
11920Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
11930Sstevel@tonic-gate 	} else {
11940Sstevel@tonic-gate 		if (p->p_as != &kas) {
11950Sstevel@tonic-gate 			struct as *as;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 			if (p->p_segacct)
11980Sstevel@tonic-gate 				shmexit(p);
11990Sstevel@tonic-gate 			/*
12000Sstevel@tonic-gate 			 * We grab p_lock for the benefit of /proc
12010Sstevel@tonic-gate 			 */
12020Sstevel@tonic-gate 			kpreempt_disable();
12030Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
12040Sstevel@tonic-gate 			prbarrier(p);	/* make sure /proc is blocked out */
12050Sstevel@tonic-gate 			as = p->p_as;
12060Sstevel@tonic-gate 			p->p_as = &kas;
12070Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 			/*
12100Sstevel@tonic-gate 			 * notify hat of change in thread's address space
12110Sstevel@tonic-gate 			 */
12120Sstevel@tonic-gate 			hat_thread_exit(curthread);
12130Sstevel@tonic-gate 			kpreempt_enable();
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 			as_free(as);
12160Sstevel@tonic-gate 		}
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate  * Wait for child to exec or exit.
12220Sstevel@tonic-gate  * Called by parent of vfork'ed process.
12230Sstevel@tonic-gate  * See important comments in relvm(), above.
12240Sstevel@tonic-gate  */
12250Sstevel@tonic-gate void
12260Sstevel@tonic-gate vfwait(pid_t pid)
12270Sstevel@tonic-gate {
12280Sstevel@tonic-gate 	int signalled = 0;
12290Sstevel@tonic-gate 	proc_t *pp = ttoproc(curthread);
12300Sstevel@tonic-gate 	proc_t *cp;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	/*
12330Sstevel@tonic-gate 	 * Wait for child to exec or exit.
12340Sstevel@tonic-gate 	 */
12350Sstevel@tonic-gate 	for (;;) {
12360Sstevel@tonic-gate 		mutex_enter(&pidlock);
12370Sstevel@tonic-gate 		cp = prfind(pid);
12380Sstevel@tonic-gate 		if (cp == NULL || cp->p_parent != pp) {
12390Sstevel@tonic-gate 			/*
12400Sstevel@tonic-gate 			 * Child has exit()ed.
12410Sstevel@tonic-gate 			 */
12420Sstevel@tonic-gate 			mutex_exit(&pidlock);
12430Sstevel@tonic-gate 			break;
12440Sstevel@tonic-gate 		}
12450Sstevel@tonic-gate 		/*
12460Sstevel@tonic-gate 		 * Grab the child's p_lock before releasing pidlock.
12470Sstevel@tonic-gate 		 * Otherwise, the child could exit and we would be
12480Sstevel@tonic-gate 		 * referencing invalid memory.
12490Sstevel@tonic-gate 		 */
12500Sstevel@tonic-gate 		mutex_enter(&cp->p_lock);
12510Sstevel@tonic-gate 		mutex_exit(&pidlock);
12520Sstevel@tonic-gate 		if (!(cp->p_flag & SVFORK)) {
12530Sstevel@tonic-gate 			/*
12540Sstevel@tonic-gate 			 * Child has exec()ed or is exit()ing.
12550Sstevel@tonic-gate 			 */
12560Sstevel@tonic-gate 			mutex_exit(&cp->p_lock);
12570Sstevel@tonic-gate 			break;
12580Sstevel@tonic-gate 		}
12590Sstevel@tonic-gate 		mutex_enter(&pp->p_lock);
12600Sstevel@tonic-gate 		mutex_exit(&cp->p_lock);
12610Sstevel@tonic-gate 		/*
12620Sstevel@tonic-gate 		 * We might be waked up spuriously from the cv_wait().
12630Sstevel@tonic-gate 		 * We have to do the whole operation over again to be
12640Sstevel@tonic-gate 		 * sure the child's SVFORK flag really is turned off.
12650Sstevel@tonic-gate 		 * We cannot make reference to the child because it can
12660Sstevel@tonic-gate 		 * exit before we return and we would be referencing
12670Sstevel@tonic-gate 		 * invalid memory.
12680Sstevel@tonic-gate 		 *
12690Sstevel@tonic-gate 		 * Because this is potentially a very long-term wait,
12700Sstevel@tonic-gate 		 * we call cv_wait_sig() (for its jobcontrol and /proc
12710Sstevel@tonic-gate 		 * side-effects) unless there is a current signal, in
12720Sstevel@tonic-gate 		 * which case we use cv_wait() because we cannot return
12730Sstevel@tonic-gate 		 * from this function until the child has released the
12740Sstevel@tonic-gate 		 * address space.  Calling cv_wait_sig() with a current
12750Sstevel@tonic-gate 		 * signal would lead to an indefinite loop here because
12760Sstevel@tonic-gate 		 * cv_wait_sig() returns immediately in this case.
12770Sstevel@tonic-gate 		 */
12780Sstevel@tonic-gate 		if (signalled)
12790Sstevel@tonic-gate 			cv_wait(&pp->p_cv, &pp->p_lock);
12800Sstevel@tonic-gate 		else
12810Sstevel@tonic-gate 			signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock);
12820Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	/* restore watchpoints to parent */
12860Sstevel@tonic-gate 	if (pr_watch_active(pp)) {
12870Sstevel@tonic-gate 		struct as *as = pp->p_as;
12880Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
12890Sstevel@tonic-gate 		as_setwatch(as);
12900Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
12940Sstevel@tonic-gate 	prbarrier(pp);	/* barrier against /proc locking */
12950Sstevel@tonic-gate 	continuelwps(pp);
12960Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
12970Sstevel@tonic-gate }
1298