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