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