xref: /titanic_52/usr/src/uts/common/fs/proc/prsubr.c (revision d58fda4376e4bf67072ce2e69f6f47036f9dbb68)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/types.h>
35 #include <sys/t_lock.h>
36 #include <sys/param.h>
37 #include <sys/cmn_err.h>
38 #include <sys/cred.h>
39 #include <sys/priv.h>
40 #include <sys/debug.h>
41 #include <sys/errno.h>
42 #include <sys/inline.h>
43 #include <sys/kmem.h>
44 #include <sys/mman.h>
45 #include <sys/proc.h>
46 #include <sys/sobject.h>
47 #include <sys/sysmacros.h>
48 #include <sys/systm.h>
49 #include <sys/uio.h>
50 #include <sys/var.h>
51 #include <sys/vfs.h>
52 #include <sys/vnode.h>
53 #include <sys/session.h>
54 #include <sys/pcb.h>
55 #include <sys/signal.h>
56 #include <sys/user.h>
57 #include <sys/disp.h>
58 #include <sys/class.h>
59 #include <sys/ts.h>
60 #include <sys/bitmap.h>
61 #include <sys/poll.h>
62 #include <sys/shm_impl.h>
63 #include <sys/fault.h>
64 #include <sys/syscall.h>
65 #include <sys/procfs.h>
66 #include <sys/processor.h>
67 #include <sys/cpuvar.h>
68 #include <sys/copyops.h>
69 #include <sys/time.h>
70 #include <sys/msacct.h>
71 #include <vm/as.h>
72 #include <vm/rm.h>
73 #include <vm/seg.h>
74 #include <vm/seg_vn.h>
75 #include <vm/seg_dev.h>
76 #include <vm/seg_spt.h>
77 #include <vm/page.h>
78 #include <sys/vmparam.h>
79 #include <sys/swap.h>
80 #include <fs/proc/prdata.h>
81 #include <sys/task.h>
82 #include <sys/project.h>
83 #include <sys/contract_impl.h>
84 #include <sys/contract/process.h>
85 #include <sys/contract/process_impl.h>
86 #include <sys/schedctl.h>
87 #include <sys/pool.h>
88 #include <sys/zone.h>
89 #include <sys/atomic.h>
90 
91 #define	MAX_ITERS_SPIN	5
92 
93 typedef struct prpagev {
94 	uint_t *pg_protv;	/* vector of page permissions */
95 	char *pg_incore;	/* vector of incore flags */
96 	size_t pg_npages;	/* number of pages in protv and incore */
97 	ulong_t pg_pnbase;	/* pn within segment of first protv element */
98 } prpagev_t;
99 
100 size_t pagev_lim = 256 * 1024;	/* limit on number of pages in prpagev_t */
101 
102 extern struct seg_ops segdev_ops;	/* needs a header file */
103 extern struct seg_ops segspt_shmops;	/* needs a header file */
104 
105 static	int	set_watched_page(proc_t *, caddr_t, caddr_t, ulong_t, ulong_t);
106 static	void	clear_watched_page(proc_t *, caddr_t, caddr_t, ulong_t);
107 
108 /*
109  * Choose an lwp from the complete set of lwps for the process.
110  * This is called for any operation applied to the process
111  * file descriptor that requires an lwp to operate upon.
112  *
113  * Returns a pointer to the thread for the selected LWP,
114  * and with the dispatcher lock held for the thread.
115  *
116  * The algorithm for choosing an lwp is critical for /proc semantics;
117  * don't touch this code unless you know all of the implications.
118  */
119 kthread_t *
120 prchoose(proc_t *p)
121 {
122 	kthread_t *t;
123 	kthread_t *t_onproc = NULL;	/* running on processor */
124 	kthread_t *t_run = NULL;	/* runnable, on disp queue */
125 	kthread_t *t_sleep = NULL;	/* sleeping */
126 	kthread_t *t_hold = NULL;	/* sleeping, performing hold */
127 	kthread_t *t_susp = NULL;	/* suspended stop */
128 	kthread_t *t_jstop = NULL;	/* jobcontrol stop, w/o directed stop */
129 	kthread_t *t_jdstop = NULL;	/* jobcontrol stop with directed stop */
130 	kthread_t *t_req = NULL;	/* requested stop */
131 	kthread_t *t_istop = NULL;	/* event-of-interest stop */
132 
133 	ASSERT(MUTEX_HELD(&p->p_lock));
134 
135 	/*
136 	 * If the agent lwp exists, it takes precedence over all others.
137 	 */
138 	if ((t = p->p_agenttp) != NULL) {
139 		thread_lock(t);
140 		return (t);
141 	}
142 
143 	if ((t = p->p_tlist) == NULL)	/* start at the head of the list */
144 		return (t);
145 	do {		/* for eacn lwp in the process */
146 		if (VSTOPPED(t)) {	/* virtually stopped */
147 			if (t_req == NULL)
148 				t_req = t;
149 			continue;
150 		}
151 
152 		thread_lock(t);		/* make sure thread is in good state */
153 		switch (t->t_state) {
154 		default:
155 			panic("prchoose: bad thread state %d, thread 0x%p",
156 			    t->t_state, (void *)t);
157 			/*NOTREACHED*/
158 		case TS_SLEEP:
159 			/* this is filthy */
160 			if (t->t_wchan == (caddr_t)&p->p_holdlwps &&
161 			    t->t_wchan0 == NULL) {
162 				if (t_hold == NULL)
163 					t_hold = t;
164 			} else {
165 				if (t_sleep == NULL)
166 					t_sleep = t;
167 			}
168 			break;
169 		case TS_RUN:
170 			if (t_run == NULL)
171 				t_run = t;
172 			break;
173 		case TS_ONPROC:
174 			if (t_onproc == NULL)
175 				t_onproc = t;
176 			break;
177 		case TS_ZOMB:		/* last possible choice */
178 			break;
179 		case TS_STOPPED:
180 			switch (t->t_whystop) {
181 			case PR_SUSPENDED:
182 				if (t_susp == NULL)
183 					t_susp = t;
184 				break;
185 			case PR_JOBCONTROL:
186 				if (t->t_proc_flag & TP_PRSTOP) {
187 					if (t_jdstop == NULL)
188 						t_jdstop = t;
189 				} else {
190 					if (t_jstop == NULL)
191 						t_jstop = t;
192 				}
193 				break;
194 			case PR_REQUESTED:
195 				if (t_req == NULL)
196 					t_req = t;
197 				break;
198 			case PR_SYSENTRY:
199 			case PR_SYSEXIT:
200 			case PR_SIGNALLED:
201 			case PR_FAULTED:
202 				/*
203 				 * Make an lwp calling exit() be the
204 				 * last lwp seen in the process.
205 				 */
206 				if (t_istop == NULL ||
207 				    (t_istop->t_whystop == PR_SYSENTRY &&
208 				    t_istop->t_whatstop == SYS_exit))
209 					t_istop = t;
210 				break;
211 			case PR_CHECKPOINT:	/* can't happen? */
212 				break;
213 			default:
214 				panic("prchoose: bad t_whystop %d, thread 0x%p",
215 				    t->t_whystop, (void *)t);
216 				/*NOTREACHED*/
217 			}
218 			break;
219 		}
220 		thread_unlock(t);
221 	} while ((t = t->t_forw) != p->p_tlist);
222 
223 	if (t_onproc)
224 		t = t_onproc;
225 	else if (t_run)
226 		t = t_run;
227 	else if (t_sleep)
228 		t = t_sleep;
229 	else if (t_jstop)
230 		t = t_jstop;
231 	else if (t_jdstop)
232 		t = t_jdstop;
233 	else if (t_istop)
234 		t = t_istop;
235 	else if (t_req)
236 		t = t_req;
237 	else if (t_hold)
238 		t = t_hold;
239 	else if (t_susp)
240 		t = t_susp;
241 	else			/* TS_ZOMB */
242 		t = p->p_tlist;
243 
244 	if (t != NULL)
245 		thread_lock(t);
246 	return (t);
247 }
248 
249 /*
250  * Wakeup anyone sleeping on the /proc vnode for the process/lwp to stop.
251  * Also call pollwakeup() if any lwps are waiting in poll() for POLLPRI
252  * on the /proc file descriptor.  Called from stop() when a traced
253  * process stops on an event of interest.  Also called from exit()
254  * and prinvalidate() to indicate POLLHUP and POLLERR respectively.
255  */
256 void
257 prnotify(struct vnode *vp)
258 {
259 	prcommon_t *pcp = VTOP(vp)->pr_common;
260 
261 	mutex_enter(&pcp->prc_mutex);
262 	cv_broadcast(&pcp->prc_wait);
263 	mutex_exit(&pcp->prc_mutex);
264 	if (pcp->prc_flags & PRC_POLL) {
265 		/*
266 		 * We call pollwakeup() with POLLHUP to ensure that
267 		 * the pollers are awakened even if they are polling
268 		 * for nothing (i.e., waiting for the process to exit).
269 		 * This enables the use of the PRC_POLL flag for optimization
270 		 * (we can turn off PRC_POLL only if we know no pollers remain).
271 		 */
272 		pcp->prc_flags &= ~PRC_POLL;
273 		pollwakeup(&pcp->prc_pollhead, POLLHUP);
274 	}
275 }
276 
277 /* called immediately below, in prfree() */
278 static void
279 prfreenotify(vnode_t *vp)
280 {
281 	prnode_t *pnp;
282 	prcommon_t *pcp;
283 
284 	while (vp != NULL) {
285 		pnp = VTOP(vp);
286 		pcp = pnp->pr_common;
287 		ASSERT(pcp->prc_thread == NULL);
288 		pcp->prc_proc = NULL;
289 		/*
290 		 * We can't call prnotify() here because we are holding
291 		 * pidlock.  We assert that there is no need to.
292 		 */
293 		mutex_enter(&pcp->prc_mutex);
294 		cv_broadcast(&pcp->prc_wait);
295 		mutex_exit(&pcp->prc_mutex);
296 		ASSERT(!(pcp->prc_flags & PRC_POLL));
297 
298 		vp = pnp->pr_next;
299 		pnp->pr_next = NULL;
300 	}
301 }
302 
303 /*
304  * Called from a hook in freeproc() when a traced process is removed
305  * from the process table.  The proc-table pointers of all associated
306  * /proc vnodes are cleared to indicate that the process has gone away.
307  */
308 void
309 prfree(proc_t *p)
310 {
311 	uint_t slot = p->p_slot;
312 
313 	ASSERT(MUTEX_HELD(&pidlock));
314 
315 	/*
316 	 * Block the process against /proc so it can be freed.
317 	 * It cannot be freed while locked by some controlling process.
318 	 * Lock ordering:
319 	 *	pidlock -> pr_pidlock -> p->p_lock -> pcp->prc_mutex
320 	 */
321 	mutex_enter(&pr_pidlock);	/* protects pcp->prc_proc */
322 	mutex_enter(&p->p_lock);
323 	while (p->p_proc_flag & P_PR_LOCK) {
324 		mutex_exit(&pr_pidlock);
325 		cv_wait(&pr_pid_cv[slot], &p->p_lock);
326 		mutex_exit(&p->p_lock);
327 		mutex_enter(&pr_pidlock);
328 		mutex_enter(&p->p_lock);
329 	}
330 
331 	ASSERT(p->p_tlist == NULL);
332 
333 	prfreenotify(p->p_plist);
334 	p->p_plist = NULL;
335 
336 	prfreenotify(p->p_trace);
337 	p->p_trace = NULL;
338 
339 	/*
340 	 * We broadcast to wake up everyone waiting for this process.
341 	 * No one can reach this process from this point on.
342 	 */
343 	cv_broadcast(&pr_pid_cv[slot]);
344 
345 	mutex_exit(&p->p_lock);
346 	mutex_exit(&pr_pidlock);
347 }
348 
349 /*
350  * Called from a hook in exit() when a traced process is becoming a zombie.
351  */
352 void
353 prexit(proc_t *p)
354 {
355 	ASSERT(MUTEX_HELD(&p->p_lock));
356 
357 	if (pr_watch_active(p)) {
358 		pr_free_watchpoints(p);
359 		watch_disable(curthread);
360 	}
361 	/* pr_free_watched_pages() is called in exit(), after dropping p_lock */
362 	if (p->p_trace) {
363 		VTOP(p->p_trace)->pr_common->prc_flags |= PRC_DESTROY;
364 		prnotify(p->p_trace);
365 	}
366 	cv_broadcast(&pr_pid_cv[p->p_slot]);	/* pauselwps() */
367 }
368 
369 /*
370  * Called when a thread calls lwp_exit().
371  */
372 void
373 prlwpexit(kthread_t *t)
374 {
375 	vnode_t *vp;
376 	prnode_t *pnp;
377 	prcommon_t *pcp;
378 	proc_t *p = ttoproc(t);
379 	lwpent_t *lep = p->p_lwpdir[t->t_dslot].ld_entry;
380 
381 	ASSERT(t == curthread);
382 	ASSERT(MUTEX_HELD(&p->p_lock));
383 
384 	/*
385 	 * The process must be blocked against /proc to do this safely.
386 	 * The lwp must not disappear while the process is marked P_PR_LOCK.
387 	 * It is the caller's responsibility to have called prbarrier(p).
388 	 */
389 	ASSERT(!(p->p_proc_flag & P_PR_LOCK));
390 
391 	for (vp = p->p_plist; vp != NULL; vp = pnp->pr_next) {
392 		pnp = VTOP(vp);
393 		pcp = pnp->pr_common;
394 		if (pcp->prc_thread == t) {
395 			pcp->prc_thread = NULL;
396 			pcp->prc_flags |= PRC_DESTROY;
397 		}
398 	}
399 
400 	for (vp = lep->le_trace; vp != NULL; vp = pnp->pr_next) {
401 		pnp = VTOP(vp);
402 		pcp = pnp->pr_common;
403 		pcp->prc_thread = NULL;
404 		pcp->prc_flags |= PRC_DESTROY;
405 		prnotify(vp);
406 	}
407 
408 	if (p->p_trace)
409 		prnotify(p->p_trace);
410 }
411 
412 /*
413  * Called when a zombie thread is joined or when a
414  * detached lwp exits.  Called from lwp_hash_out().
415  */
416 void
417 prlwpfree(proc_t *p, lwpent_t *lep)
418 {
419 	vnode_t *vp;
420 	prnode_t *pnp;
421 	prcommon_t *pcp;
422 
423 	ASSERT(MUTEX_HELD(&p->p_lock));
424 
425 	/*
426 	 * The process must be blocked against /proc to do this safely.
427 	 * The lwp must not disappear while the process is marked P_PR_LOCK.
428 	 * It is the caller's responsibility to have called prbarrier(p).
429 	 */
430 	ASSERT(!(p->p_proc_flag & P_PR_LOCK));
431 
432 	vp = lep->le_trace;
433 	lep->le_trace = NULL;
434 	while (vp) {
435 		prnotify(vp);
436 		pnp = VTOP(vp);
437 		pcp = pnp->pr_common;
438 		ASSERT(pcp->prc_thread == NULL &&
439 		    (pcp->prc_flags & PRC_DESTROY));
440 		pcp->prc_tslot = -1;
441 		vp = pnp->pr_next;
442 		pnp->pr_next = NULL;
443 	}
444 
445 	if (p->p_trace)
446 		prnotify(p->p_trace);
447 }
448 
449 /*
450  * Called from a hook in exec() when a thread starts exec().
451  */
452 void
453 prexecstart(void)
454 {
455 	proc_t *p = ttoproc(curthread);
456 	klwp_t *lwp = ttolwp(curthread);
457 
458 	/*
459 	 * The P_PR_EXEC flag blocks /proc operations for
460 	 * the duration of the exec().
461 	 * We can't start exec() while the process is
462 	 * locked by /proc, so we call prbarrier().
463 	 * lwp_nostop keeps the process from being stopped
464 	 * via job control for the duration of the exec().
465 	 */
466 
467 	ASSERT(MUTEX_HELD(&p->p_lock));
468 	prbarrier(p);
469 	lwp->lwp_nostop++;
470 	p->p_proc_flag |= P_PR_EXEC;
471 }
472 
473 /*
474  * Called from a hook in exec() when a thread finishes exec().
475  * The thread may or may not have succeeded.  Some other thread
476  * may have beat it to the punch.
477  */
478 void
479 prexecend(void)
480 {
481 	proc_t *p = ttoproc(curthread);
482 	klwp_t *lwp = ttolwp(curthread);
483 	vnode_t *vp;
484 	prnode_t *pnp;
485 	prcommon_t *pcp;
486 	model_t model = p->p_model;
487 	id_t tid = curthread->t_tid;
488 	int tslot = curthread->t_dslot;
489 
490 	ASSERT(MUTEX_HELD(&p->p_lock));
491 
492 	lwp->lwp_nostop--;
493 	if (p->p_flag & SEXITLWPS) {
494 		/*
495 		 * We are on our way to exiting because some
496 		 * other thread beat us in the race to exec().
497 		 * Don't clear the P_PR_EXEC flag in this case.
498 		 */
499 		return;
500 	}
501 
502 	/*
503 	 * Wake up anyone waiting in /proc for the process to complete exec().
504 	 */
505 	p->p_proc_flag &= ~P_PR_EXEC;
506 	if ((vp = p->p_trace) != NULL) {
507 		pcp = VTOP(vp)->pr_common;
508 		mutex_enter(&pcp->prc_mutex);
509 		cv_broadcast(&pcp->prc_wait);
510 		mutex_exit(&pcp->prc_mutex);
511 		for (; vp != NULL; vp = pnp->pr_next) {
512 			pnp = VTOP(vp);
513 			pnp->pr_common->prc_datamodel = model;
514 		}
515 	}
516 	if ((vp = p->p_lwpdir[tslot].ld_entry->le_trace) != NULL) {
517 		/*
518 		 * We dealt with the process common above.
519 		 */
520 		ASSERT(p->p_trace != NULL);
521 		pcp = VTOP(vp)->pr_common;
522 		mutex_enter(&pcp->prc_mutex);
523 		cv_broadcast(&pcp->prc_wait);
524 		mutex_exit(&pcp->prc_mutex);
525 		for (; vp != NULL; vp = pnp->pr_next) {
526 			pnp = VTOP(vp);
527 			pcp = pnp->pr_common;
528 			pcp->prc_datamodel = model;
529 			pcp->prc_tid = tid;
530 			pcp->prc_tslot = tslot;
531 		}
532 	}
533 }
534 
535 /*
536  * Called from a hook in relvm() just before freeing the address space.
537  * We free all the watched areas now.
538  */
539 void
540 prrelvm(void)
541 {
542 	proc_t *p = ttoproc(curthread);
543 
544 	mutex_enter(&p->p_lock);
545 	prbarrier(p);	/* block all other /proc operations */
546 	if (pr_watch_active(p)) {
547 		pr_free_watchpoints(p);
548 		watch_disable(curthread);
549 	}
550 	mutex_exit(&p->p_lock);
551 	pr_free_watched_pages(p);
552 }
553 
554 /*
555  * Called from hooks in exec-related code when a traced process
556  * attempts to exec(2) a setuid/setgid program or an unreadable
557  * file.  Rather than fail the exec we invalidate the associated
558  * /proc vnodes so that subsequent attempts to use them will fail.
559  *
560  * All /proc vnodes, except directory vnodes, are retained on a linked
561  * list (rooted at p_plist in the process structure) until last close.
562  *
563  * A controlling process must re-open the /proc files in order to
564  * regain control.
565  */
566 void
567 prinvalidate(struct user *up)
568 {
569 	kthread_t *t = curthread;
570 	proc_t *p = ttoproc(t);
571 	vnode_t *vp;
572 	prnode_t *pnp;
573 	int writers = 0;
574 
575 	mutex_enter(&p->p_lock);
576 	prbarrier(p);	/* block all other /proc operations */
577 
578 	/*
579 	 * At this moment, there can be only one lwp in the process.
580 	 */
581 	ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0);
582 
583 	/*
584 	 * Invalidate any currently active /proc vnodes.
585 	 */
586 	for (vp = p->p_plist; vp != NULL; vp = pnp->pr_next) {
587 		pnp = VTOP(vp);
588 		switch (pnp->pr_type) {
589 		case PR_PSINFO:		/* these files can read by anyone */
590 		case PR_LPSINFO:
591 		case PR_LWPSINFO:
592 		case PR_LWPDIR:
593 		case PR_LWPIDDIR:
594 		case PR_USAGE:
595 		case PR_LUSAGE:
596 		case PR_LWPUSAGE:
597 			break;
598 		default:
599 			pnp->pr_flags |= PR_INVAL;
600 			break;
601 		}
602 	}
603 	/*
604 	 * Wake up anyone waiting for the process or lwp.
605 	 * p->p_trace is guaranteed to be non-NULL if there
606 	 * are any open /proc files for this process.
607 	 */
608 	if ((vp = p->p_trace) != NULL) {
609 		prcommon_t *pcp = VTOP(vp)->pr_pcommon;
610 
611 		prnotify(vp);
612 		/*
613 		 * Are there any writers?
614 		 */
615 		if ((writers = pcp->prc_writers) != 0) {
616 			/*
617 			 * Clear the exclusive open flag (old /proc interface).
618 			 * Set prc_selfopens equal to prc_writers so that
619 			 * the next O_EXCL|O_WRITE open will succeed
620 			 * even with existing (though invalid) writers.
621 			 * prclose() must decrement prc_selfopens when
622 			 * the invalid files are closed.
623 			 */
624 			pcp->prc_flags &= ~PRC_EXCL;
625 			ASSERT(pcp->prc_selfopens <= writers);
626 			pcp->prc_selfopens = writers;
627 		}
628 	}
629 	vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace;
630 	while (vp != NULL) {
631 		/*
632 		 * We should not invalidate the lwpiddir vnodes,
633 		 * but the necessities of maintaining the old
634 		 * ioctl()-based version of /proc require it.
635 		 */
636 		pnp = VTOP(vp);
637 		pnp->pr_flags |= PR_INVAL;
638 		prnotify(vp);
639 		vp = pnp->pr_next;
640 	}
641 
642 	/*
643 	 * If any tracing flags are in effect and any vnodes are open for
644 	 * writing then set the requested-stop and run-on-last-close flags.
645 	 * Otherwise, clear all tracing flags.
646 	 */
647 	t->t_proc_flag &= ~TP_PAUSE;
648 	if ((p->p_proc_flag & P_PR_TRACE) && writers) {
649 		t->t_proc_flag |= TP_PRSTOP;
650 		aston(t);		/* so ISSIG will see the flag */
651 		p->p_proc_flag |= P_PR_RUNLCL;
652 	} else {
653 		premptyset(&up->u_entrymask);		/* syscalls */
654 		premptyset(&up->u_exitmask);
655 		up->u_systrap = 0;
656 		premptyset(&p->p_sigmask);		/* signals */
657 		premptyset(&p->p_fltmask);		/* faults */
658 		t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
659 		p->p_proc_flag &= ~(P_PR_RUNLCL|P_PR_KILLCL|P_PR_TRACE);
660 		prnostep(ttolwp(t));
661 	}
662 
663 	mutex_exit(&p->p_lock);
664 }
665 
666 /*
667  * Acquire the controlled process's p_lock and mark it P_PR_LOCK.
668  * Return with pr_pidlock held in all cases.
669  * Return with p_lock held if the the process still exists.
670  * Return value is the process pointer if the process still exists, else NULL.
671  * If we lock the process, give ourself kernel priority to avoid deadlocks;
672  * this is undone in prunlock().
673  */
674 proc_t *
675 pr_p_lock(prnode_t *pnp)
676 {
677 	proc_t *p;
678 	prcommon_t *pcp;
679 
680 	mutex_enter(&pr_pidlock);
681 	if ((pcp = pnp->pr_pcommon) == NULL || (p = pcp->prc_proc) == NULL)
682 		return (NULL);
683 	mutex_enter(&p->p_lock);
684 	while (p->p_proc_flag & P_PR_LOCK) {
685 		/*
686 		 * This cv/mutex pair is persistent even if
687 		 * the process disappears while we sleep.
688 		 */
689 		kcondvar_t *cv = &pr_pid_cv[p->p_slot];
690 		kmutex_t *mp = &p->p_lock;
691 
692 		mutex_exit(&pr_pidlock);
693 		cv_wait(cv, mp);
694 		mutex_exit(mp);
695 		mutex_enter(&pr_pidlock);
696 		if (pcp->prc_proc == NULL)
697 			return (NULL);
698 		ASSERT(p == pcp->prc_proc);
699 		mutex_enter(&p->p_lock);
700 	}
701 	p->p_proc_flag |= P_PR_LOCK;
702 	THREAD_KPRI_REQUEST();
703 	return (p);
704 }
705 
706 /*
707  * Lock the target process by setting P_PR_LOCK and grabbing p->p_lock.
708  * This prevents any lwp of the process from disappearing and
709  * blocks most operations that a process can perform on itself.
710  * Returns 0 on success, a non-zero error number on failure.
711  *
712  * 'zdisp' is ZYES or ZNO to indicate whether prlock() should succeed when
713  * the subject process is a zombie (ZYES) or fail for zombies (ZNO).
714  *
715  * error returns:
716  *	ENOENT: process or lwp has disappeared or process is exiting
717  *		(or has become a zombie and zdisp == ZNO).
718  *	EAGAIN: procfs vnode has become invalid.
719  *	EINTR:  signal arrived while waiting for exec to complete.
720  */
721 int
722 prlock(prnode_t *pnp, int zdisp)
723 {
724 	prcommon_t *pcp;
725 	proc_t *p;
726 
727 again:
728 	pcp = pnp->pr_common;
729 	p = pr_p_lock(pnp);
730 	mutex_exit(&pr_pidlock);
731 
732 	/*
733 	 * Return ENOENT immediately if there is no process.
734 	 */
735 	if (p == NULL)
736 		return (ENOENT);
737 
738 	ASSERT(p == pcp->prc_proc && p->p_stat != 0 && p->p_stat != SIDL);
739 
740 	/*
741 	 * Return ENOENT if process entered zombie state or is exiting
742 	 * and the 'zdisp' flag is set to ZNO indicating not to lock zombies.
743 	 */
744 	if (zdisp == ZNO &&
745 	    ((pcp->prc_flags & PRC_DESTROY) || (p->p_flag & SEXITING))) {
746 		prunlock(pnp);
747 		return (ENOENT);
748 	}
749 
750 	/*
751 	 * If lwp-specific, check to see if lwp has disappeared.
752 	 */
753 	if (pcp->prc_flags & PRC_LWP) {
754 		if ((zdisp == ZNO && (pcp->prc_flags & PRC_DESTROY)) ||
755 		    pcp->prc_tslot == -1) {
756 			prunlock(pnp);
757 			return (ENOENT);
758 		}
759 	}
760 
761 	/*
762 	 * Return EAGAIN if we have encountered a security violation.
763 	 * (The process exec'd a set-id or unreadable executable file.)
764 	 */
765 	if (pnp->pr_flags & PR_INVAL) {
766 		prunlock(pnp);
767 		return (EAGAIN);
768 	}
769 
770 	/*
771 	 * If process is undergoing an exec(), wait for
772 	 * completion and then start all over again.
773 	 */
774 	if (p->p_proc_flag & P_PR_EXEC) {
775 		pcp = pnp->pr_pcommon;	/* Put on the correct sleep queue */
776 		mutex_enter(&pcp->prc_mutex);
777 		prunlock(pnp);
778 		if (!cv_wait_sig(&pcp->prc_wait, &pcp->prc_mutex)) {
779 			mutex_exit(&pcp->prc_mutex);
780 			return (EINTR);
781 		}
782 		mutex_exit(&pcp->prc_mutex);
783 		goto again;
784 	}
785 
786 	/*
787 	 * We return holding p->p_lock.
788 	 */
789 	return (0);
790 }
791 
792 /*
793  * Undo prlock() and pr_p_lock().
794  * p->p_lock is still held; pr_pidlock is no longer held.
795  *
796  * prunmark() drops the P_PR_LOCK flag and wakes up another thread,
797  * if any, waiting for the flag to be dropped; it retains p->p_lock.
798  *
799  * prunlock() calls prunmark() and then drops p->p_lock.
800  */
801 void
802 prunmark(proc_t *p)
803 {
804 	ASSERT(p->p_proc_flag & P_PR_LOCK);
805 	ASSERT(MUTEX_HELD(&p->p_lock));
806 
807 	cv_signal(&pr_pid_cv[p->p_slot]);
808 	p->p_proc_flag &= ~P_PR_LOCK;
809 	THREAD_KPRI_RELEASE();
810 }
811 
812 void
813 prunlock(prnode_t *pnp)
814 {
815 	prcommon_t *pcp = pnp->pr_common;
816 	proc_t *p = pcp->prc_proc;
817 
818 	/*
819 	 * If we (or someone) gave it a SIGKILL, and it is not
820 	 * already a zombie, set it running unconditionally.
821 	 */
822 	if ((p->p_flag & SKILLED) &&
823 	    !(p->p_flag & SEXITING) &&
824 	    !(pcp->prc_flags & PRC_DESTROY) &&
825 	    !((pcp->prc_flags & PRC_LWP) && pcp->prc_tslot == -1))
826 		(void) pr_setrun(pnp, 0);
827 	prunmark(p);
828 	mutex_exit(&p->p_lock);
829 }
830 
831 /*
832  * Called while holding p->p_lock to delay until the process is unlocked.
833  * We enter holding p->p_lock; p->p_lock is dropped and reacquired.
834  * The process cannot become locked again until p->p_lock is dropped.
835  */
836 void
837 prbarrier(proc_t *p)
838 {
839 	ASSERT(MUTEX_HELD(&p->p_lock));
840 
841 	if (p->p_proc_flag & P_PR_LOCK) {
842 		/* The process is locked; delay until not locked */
843 		uint_t slot = p->p_slot;
844 
845 		while (p->p_proc_flag & P_PR_LOCK)
846 			cv_wait(&pr_pid_cv[slot], &p->p_lock);
847 		cv_signal(&pr_pid_cv[slot]);
848 	}
849 }
850 
851 /*
852  * Return process/lwp status.
853  * The u-block is mapped in by this routine and unmapped at the end.
854  */
855 void
856 prgetstatus(proc_t *p, pstatus_t *sp, zone_t *zp)
857 {
858 	kthread_t *t;
859 
860 	ASSERT(MUTEX_HELD(&p->p_lock));
861 
862 	t = prchoose(p);	/* returns locked thread */
863 	ASSERT(t != NULL);
864 	thread_unlock(t);
865 
866 	/* just bzero the process part, prgetlwpstatus() does the rest */
867 	bzero(sp, sizeof (pstatus_t) - sizeof (lwpstatus_t));
868 	sp->pr_nlwp = p->p_lwpcnt;
869 	sp->pr_nzomb = p->p_zombcnt;
870 	prassignset(&sp->pr_sigpend, &p->p_sig);
871 	sp->pr_brkbase = (uintptr_t)p->p_brkbase;
872 	sp->pr_brksize = p->p_brksize;
873 	sp->pr_stkbase = (uintptr_t)prgetstackbase(p);
874 	sp->pr_stksize = p->p_stksize;
875 	sp->pr_pid = p->p_pid;
876 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
877 	    (p->p_flag & SZONETOP)) {
878 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
879 		/*
880 		 * Inside local zones, fake zsched's pid as parent pids for
881 		 * processes which reference processes outside of the zone.
882 		 */
883 		sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
884 	} else {
885 		sp->pr_ppid = p->p_ppid;
886 	}
887 	sp->pr_pgid  = p->p_pgrp;
888 	sp->pr_sid   = p->p_sessp->s_sid;
889 	sp->pr_taskid = p->p_task->tk_tkid;
890 	sp->pr_projid = p->p_task->tk_proj->kpj_id;
891 	sp->pr_zoneid = p->p_zone->zone_id;
892 	hrt2ts(mstate_aggr_state(p, LMS_USER), &sp->pr_utime);
893 	hrt2ts(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime);
894 	TICK_TO_TIMESTRUC(p->p_cutime, &sp->pr_cutime);
895 	TICK_TO_TIMESTRUC(p->p_cstime, &sp->pr_cstime);
896 	prassignset(&sp->pr_sigtrace, &p->p_sigmask);
897 	prassignset(&sp->pr_flttrace, &p->p_fltmask);
898 	prassignset(&sp->pr_sysentry, &PTOU(p)->u_entrymask);
899 	prassignset(&sp->pr_sysexit, &PTOU(p)->u_exitmask);
900 	switch (p->p_model) {
901 	case DATAMODEL_ILP32:
902 		sp->pr_dmodel = PR_MODEL_ILP32;
903 		break;
904 	case DATAMODEL_LP64:
905 		sp->pr_dmodel = PR_MODEL_LP64;
906 		break;
907 	}
908 	if (p->p_agenttp)
909 		sp->pr_agentid = p->p_agenttp->t_tid;
910 
911 	/* get the chosen lwp's status */
912 	prgetlwpstatus(t, &sp->pr_lwp, zp);
913 
914 	/* replicate the flags */
915 	sp->pr_flags = sp->pr_lwp.pr_flags;
916 }
917 
918 #ifdef _SYSCALL32_IMPL
919 void
920 prgetlwpstatus32(kthread_t *t, lwpstatus32_t *sp, zone_t *zp)
921 {
922 	proc_t *p = ttoproc(t);
923 	klwp_t *lwp = ttolwp(t);
924 	struct mstate *ms = &lwp->lwp_mstate;
925 	hrtime_t usr, sys;
926 	int flags;
927 	ulong_t instr;
928 
929 	ASSERT(MUTEX_HELD(&p->p_lock));
930 
931 	bzero(sp, sizeof (*sp));
932 	flags = 0L;
933 	if (t->t_state == TS_STOPPED) {
934 		flags |= PR_STOPPED;
935 		if ((t->t_schedflag & TS_PSTART) == 0)
936 			flags |= PR_ISTOP;
937 	} else if (VSTOPPED(t)) {
938 		flags |= PR_STOPPED|PR_ISTOP;
939 	}
940 	if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP))
941 		flags |= PR_DSTOP;
942 	if (lwp->lwp_asleep)
943 		flags |= PR_ASLEEP;
944 	if (t == p->p_agenttp)
945 		flags |= PR_AGENT;
946 	if (!(t->t_proc_flag & TP_TWAIT))
947 		flags |= PR_DETACH;
948 	if (t->t_proc_flag & TP_DAEMON)
949 		flags |= PR_DAEMON;
950 	if (p->p_proc_flag & P_PR_FORK)
951 		flags |= PR_FORK;
952 	if (p->p_proc_flag & P_PR_RUNLCL)
953 		flags |= PR_RLC;
954 	if (p->p_proc_flag & P_PR_KILLCL)
955 		flags |= PR_KLC;
956 	if (p->p_proc_flag & P_PR_ASYNC)
957 		flags |= PR_ASYNC;
958 	if (p->p_proc_flag & P_PR_BPTADJ)
959 		flags |= PR_BPTADJ;
960 	if (p->p_proc_flag & P_PR_PTRACE)
961 		flags |= PR_PTRACE;
962 	if (p->p_flag & SMSACCT)
963 		flags |= PR_MSACCT;
964 	if (p->p_flag & SMSFORK)
965 		flags |= PR_MSFORK;
966 	if (p->p_flag & SVFWAIT)
967 		flags |= PR_VFORKP;
968 	sp->pr_flags = flags;
969 	if (VSTOPPED(t)) {
970 		sp->pr_why   = PR_REQUESTED;
971 		sp->pr_what  = 0;
972 	} else {
973 		sp->pr_why   = t->t_whystop;
974 		sp->pr_what  = t->t_whatstop;
975 	}
976 	sp->pr_lwpid = t->t_tid;
977 	sp->pr_cursig  = lwp->lwp_cursig;
978 	prassignset(&sp->pr_lwppend, &t->t_sig);
979 	schedctl_finish_sigblock(t);
980 	prassignset(&sp->pr_lwphold, &t->t_hold);
981 	if (t->t_whystop == PR_FAULTED) {
982 		siginfo_kto32(&lwp->lwp_siginfo, &sp->pr_info);
983 		if (t->t_whatstop == FLTPAGE)
984 			sp->pr_info.si_addr =
985 			    (caddr32_t)(uintptr_t)lwp->lwp_siginfo.si_addr;
986 	} else if (lwp->lwp_curinfo)
987 		siginfo_kto32(&lwp->lwp_curinfo->sq_info, &sp->pr_info);
988 	if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID &&
989 	    sp->pr_info.si_zoneid != zp->zone_id) {
990 		sp->pr_info.si_pid = zp->zone_zsched->p_pid;
991 		sp->pr_info.si_uid = 0;
992 		sp->pr_info.si_ctid = -1;
993 		sp->pr_info.si_zoneid = zp->zone_id;
994 	}
995 	sp->pr_altstack.ss_sp =
996 	    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
997 	sp->pr_altstack.ss_size = (size32_t)lwp->lwp_sigaltstack.ss_size;
998 	sp->pr_altstack.ss_flags = (int32_t)lwp->lwp_sigaltstack.ss_flags;
999 	prgetaction32(p, PTOU(p), lwp->lwp_cursig, &sp->pr_action);
1000 	sp->pr_oldcontext = (caddr32_t)lwp->lwp_oldcontext;
1001 	sp->pr_ustack = (caddr32_t)lwp->lwp_ustack;
1002 	(void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name,
1003 		sizeof (sp->pr_clname) - 1);
1004 	if (flags & PR_STOPPED)
1005 		hrt2ts32(t->t_stoptime, &sp->pr_tstamp);
1006 	usr = ms->ms_acct[LMS_USER];
1007 	sys = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
1008 	scalehrtime(&usr);
1009 	scalehrtime(&sys);
1010 	hrt2ts32(usr, &sp->pr_utime);
1011 	hrt2ts32(sys, &sp->pr_stime);
1012 
1013 	/*
1014 	 * Fetch the current instruction, if not a system process.
1015 	 * We don't attempt this unless the lwp is stopped.
1016 	 */
1017 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1018 		sp->pr_flags |= (PR_ISSYS|PR_PCINVAL);
1019 	else if (!(flags & PR_STOPPED))
1020 		sp->pr_flags |= PR_PCINVAL;
1021 	else if (!prfetchinstr(lwp, &instr))
1022 		sp->pr_flags |= PR_PCINVAL;
1023 	else
1024 		sp->pr_instr = (uint32_t)instr;
1025 
1026 	/*
1027 	 * Drop p_lock while touching the lwp's stack.
1028 	 */
1029 	mutex_exit(&p->p_lock);
1030 	if (prisstep(lwp))
1031 		sp->pr_flags |= PR_STEP;
1032 	if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) {
1033 		int i;
1034 
1035 		sp->pr_syscall = get_syscall32_args(lwp,
1036 			(int *)sp->pr_sysarg, &i);
1037 		sp->pr_nsysarg = (ushort_t)i;
1038 	}
1039 	if ((flags & PR_STOPPED) || t == curthread)
1040 		prgetprregs32(lwp, sp->pr_reg);
1041 	if ((t->t_state == TS_STOPPED && t->t_whystop == PR_SYSEXIT) ||
1042 	    (flags & PR_VFORKP)) {
1043 		long r1, r2;
1044 		user_t *up;
1045 		auxv_t *auxp;
1046 		int i;
1047 
1048 		sp->pr_errno = prgetrvals(lwp, &r1, &r2);
1049 		if (sp->pr_errno == 0) {
1050 			sp->pr_rval1 = (int32_t)r1;
1051 			sp->pr_rval2 = (int32_t)r2;
1052 			sp->pr_errpriv = PRIV_NONE;
1053 		} else
1054 			sp->pr_errpriv = lwp->lwp_badpriv;
1055 
1056 		if (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve) {
1057 			up = PTOU(p);
1058 			sp->pr_sysarg[0] = 0;
1059 			sp->pr_sysarg[1] = (caddr32_t)up->u_argv;
1060 			sp->pr_sysarg[2] = (caddr32_t)up->u_envp;
1061 			for (i = 0, auxp = up->u_auxv;
1062 			    i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]);
1063 			    i++, auxp++) {
1064 				if (auxp->a_type == AT_SUN_EXECNAME) {
1065 					sp->pr_sysarg[0] =
1066 					(caddr32_t)(uintptr_t)auxp->a_un.a_ptr;
1067 					break;
1068 				}
1069 			}
1070 		}
1071 	}
1072 	if (prhasfp())
1073 		prgetprfpregs32(lwp, &sp->pr_fpreg);
1074 	mutex_enter(&p->p_lock);
1075 }
1076 
1077 void
1078 prgetstatus32(proc_t *p, pstatus32_t *sp, zone_t *zp)
1079 {
1080 	kthread_t *t;
1081 
1082 	ASSERT(MUTEX_HELD(&p->p_lock));
1083 
1084 	t = prchoose(p);	/* returns locked thread */
1085 	ASSERT(t != NULL);
1086 	thread_unlock(t);
1087 
1088 	/* just bzero the process part, prgetlwpstatus32() does the rest */
1089 	bzero(sp, sizeof (pstatus32_t) - sizeof (lwpstatus32_t));
1090 	sp->pr_nlwp = p->p_lwpcnt;
1091 	sp->pr_nzomb = p->p_zombcnt;
1092 	prassignset(&sp->pr_sigpend, &p->p_sig);
1093 	sp->pr_brkbase = (uint32_t)(uintptr_t)p->p_brkbase;
1094 	sp->pr_brksize = (uint32_t)p->p_brksize;
1095 	sp->pr_stkbase = (uint32_t)(uintptr_t)prgetstackbase(p);
1096 	sp->pr_stksize = (uint32_t)p->p_stksize;
1097 	sp->pr_pid   = p->p_pid;
1098 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
1099 	    (p->p_flag & SZONETOP)) {
1100 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
1101 		/*
1102 		 * Inside local zones, fake zsched's pid as parent pids for
1103 		 * processes which reference processes outside of the zone.
1104 		 */
1105 		sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
1106 	} else {
1107 		sp->pr_ppid = p->p_ppid;
1108 	}
1109 	sp->pr_pgid  = p->p_pgrp;
1110 	sp->pr_sid   = p->p_sessp->s_sid;
1111 	sp->pr_taskid = p->p_task->tk_tkid;
1112 	sp->pr_projid = p->p_task->tk_proj->kpj_id;
1113 	sp->pr_zoneid = p->p_zone->zone_id;
1114 	hrt2ts32(mstate_aggr_state(p, LMS_USER), &sp->pr_utime);
1115 	hrt2ts32(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime);
1116 	TICK_TO_TIMESTRUC32(p->p_cutime, &sp->pr_cutime);
1117 	TICK_TO_TIMESTRUC32(p->p_cstime, &sp->pr_cstime);
1118 	prassignset(&sp->pr_sigtrace, &p->p_sigmask);
1119 	prassignset(&sp->pr_flttrace, &p->p_fltmask);
1120 	prassignset(&sp->pr_sysentry, &PTOU(p)->u_entrymask);
1121 	prassignset(&sp->pr_sysexit, &PTOU(p)->u_exitmask);
1122 	switch (p->p_model) {
1123 	case DATAMODEL_ILP32:
1124 		sp->pr_dmodel = PR_MODEL_ILP32;
1125 		break;
1126 	case DATAMODEL_LP64:
1127 		sp->pr_dmodel = PR_MODEL_LP64;
1128 		break;
1129 	}
1130 	if (p->p_agenttp)
1131 		sp->pr_agentid = p->p_agenttp->t_tid;
1132 
1133 	/* get the chosen lwp's status */
1134 	prgetlwpstatus32(t, &sp->pr_lwp, zp);
1135 
1136 	/* replicate the flags */
1137 	sp->pr_flags = sp->pr_lwp.pr_flags;
1138 }
1139 #endif	/* _SYSCALL32_IMPL */
1140 
1141 /*
1142  * Return lwp status.
1143  */
1144 void
1145 prgetlwpstatus(kthread_t *t, lwpstatus_t *sp, zone_t *zp)
1146 {
1147 	proc_t *p = ttoproc(t);
1148 	klwp_t *lwp = ttolwp(t);
1149 	struct mstate *ms = &lwp->lwp_mstate;
1150 	hrtime_t usr, sys;
1151 	int flags;
1152 	ulong_t instr;
1153 
1154 	ASSERT(MUTEX_HELD(&p->p_lock));
1155 
1156 	bzero(sp, sizeof (*sp));
1157 	flags = 0L;
1158 	if (t->t_state == TS_STOPPED) {
1159 		flags |= PR_STOPPED;
1160 		if ((t->t_schedflag & TS_PSTART) == 0)
1161 			flags |= PR_ISTOP;
1162 	} else if (VSTOPPED(t)) {
1163 		flags |= PR_STOPPED|PR_ISTOP;
1164 	}
1165 	if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP))
1166 		flags |= PR_DSTOP;
1167 	if (lwp->lwp_asleep)
1168 		flags |= PR_ASLEEP;
1169 	if (t == p->p_agenttp)
1170 		flags |= PR_AGENT;
1171 	if (!(t->t_proc_flag & TP_TWAIT))
1172 		flags |= PR_DETACH;
1173 	if (t->t_proc_flag & TP_DAEMON)
1174 		flags |= PR_DAEMON;
1175 	if (p->p_proc_flag & P_PR_FORK)
1176 		flags |= PR_FORK;
1177 	if (p->p_proc_flag & P_PR_RUNLCL)
1178 		flags |= PR_RLC;
1179 	if (p->p_proc_flag & P_PR_KILLCL)
1180 		flags |= PR_KLC;
1181 	if (p->p_proc_flag & P_PR_ASYNC)
1182 		flags |= PR_ASYNC;
1183 	if (p->p_proc_flag & P_PR_BPTADJ)
1184 		flags |= PR_BPTADJ;
1185 	if (p->p_proc_flag & P_PR_PTRACE)
1186 		flags |= PR_PTRACE;
1187 	if (p->p_flag & SMSACCT)
1188 		flags |= PR_MSACCT;
1189 	if (p->p_flag & SMSFORK)
1190 		flags |= PR_MSFORK;
1191 	if (p->p_flag & SVFWAIT)
1192 		flags |= PR_VFORKP;
1193 	if (p->p_pgidp->pid_pgorphaned)
1194 		flags |= PR_ORPHAN;
1195 	sp->pr_flags = flags;
1196 	if (VSTOPPED(t)) {
1197 		sp->pr_why   = PR_REQUESTED;
1198 		sp->pr_what  = 0;
1199 	} else {
1200 		sp->pr_why   = t->t_whystop;
1201 		sp->pr_what  = t->t_whatstop;
1202 	}
1203 	sp->pr_lwpid = t->t_tid;
1204 	sp->pr_cursig  = lwp->lwp_cursig;
1205 	prassignset(&sp->pr_lwppend, &t->t_sig);
1206 	schedctl_finish_sigblock(t);
1207 	prassignset(&sp->pr_lwphold, &t->t_hold);
1208 	if (t->t_whystop == PR_FAULTED)
1209 		bcopy(&lwp->lwp_siginfo,
1210 		    &sp->pr_info, sizeof (k_siginfo_t));
1211 	else if (lwp->lwp_curinfo)
1212 		bcopy(&lwp->lwp_curinfo->sq_info,
1213 		    &sp->pr_info, sizeof (k_siginfo_t));
1214 	if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID &&
1215 	    sp->pr_info.si_zoneid != zp->zone_id) {
1216 		sp->pr_info.si_pid = zp->zone_zsched->p_pid;
1217 		sp->pr_info.si_uid = 0;
1218 		sp->pr_info.si_ctid = -1;
1219 		sp->pr_info.si_zoneid = zp->zone_id;
1220 	}
1221 	sp->pr_altstack = lwp->lwp_sigaltstack;
1222 	prgetaction(p, PTOU(p), lwp->lwp_cursig, &sp->pr_action);
1223 	sp->pr_oldcontext = (uintptr_t)lwp->lwp_oldcontext;
1224 	sp->pr_ustack = lwp->lwp_ustack;
1225 	(void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name,
1226 		sizeof (sp->pr_clname) - 1);
1227 	if (flags & PR_STOPPED)
1228 		hrt2ts(t->t_stoptime, &sp->pr_tstamp);
1229 	usr = ms->ms_acct[LMS_USER];
1230 	sys = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
1231 	scalehrtime(&usr);
1232 	scalehrtime(&sys);
1233 	hrt2ts(usr, &sp->pr_utime);
1234 	hrt2ts(sys, &sp->pr_stime);
1235 
1236 	/*
1237 	 * Fetch the current instruction, if not a system process.
1238 	 * We don't attempt this unless the lwp is stopped.
1239 	 */
1240 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1241 		sp->pr_flags |= (PR_ISSYS|PR_PCINVAL);
1242 	else if (!(flags & PR_STOPPED))
1243 		sp->pr_flags |= PR_PCINVAL;
1244 	else if (!prfetchinstr(lwp, &instr))
1245 		sp->pr_flags |= PR_PCINVAL;
1246 	else
1247 		sp->pr_instr = instr;
1248 
1249 	/*
1250 	 * Drop p_lock while touching the lwp's stack.
1251 	 */
1252 	mutex_exit(&p->p_lock);
1253 	if (prisstep(lwp))
1254 		sp->pr_flags |= PR_STEP;
1255 	if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) {
1256 		int i;
1257 
1258 		sp->pr_syscall = get_syscall_args(lwp,
1259 			(long *)sp->pr_sysarg, &i);
1260 		sp->pr_nsysarg = (ushort_t)i;
1261 	}
1262 	if ((flags & PR_STOPPED) || t == curthread)
1263 		prgetprregs(lwp, sp->pr_reg);
1264 	if ((t->t_state == TS_STOPPED && t->t_whystop == PR_SYSEXIT) ||
1265 	    (flags & PR_VFORKP)) {
1266 		user_t *up;
1267 		auxv_t *auxp;
1268 		int i;
1269 
1270 		sp->pr_errno = prgetrvals(lwp, &sp->pr_rval1, &sp->pr_rval2);
1271 		if (sp->pr_errno == 0)
1272 			sp->pr_errpriv = PRIV_NONE;
1273 		else
1274 			sp->pr_errpriv = lwp->lwp_badpriv;
1275 
1276 		if (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve) {
1277 			up = PTOU(p);
1278 			sp->pr_sysarg[0] = 0;
1279 			sp->pr_sysarg[1] = (uintptr_t)up->u_argv;
1280 			sp->pr_sysarg[2] = (uintptr_t)up->u_envp;
1281 			for (i = 0, auxp = up->u_auxv;
1282 			    i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]);
1283 			    i++, auxp++) {
1284 				if (auxp->a_type == AT_SUN_EXECNAME) {
1285 					sp->pr_sysarg[0] =
1286 						(uintptr_t)auxp->a_un.a_ptr;
1287 					break;
1288 				}
1289 			}
1290 		}
1291 	}
1292 	if (prhasfp())
1293 		prgetprfpregs(lwp, &sp->pr_fpreg);
1294 	mutex_enter(&p->p_lock);
1295 }
1296 
1297 /*
1298  * Get the sigaction structure for the specified signal.  The u-block
1299  * must already have been mapped in by the caller.
1300  */
1301 void
1302 prgetaction(proc_t *p, user_t *up, uint_t sig, struct sigaction *sp)
1303 {
1304 	bzero(sp, sizeof (*sp));
1305 
1306 	if (sig != 0 && (unsigned)sig < NSIG) {
1307 		sp->sa_handler = up->u_signal[sig-1];
1308 		prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]);
1309 		if (sigismember(&up->u_sigonstack, sig))
1310 			sp->sa_flags |= SA_ONSTACK;
1311 		if (sigismember(&up->u_sigresethand, sig))
1312 			sp->sa_flags |= SA_RESETHAND;
1313 		if (sigismember(&up->u_sigrestart, sig))
1314 			sp->sa_flags |= SA_RESTART;
1315 		if (sigismember(&p->p_siginfo, sig))
1316 			sp->sa_flags |= SA_SIGINFO;
1317 		if (sigismember(&up->u_signodefer, sig))
1318 			sp->sa_flags |= SA_NODEFER;
1319 		if (sig == SIGCLD) {
1320 			if (p->p_flag & SNOWAIT)
1321 				sp->sa_flags |= SA_NOCLDWAIT;
1322 			if ((p->p_flag & SJCTL) == 0)
1323 				sp->sa_flags |= SA_NOCLDSTOP;
1324 		}
1325 	}
1326 }
1327 
1328 #ifdef _SYSCALL32_IMPL
1329 void
1330 prgetaction32(proc_t *p, user_t *up, uint_t sig, struct sigaction32 *sp)
1331 {
1332 	bzero(sp, sizeof (*sp));
1333 
1334 	if (sig != 0 && (unsigned)sig < NSIG) {
1335 		sp->sa_handler = (caddr32_t)(uintptr_t)up->u_signal[sig-1];
1336 		prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]);
1337 		if (sigismember(&up->u_sigonstack, sig))
1338 			sp->sa_flags |= SA_ONSTACK;
1339 		if (sigismember(&up->u_sigresethand, sig))
1340 			sp->sa_flags |= SA_RESETHAND;
1341 		if (sigismember(&up->u_sigrestart, sig))
1342 			sp->sa_flags |= SA_RESTART;
1343 		if (sigismember(&p->p_siginfo, sig))
1344 			sp->sa_flags |= SA_SIGINFO;
1345 		if (sigismember(&up->u_signodefer, sig))
1346 			sp->sa_flags |= SA_NODEFER;
1347 		if (sig == SIGCLD) {
1348 			if (p->p_flag & SNOWAIT)
1349 				sp->sa_flags |= SA_NOCLDWAIT;
1350 			if ((p->p_flag & SJCTL) == 0)
1351 				sp->sa_flags |= SA_NOCLDSTOP;
1352 		}
1353 	}
1354 }
1355 #endif	/* _SYSCALL32_IMPL */
1356 
1357 /*
1358  * Count the number of segments in this process's address space.
1359  */
1360 int
1361 prnsegs(struct as *as, int reserved)
1362 {
1363 	int n = 0;
1364 	struct seg *seg;
1365 
1366 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1367 
1368 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1369 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1370 		caddr_t saddr, naddr;
1371 		void *tmp = NULL;
1372 
1373 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1374 			(void) pr_getprot(seg, reserved, &tmp,
1375 			    &saddr, &naddr, eaddr);
1376 			if (saddr != naddr)
1377 				n++;
1378 		}
1379 
1380 		ASSERT(tmp == NULL);
1381 	}
1382 
1383 	return (n);
1384 }
1385 
1386 /*
1387  * Convert uint32_t to decimal string w/o leading zeros.
1388  * Add trailing null characters if 'len' is greater than string length.
1389  * Return the string length.
1390  */
1391 int
1392 pr_u32tos(uint32_t n, char *s, int len)
1393 {
1394 	char cbuf[11];		/* 32-bit unsigned integer fits in 10 digits */
1395 	char *cp = cbuf;
1396 	char *end = s + len;
1397 
1398 	do {
1399 		*cp++ = (char)(n % 10 + '0');
1400 		n /= 10;
1401 	} while (n);
1402 
1403 	len = (int)(cp - cbuf);
1404 
1405 	do {
1406 		*s++ = *--cp;
1407 	} while (cp > cbuf);
1408 
1409 	while (s < end)		/* optional pad */
1410 		*s++ = '\0';
1411 
1412 	return (len);
1413 }
1414 
1415 /*
1416  * Convert uint64_t to decimal string w/o leading zeros.
1417  * Return the string length.
1418  */
1419 static int
1420 pr_u64tos(uint64_t n, char *s)
1421 {
1422 	char cbuf[21];		/* 64-bit unsigned integer fits in 20 digits */
1423 	char *cp = cbuf;
1424 	int len;
1425 
1426 	do {
1427 		*cp++ = (char)(n % 10 + '0');
1428 		n /= 10;
1429 	} while (n);
1430 
1431 	len = (int)(cp - cbuf);
1432 
1433 	do {
1434 		*s++ = *--cp;
1435 	} while (cp > cbuf);
1436 
1437 	return (len);
1438 }
1439 
1440 void
1441 pr_object_name(char *name, vnode_t *vp, struct vattr *vattr)
1442 {
1443 	char *s = name;
1444 	struct vfs *vfsp;
1445 	struct vfssw *vfsswp;
1446 
1447 	if ((vfsp = vp->v_vfsp) != NULL &&
1448 	    ((vfsswp = vfssw + vfsp->vfs_fstype), vfsswp->vsw_name) &&
1449 	    *vfsswp->vsw_name) {
1450 		(void) strcpy(s, vfsswp->vsw_name);
1451 		s += strlen(s);
1452 		*s++ = '.';
1453 	}
1454 	s += pr_u32tos(getmajor(vattr->va_fsid), s, 0);
1455 	*s++ = '.';
1456 	s += pr_u32tos(getminor(vattr->va_fsid), s, 0);
1457 	*s++ = '.';
1458 	s += pr_u64tos(vattr->va_nodeid, s);
1459 	*s++ = '\0';
1460 }
1461 
1462 struct seg *
1463 break_seg(proc_t *p)
1464 {
1465 	caddr_t addr = p->p_brkbase;
1466 	struct seg *seg;
1467 	struct vnode *vp;
1468 
1469 	if (p->p_brksize != 0)
1470 		addr += p->p_brksize - 1;
1471 	seg = as_segat(p->p_as, addr);
1472 	if (seg != NULL && seg->s_ops == &segvn_ops &&
1473 	    (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL))
1474 		return (seg);
1475 	return (NULL);
1476 }
1477 
1478 /*
1479  * Return an array of structures with memory map information.
1480  * We allocate here; the caller must deallocate.
1481  */
1482 #define	INITIAL_MAPSIZE	65536
1483 #define	MAPSIZE	8192
1484 int
1485 prgetmap(proc_t *p, int reserved, prmap_t **prmapp, size_t *sizep)
1486 {
1487 	struct as *as = p->p_as;
1488 	int nmaps = 0;
1489 	prmap_t *mp;
1490 	size_t size;
1491 	struct seg *seg;
1492 	struct seg *brkseg, *stkseg;
1493 	struct vnode *vp;
1494 	struct vattr vattr;
1495 	uint_t prot;
1496 
1497 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1498 
1499 	/* initial allocation */
1500 	*sizep = size = INITIAL_MAPSIZE;
1501 	*prmapp = mp = kmem_alloc(size, KM_SLEEP);
1502 
1503 	if ((seg = AS_SEGFIRST(as)) == NULL)
1504 		return (0);
1505 
1506 	brkseg = break_seg(p);
1507 	stkseg = as_segat(as, prgetstackbase(p));
1508 
1509 	do {
1510 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1511 		caddr_t saddr, naddr;
1512 		void *tmp = NULL;
1513 
1514 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1515 			prot = pr_getprot(seg, reserved, &tmp,
1516 			    &saddr, &naddr, eaddr);
1517 			if (saddr == naddr)
1518 				continue;
1519 			/* reallocate if necessary */
1520 			if ((nmaps + 1) * sizeof (prmap_t) > size) {
1521 				size_t newsize = size + 3 * size / 16;
1522 				prmap_t *newmp = kmem_alloc(newsize, KM_SLEEP);
1523 
1524 				bcopy(*prmapp, newmp, nmaps * sizeof (prmap_t));
1525 				kmem_free(*prmapp, size);
1526 				*sizep = size = newsize;
1527 				*prmapp = newmp;
1528 				mp = newmp + nmaps;
1529 			}
1530 			bzero(mp, sizeof (*mp));
1531 			mp->pr_vaddr = (uintptr_t)saddr;
1532 			mp->pr_size = naddr - saddr;
1533 			mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1534 			mp->pr_mflags = 0;
1535 			if (prot & PROT_READ)
1536 				mp->pr_mflags |= MA_READ;
1537 			if (prot & PROT_WRITE)
1538 				mp->pr_mflags |= MA_WRITE;
1539 			if (prot & PROT_EXEC)
1540 				mp->pr_mflags |= MA_EXEC;
1541 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1542 				mp->pr_mflags |= MA_SHARED;
1543 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1544 				mp->pr_mflags |= MA_NORESERVE;
1545 			if (seg->s_ops == &segspt_shmops ||
1546 			    (seg->s_ops == &segvn_ops &&
1547 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1548 				mp->pr_mflags |= MA_ANON;
1549 			if (seg == brkseg)
1550 				mp->pr_mflags |= MA_BREAK;
1551 			else if (seg == stkseg) {
1552 				mp->pr_mflags |= MA_STACK;
1553 				if (reserved) {
1554 					size_t maxstack =
1555 					    ((size_t)p->p_stk_ctl +
1556 					    PAGEOFFSET) & PAGEMASK;
1557 					mp->pr_vaddr =
1558 					    (uintptr_t)prgetstackbase(p) +
1559 					    p->p_stksize - maxstack;
1560 					mp->pr_size = (uintptr_t)naddr -
1561 					    mp->pr_vaddr;
1562 				}
1563 			}
1564 			if (seg->s_ops == &segspt_shmops)
1565 				mp->pr_mflags |= MA_ISM | MA_SHM;
1566 			mp->pr_pagesize = PAGESIZE;
1567 
1568 			/*
1569 			 * Manufacture a filename for the "object" directory.
1570 			 */
1571 			vattr.va_mask = AT_FSID|AT_NODEID;
1572 			if (seg->s_ops == &segvn_ops &&
1573 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1574 			    vp != NULL && vp->v_type == VREG &&
1575 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
1576 				if (vp == p->p_exec)
1577 					(void) strcpy(mp->pr_mapname, "a.out");
1578 				else
1579 					pr_object_name(mp->pr_mapname,
1580 						vp, &vattr);
1581 			}
1582 
1583 			/*
1584 			 * Get the SysV shared memory id, if any.
1585 			 */
1586 			if ((mp->pr_mflags & MA_SHARED) && p->p_segacct &&
1587 			    (mp->pr_shmid = shmgetid(p, seg->s_base)) !=
1588 			    SHMID_NONE) {
1589 				if (mp->pr_shmid == SHMID_FREE)
1590 					mp->pr_shmid = -1;
1591 
1592 				mp->pr_mflags |= MA_SHM;
1593 			} else {
1594 				mp->pr_shmid = -1;
1595 			}
1596 
1597 			mp++;
1598 			nmaps++;
1599 		}
1600 		ASSERT(tmp == NULL);
1601 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1602 
1603 	return (nmaps);
1604 }
1605 
1606 #ifdef _SYSCALL32_IMPL
1607 int
1608 prgetmap32(proc_t *p, int reserved, prmap32_t **prmapp, size_t *sizep)
1609 {
1610 	struct as *as = p->p_as;
1611 	int nmaps = 0;
1612 	prmap32_t *mp;
1613 	size_t size;
1614 	struct seg *seg;
1615 	struct seg *brkseg, *stkseg;
1616 	struct vnode *vp;
1617 	struct vattr vattr;
1618 	uint_t prot;
1619 
1620 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1621 
1622 	/* initial allocation */
1623 	*sizep = size = INITIAL_MAPSIZE;
1624 	*prmapp = mp = kmem_alloc(size, KM_SLEEP);
1625 
1626 	if ((seg = AS_SEGFIRST(as)) == NULL)
1627 		return (0);
1628 
1629 	brkseg = break_seg(p);
1630 	stkseg = as_segat(as, prgetstackbase(p));
1631 
1632 	do {
1633 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1634 		caddr_t saddr, naddr;
1635 		void *tmp = NULL;
1636 
1637 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1638 			prot = pr_getprot(seg, reserved, &tmp,
1639 			    &saddr, &naddr, eaddr);
1640 			if (saddr == naddr)
1641 				continue;
1642 			/* reallocate if necessary */
1643 			if ((nmaps + 1) * sizeof (prmap32_t) > size) {
1644 				size_t newsize = size + 3 * size / 16;
1645 				prmap32_t *newmp =
1646 					kmem_alloc(newsize, KM_SLEEP);
1647 
1648 				bcopy(*prmapp, newmp,
1649 					nmaps * sizeof (prmap32_t));
1650 				kmem_free(*prmapp, size);
1651 				*sizep = size = newsize;
1652 				*prmapp = newmp;
1653 				mp = newmp + nmaps;
1654 			}
1655 			bzero(mp, sizeof (*mp));
1656 			mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
1657 			mp->pr_size = (size32_t)(naddr - saddr);
1658 			mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1659 			mp->pr_mflags = 0;
1660 			if (prot & PROT_READ)
1661 				mp->pr_mflags |= MA_READ;
1662 			if (prot & PROT_WRITE)
1663 				mp->pr_mflags |= MA_WRITE;
1664 			if (prot & PROT_EXEC)
1665 				mp->pr_mflags |= MA_EXEC;
1666 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1667 				mp->pr_mflags |= MA_SHARED;
1668 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1669 				mp->pr_mflags |= MA_NORESERVE;
1670 			if (seg->s_ops == &segspt_shmops ||
1671 			    (seg->s_ops == &segvn_ops &&
1672 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1673 				mp->pr_mflags |= MA_ANON;
1674 			if (seg == brkseg)
1675 				mp->pr_mflags |= MA_BREAK;
1676 			else if (seg == stkseg) {
1677 				mp->pr_mflags |= MA_STACK;
1678 				if (reserved) {
1679 					size_t maxstack =
1680 					    ((size_t)p->p_stk_ctl +
1681 					    PAGEOFFSET) & PAGEMASK;
1682 					uintptr_t vaddr =
1683 					    (uintptr_t)prgetstackbase(p) +
1684 					    p->p_stksize - maxstack;
1685 					mp->pr_vaddr = (caddr32_t)vaddr;
1686 					mp->pr_size = (size32_t)
1687 					    ((uintptr_t)naddr - vaddr);
1688 				}
1689 			}
1690 			if (seg->s_ops == &segspt_shmops)
1691 				mp->pr_mflags |= MA_ISM | MA_SHM;
1692 			mp->pr_pagesize = PAGESIZE;
1693 
1694 			/*
1695 			 * Manufacture a filename for the "object" directory.
1696 			 */
1697 			vattr.va_mask = AT_FSID|AT_NODEID;
1698 			if (seg->s_ops == &segvn_ops &&
1699 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1700 			    vp != NULL && vp->v_type == VREG &&
1701 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
1702 				if (vp == p->p_exec)
1703 					(void) strcpy(mp->pr_mapname, "a.out");
1704 				else
1705 					pr_object_name(mp->pr_mapname,
1706 						vp, &vattr);
1707 			}
1708 
1709 			/*
1710 			 * Get the SysV shared memory id, if any.
1711 			 */
1712 			if ((mp->pr_mflags & MA_SHARED) && p->p_segacct &&
1713 			    (mp->pr_shmid = shmgetid(p, seg->s_base)) !=
1714 			    SHMID_NONE) {
1715 				if (mp->pr_shmid == SHMID_FREE)
1716 					mp->pr_shmid = -1;
1717 
1718 				mp->pr_mflags |= MA_SHM;
1719 			} else {
1720 				mp->pr_shmid = -1;
1721 			}
1722 
1723 			mp++;
1724 			nmaps++;
1725 		}
1726 		ASSERT(tmp == NULL);
1727 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1728 
1729 	return (nmaps);
1730 }
1731 #endif	/* _SYSCALL32_IMPL */
1732 
1733 /*
1734  * Return the size of the /proc page data file.
1735  */
1736 size_t
1737 prpdsize(struct as *as)
1738 {
1739 	struct seg *seg;
1740 	size_t size;
1741 
1742 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1743 
1744 	if ((seg = AS_SEGFIRST(as)) == NULL)
1745 		return (0);
1746 
1747 	size = sizeof (prpageheader_t);
1748 	do {
1749 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1750 		caddr_t saddr, naddr;
1751 		void *tmp = NULL;
1752 		size_t npage;
1753 
1754 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1755 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1756 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
1757 				size += sizeof (prasmap_t) + round8(npage);
1758 		}
1759 		ASSERT(tmp == NULL);
1760 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1761 
1762 	return (size);
1763 }
1764 
1765 #ifdef _SYSCALL32_IMPL
1766 size_t
1767 prpdsize32(struct as *as)
1768 {
1769 	struct seg *seg;
1770 	size_t size;
1771 
1772 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1773 
1774 	if ((seg = AS_SEGFIRST(as)) == NULL)
1775 		return (0);
1776 
1777 	size = sizeof (prpageheader32_t);
1778 	do {
1779 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1780 		caddr_t saddr, naddr;
1781 		void *tmp = NULL;
1782 		size_t npage;
1783 
1784 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1785 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1786 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
1787 				size += sizeof (prasmap32_t) + round8(npage);
1788 		}
1789 		ASSERT(tmp == NULL);
1790 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1791 
1792 	return (size);
1793 }
1794 #endif	/* _SYSCALL32_IMPL */
1795 
1796 /*
1797  * Read page data information.
1798  */
1799 int
1800 prpdread(proc_t *p, uint_t hatid, struct uio *uiop)
1801 {
1802 	struct as *as = p->p_as;
1803 	caddr_t buf;
1804 	size_t size;
1805 	prpageheader_t *php;
1806 	prasmap_t *pmp;
1807 	struct seg *seg;
1808 	int error;
1809 
1810 again:
1811 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1812 
1813 	if ((seg = AS_SEGFIRST(as)) == NULL) {
1814 		AS_LOCK_EXIT(as, &as->a_lock);
1815 		return (0);
1816 	}
1817 	size = prpdsize(as);
1818 	if (uiop->uio_resid < size) {
1819 		AS_LOCK_EXIT(as, &as->a_lock);
1820 		return (E2BIG);
1821 	}
1822 
1823 	buf = kmem_zalloc(size, KM_SLEEP);
1824 	php = (prpageheader_t *)buf;
1825 	pmp = (prasmap_t *)(buf + sizeof (prpageheader_t));
1826 
1827 	hrt2ts(gethrtime(), &php->pr_tstamp);
1828 	php->pr_nmap = 0;
1829 	php->pr_npage = 0;
1830 	do {
1831 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1832 		caddr_t saddr, naddr;
1833 		void *tmp = NULL;
1834 
1835 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1836 			struct vnode *vp;
1837 			struct vattr vattr;
1838 			size_t len;
1839 			size_t npage;
1840 			uint_t prot;
1841 			uintptr_t next;
1842 
1843 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1844 			if ((len = (size_t)(naddr - saddr)) == 0)
1845 				continue;
1846 			npage = len / PAGESIZE;
1847 			next = (uintptr_t)(pmp + 1) + round8(npage);
1848 			/*
1849 			 * It's possible that the address space can change
1850 			 * subtlely even though we're holding as->a_lock
1851 			 * due to the nondeterminism of page_exists() in
1852 			 * the presence of asychronously flushed pages or
1853 			 * mapped files whose sizes are changing.
1854 			 * page_exists() may be called indirectly from
1855 			 * pr_getprot() by a SEGOP_INCORE() routine.
1856 			 * If this happens we need to make sure we don't
1857 			 * overrun the buffer whose size we computed based
1858 			 * on the initial iteration through the segments.
1859 			 * Once we've detected an overflow, we need to clean
1860 			 * up the temporary memory allocated in pr_getprot()
1861 			 * and retry. If there's a pending signal, we return
1862 			 * EINTR so that this thread can be dislodged if
1863 			 * a latent bug causes us to spin indefinitely.
1864 			 */
1865 			if (next > (uintptr_t)buf + size) {
1866 				pr_getprot_done(&tmp);
1867 				AS_LOCK_EXIT(as, &as->a_lock);
1868 
1869 				kmem_free(buf, size);
1870 
1871 				if (ISSIG(curthread, JUSTLOOKING))
1872 					return (EINTR);
1873 
1874 				goto again;
1875 			}
1876 
1877 			php->pr_nmap++;
1878 			php->pr_npage += npage;
1879 			pmp->pr_vaddr = (uintptr_t)saddr;
1880 			pmp->pr_npage = npage;
1881 			pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1882 			pmp->pr_mflags = 0;
1883 			if (prot & PROT_READ)
1884 				pmp->pr_mflags |= MA_READ;
1885 			if (prot & PROT_WRITE)
1886 				pmp->pr_mflags |= MA_WRITE;
1887 			if (prot & PROT_EXEC)
1888 				pmp->pr_mflags |= MA_EXEC;
1889 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1890 				pmp->pr_mflags |= MA_SHARED;
1891 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1892 				pmp->pr_mflags |= MA_NORESERVE;
1893 			if (seg->s_ops == &segspt_shmops ||
1894 			    (seg->s_ops == &segvn_ops &&
1895 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1896 				pmp->pr_mflags |= MA_ANON;
1897 			if (seg->s_ops == &segspt_shmops)
1898 				pmp->pr_mflags |= MA_ISM | MA_SHM;
1899 			pmp->pr_pagesize = PAGESIZE;
1900 			/*
1901 			 * Manufacture a filename for the "object" directory.
1902 			 */
1903 			vattr.va_mask = AT_FSID|AT_NODEID;
1904 			if (seg->s_ops == &segvn_ops &&
1905 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1906 			    vp != NULL && vp->v_type == VREG &&
1907 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
1908 				if (vp == p->p_exec)
1909 					(void) strcpy(pmp->pr_mapname, "a.out");
1910 				else
1911 					pr_object_name(pmp->pr_mapname,
1912 						vp, &vattr);
1913 			}
1914 
1915 			/*
1916 			 * Get the SysV shared memory id, if any.
1917 			 */
1918 			if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct &&
1919 			    (pmp->pr_shmid = shmgetid(p, seg->s_base)) !=
1920 			    SHMID_NONE) {
1921 				if (pmp->pr_shmid == SHMID_FREE)
1922 					pmp->pr_shmid = -1;
1923 
1924 				pmp->pr_mflags |= MA_SHM;
1925 			} else {
1926 				pmp->pr_shmid = -1;
1927 			}
1928 
1929 			hat_getstat(as, saddr, len, hatid,
1930 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
1931 			pmp = (prasmap_t *)next;
1932 		}
1933 		ASSERT(tmp == NULL);
1934 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1935 
1936 	AS_LOCK_EXIT(as, &as->a_lock);
1937 
1938 	ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size);
1939 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
1940 	kmem_free(buf, size);
1941 
1942 	return (error);
1943 }
1944 
1945 #ifdef _SYSCALL32_IMPL
1946 int
1947 prpdread32(proc_t *p, uint_t hatid, struct uio *uiop)
1948 {
1949 	struct as *as = p->p_as;
1950 	caddr_t buf;
1951 	size_t size;
1952 	prpageheader32_t *php;
1953 	prasmap32_t *pmp;
1954 	struct seg *seg;
1955 	int error;
1956 
1957 again:
1958 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1959 
1960 	if ((seg = AS_SEGFIRST(as)) == NULL) {
1961 		AS_LOCK_EXIT(as, &as->a_lock);
1962 		return (0);
1963 	}
1964 	size = prpdsize32(as);
1965 	if (uiop->uio_resid < size) {
1966 		AS_LOCK_EXIT(as, &as->a_lock);
1967 		return (E2BIG);
1968 	}
1969 
1970 	buf = kmem_zalloc(size, KM_SLEEP);
1971 	php = (prpageheader32_t *)buf;
1972 	pmp = (prasmap32_t *)(buf + sizeof (prpageheader32_t));
1973 
1974 	hrt2ts32(gethrtime(), &php->pr_tstamp);
1975 	php->pr_nmap = 0;
1976 	php->pr_npage = 0;
1977 	do {
1978 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1979 		caddr_t saddr, naddr;
1980 		void *tmp = NULL;
1981 
1982 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1983 			struct vnode *vp;
1984 			struct vattr vattr;
1985 			size_t len;
1986 			size_t npage;
1987 			uint_t prot;
1988 			uintptr_t next;
1989 
1990 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1991 			if ((len = (size_t)(naddr - saddr)) == 0)
1992 				continue;
1993 			npage = len / PAGESIZE;
1994 			next = (uintptr_t)(pmp + 1) + round8(npage);
1995 			/*
1996 			 * It's possible that the address space can change
1997 			 * subtlely even though we're holding as->a_lock
1998 			 * due to the nondeterminism of page_exists() in
1999 			 * the presence of asychronously flushed pages or
2000 			 * mapped files whose sizes are changing.
2001 			 * page_exists() may be called indirectly from
2002 			 * pr_getprot() by a SEGOP_INCORE() routine.
2003 			 * If this happens we need to make sure we don't
2004 			 * overrun the buffer whose size we computed based
2005 			 * on the initial iteration through the segments.
2006 			 * Once we've detected an overflow, we need to clean
2007 			 * up the temporary memory allocated in pr_getprot()
2008 			 * and retry. If there's a pending signal, we return
2009 			 * EINTR so that this thread can be dislodged if
2010 			 * a latent bug causes us to spin indefinitely.
2011 			 */
2012 			if (next > (uintptr_t)buf + size) {
2013 				pr_getprot_done(&tmp);
2014 				AS_LOCK_EXIT(as, &as->a_lock);
2015 
2016 				kmem_free(buf, size);
2017 
2018 				if (ISSIG(curthread, JUSTLOOKING))
2019 					return (EINTR);
2020 
2021 				goto again;
2022 			}
2023 
2024 			php->pr_nmap++;
2025 			php->pr_npage += npage;
2026 			pmp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
2027 			pmp->pr_npage = (size32_t)npage;
2028 			pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
2029 			pmp->pr_mflags = 0;
2030 			if (prot & PROT_READ)
2031 				pmp->pr_mflags |= MA_READ;
2032 			if (prot & PROT_WRITE)
2033 				pmp->pr_mflags |= MA_WRITE;
2034 			if (prot & PROT_EXEC)
2035 				pmp->pr_mflags |= MA_EXEC;
2036 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
2037 				pmp->pr_mflags |= MA_SHARED;
2038 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
2039 				pmp->pr_mflags |= MA_NORESERVE;
2040 			if (seg->s_ops == &segspt_shmops ||
2041 			    (seg->s_ops == &segvn_ops &&
2042 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
2043 				pmp->pr_mflags |= MA_ANON;
2044 			if (seg->s_ops == &segspt_shmops)
2045 				pmp->pr_mflags |= MA_ISM | MA_SHM;
2046 			pmp->pr_pagesize = PAGESIZE;
2047 			/*
2048 			 * Manufacture a filename for the "object" directory.
2049 			 */
2050 			vattr.va_mask = AT_FSID|AT_NODEID;
2051 			if (seg->s_ops == &segvn_ops &&
2052 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
2053 			    vp != NULL && vp->v_type == VREG &&
2054 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
2055 				if (vp == p->p_exec)
2056 					(void) strcpy(pmp->pr_mapname, "a.out");
2057 				else
2058 					pr_object_name(pmp->pr_mapname,
2059 						vp, &vattr);
2060 			}
2061 
2062 			/*
2063 			 * Get the SysV shared memory id, if any.
2064 			 */
2065 			if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct &&
2066 			    (pmp->pr_shmid = shmgetid(p, seg->s_base)) !=
2067 			    SHMID_NONE) {
2068 				if (pmp->pr_shmid == SHMID_FREE)
2069 					pmp->pr_shmid = -1;
2070 
2071 				pmp->pr_mflags |= MA_SHM;
2072 			} else {
2073 				pmp->pr_shmid = -1;
2074 			}
2075 
2076 			hat_getstat(as, saddr, len, hatid,
2077 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
2078 			pmp = (prasmap32_t *)next;
2079 		}
2080 		ASSERT(tmp == NULL);
2081 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
2082 
2083 	AS_LOCK_EXIT(as, &as->a_lock);
2084 
2085 	ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size);
2086 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
2087 	kmem_free(buf, size);
2088 
2089 	return (error);
2090 }
2091 #endif	/* _SYSCALL32_IMPL */
2092 
2093 ushort_t
2094 prgetpctcpu(uint64_t pct)
2095 {
2096 	/*
2097 	 * The value returned will be relevant in the zone of the examiner,
2098 	 * which may not be the same as the zone which performed the procfs
2099 	 * mount.
2100 	 */
2101 	int nonline = zone_ncpus_online_get(curproc->p_zone);
2102 
2103 	/*
2104 	 * Prorate over online cpus so we don't exceed 100%
2105 	 */
2106 	if (nonline > 1)
2107 		pct /= nonline;
2108 	pct >>= 16;		/* convert to 16-bit scaled integer */
2109 	if (pct > 0x8000)	/* might happen, due to rounding */
2110 		pct = 0x8000;
2111 	return ((ushort_t)pct);
2112 }
2113 
2114 /*
2115  * Return information used by ps(1).
2116  */
2117 void
2118 prgetpsinfo(proc_t *p, psinfo_t *psp)
2119 {
2120 	kthread_t *t;
2121 	struct cred *cred;
2122 	hrtime_t hrutime, hrstime;
2123 
2124 	ASSERT(MUTEX_HELD(&p->p_lock));
2125 
2126 	if ((t = prchoose(p)) == NULL)	/* returns locked thread */
2127 		bzero(psp, sizeof (*psp));
2128 	else {
2129 		thread_unlock(t);
2130 		bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp));
2131 	}
2132 
2133 	/*
2134 	 * only export SSYS and SMSACCT; everything else is off-limits to
2135 	 * userland apps.
2136 	 */
2137 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
2138 	psp->pr_nlwp = p->p_lwpcnt;
2139 	psp->pr_nzomb = p->p_zombcnt;
2140 	mutex_enter(&p->p_crlock);
2141 	cred = p->p_cred;
2142 	psp->pr_uid = crgetruid(cred);
2143 	psp->pr_euid = crgetuid(cred);
2144 	psp->pr_gid = crgetrgid(cred);
2145 	psp->pr_egid = crgetgid(cred);
2146 	mutex_exit(&p->p_crlock);
2147 	psp->pr_pid = p->p_pid;
2148 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
2149 	    (p->p_flag & SZONETOP)) {
2150 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
2151 		/*
2152 		 * Inside local zones, fake zsched's pid as parent pids for
2153 		 * processes which reference processes outside of the zone.
2154 		 */
2155 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
2156 	} else {
2157 		psp->pr_ppid = p->p_ppid;
2158 	}
2159 	psp->pr_pgid = p->p_pgrp;
2160 	psp->pr_sid = p->p_sessp->s_sid;
2161 	psp->pr_taskid = p->p_task->tk_tkid;
2162 	psp->pr_projid = p->p_task->tk_proj->kpj_id;
2163 	psp->pr_poolid = p->p_pool->pool_id;
2164 	psp->pr_zoneid = p->p_zone->zone_id;
2165 	if ((psp->pr_contract = PRCTID(p)) == 0)
2166 		psp->pr_contract = -1;
2167 	psp->pr_addr = (uintptr_t)prgetpsaddr(p);
2168 	switch (p->p_model) {
2169 	case DATAMODEL_ILP32:
2170 		psp->pr_dmodel = PR_MODEL_ILP32;
2171 		break;
2172 	case DATAMODEL_LP64:
2173 		psp->pr_dmodel = PR_MODEL_LP64;
2174 		break;
2175 	}
2176 	hrutime = mstate_aggr_state(p, LMS_USER);
2177 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
2178 	hrt2ts((hrutime + hrstime), &psp->pr_time);
2179 	TICK_TO_TIMESTRUC(p->p_cutime + p->p_cstime, &psp->pr_ctime);
2180 
2181 	if (t == NULL) {
2182 		int wcode = p->p_wcode;		/* must be atomic read */
2183 
2184 		if (wcode)
2185 			psp->pr_wstat = wstat(wcode, p->p_wdata);
2186 		psp->pr_ttydev = PRNODEV;
2187 		psp->pr_lwp.pr_state = SZOMB;
2188 		psp->pr_lwp.pr_sname = 'Z';
2189 		psp->pr_lwp.pr_bindpro = PBIND_NONE;
2190 		psp->pr_lwp.pr_bindpset = PS_NONE;
2191 	} else {
2192 		user_t *up = PTOU(p);
2193 		struct as *as;
2194 		dev_t d;
2195 		extern dev_t rwsconsdev, rconsdev, uconsdev;
2196 
2197 		d = cttydev(p);
2198 		/*
2199 		 * If the controlling terminal is the real
2200 		 * or workstation console device, map to what the
2201 		 * user thinks is the console device.
2202 		 */
2203 		if (d == rwsconsdev || d == rconsdev)
2204 			d = uconsdev;
2205 		psp->pr_ttydev = (d == NODEV) ? PRNODEV : d;
2206 		psp->pr_start = up->u_start;
2207 		bcopy(up->u_comm, psp->pr_fname,
2208 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
2209 		bcopy(up->u_psargs, psp->pr_psargs,
2210 		    MIN(PRARGSZ-1, PSARGSZ));
2211 		psp->pr_argc = up->u_argc;
2212 		psp->pr_argv = up->u_argv;
2213 		psp->pr_envp = up->u_envp;
2214 
2215 		/* get the chosen lwp's lwpsinfo */
2216 		prgetlwpsinfo(t, &psp->pr_lwp);
2217 
2218 		/* compute %cpu for the process */
2219 		if (p->p_lwpcnt == 1)
2220 			psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu;
2221 		else {
2222 			uint64_t pct = 0;
2223 			hrtime_t cur_time = gethrtime_unscaled();
2224 
2225 			t = p->p_tlist;
2226 			do {
2227 				pct += cpu_update_pct(t, cur_time);
2228 			} while ((t = t->t_forw) != p->p_tlist);
2229 
2230 			psp->pr_pctcpu = prgetpctcpu(pct);
2231 		}
2232 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
2233 			psp->pr_size = 0;
2234 			psp->pr_rssize = 0;
2235 		} else {
2236 			mutex_exit(&p->p_lock);
2237 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
2238 			psp->pr_size = btopr(rm_assize(as)) * (PAGESIZE / 1024);
2239 			psp->pr_rssize = rm_asrss(as) * (PAGESIZE / 1024);
2240 			psp->pr_pctmem = rm_pctmemory(as);
2241 			AS_LOCK_EXIT(as, &as->a_lock);
2242 			mutex_enter(&p->p_lock);
2243 		}
2244 	}
2245 }
2246 
2247 #ifdef _SYSCALL32_IMPL
2248 void
2249 prgetpsinfo32(proc_t *p, psinfo32_t *psp)
2250 {
2251 	kthread_t *t;
2252 	struct cred *cred;
2253 	hrtime_t hrutime, hrstime;
2254 
2255 	ASSERT(MUTEX_HELD(&p->p_lock));
2256 
2257 	if ((t = prchoose(p)) == NULL)	/* returns locked thread */
2258 		bzero(psp, sizeof (*psp));
2259 	else {
2260 		thread_unlock(t);
2261 		bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp));
2262 	}
2263 
2264 	/*
2265 	 * only export SSYS and SMSACCT; everything else is off-limits to
2266 	 * userland apps.
2267 	 */
2268 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
2269 	psp->pr_nlwp = p->p_lwpcnt;
2270 	psp->pr_nzomb = p->p_zombcnt;
2271 	mutex_enter(&p->p_crlock);
2272 	cred = p->p_cred;
2273 	psp->pr_uid = crgetruid(cred);
2274 	psp->pr_euid = crgetuid(cred);
2275 	psp->pr_gid = crgetrgid(cred);
2276 	psp->pr_egid = crgetgid(cred);
2277 	mutex_exit(&p->p_crlock);
2278 	psp->pr_pid = p->p_pid;
2279 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
2280 	    (p->p_flag & SZONETOP)) {
2281 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
2282 		/*
2283 		 * Inside local zones, fake zsched's pid as parent pids for
2284 		 * processes which reference processes outside of the zone.
2285 		 */
2286 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
2287 	} else {
2288 		psp->pr_ppid = p->p_ppid;
2289 	}
2290 	psp->pr_pgid = p->p_pgrp;
2291 	psp->pr_sid = p->p_sessp->s_sid;
2292 	psp->pr_taskid = p->p_task->tk_tkid;
2293 	psp->pr_projid = p->p_task->tk_proj->kpj_id;
2294 	psp->pr_poolid = p->p_pool->pool_id;
2295 	psp->pr_zoneid = p->p_zone->zone_id;
2296 	if ((psp->pr_contract = PRCTID(p)) == 0)
2297 		psp->pr_contract = -1;
2298 	psp->pr_addr = 0;	/* cannot represent 64-bit addr in 32 bits */
2299 	switch (p->p_model) {
2300 	case DATAMODEL_ILP32:
2301 		psp->pr_dmodel = PR_MODEL_ILP32;
2302 		break;
2303 	case DATAMODEL_LP64:
2304 		psp->pr_dmodel = PR_MODEL_LP64;
2305 		break;
2306 	}
2307 	hrutime = mstate_aggr_state(p, LMS_USER);
2308 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
2309 	hrt2ts32(hrutime + hrstime, &psp->pr_time);
2310 	TICK_TO_TIMESTRUC32(p->p_cutime + p->p_cstime, &psp->pr_ctime);
2311 
2312 	if (t == NULL) {
2313 		extern int wstat(int, int);	/* needs a header file */
2314 		int wcode = p->p_wcode;		/* must be atomic read */
2315 
2316 		if (wcode)
2317 			psp->pr_wstat = wstat(wcode, p->p_wdata);
2318 		psp->pr_ttydev = PRNODEV32;
2319 		psp->pr_lwp.pr_state = SZOMB;
2320 		psp->pr_lwp.pr_sname = 'Z';
2321 	} else {
2322 		user_t *up = PTOU(p);
2323 		struct as *as;
2324 		dev_t d;
2325 		extern dev_t rwsconsdev, rconsdev, uconsdev;
2326 
2327 		d = cttydev(p);
2328 		/*
2329 		 * If the controlling terminal is the real
2330 		 * or workstation console device, map to what the
2331 		 * user thinks is the console device.
2332 		 */
2333 		if (d == rwsconsdev || d == rconsdev)
2334 			d = uconsdev;
2335 		(void) cmpldev(&psp->pr_ttydev, d);
2336 		TIMESPEC_TO_TIMESPEC32(&psp->pr_start, &up->u_start);
2337 		bcopy(up->u_comm, psp->pr_fname,
2338 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
2339 		bcopy(up->u_psargs, psp->pr_psargs,
2340 		    MIN(PRARGSZ-1, PSARGSZ));
2341 		psp->pr_argc = up->u_argc;
2342 		psp->pr_argv = (caddr32_t)up->u_argv;
2343 		psp->pr_envp = (caddr32_t)up->u_envp;
2344 
2345 		/* get the chosen lwp's lwpsinfo */
2346 		prgetlwpsinfo32(t, &psp->pr_lwp);
2347 
2348 		/* compute %cpu for the process */
2349 		if (p->p_lwpcnt == 1)
2350 			psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu;
2351 		else {
2352 			uint64_t pct = 0;
2353 			hrtime_t cur_time;
2354 
2355 			t = p->p_tlist;
2356 			cur_time = gethrtime_unscaled();
2357 			do {
2358 				pct += cpu_update_pct(t, cur_time);
2359 			} while ((t = t->t_forw) != p->p_tlist);
2360 
2361 			psp->pr_pctcpu = prgetpctcpu(pct);
2362 		}
2363 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
2364 			psp->pr_size = 0;
2365 			psp->pr_rssize = 0;
2366 		} else {
2367 			mutex_exit(&p->p_lock);
2368 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
2369 			psp->pr_size = (size32_t)
2370 				(btopr(rm_assize(as)) * (PAGESIZE / 1024));
2371 			psp->pr_rssize = (size32_t)
2372 				(rm_asrss(as) * (PAGESIZE / 1024));
2373 			psp->pr_pctmem = rm_pctmemory(as);
2374 			AS_LOCK_EXIT(as, &as->a_lock);
2375 			mutex_enter(&p->p_lock);
2376 		}
2377 	}
2378 
2379 	/*
2380 	 * If we are looking at an LP64 process, zero out
2381 	 * the fields that cannot be represented in ILP32.
2382 	 */
2383 	if (p->p_model != DATAMODEL_ILP32) {
2384 		psp->pr_size = 0;
2385 		psp->pr_rssize = 0;
2386 		psp->pr_argv = 0;
2387 		psp->pr_envp = 0;
2388 	}
2389 }
2390 #endif	/* _SYSCALL32_IMPL */
2391 
2392 void
2393 prgetlwpsinfo(kthread_t *t, lwpsinfo_t *psp)
2394 {
2395 	klwp_t *lwp = ttolwp(t);
2396 	sobj_ops_t *sobj;
2397 	char c, state;
2398 	uint64_t pct;
2399 	int retval, niceval;
2400 	hrtime_t hrutime, hrstime;
2401 
2402 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2403 
2404 	bzero(psp, sizeof (*psp));
2405 
2406 	psp->pr_flag = 0;	/* lwpsinfo_t.pr_flag is deprecated */
2407 	psp->pr_lwpid = t->t_tid;
2408 	psp->pr_addr = (uintptr_t)t;
2409 	psp->pr_wchan = (uintptr_t)t->t_wchan;
2410 
2411 	/* map the thread state enum into a process state enum */
2412 	state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
2413 	switch (state) {
2414 	case TS_SLEEP:		state = SSLEEP;		c = 'S';	break;
2415 	case TS_RUN:		state = SRUN;		c = 'R';	break;
2416 	case TS_ONPROC:		state = SONPROC;	c = 'O';	break;
2417 	case TS_ZOMB:		state = SZOMB;		c = 'Z';	break;
2418 	case TS_STOPPED:	state = SSTOP;		c = 'T';	break;
2419 	default:		state = 0;		c = '?';	break;
2420 	}
2421 	psp->pr_state = state;
2422 	psp->pr_sname = c;
2423 	if ((sobj = t->t_sobj_ops) != NULL)
2424 		psp->pr_stype = SOBJ_TYPE(sobj);
2425 	retval = CL_DONICE(t, NULL, 0, &niceval);
2426 	if (retval == 0) {
2427 		psp->pr_oldpri = v.v_maxsyspri - t->t_pri;
2428 		psp->pr_nice = niceval + NZERO;
2429 	}
2430 	psp->pr_syscall = t->t_sysnum;
2431 	psp->pr_pri = t->t_pri;
2432 	psp->pr_start.tv_sec = t->t_start;
2433 	psp->pr_start.tv_nsec = 0L;
2434 	hrutime = lwp->lwp_mstate.ms_acct[LMS_USER];
2435 	scalehrtime(&hrutime);
2436 	hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] +
2437 	    lwp->lwp_mstate.ms_acct[LMS_TRAP];
2438 	scalehrtime(&hrstime);
2439 	hrt2ts(hrutime + hrstime, &psp->pr_time);
2440 	/* compute %cpu for the lwp */
2441 	pct = cpu_update_pct(t, gethrtime_unscaled());
2442 	psp->pr_pctcpu = prgetpctcpu(pct);
2443 	psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15;	/* [0..99] */
2444 	if (psp->pr_cpu > 99)
2445 		psp->pr_cpu = 99;
2446 
2447 	(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
2448 		sizeof (psp->pr_clname) - 1);
2449 	bzero(psp->pr_name, sizeof (psp->pr_name));	/* XXX ??? */
2450 	psp->pr_onpro = t->t_cpu->cpu_id;
2451 	psp->pr_bindpro = t->t_bind_cpu;
2452 	psp->pr_bindpset = t->t_bind_pset;
2453 }
2454 
2455 #ifdef _SYSCALL32_IMPL
2456 void
2457 prgetlwpsinfo32(kthread_t *t, lwpsinfo32_t *psp)
2458 {
2459 	proc_t *p = ttoproc(t);
2460 	klwp_t *lwp = ttolwp(t);
2461 	sobj_ops_t *sobj;
2462 	char c, state;
2463 	uint64_t pct;
2464 	int retval, niceval;
2465 	hrtime_t hrutime, hrstime;
2466 
2467 	ASSERT(MUTEX_HELD(&p->p_lock));
2468 
2469 	bzero(psp, sizeof (*psp));
2470 
2471 	psp->pr_flag = 0;	/* lwpsinfo_t.pr_flag is deprecated */
2472 	psp->pr_lwpid = t->t_tid;
2473 	psp->pr_addr = 0;	/* cannot represent 64-bit addr in 32 bits */
2474 	psp->pr_wchan = 0;	/* cannot represent 64-bit addr in 32 bits */
2475 
2476 	/* map the thread state enum into a process state enum */
2477 	state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
2478 	switch (state) {
2479 	case TS_SLEEP:		state = SSLEEP;		c = 'S';	break;
2480 	case TS_RUN:		state = SRUN;		c = 'R';	break;
2481 	case TS_ONPROC:		state = SONPROC;	c = 'O';	break;
2482 	case TS_ZOMB:		state = SZOMB;		c = 'Z';	break;
2483 	case TS_STOPPED:	state = SSTOP;		c = 'T';	break;
2484 	default:		state = 0;		c = '?';	break;
2485 	}
2486 	psp->pr_state = state;
2487 	psp->pr_sname = c;
2488 	if ((sobj = t->t_sobj_ops) != NULL)
2489 		psp->pr_stype = SOBJ_TYPE(sobj);
2490 	retval = CL_DONICE(t, NULL, 0, &niceval);
2491 	if (retval == 0) {
2492 		psp->pr_oldpri = v.v_maxsyspri - t->t_pri;
2493 		psp->pr_nice = niceval + NZERO;
2494 	} else {
2495 		psp->pr_oldpri = 0;
2496 		psp->pr_nice = 0;
2497 	}
2498 	psp->pr_syscall = t->t_sysnum;
2499 	psp->pr_pri = t->t_pri;
2500 	psp->pr_start.tv_sec = (time32_t)t->t_start;
2501 	psp->pr_start.tv_nsec = 0L;
2502 	hrutime = lwp->lwp_mstate.ms_acct[LMS_USER];
2503 	scalehrtime(&hrutime);
2504 	hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] +
2505 	    lwp->lwp_mstate.ms_acct[LMS_TRAP];
2506 	scalehrtime(&hrstime);
2507 	hrt2ts32(hrutime + hrstime, &psp->pr_time);
2508 	/* compute %cpu for the lwp */
2509 	pct = cpu_update_pct(t, gethrtime_unscaled());
2510 	psp->pr_pctcpu = prgetpctcpu(pct);
2511 	psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15;	/* [0..99] */
2512 	if (psp->pr_cpu > 99)
2513 		psp->pr_cpu = 99;
2514 
2515 	(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
2516 		sizeof (psp->pr_clname) - 1);
2517 	bzero(psp->pr_name, sizeof (psp->pr_name));	/* XXX ??? */
2518 	psp->pr_onpro = t->t_cpu->cpu_id;
2519 	psp->pr_bindpro = t->t_bind_cpu;
2520 	psp->pr_bindpset = t->t_bind_pset;
2521 }
2522 #endif	/* _SYSCALL32_IMPL */
2523 
2524 /*
2525  * This used to get called when microstate accounting was disabled but
2526  * microstate information was requested.  Since Microstate accounting is on
2527  * regardless of the proc flags, this simply makes it appear to procfs that
2528  * microstate accounting is on.  This is relatively meaningless since you
2529  * can't turn it off, but this is here for the sake of appearances.
2530  */
2531 
2532 /*ARGSUSED*/
2533 void
2534 estimate_msacct(kthread_t *t, hrtime_t curtime)
2535 {
2536 	proc_t *p;
2537 
2538 	if (t == NULL)
2539 		return;
2540 
2541 	p = ttoproc(t);
2542 	ASSERT(MUTEX_HELD(&p->p_lock));
2543 
2544 	/*
2545 	 * A system process (p0) could be referenced if the thread is
2546 	 * in the process of exiting.  Don't turn on microstate accounting
2547 	 * in that case.
2548 	 */
2549 	if (p->p_flag & SSYS)
2550 		return;
2551 
2552 	/*
2553 	 * Loop through all the LWPs (kernel threads) in the process.
2554 	 */
2555 	t = p->p_tlist;
2556 	do {
2557 		t->t_proc_flag |= TP_MSACCT;
2558 	} while ((t = t->t_forw) != p->p_tlist);
2559 
2560 	p->p_flag |= SMSACCT;			/* set process-wide MSACCT */
2561 }
2562 
2563 /*
2564  * It's not really possible to disable microstate accounting anymore.
2565  * However, this routine simply turns off the ms accounting flags in a process
2566  * This way procfs can still pretend to turn microstate accounting on and
2567  * off for a process, but it actually doesn't do anything.  This is
2568  * a neutered form of preemptive idiot-proofing.
2569  */
2570 void
2571 disable_msacct(proc_t *p)
2572 {
2573 	kthread_t *t;
2574 
2575 	ASSERT(MUTEX_HELD(&p->p_lock));
2576 
2577 	p->p_flag &= ~SMSACCT;		/* clear process-wide MSACCT */
2578 	/*
2579 	 * Loop through all the LWPs (kernel threads) in the process.
2580 	 */
2581 	if ((t = p->p_tlist) != NULL) {
2582 		do {
2583 			/* clear per-thread flag */
2584 			t->t_proc_flag &= ~TP_MSACCT;
2585 		} while ((t = t->t_forw) != p->p_tlist);
2586 	}
2587 }
2588 
2589 /*
2590  * Return resource usage information.
2591  */
2592 void
2593 prgetusage(kthread_t *t, prhusage_t *pup)
2594 {
2595 	klwp_t *lwp = ttolwp(t);
2596 	hrtime_t *mstimep;
2597 	struct mstate *ms = &lwp->lwp_mstate;
2598 	int state;
2599 	int i;
2600 	hrtime_t curtime;
2601 	hrtime_t waitrq;
2602 	hrtime_t tmp1;
2603 
2604 	curtime = gethrtime_unscaled();
2605 
2606 	pup->pr_lwpid	= t->t_tid;
2607 	pup->pr_count	= 1;
2608 	pup->pr_create	= ms->ms_start;
2609 	pup->pr_term    = ms->ms_term;
2610 	scalehrtime(&pup->pr_create);
2611 	scalehrtime(&pup->pr_term);
2612 	if (ms->ms_term == 0) {
2613 		pup->pr_rtime = curtime - ms->ms_start;
2614 		scalehrtime(&pup->pr_rtime);
2615 	} else {
2616 		pup->pr_rtime = ms->ms_term - ms->ms_start;
2617 		scalehrtime(&pup->pr_rtime);
2618 	}
2619 
2620 
2621 	pup->pr_utime    = ms->ms_acct[LMS_USER];
2622 	pup->pr_stime    = ms->ms_acct[LMS_SYSTEM];
2623 	pup->pr_ttime    = ms->ms_acct[LMS_TRAP];
2624 	pup->pr_tftime   = ms->ms_acct[LMS_TFAULT];
2625 	pup->pr_dftime   = ms->ms_acct[LMS_DFAULT];
2626 	pup->pr_kftime   = ms->ms_acct[LMS_KFAULT];
2627 	pup->pr_ltime    = ms->ms_acct[LMS_USER_LOCK];
2628 	pup->pr_slptime  = ms->ms_acct[LMS_SLEEP];
2629 	pup->pr_wtime    = ms->ms_acct[LMS_WAIT_CPU];
2630 	pup->pr_stoptime = ms->ms_acct[LMS_STOPPED];
2631 
2632 	prscaleusage(pup);
2633 
2634 	/*
2635 	 * Adjust for time waiting in the dispatcher queue.
2636 	 */
2637 	waitrq = t->t_waitrq;	/* hopefully atomic */
2638 	if (waitrq != 0) {
2639 		tmp1 = curtime - waitrq;
2640 		scalehrtime(&tmp1);
2641 		pup->pr_wtime += tmp1;
2642 		curtime = waitrq;
2643 	}
2644 
2645 	/*
2646 	 * Adjust for time spent in current microstate.
2647 	 */
2648 	if (ms->ms_state_start > curtime) {
2649 		curtime = gethrtime_unscaled();
2650 	}
2651 
2652 	i = 0;
2653 	do {
2654 		switch (state = t->t_mstate) {
2655 		case LMS_SLEEP:
2656 			/*
2657 			 * Update the timer for the current sleep state.
2658 			 */
2659 			switch (state = ms->ms_prev) {
2660 			case LMS_TFAULT:
2661 			case LMS_DFAULT:
2662 			case LMS_KFAULT:
2663 			case LMS_USER_LOCK:
2664 				break;
2665 			default:
2666 				state = LMS_SLEEP;
2667 				break;
2668 			}
2669 			break;
2670 		case LMS_TFAULT:
2671 		case LMS_DFAULT:
2672 		case LMS_KFAULT:
2673 		case LMS_USER_LOCK:
2674 			state = LMS_SYSTEM;
2675 			break;
2676 		}
2677 		switch (state) {
2678 		case LMS_USER:		mstimep = &pup->pr_utime;	break;
2679 		case LMS_SYSTEM:	mstimep = &pup->pr_stime;	break;
2680 		case LMS_TRAP:		mstimep = &pup->pr_ttime;	break;
2681 		case LMS_TFAULT:	mstimep = &pup->pr_tftime;	break;
2682 		case LMS_DFAULT:	mstimep = &pup->pr_dftime;	break;
2683 		case LMS_KFAULT:	mstimep = &pup->pr_kftime;	break;
2684 		case LMS_USER_LOCK:	mstimep = &pup->pr_ltime;	break;
2685 		case LMS_SLEEP:		mstimep = &pup->pr_slptime;	break;
2686 		case LMS_WAIT_CPU:	mstimep = &pup->pr_wtime;	break;
2687 		case LMS_STOPPED:	mstimep = &pup->pr_stoptime;	break;
2688 		default:		panic("prgetusage: unknown microstate");
2689 		}
2690 		tmp1 = curtime - ms->ms_state_start;
2691 		if (tmp1 <= 0) {
2692 			curtime = gethrtime_unscaled();
2693 			tmp1 = 0;
2694 			i++;
2695 			continue;
2696 		}
2697 		scalehrtime(&tmp1);
2698 	} while (tmp1 <= 0 && i < MAX_ITERS_SPIN);
2699 
2700 	*mstimep += tmp1;
2701 
2702 	/* update pup timestamp */
2703 	pup->pr_tstamp = curtime;
2704 	scalehrtime(&pup->pr_tstamp);
2705 
2706 	/*
2707 	 * Resource usage counters.
2708 	 */
2709 	pup->pr_minf  = lwp->lwp_ru.minflt;
2710 	pup->pr_majf  = lwp->lwp_ru.majflt;
2711 	pup->pr_nswap = lwp->lwp_ru.nswap;
2712 	pup->pr_inblk = lwp->lwp_ru.inblock;
2713 	pup->pr_oublk = lwp->lwp_ru.oublock;
2714 	pup->pr_msnd  = lwp->lwp_ru.msgsnd;
2715 	pup->pr_mrcv  = lwp->lwp_ru.msgrcv;
2716 	pup->pr_sigs  = lwp->lwp_ru.nsignals;
2717 	pup->pr_vctx  = lwp->lwp_ru.nvcsw;
2718 	pup->pr_ictx  = lwp->lwp_ru.nivcsw;
2719 	pup->pr_sysc  = lwp->lwp_ru.sysc;
2720 	pup->pr_ioch  = lwp->lwp_ru.ioch;
2721 }
2722 
2723 /*
2724  * Convert ms_acct stats from unscaled high-res time to nanoseconds
2725  */
2726 void
2727 prscaleusage(prhusage_t *usg)
2728 {
2729 	scalehrtime(&usg->pr_utime);
2730 	scalehrtime(&usg->pr_stime);
2731 	scalehrtime(&usg->pr_ttime);
2732 	scalehrtime(&usg->pr_tftime);
2733 	scalehrtime(&usg->pr_dftime);
2734 	scalehrtime(&usg->pr_kftime);
2735 	scalehrtime(&usg->pr_ltime);
2736 	scalehrtime(&usg->pr_slptime);
2737 	scalehrtime(&usg->pr_wtime);
2738 	scalehrtime(&usg->pr_stoptime);
2739 }
2740 
2741 
2742 /*
2743  * Sum resource usage information.
2744  */
2745 void
2746 praddusage(kthread_t *t, prhusage_t *pup)
2747 {
2748 	klwp_t *lwp = ttolwp(t);
2749 	hrtime_t *mstimep;
2750 	struct mstate *ms = &lwp->lwp_mstate;
2751 	int state;
2752 	int i;
2753 	hrtime_t curtime;
2754 	hrtime_t waitrq;
2755 	hrtime_t tmp;
2756 	prhusage_t conv;
2757 
2758 	curtime = gethrtime_unscaled();
2759 
2760 	if (ms->ms_term == 0) {
2761 		tmp = curtime - ms->ms_start;
2762 		scalehrtime(&tmp);
2763 		pup->pr_rtime += tmp;
2764 	} else {
2765 		tmp = ms->ms_term - ms->ms_start;
2766 		scalehrtime(&tmp);
2767 		pup->pr_rtime += tmp;
2768 	}
2769 
2770 	conv.pr_utime = ms->ms_acct[LMS_USER];
2771 	conv.pr_stime = ms->ms_acct[LMS_SYSTEM];
2772 	conv.pr_ttime = ms->ms_acct[LMS_TRAP];
2773 	conv.pr_tftime = ms->ms_acct[LMS_TFAULT];
2774 	conv.pr_dftime = ms->ms_acct[LMS_DFAULT];
2775 	conv.pr_kftime = ms->ms_acct[LMS_KFAULT];
2776 	conv.pr_ltime = ms->ms_acct[LMS_USER_LOCK];
2777 	conv.pr_slptime = ms->ms_acct[LMS_SLEEP];
2778 	conv.pr_wtime = ms->ms_acct[LMS_WAIT_CPU];
2779 	conv.pr_stoptime = ms->ms_acct[LMS_STOPPED];
2780 
2781 	prscaleusage(&conv);
2782 
2783 	pup->pr_utime	+= conv.pr_utime;
2784 	pup->pr_stime	+= conv.pr_stime;
2785 	pup->pr_ttime	+= conv.pr_ttime;
2786 	pup->pr_tftime	+= conv.pr_tftime;
2787 	pup->pr_dftime	+= conv.pr_dftime;
2788 	pup->pr_kftime	+= conv.pr_kftime;
2789 	pup->pr_ltime	+= conv.pr_ltime;
2790 	pup->pr_slptime	+= conv.pr_slptime;
2791 	pup->pr_wtime	+= conv.pr_wtime;
2792 	pup->pr_stoptime += conv.pr_stoptime;
2793 
2794 	/*
2795 	 * Adjust for time waiting in the dispatcher queue.
2796 	 */
2797 	waitrq = t->t_waitrq;	/* hopefully atomic */
2798 	if (waitrq != 0) {
2799 		tmp = curtime - waitrq;
2800 		scalehrtime(&tmp);
2801 		pup->pr_wtime += tmp;
2802 		curtime = waitrq;
2803 	}
2804 
2805 	/*
2806 	 * Adjust for time spent in current microstate.
2807 	 */
2808 	if (ms->ms_state_start > curtime) {
2809 		curtime = gethrtime_unscaled();
2810 	}
2811 
2812 	i = 0;
2813 	do {
2814 		switch (state = t->t_mstate) {
2815 		case LMS_SLEEP:
2816 			/*
2817 			 * Update the timer for the current sleep state.
2818 			 */
2819 			switch (state = ms->ms_prev) {
2820 			case LMS_TFAULT:
2821 			case LMS_DFAULT:
2822 			case LMS_KFAULT:
2823 			case LMS_USER_LOCK:
2824 				break;
2825 			default:
2826 				state = LMS_SLEEP;
2827 				break;
2828 			}
2829 			break;
2830 		case LMS_TFAULT:
2831 		case LMS_DFAULT:
2832 		case LMS_KFAULT:
2833 		case LMS_USER_LOCK:
2834 			state = LMS_SYSTEM;
2835 			break;
2836 		}
2837 		switch (state) {
2838 		case LMS_USER:		mstimep = &pup->pr_utime;	break;
2839 		case LMS_SYSTEM:	mstimep = &pup->pr_stime;	break;
2840 		case LMS_TRAP:		mstimep = &pup->pr_ttime;	break;
2841 		case LMS_TFAULT:	mstimep = &pup->pr_tftime;	break;
2842 		case LMS_DFAULT:	mstimep = &pup->pr_dftime;	break;
2843 		case LMS_KFAULT:	mstimep = &pup->pr_kftime;	break;
2844 		case LMS_USER_LOCK:	mstimep = &pup->pr_ltime;	break;
2845 		case LMS_SLEEP:		mstimep = &pup->pr_slptime;	break;
2846 		case LMS_WAIT_CPU:	mstimep = &pup->pr_wtime;	break;
2847 		case LMS_STOPPED:	mstimep = &pup->pr_stoptime;	break;
2848 		default:		panic("praddusage: unknown microstate");
2849 		}
2850 		tmp = curtime - ms->ms_state_start;
2851 		if (tmp <= 0) {
2852 			curtime = gethrtime_unscaled();
2853 			tmp = 0;
2854 			i++;
2855 			continue;
2856 		}
2857 		scalehrtime(&tmp);
2858 	} while (tmp <= 0 && i < MAX_ITERS_SPIN);
2859 
2860 	*mstimep += tmp;
2861 
2862 	/* update pup timestamp */
2863 	pup->pr_tstamp = curtime;
2864 	scalehrtime(&pup->pr_tstamp);
2865 
2866 	/*
2867 	 * Resource usage counters.
2868 	 */
2869 	pup->pr_minf  += lwp->lwp_ru.minflt;
2870 	pup->pr_majf  += lwp->lwp_ru.majflt;
2871 	pup->pr_nswap += lwp->lwp_ru.nswap;
2872 	pup->pr_inblk += lwp->lwp_ru.inblock;
2873 	pup->pr_oublk += lwp->lwp_ru.oublock;
2874 	pup->pr_msnd  += lwp->lwp_ru.msgsnd;
2875 	pup->pr_mrcv  += lwp->lwp_ru.msgrcv;
2876 	pup->pr_sigs  += lwp->lwp_ru.nsignals;
2877 	pup->pr_vctx  += lwp->lwp_ru.nvcsw;
2878 	pup->pr_ictx  += lwp->lwp_ru.nivcsw;
2879 	pup->pr_sysc  += lwp->lwp_ru.sysc;
2880 	pup->pr_ioch  += lwp->lwp_ru.ioch;
2881 }
2882 
2883 /*
2884  * Convert a prhusage_t to a prusage_t.
2885  * This means convert each hrtime_t to a timestruc_t
2886  * and copy the count fields uint64_t => ulong_t.
2887  */
2888 void
2889 prcvtusage(prhusage_t *pup, prusage_t *upup)
2890 {
2891 	uint64_t *ullp;
2892 	ulong_t *ulp;
2893 	int i;
2894 
2895 	upup->pr_lwpid = pup->pr_lwpid;
2896 	upup->pr_count = pup->pr_count;
2897 
2898 	hrt2ts(pup->pr_tstamp,	&upup->pr_tstamp);
2899 	hrt2ts(pup->pr_create,	&upup->pr_create);
2900 	hrt2ts(pup->pr_term,	&upup->pr_term);
2901 	hrt2ts(pup->pr_rtime,	&upup->pr_rtime);
2902 	hrt2ts(pup->pr_utime,	&upup->pr_utime);
2903 	hrt2ts(pup->pr_stime,	&upup->pr_stime);
2904 	hrt2ts(pup->pr_ttime,	&upup->pr_ttime);
2905 	hrt2ts(pup->pr_tftime,	&upup->pr_tftime);
2906 	hrt2ts(pup->pr_dftime,	&upup->pr_dftime);
2907 	hrt2ts(pup->pr_kftime,	&upup->pr_kftime);
2908 	hrt2ts(pup->pr_ltime,	&upup->pr_ltime);
2909 	hrt2ts(pup->pr_slptime,	&upup->pr_slptime);
2910 	hrt2ts(pup->pr_wtime,	&upup->pr_wtime);
2911 	hrt2ts(pup->pr_stoptime, &upup->pr_stoptime);
2912 	bzero(upup->filltime, sizeof (upup->filltime));
2913 
2914 	ullp = &pup->pr_minf;
2915 	ulp = &upup->pr_minf;
2916 	for (i = 0; i < 22; i++)
2917 		*ulp++ = (ulong_t)*ullp++;
2918 }
2919 
2920 #ifdef _SYSCALL32_IMPL
2921 void
2922 prcvtusage32(prhusage_t *pup, prusage32_t *upup)
2923 {
2924 	uint64_t *ullp;
2925 	uint32_t *ulp;
2926 	int i;
2927 
2928 	upup->pr_lwpid = pup->pr_lwpid;
2929 	upup->pr_count = pup->pr_count;
2930 
2931 	hrt2ts32(pup->pr_tstamp,	&upup->pr_tstamp);
2932 	hrt2ts32(pup->pr_create,	&upup->pr_create);
2933 	hrt2ts32(pup->pr_term,		&upup->pr_term);
2934 	hrt2ts32(pup->pr_rtime,		&upup->pr_rtime);
2935 	hrt2ts32(pup->pr_utime,		&upup->pr_utime);
2936 	hrt2ts32(pup->pr_stime,		&upup->pr_stime);
2937 	hrt2ts32(pup->pr_ttime,		&upup->pr_ttime);
2938 	hrt2ts32(pup->pr_tftime,	&upup->pr_tftime);
2939 	hrt2ts32(pup->pr_dftime,	&upup->pr_dftime);
2940 	hrt2ts32(pup->pr_kftime,	&upup->pr_kftime);
2941 	hrt2ts32(pup->pr_ltime,		&upup->pr_ltime);
2942 	hrt2ts32(pup->pr_slptime,	&upup->pr_slptime);
2943 	hrt2ts32(pup->pr_wtime,		&upup->pr_wtime);
2944 	hrt2ts32(pup->pr_stoptime,	&upup->pr_stoptime);
2945 	bzero(upup->filltime, sizeof (upup->filltime));
2946 
2947 	ullp = &pup->pr_minf;
2948 	ulp = &upup->pr_minf;
2949 	for (i = 0; i < 22; i++)
2950 		*ulp++ = (uint32_t)*ullp++;
2951 }
2952 #endif	/* _SYSCALL32_IMPL */
2953 
2954 /*
2955  * Determine whether a set is empty.
2956  */
2957 int
2958 setisempty(uint32_t *sp, uint_t n)
2959 {
2960 	while (n--)
2961 		if (*sp++)
2962 			return (0);
2963 	return (1);
2964 }
2965 
2966 /*
2967  * Utility routine for establishing a watched area in the process.
2968  * Keep the list of watched areas sorted by virtual address.
2969  */
2970 int
2971 set_watched_area(proc_t *p, struct watched_area *pwa)
2972 {
2973 	caddr_t vaddr = pwa->wa_vaddr;
2974 	caddr_t eaddr = pwa->wa_eaddr;
2975 	ulong_t flags = pwa->wa_flags;
2976 	struct watched_area *target;
2977 	avl_index_t where;
2978 	int error = 0;
2979 
2980 	/* we must not be holding p->p_lock, but the process must be locked */
2981 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
2982 	ASSERT(p->p_proc_flag & P_PR_LOCK);
2983 
2984 	/*
2985 	 * If this is our first watchpoint, enable watchpoints for the process.
2986 	 */
2987 	if (!pr_watch_active(p)) {
2988 		kthread_t *t;
2989 
2990 		mutex_enter(&p->p_lock);
2991 		if ((t = p->p_tlist) != NULL) {
2992 			do {
2993 				watch_enable(t);
2994 			} while ((t = t->t_forw) != p->p_tlist);
2995 		}
2996 		mutex_exit(&p->p_lock);
2997 	}
2998 
2999 	target = pr_find_watched_area(p, pwa, &where);
3000 	if (target != NULL) {
3001 		/*
3002 		 * We discovered an existing, overlapping watched area.
3003 		 * Allow it only if it is an exact match.
3004 		 */
3005 		if (target->wa_vaddr != vaddr ||
3006 		    target->wa_eaddr != eaddr)
3007 			error = EINVAL;
3008 		else if (target->wa_flags != flags) {
3009 			error = set_watched_page(p, vaddr, eaddr,
3010 			    flags, target->wa_flags);
3011 			target->wa_flags = flags;
3012 		}
3013 		kmem_free(pwa, sizeof (struct watched_area));
3014 	} else {
3015 		avl_insert(&p->p_warea, pwa, where);
3016 		error = set_watched_page(p, vaddr, eaddr, flags, 0);
3017 	}
3018 
3019 	return (error);
3020 }
3021 
3022 /*
3023  * Utility routine for clearing a watched area in the process.
3024  * Must be an exact match of the virtual address.
3025  * size and flags don't matter.
3026  */
3027 int
3028 clear_watched_area(proc_t *p, struct watched_area *pwa)
3029 {
3030 	struct watched_area *found;
3031 
3032 	/* we must not be holding p->p_lock, but the process must be locked */
3033 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
3034 	ASSERT(p->p_proc_flag & P_PR_LOCK);
3035 
3036 
3037 	if (!pr_watch_active(p)) {
3038 		kmem_free(pwa, sizeof (struct watched_area));
3039 		return (0);
3040 	}
3041 
3042 	/*
3043 	 * Look for a matching address in the watched areas.  If a match is
3044 	 * found, clear the old watched area and adjust the watched page(s).  It
3045 	 * is not an error if there is no match.
3046 	 */
3047 	if ((found = pr_find_watched_area(p, pwa, NULL)) != NULL &&
3048 	    found->wa_vaddr == pwa->wa_vaddr) {
3049 		clear_watched_page(p, found->wa_vaddr, found->wa_eaddr,
3050 		    found->wa_flags);
3051 		avl_remove(&p->p_warea, found);
3052 		kmem_free(found, sizeof (struct watched_area));
3053 	}
3054 
3055 	kmem_free(pwa, sizeof (struct watched_area));
3056 
3057 	/*
3058 	 * If we removed the last watched area from the process, disable
3059 	 * watchpoints.
3060 	 */
3061 	if (!pr_watch_active(p)) {
3062 		kthread_t *t;
3063 
3064 		mutex_enter(&p->p_lock);
3065 		if ((t = p->p_tlist) != NULL) {
3066 			do {
3067 				watch_disable(t);
3068 			} while ((t = t->t_forw) != p->p_tlist);
3069 		}
3070 		mutex_exit(&p->p_lock);
3071 	}
3072 
3073 	return (0);
3074 }
3075 
3076 /*
3077  * Frees all the watched_area structures
3078  */
3079 void
3080 pr_free_watchpoints(proc_t *p)
3081 {
3082 	struct watched_area *delp;
3083 	void *cookie;
3084 
3085 	cookie = NULL;
3086 	while ((delp = avl_destroy_nodes(&p->p_warea, &cookie)) != NULL)
3087 		kmem_free(delp, sizeof (struct watched_area));
3088 
3089 	avl_destroy(&p->p_warea);
3090 }
3091 
3092 /*
3093  * This one is called by the traced process to unwatch all the
3094  * pages while deallocating the list of watched_page structs.
3095  */
3096 void
3097 pr_free_watched_pages(proc_t *p)
3098 {
3099 	struct as *as = p->p_as;
3100 	struct watched_page *pwp;
3101 	uint_t prot;
3102 	int    retrycnt, err;
3103 	void *cookie;
3104 
3105 	if (as == NULL || avl_numnodes(&as->a_wpage) == 0)
3106 		return;
3107 
3108 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
3109 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3110 
3111 	pwp = avl_first(&as->a_wpage);
3112 
3113 	cookie = NULL;
3114 	while ((pwp = avl_destroy_nodes(&as->a_wpage, &cookie)) != NULL) {
3115 		retrycnt = 0;
3116 		if ((prot = pwp->wp_oprot) != 0) {
3117 			caddr_t addr = pwp->wp_vaddr;
3118 			struct seg *seg;
3119 		retry:
3120 
3121 			if ((pwp->wp_prot != prot ||
3122 			    (pwp->wp_flags & WP_NOWATCH)) &&
3123 			    (seg = as_segat(as, addr)) != NULL) {
3124 				err = SEGOP_SETPROT(seg, addr, PAGESIZE, prot);
3125 				if (err == IE_RETRY) {
3126 					ASSERT(retrycnt == 0);
3127 					retrycnt++;
3128 					goto retry;
3129 				}
3130 			}
3131 		}
3132 		kmem_free(pwp, sizeof (struct watched_page));
3133 	}
3134 
3135 	avl_destroy(&as->a_wpage);
3136 	p->p_wprot = NULL;
3137 
3138 	AS_LOCK_EXIT(as, &as->a_lock);
3139 }
3140 
3141 /*
3142  * Insert a watched area into the list of watched pages.
3143  * If oflags is zero then we are adding a new watched area.
3144  * Otherwise we are changing the flags of an existing watched area.
3145  */
3146 static int
3147 set_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr,
3148 	ulong_t flags, ulong_t oflags)
3149 {
3150 	struct as *as = p->p_as;
3151 	avl_tree_t *pwp_tree;
3152 	struct watched_page *pwp, *newpwp;
3153 	struct watched_page tpw;
3154 	avl_index_t where;
3155 	struct seg *seg;
3156 	uint_t prot;
3157 	caddr_t addr;
3158 
3159 	/*
3160 	 * We need to pre-allocate a list of structures before we grab the
3161 	 * address space lock to avoid calling kmem_alloc(KM_SLEEP) with locks
3162 	 * held.
3163 	 */
3164 	newpwp = NULL;
3165 	for (addr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3166 	    addr < eaddr; addr += PAGESIZE) {
3167 		pwp = kmem_zalloc(sizeof (struct watched_page), KM_SLEEP);
3168 		pwp->wp_list = newpwp;
3169 		newpwp = pwp;
3170 	}
3171 
3172 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3173 
3174 	/*
3175 	 * Search for an existing watched page to contain the watched area.
3176 	 * If none is found, grab a new one from the available list
3177 	 * and insert it in the active list, keeping the list sorted
3178 	 * by user-level virtual address.
3179 	 */
3180 	if (p->p_flag & SVFWAIT)
3181 		pwp_tree = &p->p_wpage;
3182 	else
3183 		pwp_tree = &as->a_wpage;
3184 
3185 again:
3186 	if (avl_numnodes(pwp_tree) > prnwatch) {
3187 		AS_LOCK_EXIT(as, &as->a_lock);
3188 		while (newpwp != NULL) {
3189 			pwp = newpwp->wp_list;
3190 			kmem_free(newpwp, sizeof (struct watched_page));
3191 			newpwp = pwp;
3192 		}
3193 		return (E2BIG);
3194 	}
3195 
3196 	tpw.wp_vaddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3197 	if ((pwp = avl_find(pwp_tree, &tpw, &where)) == NULL) {
3198 		pwp = newpwp;
3199 		newpwp = newpwp->wp_list;
3200 		pwp->wp_list = NULL;
3201 		pwp->wp_vaddr = (caddr_t)((uintptr_t)vaddr &
3202 		    (uintptr_t)PAGEMASK);
3203 		avl_insert(pwp_tree, pwp, where);
3204 	}
3205 
3206 	ASSERT(vaddr >= pwp->wp_vaddr && vaddr < pwp->wp_vaddr + PAGESIZE);
3207 
3208 	if (oflags & WA_READ)
3209 		pwp->wp_read--;
3210 	if (oflags & WA_WRITE)
3211 		pwp->wp_write--;
3212 	if (oflags & WA_EXEC)
3213 		pwp->wp_exec--;
3214 
3215 	ASSERT(pwp->wp_read >= 0);
3216 	ASSERT(pwp->wp_write >= 0);
3217 	ASSERT(pwp->wp_exec >= 0);
3218 
3219 	if (flags & WA_READ)
3220 		pwp->wp_read++;
3221 	if (flags & WA_WRITE)
3222 		pwp->wp_write++;
3223 	if (flags & WA_EXEC)
3224 		pwp->wp_exec++;
3225 
3226 	if (!(p->p_flag & SVFWAIT)) {
3227 		vaddr = pwp->wp_vaddr;
3228 		if (pwp->wp_oprot == 0 &&
3229 		    (seg = as_segat(as, vaddr)) != NULL) {
3230 			SEGOP_GETPROT(seg, vaddr, 0, &prot);
3231 			pwp->wp_oprot = (uchar_t)prot;
3232 			pwp->wp_prot = (uchar_t)prot;
3233 		}
3234 		if (pwp->wp_oprot != 0) {
3235 			prot = pwp->wp_oprot;
3236 			if (pwp->wp_read)
3237 				prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3238 			if (pwp->wp_write)
3239 				prot &= ~PROT_WRITE;
3240 			if (pwp->wp_exec)
3241 				prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3242 			if (!(pwp->wp_flags & WP_NOWATCH) &&
3243 			    pwp->wp_prot != prot &&
3244 			    (pwp->wp_flags & WP_SETPROT) == 0) {
3245 				pwp->wp_flags |= WP_SETPROT;
3246 				pwp->wp_list = p->p_wprot;
3247 				p->p_wprot = pwp;
3248 			}
3249 			pwp->wp_prot = (uchar_t)prot;
3250 		}
3251 	}
3252 
3253 	/*
3254 	 * If the watched area extends into the next page then do
3255 	 * it over again with the virtual address of the next page.
3256 	 */
3257 	if ((vaddr = pwp->wp_vaddr + PAGESIZE) < eaddr)
3258 		goto again;
3259 
3260 	AS_LOCK_EXIT(as, &as->a_lock);
3261 
3262 	/*
3263 	 * Free any pages we may have over-allocated
3264 	 */
3265 	while (newpwp != NULL) {
3266 		pwp = newpwp->wp_list;
3267 		kmem_free(newpwp, sizeof (struct watched_page));
3268 		newpwp = pwp;
3269 	}
3270 
3271 	return (0);
3272 }
3273 
3274 /*
3275  * Remove a watched area from the list of watched pages.
3276  * A watched area may extend over more than one page.
3277  */
3278 static void
3279 clear_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr, ulong_t flags)
3280 {
3281 	struct as *as = p->p_as;
3282 	struct watched_page *pwp;
3283 	struct watched_page tpw;
3284 	avl_tree_t *tree;
3285 	avl_index_t where;
3286 
3287 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3288 
3289 	if (p->p_flag & SVFWAIT)
3290 		tree = &p->p_wpage;
3291 	else
3292 		tree = &as->a_wpage;
3293 
3294 	tpw.wp_vaddr = vaddr =
3295 	    (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3296 	pwp = avl_find(tree, &tpw, &where);
3297 	if (pwp == NULL)
3298 		pwp = avl_nearest(tree, where, AVL_AFTER);
3299 
3300 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
3301 		ASSERT(vaddr <=  pwp->wp_vaddr);
3302 
3303 		if (flags & WA_READ)
3304 			pwp->wp_read--;
3305 		if (flags & WA_WRITE)
3306 			pwp->wp_write--;
3307 		if (flags & WA_EXEC)
3308 			pwp->wp_exec--;
3309 
3310 		if (pwp->wp_read + pwp->wp_write + pwp->wp_exec != 0) {
3311 			/*
3312 			 * Reset the hat layer's protections on this page.
3313 			 */
3314 			if (pwp->wp_oprot != 0) {
3315 				uint_t prot = pwp->wp_oprot;
3316 
3317 				if (pwp->wp_read)
3318 					prot &=
3319 					    ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3320 				if (pwp->wp_write)
3321 					prot &= ~PROT_WRITE;
3322 				if (pwp->wp_exec)
3323 					prot &=
3324 					    ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3325 				if (!(pwp->wp_flags & WP_NOWATCH) &&
3326 				    pwp->wp_prot != prot &&
3327 				    (pwp->wp_flags & WP_SETPROT) == 0) {
3328 					pwp->wp_flags |= WP_SETPROT;
3329 					pwp->wp_list = p->p_wprot;
3330 					p->p_wprot = pwp;
3331 				}
3332 				pwp->wp_prot = (uchar_t)prot;
3333 			}
3334 		} else {
3335 			/*
3336 			 * No watched areas remain in this page.
3337 			 * Reset everything to normal.
3338 			 */
3339 			if (pwp->wp_oprot != 0) {
3340 				pwp->wp_prot = pwp->wp_oprot;
3341 				if ((pwp->wp_flags & WP_SETPROT) == 0) {
3342 					pwp->wp_flags |= WP_SETPROT;
3343 					pwp->wp_list = p->p_wprot;
3344 					p->p_wprot = pwp;
3345 				}
3346 			}
3347 		}
3348 
3349 		pwp = AVL_NEXT(tree, pwp);
3350 	}
3351 
3352 	AS_LOCK_EXIT(as, &as->a_lock);
3353 }
3354 
3355 /*
3356  * Return the original protections for the specified page.
3357  */
3358 static void
3359 getwatchprot(struct as *as, caddr_t addr, uint_t *prot)
3360 {
3361 	struct watched_page *pwp;
3362 	struct watched_page tpw;
3363 
3364 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
3365 
3366 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3367 	if ((pwp = avl_find(&as->a_wpage, &tpw, NULL)) != NULL)
3368 		*prot = pwp->wp_oprot;
3369 }
3370 
3371 static prpagev_t *
3372 pr_pagev_create(struct seg *seg, int check_noreserve)
3373 {
3374 	prpagev_t *pagev = kmem_alloc(sizeof (prpagev_t), KM_SLEEP);
3375 	size_t total_pages = seg_pages(seg);
3376 
3377 	/*
3378 	 * Limit the size of our vectors to pagev_lim pages at a time.  We need
3379 	 * 4 or 5 bytes of storage per page, so this means we limit ourself
3380 	 * to about a megabyte of kernel heap by default.
3381 	 */
3382 	pagev->pg_npages = MIN(total_pages, pagev_lim);
3383 	pagev->pg_pnbase = 0;
3384 
3385 	pagev->pg_protv =
3386 	    kmem_alloc(pagev->pg_npages * sizeof (uint_t), KM_SLEEP);
3387 
3388 	if (check_noreserve)
3389 		pagev->pg_incore =
3390 		    kmem_alloc(pagev->pg_npages * sizeof (char), KM_SLEEP);
3391 	else
3392 		pagev->pg_incore = NULL;
3393 
3394 	return (pagev);
3395 }
3396 
3397 static void
3398 pr_pagev_destroy(prpagev_t *pagev)
3399 {
3400 	if (pagev->pg_incore != NULL)
3401 		kmem_free(pagev->pg_incore, pagev->pg_npages * sizeof (char));
3402 
3403 	kmem_free(pagev->pg_protv, pagev->pg_npages * sizeof (uint_t));
3404 	kmem_free(pagev, sizeof (prpagev_t));
3405 }
3406 
3407 static caddr_t
3408 pr_pagev_fill(prpagev_t *pagev, struct seg *seg, caddr_t addr, caddr_t eaddr)
3409 {
3410 	ulong_t lastpg = seg_page(seg, eaddr - 1);
3411 	ulong_t pn, pnlim;
3412 	caddr_t saddr;
3413 	size_t len;
3414 
3415 	ASSERT(addr >= seg->s_base && addr <= eaddr);
3416 
3417 	if (addr == eaddr)
3418 		return (eaddr);
3419 
3420 refill:
3421 	ASSERT(addr < eaddr);
3422 	pagev->pg_pnbase = seg_page(seg, addr);
3423 	pnlim = pagev->pg_pnbase + pagev->pg_npages;
3424 	saddr = addr;
3425 
3426 	if (lastpg < pnlim)
3427 		len = (size_t)(eaddr - addr);
3428 	else
3429 		len = pagev->pg_npages * PAGESIZE;
3430 
3431 	if (pagev->pg_incore != NULL) {
3432 		/*
3433 		 * INCORE cleverly has different semantics than GETPROT:
3434 		 * it returns info on pages up to but NOT including addr + len.
3435 		 */
3436 		SEGOP_INCORE(seg, addr, len, pagev->pg_incore);
3437 		pn = pagev->pg_pnbase;
3438 
3439 		do {
3440 			/*
3441 			 * Guilty knowledge here:  We know that segvn_incore
3442 			 * returns more than just the low-order bit that
3443 			 * indicates the page is actually in memory.  If any
3444 			 * bits are set, then the page has backing store.
3445 			 */
3446 			if (pagev->pg_incore[pn++ - pagev->pg_pnbase])
3447 				goto out;
3448 
3449 		} while ((addr += PAGESIZE) < eaddr && pn < pnlim);
3450 
3451 		/*
3452 		 * If we examined all the pages in the vector but we're not
3453 		 * at the end of the segment, take another lap.
3454 		 */
3455 		if (addr < eaddr)
3456 			goto refill;
3457 	}
3458 
3459 	/*
3460 	 * Need to take len - 1 because addr + len is the address of the
3461 	 * first byte of the page just past the end of what we want.
3462 	 */
3463 out:
3464 	SEGOP_GETPROT(seg, saddr, len - 1, pagev->pg_protv);
3465 	return (addr);
3466 }
3467 
3468 static caddr_t
3469 pr_pagev_nextprot(prpagev_t *pagev, struct seg *seg,
3470     caddr_t *saddrp, caddr_t eaddr, uint_t *protp)
3471 {
3472 	/*
3473 	 * Our starting address is either the specified address, or the base
3474 	 * address from the start of the pagev.  If the latter is greater,
3475 	 * this means a previous call to pr_pagev_fill has already scanned
3476 	 * further than the end of the previous mapping.
3477 	 */
3478 	caddr_t base = seg->s_base + pagev->pg_pnbase * PAGESIZE;
3479 	caddr_t addr = MAX(*saddrp, base);
3480 	ulong_t pn = seg_page(seg, addr);
3481 	uint_t prot, nprot;
3482 
3483 	/*
3484 	 * If we're dealing with noreserve pages, then advance addr to
3485 	 * the address of the next page which has backing store.
3486 	 */
3487 	if (pagev->pg_incore != NULL) {
3488 		while (pagev->pg_incore[pn - pagev->pg_pnbase] == 0) {
3489 			if ((addr += PAGESIZE) == eaddr) {
3490 				*saddrp = addr;
3491 				prot = 0;
3492 				goto out;
3493 			}
3494 			if (++pn == pagev->pg_pnbase + pagev->pg_npages) {
3495 				addr = pr_pagev_fill(pagev, seg, addr, eaddr);
3496 				if (addr == eaddr) {
3497 					*saddrp = addr;
3498 					prot = 0;
3499 					goto out;
3500 				}
3501 				pn = seg_page(seg, addr);
3502 			}
3503 		}
3504 	}
3505 
3506 	/*
3507 	 * Get the protections on the page corresponding to addr.
3508 	 */
3509 	pn = seg_page(seg, addr);
3510 	ASSERT(pn >= pagev->pg_pnbase);
3511 	ASSERT(pn < (pagev->pg_pnbase + pagev->pg_npages));
3512 
3513 	prot = pagev->pg_protv[pn - pagev->pg_pnbase];
3514 	getwatchprot(seg->s_as, addr, &prot);
3515 	*saddrp = addr;
3516 
3517 	/*
3518 	 * Now loop until we find a backed page with different protections
3519 	 * or we reach the end of this segment.
3520 	 */
3521 	while ((addr += PAGESIZE) < eaddr) {
3522 		/*
3523 		 * If pn has advanced to the page number following what we
3524 		 * have information on, refill the page vector and reset
3525 		 * addr and pn.  If pr_pagev_fill does not return the
3526 		 * address of the next page, we have a discontiguity and
3527 		 * thus have reached the end of the current mapping.
3528 		 */
3529 		if (++pn == pagev->pg_pnbase + pagev->pg_npages) {
3530 			caddr_t naddr = pr_pagev_fill(pagev, seg, addr, eaddr);
3531 			if (naddr != addr)
3532 				goto out;
3533 			pn = seg_page(seg, addr);
3534 		}
3535 
3536 		/*
3537 		 * The previous page's protections are in prot, and it has
3538 		 * backing.  If this page is MAP_NORESERVE and has no backing,
3539 		 * then end this mapping and return the previous protections.
3540 		 */
3541 		if (pagev->pg_incore != NULL &&
3542 		    pagev->pg_incore[pn - pagev->pg_pnbase] == 0)
3543 			break;
3544 
3545 		/*
3546 		 * Otherwise end the mapping if this page's protections (nprot)
3547 		 * are different than those in the previous page (prot).
3548 		 */
3549 		nprot = pagev->pg_protv[pn - pagev->pg_pnbase];
3550 		getwatchprot(seg->s_as, addr, &nprot);
3551 
3552 		if (nprot != prot)
3553 			break;
3554 	}
3555 
3556 out:
3557 	*protp = prot;
3558 	return (addr);
3559 }
3560 
3561 size_t
3562 pr_getsegsize(struct seg *seg, int reserved)
3563 {
3564 	size_t size = seg->s_size;
3565 
3566 	/*
3567 	 * If we're interested in the reserved space, return the size of the
3568 	 * segment itself.  Everything else in this function is a special case
3569 	 * to determine the actual underlying size of various segment types.
3570 	 */
3571 	if (reserved)
3572 		return (size);
3573 
3574 	/*
3575 	 * If this is a segvn mapping of a regular file, return the smaller
3576 	 * of the segment size and the remaining size of the file beyond
3577 	 * the file offset corresponding to seg->s_base.
3578 	 */
3579 	if (seg->s_ops == &segvn_ops) {
3580 		vattr_t vattr;
3581 		vnode_t *vp;
3582 
3583 		vattr.va_mask = AT_SIZE;
3584 
3585 		if (SEGOP_GETVP(seg, seg->s_base, &vp) == 0 &&
3586 		    vp != NULL && vp->v_type == VREG &&
3587 		    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
3588 
3589 			u_offset_t fsize = vattr.va_size;
3590 			u_offset_t offset = SEGOP_GETOFFSET(seg, seg->s_base);
3591 
3592 			if (fsize < offset)
3593 				fsize = 0;
3594 			else
3595 				fsize -= offset;
3596 
3597 			fsize = roundup(fsize, (u_offset_t)PAGESIZE);
3598 
3599 			if (fsize < (u_offset_t)size)
3600 				size = (size_t)fsize;
3601 		}
3602 
3603 		return (size);
3604 	}
3605 
3606 	/*
3607 	 * If this is an ISM shared segment, don't include pages that are
3608 	 * beyond the real size of the spt segment that backs it.
3609 	 */
3610 	if (seg->s_ops == &segspt_shmops)
3611 		return (MIN(spt_realsize(seg), size));
3612 
3613 	/*
3614 	 * If this is segment is a mapping from /dev/null, then this is a
3615 	 * reservation of virtual address space and has no actual size.
3616 	 * Such segments are backed by segdev and have type set to neither
3617 	 * MAP_SHARED nor MAP_PRIVATE.
3618 	 */
3619 	if (seg->s_ops == &segdev_ops &&
3620 	    ((SEGOP_GETTYPE(seg, seg->s_base) &
3621 		(MAP_SHARED | MAP_PRIVATE)) == 0))
3622 		return (0);
3623 
3624 	/*
3625 	 * If this segment doesn't match one of the special types we handle,
3626 	 * just return the size of the segment itself.
3627 	 */
3628 	return (size);
3629 }
3630 
3631 uint_t
3632 pr_getprot(struct seg *seg, int reserved, void **tmp,
3633 	caddr_t *saddrp, caddr_t *naddrp, caddr_t eaddr)
3634 {
3635 	struct as *as = seg->s_as;
3636 
3637 	caddr_t saddr = *saddrp;
3638 	caddr_t naddr;
3639 
3640 	int check_noreserve;
3641 	uint_t prot;
3642 
3643 	union {
3644 		struct segvn_data *svd;
3645 		struct segdev_data *sdp;
3646 		void *data;
3647 	} s;
3648 
3649 	s.data = seg->s_data;
3650 
3651 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
3652 	ASSERT(saddr >= seg->s_base && saddr < eaddr);
3653 	ASSERT(eaddr <= seg->s_base + seg->s_size);
3654 
3655 	/*
3656 	 * Don't include MAP_NORESERVE pages in the address range
3657 	 * unless their mappings have actually materialized.
3658 	 * We cheat by knowing that segvn is the only segment
3659 	 * driver that supports MAP_NORESERVE.
3660 	 */
3661 	check_noreserve =
3662 	    (!reserved && seg->s_ops == &segvn_ops && s.svd != NULL &&
3663 	    (s.svd->vp == NULL || s.svd->vp->v_type != VREG) &&
3664 	    (s.svd->flags & MAP_NORESERVE));
3665 
3666 	/*
3667 	 * Examine every page only as a last resort.  We use guilty knowledge
3668 	 * of segvn and segdev to avoid this: if there are no per-page
3669 	 * protections present in the segment and we don't care about
3670 	 * MAP_NORESERVE, then s_data->prot is the prot for the whole segment.
3671 	 */
3672 	if (!check_noreserve && saddr == seg->s_base &&
3673 	    seg->s_ops == &segvn_ops && s.svd != NULL && s.svd->pageprot == 0) {
3674 		prot = s.svd->prot;
3675 		getwatchprot(as, saddr, &prot);
3676 		naddr = eaddr;
3677 
3678 	} else if (saddr == seg->s_base && seg->s_ops == &segdev_ops &&
3679 	    s.sdp != NULL && s.sdp->pageprot == 0) {
3680 		prot = s.sdp->prot;
3681 		getwatchprot(as, saddr, &prot);
3682 		naddr = eaddr;
3683 
3684 	} else {
3685 		prpagev_t *pagev;
3686 
3687 		/*
3688 		 * If addr is sitting at the start of the segment, then
3689 		 * create a page vector to store protection and incore
3690 		 * information for pages in the segment, and fill it.
3691 		 * Otherwise, we expect *tmp to address the prpagev_t
3692 		 * allocated by a previous call to this function.
3693 		 */
3694 		if (saddr == seg->s_base) {
3695 			pagev = pr_pagev_create(seg, check_noreserve);
3696 			saddr = pr_pagev_fill(pagev, seg, saddr, eaddr);
3697 
3698 			ASSERT(*tmp == NULL);
3699 			*tmp = pagev;
3700 
3701 			ASSERT(saddr <= eaddr);
3702 			*saddrp = saddr;
3703 
3704 			if (saddr == eaddr) {
3705 				naddr = saddr;
3706 				prot = 0;
3707 				goto out;
3708 			}
3709 
3710 		} else {
3711 			ASSERT(*tmp != NULL);
3712 			pagev = (prpagev_t *)*tmp;
3713 		}
3714 
3715 		naddr = pr_pagev_nextprot(pagev, seg, saddrp, eaddr, &prot);
3716 		ASSERT(naddr <= eaddr);
3717 	}
3718 
3719 out:
3720 	if (naddr == eaddr)
3721 		pr_getprot_done(tmp);
3722 	*naddrp = naddr;
3723 	return (prot);
3724 }
3725 
3726 void
3727 pr_getprot_done(void **tmp)
3728 {
3729 	if (*tmp != NULL) {
3730 		pr_pagev_destroy((prpagev_t *)*tmp);
3731 		*tmp = NULL;
3732 	}
3733 }
3734 
3735 /*
3736  * Return true iff the vnode is a /proc file from the object directory.
3737  */
3738 int
3739 pr_isobject(vnode_t *vp)
3740 {
3741 	return (vn_matchops(vp, prvnodeops) && VTOP(vp)->pr_type == PR_OBJECT);
3742 }
3743 
3744 /*
3745  * Return true iff the vnode is a /proc file opened by the process itself.
3746  */
3747 int
3748 pr_isself(vnode_t *vp)
3749 {
3750 	/*
3751 	 * XXX: To retain binary compatibility with the old
3752 	 * ioctl()-based version of /proc, we exempt self-opens
3753 	 * of /proc/<pid> from being marked close-on-exec.
3754 	 */
3755 	return (vn_matchops(vp, prvnodeops) &&
3756 	    (VTOP(vp)->pr_flags & PR_ISSELF) &&
3757 	    VTOP(vp)->pr_type != PR_PIDDIR);
3758 }
3759 
3760 static ssize_t
3761 pr_getpagesize(struct seg *seg, caddr_t saddr, caddr_t *naddrp, caddr_t eaddr)
3762 {
3763 	ssize_t pagesize, hatsize;
3764 
3765 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3766 	ASSERT(IS_P2ALIGNED(saddr, PAGESIZE));
3767 	ASSERT(IS_P2ALIGNED(eaddr, PAGESIZE));
3768 	ASSERT(saddr < eaddr);
3769 
3770 	pagesize = hatsize = hat_getpagesize(seg->s_as->a_hat, saddr);
3771 	ASSERT(pagesize == -1 || IS_P2ALIGNED(pagesize, pagesize));
3772 	ASSERT(pagesize != 0);
3773 
3774 	if (pagesize == -1)
3775 		pagesize = PAGESIZE;
3776 
3777 	saddr += P2NPHASE((uintptr_t)saddr, pagesize);
3778 
3779 	while (saddr < eaddr) {
3780 		if (hatsize != hat_getpagesize(seg->s_as->a_hat, saddr))
3781 			break;
3782 		ASSERT(IS_P2ALIGNED(saddr, pagesize));
3783 		saddr += pagesize;
3784 	}
3785 
3786 	*naddrp = ((saddr < eaddr) ? saddr : eaddr);
3787 	return (hatsize);
3788 }
3789 
3790 /*
3791  * Return an array of structures with extended memory map information.
3792  * We allocate here; the caller must deallocate.
3793  */
3794 int
3795 prgetxmap(proc_t *p, prxmap_t **prxmapp, size_t *sizep)
3796 {
3797 	struct as *as = p->p_as;
3798 	int nmaps = 0;
3799 	prxmap_t *mp;
3800 	size_t size;
3801 	struct seg *seg;
3802 	struct seg *brkseg, *stkseg;
3803 	struct vnode *vp;
3804 	struct vattr vattr;
3805 	uint_t prot;
3806 
3807 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3808 
3809 	/* initial allocation */
3810 	*sizep = size = INITIAL_MAPSIZE;
3811 	*prxmapp = mp = kmem_alloc(size, KM_SLEEP);
3812 
3813 	if ((seg = AS_SEGFIRST(as)) == NULL)
3814 		return (0);
3815 
3816 	brkseg = break_seg(p);
3817 	stkseg = as_segat(as, prgetstackbase(p));
3818 
3819 	do {
3820 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3821 		caddr_t saddr, naddr, baddr;
3822 		void *tmp = NULL;
3823 		ssize_t psz;
3824 		char *parr;
3825 		uint64_t npages;
3826 		uint64_t pagenum;
3827 
3828 		/*
3829 		 * Segment loop part one: iterate from the base of the segment
3830 		 * to its end, pausing at each address boundary (baddr) between
3831 		 * ranges that have different virtual memory protections.
3832 		 */
3833 		for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) {
3834 			prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr);
3835 			ASSERT(baddr >= saddr && baddr <= eaddr);
3836 
3837 			/*
3838 			 * Segment loop part two: iterate from the current
3839 			 * position to the end of the protection boundary,
3840 			 * pausing at each address boundary (naddr) between
3841 			 * ranges that have different underlying page sizes.
3842 			 */
3843 			for (; saddr < baddr; saddr = naddr) {
3844 				psz = pr_getpagesize(seg, saddr, &naddr, baddr);
3845 				ASSERT(naddr >= saddr && naddr <= baddr);
3846 
3847 				/* reallocate if necessary */
3848 				if ((nmaps + 1) * sizeof (prxmap_t) > size) {
3849 					size_t newsize = size + 3 * size / 16;
3850 					prxmap_t *newmp =
3851 					    kmem_alloc(newsize, KM_SLEEP);
3852 
3853 					bcopy(*prxmapp, newmp,
3854 					    nmaps * sizeof (prxmap_t));
3855 					kmem_free(*prxmapp, size);
3856 					*sizep = size = newsize;
3857 					*prxmapp = newmp;
3858 					mp = newmp + nmaps;
3859 				}
3860 
3861 				bzero(mp, sizeof (*mp));
3862 				mp->pr_vaddr = (uintptr_t)saddr;
3863 				mp->pr_size = naddr - saddr;
3864 				mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
3865 				mp->pr_mflags = 0;
3866 				if (prot & PROT_READ)
3867 					mp->pr_mflags |= MA_READ;
3868 				if (prot & PROT_WRITE)
3869 					mp->pr_mflags |= MA_WRITE;
3870 				if (prot & PROT_EXEC)
3871 					mp->pr_mflags |= MA_EXEC;
3872 				if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3873 					mp->pr_mflags |= MA_SHARED;
3874 				if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
3875 					mp->pr_mflags |= MA_NORESERVE;
3876 				if (seg->s_ops == &segspt_shmops ||
3877 				    (seg->s_ops == &segvn_ops &&
3878 				    (SEGOP_GETVP(seg, saddr, &vp) != 0 ||
3879 				    vp == NULL)))
3880 					mp->pr_mflags |= MA_ANON;
3881 				if (seg == brkseg)
3882 					mp->pr_mflags |= MA_BREAK;
3883 				else if (seg == stkseg)
3884 					mp->pr_mflags |= MA_STACK;
3885 				if (seg->s_ops == &segspt_shmops)
3886 					mp->pr_mflags |= MA_ISM | MA_SHM;
3887 
3888 				mp->pr_pagesize = PAGESIZE;
3889 				if (psz == -1) {
3890 					mp->pr_hatpagesize = 0;
3891 				} else {
3892 					mp->pr_hatpagesize = psz;
3893 				}
3894 
3895 				/*
3896 				 * Manufacture a filename for the "object" dir.
3897 				 */
3898 				mp->pr_dev = PRNODEV;
3899 				vattr.va_mask = AT_FSID|AT_NODEID;
3900 				if (seg->s_ops == &segvn_ops &&
3901 				    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
3902 				    vp != NULL && vp->v_type == VREG &&
3903 				    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
3904 					mp->pr_dev = vattr.va_fsid;
3905 					mp->pr_ino = vattr.va_nodeid;
3906 					if (vp == p->p_exec)
3907 						(void) strcpy(mp->pr_mapname,
3908 						    "a.out");
3909 					else
3910 						pr_object_name(mp->pr_mapname,
3911 						    vp, &vattr);
3912 				}
3913 
3914 				/*
3915 				 * Get the SysV shared memory id, if any.
3916 				 */
3917 				if ((mp->pr_mflags & MA_SHARED) &&
3918 				    p->p_segacct && (mp->pr_shmid = shmgetid(p,
3919 				    seg->s_base)) != SHMID_NONE) {
3920 					if (mp->pr_shmid == SHMID_FREE)
3921 						mp->pr_shmid = -1;
3922 
3923 					mp->pr_mflags |= MA_SHM;
3924 				} else {
3925 					mp->pr_shmid = -1;
3926 				}
3927 
3928 				npages = ((uintptr_t)(naddr - saddr)) >>
3929 				    PAGESHIFT;
3930 				parr = kmem_zalloc(npages, KM_SLEEP);
3931 
3932 				SEGOP_INCORE(seg, saddr, naddr - saddr, parr);
3933 
3934 				for (pagenum = 0; pagenum < npages; pagenum++) {
3935 					if (parr[pagenum] & SEG_PAGE_INCORE)
3936 						mp->pr_rss++;
3937 					if (parr[pagenum] & SEG_PAGE_ANON)
3938 						mp->pr_anon++;
3939 					if (parr[pagenum] & SEG_PAGE_LOCKED)
3940 						mp->pr_locked++;
3941 				}
3942 				kmem_free(parr, npages);
3943 				mp++;
3944 				nmaps++;
3945 			}
3946 		}
3947 		ASSERT(tmp == NULL);
3948 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3949 
3950 	return (nmaps);
3951 }
3952 
3953 /*
3954  * Return the process's credentials.  We don't need a 32-bit equivalent of
3955  * this function because prcred_t and prcred32_t are actually the same.
3956  */
3957 void
3958 prgetcred(proc_t *p, prcred_t *pcrp)
3959 {
3960 	mutex_enter(&p->p_crlock);
3961 	cred2prcred(p->p_cred, pcrp);
3962 	mutex_exit(&p->p_crlock);
3963 }
3964 
3965 /*
3966  * Compute actual size of the prpriv_t structure.
3967  */
3968 
3969 size_t
3970 prgetprivsize(void)
3971 {
3972 	return (priv_prgetprivsize(NULL));
3973 }
3974 
3975 /*
3976  * Return the process's privileges.  We don't need a 32-bit equivalent of
3977  * this function because prpriv_t and prpriv32_t are actually the same.
3978  */
3979 void
3980 prgetpriv(proc_t *p, prpriv_t *pprp)
3981 {
3982 	mutex_enter(&p->p_crlock);
3983 	cred2prpriv(p->p_cred, pprp);
3984 	mutex_exit(&p->p_crlock);
3985 }
3986 
3987 #ifdef _SYSCALL32_IMPL
3988 /*
3989  * Return an array of structures with HAT memory map information.
3990  * We allocate here; the caller must deallocate.
3991  */
3992 int
3993 prgetxmap32(proc_t *p, prxmap32_t **prxmapp, size_t *sizep)
3994 {
3995 	struct as *as = p->p_as;
3996 	int nmaps = 0;
3997 	prxmap32_t *mp;
3998 	size_t size;
3999 	struct seg *seg;
4000 	struct seg *brkseg, *stkseg;
4001 	struct vnode *vp;
4002 	struct vattr vattr;
4003 	uint_t prot;
4004 
4005 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
4006 
4007 	/* initial allocation */
4008 	*sizep = size = INITIAL_MAPSIZE;
4009 	*prxmapp = mp = kmem_alloc(size, KM_SLEEP);
4010 
4011 	if ((seg = AS_SEGFIRST(as)) == NULL)
4012 		return (0);
4013 
4014 	brkseg = break_seg(p);
4015 	stkseg = as_segat(as, prgetstackbase(p));
4016 
4017 	do {
4018 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
4019 		caddr_t saddr, naddr, baddr;
4020 		void *tmp = NULL;
4021 		ssize_t psz;
4022 		char *parr;
4023 		uint64_t npages;
4024 		uint64_t pagenum;
4025 
4026 		/*
4027 		 * Segment loop part one: iterate from the base of the segment
4028 		 * to its end, pausing at each address boundary (baddr) between
4029 		 * ranges that have different virtual memory protections.
4030 		 */
4031 		for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) {
4032 			prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr);
4033 			ASSERT(baddr >= saddr && baddr <= eaddr);
4034 
4035 			/*
4036 			 * Segment loop part two: iterate from the current
4037 			 * position to the end of the protection boundary,
4038 			 * pausing at each address boundary (naddr) between
4039 			 * ranges that have different underlying page sizes.
4040 			 */
4041 			for (; saddr < baddr; saddr = naddr) {
4042 				psz = pr_getpagesize(seg, saddr, &naddr, baddr);
4043 				ASSERT(naddr >= saddr && naddr <= baddr);
4044 
4045 				/* reallocate if necessary */
4046 				if ((nmaps + 1) * sizeof (prxmap32_t) > size) {
4047 					size_t newsize = size + 3 * size / 16;
4048 					prxmap32_t *newmp =
4049 					    kmem_alloc(newsize, KM_SLEEP);
4050 
4051 					bcopy(*prxmapp, newmp,
4052 					    nmaps * sizeof (prxmap32_t));
4053 					kmem_free(*prxmapp, size);
4054 					*sizep = size = newsize;
4055 					*prxmapp = newmp;
4056 					mp = newmp + nmaps;
4057 				}
4058 
4059 				bzero(mp, sizeof (*mp));
4060 				mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
4061 				mp->pr_size = (size32_t)(naddr - saddr);
4062 				mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
4063 				mp->pr_mflags = 0;
4064 				if (prot & PROT_READ)
4065 					mp->pr_mflags |= MA_READ;
4066 				if (prot & PROT_WRITE)
4067 					mp->pr_mflags |= MA_WRITE;
4068 				if (prot & PROT_EXEC)
4069 					mp->pr_mflags |= MA_EXEC;
4070 				if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
4071 					mp->pr_mflags |= MA_SHARED;
4072 				if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
4073 					mp->pr_mflags |= MA_NORESERVE;
4074 				if (seg->s_ops == &segspt_shmops ||
4075 				    (seg->s_ops == &segvn_ops &&
4076 				    (SEGOP_GETVP(seg, saddr, &vp) != 0 ||
4077 				    vp == NULL)))
4078 					mp->pr_mflags |= MA_ANON;
4079 				if (seg == brkseg)
4080 					mp->pr_mflags |= MA_BREAK;
4081 				else if (seg == stkseg)
4082 					mp->pr_mflags |= MA_STACK;
4083 				if (seg->s_ops == &segspt_shmops)
4084 					mp->pr_mflags |= MA_ISM | MA_SHM;
4085 
4086 				mp->pr_pagesize = PAGESIZE;
4087 				if (psz == -1) {
4088 					mp->pr_hatpagesize = 0;
4089 				} else {
4090 					mp->pr_hatpagesize = psz;
4091 				}
4092 
4093 				/*
4094 				 * Manufacture a filename for the "object" dir.
4095 				 */
4096 				mp->pr_dev = PRNODEV32;
4097 				vattr.va_mask = AT_FSID|AT_NODEID;
4098 				if (seg->s_ops == &segvn_ops &&
4099 				    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
4100 				    vp != NULL && vp->v_type == VREG &&
4101 				    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
4102 					(void) cmpldev(&mp->pr_dev,
4103 					    vattr.va_fsid);
4104 					mp->pr_ino = vattr.va_nodeid;
4105 					if (vp == p->p_exec)
4106 						(void) strcpy(mp->pr_mapname,
4107 						    "a.out");
4108 					else
4109 						pr_object_name(mp->pr_mapname,
4110 						    vp, &vattr);
4111 				}
4112 
4113 				/*
4114 				 * Get the SysV shared memory id, if any.
4115 				 */
4116 				if ((mp->pr_mflags & MA_SHARED) &&
4117 				    p->p_segacct && (mp->pr_shmid = shmgetid(p,
4118 				    seg->s_base)) != SHMID_NONE) {
4119 					if (mp->pr_shmid == SHMID_FREE)
4120 						mp->pr_shmid = -1;
4121 
4122 					mp->pr_mflags |= MA_SHM;
4123 				} else {
4124 					mp->pr_shmid = -1;
4125 				}
4126 
4127 				npages = ((uintptr_t)(naddr - saddr)) >>
4128 				    PAGESHIFT;
4129 				parr = kmem_zalloc(npages, KM_SLEEP);
4130 
4131 				SEGOP_INCORE(seg, saddr, naddr - saddr, parr);
4132 
4133 				for (pagenum = 0; pagenum < npages; pagenum++) {
4134 					if (parr[pagenum] & SEG_PAGE_INCORE)
4135 						mp->pr_rss++;
4136 					if (parr[pagenum] & SEG_PAGE_ANON)
4137 						mp->pr_anon++;
4138 					if (parr[pagenum] & SEG_PAGE_LOCKED)
4139 						mp->pr_locked++;
4140 				}
4141 				kmem_free(parr, npages);
4142 				mp++;
4143 				nmaps++;
4144 			}
4145 		}
4146 		ASSERT(tmp == NULL);
4147 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
4148 
4149 	return (nmaps);
4150 }
4151 #endif	/* _SYSCALL32_IMPL */
4152