xref: /illumos-gate/usr/src/uts/common/fs/proc/prcontrol.c (revision 956b78cc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*956b78ccSsl108498  * Common Development and Distribution License (the "License").
6*956b78ccSsl108498  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
23a913396dSqiao  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/uio.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/policy.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/file.h>
387c478bd9Sstevel@tonic-gate #include <sys/inline.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/regset.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/systm.h>
447c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
457c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
467c478bd9Sstevel@tonic-gate #include <sys/signal.h>
477c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
487c478bd9Sstevel@tonic-gate #include <sys/user.h>
497c478bd9Sstevel@tonic-gate #include <sys/class.h>
507c478bd9Sstevel@tonic-gate #include <sys/fault.h>
517c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
527c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
537c478bd9Sstevel@tonic-gate #include <sys/zone.h>
547c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
557c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
567c478bd9Sstevel@tonic-gate #include <vm/as.h>
577c478bd9Sstevel@tonic-gate #include <vm/seg.h>
587c478bd9Sstevel@tonic-gate #include <fs/proc/prdata.h>
597c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static	void	pr_settrace(proc_t *, sigset_t *);
627c478bd9Sstevel@tonic-gate static	int	pr_setfpregs(prnode_t *, prfpregset_t *);
637c478bd9Sstevel@tonic-gate #if defined(__sparc)
647c478bd9Sstevel@tonic-gate static	int	pr_setxregs(prnode_t *, prxregset_t *);
657c478bd9Sstevel@tonic-gate static	int	pr_setasrs(prnode_t *, asrset_t);
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate static	int	pr_setvaddr(prnode_t *, caddr_t);
687c478bd9Sstevel@tonic-gate static	int	pr_clearsig(prnode_t *);
697c478bd9Sstevel@tonic-gate static	int	pr_clearflt(prnode_t *);
707c478bd9Sstevel@tonic-gate static	int	pr_watch(prnode_t *, prwatch_t *, int *);
717c478bd9Sstevel@tonic-gate static	int	pr_agent(prnode_t *, prgregset_t, int *);
727c478bd9Sstevel@tonic-gate static	int	pr_rdwr(proc_t *, enum uio_rw, priovec_t *);
737c478bd9Sstevel@tonic-gate static	int	pr_scred(proc_t *, prcred_t *, cred_t *, boolean_t);
747c478bd9Sstevel@tonic-gate static	int	pr_spriv(proc_t *, prpriv_t *, cred_t *);
757c478bd9Sstevel@tonic-gate static	int	pr_szoneid(proc_t *, zoneid_t, cred_t *);
767c478bd9Sstevel@tonic-gate static	void	pauselwps(proc_t *);
777c478bd9Sstevel@tonic-gate static	void	unpauselwps(proc_t *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate typedef union {
807c478bd9Sstevel@tonic-gate 	long		sig;		/* PCKILL, PCUNKILL */
817c478bd9Sstevel@tonic-gate 	long		nice;		/* PCNICE */
827c478bd9Sstevel@tonic-gate 	long		timeo;		/* PCTWSTOP */
837c478bd9Sstevel@tonic-gate 	ulong_t		flags;		/* PCRUN, PCSET, PCUNSET */
847c478bd9Sstevel@tonic-gate 	caddr_t		vaddr;		/* PCSVADDR */
857c478bd9Sstevel@tonic-gate 	siginfo_t	siginfo;	/* PCSSIG */
867c478bd9Sstevel@tonic-gate 	sigset_t	sigset;		/* PCSTRACE, PCSHOLD */
877c478bd9Sstevel@tonic-gate 	fltset_t	fltset;		/* PCSFAULT */
887c478bd9Sstevel@tonic-gate 	sysset_t	sysset;		/* PCSENTRY, PCSEXIT */
897c478bd9Sstevel@tonic-gate 	prgregset_t	prgregset;	/* PCSREG, PCAGENT */
907c478bd9Sstevel@tonic-gate 	prfpregset_t	prfpregset;	/* PCSFPREG */
917c478bd9Sstevel@tonic-gate #if defined(__sparc)
927c478bd9Sstevel@tonic-gate 	prxregset_t	prxregset;	/* PCSXREG */
937c478bd9Sstevel@tonic-gate 	asrset_t	asrset;		/* PCSASRS */
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 	prwatch_t	prwatch;	/* PCWATCH */
967c478bd9Sstevel@tonic-gate 	priovec_t	priovec;	/* PCREAD, PCWRITE */
977c478bd9Sstevel@tonic-gate 	prcred_t	prcred;		/* PCSCRED */
987c478bd9Sstevel@tonic-gate 	prpriv_t	prpriv;		/* PCSPRIV */
997c478bd9Sstevel@tonic-gate 	long		przoneid;	/* PCSZONE */
1007c478bd9Sstevel@tonic-gate } arg_t;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static	int	pr_control(long, arg_t *, prnode_t *, cred_t *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static size_t
1057c478bd9Sstevel@tonic-gate ctlsize(long cmd, size_t resid, arg_t *argp)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	size_t size = sizeof (long);
1087c478bd9Sstevel@tonic-gate 	size_t rnd;
1097c478bd9Sstevel@tonic-gate 	int ngrp;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	switch (cmd) {
1127c478bd9Sstevel@tonic-gate 	case PCNULL:
1137c478bd9Sstevel@tonic-gate 	case PCSTOP:
1147c478bd9Sstevel@tonic-gate 	case PCDSTOP:
1157c478bd9Sstevel@tonic-gate 	case PCWSTOP:
1167c478bd9Sstevel@tonic-gate 	case PCCSIG:
1177c478bd9Sstevel@tonic-gate 	case PCCFAULT:
1187c478bd9Sstevel@tonic-gate 		break;
1197c478bd9Sstevel@tonic-gate 	case PCSSIG:
1207c478bd9Sstevel@tonic-gate 		size += sizeof (siginfo_t);
1217c478bd9Sstevel@tonic-gate 		break;
1227c478bd9Sstevel@tonic-gate 	case PCTWSTOP:
1237c478bd9Sstevel@tonic-gate 		size += sizeof (long);
1247c478bd9Sstevel@tonic-gate 		break;
1257c478bd9Sstevel@tonic-gate 	case PCKILL:
1267c478bd9Sstevel@tonic-gate 	case PCUNKILL:
1277c478bd9Sstevel@tonic-gate 	case PCNICE:
1287c478bd9Sstevel@tonic-gate 		size += sizeof (long);
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 	case PCRUN:
1317c478bd9Sstevel@tonic-gate 	case PCSET:
1327c478bd9Sstevel@tonic-gate 	case PCUNSET:
1337c478bd9Sstevel@tonic-gate 		size += sizeof (ulong_t);
1347c478bd9Sstevel@tonic-gate 		break;
1357c478bd9Sstevel@tonic-gate 	case PCSVADDR:
1367c478bd9Sstevel@tonic-gate 		size += sizeof (caddr_t);
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 	case PCSTRACE:
1397c478bd9Sstevel@tonic-gate 	case PCSHOLD:
1407c478bd9Sstevel@tonic-gate 		size += sizeof (sigset_t);
1417c478bd9Sstevel@tonic-gate 		break;
1427c478bd9Sstevel@tonic-gate 	case PCSFAULT:
1437c478bd9Sstevel@tonic-gate 		size += sizeof (fltset_t);
1447c478bd9Sstevel@tonic-gate 		break;
1457c478bd9Sstevel@tonic-gate 	case PCSENTRY:
1467c478bd9Sstevel@tonic-gate 	case PCSEXIT:
1477c478bd9Sstevel@tonic-gate 		size += sizeof (sysset_t);
1487c478bd9Sstevel@tonic-gate 		break;
1497c478bd9Sstevel@tonic-gate 	case PCSREG:
1507c478bd9Sstevel@tonic-gate 	case PCAGENT:
1517c478bd9Sstevel@tonic-gate 		size += sizeof (prgregset_t);
1527c478bd9Sstevel@tonic-gate 		break;
1537c478bd9Sstevel@tonic-gate 	case PCSFPREG:
1547c478bd9Sstevel@tonic-gate 		size += sizeof (prfpregset_t);
1557c478bd9Sstevel@tonic-gate 		break;
1567c478bd9Sstevel@tonic-gate #if defined(__sparc)
1577c478bd9Sstevel@tonic-gate 	case PCSXREG:
1587c478bd9Sstevel@tonic-gate 		size += sizeof (prxregset_t);
1597c478bd9Sstevel@tonic-gate 		break;
1607c478bd9Sstevel@tonic-gate 	case PCSASRS:
1617c478bd9Sstevel@tonic-gate 		size += sizeof (asrset_t);
1627c478bd9Sstevel@tonic-gate 		break;
1637c478bd9Sstevel@tonic-gate #endif
1647c478bd9Sstevel@tonic-gate 	case PCWATCH:
1657c478bd9Sstevel@tonic-gate 		size += sizeof (prwatch_t);
1667c478bd9Sstevel@tonic-gate 		break;
1677c478bd9Sstevel@tonic-gate 	case PCREAD:
1687c478bd9Sstevel@tonic-gate 	case PCWRITE:
1697c478bd9Sstevel@tonic-gate 		size += sizeof (priovec_t);
1707c478bd9Sstevel@tonic-gate 		break;
1717c478bd9Sstevel@tonic-gate 	case PCSCRED:
1727c478bd9Sstevel@tonic-gate 		size += sizeof (prcred_t);
1737c478bd9Sstevel@tonic-gate 		break;
1747c478bd9Sstevel@tonic-gate 	case PCSCREDX:
1757c478bd9Sstevel@tonic-gate 		/*
1767c478bd9Sstevel@tonic-gate 		 * We cannot derefence the pr_ngroups fields if it
1777c478bd9Sstevel@tonic-gate 		 * we don't have enough data.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		if (resid < size + sizeof (prcred_t) - sizeof (gid_t))
1807c478bd9Sstevel@tonic-gate 			return (0);
1817c478bd9Sstevel@tonic-gate 		ngrp = argp->prcred.pr_ngroups;
1827c478bd9Sstevel@tonic-gate 		if (ngrp < 0 || ngrp > ngroups_max)
1837c478bd9Sstevel@tonic-gate 			return (0);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		/* The result can be smaller than sizeof (prcred_t) */
1867c478bd9Sstevel@tonic-gate 		size += sizeof (prcred_t) - sizeof (gid_t);
1877c478bd9Sstevel@tonic-gate 		size += ngrp * sizeof (gid_t);
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	case PCSPRIV:
1907c478bd9Sstevel@tonic-gate 		if (resid >= size + sizeof (prpriv_t))
1917c478bd9Sstevel@tonic-gate 			size += priv_prgetprivsize(&argp->prpriv);
1927c478bd9Sstevel@tonic-gate 		else
1937c478bd9Sstevel@tonic-gate 			return (0);
1947c478bd9Sstevel@tonic-gate 		break;
1957c478bd9Sstevel@tonic-gate 	case PCSZONE:
1967c478bd9Sstevel@tonic-gate 		size += sizeof (long);
1977c478bd9Sstevel@tonic-gate 		break;
1987c478bd9Sstevel@tonic-gate 	default:
1997c478bd9Sstevel@tonic-gate 		return (0);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* Round up to a multiple of long, unless exact amount written */
2037c478bd9Sstevel@tonic-gate 	if (size < resid) {
2047c478bd9Sstevel@tonic-gate 		rnd = size & (sizeof (long) - 1);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		if (rnd != 0)
2077c478bd9Sstevel@tonic-gate 			size += sizeof (long) - rnd;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (size > resid)
2117c478bd9Sstevel@tonic-gate 		return (0);
2127c478bd9Sstevel@tonic-gate 	return (size);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Control operations (lots).
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate int
2197c478bd9Sstevel@tonic-gate prwritectl(vnode_t *vp, uio_t *uiop, cred_t *cr)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate #define	MY_BUFFER_SIZE \
2227c478bd9Sstevel@tonic-gate 		100 > 1 + sizeof (arg_t) / sizeof (long) ? \
2237c478bd9Sstevel@tonic-gate 		100 : 1 + sizeof (arg_t) / sizeof (long)
2247c478bd9Sstevel@tonic-gate 	long buf[MY_BUFFER_SIZE];
2257c478bd9Sstevel@tonic-gate 	long *bufp;
2267c478bd9Sstevel@tonic-gate 	size_t resid = 0;
2277c478bd9Sstevel@tonic-gate 	size_t size;
2287c478bd9Sstevel@tonic-gate 	prnode_t *pnp = VTOP(vp);
2297c478bd9Sstevel@tonic-gate 	int error;
2307c478bd9Sstevel@tonic-gate 	int locked = 0;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	while (uiop->uio_resid) {
2337c478bd9Sstevel@tonic-gate 		/*
2347c478bd9Sstevel@tonic-gate 		 * Read several commands in one gulp.
2357c478bd9Sstevel@tonic-gate 		 */
2367c478bd9Sstevel@tonic-gate 		bufp = buf;
2377c478bd9Sstevel@tonic-gate 		if (resid) {	/* move incomplete command to front of buffer */
2387c478bd9Sstevel@tonic-gate 			long *tail;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 			if (resid >= sizeof (buf))
2417c478bd9Sstevel@tonic-gate 				break;
2427c478bd9Sstevel@tonic-gate 			tail = (long *)((char *)buf + sizeof (buf) - resid);
2437c478bd9Sstevel@tonic-gate 			do {
2447c478bd9Sstevel@tonic-gate 				*bufp++ = *tail++;
2457c478bd9Sstevel@tonic-gate 			} while ((resid -= sizeof (long)) != 0);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		resid = sizeof (buf) - ((char *)bufp - (char *)buf);
2487c478bd9Sstevel@tonic-gate 		if (resid > uiop->uio_resid)
2497c478bd9Sstevel@tonic-gate 			resid = uiop->uio_resid;
2507c478bd9Sstevel@tonic-gate 		if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
2517c478bd9Sstevel@tonic-gate 			return (error);
2527c478bd9Sstevel@tonic-gate 		resid += (char *)bufp - (char *)buf;
2537c478bd9Sstevel@tonic-gate 		bufp = buf;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		do {		/* loop over commands in buffer */
2567c478bd9Sstevel@tonic-gate 			long cmd = bufp[0];
2577c478bd9Sstevel@tonic-gate 			arg_t *argp = (arg_t *)&bufp[1];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 			size = ctlsize(cmd, resid, argp);
2607c478bd9Sstevel@tonic-gate 			if (size == 0)	/* incomplete or invalid command */
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 			/*
2637c478bd9Sstevel@tonic-gate 			 * Perform the specified control operation.
2647c478bd9Sstevel@tonic-gate 			 */
2657c478bd9Sstevel@tonic-gate 			if (!locked) {
2667c478bd9Sstevel@tonic-gate 				if ((error = prlock(pnp, ZNO)) != 0)
2677c478bd9Sstevel@tonic-gate 					return (error);
2687c478bd9Sstevel@tonic-gate 				locked = 1;
2697c478bd9Sstevel@tonic-gate 			}
2707c478bd9Sstevel@tonic-gate 			if (error = pr_control(cmd, argp, pnp, cr)) {
2717c478bd9Sstevel@tonic-gate 				if (error == -1)	/* -1 is timeout */
2727c478bd9Sstevel@tonic-gate 					locked = 0;
2737c478bd9Sstevel@tonic-gate 				else
2747c478bd9Sstevel@tonic-gate 					return (error);
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 			bufp = (long *)((char *)bufp + size);
2777c478bd9Sstevel@tonic-gate 		} while ((resid -= size) != 0);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		if (locked) {
2807c478bd9Sstevel@tonic-gate 			prunlock(pnp);
2817c478bd9Sstevel@tonic-gate 			locked = 0;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	return (resid? EINVAL : 0);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static int
2887c478bd9Sstevel@tonic-gate pr_control(long cmd, arg_t *argp, prnode_t *pnp, cred_t *cr)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	prcommon_t *pcp;
2917c478bd9Sstevel@tonic-gate 	proc_t *p;
2927c478bd9Sstevel@tonic-gate 	int unlocked;
2937c478bd9Sstevel@tonic-gate 	int error = 0;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	if (cmd == PCNULL)
2967c478bd9Sstevel@tonic-gate 		return (0);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	pcp = pnp->pr_common;
2997c478bd9Sstevel@tonic-gate 	p = pcp->prc_proc;
3007c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	switch (cmd) {
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	default:
3057c478bd9Sstevel@tonic-gate 		error = EINVAL;
3067c478bd9Sstevel@tonic-gate 		break;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	case PCSTOP:	/* direct process or lwp to stop and wait for stop */
3097c478bd9Sstevel@tonic-gate 	case PCDSTOP:	/* direct process or lwp to stop, don't wait */
3107c478bd9Sstevel@tonic-gate 	case PCWSTOP:	/* wait for process or lwp to stop */
3117c478bd9Sstevel@tonic-gate 	case PCTWSTOP:	/* wait for process or lwp to stop, with timeout */
3127c478bd9Sstevel@tonic-gate 	    {
3137c478bd9Sstevel@tonic-gate 		time_t timeo;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		/*
3167c478bd9Sstevel@tonic-gate 		 * Can't apply to a system process.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
3197c478bd9Sstevel@tonic-gate 			error = EBUSY;
3207c478bd9Sstevel@tonic-gate 			break;
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 		if (cmd == PCSTOP || cmd == PCDSTOP)
3247c478bd9Sstevel@tonic-gate 			pr_stop(pnp);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (cmd == PCDSTOP)
3277c478bd9Sstevel@tonic-gate 			break;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		/*
3307c478bd9Sstevel@tonic-gate 		 * If an lwp is waiting for itself or its process, don't wait.
3317c478bd9Sstevel@tonic-gate 		 * The stopped lwp would never see the fact that it is stopped.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		if ((pcp->prc_flags & PRC_LWP)?
3347c478bd9Sstevel@tonic-gate 		    (pcp->prc_thread == curthread) : (p == curproc)) {
3357c478bd9Sstevel@tonic-gate 			if (cmd == PCWSTOP || cmd == PCTWSTOP)
3367c478bd9Sstevel@tonic-gate 				error = EBUSY;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
3417c478bd9Sstevel@tonic-gate 		if ((error = pr_wait_stop(pnp, timeo)) != 0)
3427c478bd9Sstevel@tonic-gate 			return (error);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		break;
3457c478bd9Sstevel@tonic-gate 	    }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	case PCRUN:	/* make lwp or process runnable */
3487c478bd9Sstevel@tonic-gate 		error = pr_setrun(pnp, argp->flags);
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	case PCSTRACE:	/* set signal trace mask */
3527c478bd9Sstevel@tonic-gate 		pr_settrace(p,  &argp->sigset);
3537c478bd9Sstevel@tonic-gate 		break;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	case PCSSIG:	/* set current signal */
3567c478bd9Sstevel@tonic-gate 		error = pr_setsig(pnp, &argp->siginfo);
3577c478bd9Sstevel@tonic-gate 		if (argp->siginfo.si_signo == SIGKILL && error == 0) {
3587c478bd9Sstevel@tonic-gate 			prunlock(pnp);
3597c478bd9Sstevel@tonic-gate 			pr_wait_die(pnp);
3607c478bd9Sstevel@tonic-gate 			return (-1);
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		break;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	case PCKILL:	/* send signal */
3657c478bd9Sstevel@tonic-gate 		error = pr_kill(pnp, (int)argp->sig, cr);
3667c478bd9Sstevel@tonic-gate 		if (error == 0 && argp->sig == SIGKILL) {
3677c478bd9Sstevel@tonic-gate 			prunlock(pnp);
3687c478bd9Sstevel@tonic-gate 			pr_wait_die(pnp);
3697c478bd9Sstevel@tonic-gate 			return (-1);
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 		break;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	case PCUNKILL:	/* delete a pending signal */
3747c478bd9Sstevel@tonic-gate 		error = pr_unkill(pnp, (int)argp->sig);
3757c478bd9Sstevel@tonic-gate 		break;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	case PCNICE:	/* set nice priority */
3787c478bd9Sstevel@tonic-gate 		error = pr_nice(p, (int)argp->nice, cr);
3797c478bd9Sstevel@tonic-gate 		break;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	case PCSENTRY:	/* set syscall entry bit mask */
3827c478bd9Sstevel@tonic-gate 	case PCSEXIT:	/* set syscall exit bit mask */
3837c478bd9Sstevel@tonic-gate 		pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
3847c478bd9Sstevel@tonic-gate 		break;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	case PCSET:	/* set process flags */
3877c478bd9Sstevel@tonic-gate 		error = pr_set(p, argp->flags);
3887c478bd9Sstevel@tonic-gate 		break;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	case PCUNSET:	/* unset process flags */
3917c478bd9Sstevel@tonic-gate 		error = pr_unset(p, argp->flags);
3927c478bd9Sstevel@tonic-gate 		break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	case PCSREG:	/* set general registers */
3957c478bd9Sstevel@tonic-gate 	    {
3967c478bd9Sstevel@tonic-gate 		kthread_t *t = pr_thread(pnp);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
3997c478bd9Sstevel@tonic-gate 			thread_unlock(t);
4007c478bd9Sstevel@tonic-gate 			error = EBUSY;
4017c478bd9Sstevel@tonic-gate 		} else {
4027c478bd9Sstevel@tonic-gate 			thread_unlock(t);
4037c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
4047c478bd9Sstevel@tonic-gate 			prsetprregs(ttolwp(t), argp->prgregset, 0);
4057c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 		break;
4087c478bd9Sstevel@tonic-gate 	    }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	case PCSFPREG:	/* set floating-point registers */
4117c478bd9Sstevel@tonic-gate 		error = pr_setfpregs(pnp, &argp->prfpregset);
4127c478bd9Sstevel@tonic-gate 		break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	case PCSXREG:	/* set extra registers */
4157c478bd9Sstevel@tonic-gate #if defined(__sparc)
4167c478bd9Sstevel@tonic-gate 		error = pr_setxregs(pnp, &argp->prxregset);
4177c478bd9Sstevel@tonic-gate #else
4187c478bd9Sstevel@tonic-gate 		error = EINVAL;
4197c478bd9Sstevel@tonic-gate #endif
4207c478bd9Sstevel@tonic-gate 		break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate #if defined(__sparc)
4237c478bd9Sstevel@tonic-gate 	case PCSASRS:	/* set ancillary state registers */
4247c478bd9Sstevel@tonic-gate 		error = pr_setasrs(pnp, argp->asrset);
4257c478bd9Sstevel@tonic-gate 		break;
4267c478bd9Sstevel@tonic-gate #endif
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	case PCSVADDR:	/* set virtual address at which to resume */
4297c478bd9Sstevel@tonic-gate 		error = pr_setvaddr(pnp, argp->vaddr);
4307c478bd9Sstevel@tonic-gate 		break;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	case PCSHOLD:	/* set signal-hold mask */
4337c478bd9Sstevel@tonic-gate 		pr_sethold(pnp, &argp->sigset);
4347c478bd9Sstevel@tonic-gate 		break;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	case PCSFAULT:	/* set mask of traced faults */
4377c478bd9Sstevel@tonic-gate 		pr_setfault(p, &argp->fltset);
4387c478bd9Sstevel@tonic-gate 		break;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	case PCCSIG:	/* clear current signal */
4417c478bd9Sstevel@tonic-gate 		error = pr_clearsig(pnp);
4427c478bd9Sstevel@tonic-gate 		break;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	case PCCFAULT:	/* clear current fault */
4457c478bd9Sstevel@tonic-gate 		error = pr_clearflt(pnp);
4467c478bd9Sstevel@tonic-gate 		break;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	case PCWATCH:	/* set or clear watched areas */
4497c478bd9Sstevel@tonic-gate 		error = pr_watch(pnp, &argp->prwatch, &unlocked);
4507c478bd9Sstevel@tonic-gate 		if (error && unlocked)
4517c478bd9Sstevel@tonic-gate 			return (error);
4527c478bd9Sstevel@tonic-gate 		break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	case PCAGENT:	/* create the /proc agent lwp in the target process */
4557c478bd9Sstevel@tonic-gate 		error = pr_agent(pnp, argp->prgregset, &unlocked);
4567c478bd9Sstevel@tonic-gate 		if (error && unlocked)
4577c478bd9Sstevel@tonic-gate 			return (error);
4587c478bd9Sstevel@tonic-gate 		break;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	case PCREAD:	/* read from the address space */
4617c478bd9Sstevel@tonic-gate 		error = pr_rdwr(p, UIO_READ, &argp->priovec);
4627c478bd9Sstevel@tonic-gate 		break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	case PCWRITE:	/* write to the address space */
4657c478bd9Sstevel@tonic-gate 		error = pr_rdwr(p, UIO_WRITE, &argp->priovec);
4667c478bd9Sstevel@tonic-gate 		break;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	case PCSCRED:	/* set the process credentials */
4697c478bd9Sstevel@tonic-gate 	case PCSCREDX:
4707c478bd9Sstevel@tonic-gate 		error = pr_scred(p, &argp->prcred, cr, cmd == PCSCREDX);
4717c478bd9Sstevel@tonic-gate 		break;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	case PCSPRIV:	/* set the process privileges */
4747c478bd9Sstevel@tonic-gate 		error = pr_spriv(p, &argp->prpriv, cr);
4757c478bd9Sstevel@tonic-gate 		break;
4767c478bd9Sstevel@tonic-gate 	case PCSZONE:	/* set the process's zoneid credentials */
4777c478bd9Sstevel@tonic-gate 		error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
4787c478bd9Sstevel@tonic-gate 		break;
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (error)
4827c478bd9Sstevel@tonic-gate 		prunlock(pnp);
4837c478bd9Sstevel@tonic-gate 	return (error);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate typedef union {
4897c478bd9Sstevel@tonic-gate 	int32_t		sig;		/* PCKILL, PCUNKILL */
4907c478bd9Sstevel@tonic-gate 	int32_t		nice;		/* PCNICE */
4917c478bd9Sstevel@tonic-gate 	int32_t		timeo;		/* PCTWSTOP */
4927c478bd9Sstevel@tonic-gate 	uint32_t	flags;		/* PCRUN, PCSET, PCUNSET */
4937c478bd9Sstevel@tonic-gate 	caddr32_t	vaddr;		/* PCSVADDR */
4947c478bd9Sstevel@tonic-gate 	siginfo32_t	siginfo;	/* PCSSIG */
4957c478bd9Sstevel@tonic-gate 	sigset_t	sigset;		/* PCSTRACE, PCSHOLD */
4967c478bd9Sstevel@tonic-gate 	fltset_t	fltset;		/* PCSFAULT */
4977c478bd9Sstevel@tonic-gate 	sysset_t	sysset;		/* PCSENTRY, PCSEXIT */
4987c478bd9Sstevel@tonic-gate 	prgregset32_t	prgregset;	/* PCSREG, PCAGENT */
4997c478bd9Sstevel@tonic-gate 	prfpregset32_t	prfpregset;	/* PCSFPREG */
5007c478bd9Sstevel@tonic-gate #if defined(__sparc)
5017c478bd9Sstevel@tonic-gate 	prxregset_t	prxregset;	/* PCSXREG */
5027c478bd9Sstevel@tonic-gate #endif
5037c478bd9Sstevel@tonic-gate 	prwatch32_t	prwatch;	/* PCWATCH */
5047c478bd9Sstevel@tonic-gate 	priovec32_t	priovec;	/* PCREAD, PCWRITE */
5057c478bd9Sstevel@tonic-gate 	prcred32_t	prcred;		/* PCSCRED */
5067c478bd9Sstevel@tonic-gate 	prpriv_t	prpriv;		/* PCSPRIV */
5077c478bd9Sstevel@tonic-gate 	int32_t		przoneid;	/* PCSZONE */
5087c478bd9Sstevel@tonic-gate } arg32_t;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate static	int	pr_control32(int32_t, arg32_t *, prnode_t *, cred_t *);
5117c478bd9Sstevel@tonic-gate static	int	pr_setfpregs32(prnode_t *, prfpregset32_t *);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * Note that while ctlsize32() can use argp, it must do so only in a way
5157c478bd9Sstevel@tonic-gate  * that assumes 32-bit rather than 64-bit alignment as argp is a pointer
5167c478bd9Sstevel@tonic-gate  * to an array of 32-bit values and only 32-bit alignment is ensured.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate static size_t
5197c478bd9Sstevel@tonic-gate ctlsize32(int32_t cmd, size_t resid, arg32_t *argp)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	size_t size = sizeof (int32_t);
5227c478bd9Sstevel@tonic-gate 	size_t rnd;
5237c478bd9Sstevel@tonic-gate 	int ngrp;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	switch (cmd) {
5267c478bd9Sstevel@tonic-gate 	case PCNULL:
5277c478bd9Sstevel@tonic-gate 	case PCSTOP:
5287c478bd9Sstevel@tonic-gate 	case PCDSTOP:
5297c478bd9Sstevel@tonic-gate 	case PCWSTOP:
5307c478bd9Sstevel@tonic-gate 	case PCCSIG:
5317c478bd9Sstevel@tonic-gate 	case PCCFAULT:
5327c478bd9Sstevel@tonic-gate 		break;
5337c478bd9Sstevel@tonic-gate 	case PCSSIG:
5347c478bd9Sstevel@tonic-gate 		size += sizeof (siginfo32_t);
5357c478bd9Sstevel@tonic-gate 		break;
5367c478bd9Sstevel@tonic-gate 	case PCTWSTOP:
5377c478bd9Sstevel@tonic-gate 		size += sizeof (int32_t);
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 	case PCKILL:
5407c478bd9Sstevel@tonic-gate 	case PCUNKILL:
5417c478bd9Sstevel@tonic-gate 	case PCNICE:
5427c478bd9Sstevel@tonic-gate 		size += sizeof (int32_t);
5437c478bd9Sstevel@tonic-gate 		break;
5447c478bd9Sstevel@tonic-gate 	case PCRUN:
5457c478bd9Sstevel@tonic-gate 	case PCSET:
5467c478bd9Sstevel@tonic-gate 	case PCUNSET:
5477c478bd9Sstevel@tonic-gate 		size += sizeof (uint32_t);
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 	case PCSVADDR:
5507c478bd9Sstevel@tonic-gate 		size += sizeof (caddr32_t);
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 	case PCSTRACE:
5537c478bd9Sstevel@tonic-gate 	case PCSHOLD:
5547c478bd9Sstevel@tonic-gate 		size += sizeof (sigset_t);
5557c478bd9Sstevel@tonic-gate 		break;
5567c478bd9Sstevel@tonic-gate 	case PCSFAULT:
5577c478bd9Sstevel@tonic-gate 		size += sizeof (fltset_t);
5587c478bd9Sstevel@tonic-gate 		break;
5597c478bd9Sstevel@tonic-gate 	case PCSENTRY:
5607c478bd9Sstevel@tonic-gate 	case PCSEXIT:
5617c478bd9Sstevel@tonic-gate 		size += sizeof (sysset_t);
5627c478bd9Sstevel@tonic-gate 		break;
5637c478bd9Sstevel@tonic-gate 	case PCSREG:
5647c478bd9Sstevel@tonic-gate 	case PCAGENT:
5657c478bd9Sstevel@tonic-gate 		size += sizeof (prgregset32_t);
5667c478bd9Sstevel@tonic-gate 		break;
5677c478bd9Sstevel@tonic-gate 	case PCSFPREG:
5687c478bd9Sstevel@tonic-gate 		size += sizeof (prfpregset32_t);
5697c478bd9Sstevel@tonic-gate 		break;
5707c478bd9Sstevel@tonic-gate #if defined(__sparc)
5717c478bd9Sstevel@tonic-gate 	case PCSXREG:
5727c478bd9Sstevel@tonic-gate 		size += sizeof (prxregset_t);
5737c478bd9Sstevel@tonic-gate 		break;
5747c478bd9Sstevel@tonic-gate #endif
5757c478bd9Sstevel@tonic-gate 	case PCWATCH:
5767c478bd9Sstevel@tonic-gate 		size += sizeof (prwatch32_t);
5777c478bd9Sstevel@tonic-gate 		break;
5787c478bd9Sstevel@tonic-gate 	case PCREAD:
5797c478bd9Sstevel@tonic-gate 	case PCWRITE:
5807c478bd9Sstevel@tonic-gate 		size += sizeof (priovec32_t);
5817c478bd9Sstevel@tonic-gate 		break;
5827c478bd9Sstevel@tonic-gate 	case PCSCRED:
5837c478bd9Sstevel@tonic-gate 		size += sizeof (prcred32_t);
5847c478bd9Sstevel@tonic-gate 		break;
5857c478bd9Sstevel@tonic-gate 	case PCSCREDX:
5867c478bd9Sstevel@tonic-gate 		/*
5877c478bd9Sstevel@tonic-gate 		 * We cannot derefence the pr_ngroups fields if it
5887c478bd9Sstevel@tonic-gate 		 * we don't have enough data.
5897c478bd9Sstevel@tonic-gate 		 */
5907c478bd9Sstevel@tonic-gate 		if (resid < size + sizeof (prcred32_t) - sizeof (gid32_t))
5917c478bd9Sstevel@tonic-gate 			return (0);
5927c478bd9Sstevel@tonic-gate 		ngrp = argp->prcred.pr_ngroups;
5937c478bd9Sstevel@tonic-gate 		if (ngrp < 0 || ngrp > ngroups_max)
5947c478bd9Sstevel@tonic-gate 			return (0);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		/* The result can be smaller than sizeof (prcred32_t) */
5977c478bd9Sstevel@tonic-gate 		size += sizeof (prcred32_t) - sizeof (gid32_t);
5987c478bd9Sstevel@tonic-gate 		size += ngrp * sizeof (gid32_t);
5997c478bd9Sstevel@tonic-gate 		break;
6007c478bd9Sstevel@tonic-gate 	case PCSPRIV:
6017c478bd9Sstevel@tonic-gate 		if (resid >= size + sizeof (prpriv_t))
6027c478bd9Sstevel@tonic-gate 			size += priv_prgetprivsize(&argp->prpriv);
6037c478bd9Sstevel@tonic-gate 		else
6047c478bd9Sstevel@tonic-gate 			return (0);
6057c478bd9Sstevel@tonic-gate 		break;
6067c478bd9Sstevel@tonic-gate 	case PCSZONE:
6077c478bd9Sstevel@tonic-gate 		size += sizeof (int32_t);
6087c478bd9Sstevel@tonic-gate 		break;
6097c478bd9Sstevel@tonic-gate 	default:
6107c478bd9Sstevel@tonic-gate 		return (0);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/* Round up to a multiple of int32_t */
6147c478bd9Sstevel@tonic-gate 	rnd = size & (sizeof (int32_t) - 1);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (rnd != 0)
6177c478bd9Sstevel@tonic-gate 		size += sizeof (int32_t) - rnd;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (size > resid)
6207c478bd9Sstevel@tonic-gate 		return (0);
6217c478bd9Sstevel@tonic-gate 	return (size);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Control operations (lots).
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate int
6287c478bd9Sstevel@tonic-gate prwritectl32(struct vnode *vp, struct uio *uiop, cred_t *cr)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate #define	MY_BUFFER_SIZE32 \
6317c478bd9Sstevel@tonic-gate 		100 > 1 + sizeof (arg32_t) / sizeof (int32_t) ? \
6327c478bd9Sstevel@tonic-gate 		100 : 1 + sizeof (arg32_t) / sizeof (int32_t)
6337c478bd9Sstevel@tonic-gate 	int32_t buf[MY_BUFFER_SIZE32];
6347c478bd9Sstevel@tonic-gate 	int32_t *bufp;
6357c478bd9Sstevel@tonic-gate 	arg32_t arg;
6367c478bd9Sstevel@tonic-gate 	size_t resid = 0;
6377c478bd9Sstevel@tonic-gate 	size_t size;
6387c478bd9Sstevel@tonic-gate 	prnode_t *pnp = VTOP(vp);
6397c478bd9Sstevel@tonic-gate 	int error;
6407c478bd9Sstevel@tonic-gate 	int locked = 0;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	while (uiop->uio_resid) {
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * Read several commands in one gulp.
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate 		bufp = buf;
6477c478bd9Sstevel@tonic-gate 		if (resid) {	/* move incomplete command to front of buffer */
6487c478bd9Sstevel@tonic-gate 			int32_t *tail;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 			if (resid >= sizeof (buf))
6517c478bd9Sstevel@tonic-gate 				break;
6527c478bd9Sstevel@tonic-gate 			tail = (int32_t *)((char *)buf + sizeof (buf) - resid);
6537c478bd9Sstevel@tonic-gate 			do {
6547c478bd9Sstevel@tonic-gate 				*bufp++ = *tail++;
6557c478bd9Sstevel@tonic-gate 			} while ((resid -= sizeof (int32_t)) != 0);
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		resid = sizeof (buf) - ((char *)bufp - (char *)buf);
6587c478bd9Sstevel@tonic-gate 		if (resid > uiop->uio_resid)
6597c478bd9Sstevel@tonic-gate 			resid = uiop->uio_resid;
6607c478bd9Sstevel@tonic-gate 		if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
6617c478bd9Sstevel@tonic-gate 			return (error);
6627c478bd9Sstevel@tonic-gate 		resid += (char *)bufp - (char *)buf;
6637c478bd9Sstevel@tonic-gate 		bufp = buf;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		do {		/* loop over commands in buffer */
6667c478bd9Sstevel@tonic-gate 			int32_t cmd = bufp[0];
6677c478bd9Sstevel@tonic-gate 			arg32_t *argp = (arg32_t *)&bufp[1];
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			size = ctlsize32(cmd, resid, argp);
6707c478bd9Sstevel@tonic-gate 			if (size == 0)	/* incomplete or invalid command */
6717c478bd9Sstevel@tonic-gate 				break;
6727c478bd9Sstevel@tonic-gate 			/*
6737c478bd9Sstevel@tonic-gate 			 * Perform the specified control operation.
6747c478bd9Sstevel@tonic-gate 			 */
6757c478bd9Sstevel@tonic-gate 			if (!locked) {
6767c478bd9Sstevel@tonic-gate 				if ((error = prlock(pnp, ZNO)) != 0)
6777c478bd9Sstevel@tonic-gate 					return (error);
6787c478bd9Sstevel@tonic-gate 				locked = 1;
6797c478bd9Sstevel@tonic-gate 			}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * Since some members of the arg32_t union contain
6837c478bd9Sstevel@tonic-gate 			 * 64-bit values (which must be 64-bit aligned), we
6847c478bd9Sstevel@tonic-gate 			 * can't simply pass a pointer to the structure as
6857c478bd9Sstevel@tonic-gate 			 * it may be unaligned. Note that we do pass the
6867c478bd9Sstevel@tonic-gate 			 * potentially unaligned structure to ctlsize32()
6877c478bd9Sstevel@tonic-gate 			 * above, but that uses it a way that makes no
6887c478bd9Sstevel@tonic-gate 			 * assumptions about alignment.
6897c478bd9Sstevel@tonic-gate 			 */
6907c478bd9Sstevel@tonic-gate 			ASSERT(size - sizeof (cmd) <= sizeof (arg));
6917c478bd9Sstevel@tonic-gate 			bcopy(argp, &arg, size - sizeof (cmd));
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 			if (error = pr_control32(cmd, &arg, pnp, cr)) {
6947c478bd9Sstevel@tonic-gate 				if (error == -1)	/* -1 is timeout */
6957c478bd9Sstevel@tonic-gate 					locked = 0;
6967c478bd9Sstevel@tonic-gate 				else
6977c478bd9Sstevel@tonic-gate 					return (error);
6987c478bd9Sstevel@tonic-gate 			}
6997c478bd9Sstevel@tonic-gate 			bufp = (int32_t *)((char *)bufp + size);
7007c478bd9Sstevel@tonic-gate 		} while ((resid -= size) != 0);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		if (locked) {
7037c478bd9Sstevel@tonic-gate 			prunlock(pnp);
7047c478bd9Sstevel@tonic-gate 			locked = 0;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	return (resid? EINVAL : 0);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate static int
7117c478bd9Sstevel@tonic-gate pr_control32(int32_t cmd, arg32_t *argp, prnode_t *pnp, cred_t *cr)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	prcommon_t *pcp;
7147c478bd9Sstevel@tonic-gate 	proc_t *p;
7157c478bd9Sstevel@tonic-gate 	int unlocked;
7167c478bd9Sstevel@tonic-gate 	int error = 0;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	if (cmd == PCNULL)
7197c478bd9Sstevel@tonic-gate 		return (0);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	pcp = pnp->pr_common;
7227c478bd9Sstevel@tonic-gate 	p = pcp->prc_proc;
7237c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	switch (cmd) {
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	default:
7287c478bd9Sstevel@tonic-gate 		error = EINVAL;
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	case PCSTOP:	/* direct process or lwp to stop and wait for stop */
7327c478bd9Sstevel@tonic-gate 	case PCDSTOP:	/* direct process or lwp to stop, don't wait */
7337c478bd9Sstevel@tonic-gate 	case PCWSTOP:	/* wait for process or lwp to stop */
7347c478bd9Sstevel@tonic-gate 	case PCTWSTOP:	/* wait for process or lwp to stop, with timeout */
7357c478bd9Sstevel@tonic-gate 	    {
7367c478bd9Sstevel@tonic-gate 		time_t timeo;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		/*
7397c478bd9Sstevel@tonic-gate 		 * Can't apply to a system process.
7407c478bd9Sstevel@tonic-gate 		 */
7417c478bd9Sstevel@tonic-gate 		if ((p->p_flag & SSYS) || p->p_as == &kas) {
7427c478bd9Sstevel@tonic-gate 			error = EBUSY;
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		if (cmd == PCSTOP || cmd == PCDSTOP)
7477c478bd9Sstevel@tonic-gate 			pr_stop(pnp);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		if (cmd == PCDSTOP)
7507c478bd9Sstevel@tonic-gate 			break;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		/*
7537c478bd9Sstevel@tonic-gate 		 * If an lwp is waiting for itself or its process, don't wait.
7547c478bd9Sstevel@tonic-gate 		 * The lwp will never see the fact that itself is stopped.
7557c478bd9Sstevel@tonic-gate 		 */
7567c478bd9Sstevel@tonic-gate 		if ((pcp->prc_flags & PRC_LWP)?
7577c478bd9Sstevel@tonic-gate 		    (pcp->prc_thread == curthread) : (p == curproc)) {
7587c478bd9Sstevel@tonic-gate 			if (cmd == PCWSTOP || cmd == PCTWSTOP)
7597c478bd9Sstevel@tonic-gate 				error = EBUSY;
7607c478bd9Sstevel@tonic-gate 			break;
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
7647c478bd9Sstevel@tonic-gate 		if ((error = pr_wait_stop(pnp, timeo)) != 0)
7657c478bd9Sstevel@tonic-gate 			return (error);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 		break;
7687c478bd9Sstevel@tonic-gate 	    }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	case PCRUN:	/* make lwp or process runnable */
7717c478bd9Sstevel@tonic-gate 		error = pr_setrun(pnp, (ulong_t)argp->flags);
7727c478bd9Sstevel@tonic-gate 		break;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	case PCSTRACE:	/* set signal trace mask */
7757c478bd9Sstevel@tonic-gate 		pr_settrace(p,  &argp->sigset);
7767c478bd9Sstevel@tonic-gate 		break;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	case PCSSIG:	/* set current signal */
7797c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
7807c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
7817c478bd9Sstevel@tonic-gate 		else {
7827c478bd9Sstevel@tonic-gate 			int sig = (int)argp->siginfo.si_signo;
7837c478bd9Sstevel@tonic-gate 			siginfo_t siginfo;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 			bzero(&siginfo, sizeof (siginfo));
7867c478bd9Sstevel@tonic-gate 			siginfo_32tok(&argp->siginfo, (k_siginfo_t *)&siginfo);
7877c478bd9Sstevel@tonic-gate 			error = pr_setsig(pnp, &siginfo);
7887c478bd9Sstevel@tonic-gate 			if (sig == SIGKILL && error == 0) {
7897c478bd9Sstevel@tonic-gate 				prunlock(pnp);
7907c478bd9Sstevel@tonic-gate 				pr_wait_die(pnp);
7917c478bd9Sstevel@tonic-gate 				return (-1);
7927c478bd9Sstevel@tonic-gate 			}
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	case PCKILL:	/* send signal */
7977c478bd9Sstevel@tonic-gate 		error = pr_kill(pnp, (int)argp->sig, cr);
7987c478bd9Sstevel@tonic-gate 		if (error == 0 && argp->sig == SIGKILL) {
7997c478bd9Sstevel@tonic-gate 			prunlock(pnp);
8007c478bd9Sstevel@tonic-gate 			pr_wait_die(pnp);
8017c478bd9Sstevel@tonic-gate 			return (-1);
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	case PCUNKILL:	/* delete a pending signal */
8067c478bd9Sstevel@tonic-gate 		error = pr_unkill(pnp, (int)argp->sig);
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	case PCNICE:	/* set nice priority */
8107c478bd9Sstevel@tonic-gate 		error = pr_nice(p, (int)argp->nice, cr);
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	case PCSENTRY:	/* set syscall entry bit mask */
8147c478bd9Sstevel@tonic-gate 	case PCSEXIT:	/* set syscall exit bit mask */
8157c478bd9Sstevel@tonic-gate 		pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
8167c478bd9Sstevel@tonic-gate 		break;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	case PCSET:	/* set process flags */
8197c478bd9Sstevel@tonic-gate 		error = pr_set(p, (long)argp->flags);
8207c478bd9Sstevel@tonic-gate 		break;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	case PCUNSET:	/* unset process flags */
8237c478bd9Sstevel@tonic-gate 		error = pr_unset(p, (long)argp->flags);
8247c478bd9Sstevel@tonic-gate 		break;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	case PCSREG:	/* set general registers */
8277c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
8287c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
8297c478bd9Sstevel@tonic-gate 		else {
8307c478bd9Sstevel@tonic-gate 			kthread_t *t = pr_thread(pnp);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 			if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
8337c478bd9Sstevel@tonic-gate 				thread_unlock(t);
8347c478bd9Sstevel@tonic-gate 				error = EBUSY;
8357c478bd9Sstevel@tonic-gate 			} else {
8367c478bd9Sstevel@tonic-gate 				prgregset_t prgregset;
8377c478bd9Sstevel@tonic-gate 				klwp_t *lwp = ttolwp(t);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 				thread_unlock(t);
8407c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
8417c478bd9Sstevel@tonic-gate 				prgregset_32ton(lwp, argp->prgregset,
8427c478bd9Sstevel@tonic-gate 					prgregset);
8437c478bd9Sstevel@tonic-gate 				prsetprregs(lwp, prgregset, 0);
8447c478bd9Sstevel@tonic-gate 				mutex_enter(&p->p_lock);
8457c478bd9Sstevel@tonic-gate 			}
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		break;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	case PCSFPREG:	/* set floating-point registers */
8507c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
8517c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
8527c478bd9Sstevel@tonic-gate 		else
8537c478bd9Sstevel@tonic-gate 			error = pr_setfpregs32(pnp, &argp->prfpregset);
8547c478bd9Sstevel@tonic-gate 		break;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	case PCSXREG:	/* set extra registers */
8577c478bd9Sstevel@tonic-gate #if defined(__sparc)
8587c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
8597c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
8607c478bd9Sstevel@tonic-gate 		else
8617c478bd9Sstevel@tonic-gate 			error = pr_setxregs(pnp, &argp->prxregset);
8627c478bd9Sstevel@tonic-gate #else
8637c478bd9Sstevel@tonic-gate 		error = EINVAL;
8647c478bd9Sstevel@tonic-gate #endif
8657c478bd9Sstevel@tonic-gate 		break;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	case PCSVADDR:	/* set virtual address at which to resume */
8687c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
8697c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
8707c478bd9Sstevel@tonic-gate 		else
8717c478bd9Sstevel@tonic-gate 			error = pr_setvaddr(pnp,
8727c478bd9Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)argp->vaddr);
8737c478bd9Sstevel@tonic-gate 		break;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	case PCSHOLD:	/* set signal-hold mask */
8767c478bd9Sstevel@tonic-gate 		pr_sethold(pnp, &argp->sigset);
8777c478bd9Sstevel@tonic-gate 		break;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	case PCSFAULT:	/* set mask of traced faults */
8807c478bd9Sstevel@tonic-gate 		pr_setfault(p, &argp->fltset);
8817c478bd9Sstevel@tonic-gate 		break;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	case PCCSIG:	/* clear current signal */
8847c478bd9Sstevel@tonic-gate 		error = pr_clearsig(pnp);
8857c478bd9Sstevel@tonic-gate 		break;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	case PCCFAULT:	/* clear current fault */
8887c478bd9Sstevel@tonic-gate 		error = pr_clearflt(pnp);
8897c478bd9Sstevel@tonic-gate 		break;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	case PCWATCH:	/* set or clear watched areas */
8927c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
8937c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
8947c478bd9Sstevel@tonic-gate 		else {
8957c478bd9Sstevel@tonic-gate 			prwatch_t prwatch;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 			prwatch.pr_vaddr = argp->prwatch.pr_vaddr;
8987c478bd9Sstevel@tonic-gate 			prwatch.pr_size = argp->prwatch.pr_size;
8997c478bd9Sstevel@tonic-gate 			prwatch.pr_wflags = argp->prwatch.pr_wflags;
9007c478bd9Sstevel@tonic-gate 			prwatch.pr_pad = argp->prwatch.pr_pad;
9017c478bd9Sstevel@tonic-gate 			error = pr_watch(pnp, &prwatch, &unlocked);
9027c478bd9Sstevel@tonic-gate 			if (error && unlocked)
9037c478bd9Sstevel@tonic-gate 				return (error);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		break;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	case PCAGENT:	/* create the /proc agent lwp in the target process */
9087c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
9097c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
9107c478bd9Sstevel@tonic-gate 		else {
9117c478bd9Sstevel@tonic-gate 			prgregset_t prgregset;
9127c478bd9Sstevel@tonic-gate 			kthread_t *t = pr_thread(pnp);
9137c478bd9Sstevel@tonic-gate 			klwp_t *lwp = ttolwp(t);
9147c478bd9Sstevel@tonic-gate 			thread_unlock(t);
9157c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
9167c478bd9Sstevel@tonic-gate 			prgregset_32ton(lwp, argp->prgregset, prgregset);
9177c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9187c478bd9Sstevel@tonic-gate 			error = pr_agent(pnp, prgregset, &unlocked);
9197c478bd9Sstevel@tonic-gate 			if (error && unlocked)
9207c478bd9Sstevel@tonic-gate 				return (error);
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		break;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	case PCREAD:	/* read from the address space */
9257c478bd9Sstevel@tonic-gate 	case PCWRITE:	/* write to the address space */
9267c478bd9Sstevel@tonic-gate 		if (PROCESS_NOT_32BIT(p))
9277c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
9287c478bd9Sstevel@tonic-gate 		else {
9297c478bd9Sstevel@tonic-gate 			enum uio_rw rw = (cmd == PCREAD)? UIO_READ : UIO_WRITE;
9307c478bd9Sstevel@tonic-gate 			priovec_t priovec;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 			priovec.pio_base =
9337c478bd9Sstevel@tonic-gate 			    (void *)(uintptr_t)argp->priovec.pio_base;
9347c478bd9Sstevel@tonic-gate 			priovec.pio_len = (size_t)argp->priovec.pio_len;
9357c478bd9Sstevel@tonic-gate 			priovec.pio_offset = (off_t)
9367c478bd9Sstevel@tonic-gate 				(uint32_t)argp->priovec.pio_offset;
9377c478bd9Sstevel@tonic-gate 			error = pr_rdwr(p, rw, &priovec);
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 		break;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	case PCSCRED:	/* set the process credentials */
9427c478bd9Sstevel@tonic-gate 	case PCSCREDX:
9437c478bd9Sstevel@tonic-gate 	    {
9447c478bd9Sstevel@tonic-gate 		/*
9457c478bd9Sstevel@tonic-gate 		 * All the fields in these structures are exactly the same
9467c478bd9Sstevel@tonic-gate 		 * and so the structures are compatible.  In case this
9477c478bd9Sstevel@tonic-gate 		 * ever changes, we catch this with the ASSERT below.
9487c478bd9Sstevel@tonic-gate 		 */
9497c478bd9Sstevel@tonic-gate 		prcred_t *prcred = (prcred_t *)&argp->prcred;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate #ifndef __lint
9527c478bd9Sstevel@tonic-gate 		ASSERT(sizeof (prcred_t) == sizeof (prcred32_t));
9537c478bd9Sstevel@tonic-gate #endif
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		error = pr_scred(p, prcred, cr, cmd == PCSCREDX);
9567c478bd9Sstevel@tonic-gate 		break;
9577c478bd9Sstevel@tonic-gate 	    }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	case PCSPRIV:	/* set the process privileges */
9607c478bd9Sstevel@tonic-gate 	    {
9617c478bd9Sstevel@tonic-gate 		error = pr_spriv(p, &argp->prpriv, cr);
9627c478bd9Sstevel@tonic-gate 		break;
9637c478bd9Sstevel@tonic-gate 	    }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	case PCSZONE:	/* set the process's zoneid */
9667c478bd9Sstevel@tonic-gate 	    error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
9677c478bd9Sstevel@tonic-gate 	    break;
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (error)
9717c478bd9Sstevel@tonic-gate 		prunlock(pnp);
9727c478bd9Sstevel@tonic-gate 	return (error);
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate /*
9787c478bd9Sstevel@tonic-gate  * Return the specific or chosen thread/lwp for a control operation.
9797c478bd9Sstevel@tonic-gate  * Returns with the thread locked via thread_lock(t).
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate kthread_t *
9827c478bd9Sstevel@tonic-gate pr_thread(prnode_t *pnp)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
9857c478bd9Sstevel@tonic-gate 	kthread_t *t;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (pcp->prc_flags & PRC_LWP) {
9887c478bd9Sstevel@tonic-gate 		t = pcp->prc_thread;
9897c478bd9Sstevel@tonic-gate 		ASSERT(t != NULL);
9907c478bd9Sstevel@tonic-gate 		thread_lock(t);
9917c478bd9Sstevel@tonic-gate 	} else {
9927c478bd9Sstevel@tonic-gate 		proc_t *p = pcp->prc_proc;
9937c478bd9Sstevel@tonic-gate 		t = prchoose(p);	/* returns locked thread */
9947c478bd9Sstevel@tonic-gate 		ASSERT(t != NULL);
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	return (t);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate  * Direct the process or lwp to stop.
10027c478bd9Sstevel@tonic-gate  */
10037c478bd9Sstevel@tonic-gate void
10047c478bd9Sstevel@tonic-gate pr_stop(prnode_t *pnp)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
10077c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
10087c478bd9Sstevel@tonic-gate 	kthread_t *t;
10097c478bd9Sstevel@tonic-gate 	vnode_t *vp;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * If already stopped, do nothing; otherwise flag
10137c478bd9Sstevel@tonic-gate 	 * it to be stopped the next time it tries to run.
10147c478bd9Sstevel@tonic-gate 	 * If sleeping at interruptible priority, set it
10157c478bd9Sstevel@tonic-gate 	 * running so it will stop within cv_wait_sig().
10167c478bd9Sstevel@tonic-gate 	 *
10177c478bd9Sstevel@tonic-gate 	 * Take care to cooperate with jobcontrol: if an lwp
10187c478bd9Sstevel@tonic-gate 	 * is stopped due to the default action of a jobcontrol
10197c478bd9Sstevel@tonic-gate 	 * stop signal, flag it to be stopped the next time it
10207c478bd9Sstevel@tonic-gate 	 * starts due to a SIGCONT signal.
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	if (pcp->prc_flags & PRC_LWP)
10237c478bd9Sstevel@tonic-gate 		t = pcp->prc_thread;
10247c478bd9Sstevel@tonic-gate 	else
10257c478bd9Sstevel@tonic-gate 		t = p->p_tlist;
10267c478bd9Sstevel@tonic-gate 	ASSERT(t != NULL);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	do {
10297c478bd9Sstevel@tonic-gate 		int notify;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		notify = 0;
10327c478bd9Sstevel@tonic-gate 		thread_lock(t);
10337c478bd9Sstevel@tonic-gate 		if (!ISTOPPED(t)) {
10347c478bd9Sstevel@tonic-gate 			t->t_proc_flag |= TP_PRSTOP;
10357c478bd9Sstevel@tonic-gate 			t->t_sig_check = 1;	/* do ISSIG */
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP &&
10387c478bd9Sstevel@tonic-gate 		    (t->t_flag & T_WAKEABLE)) {
10397c478bd9Sstevel@tonic-gate 			if (t->t_wchan0 == NULL)
10407c478bd9Sstevel@tonic-gate 				setrun_locked(t);
10417c478bd9Sstevel@tonic-gate 			else if (!VSTOPPED(t)) {
10427c478bd9Sstevel@tonic-gate 				/*
10437c478bd9Sstevel@tonic-gate 				 * Mark it virtually stopped.
10447c478bd9Sstevel@tonic-gate 				 */
10457c478bd9Sstevel@tonic-gate 				t->t_proc_flag |= TP_PRVSTOP;
10467c478bd9Sstevel@tonic-gate 				notify = 1;
10477c478bd9Sstevel@tonic-gate 			}
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 		/*
10507c478bd9Sstevel@tonic-gate 		 * force the thread into the kernel
10517c478bd9Sstevel@tonic-gate 		 * if it is not already there.
10527c478bd9Sstevel@tonic-gate 		 */
10537c478bd9Sstevel@tonic-gate 		prpokethread(t);
10547c478bd9Sstevel@tonic-gate 		thread_unlock(t);
10557c478bd9Sstevel@tonic-gate 		if (notify &&
10567c478bd9Sstevel@tonic-gate 		    (vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace) != NULL)
10577c478bd9Sstevel@tonic-gate 			prnotify(vp);
10587c478bd9Sstevel@tonic-gate 		if (pcp->prc_flags & PRC_LWP)
10597c478bd9Sstevel@tonic-gate 			break;
10607c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/*
10637c478bd9Sstevel@tonic-gate 	 * We do this just in case the thread we asked
10647c478bd9Sstevel@tonic-gate 	 * to stop is in holdlwps() (called from cfork()).
10657c478bd9Sstevel@tonic-gate 	 */
10667c478bd9Sstevel@tonic-gate 	cv_broadcast(&p->p_holdlwps);
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate /*
10707c478bd9Sstevel@tonic-gate  * Sleep until the lwp stops, but cooperate with
10717c478bd9Sstevel@tonic-gate  * jobcontrol:  Don't wake up if the lwp is stopped
10727c478bd9Sstevel@tonic-gate  * due to the default action of a jobcontrol stop signal.
10737c478bd9Sstevel@tonic-gate  * If this is the process file descriptor, sleep
10747c478bd9Sstevel@tonic-gate  * until all of the process's lwps stop.
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate int
10777c478bd9Sstevel@tonic-gate pr_wait_stop(prnode_t *pnp, time_t timeo)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
10807c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
10817c478bd9Sstevel@tonic-gate 	timestruc_t rqtime;
10827c478bd9Sstevel@tonic-gate 	timestruc_t *rqtp = NULL;
10837c478bd9Sstevel@tonic-gate 	kthread_t *t;
10847c478bd9Sstevel@tonic-gate 	int error;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (timeo > 0) {	/* millisecond timeout */
10877c478bd9Sstevel@tonic-gate 		/*
10887c478bd9Sstevel@tonic-gate 		 * Determine the precise future time of the requested timeout.
10897c478bd9Sstevel@tonic-gate 		 */
10907c478bd9Sstevel@tonic-gate 		timestruc_t now;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		gethrestime(&now);
10937c478bd9Sstevel@tonic-gate 		rqtp = &rqtime;
10947c478bd9Sstevel@tonic-gate 		rqtp->tv_sec = timeo / MILLISEC;
10957c478bd9Sstevel@tonic-gate 		rqtp->tv_nsec = (timeo % MILLISEC) * MICROSEC;
10967c478bd9Sstevel@tonic-gate 		timespecadd(rqtp, &now);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (pcp->prc_flags & PRC_LWP) {	/* lwp file descriptor */
11007c478bd9Sstevel@tonic-gate 		t = pcp->prc_thread;
11017c478bd9Sstevel@tonic-gate 		ASSERT(t != NULL);
11027c478bd9Sstevel@tonic-gate 		thread_lock(t);
11037c478bd9Sstevel@tonic-gate 		while (!ISTOPPED(t) && !VSTOPPED(t)) {
11047c478bd9Sstevel@tonic-gate 			thread_unlock(t);
11057c478bd9Sstevel@tonic-gate 			mutex_enter(&pcp->prc_mutex);
11067c478bd9Sstevel@tonic-gate 			prunlock(pnp);
1107a913396dSqiao 			error = pr_wait(pcp, rqtp);
11087c478bd9Sstevel@tonic-gate 			if (error)	/* -1 is timeout */
11097c478bd9Sstevel@tonic-gate 				return (error);
11107c478bd9Sstevel@tonic-gate 			if ((error = prlock(pnp, ZNO)) != 0)
11117c478bd9Sstevel@tonic-gate 				return (error);
11127c478bd9Sstevel@tonic-gate 			ASSERT(p == pcp->prc_proc);
11137c478bd9Sstevel@tonic-gate 			ASSERT(t == pcp->prc_thread);
11147c478bd9Sstevel@tonic-gate 			thread_lock(t);
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 		thread_unlock(t);
11177c478bd9Sstevel@tonic-gate 	} else {			/* process file descriptor */
11187c478bd9Sstevel@tonic-gate 		t = prchoose(p);	/* returns locked thread */
11197c478bd9Sstevel@tonic-gate 		ASSERT(t != NULL);
11207c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
11217c478bd9Sstevel@tonic-gate 		while ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t)) ||
11227c478bd9Sstevel@tonic-gate 		    (p->p_flag & SEXITLWPS)) {
11237c478bd9Sstevel@tonic-gate 			thread_unlock(t);
11247c478bd9Sstevel@tonic-gate 			mutex_enter(&pcp->prc_mutex);
11257c478bd9Sstevel@tonic-gate 			prunlock(pnp);
1126a913396dSqiao 			error = pr_wait(pcp, rqtp);
11277c478bd9Sstevel@tonic-gate 			if (error)	/* -1 is timeout */
11287c478bd9Sstevel@tonic-gate 				return (error);
11297c478bd9Sstevel@tonic-gate 			if ((error = prlock(pnp, ZNO)) != 0)
11307c478bd9Sstevel@tonic-gate 				return (error);
11317c478bd9Sstevel@tonic-gate 			ASSERT(p == pcp->prc_proc);
11327c478bd9Sstevel@tonic-gate 			t = prchoose(p);	/* returns locked t */
11337c478bd9Sstevel@tonic-gate 			ASSERT(t != NULL);
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 		thread_unlock(t);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	ASSERT(!(pcp->prc_flags & PRC_DESTROY) && p->p_stat != SZOMB &&
11397c478bd9Sstevel@tonic-gate 	    t != NULL && t->t_state != TS_ZOMB);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	return (0);
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate int
11457c478bd9Sstevel@tonic-gate pr_setrun(prnode_t *pnp, ulong_t flags)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
11487c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
11497c478bd9Sstevel@tonic-gate 	kthread_t *t;
11507c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * Cannot set an lwp running if it is not stopped.
11547c478bd9Sstevel@tonic-gate 	 * Also, no lwp other than the /proc agent lwp can
11557c478bd9Sstevel@tonic-gate 	 * be set running so long as the /proc agent lwp exists.
11567c478bd9Sstevel@tonic-gate 	 */
11577c478bd9Sstevel@tonic-gate 	t = pr_thread(pnp);	/* returns locked thread */
11587c478bd9Sstevel@tonic-gate 	if ((!ISTOPPED(t) && !VSTOPPED(t) &&
11597c478bd9Sstevel@tonic-gate 	    !(t->t_proc_flag & TP_PRSTOP)) ||
11607c478bd9Sstevel@tonic-gate 	    (p->p_agenttp != NULL &&
11617c478bd9Sstevel@tonic-gate 	    (t != p->p_agenttp || !(pcp->prc_flags & PRC_LWP)))) {
11627c478bd9Sstevel@tonic-gate 		thread_unlock(t);
11637c478bd9Sstevel@tonic-gate 		return (EBUSY);
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 	thread_unlock(t);
11667c478bd9Sstevel@tonic-gate 	if (flags & ~(PRCSIG|PRCFAULT|PRSTEP|PRSTOP|PRSABORT))
11677c478bd9Sstevel@tonic-gate 		return (EINVAL);
11687c478bd9Sstevel@tonic-gate 	lwp = ttolwp(t);
11697c478bd9Sstevel@tonic-gate 	if ((flags & PRCSIG) && lwp->lwp_cursig != SIGKILL) {
11707c478bd9Sstevel@tonic-gate 		/*
11717c478bd9Sstevel@tonic-gate 		 * Discard current siginfo_t, if any.
11727c478bd9Sstevel@tonic-gate 		 */
11737c478bd9Sstevel@tonic-gate 		lwp->lwp_cursig = 0;
11747c478bd9Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
11757c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
11767c478bd9Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
11777c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 	if (flags & PRCFAULT)
11817c478bd9Sstevel@tonic-gate 		lwp->lwp_curflt = 0;
11827c478bd9Sstevel@tonic-gate 	/*
11837c478bd9Sstevel@tonic-gate 	 * We can't hold p->p_lock when we touch the lwp's registers.
11847c478bd9Sstevel@tonic-gate 	 * It may be swapped out and we will get a page fault.
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	if (flags & PRSTEP) {
11877c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
11887c478bd9Sstevel@tonic-gate 		prstep(lwp, 0);
11897c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	if (flags & PRSTOP) {
11927c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_PRSTOP;
11937c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;	/* do ISSIG */
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 	if (flags & PRSABORT)
11967c478bd9Sstevel@tonic-gate 		lwp->lwp_sysabort = 1;
11977c478bd9Sstevel@tonic-gate 	thread_lock(t);
11987c478bd9Sstevel@tonic-gate 	if ((pcp->prc_flags & PRC_LWP) || (flags & (PRSTEP|PRSTOP))) {
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * Here, we are dealing with a single lwp.
12017c478bd9Sstevel@tonic-gate 		 */
12027c478bd9Sstevel@tonic-gate 		if (ISTOPPED(t)) {
12037c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_PSTART;
12047c478bd9Sstevel@tonic-gate 			t->t_dtrace_stop = 0;
12057c478bd9Sstevel@tonic-gate 			setrun_locked(t);
12067c478bd9Sstevel@tonic-gate 		} else if (flags & PRSABORT) {
12077c478bd9Sstevel@tonic-gate 			t->t_proc_flag &=
12087c478bd9Sstevel@tonic-gate 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
12097c478bd9Sstevel@tonic-gate 			setrun_locked(t);
12107c478bd9Sstevel@tonic-gate 		} else if (!(flags & PRSTOP)) {
12117c478bd9Sstevel@tonic-gate 			t->t_proc_flag &=
12127c478bd9Sstevel@tonic-gate 			    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 		thread_unlock(t);
12157c478bd9Sstevel@tonic-gate 	} else {
12167c478bd9Sstevel@tonic-gate 		/*
12177c478bd9Sstevel@tonic-gate 		 * Here, we are dealing with the whole process.
12187c478bd9Sstevel@tonic-gate 		 */
12197c478bd9Sstevel@tonic-gate 		if (ISTOPPED(t)) {
12207c478bd9Sstevel@tonic-gate 			/*
12217c478bd9Sstevel@tonic-gate 			 * The representative lwp is stopped on an event
12227c478bd9Sstevel@tonic-gate 			 * of interest.  We demote it to PR_REQUESTED and
12237c478bd9Sstevel@tonic-gate 			 * choose another representative lwp.  If the new
12247c478bd9Sstevel@tonic-gate 			 * representative lwp is not stopped on an event of
12257c478bd9Sstevel@tonic-gate 			 * interest (other than PR_REQUESTED), we set the
12267c478bd9Sstevel@tonic-gate 			 * whole process running, else we leave the process
12277c478bd9Sstevel@tonic-gate 			 * stopped showing the next event of interest.
12287c478bd9Sstevel@tonic-gate 			 */
12297c478bd9Sstevel@tonic-gate 			kthread_t *tx = NULL;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 			if (!(flags & PRSABORT) &&
12327c478bd9Sstevel@tonic-gate 			    t->t_whystop == PR_SYSENTRY &&
12337c478bd9Sstevel@tonic-gate 			    t->t_whatstop == SYS_lwp_exit)
12347c478bd9Sstevel@tonic-gate 				tx = t;		/* remember the exiting lwp */
12357c478bd9Sstevel@tonic-gate 			t->t_whystop = PR_REQUESTED;
12367c478bd9Sstevel@tonic-gate 			t->t_whatstop = 0;
12377c478bd9Sstevel@tonic-gate 			thread_unlock(t);
12387c478bd9Sstevel@tonic-gate 			t = prchoose(p);	/* returns locked t */
12397c478bd9Sstevel@tonic-gate 			ASSERT(ISTOPPED(t) || VSTOPPED(t));
12407c478bd9Sstevel@tonic-gate 			if (VSTOPPED(t) ||
12417c478bd9Sstevel@tonic-gate 			    t->t_whystop == PR_REQUESTED) {
12427c478bd9Sstevel@tonic-gate 				thread_unlock(t);
12437c478bd9Sstevel@tonic-gate 				allsetrun(p);
12447c478bd9Sstevel@tonic-gate 			} else {
12457c478bd9Sstevel@tonic-gate 				thread_unlock(t);
12467c478bd9Sstevel@tonic-gate 				/*
12477c478bd9Sstevel@tonic-gate 				 * As a special case, if the old representative
12487c478bd9Sstevel@tonic-gate 				 * lwp was stopped on entry to _lwp_exit()
12497c478bd9Sstevel@tonic-gate 				 * (and we are not aborting the system call),
12507c478bd9Sstevel@tonic-gate 				 * we set the old representative lwp running.
12517c478bd9Sstevel@tonic-gate 				 * We do this so that the next process stop
12527c478bd9Sstevel@tonic-gate 				 * will find the exiting lwp gone.
12537c478bd9Sstevel@tonic-gate 				 */
12547c478bd9Sstevel@tonic-gate 				if (tx != NULL) {
12557c478bd9Sstevel@tonic-gate 					thread_lock(tx);
12567c478bd9Sstevel@tonic-gate 					tx->t_schedflag |= TS_PSTART;
12577c478bd9Sstevel@tonic-gate 					t->t_dtrace_stop = 0;
12587c478bd9Sstevel@tonic-gate 					setrun_locked(tx);
12597c478bd9Sstevel@tonic-gate 					thread_unlock(tx);
12607c478bd9Sstevel@tonic-gate 				}
12617c478bd9Sstevel@tonic-gate 			}
12627c478bd9Sstevel@tonic-gate 		} else {
12637c478bd9Sstevel@tonic-gate 			/*
12647c478bd9Sstevel@tonic-gate 			 * No event of interest; set all of the lwps running.
12657c478bd9Sstevel@tonic-gate 			 */
12667c478bd9Sstevel@tonic-gate 			if (flags & PRSABORT) {
12677c478bd9Sstevel@tonic-gate 				t->t_proc_flag &=
12687c478bd9Sstevel@tonic-gate 				    ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
12697c478bd9Sstevel@tonic-gate 				setrun_locked(t);
12707c478bd9Sstevel@tonic-gate 			}
12717c478bd9Sstevel@tonic-gate 			thread_unlock(t);
12727c478bd9Sstevel@tonic-gate 			allsetrun(p);
12737c478bd9Sstevel@tonic-gate 		}
12747c478bd9Sstevel@tonic-gate 	}
12757c478bd9Sstevel@tonic-gate 	return (0);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * Wait until process/lwp stops or until timer expires.
12807c478bd9Sstevel@tonic-gate  * Return EINTR for an interruption, -1 for timeout, else 0.
12817c478bd9Sstevel@tonic-gate  */
12827c478bd9Sstevel@tonic-gate int
12837c478bd9Sstevel@tonic-gate pr_wait(prcommon_t *pcp,	/* prcommon referring to process/lwp */
1284a913396dSqiao 	timestruc_t *ts)	/* absolute time of timeout, if any */
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	int rval;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pcp->prc_mutex));
1289a913396dSqiao 	rval = cv_waituntil_sig(&pcp->prc_wait, &pcp->prc_mutex, ts);
12907c478bd9Sstevel@tonic-gate 	mutex_exit(&pcp->prc_mutex);
12917c478bd9Sstevel@tonic-gate 	switch (rval) {
12927c478bd9Sstevel@tonic-gate 	case 0:
12937c478bd9Sstevel@tonic-gate 		return (EINTR);
12947c478bd9Sstevel@tonic-gate 	case -1:
12957c478bd9Sstevel@tonic-gate 		return (-1);
12967c478bd9Sstevel@tonic-gate 	default:
12977c478bd9Sstevel@tonic-gate 		return (0);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * Make all threads in the process runnable.
13037c478bd9Sstevel@tonic-gate  */
13047c478bd9Sstevel@tonic-gate void
13057c478bd9Sstevel@tonic-gate allsetrun(proc_t *p)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	kthread_t *t;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
13127c478bd9Sstevel@tonic-gate 		do {
13137c478bd9Sstevel@tonic-gate 			thread_lock(t);
13147c478bd9Sstevel@tonic-gate 			ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
13157c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
13167c478bd9Sstevel@tonic-gate 			if (ISTOPPED(t)) {
13177c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_PSTART;
13187c478bd9Sstevel@tonic-gate 				t->t_dtrace_stop = 0;
13197c478bd9Sstevel@tonic-gate 				setrun_locked(t);
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 			thread_unlock(t);
13227c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate /*
13277c478bd9Sstevel@tonic-gate  * Wait for the process to die.
13287c478bd9Sstevel@tonic-gate  * We do this after sending SIGKILL because we know it will
13297c478bd9Sstevel@tonic-gate  * die soon and we want subsequent operations to return ENOENT.
13307c478bd9Sstevel@tonic-gate  */
13317c478bd9Sstevel@tonic-gate void
13327c478bd9Sstevel@tonic-gate pr_wait_die(prnode_t *pnp)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	proc_t *p;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
13377c478bd9Sstevel@tonic-gate 	while ((p = pnp->pr_common->prc_proc) != NULL && p->p_stat != SZOMB) {
13387c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&p->p_srwchan_cv, &pidlock))
13397c478bd9Sstevel@tonic-gate 			break;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
13427c478bd9Sstevel@tonic-gate }
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate static void
13457c478bd9Sstevel@tonic-gate pr_settrace(proc_t *p, sigset_t *sp)
13467c478bd9Sstevel@tonic-gate {
13477c478bd9Sstevel@tonic-gate 	prdelset(sp, SIGKILL);
13487c478bd9Sstevel@tonic-gate 	prassignset(&p->p_sigmask, sp);
13497c478bd9Sstevel@tonic-gate 	if (!sigisempty(&p->p_sigmask))
13507c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_TRACE;
13517c478bd9Sstevel@tonic-gate 	else if (prisempty(&p->p_fltmask)) {
13527c478bd9Sstevel@tonic-gate 		user_t *up = PTOU(p);
13537c478bd9Sstevel@tonic-gate 		if (up->u_systrap == 0)
13547c478bd9Sstevel@tonic-gate 			p->p_proc_flag &= ~P_PR_TRACE;
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate int
13597c478bd9Sstevel@tonic-gate pr_setsig(prnode_t *pnp, siginfo_t *sip)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	int sig = sip->si_signo;
13627c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
13637c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
13647c478bd9Sstevel@tonic-gate 	kthread_t *t;
13657c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
13667c478bd9Sstevel@tonic-gate 	int error = 0;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	t = pr_thread(pnp);	/* returns locked thread */
13697c478bd9Sstevel@tonic-gate 	thread_unlock(t);
13707c478bd9Sstevel@tonic-gate 	lwp = ttolwp(t);
13717c478bd9Sstevel@tonic-gate 	if (sig < 0 || sig >= NSIG)
13727c478bd9Sstevel@tonic-gate 		/* Zero allowed here */
13737c478bd9Sstevel@tonic-gate 		error = EINVAL;
13747c478bd9Sstevel@tonic-gate 	else if (lwp->lwp_cursig == SIGKILL)
13757c478bd9Sstevel@tonic-gate 		/* "can't happen", but just in case */
13767c478bd9Sstevel@tonic-gate 		error = EBUSY;
13777c478bd9Sstevel@tonic-gate 	else if ((lwp->lwp_cursig = (uchar_t)sig) == 0) {
13787c478bd9Sstevel@tonic-gate 		lwp->lwp_extsig = 0;
13797c478bd9Sstevel@tonic-gate 		/*
13807c478bd9Sstevel@tonic-gate 		 * Discard current siginfo_t, if any.
13817c478bd9Sstevel@tonic-gate 		 */
13827c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo) {
13837c478bd9Sstevel@tonic-gate 			siginfofree(lwp->lwp_curinfo);
13847c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = NULL;
13857c478bd9Sstevel@tonic-gate 		}
13867c478bd9Sstevel@tonic-gate 	} else {
13877c478bd9Sstevel@tonic-gate 		kthread_t *tx;
13887c478bd9Sstevel@tonic-gate 		sigqueue_t *sqp;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 		/* drop p_lock to do kmem_alloc(KM_SLEEP) */
13917c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
13927c478bd9Sstevel@tonic-gate 		sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
13937c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		if (lwp->lwp_curinfo == NULL)
13967c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo = sqp;
13977c478bd9Sstevel@tonic-gate 		else
13987c478bd9Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
13997c478bd9Sstevel@tonic-gate 		/*
14007c478bd9Sstevel@tonic-gate 		 * Copy contents of info to current siginfo_t.
14017c478bd9Sstevel@tonic-gate 		 */
14027c478bd9Sstevel@tonic-gate 		bcopy(sip, &lwp->lwp_curinfo->sq_info,
14037c478bd9Sstevel@tonic-gate 		    sizeof (lwp->lwp_curinfo->sq_info));
14047c478bd9Sstevel@tonic-gate 		/*
14057c478bd9Sstevel@tonic-gate 		 * Prevent contents published by si_zoneid-unaware /proc
14067c478bd9Sstevel@tonic-gate 		 * consumers from being incorrectly filtered.  Because
14077c478bd9Sstevel@tonic-gate 		 * an uninitialized si_zoneid is the same as
14087c478bd9Sstevel@tonic-gate 		 * GLOBAL_ZONEID, this means that you can't pr_setsig a
14097c478bd9Sstevel@tonic-gate 		 * process in a non-global zone with a siginfo which
14107c478bd9Sstevel@tonic-gate 		 * appears to come from the global zone.
14117c478bd9Sstevel@tonic-gate 		 */
14127c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(sip) && sip->si_zoneid == 0)
14137c478bd9Sstevel@tonic-gate 			lwp->lwp_curinfo->sq_info.si_zoneid =
14147c478bd9Sstevel@tonic-gate 			    p->p_zone->zone_id;
14157c478bd9Sstevel@tonic-gate 		/*
14167c478bd9Sstevel@tonic-gate 		 * Side-effects for SIGKILL and jobcontrol signals.
14177c478bd9Sstevel@tonic-gate 		 */
14187c478bd9Sstevel@tonic-gate 		if (sig == SIGKILL) {
14197c478bd9Sstevel@tonic-gate 			p->p_flag |= SKILLED;
14207c478bd9Sstevel@tonic-gate 			p->p_flag &= ~SEXTKILLED;
14217c478bd9Sstevel@tonic-gate 		} else if (sig == SIGCONT) {
14227c478bd9Sstevel@tonic-gate 			p->p_flag |= SSCONT;
14237c478bd9Sstevel@tonic-gate 			sigdelq(p, NULL, SIGSTOP);
14247c478bd9Sstevel@tonic-gate 			sigdelq(p, NULL, SIGTSTP);
14257c478bd9Sstevel@tonic-gate 			sigdelq(p, NULL, SIGTTOU);
14267c478bd9Sstevel@tonic-gate 			sigdelq(p, NULL, SIGTTIN);
14277c478bd9Sstevel@tonic-gate 			sigdiffset(&p->p_sig, &stopdefault);
14287c478bd9Sstevel@tonic-gate 			sigdiffset(&p->p_extsig, &stopdefault);
14297c478bd9Sstevel@tonic-gate 			if ((tx = p->p_tlist) != NULL) {
14307c478bd9Sstevel@tonic-gate 				do {
14317c478bd9Sstevel@tonic-gate 					sigdelq(p, tx, SIGSTOP);
14327c478bd9Sstevel@tonic-gate 					sigdelq(p, tx, SIGTSTP);
14337c478bd9Sstevel@tonic-gate 					sigdelq(p, tx, SIGTTOU);
14347c478bd9Sstevel@tonic-gate 					sigdelq(p, tx, SIGTTIN);
14357c478bd9Sstevel@tonic-gate 					sigdiffset(&tx->t_sig, &stopdefault);
14367c478bd9Sstevel@tonic-gate 					sigdiffset(&tx->t_extsig, &stopdefault);
14377c478bd9Sstevel@tonic-gate 				} while ((tx = tx->t_forw) != p->p_tlist);
14387c478bd9Sstevel@tonic-gate 			}
14397c478bd9Sstevel@tonic-gate 		} else if (sigismember(&stopdefault, sig)) {
14407c478bd9Sstevel@tonic-gate 			if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
14417c478bd9Sstevel@tonic-gate 			    (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
14427c478bd9Sstevel@tonic-gate 				p->p_flag &= ~SSCONT;
14437c478bd9Sstevel@tonic-gate 			sigdelq(p, NULL, SIGCONT);
14447c478bd9Sstevel@tonic-gate 			sigdelset(&p->p_sig, SIGCONT);
14457c478bd9Sstevel@tonic-gate 			sigdelset(&p->p_extsig, SIGCONT);
14467c478bd9Sstevel@tonic-gate 			if ((tx = p->p_tlist) != NULL) {
14477c478bd9Sstevel@tonic-gate 				do {
14487c478bd9Sstevel@tonic-gate 					sigdelq(p, tx, SIGCONT);
14497c478bd9Sstevel@tonic-gate 					sigdelset(&tx->t_sig, SIGCONT);
14507c478bd9Sstevel@tonic-gate 					sigdelset(&tx->t_extsig, SIGCONT);
14517c478bd9Sstevel@tonic-gate 				} while ((tx = tx->t_forw) != p->p_tlist);
14527c478bd9Sstevel@tonic-gate 			}
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 		thread_lock(t);
14557c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP &&
14567c478bd9Sstevel@tonic-gate 		    (t->t_flag & T_WAKEABLE)) {
14577c478bd9Sstevel@tonic-gate 			/* Set signalled sleeping lwp running */
14587c478bd9Sstevel@tonic-gate 			setrun_locked(t);
14597c478bd9Sstevel@tonic-gate 		} else if (t->t_state == TS_STOPPED && sig == SIGKILL) {
14607c478bd9Sstevel@tonic-gate 			/* If SIGKILL, set stopped lwp running */
14617c478bd9Sstevel@tonic-gate 			p->p_stopsig = 0;
14627c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_XSTART | TS_PSTART;
14637c478bd9Sstevel@tonic-gate 			t->t_dtrace_stop = 0;
14647c478bd9Sstevel@tonic-gate 			setrun_locked(t);
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;	/* so ISSIG will be done */
14677c478bd9Sstevel@tonic-gate 		thread_unlock(t);
14687c478bd9Sstevel@tonic-gate 		/*
14697c478bd9Sstevel@tonic-gate 		 * More jobcontrol side-effects.
14707c478bd9Sstevel@tonic-gate 		 */
14717c478bd9Sstevel@tonic-gate 		if (sig == SIGCONT && (tx = p->p_tlist) != NULL) {
14727c478bd9Sstevel@tonic-gate 			p->p_stopsig = 0;
14737c478bd9Sstevel@tonic-gate 			do {
14747c478bd9Sstevel@tonic-gate 				thread_lock(tx);
14757c478bd9Sstevel@tonic-gate 				if (tx->t_state == TS_STOPPED &&
14767c478bd9Sstevel@tonic-gate 				    tx->t_whystop == PR_JOBCONTROL) {
14777c478bd9Sstevel@tonic-gate 					tx->t_schedflag |= TS_XSTART;
14787c478bd9Sstevel@tonic-gate 					setrun_locked(tx);
14797c478bd9Sstevel@tonic-gate 				}
14807c478bd9Sstevel@tonic-gate 				thread_unlock(tx);
14817c478bd9Sstevel@tonic-gate 			} while ((tx = tx->t_forw) != p->p_tlist);
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 	return (error);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate int
14887c478bd9Sstevel@tonic-gate pr_kill(prnode_t *pnp, int sig, cred_t *cr)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
14917c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
14927c478bd9Sstevel@tonic-gate 	k_siginfo_t info;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG)
14957c478bd9Sstevel@tonic-gate 		return (EINVAL);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	bzero(&info, sizeof (info));
14987c478bd9Sstevel@tonic-gate 	info.si_signo = sig;
14997c478bd9Sstevel@tonic-gate 	info.si_code = SI_USER;
15007c478bd9Sstevel@tonic-gate 	info.si_pid = curproc->p_pid;
15017c478bd9Sstevel@tonic-gate 	info.si_ctid = PRCTID(curproc);
15027c478bd9Sstevel@tonic-gate 	info.si_zoneid = getzoneid();
15037c478bd9Sstevel@tonic-gate 	info.si_uid = crgetruid(cr);
15047c478bd9Sstevel@tonic-gate 	sigaddq(p, (pcp->prc_flags & PRC_LWP)?
15057c478bd9Sstevel@tonic-gate 	    pcp->prc_thread : NULL, &info, KM_NOSLEEP);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	return (0);
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate int
15117c478bd9Sstevel@tonic-gate pr_unkill(prnode_t *pnp, int sig)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	prcommon_t *pcp = pnp->pr_common;
15147c478bd9Sstevel@tonic-gate 	proc_t *p = pcp->prc_proc;
15157c478bd9Sstevel@tonic-gate 	sigqueue_t *infop = NULL;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL)
15187c478bd9Sstevel@tonic-gate 		return (EINVAL);
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	if (pcp->prc_flags & PRC_LWP)
15217c478bd9Sstevel@tonic-gate 		sigdeq(p, pcp->prc_thread, sig, &infop);
15227c478bd9Sstevel@tonic-gate 	else
15237c478bd9Sstevel@tonic-gate 		sigdeq(p, NULL, sig, &infop);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	if (infop)
15267c478bd9Sstevel@tonic-gate 		siginfofree(infop);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	return (0);
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate int
15327c478bd9Sstevel@tonic-gate pr_nice(proc_t *p, int nice, cred_t *cr)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	kthread_t *t;
15357c478bd9Sstevel@tonic-gate 	int err;
15367c478bd9Sstevel@tonic-gate 	int error = 0;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
15397c478bd9Sstevel@tonic-gate 	do {
15407c478bd9Sstevel@tonic-gate 		ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
15417c478bd9Sstevel@tonic-gate 		err = CL_DONICE(t, cr, nice, (int *)NULL);
15427c478bd9Sstevel@tonic-gate 		if (error == 0)
15437c478bd9Sstevel@tonic-gate 			error = err;
15447c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	return (error);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate void
15507c478bd9Sstevel@tonic-gate pr_setentryexit(proc_t *p, sysset_t *sysset, int entry)
15517c478bd9Sstevel@tonic-gate {
15527c478bd9Sstevel@tonic-gate 	user_t *up = PTOU(p);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	if (entry) {
15557c478bd9Sstevel@tonic-gate 		prassignset(&up->u_entrymask, sysset);
15567c478bd9Sstevel@tonic-gate 	} else {
15577c478bd9Sstevel@tonic-gate 		prassignset(&up->u_exitmask, sysset);
15587c478bd9Sstevel@tonic-gate 	}
15597c478bd9Sstevel@tonic-gate 	if (!prisempty(&up->u_entrymask) ||
15607c478bd9Sstevel@tonic-gate 	    !prisempty(&up->u_exitmask)) {
15617c478bd9Sstevel@tonic-gate 		up->u_systrap = 1;
15627c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_TRACE;
15637c478bd9Sstevel@tonic-gate 		set_proc_sys(p);	/* set pre and post-sys flags */
15647c478bd9Sstevel@tonic-gate 	} else {
15657c478bd9Sstevel@tonic-gate 		up->u_systrap = 0;
15667c478bd9Sstevel@tonic-gate 		if (sigisempty(&p->p_sigmask) &&
15677c478bd9Sstevel@tonic-gate 		    prisempty(&p->p_fltmask))
15687c478bd9Sstevel@tonic-gate 			p->p_proc_flag &= ~P_PR_TRACE;
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate #define	ALLFLAGS	\
15737c478bd9Sstevel@tonic-gate 	(PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate int
15767c478bd9Sstevel@tonic-gate pr_set(proc_t *p, long flags)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) || p->p_as == &kas)
15797c478bd9Sstevel@tonic-gate 		return (EBUSY);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	if (flags & ~ALLFLAGS)
15827c478bd9Sstevel@tonic-gate 		return (EINVAL);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (flags & PR_FORK)
15857c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_FORK;
15867c478bd9Sstevel@tonic-gate 	if (flags & PR_RLC)
15877c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_RUNLCL;
15887c478bd9Sstevel@tonic-gate 	if (flags & PR_KLC)
15897c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_KILLCL;
15907c478bd9Sstevel@tonic-gate 	if (flags & PR_ASYNC)
15917c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_ASYNC;
15927c478bd9Sstevel@tonic-gate 	if (flags & PR_BPTADJ)
15937c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_BPTADJ;
15947c478bd9Sstevel@tonic-gate 	if (flags & PR_MSACCT)
15957c478bd9Sstevel@tonic-gate 		if ((p->p_flag & SMSACCT) == 0)
15967c478bd9Sstevel@tonic-gate 			estimate_msacct(p->p_tlist, gethrtime());
15977c478bd9Sstevel@tonic-gate 	if (flags & PR_MSFORK)
15987c478bd9Sstevel@tonic-gate 		p->p_flag |= SMSFORK;
15997c478bd9Sstevel@tonic-gate 	if (flags & PR_PTRACE) {
16007c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_PTRACE;
16017c478bd9Sstevel@tonic-gate 		/* ptraced process must die if parent dead */
16027c478bd9Sstevel@tonic-gate 		if (p->p_ppid == 1)
16037c478bd9Sstevel@tonic-gate 			sigtoproc(p, NULL, SIGKILL);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	return (0);
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate int
16107c478bd9Sstevel@tonic-gate pr_unset(proc_t *p, long flags)
16117c478bd9Sstevel@tonic-gate {
16127c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) || p->p_as == &kas)
16137c478bd9Sstevel@tonic-gate 		return (EBUSY);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if (flags & ~ALLFLAGS)
16167c478bd9Sstevel@tonic-gate 		return (EINVAL);
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	if (flags & PR_FORK)
16197c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_FORK;
16207c478bd9Sstevel@tonic-gate 	if (flags & PR_RLC)
16217c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_RUNLCL;
16227c478bd9Sstevel@tonic-gate 	if (flags & PR_KLC)
16237c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_KILLCL;
16247c478bd9Sstevel@tonic-gate 	if (flags & PR_ASYNC)
16257c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_ASYNC;
16267c478bd9Sstevel@tonic-gate 	if (flags & PR_BPTADJ)
16277c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_BPTADJ;
16287c478bd9Sstevel@tonic-gate 	if (flags & PR_MSACCT)
16297c478bd9Sstevel@tonic-gate 		disable_msacct(p);
16307c478bd9Sstevel@tonic-gate 	if (flags & PR_MSFORK)
16317c478bd9Sstevel@tonic-gate 		p->p_flag &= ~SMSFORK;
16327c478bd9Sstevel@tonic-gate 	if (flags & PR_PTRACE)
16337c478bd9Sstevel@tonic-gate 		p->p_proc_flag &= ~P_PR_PTRACE;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	return (0);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate static int
16397c478bd9Sstevel@tonic-gate pr_setfpregs(prnode_t *pnp, prfpregset_t *prfpregset)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
16427c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
16457c478bd9Sstevel@tonic-gate 		thread_unlock(t);
16467c478bd9Sstevel@tonic-gate 		return (EBUSY);
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate 	if (!prhasfp()) {
16497c478bd9Sstevel@tonic-gate 		thread_unlock(t);
16507c478bd9Sstevel@tonic-gate 		return (EINVAL);	/* No FP support */
16517c478bd9Sstevel@tonic-gate 	}
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/* drop p_lock while touching the lwp's stack */
16547c478bd9Sstevel@tonic-gate 	thread_unlock(t);
16557c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16567c478bd9Sstevel@tonic-gate 	prsetprfpregs(ttolwp(t), prfpregset);
16577c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	return (0);
16607c478bd9Sstevel@tonic-gate }
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
16637c478bd9Sstevel@tonic-gate static int
16647c478bd9Sstevel@tonic-gate pr_setfpregs32(prnode_t *pnp, prfpregset32_t *prfpregset)
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
16677c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
16707c478bd9Sstevel@tonic-gate 		thread_unlock(t);
16717c478bd9Sstevel@tonic-gate 		return (EBUSY);
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 	if (!prhasfp()) {
16747c478bd9Sstevel@tonic-gate 		thread_unlock(t);
16757c478bd9Sstevel@tonic-gate 		return (EINVAL);	/* No FP support */
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	/* drop p_lock while touching the lwp's stack */
16797c478bd9Sstevel@tonic-gate 	thread_unlock(t);
16807c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
16817c478bd9Sstevel@tonic-gate 	prsetprfpregs32(ttolwp(t), prfpregset);
16827c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	return (0);
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate #if defined(__sparc)
16897c478bd9Sstevel@tonic-gate /* ARGSUSED */
16907c478bd9Sstevel@tonic-gate static int
16917c478bd9Sstevel@tonic-gate pr_setxregs(prnode_t *pnp, prxregset_t *prxregset)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
16947c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
16977c478bd9Sstevel@tonic-gate 		thread_unlock(t);
16987c478bd9Sstevel@tonic-gate 		return (EBUSY);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	if (!prhasx(p))
17037c478bd9Sstevel@tonic-gate 		return (EINVAL);	/* No extra register support */
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/* drop p_lock while touching the lwp's stack */
17067c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17077c478bd9Sstevel@tonic-gate 	prsetprxregs(ttolwp(t), (caddr_t)prxregset);
17087c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	return (0);
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate static int
17147c478bd9Sstevel@tonic-gate pr_setasrs(prnode_t *pnp, asrset_t asrset)
17157c478bd9Sstevel@tonic-gate {
17167c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
17177c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
17207c478bd9Sstevel@tonic-gate 		thread_unlock(t);
17217c478bd9Sstevel@tonic-gate 		return (EBUSY);
17227c478bd9Sstevel@tonic-gate 	}
17237c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	/* drop p_lock while touching the lwp's stack */
17267c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17277c478bd9Sstevel@tonic-gate 	prsetasregs(ttolwp(t), asrset);
17287c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	return (0);
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate #endif
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate static int
17357c478bd9Sstevel@tonic-gate pr_setvaddr(prnode_t *pnp, caddr_t vaddr)
17367c478bd9Sstevel@tonic-gate {
17377c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
17387c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
17417c478bd9Sstevel@tonic-gate 		thread_unlock(t);
17427c478bd9Sstevel@tonic-gate 		return (EBUSY);
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	/* drop p_lock while touching the lwp's stack */
17467c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17477c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17487c478bd9Sstevel@tonic-gate 	prsvaddr(ttolwp(t), vaddr);
17497c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	return (0);
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate void
17557c478bd9Sstevel@tonic-gate pr_sethold(prnode_t *pnp, sigset_t *sp)
17567c478bd9Sstevel@tonic-gate {
17577c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
17587c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(t);
17617c478bd9Sstevel@tonic-gate 	sigutok(sp, &t->t_hold);
17627c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP &&
17637c478bd9Sstevel@tonic-gate 	    (t->t_flag & T_WAKEABLE) &&
17647c478bd9Sstevel@tonic-gate 	    (fsig(&p->p_sig, t) || fsig(&t->t_sig, t)))
17657c478bd9Sstevel@tonic-gate 		setrun_locked(t);
17667c478bd9Sstevel@tonic-gate 	t->t_sig_check = 1;	/* so thread will see new holdmask */
17677c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate void
17717c478bd9Sstevel@tonic-gate pr_setfault(proc_t *p, fltset_t *fltp)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	prassignset(&p->p_fltmask, fltp);
17747c478bd9Sstevel@tonic-gate 	if (!prisempty(&p->p_fltmask))
17757c478bd9Sstevel@tonic-gate 		p->p_proc_flag |= P_PR_TRACE;
17767c478bd9Sstevel@tonic-gate 	else if (sigisempty(&p->p_sigmask)) {
17777c478bd9Sstevel@tonic-gate 		user_t *up = PTOU(p);
17787c478bd9Sstevel@tonic-gate 		if (up->u_systrap == 0)
17797c478bd9Sstevel@tonic-gate 			p->p_proc_flag &= ~P_PR_TRACE;
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate static int
17847c478bd9Sstevel@tonic-gate pr_clearsig(prnode_t *pnp)
17857c478bd9Sstevel@tonic-gate {
17867c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
17877c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	thread_unlock(t);
17907c478bd9Sstevel@tonic-gate 	if (lwp->lwp_cursig == SIGKILL)
17917c478bd9Sstevel@tonic-gate 		return (EBUSY);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	/*
17947c478bd9Sstevel@tonic-gate 	 * Discard current siginfo_t, if any.
17957c478bd9Sstevel@tonic-gate 	 */
17967c478bd9Sstevel@tonic-gate 	lwp->lwp_cursig = 0;
17977c478bd9Sstevel@tonic-gate 	lwp->lwp_extsig = 0;
17987c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo) {
17997c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
18007c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	return (0);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate static int
18077c478bd9Sstevel@tonic-gate pr_clearflt(prnode_t *pnp)
18087c478bd9Sstevel@tonic-gate {
18097c478bd9Sstevel@tonic-gate 	kthread_t *t = pr_thread(pnp);	/* returns locked thread */
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	thread_unlock(t);
18127c478bd9Sstevel@tonic-gate 	ttolwp(t)->lwp_curflt = 0;
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	return (0);
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate static int
18187c478bd9Sstevel@tonic-gate pr_watch(prnode_t *pnp, prwatch_t *pwp, int *unlocked)
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
18217c478bd9Sstevel@tonic-gate 	struct as *as = p->p_as;
18227c478bd9Sstevel@tonic-gate 	uintptr_t vaddr = pwp->pr_vaddr;
18237c478bd9Sstevel@tonic-gate 	size_t size = pwp->pr_size;
18247c478bd9Sstevel@tonic-gate 	int wflags = pwp->pr_wflags;
18257c478bd9Sstevel@tonic-gate 	ulong_t newpage = 0;
18267c478bd9Sstevel@tonic-gate 	struct watched_area *pwa;
18277c478bd9Sstevel@tonic-gate 	int error;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	*unlocked = 0;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * Can't apply to a system process.
18337c478bd9Sstevel@tonic-gate 	 */
18347c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) || p->p_as == &kas)
18357c478bd9Sstevel@tonic-gate 		return (EBUSY);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	/*
18387c478bd9Sstevel@tonic-gate 	 * Verify that the address range does not wrap
18397c478bd9Sstevel@tonic-gate 	 * and that only the proper flags were specified.
18407c478bd9Sstevel@tonic-gate 	 */
18417c478bd9Sstevel@tonic-gate 	if ((wflags & ~WA_TRAPAFTER) == 0)
18427c478bd9Sstevel@tonic-gate 		size = 0;
18437c478bd9Sstevel@tonic-gate 	if (vaddr + size < vaddr ||
18447c478bd9Sstevel@tonic-gate 	    (wflags & ~(WA_READ|WA_WRITE|WA_EXEC|WA_TRAPAFTER)) != 0 ||
18457c478bd9Sstevel@tonic-gate 	    ((wflags & ~WA_TRAPAFTER) != 0 && size == 0))
18467c478bd9Sstevel@tonic-gate 		return (EINVAL);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/*
18497c478bd9Sstevel@tonic-gate 	 * Don't let the address range go above as->a_userlimit.
18507c478bd9Sstevel@tonic-gate 	 * There is no error here, just a limitation.
18517c478bd9Sstevel@tonic-gate 	 */
18527c478bd9Sstevel@tonic-gate 	if (vaddr >= (uintptr_t)as->a_userlimit)
18537c478bd9Sstevel@tonic-gate 		return (0);
18547c478bd9Sstevel@tonic-gate 	if (vaddr + size > (uintptr_t)as->a_userlimit)
18557c478bd9Sstevel@tonic-gate 		size = (uintptr_t)as->a_userlimit - vaddr;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	/*
18587c478bd9Sstevel@tonic-gate 	 * Compute maximum number of pages this will add.
18597c478bd9Sstevel@tonic-gate 	 */
18607c478bd9Sstevel@tonic-gate 	if ((wflags & ~WA_TRAPAFTER) != 0) {
18617c478bd9Sstevel@tonic-gate 		ulong_t pagespan = (vaddr + size) - (vaddr & PAGEMASK);
18627c478bd9Sstevel@tonic-gate 		newpage = btopr(pagespan);
18637c478bd9Sstevel@tonic-gate 		if (newpage > 2 * prnwatch)
18647c478bd9Sstevel@tonic-gate 			return (E2BIG);
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	/*
18687c478bd9Sstevel@tonic-gate 	 * Force the process to be fully stopped.
18697c478bd9Sstevel@tonic-gate 	 */
18707c478bd9Sstevel@tonic-gate 	if (p == curproc) {
18717c478bd9Sstevel@tonic-gate 		prunlock(pnp);
18727c478bd9Sstevel@tonic-gate 		while (holdwatch() != 0)
18737c478bd9Sstevel@tonic-gate 			continue;
18747c478bd9Sstevel@tonic-gate 		if ((error = prlock(pnp, ZNO)) != 0) {
18757c478bd9Sstevel@tonic-gate 			continuelwps(p);
18767c478bd9Sstevel@tonic-gate 			*unlocked = 1;
18777c478bd9Sstevel@tonic-gate 			return (error);
18787c478bd9Sstevel@tonic-gate 		}
18797c478bd9Sstevel@tonic-gate 	} else {
18807c478bd9Sstevel@tonic-gate 		pauselwps(p);
18817c478bd9Sstevel@tonic-gate 		while (pr_allstopped(p, 0) > 0) {
18827c478bd9Sstevel@tonic-gate 			/*
18837c478bd9Sstevel@tonic-gate 			 * This cv/mutex pair is persistent even
18847c478bd9Sstevel@tonic-gate 			 * if the process disappears after we
18857c478bd9Sstevel@tonic-gate 			 * unmark it and drop p->p_lock.
18867c478bd9Sstevel@tonic-gate 			 */
18877c478bd9Sstevel@tonic-gate 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
18887c478bd9Sstevel@tonic-gate 			kmutex_t *mp = &p->p_lock;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 			prunmark(p);
18917c478bd9Sstevel@tonic-gate 			(void) cv_wait(cv, mp);
18927c478bd9Sstevel@tonic-gate 			mutex_exit(mp);
18937c478bd9Sstevel@tonic-gate 			if ((error = prlock(pnp, ZNO)) != 0) {
18947c478bd9Sstevel@tonic-gate 				/*
18957c478bd9Sstevel@tonic-gate 				 * Unpause the process if it exists.
18967c478bd9Sstevel@tonic-gate 				 */
18977c478bd9Sstevel@tonic-gate 				p = pr_p_lock(pnp);
18987c478bd9Sstevel@tonic-gate 				mutex_exit(&pr_pidlock);
18997c478bd9Sstevel@tonic-gate 				if (p != NULL) {
19007c478bd9Sstevel@tonic-gate 					unpauselwps(p);
19017c478bd9Sstevel@tonic-gate 					prunlock(pnp);
19027c478bd9Sstevel@tonic-gate 				}
19037c478bd9Sstevel@tonic-gate 				*unlocked = 1;
19047c478bd9Sstevel@tonic-gate 				return (error);
19057c478bd9Sstevel@tonic-gate 			}
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	/*
19107c478bd9Sstevel@tonic-gate 	 * Drop p->p_lock in order to perform the rest of this.
19117c478bd9Sstevel@tonic-gate 	 * The process is still locked with the P_PR_LOCK flag.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	pwa = kmem_alloc(sizeof (struct watched_area), KM_SLEEP);
19167c478bd9Sstevel@tonic-gate 	pwa->wa_vaddr = (caddr_t)vaddr;
19177c478bd9Sstevel@tonic-gate 	pwa->wa_eaddr = (caddr_t)vaddr + size;
19187c478bd9Sstevel@tonic-gate 	pwa->wa_flags = (ulong_t)wflags;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	error = ((pwa->wa_flags & ~WA_TRAPAFTER) == 0)?
19217c478bd9Sstevel@tonic-gate 		clear_watched_area(p, pwa) :
19227c478bd9Sstevel@tonic-gate 		set_watched_area(p, pwa);
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	if (p == curproc) {
19257c478bd9Sstevel@tonic-gate 		setallwatch();
19267c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19277c478bd9Sstevel@tonic-gate 		continuelwps(p);
19287c478bd9Sstevel@tonic-gate 	} else {
19297c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19307c478bd9Sstevel@tonic-gate 		unpauselwps(p);
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	return (error);
19347c478bd9Sstevel@tonic-gate }
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate /* jobcontrol stopped, but with a /proc directed stop in effect */
19377c478bd9Sstevel@tonic-gate #define	JDSTOPPED(t)	\
19387c478bd9Sstevel@tonic-gate 	((t)->t_state == TS_STOPPED && \
19397c478bd9Sstevel@tonic-gate 	(t)->t_whystop == PR_JOBCONTROL && \
19407c478bd9Sstevel@tonic-gate 	((t)->t_proc_flag & TP_PRSTOP))
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate /*
19437c478bd9Sstevel@tonic-gate  * pr_agent() creates the agent lwp. If the process is exiting while
19447c478bd9Sstevel@tonic-gate  * we are creating an agent lwp, then exitlwps() waits until the
19457c478bd9Sstevel@tonic-gate  * agent has been created using prbarrier().
19467c478bd9Sstevel@tonic-gate  */
19477c478bd9Sstevel@tonic-gate static int
19487c478bd9Sstevel@tonic-gate pr_agent(prnode_t *pnp, prgregset_t prgregset, int *unlocked)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_common->prc_proc;
19517c478bd9Sstevel@tonic-gate 	prcommon_t *pcp;
19527c478bd9Sstevel@tonic-gate 	kthread_t *t;
19537c478bd9Sstevel@tonic-gate 	kthread_t *ct;
19547c478bd9Sstevel@tonic-gate 	klwp_t *clwp;
19557c478bd9Sstevel@tonic-gate 	k_sigset_t smask;
19567c478bd9Sstevel@tonic-gate 	int cid;
19577c478bd9Sstevel@tonic-gate 	void *bufp = NULL;
19587c478bd9Sstevel@tonic-gate 	int error;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	*unlocked = 0;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	/*
19637c478bd9Sstevel@tonic-gate 	 * Cannot create the /proc agent lwp if :-
19647c478bd9Sstevel@tonic-gate 	 * - the process is not fully stopped or directed to stop.
19657c478bd9Sstevel@tonic-gate 	 * - there is an agent lwp already.
196697eda132Sraf 	 * - the process has been killed.
196797eda132Sraf 	 * - the process is exiting.
19687c478bd9Sstevel@tonic-gate 	 * - it's a vfork(2) parent.
19697c478bd9Sstevel@tonic-gate 	 */
19707c478bd9Sstevel@tonic-gate 	t = prchoose(p);	/* returns locked thread */
19717c478bd9Sstevel@tonic-gate 	ASSERT(t != NULL);
19727c478bd9Sstevel@tonic-gate 
197397eda132Sraf 	if ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t) && !JDSTOPPED(t)) ||
197497eda132Sraf 	    p->p_agenttp != NULL ||
197597eda132Sraf 	    (p->p_flag & (SKILLED | SEXITING | SVFWAIT))) {
19767c478bd9Sstevel@tonic-gate 		thread_unlock(t);
19777c478bd9Sstevel@tonic-gate 		return (EBUSY);
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	thread_unlock(t);
19817c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	sigfillset(&smask);
19847c478bd9Sstevel@tonic-gate 	sigdiffset(&smask, &cantmask);
19857c478bd9Sstevel@tonic-gate 	clwp = lwp_create(lwp_rtt, NULL, 0, p, TS_STOPPED,
19867c478bd9Sstevel@tonic-gate 	    t->t_pri, &smask, NOCLASS, 0);
19877c478bd9Sstevel@tonic-gate 	if (clwp == NULL) {
19887c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
19897c478bd9Sstevel@tonic-gate 		return (ENOMEM);
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	prsetprregs(clwp, prgregset, 1);
19927c478bd9Sstevel@tonic-gate retry:
19937c478bd9Sstevel@tonic-gate 	cid = t->t_cid;
19947c478bd9Sstevel@tonic-gate 	(void) CL_ALLOC(&bufp, cid, KM_SLEEP);
19957c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
19967c478bd9Sstevel@tonic-gate 	if (cid != t->t_cid) {
19977c478bd9Sstevel@tonic-gate 		/*
19987c478bd9Sstevel@tonic-gate 		 * Someone just changed this thread's scheduling class,
19997c478bd9Sstevel@tonic-gate 		 * so try pre-allocating the buffer again.  Hopefully we
20007c478bd9Sstevel@tonic-gate 		 * don't hit this often.
20017c478bd9Sstevel@tonic-gate 		 */
20027c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
20037c478bd9Sstevel@tonic-gate 		CL_FREE(cid, bufp);
20047c478bd9Sstevel@tonic-gate 		goto retry;
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	clwp->lwp_ap = clwp->lwp_arg;
20087c478bd9Sstevel@tonic-gate 	clwp->lwp_eosys = NORMALRETURN;
20097c478bd9Sstevel@tonic-gate 	ct = lwptot(clwp);
20107c478bd9Sstevel@tonic-gate 	ct->t_clfuncs = t->t_clfuncs;
20117c478bd9Sstevel@tonic-gate 	CL_FORK(t, ct, bufp);
20127c478bd9Sstevel@tonic-gate 	ct->t_cid = t->t_cid;
20137c478bd9Sstevel@tonic-gate 	ct->t_proc_flag |= TP_PRSTOP;
20147c478bd9Sstevel@tonic-gate 	/*
20157c478bd9Sstevel@tonic-gate 	 * Setting t_sysnum to zero causes post_syscall()
20167c478bd9Sstevel@tonic-gate 	 * to bypass all syscall checks and go directly to
20177c478bd9Sstevel@tonic-gate 	 *	if (issig()) psig();
20187c478bd9Sstevel@tonic-gate 	 * so that the agent lwp will stop in issig_forreal()
20197c478bd9Sstevel@tonic-gate 	 * showing PR_REQUESTED.
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	ct->t_sysnum = 0;
20227c478bd9Sstevel@tonic-gate 	ct->t_post_sys = 1;
20237c478bd9Sstevel@tonic-gate 	ct->t_sig_check = 1;
20247c478bd9Sstevel@tonic-gate 	p->p_agenttp = ct;
20257c478bd9Sstevel@tonic-gate 	ct->t_proc_flag &= ~TP_HOLDLWP;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	pcp = pnp->pr_pcommon;
20287c478bd9Sstevel@tonic-gate 	mutex_enter(&pcp->prc_mutex);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	lwp_create_done(ct);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * Don't return until the agent is stopped on PR_REQUESTED.
20347c478bd9Sstevel@tonic-gate 	 */
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	for (;;) {
20377c478bd9Sstevel@tonic-gate 		prunlock(pnp);
20387c478bd9Sstevel@tonic-gate 		*unlocked = 1;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 		/*
20417c478bd9Sstevel@tonic-gate 		 * Wait for the agent to stop and notify us.
20427c478bd9Sstevel@tonic-gate 		 * If we've been interrupted, return that information.
20437c478bd9Sstevel@tonic-gate 		 */
2044a913396dSqiao 		error = pr_wait(pcp, NULL);
20457c478bd9Sstevel@tonic-gate 		if (error == EINTR) {
20467c478bd9Sstevel@tonic-gate 			error = 0;
20477c478bd9Sstevel@tonic-gate 			break;
20487c478bd9Sstevel@tonic-gate 		}
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 		/*
20517c478bd9Sstevel@tonic-gate 		 * Confirm that the agent LWP has stopped.
20527c478bd9Sstevel@tonic-gate 		 */
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		if ((error = prlock(pnp, ZNO)) != 0)
20557c478bd9Sstevel@tonic-gate 			break;
20567c478bd9Sstevel@tonic-gate 		*unlocked = 0;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 		/*
20597c478bd9Sstevel@tonic-gate 		 * Since we dropped the lock on the process, the agent
20607c478bd9Sstevel@tonic-gate 		 * may have disappeared or changed. Grab the current
20617c478bd9Sstevel@tonic-gate 		 * agent and check fail if it has disappeared.
20627c478bd9Sstevel@tonic-gate 		 */
20637c478bd9Sstevel@tonic-gate 		if ((ct = p->p_agenttp) == NULL) {
20647c478bd9Sstevel@tonic-gate 			error = ENOENT;
20657c478bd9Sstevel@tonic-gate 			break;
20667c478bd9Sstevel@tonic-gate 		}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 		mutex_enter(&pcp->prc_mutex);
20697c478bd9Sstevel@tonic-gate 		thread_lock(ct);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 		if (ISTOPPED(ct)) {
20727c478bd9Sstevel@tonic-gate 			thread_unlock(ct);
20737c478bd9Sstevel@tonic-gate 			mutex_exit(&pcp->prc_mutex);
20747c478bd9Sstevel@tonic-gate 			break;
20757c478bd9Sstevel@tonic-gate 		}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 		thread_unlock(ct);
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	return (error ? error : -1);
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate static int
20847c478bd9Sstevel@tonic-gate pr_rdwr(proc_t *p, enum uio_rw rw, priovec_t *pio)
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	caddr_t base = (caddr_t)pio->pio_base;
20877c478bd9Sstevel@tonic-gate 	size_t cnt = pio->pio_len;
20887c478bd9Sstevel@tonic-gate 	uintptr_t offset = (uintptr_t)pio->pio_offset;
20897c478bd9Sstevel@tonic-gate 	struct uio auio;
20907c478bd9Sstevel@tonic-gate 	struct iovec aiov;
20917c478bd9Sstevel@tonic-gate 	int error = 0;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if ((p->p_flag & SSYS) || p->p_as == &kas)
20947c478bd9Sstevel@tonic-gate 		error = EIO;
20957c478bd9Sstevel@tonic-gate 	else if ((base + cnt) < base || (offset + cnt) < offset)
20967c478bd9Sstevel@tonic-gate 		error = EINVAL;
20977c478bd9Sstevel@tonic-gate 	else if (cnt != 0) {
20987c478bd9Sstevel@tonic-gate 		aiov.iov_base = base;
20997c478bd9Sstevel@tonic-gate 		aiov.iov_len = cnt;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 		auio.uio_loffset = offset;
21027c478bd9Sstevel@tonic-gate 		auio.uio_iov = &aiov;
21037c478bd9Sstevel@tonic-gate 		auio.uio_iovcnt = 1;
21047c478bd9Sstevel@tonic-gate 		auio.uio_resid = cnt;
21057c478bd9Sstevel@tonic-gate 		auio.uio_segflg = UIO_USERSPACE;
21067c478bd9Sstevel@tonic-gate 		auio.uio_llimit = (longlong_t)MAXOFFSET_T;
21077c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FREAD|FWRITE;
21087c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
21117c478bd9Sstevel@tonic-gate 		error = prusrio(p, rw, &auio, 0);
21127c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 		/*
21157c478bd9Sstevel@tonic-gate 		 * We have no way to return the i/o count,
21167c478bd9Sstevel@tonic-gate 		 * like read() or write() would do, so we
21177c478bd9Sstevel@tonic-gate 		 * return an error if the i/o was truncated.
21187c478bd9Sstevel@tonic-gate 		 */
21197c478bd9Sstevel@tonic-gate 		if (auio.uio_resid != 0 && error == 0)
21207c478bd9Sstevel@tonic-gate 			error = EIO;
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	return (error);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate static int
21277c478bd9Sstevel@tonic-gate pr_scred(proc_t *p, prcred_t *prcred, cred_t *cr, boolean_t dogrps)
21287c478bd9Sstevel@tonic-gate {
21297c478bd9Sstevel@tonic-gate 	kthread_t *t;
21307c478bd9Sstevel@tonic-gate 	cred_t *oldcred;
21317c478bd9Sstevel@tonic-gate 	cred_t *newcred;
21327c478bd9Sstevel@tonic-gate 	uid_t oldruid;
21337c478bd9Sstevel@tonic-gate 	int error;
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	if ((uint_t)prcred->pr_euid > MAXUID ||
21367c478bd9Sstevel@tonic-gate 	    (uint_t)prcred->pr_ruid > MAXUID ||
21377c478bd9Sstevel@tonic-gate 	    (uint_t)prcred->pr_suid > MAXUID ||
21387c478bd9Sstevel@tonic-gate 	    (uint_t)prcred->pr_egid > MAXUID ||
21397c478bd9Sstevel@tonic-gate 	    (uint_t)prcred->pr_rgid > MAXUID ||
21407c478bd9Sstevel@tonic-gate 	    (uint_t)prcred->pr_sgid > MAXUID)
21417c478bd9Sstevel@tonic-gate 		return (EINVAL);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if (dogrps) {
21447c478bd9Sstevel@tonic-gate 		int ngrp = prcred->pr_ngroups;
21457c478bd9Sstevel@tonic-gate 		int i;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 		if (ngrp < 0 || ngrp > ngroups_max)
21487c478bd9Sstevel@tonic-gate 			return (EINVAL);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 		for (i = 0; i < ngrp; i++) {
21517c478bd9Sstevel@tonic-gate 			if ((uint_t)prcred->pr_groups[i] > MAXUID)
21527c478bd9Sstevel@tonic-gate 				return (EINVAL);
21537c478bd9Sstevel@tonic-gate 		}
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	error = secpolicy_allow_setid(cr, prcred->pr_euid, B_FALSE);
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	if (error == 0 && prcred->pr_ruid != prcred->pr_euid)
21597c478bd9Sstevel@tonic-gate 		error = secpolicy_allow_setid(cr, prcred->pr_ruid, B_FALSE);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if (error == 0 && prcred->pr_suid != prcred->pr_euid &&
21627c478bd9Sstevel@tonic-gate 	    prcred->pr_suid != prcred->pr_ruid)
21637c478bd9Sstevel@tonic-gate 		error = secpolicy_allow_setid(cr, prcred->pr_suid, B_FALSE);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	if (error)
21667c478bd9Sstevel@tonic-gate 		return (error);
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	/* hold old cred so it doesn't disappear while we dup it */
21717c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
21727c478bd9Sstevel@tonic-gate 	crhold(oldcred = p->p_cred);
21737c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
21747c478bd9Sstevel@tonic-gate 	newcred = crdup(oldcred);
21757c478bd9Sstevel@tonic-gate 	oldruid = crgetruid(oldcred);
21767c478bd9Sstevel@tonic-gate 	crfree(oldcred);
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/* Error checking done above */
21797c478bd9Sstevel@tonic-gate 	(void) crsetresuid(newcred, prcred->pr_ruid, prcred->pr_euid,
21807c478bd9Sstevel@tonic-gate 		prcred->pr_suid);
21817c478bd9Sstevel@tonic-gate 	(void) crsetresgid(newcred, prcred->pr_rgid, prcred->pr_egid,
21827c478bd9Sstevel@tonic-gate 		prcred->pr_sgid);
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	if (dogrps) {
21857c478bd9Sstevel@tonic-gate 		(void) crsetgroups(newcred, prcred->pr_ngroups,
21867c478bd9Sstevel@tonic-gate 		    prcred->pr_groups);
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
21917c478bd9Sstevel@tonic-gate 	oldcred = p->p_cred;
21927c478bd9Sstevel@tonic-gate 	p->p_cred = newcred;
21937c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
21947c478bd9Sstevel@tonic-gate 	crfree(oldcred);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Keep count of processes per uid consistent.
21987c478bd9Sstevel@tonic-gate 	 */
21997c478bd9Sstevel@tonic-gate 	if (oldruid != prcred->pr_ruid) {
22007c478bd9Sstevel@tonic-gate 		zoneid_t zoneid = crgetzoneid(newcred);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
22037c478bd9Sstevel@tonic-gate 		upcount_dec(oldruid, zoneid);
22047c478bd9Sstevel@tonic-gate 		upcount_inc(prcred->pr_ruid, zoneid);
22057c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * Broadcast the cred change to the threads.
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
22127c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
22137c478bd9Sstevel@tonic-gate 	do {
22147c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1; /* so syscall will get new cred */
22157c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	return (0);
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate /*
22217c478bd9Sstevel@tonic-gate  * Change process credentials to specified zone.  Used to temporarily
22227c478bd9Sstevel@tonic-gate  * set a process to run in the global zone; only transitions between
22237c478bd9Sstevel@tonic-gate  * the process's actual zone and the global zone are allowed.
22247c478bd9Sstevel@tonic-gate  */
22257c478bd9Sstevel@tonic-gate static int
22267c478bd9Sstevel@tonic-gate pr_szoneid(proc_t *p, zoneid_t zoneid, cred_t *cr)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	kthread_t *t;
22297c478bd9Sstevel@tonic-gate 	cred_t *oldcred;
22307c478bd9Sstevel@tonic-gate 	cred_t *newcred;
22317c478bd9Sstevel@tonic-gate 	zone_t *zptr;
2232*956b78ccSsl108498 	zoneid_t oldzoneid;
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	if (secpolicy_zone_config(cr) != 0)
22357c478bd9Sstevel@tonic-gate 		return (EPERM);
22367c478bd9Sstevel@tonic-gate 	if (zoneid != GLOBAL_ZONEID && zoneid != p->p_zone->zone_id)
22377c478bd9Sstevel@tonic-gate 		return (EINVAL);
22387c478bd9Sstevel@tonic-gate 	if ((zptr = zone_find_by_id(zoneid)) == NULL)
22397c478bd9Sstevel@tonic-gate 		return (EINVAL);
22407c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
22417c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
2242*956b78ccSsl108498 	oldcred = p->p_cred;
2243*956b78ccSsl108498 	crhold(oldcred);
22447c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
22457c478bd9Sstevel@tonic-gate 	newcred = crdup(oldcred);
2246*956b78ccSsl108498 	oldzoneid = crgetzoneid(oldcred);
22477c478bd9Sstevel@tonic-gate 	crfree(oldcred);
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	crsetzone(newcred, zptr);
22507c478bd9Sstevel@tonic-gate 	zone_rele(zptr);
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
22537c478bd9Sstevel@tonic-gate 	oldcred = p->p_cred;
22547c478bd9Sstevel@tonic-gate 	p->p_cred = newcred;
22557c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
22567c478bd9Sstevel@tonic-gate 	crfree(oldcred);
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	/*
2259*956b78ccSsl108498 	 * The target process is changing zones (according to its cred), so
2260*956b78ccSsl108498 	 * update the per-zone upcounts, which are based on process creds.
2261*956b78ccSsl108498 	 */
2262*956b78ccSsl108498 	if (oldzoneid != zoneid) {
2263*956b78ccSsl108498 		uid_t ruid = crgetruid(newcred);
2264*956b78ccSsl108498 
2265*956b78ccSsl108498 		mutex_enter(&pidlock);
2266*956b78ccSsl108498 		upcount_dec(ruid, oldzoneid);
2267*956b78ccSsl108498 		upcount_inc(ruid, zoneid);
2268*956b78ccSsl108498 		mutex_exit(&pidlock);
2269*956b78ccSsl108498 	}
2270*956b78ccSsl108498 	/*
22717c478bd9Sstevel@tonic-gate 	 * Broadcast the cred change to the threads.
22727c478bd9Sstevel@tonic-gate 	 */
22737c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
22747c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
22757c478bd9Sstevel@tonic-gate 	do {
22767c478bd9Sstevel@tonic-gate 		t->t_pre_sys = 1;	/* so syscall will get new cred */
22777c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	return (0);
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate static int
22837c478bd9Sstevel@tonic-gate pr_spriv(proc_t *p, prpriv_t *prpriv, cred_t *cr)
22847c478bd9Sstevel@tonic-gate {
22857c478bd9Sstevel@tonic-gate 	kthread_t *t;
22867c478bd9Sstevel@tonic-gate 	int err;
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	if ((err = priv_pr_spriv(p, prpriv, cr)) == 0) {
22917c478bd9Sstevel@tonic-gate 		/*
22927c478bd9Sstevel@tonic-gate 		 * Broadcast the cred change to the threads.
22937c478bd9Sstevel@tonic-gate 		 */
22947c478bd9Sstevel@tonic-gate 		t = p->p_tlist;
22957c478bd9Sstevel@tonic-gate 		do {
22967c478bd9Sstevel@tonic-gate 			t->t_pre_sys = 1; /* so syscall will get new cred */
22977c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
22987c478bd9Sstevel@tonic-gate 	}
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	return (err);
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate /*
23047c478bd9Sstevel@tonic-gate  * Return -1 if the process is the parent of a vfork(1) whose child has yet to
23057c478bd9Sstevel@tonic-gate  * terminate or perform an exec(2).
23067c478bd9Sstevel@tonic-gate  *
23077c478bd9Sstevel@tonic-gate  * Returns 0 if the process is fully stopped except for the current thread (if
23087c478bd9Sstevel@tonic-gate  * we are operating on our own process), 1 otherwise.
23097c478bd9Sstevel@tonic-gate  *
23107c478bd9Sstevel@tonic-gate  * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
23117c478bd9Sstevel@tonic-gate  * See holdwatch() for details.
23127c478bd9Sstevel@tonic-gate  */
23137c478bd9Sstevel@tonic-gate int
23147c478bd9Sstevel@tonic-gate pr_allstopped(proc_t *p, int watchstop)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate 	kthread_t *t;
23177c478bd9Sstevel@tonic-gate 	int rv = 0;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (p->p_flag & SVFWAIT)	/* waiting for vfork'd child to exec */
23227c478bd9Sstevel@tonic-gate 		return (-1);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
23257c478bd9Sstevel@tonic-gate 		do {
23267c478bd9Sstevel@tonic-gate 			if (t == curthread || VSTOPPED(t) ||
23277c478bd9Sstevel@tonic-gate 			    (watchstop && (t->t_proc_flag & TP_WATCHSTOP)))
23287c478bd9Sstevel@tonic-gate 				continue;
23297c478bd9Sstevel@tonic-gate 			thread_lock(t);
23307c478bd9Sstevel@tonic-gate 			switch (t->t_state) {
23317c478bd9Sstevel@tonic-gate 			case TS_ZOMB:
23327c478bd9Sstevel@tonic-gate 			case TS_STOPPED:
23337c478bd9Sstevel@tonic-gate 				break;
23347c478bd9Sstevel@tonic-gate 			case TS_SLEEP:
23357c478bd9Sstevel@tonic-gate 				if (!(t->t_flag & T_WAKEABLE) ||
23367c478bd9Sstevel@tonic-gate 				    t->t_wchan0 == NULL)
23377c478bd9Sstevel@tonic-gate 					rv = 1;
23387c478bd9Sstevel@tonic-gate 				break;
23397c478bd9Sstevel@tonic-gate 			default:
23407c478bd9Sstevel@tonic-gate 				rv = 1;
23417c478bd9Sstevel@tonic-gate 				break;
23427c478bd9Sstevel@tonic-gate 			}
23437c478bd9Sstevel@tonic-gate 			thread_unlock(t);
23447c478bd9Sstevel@tonic-gate 		} while (rv == 0 && (t = t->t_forw) != p->p_tlist);
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	return (rv);
23487c478bd9Sstevel@tonic-gate }
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate /*
23517c478bd9Sstevel@tonic-gate  * Cause all lwps in the process to pause (for watchpoint operations).
23527c478bd9Sstevel@tonic-gate  */
23537c478bd9Sstevel@tonic-gate static void
23547c478bd9Sstevel@tonic-gate pauselwps(proc_t *p)
23557c478bd9Sstevel@tonic-gate {
23567c478bd9Sstevel@tonic-gate 	kthread_t *t;
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
23597c478bd9Sstevel@tonic-gate 	ASSERT(p != curproc);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
23627c478bd9Sstevel@tonic-gate 		do {
23637c478bd9Sstevel@tonic-gate 			thread_lock(t);
23647c478bd9Sstevel@tonic-gate 			t->t_proc_flag |= TP_PAUSE;
23657c478bd9Sstevel@tonic-gate 			aston(t);
23667c478bd9Sstevel@tonic-gate 			if (t->t_state == TS_SLEEP &&
23677c478bd9Sstevel@tonic-gate 			    (t->t_flag & T_WAKEABLE)) {
23687c478bd9Sstevel@tonic-gate 				if (t->t_wchan0 == NULL)
23697c478bd9Sstevel@tonic-gate 					setrun_locked(t);
23707c478bd9Sstevel@tonic-gate 			}
23717c478bd9Sstevel@tonic-gate 			prpokethread(t);
23727c478bd9Sstevel@tonic-gate 			thread_unlock(t);
23737c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
23747c478bd9Sstevel@tonic-gate 	}
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate /*
23787c478bd9Sstevel@tonic-gate  * undo the effects of pauselwps()
23797c478bd9Sstevel@tonic-gate  */
23807c478bd9Sstevel@tonic-gate static void
23817c478bd9Sstevel@tonic-gate unpauselwps(proc_t *p)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	kthread_t *t;
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
23867c478bd9Sstevel@tonic-gate 	ASSERT(p != curproc);
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	if ((t = p->p_tlist) != NULL) {
23897c478bd9Sstevel@tonic-gate 		do {
23907c478bd9Sstevel@tonic-gate 			thread_lock(t);
23917c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_PAUSE;
23927c478bd9Sstevel@tonic-gate 			if (t->t_state == TS_STOPPED) {
23937c478bd9Sstevel@tonic-gate 				t->t_schedflag |= TS_UNPAUSE;
23947c478bd9Sstevel@tonic-gate 				t->t_dtrace_stop = 0;
23957c478bd9Sstevel@tonic-gate 				setrun_locked(t);
23967c478bd9Sstevel@tonic-gate 			}
23977c478bd9Sstevel@tonic-gate 			thread_unlock(t);
23987c478bd9Sstevel@tonic-gate 		} while ((t = t->t_forw) != p->p_tlist);
23997c478bd9Sstevel@tonic-gate 	}
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate /*
24037c478bd9Sstevel@tonic-gate  * Cancel all watched areas.  Called from prclose().
24047c478bd9Sstevel@tonic-gate  */
24057c478bd9Sstevel@tonic-gate proc_t *
24067c478bd9Sstevel@tonic-gate pr_cancel_watch(prnode_t *pnp)
24077c478bd9Sstevel@tonic-gate {
24087c478bd9Sstevel@tonic-gate 	proc_t *p = pnp->pr_pcommon->prc_proc;
24097c478bd9Sstevel@tonic-gate 	struct as *as;
24107c478bd9Sstevel@tonic-gate 	kthread_t *t;
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	if (!pr_watch_active(p))
24157c478bd9Sstevel@tonic-gate 		return (p);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	/*
24187c478bd9Sstevel@tonic-gate 	 * Pause the process before dealing with the watchpoints.
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 	if (p == curproc) {
24217c478bd9Sstevel@tonic-gate 		prunlock(pnp);
24227c478bd9Sstevel@tonic-gate 		while (holdwatch() != 0)
24237c478bd9Sstevel@tonic-gate 			continue;
24247c478bd9Sstevel@tonic-gate 		p = pr_p_lock(pnp);
24257c478bd9Sstevel@tonic-gate 		mutex_exit(&pr_pidlock);
24267c478bd9Sstevel@tonic-gate 		ASSERT(p == curproc);
24277c478bd9Sstevel@tonic-gate 	} else {
24287c478bd9Sstevel@tonic-gate 		pauselwps(p);
24297c478bd9Sstevel@tonic-gate 		while (p != NULL && pr_allstopped(p, 0) > 0) {
24307c478bd9Sstevel@tonic-gate 			/*
24317c478bd9Sstevel@tonic-gate 			 * This cv/mutex pair is persistent even
24327c478bd9Sstevel@tonic-gate 			 * if the process disappears after we
24337c478bd9Sstevel@tonic-gate 			 * unmark it and drop p->p_lock.
24347c478bd9Sstevel@tonic-gate 			 */
24357c478bd9Sstevel@tonic-gate 			kcondvar_t *cv = &pr_pid_cv[p->p_slot];
24367c478bd9Sstevel@tonic-gate 			kmutex_t *mp = &p->p_lock;
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 			prunmark(p);
24397c478bd9Sstevel@tonic-gate 			(void) cv_wait(cv, mp);
24407c478bd9Sstevel@tonic-gate 			mutex_exit(mp);
24417c478bd9Sstevel@tonic-gate 			p = pr_p_lock(pnp);  /* NULL if process disappeared */
24427c478bd9Sstevel@tonic-gate 			mutex_exit(&pr_pidlock);
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 	}
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	if (p == NULL)		/* the process disappeared */
24477c478bd9Sstevel@tonic-gate 		return (NULL);
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	ASSERT(p == pnp->pr_pcommon->prc_proc);
24507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	if (pr_watch_active(p)) {
24537c478bd9Sstevel@tonic-gate 		pr_free_watchpoints(p);
24547c478bd9Sstevel@tonic-gate 		if ((t = p->p_tlist) != NULL) {
24557c478bd9Sstevel@tonic-gate 			do {
24567c478bd9Sstevel@tonic-gate 				watch_disable(t);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 			} while ((t = t->t_forw) != p->p_tlist);
24597c478bd9Sstevel@tonic-gate 		}
24607c478bd9Sstevel@tonic-gate 	}
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if ((as = p->p_as) != NULL) {
24637c478bd9Sstevel@tonic-gate 		avl_tree_t *tree;
24647c478bd9Sstevel@tonic-gate 		struct watched_page *pwp;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 		/*
24677c478bd9Sstevel@tonic-gate 		 * If this is the parent of a vfork, the watched page
24687c478bd9Sstevel@tonic-gate 		 * list has been moved temporarily to p->p_wpage.
24697c478bd9Sstevel@tonic-gate 		 */
24707c478bd9Sstevel@tonic-gate 		if (avl_numnodes(&p->p_wpage) != 0)
24717c478bd9Sstevel@tonic-gate 			tree = &p->p_wpage;
24727c478bd9Sstevel@tonic-gate 		else
24737c478bd9Sstevel@tonic-gate 			tree = &as->a_wpage;
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
24767c478bd9Sstevel@tonic-gate 		AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER);
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 		for (pwp = avl_first(tree); pwp != NULL;
24797c478bd9Sstevel@tonic-gate 		    pwp = AVL_NEXT(tree, pwp)) {
24807c478bd9Sstevel@tonic-gate 			pwp->wp_read = 0;
24817c478bd9Sstevel@tonic-gate 			pwp->wp_write = 0;
24827c478bd9Sstevel@tonic-gate 			pwp->wp_exec = 0;
24837c478bd9Sstevel@tonic-gate 			if ((pwp->wp_flags & WP_SETPROT) == 0) {
24847c478bd9Sstevel@tonic-gate 				pwp->wp_flags |= WP_SETPROT;
24857c478bd9Sstevel@tonic-gate 				pwp->wp_prot = pwp->wp_oprot;
24867c478bd9Sstevel@tonic-gate 				pwp->wp_list = p->p_wprot;
24877c478bd9Sstevel@tonic-gate 				p->p_wprot = pwp;
24887c478bd9Sstevel@tonic-gate 			}
24897c478bd9Sstevel@tonic-gate 		}
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 		AS_LOCK_EXIT(as, &as->a_lock);
24927c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	/*
24967c478bd9Sstevel@tonic-gate 	 * Unpause the process now.
24977c478bd9Sstevel@tonic-gate 	 */
24987c478bd9Sstevel@tonic-gate 	if (p == curproc)
24997c478bd9Sstevel@tonic-gate 		continuelwps(p);
25007c478bd9Sstevel@tonic-gate 	else
25017c478bd9Sstevel@tonic-gate 		unpauselwps(p);
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	return (p);
25047c478bd9Sstevel@tonic-gate }
2505