xref: /titanic_41/usr/src/uts/common/fs/proc/prsubr.c (revision 6528affb110ab8cf8b4464874b4a07f3f937475d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/types.h>
33 #include <sys/t_lock.h>
34 #include <sys/param.h>
35 #include <sys/cmn_err.h>
36 #include <sys/cred.h>
37 #include <sys/priv.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
40 #include <sys/inline.h>
41 #include <sys/kmem.h>
42 #include <sys/mman.h>
43 #include <sys/proc.h>
44 #include <sys/sobject.h>
45 #include <sys/sysmacros.h>
46 #include <sys/systm.h>
47 #include <sys/uio.h>
48 #include <sys/var.h>
49 #include <sys/vfs.h>
50 #include <sys/vnode.h>
51 #include <sys/session.h>
52 #include <sys/pcb.h>
53 #include <sys/signal.h>
54 #include <sys/user.h>
55 #include <sys/disp.h>
56 #include <sys/class.h>
57 #include <sys/ts.h>
58 #include <sys/bitmap.h>
59 #include <sys/poll.h>
60 #include <sys/shm_impl.h>
61 #include <sys/fault.h>
62 #include <sys/syscall.h>
63 #include <sys/procfs.h>
64 #include <sys/processor.h>
65 #include <sys/cpuvar.h>
66 #include <sys/copyops.h>
67 #include <sys/time.h>
68 #include <sys/msacct.h>
69 #include <vm/as.h>
70 #include <vm/rm.h>
71 #include <vm/seg.h>
72 #include <vm/seg_vn.h>
73 #include <vm/seg_dev.h>
74 #include <vm/seg_spt.h>
75 #include <vm/page.h>
76 #include <sys/vmparam.h>
77 #include <sys/swap.h>
78 #include <fs/proc/prdata.h>
79 #include <sys/task.h>
80 #include <sys/project.h>
81 #include <sys/contract_impl.h>
82 #include <sys/contract/process.h>
83 #include <sys/contract/process_impl.h>
84 #include <sys/schedctl.h>
85 #include <sys/pool.h>
86 #include <sys/zone.h>
87 #include <sys/atomic.h>
88 #include <sys/sdt.h>
89 
90 #define	MAX_ITERS_SPIN	5
91 
92 typedef struct prpagev {
93 	uint_t *pg_protv;	/* vector of page permissions */
94 	char *pg_incore;	/* vector of incore flags */
95 	size_t pg_npages;	/* number of pages in protv and incore */
96 	ulong_t pg_pnbase;	/* pn within segment of first protv element */
97 } prpagev_t;
98 
99 size_t pagev_lim = 256 * 1024;	/* limit on number of pages in prpagev_t */
100 
101 extern struct seg_ops segdev_ops;	/* needs a header file */
102 extern struct seg_ops segspt_shmops;	/* needs a header file */
103 
104 static	int	set_watched_page(proc_t *, caddr_t, caddr_t, ulong_t, ulong_t);
105 static	void	clear_watched_page(proc_t *, caddr_t, caddr_t, ulong_t);
106 
107 /*
108  * Choose an lwp from the complete set of lwps for the process.
109  * This is called for any operation applied to the process
110  * file descriptor that requires an lwp to operate upon.
111  *
112  * Returns a pointer to the thread for the selected LWP,
113  * and with the dispatcher lock held for the thread.
114  *
115  * The algorithm for choosing an lwp is critical for /proc semantics;
116  * don't touch this code unless you know all of the implications.
117  */
118 kthread_t *
119 prchoose(proc_t *p)
120 {
121 	kthread_t *t;
122 	kthread_t *t_onproc = NULL;	/* running on processor */
123 	kthread_t *t_run = NULL;	/* runnable, on disp queue */
124 	kthread_t *t_sleep = NULL;	/* sleeping */
125 	kthread_t *t_hold = NULL;	/* sleeping, performing hold */
126 	kthread_t *t_susp = NULL;	/* suspended stop */
127 	kthread_t *t_jstop = NULL;	/* jobcontrol stop, w/o directed stop */
128 	kthread_t *t_jdstop = NULL;	/* jobcontrol stop with directed stop */
129 	kthread_t *t_req = NULL;	/* requested stop */
130 	kthread_t *t_istop = NULL;	/* event-of-interest stop */
131 
132 	ASSERT(MUTEX_HELD(&p->p_lock));
133 
134 	/*
135 	 * If the agent lwp exists, it takes precedence over all others.
136 	 */
137 	if ((t = p->p_agenttp) != NULL) {
138 		thread_lock(t);
139 		return (t);
140 	}
141 
142 	if ((t = p->p_tlist) == NULL)	/* start at the head of the list */
143 		return (t);
144 	do {		/* for eacn lwp in the process */
145 		if (VSTOPPED(t)) {	/* virtually stopped */
146 			if (t_req == NULL)
147 				t_req = t;
148 			continue;
149 		}
150 
151 		thread_lock(t);		/* make sure thread is in good state */
152 		switch (t->t_state) {
153 		default:
154 			panic("prchoose: bad thread state %d, thread 0x%p",
155 			    t->t_state, (void *)t);
156 			/*NOTREACHED*/
157 		case TS_SLEEP:
158 			/* this is filthy */
159 			if (t->t_wchan == (caddr_t)&p->p_holdlwps &&
160 			    t->t_wchan0 == NULL) {
161 				if (t_hold == NULL)
162 					t_hold = t;
163 			} else {
164 				if (t_sleep == NULL)
165 					t_sleep = t;
166 			}
167 			break;
168 		case TS_RUN:
169 		case TS_WAIT:
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 	if (p->p_pidflag & CLDNOSIGCHLD)
1196 		flags |= PR_NOSIGCHLD;
1197 	if (p->p_pidflag & CLDWAITPID)
1198 		flags |= PR_WAITPID;
1199 	sp->pr_flags = flags;
1200 	if (VSTOPPED(t)) {
1201 		sp->pr_why   = PR_REQUESTED;
1202 		sp->pr_what  = 0;
1203 	} else {
1204 		sp->pr_why   = t->t_whystop;
1205 		sp->pr_what  = t->t_whatstop;
1206 	}
1207 	sp->pr_lwpid = t->t_tid;
1208 	sp->pr_cursig  = lwp->lwp_cursig;
1209 	prassignset(&sp->pr_lwppend, &t->t_sig);
1210 	schedctl_finish_sigblock(t);
1211 	prassignset(&sp->pr_lwphold, &t->t_hold);
1212 	if (t->t_whystop == PR_FAULTED)
1213 		bcopy(&lwp->lwp_siginfo,
1214 		    &sp->pr_info, sizeof (k_siginfo_t));
1215 	else if (lwp->lwp_curinfo)
1216 		bcopy(&lwp->lwp_curinfo->sq_info,
1217 		    &sp->pr_info, sizeof (k_siginfo_t));
1218 	if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID &&
1219 	    sp->pr_info.si_zoneid != zp->zone_id) {
1220 		sp->pr_info.si_pid = zp->zone_zsched->p_pid;
1221 		sp->pr_info.si_uid = 0;
1222 		sp->pr_info.si_ctid = -1;
1223 		sp->pr_info.si_zoneid = zp->zone_id;
1224 	}
1225 	sp->pr_altstack = lwp->lwp_sigaltstack;
1226 	prgetaction(p, PTOU(p), lwp->lwp_cursig, &sp->pr_action);
1227 	sp->pr_oldcontext = (uintptr_t)lwp->lwp_oldcontext;
1228 	sp->pr_ustack = lwp->lwp_ustack;
1229 	(void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name,
1230 		sizeof (sp->pr_clname) - 1);
1231 	if (flags & PR_STOPPED)
1232 		hrt2ts(t->t_stoptime, &sp->pr_tstamp);
1233 	usr = ms->ms_acct[LMS_USER];
1234 	sys = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP];
1235 	scalehrtime(&usr);
1236 	scalehrtime(&sys);
1237 	hrt2ts(usr, &sp->pr_utime);
1238 	hrt2ts(sys, &sp->pr_stime);
1239 
1240 	/*
1241 	 * Fetch the current instruction, if not a system process.
1242 	 * We don't attempt this unless the lwp is stopped.
1243 	 */
1244 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1245 		sp->pr_flags |= (PR_ISSYS|PR_PCINVAL);
1246 	else if (!(flags & PR_STOPPED))
1247 		sp->pr_flags |= PR_PCINVAL;
1248 	else if (!prfetchinstr(lwp, &instr))
1249 		sp->pr_flags |= PR_PCINVAL;
1250 	else
1251 		sp->pr_instr = instr;
1252 
1253 	/*
1254 	 * Drop p_lock while touching the lwp's stack.
1255 	 */
1256 	mutex_exit(&p->p_lock);
1257 	if (prisstep(lwp))
1258 		sp->pr_flags |= PR_STEP;
1259 	if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) {
1260 		int i;
1261 
1262 		sp->pr_syscall = get_syscall_args(lwp,
1263 			(long *)sp->pr_sysarg, &i);
1264 		sp->pr_nsysarg = (ushort_t)i;
1265 	}
1266 	if ((flags & PR_STOPPED) || t == curthread)
1267 		prgetprregs(lwp, sp->pr_reg);
1268 	if ((t->t_state == TS_STOPPED && t->t_whystop == PR_SYSEXIT) ||
1269 	    (flags & PR_VFORKP)) {
1270 		user_t *up;
1271 		auxv_t *auxp;
1272 		int i;
1273 
1274 		sp->pr_errno = prgetrvals(lwp, &sp->pr_rval1, &sp->pr_rval2);
1275 		if (sp->pr_errno == 0)
1276 			sp->pr_errpriv = PRIV_NONE;
1277 		else
1278 			sp->pr_errpriv = lwp->lwp_badpriv;
1279 
1280 		if (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve) {
1281 			up = PTOU(p);
1282 			sp->pr_sysarg[0] = 0;
1283 			sp->pr_sysarg[1] = (uintptr_t)up->u_argv;
1284 			sp->pr_sysarg[2] = (uintptr_t)up->u_envp;
1285 			for (i = 0, auxp = up->u_auxv;
1286 			    i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]);
1287 			    i++, auxp++) {
1288 				if (auxp->a_type == AT_SUN_EXECNAME) {
1289 					sp->pr_sysarg[0] =
1290 						(uintptr_t)auxp->a_un.a_ptr;
1291 					break;
1292 				}
1293 			}
1294 		}
1295 	}
1296 	if (prhasfp())
1297 		prgetprfpregs(lwp, &sp->pr_fpreg);
1298 	mutex_enter(&p->p_lock);
1299 }
1300 
1301 /*
1302  * Get the sigaction structure for the specified signal.  The u-block
1303  * must already have been mapped in by the caller.
1304  */
1305 void
1306 prgetaction(proc_t *p, user_t *up, uint_t sig, struct sigaction *sp)
1307 {
1308 	bzero(sp, sizeof (*sp));
1309 
1310 	if (sig != 0 && (unsigned)sig < NSIG) {
1311 		sp->sa_handler = up->u_signal[sig-1];
1312 		prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]);
1313 		if (sigismember(&up->u_sigonstack, sig))
1314 			sp->sa_flags |= SA_ONSTACK;
1315 		if (sigismember(&up->u_sigresethand, sig))
1316 			sp->sa_flags |= SA_RESETHAND;
1317 		if (sigismember(&up->u_sigrestart, sig))
1318 			sp->sa_flags |= SA_RESTART;
1319 		if (sigismember(&p->p_siginfo, sig))
1320 			sp->sa_flags |= SA_SIGINFO;
1321 		if (sigismember(&up->u_signodefer, sig))
1322 			sp->sa_flags |= SA_NODEFER;
1323 		if (sig == SIGCLD) {
1324 			if (p->p_flag & SNOWAIT)
1325 				sp->sa_flags |= SA_NOCLDWAIT;
1326 			if ((p->p_flag & SJCTL) == 0)
1327 				sp->sa_flags |= SA_NOCLDSTOP;
1328 		}
1329 	}
1330 }
1331 
1332 #ifdef _SYSCALL32_IMPL
1333 void
1334 prgetaction32(proc_t *p, user_t *up, uint_t sig, struct sigaction32 *sp)
1335 {
1336 	bzero(sp, sizeof (*sp));
1337 
1338 	if (sig != 0 && (unsigned)sig < NSIG) {
1339 		sp->sa_handler = (caddr32_t)(uintptr_t)up->u_signal[sig-1];
1340 		prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]);
1341 		if (sigismember(&up->u_sigonstack, sig))
1342 			sp->sa_flags |= SA_ONSTACK;
1343 		if (sigismember(&up->u_sigresethand, sig))
1344 			sp->sa_flags |= SA_RESETHAND;
1345 		if (sigismember(&up->u_sigrestart, sig))
1346 			sp->sa_flags |= SA_RESTART;
1347 		if (sigismember(&p->p_siginfo, sig))
1348 			sp->sa_flags |= SA_SIGINFO;
1349 		if (sigismember(&up->u_signodefer, sig))
1350 			sp->sa_flags |= SA_NODEFER;
1351 		if (sig == SIGCLD) {
1352 			if (p->p_flag & SNOWAIT)
1353 				sp->sa_flags |= SA_NOCLDWAIT;
1354 			if ((p->p_flag & SJCTL) == 0)
1355 				sp->sa_flags |= SA_NOCLDSTOP;
1356 		}
1357 	}
1358 }
1359 #endif	/* _SYSCALL32_IMPL */
1360 
1361 /*
1362  * Count the number of segments in this process's address space.
1363  */
1364 int
1365 prnsegs(struct as *as, int reserved)
1366 {
1367 	int n = 0;
1368 	struct seg *seg;
1369 
1370 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1371 
1372 	for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) {
1373 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1374 		caddr_t saddr, naddr;
1375 		void *tmp = NULL;
1376 
1377 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1378 			(void) pr_getprot(seg, reserved, &tmp,
1379 			    &saddr, &naddr, eaddr);
1380 			if (saddr != naddr)
1381 				n++;
1382 		}
1383 
1384 		ASSERT(tmp == NULL);
1385 	}
1386 
1387 	return (n);
1388 }
1389 
1390 /*
1391  * Convert uint32_t to decimal string w/o leading zeros.
1392  * Add trailing null characters if 'len' is greater than string length.
1393  * Return the string length.
1394  */
1395 int
1396 pr_u32tos(uint32_t n, char *s, int len)
1397 {
1398 	char cbuf[11];		/* 32-bit unsigned integer fits in 10 digits */
1399 	char *cp = cbuf;
1400 	char *end = s + len;
1401 
1402 	do {
1403 		*cp++ = (char)(n % 10 + '0');
1404 		n /= 10;
1405 	} while (n);
1406 
1407 	len = (int)(cp - cbuf);
1408 
1409 	do {
1410 		*s++ = *--cp;
1411 	} while (cp > cbuf);
1412 
1413 	while (s < end)		/* optional pad */
1414 		*s++ = '\0';
1415 
1416 	return (len);
1417 }
1418 
1419 /*
1420  * Convert uint64_t to decimal string w/o leading zeros.
1421  * Return the string length.
1422  */
1423 static int
1424 pr_u64tos(uint64_t n, char *s)
1425 {
1426 	char cbuf[21];		/* 64-bit unsigned integer fits in 20 digits */
1427 	char *cp = cbuf;
1428 	int len;
1429 
1430 	do {
1431 		*cp++ = (char)(n % 10 + '0');
1432 		n /= 10;
1433 	} while (n);
1434 
1435 	len = (int)(cp - cbuf);
1436 
1437 	do {
1438 		*s++ = *--cp;
1439 	} while (cp > cbuf);
1440 
1441 	return (len);
1442 }
1443 
1444 void
1445 pr_object_name(char *name, vnode_t *vp, struct vattr *vattr)
1446 {
1447 	char *s = name;
1448 	struct vfs *vfsp;
1449 	struct vfssw *vfsswp;
1450 
1451 	if ((vfsp = vp->v_vfsp) != NULL &&
1452 	    ((vfsswp = vfssw + vfsp->vfs_fstype), vfsswp->vsw_name) &&
1453 	    *vfsswp->vsw_name) {
1454 		(void) strcpy(s, vfsswp->vsw_name);
1455 		s += strlen(s);
1456 		*s++ = '.';
1457 	}
1458 	s += pr_u32tos(getmajor(vattr->va_fsid), s, 0);
1459 	*s++ = '.';
1460 	s += pr_u32tos(getminor(vattr->va_fsid), s, 0);
1461 	*s++ = '.';
1462 	s += pr_u64tos(vattr->va_nodeid, s);
1463 	*s++ = '\0';
1464 }
1465 
1466 struct seg *
1467 break_seg(proc_t *p)
1468 {
1469 	caddr_t addr = p->p_brkbase;
1470 	struct seg *seg;
1471 	struct vnode *vp;
1472 
1473 	if (p->p_brksize != 0)
1474 		addr += p->p_brksize - 1;
1475 	seg = as_segat(p->p_as, addr);
1476 	if (seg != NULL && seg->s_ops == &segvn_ops &&
1477 	    (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL))
1478 		return (seg);
1479 	return (NULL);
1480 }
1481 
1482 /*
1483  * Implementation of service functions to handle procfs generic chained
1484  * copyout buffers.
1485  */
1486 typedef struct pr_iobuf_list {
1487 	list_node_t	piol_link;	/* buffer linkage */
1488 	size_t		piol_size;	/* total size (header + data) */
1489 	size_t		piol_usedsize;	/* amount to copy out from this buf */
1490 } piol_t;
1491 
1492 #define	MAPSIZE	(64 * 1024)
1493 #define	PIOL_DATABUF(iol)	((void *)(&(iol)[1]))
1494 
1495 void
1496 pr_iol_initlist(list_t *iolhead, size_t itemsize, int n)
1497 {
1498 	piol_t	*iol;
1499 	size_t	initial_size = MIN(1, n) * itemsize;
1500 
1501 	list_create(iolhead, sizeof (piol_t), offsetof(piol_t, piol_link));
1502 
1503 	ASSERT(list_head(iolhead) == NULL);
1504 	ASSERT(itemsize < MAPSIZE - sizeof (*iol));
1505 	ASSERT(initial_size > 0);
1506 
1507 	/*
1508 	 * Someone creating chained copyout buffers may ask for less than
1509 	 * MAPSIZE if the amount of data to be buffered is known to be
1510 	 * smaller than that.
1511 	 * But in order to prevent involuntary self-denial of service,
1512 	 * the requested input size is clamped at MAPSIZE.
1513 	 */
1514 	initial_size = MIN(MAPSIZE, initial_size + sizeof (*iol));
1515 	iol = kmem_alloc(initial_size, KM_SLEEP);
1516 	list_insert_head(iolhead, iol);
1517 	iol->piol_usedsize = 0;
1518 	iol->piol_size = initial_size;
1519 }
1520 
1521 void *
1522 pr_iol_newbuf(list_t *iolhead, size_t itemsize)
1523 {
1524 	piol_t	*iol;
1525 	char	*new;
1526 
1527 	ASSERT(itemsize < MAPSIZE - sizeof (*iol));
1528 	ASSERT(list_head(iolhead) != NULL);
1529 
1530 	iol = (piol_t *)list_tail(iolhead);
1531 
1532 	if (iol->piol_size <
1533 	    iol->piol_usedsize + sizeof (*iol) + itemsize) {
1534 		/*
1535 		 * Out of space in the current buffer. Allocate more.
1536 		 */
1537 		piol_t *newiol;
1538 
1539 		newiol = kmem_alloc(MAPSIZE, KM_SLEEP);
1540 		newiol->piol_size = MAPSIZE;
1541 		newiol->piol_usedsize = 0;
1542 
1543 		list_insert_after(iolhead, iol, newiol);
1544 		iol = list_next(iolhead, iol);
1545 		ASSERT(iol == newiol);
1546 	}
1547 	new = (char *)PIOL_DATABUF(iol) + iol->piol_usedsize;
1548 	iol->piol_usedsize += itemsize;
1549 	bzero(new, itemsize);
1550 	return (new);
1551 }
1552 
1553 int
1554 pr_iol_copyout_and_free(list_t *iolhead, caddr_t *tgt, int errin)
1555 {
1556 	int error = errin;
1557 	piol_t	*iol;
1558 
1559 	while ((iol = list_head(iolhead)) != NULL) {
1560 		list_remove(iolhead, iol);
1561 		if (!error) {
1562 			if (copyout(PIOL_DATABUF(iol), *tgt,
1563 			    iol->piol_usedsize))
1564 				error = EFAULT;
1565 			*tgt += iol->piol_usedsize;
1566 		}
1567 		kmem_free(iol, iol->piol_size);
1568 	}
1569 	list_destroy(iolhead);
1570 
1571 	return (error);
1572 }
1573 
1574 int
1575 pr_iol_uiomove_and_free(list_t *iolhead, uio_t *uiop, int errin)
1576 {
1577 	offset_t	off = uiop->uio_offset;
1578 	char		*base;
1579 	size_t		size;
1580 	piol_t		*iol;
1581 	int		error = errin;
1582 
1583 	while ((iol = list_head(iolhead)) != NULL) {
1584 		list_remove(iolhead, iol);
1585 		base = PIOL_DATABUF(iol);
1586 		size = iol->piol_usedsize;
1587 		if (off <= size && error == 0 && uiop->uio_resid > 0)
1588 			error = uiomove(base + off, size - off,
1589 			    UIO_READ, uiop);
1590 		off = MAX(0, off - (offset_t)size);
1591 		kmem_free(iol, iol->piol_size);
1592 	}
1593 	list_destroy(iolhead);
1594 
1595 	return (error);
1596 }
1597 
1598 /*
1599  * Return an array of structures with memory map information.
1600  * We allocate here; the caller must deallocate.
1601  */
1602 int
1603 prgetmap(proc_t *p, int reserved, list_t *iolhead)
1604 {
1605 	struct as *as = p->p_as;
1606 	prmap_t *mp;
1607 	struct seg *seg;
1608 	struct seg *brkseg, *stkseg;
1609 	struct vnode *vp;
1610 	struct vattr vattr;
1611 	uint_t prot;
1612 
1613 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1614 
1615 	/*
1616 	 * Request an initial buffer size that doesn't waste memory
1617 	 * if the address space has only a small number of segments.
1618 	 */
1619 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
1620 
1621 	if ((seg = AS_SEGFIRST(as)) == NULL)
1622 		return (0);
1623 
1624 	brkseg = break_seg(p);
1625 	stkseg = as_segat(as, prgetstackbase(p));
1626 
1627 	do {
1628 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1629 		caddr_t saddr, naddr;
1630 		void *tmp = NULL;
1631 
1632 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1633 			prot = pr_getprot(seg, reserved, &tmp,
1634 			    &saddr, &naddr, eaddr);
1635 			if (saddr == naddr)
1636 				continue;
1637 
1638 			mp = pr_iol_newbuf(iolhead, sizeof (*mp));
1639 
1640 			mp->pr_vaddr = (uintptr_t)saddr;
1641 			mp->pr_size = naddr - saddr;
1642 			mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1643 			mp->pr_mflags = 0;
1644 			if (prot & PROT_READ)
1645 				mp->pr_mflags |= MA_READ;
1646 			if (prot & PROT_WRITE)
1647 				mp->pr_mflags |= MA_WRITE;
1648 			if (prot & PROT_EXEC)
1649 				mp->pr_mflags |= MA_EXEC;
1650 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1651 				mp->pr_mflags |= MA_SHARED;
1652 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1653 				mp->pr_mflags |= MA_NORESERVE;
1654 			if (seg->s_ops == &segspt_shmops ||
1655 			    (seg->s_ops == &segvn_ops &&
1656 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1657 				mp->pr_mflags |= MA_ANON;
1658 			if (seg == brkseg)
1659 				mp->pr_mflags |= MA_BREAK;
1660 			else if (seg == stkseg) {
1661 				mp->pr_mflags |= MA_STACK;
1662 				if (reserved) {
1663 					size_t maxstack =
1664 					    ((size_t)p->p_stk_ctl +
1665 					    PAGEOFFSET) & PAGEMASK;
1666 					mp->pr_vaddr =
1667 					    (uintptr_t)prgetstackbase(p) +
1668 					    p->p_stksize - maxstack;
1669 					mp->pr_size = (uintptr_t)naddr -
1670 					    mp->pr_vaddr;
1671 				}
1672 			}
1673 			if (seg->s_ops == &segspt_shmops)
1674 				mp->pr_mflags |= MA_ISM | MA_SHM;
1675 			mp->pr_pagesize = PAGESIZE;
1676 
1677 			/*
1678 			 * Manufacture a filename for the "object" directory.
1679 			 */
1680 			vattr.va_mask = AT_FSID|AT_NODEID;
1681 			if (seg->s_ops == &segvn_ops &&
1682 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1683 			    vp != NULL && vp->v_type == VREG &&
1684 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
1685 				if (vp == p->p_exec)
1686 					(void) strcpy(mp->pr_mapname, "a.out");
1687 				else
1688 					pr_object_name(mp->pr_mapname,
1689 						vp, &vattr);
1690 			}
1691 
1692 			/*
1693 			 * Get the SysV shared memory id, if any.
1694 			 */
1695 			if ((mp->pr_mflags & MA_SHARED) && p->p_segacct &&
1696 			    (mp->pr_shmid = shmgetid(p, seg->s_base)) !=
1697 			    SHMID_NONE) {
1698 				if (mp->pr_shmid == SHMID_FREE)
1699 					mp->pr_shmid = -1;
1700 
1701 				mp->pr_mflags |= MA_SHM;
1702 			} else {
1703 				mp->pr_shmid = -1;
1704 			}
1705 		}
1706 		ASSERT(tmp == NULL);
1707 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1708 
1709 	return (0);
1710 }
1711 
1712 #ifdef _SYSCALL32_IMPL
1713 int
1714 prgetmap32(proc_t *p, int reserved, list_t *iolhead)
1715 {
1716 	struct as *as = p->p_as;
1717 	prmap32_t *mp;
1718 	struct seg *seg;
1719 	struct seg *brkseg, *stkseg;
1720 	struct vnode *vp;
1721 	struct vattr vattr;
1722 	uint_t prot;
1723 
1724 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1725 
1726 	/*
1727 	 * Request an initial buffer size that doesn't waste memory
1728 	 * if the address space has only a small number of segments.
1729 	 */
1730 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
1731 
1732 	if ((seg = AS_SEGFIRST(as)) == NULL)
1733 		return (0);
1734 
1735 	brkseg = break_seg(p);
1736 	stkseg = as_segat(as, prgetstackbase(p));
1737 
1738 	do {
1739 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved);
1740 		caddr_t saddr, naddr;
1741 		void *tmp = NULL;
1742 
1743 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1744 			prot = pr_getprot(seg, reserved, &tmp,
1745 			    &saddr, &naddr, eaddr);
1746 			if (saddr == naddr)
1747 				continue;
1748 
1749 			mp = pr_iol_newbuf(iolhead, sizeof (*mp));
1750 
1751 			mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
1752 			mp->pr_size = (size32_t)(naddr - saddr);
1753 			mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1754 			mp->pr_mflags = 0;
1755 			if (prot & PROT_READ)
1756 				mp->pr_mflags |= MA_READ;
1757 			if (prot & PROT_WRITE)
1758 				mp->pr_mflags |= MA_WRITE;
1759 			if (prot & PROT_EXEC)
1760 				mp->pr_mflags |= MA_EXEC;
1761 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1762 				mp->pr_mflags |= MA_SHARED;
1763 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1764 				mp->pr_mflags |= MA_NORESERVE;
1765 			if (seg->s_ops == &segspt_shmops ||
1766 			    (seg->s_ops == &segvn_ops &&
1767 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1768 				mp->pr_mflags |= MA_ANON;
1769 			if (seg == brkseg)
1770 				mp->pr_mflags |= MA_BREAK;
1771 			else if (seg == stkseg) {
1772 				mp->pr_mflags |= MA_STACK;
1773 				if (reserved) {
1774 					size_t maxstack =
1775 					    ((size_t)p->p_stk_ctl +
1776 					    PAGEOFFSET) & PAGEMASK;
1777 					uintptr_t vaddr =
1778 					    (uintptr_t)prgetstackbase(p) +
1779 					    p->p_stksize - maxstack;
1780 					mp->pr_vaddr = (caddr32_t)vaddr;
1781 					mp->pr_size = (size32_t)
1782 					    ((uintptr_t)naddr - vaddr);
1783 				}
1784 			}
1785 			if (seg->s_ops == &segspt_shmops)
1786 				mp->pr_mflags |= MA_ISM | MA_SHM;
1787 			mp->pr_pagesize = PAGESIZE;
1788 
1789 			/*
1790 			 * Manufacture a filename for the "object" directory.
1791 			 */
1792 			vattr.va_mask = AT_FSID|AT_NODEID;
1793 			if (seg->s_ops == &segvn_ops &&
1794 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1795 			    vp != NULL && vp->v_type == VREG &&
1796 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
1797 				if (vp == p->p_exec)
1798 					(void) strcpy(mp->pr_mapname, "a.out");
1799 				else
1800 					pr_object_name(mp->pr_mapname,
1801 						vp, &vattr);
1802 			}
1803 
1804 			/*
1805 			 * Get the SysV shared memory id, if any.
1806 			 */
1807 			if ((mp->pr_mflags & MA_SHARED) && p->p_segacct &&
1808 			    (mp->pr_shmid = shmgetid(p, seg->s_base)) !=
1809 			    SHMID_NONE) {
1810 				if (mp->pr_shmid == SHMID_FREE)
1811 					mp->pr_shmid = -1;
1812 
1813 				mp->pr_mflags |= MA_SHM;
1814 			} else {
1815 				mp->pr_shmid = -1;
1816 			}
1817 		}
1818 		ASSERT(tmp == NULL);
1819 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1820 
1821 	return (0);
1822 }
1823 #endif	/* _SYSCALL32_IMPL */
1824 
1825 /*
1826  * Return the size of the /proc page data file.
1827  */
1828 size_t
1829 prpdsize(struct as *as)
1830 {
1831 	struct seg *seg;
1832 	size_t size;
1833 
1834 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1835 
1836 	if ((seg = AS_SEGFIRST(as)) == NULL)
1837 		return (0);
1838 
1839 	size = sizeof (prpageheader_t);
1840 	do {
1841 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1842 		caddr_t saddr, naddr;
1843 		void *tmp = NULL;
1844 		size_t npage;
1845 
1846 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1847 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1848 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
1849 				size += sizeof (prasmap_t) + round8(npage);
1850 		}
1851 		ASSERT(tmp == NULL);
1852 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1853 
1854 	return (size);
1855 }
1856 
1857 #ifdef _SYSCALL32_IMPL
1858 size_t
1859 prpdsize32(struct as *as)
1860 {
1861 	struct seg *seg;
1862 	size_t size;
1863 
1864 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
1865 
1866 	if ((seg = AS_SEGFIRST(as)) == NULL)
1867 		return (0);
1868 
1869 	size = sizeof (prpageheader32_t);
1870 	do {
1871 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1872 		caddr_t saddr, naddr;
1873 		void *tmp = NULL;
1874 		size_t npage;
1875 
1876 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1877 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1878 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
1879 				size += sizeof (prasmap32_t) + round8(npage);
1880 		}
1881 		ASSERT(tmp == NULL);
1882 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
1883 
1884 	return (size);
1885 }
1886 #endif	/* _SYSCALL32_IMPL */
1887 
1888 /*
1889  * Read page data information.
1890  */
1891 int
1892 prpdread(proc_t *p, uint_t hatid, struct uio *uiop)
1893 {
1894 	struct as *as = p->p_as;
1895 	caddr_t buf;
1896 	size_t size;
1897 	prpageheader_t *php;
1898 	prasmap_t *pmp;
1899 	struct seg *seg;
1900 	int error;
1901 
1902 again:
1903 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
1904 
1905 	if ((seg = AS_SEGFIRST(as)) == NULL) {
1906 		AS_LOCK_EXIT(as, &as->a_lock);
1907 		return (0);
1908 	}
1909 	size = prpdsize(as);
1910 	if (uiop->uio_resid < size) {
1911 		AS_LOCK_EXIT(as, &as->a_lock);
1912 		return (E2BIG);
1913 	}
1914 
1915 	buf = kmem_zalloc(size, KM_SLEEP);
1916 	php = (prpageheader_t *)buf;
1917 	pmp = (prasmap_t *)(buf + sizeof (prpageheader_t));
1918 
1919 	hrt2ts(gethrtime(), &php->pr_tstamp);
1920 	php->pr_nmap = 0;
1921 	php->pr_npage = 0;
1922 	do {
1923 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
1924 		caddr_t saddr, naddr;
1925 		void *tmp = NULL;
1926 
1927 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
1928 			struct vnode *vp;
1929 			struct vattr vattr;
1930 			size_t len;
1931 			size_t npage;
1932 			uint_t prot;
1933 			uintptr_t next;
1934 
1935 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
1936 			if ((len = (size_t)(naddr - saddr)) == 0)
1937 				continue;
1938 			npage = len / PAGESIZE;
1939 			next = (uintptr_t)(pmp + 1) + round8(npage);
1940 			/*
1941 			 * It's possible that the address space can change
1942 			 * subtlely even though we're holding as->a_lock
1943 			 * due to the nondeterminism of page_exists() in
1944 			 * the presence of asychronously flushed pages or
1945 			 * mapped files whose sizes are changing.
1946 			 * page_exists() may be called indirectly from
1947 			 * pr_getprot() by a SEGOP_INCORE() routine.
1948 			 * If this happens we need to make sure we don't
1949 			 * overrun the buffer whose size we computed based
1950 			 * on the initial iteration through the segments.
1951 			 * Once we've detected an overflow, we need to clean
1952 			 * up the temporary memory allocated in pr_getprot()
1953 			 * and retry. If there's a pending signal, we return
1954 			 * EINTR so that this thread can be dislodged if
1955 			 * a latent bug causes us to spin indefinitely.
1956 			 */
1957 			if (next > (uintptr_t)buf + size) {
1958 				pr_getprot_done(&tmp);
1959 				AS_LOCK_EXIT(as, &as->a_lock);
1960 
1961 				kmem_free(buf, size);
1962 
1963 				if (ISSIG(curthread, JUSTLOOKING))
1964 					return (EINTR);
1965 
1966 				goto again;
1967 			}
1968 
1969 			php->pr_nmap++;
1970 			php->pr_npage += npage;
1971 			pmp->pr_vaddr = (uintptr_t)saddr;
1972 			pmp->pr_npage = npage;
1973 			pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
1974 			pmp->pr_mflags = 0;
1975 			if (prot & PROT_READ)
1976 				pmp->pr_mflags |= MA_READ;
1977 			if (prot & PROT_WRITE)
1978 				pmp->pr_mflags |= MA_WRITE;
1979 			if (prot & PROT_EXEC)
1980 				pmp->pr_mflags |= MA_EXEC;
1981 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
1982 				pmp->pr_mflags |= MA_SHARED;
1983 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
1984 				pmp->pr_mflags |= MA_NORESERVE;
1985 			if (seg->s_ops == &segspt_shmops ||
1986 			    (seg->s_ops == &segvn_ops &&
1987 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
1988 				pmp->pr_mflags |= MA_ANON;
1989 			if (seg->s_ops == &segspt_shmops)
1990 				pmp->pr_mflags |= MA_ISM | MA_SHM;
1991 			pmp->pr_pagesize = PAGESIZE;
1992 			/*
1993 			 * Manufacture a filename for the "object" directory.
1994 			 */
1995 			vattr.va_mask = AT_FSID|AT_NODEID;
1996 			if (seg->s_ops == &segvn_ops &&
1997 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
1998 			    vp != NULL && vp->v_type == VREG &&
1999 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
2000 				if (vp == p->p_exec)
2001 					(void) strcpy(pmp->pr_mapname, "a.out");
2002 				else
2003 					pr_object_name(pmp->pr_mapname,
2004 						vp, &vattr);
2005 			}
2006 
2007 			/*
2008 			 * Get the SysV shared memory id, if any.
2009 			 */
2010 			if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct &&
2011 			    (pmp->pr_shmid = shmgetid(p, seg->s_base)) !=
2012 			    SHMID_NONE) {
2013 				if (pmp->pr_shmid == SHMID_FREE)
2014 					pmp->pr_shmid = -1;
2015 
2016 				pmp->pr_mflags |= MA_SHM;
2017 			} else {
2018 				pmp->pr_shmid = -1;
2019 			}
2020 
2021 			hat_getstat(as, saddr, len, hatid,
2022 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
2023 			pmp = (prasmap_t *)next;
2024 		}
2025 		ASSERT(tmp == NULL);
2026 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
2027 
2028 	AS_LOCK_EXIT(as, &as->a_lock);
2029 
2030 	ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size);
2031 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
2032 	kmem_free(buf, size);
2033 
2034 	return (error);
2035 }
2036 
2037 #ifdef _SYSCALL32_IMPL
2038 int
2039 prpdread32(proc_t *p, uint_t hatid, struct uio *uiop)
2040 {
2041 	struct as *as = p->p_as;
2042 	caddr_t buf;
2043 	size_t size;
2044 	prpageheader32_t *php;
2045 	prasmap32_t *pmp;
2046 	struct seg *seg;
2047 	int error;
2048 
2049 again:
2050 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2051 
2052 	if ((seg = AS_SEGFIRST(as)) == NULL) {
2053 		AS_LOCK_EXIT(as, &as->a_lock);
2054 		return (0);
2055 	}
2056 	size = prpdsize32(as);
2057 	if (uiop->uio_resid < size) {
2058 		AS_LOCK_EXIT(as, &as->a_lock);
2059 		return (E2BIG);
2060 	}
2061 
2062 	buf = kmem_zalloc(size, KM_SLEEP);
2063 	php = (prpageheader32_t *)buf;
2064 	pmp = (prasmap32_t *)(buf + sizeof (prpageheader32_t));
2065 
2066 	hrt2ts32(gethrtime(), &php->pr_tstamp);
2067 	php->pr_nmap = 0;
2068 	php->pr_npage = 0;
2069 	do {
2070 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
2071 		caddr_t saddr, naddr;
2072 		void *tmp = NULL;
2073 
2074 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
2075 			struct vnode *vp;
2076 			struct vattr vattr;
2077 			size_t len;
2078 			size_t npage;
2079 			uint_t prot;
2080 			uintptr_t next;
2081 
2082 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
2083 			if ((len = (size_t)(naddr - saddr)) == 0)
2084 				continue;
2085 			npage = len / PAGESIZE;
2086 			next = (uintptr_t)(pmp + 1) + round8(npage);
2087 			/*
2088 			 * It's possible that the address space can change
2089 			 * subtlely even though we're holding as->a_lock
2090 			 * due to the nondeterminism of page_exists() in
2091 			 * the presence of asychronously flushed pages or
2092 			 * mapped files whose sizes are changing.
2093 			 * page_exists() may be called indirectly from
2094 			 * pr_getprot() by a SEGOP_INCORE() routine.
2095 			 * If this happens we need to make sure we don't
2096 			 * overrun the buffer whose size we computed based
2097 			 * on the initial iteration through the segments.
2098 			 * Once we've detected an overflow, we need to clean
2099 			 * up the temporary memory allocated in pr_getprot()
2100 			 * and retry. If there's a pending signal, we return
2101 			 * EINTR so that this thread can be dislodged if
2102 			 * a latent bug causes us to spin indefinitely.
2103 			 */
2104 			if (next > (uintptr_t)buf + size) {
2105 				pr_getprot_done(&tmp);
2106 				AS_LOCK_EXIT(as, &as->a_lock);
2107 
2108 				kmem_free(buf, size);
2109 
2110 				if (ISSIG(curthread, JUSTLOOKING))
2111 					return (EINTR);
2112 
2113 				goto again;
2114 			}
2115 
2116 			php->pr_nmap++;
2117 			php->pr_npage += npage;
2118 			pmp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
2119 			pmp->pr_npage = (size32_t)npage;
2120 			pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
2121 			pmp->pr_mflags = 0;
2122 			if (prot & PROT_READ)
2123 				pmp->pr_mflags |= MA_READ;
2124 			if (prot & PROT_WRITE)
2125 				pmp->pr_mflags |= MA_WRITE;
2126 			if (prot & PROT_EXEC)
2127 				pmp->pr_mflags |= MA_EXEC;
2128 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
2129 				pmp->pr_mflags |= MA_SHARED;
2130 			if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
2131 				pmp->pr_mflags |= MA_NORESERVE;
2132 			if (seg->s_ops == &segspt_shmops ||
2133 			    (seg->s_ops == &segvn_ops &&
2134 			    (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL)))
2135 				pmp->pr_mflags |= MA_ANON;
2136 			if (seg->s_ops == &segspt_shmops)
2137 				pmp->pr_mflags |= MA_ISM | MA_SHM;
2138 			pmp->pr_pagesize = PAGESIZE;
2139 			/*
2140 			 * Manufacture a filename for the "object" directory.
2141 			 */
2142 			vattr.va_mask = AT_FSID|AT_NODEID;
2143 			if (seg->s_ops == &segvn_ops &&
2144 			    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
2145 			    vp != NULL && vp->v_type == VREG &&
2146 			    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
2147 				if (vp == p->p_exec)
2148 					(void) strcpy(pmp->pr_mapname, "a.out");
2149 				else
2150 					pr_object_name(pmp->pr_mapname,
2151 						vp, &vattr);
2152 			}
2153 
2154 			/*
2155 			 * Get the SysV shared memory id, if any.
2156 			 */
2157 			if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct &&
2158 			    (pmp->pr_shmid = shmgetid(p, seg->s_base)) !=
2159 			    SHMID_NONE) {
2160 				if (pmp->pr_shmid == SHMID_FREE)
2161 					pmp->pr_shmid = -1;
2162 
2163 				pmp->pr_mflags |= MA_SHM;
2164 			} else {
2165 				pmp->pr_shmid = -1;
2166 			}
2167 
2168 			hat_getstat(as, saddr, len, hatid,
2169 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
2170 			pmp = (prasmap32_t *)next;
2171 		}
2172 		ASSERT(tmp == NULL);
2173 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
2174 
2175 	AS_LOCK_EXIT(as, &as->a_lock);
2176 
2177 	ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size);
2178 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
2179 	kmem_free(buf, size);
2180 
2181 	return (error);
2182 }
2183 #endif	/* _SYSCALL32_IMPL */
2184 
2185 ushort_t
2186 prgetpctcpu(uint64_t pct)
2187 {
2188 	/*
2189 	 * The value returned will be relevant in the zone of the examiner,
2190 	 * which may not be the same as the zone which performed the procfs
2191 	 * mount.
2192 	 */
2193 	int nonline = zone_ncpus_online_get(curproc->p_zone);
2194 
2195 	/*
2196 	 * Prorate over online cpus so we don't exceed 100%
2197 	 */
2198 	if (nonline > 1)
2199 		pct /= nonline;
2200 	pct >>= 16;		/* convert to 16-bit scaled integer */
2201 	if (pct > 0x8000)	/* might happen, due to rounding */
2202 		pct = 0x8000;
2203 	return ((ushort_t)pct);
2204 }
2205 
2206 /*
2207  * Return information used by ps(1).
2208  */
2209 void
2210 prgetpsinfo(proc_t *p, psinfo_t *psp)
2211 {
2212 	kthread_t *t;
2213 	struct cred *cred;
2214 	hrtime_t hrutime, hrstime;
2215 
2216 	ASSERT(MUTEX_HELD(&p->p_lock));
2217 
2218 	if ((t = prchoose(p)) == NULL)	/* returns locked thread */
2219 		bzero(psp, sizeof (*psp));
2220 	else {
2221 		thread_unlock(t);
2222 		bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp));
2223 	}
2224 
2225 	/*
2226 	 * only export SSYS and SMSACCT; everything else is off-limits to
2227 	 * userland apps.
2228 	 */
2229 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
2230 	psp->pr_nlwp = p->p_lwpcnt;
2231 	psp->pr_nzomb = p->p_zombcnt;
2232 	mutex_enter(&p->p_crlock);
2233 	cred = p->p_cred;
2234 	psp->pr_uid = crgetruid(cred);
2235 	psp->pr_euid = crgetuid(cred);
2236 	psp->pr_gid = crgetrgid(cred);
2237 	psp->pr_egid = crgetgid(cred);
2238 	mutex_exit(&p->p_crlock);
2239 	psp->pr_pid = p->p_pid;
2240 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
2241 	    (p->p_flag & SZONETOP)) {
2242 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
2243 		/*
2244 		 * Inside local zones, fake zsched's pid as parent pids for
2245 		 * processes which reference processes outside of the zone.
2246 		 */
2247 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
2248 	} else {
2249 		psp->pr_ppid = p->p_ppid;
2250 	}
2251 	psp->pr_pgid = p->p_pgrp;
2252 	psp->pr_sid = p->p_sessp->s_sid;
2253 	psp->pr_taskid = p->p_task->tk_tkid;
2254 	psp->pr_projid = p->p_task->tk_proj->kpj_id;
2255 	psp->pr_poolid = p->p_pool->pool_id;
2256 	psp->pr_zoneid = p->p_zone->zone_id;
2257 	if ((psp->pr_contract = PRCTID(p)) == 0)
2258 		psp->pr_contract = -1;
2259 	psp->pr_addr = (uintptr_t)prgetpsaddr(p);
2260 	switch (p->p_model) {
2261 	case DATAMODEL_ILP32:
2262 		psp->pr_dmodel = PR_MODEL_ILP32;
2263 		break;
2264 	case DATAMODEL_LP64:
2265 		psp->pr_dmodel = PR_MODEL_LP64;
2266 		break;
2267 	}
2268 	hrutime = mstate_aggr_state(p, LMS_USER);
2269 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
2270 	hrt2ts((hrutime + hrstime), &psp->pr_time);
2271 	TICK_TO_TIMESTRUC(p->p_cutime + p->p_cstime, &psp->pr_ctime);
2272 
2273 	if (t == NULL) {
2274 		int wcode = p->p_wcode;		/* must be atomic read */
2275 
2276 		if (wcode)
2277 			psp->pr_wstat = wstat(wcode, p->p_wdata);
2278 		psp->pr_ttydev = PRNODEV;
2279 		psp->pr_lwp.pr_state = SZOMB;
2280 		psp->pr_lwp.pr_sname = 'Z';
2281 		psp->pr_lwp.pr_bindpro = PBIND_NONE;
2282 		psp->pr_lwp.pr_bindpset = PS_NONE;
2283 	} else {
2284 		user_t *up = PTOU(p);
2285 		struct as *as;
2286 		dev_t d;
2287 		extern dev_t rwsconsdev, rconsdev, uconsdev;
2288 
2289 		d = cttydev(p);
2290 		/*
2291 		 * If the controlling terminal is the real
2292 		 * or workstation console device, map to what the
2293 		 * user thinks is the console device.
2294 		 */
2295 		if (d == rwsconsdev || d == rconsdev)
2296 			d = uconsdev;
2297 		psp->pr_ttydev = (d == NODEV) ? PRNODEV : d;
2298 		psp->pr_start = up->u_start;
2299 		bcopy(up->u_comm, psp->pr_fname,
2300 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
2301 		bcopy(up->u_psargs, psp->pr_psargs,
2302 		    MIN(PRARGSZ-1, PSARGSZ));
2303 		psp->pr_argc = up->u_argc;
2304 		psp->pr_argv = up->u_argv;
2305 		psp->pr_envp = up->u_envp;
2306 
2307 		/* get the chosen lwp's lwpsinfo */
2308 		prgetlwpsinfo(t, &psp->pr_lwp);
2309 
2310 		/* compute %cpu for the process */
2311 		if (p->p_lwpcnt == 1)
2312 			psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu;
2313 		else {
2314 			uint64_t pct = 0;
2315 			hrtime_t cur_time = gethrtime_unscaled();
2316 
2317 			t = p->p_tlist;
2318 			do {
2319 				pct += cpu_update_pct(t, cur_time);
2320 			} while ((t = t->t_forw) != p->p_tlist);
2321 
2322 			psp->pr_pctcpu = prgetpctcpu(pct);
2323 		}
2324 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
2325 			psp->pr_size = 0;
2326 			psp->pr_rssize = 0;
2327 		} else {
2328 			mutex_exit(&p->p_lock);
2329 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
2330 			psp->pr_size = btopr(rm_assize(as)) * (PAGESIZE / 1024);
2331 			psp->pr_rssize = rm_asrss(as) * (PAGESIZE / 1024);
2332 			psp->pr_pctmem = rm_pctmemory(as);
2333 			AS_LOCK_EXIT(as, &as->a_lock);
2334 			mutex_enter(&p->p_lock);
2335 		}
2336 	}
2337 }
2338 
2339 #ifdef _SYSCALL32_IMPL
2340 void
2341 prgetpsinfo32(proc_t *p, psinfo32_t *psp)
2342 {
2343 	kthread_t *t;
2344 	struct cred *cred;
2345 	hrtime_t hrutime, hrstime;
2346 
2347 	ASSERT(MUTEX_HELD(&p->p_lock));
2348 
2349 	if ((t = prchoose(p)) == NULL)	/* returns locked thread */
2350 		bzero(psp, sizeof (*psp));
2351 	else {
2352 		thread_unlock(t);
2353 		bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp));
2354 	}
2355 
2356 	/*
2357 	 * only export SSYS and SMSACCT; everything else is off-limits to
2358 	 * userland apps.
2359 	 */
2360 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
2361 	psp->pr_nlwp = p->p_lwpcnt;
2362 	psp->pr_nzomb = p->p_zombcnt;
2363 	mutex_enter(&p->p_crlock);
2364 	cred = p->p_cred;
2365 	psp->pr_uid = crgetruid(cred);
2366 	psp->pr_euid = crgetuid(cred);
2367 	psp->pr_gid = crgetrgid(cred);
2368 	psp->pr_egid = crgetgid(cred);
2369 	mutex_exit(&p->p_crlock);
2370 	psp->pr_pid = p->p_pid;
2371 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
2372 	    (p->p_flag & SZONETOP)) {
2373 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
2374 		/*
2375 		 * Inside local zones, fake zsched's pid as parent pids for
2376 		 * processes which reference processes outside of the zone.
2377 		 */
2378 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
2379 	} else {
2380 		psp->pr_ppid = p->p_ppid;
2381 	}
2382 	psp->pr_pgid = p->p_pgrp;
2383 	psp->pr_sid = p->p_sessp->s_sid;
2384 	psp->pr_taskid = p->p_task->tk_tkid;
2385 	psp->pr_projid = p->p_task->tk_proj->kpj_id;
2386 	psp->pr_poolid = p->p_pool->pool_id;
2387 	psp->pr_zoneid = p->p_zone->zone_id;
2388 	if ((psp->pr_contract = PRCTID(p)) == 0)
2389 		psp->pr_contract = -1;
2390 	psp->pr_addr = 0;	/* cannot represent 64-bit addr in 32 bits */
2391 	switch (p->p_model) {
2392 	case DATAMODEL_ILP32:
2393 		psp->pr_dmodel = PR_MODEL_ILP32;
2394 		break;
2395 	case DATAMODEL_LP64:
2396 		psp->pr_dmodel = PR_MODEL_LP64;
2397 		break;
2398 	}
2399 	hrutime = mstate_aggr_state(p, LMS_USER);
2400 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
2401 	hrt2ts32(hrutime + hrstime, &psp->pr_time);
2402 	TICK_TO_TIMESTRUC32(p->p_cutime + p->p_cstime, &psp->pr_ctime);
2403 
2404 	if (t == NULL) {
2405 		extern int wstat(int, int);	/* needs a header file */
2406 		int wcode = p->p_wcode;		/* must be atomic read */
2407 
2408 		if (wcode)
2409 			psp->pr_wstat = wstat(wcode, p->p_wdata);
2410 		psp->pr_ttydev = PRNODEV32;
2411 		psp->pr_lwp.pr_state = SZOMB;
2412 		psp->pr_lwp.pr_sname = 'Z';
2413 	} else {
2414 		user_t *up = PTOU(p);
2415 		struct as *as;
2416 		dev_t d;
2417 		extern dev_t rwsconsdev, rconsdev, uconsdev;
2418 
2419 		d = cttydev(p);
2420 		/*
2421 		 * If the controlling terminal is the real
2422 		 * or workstation console device, map to what the
2423 		 * user thinks is the console device.
2424 		 */
2425 		if (d == rwsconsdev || d == rconsdev)
2426 			d = uconsdev;
2427 		(void) cmpldev(&psp->pr_ttydev, d);
2428 		TIMESPEC_TO_TIMESPEC32(&psp->pr_start, &up->u_start);
2429 		bcopy(up->u_comm, psp->pr_fname,
2430 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
2431 		bcopy(up->u_psargs, psp->pr_psargs,
2432 		    MIN(PRARGSZ-1, PSARGSZ));
2433 		psp->pr_argc = up->u_argc;
2434 		psp->pr_argv = (caddr32_t)up->u_argv;
2435 		psp->pr_envp = (caddr32_t)up->u_envp;
2436 
2437 		/* get the chosen lwp's lwpsinfo */
2438 		prgetlwpsinfo32(t, &psp->pr_lwp);
2439 
2440 		/* compute %cpu for the process */
2441 		if (p->p_lwpcnt == 1)
2442 			psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu;
2443 		else {
2444 			uint64_t pct = 0;
2445 			hrtime_t cur_time;
2446 
2447 			t = p->p_tlist;
2448 			cur_time = gethrtime_unscaled();
2449 			do {
2450 				pct += cpu_update_pct(t, cur_time);
2451 			} while ((t = t->t_forw) != p->p_tlist);
2452 
2453 			psp->pr_pctcpu = prgetpctcpu(pct);
2454 		}
2455 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
2456 			psp->pr_size = 0;
2457 			psp->pr_rssize = 0;
2458 		} else {
2459 			mutex_exit(&p->p_lock);
2460 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
2461 			psp->pr_size = (size32_t)
2462 				(btopr(rm_assize(as)) * (PAGESIZE / 1024));
2463 			psp->pr_rssize = (size32_t)
2464 				(rm_asrss(as) * (PAGESIZE / 1024));
2465 			psp->pr_pctmem = rm_pctmemory(as);
2466 			AS_LOCK_EXIT(as, &as->a_lock);
2467 			mutex_enter(&p->p_lock);
2468 		}
2469 	}
2470 
2471 	/*
2472 	 * If we are looking at an LP64 process, zero out
2473 	 * the fields that cannot be represented in ILP32.
2474 	 */
2475 	if (p->p_model != DATAMODEL_ILP32) {
2476 		psp->pr_size = 0;
2477 		psp->pr_rssize = 0;
2478 		psp->pr_argv = 0;
2479 		psp->pr_envp = 0;
2480 	}
2481 }
2482 #endif	/* _SYSCALL32_IMPL */
2483 
2484 void
2485 prgetlwpsinfo(kthread_t *t, lwpsinfo_t *psp)
2486 {
2487 	klwp_t *lwp = ttolwp(t);
2488 	sobj_ops_t *sobj;
2489 	char c, state;
2490 	uint64_t pct;
2491 	int retval, niceval;
2492 	hrtime_t hrutime, hrstime;
2493 
2494 	ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock));
2495 
2496 	bzero(psp, sizeof (*psp));
2497 
2498 	psp->pr_flag = 0;	/* lwpsinfo_t.pr_flag is deprecated */
2499 	psp->pr_lwpid = t->t_tid;
2500 	psp->pr_addr = (uintptr_t)t;
2501 	psp->pr_wchan = (uintptr_t)t->t_wchan;
2502 
2503 	/* map the thread state enum into a process state enum */
2504 	state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
2505 	switch (state) {
2506 	case TS_SLEEP:		state = SSLEEP;		c = 'S';	break;
2507 	case TS_RUN:		state = SRUN;		c = 'R';	break;
2508 	case TS_ONPROC:		state = SONPROC;	c = 'O';	break;
2509 	case TS_ZOMB:		state = SZOMB;		c = 'Z';	break;
2510 	case TS_STOPPED:	state = SSTOP;		c = 'T';	break;
2511 	case TS_WAIT:		state = SWAIT;		c = 'W';	break;
2512 	default:		state = 0;		c = '?';	break;
2513 	}
2514 	psp->pr_state = state;
2515 	psp->pr_sname = c;
2516 	if ((sobj = t->t_sobj_ops) != NULL)
2517 		psp->pr_stype = SOBJ_TYPE(sobj);
2518 	retval = CL_DONICE(t, NULL, 0, &niceval);
2519 	if (retval == 0) {
2520 		psp->pr_oldpri = v.v_maxsyspri - t->t_pri;
2521 		psp->pr_nice = niceval + NZERO;
2522 	}
2523 	psp->pr_syscall = t->t_sysnum;
2524 	psp->pr_pri = t->t_pri;
2525 	psp->pr_start.tv_sec = t->t_start;
2526 	psp->pr_start.tv_nsec = 0L;
2527 	hrutime = lwp->lwp_mstate.ms_acct[LMS_USER];
2528 	scalehrtime(&hrutime);
2529 	hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] +
2530 	    lwp->lwp_mstate.ms_acct[LMS_TRAP];
2531 	scalehrtime(&hrstime);
2532 	hrt2ts(hrutime + hrstime, &psp->pr_time);
2533 	/* compute %cpu for the lwp */
2534 	pct = cpu_update_pct(t, gethrtime_unscaled());
2535 	psp->pr_pctcpu = prgetpctcpu(pct);
2536 	psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15;	/* [0..99] */
2537 	if (psp->pr_cpu > 99)
2538 		psp->pr_cpu = 99;
2539 
2540 	(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
2541 		sizeof (psp->pr_clname) - 1);
2542 	bzero(psp->pr_name, sizeof (psp->pr_name));	/* XXX ??? */
2543 	psp->pr_onpro = t->t_cpu->cpu_id;
2544 	psp->pr_bindpro = t->t_bind_cpu;
2545 	psp->pr_bindpset = t->t_bind_pset;
2546 	psp->pr_lgrp = t->t_lpl->lpl_lgrpid;
2547 }
2548 
2549 #ifdef _SYSCALL32_IMPL
2550 void
2551 prgetlwpsinfo32(kthread_t *t, lwpsinfo32_t *psp)
2552 {
2553 	proc_t *p = ttoproc(t);
2554 	klwp_t *lwp = ttolwp(t);
2555 	sobj_ops_t *sobj;
2556 	char c, state;
2557 	uint64_t pct;
2558 	int retval, niceval;
2559 	hrtime_t hrutime, hrstime;
2560 
2561 	ASSERT(MUTEX_HELD(&p->p_lock));
2562 
2563 	bzero(psp, sizeof (*psp));
2564 
2565 	psp->pr_flag = 0;	/* lwpsinfo_t.pr_flag is deprecated */
2566 	psp->pr_lwpid = t->t_tid;
2567 	psp->pr_addr = 0;	/* cannot represent 64-bit addr in 32 bits */
2568 	psp->pr_wchan = 0;	/* cannot represent 64-bit addr in 32 bits */
2569 
2570 	/* map the thread state enum into a process state enum */
2571 	state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
2572 	switch (state) {
2573 	case TS_SLEEP:		state = SSLEEP;		c = 'S';	break;
2574 	case TS_RUN:		state = SRUN;		c = 'R';	break;
2575 	case TS_ONPROC:		state = SONPROC;	c = 'O';	break;
2576 	case TS_ZOMB:		state = SZOMB;		c = 'Z';	break;
2577 	case TS_STOPPED:	state = SSTOP;		c = 'T';	break;
2578 	case TS_WAIT:		state = SWAIT;		c = 'W';	break;
2579 	default:		state = 0;		c = '?';	break;
2580 	}
2581 	psp->pr_state = state;
2582 	psp->pr_sname = c;
2583 	if ((sobj = t->t_sobj_ops) != NULL)
2584 		psp->pr_stype = SOBJ_TYPE(sobj);
2585 	retval = CL_DONICE(t, NULL, 0, &niceval);
2586 	if (retval == 0) {
2587 		psp->pr_oldpri = v.v_maxsyspri - t->t_pri;
2588 		psp->pr_nice = niceval + NZERO;
2589 	} else {
2590 		psp->pr_oldpri = 0;
2591 		psp->pr_nice = 0;
2592 	}
2593 	psp->pr_syscall = t->t_sysnum;
2594 	psp->pr_pri = t->t_pri;
2595 	psp->pr_start.tv_sec = (time32_t)t->t_start;
2596 	psp->pr_start.tv_nsec = 0L;
2597 	hrutime = lwp->lwp_mstate.ms_acct[LMS_USER];
2598 	scalehrtime(&hrutime);
2599 	hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] +
2600 	    lwp->lwp_mstate.ms_acct[LMS_TRAP];
2601 	scalehrtime(&hrstime);
2602 	hrt2ts32(hrutime + hrstime, &psp->pr_time);
2603 	/* compute %cpu for the lwp */
2604 	pct = cpu_update_pct(t, gethrtime_unscaled());
2605 	psp->pr_pctcpu = prgetpctcpu(pct);
2606 	psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15;	/* [0..99] */
2607 	if (psp->pr_cpu > 99)
2608 		psp->pr_cpu = 99;
2609 
2610 	(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
2611 		sizeof (psp->pr_clname) - 1);
2612 	bzero(psp->pr_name, sizeof (psp->pr_name));	/* XXX ??? */
2613 	psp->pr_onpro = t->t_cpu->cpu_id;
2614 	psp->pr_bindpro = t->t_bind_cpu;
2615 	psp->pr_bindpset = t->t_bind_pset;
2616 	psp->pr_lgrp = t->t_lpl->lpl_lgrpid;
2617 }
2618 #endif	/* _SYSCALL32_IMPL */
2619 
2620 /*
2621  * This used to get called when microstate accounting was disabled but
2622  * microstate information was requested.  Since Microstate accounting is on
2623  * regardless of the proc flags, this simply makes it appear to procfs that
2624  * microstate accounting is on.  This is relatively meaningless since you
2625  * can't turn it off, but this is here for the sake of appearances.
2626  */
2627 
2628 /*ARGSUSED*/
2629 void
2630 estimate_msacct(kthread_t *t, hrtime_t curtime)
2631 {
2632 	proc_t *p;
2633 
2634 	if (t == NULL)
2635 		return;
2636 
2637 	p = ttoproc(t);
2638 	ASSERT(MUTEX_HELD(&p->p_lock));
2639 
2640 	/*
2641 	 * A system process (p0) could be referenced if the thread is
2642 	 * in the process of exiting.  Don't turn on microstate accounting
2643 	 * in that case.
2644 	 */
2645 	if (p->p_flag & SSYS)
2646 		return;
2647 
2648 	/*
2649 	 * Loop through all the LWPs (kernel threads) in the process.
2650 	 */
2651 	t = p->p_tlist;
2652 	do {
2653 		t->t_proc_flag |= TP_MSACCT;
2654 	} while ((t = t->t_forw) != p->p_tlist);
2655 
2656 	p->p_flag |= SMSACCT;			/* set process-wide MSACCT */
2657 }
2658 
2659 /*
2660  * It's not really possible to disable microstate accounting anymore.
2661  * However, this routine simply turns off the ms accounting flags in a process
2662  * This way procfs can still pretend to turn microstate accounting on and
2663  * off for a process, but it actually doesn't do anything.  This is
2664  * a neutered form of preemptive idiot-proofing.
2665  */
2666 void
2667 disable_msacct(proc_t *p)
2668 {
2669 	kthread_t *t;
2670 
2671 	ASSERT(MUTEX_HELD(&p->p_lock));
2672 
2673 	p->p_flag &= ~SMSACCT;		/* clear process-wide MSACCT */
2674 	/*
2675 	 * Loop through all the LWPs (kernel threads) in the process.
2676 	 */
2677 	if ((t = p->p_tlist) != NULL) {
2678 		do {
2679 			/* clear per-thread flag */
2680 			t->t_proc_flag &= ~TP_MSACCT;
2681 		} while ((t = t->t_forw) != p->p_tlist);
2682 	}
2683 }
2684 
2685 /*
2686  * Return resource usage information.
2687  */
2688 void
2689 prgetusage(kthread_t *t, prhusage_t *pup)
2690 {
2691 	klwp_t *lwp = ttolwp(t);
2692 	hrtime_t *mstimep;
2693 	struct mstate *ms = &lwp->lwp_mstate;
2694 	int state;
2695 	int i;
2696 	hrtime_t curtime;
2697 	hrtime_t waitrq;
2698 	hrtime_t tmp1;
2699 
2700 	curtime = gethrtime_unscaled();
2701 
2702 	pup->pr_lwpid	= t->t_tid;
2703 	pup->pr_count	= 1;
2704 	pup->pr_create	= ms->ms_start;
2705 	pup->pr_term    = ms->ms_term;
2706 	scalehrtime(&pup->pr_create);
2707 	scalehrtime(&pup->pr_term);
2708 	if (ms->ms_term == 0) {
2709 		pup->pr_rtime = curtime - ms->ms_start;
2710 		scalehrtime(&pup->pr_rtime);
2711 	} else {
2712 		pup->pr_rtime = ms->ms_term - ms->ms_start;
2713 		scalehrtime(&pup->pr_rtime);
2714 	}
2715 
2716 
2717 	pup->pr_utime    = ms->ms_acct[LMS_USER];
2718 	pup->pr_stime    = ms->ms_acct[LMS_SYSTEM];
2719 	pup->pr_ttime    = ms->ms_acct[LMS_TRAP];
2720 	pup->pr_tftime   = ms->ms_acct[LMS_TFAULT];
2721 	pup->pr_dftime   = ms->ms_acct[LMS_DFAULT];
2722 	pup->pr_kftime   = ms->ms_acct[LMS_KFAULT];
2723 	pup->pr_ltime    = ms->ms_acct[LMS_USER_LOCK];
2724 	pup->pr_slptime  = ms->ms_acct[LMS_SLEEP];
2725 	pup->pr_wtime    = ms->ms_acct[LMS_WAIT_CPU];
2726 	pup->pr_stoptime = ms->ms_acct[LMS_STOPPED];
2727 
2728 	prscaleusage(pup);
2729 
2730 	/*
2731 	 * Adjust for time waiting in the dispatcher queue.
2732 	 */
2733 	waitrq = t->t_waitrq;	/* hopefully atomic */
2734 	if (waitrq != 0) {
2735 		tmp1 = curtime - waitrq;
2736 		scalehrtime(&tmp1);
2737 		pup->pr_wtime += tmp1;
2738 		curtime = waitrq;
2739 	}
2740 
2741 	/*
2742 	 * Adjust for time spent in current microstate.
2743 	 */
2744 	if (ms->ms_state_start > curtime) {
2745 		curtime = gethrtime_unscaled();
2746 	}
2747 
2748 	i = 0;
2749 	do {
2750 		switch (state = t->t_mstate) {
2751 		case LMS_SLEEP:
2752 			/*
2753 			 * Update the timer for the current sleep state.
2754 			 */
2755 			switch (state = ms->ms_prev) {
2756 			case LMS_TFAULT:
2757 			case LMS_DFAULT:
2758 			case LMS_KFAULT:
2759 			case LMS_USER_LOCK:
2760 				break;
2761 			default:
2762 				state = LMS_SLEEP;
2763 				break;
2764 			}
2765 			break;
2766 		case LMS_TFAULT:
2767 		case LMS_DFAULT:
2768 		case LMS_KFAULT:
2769 		case LMS_USER_LOCK:
2770 			state = LMS_SYSTEM;
2771 			break;
2772 		}
2773 		switch (state) {
2774 		case LMS_USER:		mstimep = &pup->pr_utime;	break;
2775 		case LMS_SYSTEM:	mstimep = &pup->pr_stime;	break;
2776 		case LMS_TRAP:		mstimep = &pup->pr_ttime;	break;
2777 		case LMS_TFAULT:	mstimep = &pup->pr_tftime;	break;
2778 		case LMS_DFAULT:	mstimep = &pup->pr_dftime;	break;
2779 		case LMS_KFAULT:	mstimep = &pup->pr_kftime;	break;
2780 		case LMS_USER_LOCK:	mstimep = &pup->pr_ltime;	break;
2781 		case LMS_SLEEP:		mstimep = &pup->pr_slptime;	break;
2782 		case LMS_WAIT_CPU:	mstimep = &pup->pr_wtime;	break;
2783 		case LMS_STOPPED:	mstimep = &pup->pr_stoptime;	break;
2784 		default:		panic("prgetusage: unknown microstate");
2785 		}
2786 		tmp1 = curtime - ms->ms_state_start;
2787 		if (tmp1 < 0) {
2788 			curtime = gethrtime_unscaled();
2789 			i++;
2790 			continue;
2791 		}
2792 		scalehrtime(&tmp1);
2793 	} while (tmp1 < 0 && i < MAX_ITERS_SPIN);
2794 
2795 	*mstimep += tmp1;
2796 
2797 	/* update pup timestamp */
2798 	pup->pr_tstamp = curtime;
2799 	scalehrtime(&pup->pr_tstamp);
2800 
2801 	/*
2802 	 * Resource usage counters.
2803 	 */
2804 	pup->pr_minf  = lwp->lwp_ru.minflt;
2805 	pup->pr_majf  = lwp->lwp_ru.majflt;
2806 	pup->pr_nswap = lwp->lwp_ru.nswap;
2807 	pup->pr_inblk = lwp->lwp_ru.inblock;
2808 	pup->pr_oublk = lwp->lwp_ru.oublock;
2809 	pup->pr_msnd  = lwp->lwp_ru.msgsnd;
2810 	pup->pr_mrcv  = lwp->lwp_ru.msgrcv;
2811 	pup->pr_sigs  = lwp->lwp_ru.nsignals;
2812 	pup->pr_vctx  = lwp->lwp_ru.nvcsw;
2813 	pup->pr_ictx  = lwp->lwp_ru.nivcsw;
2814 	pup->pr_sysc  = lwp->lwp_ru.sysc;
2815 	pup->pr_ioch  = lwp->lwp_ru.ioch;
2816 }
2817 
2818 /*
2819  * Convert ms_acct stats from unscaled high-res time to nanoseconds
2820  */
2821 void
2822 prscaleusage(prhusage_t *usg)
2823 {
2824 	scalehrtime(&usg->pr_utime);
2825 	scalehrtime(&usg->pr_stime);
2826 	scalehrtime(&usg->pr_ttime);
2827 	scalehrtime(&usg->pr_tftime);
2828 	scalehrtime(&usg->pr_dftime);
2829 	scalehrtime(&usg->pr_kftime);
2830 	scalehrtime(&usg->pr_ltime);
2831 	scalehrtime(&usg->pr_slptime);
2832 	scalehrtime(&usg->pr_wtime);
2833 	scalehrtime(&usg->pr_stoptime);
2834 }
2835 
2836 
2837 /*
2838  * Sum resource usage information.
2839  */
2840 void
2841 praddusage(kthread_t *t, prhusage_t *pup)
2842 {
2843 	klwp_t *lwp = ttolwp(t);
2844 	hrtime_t *mstimep;
2845 	struct mstate *ms = &lwp->lwp_mstate;
2846 	int state;
2847 	int i;
2848 	hrtime_t curtime;
2849 	hrtime_t waitrq;
2850 	hrtime_t tmp;
2851 	prhusage_t conv;
2852 
2853 	curtime = gethrtime_unscaled();
2854 
2855 	if (ms->ms_term == 0) {
2856 		tmp = curtime - ms->ms_start;
2857 		scalehrtime(&tmp);
2858 		pup->pr_rtime += tmp;
2859 	} else {
2860 		tmp = ms->ms_term - ms->ms_start;
2861 		scalehrtime(&tmp);
2862 		pup->pr_rtime += tmp;
2863 	}
2864 
2865 	conv.pr_utime = ms->ms_acct[LMS_USER];
2866 	conv.pr_stime = ms->ms_acct[LMS_SYSTEM];
2867 	conv.pr_ttime = ms->ms_acct[LMS_TRAP];
2868 	conv.pr_tftime = ms->ms_acct[LMS_TFAULT];
2869 	conv.pr_dftime = ms->ms_acct[LMS_DFAULT];
2870 	conv.pr_kftime = ms->ms_acct[LMS_KFAULT];
2871 	conv.pr_ltime = ms->ms_acct[LMS_USER_LOCK];
2872 	conv.pr_slptime = ms->ms_acct[LMS_SLEEP];
2873 	conv.pr_wtime = ms->ms_acct[LMS_WAIT_CPU];
2874 	conv.pr_stoptime = ms->ms_acct[LMS_STOPPED];
2875 
2876 	prscaleusage(&conv);
2877 
2878 	pup->pr_utime	+= conv.pr_utime;
2879 	pup->pr_stime	+= conv.pr_stime;
2880 	pup->pr_ttime	+= conv.pr_ttime;
2881 	pup->pr_tftime	+= conv.pr_tftime;
2882 	pup->pr_dftime	+= conv.pr_dftime;
2883 	pup->pr_kftime	+= conv.pr_kftime;
2884 	pup->pr_ltime	+= conv.pr_ltime;
2885 	pup->pr_slptime	+= conv.pr_slptime;
2886 	pup->pr_wtime	+= conv.pr_wtime;
2887 	pup->pr_stoptime += conv.pr_stoptime;
2888 
2889 	/*
2890 	 * Adjust for time waiting in the dispatcher queue.
2891 	 */
2892 	waitrq = t->t_waitrq;	/* hopefully atomic */
2893 	if (waitrq != 0) {
2894 		tmp = curtime - waitrq;
2895 		scalehrtime(&tmp);
2896 		pup->pr_wtime += tmp;
2897 		curtime = waitrq;
2898 	}
2899 
2900 	/*
2901 	 * Adjust for time spent in current microstate.
2902 	 */
2903 	if (ms->ms_state_start > curtime) {
2904 		curtime = gethrtime_unscaled();
2905 	}
2906 
2907 	i = 0;
2908 	do {
2909 		switch (state = t->t_mstate) {
2910 		case LMS_SLEEP:
2911 			/*
2912 			 * Update the timer for the current sleep state.
2913 			 */
2914 			switch (state = ms->ms_prev) {
2915 			case LMS_TFAULT:
2916 			case LMS_DFAULT:
2917 			case LMS_KFAULT:
2918 			case LMS_USER_LOCK:
2919 				break;
2920 			default:
2921 				state = LMS_SLEEP;
2922 				break;
2923 			}
2924 			break;
2925 		case LMS_TFAULT:
2926 		case LMS_DFAULT:
2927 		case LMS_KFAULT:
2928 		case LMS_USER_LOCK:
2929 			state = LMS_SYSTEM;
2930 			break;
2931 		}
2932 		switch (state) {
2933 		case LMS_USER:		mstimep = &pup->pr_utime;	break;
2934 		case LMS_SYSTEM:	mstimep = &pup->pr_stime;	break;
2935 		case LMS_TRAP:		mstimep = &pup->pr_ttime;	break;
2936 		case LMS_TFAULT:	mstimep = &pup->pr_tftime;	break;
2937 		case LMS_DFAULT:	mstimep = &pup->pr_dftime;	break;
2938 		case LMS_KFAULT:	mstimep = &pup->pr_kftime;	break;
2939 		case LMS_USER_LOCK:	mstimep = &pup->pr_ltime;	break;
2940 		case LMS_SLEEP:		mstimep = &pup->pr_slptime;	break;
2941 		case LMS_WAIT_CPU:	mstimep = &pup->pr_wtime;	break;
2942 		case LMS_STOPPED:	mstimep = &pup->pr_stoptime;	break;
2943 		default:		panic("praddusage: unknown microstate");
2944 		}
2945 		tmp = curtime - ms->ms_state_start;
2946 		if (tmp < 0) {
2947 			curtime = gethrtime_unscaled();
2948 			i++;
2949 			continue;
2950 		}
2951 		scalehrtime(&tmp);
2952 	} while (tmp < 0 && i < MAX_ITERS_SPIN);
2953 
2954 	*mstimep += tmp;
2955 
2956 	/* update pup timestamp */
2957 	pup->pr_tstamp = curtime;
2958 	scalehrtime(&pup->pr_tstamp);
2959 
2960 	/*
2961 	 * Resource usage counters.
2962 	 */
2963 	pup->pr_minf  += lwp->lwp_ru.minflt;
2964 	pup->pr_majf  += lwp->lwp_ru.majflt;
2965 	pup->pr_nswap += lwp->lwp_ru.nswap;
2966 	pup->pr_inblk += lwp->lwp_ru.inblock;
2967 	pup->pr_oublk += lwp->lwp_ru.oublock;
2968 	pup->pr_msnd  += lwp->lwp_ru.msgsnd;
2969 	pup->pr_mrcv  += lwp->lwp_ru.msgrcv;
2970 	pup->pr_sigs  += lwp->lwp_ru.nsignals;
2971 	pup->pr_vctx  += lwp->lwp_ru.nvcsw;
2972 	pup->pr_ictx  += lwp->lwp_ru.nivcsw;
2973 	pup->pr_sysc  += lwp->lwp_ru.sysc;
2974 	pup->pr_ioch  += lwp->lwp_ru.ioch;
2975 }
2976 
2977 /*
2978  * Convert a prhusage_t to a prusage_t.
2979  * This means convert each hrtime_t to a timestruc_t
2980  * and copy the count fields uint64_t => ulong_t.
2981  */
2982 void
2983 prcvtusage(prhusage_t *pup, prusage_t *upup)
2984 {
2985 	uint64_t *ullp;
2986 	ulong_t *ulp;
2987 	int i;
2988 
2989 	upup->pr_lwpid = pup->pr_lwpid;
2990 	upup->pr_count = pup->pr_count;
2991 
2992 	hrt2ts(pup->pr_tstamp,	&upup->pr_tstamp);
2993 	hrt2ts(pup->pr_create,	&upup->pr_create);
2994 	hrt2ts(pup->pr_term,	&upup->pr_term);
2995 	hrt2ts(pup->pr_rtime,	&upup->pr_rtime);
2996 	hrt2ts(pup->pr_utime,	&upup->pr_utime);
2997 	hrt2ts(pup->pr_stime,	&upup->pr_stime);
2998 	hrt2ts(pup->pr_ttime,	&upup->pr_ttime);
2999 	hrt2ts(pup->pr_tftime,	&upup->pr_tftime);
3000 	hrt2ts(pup->pr_dftime,	&upup->pr_dftime);
3001 	hrt2ts(pup->pr_kftime,	&upup->pr_kftime);
3002 	hrt2ts(pup->pr_ltime,	&upup->pr_ltime);
3003 	hrt2ts(pup->pr_slptime,	&upup->pr_slptime);
3004 	hrt2ts(pup->pr_wtime,	&upup->pr_wtime);
3005 	hrt2ts(pup->pr_stoptime, &upup->pr_stoptime);
3006 	bzero(upup->filltime, sizeof (upup->filltime));
3007 
3008 	ullp = &pup->pr_minf;
3009 	ulp = &upup->pr_minf;
3010 	for (i = 0; i < 22; i++)
3011 		*ulp++ = (ulong_t)*ullp++;
3012 }
3013 
3014 #ifdef _SYSCALL32_IMPL
3015 void
3016 prcvtusage32(prhusage_t *pup, prusage32_t *upup)
3017 {
3018 	uint64_t *ullp;
3019 	uint32_t *ulp;
3020 	int i;
3021 
3022 	upup->pr_lwpid = pup->pr_lwpid;
3023 	upup->pr_count = pup->pr_count;
3024 
3025 	hrt2ts32(pup->pr_tstamp,	&upup->pr_tstamp);
3026 	hrt2ts32(pup->pr_create,	&upup->pr_create);
3027 	hrt2ts32(pup->pr_term,		&upup->pr_term);
3028 	hrt2ts32(pup->pr_rtime,		&upup->pr_rtime);
3029 	hrt2ts32(pup->pr_utime,		&upup->pr_utime);
3030 	hrt2ts32(pup->pr_stime,		&upup->pr_stime);
3031 	hrt2ts32(pup->pr_ttime,		&upup->pr_ttime);
3032 	hrt2ts32(pup->pr_tftime,	&upup->pr_tftime);
3033 	hrt2ts32(pup->pr_dftime,	&upup->pr_dftime);
3034 	hrt2ts32(pup->pr_kftime,	&upup->pr_kftime);
3035 	hrt2ts32(pup->pr_ltime,		&upup->pr_ltime);
3036 	hrt2ts32(pup->pr_slptime,	&upup->pr_slptime);
3037 	hrt2ts32(pup->pr_wtime,		&upup->pr_wtime);
3038 	hrt2ts32(pup->pr_stoptime,	&upup->pr_stoptime);
3039 	bzero(upup->filltime, sizeof (upup->filltime));
3040 
3041 	ullp = &pup->pr_minf;
3042 	ulp = &upup->pr_minf;
3043 	for (i = 0; i < 22; i++)
3044 		*ulp++ = (uint32_t)*ullp++;
3045 }
3046 #endif	/* _SYSCALL32_IMPL */
3047 
3048 /*
3049  * Determine whether a set is empty.
3050  */
3051 int
3052 setisempty(uint32_t *sp, uint_t n)
3053 {
3054 	while (n--)
3055 		if (*sp++)
3056 			return (0);
3057 	return (1);
3058 }
3059 
3060 /*
3061  * Utility routine for establishing a watched area in the process.
3062  * Keep the list of watched areas sorted by virtual address.
3063  */
3064 int
3065 set_watched_area(proc_t *p, struct watched_area *pwa)
3066 {
3067 	caddr_t vaddr = pwa->wa_vaddr;
3068 	caddr_t eaddr = pwa->wa_eaddr;
3069 	ulong_t flags = pwa->wa_flags;
3070 	struct watched_area *target;
3071 	avl_index_t where;
3072 	int error = 0;
3073 
3074 	/* we must not be holding p->p_lock, but the process must be locked */
3075 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
3076 	ASSERT(p->p_proc_flag & P_PR_LOCK);
3077 
3078 	/*
3079 	 * If this is our first watchpoint, enable watchpoints for the process.
3080 	 */
3081 	if (!pr_watch_active(p)) {
3082 		kthread_t *t;
3083 
3084 		mutex_enter(&p->p_lock);
3085 		if ((t = p->p_tlist) != NULL) {
3086 			do {
3087 				watch_enable(t);
3088 			} while ((t = t->t_forw) != p->p_tlist);
3089 		}
3090 		mutex_exit(&p->p_lock);
3091 	}
3092 
3093 	target = pr_find_watched_area(p, pwa, &where);
3094 	if (target != NULL) {
3095 		/*
3096 		 * We discovered an existing, overlapping watched area.
3097 		 * Allow it only if it is an exact match.
3098 		 */
3099 		if (target->wa_vaddr != vaddr ||
3100 		    target->wa_eaddr != eaddr)
3101 			error = EINVAL;
3102 		else if (target->wa_flags != flags) {
3103 			error = set_watched_page(p, vaddr, eaddr,
3104 			    flags, target->wa_flags);
3105 			target->wa_flags = flags;
3106 		}
3107 		kmem_free(pwa, sizeof (struct watched_area));
3108 	} else {
3109 		avl_insert(&p->p_warea, pwa, where);
3110 		error = set_watched_page(p, vaddr, eaddr, flags, 0);
3111 	}
3112 
3113 	return (error);
3114 }
3115 
3116 /*
3117  * Utility routine for clearing a watched area in the process.
3118  * Must be an exact match of the virtual address.
3119  * size and flags don't matter.
3120  */
3121 int
3122 clear_watched_area(proc_t *p, struct watched_area *pwa)
3123 {
3124 	struct watched_area *found;
3125 
3126 	/* we must not be holding p->p_lock, but the process must be locked */
3127 	ASSERT(MUTEX_NOT_HELD(&p->p_lock));
3128 	ASSERT(p->p_proc_flag & P_PR_LOCK);
3129 
3130 
3131 	if (!pr_watch_active(p)) {
3132 		kmem_free(pwa, sizeof (struct watched_area));
3133 		return (0);
3134 	}
3135 
3136 	/*
3137 	 * Look for a matching address in the watched areas.  If a match is
3138 	 * found, clear the old watched area and adjust the watched page(s).  It
3139 	 * is not an error if there is no match.
3140 	 */
3141 	if ((found = pr_find_watched_area(p, pwa, NULL)) != NULL &&
3142 	    found->wa_vaddr == pwa->wa_vaddr) {
3143 		clear_watched_page(p, found->wa_vaddr, found->wa_eaddr,
3144 		    found->wa_flags);
3145 		avl_remove(&p->p_warea, found);
3146 		kmem_free(found, sizeof (struct watched_area));
3147 	}
3148 
3149 	kmem_free(pwa, sizeof (struct watched_area));
3150 
3151 	/*
3152 	 * If we removed the last watched area from the process, disable
3153 	 * watchpoints.
3154 	 */
3155 	if (!pr_watch_active(p)) {
3156 		kthread_t *t;
3157 
3158 		mutex_enter(&p->p_lock);
3159 		if ((t = p->p_tlist) != NULL) {
3160 			do {
3161 				watch_disable(t);
3162 			} while ((t = t->t_forw) != p->p_tlist);
3163 		}
3164 		mutex_exit(&p->p_lock);
3165 	}
3166 
3167 	return (0);
3168 }
3169 
3170 /*
3171  * Frees all the watched_area structures
3172  */
3173 void
3174 pr_free_watchpoints(proc_t *p)
3175 {
3176 	struct watched_area *delp;
3177 	void *cookie;
3178 
3179 	cookie = NULL;
3180 	while ((delp = avl_destroy_nodes(&p->p_warea, &cookie)) != NULL)
3181 		kmem_free(delp, sizeof (struct watched_area));
3182 
3183 	avl_destroy(&p->p_warea);
3184 }
3185 
3186 /*
3187  * This one is called by the traced process to unwatch all the
3188  * pages while deallocating the list of watched_page structs.
3189  */
3190 void
3191 pr_free_watched_pages(proc_t *p)
3192 {
3193 	struct as *as = p->p_as;
3194 	struct watched_page *pwp;
3195 	uint_t prot;
3196 	int    retrycnt, err;
3197 	void *cookie;
3198 
3199 	if (as == NULL || avl_numnodes(&as->a_wpage) == 0)
3200 		return;
3201 
3202 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
3203 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3204 
3205 	pwp = avl_first(&as->a_wpage);
3206 
3207 	cookie = NULL;
3208 	while ((pwp = avl_destroy_nodes(&as->a_wpage, &cookie)) != NULL) {
3209 		retrycnt = 0;
3210 		if ((prot = pwp->wp_oprot) != 0) {
3211 			caddr_t addr = pwp->wp_vaddr;
3212 			struct seg *seg;
3213 		retry:
3214 
3215 			if ((pwp->wp_prot != prot ||
3216 			    (pwp->wp_flags & WP_NOWATCH)) &&
3217 			    (seg = as_segat(as, addr)) != NULL) {
3218 				err = SEGOP_SETPROT(seg, addr, PAGESIZE, prot);
3219 				if (err == IE_RETRY) {
3220 					ASSERT(retrycnt == 0);
3221 					retrycnt++;
3222 					goto retry;
3223 				}
3224 			}
3225 		}
3226 		kmem_free(pwp, sizeof (struct watched_page));
3227 	}
3228 
3229 	avl_destroy(&as->a_wpage);
3230 	p->p_wprot = NULL;
3231 
3232 	AS_LOCK_EXIT(as, &as->a_lock);
3233 }
3234 
3235 /*
3236  * Insert a watched area into the list of watched pages.
3237  * If oflags is zero then we are adding a new watched area.
3238  * Otherwise we are changing the flags of an existing watched area.
3239  */
3240 static int
3241 set_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr,
3242 	ulong_t flags, ulong_t oflags)
3243 {
3244 	struct as *as = p->p_as;
3245 	avl_tree_t *pwp_tree;
3246 	struct watched_page *pwp, *newpwp;
3247 	struct watched_page tpw;
3248 	avl_index_t where;
3249 	struct seg *seg;
3250 	uint_t prot;
3251 	caddr_t addr;
3252 
3253 	/*
3254 	 * We need to pre-allocate a list of structures before we grab the
3255 	 * address space lock to avoid calling kmem_alloc(KM_SLEEP) with locks
3256 	 * held.
3257 	 */
3258 	newpwp = NULL;
3259 	for (addr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3260 	    addr < eaddr; addr += PAGESIZE) {
3261 		pwp = kmem_zalloc(sizeof (struct watched_page), KM_SLEEP);
3262 		pwp->wp_list = newpwp;
3263 		newpwp = pwp;
3264 	}
3265 
3266 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3267 
3268 	/*
3269 	 * Search for an existing watched page to contain the watched area.
3270 	 * If none is found, grab a new one from the available list
3271 	 * and insert it in the active list, keeping the list sorted
3272 	 * by user-level virtual address.
3273 	 */
3274 	if (p->p_flag & SVFWAIT)
3275 		pwp_tree = &p->p_wpage;
3276 	else
3277 		pwp_tree = &as->a_wpage;
3278 
3279 again:
3280 	if (avl_numnodes(pwp_tree) > prnwatch) {
3281 		AS_LOCK_EXIT(as, &as->a_lock);
3282 		while (newpwp != NULL) {
3283 			pwp = newpwp->wp_list;
3284 			kmem_free(newpwp, sizeof (struct watched_page));
3285 			newpwp = pwp;
3286 		}
3287 		return (E2BIG);
3288 	}
3289 
3290 	tpw.wp_vaddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3291 	if ((pwp = avl_find(pwp_tree, &tpw, &where)) == NULL) {
3292 		pwp = newpwp;
3293 		newpwp = newpwp->wp_list;
3294 		pwp->wp_list = NULL;
3295 		pwp->wp_vaddr = (caddr_t)((uintptr_t)vaddr &
3296 		    (uintptr_t)PAGEMASK);
3297 		avl_insert(pwp_tree, pwp, where);
3298 	}
3299 
3300 	ASSERT(vaddr >= pwp->wp_vaddr && vaddr < pwp->wp_vaddr + PAGESIZE);
3301 
3302 	if (oflags & WA_READ)
3303 		pwp->wp_read--;
3304 	if (oflags & WA_WRITE)
3305 		pwp->wp_write--;
3306 	if (oflags & WA_EXEC)
3307 		pwp->wp_exec--;
3308 
3309 	ASSERT(pwp->wp_read >= 0);
3310 	ASSERT(pwp->wp_write >= 0);
3311 	ASSERT(pwp->wp_exec >= 0);
3312 
3313 	if (flags & WA_READ)
3314 		pwp->wp_read++;
3315 	if (flags & WA_WRITE)
3316 		pwp->wp_write++;
3317 	if (flags & WA_EXEC)
3318 		pwp->wp_exec++;
3319 
3320 	if (!(p->p_flag & SVFWAIT)) {
3321 		vaddr = pwp->wp_vaddr;
3322 		if (pwp->wp_oprot == 0 &&
3323 		    (seg = as_segat(as, vaddr)) != NULL) {
3324 			SEGOP_GETPROT(seg, vaddr, 0, &prot);
3325 			pwp->wp_oprot = (uchar_t)prot;
3326 			pwp->wp_prot = (uchar_t)prot;
3327 		}
3328 		if (pwp->wp_oprot != 0) {
3329 			prot = pwp->wp_oprot;
3330 			if (pwp->wp_read)
3331 				prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3332 			if (pwp->wp_write)
3333 				prot &= ~PROT_WRITE;
3334 			if (pwp->wp_exec)
3335 				prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3336 			if (!(pwp->wp_flags & WP_NOWATCH) &&
3337 			    pwp->wp_prot != prot &&
3338 			    (pwp->wp_flags & WP_SETPROT) == 0) {
3339 				pwp->wp_flags |= WP_SETPROT;
3340 				pwp->wp_list = p->p_wprot;
3341 				p->p_wprot = pwp;
3342 			}
3343 			pwp->wp_prot = (uchar_t)prot;
3344 		}
3345 	}
3346 
3347 	/*
3348 	 * If the watched area extends into the next page then do
3349 	 * it over again with the virtual address of the next page.
3350 	 */
3351 	if ((vaddr = pwp->wp_vaddr + PAGESIZE) < eaddr)
3352 		goto again;
3353 
3354 	AS_LOCK_EXIT(as, &as->a_lock);
3355 
3356 	/*
3357 	 * Free any pages we may have over-allocated
3358 	 */
3359 	while (newpwp != NULL) {
3360 		pwp = newpwp->wp_list;
3361 		kmem_free(newpwp, sizeof (struct watched_page));
3362 		newpwp = pwp;
3363 	}
3364 
3365 	return (0);
3366 }
3367 
3368 /*
3369  * Remove a watched area from the list of watched pages.
3370  * A watched area may extend over more than one page.
3371  */
3372 static void
3373 clear_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr, ulong_t flags)
3374 {
3375 	struct as *as = p->p_as;
3376 	struct watched_page *pwp;
3377 	struct watched_page tpw;
3378 	avl_tree_t *tree;
3379 	avl_index_t where;
3380 
3381 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3382 
3383 	if (p->p_flag & SVFWAIT)
3384 		tree = &p->p_wpage;
3385 	else
3386 		tree = &as->a_wpage;
3387 
3388 	tpw.wp_vaddr = vaddr =
3389 	    (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK);
3390 	pwp = avl_find(tree, &tpw, &where);
3391 	if (pwp == NULL)
3392 		pwp = avl_nearest(tree, where, AVL_AFTER);
3393 
3394 	while (pwp != NULL && pwp->wp_vaddr < eaddr) {
3395 		ASSERT(vaddr <=  pwp->wp_vaddr);
3396 
3397 		if (flags & WA_READ)
3398 			pwp->wp_read--;
3399 		if (flags & WA_WRITE)
3400 			pwp->wp_write--;
3401 		if (flags & WA_EXEC)
3402 			pwp->wp_exec--;
3403 
3404 		if (pwp->wp_read + pwp->wp_write + pwp->wp_exec != 0) {
3405 			/*
3406 			 * Reset the hat layer's protections on this page.
3407 			 */
3408 			if (pwp->wp_oprot != 0) {
3409 				uint_t prot = pwp->wp_oprot;
3410 
3411 				if (pwp->wp_read)
3412 					prot &=
3413 					    ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3414 				if (pwp->wp_write)
3415 					prot &= ~PROT_WRITE;
3416 				if (pwp->wp_exec)
3417 					prot &=
3418 					    ~(PROT_READ|PROT_WRITE|PROT_EXEC);
3419 				if (!(pwp->wp_flags & WP_NOWATCH) &&
3420 				    pwp->wp_prot != prot &&
3421 				    (pwp->wp_flags & WP_SETPROT) == 0) {
3422 					pwp->wp_flags |= WP_SETPROT;
3423 					pwp->wp_list = p->p_wprot;
3424 					p->p_wprot = pwp;
3425 				}
3426 				pwp->wp_prot = (uchar_t)prot;
3427 			}
3428 		} else {
3429 			/*
3430 			 * No watched areas remain in this page.
3431 			 * Reset everything to normal.
3432 			 */
3433 			if (pwp->wp_oprot != 0) {
3434 				pwp->wp_prot = pwp->wp_oprot;
3435 				if ((pwp->wp_flags & WP_SETPROT) == 0) {
3436 					pwp->wp_flags |= WP_SETPROT;
3437 					pwp->wp_list = p->p_wprot;
3438 					p->p_wprot = pwp;
3439 				}
3440 			}
3441 		}
3442 
3443 		pwp = AVL_NEXT(tree, pwp);
3444 	}
3445 
3446 	AS_LOCK_EXIT(as, &as->a_lock);
3447 }
3448 
3449 /*
3450  * Return the original protections for the specified page.
3451  */
3452 static void
3453 getwatchprot(struct as *as, caddr_t addr, uint_t *prot)
3454 {
3455 	struct watched_page *pwp;
3456 	struct watched_page tpw;
3457 
3458 	ASSERT(AS_LOCK_HELD(as, &as->a_lock));
3459 
3460 	tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK);
3461 	if ((pwp = avl_find(&as->a_wpage, &tpw, NULL)) != NULL)
3462 		*prot = pwp->wp_oprot;
3463 }
3464 
3465 static prpagev_t *
3466 pr_pagev_create(struct seg *seg, int check_noreserve)
3467 {
3468 	prpagev_t *pagev = kmem_alloc(sizeof (prpagev_t), KM_SLEEP);
3469 	size_t total_pages = seg_pages(seg);
3470 
3471 	/*
3472 	 * Limit the size of our vectors to pagev_lim pages at a time.  We need
3473 	 * 4 or 5 bytes of storage per page, so this means we limit ourself
3474 	 * to about a megabyte of kernel heap by default.
3475 	 */
3476 	pagev->pg_npages = MIN(total_pages, pagev_lim);
3477 	pagev->pg_pnbase = 0;
3478 
3479 	pagev->pg_protv =
3480 	    kmem_alloc(pagev->pg_npages * sizeof (uint_t), KM_SLEEP);
3481 
3482 	if (check_noreserve)
3483 		pagev->pg_incore =
3484 		    kmem_alloc(pagev->pg_npages * sizeof (char), KM_SLEEP);
3485 	else
3486 		pagev->pg_incore = NULL;
3487 
3488 	return (pagev);
3489 }
3490 
3491 static void
3492 pr_pagev_destroy(prpagev_t *pagev)
3493 {
3494 	if (pagev->pg_incore != NULL)
3495 		kmem_free(pagev->pg_incore, pagev->pg_npages * sizeof (char));
3496 
3497 	kmem_free(pagev->pg_protv, pagev->pg_npages * sizeof (uint_t));
3498 	kmem_free(pagev, sizeof (prpagev_t));
3499 }
3500 
3501 static caddr_t
3502 pr_pagev_fill(prpagev_t *pagev, struct seg *seg, caddr_t addr, caddr_t eaddr)
3503 {
3504 	ulong_t lastpg = seg_page(seg, eaddr - 1);
3505 	ulong_t pn, pnlim;
3506 	caddr_t saddr;
3507 	size_t len;
3508 
3509 	ASSERT(addr >= seg->s_base && addr <= eaddr);
3510 
3511 	if (addr == eaddr)
3512 		return (eaddr);
3513 
3514 refill:
3515 	ASSERT(addr < eaddr);
3516 	pagev->pg_pnbase = seg_page(seg, addr);
3517 	pnlim = pagev->pg_pnbase + pagev->pg_npages;
3518 	saddr = addr;
3519 
3520 	if (lastpg < pnlim)
3521 		len = (size_t)(eaddr - addr);
3522 	else
3523 		len = pagev->pg_npages * PAGESIZE;
3524 
3525 	if (pagev->pg_incore != NULL) {
3526 		/*
3527 		 * INCORE cleverly has different semantics than GETPROT:
3528 		 * it returns info on pages up to but NOT including addr + len.
3529 		 */
3530 		SEGOP_INCORE(seg, addr, len, pagev->pg_incore);
3531 		pn = pagev->pg_pnbase;
3532 
3533 		do {
3534 			/*
3535 			 * Guilty knowledge here:  We know that segvn_incore
3536 			 * returns more than just the low-order bit that
3537 			 * indicates the page is actually in memory.  If any
3538 			 * bits are set, then the page has backing store.
3539 			 */
3540 			if (pagev->pg_incore[pn++ - pagev->pg_pnbase])
3541 				goto out;
3542 
3543 		} while ((addr += PAGESIZE) < eaddr && pn < pnlim);
3544 
3545 		/*
3546 		 * If we examined all the pages in the vector but we're not
3547 		 * at the end of the segment, take another lap.
3548 		 */
3549 		if (addr < eaddr)
3550 			goto refill;
3551 	}
3552 
3553 	/*
3554 	 * Need to take len - 1 because addr + len is the address of the
3555 	 * first byte of the page just past the end of what we want.
3556 	 */
3557 out:
3558 	SEGOP_GETPROT(seg, saddr, len - 1, pagev->pg_protv);
3559 	return (addr);
3560 }
3561 
3562 static caddr_t
3563 pr_pagev_nextprot(prpagev_t *pagev, struct seg *seg,
3564     caddr_t *saddrp, caddr_t eaddr, uint_t *protp)
3565 {
3566 	/*
3567 	 * Our starting address is either the specified address, or the base
3568 	 * address from the start of the pagev.  If the latter is greater,
3569 	 * this means a previous call to pr_pagev_fill has already scanned
3570 	 * further than the end of the previous mapping.
3571 	 */
3572 	caddr_t base = seg->s_base + pagev->pg_pnbase * PAGESIZE;
3573 	caddr_t addr = MAX(*saddrp, base);
3574 	ulong_t pn = seg_page(seg, addr);
3575 	uint_t prot, nprot;
3576 
3577 	/*
3578 	 * If we're dealing with noreserve pages, then advance addr to
3579 	 * the address of the next page which has backing store.
3580 	 */
3581 	if (pagev->pg_incore != NULL) {
3582 		while (pagev->pg_incore[pn - pagev->pg_pnbase] == 0) {
3583 			if ((addr += PAGESIZE) == eaddr) {
3584 				*saddrp = addr;
3585 				prot = 0;
3586 				goto out;
3587 			}
3588 			if (++pn == pagev->pg_pnbase + pagev->pg_npages) {
3589 				addr = pr_pagev_fill(pagev, seg, addr, eaddr);
3590 				if (addr == eaddr) {
3591 					*saddrp = addr;
3592 					prot = 0;
3593 					goto out;
3594 				}
3595 				pn = seg_page(seg, addr);
3596 			}
3597 		}
3598 	}
3599 
3600 	/*
3601 	 * Get the protections on the page corresponding to addr.
3602 	 */
3603 	pn = seg_page(seg, addr);
3604 	ASSERT(pn >= pagev->pg_pnbase);
3605 	ASSERT(pn < (pagev->pg_pnbase + pagev->pg_npages));
3606 
3607 	prot = pagev->pg_protv[pn - pagev->pg_pnbase];
3608 	getwatchprot(seg->s_as, addr, &prot);
3609 	*saddrp = addr;
3610 
3611 	/*
3612 	 * Now loop until we find a backed page with different protections
3613 	 * or we reach the end of this segment.
3614 	 */
3615 	while ((addr += PAGESIZE) < eaddr) {
3616 		/*
3617 		 * If pn has advanced to the page number following what we
3618 		 * have information on, refill the page vector and reset
3619 		 * addr and pn.  If pr_pagev_fill does not return the
3620 		 * address of the next page, we have a discontiguity and
3621 		 * thus have reached the end of the current mapping.
3622 		 */
3623 		if (++pn == pagev->pg_pnbase + pagev->pg_npages) {
3624 			caddr_t naddr = pr_pagev_fill(pagev, seg, addr, eaddr);
3625 			if (naddr != addr)
3626 				goto out;
3627 			pn = seg_page(seg, addr);
3628 		}
3629 
3630 		/*
3631 		 * The previous page's protections are in prot, and it has
3632 		 * backing.  If this page is MAP_NORESERVE and has no backing,
3633 		 * then end this mapping and return the previous protections.
3634 		 */
3635 		if (pagev->pg_incore != NULL &&
3636 		    pagev->pg_incore[pn - pagev->pg_pnbase] == 0)
3637 			break;
3638 
3639 		/*
3640 		 * Otherwise end the mapping if this page's protections (nprot)
3641 		 * are different than those in the previous page (prot).
3642 		 */
3643 		nprot = pagev->pg_protv[pn - pagev->pg_pnbase];
3644 		getwatchprot(seg->s_as, addr, &nprot);
3645 
3646 		if (nprot != prot)
3647 			break;
3648 	}
3649 
3650 out:
3651 	*protp = prot;
3652 	return (addr);
3653 }
3654 
3655 size_t
3656 pr_getsegsize(struct seg *seg, int reserved)
3657 {
3658 	size_t size = seg->s_size;
3659 
3660 	/*
3661 	 * If we're interested in the reserved space, return the size of the
3662 	 * segment itself.  Everything else in this function is a special case
3663 	 * to determine the actual underlying size of various segment types.
3664 	 */
3665 	if (reserved)
3666 		return (size);
3667 
3668 	/*
3669 	 * If this is a segvn mapping of a regular file, return the smaller
3670 	 * of the segment size and the remaining size of the file beyond
3671 	 * the file offset corresponding to seg->s_base.
3672 	 */
3673 	if (seg->s_ops == &segvn_ops) {
3674 		vattr_t vattr;
3675 		vnode_t *vp;
3676 
3677 		vattr.va_mask = AT_SIZE;
3678 
3679 		if (SEGOP_GETVP(seg, seg->s_base, &vp) == 0 &&
3680 		    vp != NULL && vp->v_type == VREG &&
3681 		    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
3682 
3683 			u_offset_t fsize = vattr.va_size;
3684 			u_offset_t offset = SEGOP_GETOFFSET(seg, seg->s_base);
3685 
3686 			if (fsize < offset)
3687 				fsize = 0;
3688 			else
3689 				fsize -= offset;
3690 
3691 			fsize = roundup(fsize, (u_offset_t)PAGESIZE);
3692 
3693 			if (fsize < (u_offset_t)size)
3694 				size = (size_t)fsize;
3695 		}
3696 
3697 		return (size);
3698 	}
3699 
3700 	/*
3701 	 * If this is an ISM shared segment, don't include pages that are
3702 	 * beyond the real size of the spt segment that backs it.
3703 	 */
3704 	if (seg->s_ops == &segspt_shmops)
3705 		return (MIN(spt_realsize(seg), size));
3706 
3707 	/*
3708 	 * If this is segment is a mapping from /dev/null, then this is a
3709 	 * reservation of virtual address space and has no actual size.
3710 	 * Such segments are backed by segdev and have type set to neither
3711 	 * MAP_SHARED nor MAP_PRIVATE.
3712 	 */
3713 	if (seg->s_ops == &segdev_ops &&
3714 	    ((SEGOP_GETTYPE(seg, seg->s_base) &
3715 		(MAP_SHARED | MAP_PRIVATE)) == 0))
3716 		return (0);
3717 
3718 	/*
3719 	 * If this segment doesn't match one of the special types we handle,
3720 	 * just return the size of the segment itself.
3721 	 */
3722 	return (size);
3723 }
3724 
3725 uint_t
3726 pr_getprot(struct seg *seg, int reserved, void **tmp,
3727 	caddr_t *saddrp, caddr_t *naddrp, caddr_t eaddr)
3728 {
3729 	struct as *as = seg->s_as;
3730 
3731 	caddr_t saddr = *saddrp;
3732 	caddr_t naddr;
3733 
3734 	int check_noreserve;
3735 	uint_t prot;
3736 
3737 	union {
3738 		struct segvn_data *svd;
3739 		struct segdev_data *sdp;
3740 		void *data;
3741 	} s;
3742 
3743 	s.data = seg->s_data;
3744 
3745 	ASSERT(AS_WRITE_HELD(as, &as->a_lock));
3746 	ASSERT(saddr >= seg->s_base && saddr < eaddr);
3747 	ASSERT(eaddr <= seg->s_base + seg->s_size);
3748 
3749 	/*
3750 	 * Don't include MAP_NORESERVE pages in the address range
3751 	 * unless their mappings have actually materialized.
3752 	 * We cheat by knowing that segvn is the only segment
3753 	 * driver that supports MAP_NORESERVE.
3754 	 */
3755 	check_noreserve =
3756 	    (!reserved && seg->s_ops == &segvn_ops && s.svd != NULL &&
3757 	    (s.svd->vp == NULL || s.svd->vp->v_type != VREG) &&
3758 	    (s.svd->flags & MAP_NORESERVE));
3759 
3760 	/*
3761 	 * Examine every page only as a last resort.  We use guilty knowledge
3762 	 * of segvn and segdev to avoid this: if there are no per-page
3763 	 * protections present in the segment and we don't care about
3764 	 * MAP_NORESERVE, then s_data->prot is the prot for the whole segment.
3765 	 */
3766 	if (!check_noreserve && saddr == seg->s_base &&
3767 	    seg->s_ops == &segvn_ops && s.svd != NULL && s.svd->pageprot == 0) {
3768 		prot = s.svd->prot;
3769 		getwatchprot(as, saddr, &prot);
3770 		naddr = eaddr;
3771 
3772 	} else if (saddr == seg->s_base && seg->s_ops == &segdev_ops &&
3773 	    s.sdp != NULL && s.sdp->pageprot == 0) {
3774 		prot = s.sdp->prot;
3775 		getwatchprot(as, saddr, &prot);
3776 		naddr = eaddr;
3777 
3778 	} else {
3779 		prpagev_t *pagev;
3780 
3781 		/*
3782 		 * If addr is sitting at the start of the segment, then
3783 		 * create a page vector to store protection and incore
3784 		 * information for pages in the segment, and fill it.
3785 		 * Otherwise, we expect *tmp to address the prpagev_t
3786 		 * allocated by a previous call to this function.
3787 		 */
3788 		if (saddr == seg->s_base) {
3789 			pagev = pr_pagev_create(seg, check_noreserve);
3790 			saddr = pr_pagev_fill(pagev, seg, saddr, eaddr);
3791 
3792 			ASSERT(*tmp == NULL);
3793 			*tmp = pagev;
3794 
3795 			ASSERT(saddr <= eaddr);
3796 			*saddrp = saddr;
3797 
3798 			if (saddr == eaddr) {
3799 				naddr = saddr;
3800 				prot = 0;
3801 				goto out;
3802 			}
3803 
3804 		} else {
3805 			ASSERT(*tmp != NULL);
3806 			pagev = (prpagev_t *)*tmp;
3807 		}
3808 
3809 		naddr = pr_pagev_nextprot(pagev, seg, saddrp, eaddr, &prot);
3810 		ASSERT(naddr <= eaddr);
3811 	}
3812 
3813 out:
3814 	if (naddr == eaddr)
3815 		pr_getprot_done(tmp);
3816 	*naddrp = naddr;
3817 	return (prot);
3818 }
3819 
3820 void
3821 pr_getprot_done(void **tmp)
3822 {
3823 	if (*tmp != NULL) {
3824 		pr_pagev_destroy((prpagev_t *)*tmp);
3825 		*tmp = NULL;
3826 	}
3827 }
3828 
3829 /*
3830  * Return true iff the vnode is a /proc file from the object directory.
3831  */
3832 int
3833 pr_isobject(vnode_t *vp)
3834 {
3835 	return (vn_matchops(vp, prvnodeops) && VTOP(vp)->pr_type == PR_OBJECT);
3836 }
3837 
3838 /*
3839  * Return true iff the vnode is a /proc file opened by the process itself.
3840  */
3841 int
3842 pr_isself(vnode_t *vp)
3843 {
3844 	/*
3845 	 * XXX: To retain binary compatibility with the old
3846 	 * ioctl()-based version of /proc, we exempt self-opens
3847 	 * of /proc/<pid> from being marked close-on-exec.
3848 	 */
3849 	return (vn_matchops(vp, prvnodeops) &&
3850 	    (VTOP(vp)->pr_flags & PR_ISSELF) &&
3851 	    VTOP(vp)->pr_type != PR_PIDDIR);
3852 }
3853 
3854 static ssize_t
3855 pr_getpagesize(struct seg *seg, caddr_t saddr, caddr_t *naddrp, caddr_t eaddr)
3856 {
3857 	ssize_t pagesize, hatsize;
3858 
3859 	ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3860 	ASSERT(IS_P2ALIGNED(saddr, PAGESIZE));
3861 	ASSERT(IS_P2ALIGNED(eaddr, PAGESIZE));
3862 	ASSERT(saddr < eaddr);
3863 
3864 	pagesize = hatsize = hat_getpagesize(seg->s_as->a_hat, saddr);
3865 	ASSERT(pagesize == -1 || IS_P2ALIGNED(pagesize, pagesize));
3866 	ASSERT(pagesize != 0);
3867 
3868 	if (pagesize == -1)
3869 		pagesize = PAGESIZE;
3870 
3871 	saddr += P2NPHASE((uintptr_t)saddr, pagesize);
3872 
3873 	while (saddr < eaddr) {
3874 		if (hatsize != hat_getpagesize(seg->s_as->a_hat, saddr))
3875 			break;
3876 		ASSERT(IS_P2ALIGNED(saddr, pagesize));
3877 		saddr += pagesize;
3878 	}
3879 
3880 	*naddrp = ((saddr < eaddr) ? saddr : eaddr);
3881 	return (hatsize);
3882 }
3883 
3884 /*
3885  * Return an array of structures with extended memory map information.
3886  * We allocate here; the caller must deallocate.
3887  */
3888 int
3889 prgetxmap(proc_t *p, list_t *iolhead)
3890 {
3891 	struct as *as = p->p_as;
3892 	prxmap_t *mp;
3893 	struct seg *seg;
3894 	struct seg *brkseg, *stkseg;
3895 	struct vnode *vp;
3896 	struct vattr vattr;
3897 	uint_t prot;
3898 
3899 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3900 
3901 	/*
3902 	 * Request an initial buffer size that doesn't waste memory
3903 	 * if the address space has only a small number of segments.
3904 	 */
3905 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
3906 
3907 	if ((seg = AS_SEGFIRST(as)) == NULL)
3908 		return (0);
3909 
3910 	brkseg = break_seg(p);
3911 	stkseg = as_segat(as, prgetstackbase(p));
3912 
3913 	do {
3914 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3915 		caddr_t saddr, naddr, baddr;
3916 		void *tmp = NULL;
3917 		ssize_t psz;
3918 		char *parr;
3919 		uint64_t npages;
3920 		uint64_t pagenum;
3921 
3922 		/*
3923 		 * Segment loop part one: iterate from the base of the segment
3924 		 * to its end, pausing at each address boundary (baddr) between
3925 		 * ranges that have different virtual memory protections.
3926 		 */
3927 		for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) {
3928 			prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr);
3929 			ASSERT(baddr >= saddr && baddr <= eaddr);
3930 
3931 			/*
3932 			 * Segment loop part two: iterate from the current
3933 			 * position to the end of the protection boundary,
3934 			 * pausing at each address boundary (naddr) between
3935 			 * ranges that have different underlying page sizes.
3936 			 */
3937 			for (; saddr < baddr; saddr = naddr) {
3938 				psz = pr_getpagesize(seg, saddr, &naddr, baddr);
3939 				ASSERT(naddr >= saddr && naddr <= baddr);
3940 
3941 				mp = pr_iol_newbuf(iolhead, sizeof (*mp));
3942 
3943 				mp->pr_vaddr = (uintptr_t)saddr;
3944 				mp->pr_size = naddr - saddr;
3945 				mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
3946 				mp->pr_mflags = 0;
3947 				if (prot & PROT_READ)
3948 					mp->pr_mflags |= MA_READ;
3949 				if (prot & PROT_WRITE)
3950 					mp->pr_mflags |= MA_WRITE;
3951 				if (prot & PROT_EXEC)
3952 					mp->pr_mflags |= MA_EXEC;
3953 				if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3954 					mp->pr_mflags |= MA_SHARED;
3955 				if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
3956 					mp->pr_mflags |= MA_NORESERVE;
3957 				if (seg->s_ops == &segspt_shmops ||
3958 				    (seg->s_ops == &segvn_ops &&
3959 				    (SEGOP_GETVP(seg, saddr, &vp) != 0 ||
3960 				    vp == NULL)))
3961 					mp->pr_mflags |= MA_ANON;
3962 				if (seg == brkseg)
3963 					mp->pr_mflags |= MA_BREAK;
3964 				else if (seg == stkseg)
3965 					mp->pr_mflags |= MA_STACK;
3966 				if (seg->s_ops == &segspt_shmops)
3967 					mp->pr_mflags |= MA_ISM | MA_SHM;
3968 
3969 				mp->pr_pagesize = PAGESIZE;
3970 				if (psz == -1) {
3971 					mp->pr_hatpagesize = 0;
3972 				} else {
3973 					mp->pr_hatpagesize = psz;
3974 				}
3975 
3976 				/*
3977 				 * Manufacture a filename for the "object" dir.
3978 				 */
3979 				mp->pr_dev = PRNODEV;
3980 				vattr.va_mask = AT_FSID|AT_NODEID;
3981 				if (seg->s_ops == &segvn_ops &&
3982 				    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
3983 				    vp != NULL && vp->v_type == VREG &&
3984 				    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
3985 					mp->pr_dev = vattr.va_fsid;
3986 					mp->pr_ino = vattr.va_nodeid;
3987 					if (vp == p->p_exec)
3988 						(void) strcpy(mp->pr_mapname,
3989 						    "a.out");
3990 					else
3991 						pr_object_name(mp->pr_mapname,
3992 						    vp, &vattr);
3993 				}
3994 
3995 				/*
3996 				 * Get the SysV shared memory id, if any.
3997 				 */
3998 				if ((mp->pr_mflags & MA_SHARED) &&
3999 				    p->p_segacct && (mp->pr_shmid = shmgetid(p,
4000 				    seg->s_base)) != SHMID_NONE) {
4001 					if (mp->pr_shmid == SHMID_FREE)
4002 						mp->pr_shmid = -1;
4003 
4004 					mp->pr_mflags |= MA_SHM;
4005 				} else {
4006 					mp->pr_shmid = -1;
4007 				}
4008 
4009 				npages = ((uintptr_t)(naddr - saddr)) >>
4010 				    PAGESHIFT;
4011 				parr = kmem_zalloc(npages, KM_SLEEP);
4012 
4013 				SEGOP_INCORE(seg, saddr, naddr - saddr, parr);
4014 
4015 				for (pagenum = 0; pagenum < npages; pagenum++) {
4016 					if (parr[pagenum] & SEG_PAGE_INCORE)
4017 						mp->pr_rss++;
4018 					if (parr[pagenum] & SEG_PAGE_ANON)
4019 						mp->pr_anon++;
4020 					if (parr[pagenum] & SEG_PAGE_LOCKED)
4021 						mp->pr_locked++;
4022 				}
4023 				kmem_free(parr, npages);
4024 			}
4025 		}
4026 		ASSERT(tmp == NULL);
4027 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
4028 
4029 	return (0);
4030 }
4031 
4032 /*
4033  * Return the process's credentials.  We don't need a 32-bit equivalent of
4034  * this function because prcred_t and prcred32_t are actually the same.
4035  */
4036 void
4037 prgetcred(proc_t *p, prcred_t *pcrp)
4038 {
4039 	mutex_enter(&p->p_crlock);
4040 	cred2prcred(p->p_cred, pcrp);
4041 	mutex_exit(&p->p_crlock);
4042 }
4043 
4044 /*
4045  * Compute actual size of the prpriv_t structure.
4046  */
4047 
4048 size_t
4049 prgetprivsize(void)
4050 {
4051 	return (priv_prgetprivsize(NULL));
4052 }
4053 
4054 /*
4055  * Return the process's privileges.  We don't need a 32-bit equivalent of
4056  * this function because prpriv_t and prpriv32_t are actually the same.
4057  */
4058 void
4059 prgetpriv(proc_t *p, prpriv_t *pprp)
4060 {
4061 	mutex_enter(&p->p_crlock);
4062 	cred2prpriv(p->p_cred, pprp);
4063 	mutex_exit(&p->p_crlock);
4064 }
4065 
4066 #ifdef _SYSCALL32_IMPL
4067 /*
4068  * Return an array of structures with HAT memory map information.
4069  * We allocate here; the caller must deallocate.
4070  */
4071 int
4072 prgetxmap32(proc_t *p, list_t *iolhead)
4073 {
4074 	struct as *as = p->p_as;
4075 	prxmap32_t *mp;
4076 	struct seg *seg;
4077 	struct seg *brkseg, *stkseg;
4078 	struct vnode *vp;
4079 	struct vattr vattr;
4080 	uint_t prot;
4081 
4082 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
4083 
4084 	/*
4085 	 * Request an initial buffer size that doesn't waste memory
4086 	 * if the address space has only a small number of segments.
4087 	 */
4088 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
4089 
4090 	if ((seg = AS_SEGFIRST(as)) == NULL)
4091 		return (0);
4092 
4093 	brkseg = break_seg(p);
4094 	stkseg = as_segat(as, prgetstackbase(p));
4095 
4096 	do {
4097 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
4098 		caddr_t saddr, naddr, baddr;
4099 		void *tmp = NULL;
4100 		ssize_t psz;
4101 		char *parr;
4102 		uint64_t npages;
4103 		uint64_t pagenum;
4104 
4105 		/*
4106 		 * Segment loop part one: iterate from the base of the segment
4107 		 * to its end, pausing at each address boundary (baddr) between
4108 		 * ranges that have different virtual memory protections.
4109 		 */
4110 		for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) {
4111 			prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr);
4112 			ASSERT(baddr >= saddr && baddr <= eaddr);
4113 
4114 			/*
4115 			 * Segment loop part two: iterate from the current
4116 			 * position to the end of the protection boundary,
4117 			 * pausing at each address boundary (naddr) between
4118 			 * ranges that have different underlying page sizes.
4119 			 */
4120 			for (; saddr < baddr; saddr = naddr) {
4121 				psz = pr_getpagesize(seg, saddr, &naddr, baddr);
4122 				ASSERT(naddr >= saddr && naddr <= baddr);
4123 
4124 				mp = pr_iol_newbuf(iolhead, sizeof (*mp));
4125 
4126 				mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
4127 				mp->pr_size = (size32_t)(naddr - saddr);
4128 				mp->pr_offset = SEGOP_GETOFFSET(seg, saddr);
4129 				mp->pr_mflags = 0;
4130 				if (prot & PROT_READ)
4131 					mp->pr_mflags |= MA_READ;
4132 				if (prot & PROT_WRITE)
4133 					mp->pr_mflags |= MA_WRITE;
4134 				if (prot & PROT_EXEC)
4135 					mp->pr_mflags |= MA_EXEC;
4136 				if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
4137 					mp->pr_mflags |= MA_SHARED;
4138 				if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE)
4139 					mp->pr_mflags |= MA_NORESERVE;
4140 				if (seg->s_ops == &segspt_shmops ||
4141 				    (seg->s_ops == &segvn_ops &&
4142 				    (SEGOP_GETVP(seg, saddr, &vp) != 0 ||
4143 				    vp == NULL)))
4144 					mp->pr_mflags |= MA_ANON;
4145 				if (seg == brkseg)
4146 					mp->pr_mflags |= MA_BREAK;
4147 				else if (seg == stkseg)
4148 					mp->pr_mflags |= MA_STACK;
4149 				if (seg->s_ops == &segspt_shmops)
4150 					mp->pr_mflags |= MA_ISM | MA_SHM;
4151 
4152 				mp->pr_pagesize = PAGESIZE;
4153 				if (psz == -1) {
4154 					mp->pr_hatpagesize = 0;
4155 				} else {
4156 					mp->pr_hatpagesize = psz;
4157 				}
4158 
4159 				/*
4160 				 * Manufacture a filename for the "object" dir.
4161 				 */
4162 				mp->pr_dev = PRNODEV32;
4163 				vattr.va_mask = AT_FSID|AT_NODEID;
4164 				if (seg->s_ops == &segvn_ops &&
4165 				    SEGOP_GETVP(seg, saddr, &vp) == 0 &&
4166 				    vp != NULL && vp->v_type == VREG &&
4167 				    VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) {
4168 					(void) cmpldev(&mp->pr_dev,
4169 					    vattr.va_fsid);
4170 					mp->pr_ino = vattr.va_nodeid;
4171 					if (vp == p->p_exec)
4172 						(void) strcpy(mp->pr_mapname,
4173 						    "a.out");
4174 					else
4175 						pr_object_name(mp->pr_mapname,
4176 						    vp, &vattr);
4177 				}
4178 
4179 				/*
4180 				 * Get the SysV shared memory id, if any.
4181 				 */
4182 				if ((mp->pr_mflags & MA_SHARED) &&
4183 				    p->p_segacct && (mp->pr_shmid = shmgetid(p,
4184 				    seg->s_base)) != SHMID_NONE) {
4185 					if (mp->pr_shmid == SHMID_FREE)
4186 						mp->pr_shmid = -1;
4187 
4188 					mp->pr_mflags |= MA_SHM;
4189 				} else {
4190 					mp->pr_shmid = -1;
4191 				}
4192 
4193 				npages = ((uintptr_t)(naddr - saddr)) >>
4194 				    PAGESHIFT;
4195 				parr = kmem_zalloc(npages, KM_SLEEP);
4196 
4197 				SEGOP_INCORE(seg, saddr, naddr - saddr, parr);
4198 
4199 				for (pagenum = 0; pagenum < npages; pagenum++) {
4200 					if (parr[pagenum] & SEG_PAGE_INCORE)
4201 						mp->pr_rss++;
4202 					if (parr[pagenum] & SEG_PAGE_ANON)
4203 						mp->pr_anon++;
4204 					if (parr[pagenum] & SEG_PAGE_LOCKED)
4205 						mp->pr_locked++;
4206 				}
4207 				kmem_free(parr, npages);
4208 			}
4209 		}
4210 		ASSERT(tmp == NULL);
4211 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
4212 
4213 	return (0);
4214 }
4215 #endif	/* _SYSCALL32_IMPL */
4216