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