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