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