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