xref: /titanic_50/usr/src/uts/common/fs/proc/prcontrol.c (revision f48205be61a214698b763ff550ab9e657525104c)
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 	int timecheck = 0;
1088 	kthread_t *t;
1089 	int error;
1090 
1091 	if (timeo > 0) {	/* millisecond timeout */
1092 		/*
1093 		 * Determine the precise future time of the requested timeout.
1094 		 */
1095 		timestruc_t now;
1096 
1097 		timecheck = timechanged;
1098 		gethrestime(&now);
1099 		rqtp = &rqtime;
1100 		rqtp->tv_sec = timeo / MILLISEC;
1101 		rqtp->tv_nsec = (timeo % MILLISEC) * MICROSEC;
1102 		timespecadd(rqtp, &now);
1103 	}
1104 
1105 	if (pcp->prc_flags & PRC_LWP) {	/* lwp file descriptor */
1106 		t = pcp->prc_thread;
1107 		ASSERT(t != NULL);
1108 		thread_lock(t);
1109 		while (!ISTOPPED(t) && !VSTOPPED(t)) {
1110 			thread_unlock(t);
1111 			mutex_enter(&pcp->prc_mutex);
1112 			prunlock(pnp);
1113 			error = pr_wait(pcp, rqtp, timecheck);
1114 			if (error)	/* -1 is timeout */
1115 				return (error);
1116 			if ((error = prlock(pnp, ZNO)) != 0)
1117 				return (error);
1118 			ASSERT(p == pcp->prc_proc);
1119 			ASSERT(t == pcp->prc_thread);
1120 			thread_lock(t);
1121 		}
1122 		thread_unlock(t);
1123 	} else {			/* process file descriptor */
1124 		t = prchoose(p);	/* returns locked thread */
1125 		ASSERT(t != NULL);
1126 		ASSERT(MUTEX_HELD(&p->p_lock));
1127 		while ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t)) ||
1128 		    (p->p_flag & SEXITLWPS)) {
1129 			thread_unlock(t);
1130 			mutex_enter(&pcp->prc_mutex);
1131 			prunlock(pnp);
1132 			error = pr_wait(pcp, rqtp, timecheck);
1133 			if (error)	/* -1 is timeout */
1134 				return (error);
1135 			if ((error = prlock(pnp, ZNO)) != 0)
1136 				return (error);
1137 			ASSERT(p == pcp->prc_proc);
1138 			t = prchoose(p);	/* returns locked t */
1139 			ASSERT(t != NULL);
1140 		}
1141 		thread_unlock(t);
1142 	}
1143 
1144 	ASSERT(!(pcp->prc_flags & PRC_DESTROY) && p->p_stat != SZOMB &&
1145 	    t != NULL && t->t_state != TS_ZOMB);
1146 
1147 	return (0);
1148 }
1149 
1150 int
1151 pr_setrun(prnode_t *pnp, ulong_t flags)
1152 {
1153 	prcommon_t *pcp = pnp->pr_common;
1154 	proc_t *p = pcp->prc_proc;
1155 	kthread_t *t;
1156 	klwp_t *lwp;
1157 
1158 	/*
1159 	 * Cannot set an lwp running if it is not stopped.
1160 	 * Also, no lwp other than the /proc agent lwp can
1161 	 * be set running so long as the /proc agent lwp exists.
1162 	 */
1163 	t = pr_thread(pnp);	/* returns locked thread */
1164 	if ((!ISTOPPED(t) && !VSTOPPED(t) &&
1165 	    !(t->t_proc_flag & TP_PRSTOP)) ||
1166 	    (p->p_agenttp != NULL &&
1167 	    (t != p->p_agenttp || !(pcp->prc_flags & PRC_LWP)))) {
1168 		thread_unlock(t);
1169 		return (EBUSY);
1170 	}
1171 	thread_unlock(t);
1172 	if (flags & ~(PRCSIG|PRCFAULT|PRSTEP|PRSTOP|PRSABORT))
1173 		return (EINVAL);
1174 	lwp = ttolwp(t);
1175 	if ((flags & PRCSIG) && lwp->lwp_cursig != SIGKILL) {
1176 		/*
1177 		 * Discard current siginfo_t, if any.
1178 		 */
1179 		lwp->lwp_cursig = 0;
1180 		lwp->lwp_extsig = 0;
1181 		if (lwp->lwp_curinfo) {
1182 			siginfofree(lwp->lwp_curinfo);
1183 			lwp->lwp_curinfo = NULL;
1184 		}
1185 	}
1186 	if (flags & PRCFAULT)
1187 		lwp->lwp_curflt = 0;
1188 	/*
1189 	 * We can't hold p->p_lock when we touch the lwp's registers.
1190 	 * It may be swapped out and we will get a page fault.
1191 	 */
1192 	if (flags & PRSTEP) {
1193 		mutex_exit(&p->p_lock);
1194 		prstep(lwp, 0);
1195 		mutex_enter(&p->p_lock);
1196 	}
1197 	if (flags & PRSTOP) {
1198 		t->t_proc_flag |= TP_PRSTOP;
1199 		t->t_sig_check = 1;	/* do ISSIG */
1200 	}
1201 	if (flags & PRSABORT)
1202 		lwp->lwp_sysabort = 1;
1203 	thread_lock(t);
1204 	if ((pcp->prc_flags & PRC_LWP) || (flags & (PRSTEP|PRSTOP))) {
1205 		/*
1206 		 * Here, we are dealing with a single lwp.
1207 		 */
1208 		if (ISTOPPED(t)) {
1209 			t->t_schedflag |= TS_PSTART;
1210 			t->t_dtrace_stop = 0;
1211 			setrun_locked(t);
1212 		} else if (flags & PRSABORT) {
1213 			t->t_proc_flag &=
1214 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1215 			setrun_locked(t);
1216 		} else if (!(flags & PRSTOP)) {
1217 			t->t_proc_flag &=
1218 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1219 		}
1220 		thread_unlock(t);
1221 	} else {
1222 		/*
1223 		 * Here, we are dealing with the whole process.
1224 		 */
1225 		if (ISTOPPED(t)) {
1226 			/*
1227 			 * The representative lwp is stopped on an event
1228 			 * of interest.  We demote it to PR_REQUESTED and
1229 			 * choose another representative lwp.  If the new
1230 			 * representative lwp is not stopped on an event of
1231 			 * interest (other than PR_REQUESTED), we set the
1232 			 * whole process running, else we leave the process
1233 			 * stopped showing the next event of interest.
1234 			 */
1235 			kthread_t *tx = NULL;
1236 
1237 			if (!(flags & PRSABORT) &&
1238 			    t->t_whystop == PR_SYSENTRY &&
1239 			    t->t_whatstop == SYS_lwp_exit)
1240 				tx = t;		/* remember the exiting lwp */
1241 			t->t_whystop = PR_REQUESTED;
1242 			t->t_whatstop = 0;
1243 			thread_unlock(t);
1244 			t = prchoose(p);	/* returns locked t */
1245 			ASSERT(ISTOPPED(t) || VSTOPPED(t));
1246 			if (VSTOPPED(t) ||
1247 			    t->t_whystop == PR_REQUESTED) {
1248 				thread_unlock(t);
1249 				allsetrun(p);
1250 			} else {
1251 				thread_unlock(t);
1252 				/*
1253 				 * As a special case, if the old representative
1254 				 * lwp was stopped on entry to _lwp_exit()
1255 				 * (and we are not aborting the system call),
1256 				 * we set the old representative lwp running.
1257 				 * We do this so that the next process stop
1258 				 * will find the exiting lwp gone.
1259 				 */
1260 				if (tx != NULL) {
1261 					thread_lock(tx);
1262 					tx->t_schedflag |= TS_PSTART;
1263 					t->t_dtrace_stop = 0;
1264 					setrun_locked(tx);
1265 					thread_unlock(tx);
1266 				}
1267 			}
1268 		} else {
1269 			/*
1270 			 * No event of interest; set all of the lwps running.
1271 			 */
1272 			if (flags & PRSABORT) {
1273 				t->t_proc_flag &=
1274 				    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1275 				setrun_locked(t);
1276 			}
1277 			thread_unlock(t);
1278 			allsetrun(p);
1279 		}
1280 	}
1281 	return (0);
1282 }
1283 
1284 /*
1285  * Wait until process/lwp stops or until timer expires.
1286  * Return EINTR for an interruption, -1 for timeout, else 0.
1287  */
1288 int
1289 pr_wait(prcommon_t *pcp,	/* prcommon referring to process/lwp */
1290 	timestruc_t *ts,	/* absolute time of timeout, if any */
1291 	int timecheck)
1292 {
1293 	int rval;
1294 
1295 	ASSERT(MUTEX_HELD(&pcp->prc_mutex));
1296 	rval = cv_waituntil_sig(&pcp->prc_wait, &pcp->prc_mutex, ts, timecheck);
1297 	mutex_exit(&pcp->prc_mutex);
1298 	switch (rval) {
1299 	case 0:
1300 		return (EINTR);
1301 	case -1:
1302 		return (-1);
1303 	default:
1304 		return (0);
1305 	}
1306 }
1307 
1308 /*
1309  * Make all threads in the process runnable.
1310  */
1311 void
1312 allsetrun(proc_t *p)
1313 {
1314 	kthread_t *t;
1315 
1316 	ASSERT(MUTEX_HELD(&p->p_lock));
1317 
1318 	if ((t = p->p_tlist) != NULL) {
1319 		do {
1320 			thread_lock(t);
1321 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1322 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1323 			if (ISTOPPED(t)) {
1324 				t->t_schedflag |= TS_PSTART;
1325 				t->t_dtrace_stop = 0;
1326 				setrun_locked(t);
1327 			}
1328 			thread_unlock(t);
1329 		} while ((t = t->t_forw) != p->p_tlist);
1330 	}
1331 }
1332 
1333 /*
1334  * Wait for the process to die.
1335  * We do this after sending SIGKILL because we know it will
1336  * die soon and we want subsequent operations to return ENOENT.
1337  */
1338 void
1339 pr_wait_die(prnode_t *pnp)
1340 {
1341 	proc_t *p;
1342 
1343 	mutex_enter(&pidlock);
1344 	while ((p = pnp->pr_common->prc_proc) != NULL && p->p_stat != SZOMB) {
1345 		if (!cv_wait_sig(&p->p_srwchan_cv, &pidlock))
1346 			break;
1347 	}
1348 	mutex_exit(&pidlock);
1349 }
1350 
1351 static void
1352 pr_settrace(proc_t *p, sigset_t *sp)
1353 {
1354 	prdelset(sp, SIGKILL);
1355 	prassignset(&p->p_sigmask, sp);
1356 	if (!sigisempty(&p->p_sigmask))
1357 		p->p_proc_flag |= P_PR_TRACE;
1358 	else if (prisempty(&p->p_fltmask)) {
1359 		user_t *up = PTOU(p);
1360 		if (up->u_systrap == 0)
1361 			p->p_proc_flag &= ~P_PR_TRACE;
1362 	}
1363 }
1364 
1365 int
1366 pr_setsig(prnode_t *pnp, siginfo_t *sip)
1367 {
1368 	int sig = sip->si_signo;
1369 	prcommon_t *pcp = pnp->pr_common;
1370 	proc_t *p = pcp->prc_proc;
1371 	kthread_t *t;
1372 	klwp_t *lwp;
1373 	int error = 0;
1374 
1375 	t = pr_thread(pnp);	/* returns locked thread */
1376 	thread_unlock(t);
1377 	lwp = ttolwp(t);
1378 	if (sig < 0 || sig >= NSIG)
1379 		/* Zero allowed here */
1380 		error = EINVAL;
1381 	else if (lwp->lwp_cursig == SIGKILL)
1382 		/* "can't happen", but just in case */
1383 		error = EBUSY;
1384 	else if ((lwp->lwp_cursig = (uchar_t)sig) == 0) {
1385 		lwp->lwp_extsig = 0;
1386 		/*
1387 		 * Discard current siginfo_t, if any.
1388 		 */
1389 		if (lwp->lwp_curinfo) {
1390 			siginfofree(lwp->lwp_curinfo);
1391 			lwp->lwp_curinfo = NULL;
1392 		}
1393 	} else {
1394 		kthread_t *tx;
1395 		sigqueue_t *sqp;
1396 
1397 		/* drop p_lock to do kmem_alloc(KM_SLEEP) */
1398 		mutex_exit(&p->p_lock);
1399 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1400 		mutex_enter(&p->p_lock);
1401 
1402 		if (lwp->lwp_curinfo == NULL)
1403 			lwp->lwp_curinfo = sqp;
1404 		else
1405 			kmem_free(sqp, sizeof (sigqueue_t));
1406 		/*
1407 		 * Copy contents of info to current siginfo_t.
1408 		 */
1409 		bcopy(sip, &lwp->lwp_curinfo->sq_info,
1410 		    sizeof (lwp->lwp_curinfo->sq_info));
1411 		/*
1412 		 * Prevent contents published by si_zoneid-unaware /proc
1413 		 * consumers from being incorrectly filtered.  Because
1414 		 * an uninitialized si_zoneid is the same as
1415 		 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1416 		 * process in a non-global zone with a siginfo which
1417 		 * appears to come from the global zone.
1418 		 */
1419 		if (SI_FROMUSER(sip) && sip->si_zoneid == 0)
1420 			lwp->lwp_curinfo->sq_info.si_zoneid =
1421 			    p->p_zone->zone_id;
1422 		/*
1423 		 * Side-effects for SIGKILL and jobcontrol signals.
1424 		 */
1425 		if (sig == SIGKILL) {
1426 			p->p_flag |= SKILLED;
1427 			p->p_flag &= ~SEXTKILLED;
1428 		} else if (sig == SIGCONT) {
1429 			p->p_flag |= SSCONT;
1430 			sigdelq(p, NULL, SIGSTOP);
1431 			sigdelq(p, NULL, SIGTSTP);
1432 			sigdelq(p, NULL, SIGTTOU);
1433 			sigdelq(p, NULL, SIGTTIN);
1434 			sigdiffset(&p->p_sig, &stopdefault);
1435 			sigdiffset(&p->p_extsig, &stopdefault);
1436 			if ((tx = p->p_tlist) != NULL) {
1437 				do {
1438 					sigdelq(p, tx, SIGSTOP);
1439 					sigdelq(p, tx, SIGTSTP);
1440 					sigdelq(p, tx, SIGTTOU);
1441 					sigdelq(p, tx, SIGTTIN);
1442 					sigdiffset(&tx->t_sig, &stopdefault);
1443 					sigdiffset(&tx->t_extsig, &stopdefault);
1444 				} while ((tx = tx->t_forw) != p->p_tlist);
1445 			}
1446 		} else if (sigismember(&stopdefault, sig)) {
1447 			if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
1448 			    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
1449 				p->p_flag &= ~SSCONT;
1450 			sigdelq(p, NULL, SIGCONT);
1451 			sigdelset(&p->p_sig, SIGCONT);
1452 			sigdelset(&p->p_extsig, SIGCONT);
1453 			if ((tx = p->p_tlist) != NULL) {
1454 				do {
1455 					sigdelq(p, tx, SIGCONT);
1456 					sigdelset(&tx->t_sig, SIGCONT);
1457 					sigdelset(&tx->t_extsig, SIGCONT);
1458 				} while ((tx = tx->t_forw) != p->p_tlist);
1459 			}
1460 		}
1461 		thread_lock(t);
1462 		if (ISWAKEABLE(t) || ISWAITING(t)) {
1463 			/* Set signalled sleeping/waiting lwp running */
1464 			setrun_locked(t);
1465 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL) {
1466 			/* If SIGKILL, set stopped lwp running */
1467 			p->p_stopsig = 0;
1468 			t->t_schedflag |= TS_XSTART | TS_PSTART;
1469 			t->t_dtrace_stop = 0;
1470 			setrun_locked(t);
1471 		}
1472 		t->t_sig_check = 1;	/* so ISSIG will be done */
1473 		thread_unlock(t);
1474 		/*
1475 		 * More jobcontrol side-effects.
1476 		 */
1477 		if (sig == SIGCONT && (tx = p->p_tlist) != NULL) {
1478 			p->p_stopsig = 0;
1479 			do {
1480 				thread_lock(tx);
1481 				if (tx->t_state == TS_STOPPED &&
1482 				    tx->t_whystop == PR_JOBCONTROL) {
1483 					tx->t_schedflag |= TS_XSTART;
1484 					setrun_locked(tx);
1485 				}
1486 				thread_unlock(tx);
1487 			} while ((tx = tx->t_forw) != p->p_tlist);
1488 		}
1489 	}
1490 	return (error);
1491 }
1492 
1493 int
1494 pr_kill(prnode_t *pnp, int sig, cred_t *cr)
1495 {
1496 	prcommon_t *pcp = pnp->pr_common;
1497 	proc_t *p = pcp->prc_proc;
1498 	k_siginfo_t info;
1499 
1500 	if (sig <= 0 || sig >= NSIG)
1501 		return (EINVAL);
1502 
1503 	bzero(&info, sizeof (info));
1504 	info.si_signo = sig;
1505 	info.si_code = SI_USER;
1506 	info.si_pid = curproc->p_pid;
1507 	info.si_ctid = PRCTID(curproc);
1508 	info.si_zoneid = getzoneid();
1509 	info.si_uid = crgetruid(cr);
1510 	sigaddq(p, (pcp->prc_flags & PRC_LWP)?
1511 	    pcp->prc_thread : NULL, &info, KM_NOSLEEP);
1512 
1513 	return (0);
1514 }
1515 
1516 int
1517 pr_unkill(prnode_t *pnp, int sig)
1518 {
1519 	prcommon_t *pcp = pnp->pr_common;
1520 	proc_t *p = pcp->prc_proc;
1521 	sigqueue_t *infop = NULL;
1522 
1523 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL)
1524 		return (EINVAL);
1525 
1526 	if (pcp->prc_flags & PRC_LWP)
1527 		sigdeq(p, pcp->prc_thread, sig, &infop);
1528 	else
1529 		sigdeq(p, NULL, sig, &infop);
1530 
1531 	if (infop)
1532 		siginfofree(infop);
1533 
1534 	return (0);
1535 }
1536 
1537 int
1538 pr_nice(proc_t *p, int nice, cred_t *cr)
1539 {
1540 	kthread_t *t;
1541 	int err;
1542 	int error = 0;
1543 
1544 	t = p->p_tlist;
1545 	do {
1546 		ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1547 		err = CL_DONICE(t, cr, nice, (int *)NULL);
1548 		if (error == 0)
1549 			error = err;
1550 	} while ((t = t->t_forw) != p->p_tlist);
1551 
1552 	return (error);
1553 }
1554 
1555 void
1556 pr_setentryexit(proc_t *p, sysset_t *sysset, int entry)
1557 {
1558 	user_t *up = PTOU(p);
1559 
1560 	if (entry) {
1561 		prassignset(&up->u_entrymask, sysset);
1562 	} else {
1563 		prassignset(&up->u_exitmask, sysset);
1564 	}
1565 	if (!prisempty(&up->u_entrymask) ||
1566 	    !prisempty(&up->u_exitmask)) {
1567 		up->u_systrap = 1;
1568 		p->p_proc_flag |= P_PR_TRACE;
1569 		set_proc_sys(p);	/* set pre and post-sys flags */
1570 	} else {
1571 		up->u_systrap = 0;
1572 		if (sigisempty(&p->p_sigmask) &&
1573 		    prisempty(&p->p_fltmask))
1574 			p->p_proc_flag &= ~P_PR_TRACE;
1575 	}
1576 }
1577 
1578 #define	ALLFLAGS	\
1579 	(PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
1580 
1581 int
1582 pr_set(proc_t *p, long flags)
1583 {
1584 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1585 		return (EBUSY);
1586 
1587 	if (flags & ~ALLFLAGS)
1588 		return (EINVAL);
1589 
1590 	if (flags & PR_FORK)
1591 		p->p_proc_flag |= P_PR_FORK;
1592 	if (flags & PR_RLC)
1593 		p->p_proc_flag |= P_PR_RUNLCL;
1594 	if (flags & PR_KLC)
1595 		p->p_proc_flag |= P_PR_KILLCL;
1596 	if (flags & PR_ASYNC)
1597 		p->p_proc_flag |= P_PR_ASYNC;
1598 	if (flags & PR_BPTADJ)
1599 		p->p_proc_flag |= P_PR_BPTADJ;
1600 	if (flags & PR_MSACCT)
1601 		if ((p->p_flag & SMSACCT) == 0)
1602 			estimate_msacct(p->p_tlist, gethrtime());
1603 	if (flags & PR_MSFORK)
1604 		p->p_flag |= SMSFORK;
1605 	if (flags & PR_PTRACE) {
1606 		p->p_proc_flag |= P_PR_PTRACE;
1607 		/* ptraced process must die if parent dead */
1608 		if (p->p_ppid == 1)
1609 			sigtoproc(p, NULL, SIGKILL);
1610 	}
1611 
1612 	return (0);
1613 }
1614 
1615 int
1616 pr_unset(proc_t *p, long flags)
1617 {
1618 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1619 		return (EBUSY);
1620 
1621 	if (flags & ~ALLFLAGS)
1622 		return (EINVAL);
1623 
1624 	if (flags & PR_FORK)
1625 		p->p_proc_flag &= ~P_PR_FORK;
1626 	if (flags & PR_RLC)
1627 		p->p_proc_flag &= ~P_PR_RUNLCL;
1628 	if (flags & PR_KLC)
1629 		p->p_proc_flag &= ~P_PR_KILLCL;
1630 	if (flags & PR_ASYNC)
1631 		p->p_proc_flag &= ~P_PR_ASYNC;
1632 	if (flags & PR_BPTADJ)
1633 		p->p_proc_flag &= ~P_PR_BPTADJ;
1634 	if (flags & PR_MSACCT)
1635 		disable_msacct(p);
1636 	if (flags & PR_MSFORK)
1637 		p->p_flag &= ~SMSFORK;
1638 	if (flags & PR_PTRACE)
1639 		p->p_proc_flag &= ~P_PR_PTRACE;
1640 
1641 	return (0);
1642 }
1643 
1644 static int
1645 pr_setfpregs(prnode_t *pnp, prfpregset_t *prfpregset)
1646 {
1647 	proc_t *p = pnp->pr_common->prc_proc;
1648 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1649 
1650 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1651 		thread_unlock(t);
1652 		return (EBUSY);
1653 	}
1654 	if (!prhasfp()) {
1655 		thread_unlock(t);
1656 		return (EINVAL);	/* No FP support */
1657 	}
1658 
1659 	/* drop p_lock while touching the lwp's stack */
1660 	thread_unlock(t);
1661 	mutex_exit(&p->p_lock);
1662 	prsetprfpregs(ttolwp(t), prfpregset);
1663 	mutex_enter(&p->p_lock);
1664 
1665 	return (0);
1666 }
1667 
1668 #ifdef	_SYSCALL32_IMPL
1669 static int
1670 pr_setfpregs32(prnode_t *pnp, prfpregset32_t *prfpregset)
1671 {
1672 	proc_t *p = pnp->pr_common->prc_proc;
1673 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1674 
1675 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1676 		thread_unlock(t);
1677 		return (EBUSY);
1678 	}
1679 	if (!prhasfp()) {
1680 		thread_unlock(t);
1681 		return (EINVAL);	/* No FP support */
1682 	}
1683 
1684 	/* drop p_lock while touching the lwp's stack */
1685 	thread_unlock(t);
1686 	mutex_exit(&p->p_lock);
1687 	prsetprfpregs32(ttolwp(t), prfpregset);
1688 	mutex_enter(&p->p_lock);
1689 
1690 	return (0);
1691 }
1692 #endif	/* _SYSCALL32_IMPL */
1693 
1694 #if defined(__sparc)
1695 /* ARGSUSED */
1696 static int
1697 pr_setxregs(prnode_t *pnp, prxregset_t *prxregset)
1698 {
1699 	proc_t *p = pnp->pr_common->prc_proc;
1700 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1701 
1702 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1703 		thread_unlock(t);
1704 		return (EBUSY);
1705 	}
1706 	thread_unlock(t);
1707 
1708 	if (!prhasx(p))
1709 		return (EINVAL);	/* No extra register support */
1710 
1711 	/* drop p_lock while touching the lwp's stack */
1712 	mutex_exit(&p->p_lock);
1713 	prsetprxregs(ttolwp(t), (caddr_t)prxregset);
1714 	mutex_enter(&p->p_lock);
1715 
1716 	return (0);
1717 }
1718 
1719 static int
1720 pr_setasrs(prnode_t *pnp, asrset_t asrset)
1721 {
1722 	proc_t *p = pnp->pr_common->prc_proc;
1723 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1724 
1725 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1726 		thread_unlock(t);
1727 		return (EBUSY);
1728 	}
1729 	thread_unlock(t);
1730 
1731 	/* drop p_lock while touching the lwp's stack */
1732 	mutex_exit(&p->p_lock);
1733 	prsetasregs(ttolwp(t), asrset);
1734 	mutex_enter(&p->p_lock);
1735 
1736 	return (0);
1737 }
1738 #endif
1739 
1740 static int
1741 pr_setvaddr(prnode_t *pnp, caddr_t vaddr)
1742 {
1743 	proc_t *p = pnp->pr_common->prc_proc;
1744 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1745 
1746 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1747 		thread_unlock(t);
1748 		return (EBUSY);
1749 	}
1750 
1751 	/* drop p_lock while touching the lwp's stack */
1752 	thread_unlock(t);
1753 	mutex_exit(&p->p_lock);
1754 	prsvaddr(ttolwp(t), vaddr);
1755 	mutex_enter(&p->p_lock);
1756 
1757 	return (0);
1758 }
1759 
1760 void
1761 pr_sethold(prnode_t *pnp, sigset_t *sp)
1762 {
1763 	proc_t *p = pnp->pr_common->prc_proc;
1764 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1765 
1766 	schedctl_finish_sigblock(t);
1767 	sigutok(sp, &t->t_hold);
1768 	if (ISWAKEABLE(t) &&
1769 	    (fsig(&p->p_sig, t) || fsig(&t->t_sig, t)))
1770 		setrun_locked(t);
1771 	t->t_sig_check = 1;	/* so thread will see new holdmask */
1772 	thread_unlock(t);
1773 }
1774 
1775 void
1776 pr_setfault(proc_t *p, fltset_t *fltp)
1777 {
1778 	prassignset(&p->p_fltmask, fltp);
1779 	if (!prisempty(&p->p_fltmask))
1780 		p->p_proc_flag |= P_PR_TRACE;
1781 	else if (sigisempty(&p->p_sigmask)) {
1782 		user_t *up = PTOU(p);
1783 		if (up->u_systrap == 0)
1784 			p->p_proc_flag &= ~P_PR_TRACE;
1785 	}
1786 }
1787 
1788 static int
1789 pr_clearsig(prnode_t *pnp)
1790 {
1791 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1792 	klwp_t *lwp = ttolwp(t);
1793 
1794 	thread_unlock(t);
1795 	if (lwp->lwp_cursig == SIGKILL)
1796 		return (EBUSY);
1797 
1798 	/*
1799 	 * Discard current siginfo_t, if any.
1800 	 */
1801 	lwp->lwp_cursig = 0;
1802 	lwp->lwp_extsig = 0;
1803 	if (lwp->lwp_curinfo) {
1804 		siginfofree(lwp->lwp_curinfo);
1805 		lwp->lwp_curinfo = NULL;
1806 	}
1807 
1808 	return (0);
1809 }
1810 
1811 static int
1812 pr_clearflt(prnode_t *pnp)
1813 {
1814 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
1815 
1816 	thread_unlock(t);
1817 	ttolwp(t)->lwp_curflt = 0;
1818 
1819 	return (0);
1820 }
1821 
1822 static int
1823 pr_watch(prnode_t *pnp, prwatch_t *pwp, int *unlocked)
1824 {
1825 	proc_t *p = pnp->pr_common->prc_proc;
1826 	struct as *as = p->p_as;
1827 	uintptr_t vaddr = pwp->pr_vaddr;
1828 	size_t size = pwp->pr_size;
1829 	int wflags = pwp->pr_wflags;
1830 	ulong_t newpage = 0;
1831 	struct watched_area *pwa;
1832 	int error;
1833 
1834 	*unlocked = 0;
1835 
1836 	/*
1837 	 * Can't apply to a system process.
1838 	 */
1839 	if ((p->p_flag & SSYS) || p->p_as == &kas)
1840 		return (EBUSY);
1841 
1842 	/*
1843 	 * Verify that the address range does not wrap
1844 	 * and that only the proper flags were specified.
1845 	 */
1846 	if ((wflags & ~WA_TRAPAFTER) == 0)
1847 		size = 0;
1848 	if (vaddr + size < vaddr ||
1849 	    (wflags & ~(WA_READ|WA_WRITE|WA_EXEC|WA_TRAPAFTER)) != 0 ||
1850 	    ((wflags & ~WA_TRAPAFTER) != 0 && size == 0))
1851 		return (EINVAL);
1852 
1853 	/*
1854 	 * Don't let the address range go above as->a_userlimit.
1855 	 * There is no error here, just a limitation.
1856 	 */
1857 	if (vaddr >= (uintptr_t)as->a_userlimit)
1858 		return (0);
1859 	if (vaddr + size > (uintptr_t)as->a_userlimit)
1860 		size = (uintptr_t)as->a_userlimit - vaddr;
1861 
1862 	/*
1863 	 * Compute maximum number of pages this will add.
1864 	 */
1865 	if ((wflags & ~WA_TRAPAFTER) != 0) {
1866 		ulong_t pagespan = (vaddr + size) - (vaddr & PAGEMASK);
1867 		newpage = btopr(pagespan);
1868 		if (newpage > 2 * prnwatch)
1869 			return (E2BIG);
1870 	}
1871 
1872 	/*
1873 	 * Force the process to be fully stopped.
1874 	 */
1875 	if (p == curproc) {
1876 		prunlock(pnp);
1877 		while (holdwatch() != 0)
1878 			continue;
1879 		if ((error = prlock(pnp, ZNO)) != 0) {
1880 			continuelwps(p);
1881 			*unlocked = 1;
1882 			return (error);
1883 		}
1884 	} else {
1885 		pauselwps(p);
1886 		while (pr_allstopped(p, 0) > 0) {
1887 			/*
1888 			 * This cv/mutex pair is persistent even
1889 			 * if the process disappears after we
1890 			 * unmark it and drop p->p_lock.
1891 			 */
1892 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
1893 			kmutex_t *mp = &p->p_lock;
1894 
1895 			prunmark(p);
1896 			(void) cv_wait(cv, mp);
1897 			mutex_exit(mp);
1898 			if ((error = prlock(pnp, ZNO)) != 0) {
1899 				/*
1900 				 * Unpause the process if it exists.
1901 				 */
1902 				p = pr_p_lock(pnp);
1903 				mutex_exit(&pr_pidlock);
1904 				if (p != NULL) {
1905 					unpauselwps(p);
1906 					prunlock(pnp);
1907 				}
1908 				*unlocked = 1;
1909 				return (error);
1910 			}
1911 		}
1912 	}
1913 
1914 	/*
1915 	 * Drop p->p_lock in order to perform the rest of this.
1916 	 * The process is still locked with the P_PR_LOCK flag.
1917 	 */
1918 	mutex_exit(&p->p_lock);
1919 
1920 	pwa = kmem_alloc(sizeof (struct watched_area), KM_SLEEP);
1921 	pwa->wa_vaddr = (caddr_t)vaddr;
1922 	pwa->wa_eaddr = (caddr_t)vaddr + size;
1923 	pwa->wa_flags = (ulong_t)wflags;
1924 
1925 	error = ((pwa->wa_flags & ~WA_TRAPAFTER) == 0)?
1926 		clear_watched_area(p, pwa) :
1927 		set_watched_area(p, pwa);
1928 
1929 	if (p == curproc) {
1930 		setallwatch();
1931 		mutex_enter(&p->p_lock);
1932 		continuelwps(p);
1933 	} else {
1934 		mutex_enter(&p->p_lock);
1935 		unpauselwps(p);
1936 	}
1937 
1938 	return (error);
1939 }
1940 
1941 /* jobcontrol stopped, but with a /proc directed stop in effect */
1942 #define	JDSTOPPED(t)	\
1943 	((t)->t_state == TS_STOPPED && \
1944 	(t)->t_whystop == PR_JOBCONTROL && \
1945 	((t)->t_proc_flag & TP_PRSTOP))
1946 
1947 /*
1948  * pr_agent() creates the agent lwp. If the process is exiting while
1949  * we are creating an agent lwp, then exitlwps() waits until the
1950  * agent has been created using prbarrier().
1951  */
1952 static int
1953 pr_agent(prnode_t *pnp, prgregset_t prgregset, int *unlocked)
1954 {
1955 	proc_t *p = pnp->pr_common->prc_proc;
1956 	prcommon_t *pcp;
1957 	kthread_t *t;
1958 	kthread_t *ct;
1959 	klwp_t *clwp;
1960 	k_sigset_t smask;
1961 	int cid;
1962 	void *bufp = NULL;
1963 	int error;
1964 
1965 	*unlocked = 0;
1966 
1967 	/*
1968 	 * Cannot create the /proc agent lwp if :-
1969 	 * - the process is not fully stopped or directed to stop.
1970 	 * - there is an agent lwp already.
1971 	 * - the process has been killed.
1972 	 * - the process is exiting.
1973 	 * - it's a vfork(2) parent.
1974 	 */
1975 	t = prchoose(p);	/* returns locked thread */
1976 	ASSERT(t != NULL);
1977 
1978 	if ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t) && !JDSTOPPED(t)) ||
1979 	    p->p_agenttp != NULL ||
1980 	    (p->p_flag & (SKILLED | SEXITING | SVFWAIT))) {
1981 		thread_unlock(t);
1982 		return (EBUSY);
1983 	}
1984 
1985 	thread_unlock(t);
1986 	mutex_exit(&p->p_lock);
1987 
1988 	sigfillset(&smask);
1989 	sigdiffset(&smask, &cantmask);
1990 	clwp = lwp_create(lwp_rtt, NULL, 0, p, TS_STOPPED,
1991 	    t->t_pri, &smask, NOCLASS, 0);
1992 	if (clwp == NULL) {
1993 		mutex_enter(&p->p_lock);
1994 		return (ENOMEM);
1995 	}
1996 	prsetprregs(clwp, prgregset, 1);
1997 retry:
1998 	cid = t->t_cid;
1999 	(void) CL_ALLOC(&bufp, cid, KM_SLEEP);
2000 	mutex_enter(&p->p_lock);
2001 	if (cid != t->t_cid) {
2002 		/*
2003 		 * Someone just changed this thread's scheduling class,
2004 		 * so try pre-allocating the buffer again.  Hopefully we
2005 		 * don't hit this often.
2006 		 */
2007 		mutex_exit(&p->p_lock);
2008 		CL_FREE(cid, bufp);
2009 		goto retry;
2010 	}
2011 
2012 	clwp->lwp_ap = clwp->lwp_arg;
2013 	clwp->lwp_eosys = NORMALRETURN;
2014 	ct = lwptot(clwp);
2015 	ct->t_clfuncs = t->t_clfuncs;
2016 	CL_FORK(t, ct, bufp);
2017 	ct->t_cid = t->t_cid;
2018 	ct->t_proc_flag |= TP_PRSTOP;
2019 	/*
2020 	 * Setting t_sysnum to zero causes post_syscall()
2021 	 * to bypass all syscall checks and go directly to
2022 	 *	if (issig()) psig();
2023 	 * so that the agent lwp will stop in issig_forreal()
2024 	 * showing PR_REQUESTED.
2025 	 */
2026 	ct->t_sysnum = 0;
2027 	ct->t_post_sys = 1;
2028 	ct->t_sig_check = 1;
2029 	p->p_agenttp = ct;
2030 	ct->t_proc_flag &= ~TP_HOLDLWP;
2031 
2032 	pcp = pnp->pr_pcommon;
2033 	mutex_enter(&pcp->prc_mutex);
2034 
2035 	lwp_create_done(ct);
2036 
2037 	/*
2038 	 * Don't return until the agent is stopped on PR_REQUESTED.
2039 	 */
2040 
2041 	for (;;) {
2042 		prunlock(pnp);
2043 		*unlocked = 1;
2044 
2045 		/*
2046 		 * Wait for the agent to stop and notify us.
2047 		 * If we've been interrupted, return that information.
2048 		 */
2049 		error = pr_wait(pcp, NULL, 0);
2050 		if (error == EINTR) {
2051 			error = 0;
2052 			break;
2053 		}
2054 
2055 		/*
2056 		 * Confirm that the agent LWP has stopped.
2057 		 */
2058 
2059 		if ((error = prlock(pnp, ZNO)) != 0)
2060 			break;
2061 		*unlocked = 0;
2062 
2063 		/*
2064 		 * Since we dropped the lock on the process, the agent
2065 		 * may have disappeared or changed. Grab the current
2066 		 * agent and check fail if it has disappeared.
2067 		 */
2068 		if ((ct = p->p_agenttp) == NULL) {
2069 			error = ENOENT;
2070 			break;
2071 		}
2072 
2073 		mutex_enter(&pcp->prc_mutex);
2074 		thread_lock(ct);
2075 
2076 		if (ISTOPPED(ct)) {
2077 			thread_unlock(ct);
2078 			mutex_exit(&pcp->prc_mutex);
2079 			break;
2080 		}
2081 
2082 		thread_unlock(ct);
2083 	}
2084 
2085 	return (error ? error : -1);
2086 }
2087 
2088 static int
2089 pr_rdwr(proc_t *p, enum uio_rw rw, priovec_t *pio)
2090 {
2091 	caddr_t base = (caddr_t)pio->pio_base;
2092 	size_t cnt = pio->pio_len;
2093 	uintptr_t offset = (uintptr_t)pio->pio_offset;
2094 	struct uio auio;
2095 	struct iovec aiov;
2096 	int error = 0;
2097 
2098 	if ((p->p_flag & SSYS) || p->p_as == &kas)
2099 		error = EIO;
2100 	else if ((base + cnt) < base || (offset + cnt) < offset)
2101 		error = EINVAL;
2102 	else if (cnt != 0) {
2103 		aiov.iov_base = base;
2104 		aiov.iov_len = cnt;
2105 
2106 		auio.uio_loffset = offset;
2107 		auio.uio_iov = &aiov;
2108 		auio.uio_iovcnt = 1;
2109 		auio.uio_resid = cnt;
2110 		auio.uio_segflg = UIO_USERSPACE;
2111 		auio.uio_llimit = (longlong_t)MAXOFFSET_T;
2112 		auio.uio_fmode = FREAD|FWRITE;
2113 		auio.uio_extflg = UIO_COPY_DEFAULT;
2114 
2115 		mutex_exit(&p->p_lock);
2116 		error = prusrio(p, rw, &auio, 0);
2117 		mutex_enter(&p->p_lock);
2118 
2119 		/*
2120 		 * We have no way to return the i/o count,
2121 		 * like read() or write() would do, so we
2122 		 * return an error if the i/o was truncated.
2123 		 */
2124 		if (auio.uio_resid != 0 && error == 0)
2125 			error = EIO;
2126 	}
2127 
2128 	return (error);
2129 }
2130 
2131 static int
2132 pr_scred(proc_t *p, prcred_t *prcred, cred_t *cr, boolean_t dogrps)
2133 {
2134 	kthread_t *t;
2135 	cred_t *oldcred;
2136 	cred_t *newcred;
2137 	uid_t oldruid;
2138 	int error;
2139 
2140 	if (!VALID_UID(prcred->pr_euid) ||
2141 	    !VALID_UID(prcred->pr_ruid) ||
2142 	    !VALID_UID(prcred->pr_suid) ||
2143 	    !VALID_GID(prcred->pr_egid) ||
2144 	    !VALID_GID(prcred->pr_rgid) ||
2145 	    !VALID_GID(prcred->pr_sgid))
2146 		return (EINVAL);
2147 
2148 	if (dogrps) {
2149 		int ngrp = prcred->pr_ngroups;
2150 		int i;
2151 
2152 		if (ngrp < 0 || ngrp > ngroups_max)
2153 			return (EINVAL);
2154 
2155 		for (i = 0; i < ngrp; i++) {
2156 			if (!VALID_GID(prcred->pr_groups[i]))
2157 				return (EINVAL);
2158 		}
2159 	}
2160 
2161 	error = secpolicy_allow_setid(cr, prcred->pr_euid, B_FALSE);
2162 
2163 	if (error == 0 && prcred->pr_ruid != prcred->pr_euid)
2164 		error = secpolicy_allow_setid(cr, prcred->pr_ruid, B_FALSE);
2165 
2166 	if (error == 0 && prcred->pr_suid != prcred->pr_euid &&
2167 	    prcred->pr_suid != prcred->pr_ruid)
2168 		error = secpolicy_allow_setid(cr, prcred->pr_suid, B_FALSE);
2169 
2170 	if (error)
2171 		return (error);
2172 
2173 	mutex_exit(&p->p_lock);
2174 
2175 	/* hold old cred so it doesn't disappear while we dup it */
2176 	mutex_enter(&p->p_crlock);
2177 	crhold(oldcred = p->p_cred);
2178 	mutex_exit(&p->p_crlock);
2179 	newcred = crdup(oldcred);
2180 	oldruid = crgetruid(oldcred);
2181 	crfree(oldcred);
2182 
2183 	/* Error checking done above */
2184 	(void) crsetresuid(newcred, prcred->pr_ruid, prcred->pr_euid,
2185 		prcred->pr_suid);
2186 	(void) crsetresgid(newcred, prcred->pr_rgid, prcred->pr_egid,
2187 		prcred->pr_sgid);
2188 
2189 	if (dogrps) {
2190 		(void) crsetgroups(newcred, prcred->pr_ngroups,
2191 		    prcred->pr_groups);
2192 
2193 	}
2194 
2195 	mutex_enter(&p->p_crlock);
2196 	oldcred = p->p_cred;
2197 	p->p_cred = newcred;
2198 	mutex_exit(&p->p_crlock);
2199 	crfree(oldcred);
2200 
2201 	/*
2202 	 * Keep count of processes per uid consistent.
2203 	 */
2204 	if (oldruid != prcred->pr_ruid) {
2205 		zoneid_t zoneid = crgetzoneid(newcred);
2206 
2207 		mutex_enter(&pidlock);
2208 		upcount_dec(oldruid, zoneid);
2209 		upcount_inc(prcred->pr_ruid, zoneid);
2210 		mutex_exit(&pidlock);
2211 	}
2212 
2213 	/*
2214 	 * Broadcast the cred change to the threads.
2215 	 */
2216 	mutex_enter(&p->p_lock);
2217 	t = p->p_tlist;
2218 	do {
2219 		t->t_pre_sys = 1; /* so syscall will get new cred */
2220 	} while ((t = t->t_forw) != p->p_tlist);
2221 
2222 	return (0);
2223 }
2224 
2225 /*
2226  * Change process credentials to specified zone.  Used to temporarily
2227  * set a process to run in the global zone; only transitions between
2228  * the process's actual zone and the global zone are allowed.
2229  */
2230 static int
2231 pr_szoneid(proc_t *p, zoneid_t zoneid, cred_t *cr)
2232 {
2233 	kthread_t *t;
2234 	cred_t *oldcred;
2235 	cred_t *newcred;
2236 	zone_t *zptr;
2237 	zoneid_t oldzoneid;
2238 
2239 	if (secpolicy_zone_config(cr) != 0)
2240 		return (EPERM);
2241 	if (zoneid != GLOBAL_ZONEID && zoneid != p->p_zone->zone_id)
2242 		return (EINVAL);
2243 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
2244 		return (EINVAL);
2245 	mutex_exit(&p->p_lock);
2246 	mutex_enter(&p->p_crlock);
2247 	oldcred = p->p_cred;
2248 	crhold(oldcred);
2249 	mutex_exit(&p->p_crlock);
2250 	newcred = crdup(oldcred);
2251 	oldzoneid = crgetzoneid(oldcred);
2252 	crfree(oldcred);
2253 
2254 	crsetzone(newcred, zptr);
2255 	zone_rele(zptr);
2256 
2257 	mutex_enter(&p->p_crlock);
2258 	oldcred = p->p_cred;
2259 	p->p_cred = newcred;
2260 	mutex_exit(&p->p_crlock);
2261 	crfree(oldcred);
2262 
2263 	/*
2264 	 * The target process is changing zones (according to its cred), so
2265 	 * update the per-zone upcounts, which are based on process creds.
2266 	 */
2267 	if (oldzoneid != zoneid) {
2268 		uid_t ruid = crgetruid(newcred);
2269 
2270 		mutex_enter(&pidlock);
2271 		upcount_dec(ruid, oldzoneid);
2272 		upcount_inc(ruid, zoneid);
2273 		mutex_exit(&pidlock);
2274 	}
2275 	/*
2276 	 * Broadcast the cred change to the threads.
2277 	 */
2278 	mutex_enter(&p->p_lock);
2279 	t = p->p_tlist;
2280 	do {
2281 		t->t_pre_sys = 1;	/* so syscall will get new cred */
2282 	} while ((t = t->t_forw) != p->p_tlist);
2283 
2284 	return (0);
2285 }
2286 
2287 static int
2288 pr_spriv(proc_t *p, prpriv_t *prpriv, cred_t *cr)
2289 {
2290 	kthread_t *t;
2291 	int err;
2292 
2293 	ASSERT(MUTEX_HELD(&p->p_lock));
2294 
2295 	if ((err = priv_pr_spriv(p, prpriv, cr)) == 0) {
2296 		/*
2297 		 * Broadcast the cred change to the threads.
2298 		 */
2299 		t = p->p_tlist;
2300 		do {
2301 			t->t_pre_sys = 1; /* so syscall will get new cred */
2302 		} while ((t = t->t_forw) != p->p_tlist);
2303 	}
2304 
2305 	return (err);
2306 }
2307 
2308 /*
2309  * Return -1 if the process is the parent of a vfork(1) whose child has yet to
2310  * terminate or perform an exec(2).
2311  *
2312  * Returns 0 if the process is fully stopped except for the current thread (if
2313  * we are operating on our own process), 1 otherwise.
2314  *
2315  * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
2316  * See holdwatch() for details.
2317  */
2318 int
2319 pr_allstopped(proc_t *p, int watchstop)
2320 {
2321 	kthread_t *t;
2322 	int rv = 0;
2323 
2324 	ASSERT(MUTEX_HELD(&p->p_lock));
2325 
2326 	if (p->p_flag & SVFWAIT)	/* waiting for vfork'd child to exec */
2327 		return (-1);
2328 
2329 	if ((t = p->p_tlist) != NULL) {
2330 		do {
2331 			if (t == curthread || VSTOPPED(t) ||
2332 			    (watchstop && (t->t_proc_flag & TP_WATCHSTOP)))
2333 				continue;
2334 			thread_lock(t);
2335 			switch (t->t_state) {
2336 			case TS_ZOMB:
2337 			case TS_STOPPED:
2338 				break;
2339 			case TS_SLEEP:
2340 				if (!(t->t_flag & T_WAKEABLE) ||
2341 				    t->t_wchan0 == NULL)
2342 					rv = 1;
2343 				break;
2344 			default:
2345 				rv = 1;
2346 				break;
2347 			}
2348 			thread_unlock(t);
2349 		} while (rv == 0 && (t = t->t_forw) != p->p_tlist);
2350 	}
2351 
2352 	return (rv);
2353 }
2354 
2355 /*
2356  * Cause all lwps in the process to pause (for watchpoint operations).
2357  */
2358 static void
2359 pauselwps(proc_t *p)
2360 {
2361 	kthread_t *t;
2362 
2363 	ASSERT(MUTEX_HELD(&p->p_lock));
2364 	ASSERT(p != curproc);
2365 
2366 	if ((t = p->p_tlist) != NULL) {
2367 		do {
2368 			thread_lock(t);
2369 			t->t_proc_flag |= TP_PAUSE;
2370 			aston(t);
2371 			if ((ISWAKEABLE(t) && (t->t_wchan0 == NULL)) ||
2372 			    ISWAITING(t)) {
2373 				setrun_locked(t);
2374 			}
2375 			prpokethread(t);
2376 			thread_unlock(t);
2377 		} while ((t = t->t_forw) != p->p_tlist);
2378 	}
2379 }
2380 
2381 /*
2382  * undo the effects of pauselwps()
2383  */
2384 static void
2385 unpauselwps(proc_t *p)
2386 {
2387 	kthread_t *t;
2388 
2389 	ASSERT(MUTEX_HELD(&p->p_lock));
2390 	ASSERT(p != curproc);
2391 
2392 	if ((t = p->p_tlist) != NULL) {
2393 		do {
2394 			thread_lock(t);
2395 			t->t_proc_flag &= ~TP_PAUSE;
2396 			if (t->t_state == TS_STOPPED) {
2397 				t->t_schedflag |= TS_UNPAUSE;
2398 				t->t_dtrace_stop = 0;
2399 				setrun_locked(t);
2400 			}
2401 			thread_unlock(t);
2402 		} while ((t = t->t_forw) != p->p_tlist);
2403 	}
2404 }
2405 
2406 /*
2407  * Cancel all watched areas.  Called from prclose().
2408  */
2409 proc_t *
2410 pr_cancel_watch(prnode_t *pnp)
2411 {
2412 	proc_t *p = pnp->pr_pcommon->prc_proc;
2413 	struct as *as;
2414 	kthread_t *t;
2415 
2416 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2417 
2418 	if (!pr_watch_active(p))
2419 		return (p);
2420 
2421 	/*
2422 	 * Pause the process before dealing with the watchpoints.
2423 	 */
2424 	if (p == curproc) {
2425 		prunlock(pnp);
2426 		while (holdwatch() != 0)
2427 			continue;
2428 		p = pr_p_lock(pnp);
2429 		mutex_exit(&pr_pidlock);
2430 		ASSERT(p == curproc);
2431 	} else {
2432 		pauselwps(p);
2433 		while (p != NULL && pr_allstopped(p, 0) > 0) {
2434 			/*
2435 			 * This cv/mutex pair is persistent even
2436 			 * if the process disappears after we
2437 			 * unmark it and drop p->p_lock.
2438 			 */
2439 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
2440 			kmutex_t *mp = &p->p_lock;
2441 
2442 			prunmark(p);
2443 			(void) cv_wait(cv, mp);
2444 			mutex_exit(mp);
2445 			p = pr_p_lock(pnp);  /* NULL if process disappeared */
2446 			mutex_exit(&pr_pidlock);
2447 		}
2448 	}
2449 
2450 	if (p == NULL)		/* the process disappeared */
2451 		return (NULL);
2452 
2453 	ASSERT(p == pnp->pr_pcommon->prc_proc);
2454 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2455 
2456 	if (pr_watch_active(p)) {
2457 		pr_free_watchpoints(p);
2458 		if ((t = p->p_tlist) != NULL) {
2459 			do {
2460 				watch_disable(t);
2461 
2462 			} while ((t = t->t_forw) != p->p_tlist);
2463 		}
2464 	}
2465 
2466 	if ((as = p->p_as) != NULL) {
2467 		avl_tree_t *tree;
2468 		struct watched_page *pwp;
2469 
2470 		/*
2471 		 * If this is the parent of a vfork, the watched page
2472 		 * list has been moved temporarily to p->p_wpage.
2473 		 */
2474 		if (avl_numnodes(&p->p_wpage) != 0)
2475 			tree = &p->p_wpage;
2476 		else
2477 			tree = &as->a_wpage;
2478 
2479 		mutex_exit(&p->p_lock);
2480 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
2481 
2482 		for (pwp = avl_first(tree); pwp != NULL;
2483 		    pwp = AVL_NEXT(tree, pwp)) {
2484 			pwp->wp_read = 0;
2485 			pwp->wp_write = 0;
2486 			pwp->wp_exec = 0;
2487 			if ((pwp->wp_flags & WP_SETPROT) == 0) {
2488 				pwp->wp_flags |= WP_SETPROT;
2489 				pwp->wp_prot = pwp->wp_oprot;
2490 				pwp->wp_list = p->p_wprot;
2491 				p->p_wprot = pwp;
2492 			}
2493 		}
2494 
2495 		AS_LOCK_EXIT(as, &as->a_lock);
2496 		mutex_enter(&p->p_lock);
2497 	}
2498 
2499 	/*
2500 	 * Unpause the process now.
2501 	 */
2502 	if (p == curproc)
2503 		continuelwps(p);
2504 	else
2505 		unpauselwps(p);
2506 
2507 	return (p);
2508 }
2509