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