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