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