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