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