xref: /titanic_52/usr/src/uts/common/fs/proc/prcontrol.c (revision a99982a76d4cc12b1e9021e88531cf425d1e7369)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <sys/uio.h>
32 #include <sys/param.h>
33 #include <sys/cmn_err.h>
34 #include <sys/cred.h>
35 #include <sys/policy.h>
36 #include <sys/debug.h>
37 #include <sys/errno.h>
38 #include <sys/file.h>
39 #include <sys/inline.h>
40 #include <sys/kmem.h>
41 #include <sys/proc.h>
42 #include <sys/regset.h>
43 #include <sys/sysmacros.h>
44 #include <sys/systm.h>
45 #include <sys/vfs.h>
46 #include <sys/vnode.h>
47 #include <sys/signal.h>
48 #include <sys/auxv.h>
49 #include <sys/user.h>
50 #include <sys/class.h>
51 #include <sys/fault.h>
52 #include <sys/syscall.h>
53 #include <sys/procfs.h>
54 #include <sys/zone.h>
55 #include <sys/copyops.h>
56 #include <sys/schedctl.h>
57 #include <vm/as.h>
58 #include <vm/seg.h>
59 #include <fs/proc/prdata.h>
60 #include <sys/contract/process_impl.h>
61 
62 static	void	pr_settrace(proc_t *, sigset_t *);
63 static	int	pr_setfpregs(prnode_t *, prfpregset_t *);
64 #if defined(__sparc)
65 static	int	pr_setxregs(prnode_t *, prxregset_t *);
66 static	int	pr_setasrs(prnode_t *, asrset_t);
67 #endif
68 static	int	pr_setvaddr(prnode_t *, caddr_t);
69 static	int	pr_clearsig(prnode_t *);
70 static	int	pr_clearflt(prnode_t *);
71 static	int	pr_watch(prnode_t *, prwatch_t *, int *);
72 static	int	pr_agent(prnode_t *, prgregset_t, int *);
73 static	int	pr_rdwr(proc_t *, enum uio_rw, priovec_t *);
74 static	int	pr_scred(proc_t *, prcred_t *, cred_t *, boolean_t);
75 static	int	pr_spriv(proc_t *, prpriv_t *, cred_t *);
76 static	int	pr_szoneid(proc_t *, zoneid_t, cred_t *);
77 static	void	pauselwps(proc_t *);
78 static	void	unpauselwps(proc_t *);
79 
80 typedef union {
81 	long		sig;		/* PCKILL, PCUNKILL */
82 	long		nice;		/* PCNICE */
83 	long		timeo;		/* PCTWSTOP */
84 	ulong_t		flags;		/* PCRUN, PCSET, PCUNSET */
85 	caddr_t		vaddr;		/* PCSVADDR */
86 	siginfo_t	siginfo;	/* PCSSIG */
87 	sigset_t	sigset;		/* PCSTRACE, PCSHOLD */
88 	fltset_t	fltset;		/* PCSFAULT */
89 	sysset_t	sysset;		/* PCSENTRY, PCSEXIT */
90 	prgregset_t	prgregset;	/* PCSREG, PCAGENT */
91 	prfpregset_t	prfpregset;	/* PCSFPREG */
92 #if defined(__sparc)
93 	prxregset_t	prxregset;	/* PCSXREG */
94 	asrset_t	asrset;		/* PCSASRS */
95 #endif
96 	prwatch_t	prwatch;	/* PCWATCH */
97 	priovec_t	priovec;	/* PCREAD, PCWRITE */
98 	prcred_t	prcred;		/* PCSCRED */
99 	prpriv_t	prpriv;		/* PCSPRIV */
100 	long		przoneid;	/* PCSZONE */
101 } arg_t;
102 
103 static	int	pr_control(long, arg_t *, prnode_t *, cred_t *);
104 
105 static size_t
106 ctlsize(long cmd, size_t resid, arg_t *argp)
107 {
108 	size_t size = sizeof (long);
109 	size_t rnd;
110 	int ngrp;
111 
112 	switch (cmd) {
113 	case PCNULL:
114 	case PCSTOP:
115 	case PCDSTOP:
116 	case PCWSTOP:
117 	case PCCSIG:
118 	case PCCFAULT:
119 		break;
120 	case PCSSIG:
121 		size += sizeof (siginfo_t);
122 		break;
123 	case PCTWSTOP:
124 		size += sizeof (long);
125 		break;
126 	case PCKILL:
127 	case PCUNKILL:
128 	case PCNICE:
129 		size += sizeof (long);
130 		break;
131 	case PCRUN:
132 	case PCSET:
133 	case PCUNSET:
134 		size += sizeof (ulong_t);
135 		break;
136 	case PCSVADDR:
137 		size += sizeof (caddr_t);
138 		break;
139 	case PCSTRACE:
140 	case PCSHOLD:
141 		size += sizeof (sigset_t);
142 		break;
143 	case PCSFAULT:
144 		size += sizeof (fltset_t);
145 		break;
146 	case PCSENTRY:
147 	case PCSEXIT:
148 		size += sizeof (sysset_t);
149 		break;
150 	case PCSREG:
151 	case PCAGENT:
152 		size += sizeof (prgregset_t);
153 		break;
154 	case PCSFPREG:
155 		size += sizeof (prfpregset_t);
156 		break;
157 #if defined(__sparc)
158 	case PCSXREG:
159 		size += sizeof (prxregset_t);
160 		break;
161 	case PCSASRS:
162 		size += sizeof (asrset_t);
163 		break;
164 #endif
165 	case PCWATCH:
166 		size += sizeof (prwatch_t);
167 		break;
168 	case PCREAD:
169 	case PCWRITE:
170 		size += sizeof (priovec_t);
171 		break;
172 	case PCSCRED:
173 		size += sizeof (prcred_t);
174 		break;
175 	case PCSCREDX:
176 		/*
177 		 * We cannot derefence the pr_ngroups fields if it
178 		 * we don't have enough data.
179 		 */
180 		if (resid < size + sizeof (prcred_t) - sizeof (gid_t))
181 			return (0);
182 		ngrp = argp->prcred.pr_ngroups;
183 		if (ngrp < 0 || ngrp > ngroups_max)
184 			return (0);
185 
186 		/* The result can be smaller than sizeof (prcred_t) */
187 		size += sizeof (prcred_t) - sizeof (gid_t);
188 		size += ngrp * sizeof (gid_t);
189 		break;
190 	case PCSPRIV:
191 		if (resid >= size + sizeof (prpriv_t))
192 			size += priv_prgetprivsize(&argp->prpriv);
193 		else
194 			return (0);
195 		break;
196 	case PCSZONE:
197 		size += sizeof (long);
198 		break;
199 	default:
200 		return (0);
201 	}
202 
203 	/* Round up to a multiple of long, unless exact amount written */
204 	if (size < resid) {
205 		rnd = size & (sizeof (long) - 1);
206 
207 		if (rnd != 0)
208 			size += sizeof (long) - rnd;
209 	}
210 
211 	if (size > resid)
212 		return (0);
213 	return (size);
214 }
215 
216 /*
217  * Control operations (lots).
218  */
219 int
220 prwritectl(vnode_t *vp, uio_t *uiop, cred_t *cr)
221 {
222 #define	MY_BUFFER_SIZE \
223 		100 > 1 + sizeof (arg_t) / sizeof (long) ? \
224 		100 : 1 + sizeof (arg_t) / sizeof (long)
225 	long buf[MY_BUFFER_SIZE];
226 	long *bufp;
227 	size_t resid = 0;
228 	size_t size;
229 	prnode_t *pnp = VTOP(vp);
230 	int error;
231 	int locked = 0;
232 
233 	while (uiop->uio_resid) {
234 		/*
235 		 * Read several commands in one gulp.
236 		 */
237 		bufp = buf;
238 		if (resid) {	/* move incomplete command to front of buffer */
239 			long *tail;
240 
241 			if (resid >= sizeof (buf))
242 				break;
243 			tail = (long *)((char *)buf + sizeof (buf) - resid);
244 			do {
245 				*bufp++ = *tail++;
246 			} while ((resid -= sizeof (long)) != 0);
247 		}
248 		resid = sizeof (buf) - ((char *)bufp - (char *)buf);
249 		if (resid > uiop->uio_resid)
250 			resid = uiop->uio_resid;
251 		if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
252 			return (error);
253 		resid += (char *)bufp - (char *)buf;
254 		bufp = buf;
255 
256 		do {		/* loop over commands in buffer */
257 			long cmd = bufp[0];
258 			arg_t *argp = (arg_t *)&bufp[1];
259 
260 			size = ctlsize(cmd, resid, argp);
261 			if (size == 0)	/* incomplete or invalid command */
262 				break;
263 			/*
264 			 * Perform the specified control operation.
265 			 */
266 			if (!locked) {
267 				if ((error = prlock(pnp, ZNO)) != 0)
268 					return (error);
269 				locked = 1;
270 			}
271 			if (error = pr_control(cmd, argp, pnp, cr)) {
272 				if (error == -1)	/* -1 is timeout */
273 					locked = 0;
274 				else
275 					return (error);
276 			}
277 			bufp = (long *)((char *)bufp + size);
278 		} while ((resid -= size) != 0);
279 
280 		if (locked) {
281 			prunlock(pnp);
282 			locked = 0;
283 		}
284 	}
285 	return (resid? EINVAL : 0);
286 }
287 
288 static int
289 pr_control(long cmd, arg_t *argp, prnode_t *pnp, cred_t *cr)
290 {
291 	prcommon_t *pcp;
292 	proc_t *p;
293 	int unlocked;
294 	int error = 0;
295 
296 	if (cmd == PCNULL)
297 		return (0);
298 
299 	pcp = pnp->pr_common;
300 	p = pcp->prc_proc;
301 	ASSERT(p != NULL);
302 
303 	switch (cmd) {
304 
305 	default:
306 		error = EINVAL;
307 		break;
308 
309 	case PCSTOP:	/* direct process or lwp to stop and wait for stop */
310 	case PCDSTOP:	/* direct process or lwp to stop, don't wait */
311 	case PCWSTOP:	/* wait for process or lwp to stop */
312 	case PCTWSTOP:	/* wait for process or lwp to stop, with timeout */
313 	    {
314 		time_t timeo;
315 
316 		/*
317 		 * Can't apply to a system process.
318 		 */
319 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
320 			error = EBUSY;
321 			break;
322 		}
323 
324 		if (cmd == PCSTOP || cmd == PCDSTOP)
325 			pr_stop(pnp);
326 
327 		if (cmd == PCDSTOP)
328 			break;
329 
330 		/*
331 		 * If an lwp is waiting for itself or its process, don't wait.
332 		 * The stopped lwp would never see the fact that it is stopped.
333 		 */
334 		if ((pcp->prc_flags & PRC_LWP)?
335 		    (pcp->prc_thread == curthread) : (p == curproc)) {
336 			if (cmd == PCWSTOP || cmd == PCTWSTOP)
337 				error = EBUSY;
338 			break;
339 		}
340 
341 		timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
342 		if ((error = pr_wait_stop(pnp, timeo)) != 0)
343 			return (error);
344 
345 		break;
346 	    }
347 
348 	case PCRUN:	/* make lwp or process runnable */
349 		error = pr_setrun(pnp, argp->flags);
350 		break;
351 
352 	case PCSTRACE:	/* set signal trace mask */
353 		pr_settrace(p,  &argp->sigset);
354 		break;
355 
356 	case PCSSIG:	/* set current signal */
357 		error = pr_setsig(pnp, &argp->siginfo);
358 		if (argp->siginfo.si_signo == SIGKILL && error == 0) {
359 			prunlock(pnp);
360 			pr_wait_die(pnp);
361 			return (-1);
362 		}
363 		break;
364 
365 	case PCKILL:	/* send signal */
366 		error = pr_kill(pnp, (int)argp->sig, cr);
367 		if (error == 0 && argp->sig == SIGKILL) {
368 			prunlock(pnp);
369 			pr_wait_die(pnp);
370 			return (-1);
371 		}
372 		break;
373 
374 	case PCUNKILL:	/* delete a pending signal */
375 		error = pr_unkill(pnp, (int)argp->sig);
376 		break;
377 
378 	case PCNICE:	/* set nice priority */
379 		error = pr_nice(p, (int)argp->nice, cr);
380 		break;
381 
382 	case PCSENTRY:	/* set syscall entry bit mask */
383 	case PCSEXIT:	/* set syscall exit bit mask */
384 		pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
385 		break;
386 
387 	case PCSET:	/* set process flags */
388 		error = pr_set(p, argp->flags);
389 		break;
390 
391 	case PCUNSET:	/* unset process flags */
392 		error = pr_unset(p, argp->flags);
393 		break;
394 
395 	case PCSREG:	/* set general registers */
396 	    {
397 		kthread_t *t = pr_thread(pnp);
398 
399 		if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
400 			thread_unlock(t);
401 			error = EBUSY;
402 		} else {
403 			thread_unlock(t);
404 			mutex_exit(&p->p_lock);
405 			prsetprregs(ttolwp(t), argp->prgregset, 0);
406 			mutex_enter(&p->p_lock);
407 		}
408 		break;
409 	    }
410 
411 	case PCSFPREG:	/* set floating-point registers */
412 		error = pr_setfpregs(pnp, &argp->prfpregset);
413 		break;
414 
415 	case PCSXREG:	/* set extra registers */
416 #if defined(__sparc)
417 		error = pr_setxregs(pnp, &argp->prxregset);
418 #else
419 		error = EINVAL;
420 #endif
421 		break;
422 
423 #if defined(__sparc)
424 	case PCSASRS:	/* set ancillary state registers */
425 		error = pr_setasrs(pnp, argp->asrset);
426 		break;
427 #endif
428 
429 	case PCSVADDR:	/* set virtual address at which to resume */
430 		error = pr_setvaddr(pnp, argp->vaddr);
431 		break;
432 
433 	case PCSHOLD:	/* set signal-hold mask */
434 		pr_sethold(pnp, &argp->sigset);
435 		break;
436 
437 	case PCSFAULT:	/* set mask of traced faults */
438 		pr_setfault(p, &argp->fltset);
439 		break;
440 
441 	case PCCSIG:	/* clear current signal */
442 		error = pr_clearsig(pnp);
443 		break;
444 
445 	case PCCFAULT:	/* clear current fault */
446 		error = pr_clearflt(pnp);
447 		break;
448 
449 	case PCWATCH:	/* set or clear watched areas */
450 		error = pr_watch(pnp, &argp->prwatch, &unlocked);
451 		if (error && unlocked)
452 			return (error);
453 		break;
454 
455 	case PCAGENT:	/* create the /proc agent lwp in the target process */
456 		error = pr_agent(pnp, argp->prgregset, &unlocked);
457 		if (error && unlocked)
458 			return (error);
459 		break;
460 
461 	case PCREAD:	/* read from the address space */
462 		error = pr_rdwr(p, UIO_READ, &argp->priovec);
463 		break;
464 
465 	case PCWRITE:	/* write to the address space */
466 		error = pr_rdwr(p, UIO_WRITE, &argp->priovec);
467 		break;
468 
469 	case PCSCRED:	/* set the process credentials */
470 	case PCSCREDX:
471 		error = pr_scred(p, &argp->prcred, cr, cmd == PCSCREDX);
472 		break;
473 
474 	case PCSPRIV:	/* set the process privileges */
475 		error = pr_spriv(p, &argp->prpriv, cr);
476 		break;
477 	case PCSZONE:	/* set the process's zoneid credentials */
478 		error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
479 		break;
480 	}
481 
482 	if (error)
483 		prunlock(pnp);
484 	return (error);
485 }
486 
487 #ifdef _SYSCALL32_IMPL
488 
489 typedef union {
490 	int32_t		sig;		/* PCKILL, PCUNKILL */
491 	int32_t		nice;		/* PCNICE */
492 	int32_t		timeo;		/* PCTWSTOP */
493 	uint32_t	flags;		/* PCRUN, PCSET, PCUNSET */
494 	caddr32_t	vaddr;		/* PCSVADDR */
495 	siginfo32_t	siginfo;	/* PCSSIG */
496 	sigset_t	sigset;		/* PCSTRACE, PCSHOLD */
497 	fltset_t	fltset;		/* PCSFAULT */
498 	sysset_t	sysset;		/* PCSENTRY, PCSEXIT */
499 	prgregset32_t	prgregset;	/* PCSREG, PCAGENT */
500 	prfpregset32_t	prfpregset;	/* PCSFPREG */
501 #if defined(__sparc)
502 	prxregset_t	prxregset;	/* PCSXREG */
503 #endif
504 	prwatch32_t	prwatch;	/* PCWATCH */
505 	priovec32_t	priovec;	/* PCREAD, PCWRITE */
506 	prcred32_t	prcred;		/* PCSCRED */
507 	prpriv_t	prpriv;		/* PCSPRIV */
508 	int32_t		przoneid;	/* PCSZONE */
509 } arg32_t;
510 
511 static	int	pr_control32(int32_t, arg32_t *, prnode_t *, cred_t *);
512 static	int	pr_setfpregs32(prnode_t *, prfpregset32_t *);
513 
514 /*
515  * Note that while ctlsize32() can use argp, it must do so only in a way
516  * that assumes 32-bit rather than 64-bit alignment as argp is a pointer
517  * to an array of 32-bit values and only 32-bit alignment is ensured.
518  */
519 static size_t
520 ctlsize32(int32_t cmd, size_t resid, arg32_t *argp)
521 {
522 	size_t size = sizeof (int32_t);
523 	size_t rnd;
524 	int ngrp;
525 
526 	switch (cmd) {
527 	case PCNULL:
528 	case PCSTOP:
529 	case PCDSTOP:
530 	case PCWSTOP:
531 	case PCCSIG:
532 	case PCCFAULT:
533 		break;
534 	case PCSSIG:
535 		size += sizeof (siginfo32_t);
536 		break;
537 	case PCTWSTOP:
538 		size += sizeof (int32_t);
539 		break;
540 	case PCKILL:
541 	case PCUNKILL:
542 	case PCNICE:
543 		size += sizeof (int32_t);
544 		break;
545 	case PCRUN:
546 	case PCSET:
547 	case PCUNSET:
548 		size += sizeof (uint32_t);
549 		break;
550 	case PCSVADDR:
551 		size += sizeof (caddr32_t);
552 		break;
553 	case PCSTRACE:
554 	case PCSHOLD:
555 		size += sizeof (sigset_t);
556 		break;
557 	case PCSFAULT:
558 		size += sizeof (fltset_t);
559 		break;
560 	case PCSENTRY:
561 	case PCSEXIT:
562 		size += sizeof (sysset_t);
563 		break;
564 	case PCSREG:
565 	case PCAGENT:
566 		size += sizeof (prgregset32_t);
567 		break;
568 	case PCSFPREG:
569 		size += sizeof (prfpregset32_t);
570 		break;
571 #if defined(__sparc)
572 	case PCSXREG:
573 		size += sizeof (prxregset_t);
574 		break;
575 #endif
576 	case PCWATCH:
577 		size += sizeof (prwatch32_t);
578 		break;
579 	case PCREAD:
580 	case PCWRITE:
581 		size += sizeof (priovec32_t);
582 		break;
583 	case PCSCRED:
584 		size += sizeof (prcred32_t);
585 		break;
586 	case PCSCREDX:
587 		/*
588 		 * We cannot derefence the pr_ngroups fields if it
589 		 * we don't have enough data.
590 		 */
591 		if (resid < size + sizeof (prcred32_t) - sizeof (gid32_t))
592 			return (0);
593 		ngrp = argp->prcred.pr_ngroups;
594 		if (ngrp < 0 || ngrp > ngroups_max)
595 			return (0);
596 
597 		/* The result can be smaller than sizeof (prcred32_t) */
598 		size += sizeof (prcred32_t) - sizeof (gid32_t);
599 		size += ngrp * sizeof (gid32_t);
600 		break;
601 	case PCSPRIV:
602 		if (resid >= size + sizeof (prpriv_t))
603 			size += priv_prgetprivsize(&argp->prpriv);
604 		else
605 			return (0);
606 		break;
607 	case PCSZONE:
608 		size += sizeof (int32_t);
609 		break;
610 	default:
611 		return (0);
612 	}
613 
614 	/* Round up to a multiple of int32_t */
615 	rnd = size & (sizeof (int32_t) - 1);
616 
617 	if (rnd != 0)
618 		size += sizeof (int32_t) - rnd;
619 
620 	if (size > resid)
621 		return (0);
622 	return (size);
623 }
624 
625 /*
626  * Control operations (lots).
627  */
628 int
629 prwritectl32(struct vnode *vp, struct uio *uiop, cred_t *cr)
630 {
631 #define	MY_BUFFER_SIZE32 \
632 		100 > 1 + sizeof (arg32_t) / sizeof (int32_t) ? \
633 		100 : 1 + sizeof (arg32_t) / sizeof (int32_t)
634 	int32_t buf[MY_BUFFER_SIZE32];
635 	int32_t *bufp;
636 	arg32_t arg;
637 	size_t resid = 0;
638 	size_t size;
639 	prnode_t *pnp = VTOP(vp);
640 	int error;
641 	int locked = 0;
642 
643 	while (uiop->uio_resid) {
644 		/*
645 		 * Read several commands in one gulp.
646 		 */
647 		bufp = buf;
648 		if (resid) {	/* move incomplete command to front of buffer */
649 			int32_t *tail;
650 
651 			if (resid >= sizeof (buf))
652 				break;
653 			tail = (int32_t *)((char *)buf + sizeof (buf) - resid);
654 			do {
655 				*bufp++ = *tail++;
656 			} while ((resid -= sizeof (int32_t)) != 0);
657 		}
658 		resid = sizeof (buf) - ((char *)bufp - (char *)buf);
659 		if (resid > uiop->uio_resid)
660 			resid = uiop->uio_resid;
661 		if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
662 			return (error);
663 		resid += (char *)bufp - (char *)buf;
664 		bufp = buf;
665 
666 		do {		/* loop over commands in buffer */
667 			int32_t cmd = bufp[0];
668 			arg32_t *argp = (arg32_t *)&bufp[1];
669 
670 			size = ctlsize32(cmd, resid, argp);
671 			if (size == 0)	/* incomplete or invalid command */
672 				break;
673 			/*
674 			 * Perform the specified control operation.
675 			 */
676 			if (!locked) {
677 				if ((error = prlock(pnp, ZNO)) != 0)
678 					return (error);
679 				locked = 1;
680 			}
681 
682 			/*
683 			 * Since some members of the arg32_t union contain
684 			 * 64-bit values (which must be 64-bit aligned), we
685 			 * can't simply pass a pointer to the structure as
686 			 * it may be unaligned. Note that we do pass the
687 			 * potentially unaligned structure to ctlsize32()
688 			 * above, but that uses it a way that makes no
689 			 * assumptions about alignment.
690 			 */
691 			ASSERT(size - sizeof (cmd) <= sizeof (arg));
692 			bcopy(argp, &arg, size - sizeof (cmd));
693 
694 			if (error = pr_control32(cmd, &arg, pnp, cr)) {
695 				if (error == -1)	/* -1 is timeout */
696 					locked = 0;
697 				else
698 					return (error);
699 			}
700 			bufp = (int32_t *)((char *)bufp + size);
701 		} while ((resid -= size) != 0);
702 
703 		if (locked) {
704 			prunlock(pnp);
705 			locked = 0;
706 		}
707 	}
708 	return (resid? EINVAL : 0);
709 }
710 
711 static int
712 pr_control32(int32_t cmd, arg32_t *argp, prnode_t *pnp, cred_t *cr)
713 {
714 	prcommon_t *pcp;
715 	proc_t *p;
716 	int unlocked;
717 	int error = 0;
718 
719 	if (cmd == PCNULL)
720 		return (0);
721 
722 	pcp = pnp->pr_common;
723 	p = pcp->prc_proc;
724 	ASSERT(p != NULL);
725 
726 	switch (cmd) {
727 
728 	default:
729 		error = EINVAL;
730 		break;
731 
732 	case PCSTOP:	/* direct process or lwp to stop and wait for stop */
733 	case PCDSTOP:	/* direct process or lwp to stop, don't wait */
734 	case PCWSTOP:	/* wait for process or lwp to stop */
735 	case PCTWSTOP:	/* wait for process or lwp to stop, with timeout */
736 	    {
737 		time_t timeo;
738 
739 		/*
740 		 * Can't apply to a system process.
741 		 */
742 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
743 			error = EBUSY;
744 			break;
745 		}
746 
747 		if (cmd == PCSTOP || cmd == PCDSTOP)
748 			pr_stop(pnp);
749 
750 		if (cmd == PCDSTOP)
751 			break;
752 
753 		/*
754 		 * If an lwp is waiting for itself or its process, don't wait.
755 		 * The lwp will never see the fact that itself is stopped.
756 		 */
757 		if ((pcp->prc_flags & PRC_LWP)?
758 		    (pcp->prc_thread == curthread) : (p == curproc)) {
759 			if (cmd == PCWSTOP || cmd == PCTWSTOP)
760 				error = EBUSY;
761 			break;
762 		}
763 
764 		timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
765 		if ((error = pr_wait_stop(pnp, timeo)) != 0)
766 			return (error);
767 
768 		break;
769 	    }
770 
771 	case PCRUN:	/* make lwp or process runnable */
772 		error = pr_setrun(pnp, (ulong_t)argp->flags);
773 		break;
774 
775 	case PCSTRACE:	/* set signal trace mask */
776 		pr_settrace(p,  &argp->sigset);
777 		break;
778 
779 	case PCSSIG:	/* set current signal */
780 		if (PROCESS_NOT_32BIT(p))
781 			error = EOVERFLOW;
782 		else {
783 			int sig = (int)argp->siginfo.si_signo;
784 			siginfo_t siginfo;
785 
786 			bzero(&siginfo, sizeof (siginfo));
787 			siginfo_32tok(&argp->siginfo, (k_siginfo_t *)&siginfo);
788 			error = pr_setsig(pnp, &siginfo);
789 			if (sig == SIGKILL && error == 0) {
790 				prunlock(pnp);
791 				pr_wait_die(pnp);
792 				return (-1);
793 			}
794 		}
795 		break;
796 
797 	case PCKILL:	/* send signal */
798 		error = pr_kill(pnp, (int)argp->sig, cr);
799 		if (error == 0 && argp->sig == SIGKILL) {
800 			prunlock(pnp);
801 			pr_wait_die(pnp);
802 			return (-1);
803 		}
804 		break;
805 
806 	case PCUNKILL:	/* delete a pending signal */
807 		error = pr_unkill(pnp, (int)argp->sig);
808 		break;
809 
810 	case PCNICE:	/* set nice priority */
811 		error = pr_nice(p, (int)argp->nice, cr);
812 		break;
813 
814 	case PCSENTRY:	/* set syscall entry bit mask */
815 	case PCSEXIT:	/* set syscall exit bit mask */
816 		pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
817 		break;
818 
819 	case PCSET:	/* set process flags */
820 		error = pr_set(p, (long)argp->flags);
821 		break;
822 
823 	case PCUNSET:	/* unset process flags */
824 		error = pr_unset(p, (long)argp->flags);
825 		break;
826 
827 	case PCSREG:	/* set general registers */
828 		if (PROCESS_NOT_32BIT(p))
829 			error = EOVERFLOW;
830 		else {
831 			kthread_t *t = pr_thread(pnp);
832 
833 			if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
834 				thread_unlock(t);
835 				error = EBUSY;
836 			} else {
837 				prgregset_t prgregset;
838 				klwp_t *lwp = ttolwp(t);
839 
840 				thread_unlock(t);
841 				mutex_exit(&p->p_lock);
842 				prgregset_32ton(lwp, argp->prgregset,
843 					prgregset);
844 				prsetprregs(lwp, prgregset, 0);
845 				mutex_enter(&p->p_lock);
846 			}
847 		}
848 		break;
849 
850 	case PCSFPREG:	/* set floating-point registers */
851 		if (PROCESS_NOT_32BIT(p))
852 			error = EOVERFLOW;
853 		else
854 			error = pr_setfpregs32(pnp, &argp->prfpregset);
855 		break;
856 
857 	case PCSXREG:	/* set extra registers */
858 #if defined(__sparc)
859 		if (PROCESS_NOT_32BIT(p))
860 			error = EOVERFLOW;
861 		else
862 			error = pr_setxregs(pnp, &argp->prxregset);
863 #else
864 		error = EINVAL;
865 #endif
866 		break;
867 
868 	case PCSVADDR:	/* set virtual address at which to resume */
869 		if (PROCESS_NOT_32BIT(p))
870 			error = EOVERFLOW;
871 		else
872 			error = pr_setvaddr(pnp,
873 			    (caddr_t)(uintptr_t)argp->vaddr);
874 		break;
875 
876 	case PCSHOLD:	/* set signal-hold mask */
877 		pr_sethold(pnp, &argp->sigset);
878 		break;
879 
880 	case PCSFAULT:	/* set mask of traced faults */
881 		pr_setfault(p, &argp->fltset);
882 		break;
883 
884 	case PCCSIG:	/* clear current signal */
885 		error = pr_clearsig(pnp);
886 		break;
887 
888 	case PCCFAULT:	/* clear current fault */
889 		error = pr_clearflt(pnp);
890 		break;
891 
892 	case PCWATCH:	/* set or clear watched areas */
893 		if (PROCESS_NOT_32BIT(p))
894 			error = EOVERFLOW;
895 		else {
896 			prwatch_t prwatch;
897 
898 			prwatch.pr_vaddr = argp->prwatch.pr_vaddr;
899 			prwatch.pr_size = argp->prwatch.pr_size;
900 			prwatch.pr_wflags = argp->prwatch.pr_wflags;
901 			prwatch.pr_pad = argp->prwatch.pr_pad;
902 			error = pr_watch(pnp, &prwatch, &unlocked);
903 			if (error && unlocked)
904 				return (error);
905 		}
906 		break;
907 
908 	case PCAGENT:	/* create the /proc agent lwp in the target process */
909 		if (PROCESS_NOT_32BIT(p))
910 			error = EOVERFLOW;
911 		else {
912 			prgregset_t prgregset;
913 			kthread_t *t = pr_thread(pnp);
914 			klwp_t *lwp = ttolwp(t);
915 			thread_unlock(t);
916 			mutex_exit(&p->p_lock);
917 			prgregset_32ton(lwp, argp->prgregset, prgregset);
918 			mutex_enter(&p->p_lock);
919 			error = pr_agent(pnp, prgregset, &unlocked);
920 			if (error && unlocked)
921 				return (error);
922 		}
923 		break;
924 
925 	case PCREAD:	/* read from the address space */
926 	case PCWRITE:	/* write to the address space */
927 		if (PROCESS_NOT_32BIT(p))
928 			error = EOVERFLOW;
929 		else {
930 			enum uio_rw rw = (cmd == PCREAD)? UIO_READ : UIO_WRITE;
931 			priovec_t priovec;
932 
933 			priovec.pio_base =
934 			    (void *)(uintptr_t)argp->priovec.pio_base;
935 			priovec.pio_len = (size_t)argp->priovec.pio_len;
936 			priovec.pio_offset = (off_t)
937 				(uint32_t)argp->priovec.pio_offset;
938 			error = pr_rdwr(p, rw, &priovec);
939 		}
940 		break;
941 
942 	case PCSCRED:	/* set the process credentials */
943 	case PCSCREDX:
944 	    {
945 		/*
946 		 * All the fields in these structures are exactly the same
947 		 * and so the structures are compatible.  In case this
948 		 * ever changes, we catch this with the ASSERT below.
949 		 */
950 		prcred_t *prcred = (prcred_t *)&argp->prcred;
951 
952 #ifndef __lint
953 		ASSERT(sizeof (prcred_t) == sizeof (prcred32_t));
954 #endif
955 
956 		error = pr_scred(p, prcred, cr, cmd == PCSCREDX);
957 		break;
958 	    }
959 
960 	case PCSPRIV:	/* set the process privileges */
961 	    {
962 		error = pr_spriv(p, &argp->prpriv, cr);
963 		break;
964 	    }
965 
966 	case PCSZONE:	/* set the process's zoneid */
967 	    error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
968 	    break;
969 	}
970 
971 	if (error)
972 		prunlock(pnp);
973 	return (error);
974 }
975 
976 #endif	/* _SYSCALL32_IMPL */
977 
978 /*
979  * Return the specific or chosen thread/lwp for a control operation.
980  * Returns with the thread locked via thread_lock(t).
981  */
982 kthread_t *
983 pr_thread(prnode_t *pnp)
984 {
985 	prcommon_t *pcp = pnp->pr_common;
986 	kthread_t *t;
987 
988 	if (pcp->prc_flags & PRC_LWP) {
989 		t = pcp->prc_thread;
990 		ASSERT(t != NULL);
991 		thread_lock(t);
992 	} else {
993 		proc_t *p = pcp->prc_proc;
994 		t = prchoose(p);	/* returns locked thread */
995 		ASSERT(t != NULL);
996 	}
997 
998 	return (t);
999 }
1000 
1001 /*
1002  * Direct the process or lwp to stop.
1003  */
1004 void
1005 pr_stop(prnode_t *pnp)
1006 {
1007 	prcommon_t *pcp = pnp->pr_common;
1008 	proc_t *p = pcp->prc_proc;
1009 	kthread_t *t;
1010 	vnode_t *vp;
1011 
1012 	/*
1013 	 * If already stopped, do nothing; otherwise flag
1014 	 * it to be stopped the next time it tries to run.
1015 	 * If sleeping at interruptible priority, set it
1016 	 * running so it will stop within cv_wait_sig().
1017 	 *
1018 	 * Take care to cooperate with jobcontrol: if an lwp
1019 	 * is stopped due to the default action of a jobcontrol
1020 	 * stop signal, flag it to be stopped the next time it
1021 	 * starts due to a SIGCONT signal.
1022 	 */
1023 	if (pcp->prc_flags & PRC_LWP)
1024 		t = pcp->prc_thread;
1025 	else
1026 		t = p->p_tlist;
1027 	ASSERT(t != NULL);
1028 
1029 	do {
1030 		int notify;
1031 
1032 		notify = 0;
1033 		thread_lock(t);
1034 		if (!ISTOPPED(t)) {
1035 			t->t_proc_flag |= TP_PRSTOP;
1036 			t->t_sig_check = 1;	/* do ISSIG */
1037 		}
1038 		if (t->t_state == TS_SLEEP &&
1039 		    (t->t_flag & T_WAKEABLE)) {
1040 			if (t->t_wchan0 == NULL)
1041 				setrun_locked(t);
1042 			else if (!VSTOPPED(t)) {
1043 				/*
1044 				 * Mark it virtually stopped.
1045 				 */
1046 				t->t_proc_flag |= TP_PRVSTOP;
1047 				notify = 1;
1048 			}
1049 		}
1050 		/*
1051 		 * force the thread into the kernel
1052 		 * if it is not already there.
1053 		 */
1054 		prpokethread(t);
1055 		thread_unlock(t);
1056 		if (notify &&
1057 		    (vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace) != NULL)
1058 			prnotify(vp);
1059 		if (pcp->prc_flags & PRC_LWP)
1060 			break;
1061 	} while ((t = t->t_forw) != p->p_tlist);
1062 
1063 	/*
1064 	 * We do this just in case the thread we asked
1065 	 * to stop is in holdlwps() (called from cfork()).
1066 	 */
1067 	cv_broadcast(&p->p_holdlwps);
1068 }
1069 
1070 /*
1071  * Sleep until the lwp stops, but cooperate with
1072  * jobcontrol:  Don't wake up if the lwp is stopped
1073  * due to the default action of a jobcontrol stop signal.
1074  * If this is the process file descriptor, sleep
1075  * until all of the process's lwps stop.
1076  */
1077 int
1078 pr_wait_stop(prnode_t *pnp, time_t timeo)
1079 {
1080 	prcommon_t *pcp = pnp->pr_common;
1081 	proc_t *p = pcp->prc_proc;
1082 	timestruc_t rqtime;
1083 	timestruc_t *rqtp = NULL;
1084 	kthread_t *t;
1085 	int error;
1086 
1087 	if (timeo > 0) {	/* millisecond timeout */
1088 		/*
1089 		 * Determine the precise future time of the requested timeout.
1090 		 */
1091 		timestruc_t now;
1092 
1093 		gethrestime(&now);
1094 		rqtp = &rqtime;
1095 		rqtp->tv_sec = timeo / MILLISEC;
1096 		rqtp->tv_nsec = (timeo % MILLISEC) * MICROSEC;
1097 		timespecadd(rqtp, &now);
1098 	}
1099 
1100 	if (pcp->prc_flags & PRC_LWP) {	/* lwp file descriptor */
1101 		t = pcp->prc_thread;
1102 		ASSERT(t != NULL);
1103 		thread_lock(t);
1104 		while (!ISTOPPED(t) && !VSTOPPED(t)) {
1105 			thread_unlock(t);
1106 			mutex_enter(&pcp->prc_mutex);
1107 			prunlock(pnp);
1108 			error = pr_wait(pcp, rqtp);
1109 			if (error)	/* -1 is timeout */
1110 				return (error);
1111 			if ((error = prlock(pnp, ZNO)) != 0)
1112 				return (error);
1113 			ASSERT(p == pcp->prc_proc);
1114 			ASSERT(t == pcp->prc_thread);
1115 			thread_lock(t);
1116 		}
1117 		thread_unlock(t);
1118 	} else {			/* process file descriptor */
1119 		t = prchoose(p);	/* returns locked thread */
1120 		ASSERT(t != NULL);
1121 		ASSERT(MUTEX_HELD(&p->p_lock));
1122 		while ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t)) ||
1123 		    (p->p_flag & SEXITLWPS)) {
1124 			thread_unlock(t);
1125 			mutex_enter(&pcp->prc_mutex);
1126 			prunlock(pnp);
1127 			error = pr_wait(pcp, rqtp);
1128 			if (error)	/* -1 is timeout */
1129 				return (error);
1130 			if ((error = prlock(pnp, ZNO)) != 0)
1131 				return (error);
1132 			ASSERT(p == pcp->prc_proc);
1133 			t = prchoose(p);	/* returns locked t */
1134 			ASSERT(t != NULL);
1135 		}
1136 		thread_unlock(t);
1137 	}
1138 
1139 	ASSERT(!(pcp->prc_flags & PRC_DESTROY) && p->p_stat != SZOMB &&
1140 	    t != NULL && t->t_state != TS_ZOMB);
1141 
1142 	return (0);
1143 }
1144 
1145 int
1146 pr_setrun(prnode_t *pnp, ulong_t flags)
1147 {
1148 	prcommon_t *pcp = pnp->pr_common;
1149 	proc_t *p = pcp->prc_proc;
1150 	kthread_t *t;
1151 	klwp_t *lwp;
1152 
1153 	/*
1154 	 * Cannot set an lwp running if it is not stopped.
1155 	 * Also, no lwp other than the /proc agent lwp can
1156 	 * be set running so long as the /proc agent lwp exists.
1157 	 */
1158 	t = pr_thread(pnp);	/* returns locked thread */
1159 	if ((!ISTOPPED(t) && !VSTOPPED(t) &&
1160 	    !(t->t_proc_flag & TP_PRSTOP)) ||
1161 	    (p->p_agenttp != NULL &&
1162 	    (t != p->p_agenttp || !(pcp->prc_flags & PRC_LWP)))) {
1163 		thread_unlock(t);
1164 		return (EBUSY);
1165 	}
1166 	thread_unlock(t);
1167 	if (flags & ~(PRCSIG|PRCFAULT|PRSTEP|PRSTOP|PRSABORT))
1168 		return (EINVAL);
1169 	lwp = ttolwp(t);
1170 	if ((flags & PRCSIG) && lwp->lwp_cursig != SIGKILL) {
1171 		/*
1172 		 * Discard current siginfo_t, if any.
1173 		 */
1174 		lwp->lwp_cursig = 0;
1175 		lwp->lwp_extsig = 0;
1176 		if (lwp->lwp_curinfo) {
1177 			siginfofree(lwp->lwp_curinfo);
1178 			lwp->lwp_curinfo = NULL;
1179 		}
1180 	}
1181 	if (flags & PRCFAULT)
1182 		lwp->lwp_curflt = 0;
1183 	/*
1184 	 * We can't hold p->p_lock when we touch the lwp's registers.
1185 	 * It may be swapped out and we will get a page fault.
1186 	 */
1187 	if (flags & PRSTEP) {
1188 		mutex_exit(&p->p_lock);
1189 		prstep(lwp, 0);
1190 		mutex_enter(&p->p_lock);
1191 	}
1192 	if (flags & PRSTOP) {
1193 		t->t_proc_flag |= TP_PRSTOP;
1194 		t->t_sig_check = 1;	/* do ISSIG */
1195 	}
1196 	if (flags & PRSABORT)
1197 		lwp->lwp_sysabort = 1;
1198 	thread_lock(t);
1199 	if ((pcp->prc_flags & PRC_LWP) || (flags & (PRSTEP|PRSTOP))) {
1200 		/*
1201 		 * Here, we are dealing with a single lwp.
1202 		 */
1203 		if (ISTOPPED(t)) {
1204 			t->t_schedflag |= TS_PSTART;
1205 			t->t_dtrace_stop = 0;
1206 			setrun_locked(t);
1207 		} else if (flags & PRSABORT) {
1208 			t->t_proc_flag &=
1209 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1210 			setrun_locked(t);
1211 		} else if (!(flags & PRSTOP)) {
1212 			t->t_proc_flag &=
1213 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1214 		}
1215 		thread_unlock(t);
1216 	} else {
1217 		/*
1218 		 * Here, we are dealing with the whole process.
1219 		 */
1220 		if (ISTOPPED(t)) {
1221 			/*
1222 			 * The representative lwp is stopped on an event
1223 			 * of interest.  We demote it to PR_REQUESTED and
1224 			 * choose another representative lwp.  If the new
1225 			 * representative lwp is not stopped on an event of
1226 			 * interest (other than PR_REQUESTED), we set the
1227 			 * whole process running, else we leave the process
1228 			 * stopped showing the next event of interest.
1229 			 */
1230 			kthread_t *tx = NULL;
1231 
1232 			if (!(flags & PRSABORT) &&
1233 			    t->t_whystop == PR_SYSENTRY &&
1234 			    t->t_whatstop == SYS_lwp_exit)
1235 				tx = t;		/* remember the exiting lwp */
1236 			t->t_whystop = PR_REQUESTED;
1237 			t->t_whatstop = 0;
1238 			thread_unlock(t);
1239 			t = prchoose(p);	/* returns locked t */
1240 			ASSERT(ISTOPPED(t) || VSTOPPED(t));
1241 			if (VSTOPPED(t) ||
1242 			    t->t_whystop == PR_REQUESTED) {
1243 				thread_unlock(t);
1244 				allsetrun(p);
1245 			} else {
1246 				thread_unlock(t);
1247 				/*
1248 				 * As a special case, if the old representative
1249 				 * lwp was stopped on entry to _lwp_exit()
1250 				 * (and we are not aborting the system call),
1251 				 * we set the old representative lwp running.
1252 				 * We do this so that the next process stop
1253 				 * will find the exiting lwp gone.
1254 				 */
1255 				if (tx != NULL) {
1256 					thread_lock(tx);
1257 					tx->t_schedflag |= TS_PSTART;
1258 					t->t_dtrace_stop = 0;
1259 					setrun_locked(tx);
1260 					thread_unlock(tx);
1261 				}
1262 			}
1263 		} else {
1264 			/*
1265 			 * No event of interest; set all of the lwps running.
1266 			 */
1267 			if (flags & PRSABORT) {
1268 				t->t_proc_flag &=
1269 				    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1270 				setrun_locked(t);
1271 			}
1272 			thread_unlock(t);
1273 			allsetrun(p);
1274 		}
1275 	}
1276 	return (0);
1277 }
1278 
1279 /*
1280  * Wait until process/lwp stops or until timer expires.
1281  * Return EINTR for an interruption, -1 for timeout, else 0.
1282  */
1283 int
1284 pr_wait(prcommon_t *pcp,	/* prcommon referring to process/lwp */
1285 	timestruc_t *ts)	/* absolute time of timeout, if any */
1286 {
1287 	int rval;
1288 
1289 	ASSERT(MUTEX_HELD(&pcp->prc_mutex));
1290 	rval = cv_waituntil_sig(&pcp->prc_wait, &pcp->prc_mutex, ts);
1291 	mutex_exit(&pcp->prc_mutex);
1292 	switch (rval) {
1293 	case 0:
1294 		return (EINTR);
1295 	case -1:
1296 		return (-1);
1297 	default:
1298 		return (0);
1299 	}
1300 }
1301 
1302 /*
1303  * Make all threads in the process runnable.
1304  */
1305 void
1306 allsetrun(proc_t *p)
1307 {
1308 	kthread_t *t;
1309 
1310 	ASSERT(MUTEX_HELD(&p->p_lock));
1311 
1312 	if ((t = p->p_tlist) != NULL) {
1313 		do {
1314 			thread_lock(t);
1315 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1316 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1317 			if (ISTOPPED(t)) {
1318 				t->t_schedflag |= TS_PSTART;
1319 				t->t_dtrace_stop = 0;
1320 				setrun_locked(t);
1321 			}
1322 			thread_unlock(t);
1323 		} while ((t = t->t_forw) != p->p_tlist);
1324 	}
1325 }
1326 
1327 /*
1328  * Wait for the process to die.
1329  * We do this after sending SIGKILL because we know it will
1330  * die soon and we want subsequent operations to return ENOENT.
1331  */
1332 void
1333 pr_wait_die(prnode_t *pnp)
1334 {
1335 	proc_t *p;
1336 
1337 	mutex_enter(&pidlock);
1338 	while ((p = pnp->pr_common->prc_proc) != NULL && p->p_stat != SZOMB) {
1339 		if (!cv_wait_sig(&p->p_srwchan_cv, &pidlock))
1340 			break;
1341 	}
1342 	mutex_exit(&pidlock);
1343 }
1344 
1345 static void
1346 pr_settrace(proc_t *p, sigset_t *sp)
1347 {
1348 	prdelset(sp, SIGKILL);
1349 	prassignset(&p->p_sigmask, sp);
1350 	if (!sigisempty(&p->p_sigmask))
1351 		p->p_proc_flag |= P_PR_TRACE;
1352 	else if (prisempty(&p->p_fltmask)) {
1353 		user_t *up = PTOU(p);
1354 		if (up->u_systrap == 0)
1355 			p->p_proc_flag &= ~P_PR_TRACE;
1356 	}
1357 }
1358 
1359 int
1360 pr_setsig(prnode_t *pnp, siginfo_t *sip)
1361 {
1362 	int sig = sip->si_signo;
1363 	prcommon_t *pcp = pnp->pr_common;
1364 	proc_t *p = pcp->prc_proc;
1365 	kthread_t *t;
1366 	klwp_t *lwp;
1367 	int error = 0;
1368 
1369 	t = pr_thread(pnp);	/* returns locked thread */
1370 	thread_unlock(t);
1371 	lwp = ttolwp(t);
1372 	if (sig < 0 || sig >= NSIG)
1373 		/* Zero allowed here */
1374 		error = EINVAL;
1375 	else if (lwp->lwp_cursig == SIGKILL)
1376 		/* "can't happen", but just in case */
1377 		error = EBUSY;
1378 	else if ((lwp->lwp_cursig = (uchar_t)sig) == 0) {
1379 		lwp->lwp_extsig = 0;
1380 		/*
1381 		 * Discard current siginfo_t, if any.
1382 		 */
1383 		if (lwp->lwp_curinfo) {
1384 			siginfofree(lwp->lwp_curinfo);
1385 			lwp->lwp_curinfo = NULL;
1386 		}
1387 	} else {
1388 		kthread_t *tx;
1389 		sigqueue_t *sqp;
1390 
1391 		/* drop p_lock to do kmem_alloc(KM_SLEEP) */
1392 		mutex_exit(&p->p_lock);
1393 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1394 		mutex_enter(&p->p_lock);
1395 
1396 		if (lwp->lwp_curinfo == NULL)
1397 			lwp->lwp_curinfo = sqp;
1398 		else
1399 			kmem_free(sqp, sizeof (sigqueue_t));
1400 		/*
1401 		 * Copy contents of info to current siginfo_t.
1402 		 */
1403 		bcopy(sip, &lwp->lwp_curinfo->sq_info,
1404 		    sizeof (lwp->lwp_curinfo->sq_info));
1405 		/*
1406 		 * Prevent contents published by si_zoneid-unaware /proc
1407 		 * consumers from being incorrectly filtered.  Because
1408 		 * an uninitialized si_zoneid is the same as
1409 		 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1410 		 * process in a non-global zone with a siginfo which
1411 		 * appears to come from the global zone.
1412 		 */
1413 		if (SI_FROMUSER(sip) && sip->si_zoneid == 0)
1414 			lwp->lwp_curinfo->sq_info.si_zoneid =
1415 			    p->p_zone->zone_id;
1416 		/*
1417 		 * Side-effects for SIGKILL and jobcontrol signals.
1418 		 */
1419 		if (sig == SIGKILL) {
1420 			p->p_flag |= SKILLED;
1421 			p->p_flag &= ~SEXTKILLED;
1422 		} else if (sig == SIGCONT) {
1423 			p->p_flag |= SSCONT;
1424 			sigdelq(p, NULL, SIGSTOP);
1425 			sigdelq(p, NULL, SIGTSTP);
1426 			sigdelq(p, NULL, SIGTTOU);
1427 			sigdelq(p, NULL, SIGTTIN);
1428 			sigdiffset(&p->p_sig, &stopdefault);
1429 			sigdiffset(&p->p_extsig, &stopdefault);
1430 			if ((tx = p->p_tlist) != NULL) {
1431 				do {
1432 					sigdelq(p, tx, SIGSTOP);
1433 					sigdelq(p, tx, SIGTSTP);
1434 					sigdelq(p, tx, SIGTTOU);
1435 					sigdelq(p, tx, SIGTTIN);
1436 					sigdiffset(&tx->t_sig, &stopdefault);
1437 					sigdiffset(&tx->t_extsig, &stopdefault);
1438 				} while ((tx = tx->t_forw) != p->p_tlist);
1439 			}
1440 		} else if (sigismember(&stopdefault, sig)) {
1441 			if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
1442 			    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
1443 				p->p_flag &= ~SSCONT;
1444 			sigdelq(p, NULL, SIGCONT);
1445 			sigdelset(&p->p_sig, SIGCONT);
1446 			sigdelset(&p->p_extsig, SIGCONT);
1447 			if ((tx = p->p_tlist) != NULL) {
1448 				do {
1449 					sigdelq(p, tx, SIGCONT);
1450 					sigdelset(&tx->t_sig, SIGCONT);
1451 					sigdelset(&tx->t_extsig, SIGCONT);
1452 				} while ((tx = tx->t_forw) != p->p_tlist);
1453 			}
1454 		}
1455 		thread_lock(t);
1456 		if (t->t_state == TS_SLEEP &&
1457 		    (t->t_flag & T_WAKEABLE)) {
1458 			/* Set signalled sleeping lwp running */
1459 			setrun_locked(t);
1460 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL) {
1461 			/* If SIGKILL, set stopped lwp running */
1462 			p->p_stopsig = 0;
1463 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1464 			t->t_dtrace_stop = 0;
1465 			setrun_locked(t);
1466 		}
1467 		t->t_sig_check = 1;	/* so ISSIG will be done */
1468 		thread_unlock(t);
1469 		/*
1470 		 * More jobcontrol side-effects.
1471 		 */
1472 		if (sig == SIGCONT && (tx = p->p_tlist) != NULL) {
1473 			p->p_stopsig = 0;
1474 			do {
1475 				thread_lock(tx);
1476 				if (tx->t_state == TS_STOPPED &&
1477 				    tx->t_whystop == PR_JOBCONTROL) {
1478 					tx->t_schedflag |= TS_XSTART;
1479 					setrun_locked(tx);
1480 				}
1481 				thread_unlock(tx);
1482 			} while ((tx = tx->t_forw) != p->p_tlist);
1483 		}
1484 	}
1485 	return (error);
1486 }
1487 
1488 int
1489 pr_kill(prnode_t *pnp, int sig, cred_t *cr)
1490 {
1491 	prcommon_t *pcp = pnp->pr_common;
1492 	proc_t *p = pcp->prc_proc;
1493 	k_siginfo_t info;
1494 
1495 	if (sig <= 0 || sig >= NSIG)
1496 		return (EINVAL);
1497 
1498 	bzero(&info, sizeof (info));
1499 	info.si_signo = sig;
1500 	info.si_code = SI_USER;
1501 	info.si_pid = curproc->p_pid;
1502 	info.si_ctid = PRCTID(curproc);
1503 	info.si_zoneid = getzoneid();
1504 	info.si_uid = crgetruid(cr);
1505 	sigaddq(p, (pcp->prc_flags & PRC_LWP)?
1506 	    pcp->prc_thread : NULL, &info, KM_NOSLEEP);
1507 
1508 	return (0);
1509 }
1510 
1511 int
1512 pr_unkill(prnode_t *pnp, int sig)
1513 {
1514 	prcommon_t *pcp = pnp->pr_common;
1515 	proc_t *p = pcp->prc_proc;
1516 	sigqueue_t *infop = NULL;
1517 
1518 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL)
1519 		return (EINVAL);
1520 
1521 	if (pcp->prc_flags & PRC_LWP)
1522 		sigdeq(p, pcp->prc_thread, sig, &infop);
1523 	else
1524 		sigdeq(p, NULL, sig, &infop);
1525 
1526 	if (infop)
1527 		siginfofree(infop);
1528 
1529 	return (0);
1530 }
1531 
1532 int
1533 pr_nice(proc_t *p, int nice, cred_t *cr)
1534 {
1535 	kthread_t *t;
1536 	int err;
1537 	int error = 0;
1538 
1539 	t = p->p_tlist;
1540 	do {
1541 		ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1542 		err = CL_DONICE(t, cr, nice, (int *)NULL);
1543 		if (error == 0)
1544 			error = err;
1545 	} while ((t = t->t_forw) != p->p_tlist);
1546 
1547 	return (error);
1548 }
1549 
1550 void
1551 pr_setentryexit(proc_t *p, sysset_t *sysset, int entry)
1552 {
1553 	user_t *up = PTOU(p);
1554 
1555 	if (entry) {
1556 		prassignset(&up->u_entrymask, sysset);
1557 	} else {
1558 		prassignset(&up->u_exitmask, sysset);
1559 	}
1560 	if (!prisempty(&up->u_entrymask) ||
1561 	    !prisempty(&up->u_exitmask)) {
1562 		up->u_systrap = 1;
1563 		p->p_proc_flag |= P_PR_TRACE;
1564 		set_proc_sys(p);	/* set pre and post-sys flags */
1565 	} else {
1566 		up->u_systrap = 0;
1567 		if (sigisempty(&p->p_sigmask) &&
1568 		    prisempty(&p->p_fltmask))
1569 			p->p_proc_flag &= ~P_PR_TRACE;
1570 	}
1571 }
1572 
1573 #define	ALLFLAGS	\
1574 	(PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
1575 
1576 int
1577 pr_set(proc_t *p, long flags)
1578 {
1579 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1580 		return (EBUSY);
1581 
1582 	if (flags & ~ALLFLAGS)
1583 		return (EINVAL);
1584 
1585 	if (flags & PR_FORK)
1586 		p->p_proc_flag |= P_PR_FORK;
1587 	if (flags & PR_RLC)
1588 		p->p_proc_flag |= P_PR_RUNLCL;
1589 	if (flags & PR_KLC)
1590 		p->p_proc_flag |= P_PR_KILLCL;
1591 	if (flags & PR_ASYNC)
1592 		p->p_proc_flag |= P_PR_ASYNC;
1593 	if (flags & PR_BPTADJ)
1594 		p->p_proc_flag |= P_PR_BPTADJ;
1595 	if (flags & PR_MSACCT)
1596 		if ((p->p_flag & SMSACCT) == 0)
1597 			estimate_msacct(p->p_tlist, gethrtime());
1598 	if (flags & PR_MSFORK)
1599 		p->p_flag |= SMSFORK;
1600 	if (flags & PR_PTRACE) {
1601 		p->p_proc_flag |= P_PR_PTRACE;
1602 		/* ptraced process must die if parent dead */
1603 		if (p->p_ppid == 1)
1604 			sigtoproc(p, NULL, SIGKILL);
1605 	}
1606 
1607 	return (0);
1608 }
1609 
1610 int
1611 pr_unset(proc_t *p, long flags)
1612 {
1613 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1614 		return (EBUSY);
1615 
1616 	if (flags & ~ALLFLAGS)
1617 		return (EINVAL);
1618 
1619 	if (flags & PR_FORK)
1620 		p->p_proc_flag &= ~P_PR_FORK;
1621 	if (flags & PR_RLC)
1622 		p->p_proc_flag &= ~P_PR_RUNLCL;
1623 	if (flags & PR_KLC)
1624 		p->p_proc_flag &= ~P_PR_KILLCL;
1625 	if (flags & PR_ASYNC)
1626 		p->p_proc_flag &= ~P_PR_ASYNC;
1627 	if (flags & PR_BPTADJ)
1628 		p->p_proc_flag &= ~P_PR_BPTADJ;
1629 	if (flags & PR_MSACCT)
1630 		disable_msacct(p);
1631 	if (flags & PR_MSFORK)
1632 		p->p_flag &= ~SMSFORK;
1633 	if (flags & PR_PTRACE)
1634 		p->p_proc_flag &= ~P_PR_PTRACE;
1635 
1636 	return (0);
1637 }
1638 
1639 static int
1640 pr_setfpregs(prnode_t *pnp, prfpregset_t *prfpregset)
1641 {
1642 	proc_t *p = pnp->pr_common->prc_proc;
1643 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1644 
1645 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1646 		thread_unlock(t);
1647 		return (EBUSY);
1648 	}
1649 	if (!prhasfp()) {
1650 		thread_unlock(t);
1651 		return (EINVAL);	/* No FP support */
1652 	}
1653 
1654 	/* drop p_lock while touching the lwp's stack */
1655 	thread_unlock(t);
1656 	mutex_exit(&p->p_lock);
1657 	prsetprfpregs(ttolwp(t), prfpregset);
1658 	mutex_enter(&p->p_lock);
1659 
1660 	return (0);
1661 }
1662 
1663 #ifdef	_SYSCALL32_IMPL
1664 static int
1665 pr_setfpregs32(prnode_t *pnp, prfpregset32_t *prfpregset)
1666 {
1667 	proc_t *p = pnp->pr_common->prc_proc;
1668 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1669 
1670 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1671 		thread_unlock(t);
1672 		return (EBUSY);
1673 	}
1674 	if (!prhasfp()) {
1675 		thread_unlock(t);
1676 		return (EINVAL);	/* No FP support */
1677 	}
1678 
1679 	/* drop p_lock while touching the lwp's stack */
1680 	thread_unlock(t);
1681 	mutex_exit(&p->p_lock);
1682 	prsetprfpregs32(ttolwp(t), prfpregset);
1683 	mutex_enter(&p->p_lock);
1684 
1685 	return (0);
1686 }
1687 #endif	/* _SYSCALL32_IMPL */
1688 
1689 #if defined(__sparc)
1690 /* ARGSUSED */
1691 static int
1692 pr_setxregs(prnode_t *pnp, prxregset_t *prxregset)
1693 {
1694 	proc_t *p = pnp->pr_common->prc_proc;
1695 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1696 
1697 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1698 		thread_unlock(t);
1699 		return (EBUSY);
1700 	}
1701 	thread_unlock(t);
1702 
1703 	if (!prhasx(p))
1704 		return (EINVAL);	/* No extra register support */
1705 
1706 	/* drop p_lock while touching the lwp's stack */
1707 	mutex_exit(&p->p_lock);
1708 	prsetprxregs(ttolwp(t), (caddr_t)prxregset);
1709 	mutex_enter(&p->p_lock);
1710 
1711 	return (0);
1712 }
1713 
1714 static int
1715 pr_setasrs(prnode_t *pnp, asrset_t asrset)
1716 {
1717 	proc_t *p = pnp->pr_common->prc_proc;
1718 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1719 
1720 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1721 		thread_unlock(t);
1722 		return (EBUSY);
1723 	}
1724 	thread_unlock(t);
1725 
1726 	/* drop p_lock while touching the lwp's stack */
1727 	mutex_exit(&p->p_lock);
1728 	prsetasregs(ttolwp(t), asrset);
1729 	mutex_enter(&p->p_lock);
1730 
1731 	return (0);
1732 }
1733 #endif
1734 
1735 static int
1736 pr_setvaddr(prnode_t *pnp, caddr_t vaddr)
1737 {
1738 	proc_t *p = pnp->pr_common->prc_proc;
1739 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1740 
1741 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1742 		thread_unlock(t);
1743 		return (EBUSY);
1744 	}
1745 
1746 	/* drop p_lock while touching the lwp's stack */
1747 	thread_unlock(t);
1748 	mutex_exit(&p->p_lock);
1749 	prsvaddr(ttolwp(t), vaddr);
1750 	mutex_enter(&p->p_lock);
1751 
1752 	return (0);
1753 }
1754 
1755 void
1756 pr_sethold(prnode_t *pnp, sigset_t *sp)
1757 {
1758 	proc_t *p = pnp->pr_common->prc_proc;
1759 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1760 
1761 	schedctl_finish_sigblock(t);
1762 	sigutok(sp, &t->t_hold);
1763 	if (t->t_state == TS_SLEEP &&
1764 	    (t->t_flag & T_WAKEABLE) &&
1765 	    (fsig(&p->p_sig, t) || fsig(&t->t_sig, t)))
1766 		setrun_locked(t);
1767 	t->t_sig_check = 1;	/* so thread will see new holdmask */
1768 	thread_unlock(t);
1769 }
1770 
1771 void
1772 pr_setfault(proc_t *p, fltset_t *fltp)
1773 {
1774 	prassignset(&p->p_fltmask, fltp);
1775 	if (!prisempty(&p->p_fltmask))
1776 		p->p_proc_flag |= P_PR_TRACE;
1777 	else if (sigisempty(&p->p_sigmask)) {
1778 		user_t *up = PTOU(p);
1779 		if (up->u_systrap == 0)
1780 			p->p_proc_flag &= ~P_PR_TRACE;
1781 	}
1782 }
1783 
1784 static int
1785 pr_clearsig(prnode_t *pnp)
1786 {
1787 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1788 	klwp_t *lwp = ttolwp(t);
1789 
1790 	thread_unlock(t);
1791 	if (lwp->lwp_cursig == SIGKILL)
1792 		return (EBUSY);
1793 
1794 	/*
1795 	 * Discard current siginfo_t, if any.
1796 	 */
1797 	lwp->lwp_cursig = 0;
1798 	lwp->lwp_extsig = 0;
1799 	if (lwp->lwp_curinfo) {
1800 		siginfofree(lwp->lwp_curinfo);
1801 		lwp->lwp_curinfo = NULL;
1802 	}
1803 
1804 	return (0);
1805 }
1806 
1807 static int
1808 pr_clearflt(prnode_t *pnp)
1809 {
1810 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1811 
1812 	thread_unlock(t);
1813 	ttolwp(t)->lwp_curflt = 0;
1814 
1815 	return (0);
1816 }
1817 
1818 static int
1819 pr_watch(prnode_t *pnp, prwatch_t *pwp, int *unlocked)
1820 {
1821 	proc_t *p = pnp->pr_common->prc_proc;
1822 	struct as *as = p->p_as;
1823 	uintptr_t vaddr = pwp->pr_vaddr;
1824 	size_t size = pwp->pr_size;
1825 	int wflags = pwp->pr_wflags;
1826 	ulong_t newpage = 0;
1827 	struct watched_area *pwa;
1828 	int error;
1829 
1830 	*unlocked = 0;
1831 
1832 	/*
1833 	 * Can't apply to a system process.
1834 	 */
1835 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1836 		return (EBUSY);
1837 
1838 	/*
1839 	 * Verify that the address range does not wrap
1840 	 * and that only the proper flags were specified.
1841 	 */
1842 	if ((wflags & ~WA_TRAPAFTER) == 0)
1843 		size = 0;
1844 	if (vaddr + size < vaddr ||
1845 	    (wflags & ~(WA_READ|WA_WRITE|WA_EXEC|WA_TRAPAFTER)) != 0 ||
1846 	    ((wflags & ~WA_TRAPAFTER) != 0 && size == 0))
1847 		return (EINVAL);
1848 
1849 	/*
1850 	 * Don't let the address range go above as->a_userlimit.
1851 	 * There is no error here, just a limitation.
1852 	 */
1853 	if (vaddr >= (uintptr_t)as->a_userlimit)
1854 		return (0);
1855 	if (vaddr + size > (uintptr_t)as->a_userlimit)
1856 		size = (uintptr_t)as->a_userlimit - vaddr;
1857 
1858 	/*
1859 	 * Compute maximum number of pages this will add.
1860 	 */
1861 	if ((wflags & ~WA_TRAPAFTER) != 0) {
1862 		ulong_t pagespan = (vaddr + size) - (vaddr & PAGEMASK);
1863 		newpage = btopr(pagespan);
1864 		if (newpage > 2 * prnwatch)
1865 			return (E2BIG);
1866 	}
1867 
1868 	/*
1869 	 * Force the process to be fully stopped.
1870 	 */
1871 	if (p == curproc) {
1872 		prunlock(pnp);
1873 		while (holdwatch() != 0)
1874 			continue;
1875 		if ((error = prlock(pnp, ZNO)) != 0) {
1876 			continuelwps(p);
1877 			*unlocked = 1;
1878 			return (error);
1879 		}
1880 	} else {
1881 		pauselwps(p);
1882 		while (pr_allstopped(p, 0) > 0) {
1883 			/*
1884 			 * This cv/mutex pair is persistent even
1885 			 * if the process disappears after we
1886 			 * unmark it and drop p->p_lock.
1887 			 */
1888 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
1889 			kmutex_t *mp = &p->p_lock;
1890 
1891 			prunmark(p);
1892 			(void) cv_wait(cv, mp);
1893 			mutex_exit(mp);
1894 			if ((error = prlock(pnp, ZNO)) != 0) {
1895 				/*
1896 				 * Unpause the process if it exists.
1897 				 */
1898 				p = pr_p_lock(pnp);
1899 				mutex_exit(&pr_pidlock);
1900 				if (p != NULL) {
1901 					unpauselwps(p);
1902 					prunlock(pnp);
1903 				}
1904 				*unlocked = 1;
1905 				return (error);
1906 			}
1907 		}
1908 	}
1909 
1910 	/*
1911 	 * Drop p->p_lock in order to perform the rest of this.
1912 	 * The process is still locked with the P_PR_LOCK flag.
1913 	 */
1914 	mutex_exit(&p->p_lock);
1915 
1916 	pwa = kmem_alloc(sizeof (struct watched_area), KM_SLEEP);
1917 	pwa->wa_vaddr = (caddr_t)vaddr;
1918 	pwa->wa_eaddr = (caddr_t)vaddr + size;
1919 	pwa->wa_flags = (ulong_t)wflags;
1920 
1921 	error = ((pwa->wa_flags & ~WA_TRAPAFTER) == 0)?
1922 		clear_watched_area(p, pwa) :
1923 		set_watched_area(p, pwa);
1924 
1925 	if (p == curproc) {
1926 		setallwatch();
1927 		mutex_enter(&p->p_lock);
1928 		continuelwps(p);
1929 	} else {
1930 		mutex_enter(&p->p_lock);
1931 		unpauselwps(p);
1932 	}
1933 
1934 	return (error);
1935 }
1936 
1937 /* jobcontrol stopped, but with a /proc directed stop in effect */
1938 #define	JDSTOPPED(t)	\
1939 	((t)->t_state == TS_STOPPED && \
1940 	(t)->t_whystop == PR_JOBCONTROL && \
1941 	((t)->t_proc_flag & TP_PRSTOP))
1942 
1943 /*
1944  * pr_agent() creates the agent lwp. If the process is exiting while
1945  * we are creating an agent lwp, then exitlwps() waits until the
1946  * agent has been created using prbarrier().
1947  */
1948 static int
1949 pr_agent(prnode_t *pnp, prgregset_t prgregset, int *unlocked)
1950 {
1951 	proc_t *p = pnp->pr_common->prc_proc;
1952 	prcommon_t *pcp;
1953 	kthread_t *t;
1954 	kthread_t *ct;
1955 	klwp_t *clwp;
1956 	k_sigset_t smask;
1957 	int cid;
1958 	void *bufp = NULL;
1959 	int error;
1960 
1961 	*unlocked = 0;
1962 
1963 	/*
1964 	 * Cannot create the /proc agent lwp if :-
1965 	 * - the process is not fully stopped or directed to stop.
1966 	 * - there is an agent lwp already.
1967 	 * - the process has been killed.
1968 	 * - the process is exiting.
1969 	 * - it's a vfork(2) parent.
1970 	 */
1971 	t = prchoose(p);	/* returns locked thread */
1972 	ASSERT(t != NULL);
1973 
1974 	if ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t) && !JDSTOPPED(t)) ||
1975 	    p->p_agenttp != NULL ||
1976 	    (p->p_flag & (SKILLED | SEXITING | SVFWAIT))) {
1977 		thread_unlock(t);
1978 		return (EBUSY);
1979 	}
1980 
1981 	thread_unlock(t);
1982 	mutex_exit(&p->p_lock);
1983 
1984 	sigfillset(&smask);
1985 	sigdiffset(&smask, &cantmask);
1986 	clwp = lwp_create(lwp_rtt, NULL, 0, p, TS_STOPPED,
1987 	    t->t_pri, &smask, NOCLASS, 0);
1988 	if (clwp == NULL) {
1989 		mutex_enter(&p->p_lock);
1990 		return (ENOMEM);
1991 	}
1992 	prsetprregs(clwp, prgregset, 1);
1993 retry:
1994 	cid = t->t_cid;
1995 	(void) CL_ALLOC(&bufp, cid, KM_SLEEP);
1996 	mutex_enter(&p->p_lock);
1997 	if (cid != t->t_cid) {
1998 		/*
1999 		 * Someone just changed this thread's scheduling class,
2000 		 * so try pre-allocating the buffer again.  Hopefully we
2001 		 * don't hit this often.
2002 		 */
2003 		mutex_exit(&p->p_lock);
2004 		CL_FREE(cid, bufp);
2005 		goto retry;
2006 	}
2007 
2008 	clwp->lwp_ap = clwp->lwp_arg;
2009 	clwp->lwp_eosys = NORMALRETURN;
2010 	ct = lwptot(clwp);
2011 	ct->t_clfuncs = t->t_clfuncs;
2012 	CL_FORK(t, ct, bufp);
2013 	ct->t_cid = t->t_cid;
2014 	ct->t_proc_flag |= TP_PRSTOP;
2015 	/*
2016 	 * Setting t_sysnum to zero causes post_syscall()
2017 	 * to bypass all syscall checks and go directly to
2018 	 *	if (issig()) psig();
2019 	 * so that the agent lwp will stop in issig_forreal()
2020 	 * showing PR_REQUESTED.
2021 	 */
2022 	ct->t_sysnum = 0;
2023 	ct->t_post_sys = 1;
2024 	ct->t_sig_check = 1;
2025 	p->p_agenttp = ct;
2026 	ct->t_proc_flag &= ~TP_HOLDLWP;
2027 
2028 	pcp = pnp->pr_pcommon;
2029 	mutex_enter(&pcp->prc_mutex);
2030 
2031 	lwp_create_done(ct);
2032 
2033 	/*
2034 	 * Don't return until the agent is stopped on PR_REQUESTED.
2035 	 */
2036 
2037 	for (;;) {
2038 		prunlock(pnp);
2039 		*unlocked = 1;
2040 
2041 		/*
2042 		 * Wait for the agent to stop and notify us.
2043 		 * If we've been interrupted, return that information.
2044 		 */
2045 		error = pr_wait(pcp, NULL);
2046 		if (error == EINTR) {
2047 			error = 0;
2048 			break;
2049 		}
2050 
2051 		/*
2052 		 * Confirm that the agent LWP has stopped.
2053 		 */
2054 
2055 		if ((error = prlock(pnp, ZNO)) != 0)
2056 			break;
2057 		*unlocked = 0;
2058 
2059 		/*
2060 		 * Since we dropped the lock on the process, the agent
2061 		 * may have disappeared or changed. Grab the current
2062 		 * agent and check fail if it has disappeared.
2063 		 */
2064 		if ((ct = p->p_agenttp) == NULL) {
2065 			error = ENOENT;
2066 			break;
2067 		}
2068 
2069 		mutex_enter(&pcp->prc_mutex);
2070 		thread_lock(ct);
2071 
2072 		if (ISTOPPED(ct)) {
2073 			thread_unlock(ct);
2074 			mutex_exit(&pcp->prc_mutex);
2075 			break;
2076 		}
2077 
2078 		thread_unlock(ct);
2079 	}
2080 
2081 	return (error ? error : -1);
2082 }
2083 
2084 static int
2085 pr_rdwr(proc_t *p, enum uio_rw rw, priovec_t *pio)
2086 {
2087 	caddr_t base = (caddr_t)pio->pio_base;
2088 	size_t cnt = pio->pio_len;
2089 	uintptr_t offset = (uintptr_t)pio->pio_offset;
2090 	struct uio auio;
2091 	struct iovec aiov;
2092 	int error = 0;
2093 
2094 	if ((p->p_flag & SSYS) || p->p_as == &kas)
2095 		error = EIO;
2096 	else if ((base + cnt) < base || (offset + cnt) < offset)
2097 		error = EINVAL;
2098 	else if (cnt != 0) {
2099 		aiov.iov_base = base;
2100 		aiov.iov_len = cnt;
2101 
2102 		auio.uio_loffset = offset;
2103 		auio.uio_iov = &aiov;
2104 		auio.uio_iovcnt = 1;
2105 		auio.uio_resid = cnt;
2106 		auio.uio_segflg = UIO_USERSPACE;
2107 		auio.uio_llimit = (longlong_t)MAXOFFSET_T;
2108 		auio.uio_fmode = FREAD|FWRITE;
2109 		auio.uio_extflg = UIO_COPY_DEFAULT;
2110 
2111 		mutex_exit(&p->p_lock);
2112 		error = prusrio(p, rw, &auio, 0);
2113 		mutex_enter(&p->p_lock);
2114 
2115 		/*
2116 		 * We have no way to return the i/o count,
2117 		 * like read() or write() would do, so we
2118 		 * return an error if the i/o was truncated.
2119 		 */
2120 		if (auio.uio_resid != 0 && error == 0)
2121 			error = EIO;
2122 	}
2123 
2124 	return (error);
2125 }
2126 
2127 static int
2128 pr_scred(proc_t *p, prcred_t *prcred, cred_t *cr, boolean_t dogrps)
2129 {
2130 	kthread_t *t;
2131 	cred_t *oldcred;
2132 	cred_t *newcred;
2133 	uid_t oldruid;
2134 	int error;
2135 
2136 	if ((uint_t)prcred->pr_euid > MAXUID ||
2137 	    (uint_t)prcred->pr_ruid > MAXUID ||
2138 	    (uint_t)prcred->pr_suid > MAXUID ||
2139 	    (uint_t)prcred->pr_egid > MAXUID ||
2140 	    (uint_t)prcred->pr_rgid > MAXUID ||
2141 	    (uint_t)prcred->pr_sgid > MAXUID)
2142 		return (EINVAL);
2143 
2144 	if (dogrps) {
2145 		int ngrp = prcred->pr_ngroups;
2146 		int i;
2147 
2148 		if (ngrp < 0 || ngrp > ngroups_max)
2149 			return (EINVAL);
2150 
2151 		for (i = 0; i < ngrp; i++) {
2152 			if ((uint_t)prcred->pr_groups[i] > MAXUID)
2153 				return (EINVAL);
2154 		}
2155 	}
2156 
2157 	error = secpolicy_allow_setid(cr, prcred->pr_euid, B_FALSE);
2158 
2159 	if (error == 0 && prcred->pr_ruid != prcred->pr_euid)
2160 		error = secpolicy_allow_setid(cr, prcred->pr_ruid, B_FALSE);
2161 
2162 	if (error == 0 && prcred->pr_suid != prcred->pr_euid &&
2163 	    prcred->pr_suid != prcred->pr_ruid)
2164 		error = secpolicy_allow_setid(cr, prcred->pr_suid, B_FALSE);
2165 
2166 	if (error)
2167 		return (error);
2168 
2169 	mutex_exit(&p->p_lock);
2170 
2171 	/* hold old cred so it doesn't disappear while we dup it */
2172 	mutex_enter(&p->p_crlock);
2173 	crhold(oldcred = p->p_cred);
2174 	mutex_exit(&p->p_crlock);
2175 	newcred = crdup(oldcred);
2176 	oldruid = crgetruid(oldcred);
2177 	crfree(oldcred);
2178 
2179 	/* Error checking done above */
2180 	(void) crsetresuid(newcred, prcred->pr_ruid, prcred->pr_euid,
2181 		prcred->pr_suid);
2182 	(void) crsetresgid(newcred, prcred->pr_rgid, prcred->pr_egid,
2183 		prcred->pr_sgid);
2184 
2185 	if (dogrps) {
2186 		(void) crsetgroups(newcred, prcred->pr_ngroups,
2187 		    prcred->pr_groups);
2188 
2189 	}
2190 
2191 	mutex_enter(&p->p_crlock);
2192 	oldcred = p->p_cred;
2193 	p->p_cred = newcred;
2194 	mutex_exit(&p->p_crlock);
2195 	crfree(oldcred);
2196 
2197 	/*
2198 	 * Keep count of processes per uid consistent.
2199 	 */
2200 	if (oldruid != prcred->pr_ruid) {
2201 		zoneid_t zoneid = crgetzoneid(newcred);
2202 
2203 		mutex_enter(&pidlock);
2204 		upcount_dec(oldruid, zoneid);
2205 		upcount_inc(prcred->pr_ruid, zoneid);
2206 		mutex_exit(&pidlock);
2207 	}
2208 
2209 	/*
2210 	 * Broadcast the cred change to the threads.
2211 	 */
2212 	mutex_enter(&p->p_lock);
2213 	t = p->p_tlist;
2214 	do {
2215 		t->t_pre_sys = 1; /* so syscall will get new cred */
2216 	} while ((t = t->t_forw) != p->p_tlist);
2217 
2218 	return (0);
2219 }
2220 
2221 /*
2222  * Change process credentials to specified zone.  Used to temporarily
2223  * set a process to run in the global zone; only transitions between
2224  * the process's actual zone and the global zone are allowed.
2225  */
2226 static int
2227 pr_szoneid(proc_t *p, zoneid_t zoneid, cred_t *cr)
2228 {
2229 	kthread_t *t;
2230 	cred_t *oldcred;
2231 	cred_t *newcred;
2232 	zone_t *zptr;
2233 
2234 	if (secpolicy_zone_config(cr) != 0)
2235 		return (EPERM);
2236 	if (zoneid != GLOBAL_ZONEID && zoneid != p->p_zone->zone_id)
2237 		return (EINVAL);
2238 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
2239 		return (EINVAL);
2240 	mutex_exit(&p->p_lock);
2241 	mutex_enter(&p->p_crlock);
2242 	crhold(oldcred = p->p_cred);
2243 	mutex_exit(&p->p_crlock);
2244 	newcred = crdup(oldcred);
2245 	crfree(oldcred);
2246 
2247 	crsetzone(newcred, zptr);
2248 	zone_rele(zptr);
2249 
2250 	mutex_enter(&p->p_crlock);
2251 	oldcred = p->p_cred;
2252 	p->p_cred = newcred;
2253 	mutex_exit(&p->p_crlock);
2254 	crfree(oldcred);
2255 
2256 	/*
2257 	 * Broadcast the cred change to the threads.
2258 	 */
2259 	mutex_enter(&p->p_lock);
2260 	t = p->p_tlist;
2261 	do {
2262 		t->t_pre_sys = 1;	/* so syscall will get new cred */
2263 	} while ((t = t->t_forw) != p->p_tlist);
2264 
2265 	return (0);
2266 }
2267 
2268 static int
2269 pr_spriv(proc_t *p, prpriv_t *prpriv, cred_t *cr)
2270 {
2271 	kthread_t *t;
2272 	int err;
2273 
2274 	ASSERT(MUTEX_HELD(&p->p_lock));
2275 
2276 	if ((err = priv_pr_spriv(p, prpriv, cr)) == 0) {
2277 		/*
2278 		 * Broadcast the cred change to the threads.
2279 		 */
2280 		t = p->p_tlist;
2281 		do {
2282 			t->t_pre_sys = 1; /* so syscall will get new cred */
2283 		} while ((t = t->t_forw) != p->p_tlist);
2284 	}
2285 
2286 	return (err);
2287 }
2288 
2289 /*
2290  * Return -1 if the process is the parent of a vfork(1) whose child has yet to
2291  * terminate or perform an exec(2).
2292  *
2293  * Returns 0 if the process is fully stopped except for the current thread (if
2294  * we are operating on our own process), 1 otherwise.
2295  *
2296  * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
2297  * See holdwatch() for details.
2298  */
2299 int
2300 pr_allstopped(proc_t *p, int watchstop)
2301 {
2302 	kthread_t *t;
2303 	int rv = 0;
2304 
2305 	ASSERT(MUTEX_HELD(&p->p_lock));
2306 
2307 	if (p->p_flag & SVFWAIT)	/* waiting for vfork'd child to exec */
2308 		return (-1);
2309 
2310 	if ((t = p->p_tlist) != NULL) {
2311 		do {
2312 			if (t == curthread || VSTOPPED(t) ||
2313 			    (watchstop && (t->t_proc_flag & TP_WATCHSTOP)))
2314 				continue;
2315 			thread_lock(t);
2316 			switch (t->t_state) {
2317 			case TS_ZOMB:
2318 			case TS_STOPPED:
2319 				break;
2320 			case TS_SLEEP:
2321 				if (!(t->t_flag & T_WAKEABLE) ||
2322 				    t->t_wchan0 == NULL)
2323 					rv = 1;
2324 				break;
2325 			default:
2326 				rv = 1;
2327 				break;
2328 			}
2329 			thread_unlock(t);
2330 		} while (rv == 0 && (t = t->t_forw) != p->p_tlist);
2331 	}
2332 
2333 	return (rv);
2334 }
2335 
2336 /*
2337  * Cause all lwps in the process to pause (for watchpoint operations).
2338  */
2339 static void
2340 pauselwps(proc_t *p)
2341 {
2342 	kthread_t *t;
2343 
2344 	ASSERT(MUTEX_HELD(&p->p_lock));
2345 	ASSERT(p != curproc);
2346 
2347 	if ((t = p->p_tlist) != NULL) {
2348 		do {
2349 			thread_lock(t);
2350 			t->t_proc_flag |= TP_PAUSE;
2351 			aston(t);
2352 			if (t->t_state == TS_SLEEP &&
2353 			    (t->t_flag & T_WAKEABLE)) {
2354 				if (t->t_wchan0 == NULL)
2355 					setrun_locked(t);
2356 			}
2357 			prpokethread(t);
2358 			thread_unlock(t);
2359 		} while ((t = t->t_forw) != p->p_tlist);
2360 	}
2361 }
2362 
2363 /*
2364  * undo the effects of pauselwps()
2365  */
2366 static void
2367 unpauselwps(proc_t *p)
2368 {
2369 	kthread_t *t;
2370 
2371 	ASSERT(MUTEX_HELD(&p->p_lock));
2372 	ASSERT(p != curproc);
2373 
2374 	if ((t = p->p_tlist) != NULL) {
2375 		do {
2376 			thread_lock(t);
2377 			t->t_proc_flag &= ~TP_PAUSE;
2378 			if (t->t_state == TS_STOPPED) {
2379 				t->t_schedflag |= TS_UNPAUSE;
2380 				t->t_dtrace_stop = 0;
2381 				setrun_locked(t);
2382 			}
2383 			thread_unlock(t);
2384 		} while ((t = t->t_forw) != p->p_tlist);
2385 	}
2386 }
2387 
2388 /*
2389  * Cancel all watched areas.  Called from prclose().
2390  */
2391 proc_t *
2392 pr_cancel_watch(prnode_t *pnp)
2393 {
2394 	proc_t *p = pnp->pr_pcommon->prc_proc;
2395 	struct as *as;
2396 	kthread_t *t;
2397 
2398 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2399 
2400 	if (!pr_watch_active(p))
2401 		return (p);
2402 
2403 	/*
2404 	 * Pause the process before dealing with the watchpoints.
2405 	 */
2406 	if (p == curproc) {
2407 		prunlock(pnp);
2408 		while (holdwatch() != 0)
2409 			continue;
2410 		p = pr_p_lock(pnp);
2411 		mutex_exit(&pr_pidlock);
2412 		ASSERT(p == curproc);
2413 	} else {
2414 		pauselwps(p);
2415 		while (p != NULL && pr_allstopped(p, 0) > 0) {
2416 			/*
2417 			 * This cv/mutex pair is persistent even
2418 			 * if the process disappears after we
2419 			 * unmark it and drop p->p_lock.
2420 			 */
2421 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
2422 			kmutex_t *mp = &p->p_lock;
2423 
2424 			prunmark(p);
2425 			(void) cv_wait(cv, mp);
2426 			mutex_exit(mp);
2427 			p = pr_p_lock(pnp);  /* NULL if process disappeared */
2428 			mutex_exit(&pr_pidlock);
2429 		}
2430 	}
2431 
2432 	if (p == NULL)		/* the process disappeared */
2433 		return (NULL);
2434 
2435 	ASSERT(p == pnp->pr_pcommon->prc_proc);
2436 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2437 
2438 	if (pr_watch_active(p)) {
2439 		pr_free_watchpoints(p);
2440 		if ((t = p->p_tlist) != NULL) {
2441 			do {
2442 				watch_disable(t);
2443 
2444 			} while ((t = t->t_forw) != p->p_tlist);
2445 		}
2446 	}
2447 
2448 	if ((as = p->p_as) != NULL) {
2449 		avl_tree_t *tree;
2450 		struct watched_page *pwp;
2451 
2452 		/*
2453 		 * If this is the parent of a vfork, the watched page
2454 		 * list has been moved temporarily to p->p_wpage.
2455 		 */
2456 		if (avl_numnodes(&p->p_wpage) != 0)
2457 			tree = &p->p_wpage;
2458 		else
2459 			tree = &as->a_wpage;
2460 
2461 		mutex_exit(&p->p_lock);
2462 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2463 
2464 		for (pwp = avl_first(tree); pwp != NULL;
2465 		    pwp = AVL_NEXT(tree, pwp)) {
2466 			pwp->wp_read = 0;
2467 			pwp->wp_write = 0;
2468 			pwp->wp_exec = 0;
2469 			if ((pwp->wp_flags & WP_SETPROT) == 0) {
2470 				pwp->wp_flags |= WP_SETPROT;
2471 				pwp->wp_prot = pwp->wp_oprot;
2472 				pwp->wp_list = p->p_wprot;
2473 				p->p_wprot = pwp;
2474 			}
2475 		}
2476 
2477 		AS_LOCK_EXIT(as, &as->a_lock);
2478 		mutex_enter(&p->p_lock);
2479 	}
2480 
2481 	/*
2482 	 * Unpause the process now.
2483 	 */
2484 	if (p == curproc)
2485 		continuelwps(p);
2486 	else
2487 		unpauselwps(p);
2488 
2489 	return (p);
2490 }
2491