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