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