xref: /titanic_44/usr/src/uts/common/fs/proc/prioctl.c (revision 4a6ec905b96eb96a398c346f59e034a90ce8ad37)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All rights reserved.  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.29	*/
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/vmparam.h>
35 #include <sys/var.h>
36 #include <sys/cmn_err.h>
37 #include <sys/cred.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
40 #include <sys/file.h>
41 #include <sys/inline.h>
42 #include <sys/kmem.h>
43 #include <sys/proc.h>
44 #include <sys/sysmacros.h>
45 #include <sys/systm.h>
46 #include <sys/vfs.h>
47 #include <sys/vnode.h>
48 #include <sys/cpuvar.h>
49 #include <sys/session.h>
50 #include <sys/signal.h>
51 #include <sys/auxv.h>
52 #include <sys/user.h>
53 #include <sys/disp.h>
54 #include <sys/class.h>
55 #include <sys/ts.h>
56 #include <sys/mman.h>
57 #include <sys/fault.h>
58 #include <sys/syscall.h>
59 #include <sys/schedctl.h>
60 #include <sys/pset.h>
61 #include <sys/old_procfs.h>
62 #include <sys/zone.h>
63 #include <sys/time.h>
64 #include <sys/msacct.h>
65 #include <vm/rm.h>
66 #include <vm/as.h>
67 #include <vm/rm.h>
68 #include <vm/seg.h>
69 #include <vm/seg_vn.h>
70 #include <sys/contract_impl.h>
71 #include <sys/ctfs_impl.h>
72 #include <sys/ctfs.h>
73 
74 #if defined(__i386) || defined(__i386_COMPAT)
75 #include <sys/sysi86.h>
76 #endif
77 
78 #include <fs/proc/prdata.h>
79 
80 static	int	isprwrioctl(int);
81 static	ulong_t	prmaprunflags(long);
82 static	long	prmapsetflags(long);
83 static	void	prsetrun(kthread_t *, prrun_t *);
84 static	int	propenm(prnode_t *, caddr_t, caddr_t, int *, cred_t *);
85 extern	void	oprgetstatus(kthread_t *, prstatus_t *, zone_t *);
86 extern	void	oprgetpsinfo(proc_t *, prpsinfo_t *, kthread_t *);
87 static	int	oprgetmap(proc_t *, list_t *);
88 
89 /*ARGSUSED*/
90 static int
91 prctioctl(prnode_t *pnp, int cmd, intptr_t arg, int flag, cred_t *cr)
92 {
93 	int error = 0;
94 	ct_param_t param;
95 	ct_template_t *tmpl;
96 
97 	if (cmd != CT_TSET && cmd != CT_TGET)
98 		return (EINVAL);
99 
100 	if (copyin((void *)arg, &param, sizeof (ct_param_t)))
101 		return (EFAULT);
102 
103 	if ((error = prlock(pnp, ZNO)) != 0)
104 		return (error);
105 
106 	tmpl = pnp->pr_common->prc_thread->t_lwp->lwp_ct_active[pnp->pr_cttype];
107 	if (tmpl == NULL) {
108 		prunlock(pnp);
109 		return (ESTALE);
110 	}
111 
112 	if (cmd == CT_TSET)
113 		error = ctmpl_set(tmpl, &param, cr);
114 	else
115 		error = ctmpl_get(tmpl, &param);
116 
117 	prunlock(pnp);
118 
119 	if (cmd == CT_TGET && error == 0 &&
120 	    copyout(&param, (void *)arg, sizeof (ct_param_t)))
121 		error = EFAULT;
122 
123 	return (error);
124 }
125 
126 
127 /*
128  * Control operations (lots).
129  */
130 /*ARGSUSED*/
131 #ifdef _SYSCALL32_IMPL
132 static int
133 prioctl64(
134 	struct vnode *vp,
135 	int cmd,
136 	intptr_t arg,
137 	int flag,
138 	cred_t *cr,
139 	int *rvalp,
140 	caller_context_t *ct)
141 #else
142 int
143 prioctl(
144 	struct vnode *vp,
145 	int cmd,
146 	intptr_t arg,
147 	int flag,
148 	cred_t *cr,
149 	int *rvalp,
150 	caller_context_t *ct)
151 #endif	/* _SYSCALL32_IMPL */
152 {
153 	caddr_t cmaddr = (caddr_t)arg;
154 	proc_t *p;
155 	user_t *up;
156 	kthread_t *t;
157 	klwp_t *lwp;
158 	prnode_t *pnp = VTOP(vp);
159 	prcommon_t *pcp;
160 	prnode_t *xpnp = NULL;
161 	int error;
162 	int zdisp;
163 	void *thing = NULL;
164 	size_t thingsize = 0;
165 
166 	/*
167 	 * For copyin()/copyout().
168 	 */
169 	union {
170 		caddr_t		va;
171 		int		signo;
172 		int		nice;
173 		uint_t		lwpid;
174 		long		flags;
175 		prstatus_t	prstat;
176 		prrun_t		prrun;
177 		sigset_t	smask;
178 		siginfo_t	info;
179 		sysset_t	prmask;
180 		prgregset_t	regs;
181 		prfpregset_t	fpregs;
182 		prpsinfo_t	prps;
183 		sigset_t	holdmask;
184 		fltset_t	fltmask;
185 		prcred_t	prcred;
186 		prhusage_t	prhusage;
187 		prmap_t		prmap;
188 		auxv_t		auxv[__KERN_NAUXV_IMPL];
189 	} un;
190 
191 	if (pnp->pr_type == PR_TMPL)
192 		return (prctioctl(pnp, cmd, arg, flag, cr));
193 
194 	/*
195 	 * Support for old /proc interface.
196 	 */
197 	if (pnp->pr_pidfile != NULL) {
198 		ASSERT(pnp->pr_type == PR_PIDDIR);
199 		vp = pnp->pr_pidfile;
200 		pnp = VTOP(vp);
201 		ASSERT(pnp->pr_type == PR_PIDFILE);
202 	}
203 
204 	if (pnp->pr_type != PR_PIDFILE && pnp->pr_type != PR_LWPIDFILE)
205 		return (ENOTTY);
206 
207 	/*
208 	 * Fail ioctls which are logically "write" requests unless
209 	 * the user has write permission.
210 	 */
211 	if ((flag & FWRITE) == 0 && isprwrioctl(cmd))
212 		return (EBADF);
213 
214 	/*
215 	 * Perform any necessary copyin() operations before
216 	 * locking the process.  Helps avoid deadlocks and
217 	 * improves performance.
218 	 *
219 	 * Also, detect invalid ioctl codes here to avoid
220 	 * locking a process unnnecessarily.
221 	 *
222 	 * Also, prepare to allocate space that will be needed below,
223 	 * case by case.
224 	 */
225 	error = 0;
226 	switch (cmd) {
227 	case PIOCGETPR:
228 		thingsize = sizeof (proc_t);
229 		break;
230 	case PIOCGETU:
231 		thingsize = sizeof (user_t);
232 		break;
233 	case PIOCSTOP:
234 	case PIOCWSTOP:
235 	case PIOCLWPIDS:
236 	case PIOCGTRACE:
237 	case PIOCGENTRY:
238 	case PIOCGEXIT:
239 	case PIOCSRLC:
240 	case PIOCRRLC:
241 	case PIOCSFORK:
242 	case PIOCRFORK:
243 	case PIOCGREG:
244 	case PIOCGFPREG:
245 	case PIOCSTATUS:
246 	case PIOCLSTATUS:
247 	case PIOCPSINFO:
248 	case PIOCMAXSIG:
249 	case PIOCGXREGSIZE:
250 		break;
251 	case PIOCSXREG:		/* set extra registers */
252 	case PIOCGXREG:		/* get extra registers */
253 #if defined(__sparc)
254 		thingsize = sizeof (prxregset_t);
255 #else
256 		thingsize = 0;
257 #endif
258 		break;
259 	case PIOCACTION:
260 		thingsize = (NSIG-1) * sizeof (struct sigaction);
261 		break;
262 	case PIOCGHOLD:
263 	case PIOCNMAP:
264 	case PIOCMAP:
265 	case PIOCGFAULT:
266 	case PIOCCFAULT:
267 	case PIOCCRED:
268 	case PIOCGROUPS:
269 	case PIOCUSAGE:
270 	case PIOCLUSAGE:
271 		break;
272 	case PIOCOPENPD:
273 		/*
274 		 * We will need this below.
275 		 * Allocate it now, before locking the process.
276 		 */
277 		xpnp = prgetnode(vp, PR_OPAGEDATA);
278 		break;
279 	case PIOCNAUXV:
280 	case PIOCAUXV:
281 		break;
282 
283 #if defined(__i386) || defined(__amd64)
284 	case PIOCNLDT:
285 	case PIOCLDT:
286 		break;
287 #endif	/* __i386 || __amd64 */
288 
289 #if defined(__sparc)
290 	case PIOCGWIN:
291 		thingsize = sizeof (gwindows_t);
292 		break;
293 #endif	/* __sparc */
294 
295 	case PIOCOPENM:		/* open mapped object for reading */
296 		if (cmaddr == NULL)
297 			un.va = NULL;
298 		else if (copyin(cmaddr, &un.va, sizeof (un.va)))
299 			error = EFAULT;
300 		break;
301 
302 	case PIOCRUN:		/* make lwp or process runnable */
303 		if (cmaddr == NULL)
304 			un.prrun.pr_flags = 0;
305 		else if (copyin(cmaddr, &un.prrun, sizeof (un.prrun)))
306 			error = EFAULT;
307 		break;
308 
309 	case PIOCOPENLWP:	/* return /proc lwp file descriptor */
310 		if (copyin(cmaddr, &un.lwpid, sizeof (un.lwpid)))
311 			error = EFAULT;
312 		break;
313 
314 	case PIOCSTRACE:	/* set signal trace mask */
315 		if (copyin(cmaddr, &un.smask, sizeof (un.smask)))
316 			error = EFAULT;
317 		break;
318 
319 	case PIOCSSIG:		/* set current signal */
320 		if (cmaddr == NULL)
321 			un.info.si_signo = 0;
322 		else if (copyin(cmaddr, &un.info, sizeof (un.info)))
323 			error = EFAULT;
324 		break;
325 
326 	case PIOCKILL:		/* send signal */
327 	case PIOCUNKILL:	/* delete a signal */
328 		if (copyin(cmaddr, &un.signo, sizeof (un.signo)))
329 			error = EFAULT;
330 		break;
331 
332 	case PIOCNICE:		/* set nice priority */
333 		if (copyin(cmaddr, &un.nice, sizeof (un.nice)))
334 			error = EFAULT;
335 		break;
336 
337 	case PIOCSENTRY:	/* set syscall entry bit mask */
338 	case PIOCSEXIT:		/* set syscall exit bit mask */
339 		if (copyin(cmaddr, &un.prmask, sizeof (un.prmask)))
340 			error = EFAULT;
341 		break;
342 
343 	case PIOCSET:		/* set process flags */
344 	case PIOCRESET:		/* reset process flags */
345 		if (copyin(cmaddr, &un.flags, sizeof (un.flags)))
346 			error = EFAULT;
347 		break;
348 
349 	case PIOCSREG:		/* set general registers */
350 		if (copyin(cmaddr, un.regs, sizeof (un.regs)))
351 			error = EFAULT;
352 		break;
353 
354 	case PIOCSFPREG:	/* set floating-point registers */
355 		if (copyin(cmaddr, &un.fpregs, sizeof (un.fpregs)))
356 			error = EFAULT;
357 		break;
358 
359 	case PIOCSHOLD:		/* set signal-hold mask */
360 		if (copyin(cmaddr, &un.holdmask, sizeof (un.holdmask)))
361 			error = EFAULT;
362 		break;
363 
364 	case PIOCSFAULT:	/* set mask of traced faults */
365 		if (copyin(cmaddr, &un.fltmask, sizeof (un.fltmask)))
366 			error = EFAULT;
367 		break;
368 
369 	default:
370 		error = EINVAL;
371 		break;
372 	}
373 
374 	if (error)
375 		return (error);
376 
377 startover:
378 	/*
379 	 * If we need kmem_alloc()d space then we allocate it now, before
380 	 * grabbing the process lock.  Using kmem_alloc(KM_SLEEP) while
381 	 * holding the process lock leads to deadlock with the clock thread.
382 	 * (The clock thread wakes up the pageout daemon to free up space.
383 	 * If the clock thread blocks behind us and we are sleeping waiting
384 	 * for space, then space may never become available.)
385 	 */
386 	if (thingsize) {
387 		ASSERT(thing == NULL);
388 		thing = kmem_alloc(thingsize, KM_SLEEP);
389 	}
390 
391 	switch (cmd) {
392 	case PIOCPSINFO:
393 	case PIOCGETPR:
394 	case PIOCUSAGE:
395 	case PIOCLUSAGE:
396 		zdisp = ZYES;
397 		break;
398 	case PIOCSXREG:		/* set extra registers */
399 		/*
400 		 * perform copyin before grabbing the process lock
401 		 */
402 		if (thing) {
403 			if (copyin(cmaddr, thing, thingsize)) {
404 				kmem_free(thing, thingsize);
405 				return (EFAULT);
406 			}
407 		}
408 		/* fall through... */
409 	default:
410 		zdisp = ZNO;
411 		break;
412 	}
413 
414 	if ((error = prlock(pnp, zdisp)) != 0) {
415 		if (thing != NULL)
416 			kmem_free(thing, thingsize);
417 		if (xpnp)
418 			prfreenode(xpnp);
419 		return (error);
420 	}
421 
422 	pcp = pnp->pr_common;
423 	p = pcp->prc_proc;
424 	ASSERT(p != NULL);
425 
426 	/*
427 	 * Choose a thread/lwp for the operation.
428 	 */
429 	if (zdisp == ZNO && cmd != PIOCSTOP && cmd != PIOCWSTOP) {
430 		if (pnp->pr_type == PR_LWPIDFILE && cmd != PIOCLSTATUS) {
431 			t = pcp->prc_thread;
432 			ASSERT(t != NULL);
433 		} else {
434 			t = prchoose(p);	/* returns locked thread */
435 			ASSERT(t != NULL);
436 			thread_unlock(t);
437 		}
438 		lwp = ttolwp(t);
439 	}
440 
441 	error = 0;
442 	switch (cmd) {
443 
444 	case PIOCGETPR:		/* read struct proc */
445 	{
446 		proc_t *prp = thing;
447 
448 		*prp = *p;
449 		prunlock(pnp);
450 		if (copyout(prp, cmaddr, sizeof (proc_t)))
451 			error = EFAULT;
452 		kmem_free(prp, sizeof (proc_t));
453 		thing = NULL;
454 		break;
455 	}
456 
457 	case PIOCGETU:		/* read u-area */
458 	{
459 		user_t *userp = thing;
460 
461 		up = PTOU(p);
462 		*userp = *up;
463 		prunlock(pnp);
464 		if (copyout(userp, cmaddr, sizeof (user_t)))
465 			error = EFAULT;
466 		kmem_free(userp, sizeof (user_t));
467 		thing = NULL;
468 		break;
469 	}
470 
471 	case PIOCOPENM:		/* open mapped object for reading */
472 		error = propenm(pnp, cmaddr, un.va, rvalp, cr);
473 		/* propenm() called prunlock(pnp) */
474 		break;
475 
476 	case PIOCSTOP:		/* stop process or lwp from running */
477 	case PIOCWSTOP:		/* wait for process or lwp to stop */
478 		/*
479 		 * Can't apply to a system process.
480 		 */
481 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
482 			prunlock(pnp);
483 			error = EBUSY;
484 			break;
485 		}
486 
487 		if (cmd == PIOCSTOP)
488 			pr_stop(pnp);
489 
490 		/*
491 		 * If an lwp is waiting for itself or its process, don't wait.
492 		 * The stopped lwp would never see the fact that it is stopped.
493 		 */
494 		if ((pnp->pr_type == PR_LWPIDFILE)?
495 		    (pcp->prc_thread == curthread) : (p == curproc)) {
496 			if (cmd == PIOCWSTOP)
497 				error = EBUSY;
498 			prunlock(pnp);
499 			break;
500 		}
501 
502 		if ((error = pr_wait_stop(pnp, (time_t)0)) != 0)
503 			break;	/* pr_wait_stop() unlocked the process */
504 
505 		if (cmaddr == NULL)
506 			prunlock(pnp);
507 		else {
508 			/*
509 			 * Return process/lwp status information.
510 			 */
511 			t = pr_thread(pnp);	/* returns locked thread */
512 			thread_unlock(t);
513 			oprgetstatus(t, &un.prstat, VTOZONE(vp));
514 			prunlock(pnp);
515 			if (copyout(&un.prstat, cmaddr, sizeof (un.prstat)))
516 				error = EFAULT;
517 		}
518 		break;
519 
520 	case PIOCRUN:		/* make lwp or process runnable */
521 	{
522 		long flags = un.prrun.pr_flags;
523 
524 		/*
525 		 * Cannot set an lwp running is it is not stopped.
526 		 * Also, no lwp other than the /proc agent lwp can
527 		 * be set running so long as the /proc agent lwp exists.
528 		 */
529 		if ((!ISTOPPED(t) && !VSTOPPED(t) &&
530 		    !(t->t_proc_flag & TP_PRSTOP)) ||
531 		    (p->p_agenttp != NULL &&
532 		    (t != p->p_agenttp || pnp->pr_type != PR_LWPIDFILE))) {
533 			prunlock(pnp);
534 			error = EBUSY;
535 			break;
536 		}
537 
538 		if (flags & (PRSHOLD|PRSTRACE|PRSFAULT|PRSVADDR))
539 			prsetrun(t, &un.prrun);
540 
541 		error = pr_setrun(pnp, prmaprunflags(flags));
542 
543 		prunlock(pnp);
544 		break;
545 	}
546 
547 	case PIOCLWPIDS:	/* get array of lwp identifiers */
548 	{
549 		int nlwp;
550 		int Nlwp;
551 		id_t *idp;
552 		id_t *Bidp;
553 
554 		Nlwp = nlwp = p->p_lwpcnt;
555 
556 		if (thing && thingsize != (Nlwp+1) * sizeof (id_t)) {
557 			kmem_free(thing, thingsize);
558 			thing = NULL;
559 		}
560 		if (thing == NULL) {
561 			thingsize = (Nlwp+1) * sizeof (id_t);
562 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
563 		}
564 		if (thing == NULL) {
565 			prunlock(pnp);
566 			goto startover;
567 		}
568 
569 		idp = thing;
570 		thing = NULL;
571 		Bidp = idp;
572 		if ((t = p->p_tlist) != NULL) {
573 			do {
574 				ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
575 				ASSERT(nlwp > 0);
576 				--nlwp;
577 				*idp++ = t->t_tid;
578 			} while ((t = t->t_forw) != p->p_tlist);
579 		}
580 		*idp = 0;
581 		ASSERT(nlwp == 0);
582 		prunlock(pnp);
583 		if (copyout(Bidp, cmaddr, (Nlwp+1) * sizeof (id_t)))
584 			error = EFAULT;
585 		kmem_free(Bidp, (Nlwp+1) * sizeof (id_t));
586 		break;
587 	}
588 
589 	case PIOCOPENLWP:	/* return /proc lwp file descriptor */
590 	{
591 		vnode_t *xvp;
592 		int n;
593 
594 		prunlock(pnp);
595 		if ((xvp = prlwpnode(pnp, un.lwpid)) == NULL)
596 			error = ENOENT;
597 		else if (error = fassign(&xvp, flag & (FREAD|FWRITE), &n)) {
598 			VN_RELE(xvp);
599 		} else
600 			*rvalp = n;
601 		break;
602 	}
603 
604 	case PIOCOPENPD:	/* return /proc page data file descriptor */
605 	{
606 		vnode_t *xvp = PTOV(xpnp);
607 		vnode_t *dp = pnp->pr_parent;
608 		int n;
609 
610 		if (pnp->pr_type == PR_LWPIDFILE) {
611 			dp = VTOP(dp)->pr_parent;
612 			dp = VTOP(dp)->pr_parent;
613 		}
614 		ASSERT(VTOP(dp)->pr_type == PR_PIDDIR);
615 
616 		VN_HOLD(dp);
617 		pcp = pnp->pr_pcommon;
618 		xpnp->pr_ino = ptoi(pcp->prc_pid);
619 		xpnp->pr_common = pcp;
620 		xpnp->pr_pcommon = pcp;
621 		xpnp->pr_parent = dp;
622 
623 		xpnp->pr_next = p->p_plist;
624 		p->p_plist = xvp;
625 
626 		prunlock(pnp);
627 		if (error = fassign(&xvp, FREAD, &n)) {
628 			VN_RELE(xvp);
629 		} else
630 			*rvalp = n;
631 
632 		xpnp = NULL;
633 		break;
634 	}
635 
636 	case PIOCGTRACE:	/* get signal trace mask */
637 		prassignset(&un.smask, &p->p_sigmask);
638 		prunlock(pnp);
639 		if (copyout(&un.smask, cmaddr, sizeof (un.smask)))
640 			error = EFAULT;
641 		break;
642 
643 	case PIOCSTRACE:	/* set signal trace mask */
644 		prdelset(&un.smask, SIGKILL);
645 		prassignset(&p->p_sigmask, &un.smask);
646 		if (!sigisempty(&p->p_sigmask))
647 			p->p_proc_flag |= P_PR_TRACE;
648 		else if (prisempty(&p->p_fltmask)) {
649 			up = PTOU(p);
650 			if (up->u_systrap == 0)
651 				p->p_proc_flag &= ~P_PR_TRACE;
652 		}
653 		prunlock(pnp);
654 		break;
655 
656 	case PIOCSSIG:		/* set current signal */
657 		error = pr_setsig(pnp, &un.info);
658 		prunlock(pnp);
659 		if (un.info.si_signo == SIGKILL && error == 0)
660 			pr_wait_die(pnp);
661 		break;
662 
663 	case PIOCKILL:		/* send signal */
664 	{
665 		int sig = (int)un.signo;
666 
667 		error = pr_kill(pnp, sig, cr);
668 		prunlock(pnp);
669 		if (sig == SIGKILL && error == 0)
670 			pr_wait_die(pnp);
671 		break;
672 	}
673 
674 	case PIOCUNKILL:	/* delete a signal */
675 		error = pr_unkill(pnp, (int)un.signo);
676 		prunlock(pnp);
677 		break;
678 
679 	case PIOCNICE:		/* set nice priority */
680 		error = pr_nice(p, (int)un.nice, cr);
681 		prunlock(pnp);
682 		break;
683 
684 	case PIOCGENTRY:	/* get syscall entry bit mask */
685 	case PIOCGEXIT:		/* get syscall exit bit mask */
686 		up = PTOU(p);
687 		if (cmd == PIOCGENTRY) {
688 			prassignset(&un.prmask, &up->u_entrymask);
689 		} else {
690 			prassignset(&un.prmask, &up->u_exitmask);
691 		}
692 		prunlock(pnp);
693 		if (copyout(&un.prmask, cmaddr, sizeof (un.prmask)))
694 			error = EFAULT;
695 		break;
696 
697 	case PIOCSENTRY:	/* set syscall entry bit mask */
698 	case PIOCSEXIT:		/* set syscall exit bit mask */
699 		pr_setentryexit(p, &un.prmask, cmd == PIOCSENTRY);
700 		prunlock(pnp);
701 		break;
702 
703 	case PIOCSRLC:		/* obsolete: set running on last /proc close */
704 		error = pr_set(p, prmapsetflags(PR_RLC));
705 		prunlock(pnp);
706 		break;
707 
708 	case PIOCRRLC:		/* obsolete: reset run-on-last-close flag */
709 		error = pr_unset(p, prmapsetflags(PR_RLC));
710 		prunlock(pnp);
711 		break;
712 
713 	case PIOCSFORK:		/* obsolete: set inherit-on-fork flag */
714 		error = pr_set(p, prmapsetflags(PR_FORK));
715 		prunlock(pnp);
716 		break;
717 
718 	case PIOCRFORK:		/* obsolete: reset inherit-on-fork flag */
719 		error = pr_unset(p, prmapsetflags(PR_FORK));
720 		prunlock(pnp);
721 		break;
722 
723 	case PIOCSET:		/* set process flags */
724 		error = pr_set(p, prmapsetflags(un.flags));
725 		prunlock(pnp);
726 		break;
727 
728 	case PIOCRESET:		/* reset process flags */
729 		error = pr_unset(p, prmapsetflags(un.flags));
730 		prunlock(pnp);
731 		break;
732 
733 	case PIOCGREG:		/* get general registers */
734 		if (t->t_state != TS_STOPPED && !VSTOPPED(t))
735 			bzero(un.regs, sizeof (un.regs));
736 		else {
737 			/* drop p_lock while touching the lwp's stack */
738 			mutex_exit(&p->p_lock);
739 			prgetprregs(lwp, un.regs);
740 			mutex_enter(&p->p_lock);
741 		}
742 		prunlock(pnp);
743 		if (copyout(un.regs, cmaddr, sizeof (un.regs)))
744 			error = EFAULT;
745 		break;
746 
747 	case PIOCSREG:		/* set general registers */
748 		if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
749 			error = EBUSY;
750 		else {
751 			/* drop p_lock while touching the lwp's stack */
752 			mutex_exit(&p->p_lock);
753 			prsetprregs(lwp, un.regs, 0);
754 			mutex_enter(&p->p_lock);
755 		}
756 		prunlock(pnp);
757 		break;
758 
759 	case PIOCGFPREG:	/* get floating-point registers */
760 		if (!prhasfp()) {
761 			prunlock(pnp);
762 			error = EINVAL;	/* No FP support */
763 			break;
764 		}
765 
766 		if (t->t_state != TS_STOPPED && !VSTOPPED(t))
767 			bzero(&un.fpregs, sizeof (un.fpregs));
768 		else {
769 			/* drop p_lock while touching the lwp's stack */
770 			mutex_exit(&p->p_lock);
771 			prgetprfpregs(lwp, &un.fpregs);
772 			mutex_enter(&p->p_lock);
773 		}
774 		prunlock(pnp);
775 		if (copyout(&un.fpregs, cmaddr, sizeof (un.fpregs)))
776 			error = EFAULT;
777 		break;
778 
779 	case PIOCSFPREG:	/* set floating-point registers */
780 		if (!prhasfp())
781 			error = EINVAL;	/* No FP support */
782 		else if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
783 			error = EBUSY;
784 		else {
785 			/* drop p_lock while touching the lwp's stack */
786 			mutex_exit(&p->p_lock);
787 			prsetprfpregs(lwp, &un.fpregs);
788 			mutex_enter(&p->p_lock);
789 		}
790 		prunlock(pnp);
791 		break;
792 
793 	case PIOCGXREGSIZE:	/* get the size of the extra registers */
794 	{
795 		int xregsize;
796 
797 		if (prhasx(p)) {
798 			xregsize = prgetprxregsize(p);
799 			prunlock(pnp);
800 			if (copyout(&xregsize, cmaddr, sizeof (xregsize)))
801 				error = EFAULT;
802 		} else {
803 			prunlock(pnp);
804 			error = EINVAL;	/* No extra register support */
805 		}
806 		break;
807 	}
808 
809 	case PIOCGXREG:		/* get extra registers */
810 		if (prhasx(p)) {
811 			bzero(thing, thingsize);
812 			if (t->t_state == TS_STOPPED || VSTOPPED(t)) {
813 				/* drop p_lock to touch the stack */
814 				mutex_exit(&p->p_lock);
815 				prgetprxregs(lwp, thing);
816 				mutex_enter(&p->p_lock);
817 			}
818 			prunlock(pnp);
819 			if (copyout(thing, cmaddr, thingsize))
820 				error = EFAULT;
821 		} else {
822 			prunlock(pnp);
823 			error = EINVAL;	/* No extra register support */
824 		}
825 		if (thing) {
826 			kmem_free(thing, thingsize);
827 			thing = NULL;
828 		}
829 		break;
830 
831 	case PIOCSXREG:		/* set extra registers */
832 		if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
833 			error = EBUSY;
834 		else if (!prhasx(p))
835 			error = EINVAL;	/* No extra register support */
836 		else if (thing) {
837 			/* drop p_lock while touching the lwp's stack */
838 			mutex_exit(&p->p_lock);
839 			prsetprxregs(lwp, thing);
840 			mutex_enter(&p->p_lock);
841 		}
842 		prunlock(pnp);
843 		if (thing) {
844 			kmem_free(thing, thingsize);
845 			thing = NULL;
846 		}
847 		break;
848 
849 	case PIOCSTATUS:	/* get process/lwp status */
850 		oprgetstatus(t, &un.prstat, VTOZONE(vp));
851 		prunlock(pnp);
852 		if (copyout(&un.prstat, cmaddr, sizeof (un.prstat)))
853 			error = EFAULT;
854 		break;
855 
856 	case PIOCLSTATUS:	/* get status for process & all lwps */
857 	{
858 		int Nlwp;
859 		int nlwp;
860 		prstatus_t *Bprsp;
861 		prstatus_t *prsp;
862 
863 		nlwp = Nlwp = p->p_lwpcnt;
864 
865 		if (thing && thingsize != (Nlwp+1) * sizeof (prstatus_t)) {
866 			kmem_free(thing, thingsize);
867 			thing = NULL;
868 		}
869 		if (thing == NULL) {
870 			thingsize = (Nlwp+1) * sizeof (prstatus_t);
871 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
872 		}
873 		if (thing == NULL) {
874 			prunlock(pnp);
875 			goto startover;
876 		}
877 
878 		Bprsp = thing;
879 		thing = NULL;
880 		prsp = Bprsp;
881 		oprgetstatus(t, prsp, VTOZONE(vp));
882 		t = p->p_tlist;
883 		do {
884 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
885 			ASSERT(nlwp > 0);
886 			--nlwp;
887 			oprgetstatus(t, ++prsp, VTOZONE(vp));
888 		} while ((t = t->t_forw) != p->p_tlist);
889 		ASSERT(nlwp == 0);
890 		prunlock(pnp);
891 		if (copyout(Bprsp, cmaddr, (Nlwp+1) * sizeof (prstatus_t)))
892 			error = EFAULT;
893 
894 		kmem_free(Bprsp, (Nlwp+1) * sizeof (prstatus_t));
895 		break;
896 	}
897 
898 	case PIOCPSINFO:	/* get ps(1) information */
899 	{
900 		prpsinfo_t *psp = &un.prps;
901 
902 		oprgetpsinfo(p, psp,
903 		    (pnp->pr_type == PR_LWPIDFILE)? pcp->prc_thread : NULL);
904 
905 		prunlock(pnp);
906 		if (copyout(&un.prps, cmaddr, sizeof (un.prps)))
907 			error = EFAULT;
908 		break;
909 	}
910 
911 	case PIOCMAXSIG:	/* get maximum signal number */
912 	{
913 		int n = NSIG-1;
914 
915 		prunlock(pnp);
916 		if (copyout(&n, cmaddr, sizeof (n)))
917 			error = EFAULT;
918 		break;
919 	}
920 
921 	case PIOCACTION:	/* get signal action structures */
922 	{
923 		uint_t sig;
924 		struct sigaction *sap = thing;
925 
926 		up = PTOU(p);
927 		for (sig = 1; sig < NSIG; sig++)
928 			prgetaction(p, up, sig, &sap[sig-1]);
929 		prunlock(pnp);
930 		if (copyout(sap, cmaddr, (NSIG-1) * sizeof (struct sigaction)))
931 			error = EFAULT;
932 		kmem_free(sap, (NSIG-1) * sizeof (struct sigaction));
933 		thing = NULL;
934 		break;
935 	}
936 
937 	case PIOCGHOLD:		/* get signal-hold mask */
938 		schedctl_finish_sigblock(t);
939 		sigktou(&t->t_hold, &un.holdmask);
940 		prunlock(pnp);
941 		if (copyout(&un.holdmask, cmaddr, sizeof (un.holdmask)))
942 			error = EFAULT;
943 		break;
944 
945 	case PIOCSHOLD:		/* set signal-hold mask */
946 		pr_sethold(pnp, &un.holdmask);
947 		prunlock(pnp);
948 		break;
949 
950 	case PIOCNMAP:		/* get number of memory mappings */
951 	{
952 		int n;
953 		struct as *as = p->p_as;
954 
955 		if ((p->p_flag & SSYS) || as == &kas)
956 			n = 0;
957 		else {
958 			mutex_exit(&p->p_lock);
959 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
960 			n = prnsegs(as, 0);
961 			AS_LOCK_EXIT(as, &as->a_lock);
962 			mutex_enter(&p->p_lock);
963 		}
964 		prunlock(pnp);
965 		if (copyout(&n, cmaddr, sizeof (int)))
966 			error = EFAULT;
967 		break;
968 	}
969 
970 	case PIOCMAP:		/* get memory map information */
971 	{
972 		list_t iolhead;
973 		struct as *as = p->p_as;
974 
975 		if ((p->p_flag & SSYS) || as == &kas) {
976 			error = 0;
977 			prunlock(pnp);
978 		} else {
979 			mutex_exit(&p->p_lock);
980 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
981 			error = oprgetmap(p, &iolhead);
982 			AS_LOCK_EXIT(as, &as->a_lock);
983 			mutex_enter(&p->p_lock);
984 			prunlock(pnp);
985 
986 			error = pr_iol_copyout_and_free(&iolhead,
987 			    &cmaddr, error);
988 		}
989 		/*
990 		 * The procfs PIOCMAP ioctl returns an all-zero buffer
991 		 * to indicate the end of the prmap[] array.
992 		 * Append it to whatever has already been copied out.
993 		 */
994 		bzero(&un.prmap, sizeof (un.prmap));
995 		if (!error && copyout(&un.prmap, cmaddr, sizeof (un.prmap)))
996 			error = EFAULT;
997 
998 		break;
999 	}
1000 
1001 	case PIOCGFAULT:	/* get mask of traced faults */
1002 		prassignset(&un.fltmask, &p->p_fltmask);
1003 		prunlock(pnp);
1004 		if (copyout(&un.fltmask, cmaddr, sizeof (un.fltmask)))
1005 			error = EFAULT;
1006 		break;
1007 
1008 	case PIOCSFAULT:	/* set mask of traced faults */
1009 		pr_setfault(p, &un.fltmask);
1010 		prunlock(pnp);
1011 		break;
1012 
1013 	case PIOCCFAULT:	/* clear current fault */
1014 		lwp->lwp_curflt = 0;
1015 		prunlock(pnp);
1016 		break;
1017 
1018 	case PIOCCRED:		/* get process credentials */
1019 	{
1020 		cred_t *cp;
1021 
1022 		mutex_enter(&p->p_crlock);
1023 		cp = p->p_cred;
1024 		un.prcred.pr_euid = crgetuid(cp);
1025 		un.prcred.pr_ruid = crgetruid(cp);
1026 		un.prcred.pr_suid = crgetsuid(cp);
1027 		un.prcred.pr_egid = crgetgid(cp);
1028 		un.prcred.pr_rgid = crgetrgid(cp);
1029 		un.prcred.pr_sgid = crgetsgid(cp);
1030 		un.prcred.pr_ngroups = crgetngroups(cp);
1031 		mutex_exit(&p->p_crlock);
1032 
1033 		prunlock(pnp);
1034 		if (copyout(&un.prcred, cmaddr, sizeof (un.prcred)))
1035 			error = EFAULT;
1036 		break;
1037 	}
1038 
1039 	case PIOCGROUPS:	/* get supplementary groups */
1040 	{
1041 		cred_t *cp;
1042 
1043 		mutex_enter(&p->p_crlock);
1044 		cp = p->p_cred;
1045 		crhold(cp);
1046 		mutex_exit(&p->p_crlock);
1047 
1048 		prunlock(pnp);
1049 		if (copyout(crgetgroups(cp), cmaddr,
1050 		    MAX(crgetngroups(cp), 1) * sizeof (gid_t)))
1051 			error = EFAULT;
1052 		crfree(cp);
1053 		break;
1054 	}
1055 
1056 	case PIOCUSAGE:		/* get usage info */
1057 	{
1058 		/*
1059 		 * For an lwp file descriptor, return just the lwp usage.
1060 		 * For a process file descriptor, return total usage,
1061 		 * all current lwps plus all defunct lwps.
1062 		 */
1063 		prhusage_t *pup = &un.prhusage;
1064 		prusage_t *upup;
1065 
1066 		bzero(pup, sizeof (*pup));
1067 		pup->pr_tstamp = gethrtime();
1068 
1069 		if (pnp->pr_type == PR_LWPIDFILE) {
1070 			t = pcp->prc_thread;
1071 			if (t != NULL)
1072 				prgetusage(t, pup);
1073 			else
1074 				error = ENOENT;
1075 		} else {
1076 			pup->pr_count  = p->p_defunct;
1077 			pup->pr_create = p->p_mstart;
1078 			pup->pr_term   = p->p_mterm;
1079 
1080 			pup->pr_rtime    = p->p_mlreal;
1081 			pup->pr_utime    = p->p_acct[LMS_USER];
1082 			pup->pr_stime    = p->p_acct[LMS_SYSTEM];
1083 			pup->pr_ttime    = p->p_acct[LMS_TRAP];
1084 			pup->pr_tftime   = p->p_acct[LMS_TFAULT];
1085 			pup->pr_dftime   = p->p_acct[LMS_DFAULT];
1086 			pup->pr_kftime   = p->p_acct[LMS_KFAULT];
1087 			pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
1088 			pup->pr_slptime  = p->p_acct[LMS_SLEEP];
1089 			pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
1090 			pup->pr_stoptime = p->p_acct[LMS_STOPPED];
1091 
1092 			pup->pr_minf  = p->p_ru.minflt;
1093 			pup->pr_majf  = p->p_ru.majflt;
1094 			pup->pr_nswap = p->p_ru.nswap;
1095 			pup->pr_inblk = p->p_ru.inblock;
1096 			pup->pr_oublk = p->p_ru.oublock;
1097 			pup->pr_msnd  = p->p_ru.msgsnd;
1098 			pup->pr_mrcv  = p->p_ru.msgrcv;
1099 			pup->pr_sigs  = p->p_ru.nsignals;
1100 			pup->pr_vctx  = p->p_ru.nvcsw;
1101 			pup->pr_ictx  = p->p_ru.nivcsw;
1102 			pup->pr_sysc  = p->p_ru.sysc;
1103 			pup->pr_ioch  = p->p_ru.ioch;
1104 
1105 			/*
1106 			 * Add the usage information for each active lwp.
1107 			 */
1108 			if ((t = p->p_tlist) != NULL &&
1109 			    !(pcp->prc_flags & PRC_DESTROY)) {
1110 				do {
1111 					ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1112 					pup->pr_count++;
1113 					praddusage(t, pup);
1114 				} while ((t = t->t_forw) != p->p_tlist);
1115 			}
1116 		}
1117 
1118 		prunlock(pnp);
1119 
1120 		upup = kmem_zalloc(sizeof (*upup), KM_SLEEP);
1121 		prcvtusage(&un.prhusage, upup);
1122 		if (copyout(upup, cmaddr, sizeof (*upup)))
1123 			error = EFAULT;
1124 		kmem_free(upup, sizeof (*upup));
1125 
1126 		break;
1127 	}
1128 
1129 	case PIOCLUSAGE:	/* get detailed usage info */
1130 	{
1131 		int Nlwp;
1132 		int nlwp;
1133 		prusage_t *upup;
1134 		prusage_t *Bupup;
1135 		prhusage_t *pup;
1136 		hrtime_t curtime;
1137 
1138 		nlwp = Nlwp = (pcp->prc_flags & PRC_DESTROY)? 0 : p->p_lwpcnt;
1139 
1140 		if (thing && thingsize !=
1141 		    sizeof (prhusage_t) + (Nlwp+1) * sizeof (prusage_t)) {
1142 			kmem_free(thing, thingsize);
1143 			thing = NULL;
1144 		}
1145 		if (thing == NULL) {
1146 			thingsize = sizeof (prhusage_t) +
1147 			    (Nlwp+1) * sizeof (prusage_t);
1148 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
1149 		}
1150 		if (thing == NULL) {
1151 			prunlock(pnp);
1152 			goto startover;
1153 		}
1154 
1155 		pup = thing;
1156 		upup = Bupup = (prusage_t *)(pup + 1);
1157 
1158 		ASSERT(p == pcp->prc_proc);
1159 
1160 		curtime = gethrtime();
1161 
1162 		/*
1163 		 * First the summation over defunct lwps.
1164 		 */
1165 		bzero(pup, sizeof (*pup));
1166 		pup->pr_count  = p->p_defunct;
1167 		pup->pr_tstamp = curtime;
1168 		pup->pr_create = p->p_mstart;
1169 		pup->pr_term   = p->p_mterm;
1170 
1171 		pup->pr_rtime    = p->p_mlreal;
1172 		pup->pr_utime    = p->p_acct[LMS_USER];
1173 		pup->pr_stime    = p->p_acct[LMS_SYSTEM];
1174 		pup->pr_ttime    = p->p_acct[LMS_TRAP];
1175 		pup->pr_tftime   = p->p_acct[LMS_TFAULT];
1176 		pup->pr_dftime   = p->p_acct[LMS_DFAULT];
1177 		pup->pr_kftime   = p->p_acct[LMS_KFAULT];
1178 		pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
1179 		pup->pr_slptime  = p->p_acct[LMS_SLEEP];
1180 		pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
1181 		pup->pr_stoptime = p->p_acct[LMS_STOPPED];
1182 
1183 		pup->pr_minf  = p->p_ru.minflt;
1184 		pup->pr_majf  = p->p_ru.majflt;
1185 		pup->pr_nswap = p->p_ru.nswap;
1186 		pup->pr_inblk = p->p_ru.inblock;
1187 		pup->pr_oublk = p->p_ru.oublock;
1188 		pup->pr_msnd  = p->p_ru.msgsnd;
1189 		pup->pr_mrcv  = p->p_ru.msgrcv;
1190 		pup->pr_sigs  = p->p_ru.nsignals;
1191 		pup->pr_vctx  = p->p_ru.nvcsw;
1192 		pup->pr_ictx  = p->p_ru.nivcsw;
1193 		pup->pr_sysc  = p->p_ru.sysc;
1194 		pup->pr_ioch  = p->p_ru.ioch;
1195 
1196 		prcvtusage(pup, upup);
1197 
1198 		/*
1199 		 * Fill one prusage struct for each active lwp.
1200 		 */
1201 		if ((t = p->p_tlist) != NULL &&
1202 		    !(pcp->prc_flags & PRC_DESTROY)) {
1203 			do {
1204 				ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1205 				ASSERT(nlwp > 0);
1206 				--nlwp;
1207 				upup++;
1208 				prgetusage(t, pup);
1209 				prcvtusage(pup, upup);
1210 			} while ((t = t->t_forw) != p->p_tlist);
1211 		}
1212 		ASSERT(nlwp == 0);
1213 
1214 		prunlock(pnp);
1215 		if (copyout(Bupup, cmaddr, (Nlwp+1) * sizeof (prusage_t)))
1216 			error = EFAULT;
1217 		kmem_free(thing, thingsize);
1218 		thing = NULL;
1219 		break;
1220 	}
1221 
1222 	case PIOCNAUXV:		/* get number of aux vector entries */
1223 	{
1224 		int n = __KERN_NAUXV_IMPL;
1225 
1226 		prunlock(pnp);
1227 		if (copyout(&n, cmaddr, sizeof (int)))
1228 			error = EFAULT;
1229 		break;
1230 	}
1231 
1232 	case PIOCAUXV:		/* get aux vector (see sys/auxv.h) */
1233 	{
1234 		up = PTOU(p);
1235 		bcopy(up->u_auxv, un.auxv,
1236 		    __KERN_NAUXV_IMPL * sizeof (auxv_t));
1237 		prunlock(pnp);
1238 		if (copyout(un.auxv, cmaddr,
1239 		    __KERN_NAUXV_IMPL * sizeof (auxv_t)))
1240 			error = EFAULT;
1241 		break;
1242 	}
1243 
1244 #if defined(__i386) || defined(__amd64)
1245 	case PIOCNLDT:		/* get number of LDT entries */
1246 	{
1247 		int n;
1248 
1249 		mutex_enter(&p->p_ldtlock);
1250 		n = prnldt(p);
1251 		mutex_exit(&p->p_ldtlock);
1252 		prunlock(pnp);
1253 		if (copyout(&n, cmaddr, sizeof (n)))
1254 			error = EFAULT;
1255 		break;
1256 	}
1257 
1258 	case PIOCLDT:		/* get LDT entries */
1259 	{
1260 		struct ssd *ssd;
1261 		int n;
1262 
1263 		mutex_enter(&p->p_ldtlock);
1264 		n = prnldt(p);
1265 
1266 		if (thing && thingsize != (n+1) * sizeof (*ssd)) {
1267 			kmem_free(thing, thingsize);
1268 			thing = NULL;
1269 		}
1270 		if (thing == NULL) {
1271 			thingsize = (n+1) * sizeof (*ssd);
1272 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
1273 		}
1274 		if (thing == NULL) {
1275 			mutex_exit(&p->p_ldtlock);
1276 			prunlock(pnp);
1277 			goto startover;
1278 		}
1279 
1280 		ssd = thing;
1281 		thing = NULL;
1282 		if (n != 0)
1283 			prgetldt(p, ssd);
1284 		mutex_exit(&p->p_ldtlock);
1285 		prunlock(pnp);
1286 
1287 		/* mark the end of the list with a null entry */
1288 		bzero(&ssd[n], sizeof (*ssd));
1289 		if (copyout(ssd, cmaddr, (n+1) * sizeof (*ssd)))
1290 			error = EFAULT;
1291 		kmem_free(ssd, (n+1) * sizeof (*ssd));
1292 		break;
1293 	}
1294 #endif	/* __i386 || __amd64 */
1295 
1296 #if defined(__sparc)
1297 	case PIOCGWIN:		/* get gwindows_t (see sys/reg.h) */
1298 	{
1299 		gwindows_t *gwp = thing;
1300 
1301 		/* drop p->p_lock while touching the stack */
1302 		mutex_exit(&p->p_lock);
1303 		bzero(gwp, sizeof (*gwp));
1304 		prgetwindows(lwp, gwp);
1305 		mutex_enter(&p->p_lock);
1306 		prunlock(pnp);
1307 		if (copyout(gwp, cmaddr, sizeof (*gwp)))
1308 			error = EFAULT;
1309 		kmem_free(gwp, sizeof (gwindows_t));
1310 		thing = NULL;
1311 		break;
1312 	}
1313 #endif	/* __sparc */
1314 
1315 	default:
1316 		prunlock(pnp);
1317 		error = EINVAL;
1318 		break;
1319 
1320 	}
1321 
1322 	ASSERT(thing == NULL);
1323 	ASSERT(xpnp == NULL);
1324 	return (error);
1325 }
1326 
1327 #ifdef _SYSCALL32_IMPL
1328 
1329 static int oprgetmap32(proc_t *, list_t *);
1330 
1331 void
1332 oprgetstatus32(kthread_t *t, prstatus32_t *sp, zone_t *zp)
1333 {
1334 	proc_t *p = ttoproc(t);
1335 	klwp_t *lwp = ttolwp(t);
1336 	int32_t flags;
1337 	user_t *up;
1338 	ulong_t instr;
1339 
1340 	ASSERT(MUTEX_HELD(&p->p_lock));
1341 
1342 	up = PTOU(p);
1343 	bzero(sp, sizeof (*sp));
1344 	flags = 0L;
1345 	if (t->t_state == TS_STOPPED) {
1346 		flags |= PR_STOPPED;
1347 		if ((t->t_schedflag & TS_PSTART) == 0)
1348 			flags |= PR_ISTOP;
1349 	} else if (VSTOPPED(t)) {
1350 		flags |= PR_STOPPED|PR_ISTOP;
1351 	}
1352 	if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP))
1353 		flags |= PR_DSTOP;
1354 	if (lwp->lwp_asleep)
1355 		flags |= PR_ASLEEP;
1356 	if (p->p_proc_flag & P_PR_FORK)
1357 		flags |= PR_FORK;
1358 	if (p->p_proc_flag & P_PR_RUNLCL)
1359 		flags |= PR_RLC;
1360 	if (p->p_proc_flag & P_PR_KILLCL)
1361 		flags |= PR_KLC;
1362 	if (p->p_proc_flag & P_PR_ASYNC)
1363 		flags |= PR_ASYNC;
1364 	if (p->p_proc_flag & P_PR_BPTADJ)
1365 		flags |= PR_BPTADJ;
1366 	if (p->p_proc_flag & P_PR_PTRACE)
1367 		flags |= PR_PCOMPAT;
1368 	if (t->t_proc_flag & TP_MSACCT)
1369 		flags |= PR_MSACCT;
1370 	sp->pr_flags = flags;
1371 	if (VSTOPPED(t)) {
1372 		sp->pr_why   = PR_REQUESTED;
1373 		sp->pr_what  = 0;
1374 	} else {
1375 		sp->pr_why   = t->t_whystop;
1376 		sp->pr_what  = t->t_whatstop;
1377 	}
1378 
1379 	if (t->t_whystop == PR_FAULTED) {
1380 		siginfo_kto32(&lwp->lwp_siginfo, &sp->pr_info);
1381 		if (t->t_whatstop == FLTPAGE)
1382 			sp->pr_info.si_addr =
1383 			    (caddr32_t)(uintptr_t)lwp->lwp_siginfo.si_addr;
1384 	} else if (lwp->lwp_curinfo)
1385 		siginfo_kto32(&lwp->lwp_curinfo->sq_info, &sp->pr_info);
1386 
1387 	if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID &&
1388 	    sp->pr_info.si_zoneid != zp->zone_id) {
1389 		sp->pr_info.si_pid = zp->zone_zsched->p_pid;
1390 		sp->pr_info.si_uid = 0;
1391 		sp->pr_info.si_ctid = -1;
1392 		sp->pr_info.si_zoneid = zp->zone_id;
1393 	}
1394 
1395 	sp->pr_cursig  = lwp->lwp_cursig;
1396 	prassignset(&sp->pr_sigpend, &p->p_sig);
1397 	prassignset(&sp->pr_lwppend, &t->t_sig);
1398 	schedctl_finish_sigblock(t);
1399 	prassignset(&sp->pr_sighold, &t->t_hold);
1400 	sp->pr_altstack.ss_sp =
1401 	    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
1402 	sp->pr_altstack.ss_size = (size32_t)lwp->lwp_sigaltstack.ss_size;
1403 	sp->pr_altstack.ss_flags = (int32_t)lwp->lwp_sigaltstack.ss_flags;
1404 	prgetaction32(p, up, lwp->lwp_cursig, &sp->pr_action);
1405 	sp->pr_pid   = p->p_pid;
1406 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
1407 	    (p->p_flag & SZONETOP)) {
1408 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
1409 		/*
1410 		 * Inside local zones, fake zsched's pid as parent pids for
1411 		 * processes which reference processes outside of the zone.
1412 		 */
1413 		sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
1414 	} else {
1415 		sp->pr_ppid = p->p_ppid;
1416 	}
1417 	sp->pr_pgrp  = p->p_pgrp;
1418 	sp->pr_sid   = p->p_sessp->s_sid;
1419 	hrt2ts32(mstate_aggr_state(p, LMS_USER), &sp->pr_utime);
1420 	hrt2ts32(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime);
1421 	TICK_TO_TIMESTRUC32(p->p_cutime, &sp->pr_cutime);
1422 	TICK_TO_TIMESTRUC32(p->p_cstime, &sp->pr_cstime);
1423 	(void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name,
1424 	    sizeof (sp->pr_clname) - 1);
1425 	sp->pr_who = t->t_tid;
1426 	sp->pr_nlwp = p->p_lwpcnt;
1427 	sp->pr_brkbase = (caddr32_t)(uintptr_t)p->p_brkbase;
1428 	sp->pr_brksize = (size32_t)p->p_brksize;
1429 	sp->pr_stkbase = (caddr32_t)(uintptr_t)prgetstackbase(p);
1430 	sp->pr_stksize = (size32_t)p->p_stksize;
1431 	sp->pr_oldcontext = (caddr32_t)lwp->lwp_oldcontext;
1432 	sp->pr_processor = t->t_cpu->cpu_id;
1433 	sp->pr_bind = t->t_bind_cpu;
1434 
1435 	/*
1436 	 * Fetch the current instruction, if not a system process.
1437 	 * We don't attempt this unless the lwp is stopped.
1438 	 */
1439 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1440 		sp->pr_flags |= (PR_ISSYS|PR_PCINVAL);
1441 	else if (!(flags & PR_STOPPED))
1442 		sp->pr_flags |= PR_PCINVAL;
1443 	else if (!prfetchinstr(lwp, &instr))
1444 		sp->pr_flags |= PR_PCINVAL;
1445 	else
1446 		sp->pr_instr = (uint32_t)instr;
1447 
1448 	/*
1449 	 * Drop p_lock while touching the lwp's stack.
1450 	 */
1451 	mutex_exit(&p->p_lock);
1452 	if (prisstep(lwp))
1453 		sp->pr_flags |= PR_STEP;
1454 	if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) {
1455 		int i;
1456 		auxv_t *auxp;
1457 
1458 		sp->pr_syscall = get_syscall32_args(lwp,
1459 		    (int *)sp->pr_sysarg, &i);
1460 		sp->pr_nsysarg = (short)i;
1461 		if (t->t_whystop == PR_SYSEXIT &&
1462 		    (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve)) {
1463 			sp->pr_sysarg[0] = 0;
1464 			sp->pr_sysarg[1] = (caddr32_t)up->u_argv;
1465 			sp->pr_sysarg[2] = (caddr32_t)up->u_envp;
1466 			for (i = 0, auxp = up->u_auxv;
1467 			    i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]);
1468 			    i++, auxp++) {
1469 				if (auxp->a_type == AT_SUN_EXECNAME) {
1470 					sp->pr_sysarg[0] =
1471 					    (caddr32_t)
1472 					    (uintptr_t)auxp->a_un.a_ptr;
1473 					break;
1474 				}
1475 			}
1476 		}
1477 	}
1478 	if ((flags & PR_STOPPED) || t == curthread)
1479 		prgetprregs32(lwp, sp->pr_reg);
1480 	mutex_enter(&p->p_lock);
1481 }
1482 
1483 void
1484 oprgetpsinfo32(proc_t *p, prpsinfo32_t *psp, kthread_t *tp)
1485 {
1486 	kthread_t *t;
1487 	char c, state;
1488 	user_t *up;
1489 	dev_t d;
1490 	uint64_t pct;
1491 	int retval, niceval;
1492 	cred_t *cred;
1493 	struct as *as;
1494 	hrtime_t hrutime, hrstime, cur_time;
1495 
1496 	ASSERT(MUTEX_HELD(&p->p_lock));
1497 
1498 	bzero(psp, sizeof (*psp));
1499 
1500 	if ((t = tp) == NULL)
1501 		t = prchoose(p);	/* returns locked thread */
1502 	else
1503 		thread_lock(t);
1504 
1505 	/* kludge: map thread state enum into process state enum */
1506 
1507 	if (t == NULL) {
1508 		state = TS_ZOMB;
1509 	} else {
1510 		state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
1511 		thread_unlock(t);
1512 	}
1513 
1514 	switch (state) {
1515 	case TS_SLEEP:		state = SSLEEP;		break;
1516 	case TS_RUN:		state = SRUN;		break;
1517 	case TS_ONPROC:		state = SONPROC;	break;
1518 	case TS_ZOMB:		state = SZOMB;		break;
1519 	case TS_STOPPED:	state = SSTOP;		break;
1520 	default:		state = 0;		break;
1521 	}
1522 	switch (state) {
1523 	case SSLEEP:	c = 'S';	break;
1524 	case SRUN:	c = 'R';	break;
1525 	case SZOMB:	c = 'Z';	break;
1526 	case SSTOP:	c = 'T';	break;
1527 	case SIDL:	c = 'I';	break;
1528 	case SONPROC:	c = 'O';	break;
1529 #ifdef SXBRK
1530 	case SXBRK:	c = 'X';	break;
1531 #endif
1532 	default:	c = '?';	break;
1533 	}
1534 	psp->pr_state = state;
1535 	psp->pr_sname = c;
1536 	psp->pr_zomb = (state == SZOMB);
1537 	/*
1538 	 * only export SSYS and SMSACCT; everything else is off-limits to
1539 	 * userland apps.
1540 	 */
1541 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
1542 
1543 	mutex_enter(&p->p_crlock);
1544 	cred = p->p_cred;
1545 	psp->pr_uid = crgetruid(cred);
1546 	psp->pr_gid = crgetrgid(cred);
1547 	psp->pr_euid = crgetuid(cred);
1548 	psp->pr_egid = crgetgid(cred);
1549 	mutex_exit(&p->p_crlock);
1550 
1551 	psp->pr_pid = p->p_pid;
1552 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
1553 	    (p->p_flag & SZONETOP)) {
1554 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
1555 		/*
1556 		 * Inside local zones, fake zsched's pid as parent pids for
1557 		 * processes which reference processes outside of the zone.
1558 		 */
1559 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
1560 	} else {
1561 		psp->pr_ppid = p->p_ppid;
1562 	}
1563 	psp->pr_pgrp = p->p_pgrp;
1564 	psp->pr_sid = p->p_sessp->s_sid;
1565 	psp->pr_addr = 0;	/* cannot represent 64-bit addr in 32 bits */
1566 	hrutime = mstate_aggr_state(p, LMS_USER);
1567 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
1568 	hrt2ts32(hrutime + hrstime, &psp->pr_time);
1569 	TICK_TO_TIMESTRUC32(p->p_cutime + p->p_cstime, &psp->pr_ctime);
1570 	switch (p->p_model) {
1571 	case DATAMODEL_ILP32:
1572 		psp->pr_dmodel = PR_MODEL_ILP32;
1573 		break;
1574 	case DATAMODEL_LP64:
1575 		psp->pr_dmodel = PR_MODEL_LP64;
1576 		break;
1577 	}
1578 	if (state == SZOMB || t == NULL) {
1579 		int wcode = p->p_wcode;		/* must be atomic read */
1580 
1581 		if (wcode)
1582 			psp->pr_wstat = wstat(wcode, p->p_wdata);
1583 		psp->pr_lttydev = PRNODEV32;
1584 		psp->pr_ottydev = (o_dev_t)PRNODEV32;
1585 		psp->pr_size = 0;
1586 		psp->pr_rssize = 0;
1587 		psp->pr_pctmem = 0;
1588 	} else {
1589 		up = PTOU(p);
1590 		psp->pr_wchan = 0;	/* cannot represent in 32 bits */
1591 		psp->pr_pri = t->t_pri;
1592 		(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
1593 		    sizeof (psp->pr_clname) - 1);
1594 		retval = CL_DONICE(t, NULL, 0, &niceval);
1595 		if (retval == 0) {
1596 			psp->pr_oldpri = v.v_maxsyspri - psp->pr_pri;
1597 			psp->pr_nice = niceval + NZERO;
1598 		} else {
1599 			psp->pr_oldpri = 0;
1600 			psp->pr_nice = 0;
1601 		}
1602 		d = cttydev(p);
1603 #ifdef sun
1604 		{
1605 			extern dev_t rwsconsdev, rconsdev, uconsdev;
1606 			/*
1607 			 * If the controlling terminal is the real
1608 			 * or workstation console device, map to what the
1609 			 * user thinks is the console device. Handle case when
1610 			 * rwsconsdev or rconsdev is set to NODEV for Starfire.
1611 			 */
1612 			if ((d == rwsconsdev || d == rconsdev) && d != NODEV)
1613 				d = uconsdev;
1614 		}
1615 #endif
1616 		(void) cmpldev(&psp->pr_lttydev, d);
1617 		psp->pr_ottydev = cmpdev(d);
1618 		TIMESPEC_TO_TIMESPEC32(&psp->pr_start, &up->u_start);
1619 		bcopy(up->u_comm, psp->pr_fname,
1620 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
1621 		bcopy(up->u_psargs, psp->pr_psargs,
1622 		    MIN(PRARGSZ-1, PSARGSZ));
1623 		psp->pr_syscall = t->t_sysnum;
1624 		psp->pr_argc = up->u_argc;
1625 		psp->pr_argv = (caddr32_t)up->u_argv;
1626 		psp->pr_envp = (caddr32_t)up->u_envp;
1627 
1628 		/* compute %cpu for the lwp or process */
1629 		pct = 0;
1630 		if ((t = tp) == NULL)
1631 			t = p->p_tlist;
1632 		cur_time = gethrtime_unscaled();
1633 		do {
1634 			pct += cpu_update_pct(t, cur_time);
1635 			if (tp != NULL)		/* just do the one lwp */
1636 				break;
1637 		} while ((t = t->t_forw) != p->p_tlist);
1638 
1639 		psp->pr_pctcpu = prgetpctcpu(pct);
1640 		psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15; /* [0..99] */
1641 		if (psp->pr_cpu > 99)
1642 			psp->pr_cpu = 99;
1643 
1644 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
1645 			psp->pr_size = 0;
1646 			psp->pr_rssize = 0;
1647 			psp->pr_pctmem = 0;
1648 		} else {
1649 			mutex_exit(&p->p_lock);
1650 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
1651 			psp->pr_size = (size32_t)btopr(rm_assize(as));
1652 			psp->pr_rssize = (size32_t)rm_asrss(as);
1653 			psp->pr_pctmem = rm_pctmemory(as);
1654 			AS_LOCK_EXIT(as, &as->a_lock);
1655 			mutex_enter(&p->p_lock);
1656 		}
1657 	}
1658 	psp->pr_bysize = (size32_t)ptob(psp->pr_size);
1659 	psp->pr_byrssize = (size32_t)ptob(psp->pr_rssize);
1660 
1661 	/*
1662 	 * If we are looking at an LP64 process, zero out
1663 	 * the fields that cannot be represented in ILP32.
1664 	 */
1665 	if (p->p_model != DATAMODEL_ILP32) {
1666 		psp->pr_size = 0;
1667 		psp->pr_rssize = 0;
1668 		psp->pr_bysize = 0;
1669 		psp->pr_byrssize = 0;
1670 		psp->pr_argv = 0;
1671 		psp->pr_envp = 0;
1672 	}
1673 }
1674 
1675 /*ARGSUSED*/
1676 static int
1677 prioctl32(
1678 	struct vnode *vp,
1679 	int cmd,
1680 	intptr_t arg,
1681 	int flag,
1682 	cred_t *cr,
1683 	int *rvalp,
1684 	caller_context_t *ct)
1685 {
1686 	caddr_t cmaddr = (caddr_t)arg;
1687 	proc_t *p;
1688 	user_t *up;
1689 	kthread_t *t;
1690 	klwp_t *lwp;
1691 	prnode_t *pnp = VTOP(vp);
1692 	prcommon_t *pcp;
1693 	prnode_t *xpnp = NULL;
1694 	int error;
1695 	int zdisp;
1696 	void *thing = NULL;
1697 	size_t thingsize = 0;
1698 
1699 	/*
1700 	 * For copyin()/copyout().
1701 	 */
1702 	union {
1703 		caddr32_t	va;
1704 		int		signo;
1705 		int		nice;
1706 		uint_t		lwpid;
1707 		int32_t		flags;
1708 		prstatus32_t	prstat;
1709 		prrun32_t	prrun;
1710 		sigset_t	smask;
1711 		siginfo32_t	info;
1712 		sysset_t	prmask;
1713 		prgregset32_t	regs;
1714 		prfpregset32_t	fpregs;
1715 		prpsinfo32_t	prps;
1716 		sigset_t	holdmask;
1717 		fltset_t	fltmask;
1718 		prcred_t	prcred;
1719 		prusage32_t	prusage;
1720 		prhusage_t	prhusage;
1721 		ioc_prmap32_t	prmap;
1722 		auxv32_t	auxv[__KERN_NAUXV_IMPL];
1723 	} un32;
1724 
1725 	/*
1726 	 * Native objects for internal use.
1727 	 */
1728 	union {
1729 		caddr_t		va;
1730 		int		signo;
1731 		int		nice;
1732 		uint_t		lwpid;
1733 		long		flags;
1734 		prstatus_t	prstat;
1735 		prrun_t		prrun;
1736 		sigset_t	smask;
1737 		siginfo_t	info;
1738 		sysset_t	prmask;
1739 		prgregset_t	regs;
1740 		prpsinfo_t	prps;
1741 		sigset_t	holdmask;
1742 		fltset_t	fltmask;
1743 		prcred_t	prcred;
1744 		prusage_t	prusage;
1745 		prhusage_t	prhusage;
1746 		auxv_t		auxv[__KERN_NAUXV_IMPL];
1747 	} un;
1748 
1749 	if (pnp->pr_type == PR_TMPL)
1750 		return (prctioctl(pnp, cmd, arg, flag, cr));
1751 
1752 	/*
1753 	 * Support for old /proc interface.
1754 	 */
1755 	if (pnp->pr_pidfile != NULL) {
1756 		ASSERT(pnp->pr_type == PR_PIDDIR);
1757 		vp = pnp->pr_pidfile;
1758 		pnp = VTOP(vp);
1759 		ASSERT(pnp->pr_type == PR_PIDFILE);
1760 	}
1761 
1762 	if (pnp->pr_type != PR_PIDFILE && pnp->pr_type != PR_LWPIDFILE)
1763 		return (ENOTTY);
1764 
1765 	/*
1766 	 * Fail ioctls which are logically "write" requests unless
1767 	 * the user has write permission.
1768 	 */
1769 	if ((flag & FWRITE) == 0 && isprwrioctl(cmd))
1770 		return (EBADF);
1771 
1772 	/*
1773 	 * Perform any necessary copyin() operations before
1774 	 * locking the process.  Helps avoid deadlocks and
1775 	 * improves performance.
1776 	 *
1777 	 * Also, detect invalid ioctl codes here to avoid
1778 	 * locking a process unnnecessarily.
1779 	 *
1780 	 * Also, prepare to allocate space that will be needed below,
1781 	 * case by case.
1782 	 */
1783 	error = 0;
1784 	switch (cmd) {
1785 	case PIOCGETPR:
1786 		thingsize = sizeof (proc_t);
1787 		break;
1788 	case PIOCGETU:
1789 		thingsize = sizeof (user_t);
1790 		break;
1791 	case PIOCSTOP:
1792 	case PIOCWSTOP:
1793 	case PIOCLWPIDS:
1794 	case PIOCGTRACE:
1795 	case PIOCGENTRY:
1796 	case PIOCGEXIT:
1797 	case PIOCSRLC:
1798 	case PIOCRRLC:
1799 	case PIOCSFORK:
1800 	case PIOCRFORK:
1801 	case PIOCGREG:
1802 	case PIOCGFPREG:
1803 	case PIOCSTATUS:
1804 	case PIOCLSTATUS:
1805 	case PIOCPSINFO:
1806 	case PIOCMAXSIG:
1807 	case PIOCGXREGSIZE:
1808 		break;
1809 	case PIOCSXREG:		/* set extra registers */
1810 	case PIOCGXREG:		/* get extra registers */
1811 #if defined(__sparc)
1812 		thingsize = sizeof (prxregset_t);
1813 #else
1814 		thingsize = 0;
1815 #endif
1816 		break;
1817 	case PIOCACTION:
1818 		thingsize = (NSIG-1) * sizeof (struct sigaction32);
1819 		break;
1820 	case PIOCGHOLD:
1821 	case PIOCNMAP:
1822 	case PIOCMAP:
1823 	case PIOCGFAULT:
1824 	case PIOCCFAULT:
1825 	case PIOCCRED:
1826 	case PIOCGROUPS:
1827 	case PIOCUSAGE:
1828 	case PIOCLUSAGE:
1829 		break;
1830 	case PIOCOPENPD:
1831 		/*
1832 		 * We will need this below.
1833 		 * Allocate it now, before locking the process.
1834 		 */
1835 		xpnp = prgetnode(vp, PR_OPAGEDATA);
1836 		break;
1837 	case PIOCNAUXV:
1838 	case PIOCAUXV:
1839 		break;
1840 
1841 #if defined(__i386) || defined(__i386_COMPAT)
1842 	case PIOCNLDT:
1843 	case PIOCLDT:
1844 		break;
1845 #endif	/* __i386 || __i386_COMPAT */
1846 
1847 #if defined(__sparc)
1848 	case PIOCGWIN:
1849 		thingsize = sizeof (gwindows32_t);
1850 		break;
1851 #endif	/* __sparc */
1852 
1853 	case PIOCOPENM:		/* open mapped object for reading */
1854 		if (cmaddr == NULL)
1855 			un32.va = NULL;
1856 		else if (copyin(cmaddr, &un32.va, sizeof (un32.va)))
1857 			error = EFAULT;
1858 		break;
1859 
1860 	case PIOCRUN:		/* make lwp or process runnable */
1861 		if (cmaddr == NULL)
1862 			un32.prrun.pr_flags = 0;
1863 		else if (copyin(cmaddr, &un32.prrun, sizeof (un32.prrun)))
1864 			error = EFAULT;
1865 		break;
1866 
1867 	case PIOCOPENLWP:	/* return /proc lwp file descriptor */
1868 		if (copyin(cmaddr, &un32.lwpid, sizeof (un32.lwpid)))
1869 			error = EFAULT;
1870 		break;
1871 
1872 	case PIOCSTRACE:	/* set signal trace mask */
1873 		if (copyin(cmaddr, &un32.smask, sizeof (un32.smask)))
1874 			error = EFAULT;
1875 		break;
1876 
1877 	case PIOCSSIG:		/* set current signal */
1878 		if (cmaddr == NULL)
1879 			un32.info.si_signo = 0;
1880 		else if (copyin(cmaddr, &un32.info, sizeof (un32.info)))
1881 			error = EFAULT;
1882 		break;
1883 
1884 	case PIOCKILL:		/* send signal */
1885 	case PIOCUNKILL:	/* delete a signal */
1886 		if (copyin(cmaddr, &un32.signo, sizeof (un32.signo)))
1887 			error = EFAULT;
1888 		break;
1889 
1890 	case PIOCNICE:		/* set nice priority */
1891 		if (copyin(cmaddr, &un32.nice, sizeof (un32.nice)))
1892 			error = EFAULT;
1893 		break;
1894 
1895 	case PIOCSENTRY:	/* set syscall entry bit mask */
1896 	case PIOCSEXIT:		/* set syscall exit bit mask */
1897 		if (copyin(cmaddr, &un32.prmask, sizeof (un32.prmask)))
1898 			error = EFAULT;
1899 		break;
1900 
1901 	case PIOCSET:		/* set process flags */
1902 	case PIOCRESET:		/* reset process flags */
1903 		if (copyin(cmaddr, &un32.flags, sizeof (un32.flags)))
1904 			error = EFAULT;
1905 		break;
1906 
1907 	case PIOCSREG:		/* set general registers */
1908 		if (copyin(cmaddr, un32.regs, sizeof (un32.regs)))
1909 			error = EFAULT;
1910 		break;
1911 
1912 	case PIOCSFPREG:	/* set floating-point registers */
1913 		if (copyin(cmaddr, &un32.fpregs, sizeof (un32.fpregs)))
1914 			error = EFAULT;
1915 		break;
1916 
1917 	case PIOCSHOLD:		/* set signal-hold mask */
1918 		if (copyin(cmaddr, &un32.holdmask, sizeof (un32.holdmask)))
1919 			error = EFAULT;
1920 		break;
1921 
1922 	case PIOCSFAULT:	/* set mask of traced faults */
1923 		if (copyin(cmaddr, &un32.fltmask, sizeof (un32.fltmask)))
1924 			error = EFAULT;
1925 		break;
1926 
1927 	default:
1928 		error = EINVAL;
1929 		break;
1930 	}
1931 
1932 	if (error)
1933 		return (error);
1934 
1935 startover:
1936 	/*
1937 	 * If we need kmem_alloc()d space then we allocate it now, before
1938 	 * grabbing the process lock.  Using kmem_alloc(KM_SLEEP) while
1939 	 * holding the process lock leads to deadlock with the clock thread.
1940 	 * (The clock thread wakes up the pageout daemon to free up space.
1941 	 * If the clock thread blocks behind us and we are sleeping waiting
1942 	 * for space, then space may never become available.)
1943 	 */
1944 	if (thingsize) {
1945 		ASSERT(thing == NULL);
1946 		thing = kmem_alloc(thingsize, KM_SLEEP);
1947 	}
1948 
1949 	switch (cmd) {
1950 	case PIOCPSINFO:
1951 	case PIOCGETPR:
1952 	case PIOCUSAGE:
1953 	case PIOCLUSAGE:
1954 		zdisp = ZYES;
1955 		break;
1956 	case PIOCSXREG:		/* set extra registers */
1957 		/*
1958 		 * perform copyin before grabbing the process lock
1959 		 */
1960 		if (thing) {
1961 			if (copyin(cmaddr, thing, thingsize)) {
1962 				kmem_free(thing, thingsize);
1963 				return (EFAULT);
1964 			}
1965 		}
1966 		/* fall through... */
1967 	default:
1968 		zdisp = ZNO;
1969 		break;
1970 	}
1971 
1972 	if ((error = prlock(pnp, zdisp)) != 0) {
1973 		if (thing != NULL)
1974 			kmem_free(thing, thingsize);
1975 		if (xpnp)
1976 			prfreenode(xpnp);
1977 		return (error);
1978 	}
1979 
1980 	pcp = pnp->pr_common;
1981 	p = pcp->prc_proc;
1982 	ASSERT(p != NULL);
1983 
1984 	/*
1985 	 * Choose a thread/lwp for the operation.
1986 	 */
1987 	if (zdisp == ZNO && cmd != PIOCSTOP && cmd != PIOCWSTOP) {
1988 		if (pnp->pr_type == PR_LWPIDFILE && cmd != PIOCLSTATUS) {
1989 			t = pcp->prc_thread;
1990 			ASSERT(t != NULL);
1991 		} else {
1992 			t = prchoose(p);	/* returns locked thread */
1993 			ASSERT(t != NULL);
1994 			thread_unlock(t);
1995 		}
1996 		lwp = ttolwp(t);
1997 	}
1998 
1999 	error = 0;
2000 	switch (cmd) {
2001 
2002 	case PIOCGETPR:		/* read struct proc */
2003 	{
2004 		proc_t *prp = thing;
2005 
2006 		*prp = *p;
2007 		prunlock(pnp);
2008 		if (copyout(prp, cmaddr, sizeof (proc_t)))
2009 			error = EFAULT;
2010 		kmem_free(prp, sizeof (proc_t));
2011 		thing = NULL;
2012 		break;
2013 	}
2014 
2015 	case PIOCGETU:		/* read u-area */
2016 	{
2017 		user_t *userp = thing;
2018 
2019 		up = PTOU(p);
2020 		*userp = *up;
2021 		prunlock(pnp);
2022 		if (copyout(userp, cmaddr, sizeof (user_t)))
2023 			error = EFAULT;
2024 		kmem_free(userp, sizeof (user_t));
2025 		thing = NULL;
2026 		break;
2027 	}
2028 
2029 	case PIOCOPENM:		/* open mapped object for reading */
2030 		if (PROCESS_NOT_32BIT(p) && cmaddr != NULL) {
2031 			prunlock(pnp);
2032 			error = EOVERFLOW;
2033 			break;
2034 		}
2035 		error = propenm(pnp, cmaddr,
2036 		    (caddr_t)(uintptr_t)un32.va, rvalp, cr);
2037 		/* propenm() called prunlock(pnp) */
2038 		break;
2039 
2040 	case PIOCSTOP:		/* stop process or lwp from running */
2041 	case PIOCWSTOP:		/* wait for process or lwp to stop */
2042 		/*
2043 		 * Can't apply to a system process.
2044 		 */
2045 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
2046 			prunlock(pnp);
2047 			error = EBUSY;
2048 			break;
2049 		}
2050 
2051 		if (cmd == PIOCSTOP)
2052 			pr_stop(pnp);
2053 
2054 		/*
2055 		 * If an lwp is waiting for itself or its process, don't wait.
2056 		 * The lwp will never see the fact that itself is stopped.
2057 		 */
2058 		if ((pnp->pr_type == PR_LWPIDFILE)?
2059 		    (pcp->prc_thread == curthread) : (p == curproc)) {
2060 			if (cmd == PIOCWSTOP)
2061 				error = EBUSY;
2062 			prunlock(pnp);
2063 			break;
2064 		}
2065 
2066 		if ((error = pr_wait_stop(pnp, (time_t)0)) != 0)
2067 			break;	/* pr_wait_stop() unlocked the process */
2068 
2069 		if (cmaddr == NULL)
2070 			prunlock(pnp);
2071 		else if (PROCESS_NOT_32BIT(p)) {
2072 			prunlock(pnp);
2073 			error = EOVERFLOW;
2074 		} else {
2075 			/*
2076 			 * Return process/lwp status information.
2077 			 */
2078 			t = pr_thread(pnp);	/* returns locked thread */
2079 			thread_unlock(t);
2080 			oprgetstatus32(t, &un32.prstat, VTOZONE(vp));
2081 			prunlock(pnp);
2082 			if (copyout(&un32.prstat, cmaddr, sizeof (un32.prstat)))
2083 				error = EFAULT;
2084 		}
2085 		break;
2086 
2087 	case PIOCRUN:		/* make lwp or process runnable */
2088 	{
2089 		long flags = un32.prrun.pr_flags;
2090 
2091 		/*
2092 		 * Cannot set an lwp running is it is not stopped.
2093 		 * Also, no lwp other than the /proc agent lwp can
2094 		 * be set running so long as the /proc agent lwp exists.
2095 		 */
2096 		if ((!ISTOPPED(t) && !VSTOPPED(t) &&
2097 		    !(t->t_proc_flag & TP_PRSTOP)) ||
2098 		    (p->p_agenttp != NULL &&
2099 		    (t != p->p_agenttp || pnp->pr_type != PR_LWPIDFILE))) {
2100 			prunlock(pnp);
2101 			error = EBUSY;
2102 			break;
2103 		}
2104 
2105 		if ((flags & PRSVADDR) && PROCESS_NOT_32BIT(p)) {
2106 			prunlock(pnp);
2107 			error = EOVERFLOW;
2108 			break;
2109 		}
2110 
2111 		if (flags & (PRSHOLD|PRSTRACE|PRSFAULT|PRSVADDR)) {
2112 			un.prrun.pr_flags = (int)flags;
2113 			un.prrun.pr_trace = un32.prrun.pr_trace;
2114 			un.prrun.pr_sighold = un32.prrun.pr_sighold;
2115 			un.prrun.pr_fault = un32.prrun.pr_fault;
2116 			un.prrun.pr_vaddr =
2117 			    (caddr_t)(uintptr_t)un32.prrun.pr_vaddr;
2118 			prsetrun(t, &un.prrun);
2119 		}
2120 
2121 		error = pr_setrun(pnp, prmaprunflags(flags));
2122 
2123 		prunlock(pnp);
2124 		break;
2125 	}
2126 
2127 	case PIOCLWPIDS:	/* get array of lwp identifiers */
2128 	{
2129 		int nlwp;
2130 		int Nlwp;
2131 		id_t *idp;
2132 		id_t *Bidp;
2133 
2134 		Nlwp = nlwp = p->p_lwpcnt;
2135 
2136 		if (thing && thingsize != (Nlwp+1) * sizeof (id_t)) {
2137 			kmem_free(thing, thingsize);
2138 			thing = NULL;
2139 		}
2140 		if (thing == NULL) {
2141 			thingsize = (Nlwp+1) * sizeof (id_t);
2142 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
2143 		}
2144 		if (thing == NULL) {
2145 			prunlock(pnp);
2146 			goto startover;
2147 		}
2148 
2149 		idp = thing;
2150 		thing = NULL;
2151 		Bidp = idp;
2152 		if ((t = p->p_tlist) != NULL) {
2153 			do {
2154 				ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
2155 				ASSERT(nlwp > 0);
2156 				--nlwp;
2157 				*idp++ = t->t_tid;
2158 			} while ((t = t->t_forw) != p->p_tlist);
2159 		}
2160 		*idp = 0;
2161 		ASSERT(nlwp == 0);
2162 		prunlock(pnp);
2163 		if (copyout(Bidp, cmaddr, (Nlwp+1) * sizeof (id_t)))
2164 			error = EFAULT;
2165 		kmem_free(Bidp, (Nlwp+1) * sizeof (id_t));
2166 		break;
2167 	}
2168 
2169 	case PIOCOPENLWP:	/* return /proc lwp file descriptor */
2170 	{
2171 		vnode_t *xvp;
2172 		int n;
2173 
2174 		prunlock(pnp);
2175 		if ((xvp = prlwpnode(pnp, un32.lwpid)) == NULL)
2176 			error = ENOENT;
2177 		else if (error = fassign(&xvp, flag & (FREAD|FWRITE), &n)) {
2178 			VN_RELE(xvp);
2179 		} else
2180 			*rvalp = n;
2181 		break;
2182 	}
2183 
2184 	case PIOCOPENPD:	/* return /proc page data file descriptor */
2185 	{
2186 		vnode_t *xvp = PTOV(xpnp);
2187 		vnode_t *dp = pnp->pr_parent;
2188 		int n;
2189 
2190 		if (PROCESS_NOT_32BIT(p)) {
2191 			prunlock(pnp);
2192 			prfreenode(xpnp);
2193 			xpnp = NULL;
2194 			error = EOVERFLOW;
2195 			break;
2196 		}
2197 
2198 		if (pnp->pr_type == PR_LWPIDFILE) {
2199 			dp = VTOP(dp)->pr_parent;
2200 			dp = VTOP(dp)->pr_parent;
2201 		}
2202 		ASSERT(VTOP(dp)->pr_type == PR_PIDDIR);
2203 
2204 		VN_HOLD(dp);
2205 		pcp = pnp->pr_pcommon;
2206 		xpnp->pr_ino = ptoi(pcp->prc_pid);
2207 		xpnp->pr_common = pcp;
2208 		xpnp->pr_pcommon = pcp;
2209 		xpnp->pr_parent = dp;
2210 
2211 		xpnp->pr_next = p->p_plist;
2212 		p->p_plist = xvp;
2213 
2214 		prunlock(pnp);
2215 		if (error = fassign(&xvp, FREAD, &n)) {
2216 			VN_RELE(xvp);
2217 		} else
2218 			*rvalp = n;
2219 
2220 		xpnp = NULL;
2221 		break;
2222 	}
2223 
2224 	case PIOCGTRACE:	/* get signal trace mask */
2225 		prassignset(&un32.smask, &p->p_sigmask);
2226 		prunlock(pnp);
2227 		if (copyout(&un32.smask, cmaddr, sizeof (un32.smask)))
2228 			error = EFAULT;
2229 		break;
2230 
2231 	case PIOCSTRACE:	/* set signal trace mask */
2232 		prdelset(&un32.smask, SIGKILL);
2233 		prassignset(&p->p_sigmask, &un32.smask);
2234 		if (!sigisempty(&p->p_sigmask))
2235 			p->p_proc_flag |= P_PR_TRACE;
2236 		else if (prisempty(&p->p_fltmask)) {
2237 			up = PTOU(p);
2238 			if (up->u_systrap == 0)
2239 				p->p_proc_flag &= ~P_PR_TRACE;
2240 		}
2241 		prunlock(pnp);
2242 		break;
2243 
2244 	case PIOCSSIG:		/* set current signal */
2245 		if (un32.info.si_signo != 0 && PROCESS_NOT_32BIT(p)) {
2246 			prunlock(pnp);
2247 			error = EOVERFLOW;
2248 		} else {
2249 			bzero(&un.info, sizeof (un.info));
2250 			siginfo_32tok(&un32.info, (k_siginfo_t *)&un.info);
2251 			error = pr_setsig(pnp, &un.info);
2252 			prunlock(pnp);
2253 			if (un32.info.si_signo == SIGKILL && error == 0)
2254 				pr_wait_die(pnp);
2255 		}
2256 		break;
2257 
2258 	case PIOCKILL:		/* send signal */
2259 		error = pr_kill(pnp, un32.signo, cr);
2260 		prunlock(pnp);
2261 		if (un32.signo == SIGKILL && error == 0)
2262 			pr_wait_die(pnp);
2263 		break;
2264 
2265 	case PIOCUNKILL:	/* delete a signal */
2266 		error = pr_unkill(pnp, un32.signo);
2267 		prunlock(pnp);
2268 		break;
2269 
2270 	case PIOCNICE:		/* set nice priority */
2271 		error = pr_nice(p, un32.nice, cr);
2272 		prunlock(pnp);
2273 		break;
2274 
2275 	case PIOCGENTRY:	/* get syscall entry bit mask */
2276 	case PIOCGEXIT:		/* get syscall exit bit mask */
2277 		up = PTOU(p);
2278 		if (cmd == PIOCGENTRY) {
2279 			prassignset(&un32.prmask, &up->u_entrymask);
2280 		} else {
2281 			prassignset(&un32.prmask, &up->u_exitmask);
2282 		}
2283 		prunlock(pnp);
2284 		if (copyout(&un32.prmask, cmaddr, sizeof (un32.prmask)))
2285 			error = EFAULT;
2286 		break;
2287 
2288 	case PIOCSENTRY:	/* set syscall entry bit mask */
2289 	case PIOCSEXIT:		/* set syscall exit bit mask */
2290 		pr_setentryexit(p, &un32.prmask, cmd == PIOCSENTRY);
2291 		prunlock(pnp);
2292 		break;
2293 
2294 	case PIOCSRLC:		/* obsolete: set running on last /proc close */
2295 		error = pr_set(p, prmapsetflags(PR_RLC));
2296 		prunlock(pnp);
2297 		break;
2298 
2299 	case PIOCRRLC:		/* obsolete: reset run-on-last-close flag */
2300 		error = pr_unset(p, prmapsetflags(PR_RLC));
2301 		prunlock(pnp);
2302 		break;
2303 
2304 	case PIOCSFORK:		/* obsolete: set inherit-on-fork flag */
2305 		error = pr_set(p, prmapsetflags(PR_FORK));
2306 		prunlock(pnp);
2307 		break;
2308 
2309 	case PIOCRFORK:		/* obsolete: reset inherit-on-fork flag */
2310 		error = pr_unset(p, prmapsetflags(PR_FORK));
2311 		prunlock(pnp);
2312 		break;
2313 
2314 	case PIOCSET:		/* set process flags */
2315 		error = pr_set(p, prmapsetflags((long)un32.flags));
2316 		prunlock(pnp);
2317 		break;
2318 
2319 	case PIOCRESET:		/* reset process flags */
2320 		error = pr_unset(p, prmapsetflags((long)un32.flags));
2321 		prunlock(pnp);
2322 		break;
2323 
2324 	case PIOCGREG:		/* get general registers */
2325 		if (PROCESS_NOT_32BIT(p))
2326 			error = EOVERFLOW;
2327 		else if (t->t_state != TS_STOPPED && !VSTOPPED(t))
2328 			bzero(un32.regs, sizeof (un32.regs));
2329 		else {
2330 			/* drop p_lock while touching the lwp's stack */
2331 			mutex_exit(&p->p_lock);
2332 			prgetprregs32(lwp, un32.regs);
2333 			mutex_enter(&p->p_lock);
2334 		}
2335 		prunlock(pnp);
2336 		if (error == 0 &&
2337 		    copyout(un32.regs, cmaddr, sizeof (un32.regs)))
2338 			error = EFAULT;
2339 		break;
2340 
2341 	case PIOCSREG:		/* set general registers */
2342 		if (PROCESS_NOT_32BIT(p))
2343 			error = EOVERFLOW;
2344 		else if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
2345 			error = EBUSY;
2346 		else {
2347 			/* drop p_lock while touching the lwp's stack */
2348 			mutex_exit(&p->p_lock);
2349 			prgregset_32ton(lwp, un32.regs, un.regs);
2350 			prsetprregs(lwp, un.regs, 0);
2351 			mutex_enter(&p->p_lock);
2352 		}
2353 		prunlock(pnp);
2354 		break;
2355 
2356 	case PIOCGFPREG:	/* get floating-point registers */
2357 		if (!prhasfp())
2358 			error = EINVAL;	/* No FP support */
2359 		else if (PROCESS_NOT_32BIT(p))
2360 			error = EOVERFLOW;
2361 		else if (t->t_state != TS_STOPPED && !VSTOPPED(t))
2362 			bzero(&un32.fpregs, sizeof (un32.fpregs));
2363 		else {
2364 			/* drop p_lock while touching the lwp's stack */
2365 			mutex_exit(&p->p_lock);
2366 			prgetprfpregs32(lwp, &un32.fpregs);
2367 			mutex_enter(&p->p_lock);
2368 		}
2369 		prunlock(pnp);
2370 		if (error == 0 &&
2371 		    copyout(&un32.fpregs, cmaddr, sizeof (un32.fpregs)))
2372 			error = EFAULT;
2373 		break;
2374 
2375 	case PIOCSFPREG:	/* set floating-point registers */
2376 		if (!prhasfp())
2377 			error = EINVAL;	/* No FP support */
2378 		else if (PROCESS_NOT_32BIT(p))
2379 			error = EOVERFLOW;
2380 		else if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
2381 			error = EBUSY;
2382 		else {
2383 			/* drop p_lock while touching the lwp's stack */
2384 			mutex_exit(&p->p_lock);
2385 			prsetprfpregs32(lwp, &un32.fpregs);
2386 			mutex_enter(&p->p_lock);
2387 		}
2388 		prunlock(pnp);
2389 		break;
2390 
2391 	case PIOCGXREGSIZE:	/* get the size of the extra registers */
2392 	{
2393 		int xregsize;
2394 
2395 		if (prhasx(p)) {
2396 			xregsize = prgetprxregsize(p);
2397 			prunlock(pnp);
2398 			if (copyout(&xregsize, cmaddr, sizeof (xregsize)))
2399 				error = EFAULT;
2400 		} else {
2401 			prunlock(pnp);
2402 			error = EINVAL;	/* No extra register support */
2403 		}
2404 		break;
2405 	}
2406 
2407 	case PIOCGXREG:		/* get extra registers */
2408 		if (PROCESS_NOT_32BIT(p))
2409 			error = EOVERFLOW;
2410 		else if (!prhasx(p))
2411 			error = EINVAL;	/* No extra register support */
2412 		else {
2413 			bzero(thing, thingsize);
2414 			if (t->t_state == TS_STOPPED || VSTOPPED(t)) {
2415 				/* drop p_lock to touch the stack */
2416 				mutex_exit(&p->p_lock);
2417 				prgetprxregs(lwp, thing);
2418 				mutex_enter(&p->p_lock);
2419 			}
2420 		}
2421 		prunlock(pnp);
2422 		if (error == 0 &&
2423 		    copyout(thing, cmaddr, thingsize))
2424 			error = EFAULT;
2425 		if (thing) {
2426 			kmem_free(thing, thingsize);
2427 			thing = NULL;
2428 		}
2429 		break;
2430 
2431 	case PIOCSXREG:		/* set extra registers */
2432 		if (PROCESS_NOT_32BIT(p))
2433 			error = EOVERFLOW;
2434 		else if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t))
2435 			error = EBUSY;
2436 		else if (!prhasx(p))
2437 			error = EINVAL;	/* No extra register support */
2438 		else if (thing) {
2439 			/* drop p_lock while touching the lwp's stack */
2440 			mutex_exit(&p->p_lock);
2441 			prsetprxregs(lwp, thing);
2442 			mutex_enter(&p->p_lock);
2443 		}
2444 		prunlock(pnp);
2445 		if (thing) {
2446 			kmem_free(thing, thingsize);
2447 			thing = NULL;
2448 		}
2449 		break;
2450 
2451 	case PIOCSTATUS:	/* get process/lwp status */
2452 		if (PROCESS_NOT_32BIT(p)) {
2453 			prunlock(pnp);
2454 			error = EOVERFLOW;
2455 			break;
2456 		}
2457 		oprgetstatus32(t, &un32.prstat, VTOZONE(vp));
2458 		prunlock(pnp);
2459 		if (copyout(&un32.prstat, cmaddr, sizeof (un32.prstat)))
2460 			error = EFAULT;
2461 		break;
2462 
2463 	case PIOCLSTATUS:	/* get status for process & all lwps */
2464 	{
2465 		int Nlwp;
2466 		int nlwp;
2467 		prstatus32_t *Bprsp;
2468 		prstatus32_t *prsp;
2469 
2470 		if (PROCESS_NOT_32BIT(p)) {
2471 			prunlock(pnp);
2472 			if (thing) {
2473 				kmem_free(thing, thingsize);
2474 				thing = NULL;
2475 			}
2476 			error = EOVERFLOW;
2477 			break;
2478 		}
2479 
2480 		nlwp = Nlwp = p->p_lwpcnt;
2481 
2482 		if (thing && thingsize != (Nlwp+1) * sizeof (prstatus32_t)) {
2483 			kmem_free(thing, thingsize);
2484 			thing = NULL;
2485 		}
2486 		if (thing == NULL) {
2487 			thingsize = (Nlwp+1) * sizeof (prstatus32_t);
2488 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
2489 		}
2490 		if (thing == NULL) {
2491 			prunlock(pnp);
2492 			goto startover;
2493 		}
2494 
2495 		Bprsp = (prstatus32_t *)thing;
2496 		thing = NULL;
2497 		prsp = Bprsp;
2498 		oprgetstatus32(t, prsp, VTOZONE(vp));
2499 		t = p->p_tlist;
2500 		do {
2501 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
2502 			ASSERT(nlwp > 0);
2503 			--nlwp;
2504 			oprgetstatus32(t, ++prsp, VTOZONE(vp));
2505 		} while ((t = t->t_forw) != p->p_tlist);
2506 		ASSERT(nlwp == 0);
2507 		prunlock(pnp);
2508 		if (copyout(Bprsp, cmaddr, (Nlwp+1) * sizeof (prstatus32_t)))
2509 			error = EFAULT;
2510 
2511 		kmem_free(Bprsp, (Nlwp + 1) * sizeof (prstatus32_t));
2512 		break;
2513 	}
2514 
2515 	case PIOCPSINFO:	/* get ps(1) information */
2516 	{
2517 		prpsinfo32_t *psp = &un32.prps;
2518 
2519 		oprgetpsinfo32(p, psp,
2520 		    (pnp->pr_type == PR_LWPIDFILE)? pcp->prc_thread : NULL);
2521 
2522 		prunlock(pnp);
2523 		if (copyout(&un32.prps, cmaddr, sizeof (un32.prps)))
2524 			error = EFAULT;
2525 		break;
2526 	}
2527 
2528 	case PIOCMAXSIG:	/* get maximum signal number */
2529 	{
2530 		int n = NSIG-1;
2531 
2532 		prunlock(pnp);
2533 		if (copyout(&n, cmaddr, sizeof (int)))
2534 			error = EFAULT;
2535 		break;
2536 	}
2537 
2538 	case PIOCACTION:	/* get signal action structures */
2539 	{
2540 		uint_t sig;
2541 		struct sigaction32 *sap = thing;
2542 
2543 		if (PROCESS_NOT_32BIT(p))
2544 			error = EOVERFLOW;
2545 		else {
2546 			up = PTOU(p);
2547 			for (sig = 1; sig < NSIG; sig++)
2548 				prgetaction32(p, up, sig, &sap[sig-1]);
2549 		}
2550 		prunlock(pnp);
2551 		if (error == 0 &&
2552 		    copyout(sap, cmaddr, (NSIG-1)*sizeof (struct sigaction32)))
2553 			error = EFAULT;
2554 		kmem_free(sap, (NSIG-1)*sizeof (struct sigaction32));
2555 		thing = NULL;
2556 		break;
2557 	}
2558 
2559 	case PIOCGHOLD:		/* get signal-hold mask */
2560 		schedctl_finish_sigblock(t);
2561 		sigktou(&t->t_hold, &un32.holdmask);
2562 		prunlock(pnp);
2563 		if (copyout(&un32.holdmask, cmaddr, sizeof (un32.holdmask)))
2564 			error = EFAULT;
2565 		break;
2566 
2567 	case PIOCSHOLD:		/* set signal-hold mask */
2568 		pr_sethold(pnp, &un32.holdmask);
2569 		prunlock(pnp);
2570 		break;
2571 
2572 	case PIOCNMAP:		/* get number of memory mappings */
2573 	{
2574 		int n;
2575 		struct as *as = p->p_as;
2576 
2577 		if ((p->p_flag & SSYS) || as == &kas)
2578 			n = 0;
2579 		else {
2580 			mutex_exit(&p->p_lock);
2581 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2582 			n = prnsegs(as, 0);
2583 			AS_LOCK_EXIT(as, &as->a_lock);
2584 			mutex_enter(&p->p_lock);
2585 		}
2586 		prunlock(pnp);
2587 		if (copyout(&n, cmaddr, sizeof (int)))
2588 			error = EFAULT;
2589 		break;
2590 	}
2591 
2592 	case PIOCMAP:		/* get memory map information */
2593 	{
2594 		list_t iolhead;
2595 		struct as *as = p->p_as;
2596 
2597 		if ((p->p_flag & SSYS) || as == &kas) {
2598 			error = 0;
2599 			prunlock(pnp);
2600 		} else if (PROCESS_NOT_32BIT(p)) {
2601 			error = EOVERFLOW;
2602 			prunlock(pnp);
2603 		} else {
2604 			mutex_exit(&p->p_lock);
2605 			AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2606 			error = oprgetmap32(p, &iolhead);
2607 			AS_LOCK_EXIT(as, &as->a_lock);
2608 			mutex_enter(&p->p_lock);
2609 			prunlock(pnp);
2610 
2611 			error = pr_iol_copyout_and_free(&iolhead,
2612 			    &cmaddr, error);
2613 		}
2614 		/*
2615 		 * The procfs PIOCMAP ioctl returns an all-zero buffer
2616 		 * to indicate the end of the prmap[] array.
2617 		 * Append it to whatever has already been copied out.
2618 		 */
2619 		bzero(&un32.prmap, sizeof (un32.prmap));
2620 		if (!error &&
2621 		    copyout(&un32.prmap, cmaddr, sizeof (un32.prmap)))
2622 				error = EFAULT;
2623 		break;
2624 	}
2625 
2626 	case PIOCGFAULT:	/* get mask of traced faults */
2627 		prassignset(&un32.fltmask, &p->p_fltmask);
2628 		prunlock(pnp);
2629 		if (copyout(&un32.fltmask, cmaddr, sizeof (un32.fltmask)))
2630 			error = EFAULT;
2631 		break;
2632 
2633 	case PIOCSFAULT:	/* set mask of traced faults */
2634 		pr_setfault(p, &un32.fltmask);
2635 		prunlock(pnp);
2636 		break;
2637 
2638 	case PIOCCFAULT:	/* clear current fault */
2639 		lwp->lwp_curflt = 0;
2640 		prunlock(pnp);
2641 		break;
2642 
2643 	case PIOCCRED:		/* get process credentials */
2644 	{
2645 		cred_t *cp;
2646 
2647 		mutex_enter(&p->p_crlock);
2648 		cp = p->p_cred;
2649 		un32.prcred.pr_euid = crgetuid(cp);
2650 		un32.prcred.pr_ruid = crgetruid(cp);
2651 		un32.prcred.pr_suid = crgetsuid(cp);
2652 		un32.prcred.pr_egid = crgetgid(cp);
2653 		un32.prcred.pr_rgid = crgetrgid(cp);
2654 		un32.prcred.pr_sgid = crgetsgid(cp);
2655 		un32.prcred.pr_ngroups = crgetngroups(cp);
2656 		mutex_exit(&p->p_crlock);
2657 
2658 		prunlock(pnp);
2659 		if (copyout(&un32.prcred, cmaddr, sizeof (un32.prcred)))
2660 			error = EFAULT;
2661 		break;
2662 	}
2663 
2664 	case PIOCGROUPS:	/* get supplementary groups */
2665 	{
2666 		cred_t *cp;
2667 
2668 		mutex_enter(&p->p_crlock);
2669 		cp = p->p_cred;
2670 		crhold(cp);
2671 		mutex_exit(&p->p_crlock);
2672 
2673 		prunlock(pnp);
2674 		if (copyout(crgetgroups(cp), cmaddr,
2675 		    MAX(crgetngroups(cp), 1) * sizeof (gid_t)))
2676 			error = EFAULT;
2677 		crfree(cp);
2678 		break;
2679 	}
2680 
2681 	case PIOCUSAGE:		/* get usage info */
2682 	{
2683 		/*
2684 		 * For an lwp file descriptor, return just the lwp usage.
2685 		 * For a process file descriptor, return total usage,
2686 		 * all current lwps plus all defunct lwps.
2687 		 */
2688 		prhusage_t *pup = &un32.prhusage;
2689 		prusage32_t *upup;
2690 
2691 		bzero(pup, sizeof (*pup));
2692 		pup->pr_tstamp = gethrtime();
2693 
2694 		if (pnp->pr_type == PR_LWPIDFILE) {
2695 			t = pcp->prc_thread;
2696 			if (t != NULL)
2697 				prgetusage(t, pup);
2698 			else
2699 				error = ENOENT;
2700 		} else {
2701 			pup->pr_count  = p->p_defunct;
2702 			pup->pr_create = p->p_mstart;
2703 			pup->pr_term   = p->p_mterm;
2704 
2705 			pup->pr_rtime    = p->p_mlreal;
2706 			pup->pr_utime    = p->p_acct[LMS_USER];
2707 			pup->pr_stime    = p->p_acct[LMS_SYSTEM];
2708 			pup->pr_ttime    = p->p_acct[LMS_TRAP];
2709 			pup->pr_tftime   = p->p_acct[LMS_TFAULT];
2710 			pup->pr_dftime   = p->p_acct[LMS_DFAULT];
2711 			pup->pr_kftime   = p->p_acct[LMS_KFAULT];
2712 			pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
2713 			pup->pr_slptime  = p->p_acct[LMS_SLEEP];
2714 			pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
2715 			pup->pr_stoptime = p->p_acct[LMS_STOPPED];
2716 
2717 			pup->pr_minf  = p->p_ru.minflt;
2718 			pup->pr_majf  = p->p_ru.majflt;
2719 			pup->pr_nswap = p->p_ru.nswap;
2720 			pup->pr_inblk = p->p_ru.inblock;
2721 			pup->pr_oublk = p->p_ru.oublock;
2722 			pup->pr_msnd  = p->p_ru.msgsnd;
2723 			pup->pr_mrcv  = p->p_ru.msgrcv;
2724 			pup->pr_sigs  = p->p_ru.nsignals;
2725 			pup->pr_vctx  = p->p_ru.nvcsw;
2726 			pup->pr_ictx  = p->p_ru.nivcsw;
2727 			pup->pr_sysc  = p->p_ru.sysc;
2728 			pup->pr_ioch  = p->p_ru.ioch;
2729 
2730 			/*
2731 			 * Add the usage information for each active lwp.
2732 			 */
2733 			if ((t = p->p_tlist) != NULL &&
2734 			    !(pcp->prc_flags & PRC_DESTROY)) {
2735 				do {
2736 					ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
2737 					pup->pr_count++;
2738 					praddusage(t, pup);
2739 				} while ((t = t->t_forw) != p->p_tlist);
2740 			}
2741 		}
2742 
2743 		prunlock(pnp);
2744 
2745 		upup = kmem_alloc(sizeof (*upup), KM_SLEEP);
2746 		prcvtusage32(pup, upup);
2747 		if (copyout(upup, cmaddr, sizeof (*upup)))
2748 			error = EFAULT;
2749 		kmem_free(upup, sizeof (*upup));
2750 
2751 		break;
2752 	}
2753 
2754 	case PIOCLUSAGE:	/* get detailed usage info */
2755 	{
2756 		int Nlwp;
2757 		int nlwp;
2758 		prusage32_t *upup;
2759 		prusage32_t *Bupup;
2760 		prhusage_t *pup;
2761 		hrtime_t curtime;
2762 
2763 		nlwp = Nlwp = (pcp->prc_flags & PRC_DESTROY)? 0 : p->p_lwpcnt;
2764 
2765 		if (thing && thingsize !=
2766 		    sizeof (prhusage_t) + (Nlwp+1) * sizeof (prusage32_t)) {
2767 			kmem_free(thing, thingsize);
2768 			thing = NULL;
2769 		}
2770 		if (thing == NULL) {
2771 			thingsize = sizeof (prhusage_t) +
2772 			    (Nlwp+1) * sizeof (prusage32_t);
2773 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
2774 		}
2775 		if (thing == NULL) {
2776 			prunlock(pnp);
2777 			goto startover;
2778 		}
2779 
2780 		pup = (prhusage_t *)thing;
2781 		upup = Bupup = (prusage32_t *)(pup + 1);
2782 
2783 		ASSERT(p == pcp->prc_proc);
2784 
2785 		curtime = gethrtime();
2786 
2787 		/*
2788 		 * First the summation over defunct lwps.
2789 		 */
2790 		bzero(pup, sizeof (*pup));
2791 		pup->pr_count  = p->p_defunct;
2792 		pup->pr_tstamp = curtime;
2793 		pup->pr_create = p->p_mstart;
2794 		pup->pr_term   = p->p_mterm;
2795 
2796 		pup->pr_rtime    = p->p_mlreal;
2797 		pup->pr_utime    = p->p_acct[LMS_USER];
2798 		pup->pr_stime    = p->p_acct[LMS_SYSTEM];
2799 		pup->pr_ttime    = p->p_acct[LMS_TRAP];
2800 		pup->pr_tftime   = p->p_acct[LMS_TFAULT];
2801 		pup->pr_dftime   = p->p_acct[LMS_DFAULT];
2802 		pup->pr_kftime   = p->p_acct[LMS_KFAULT];
2803 		pup->pr_ltime    = p->p_acct[LMS_USER_LOCK];
2804 		pup->pr_slptime  = p->p_acct[LMS_SLEEP];
2805 		pup->pr_wtime    = p->p_acct[LMS_WAIT_CPU];
2806 		pup->pr_stoptime = p->p_acct[LMS_STOPPED];
2807 
2808 		pup->pr_minf  = p->p_ru.minflt;
2809 		pup->pr_majf  = p->p_ru.majflt;
2810 		pup->pr_nswap = p->p_ru.nswap;
2811 		pup->pr_inblk = p->p_ru.inblock;
2812 		pup->pr_oublk = p->p_ru.oublock;
2813 		pup->pr_msnd  = p->p_ru.msgsnd;
2814 		pup->pr_mrcv  = p->p_ru.msgrcv;
2815 		pup->pr_sigs  = p->p_ru.nsignals;
2816 		pup->pr_vctx  = p->p_ru.nvcsw;
2817 		pup->pr_ictx  = p->p_ru.nivcsw;
2818 		pup->pr_sysc  = p->p_ru.sysc;
2819 		pup->pr_ioch  = p->p_ru.ioch;
2820 
2821 		prcvtusage32(pup, upup);
2822 
2823 		/*
2824 		 * Fill one prusage struct for each active lwp.
2825 		 */
2826 		if ((t = p->p_tlist) != NULL &&
2827 		    !(pcp->prc_flags & PRC_DESTROY)) {
2828 			do {
2829 				ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
2830 				ASSERT(nlwp > 0);
2831 				--nlwp;
2832 				upup++;
2833 				prgetusage(t, pup);
2834 				prcvtusage32(pup, upup);
2835 			} while ((t = t->t_forw) != p->p_tlist);
2836 		}
2837 		ASSERT(nlwp == 0);
2838 
2839 		prunlock(pnp);
2840 		if (copyout(Bupup, cmaddr, (Nlwp+1) * sizeof (prusage32_t)))
2841 			error = EFAULT;
2842 		kmem_free(thing, thingsize);
2843 		thing = NULL;
2844 		break;
2845 	}
2846 
2847 	case PIOCNAUXV:		/* get number of aux vector entries */
2848 	{
2849 		int n = __KERN_NAUXV_IMPL;
2850 
2851 		prunlock(pnp);
2852 		if (copyout(&n, cmaddr, sizeof (int)))
2853 			error = EFAULT;
2854 		break;
2855 	}
2856 
2857 	case PIOCAUXV:		/* get aux vector (see sys/auxv.h) */
2858 	{
2859 		int i;
2860 
2861 		if (PROCESS_NOT_32BIT(p)) {
2862 			prunlock(pnp);
2863 			error = EOVERFLOW;
2864 		} else {
2865 			up = PTOU(p);
2866 			for (i = 0; i < __KERN_NAUXV_IMPL; i++) {
2867 				un32.auxv[i].a_type = up->u_auxv[i].a_type;
2868 				un32.auxv[i].a_un.a_val =
2869 				    (int32_t)up->u_auxv[i].a_un.a_val;
2870 			}
2871 			prunlock(pnp);
2872 			if (copyout(un32.auxv, cmaddr,
2873 			    __KERN_NAUXV_IMPL * sizeof (auxv32_t)))
2874 				error = EFAULT;
2875 		}
2876 		break;
2877 	}
2878 
2879 #if defined(__i386) || defined(__i386_COMPAT)
2880 	case PIOCNLDT:		/* get number of LDT entries */
2881 	{
2882 		int n;
2883 
2884 		mutex_enter(&p->p_ldtlock);
2885 		n = prnldt(p);
2886 		mutex_exit(&p->p_ldtlock);
2887 		prunlock(pnp);
2888 		if (copyout(&n, cmaddr, sizeof (n)))
2889 			error = EFAULT;
2890 		break;
2891 	}
2892 
2893 	case PIOCLDT:		/* get LDT entries */
2894 	{
2895 		struct ssd *ssd;
2896 		int n;
2897 
2898 		mutex_enter(&p->p_ldtlock);
2899 		n = prnldt(p);
2900 
2901 		if (thing && thingsize != (n+1) * sizeof (*ssd)) {
2902 			kmem_free(thing, thingsize);
2903 			thing = NULL;
2904 		}
2905 		if (thing == NULL) {
2906 			thingsize = (n+1) * sizeof (*ssd);
2907 			thing = kmem_alloc(thingsize, KM_NOSLEEP);
2908 		}
2909 		if (thing == NULL) {
2910 			mutex_exit(&p->p_ldtlock);
2911 			prunlock(pnp);
2912 			goto startover;
2913 		}
2914 
2915 		ssd = thing;
2916 		thing = NULL;
2917 		if (n != 0)
2918 			prgetldt(p, ssd);
2919 		mutex_exit(&p->p_ldtlock);
2920 		prunlock(pnp);
2921 
2922 		/* mark the end of the list with a null entry */
2923 		bzero(&ssd[n], sizeof (*ssd));
2924 		if (copyout(ssd, cmaddr, (n+1) * sizeof (*ssd)))
2925 			error = EFAULT;
2926 		kmem_free(ssd, (n+1) * sizeof (*ssd));
2927 		break;
2928 	}
2929 #endif	/* __i386 || __i386_COMPAT */
2930 
2931 #if defined(__sparc)
2932 	case PIOCGWIN:		/* get gwindows_t (see sys/reg.h) */
2933 	{
2934 		gwindows32_t *gwp = thing;
2935 
2936 		if (PROCESS_NOT_32BIT(p)) {
2937 			prunlock(pnp);
2938 			error = EOVERFLOW;
2939 		} else {
2940 			/* drop p->p_lock while touching the stack */
2941 			mutex_exit(&p->p_lock);
2942 			bzero(gwp, sizeof (*gwp));
2943 			prgetwindows32(lwp, gwp);
2944 			mutex_enter(&p->p_lock);
2945 			prunlock(pnp);
2946 			if (copyout(gwp, cmaddr, sizeof (*gwp)))
2947 				error = EFAULT;
2948 		}
2949 		kmem_free(gwp, sizeof (*gwp));
2950 		thing = NULL;
2951 		break;
2952 	}
2953 #endif	/* __sparc */
2954 
2955 	default:
2956 		prunlock(pnp);
2957 		error = EINVAL;
2958 		break;
2959 
2960 	}
2961 
2962 	ASSERT(thing == NULL);
2963 	ASSERT(xpnp == NULL);
2964 	return (error);
2965 }
2966 #endif	/* _SYSCALL32_IMPL */
2967 
2968 /*
2969  * Distinguish "writeable" ioctl requests from others.
2970  */
2971 static int
2972 isprwrioctl(int cmd)
2973 {
2974 	switch (cmd) {
2975 	case PIOCSTOP:
2976 	case PIOCRUN:
2977 	case PIOCSTRACE:
2978 	case PIOCSSIG:
2979 	case PIOCKILL:
2980 	case PIOCUNKILL:
2981 	case PIOCNICE:
2982 	case PIOCSENTRY:
2983 	case PIOCSEXIT:
2984 	case PIOCSRLC:
2985 	case PIOCRRLC:
2986 	case PIOCSREG:
2987 	case PIOCSFPREG:
2988 	case PIOCSXREG:
2989 	case PIOCSHOLD:
2990 	case PIOCSFAULT:
2991 	case PIOCCFAULT:
2992 	case PIOCSFORK:
2993 	case PIOCRFORK:
2994 	case PIOCSET:
2995 	case PIOCRESET:
2996 		return (1);
2997 	}
2998 	return (0);
2999 }
3000 
3001 /*
3002  * Map the ioctl() interface run flags to the new interface run flags.
3003  */
3004 static ulong_t
3005 prmaprunflags(long flags)
3006 {
3007 	ulong_t newflags = 0;
3008 
3009 	if (flags & PRCSIG)
3010 		newflags |= 0x01;
3011 	if (flags & PRCFAULT)
3012 		newflags |= 0x02;
3013 	if (flags & PRSTEP)
3014 		newflags |= 0x04;
3015 	if (flags & PRSABORT)
3016 		newflags |= 0x08;
3017 	if (flags & PRSTOP)
3018 		newflags |= 0x10;
3019 	return (newflags);
3020 }
3021 
3022 /*
3023  * Map the ioctl() interface settable mode flags to the new interface flags.
3024  */
3025 static long
3026 prmapsetflags(long flags)
3027 {
3028 	long newflags = 0;
3029 
3030 #define	ALLFLAGS	\
3031 	(PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_PCOMPAT)
3032 
3033 	if (flags & ~ALLFLAGS)
3034 		newflags = 0xffff;	/* forces EINVAL */
3035 	if (flags & PR_FORK)
3036 		newflags |= (0x00100000 | 0x08000000);
3037 	if (flags & PR_RLC)
3038 		newflags |= 0x00200000;
3039 	if (flags & PR_KLC)
3040 		newflags |= 0x00400000;
3041 	if (flags & PR_ASYNC)
3042 		newflags |= 0x00800000;
3043 	if (flags & PR_MSACCT)
3044 		newflags |= 0x01000000;
3045 	if (flags & PR_BPTADJ)
3046 		newflags |= 0x02000000;
3047 	if (flags & PR_PCOMPAT)
3048 		newflags |= 0x04000000;
3049 	return (newflags);
3050 }
3051 
3052 /*
3053  * Apply PIOCRUN options specific to the ioctl() interface.
3054  */
3055 static void
3056 prsetrun(kthread_t *t, prrun_t *prp)
3057 {
3058 	proc_t *p = ttoproc(t);
3059 	klwp_t *lwp = ttolwp(t);
3060 	long flags = prp->pr_flags;
3061 	user_t *up = PTOU(p);
3062 
3063 	ASSERT(MUTEX_HELD(&p->p_lock));
3064 
3065 	if (flags & PRSHOLD) {
3066 		schedctl_finish_sigblock(t);
3067 		sigutok(&prp->pr_sighold, &t->t_hold);
3068 		t->t_sig_check = 1;	/* so ISSIG will be done */
3069 	}
3070 	if (flags & PRSTRACE) {
3071 		prdelset(&prp->pr_trace, SIGKILL);
3072 		prassignset(&p->p_sigmask, &prp->pr_trace);
3073 		if (!sigisempty(&p->p_sigmask))
3074 			p->p_proc_flag |= P_PR_TRACE;
3075 		else if (prisempty(&p->p_fltmask)) {
3076 			if (up->u_systrap == 0)
3077 				p->p_proc_flag &= ~P_PR_TRACE;
3078 		}
3079 	}
3080 	if (flags & PRSFAULT) {
3081 		prassignset(&p->p_fltmask, &prp->pr_fault);
3082 		if (!prisempty(&p->p_fltmask))
3083 			p->p_proc_flag |= P_PR_TRACE;
3084 		else if (sigisempty(&p->p_sigmask)) {
3085 			if (up->u_systrap == 0)
3086 				p->p_proc_flag &= ~P_PR_TRACE;
3087 		}
3088 	}
3089 	/*
3090 	 * prsvaddr() must be called before prstep() because
3091 	 * stepping can depend on the current value of the PC.
3092 	 * We drop p_lock while touching the lwp's registers (on stack).
3093 	 */
3094 	if (flags & PRSVADDR) {
3095 		mutex_exit(&p->p_lock);
3096 		prsvaddr(lwp, prp->pr_vaddr);
3097 		mutex_enter(&p->p_lock);
3098 	}
3099 }
3100 
3101 /*
3102  * Common code for PIOCOPENM
3103  * Returns with the process unlocked.
3104  */
3105 static int
3106 propenm(prnode_t *pnp, caddr_t cmaddr, caddr_t va, int *rvalp, cred_t *cr)
3107 {
3108 	proc_t *p = pnp->pr_common->prc_proc;
3109 	struct as *as = p->p_as;
3110 	int error = 0;
3111 	struct seg *seg;
3112 	struct vnode *xvp;
3113 	int n;
3114 
3115 	/*
3116 	 * By fiat, a system process has no address space.
3117 	 */
3118 	if ((p->p_flag & SSYS) || as == &kas) {
3119 		error = EINVAL;
3120 	} else if (cmaddr) {
3121 		/*
3122 		 * We drop p_lock before grabbing the address
3123 		 * space lock in order to avoid a deadlock with
3124 		 * the clock thread.  The process will not
3125 		 * disappear and its address space will not
3126 		 * change because it is marked P_PR_LOCK.
3127 		 */
3128 		mutex_exit(&p->p_lock);
3129 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
3130 		seg = as_segat(as, va);
3131 		if (seg != NULL &&
3132 		    seg->s_ops == &segvn_ops &&
3133 		    SEGOP_GETVP(seg, va, &xvp) == 0 &&
3134 		    xvp != NULL &&
3135 		    xvp->v_type == VREG) {
3136 			VN_HOLD(xvp);
3137 		} else {
3138 			error = EINVAL;
3139 		}
3140 		AS_LOCK_EXIT(as, &as->a_lock);
3141 		mutex_enter(&p->p_lock);
3142 	} else if ((xvp = p->p_exec) == NULL) {
3143 		error = EINVAL;
3144 	} else {
3145 		VN_HOLD(xvp);
3146 	}
3147 
3148 	prunlock(pnp);
3149 
3150 	if (error == 0) {
3151 		if ((error = VOP_ACCESS(xvp, VREAD, 0, cr, NULL)) == 0)
3152 			error = fassign(&xvp, FREAD, &n);
3153 		if (error) {
3154 			VN_RELE(xvp);
3155 		} else {
3156 			*rvalp = n;
3157 		}
3158 	}
3159 
3160 	return (error);
3161 }
3162 
3163 /*
3164  * Return old version of process/lwp status.
3165  * The u-block is mapped in by this routine and unmapped at the end.
3166  */
3167 void
3168 oprgetstatus(kthread_t *t, prstatus_t *sp, zone_t *zp)
3169 {
3170 	proc_t *p = ttoproc(t);
3171 	klwp_t *lwp = ttolwp(t);
3172 	int flags;
3173 	user_t *up;
3174 	ulong_t instr;
3175 
3176 	ASSERT(MUTEX_HELD(&p->p_lock));
3177 
3178 	up = PTOU(p);
3179 	bzero(sp, sizeof (*sp));
3180 	flags = 0;
3181 	if (t->t_state == TS_STOPPED) {
3182 		flags |= PR_STOPPED;
3183 		if ((t->t_schedflag & TS_PSTART) == 0)
3184 			flags |= PR_ISTOP;
3185 	} else if (VSTOPPED(t)) {
3186 		flags |= PR_STOPPED|PR_ISTOP;
3187 	}
3188 	if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP))
3189 		flags |= PR_DSTOP;
3190 	if (lwp->lwp_asleep)
3191 		flags |= PR_ASLEEP;
3192 	if (p->p_proc_flag & P_PR_FORK)
3193 		flags |= PR_FORK;
3194 	if (p->p_proc_flag & P_PR_RUNLCL)
3195 		flags |= PR_RLC;
3196 	if (p->p_proc_flag & P_PR_KILLCL)
3197 		flags |= PR_KLC;
3198 	if (p->p_proc_flag & P_PR_ASYNC)
3199 		flags |= PR_ASYNC;
3200 	if (p->p_proc_flag & P_PR_BPTADJ)
3201 		flags |= PR_BPTADJ;
3202 	if (p->p_proc_flag & P_PR_PTRACE)
3203 		flags |= PR_PCOMPAT;
3204 	if (t->t_proc_flag & TP_MSACCT)
3205 		flags |= PR_MSACCT;
3206 	sp->pr_flags = flags;
3207 	if (VSTOPPED(t)) {
3208 		sp->pr_why   = PR_REQUESTED;
3209 		sp->pr_what  = 0;
3210 	} else {
3211 		sp->pr_why   = t->t_whystop;
3212 		sp->pr_what  = t->t_whatstop;
3213 	}
3214 
3215 	if (t->t_whystop == PR_FAULTED)
3216 		bcopy(&lwp->lwp_siginfo,
3217 		    &sp->pr_info, sizeof (k_siginfo_t));
3218 	else if (lwp->lwp_curinfo)
3219 		bcopy(&lwp->lwp_curinfo->sq_info,
3220 		    &sp->pr_info, sizeof (k_siginfo_t));
3221 
3222 	if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID &&
3223 	    sp->pr_info.si_zoneid != zp->zone_id) {
3224 		sp->pr_info.si_pid = zp->zone_zsched->p_pid;
3225 		sp->pr_info.si_uid = 0;
3226 		sp->pr_info.si_ctid = -1;
3227 		sp->pr_info.si_zoneid = zp->zone_id;
3228 	}
3229 
3230 	sp->pr_cursig  = lwp->lwp_cursig;
3231 	prassignset(&sp->pr_sigpend, &p->p_sig);
3232 	prassignset(&sp->pr_lwppend, &t->t_sig);
3233 	schedctl_finish_sigblock(t);
3234 	prassignset(&sp->pr_sighold, &t->t_hold);
3235 	sp->pr_altstack = lwp->lwp_sigaltstack;
3236 	prgetaction(p, up, lwp->lwp_cursig, &sp->pr_action);
3237 	sp->pr_pid   = p->p_pid;
3238 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
3239 	    (p->p_flag & SZONETOP)) {
3240 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
3241 		/*
3242 		 * Inside local zones, fake zsched's pid as parent pids for
3243 		 * processes which reference processes outside of the zone.
3244 		 */
3245 		sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
3246 	} else {
3247 		sp->pr_ppid = p->p_ppid;
3248 	}
3249 	sp->pr_pgrp  = p->p_pgrp;
3250 	sp->pr_sid   = p->p_sessp->s_sid;
3251 	hrt2ts(mstate_aggr_state(p, LMS_USER), &sp->pr_utime);
3252 	hrt2ts(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime);
3253 	TICK_TO_TIMESTRUC(p->p_cutime, &sp->pr_cutime);
3254 	TICK_TO_TIMESTRUC(p->p_cstime, &sp->pr_cstime);
3255 	(void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name,
3256 	    sizeof (sp->pr_clname) - 1);
3257 	sp->pr_who = t->t_tid;
3258 	sp->pr_nlwp = p->p_lwpcnt;
3259 	sp->pr_brkbase = p->p_brkbase;
3260 	sp->pr_brksize = p->p_brksize;
3261 	sp->pr_stkbase = prgetstackbase(p);
3262 	sp->pr_stksize = p->p_stksize;
3263 	sp->pr_oldcontext = (struct ucontext *)lwp->lwp_oldcontext;
3264 	sp->pr_processor = t->t_cpu->cpu_id;
3265 	sp->pr_bind = t->t_bind_cpu;
3266 
3267 	/*
3268 	 * Fetch the current instruction, if not a system process.
3269 	 * We don't attempt this unless the lwp is stopped.
3270 	 */
3271 	if ((p->p_flag & SSYS) || p->p_as == &kas)
3272 		sp->pr_flags |= (PR_ISSYS|PR_PCINVAL);
3273 	else if (!(flags & PR_STOPPED))
3274 		sp->pr_flags |= PR_PCINVAL;
3275 	else if (!prfetchinstr(lwp, &instr))
3276 		sp->pr_flags |= PR_PCINVAL;
3277 	else
3278 		sp->pr_instr = instr;
3279 
3280 	/*
3281 	 * Drop p_lock while touching the lwp's stack.
3282 	 */
3283 	mutex_exit(&p->p_lock);
3284 	if (prisstep(lwp))
3285 		sp->pr_flags |= PR_STEP;
3286 	if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) {
3287 		int i;
3288 		auxv_t *auxp;
3289 
3290 		sp->pr_syscall = get_syscall_args(lwp,
3291 		    (long *)sp->pr_sysarg, &i);
3292 		sp->pr_nsysarg = (short)i;
3293 		if (t->t_whystop == PR_SYSEXIT &&
3294 		    (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve)) {
3295 			sp->pr_sysarg[0] = 0;
3296 			sp->pr_sysarg[1] = (uintptr_t)up->u_argv;
3297 			sp->pr_sysarg[2] = (uintptr_t)up->u_envp;
3298 			for (i = 0, auxp = up->u_auxv;
3299 			    i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]);
3300 			    i++, auxp++) {
3301 				if (auxp->a_type == AT_SUN_EXECNAME) {
3302 					sp->pr_sysarg[0] =
3303 					    (uintptr_t)auxp->a_un.a_ptr;
3304 					break;
3305 				}
3306 			}
3307 		}
3308 	}
3309 	if ((flags & PR_STOPPED) || t == curthread)
3310 		prgetprregs(lwp, sp->pr_reg);
3311 	mutex_enter(&p->p_lock);
3312 }
3313 
3314 /*
3315  * Return old version of information used by ps(1).
3316  */
3317 void
3318 oprgetpsinfo(proc_t *p, prpsinfo_t *psp, kthread_t *tp)
3319 {
3320 	kthread_t *t;
3321 	char c, state;
3322 	user_t *up;
3323 	dev_t d;
3324 	uint64_t pct;
3325 	int retval, niceval;
3326 	cred_t *cred;
3327 	struct as *as;
3328 	hrtime_t hrutime, hrstime, cur_time;
3329 
3330 	ASSERT(MUTEX_HELD(&p->p_lock));
3331 
3332 	bzero(psp, sizeof (*psp));
3333 
3334 	if ((t = tp) == NULL)
3335 		t = prchoose(p);	/* returns locked thread */
3336 	else
3337 		thread_lock(t);
3338 
3339 	/* kludge: map thread state enum into process state enum */
3340 
3341 	if (t == NULL) {
3342 		state = TS_ZOMB;
3343 	} else {
3344 		state = VSTOPPED(t) ? TS_STOPPED : t->t_state;
3345 		thread_unlock(t);
3346 	}
3347 
3348 	switch (state) {
3349 	case TS_SLEEP:		state = SSLEEP;		break;
3350 	case TS_RUN:		state = SRUN;		break;
3351 	case TS_ONPROC:		state = SONPROC;	break;
3352 	case TS_ZOMB:		state = SZOMB;		break;
3353 	case TS_STOPPED:	state = SSTOP;		break;
3354 	default:		state = 0;		break;
3355 	}
3356 	switch (state) {
3357 	case SSLEEP:	c = 'S';	break;
3358 	case SRUN:	c = 'R';	break;
3359 	case SZOMB:	c = 'Z';	break;
3360 	case SSTOP:	c = 'T';	break;
3361 	case SIDL:	c = 'I';	break;
3362 	case SONPROC:	c = 'O';	break;
3363 #ifdef SXBRK
3364 	case SXBRK:	c = 'X';	break;
3365 #endif
3366 	default:	c = '?';	break;
3367 	}
3368 	psp->pr_state = state;
3369 	psp->pr_sname = c;
3370 	psp->pr_zomb = (state == SZOMB);
3371 	/*
3372 	 * only export SSYS and SMSACCT; everything else is off-limits to
3373 	 * userland apps.
3374 	 */
3375 	psp->pr_flag = p->p_flag & (SSYS | SMSACCT);
3376 
3377 	mutex_enter(&p->p_crlock);
3378 	cred = p->p_cred;
3379 	psp->pr_uid = crgetruid(cred);
3380 	psp->pr_gid = crgetrgid(cred);
3381 	psp->pr_euid = crgetuid(cred);
3382 	psp->pr_egid = crgetgid(cred);
3383 	mutex_exit(&p->p_crlock);
3384 
3385 	psp->pr_pid = p->p_pid;
3386 	if (curproc->p_zone->zone_id != GLOBAL_ZONEID &&
3387 	    (p->p_flag & SZONETOP)) {
3388 		ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID);
3389 		/*
3390 		 * Inside local zones, fake zsched's pid as parent pids for
3391 		 * processes which reference processes outside of the zone.
3392 		 */
3393 		psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid;
3394 	} else {
3395 		psp->pr_ppid = p->p_ppid;
3396 	}
3397 	psp->pr_pgrp = p->p_pgrp;
3398 	psp->pr_sid = p->p_sessp->s_sid;
3399 	psp->pr_addr = prgetpsaddr(p);
3400 	hrutime = mstate_aggr_state(p, LMS_USER);
3401 	hrstime = mstate_aggr_state(p, LMS_SYSTEM);
3402 	hrt2ts(hrutime + hrstime, &psp->pr_time);
3403 	TICK_TO_TIMESTRUC(p->p_cutime + p->p_cstime, &psp->pr_ctime);
3404 	switch (p->p_model) {
3405 	case DATAMODEL_ILP32:
3406 		psp->pr_dmodel = PR_MODEL_ILP32;
3407 		break;
3408 	case DATAMODEL_LP64:
3409 		psp->pr_dmodel = PR_MODEL_LP64;
3410 		break;
3411 	}
3412 	if (state == SZOMB || t == NULL) {
3413 		int wcode = p->p_wcode;		/* must be atomic read */
3414 
3415 		if (wcode)
3416 			psp->pr_wstat = wstat(wcode, p->p_wdata);
3417 		psp->pr_lttydev = PRNODEV;
3418 		psp->pr_ottydev = (o_dev_t)PRNODEV;
3419 		psp->pr_size = 0;
3420 		psp->pr_rssize = 0;
3421 		psp->pr_pctmem = 0;
3422 	} else {
3423 		up = PTOU(p);
3424 		psp->pr_wchan = t->t_wchan;
3425 		psp->pr_pri = t->t_pri;
3426 		(void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name,
3427 		    sizeof (psp->pr_clname) - 1);
3428 		retval = CL_DONICE(t, NULL, 0, &niceval);
3429 		if (retval == 0) {
3430 			psp->pr_oldpri = v.v_maxsyspri - psp->pr_pri;
3431 			psp->pr_nice = niceval + NZERO;
3432 		} else {
3433 			psp->pr_oldpri = 0;
3434 			psp->pr_nice = 0;
3435 		}
3436 		d = cttydev(p);
3437 #ifdef sun
3438 		{
3439 			extern dev_t rwsconsdev, rconsdev, uconsdev;
3440 			/*
3441 			 * If the controlling terminal is the real
3442 			 * or workstation console device, map to what the
3443 			 * user thinks is the console device. Handle case when
3444 			 * rwsconsdev or rconsdev is set to NODEV for Starfire.
3445 			 */
3446 			if ((d == rwsconsdev || d == rconsdev) && d != NODEV)
3447 				d = uconsdev;
3448 		}
3449 #endif
3450 		psp->pr_lttydev = (d == NODEV) ? PRNODEV : d;
3451 		psp->pr_ottydev = cmpdev(d);
3452 		psp->pr_start = up->u_start;
3453 		bcopy(up->u_comm, psp->pr_fname,
3454 		    MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1));
3455 		bcopy(up->u_psargs, psp->pr_psargs,
3456 		    MIN(PRARGSZ-1, PSARGSZ));
3457 		psp->pr_syscall = t->t_sysnum;
3458 		psp->pr_argc = up->u_argc;
3459 		psp->pr_argv = (char **)up->u_argv;
3460 		psp->pr_envp = (char **)up->u_envp;
3461 
3462 		/* compute %cpu for the lwp or process */
3463 		pct = 0;
3464 		if ((t = tp) == NULL)
3465 			t = p->p_tlist;
3466 		cur_time = gethrtime_unscaled();
3467 		do {
3468 			pct += cpu_update_pct(t, cur_time);
3469 			if (tp != NULL)		/* just do the one lwp */
3470 				break;
3471 		} while ((t = t->t_forw) != p->p_tlist);
3472 
3473 		psp->pr_pctcpu = prgetpctcpu(pct);
3474 		psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15; /* [0..99] */
3475 		if (psp->pr_cpu > 99)
3476 			psp->pr_cpu = 99;
3477 
3478 		if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) {
3479 			psp->pr_size = 0;
3480 			psp->pr_rssize = 0;
3481 			psp->pr_pctmem = 0;
3482 		} else {
3483 			mutex_exit(&p->p_lock);
3484 			AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
3485 			psp->pr_size = btopr(rm_assize(as));
3486 			psp->pr_rssize = rm_asrss(as);
3487 			psp->pr_pctmem = rm_pctmemory(as);
3488 			AS_LOCK_EXIT(as, &as->a_lock);
3489 			mutex_enter(&p->p_lock);
3490 		}
3491 	}
3492 	psp->pr_bysize = ptob(psp->pr_size);
3493 	psp->pr_byrssize = ptob(psp->pr_rssize);
3494 }
3495 
3496 /*
3497  * Return an array of structures with memory map information.
3498  * We allocate here; the caller must deallocate.
3499  * The caller is also responsible to append the zero-filled entry
3500  * that terminates the PIOCMAP output buffer.
3501  */
3502 static int
3503 oprgetmap(proc_t *p, list_t *iolhead)
3504 {
3505 	struct as *as = p->p_as;
3506 	prmap_t *mp;
3507 	struct seg *seg;
3508 	struct seg *brkseg, *stkseg;
3509 	uint_t prot;
3510 
3511 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3512 
3513 	/*
3514 	 * Request an initial buffer size that doesn't waste memory
3515 	 * if the address space has only a small number of segments.
3516 	 */
3517 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
3518 
3519 	if ((seg = AS_SEGFIRST(as)) == NULL)
3520 		return (0);
3521 
3522 	brkseg = break_seg(p);
3523 	stkseg = as_segat(as, prgetstackbase(p));
3524 
3525 	do {
3526 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3527 		caddr_t saddr, naddr;
3528 		void *tmp = NULL;
3529 
3530 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3531 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3532 			if (saddr == naddr)
3533 				continue;
3534 
3535 			mp = pr_iol_newbuf(iolhead, sizeof (*mp));
3536 
3537 			mp->pr_vaddr = saddr;
3538 			mp->pr_size = naddr - saddr;
3539 			mp->pr_off = SEGOP_GETOFFSET(seg, saddr);
3540 			mp->pr_mflags = 0;
3541 			if (prot & PROT_READ)
3542 				mp->pr_mflags |= MA_READ;
3543 			if (prot & PROT_WRITE)
3544 				mp->pr_mflags |= MA_WRITE;
3545 			if (prot & PROT_EXEC)
3546 				mp->pr_mflags |= MA_EXEC;
3547 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3548 				mp->pr_mflags |= MA_SHARED;
3549 			if (seg == brkseg)
3550 				mp->pr_mflags |= MA_BREAK;
3551 			else if (seg == stkseg)
3552 				mp->pr_mflags |= MA_STACK;
3553 			mp->pr_pagesize = PAGESIZE;
3554 		}
3555 		ASSERT(tmp == NULL);
3556 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3557 
3558 	return (0);
3559 }
3560 
3561 #ifdef _SYSCALL32_IMPL
3562 static int
3563 oprgetmap32(proc_t *p, list_t *iolhead)
3564 {
3565 	struct as *as = p->p_as;
3566 	ioc_prmap32_t *mp;
3567 	struct seg *seg;
3568 	struct seg *brkseg, *stkseg;
3569 	uint_t prot;
3570 
3571 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3572 
3573 	/*
3574 	 * Request an initial buffer size that doesn't waste memory
3575 	 * if the address space has only a small number of segments.
3576 	 */
3577 	pr_iol_initlist(iolhead, sizeof (*mp), avl_numnodes(&as->a_segtree));
3578 
3579 	if ((seg = AS_SEGFIRST(as)) == NULL)
3580 		return (0);
3581 
3582 	brkseg = break_seg(p);
3583 	stkseg = as_segat(as, prgetstackbase(p));
3584 
3585 	do {
3586 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3587 		caddr_t saddr, naddr;
3588 		void *tmp = NULL;
3589 
3590 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3591 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3592 			if (saddr == naddr)
3593 				continue;
3594 
3595 			mp = pr_iol_newbuf(iolhead, sizeof (*mp));
3596 
3597 			mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr;
3598 			mp->pr_size = (size32_t)(naddr - saddr);
3599 			mp->pr_off = (off32_t)SEGOP_GETOFFSET(seg, saddr);
3600 			mp->pr_mflags = 0;
3601 			if (prot & PROT_READ)
3602 				mp->pr_mflags |= MA_READ;
3603 			if (prot & PROT_WRITE)
3604 				mp->pr_mflags |= MA_WRITE;
3605 			if (prot & PROT_EXEC)
3606 				mp->pr_mflags |= MA_EXEC;
3607 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3608 				mp->pr_mflags |= MA_SHARED;
3609 			if (seg == brkseg)
3610 				mp->pr_mflags |= MA_BREAK;
3611 			else if (seg == stkseg)
3612 				mp->pr_mflags |= MA_STACK;
3613 			mp->pr_pagesize = PAGESIZE;
3614 		}
3615 		ASSERT(tmp == NULL);
3616 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3617 
3618 	return (0);
3619 }
3620 #endif	/* _SYSCALL32_IMPL */
3621 
3622 /*
3623  * Return the size of the old /proc page data file.
3624  */
3625 size_t
3626 oprpdsize(struct as *as)
3627 {
3628 	struct seg *seg;
3629 	size_t size;
3630 
3631 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3632 
3633 	if ((seg = AS_SEGFIRST(as)) == NULL)
3634 		return (0);
3635 
3636 	size = sizeof (prpageheader_t);
3637 	do {
3638 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3639 		caddr_t saddr, naddr;
3640 		void *tmp = NULL;
3641 		size_t npage;
3642 
3643 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3644 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3645 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
3646 				size += sizeof (prasmap_t) + roundlong(npage);
3647 		}
3648 		ASSERT(tmp == NULL);
3649 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3650 
3651 	return (size);
3652 }
3653 
3654 #ifdef _SYSCALL32_IMPL
3655 size_t
3656 oprpdsize32(struct as *as)
3657 {
3658 	struct seg *seg;
3659 	size_t size;
3660 
3661 	ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock));
3662 
3663 	if ((seg = AS_SEGFIRST(as)) == NULL)
3664 		return (0);
3665 
3666 	size = sizeof (ioc_prpageheader32_t);
3667 	do {
3668 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3669 		caddr_t saddr, naddr;
3670 		void *tmp = NULL;
3671 		size_t npage;
3672 
3673 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3674 			(void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3675 			if ((npage = (naddr - saddr) / PAGESIZE) != 0)
3676 				size += sizeof (ioc_prmap32_t) + round4(npage);
3677 		}
3678 		ASSERT(tmp == NULL);
3679 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3680 
3681 	return (size);
3682 }
3683 #endif	/* _SYSCALL32_IMPL */
3684 
3685 /*
3686  * Read old /proc page data information.
3687  */
3688 int
3689 oprpdread(struct as *as, uint_t hatid, struct uio *uiop)
3690 {
3691 	caddr_t buf;
3692 	size_t size;
3693 	prpageheader_t *php;
3694 	prasmap_t *pmp;
3695 	struct seg *seg;
3696 	int error;
3697 
3698 again:
3699 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3700 
3701 	if ((seg = AS_SEGFIRST(as)) == NULL) {
3702 		AS_LOCK_EXIT(as, &as->a_lock);
3703 		return (0);
3704 	}
3705 	size = oprpdsize(as);
3706 	if (uiop->uio_resid < size) {
3707 		AS_LOCK_EXIT(as, &as->a_lock);
3708 		return (E2BIG);
3709 	}
3710 
3711 	buf = kmem_zalloc(size, KM_SLEEP);
3712 	php = (prpageheader_t *)buf;
3713 	pmp = (prasmap_t *)(buf + sizeof (prpageheader_t));
3714 
3715 	hrt2ts(gethrtime(), &php->pr_tstamp);
3716 	php->pr_nmap = 0;
3717 	php->pr_npage = 0;
3718 	do {
3719 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3720 		caddr_t saddr, naddr;
3721 		void *tmp = NULL;
3722 
3723 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3724 			size_t len;
3725 			size_t npage;
3726 			uint_t prot;
3727 			uintptr_t next;
3728 
3729 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3730 			if ((len = naddr - saddr) == 0)
3731 				continue;
3732 			npage = len / PAGESIZE;
3733 			next = (uintptr_t)(pmp + 1) + roundlong(npage);
3734 			/*
3735 			 * It's possible that the address space can change
3736 			 * subtlely even though we're holding as->a_lock
3737 			 * due to the nondeterminism of page_exists() in
3738 			 * the presence of asychronously flushed pages or
3739 			 * mapped files whose sizes are changing.
3740 			 * page_exists() may be called indirectly from
3741 			 * pr_getprot() by a SEGOP_INCORE() routine.
3742 			 * If this happens we need to make sure we don't
3743 			 * overrun the buffer whose size we computed based
3744 			 * on the initial iteration through the segments.
3745 			 * Once we've detected an overflow, we need to clean
3746 			 * up the temporary memory allocated in pr_getprot()
3747 			 * and retry. If there's a pending signal, we return
3748 			 * EINTR so that this thread can be dislodged if
3749 			 * a latent bug causes us to spin indefinitely.
3750 			 */
3751 			if (next > (uintptr_t)buf + size) {
3752 				pr_getprot_done(&tmp);
3753 				AS_LOCK_EXIT(as, &as->a_lock);
3754 
3755 				kmem_free(buf, size);
3756 
3757 				if (ISSIG(curthread, JUSTLOOKING))
3758 					return (EINTR);
3759 
3760 				goto again;
3761 			}
3762 
3763 			php->pr_nmap++;
3764 			php->pr_npage += npage;
3765 			pmp->pr_vaddr = saddr;
3766 			pmp->pr_npage = npage;
3767 			pmp->pr_off = SEGOP_GETOFFSET(seg, saddr);
3768 			pmp->pr_mflags = 0;
3769 			if (prot & PROT_READ)
3770 				pmp->pr_mflags |= MA_READ;
3771 			if (prot & PROT_WRITE)
3772 				pmp->pr_mflags |= MA_WRITE;
3773 			if (prot & PROT_EXEC)
3774 				pmp->pr_mflags |= MA_EXEC;
3775 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3776 				pmp->pr_mflags |= MA_SHARED;
3777 			pmp->pr_pagesize = PAGESIZE;
3778 			hat_getstat(as, saddr, len, hatid,
3779 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
3780 			pmp = (prasmap_t *)next;
3781 		}
3782 		ASSERT(tmp == NULL);
3783 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3784 
3785 	AS_LOCK_EXIT(as, &as->a_lock);
3786 
3787 	ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size);
3788 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
3789 	kmem_free(buf, size);
3790 
3791 	return (error);
3792 }
3793 
3794 #ifdef _SYSCALL32_IMPL
3795 int
3796 oprpdread32(struct as *as, uint_t hatid, struct uio *uiop)
3797 {
3798 	caddr_t buf;
3799 	size_t size;
3800 	ioc_prpageheader32_t *php;
3801 	ioc_prasmap32_t *pmp;
3802 	struct seg *seg;
3803 	int error;
3804 
3805 again:
3806 	AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
3807 
3808 	if ((seg = AS_SEGFIRST(as)) == NULL) {
3809 		AS_LOCK_EXIT(as, &as->a_lock);
3810 		return (0);
3811 	}
3812 	size = oprpdsize32(as);
3813 	if (uiop->uio_resid < size) {
3814 		AS_LOCK_EXIT(as, &as->a_lock);
3815 		return (E2BIG);
3816 	}
3817 
3818 	buf = kmem_zalloc(size, KM_SLEEP);
3819 	php = (ioc_prpageheader32_t *)buf;
3820 	pmp = (ioc_prasmap32_t *)(buf + sizeof (ioc_prpageheader32_t));
3821 
3822 	hrt2ts32(gethrtime(), &php->pr_tstamp);
3823 	php->pr_nmap = 0;
3824 	php->pr_npage = 0;
3825 	do {
3826 		caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0);
3827 		caddr_t saddr, naddr;
3828 		void *tmp = NULL;
3829 
3830 		for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) {
3831 			size_t len;
3832 			size_t npage;
3833 			uint_t prot;
3834 			uintptr_t next;
3835 
3836 			prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr);
3837 			if ((len = naddr - saddr) == 0)
3838 				continue;
3839 			npage = len / PAGESIZE;
3840 			next = (uintptr_t)(pmp + 1) + round4(npage);
3841 			/*
3842 			 * It's possible that the address space can change
3843 			 * subtlely even though we're holding as->a_lock
3844 			 * due to the nondeterminism of page_exists() in
3845 			 * the presence of asychronously flushed pages or
3846 			 * mapped files whose sizes are changing.
3847 			 * page_exists() may be called indirectly from
3848 			 * pr_getprot() by a SEGOP_INCORE() routine.
3849 			 * If this happens we need to make sure we don't
3850 			 * overrun the buffer whose size we computed based
3851 			 * on the initial iteration through the segments.
3852 			 * Once we've detected an overflow, we need to clean
3853 			 * up the temporary memory allocated in pr_getprot()
3854 			 * and retry. If there's a pending signal, we return
3855 			 * EINTR so that this thread can be dislodged if
3856 			 * a latent bug causes us to spin indefinitely.
3857 			 */
3858 			if (next > (uintptr_t)buf + size) {
3859 				pr_getprot_done(&tmp);
3860 				AS_LOCK_EXIT(as, &as->a_lock);
3861 
3862 				kmem_free(buf, size);
3863 
3864 				if (ISSIG(curthread, JUSTLOOKING))
3865 					return (EINTR);
3866 
3867 				goto again;
3868 			}
3869 
3870 			php->pr_nmap++;
3871 			php->pr_npage += npage;
3872 			pmp->pr_vaddr = (uint32_t)(uintptr_t)saddr;
3873 			pmp->pr_npage = (uint32_t)npage;
3874 			pmp->pr_off = (int32_t)SEGOP_GETOFFSET(seg, saddr);
3875 			pmp->pr_mflags = 0;
3876 			if (prot & PROT_READ)
3877 				pmp->pr_mflags |= MA_READ;
3878 			if (prot & PROT_WRITE)
3879 				pmp->pr_mflags |= MA_WRITE;
3880 			if (prot & PROT_EXEC)
3881 				pmp->pr_mflags |= MA_EXEC;
3882 			if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED)
3883 				pmp->pr_mflags |= MA_SHARED;
3884 			pmp->pr_pagesize = PAGESIZE;
3885 			hat_getstat(as, saddr, len, hatid,
3886 			    (char *)(pmp + 1), HAT_SYNC_ZERORM);
3887 			pmp = (ioc_prasmap32_t *)next;
3888 		}
3889 		ASSERT(tmp == NULL);
3890 	} while ((seg = AS_SEGNEXT(as, seg)) != NULL);
3891 
3892 	AS_LOCK_EXIT(as, &as->a_lock);
3893 
3894 	ASSERT((uintptr_t)pmp == (uintptr_t)buf + size);
3895 	error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop);
3896 	kmem_free(buf, size);
3897 
3898 	return (error);
3899 }
3900 #endif	/* _SYSCALL32_IMPL */
3901 
3902 /*ARGSUSED*/
3903 #ifdef _SYSCALL32_IMPL
3904 int
3905 prioctl(
3906 	struct vnode *vp,
3907 	int cmd,
3908 	intptr_t arg,
3909 	int flag,
3910 	cred_t *cr,
3911 	int *rvalp,
3912 	caller_context_t *ct)
3913 {
3914 	switch (curproc->p_model) {
3915 	case DATAMODEL_ILP32:
3916 		return (prioctl32(vp, cmd, arg, flag, cr, rvalp, ct));
3917 	case DATAMODEL_LP64:
3918 		return (prioctl64(vp, cmd, arg, flag, cr, rvalp, ct));
3919 	default:
3920 		return (ENOSYS);
3921 	}
3922 }
3923 #endif	/* _SYSCALL32_IMPL */
3924