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