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