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