xref: /illumos-gate/usr/src/uts/common/fs/proc/prcontrol.c (revision 031681dd1958c6fe4ff0cf45f01b184de255680c)
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 2007 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 
1038 		/* Move the thread from wait queue to run queue */
1039 		if (ISWAITING(t))
1040 			setrun_locked(t);
1041 
1042 		if (ISWAKEABLE(t)) {
1043 			if (t->t_wchan0 == NULL)
1044 				setrun_locked(t);
1045 			else if (!VSTOPPED(t)) {
1046 				/*
1047 				 * Mark it virtually stopped.
1048 				 */
1049 				t->t_proc_flag |= TP_PRVSTOP;
1050 				notify = 1;
1051 			}
1052 		}
1053 		/*
1054 		 * force the thread into the kernel
1055 		 * if it is not already there.
1056 		 */
1057 		prpokethread(t);
1058 		thread_unlock(t);
1059 		if (notify &&
1060 		    (vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace) != NULL)
1061 			prnotify(vp);
1062 		if (pcp->prc_flags & PRC_LWP)
1063 			break;
1064 	} while ((t = t->t_forw) != p->p_tlist);
1065 
1066 	/*
1067 	 * We do this just in case the thread we asked
1068 	 * to stop is in holdlwps() (called from cfork()).
1069 	 */
1070 	cv_broadcast(&p->p_holdlwps);
1071 }
1072 
1073 /*
1074  * Sleep until the lwp stops, but cooperate with
1075  * jobcontrol:  Don't wake up if the lwp is stopped
1076  * due to the default action of a jobcontrol stop signal.
1077  * If this is the process file descriptor, sleep
1078  * until all of the process's lwps stop.
1079  */
1080 int
1081 pr_wait_stop(prnode_t *pnp, time_t timeo)
1082 {
1083 	prcommon_t *pcp = pnp->pr_common;
1084 	proc_t *p = pcp->prc_proc;
1085 	timestruc_t rqtime;
1086 	timestruc_t *rqtp = NULL;
1087 	kthread_t *t;
1088 	int error;
1089 
1090 	if (timeo > 0) {	/* millisecond timeout */
1091 		/*
1092 		 * Determine the precise future time of the requested timeout.
1093 		 */
1094 		timestruc_t now;
1095 
1096 		gethrestime(&now);
1097 		rqtp = &rqtime;
1098 		rqtp->tv_sec = timeo / MILLISEC;
1099 		rqtp->tv_nsec = (timeo % MILLISEC) * MICROSEC;
1100 		timespecadd(rqtp, &now);
1101 	}
1102 
1103 	if (pcp->prc_flags & PRC_LWP) {	/* lwp file descriptor */
1104 		t = pcp->prc_thread;
1105 		ASSERT(t != NULL);
1106 		thread_lock(t);
1107 		while (!ISTOPPED(t) && !VSTOPPED(t)) {
1108 			thread_unlock(t);
1109 			mutex_enter(&pcp->prc_mutex);
1110 			prunlock(pnp);
1111 			error = pr_wait(pcp, rqtp);
1112 			if (error)	/* -1 is timeout */
1113 				return (error);
1114 			if ((error = prlock(pnp, ZNO)) != 0)
1115 				return (error);
1116 			ASSERT(p == pcp->prc_proc);
1117 			ASSERT(t == pcp->prc_thread);
1118 			thread_lock(t);
1119 		}
1120 		thread_unlock(t);
1121 	} else {			/* process file descriptor */
1122 		t = prchoose(p);	/* returns locked thread */
1123 		ASSERT(t != NULL);
1124 		ASSERT(MUTEX_HELD(&p->p_lock));
1125 		while ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t)) ||
1126 		    (p->p_flag & SEXITLWPS)) {
1127 			thread_unlock(t);
1128 			mutex_enter(&pcp->prc_mutex);
1129 			prunlock(pnp);
1130 			error = pr_wait(pcp, rqtp);
1131 			if (error)	/* -1 is timeout */
1132 				return (error);
1133 			if ((error = prlock(pnp, ZNO)) != 0)
1134 				return (error);
1135 			ASSERT(p == pcp->prc_proc);
1136 			t = prchoose(p);	/* returns locked t */
1137 			ASSERT(t != NULL);
1138 		}
1139 		thread_unlock(t);
1140 	}
1141 
1142 	ASSERT(!(pcp->prc_flags & PRC_DESTROY) && p->p_stat != SZOMB &&
1143 	    t != NULL && t->t_state != TS_ZOMB);
1144 
1145 	return (0);
1146 }
1147 
1148 int
1149 pr_setrun(prnode_t *pnp, ulong_t flags)
1150 {
1151 	prcommon_t *pcp = pnp->pr_common;
1152 	proc_t *p = pcp->prc_proc;
1153 	kthread_t *t;
1154 	klwp_t *lwp;
1155 
1156 	/*
1157 	 * Cannot set an lwp running if it is not stopped.
1158 	 * Also, no lwp other than the /proc agent lwp can
1159 	 * be set running so long as the /proc agent lwp exists.
1160 	 */
1161 	t = pr_thread(pnp);	/* returns locked thread */
1162 	if ((!ISTOPPED(t) && !VSTOPPED(t) &&
1163 	    !(t->t_proc_flag & TP_PRSTOP)) ||
1164 	    (p->p_agenttp != NULL &&
1165 	    (t != p->p_agenttp || !(pcp->prc_flags & PRC_LWP)))) {
1166 		thread_unlock(t);
1167 		return (EBUSY);
1168 	}
1169 	thread_unlock(t);
1170 	if (flags & ~(PRCSIG|PRCFAULT|PRSTEP|PRSTOP|PRSABORT))
1171 		return (EINVAL);
1172 	lwp = ttolwp(t);
1173 	if ((flags & PRCSIG) && lwp->lwp_cursig != SIGKILL) {
1174 		/*
1175 		 * Discard current siginfo_t, if any.
1176 		 */
1177 		lwp->lwp_cursig = 0;
1178 		lwp->lwp_extsig = 0;
1179 		if (lwp->lwp_curinfo) {
1180 			siginfofree(lwp->lwp_curinfo);
1181 			lwp->lwp_curinfo = NULL;
1182 		}
1183 	}
1184 	if (flags & PRCFAULT)
1185 		lwp->lwp_curflt = 0;
1186 	/*
1187 	 * We can't hold p->p_lock when we touch the lwp's registers.
1188 	 * It may be swapped out and we will get a page fault.
1189 	 */
1190 	if (flags & PRSTEP) {
1191 		mutex_exit(&p->p_lock);
1192 		prstep(lwp, 0);
1193 		mutex_enter(&p->p_lock);
1194 	}
1195 	if (flags & PRSTOP) {
1196 		t->t_proc_flag |= TP_PRSTOP;
1197 		t->t_sig_check = 1;	/* do ISSIG */
1198 	}
1199 	if (flags & PRSABORT)
1200 		lwp->lwp_sysabort = 1;
1201 	thread_lock(t);
1202 	if ((pcp->prc_flags & PRC_LWP) || (flags & (PRSTEP|PRSTOP))) {
1203 		/*
1204 		 * Here, we are dealing with a single lwp.
1205 		 */
1206 		if (ISTOPPED(t)) {
1207 			t->t_schedflag |= TS_PSTART;
1208 			t->t_dtrace_stop = 0;
1209 			setrun_locked(t);
1210 		} else if (flags & PRSABORT) {
1211 			t->t_proc_flag &=
1212 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1213 			setrun_locked(t);
1214 		} else if (!(flags & PRSTOP)) {
1215 			t->t_proc_flag &=
1216 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1217 		}
1218 		thread_unlock(t);
1219 	} else {
1220 		/*
1221 		 * Here, we are dealing with the whole process.
1222 		 */
1223 		if (ISTOPPED(t)) {
1224 			/*
1225 			 * The representative lwp is stopped on an event
1226 			 * of interest.  We demote it to PR_REQUESTED and
1227 			 * choose another representative lwp.  If the new
1228 			 * representative lwp is not stopped on an event of
1229 			 * interest (other than PR_REQUESTED), we set the
1230 			 * whole process running, else we leave the process
1231 			 * stopped showing the next event of interest.
1232 			 */
1233 			kthread_t *tx = NULL;
1234 
1235 			if (!(flags & PRSABORT) &&
1236 			    t->t_whystop == PR_SYSENTRY &&
1237 			    t->t_whatstop == SYS_lwp_exit)
1238 				tx = t;		/* remember the exiting lwp */
1239 			t->t_whystop = PR_REQUESTED;
1240 			t->t_whatstop = 0;
1241 			thread_unlock(t);
1242 			t = prchoose(p);	/* returns locked t */
1243 			ASSERT(ISTOPPED(t) || VSTOPPED(t));
1244 			if (VSTOPPED(t) ||
1245 			    t->t_whystop == PR_REQUESTED) {
1246 				thread_unlock(t);
1247 				allsetrun(p);
1248 			} else {
1249 				thread_unlock(t);
1250 				/*
1251 				 * As a special case, if the old representative
1252 				 * lwp was stopped on entry to _lwp_exit()
1253 				 * (and we are not aborting the system call),
1254 				 * we set the old representative lwp running.
1255 				 * We do this so that the next process stop
1256 				 * will find the exiting lwp gone.
1257 				 */
1258 				if (tx != NULL) {
1259 					thread_lock(tx);
1260 					tx->t_schedflag |= TS_PSTART;
1261 					t->t_dtrace_stop = 0;
1262 					setrun_locked(tx);
1263 					thread_unlock(tx);
1264 				}
1265 			}
1266 		} else {
1267 			/*
1268 			 * No event of interest; set all of the lwps running.
1269 			 */
1270 			if (flags & PRSABORT) {
1271 				t->t_proc_flag &=
1272 				    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1273 				setrun_locked(t);
1274 			}
1275 			thread_unlock(t);
1276 			allsetrun(p);
1277 		}
1278 	}
1279 	return (0);
1280 }
1281 
1282 /*
1283  * Wait until process/lwp stops or until timer expires.
1284  * Return EINTR for an interruption, -1 for timeout, else 0.
1285  */
1286 int
1287 pr_wait(prcommon_t *pcp,	/* prcommon referring to process/lwp */
1288 	timestruc_t *ts)	/* absolute time of timeout, if any */
1289 {
1290 	int rval;
1291 
1292 	ASSERT(MUTEX_HELD(&pcp->prc_mutex));
1293 	rval = cv_waituntil_sig(&pcp->prc_wait, &pcp->prc_mutex, ts);
1294 	mutex_exit(&pcp->prc_mutex);
1295 	switch (rval) {
1296 	case 0:
1297 		return (EINTR);
1298 	case -1:
1299 		return (-1);
1300 	default:
1301 		return (0);
1302 	}
1303 }
1304 
1305 /*
1306  * Make all threads in the process runnable.
1307  */
1308 void
1309 allsetrun(proc_t *p)
1310 {
1311 	kthread_t *t;
1312 
1313 	ASSERT(MUTEX_HELD(&p->p_lock));
1314 
1315 	if ((t = p->p_tlist) != NULL) {
1316 		do {
1317 			thread_lock(t);
1318 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1319 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1320 			if (ISTOPPED(t)) {
1321 				t->t_schedflag |= TS_PSTART;
1322 				t->t_dtrace_stop = 0;
1323 				setrun_locked(t);
1324 			}
1325 			thread_unlock(t);
1326 		} while ((t = t->t_forw) != p->p_tlist);
1327 	}
1328 }
1329 
1330 /*
1331  * Wait for the process to die.
1332  * We do this after sending SIGKILL because we know it will
1333  * die soon and we want subsequent operations to return ENOENT.
1334  */
1335 void
1336 pr_wait_die(prnode_t *pnp)
1337 {
1338 	proc_t *p;
1339 
1340 	mutex_enter(&pidlock);
1341 	while ((p = pnp->pr_common->prc_proc) != NULL && p->p_stat != SZOMB) {
1342 		if (!cv_wait_sig(&p->p_srwchan_cv, &pidlock))
1343 			break;
1344 	}
1345 	mutex_exit(&pidlock);
1346 }
1347 
1348 static void
1349 pr_settrace(proc_t *p, sigset_t *sp)
1350 {
1351 	prdelset(sp, SIGKILL);
1352 	prassignset(&p->p_sigmask, sp);
1353 	if (!sigisempty(&p->p_sigmask))
1354 		p->p_proc_flag |= P_PR_TRACE;
1355 	else if (prisempty(&p->p_fltmask)) {
1356 		user_t *up = PTOU(p);
1357 		if (up->u_systrap == 0)
1358 			p->p_proc_flag &= ~P_PR_TRACE;
1359 	}
1360 }
1361 
1362 int
1363 pr_setsig(prnode_t *pnp, siginfo_t *sip)
1364 {
1365 	int sig = sip->si_signo;
1366 	prcommon_t *pcp = pnp->pr_common;
1367 	proc_t *p = pcp->prc_proc;
1368 	kthread_t *t;
1369 	klwp_t *lwp;
1370 	int error = 0;
1371 
1372 	t = pr_thread(pnp);	/* returns locked thread */
1373 	thread_unlock(t);
1374 	lwp = ttolwp(t);
1375 	if (sig < 0 || sig >= NSIG)
1376 		/* Zero allowed here */
1377 		error = EINVAL;
1378 	else if (lwp->lwp_cursig == SIGKILL)
1379 		/* "can't happen", but just in case */
1380 		error = EBUSY;
1381 	else if ((lwp->lwp_cursig = (uchar_t)sig) == 0) {
1382 		lwp->lwp_extsig = 0;
1383 		/*
1384 		 * Discard current siginfo_t, if any.
1385 		 */
1386 		if (lwp->lwp_curinfo) {
1387 			siginfofree(lwp->lwp_curinfo);
1388 			lwp->lwp_curinfo = NULL;
1389 		}
1390 	} else {
1391 		kthread_t *tx;
1392 		sigqueue_t *sqp;
1393 
1394 		/* drop p_lock to do kmem_alloc(KM_SLEEP) */
1395 		mutex_exit(&p->p_lock);
1396 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1397 		mutex_enter(&p->p_lock);
1398 
1399 		if (lwp->lwp_curinfo == NULL)
1400 			lwp->lwp_curinfo = sqp;
1401 		else
1402 			kmem_free(sqp, sizeof (sigqueue_t));
1403 		/*
1404 		 * Copy contents of info to current siginfo_t.
1405 		 */
1406 		bcopy(sip, &lwp->lwp_curinfo->sq_info,
1407 		    sizeof (lwp->lwp_curinfo->sq_info));
1408 		/*
1409 		 * Prevent contents published by si_zoneid-unaware /proc
1410 		 * consumers from being incorrectly filtered.  Because
1411 		 * an uninitialized si_zoneid is the same as
1412 		 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1413 		 * process in a non-global zone with a siginfo which
1414 		 * appears to come from the global zone.
1415 		 */
1416 		if (SI_FROMUSER(sip) && sip->si_zoneid == 0)
1417 			lwp->lwp_curinfo->sq_info.si_zoneid =
1418 			    p->p_zone->zone_id;
1419 		/*
1420 		 * Side-effects for SIGKILL and jobcontrol signals.
1421 		 */
1422 		if (sig == SIGKILL) {
1423 			p->p_flag |= SKILLED;
1424 			p->p_flag &= ~SEXTKILLED;
1425 		} else if (sig == SIGCONT) {
1426 			p->p_flag |= SSCONT;
1427 			sigdelq(p, NULL, SIGSTOP);
1428 			sigdelq(p, NULL, SIGTSTP);
1429 			sigdelq(p, NULL, SIGTTOU);
1430 			sigdelq(p, NULL, SIGTTIN);
1431 			sigdiffset(&p->p_sig, &stopdefault);
1432 			sigdiffset(&p->p_extsig, &stopdefault);
1433 			if ((tx = p->p_tlist) != NULL) {
1434 				do {
1435 					sigdelq(p, tx, SIGSTOP);
1436 					sigdelq(p, tx, SIGTSTP);
1437 					sigdelq(p, tx, SIGTTOU);
1438 					sigdelq(p, tx, SIGTTIN);
1439 					sigdiffset(&tx->t_sig, &stopdefault);
1440 					sigdiffset(&tx->t_extsig, &stopdefault);
1441 				} while ((tx = tx->t_forw) != p->p_tlist);
1442 			}
1443 		} else if (sigismember(&stopdefault, sig)) {
1444 			if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
1445 			    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
1446 				p->p_flag &= ~SSCONT;
1447 			sigdelq(p, NULL, SIGCONT);
1448 			sigdelset(&p->p_sig, SIGCONT);
1449 			sigdelset(&p->p_extsig, SIGCONT);
1450 			if ((tx = p->p_tlist) != NULL) {
1451 				do {
1452 					sigdelq(p, tx, SIGCONT);
1453 					sigdelset(&tx->t_sig, SIGCONT);
1454 					sigdelset(&tx->t_extsig, SIGCONT);
1455 				} while ((tx = tx->t_forw) != p->p_tlist);
1456 			}
1457 		}
1458 		thread_lock(t);
1459 		if (ISWAKEABLE(t) || ISWAITING(t)) {
1460 			/* Set signalled sleeping/waiting 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 (ISWAKEABLE(t) &&
1766 	    (fsig(&p->p_sig, t) || fsig(&t->t_sig, t)))
1767 		setrun_locked(t);
1768 	t->t_sig_check = 1;	/* so thread will see new holdmask */
1769 	thread_unlock(t);
1770 }
1771 
1772 void
1773 pr_setfault(proc_t *p, fltset_t *fltp)
1774 {
1775 	prassignset(&p->p_fltmask, fltp);
1776 	if (!prisempty(&p->p_fltmask))
1777 		p->p_proc_flag |= P_PR_TRACE;
1778 	else if (sigisempty(&p->p_sigmask)) {
1779 		user_t *up = PTOU(p);
1780 		if (up->u_systrap == 0)
1781 			p->p_proc_flag &= ~P_PR_TRACE;
1782 	}
1783 }
1784 
1785 static int
1786 pr_clearsig(prnode_t *pnp)
1787 {
1788 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1789 	klwp_t *lwp = ttolwp(t);
1790 
1791 	thread_unlock(t);
1792 	if (lwp->lwp_cursig == SIGKILL)
1793 		return (EBUSY);
1794 
1795 	/*
1796 	 * Discard current siginfo_t, if any.
1797 	 */
1798 	lwp->lwp_cursig = 0;
1799 	lwp->lwp_extsig = 0;
1800 	if (lwp->lwp_curinfo) {
1801 		siginfofree(lwp->lwp_curinfo);
1802 		lwp->lwp_curinfo = NULL;
1803 	}
1804 
1805 	return (0);
1806 }
1807 
1808 static int
1809 pr_clearflt(prnode_t *pnp)
1810 {
1811 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1812 
1813 	thread_unlock(t);
1814 	ttolwp(t)->lwp_curflt = 0;
1815 
1816 	return (0);
1817 }
1818 
1819 static int
1820 pr_watch(prnode_t *pnp, prwatch_t *pwp, int *unlocked)
1821 {
1822 	proc_t *p = pnp->pr_common->prc_proc;
1823 	struct as *as = p->p_as;
1824 	uintptr_t vaddr = pwp->pr_vaddr;
1825 	size_t size = pwp->pr_size;
1826 	int wflags = pwp->pr_wflags;
1827 	ulong_t newpage = 0;
1828 	struct watched_area *pwa;
1829 	int error;
1830 
1831 	*unlocked = 0;
1832 
1833 	/*
1834 	 * Can't apply to a system process.
1835 	 */
1836 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1837 		return (EBUSY);
1838 
1839 	/*
1840 	 * Verify that the address range does not wrap
1841 	 * and that only the proper flags were specified.
1842 	 */
1843 	if ((wflags & ~WA_TRAPAFTER) == 0)
1844 		size = 0;
1845 	if (vaddr + size < vaddr ||
1846 	    (wflags & ~(WA_READ|WA_WRITE|WA_EXEC|WA_TRAPAFTER)) != 0 ||
1847 	    ((wflags & ~WA_TRAPAFTER) != 0 && size == 0))
1848 		return (EINVAL);
1849 
1850 	/*
1851 	 * Don't let the address range go above as->a_userlimit.
1852 	 * There is no error here, just a limitation.
1853 	 */
1854 	if (vaddr >= (uintptr_t)as->a_userlimit)
1855 		return (0);
1856 	if (vaddr + size > (uintptr_t)as->a_userlimit)
1857 		size = (uintptr_t)as->a_userlimit - vaddr;
1858 
1859 	/*
1860 	 * Compute maximum number of pages this will add.
1861 	 */
1862 	if ((wflags & ~WA_TRAPAFTER) != 0) {
1863 		ulong_t pagespan = (vaddr + size) - (vaddr & PAGEMASK);
1864 		newpage = btopr(pagespan);
1865 		if (newpage > 2 * prnwatch)
1866 			return (E2BIG);
1867 	}
1868 
1869 	/*
1870 	 * Force the process to be fully stopped.
1871 	 */
1872 	if (p == curproc) {
1873 		prunlock(pnp);
1874 		while (holdwatch() != 0)
1875 			continue;
1876 		if ((error = prlock(pnp, ZNO)) != 0) {
1877 			continuelwps(p);
1878 			*unlocked = 1;
1879 			return (error);
1880 		}
1881 	} else {
1882 		pauselwps(p);
1883 		while (pr_allstopped(p, 0) > 0) {
1884 			/*
1885 			 * This cv/mutex pair is persistent even
1886 			 * if the process disappears after we
1887 			 * unmark it and drop p->p_lock.
1888 			 */
1889 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
1890 			kmutex_t *mp = &p->p_lock;
1891 
1892 			prunmark(p);
1893 			(void) cv_wait(cv, mp);
1894 			mutex_exit(mp);
1895 			if ((error = prlock(pnp, ZNO)) != 0) {
1896 				/*
1897 				 * Unpause the process if it exists.
1898 				 */
1899 				p = pr_p_lock(pnp);
1900 				mutex_exit(&pr_pidlock);
1901 				if (p != NULL) {
1902 					unpauselwps(p);
1903 					prunlock(pnp);
1904 				}
1905 				*unlocked = 1;
1906 				return (error);
1907 			}
1908 		}
1909 	}
1910 
1911 	/*
1912 	 * Drop p->p_lock in order to perform the rest of this.
1913 	 * The process is still locked with the P_PR_LOCK flag.
1914 	 */
1915 	mutex_exit(&p->p_lock);
1916 
1917 	pwa = kmem_alloc(sizeof (struct watched_area), KM_SLEEP);
1918 	pwa->wa_vaddr = (caddr_t)vaddr;
1919 	pwa->wa_eaddr = (caddr_t)vaddr + size;
1920 	pwa->wa_flags = (ulong_t)wflags;
1921 
1922 	error = ((pwa->wa_flags & ~WA_TRAPAFTER) == 0)?
1923 		clear_watched_area(p, pwa) :
1924 		set_watched_area(p, pwa);
1925 
1926 	if (p == curproc) {
1927 		setallwatch();
1928 		mutex_enter(&p->p_lock);
1929 		continuelwps(p);
1930 	} else {
1931 		mutex_enter(&p->p_lock);
1932 		unpauselwps(p);
1933 	}
1934 
1935 	return (error);
1936 }
1937 
1938 /* jobcontrol stopped, but with a /proc directed stop in effect */
1939 #define	JDSTOPPED(t)	\
1940 	((t)->t_state == TS_STOPPED && \
1941 	(t)->t_whystop == PR_JOBCONTROL && \
1942 	((t)->t_proc_flag & TP_PRSTOP))
1943 
1944 /*
1945  * pr_agent() creates the agent lwp. If the process is exiting while
1946  * we are creating an agent lwp, then exitlwps() waits until the
1947  * agent has been created using prbarrier().
1948  */
1949 static int
1950 pr_agent(prnode_t *pnp, prgregset_t prgregset, int *unlocked)
1951 {
1952 	proc_t *p = pnp->pr_common->prc_proc;
1953 	prcommon_t *pcp;
1954 	kthread_t *t;
1955 	kthread_t *ct;
1956 	klwp_t *clwp;
1957 	k_sigset_t smask;
1958 	int cid;
1959 	void *bufp = NULL;
1960 	int error;
1961 
1962 	*unlocked = 0;
1963 
1964 	/*
1965 	 * Cannot create the /proc agent lwp if :-
1966 	 * - the process is not fully stopped or directed to stop.
1967 	 * - there is an agent lwp already.
1968 	 * - the process has been killed.
1969 	 * - the process is exiting.
1970 	 * - it's a vfork(2) parent.
1971 	 */
1972 	t = prchoose(p);	/* returns locked thread */
1973 	ASSERT(t != NULL);
1974 
1975 	if ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t) && !JDSTOPPED(t)) ||
1976 	    p->p_agenttp != NULL ||
1977 	    (p->p_flag & (SKILLED | SEXITING | 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);
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 	zoneid_t oldzoneid;
2235 
2236 	if (secpolicy_zone_config(cr) != 0)
2237 		return (EPERM);
2238 	if (zoneid != GLOBAL_ZONEID && zoneid != p->p_zone->zone_id)
2239 		return (EINVAL);
2240 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
2241 		return (EINVAL);
2242 	mutex_exit(&p->p_lock);
2243 	mutex_enter(&p->p_crlock);
2244 	oldcred = p->p_cred;
2245 	crhold(oldcred);
2246 	mutex_exit(&p->p_crlock);
2247 	newcred = crdup(oldcred);
2248 	oldzoneid = crgetzoneid(oldcred);
2249 	crfree(oldcred);
2250 
2251 	crsetzone(newcred, zptr);
2252 	zone_rele(zptr);
2253 
2254 	mutex_enter(&p->p_crlock);
2255 	oldcred = p->p_cred;
2256 	p->p_cred = newcred;
2257 	mutex_exit(&p->p_crlock);
2258 	crfree(oldcred);
2259 
2260 	/*
2261 	 * The target process is changing zones (according to its cred), so
2262 	 * update the per-zone upcounts, which are based on process creds.
2263 	 */
2264 	if (oldzoneid != zoneid) {
2265 		uid_t ruid = crgetruid(newcred);
2266 
2267 		mutex_enter(&pidlock);
2268 		upcount_dec(ruid, oldzoneid);
2269 		upcount_inc(ruid, zoneid);
2270 		mutex_exit(&pidlock);
2271 	}
2272 	/*
2273 	 * Broadcast the cred change to the threads.
2274 	 */
2275 	mutex_enter(&p->p_lock);
2276 	t = p->p_tlist;
2277 	do {
2278 		t->t_pre_sys = 1;	/* so syscall will get new cred */
2279 	} while ((t = t->t_forw) != p->p_tlist);
2280 
2281 	return (0);
2282 }
2283 
2284 static int
2285 pr_spriv(proc_t *p, prpriv_t *prpriv, cred_t *cr)
2286 {
2287 	kthread_t *t;
2288 	int err;
2289 
2290 	ASSERT(MUTEX_HELD(&p->p_lock));
2291 
2292 	if ((err = priv_pr_spriv(p, prpriv, cr)) == 0) {
2293 		/*
2294 		 * Broadcast the cred change to the threads.
2295 		 */
2296 		t = p->p_tlist;
2297 		do {
2298 			t->t_pre_sys = 1; /* so syscall will get new cred */
2299 		} while ((t = t->t_forw) != p->p_tlist);
2300 	}
2301 
2302 	return (err);
2303 }
2304 
2305 /*
2306  * Return -1 if the process is the parent of a vfork(1) whose child has yet to
2307  * terminate or perform an exec(2).
2308  *
2309  * Returns 0 if the process is fully stopped except for the current thread (if
2310  * we are operating on our own process), 1 otherwise.
2311  *
2312  * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
2313  * See holdwatch() for details.
2314  */
2315 int
2316 pr_allstopped(proc_t *p, int watchstop)
2317 {
2318 	kthread_t *t;
2319 	int rv = 0;
2320 
2321 	ASSERT(MUTEX_HELD(&p->p_lock));
2322 
2323 	if (p->p_flag & SVFWAIT)	/* waiting for vfork'd child to exec */
2324 		return (-1);
2325 
2326 	if ((t = p->p_tlist) != NULL) {
2327 		do {
2328 			if (t == curthread || VSTOPPED(t) ||
2329 			    (watchstop && (t->t_proc_flag & TP_WATCHSTOP)))
2330 				continue;
2331 			thread_lock(t);
2332 			switch (t->t_state) {
2333 			case TS_ZOMB:
2334 			case TS_STOPPED:
2335 				break;
2336 			case TS_SLEEP:
2337 				if (!(t->t_flag & T_WAKEABLE) ||
2338 				    t->t_wchan0 == NULL)
2339 					rv = 1;
2340 				break;
2341 			default:
2342 				rv = 1;
2343 				break;
2344 			}
2345 			thread_unlock(t);
2346 		} while (rv == 0 && (t = t->t_forw) != p->p_tlist);
2347 	}
2348 
2349 	return (rv);
2350 }
2351 
2352 /*
2353  * Cause all lwps in the process to pause (for watchpoint operations).
2354  */
2355 static void
2356 pauselwps(proc_t *p)
2357 {
2358 	kthread_t *t;
2359 
2360 	ASSERT(MUTEX_HELD(&p->p_lock));
2361 	ASSERT(p != curproc);
2362 
2363 	if ((t = p->p_tlist) != NULL) {
2364 		do {
2365 			thread_lock(t);
2366 			t->t_proc_flag |= TP_PAUSE;
2367 			aston(t);
2368 			if ((ISWAKEABLE(t) && (t->t_wchan0 == NULL)) ||
2369 			    ISWAITING(t)) {
2370 				setrun_locked(t);
2371 			}
2372 			prpokethread(t);
2373 			thread_unlock(t);
2374 		} while ((t = t->t_forw) != p->p_tlist);
2375 	}
2376 }
2377 
2378 /*
2379  * undo the effects of pauselwps()
2380  */
2381 static void
2382 unpauselwps(proc_t *p)
2383 {
2384 	kthread_t *t;
2385 
2386 	ASSERT(MUTEX_HELD(&p->p_lock));
2387 	ASSERT(p != curproc);
2388 
2389 	if ((t = p->p_tlist) != NULL) {
2390 		do {
2391 			thread_lock(t);
2392 			t->t_proc_flag &= ~TP_PAUSE;
2393 			if (t->t_state == TS_STOPPED) {
2394 				t->t_schedflag |= TS_UNPAUSE;
2395 				t->t_dtrace_stop = 0;
2396 				setrun_locked(t);
2397 			}
2398 			thread_unlock(t);
2399 		} while ((t = t->t_forw) != p->p_tlist);
2400 	}
2401 }
2402 
2403 /*
2404  * Cancel all watched areas.  Called from prclose().
2405  */
2406 proc_t *
2407 pr_cancel_watch(prnode_t *pnp)
2408 {
2409 	proc_t *p = pnp->pr_pcommon->prc_proc;
2410 	struct as *as;
2411 	kthread_t *t;
2412 
2413 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2414 
2415 	if (!pr_watch_active(p))
2416 		return (p);
2417 
2418 	/*
2419 	 * Pause the process before dealing with the watchpoints.
2420 	 */
2421 	if (p == curproc) {
2422 		prunlock(pnp);
2423 		while (holdwatch() != 0)
2424 			continue;
2425 		p = pr_p_lock(pnp);
2426 		mutex_exit(&pr_pidlock);
2427 		ASSERT(p == curproc);
2428 	} else {
2429 		pauselwps(p);
2430 		while (p != NULL && pr_allstopped(p, 0) > 0) {
2431 			/*
2432 			 * This cv/mutex pair is persistent even
2433 			 * if the process disappears after we
2434 			 * unmark it and drop p->p_lock.
2435 			 */
2436 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
2437 			kmutex_t *mp = &p->p_lock;
2438 
2439 			prunmark(p);
2440 			(void) cv_wait(cv, mp);
2441 			mutex_exit(mp);
2442 			p = pr_p_lock(pnp);  /* NULL if process disappeared */
2443 			mutex_exit(&pr_pidlock);
2444 		}
2445 	}
2446 
2447 	if (p == NULL)		/* the process disappeared */
2448 		return (NULL);
2449 
2450 	ASSERT(p == pnp->pr_pcommon->prc_proc);
2451 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2452 
2453 	if (pr_watch_active(p)) {
2454 		pr_free_watchpoints(p);
2455 		if ((t = p->p_tlist) != NULL) {
2456 			do {
2457 				watch_disable(t);
2458 
2459 			} while ((t = t->t_forw) != p->p_tlist);
2460 		}
2461 	}
2462 
2463 	if ((as = p->p_as) != NULL) {
2464 		avl_tree_t *tree;
2465 		struct watched_page *pwp;
2466 
2467 		/*
2468 		 * If this is the parent of a vfork, the watched page
2469 		 * list has been moved temporarily to p->p_wpage.
2470 		 */
2471 		if (avl_numnodes(&p->p_wpage) != 0)
2472 			tree = &p->p_wpage;
2473 		else
2474 			tree = &as->a_wpage;
2475 
2476 		mutex_exit(&p->p_lock);
2477 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2478 
2479 		for (pwp = avl_first(tree); pwp != NULL;
2480 		    pwp = AVL_NEXT(tree, pwp)) {
2481 			pwp->wp_read = 0;
2482 			pwp->wp_write = 0;
2483 			pwp->wp_exec = 0;
2484 			if ((pwp->wp_flags & WP_SETPROT) == 0) {
2485 				pwp->wp_flags |= WP_SETPROT;
2486 				pwp->wp_prot = pwp->wp_oprot;
2487 				pwp->wp_list = p->p_wprot;
2488 				p->p_wprot = pwp;
2489 			}
2490 		}
2491 
2492 		AS_LOCK_EXIT(as, &as->a_lock);
2493 		mutex_enter(&p->p_lock);
2494 	}
2495 
2496 	/*
2497 	 * Unpause the process now.
2498 	 */
2499 	if (p == curproc)
2500 		continuelwps(p);
2501 	else
2502 		unpauselwps(p);
2503 
2504 	return (p);
2505 }
2506