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