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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 #include <sys/types.h> 34 #include <sys/t_lock.h> 35 #include <sys/param.h> 36 #include <sys/cmn_err.h> 37 #include <sys/cred.h> 38 #include <sys/priv.h> 39 #include <sys/debug.h> 40 #include <sys/errno.h> 41 #include <sys/inline.h> 42 #include <sys/kmem.h> 43 #include <sys/mman.h> 44 #include <sys/proc.h> 45 #include <sys/sobject.h> 46 #include <sys/sysmacros.h> 47 #include <sys/systm.h> 48 #include <sys/uio.h> 49 #include <sys/var.h> 50 #include <sys/vfs.h> 51 #include <sys/vnode.h> 52 #include <sys/session.h> 53 #include <sys/pcb.h> 54 #include <sys/signal.h> 55 #include <sys/user.h> 56 #include <sys/disp.h> 57 #include <sys/class.h> 58 #include <sys/ts.h> 59 #include <sys/bitmap.h> 60 #include <sys/poll.h> 61 #include <sys/shm_impl.h> 62 #include <sys/fault.h> 63 #include <sys/syscall.h> 64 #include <sys/procfs.h> 65 #include <sys/processor.h> 66 #include <sys/cpuvar.h> 67 #include <sys/copyops.h> 68 #include <sys/time.h> 69 #include <sys/msacct.h> 70 #include <vm/as.h> 71 #include <vm/rm.h> 72 #include <vm/seg.h> 73 #include <vm/seg_vn.h> 74 #include <vm/seg_dev.h> 75 #include <vm/seg_spt.h> 76 #include <vm/page.h> 77 #include <sys/vmparam.h> 78 #include <sys/swap.h> 79 #include <fs/proc/prdata.h> 80 #include <sys/task.h> 81 #include <sys/project.h> 82 #include <sys/contract_impl.h> 83 #include <sys/contract/process.h> 84 #include <sys/contract/process_impl.h> 85 #include <sys/schedctl.h> 86 #include <sys/pool.h> 87 #include <sys/zone.h> 88 #include <sys/atomic.h> 89 90 #define MAX_ITERS_SPIN 5 91 92 typedef struct prpagev { 93 uint_t *pg_protv; /* vector of page permissions */ 94 char *pg_incore; /* vector of incore flags */ 95 size_t pg_npages; /* number of pages in protv and incore */ 96 ulong_t pg_pnbase; /* pn within segment of first protv element */ 97 } prpagev_t; 98 99 size_t pagev_lim = 256 * 1024; /* limit on number of pages in prpagev_t */ 100 101 extern struct seg_ops segdev_ops; /* needs a header file */ 102 extern struct seg_ops segspt_shmops; /* needs a header file */ 103 104 static int set_watched_page(proc_t *, caddr_t, caddr_t, ulong_t, ulong_t); 105 static void clear_watched_page(proc_t *, caddr_t, caddr_t, ulong_t); 106 107 /* 108 * Choose an lwp from the complete set of lwps for the process. 109 * This is called for any operation applied to the process 110 * file descriptor that requires an lwp to operate upon. 111 * 112 * Returns a pointer to the thread for the selected LWP, 113 * and with the dispatcher lock held for the thread. 114 * 115 * The algorithm for choosing an lwp is critical for /proc semantics; 116 * don't touch this code unless you know all of the implications. 117 */ 118 kthread_t * 119 prchoose(proc_t *p) 120 { 121 kthread_t *t; 122 kthread_t *t_onproc = NULL; /* running on processor */ 123 kthread_t *t_run = NULL; /* runnable, on disp queue */ 124 kthread_t *t_sleep = NULL; /* sleeping */ 125 kthread_t *t_hold = NULL; /* sleeping, performing hold */ 126 kthread_t *t_susp = NULL; /* suspended stop */ 127 kthread_t *t_jstop = NULL; /* jobcontrol stop, w/o directed stop */ 128 kthread_t *t_jdstop = NULL; /* jobcontrol stop with directed stop */ 129 kthread_t *t_req = NULL; /* requested stop */ 130 kthread_t *t_istop = NULL; /* event-of-interest stop */ 131 132 ASSERT(MUTEX_HELD(&p->p_lock)); 133 134 /* 135 * If the agent lwp exists, it takes precedence over all others. 136 */ 137 if ((t = p->p_agenttp) != NULL) { 138 thread_lock(t); 139 return (t); 140 } 141 142 if ((t = p->p_tlist) == NULL) /* start at the head of the list */ 143 return (t); 144 do { /* for eacn lwp in the process */ 145 if (VSTOPPED(t)) { /* virtually stopped */ 146 if (t_req == NULL) 147 t_req = t; 148 continue; 149 } 150 151 thread_lock(t); /* make sure thread is in good state */ 152 switch (t->t_state) { 153 default: 154 panic("prchoose: bad thread state %d, thread 0x%p", 155 t->t_state, (void *)t); 156 /*NOTREACHED*/ 157 case TS_SLEEP: 158 /* this is filthy */ 159 if (t->t_wchan == (caddr_t)&p->p_holdlwps && 160 t->t_wchan0 == NULL) { 161 if (t_hold == NULL) 162 t_hold = t; 163 } else { 164 if (t_sleep == NULL) 165 t_sleep = t; 166 } 167 break; 168 case TS_RUN: 169 if (t_run == NULL) 170 t_run = t; 171 break; 172 case TS_ONPROC: 173 if (t_onproc == NULL) 174 t_onproc = t; 175 break; 176 case TS_ZOMB: /* last possible choice */ 177 break; 178 case TS_STOPPED: 179 switch (t->t_whystop) { 180 case PR_SUSPENDED: 181 if (t_susp == NULL) 182 t_susp = t; 183 break; 184 case PR_JOBCONTROL: 185 if (t->t_proc_flag & TP_PRSTOP) { 186 if (t_jdstop == NULL) 187 t_jdstop = t; 188 } else { 189 if (t_jstop == NULL) 190 t_jstop = t; 191 } 192 break; 193 case PR_REQUESTED: 194 if (t_req == NULL) 195 t_req = t; 196 break; 197 case PR_SYSENTRY: 198 case PR_SYSEXIT: 199 case PR_SIGNALLED: 200 case PR_FAULTED: 201 /* 202 * Make an lwp calling exit() be the 203 * last lwp seen in the process. 204 */ 205 if (t_istop == NULL || 206 (t_istop->t_whystop == PR_SYSENTRY && 207 t_istop->t_whatstop == SYS_exit)) 208 t_istop = t; 209 break; 210 case PR_CHECKPOINT: /* can't happen? */ 211 break; 212 default: 213 panic("prchoose: bad t_whystop %d, thread 0x%p", 214 t->t_whystop, (void *)t); 215 /*NOTREACHED*/ 216 } 217 break; 218 } 219 thread_unlock(t); 220 } while ((t = t->t_forw) != p->p_tlist); 221 222 if (t_onproc) 223 t = t_onproc; 224 else if (t_run) 225 t = t_run; 226 else if (t_sleep) 227 t = t_sleep; 228 else if (t_jstop) 229 t = t_jstop; 230 else if (t_jdstop) 231 t = t_jdstop; 232 else if (t_istop) 233 t = t_istop; 234 else if (t_req) 235 t = t_req; 236 else if (t_hold) 237 t = t_hold; 238 else if (t_susp) 239 t = t_susp; 240 else /* TS_ZOMB */ 241 t = p->p_tlist; 242 243 if (t != NULL) 244 thread_lock(t); 245 return (t); 246 } 247 248 /* 249 * Wakeup anyone sleeping on the /proc vnode for the process/lwp to stop. 250 * Also call pollwakeup() if any lwps are waiting in poll() for POLLPRI 251 * on the /proc file descriptor. Called from stop() when a traced 252 * process stops on an event of interest. Also called from exit() 253 * and prinvalidate() to indicate POLLHUP and POLLERR respectively. 254 */ 255 void 256 prnotify(struct vnode *vp) 257 { 258 prcommon_t *pcp = VTOP(vp)->pr_common; 259 260 mutex_enter(&pcp->prc_mutex); 261 cv_broadcast(&pcp->prc_wait); 262 mutex_exit(&pcp->prc_mutex); 263 if (pcp->prc_flags & PRC_POLL) { 264 /* 265 * We call pollwakeup() with POLLHUP to ensure that 266 * the pollers are awakened even if they are polling 267 * for nothing (i.e., waiting for the process to exit). 268 * This enables the use of the PRC_POLL flag for optimization 269 * (we can turn off PRC_POLL only if we know no pollers remain). 270 */ 271 pcp->prc_flags &= ~PRC_POLL; 272 pollwakeup(&pcp->prc_pollhead, POLLHUP); 273 } 274 } 275 276 /* called immediately below, in prfree() */ 277 static void 278 prfreenotify(vnode_t *vp) 279 { 280 prnode_t *pnp; 281 prcommon_t *pcp; 282 283 while (vp != NULL) { 284 pnp = VTOP(vp); 285 pcp = pnp->pr_common; 286 ASSERT(pcp->prc_thread == NULL); 287 pcp->prc_proc = NULL; 288 /* 289 * We can't call prnotify() here because we are holding 290 * pidlock. We assert that there is no need to. 291 */ 292 mutex_enter(&pcp->prc_mutex); 293 cv_broadcast(&pcp->prc_wait); 294 mutex_exit(&pcp->prc_mutex); 295 ASSERT(!(pcp->prc_flags & PRC_POLL)); 296 297 vp = pnp->pr_next; 298 pnp->pr_next = NULL; 299 } 300 } 301 302 /* 303 * Called from a hook in freeproc() when a traced process is removed 304 * from the process table. The proc-table pointers of all associated 305 * /proc vnodes are cleared to indicate that the process has gone away. 306 */ 307 void 308 prfree(proc_t *p) 309 { 310 uint_t slot = p->p_slot; 311 312 ASSERT(MUTEX_HELD(&pidlock)); 313 314 /* 315 * Block the process against /proc so it can be freed. 316 * It cannot be freed while locked by some controlling process. 317 * Lock ordering: 318 * pidlock -> pr_pidlock -> p->p_lock -> pcp->prc_mutex 319 */ 320 mutex_enter(&pr_pidlock); /* protects pcp->prc_proc */ 321 mutex_enter(&p->p_lock); 322 while (p->p_proc_flag & P_PR_LOCK) { 323 mutex_exit(&pr_pidlock); 324 cv_wait(&pr_pid_cv[slot], &p->p_lock); 325 mutex_exit(&p->p_lock); 326 mutex_enter(&pr_pidlock); 327 mutex_enter(&p->p_lock); 328 } 329 330 ASSERT(p->p_tlist == NULL); 331 332 prfreenotify(p->p_plist); 333 p->p_plist = NULL; 334 335 prfreenotify(p->p_trace); 336 p->p_trace = NULL; 337 338 /* 339 * We broadcast to wake up everyone waiting for this process. 340 * No one can reach this process from this point on. 341 */ 342 cv_broadcast(&pr_pid_cv[slot]); 343 344 mutex_exit(&p->p_lock); 345 mutex_exit(&pr_pidlock); 346 } 347 348 /* 349 * Called from a hook in exit() when a traced process is becoming a zombie. 350 */ 351 void 352 prexit(proc_t *p) 353 { 354 ASSERT(MUTEX_HELD(&p->p_lock)); 355 356 if (pr_watch_active(p)) { 357 pr_free_watchpoints(p); 358 watch_disable(curthread); 359 } 360 /* pr_free_watched_pages() is called in exit(), after dropping p_lock */ 361 if (p->p_trace) { 362 VTOP(p->p_trace)->pr_common->prc_flags |= PRC_DESTROY; 363 prnotify(p->p_trace); 364 } 365 cv_broadcast(&pr_pid_cv[p->p_slot]); /* pauselwps() */ 366 } 367 368 /* 369 * Called when a thread calls lwp_exit(). 370 */ 371 void 372 prlwpexit(kthread_t *t) 373 { 374 vnode_t *vp; 375 prnode_t *pnp; 376 prcommon_t *pcp; 377 proc_t *p = ttoproc(t); 378 lwpent_t *lep = p->p_lwpdir[t->t_dslot].ld_entry; 379 380 ASSERT(t == curthread); 381 ASSERT(MUTEX_HELD(&p->p_lock)); 382 383 /* 384 * The process must be blocked against /proc to do this safely. 385 * The lwp must not disappear while the process is marked P_PR_LOCK. 386 * It is the caller's responsibility to have called prbarrier(p). 387 */ 388 ASSERT(!(p->p_proc_flag & P_PR_LOCK)); 389 390 for (vp = p->p_plist; vp != NULL; vp = pnp->pr_next) { 391 pnp = VTOP(vp); 392 pcp = pnp->pr_common; 393 if (pcp->prc_thread == t) { 394 pcp->prc_thread = NULL; 395 pcp->prc_flags |= PRC_DESTROY; 396 } 397 } 398 399 for (vp = lep->le_trace; vp != NULL; vp = pnp->pr_next) { 400 pnp = VTOP(vp); 401 pcp = pnp->pr_common; 402 pcp->prc_thread = NULL; 403 pcp->prc_flags |= PRC_DESTROY; 404 prnotify(vp); 405 } 406 407 if (p->p_trace) 408 prnotify(p->p_trace); 409 } 410 411 /* 412 * Called when a zombie thread is joined or when a 413 * detached lwp exits. Called from lwp_hash_out(). 414 */ 415 void 416 prlwpfree(proc_t *p, lwpent_t *lep) 417 { 418 vnode_t *vp; 419 prnode_t *pnp; 420 prcommon_t *pcp; 421 422 ASSERT(MUTEX_HELD(&p->p_lock)); 423 424 /* 425 * The process must be blocked against /proc to do this safely. 426 * The lwp must not disappear while the process is marked P_PR_LOCK. 427 * It is the caller's responsibility to have called prbarrier(p). 428 */ 429 ASSERT(!(p->p_proc_flag & P_PR_LOCK)); 430 431 vp = lep->le_trace; 432 lep->le_trace = NULL; 433 while (vp) { 434 prnotify(vp); 435 pnp = VTOP(vp); 436 pcp = pnp->pr_common; 437 ASSERT(pcp->prc_thread == NULL && 438 (pcp->prc_flags & PRC_DESTROY)); 439 pcp->prc_tslot = -1; 440 vp = pnp->pr_next; 441 pnp->pr_next = NULL; 442 } 443 444 if (p->p_trace) 445 prnotify(p->p_trace); 446 } 447 448 /* 449 * Called from a hook in exec() when a thread starts exec(). 450 */ 451 void 452 prexecstart(void) 453 { 454 proc_t *p = ttoproc(curthread); 455 klwp_t *lwp = ttolwp(curthread); 456 457 /* 458 * The P_PR_EXEC flag blocks /proc operations for 459 * the duration of the exec(). 460 * We can't start exec() while the process is 461 * locked by /proc, so we call prbarrier(). 462 * lwp_nostop keeps the process from being stopped 463 * via job control for the duration of the exec(). 464 */ 465 466 ASSERT(MUTEX_HELD(&p->p_lock)); 467 prbarrier(p); 468 lwp->lwp_nostop++; 469 p->p_proc_flag |= P_PR_EXEC; 470 } 471 472 /* 473 * Called from a hook in exec() when a thread finishes exec(). 474 * The thread may or may not have succeeded. Some other thread 475 * may have beat it to the punch. 476 */ 477 void 478 prexecend(void) 479 { 480 proc_t *p = ttoproc(curthread); 481 klwp_t *lwp = ttolwp(curthread); 482 vnode_t *vp; 483 prnode_t *pnp; 484 prcommon_t *pcp; 485 model_t model = p->p_model; 486 id_t tid = curthread->t_tid; 487 int tslot = curthread->t_dslot; 488 489 ASSERT(MUTEX_HELD(&p->p_lock)); 490 491 lwp->lwp_nostop--; 492 if (p->p_flag & SEXITLWPS) { 493 /* 494 * We are on our way to exiting because some 495 * other thread beat us in the race to exec(). 496 * Don't clear the P_PR_EXEC flag in this case. 497 */ 498 return; 499 } 500 501 /* 502 * Wake up anyone waiting in /proc for the process to complete exec(). 503 */ 504 p->p_proc_flag &= ~P_PR_EXEC; 505 if ((vp = p->p_trace) != NULL) { 506 pcp = VTOP(vp)->pr_common; 507 mutex_enter(&pcp->prc_mutex); 508 cv_broadcast(&pcp->prc_wait); 509 mutex_exit(&pcp->prc_mutex); 510 for (; vp != NULL; vp = pnp->pr_next) { 511 pnp = VTOP(vp); 512 pnp->pr_common->prc_datamodel = model; 513 } 514 } 515 if ((vp = p->p_lwpdir[tslot].ld_entry->le_trace) != NULL) { 516 /* 517 * We dealt with the process common above. 518 */ 519 ASSERT(p->p_trace != NULL); 520 pcp = VTOP(vp)->pr_common; 521 mutex_enter(&pcp->prc_mutex); 522 cv_broadcast(&pcp->prc_wait); 523 mutex_exit(&pcp->prc_mutex); 524 for (; vp != NULL; vp = pnp->pr_next) { 525 pnp = VTOP(vp); 526 pcp = pnp->pr_common; 527 pcp->prc_datamodel = model; 528 pcp->prc_tid = tid; 529 pcp->prc_tslot = tslot; 530 } 531 } 532 } 533 534 /* 535 * Called from a hook in relvm() just before freeing the address space. 536 * We free all the watched areas now. 537 */ 538 void 539 prrelvm(void) 540 { 541 proc_t *p = ttoproc(curthread); 542 543 mutex_enter(&p->p_lock); 544 prbarrier(p); /* block all other /proc operations */ 545 if (pr_watch_active(p)) { 546 pr_free_watchpoints(p); 547 watch_disable(curthread); 548 } 549 mutex_exit(&p->p_lock); 550 pr_free_watched_pages(p); 551 } 552 553 /* 554 * Called from hooks in exec-related code when a traced process 555 * attempts to exec(2) a setuid/setgid program or an unreadable 556 * file. Rather than fail the exec we invalidate the associated 557 * /proc vnodes so that subsequent attempts to use them will fail. 558 * 559 * All /proc vnodes, except directory vnodes, are retained on a linked 560 * list (rooted at p_plist in the process structure) until last close. 561 * 562 * A controlling process must re-open the /proc files in order to 563 * regain control. 564 */ 565 void 566 prinvalidate(struct user *up) 567 { 568 kthread_t *t = curthread; 569 proc_t *p = ttoproc(t); 570 vnode_t *vp; 571 prnode_t *pnp; 572 int writers = 0; 573 574 mutex_enter(&p->p_lock); 575 prbarrier(p); /* block all other /proc operations */ 576 577 /* 578 * At this moment, there can be only one lwp in the process. 579 */ 580 ASSERT(p->p_lwpcnt == 1 && p->p_zombcnt == 0); 581 582 /* 583 * Invalidate any currently active /proc vnodes. 584 */ 585 for (vp = p->p_plist; vp != NULL; vp = pnp->pr_next) { 586 pnp = VTOP(vp); 587 switch (pnp->pr_type) { 588 case PR_PSINFO: /* these files can read by anyone */ 589 case PR_LPSINFO: 590 case PR_LWPSINFO: 591 case PR_LWPDIR: 592 case PR_LWPIDDIR: 593 case PR_USAGE: 594 case PR_LUSAGE: 595 case PR_LWPUSAGE: 596 break; 597 default: 598 pnp->pr_flags |= PR_INVAL; 599 break; 600 } 601 } 602 /* 603 * Wake up anyone waiting for the process or lwp. 604 * p->p_trace is guaranteed to be non-NULL if there 605 * are any open /proc files for this process. 606 */ 607 if ((vp = p->p_trace) != NULL) { 608 prcommon_t *pcp = VTOP(vp)->pr_pcommon; 609 610 prnotify(vp); 611 /* 612 * Are there any writers? 613 */ 614 if ((writers = pcp->prc_writers) != 0) { 615 /* 616 * Clear the exclusive open flag (old /proc interface). 617 * Set prc_selfopens equal to prc_writers so that 618 * the next O_EXCL|O_WRITE open will succeed 619 * even with existing (though invalid) writers. 620 * prclose() must decrement prc_selfopens when 621 * the invalid files are closed. 622 */ 623 pcp->prc_flags &= ~PRC_EXCL; 624 ASSERT(pcp->prc_selfopens <= writers); 625 pcp->prc_selfopens = writers; 626 } 627 } 628 vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace; 629 while (vp != NULL) { 630 /* 631 * We should not invalidate the lwpiddir vnodes, 632 * but the necessities of maintaining the old 633 * ioctl()-based version of /proc require it. 634 */ 635 pnp = VTOP(vp); 636 pnp->pr_flags |= PR_INVAL; 637 prnotify(vp); 638 vp = pnp->pr_next; 639 } 640 641 /* 642 * If any tracing flags are in effect and any vnodes are open for 643 * writing then set the requested-stop and run-on-last-close flags. 644 * Otherwise, clear all tracing flags. 645 */ 646 t->t_proc_flag &= ~TP_PAUSE; 647 if ((p->p_proc_flag & P_PR_TRACE) && writers) { 648 t->t_proc_flag |= TP_PRSTOP; 649 aston(t); /* so ISSIG will see the flag */ 650 p->p_proc_flag |= P_PR_RUNLCL; 651 } else { 652 premptyset(&up->u_entrymask); /* syscalls */ 653 premptyset(&up->u_exitmask); 654 up->u_systrap = 0; 655 premptyset(&p->p_sigmask); /* signals */ 656 premptyset(&p->p_fltmask); /* faults */ 657 t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING); 658 p->p_proc_flag &= ~(P_PR_RUNLCL|P_PR_KILLCL|P_PR_TRACE); 659 prnostep(ttolwp(t)); 660 } 661 662 mutex_exit(&p->p_lock); 663 } 664 665 /* 666 * Acquire the controlled process's p_lock and mark it P_PR_LOCK. 667 * Return with pr_pidlock held in all cases. 668 * Return with p_lock held if the the process still exists. 669 * Return value is the process pointer if the process still exists, else NULL. 670 * If we lock the process, give ourself kernel priority to avoid deadlocks; 671 * this is undone in prunlock(). 672 */ 673 proc_t * 674 pr_p_lock(prnode_t *pnp) 675 { 676 proc_t *p; 677 prcommon_t *pcp; 678 679 mutex_enter(&pr_pidlock); 680 if ((pcp = pnp->pr_pcommon) == NULL || (p = pcp->prc_proc) == NULL) 681 return (NULL); 682 mutex_enter(&p->p_lock); 683 while (p->p_proc_flag & P_PR_LOCK) { 684 /* 685 * This cv/mutex pair is persistent even if 686 * the process disappears while we sleep. 687 */ 688 kcondvar_t *cv = &pr_pid_cv[p->p_slot]; 689 kmutex_t *mp = &p->p_lock; 690 691 mutex_exit(&pr_pidlock); 692 cv_wait(cv, mp); 693 mutex_exit(mp); 694 mutex_enter(&pr_pidlock); 695 if (pcp->prc_proc == NULL) 696 return (NULL); 697 ASSERT(p == pcp->prc_proc); 698 mutex_enter(&p->p_lock); 699 } 700 p->p_proc_flag |= P_PR_LOCK; 701 THREAD_KPRI_REQUEST(); 702 return (p); 703 } 704 705 /* 706 * Lock the target process by setting P_PR_LOCK and grabbing p->p_lock. 707 * This prevents any lwp of the process from disappearing and 708 * blocks most operations that a process can perform on itself. 709 * Returns 0 on success, a non-zero error number on failure. 710 * 711 * 'zdisp' is ZYES or ZNO to indicate whether prlock() should succeed when 712 * the subject process is a zombie (ZYES) or fail for zombies (ZNO). 713 * 714 * error returns: 715 * ENOENT: process or lwp has disappeared or process is exiting 716 * (or has become a zombie and zdisp == ZNO). 717 * EAGAIN: procfs vnode has become invalid. 718 * EINTR: signal arrived while waiting for exec to complete. 719 */ 720 int 721 prlock(prnode_t *pnp, int zdisp) 722 { 723 prcommon_t *pcp; 724 proc_t *p; 725 726 again: 727 pcp = pnp->pr_common; 728 p = pr_p_lock(pnp); 729 mutex_exit(&pr_pidlock); 730 731 /* 732 * Return ENOENT immediately if there is no process. 733 */ 734 if (p == NULL) 735 return (ENOENT); 736 737 ASSERT(p == pcp->prc_proc && p->p_stat != 0 && p->p_stat != SIDL); 738 739 /* 740 * Return ENOENT if process entered zombie state or is exiting 741 * and the 'zdisp' flag is set to ZNO indicating not to lock zombies. 742 */ 743 if (zdisp == ZNO && 744 ((pcp->prc_flags & PRC_DESTROY) || (p->p_flag & SEXITING))) { 745 prunlock(pnp); 746 return (ENOENT); 747 } 748 749 /* 750 * If lwp-specific, check to see if lwp has disappeared. 751 */ 752 if (pcp->prc_flags & PRC_LWP) { 753 if ((zdisp == ZNO && (pcp->prc_flags & PRC_DESTROY)) || 754 pcp->prc_tslot == -1) { 755 prunlock(pnp); 756 return (ENOENT); 757 } 758 } 759 760 /* 761 * Return EAGAIN if we have encountered a security violation. 762 * (The process exec'd a set-id or unreadable executable file.) 763 */ 764 if (pnp->pr_flags & PR_INVAL) { 765 prunlock(pnp); 766 return (EAGAIN); 767 } 768 769 /* 770 * If process is undergoing an exec(), wait for 771 * completion and then start all over again. 772 */ 773 if (p->p_proc_flag & P_PR_EXEC) { 774 pcp = pnp->pr_pcommon; /* Put on the correct sleep queue */ 775 mutex_enter(&pcp->prc_mutex); 776 prunlock(pnp); 777 if (!cv_wait_sig(&pcp->prc_wait, &pcp->prc_mutex)) { 778 mutex_exit(&pcp->prc_mutex); 779 return (EINTR); 780 } 781 mutex_exit(&pcp->prc_mutex); 782 goto again; 783 } 784 785 /* 786 * We return holding p->p_lock. 787 */ 788 return (0); 789 } 790 791 /* 792 * Undo prlock() and pr_p_lock(). 793 * p->p_lock is still held; pr_pidlock is no longer held. 794 * 795 * prunmark() drops the P_PR_LOCK flag and wakes up another thread, 796 * if any, waiting for the flag to be dropped; it retains p->p_lock. 797 * 798 * prunlock() calls prunmark() and then drops p->p_lock. 799 */ 800 void 801 prunmark(proc_t *p) 802 { 803 ASSERT(p->p_proc_flag & P_PR_LOCK); 804 ASSERT(MUTEX_HELD(&p->p_lock)); 805 806 cv_signal(&pr_pid_cv[p->p_slot]); 807 p->p_proc_flag &= ~P_PR_LOCK; 808 THREAD_KPRI_RELEASE(); 809 } 810 811 void 812 prunlock(prnode_t *pnp) 813 { 814 prcommon_t *pcp = pnp->pr_common; 815 proc_t *p = pcp->prc_proc; 816 817 /* 818 * If we (or someone) gave it a SIGKILL, and it is not 819 * already a zombie, set it running unconditionally. 820 */ 821 if ((p->p_flag & SKILLED) && 822 !(p->p_flag & SEXITING) && 823 !(pcp->prc_flags & PRC_DESTROY) && 824 !((pcp->prc_flags & PRC_LWP) && pcp->prc_tslot == -1)) 825 (void) pr_setrun(pnp, 0); 826 prunmark(p); 827 mutex_exit(&p->p_lock); 828 } 829 830 /* 831 * Called while holding p->p_lock to delay until the process is unlocked. 832 * We enter holding p->p_lock; p->p_lock is dropped and reacquired. 833 * The process cannot become locked again until p->p_lock is dropped. 834 */ 835 void 836 prbarrier(proc_t *p) 837 { 838 ASSERT(MUTEX_HELD(&p->p_lock)); 839 840 if (p->p_proc_flag & P_PR_LOCK) { 841 /* The process is locked; delay until not locked */ 842 uint_t slot = p->p_slot; 843 844 while (p->p_proc_flag & P_PR_LOCK) 845 cv_wait(&pr_pid_cv[slot], &p->p_lock); 846 cv_signal(&pr_pid_cv[slot]); 847 } 848 } 849 850 /* 851 * Return process/lwp status. 852 * The u-block is mapped in by this routine and unmapped at the end. 853 */ 854 void 855 prgetstatus(proc_t *p, pstatus_t *sp, zone_t *zp) 856 { 857 kthread_t *t; 858 859 ASSERT(MUTEX_HELD(&p->p_lock)); 860 861 t = prchoose(p); /* returns locked thread */ 862 ASSERT(t != NULL); 863 thread_unlock(t); 864 865 /* just bzero the process part, prgetlwpstatus() does the rest */ 866 bzero(sp, sizeof (pstatus_t) - sizeof (lwpstatus_t)); 867 sp->pr_nlwp = p->p_lwpcnt; 868 sp->pr_nzomb = p->p_zombcnt; 869 prassignset(&sp->pr_sigpend, &p->p_sig); 870 sp->pr_brkbase = (uintptr_t)p->p_brkbase; 871 sp->pr_brksize = p->p_brksize; 872 sp->pr_stkbase = (uintptr_t)prgetstackbase(p); 873 sp->pr_stksize = p->p_stksize; 874 sp->pr_pid = p->p_pid; 875 if (curproc->p_zone->zone_id != GLOBAL_ZONEID && 876 (p->p_flag & SZONETOP)) { 877 ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID); 878 /* 879 * Inside local zones, fake zsched's pid as parent pids for 880 * processes which reference processes outside of the zone. 881 */ 882 sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid; 883 } else { 884 sp->pr_ppid = p->p_ppid; 885 } 886 sp->pr_pgid = p->p_pgrp; 887 sp->pr_sid = p->p_sessp->s_sid; 888 sp->pr_taskid = p->p_task->tk_tkid; 889 sp->pr_projid = p->p_task->tk_proj->kpj_id; 890 sp->pr_zoneid = p->p_zone->zone_id; 891 hrt2ts(mstate_aggr_state(p, LMS_USER), &sp->pr_utime); 892 hrt2ts(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime); 893 TICK_TO_TIMESTRUC(p->p_cutime, &sp->pr_cutime); 894 TICK_TO_TIMESTRUC(p->p_cstime, &sp->pr_cstime); 895 prassignset(&sp->pr_sigtrace, &p->p_sigmask); 896 prassignset(&sp->pr_flttrace, &p->p_fltmask); 897 prassignset(&sp->pr_sysentry, &PTOU(p)->u_entrymask); 898 prassignset(&sp->pr_sysexit, &PTOU(p)->u_exitmask); 899 switch (p->p_model) { 900 case DATAMODEL_ILP32: 901 sp->pr_dmodel = PR_MODEL_ILP32; 902 break; 903 case DATAMODEL_LP64: 904 sp->pr_dmodel = PR_MODEL_LP64; 905 break; 906 } 907 if (p->p_agenttp) 908 sp->pr_agentid = p->p_agenttp->t_tid; 909 910 /* get the chosen lwp's status */ 911 prgetlwpstatus(t, &sp->pr_lwp, zp); 912 913 /* replicate the flags */ 914 sp->pr_flags = sp->pr_lwp.pr_flags; 915 } 916 917 #ifdef _SYSCALL32_IMPL 918 void 919 prgetlwpstatus32(kthread_t *t, lwpstatus32_t *sp, zone_t *zp) 920 { 921 proc_t *p = ttoproc(t); 922 klwp_t *lwp = ttolwp(t); 923 struct mstate *ms = &lwp->lwp_mstate; 924 hrtime_t usr, sys; 925 int flags; 926 ulong_t instr; 927 928 ASSERT(MUTEX_HELD(&p->p_lock)); 929 930 bzero(sp, sizeof (*sp)); 931 flags = 0L; 932 if (t->t_state == TS_STOPPED) { 933 flags |= PR_STOPPED; 934 if ((t->t_schedflag & TS_PSTART) == 0) 935 flags |= PR_ISTOP; 936 } else if (VSTOPPED(t)) { 937 flags |= PR_STOPPED|PR_ISTOP; 938 } 939 if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP)) 940 flags |= PR_DSTOP; 941 if (lwp->lwp_asleep) 942 flags |= PR_ASLEEP; 943 if (t == p->p_agenttp) 944 flags |= PR_AGENT; 945 if (!(t->t_proc_flag & TP_TWAIT)) 946 flags |= PR_DETACH; 947 if (t->t_proc_flag & TP_DAEMON) 948 flags |= PR_DAEMON; 949 if (p->p_proc_flag & P_PR_FORK) 950 flags |= PR_FORK; 951 if (p->p_proc_flag & P_PR_RUNLCL) 952 flags |= PR_RLC; 953 if (p->p_proc_flag & P_PR_KILLCL) 954 flags |= PR_KLC; 955 if (p->p_proc_flag & P_PR_ASYNC) 956 flags |= PR_ASYNC; 957 if (p->p_proc_flag & P_PR_BPTADJ) 958 flags |= PR_BPTADJ; 959 if (p->p_proc_flag & P_PR_PTRACE) 960 flags |= PR_PTRACE; 961 if (p->p_flag & SMSACCT) 962 flags |= PR_MSACCT; 963 if (p->p_flag & SMSFORK) 964 flags |= PR_MSFORK; 965 if (p->p_flag & SVFWAIT) 966 flags |= PR_VFORKP; 967 sp->pr_flags = flags; 968 if (VSTOPPED(t)) { 969 sp->pr_why = PR_REQUESTED; 970 sp->pr_what = 0; 971 } else { 972 sp->pr_why = t->t_whystop; 973 sp->pr_what = t->t_whatstop; 974 } 975 sp->pr_lwpid = t->t_tid; 976 sp->pr_cursig = lwp->lwp_cursig; 977 prassignset(&sp->pr_lwppend, &t->t_sig); 978 schedctl_finish_sigblock(t); 979 prassignset(&sp->pr_lwphold, &t->t_hold); 980 if (t->t_whystop == PR_FAULTED) { 981 siginfo_kto32(&lwp->lwp_siginfo, &sp->pr_info); 982 if (t->t_whatstop == FLTPAGE) 983 sp->pr_info.si_addr = 984 (caddr32_t)(uintptr_t)lwp->lwp_siginfo.si_addr; 985 } else if (lwp->lwp_curinfo) 986 siginfo_kto32(&lwp->lwp_curinfo->sq_info, &sp->pr_info); 987 if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID && 988 sp->pr_info.si_zoneid != zp->zone_id) { 989 sp->pr_info.si_pid = zp->zone_zsched->p_pid; 990 sp->pr_info.si_uid = 0; 991 sp->pr_info.si_ctid = -1; 992 sp->pr_info.si_zoneid = zp->zone_id; 993 } 994 sp->pr_altstack.ss_sp = 995 (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp; 996 sp->pr_altstack.ss_size = (size32_t)lwp->lwp_sigaltstack.ss_size; 997 sp->pr_altstack.ss_flags = (int32_t)lwp->lwp_sigaltstack.ss_flags; 998 prgetaction32(p, PTOU(p), lwp->lwp_cursig, &sp->pr_action); 999 sp->pr_oldcontext = (caddr32_t)lwp->lwp_oldcontext; 1000 sp->pr_ustack = (caddr32_t)lwp->lwp_ustack; 1001 (void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name, 1002 sizeof (sp->pr_clname) - 1); 1003 if (flags & PR_STOPPED) 1004 hrt2ts32(t->t_stoptime, &sp->pr_tstamp); 1005 usr = ms->ms_acct[LMS_USER]; 1006 sys = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP]; 1007 scalehrtime(&usr); 1008 scalehrtime(&sys); 1009 hrt2ts32(usr, &sp->pr_utime); 1010 hrt2ts32(sys, &sp->pr_stime); 1011 1012 /* 1013 * Fetch the current instruction, if not a system process. 1014 * We don't attempt this unless the lwp is stopped. 1015 */ 1016 if ((p->p_flag & SSYS) || p->p_as == &kas) 1017 sp->pr_flags |= (PR_ISSYS|PR_PCINVAL); 1018 else if (!(flags & PR_STOPPED)) 1019 sp->pr_flags |= PR_PCINVAL; 1020 else if (!prfetchinstr(lwp, &instr)) 1021 sp->pr_flags |= PR_PCINVAL; 1022 else 1023 sp->pr_instr = (uint32_t)instr; 1024 1025 /* 1026 * Drop p_lock while touching the lwp's stack. 1027 */ 1028 mutex_exit(&p->p_lock); 1029 if (prisstep(lwp)) 1030 sp->pr_flags |= PR_STEP; 1031 if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) { 1032 int i; 1033 1034 sp->pr_syscall = get_syscall32_args(lwp, 1035 (int *)sp->pr_sysarg, &i); 1036 sp->pr_nsysarg = (ushort_t)i; 1037 } 1038 if ((flags & PR_STOPPED) || t == curthread) 1039 prgetprregs32(lwp, sp->pr_reg); 1040 if ((t->t_state == TS_STOPPED && t->t_whystop == PR_SYSEXIT) || 1041 (flags & PR_VFORKP)) { 1042 long r1, r2; 1043 user_t *up; 1044 auxv_t *auxp; 1045 int i; 1046 1047 sp->pr_errno = prgetrvals(lwp, &r1, &r2); 1048 if (sp->pr_errno == 0) { 1049 sp->pr_rval1 = (int32_t)r1; 1050 sp->pr_rval2 = (int32_t)r2; 1051 sp->pr_errpriv = PRIV_NONE; 1052 } else 1053 sp->pr_errpriv = lwp->lwp_badpriv; 1054 1055 if (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve) { 1056 up = PTOU(p); 1057 sp->pr_sysarg[0] = 0; 1058 sp->pr_sysarg[1] = (caddr32_t)up->u_argv; 1059 sp->pr_sysarg[2] = (caddr32_t)up->u_envp; 1060 for (i = 0, auxp = up->u_auxv; 1061 i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]); 1062 i++, auxp++) { 1063 if (auxp->a_type == AT_SUN_EXECNAME) { 1064 sp->pr_sysarg[0] = 1065 (caddr32_t)(uintptr_t)auxp->a_un.a_ptr; 1066 break; 1067 } 1068 } 1069 } 1070 } 1071 if (prhasfp()) 1072 prgetprfpregs32(lwp, &sp->pr_fpreg); 1073 mutex_enter(&p->p_lock); 1074 } 1075 1076 void 1077 prgetstatus32(proc_t *p, pstatus32_t *sp, zone_t *zp) 1078 { 1079 kthread_t *t; 1080 1081 ASSERT(MUTEX_HELD(&p->p_lock)); 1082 1083 t = prchoose(p); /* returns locked thread */ 1084 ASSERT(t != NULL); 1085 thread_unlock(t); 1086 1087 /* just bzero the process part, prgetlwpstatus32() does the rest */ 1088 bzero(sp, sizeof (pstatus32_t) - sizeof (lwpstatus32_t)); 1089 sp->pr_nlwp = p->p_lwpcnt; 1090 sp->pr_nzomb = p->p_zombcnt; 1091 prassignset(&sp->pr_sigpend, &p->p_sig); 1092 sp->pr_brkbase = (uint32_t)(uintptr_t)p->p_brkbase; 1093 sp->pr_brksize = (uint32_t)p->p_brksize; 1094 sp->pr_stkbase = (uint32_t)(uintptr_t)prgetstackbase(p); 1095 sp->pr_stksize = (uint32_t)p->p_stksize; 1096 sp->pr_pid = p->p_pid; 1097 if (curproc->p_zone->zone_id != GLOBAL_ZONEID && 1098 (p->p_flag & SZONETOP)) { 1099 ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID); 1100 /* 1101 * Inside local zones, fake zsched's pid as parent pids for 1102 * processes which reference processes outside of the zone. 1103 */ 1104 sp->pr_ppid = curproc->p_zone->zone_zsched->p_pid; 1105 } else { 1106 sp->pr_ppid = p->p_ppid; 1107 } 1108 sp->pr_pgid = p->p_pgrp; 1109 sp->pr_sid = p->p_sessp->s_sid; 1110 sp->pr_taskid = p->p_task->tk_tkid; 1111 sp->pr_projid = p->p_task->tk_proj->kpj_id; 1112 sp->pr_zoneid = p->p_zone->zone_id; 1113 hrt2ts32(mstate_aggr_state(p, LMS_USER), &sp->pr_utime); 1114 hrt2ts32(mstate_aggr_state(p, LMS_SYSTEM), &sp->pr_stime); 1115 TICK_TO_TIMESTRUC32(p->p_cutime, &sp->pr_cutime); 1116 TICK_TO_TIMESTRUC32(p->p_cstime, &sp->pr_cstime); 1117 prassignset(&sp->pr_sigtrace, &p->p_sigmask); 1118 prassignset(&sp->pr_flttrace, &p->p_fltmask); 1119 prassignset(&sp->pr_sysentry, &PTOU(p)->u_entrymask); 1120 prassignset(&sp->pr_sysexit, &PTOU(p)->u_exitmask); 1121 switch (p->p_model) { 1122 case DATAMODEL_ILP32: 1123 sp->pr_dmodel = PR_MODEL_ILP32; 1124 break; 1125 case DATAMODEL_LP64: 1126 sp->pr_dmodel = PR_MODEL_LP64; 1127 break; 1128 } 1129 if (p->p_agenttp) 1130 sp->pr_agentid = p->p_agenttp->t_tid; 1131 1132 /* get the chosen lwp's status */ 1133 prgetlwpstatus32(t, &sp->pr_lwp, zp); 1134 1135 /* replicate the flags */ 1136 sp->pr_flags = sp->pr_lwp.pr_flags; 1137 } 1138 #endif /* _SYSCALL32_IMPL */ 1139 1140 /* 1141 * Return lwp status. 1142 */ 1143 void 1144 prgetlwpstatus(kthread_t *t, lwpstatus_t *sp, zone_t *zp) 1145 { 1146 proc_t *p = ttoproc(t); 1147 klwp_t *lwp = ttolwp(t); 1148 struct mstate *ms = &lwp->lwp_mstate; 1149 hrtime_t usr, sys; 1150 int flags; 1151 ulong_t instr; 1152 1153 ASSERT(MUTEX_HELD(&p->p_lock)); 1154 1155 bzero(sp, sizeof (*sp)); 1156 flags = 0L; 1157 if (t->t_state == TS_STOPPED) { 1158 flags |= PR_STOPPED; 1159 if ((t->t_schedflag & TS_PSTART) == 0) 1160 flags |= PR_ISTOP; 1161 } else if (VSTOPPED(t)) { 1162 flags |= PR_STOPPED|PR_ISTOP; 1163 } 1164 if (!(flags & PR_ISTOP) && (t->t_proc_flag & TP_PRSTOP)) 1165 flags |= PR_DSTOP; 1166 if (lwp->lwp_asleep) 1167 flags |= PR_ASLEEP; 1168 if (t == p->p_agenttp) 1169 flags |= PR_AGENT; 1170 if (!(t->t_proc_flag & TP_TWAIT)) 1171 flags |= PR_DETACH; 1172 if (t->t_proc_flag & TP_DAEMON) 1173 flags |= PR_DAEMON; 1174 if (p->p_proc_flag & P_PR_FORK) 1175 flags |= PR_FORK; 1176 if (p->p_proc_flag & P_PR_RUNLCL) 1177 flags |= PR_RLC; 1178 if (p->p_proc_flag & P_PR_KILLCL) 1179 flags |= PR_KLC; 1180 if (p->p_proc_flag & P_PR_ASYNC) 1181 flags |= PR_ASYNC; 1182 if (p->p_proc_flag & P_PR_BPTADJ) 1183 flags |= PR_BPTADJ; 1184 if (p->p_proc_flag & P_PR_PTRACE) 1185 flags |= PR_PTRACE; 1186 if (p->p_flag & SMSACCT) 1187 flags |= PR_MSACCT; 1188 if (p->p_flag & SMSFORK) 1189 flags |= PR_MSFORK; 1190 if (p->p_flag & SVFWAIT) 1191 flags |= PR_VFORKP; 1192 if (p->p_pgidp->pid_pgorphaned) 1193 flags |= PR_ORPHAN; 1194 sp->pr_flags = flags; 1195 if (VSTOPPED(t)) { 1196 sp->pr_why = PR_REQUESTED; 1197 sp->pr_what = 0; 1198 } else { 1199 sp->pr_why = t->t_whystop; 1200 sp->pr_what = t->t_whatstop; 1201 } 1202 sp->pr_lwpid = t->t_tid; 1203 sp->pr_cursig = lwp->lwp_cursig; 1204 prassignset(&sp->pr_lwppend, &t->t_sig); 1205 schedctl_finish_sigblock(t); 1206 prassignset(&sp->pr_lwphold, &t->t_hold); 1207 if (t->t_whystop == PR_FAULTED) 1208 bcopy(&lwp->lwp_siginfo, 1209 &sp->pr_info, sizeof (k_siginfo_t)); 1210 else if (lwp->lwp_curinfo) 1211 bcopy(&lwp->lwp_curinfo->sq_info, 1212 &sp->pr_info, sizeof (k_siginfo_t)); 1213 if (SI_FROMUSER(&lwp->lwp_siginfo) && zp->zone_id != GLOBAL_ZONEID && 1214 sp->pr_info.si_zoneid != zp->zone_id) { 1215 sp->pr_info.si_pid = zp->zone_zsched->p_pid; 1216 sp->pr_info.si_uid = 0; 1217 sp->pr_info.si_ctid = -1; 1218 sp->pr_info.si_zoneid = zp->zone_id; 1219 } 1220 sp->pr_altstack = lwp->lwp_sigaltstack; 1221 prgetaction(p, PTOU(p), lwp->lwp_cursig, &sp->pr_action); 1222 sp->pr_oldcontext = (uintptr_t)lwp->lwp_oldcontext; 1223 sp->pr_ustack = lwp->lwp_ustack; 1224 (void) strncpy(sp->pr_clname, sclass[t->t_cid].cl_name, 1225 sizeof (sp->pr_clname) - 1); 1226 if (flags & PR_STOPPED) 1227 hrt2ts(t->t_stoptime, &sp->pr_tstamp); 1228 usr = ms->ms_acct[LMS_USER]; 1229 sys = ms->ms_acct[LMS_SYSTEM] + ms->ms_acct[LMS_TRAP]; 1230 scalehrtime(&usr); 1231 scalehrtime(&sys); 1232 hrt2ts(usr, &sp->pr_utime); 1233 hrt2ts(sys, &sp->pr_stime); 1234 1235 /* 1236 * Fetch the current instruction, if not a system process. 1237 * We don't attempt this unless the lwp is stopped. 1238 */ 1239 if ((p->p_flag & SSYS) || p->p_as == &kas) 1240 sp->pr_flags |= (PR_ISSYS|PR_PCINVAL); 1241 else if (!(flags & PR_STOPPED)) 1242 sp->pr_flags |= PR_PCINVAL; 1243 else if (!prfetchinstr(lwp, &instr)) 1244 sp->pr_flags |= PR_PCINVAL; 1245 else 1246 sp->pr_instr = instr; 1247 1248 /* 1249 * Drop p_lock while touching the lwp's stack. 1250 */ 1251 mutex_exit(&p->p_lock); 1252 if (prisstep(lwp)) 1253 sp->pr_flags |= PR_STEP; 1254 if ((flags & (PR_STOPPED|PR_ASLEEP)) && t->t_sysnum) { 1255 int i; 1256 1257 sp->pr_syscall = get_syscall_args(lwp, 1258 (long *)sp->pr_sysarg, &i); 1259 sp->pr_nsysarg = (ushort_t)i; 1260 } 1261 if ((flags & PR_STOPPED) || t == curthread) 1262 prgetprregs(lwp, sp->pr_reg); 1263 if ((t->t_state == TS_STOPPED && t->t_whystop == PR_SYSEXIT) || 1264 (flags & PR_VFORKP)) { 1265 user_t *up; 1266 auxv_t *auxp; 1267 int i; 1268 1269 sp->pr_errno = prgetrvals(lwp, &sp->pr_rval1, &sp->pr_rval2); 1270 if (sp->pr_errno == 0) 1271 sp->pr_errpriv = PRIV_NONE; 1272 else 1273 sp->pr_errpriv = lwp->lwp_badpriv; 1274 1275 if (t->t_sysnum == SYS_exec || t->t_sysnum == SYS_execve) { 1276 up = PTOU(p); 1277 sp->pr_sysarg[0] = 0; 1278 sp->pr_sysarg[1] = (uintptr_t)up->u_argv; 1279 sp->pr_sysarg[2] = (uintptr_t)up->u_envp; 1280 for (i = 0, auxp = up->u_auxv; 1281 i < sizeof (up->u_auxv) / sizeof (up->u_auxv[0]); 1282 i++, auxp++) { 1283 if (auxp->a_type == AT_SUN_EXECNAME) { 1284 sp->pr_sysarg[0] = 1285 (uintptr_t)auxp->a_un.a_ptr; 1286 break; 1287 } 1288 } 1289 } 1290 } 1291 if (prhasfp()) 1292 prgetprfpregs(lwp, &sp->pr_fpreg); 1293 mutex_enter(&p->p_lock); 1294 } 1295 1296 /* 1297 * Get the sigaction structure for the specified signal. The u-block 1298 * must already have been mapped in by the caller. 1299 */ 1300 void 1301 prgetaction(proc_t *p, user_t *up, uint_t sig, struct sigaction *sp) 1302 { 1303 bzero(sp, sizeof (*sp)); 1304 1305 if (sig != 0 && (unsigned)sig < NSIG) { 1306 sp->sa_handler = up->u_signal[sig-1]; 1307 prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]); 1308 if (sigismember(&up->u_sigonstack, sig)) 1309 sp->sa_flags |= SA_ONSTACK; 1310 if (sigismember(&up->u_sigresethand, sig)) 1311 sp->sa_flags |= SA_RESETHAND; 1312 if (sigismember(&up->u_sigrestart, sig)) 1313 sp->sa_flags |= SA_RESTART; 1314 if (sigismember(&p->p_siginfo, sig)) 1315 sp->sa_flags |= SA_SIGINFO; 1316 if (sigismember(&up->u_signodefer, sig)) 1317 sp->sa_flags |= SA_NODEFER; 1318 if (sig == SIGCLD) { 1319 if (p->p_flag & SNOWAIT) 1320 sp->sa_flags |= SA_NOCLDWAIT; 1321 if ((p->p_flag & SJCTL) == 0) 1322 sp->sa_flags |= SA_NOCLDSTOP; 1323 } 1324 } 1325 } 1326 1327 #ifdef _SYSCALL32_IMPL 1328 void 1329 prgetaction32(proc_t *p, user_t *up, uint_t sig, struct sigaction32 *sp) 1330 { 1331 bzero(sp, sizeof (*sp)); 1332 1333 if (sig != 0 && (unsigned)sig < NSIG) { 1334 sp->sa_handler = (caddr32_t)(uintptr_t)up->u_signal[sig-1]; 1335 prassignset(&sp->sa_mask, &up->u_sigmask[sig-1]); 1336 if (sigismember(&up->u_sigonstack, sig)) 1337 sp->sa_flags |= SA_ONSTACK; 1338 if (sigismember(&up->u_sigresethand, sig)) 1339 sp->sa_flags |= SA_RESETHAND; 1340 if (sigismember(&up->u_sigrestart, sig)) 1341 sp->sa_flags |= SA_RESTART; 1342 if (sigismember(&p->p_siginfo, sig)) 1343 sp->sa_flags |= SA_SIGINFO; 1344 if (sigismember(&up->u_signodefer, sig)) 1345 sp->sa_flags |= SA_NODEFER; 1346 if (sig == SIGCLD) { 1347 if (p->p_flag & SNOWAIT) 1348 sp->sa_flags |= SA_NOCLDWAIT; 1349 if ((p->p_flag & SJCTL) == 0) 1350 sp->sa_flags |= SA_NOCLDSTOP; 1351 } 1352 } 1353 } 1354 #endif /* _SYSCALL32_IMPL */ 1355 1356 /* 1357 * Count the number of segments in this process's address space. 1358 */ 1359 int 1360 prnsegs(struct as *as, int reserved) 1361 { 1362 int n = 0; 1363 struct seg *seg; 1364 1365 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 1366 1367 for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 1368 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved); 1369 caddr_t saddr, naddr; 1370 void *tmp = NULL; 1371 1372 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1373 (void) pr_getprot(seg, reserved, &tmp, 1374 &saddr, &naddr, eaddr); 1375 if (saddr != naddr) 1376 n++; 1377 } 1378 1379 ASSERT(tmp == NULL); 1380 } 1381 1382 return (n); 1383 } 1384 1385 /* 1386 * Convert uint32_t to decimal string w/o leading zeros. 1387 * Add trailing null characters if 'len' is greater than string length. 1388 * Return the string length. 1389 */ 1390 int 1391 pr_u32tos(uint32_t n, char *s, int len) 1392 { 1393 char cbuf[11]; /* 32-bit unsigned integer fits in 10 digits */ 1394 char *cp = cbuf; 1395 char *end = s + len; 1396 1397 do { 1398 *cp++ = (char)(n % 10 + '0'); 1399 n /= 10; 1400 } while (n); 1401 1402 len = (int)(cp - cbuf); 1403 1404 do { 1405 *s++ = *--cp; 1406 } while (cp > cbuf); 1407 1408 while (s < end) /* optional pad */ 1409 *s++ = '\0'; 1410 1411 return (len); 1412 } 1413 1414 /* 1415 * Convert uint64_t to decimal string w/o leading zeros. 1416 * Return the string length. 1417 */ 1418 static int 1419 pr_u64tos(uint64_t n, char *s) 1420 { 1421 char cbuf[21]; /* 64-bit unsigned integer fits in 20 digits */ 1422 char *cp = cbuf; 1423 int len; 1424 1425 do { 1426 *cp++ = (char)(n % 10 + '0'); 1427 n /= 10; 1428 } while (n); 1429 1430 len = (int)(cp - cbuf); 1431 1432 do { 1433 *s++ = *--cp; 1434 } while (cp > cbuf); 1435 1436 return (len); 1437 } 1438 1439 void 1440 pr_object_name(char *name, vnode_t *vp, struct vattr *vattr) 1441 { 1442 char *s = name; 1443 struct vfs *vfsp; 1444 struct vfssw *vfsswp; 1445 1446 if ((vfsp = vp->v_vfsp) != NULL && 1447 ((vfsswp = vfssw + vfsp->vfs_fstype), vfsswp->vsw_name) && 1448 *vfsswp->vsw_name) { 1449 (void) strcpy(s, vfsswp->vsw_name); 1450 s += strlen(s); 1451 *s++ = '.'; 1452 } 1453 s += pr_u32tos(getmajor(vattr->va_fsid), s, 0); 1454 *s++ = '.'; 1455 s += pr_u32tos(getminor(vattr->va_fsid), s, 0); 1456 *s++ = '.'; 1457 s += pr_u64tos(vattr->va_nodeid, s); 1458 *s++ = '\0'; 1459 } 1460 1461 struct seg * 1462 break_seg(proc_t *p) 1463 { 1464 caddr_t addr = p->p_brkbase; 1465 struct seg *seg; 1466 struct vnode *vp; 1467 1468 if (p->p_brksize != 0) 1469 addr += p->p_brksize - 1; 1470 seg = as_segat(p->p_as, addr); 1471 if (seg != NULL && seg->s_ops == &segvn_ops && 1472 (SEGOP_GETVP(seg, seg->s_base, &vp) != 0 || vp == NULL)) 1473 return (seg); 1474 return (NULL); 1475 } 1476 1477 /* 1478 * Return an array of structures with memory map information. 1479 * We allocate here; the caller must deallocate. 1480 */ 1481 #define INITIAL_MAPSIZE 65536 1482 #define MAPSIZE 8192 1483 int 1484 prgetmap(proc_t *p, int reserved, prmap_t **prmapp, size_t *sizep) 1485 { 1486 struct as *as = p->p_as; 1487 int nmaps = 0; 1488 prmap_t *mp; 1489 size_t size; 1490 struct seg *seg; 1491 struct seg *brkseg, *stkseg; 1492 struct vnode *vp; 1493 struct vattr vattr; 1494 uint_t prot; 1495 1496 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 1497 1498 /* initial allocation */ 1499 *sizep = size = INITIAL_MAPSIZE; 1500 *prmapp = mp = kmem_alloc(size, KM_SLEEP); 1501 1502 if ((seg = AS_SEGFIRST(as)) == NULL) 1503 return (0); 1504 1505 brkseg = break_seg(p); 1506 stkseg = as_segat(as, prgetstackbase(p)); 1507 1508 do { 1509 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved); 1510 caddr_t saddr, naddr; 1511 void *tmp = NULL; 1512 1513 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1514 prot = pr_getprot(seg, reserved, &tmp, 1515 &saddr, &naddr, eaddr); 1516 if (saddr == naddr) 1517 continue; 1518 /* reallocate if necessary */ 1519 if ((nmaps + 1) * sizeof (prmap_t) > size) { 1520 size_t newsize = size + 3 * size / 16; 1521 prmap_t *newmp = kmem_alloc(newsize, KM_SLEEP); 1522 1523 bcopy(*prmapp, newmp, nmaps * sizeof (prmap_t)); 1524 kmem_free(*prmapp, size); 1525 *sizep = size = newsize; 1526 *prmapp = newmp; 1527 mp = newmp + nmaps; 1528 } 1529 bzero(mp, sizeof (*mp)); 1530 mp->pr_vaddr = (uintptr_t)saddr; 1531 mp->pr_size = naddr - saddr; 1532 mp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 1533 mp->pr_mflags = 0; 1534 if (prot & PROT_READ) 1535 mp->pr_mflags |= MA_READ; 1536 if (prot & PROT_WRITE) 1537 mp->pr_mflags |= MA_WRITE; 1538 if (prot & PROT_EXEC) 1539 mp->pr_mflags |= MA_EXEC; 1540 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 1541 mp->pr_mflags |= MA_SHARED; 1542 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 1543 mp->pr_mflags |= MA_NORESERVE; 1544 if (seg->s_ops == &segspt_shmops || 1545 (seg->s_ops == &segvn_ops && 1546 (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL))) 1547 mp->pr_mflags |= MA_ANON; 1548 if (seg == brkseg) 1549 mp->pr_mflags |= MA_BREAK; 1550 else if (seg == stkseg) { 1551 mp->pr_mflags |= MA_STACK; 1552 if (reserved) { 1553 size_t maxstack = 1554 ((size_t)p->p_stk_ctl + 1555 PAGEOFFSET) & PAGEMASK; 1556 mp->pr_vaddr = 1557 (uintptr_t)prgetstackbase(p) + 1558 p->p_stksize - maxstack; 1559 mp->pr_size = (uintptr_t)naddr - 1560 mp->pr_vaddr; 1561 } 1562 } 1563 if (seg->s_ops == &segspt_shmops) 1564 mp->pr_mflags |= MA_ISM | MA_SHM; 1565 mp->pr_pagesize = PAGESIZE; 1566 1567 /* 1568 * Manufacture a filename for the "object" directory. 1569 */ 1570 vattr.va_mask = AT_FSID|AT_NODEID; 1571 if (seg->s_ops == &segvn_ops && 1572 SEGOP_GETVP(seg, saddr, &vp) == 0 && 1573 vp != NULL && vp->v_type == VREG && 1574 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 1575 if (vp == p->p_exec) 1576 (void) strcpy(mp->pr_mapname, "a.out"); 1577 else 1578 pr_object_name(mp->pr_mapname, 1579 vp, &vattr); 1580 } 1581 1582 /* 1583 * Get the SysV shared memory id, if any. 1584 */ 1585 if ((mp->pr_mflags & MA_SHARED) && p->p_segacct && 1586 (mp->pr_shmid = shmgetid(p, seg->s_base)) != 1587 SHMID_NONE) { 1588 if (mp->pr_shmid == SHMID_FREE) 1589 mp->pr_shmid = -1; 1590 1591 mp->pr_mflags |= MA_SHM; 1592 } else { 1593 mp->pr_shmid = -1; 1594 } 1595 1596 mp++; 1597 nmaps++; 1598 } 1599 ASSERT(tmp == NULL); 1600 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 1601 1602 return (nmaps); 1603 } 1604 1605 #ifdef _SYSCALL32_IMPL 1606 int 1607 prgetmap32(proc_t *p, int reserved, prmap32_t **prmapp, size_t *sizep) 1608 { 1609 struct as *as = p->p_as; 1610 int nmaps = 0; 1611 prmap32_t *mp; 1612 size_t size; 1613 struct seg *seg; 1614 struct seg *brkseg, *stkseg; 1615 struct vnode *vp; 1616 struct vattr vattr; 1617 uint_t prot; 1618 1619 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 1620 1621 /* initial allocation */ 1622 *sizep = size = INITIAL_MAPSIZE; 1623 *prmapp = mp = kmem_alloc(size, KM_SLEEP); 1624 1625 if ((seg = AS_SEGFIRST(as)) == NULL) 1626 return (0); 1627 1628 brkseg = break_seg(p); 1629 stkseg = as_segat(as, prgetstackbase(p)); 1630 1631 do { 1632 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, reserved); 1633 caddr_t saddr, naddr; 1634 void *tmp = NULL; 1635 1636 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1637 prot = pr_getprot(seg, reserved, &tmp, 1638 &saddr, &naddr, eaddr); 1639 if (saddr == naddr) 1640 continue; 1641 /* reallocate if necessary */ 1642 if ((nmaps + 1) * sizeof (prmap32_t) > size) { 1643 size_t newsize = size + 3 * size / 16; 1644 prmap32_t *newmp = 1645 kmem_alloc(newsize, KM_SLEEP); 1646 1647 bcopy(*prmapp, newmp, 1648 nmaps * sizeof (prmap32_t)); 1649 kmem_free(*prmapp, size); 1650 *sizep = size = newsize; 1651 *prmapp = newmp; 1652 mp = newmp + nmaps; 1653 } 1654 bzero(mp, sizeof (*mp)); 1655 mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr; 1656 mp->pr_size = (size32_t)(naddr - saddr); 1657 mp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 1658 mp->pr_mflags = 0; 1659 if (prot & PROT_READ) 1660 mp->pr_mflags |= MA_READ; 1661 if (prot & PROT_WRITE) 1662 mp->pr_mflags |= MA_WRITE; 1663 if (prot & PROT_EXEC) 1664 mp->pr_mflags |= MA_EXEC; 1665 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 1666 mp->pr_mflags |= MA_SHARED; 1667 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 1668 mp->pr_mflags |= MA_NORESERVE; 1669 if (seg->s_ops == &segspt_shmops || 1670 (seg->s_ops == &segvn_ops && 1671 (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL))) 1672 mp->pr_mflags |= MA_ANON; 1673 if (seg == brkseg) 1674 mp->pr_mflags |= MA_BREAK; 1675 else if (seg == stkseg) { 1676 mp->pr_mflags |= MA_STACK; 1677 if (reserved) { 1678 size_t maxstack = 1679 ((size_t)p->p_stk_ctl + 1680 PAGEOFFSET) & PAGEMASK; 1681 uintptr_t vaddr = 1682 (uintptr_t)prgetstackbase(p) + 1683 p->p_stksize - maxstack; 1684 mp->pr_vaddr = (caddr32_t)vaddr; 1685 mp->pr_size = (size32_t) 1686 ((uintptr_t)naddr - vaddr); 1687 } 1688 } 1689 if (seg->s_ops == &segspt_shmops) 1690 mp->pr_mflags |= MA_ISM | MA_SHM; 1691 mp->pr_pagesize = PAGESIZE; 1692 1693 /* 1694 * Manufacture a filename for the "object" directory. 1695 */ 1696 vattr.va_mask = AT_FSID|AT_NODEID; 1697 if (seg->s_ops == &segvn_ops && 1698 SEGOP_GETVP(seg, saddr, &vp) == 0 && 1699 vp != NULL && vp->v_type == VREG && 1700 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 1701 if (vp == p->p_exec) 1702 (void) strcpy(mp->pr_mapname, "a.out"); 1703 else 1704 pr_object_name(mp->pr_mapname, 1705 vp, &vattr); 1706 } 1707 1708 /* 1709 * Get the SysV shared memory id, if any. 1710 */ 1711 if ((mp->pr_mflags & MA_SHARED) && p->p_segacct && 1712 (mp->pr_shmid = shmgetid(p, seg->s_base)) != 1713 SHMID_NONE) { 1714 if (mp->pr_shmid == SHMID_FREE) 1715 mp->pr_shmid = -1; 1716 1717 mp->pr_mflags |= MA_SHM; 1718 } else { 1719 mp->pr_shmid = -1; 1720 } 1721 1722 mp++; 1723 nmaps++; 1724 } 1725 ASSERT(tmp == NULL); 1726 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 1727 1728 return (nmaps); 1729 } 1730 #endif /* _SYSCALL32_IMPL */ 1731 1732 /* 1733 * Return the size of the /proc page data file. 1734 */ 1735 size_t 1736 prpdsize(struct as *as) 1737 { 1738 struct seg *seg; 1739 size_t size; 1740 1741 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 1742 1743 if ((seg = AS_SEGFIRST(as)) == NULL) 1744 return (0); 1745 1746 size = sizeof (prpageheader_t); 1747 do { 1748 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 1749 caddr_t saddr, naddr; 1750 void *tmp = NULL; 1751 size_t npage; 1752 1753 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1754 (void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 1755 if ((npage = (naddr - saddr) / PAGESIZE) != 0) 1756 size += sizeof (prasmap_t) + round8(npage); 1757 } 1758 ASSERT(tmp == NULL); 1759 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 1760 1761 return (size); 1762 } 1763 1764 #ifdef _SYSCALL32_IMPL 1765 size_t 1766 prpdsize32(struct as *as) 1767 { 1768 struct seg *seg; 1769 size_t size; 1770 1771 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 1772 1773 if ((seg = AS_SEGFIRST(as)) == NULL) 1774 return (0); 1775 1776 size = sizeof (prpageheader32_t); 1777 do { 1778 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 1779 caddr_t saddr, naddr; 1780 void *tmp = NULL; 1781 size_t npage; 1782 1783 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1784 (void) pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 1785 if ((npage = (naddr - saddr) / PAGESIZE) != 0) 1786 size += sizeof (prasmap32_t) + round8(npage); 1787 } 1788 ASSERT(tmp == NULL); 1789 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 1790 1791 return (size); 1792 } 1793 #endif /* _SYSCALL32_IMPL */ 1794 1795 /* 1796 * Read page data information. 1797 */ 1798 int 1799 prpdread(proc_t *p, uint_t hatid, struct uio *uiop) 1800 { 1801 struct as *as = p->p_as; 1802 caddr_t buf; 1803 size_t size; 1804 prpageheader_t *php; 1805 prasmap_t *pmp; 1806 struct seg *seg; 1807 int error; 1808 1809 again: 1810 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1811 1812 if ((seg = AS_SEGFIRST(as)) == NULL) { 1813 AS_LOCK_EXIT(as, &as->a_lock); 1814 return (0); 1815 } 1816 size = prpdsize(as); 1817 if (uiop->uio_resid < size) { 1818 AS_LOCK_EXIT(as, &as->a_lock); 1819 return (E2BIG); 1820 } 1821 1822 buf = kmem_zalloc(size, KM_SLEEP); 1823 php = (prpageheader_t *)buf; 1824 pmp = (prasmap_t *)(buf + sizeof (prpageheader_t)); 1825 1826 hrt2ts(gethrtime(), &php->pr_tstamp); 1827 php->pr_nmap = 0; 1828 php->pr_npage = 0; 1829 do { 1830 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 1831 caddr_t saddr, naddr; 1832 void *tmp = NULL; 1833 1834 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1835 struct vnode *vp; 1836 struct vattr vattr; 1837 size_t len; 1838 size_t npage; 1839 uint_t prot; 1840 uintptr_t next; 1841 1842 prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 1843 if ((len = (size_t)(naddr - saddr)) == 0) 1844 continue; 1845 npage = len / PAGESIZE; 1846 next = (uintptr_t)(pmp + 1) + round8(npage); 1847 /* 1848 * It's possible that the address space can change 1849 * subtlely even though we're holding as->a_lock 1850 * due to the nondeterminism of page_exists() in 1851 * the presence of asychronously flushed pages or 1852 * mapped files whose sizes are changing. 1853 * page_exists() may be called indirectly from 1854 * pr_getprot() by a SEGOP_INCORE() routine. 1855 * If this happens we need to make sure we don't 1856 * overrun the buffer whose size we computed based 1857 * on the initial iteration through the segments. 1858 * Once we've detected an overflow, we need to clean 1859 * up the temporary memory allocated in pr_getprot() 1860 * and retry. If there's a pending signal, we return 1861 * EINTR so that this thread can be dislodged if 1862 * a latent bug causes us to spin indefinitely. 1863 */ 1864 if (next > (uintptr_t)buf + size) { 1865 pr_getprot_done(&tmp); 1866 AS_LOCK_EXIT(as, &as->a_lock); 1867 1868 kmem_free(buf, size); 1869 1870 if (ISSIG(curthread, JUSTLOOKING)) 1871 return (EINTR); 1872 1873 goto again; 1874 } 1875 1876 php->pr_nmap++; 1877 php->pr_npage += npage; 1878 pmp->pr_vaddr = (uintptr_t)saddr; 1879 pmp->pr_npage = npage; 1880 pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 1881 pmp->pr_mflags = 0; 1882 if (prot & PROT_READ) 1883 pmp->pr_mflags |= MA_READ; 1884 if (prot & PROT_WRITE) 1885 pmp->pr_mflags |= MA_WRITE; 1886 if (prot & PROT_EXEC) 1887 pmp->pr_mflags |= MA_EXEC; 1888 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 1889 pmp->pr_mflags |= MA_SHARED; 1890 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 1891 pmp->pr_mflags |= MA_NORESERVE; 1892 if (seg->s_ops == &segspt_shmops || 1893 (seg->s_ops == &segvn_ops && 1894 (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL))) 1895 pmp->pr_mflags |= MA_ANON; 1896 if (seg->s_ops == &segspt_shmops) 1897 pmp->pr_mflags |= MA_ISM | MA_SHM; 1898 pmp->pr_pagesize = PAGESIZE; 1899 /* 1900 * Manufacture a filename for the "object" directory. 1901 */ 1902 vattr.va_mask = AT_FSID|AT_NODEID; 1903 if (seg->s_ops == &segvn_ops && 1904 SEGOP_GETVP(seg, saddr, &vp) == 0 && 1905 vp != NULL && vp->v_type == VREG && 1906 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 1907 if (vp == p->p_exec) 1908 (void) strcpy(pmp->pr_mapname, "a.out"); 1909 else 1910 pr_object_name(pmp->pr_mapname, 1911 vp, &vattr); 1912 } 1913 1914 /* 1915 * Get the SysV shared memory id, if any. 1916 */ 1917 if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct && 1918 (pmp->pr_shmid = shmgetid(p, seg->s_base)) != 1919 SHMID_NONE) { 1920 if (pmp->pr_shmid == SHMID_FREE) 1921 pmp->pr_shmid = -1; 1922 1923 pmp->pr_mflags |= MA_SHM; 1924 } else { 1925 pmp->pr_shmid = -1; 1926 } 1927 1928 hat_getstat(as, saddr, len, hatid, 1929 (char *)(pmp + 1), HAT_SYNC_ZERORM); 1930 pmp = (prasmap_t *)next; 1931 } 1932 ASSERT(tmp == NULL); 1933 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 1934 1935 AS_LOCK_EXIT(as, &as->a_lock); 1936 1937 ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size); 1938 error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop); 1939 kmem_free(buf, size); 1940 1941 return (error); 1942 } 1943 1944 #ifdef _SYSCALL32_IMPL 1945 int 1946 prpdread32(proc_t *p, uint_t hatid, struct uio *uiop) 1947 { 1948 struct as *as = p->p_as; 1949 caddr_t buf; 1950 size_t size; 1951 prpageheader32_t *php; 1952 prasmap32_t *pmp; 1953 struct seg *seg; 1954 int error; 1955 1956 again: 1957 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 1958 1959 if ((seg = AS_SEGFIRST(as)) == NULL) { 1960 AS_LOCK_EXIT(as, &as->a_lock); 1961 return (0); 1962 } 1963 size = prpdsize32(as); 1964 if (uiop->uio_resid < size) { 1965 AS_LOCK_EXIT(as, &as->a_lock); 1966 return (E2BIG); 1967 } 1968 1969 buf = kmem_zalloc(size, KM_SLEEP); 1970 php = (prpageheader32_t *)buf; 1971 pmp = (prasmap32_t *)(buf + sizeof (prpageheader32_t)); 1972 1973 hrt2ts32(gethrtime(), &php->pr_tstamp); 1974 php->pr_nmap = 0; 1975 php->pr_npage = 0; 1976 do { 1977 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 1978 caddr_t saddr, naddr; 1979 void *tmp = NULL; 1980 1981 for (saddr = seg->s_base; saddr < eaddr; saddr = naddr) { 1982 struct vnode *vp; 1983 struct vattr vattr; 1984 size_t len; 1985 size_t npage; 1986 uint_t prot; 1987 uintptr_t next; 1988 1989 prot = pr_getprot(seg, 0, &tmp, &saddr, &naddr, eaddr); 1990 if ((len = (size_t)(naddr - saddr)) == 0) 1991 continue; 1992 npage = len / PAGESIZE; 1993 next = (uintptr_t)(pmp + 1) + round8(npage); 1994 /* 1995 * It's possible that the address space can change 1996 * subtlely even though we're holding as->a_lock 1997 * due to the nondeterminism of page_exists() in 1998 * the presence of asychronously flushed pages or 1999 * mapped files whose sizes are changing. 2000 * page_exists() may be called indirectly from 2001 * pr_getprot() by a SEGOP_INCORE() routine. 2002 * If this happens we need to make sure we don't 2003 * overrun the buffer whose size we computed based 2004 * on the initial iteration through the segments. 2005 * Once we've detected an overflow, we need to clean 2006 * up the temporary memory allocated in pr_getprot() 2007 * and retry. If there's a pending signal, we return 2008 * EINTR so that this thread can be dislodged if 2009 * a latent bug causes us to spin indefinitely. 2010 */ 2011 if (next > (uintptr_t)buf + size) { 2012 pr_getprot_done(&tmp); 2013 AS_LOCK_EXIT(as, &as->a_lock); 2014 2015 kmem_free(buf, size); 2016 2017 if (ISSIG(curthread, JUSTLOOKING)) 2018 return (EINTR); 2019 2020 goto again; 2021 } 2022 2023 php->pr_nmap++; 2024 php->pr_npage += npage; 2025 pmp->pr_vaddr = (caddr32_t)(uintptr_t)saddr; 2026 pmp->pr_npage = (size32_t)npage; 2027 pmp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 2028 pmp->pr_mflags = 0; 2029 if (prot & PROT_READ) 2030 pmp->pr_mflags |= MA_READ; 2031 if (prot & PROT_WRITE) 2032 pmp->pr_mflags |= MA_WRITE; 2033 if (prot & PROT_EXEC) 2034 pmp->pr_mflags |= MA_EXEC; 2035 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 2036 pmp->pr_mflags |= MA_SHARED; 2037 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 2038 pmp->pr_mflags |= MA_NORESERVE; 2039 if (seg->s_ops == &segspt_shmops || 2040 (seg->s_ops == &segvn_ops && 2041 (SEGOP_GETVP(seg, saddr, &vp) != 0 || vp == NULL))) 2042 pmp->pr_mflags |= MA_ANON; 2043 if (seg->s_ops == &segspt_shmops) 2044 pmp->pr_mflags |= MA_ISM | MA_SHM; 2045 pmp->pr_pagesize = PAGESIZE; 2046 /* 2047 * Manufacture a filename for the "object" directory. 2048 */ 2049 vattr.va_mask = AT_FSID|AT_NODEID; 2050 if (seg->s_ops == &segvn_ops && 2051 SEGOP_GETVP(seg, saddr, &vp) == 0 && 2052 vp != NULL && vp->v_type == VREG && 2053 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 2054 if (vp == p->p_exec) 2055 (void) strcpy(pmp->pr_mapname, "a.out"); 2056 else 2057 pr_object_name(pmp->pr_mapname, 2058 vp, &vattr); 2059 } 2060 2061 /* 2062 * Get the SysV shared memory id, if any. 2063 */ 2064 if ((pmp->pr_mflags & MA_SHARED) && p->p_segacct && 2065 (pmp->pr_shmid = shmgetid(p, seg->s_base)) != 2066 SHMID_NONE) { 2067 if (pmp->pr_shmid == SHMID_FREE) 2068 pmp->pr_shmid = -1; 2069 2070 pmp->pr_mflags |= MA_SHM; 2071 } else { 2072 pmp->pr_shmid = -1; 2073 } 2074 2075 hat_getstat(as, saddr, len, hatid, 2076 (char *)(pmp + 1), HAT_SYNC_ZERORM); 2077 pmp = (prasmap32_t *)next; 2078 } 2079 ASSERT(tmp == NULL); 2080 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 2081 2082 AS_LOCK_EXIT(as, &as->a_lock); 2083 2084 ASSERT((uintptr_t)pmp <= (uintptr_t)buf + size); 2085 error = uiomove(buf, (caddr_t)pmp - buf, UIO_READ, uiop); 2086 kmem_free(buf, size); 2087 2088 return (error); 2089 } 2090 #endif /* _SYSCALL32_IMPL */ 2091 2092 ushort_t 2093 prgetpctcpu(uint64_t pct) 2094 { 2095 /* 2096 * The value returned will be relevant in the zone of the examiner, 2097 * which may not be the same as the zone which performed the procfs 2098 * mount. 2099 */ 2100 int nonline = zone_ncpus_online_get(curproc->p_zone); 2101 2102 /* 2103 * Prorate over online cpus so we don't exceed 100% 2104 */ 2105 if (nonline > 1) 2106 pct /= nonline; 2107 pct >>= 16; /* convert to 16-bit scaled integer */ 2108 if (pct > 0x8000) /* might happen, due to rounding */ 2109 pct = 0x8000; 2110 return ((ushort_t)pct); 2111 } 2112 2113 /* 2114 * Return information used by ps(1). 2115 */ 2116 void 2117 prgetpsinfo(proc_t *p, psinfo_t *psp) 2118 { 2119 kthread_t *t; 2120 struct cred *cred; 2121 hrtime_t hrutime, hrstime; 2122 2123 ASSERT(MUTEX_HELD(&p->p_lock)); 2124 2125 if ((t = prchoose(p)) == NULL) /* returns locked thread */ 2126 bzero(psp, sizeof (*psp)); 2127 else { 2128 thread_unlock(t); 2129 bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp)); 2130 } 2131 2132 /* 2133 * only export SSYS and SMSACCT; everything else is off-limits to 2134 * userland apps. 2135 */ 2136 psp->pr_flag = p->p_flag & (SSYS | SMSACCT); 2137 psp->pr_nlwp = p->p_lwpcnt; 2138 psp->pr_nzomb = p->p_zombcnt; 2139 mutex_enter(&p->p_crlock); 2140 cred = p->p_cred; 2141 psp->pr_uid = crgetruid(cred); 2142 psp->pr_euid = crgetuid(cred); 2143 psp->pr_gid = crgetrgid(cred); 2144 psp->pr_egid = crgetgid(cred); 2145 mutex_exit(&p->p_crlock); 2146 psp->pr_pid = p->p_pid; 2147 if (curproc->p_zone->zone_id != GLOBAL_ZONEID && 2148 (p->p_flag & SZONETOP)) { 2149 ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID); 2150 /* 2151 * Inside local zones, fake zsched's pid as parent pids for 2152 * processes which reference processes outside of the zone. 2153 */ 2154 psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid; 2155 } else { 2156 psp->pr_ppid = p->p_ppid; 2157 } 2158 psp->pr_pgid = p->p_pgrp; 2159 psp->pr_sid = p->p_sessp->s_sid; 2160 psp->pr_taskid = p->p_task->tk_tkid; 2161 psp->pr_projid = p->p_task->tk_proj->kpj_id; 2162 psp->pr_poolid = p->p_pool->pool_id; 2163 psp->pr_zoneid = p->p_zone->zone_id; 2164 if ((psp->pr_contract = PRCTID(p)) == 0) 2165 psp->pr_contract = -1; 2166 psp->pr_addr = (uintptr_t)prgetpsaddr(p); 2167 switch (p->p_model) { 2168 case DATAMODEL_ILP32: 2169 psp->pr_dmodel = PR_MODEL_ILP32; 2170 break; 2171 case DATAMODEL_LP64: 2172 psp->pr_dmodel = PR_MODEL_LP64; 2173 break; 2174 } 2175 hrutime = mstate_aggr_state(p, LMS_USER); 2176 hrstime = mstate_aggr_state(p, LMS_SYSTEM); 2177 hrt2ts((hrutime + hrstime), &psp->pr_time); 2178 TICK_TO_TIMESTRUC(p->p_cutime + p->p_cstime, &psp->pr_ctime); 2179 2180 if (t == NULL) { 2181 int wcode = p->p_wcode; /* must be atomic read */ 2182 2183 if (wcode) 2184 psp->pr_wstat = wstat(wcode, p->p_wdata); 2185 psp->pr_ttydev = PRNODEV; 2186 psp->pr_lwp.pr_state = SZOMB; 2187 psp->pr_lwp.pr_sname = 'Z'; 2188 psp->pr_lwp.pr_bindpro = PBIND_NONE; 2189 psp->pr_lwp.pr_bindpset = PS_NONE; 2190 } else { 2191 user_t *up = PTOU(p); 2192 struct as *as; 2193 dev_t d; 2194 extern dev_t rwsconsdev, rconsdev, uconsdev; 2195 2196 d = cttydev(p); 2197 /* 2198 * If the controlling terminal is the real 2199 * or workstation console device, map to what the 2200 * user thinks is the console device. 2201 */ 2202 if (d == rwsconsdev || d == rconsdev) 2203 d = uconsdev; 2204 psp->pr_ttydev = (d == NODEV) ? PRNODEV : d; 2205 psp->pr_start = up->u_start; 2206 bcopy(up->u_comm, psp->pr_fname, 2207 MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1)); 2208 bcopy(up->u_psargs, psp->pr_psargs, 2209 MIN(PRARGSZ-1, PSARGSZ)); 2210 psp->pr_argc = up->u_argc; 2211 psp->pr_argv = up->u_argv; 2212 psp->pr_envp = up->u_envp; 2213 2214 /* get the chosen lwp's lwpsinfo */ 2215 prgetlwpsinfo(t, &psp->pr_lwp); 2216 2217 /* compute %cpu for the process */ 2218 if (p->p_lwpcnt == 1) 2219 psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu; 2220 else { 2221 uint64_t pct = 0; 2222 hrtime_t cur_time = gethrtime_unscaled(); 2223 2224 t = p->p_tlist; 2225 do { 2226 pct += cpu_update_pct(t, cur_time); 2227 } while ((t = t->t_forw) != p->p_tlist); 2228 2229 psp->pr_pctcpu = prgetpctcpu(pct); 2230 } 2231 if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) { 2232 psp->pr_size = 0; 2233 psp->pr_rssize = 0; 2234 } else { 2235 mutex_exit(&p->p_lock); 2236 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2237 psp->pr_size = btopr(rm_assize(as)) * (PAGESIZE / 1024); 2238 psp->pr_rssize = rm_asrss(as) * (PAGESIZE / 1024); 2239 psp->pr_pctmem = rm_pctmemory(as); 2240 AS_LOCK_EXIT(as, &as->a_lock); 2241 mutex_enter(&p->p_lock); 2242 } 2243 } 2244 } 2245 2246 #ifdef _SYSCALL32_IMPL 2247 void 2248 prgetpsinfo32(proc_t *p, psinfo32_t *psp) 2249 { 2250 kthread_t *t; 2251 struct cred *cred; 2252 hrtime_t hrutime, hrstime; 2253 2254 ASSERT(MUTEX_HELD(&p->p_lock)); 2255 2256 if ((t = prchoose(p)) == NULL) /* returns locked thread */ 2257 bzero(psp, sizeof (*psp)); 2258 else { 2259 thread_unlock(t); 2260 bzero(psp, sizeof (*psp) - sizeof (psp->pr_lwp)); 2261 } 2262 2263 /* 2264 * only export SSYS and SMSACCT; everything else is off-limits to 2265 * userland apps. 2266 */ 2267 psp->pr_flag = p->p_flag & (SSYS | SMSACCT); 2268 psp->pr_nlwp = p->p_lwpcnt; 2269 psp->pr_nzomb = p->p_zombcnt; 2270 mutex_enter(&p->p_crlock); 2271 cred = p->p_cred; 2272 psp->pr_uid = crgetruid(cred); 2273 psp->pr_euid = crgetuid(cred); 2274 psp->pr_gid = crgetrgid(cred); 2275 psp->pr_egid = crgetgid(cred); 2276 mutex_exit(&p->p_crlock); 2277 psp->pr_pid = p->p_pid; 2278 if (curproc->p_zone->zone_id != GLOBAL_ZONEID && 2279 (p->p_flag & SZONETOP)) { 2280 ASSERT(p->p_zone->zone_id != GLOBAL_ZONEID); 2281 /* 2282 * Inside local zones, fake zsched's pid as parent pids for 2283 * processes which reference processes outside of the zone. 2284 */ 2285 psp->pr_ppid = curproc->p_zone->zone_zsched->p_pid; 2286 } else { 2287 psp->pr_ppid = p->p_ppid; 2288 } 2289 psp->pr_pgid = p->p_pgrp; 2290 psp->pr_sid = p->p_sessp->s_sid; 2291 psp->pr_taskid = p->p_task->tk_tkid; 2292 psp->pr_projid = p->p_task->tk_proj->kpj_id; 2293 psp->pr_poolid = p->p_pool->pool_id; 2294 psp->pr_zoneid = p->p_zone->zone_id; 2295 if ((psp->pr_contract = PRCTID(p)) == 0) 2296 psp->pr_contract = -1; 2297 psp->pr_addr = 0; /* cannot represent 64-bit addr in 32 bits */ 2298 switch (p->p_model) { 2299 case DATAMODEL_ILP32: 2300 psp->pr_dmodel = PR_MODEL_ILP32; 2301 break; 2302 case DATAMODEL_LP64: 2303 psp->pr_dmodel = PR_MODEL_LP64; 2304 break; 2305 } 2306 hrutime = mstate_aggr_state(p, LMS_USER); 2307 hrstime = mstate_aggr_state(p, LMS_SYSTEM); 2308 hrt2ts32(hrutime + hrstime, &psp->pr_time); 2309 TICK_TO_TIMESTRUC32(p->p_cutime + p->p_cstime, &psp->pr_ctime); 2310 2311 if (t == NULL) { 2312 extern int wstat(int, int); /* needs a header file */ 2313 int wcode = p->p_wcode; /* must be atomic read */ 2314 2315 if (wcode) 2316 psp->pr_wstat = wstat(wcode, p->p_wdata); 2317 psp->pr_ttydev = PRNODEV32; 2318 psp->pr_lwp.pr_state = SZOMB; 2319 psp->pr_lwp.pr_sname = 'Z'; 2320 } else { 2321 user_t *up = PTOU(p); 2322 struct as *as; 2323 dev_t d; 2324 extern dev_t rwsconsdev, rconsdev, uconsdev; 2325 2326 d = cttydev(p); 2327 /* 2328 * If the controlling terminal is the real 2329 * or workstation console device, map to what the 2330 * user thinks is the console device. 2331 */ 2332 if (d == rwsconsdev || d == rconsdev) 2333 d = uconsdev; 2334 (void) cmpldev(&psp->pr_ttydev, d); 2335 TIMESPEC_TO_TIMESPEC32(&psp->pr_start, &up->u_start); 2336 bcopy(up->u_comm, psp->pr_fname, 2337 MIN(sizeof (up->u_comm), sizeof (psp->pr_fname)-1)); 2338 bcopy(up->u_psargs, psp->pr_psargs, 2339 MIN(PRARGSZ-1, PSARGSZ)); 2340 psp->pr_argc = up->u_argc; 2341 psp->pr_argv = (caddr32_t)up->u_argv; 2342 psp->pr_envp = (caddr32_t)up->u_envp; 2343 2344 /* get the chosen lwp's lwpsinfo */ 2345 prgetlwpsinfo32(t, &psp->pr_lwp); 2346 2347 /* compute %cpu for the process */ 2348 if (p->p_lwpcnt == 1) 2349 psp->pr_pctcpu = psp->pr_lwp.pr_pctcpu; 2350 else { 2351 uint64_t pct = 0; 2352 hrtime_t cur_time; 2353 2354 t = p->p_tlist; 2355 cur_time = gethrtime_unscaled(); 2356 do { 2357 pct += cpu_update_pct(t, cur_time); 2358 } while ((t = t->t_forw) != p->p_tlist); 2359 2360 psp->pr_pctcpu = prgetpctcpu(pct); 2361 } 2362 if ((p->p_flag & SSYS) || (as = p->p_as) == &kas) { 2363 psp->pr_size = 0; 2364 psp->pr_rssize = 0; 2365 } else { 2366 mutex_exit(&p->p_lock); 2367 AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 2368 psp->pr_size = (size32_t) 2369 (btopr(rm_assize(as)) * (PAGESIZE / 1024)); 2370 psp->pr_rssize = (size32_t) 2371 (rm_asrss(as) * (PAGESIZE / 1024)); 2372 psp->pr_pctmem = rm_pctmemory(as); 2373 AS_LOCK_EXIT(as, &as->a_lock); 2374 mutex_enter(&p->p_lock); 2375 } 2376 } 2377 2378 /* 2379 * If we are looking at an LP64 process, zero out 2380 * the fields that cannot be represented in ILP32. 2381 */ 2382 if (p->p_model != DATAMODEL_ILP32) { 2383 psp->pr_size = 0; 2384 psp->pr_rssize = 0; 2385 psp->pr_argv = 0; 2386 psp->pr_envp = 0; 2387 } 2388 } 2389 #endif /* _SYSCALL32_IMPL */ 2390 2391 void 2392 prgetlwpsinfo(kthread_t *t, lwpsinfo_t *psp) 2393 { 2394 klwp_t *lwp = ttolwp(t); 2395 sobj_ops_t *sobj; 2396 char c, state; 2397 uint64_t pct; 2398 int retval, niceval; 2399 hrtime_t hrutime, hrstime; 2400 2401 ASSERT(MUTEX_HELD(&ttoproc(t)->p_lock)); 2402 2403 bzero(psp, sizeof (*psp)); 2404 2405 psp->pr_flag = 0; /* lwpsinfo_t.pr_flag is deprecated */ 2406 psp->pr_lwpid = t->t_tid; 2407 psp->pr_addr = (uintptr_t)t; 2408 psp->pr_wchan = (uintptr_t)t->t_wchan; 2409 2410 /* map the thread state enum into a process state enum */ 2411 state = VSTOPPED(t) ? TS_STOPPED : t->t_state; 2412 switch (state) { 2413 case TS_SLEEP: state = SSLEEP; c = 'S'; break; 2414 case TS_RUN: state = SRUN; c = 'R'; break; 2415 case TS_ONPROC: state = SONPROC; c = 'O'; break; 2416 case TS_ZOMB: state = SZOMB; c = 'Z'; break; 2417 case TS_STOPPED: state = SSTOP; c = 'T'; break; 2418 default: state = 0; c = '?'; break; 2419 } 2420 psp->pr_state = state; 2421 psp->pr_sname = c; 2422 if ((sobj = t->t_sobj_ops) != NULL) 2423 psp->pr_stype = SOBJ_TYPE(sobj); 2424 retval = CL_DONICE(t, NULL, 0, &niceval); 2425 if (retval == 0) { 2426 psp->pr_oldpri = v.v_maxsyspri - t->t_pri; 2427 psp->pr_nice = niceval + NZERO; 2428 } 2429 psp->pr_syscall = t->t_sysnum; 2430 psp->pr_pri = t->t_pri; 2431 psp->pr_start.tv_sec = t->t_start; 2432 psp->pr_start.tv_nsec = 0L; 2433 hrutime = lwp->lwp_mstate.ms_acct[LMS_USER]; 2434 scalehrtime(&hrutime); 2435 hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] + 2436 lwp->lwp_mstate.ms_acct[LMS_TRAP]; 2437 scalehrtime(&hrstime); 2438 hrt2ts(hrutime + hrstime, &psp->pr_time); 2439 /* compute %cpu for the lwp */ 2440 pct = cpu_update_pct(t, gethrtime_unscaled()); 2441 psp->pr_pctcpu = prgetpctcpu(pct); 2442 psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15; /* [0..99] */ 2443 if (psp->pr_cpu > 99) 2444 psp->pr_cpu = 99; 2445 2446 (void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name, 2447 sizeof (psp->pr_clname) - 1); 2448 bzero(psp->pr_name, sizeof (psp->pr_name)); /* XXX ??? */ 2449 psp->pr_onpro = t->t_cpu->cpu_id; 2450 psp->pr_bindpro = t->t_bind_cpu; 2451 psp->pr_bindpset = t->t_bind_pset; 2452 } 2453 2454 #ifdef _SYSCALL32_IMPL 2455 void 2456 prgetlwpsinfo32(kthread_t *t, lwpsinfo32_t *psp) 2457 { 2458 proc_t *p = ttoproc(t); 2459 klwp_t *lwp = ttolwp(t); 2460 sobj_ops_t *sobj; 2461 char c, state; 2462 uint64_t pct; 2463 int retval, niceval; 2464 hrtime_t hrutime, hrstime; 2465 2466 ASSERT(MUTEX_HELD(&p->p_lock)); 2467 2468 bzero(psp, sizeof (*psp)); 2469 2470 psp->pr_flag = 0; /* lwpsinfo_t.pr_flag is deprecated */ 2471 psp->pr_lwpid = t->t_tid; 2472 psp->pr_addr = 0; /* cannot represent 64-bit addr in 32 bits */ 2473 psp->pr_wchan = 0; /* cannot represent 64-bit addr in 32 bits */ 2474 2475 /* map the thread state enum into a process state enum */ 2476 state = VSTOPPED(t) ? TS_STOPPED : t->t_state; 2477 switch (state) { 2478 case TS_SLEEP: state = SSLEEP; c = 'S'; break; 2479 case TS_RUN: state = SRUN; c = 'R'; break; 2480 case TS_ONPROC: state = SONPROC; c = 'O'; break; 2481 case TS_ZOMB: state = SZOMB; c = 'Z'; break; 2482 case TS_STOPPED: state = SSTOP; c = 'T'; break; 2483 default: state = 0; c = '?'; break; 2484 } 2485 psp->pr_state = state; 2486 psp->pr_sname = c; 2487 if ((sobj = t->t_sobj_ops) != NULL) 2488 psp->pr_stype = SOBJ_TYPE(sobj); 2489 retval = CL_DONICE(t, NULL, 0, &niceval); 2490 if (retval == 0) { 2491 psp->pr_oldpri = v.v_maxsyspri - t->t_pri; 2492 psp->pr_nice = niceval + NZERO; 2493 } else { 2494 psp->pr_oldpri = 0; 2495 psp->pr_nice = 0; 2496 } 2497 psp->pr_syscall = t->t_sysnum; 2498 psp->pr_pri = t->t_pri; 2499 psp->pr_start.tv_sec = (time32_t)t->t_start; 2500 psp->pr_start.tv_nsec = 0L; 2501 hrutime = lwp->lwp_mstate.ms_acct[LMS_USER]; 2502 scalehrtime(&hrutime); 2503 hrstime = lwp->lwp_mstate.ms_acct[LMS_SYSTEM] + 2504 lwp->lwp_mstate.ms_acct[LMS_TRAP]; 2505 scalehrtime(&hrstime); 2506 hrt2ts32(hrutime + hrstime, &psp->pr_time); 2507 /* compute %cpu for the lwp */ 2508 pct = cpu_update_pct(t, gethrtime_unscaled()); 2509 psp->pr_pctcpu = prgetpctcpu(pct); 2510 psp->pr_cpu = (psp->pr_pctcpu*100 + 0x6000) >> 15; /* [0..99] */ 2511 if (psp->pr_cpu > 99) 2512 psp->pr_cpu = 99; 2513 2514 (void) strncpy(psp->pr_clname, sclass[t->t_cid].cl_name, 2515 sizeof (psp->pr_clname) - 1); 2516 bzero(psp->pr_name, sizeof (psp->pr_name)); /* XXX ??? */ 2517 psp->pr_onpro = t->t_cpu->cpu_id; 2518 psp->pr_bindpro = t->t_bind_cpu; 2519 psp->pr_bindpset = t->t_bind_pset; 2520 } 2521 #endif /* _SYSCALL32_IMPL */ 2522 2523 /* 2524 * This used to get called when microstate accounting was disabled but 2525 * microstate information was requested. Since Microstate accounting is on 2526 * regardless of the proc flags, this simply makes it appear to procfs that 2527 * microstate accounting is on. This is relatively meaningless since you 2528 * can't turn it off, but this is here for the sake of appearances. 2529 */ 2530 2531 /*ARGSUSED*/ 2532 void 2533 estimate_msacct(kthread_t *t, hrtime_t curtime) 2534 { 2535 proc_t *p; 2536 2537 if (t == NULL) 2538 return; 2539 2540 p = ttoproc(t); 2541 ASSERT(MUTEX_HELD(&p->p_lock)); 2542 2543 /* 2544 * A system process (p0) could be referenced if the thread is 2545 * in the process of exiting. Don't turn on microstate accounting 2546 * in that case. 2547 */ 2548 if (p->p_flag & SSYS) 2549 return; 2550 2551 /* 2552 * Loop through all the LWPs (kernel threads) in the process. 2553 */ 2554 t = p->p_tlist; 2555 do { 2556 t->t_proc_flag |= TP_MSACCT; 2557 } while ((t = t->t_forw) != p->p_tlist); 2558 2559 p->p_flag |= SMSACCT; /* set process-wide MSACCT */ 2560 } 2561 2562 /* 2563 * It's not really possible to disable microstate accounting anymore. 2564 * However, this routine simply turns off the ms accounting flags in a process 2565 * This way procfs can still pretend to turn microstate accounting on and 2566 * off for a process, but it actually doesn't do anything. This is 2567 * a neutered form of preemptive idiot-proofing. 2568 */ 2569 void 2570 disable_msacct(proc_t *p) 2571 { 2572 kthread_t *t; 2573 2574 ASSERT(MUTEX_HELD(&p->p_lock)); 2575 2576 p->p_flag &= ~SMSACCT; /* clear process-wide MSACCT */ 2577 /* 2578 * Loop through all the LWPs (kernel threads) in the process. 2579 */ 2580 if ((t = p->p_tlist) != NULL) { 2581 do { 2582 /* clear per-thread flag */ 2583 t->t_proc_flag &= ~TP_MSACCT; 2584 } while ((t = t->t_forw) != p->p_tlist); 2585 } 2586 } 2587 2588 /* 2589 * Return resource usage information. 2590 */ 2591 void 2592 prgetusage(kthread_t *t, prhusage_t *pup) 2593 { 2594 klwp_t *lwp = ttolwp(t); 2595 hrtime_t *mstimep; 2596 struct mstate *ms = &lwp->lwp_mstate; 2597 int state; 2598 int i; 2599 hrtime_t curtime; 2600 hrtime_t waitrq; 2601 hrtime_t tmp1; 2602 2603 curtime = gethrtime_unscaled(); 2604 2605 pup->pr_lwpid = t->t_tid; 2606 pup->pr_count = 1; 2607 pup->pr_create = ms->ms_start; 2608 pup->pr_term = ms->ms_term; 2609 scalehrtime(&pup->pr_create); 2610 scalehrtime(&pup->pr_term); 2611 if (ms->ms_term == 0) { 2612 pup->pr_rtime = curtime - ms->ms_start; 2613 scalehrtime(&pup->pr_rtime); 2614 } else { 2615 pup->pr_rtime = ms->ms_term - ms->ms_start; 2616 scalehrtime(&pup->pr_rtime); 2617 } 2618 2619 2620 pup->pr_utime = ms->ms_acct[LMS_USER]; 2621 pup->pr_stime = ms->ms_acct[LMS_SYSTEM]; 2622 pup->pr_ttime = ms->ms_acct[LMS_TRAP]; 2623 pup->pr_tftime = ms->ms_acct[LMS_TFAULT]; 2624 pup->pr_dftime = ms->ms_acct[LMS_DFAULT]; 2625 pup->pr_kftime = ms->ms_acct[LMS_KFAULT]; 2626 pup->pr_ltime = ms->ms_acct[LMS_USER_LOCK]; 2627 pup->pr_slptime = ms->ms_acct[LMS_SLEEP]; 2628 pup->pr_wtime = ms->ms_acct[LMS_WAIT_CPU]; 2629 pup->pr_stoptime = ms->ms_acct[LMS_STOPPED]; 2630 2631 prscaleusage(pup); 2632 2633 /* 2634 * Adjust for time waiting in the dispatcher queue. 2635 */ 2636 waitrq = t->t_waitrq; /* hopefully atomic */ 2637 if (waitrq != 0) { 2638 tmp1 = curtime - waitrq; 2639 scalehrtime(&tmp1); 2640 pup->pr_wtime += tmp1; 2641 curtime = waitrq; 2642 } 2643 2644 /* 2645 * Adjust for time spent in current microstate. 2646 */ 2647 if (ms->ms_state_start > curtime) { 2648 curtime = gethrtime_unscaled(); 2649 } 2650 2651 i = 0; 2652 do { 2653 switch (state = t->t_mstate) { 2654 case LMS_SLEEP: 2655 /* 2656 * Update the timer for the current sleep state. 2657 */ 2658 switch (state = ms->ms_prev) { 2659 case LMS_TFAULT: 2660 case LMS_DFAULT: 2661 case LMS_KFAULT: 2662 case LMS_USER_LOCK: 2663 break; 2664 default: 2665 state = LMS_SLEEP; 2666 break; 2667 } 2668 break; 2669 case LMS_TFAULT: 2670 case LMS_DFAULT: 2671 case LMS_KFAULT: 2672 case LMS_USER_LOCK: 2673 state = LMS_SYSTEM; 2674 break; 2675 } 2676 switch (state) { 2677 case LMS_USER: mstimep = &pup->pr_utime; break; 2678 case LMS_SYSTEM: mstimep = &pup->pr_stime; break; 2679 case LMS_TRAP: mstimep = &pup->pr_ttime; break; 2680 case LMS_TFAULT: mstimep = &pup->pr_tftime; break; 2681 case LMS_DFAULT: mstimep = &pup->pr_dftime; break; 2682 case LMS_KFAULT: mstimep = &pup->pr_kftime; break; 2683 case LMS_USER_LOCK: mstimep = &pup->pr_ltime; break; 2684 case LMS_SLEEP: mstimep = &pup->pr_slptime; break; 2685 case LMS_WAIT_CPU: mstimep = &pup->pr_wtime; break; 2686 case LMS_STOPPED: mstimep = &pup->pr_stoptime; break; 2687 default: panic("prgetusage: unknown microstate"); 2688 } 2689 tmp1 = curtime - ms->ms_state_start; 2690 if (tmp1 < 0) { 2691 curtime = gethrtime_unscaled(); 2692 i++; 2693 continue; 2694 } 2695 scalehrtime(&tmp1); 2696 } while (tmp1 < 0 && i < MAX_ITERS_SPIN); 2697 2698 *mstimep += tmp1; 2699 2700 /* update pup timestamp */ 2701 pup->pr_tstamp = curtime; 2702 scalehrtime(&pup->pr_tstamp); 2703 2704 /* 2705 * Resource usage counters. 2706 */ 2707 pup->pr_minf = lwp->lwp_ru.minflt; 2708 pup->pr_majf = lwp->lwp_ru.majflt; 2709 pup->pr_nswap = lwp->lwp_ru.nswap; 2710 pup->pr_inblk = lwp->lwp_ru.inblock; 2711 pup->pr_oublk = lwp->lwp_ru.oublock; 2712 pup->pr_msnd = lwp->lwp_ru.msgsnd; 2713 pup->pr_mrcv = lwp->lwp_ru.msgrcv; 2714 pup->pr_sigs = lwp->lwp_ru.nsignals; 2715 pup->pr_vctx = lwp->lwp_ru.nvcsw; 2716 pup->pr_ictx = lwp->lwp_ru.nivcsw; 2717 pup->pr_sysc = lwp->lwp_ru.sysc; 2718 pup->pr_ioch = lwp->lwp_ru.ioch; 2719 } 2720 2721 /* 2722 * Convert ms_acct stats from unscaled high-res time to nanoseconds 2723 */ 2724 void 2725 prscaleusage(prhusage_t *usg) 2726 { 2727 scalehrtime(&usg->pr_utime); 2728 scalehrtime(&usg->pr_stime); 2729 scalehrtime(&usg->pr_ttime); 2730 scalehrtime(&usg->pr_tftime); 2731 scalehrtime(&usg->pr_dftime); 2732 scalehrtime(&usg->pr_kftime); 2733 scalehrtime(&usg->pr_ltime); 2734 scalehrtime(&usg->pr_slptime); 2735 scalehrtime(&usg->pr_wtime); 2736 scalehrtime(&usg->pr_stoptime); 2737 } 2738 2739 2740 /* 2741 * Sum resource usage information. 2742 */ 2743 void 2744 praddusage(kthread_t *t, prhusage_t *pup) 2745 { 2746 klwp_t *lwp = ttolwp(t); 2747 hrtime_t *mstimep; 2748 struct mstate *ms = &lwp->lwp_mstate; 2749 int state; 2750 int i; 2751 hrtime_t curtime; 2752 hrtime_t waitrq; 2753 hrtime_t tmp; 2754 prhusage_t conv; 2755 2756 curtime = gethrtime_unscaled(); 2757 2758 if (ms->ms_term == 0) { 2759 tmp = curtime - ms->ms_start; 2760 scalehrtime(&tmp); 2761 pup->pr_rtime += tmp; 2762 } else { 2763 tmp = ms->ms_term - ms->ms_start; 2764 scalehrtime(&tmp); 2765 pup->pr_rtime += tmp; 2766 } 2767 2768 conv.pr_utime = ms->ms_acct[LMS_USER]; 2769 conv.pr_stime = ms->ms_acct[LMS_SYSTEM]; 2770 conv.pr_ttime = ms->ms_acct[LMS_TRAP]; 2771 conv.pr_tftime = ms->ms_acct[LMS_TFAULT]; 2772 conv.pr_dftime = ms->ms_acct[LMS_DFAULT]; 2773 conv.pr_kftime = ms->ms_acct[LMS_KFAULT]; 2774 conv.pr_ltime = ms->ms_acct[LMS_USER_LOCK]; 2775 conv.pr_slptime = ms->ms_acct[LMS_SLEEP]; 2776 conv.pr_wtime = ms->ms_acct[LMS_WAIT_CPU]; 2777 conv.pr_stoptime = ms->ms_acct[LMS_STOPPED]; 2778 2779 prscaleusage(&conv); 2780 2781 pup->pr_utime += conv.pr_utime; 2782 pup->pr_stime += conv.pr_stime; 2783 pup->pr_ttime += conv.pr_ttime; 2784 pup->pr_tftime += conv.pr_tftime; 2785 pup->pr_dftime += conv.pr_dftime; 2786 pup->pr_kftime += conv.pr_kftime; 2787 pup->pr_ltime += conv.pr_ltime; 2788 pup->pr_slptime += conv.pr_slptime; 2789 pup->pr_wtime += conv.pr_wtime; 2790 pup->pr_stoptime += conv.pr_stoptime; 2791 2792 /* 2793 * Adjust for time waiting in the dispatcher queue. 2794 */ 2795 waitrq = t->t_waitrq; /* hopefully atomic */ 2796 if (waitrq != 0) { 2797 tmp = curtime - waitrq; 2798 scalehrtime(&tmp); 2799 pup->pr_wtime += tmp; 2800 curtime = waitrq; 2801 } 2802 2803 /* 2804 * Adjust for time spent in current microstate. 2805 */ 2806 if (ms->ms_state_start > curtime) { 2807 curtime = gethrtime_unscaled(); 2808 } 2809 2810 i = 0; 2811 do { 2812 switch (state = t->t_mstate) { 2813 case LMS_SLEEP: 2814 /* 2815 * Update the timer for the current sleep state. 2816 */ 2817 switch (state = ms->ms_prev) { 2818 case LMS_TFAULT: 2819 case LMS_DFAULT: 2820 case LMS_KFAULT: 2821 case LMS_USER_LOCK: 2822 break; 2823 default: 2824 state = LMS_SLEEP; 2825 break; 2826 } 2827 break; 2828 case LMS_TFAULT: 2829 case LMS_DFAULT: 2830 case LMS_KFAULT: 2831 case LMS_USER_LOCK: 2832 state = LMS_SYSTEM; 2833 break; 2834 } 2835 switch (state) { 2836 case LMS_USER: mstimep = &pup->pr_utime; break; 2837 case LMS_SYSTEM: mstimep = &pup->pr_stime; break; 2838 case LMS_TRAP: mstimep = &pup->pr_ttime; break; 2839 case LMS_TFAULT: mstimep = &pup->pr_tftime; break; 2840 case LMS_DFAULT: mstimep = &pup->pr_dftime; break; 2841 case LMS_KFAULT: mstimep = &pup->pr_kftime; break; 2842 case LMS_USER_LOCK: mstimep = &pup->pr_ltime; break; 2843 case LMS_SLEEP: mstimep = &pup->pr_slptime; break; 2844 case LMS_WAIT_CPU: mstimep = &pup->pr_wtime; break; 2845 case LMS_STOPPED: mstimep = &pup->pr_stoptime; break; 2846 default: panic("praddusage: unknown microstate"); 2847 } 2848 tmp = curtime - ms->ms_state_start; 2849 if (tmp < 0) { 2850 curtime = gethrtime_unscaled(); 2851 i++; 2852 continue; 2853 } 2854 scalehrtime(&tmp); 2855 } while (tmp < 0 && i < MAX_ITERS_SPIN); 2856 2857 *mstimep += tmp; 2858 2859 /* update pup timestamp */ 2860 pup->pr_tstamp = curtime; 2861 scalehrtime(&pup->pr_tstamp); 2862 2863 /* 2864 * Resource usage counters. 2865 */ 2866 pup->pr_minf += lwp->lwp_ru.minflt; 2867 pup->pr_majf += lwp->lwp_ru.majflt; 2868 pup->pr_nswap += lwp->lwp_ru.nswap; 2869 pup->pr_inblk += lwp->lwp_ru.inblock; 2870 pup->pr_oublk += lwp->lwp_ru.oublock; 2871 pup->pr_msnd += lwp->lwp_ru.msgsnd; 2872 pup->pr_mrcv += lwp->lwp_ru.msgrcv; 2873 pup->pr_sigs += lwp->lwp_ru.nsignals; 2874 pup->pr_vctx += lwp->lwp_ru.nvcsw; 2875 pup->pr_ictx += lwp->lwp_ru.nivcsw; 2876 pup->pr_sysc += lwp->lwp_ru.sysc; 2877 pup->pr_ioch += lwp->lwp_ru.ioch; 2878 } 2879 2880 /* 2881 * Convert a prhusage_t to a prusage_t. 2882 * This means convert each hrtime_t to a timestruc_t 2883 * and copy the count fields uint64_t => ulong_t. 2884 */ 2885 void 2886 prcvtusage(prhusage_t *pup, prusage_t *upup) 2887 { 2888 uint64_t *ullp; 2889 ulong_t *ulp; 2890 int i; 2891 2892 upup->pr_lwpid = pup->pr_lwpid; 2893 upup->pr_count = pup->pr_count; 2894 2895 hrt2ts(pup->pr_tstamp, &upup->pr_tstamp); 2896 hrt2ts(pup->pr_create, &upup->pr_create); 2897 hrt2ts(pup->pr_term, &upup->pr_term); 2898 hrt2ts(pup->pr_rtime, &upup->pr_rtime); 2899 hrt2ts(pup->pr_utime, &upup->pr_utime); 2900 hrt2ts(pup->pr_stime, &upup->pr_stime); 2901 hrt2ts(pup->pr_ttime, &upup->pr_ttime); 2902 hrt2ts(pup->pr_tftime, &upup->pr_tftime); 2903 hrt2ts(pup->pr_dftime, &upup->pr_dftime); 2904 hrt2ts(pup->pr_kftime, &upup->pr_kftime); 2905 hrt2ts(pup->pr_ltime, &upup->pr_ltime); 2906 hrt2ts(pup->pr_slptime, &upup->pr_slptime); 2907 hrt2ts(pup->pr_wtime, &upup->pr_wtime); 2908 hrt2ts(pup->pr_stoptime, &upup->pr_stoptime); 2909 bzero(upup->filltime, sizeof (upup->filltime)); 2910 2911 ullp = &pup->pr_minf; 2912 ulp = &upup->pr_minf; 2913 for (i = 0; i < 22; i++) 2914 *ulp++ = (ulong_t)*ullp++; 2915 } 2916 2917 #ifdef _SYSCALL32_IMPL 2918 void 2919 prcvtusage32(prhusage_t *pup, prusage32_t *upup) 2920 { 2921 uint64_t *ullp; 2922 uint32_t *ulp; 2923 int i; 2924 2925 upup->pr_lwpid = pup->pr_lwpid; 2926 upup->pr_count = pup->pr_count; 2927 2928 hrt2ts32(pup->pr_tstamp, &upup->pr_tstamp); 2929 hrt2ts32(pup->pr_create, &upup->pr_create); 2930 hrt2ts32(pup->pr_term, &upup->pr_term); 2931 hrt2ts32(pup->pr_rtime, &upup->pr_rtime); 2932 hrt2ts32(pup->pr_utime, &upup->pr_utime); 2933 hrt2ts32(pup->pr_stime, &upup->pr_stime); 2934 hrt2ts32(pup->pr_ttime, &upup->pr_ttime); 2935 hrt2ts32(pup->pr_tftime, &upup->pr_tftime); 2936 hrt2ts32(pup->pr_dftime, &upup->pr_dftime); 2937 hrt2ts32(pup->pr_kftime, &upup->pr_kftime); 2938 hrt2ts32(pup->pr_ltime, &upup->pr_ltime); 2939 hrt2ts32(pup->pr_slptime, &upup->pr_slptime); 2940 hrt2ts32(pup->pr_wtime, &upup->pr_wtime); 2941 hrt2ts32(pup->pr_stoptime, &upup->pr_stoptime); 2942 bzero(upup->filltime, sizeof (upup->filltime)); 2943 2944 ullp = &pup->pr_minf; 2945 ulp = &upup->pr_minf; 2946 for (i = 0; i < 22; i++) 2947 *ulp++ = (uint32_t)*ullp++; 2948 } 2949 #endif /* _SYSCALL32_IMPL */ 2950 2951 /* 2952 * Determine whether a set is empty. 2953 */ 2954 int 2955 setisempty(uint32_t *sp, uint_t n) 2956 { 2957 while (n--) 2958 if (*sp++) 2959 return (0); 2960 return (1); 2961 } 2962 2963 /* 2964 * Utility routine for establishing a watched area in the process. 2965 * Keep the list of watched areas sorted by virtual address. 2966 */ 2967 int 2968 set_watched_area(proc_t *p, struct watched_area *pwa) 2969 { 2970 caddr_t vaddr = pwa->wa_vaddr; 2971 caddr_t eaddr = pwa->wa_eaddr; 2972 ulong_t flags = pwa->wa_flags; 2973 struct watched_area *target; 2974 avl_index_t where; 2975 int error = 0; 2976 2977 /* we must not be holding p->p_lock, but the process must be locked */ 2978 ASSERT(MUTEX_NOT_HELD(&p->p_lock)); 2979 ASSERT(p->p_proc_flag & P_PR_LOCK); 2980 2981 /* 2982 * If this is our first watchpoint, enable watchpoints for the process. 2983 */ 2984 if (!pr_watch_active(p)) { 2985 kthread_t *t; 2986 2987 mutex_enter(&p->p_lock); 2988 if ((t = p->p_tlist) != NULL) { 2989 do { 2990 watch_enable(t); 2991 } while ((t = t->t_forw) != p->p_tlist); 2992 } 2993 mutex_exit(&p->p_lock); 2994 } 2995 2996 target = pr_find_watched_area(p, pwa, &where); 2997 if (target != NULL) { 2998 /* 2999 * We discovered an existing, overlapping watched area. 3000 * Allow it only if it is an exact match. 3001 */ 3002 if (target->wa_vaddr != vaddr || 3003 target->wa_eaddr != eaddr) 3004 error = EINVAL; 3005 else if (target->wa_flags != flags) { 3006 error = set_watched_page(p, vaddr, eaddr, 3007 flags, target->wa_flags); 3008 target->wa_flags = flags; 3009 } 3010 kmem_free(pwa, sizeof (struct watched_area)); 3011 } else { 3012 avl_insert(&p->p_warea, pwa, where); 3013 error = set_watched_page(p, vaddr, eaddr, flags, 0); 3014 } 3015 3016 return (error); 3017 } 3018 3019 /* 3020 * Utility routine for clearing a watched area in the process. 3021 * Must be an exact match of the virtual address. 3022 * size and flags don't matter. 3023 */ 3024 int 3025 clear_watched_area(proc_t *p, struct watched_area *pwa) 3026 { 3027 struct watched_area *found; 3028 3029 /* we must not be holding p->p_lock, but the process must be locked */ 3030 ASSERT(MUTEX_NOT_HELD(&p->p_lock)); 3031 ASSERT(p->p_proc_flag & P_PR_LOCK); 3032 3033 3034 if (!pr_watch_active(p)) { 3035 kmem_free(pwa, sizeof (struct watched_area)); 3036 return (0); 3037 } 3038 3039 /* 3040 * Look for a matching address in the watched areas. If a match is 3041 * found, clear the old watched area and adjust the watched page(s). It 3042 * is not an error if there is no match. 3043 */ 3044 if ((found = pr_find_watched_area(p, pwa, NULL)) != NULL && 3045 found->wa_vaddr == pwa->wa_vaddr) { 3046 clear_watched_page(p, found->wa_vaddr, found->wa_eaddr, 3047 found->wa_flags); 3048 avl_remove(&p->p_warea, found); 3049 kmem_free(found, sizeof (struct watched_area)); 3050 } 3051 3052 kmem_free(pwa, sizeof (struct watched_area)); 3053 3054 /* 3055 * If we removed the last watched area from the process, disable 3056 * watchpoints. 3057 */ 3058 if (!pr_watch_active(p)) { 3059 kthread_t *t; 3060 3061 mutex_enter(&p->p_lock); 3062 if ((t = p->p_tlist) != NULL) { 3063 do { 3064 watch_disable(t); 3065 } while ((t = t->t_forw) != p->p_tlist); 3066 } 3067 mutex_exit(&p->p_lock); 3068 } 3069 3070 return (0); 3071 } 3072 3073 /* 3074 * Frees all the watched_area structures 3075 */ 3076 void 3077 pr_free_watchpoints(proc_t *p) 3078 { 3079 struct watched_area *delp; 3080 void *cookie; 3081 3082 cookie = NULL; 3083 while ((delp = avl_destroy_nodes(&p->p_warea, &cookie)) != NULL) 3084 kmem_free(delp, sizeof (struct watched_area)); 3085 3086 avl_destroy(&p->p_warea); 3087 } 3088 3089 /* 3090 * This one is called by the traced process to unwatch all the 3091 * pages while deallocating the list of watched_page structs. 3092 */ 3093 void 3094 pr_free_watched_pages(proc_t *p) 3095 { 3096 struct as *as = p->p_as; 3097 struct watched_page *pwp; 3098 uint_t prot; 3099 int retrycnt, err; 3100 void *cookie; 3101 3102 if (as == NULL || avl_numnodes(&as->a_wpage) == 0) 3103 return; 3104 3105 ASSERT(MUTEX_NOT_HELD(&curproc->p_lock)); 3106 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 3107 3108 pwp = avl_first(&as->a_wpage); 3109 3110 cookie = NULL; 3111 while ((pwp = avl_destroy_nodes(&as->a_wpage, &cookie)) != NULL) { 3112 retrycnt = 0; 3113 if ((prot = pwp->wp_oprot) != 0) { 3114 caddr_t addr = pwp->wp_vaddr; 3115 struct seg *seg; 3116 retry: 3117 3118 if ((pwp->wp_prot != prot || 3119 (pwp->wp_flags & WP_NOWATCH)) && 3120 (seg = as_segat(as, addr)) != NULL) { 3121 err = SEGOP_SETPROT(seg, addr, PAGESIZE, prot); 3122 if (err == IE_RETRY) { 3123 ASSERT(retrycnt == 0); 3124 retrycnt++; 3125 goto retry; 3126 } 3127 } 3128 } 3129 kmem_free(pwp, sizeof (struct watched_page)); 3130 } 3131 3132 avl_destroy(&as->a_wpage); 3133 p->p_wprot = NULL; 3134 3135 AS_LOCK_EXIT(as, &as->a_lock); 3136 } 3137 3138 /* 3139 * Insert a watched area into the list of watched pages. 3140 * If oflags is zero then we are adding a new watched area. 3141 * Otherwise we are changing the flags of an existing watched area. 3142 */ 3143 static int 3144 set_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr, 3145 ulong_t flags, ulong_t oflags) 3146 { 3147 struct as *as = p->p_as; 3148 avl_tree_t *pwp_tree; 3149 struct watched_page *pwp, *newpwp; 3150 struct watched_page tpw; 3151 avl_index_t where; 3152 struct seg *seg; 3153 uint_t prot; 3154 caddr_t addr; 3155 3156 /* 3157 * We need to pre-allocate a list of structures before we grab the 3158 * address space lock to avoid calling kmem_alloc(KM_SLEEP) with locks 3159 * held. 3160 */ 3161 newpwp = NULL; 3162 for (addr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 3163 addr < eaddr; addr += PAGESIZE) { 3164 pwp = kmem_zalloc(sizeof (struct watched_page), KM_SLEEP); 3165 pwp->wp_list = newpwp; 3166 newpwp = pwp; 3167 } 3168 3169 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 3170 3171 /* 3172 * Search for an existing watched page to contain the watched area. 3173 * If none is found, grab a new one from the available list 3174 * and insert it in the active list, keeping the list sorted 3175 * by user-level virtual address. 3176 */ 3177 if (p->p_flag & SVFWAIT) 3178 pwp_tree = &p->p_wpage; 3179 else 3180 pwp_tree = &as->a_wpage; 3181 3182 again: 3183 if (avl_numnodes(pwp_tree) > prnwatch) { 3184 AS_LOCK_EXIT(as, &as->a_lock); 3185 while (newpwp != NULL) { 3186 pwp = newpwp->wp_list; 3187 kmem_free(newpwp, sizeof (struct watched_page)); 3188 newpwp = pwp; 3189 } 3190 return (E2BIG); 3191 } 3192 3193 tpw.wp_vaddr = (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 3194 if ((pwp = avl_find(pwp_tree, &tpw, &where)) == NULL) { 3195 pwp = newpwp; 3196 newpwp = newpwp->wp_list; 3197 pwp->wp_list = NULL; 3198 pwp->wp_vaddr = (caddr_t)((uintptr_t)vaddr & 3199 (uintptr_t)PAGEMASK); 3200 avl_insert(pwp_tree, pwp, where); 3201 } 3202 3203 ASSERT(vaddr >= pwp->wp_vaddr && vaddr < pwp->wp_vaddr + PAGESIZE); 3204 3205 if (oflags & WA_READ) 3206 pwp->wp_read--; 3207 if (oflags & WA_WRITE) 3208 pwp->wp_write--; 3209 if (oflags & WA_EXEC) 3210 pwp->wp_exec--; 3211 3212 ASSERT(pwp->wp_read >= 0); 3213 ASSERT(pwp->wp_write >= 0); 3214 ASSERT(pwp->wp_exec >= 0); 3215 3216 if (flags & WA_READ) 3217 pwp->wp_read++; 3218 if (flags & WA_WRITE) 3219 pwp->wp_write++; 3220 if (flags & WA_EXEC) 3221 pwp->wp_exec++; 3222 3223 if (!(p->p_flag & SVFWAIT)) { 3224 vaddr = pwp->wp_vaddr; 3225 if (pwp->wp_oprot == 0 && 3226 (seg = as_segat(as, vaddr)) != NULL) { 3227 SEGOP_GETPROT(seg, vaddr, 0, &prot); 3228 pwp->wp_oprot = (uchar_t)prot; 3229 pwp->wp_prot = (uchar_t)prot; 3230 } 3231 if (pwp->wp_oprot != 0) { 3232 prot = pwp->wp_oprot; 3233 if (pwp->wp_read) 3234 prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 3235 if (pwp->wp_write) 3236 prot &= ~PROT_WRITE; 3237 if (pwp->wp_exec) 3238 prot &= ~(PROT_READ|PROT_WRITE|PROT_EXEC); 3239 if (!(pwp->wp_flags & WP_NOWATCH) && 3240 pwp->wp_prot != prot && 3241 (pwp->wp_flags & WP_SETPROT) == 0) { 3242 pwp->wp_flags |= WP_SETPROT; 3243 pwp->wp_list = p->p_wprot; 3244 p->p_wprot = pwp; 3245 } 3246 pwp->wp_prot = (uchar_t)prot; 3247 } 3248 } 3249 3250 /* 3251 * If the watched area extends into the next page then do 3252 * it over again with the virtual address of the next page. 3253 */ 3254 if ((vaddr = pwp->wp_vaddr + PAGESIZE) < eaddr) 3255 goto again; 3256 3257 AS_LOCK_EXIT(as, &as->a_lock); 3258 3259 /* 3260 * Free any pages we may have over-allocated 3261 */ 3262 while (newpwp != NULL) { 3263 pwp = newpwp->wp_list; 3264 kmem_free(newpwp, sizeof (struct watched_page)); 3265 newpwp = pwp; 3266 } 3267 3268 return (0); 3269 } 3270 3271 /* 3272 * Remove a watched area from the list of watched pages. 3273 * A watched area may extend over more than one page. 3274 */ 3275 static void 3276 clear_watched_page(proc_t *p, caddr_t vaddr, caddr_t eaddr, ulong_t flags) 3277 { 3278 struct as *as = p->p_as; 3279 struct watched_page *pwp; 3280 struct watched_page tpw; 3281 avl_tree_t *tree; 3282 avl_index_t where; 3283 3284 AS_LOCK_ENTER(as, &as->a_lock, RW_WRITER); 3285 3286 if (p->p_flag & SVFWAIT) 3287 tree = &p->p_wpage; 3288 else 3289 tree = &as->a_wpage; 3290 3291 tpw.wp_vaddr = vaddr = 3292 (caddr_t)((uintptr_t)vaddr & (uintptr_t)PAGEMASK); 3293 pwp = avl_find(tree, &tpw, &where); 3294 if (pwp == NULL) 3295 pwp = avl_nearest(tree, where, AVL_AFTER); 3296 3297 while (pwp != NULL && pwp->wp_vaddr < eaddr) { 3298 ASSERT(vaddr <= pwp->wp_vaddr); 3299 3300 if (flags & WA_READ) 3301 pwp->wp_read--; 3302 if (flags & WA_WRITE) 3303 pwp->wp_write--; 3304 if (flags & WA_EXEC) 3305 pwp->wp_exec--; 3306 3307 if (pwp->wp_read + pwp->wp_write + pwp->wp_exec != 0) { 3308 /* 3309 * Reset the hat layer's protections on this page. 3310 */ 3311 if (pwp->wp_oprot != 0) { 3312 uint_t prot = pwp->wp_oprot; 3313 3314 if (pwp->wp_read) 3315 prot &= 3316 ~(PROT_READ|PROT_WRITE|PROT_EXEC); 3317 if (pwp->wp_write) 3318 prot &= ~PROT_WRITE; 3319 if (pwp->wp_exec) 3320 prot &= 3321 ~(PROT_READ|PROT_WRITE|PROT_EXEC); 3322 if (!(pwp->wp_flags & WP_NOWATCH) && 3323 pwp->wp_prot != prot && 3324 (pwp->wp_flags & WP_SETPROT) == 0) { 3325 pwp->wp_flags |= WP_SETPROT; 3326 pwp->wp_list = p->p_wprot; 3327 p->p_wprot = pwp; 3328 } 3329 pwp->wp_prot = (uchar_t)prot; 3330 } 3331 } else { 3332 /* 3333 * No watched areas remain in this page. 3334 * Reset everything to normal. 3335 */ 3336 if (pwp->wp_oprot != 0) { 3337 pwp->wp_prot = pwp->wp_oprot; 3338 if ((pwp->wp_flags & WP_SETPROT) == 0) { 3339 pwp->wp_flags |= WP_SETPROT; 3340 pwp->wp_list = p->p_wprot; 3341 p->p_wprot = pwp; 3342 } 3343 } 3344 } 3345 3346 pwp = AVL_NEXT(tree, pwp); 3347 } 3348 3349 AS_LOCK_EXIT(as, &as->a_lock); 3350 } 3351 3352 /* 3353 * Return the original protections for the specified page. 3354 */ 3355 static void 3356 getwatchprot(struct as *as, caddr_t addr, uint_t *prot) 3357 { 3358 struct watched_page *pwp; 3359 struct watched_page tpw; 3360 3361 ASSERT(AS_LOCK_HELD(as, &as->a_lock)); 3362 3363 tpw.wp_vaddr = (caddr_t)((uintptr_t)addr & (uintptr_t)PAGEMASK); 3364 if ((pwp = avl_find(&as->a_wpage, &tpw, NULL)) != NULL) 3365 *prot = pwp->wp_oprot; 3366 } 3367 3368 static prpagev_t * 3369 pr_pagev_create(struct seg *seg, int check_noreserve) 3370 { 3371 prpagev_t *pagev = kmem_alloc(sizeof (prpagev_t), KM_SLEEP); 3372 size_t total_pages = seg_pages(seg); 3373 3374 /* 3375 * Limit the size of our vectors to pagev_lim pages at a time. We need 3376 * 4 or 5 bytes of storage per page, so this means we limit ourself 3377 * to about a megabyte of kernel heap by default. 3378 */ 3379 pagev->pg_npages = MIN(total_pages, pagev_lim); 3380 pagev->pg_pnbase = 0; 3381 3382 pagev->pg_protv = 3383 kmem_alloc(pagev->pg_npages * sizeof (uint_t), KM_SLEEP); 3384 3385 if (check_noreserve) 3386 pagev->pg_incore = 3387 kmem_alloc(pagev->pg_npages * sizeof (char), KM_SLEEP); 3388 else 3389 pagev->pg_incore = NULL; 3390 3391 return (pagev); 3392 } 3393 3394 static void 3395 pr_pagev_destroy(prpagev_t *pagev) 3396 { 3397 if (pagev->pg_incore != NULL) 3398 kmem_free(pagev->pg_incore, pagev->pg_npages * sizeof (char)); 3399 3400 kmem_free(pagev->pg_protv, pagev->pg_npages * sizeof (uint_t)); 3401 kmem_free(pagev, sizeof (prpagev_t)); 3402 } 3403 3404 static caddr_t 3405 pr_pagev_fill(prpagev_t *pagev, struct seg *seg, caddr_t addr, caddr_t eaddr) 3406 { 3407 ulong_t lastpg = seg_page(seg, eaddr - 1); 3408 ulong_t pn, pnlim; 3409 caddr_t saddr; 3410 size_t len; 3411 3412 ASSERT(addr >= seg->s_base && addr <= eaddr); 3413 3414 if (addr == eaddr) 3415 return (eaddr); 3416 3417 refill: 3418 ASSERT(addr < eaddr); 3419 pagev->pg_pnbase = seg_page(seg, addr); 3420 pnlim = pagev->pg_pnbase + pagev->pg_npages; 3421 saddr = addr; 3422 3423 if (lastpg < pnlim) 3424 len = (size_t)(eaddr - addr); 3425 else 3426 len = pagev->pg_npages * PAGESIZE; 3427 3428 if (pagev->pg_incore != NULL) { 3429 /* 3430 * INCORE cleverly has different semantics than GETPROT: 3431 * it returns info on pages up to but NOT including addr + len. 3432 */ 3433 SEGOP_INCORE(seg, addr, len, pagev->pg_incore); 3434 pn = pagev->pg_pnbase; 3435 3436 do { 3437 /* 3438 * Guilty knowledge here: We know that segvn_incore 3439 * returns more than just the low-order bit that 3440 * indicates the page is actually in memory. If any 3441 * bits are set, then the page has backing store. 3442 */ 3443 if (pagev->pg_incore[pn++ - pagev->pg_pnbase]) 3444 goto out; 3445 3446 } while ((addr += PAGESIZE) < eaddr && pn < pnlim); 3447 3448 /* 3449 * If we examined all the pages in the vector but we're not 3450 * at the end of the segment, take another lap. 3451 */ 3452 if (addr < eaddr) 3453 goto refill; 3454 } 3455 3456 /* 3457 * Need to take len - 1 because addr + len is the address of the 3458 * first byte of the page just past the end of what we want. 3459 */ 3460 out: 3461 SEGOP_GETPROT(seg, saddr, len - 1, pagev->pg_protv); 3462 return (addr); 3463 } 3464 3465 static caddr_t 3466 pr_pagev_nextprot(prpagev_t *pagev, struct seg *seg, 3467 caddr_t *saddrp, caddr_t eaddr, uint_t *protp) 3468 { 3469 /* 3470 * Our starting address is either the specified address, or the base 3471 * address from the start of the pagev. If the latter is greater, 3472 * this means a previous call to pr_pagev_fill has already scanned 3473 * further than the end of the previous mapping. 3474 */ 3475 caddr_t base = seg->s_base + pagev->pg_pnbase * PAGESIZE; 3476 caddr_t addr = MAX(*saddrp, base); 3477 ulong_t pn = seg_page(seg, addr); 3478 uint_t prot, nprot; 3479 3480 /* 3481 * If we're dealing with noreserve pages, then advance addr to 3482 * the address of the next page which has backing store. 3483 */ 3484 if (pagev->pg_incore != NULL) { 3485 while (pagev->pg_incore[pn - pagev->pg_pnbase] == 0) { 3486 if ((addr += PAGESIZE) == eaddr) { 3487 *saddrp = addr; 3488 prot = 0; 3489 goto out; 3490 } 3491 if (++pn == pagev->pg_pnbase + pagev->pg_npages) { 3492 addr = pr_pagev_fill(pagev, seg, addr, eaddr); 3493 if (addr == eaddr) { 3494 *saddrp = addr; 3495 prot = 0; 3496 goto out; 3497 } 3498 pn = seg_page(seg, addr); 3499 } 3500 } 3501 } 3502 3503 /* 3504 * Get the protections on the page corresponding to addr. 3505 */ 3506 pn = seg_page(seg, addr); 3507 ASSERT(pn >= pagev->pg_pnbase); 3508 ASSERT(pn < (pagev->pg_pnbase + pagev->pg_npages)); 3509 3510 prot = pagev->pg_protv[pn - pagev->pg_pnbase]; 3511 getwatchprot(seg->s_as, addr, &prot); 3512 *saddrp = addr; 3513 3514 /* 3515 * Now loop until we find a backed page with different protections 3516 * or we reach the end of this segment. 3517 */ 3518 while ((addr += PAGESIZE) < eaddr) { 3519 /* 3520 * If pn has advanced to the page number following what we 3521 * have information on, refill the page vector and reset 3522 * addr and pn. If pr_pagev_fill does not return the 3523 * address of the next page, we have a discontiguity and 3524 * thus have reached the end of the current mapping. 3525 */ 3526 if (++pn == pagev->pg_pnbase + pagev->pg_npages) { 3527 caddr_t naddr = pr_pagev_fill(pagev, seg, addr, eaddr); 3528 if (naddr != addr) 3529 goto out; 3530 pn = seg_page(seg, addr); 3531 } 3532 3533 /* 3534 * The previous page's protections are in prot, and it has 3535 * backing. If this page is MAP_NORESERVE and has no backing, 3536 * then end this mapping and return the previous protections. 3537 */ 3538 if (pagev->pg_incore != NULL && 3539 pagev->pg_incore[pn - pagev->pg_pnbase] == 0) 3540 break; 3541 3542 /* 3543 * Otherwise end the mapping if this page's protections (nprot) 3544 * are different than those in the previous page (prot). 3545 */ 3546 nprot = pagev->pg_protv[pn - pagev->pg_pnbase]; 3547 getwatchprot(seg->s_as, addr, &nprot); 3548 3549 if (nprot != prot) 3550 break; 3551 } 3552 3553 out: 3554 *protp = prot; 3555 return (addr); 3556 } 3557 3558 size_t 3559 pr_getsegsize(struct seg *seg, int reserved) 3560 { 3561 size_t size = seg->s_size; 3562 3563 /* 3564 * If we're interested in the reserved space, return the size of the 3565 * segment itself. Everything else in this function is a special case 3566 * to determine the actual underlying size of various segment types. 3567 */ 3568 if (reserved) 3569 return (size); 3570 3571 /* 3572 * If this is a segvn mapping of a regular file, return the smaller 3573 * of the segment size and the remaining size of the file beyond 3574 * the file offset corresponding to seg->s_base. 3575 */ 3576 if (seg->s_ops == &segvn_ops) { 3577 vattr_t vattr; 3578 vnode_t *vp; 3579 3580 vattr.va_mask = AT_SIZE; 3581 3582 if (SEGOP_GETVP(seg, seg->s_base, &vp) == 0 && 3583 vp != NULL && vp->v_type == VREG && 3584 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 3585 3586 u_offset_t fsize = vattr.va_size; 3587 u_offset_t offset = SEGOP_GETOFFSET(seg, seg->s_base); 3588 3589 if (fsize < offset) 3590 fsize = 0; 3591 else 3592 fsize -= offset; 3593 3594 fsize = roundup(fsize, (u_offset_t)PAGESIZE); 3595 3596 if (fsize < (u_offset_t)size) 3597 size = (size_t)fsize; 3598 } 3599 3600 return (size); 3601 } 3602 3603 /* 3604 * If this is an ISM shared segment, don't include pages that are 3605 * beyond the real size of the spt segment that backs it. 3606 */ 3607 if (seg->s_ops == &segspt_shmops) 3608 return (MIN(spt_realsize(seg), size)); 3609 3610 /* 3611 * If this is segment is a mapping from /dev/null, then this is a 3612 * reservation of virtual address space and has no actual size. 3613 * Such segments are backed by segdev and have type set to neither 3614 * MAP_SHARED nor MAP_PRIVATE. 3615 */ 3616 if (seg->s_ops == &segdev_ops && 3617 ((SEGOP_GETTYPE(seg, seg->s_base) & 3618 (MAP_SHARED | MAP_PRIVATE)) == 0)) 3619 return (0); 3620 3621 /* 3622 * If this segment doesn't match one of the special types we handle, 3623 * just return the size of the segment itself. 3624 */ 3625 return (size); 3626 } 3627 3628 uint_t 3629 pr_getprot(struct seg *seg, int reserved, void **tmp, 3630 caddr_t *saddrp, caddr_t *naddrp, caddr_t eaddr) 3631 { 3632 struct as *as = seg->s_as; 3633 3634 caddr_t saddr = *saddrp; 3635 caddr_t naddr; 3636 3637 int check_noreserve; 3638 uint_t prot; 3639 3640 union { 3641 struct segvn_data *svd; 3642 struct segdev_data *sdp; 3643 void *data; 3644 } s; 3645 3646 s.data = seg->s_data; 3647 3648 ASSERT(AS_WRITE_HELD(as, &as->a_lock)); 3649 ASSERT(saddr >= seg->s_base && saddr < eaddr); 3650 ASSERT(eaddr <= seg->s_base + seg->s_size); 3651 3652 /* 3653 * Don't include MAP_NORESERVE pages in the address range 3654 * unless their mappings have actually materialized. 3655 * We cheat by knowing that segvn is the only segment 3656 * driver that supports MAP_NORESERVE. 3657 */ 3658 check_noreserve = 3659 (!reserved && seg->s_ops == &segvn_ops && s.svd != NULL && 3660 (s.svd->vp == NULL || s.svd->vp->v_type != VREG) && 3661 (s.svd->flags & MAP_NORESERVE)); 3662 3663 /* 3664 * Examine every page only as a last resort. We use guilty knowledge 3665 * of segvn and segdev to avoid this: if there are no per-page 3666 * protections present in the segment and we don't care about 3667 * MAP_NORESERVE, then s_data->prot is the prot for the whole segment. 3668 */ 3669 if (!check_noreserve && saddr == seg->s_base && 3670 seg->s_ops == &segvn_ops && s.svd != NULL && s.svd->pageprot == 0) { 3671 prot = s.svd->prot; 3672 getwatchprot(as, saddr, &prot); 3673 naddr = eaddr; 3674 3675 } else if (saddr == seg->s_base && seg->s_ops == &segdev_ops && 3676 s.sdp != NULL && s.sdp->pageprot == 0) { 3677 prot = s.sdp->prot; 3678 getwatchprot(as, saddr, &prot); 3679 naddr = eaddr; 3680 3681 } else { 3682 prpagev_t *pagev; 3683 3684 /* 3685 * If addr is sitting at the start of the segment, then 3686 * create a page vector to store protection and incore 3687 * information for pages in the segment, and fill it. 3688 * Otherwise, we expect *tmp to address the prpagev_t 3689 * allocated by a previous call to this function. 3690 */ 3691 if (saddr == seg->s_base) { 3692 pagev = pr_pagev_create(seg, check_noreserve); 3693 saddr = pr_pagev_fill(pagev, seg, saddr, eaddr); 3694 3695 ASSERT(*tmp == NULL); 3696 *tmp = pagev; 3697 3698 ASSERT(saddr <= eaddr); 3699 *saddrp = saddr; 3700 3701 if (saddr == eaddr) { 3702 naddr = saddr; 3703 prot = 0; 3704 goto out; 3705 } 3706 3707 } else { 3708 ASSERT(*tmp != NULL); 3709 pagev = (prpagev_t *)*tmp; 3710 } 3711 3712 naddr = pr_pagev_nextprot(pagev, seg, saddrp, eaddr, &prot); 3713 ASSERT(naddr <= eaddr); 3714 } 3715 3716 out: 3717 if (naddr == eaddr) 3718 pr_getprot_done(tmp); 3719 *naddrp = naddr; 3720 return (prot); 3721 } 3722 3723 void 3724 pr_getprot_done(void **tmp) 3725 { 3726 if (*tmp != NULL) { 3727 pr_pagev_destroy((prpagev_t *)*tmp); 3728 *tmp = NULL; 3729 } 3730 } 3731 3732 /* 3733 * Return true iff the vnode is a /proc file from the object directory. 3734 */ 3735 int 3736 pr_isobject(vnode_t *vp) 3737 { 3738 return (vn_matchops(vp, prvnodeops) && VTOP(vp)->pr_type == PR_OBJECT); 3739 } 3740 3741 /* 3742 * Return true iff the vnode is a /proc file opened by the process itself. 3743 */ 3744 int 3745 pr_isself(vnode_t *vp) 3746 { 3747 /* 3748 * XXX: To retain binary compatibility with the old 3749 * ioctl()-based version of /proc, we exempt self-opens 3750 * of /proc/<pid> from being marked close-on-exec. 3751 */ 3752 return (vn_matchops(vp, prvnodeops) && 3753 (VTOP(vp)->pr_flags & PR_ISSELF) && 3754 VTOP(vp)->pr_type != PR_PIDDIR); 3755 } 3756 3757 static ssize_t 3758 pr_getpagesize(struct seg *seg, caddr_t saddr, caddr_t *naddrp, caddr_t eaddr) 3759 { 3760 ssize_t pagesize, hatsize; 3761 3762 ASSERT(AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock)); 3763 ASSERT(IS_P2ALIGNED(saddr, PAGESIZE)); 3764 ASSERT(IS_P2ALIGNED(eaddr, PAGESIZE)); 3765 ASSERT(saddr < eaddr); 3766 3767 pagesize = hatsize = hat_getpagesize(seg->s_as->a_hat, saddr); 3768 ASSERT(pagesize == -1 || IS_P2ALIGNED(pagesize, pagesize)); 3769 ASSERT(pagesize != 0); 3770 3771 if (pagesize == -1) 3772 pagesize = PAGESIZE; 3773 3774 saddr += P2NPHASE((uintptr_t)saddr, pagesize); 3775 3776 while (saddr < eaddr) { 3777 if (hatsize != hat_getpagesize(seg->s_as->a_hat, saddr)) 3778 break; 3779 ASSERT(IS_P2ALIGNED(saddr, pagesize)); 3780 saddr += pagesize; 3781 } 3782 3783 *naddrp = ((saddr < eaddr) ? saddr : eaddr); 3784 return (hatsize); 3785 } 3786 3787 /* 3788 * Return an array of structures with extended memory map information. 3789 * We allocate here; the caller must deallocate. 3790 */ 3791 int 3792 prgetxmap(proc_t *p, prxmap_t **prxmapp, size_t *sizep) 3793 { 3794 struct as *as = p->p_as; 3795 int nmaps = 0; 3796 prxmap_t *mp; 3797 size_t size; 3798 struct seg *seg; 3799 struct seg *brkseg, *stkseg; 3800 struct vnode *vp; 3801 struct vattr vattr; 3802 uint_t prot; 3803 3804 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 3805 3806 /* initial allocation */ 3807 *sizep = size = INITIAL_MAPSIZE; 3808 *prxmapp = mp = kmem_alloc(size, KM_SLEEP); 3809 3810 if ((seg = AS_SEGFIRST(as)) == NULL) 3811 return (0); 3812 3813 brkseg = break_seg(p); 3814 stkseg = as_segat(as, prgetstackbase(p)); 3815 3816 do { 3817 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 3818 caddr_t saddr, naddr, baddr; 3819 void *tmp = NULL; 3820 ssize_t psz; 3821 char *parr; 3822 uint64_t npages; 3823 uint64_t pagenum; 3824 3825 /* 3826 * Segment loop part one: iterate from the base of the segment 3827 * to its end, pausing at each address boundary (baddr) between 3828 * ranges that have different virtual memory protections. 3829 */ 3830 for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) { 3831 prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr); 3832 ASSERT(baddr >= saddr && baddr <= eaddr); 3833 3834 /* 3835 * Segment loop part two: iterate from the current 3836 * position to the end of the protection boundary, 3837 * pausing at each address boundary (naddr) between 3838 * ranges that have different underlying page sizes. 3839 */ 3840 for (; saddr < baddr; saddr = naddr) { 3841 psz = pr_getpagesize(seg, saddr, &naddr, baddr); 3842 ASSERT(naddr >= saddr && naddr <= baddr); 3843 3844 /* reallocate if necessary */ 3845 if ((nmaps + 1) * sizeof (prxmap_t) > size) { 3846 size_t newsize = size + 3 * size / 16; 3847 prxmap_t *newmp = 3848 kmem_alloc(newsize, KM_SLEEP); 3849 3850 bcopy(*prxmapp, newmp, 3851 nmaps * sizeof (prxmap_t)); 3852 kmem_free(*prxmapp, size); 3853 *sizep = size = newsize; 3854 *prxmapp = newmp; 3855 mp = newmp + nmaps; 3856 } 3857 3858 bzero(mp, sizeof (*mp)); 3859 mp->pr_vaddr = (uintptr_t)saddr; 3860 mp->pr_size = naddr - saddr; 3861 mp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 3862 mp->pr_mflags = 0; 3863 if (prot & PROT_READ) 3864 mp->pr_mflags |= MA_READ; 3865 if (prot & PROT_WRITE) 3866 mp->pr_mflags |= MA_WRITE; 3867 if (prot & PROT_EXEC) 3868 mp->pr_mflags |= MA_EXEC; 3869 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 3870 mp->pr_mflags |= MA_SHARED; 3871 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 3872 mp->pr_mflags |= MA_NORESERVE; 3873 if (seg->s_ops == &segspt_shmops || 3874 (seg->s_ops == &segvn_ops && 3875 (SEGOP_GETVP(seg, saddr, &vp) != 0 || 3876 vp == NULL))) 3877 mp->pr_mflags |= MA_ANON; 3878 if (seg == brkseg) 3879 mp->pr_mflags |= MA_BREAK; 3880 else if (seg == stkseg) 3881 mp->pr_mflags |= MA_STACK; 3882 if (seg->s_ops == &segspt_shmops) 3883 mp->pr_mflags |= MA_ISM | MA_SHM; 3884 3885 mp->pr_pagesize = PAGESIZE; 3886 if (psz == -1) { 3887 mp->pr_hatpagesize = 0; 3888 } else { 3889 mp->pr_hatpagesize = psz; 3890 } 3891 3892 /* 3893 * Manufacture a filename for the "object" dir. 3894 */ 3895 mp->pr_dev = PRNODEV; 3896 vattr.va_mask = AT_FSID|AT_NODEID; 3897 if (seg->s_ops == &segvn_ops && 3898 SEGOP_GETVP(seg, saddr, &vp) == 0 && 3899 vp != NULL && vp->v_type == VREG && 3900 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 3901 mp->pr_dev = vattr.va_fsid; 3902 mp->pr_ino = vattr.va_nodeid; 3903 if (vp == p->p_exec) 3904 (void) strcpy(mp->pr_mapname, 3905 "a.out"); 3906 else 3907 pr_object_name(mp->pr_mapname, 3908 vp, &vattr); 3909 } 3910 3911 /* 3912 * Get the SysV shared memory id, if any. 3913 */ 3914 if ((mp->pr_mflags & MA_SHARED) && 3915 p->p_segacct && (mp->pr_shmid = shmgetid(p, 3916 seg->s_base)) != SHMID_NONE) { 3917 if (mp->pr_shmid == SHMID_FREE) 3918 mp->pr_shmid = -1; 3919 3920 mp->pr_mflags |= MA_SHM; 3921 } else { 3922 mp->pr_shmid = -1; 3923 } 3924 3925 npages = ((uintptr_t)(naddr - saddr)) >> 3926 PAGESHIFT; 3927 parr = kmem_zalloc(npages, KM_SLEEP); 3928 3929 SEGOP_INCORE(seg, saddr, naddr - saddr, parr); 3930 3931 for (pagenum = 0; pagenum < npages; pagenum++) { 3932 if (parr[pagenum] & SEG_PAGE_INCORE) 3933 mp->pr_rss++; 3934 if (parr[pagenum] & SEG_PAGE_ANON) 3935 mp->pr_anon++; 3936 if (parr[pagenum] & SEG_PAGE_LOCKED) 3937 mp->pr_locked++; 3938 } 3939 kmem_free(parr, npages); 3940 mp++; 3941 nmaps++; 3942 } 3943 } 3944 ASSERT(tmp == NULL); 3945 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 3946 3947 return (nmaps); 3948 } 3949 3950 /* 3951 * Return the process's credentials. We don't need a 32-bit equivalent of 3952 * this function because prcred_t and prcred32_t are actually the same. 3953 */ 3954 void 3955 prgetcred(proc_t *p, prcred_t *pcrp) 3956 { 3957 mutex_enter(&p->p_crlock); 3958 cred2prcred(p->p_cred, pcrp); 3959 mutex_exit(&p->p_crlock); 3960 } 3961 3962 /* 3963 * Compute actual size of the prpriv_t structure. 3964 */ 3965 3966 size_t 3967 prgetprivsize(void) 3968 { 3969 return (priv_prgetprivsize(NULL)); 3970 } 3971 3972 /* 3973 * Return the process's privileges. We don't need a 32-bit equivalent of 3974 * this function because prpriv_t and prpriv32_t are actually the same. 3975 */ 3976 void 3977 prgetpriv(proc_t *p, prpriv_t *pprp) 3978 { 3979 mutex_enter(&p->p_crlock); 3980 cred2prpriv(p->p_cred, pprp); 3981 mutex_exit(&p->p_crlock); 3982 } 3983 3984 #ifdef _SYSCALL32_IMPL 3985 /* 3986 * Return an array of structures with HAT memory map information. 3987 * We allocate here; the caller must deallocate. 3988 */ 3989 int 3990 prgetxmap32(proc_t *p, prxmap32_t **prxmapp, size_t *sizep) 3991 { 3992 struct as *as = p->p_as; 3993 int nmaps = 0; 3994 prxmap32_t *mp; 3995 size_t size; 3996 struct seg *seg; 3997 struct seg *brkseg, *stkseg; 3998 struct vnode *vp; 3999 struct vattr vattr; 4000 uint_t prot; 4001 4002 ASSERT(as != &kas && AS_WRITE_HELD(as, &as->a_lock)); 4003 4004 /* initial allocation */ 4005 *sizep = size = INITIAL_MAPSIZE; 4006 *prxmapp = mp = kmem_alloc(size, KM_SLEEP); 4007 4008 if ((seg = AS_SEGFIRST(as)) == NULL) 4009 return (0); 4010 4011 brkseg = break_seg(p); 4012 stkseg = as_segat(as, prgetstackbase(p)); 4013 4014 do { 4015 caddr_t eaddr = seg->s_base + pr_getsegsize(seg, 0); 4016 caddr_t saddr, naddr, baddr; 4017 void *tmp = NULL; 4018 ssize_t psz; 4019 char *parr; 4020 uint64_t npages; 4021 uint64_t pagenum; 4022 4023 /* 4024 * Segment loop part one: iterate from the base of the segment 4025 * to its end, pausing at each address boundary (baddr) between 4026 * ranges that have different virtual memory protections. 4027 */ 4028 for (saddr = seg->s_base; saddr < eaddr; saddr = baddr) { 4029 prot = pr_getprot(seg, 0, &tmp, &saddr, &baddr, eaddr); 4030 ASSERT(baddr >= saddr && baddr <= eaddr); 4031 4032 /* 4033 * Segment loop part two: iterate from the current 4034 * position to the end of the protection boundary, 4035 * pausing at each address boundary (naddr) between 4036 * ranges that have different underlying page sizes. 4037 */ 4038 for (; saddr < baddr; saddr = naddr) { 4039 psz = pr_getpagesize(seg, saddr, &naddr, baddr); 4040 ASSERT(naddr >= saddr && naddr <= baddr); 4041 4042 /* reallocate if necessary */ 4043 if ((nmaps + 1) * sizeof (prxmap32_t) > size) { 4044 size_t newsize = size + 3 * size / 16; 4045 prxmap32_t *newmp = 4046 kmem_alloc(newsize, KM_SLEEP); 4047 4048 bcopy(*prxmapp, newmp, 4049 nmaps * sizeof (prxmap32_t)); 4050 kmem_free(*prxmapp, size); 4051 *sizep = size = newsize; 4052 *prxmapp = newmp; 4053 mp = newmp + nmaps; 4054 } 4055 4056 bzero(mp, sizeof (*mp)); 4057 mp->pr_vaddr = (caddr32_t)(uintptr_t)saddr; 4058 mp->pr_size = (size32_t)(naddr - saddr); 4059 mp->pr_offset = SEGOP_GETOFFSET(seg, saddr); 4060 mp->pr_mflags = 0; 4061 if (prot & PROT_READ) 4062 mp->pr_mflags |= MA_READ; 4063 if (prot & PROT_WRITE) 4064 mp->pr_mflags |= MA_WRITE; 4065 if (prot & PROT_EXEC) 4066 mp->pr_mflags |= MA_EXEC; 4067 if (SEGOP_GETTYPE(seg, saddr) & MAP_SHARED) 4068 mp->pr_mflags |= MA_SHARED; 4069 if (SEGOP_GETTYPE(seg, saddr) & MAP_NORESERVE) 4070 mp->pr_mflags |= MA_NORESERVE; 4071 if (seg->s_ops == &segspt_shmops || 4072 (seg->s_ops == &segvn_ops && 4073 (SEGOP_GETVP(seg, saddr, &vp) != 0 || 4074 vp == NULL))) 4075 mp->pr_mflags |= MA_ANON; 4076 if (seg == brkseg) 4077 mp->pr_mflags |= MA_BREAK; 4078 else if (seg == stkseg) 4079 mp->pr_mflags |= MA_STACK; 4080 if (seg->s_ops == &segspt_shmops) 4081 mp->pr_mflags |= MA_ISM | MA_SHM; 4082 4083 mp->pr_pagesize = PAGESIZE; 4084 if (psz == -1) { 4085 mp->pr_hatpagesize = 0; 4086 } else { 4087 mp->pr_hatpagesize = psz; 4088 } 4089 4090 /* 4091 * Manufacture a filename for the "object" dir. 4092 */ 4093 mp->pr_dev = PRNODEV32; 4094 vattr.va_mask = AT_FSID|AT_NODEID; 4095 if (seg->s_ops == &segvn_ops && 4096 SEGOP_GETVP(seg, saddr, &vp) == 0 && 4097 vp != NULL && vp->v_type == VREG && 4098 VOP_GETATTR(vp, &vattr, 0, CRED()) == 0) { 4099 (void) cmpldev(&mp->pr_dev, 4100 vattr.va_fsid); 4101 mp->pr_ino = vattr.va_nodeid; 4102 if (vp == p->p_exec) 4103 (void) strcpy(mp->pr_mapname, 4104 "a.out"); 4105 else 4106 pr_object_name(mp->pr_mapname, 4107 vp, &vattr); 4108 } 4109 4110 /* 4111 * Get the SysV shared memory id, if any. 4112 */ 4113 if ((mp->pr_mflags & MA_SHARED) && 4114 p->p_segacct && (mp->pr_shmid = shmgetid(p, 4115 seg->s_base)) != SHMID_NONE) { 4116 if (mp->pr_shmid == SHMID_FREE) 4117 mp->pr_shmid = -1; 4118 4119 mp->pr_mflags |= MA_SHM; 4120 } else { 4121 mp->pr_shmid = -1; 4122 } 4123 4124 npages = ((uintptr_t)(naddr - saddr)) >> 4125 PAGESHIFT; 4126 parr = kmem_zalloc(npages, KM_SLEEP); 4127 4128 SEGOP_INCORE(seg, saddr, naddr - saddr, parr); 4129 4130 for (pagenum = 0; pagenum < npages; pagenum++) { 4131 if (parr[pagenum] & SEG_PAGE_INCORE) 4132 mp->pr_rss++; 4133 if (parr[pagenum] & SEG_PAGE_ANON) 4134 mp->pr_anon++; 4135 if (parr[pagenum] & SEG_PAGE_LOCKED) 4136 mp->pr_locked++; 4137 } 4138 kmem_free(parr, npages); 4139 mp++; 4140 nmaps++; 4141 } 4142 } 4143 ASSERT(tmp == NULL); 4144 } while ((seg = AS_SEGNEXT(as, seg)) != NULL); 4145 4146 return (nmaps); 4147 } 4148 #endif /* _SYSCALL32_IMPL */ 4149