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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include <sys/atomic.h> 31 #include <sys/errno.h> 32 #include <sys/stat.h> 33 #include <sys/modctl.h> 34 #include <sys/conf.h> 35 #include <sys/systm.h> 36 #include <sys/ddi.h> 37 #include <sys/sunddi.h> 38 #include <sys/cpuvar.h> 39 #include <sys/kmem.h> 40 #include <sys/strsubr.h> 41 #include <sys/fasttrap.h> 42 #include <sys/fasttrap_impl.h> 43 #include <sys/fasttrap_isa.h> 44 #include <sys/dtrace.h> 45 #include <sys/dtrace_impl.h> 46 #include <sys/sysmacros.h> 47 #include <sys/frame.h> 48 #include <sys/stack.h> 49 #include <sys/proc.h> 50 #include <sys/priv.h> 51 #include <sys/policy.h> 52 #include <sys/ontrap.h> 53 #include <sys/vmsystm.h> 54 #include <sys/prsystm.h> 55 56 57 #include <vm/as.h> 58 #include <vm/seg.h> 59 #include <vm/seg_dev.h> 60 #include <vm/seg_vn.h> 61 #include <vm/seg_spt.h> 62 #include <vm/seg_kmem.h> 63 64 /* 65 * User-Land Trap-Based Tracing 66 * ---------------------------- 67 * 68 * The fasttrap provider allows DTrace consumers to instrument any user-level 69 * instruction to gather data; this includes probes with semantic 70 * signifigance like entry and return as well as simple offsets into the 71 * function. While the specific techniques used are very ISA specific, the 72 * methodology is generalizable to any architecture. 73 * 74 * 75 * The General Methodology 76 * ----------------------- 77 * 78 * With the primary goal of tracing every user-land instruction and the 79 * limitation that we can't trust user space so don't want to rely on much 80 * information there, we begin by replacing the instructions we want to trace 81 * with trap instructions. Each instruction we overwrite is saved into a hash 82 * table keyed by process ID and pc address. When we enter the kernel due to 83 * this trap instruction, we need the effects of the replaced instruction to 84 * appear to have occurred before we proceed with the user thread's 85 * execution. 86 * 87 * Each user level thread is represented by a ulwp_t structure which is 88 * always easily accessible through a register. The most basic way to produce 89 * the effects of the instruction we replaced is to copy that instruction out 90 * to a bit of scratch space reserved in the user thread's ulwp_t structure 91 * (a sort of kernel-private thread local storage), set the PC to that 92 * scratch space and single step. When we reenter the kernel after single 93 * stepping the instruction we must then adjust the PC to point to what would 94 * normally be the next instruction. Of course, special care must be taken 95 * for branches and jumps, but these represent such a small fraction of any 96 * instruction set that writing the code to emulate these in the kernel is 97 * not too difficult. 98 * 99 * Return probes may require several tracepoints to trace every return site, 100 * and, conversely, each tracepoint may activate several probes (the entry 101 * and offset 0 probes, for example). To solve this muliplexing problem, 102 * tracepoints contain lists of probes to activate and probes contain lists 103 * of tracepoints to enable. If a probe is activated, it adds its ID to 104 * existing tracepoints or creates new ones as necessary. 105 * 106 * Most probes are activated _before_ the instruction is executed, but return 107 * probes are activated _after_ the effects of the last instruction of the 108 * function are visible. Return probes must be fired _after_ we have 109 * single-stepped the instruction whereas all other probes are fired 110 * beforehand. 111 */ 112 113 static dev_info_t *fasttrap_devi; 114 static dtrace_provider_id_t fasttrap_id; 115 static dtrace_meta_provider_id_t fasttrap_meta_id; 116 117 static timeout_id_t fasttrap_timeout; 118 static kmutex_t fasttrap_cleanup_mtx; 119 static uint_t fasttrap_cleanup_work; 120 121 /* 122 * Generation count on modifications to the global tracepoint lookup table. 123 */ 124 static volatile uint64_t fasttrap_mod_gen; 125 126 /* 127 * When the fasttrap provider is loaded, fasttrap_max is set to either 128 * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the 129 * fasttrap.conf file. Each time a probe is created, fasttrap_total is 130 * incremented by the number of tracepoints that may be associated with that 131 * probe; fasttrap_total is capped at fasttrap_max. 132 */ 133 #define FASTTRAP_MAX_DEFAULT 250000 134 static uint32_t fasttrap_max; 135 static uint32_t fasttrap_total; 136 137 138 #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 139 #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 140 #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100 141 142 #define FASTTRAP_PID_NAME "pid" 143 144 fasttrap_hash_t fasttrap_tpoints; 145 static fasttrap_hash_t fasttrap_provs; 146 static fasttrap_hash_t fasttrap_procs; 147 148 dtrace_id_t fasttrap_probe_id; 149 static int fasttrap_count; /* ref count */ 150 static int fasttrap_pid_count; /* pid ref count */ 151 static kmutex_t fasttrap_count_mtx; /* lock on ref count */ 152 153 #define FASTTRAP_ENABLE_FAIL 1 154 #define FASTTRAP_ENABLE_PARTIAL 2 155 156 static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t); 157 static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t); 158 159 static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *, 160 const dtrace_pattr_t *); 161 static void fasttrap_provider_free(fasttrap_provider_t *); 162 static void fasttrap_provider_retire(fasttrap_provider_t *); 163 164 static fasttrap_proc_t *fasttrap_proc_lookup(pid_t); 165 static void fasttrap_proc_release(fasttrap_proc_t *); 166 167 #define FASTTRAP_PROVS_INDEX(pid, name) \ 168 ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 169 170 #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask) 171 172 static int 173 fasttrap_highbit(ulong_t i) 174 { 175 int h = 1; 176 177 if (i == 0) 178 return (0); 179 #ifdef _LP64 180 if (i & 0xffffffff00000000ul) { 181 h += 32; i >>= 32; 182 } 183 #endif 184 if (i & 0xffff0000) { 185 h += 16; i >>= 16; 186 } 187 if (i & 0xff00) { 188 h += 8; i >>= 8; 189 } 190 if (i & 0xf0) { 191 h += 4; i >>= 4; 192 } 193 if (i & 0xc) { 194 h += 2; i >>= 2; 195 } 196 if (i & 0x2) { 197 h += 1; 198 } 199 return (h); 200 } 201 202 static uint_t 203 fasttrap_hash_str(const char *p) 204 { 205 unsigned int g; 206 uint_t hval = 0; 207 208 while (*p) { 209 hval = (hval << 4) + *p++; 210 if ((g = (hval & 0xf0000000)) != 0) 211 hval ^= g >> 24; 212 hval &= ~g; 213 } 214 return (hval); 215 } 216 217 void 218 fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 219 { 220 sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 221 222 sqp->sq_info.si_signo = SIGTRAP; 223 sqp->sq_info.si_code = TRAP_DTRACE; 224 sqp->sq_info.si_addr = (caddr_t)pc; 225 226 mutex_enter(&p->p_lock); 227 sigaddqa(p, t, sqp); 228 mutex_exit(&p->p_lock); 229 230 if (t != NULL) 231 aston(t); 232 } 233 234 /* 235 * This function ensures that no threads are actively using the memory 236 * associated with probes that were formerly live. 237 */ 238 static void 239 fasttrap_mod_barrier(uint64_t gen) 240 { 241 int i; 242 243 if (gen < fasttrap_mod_gen) 244 return; 245 246 fasttrap_mod_gen++; 247 248 for (i = 0; i < NCPU; i++) { 249 mutex_enter(&cpu_core[i].cpuc_pid_lock); 250 mutex_exit(&cpu_core[i].cpuc_pid_lock); 251 } 252 } 253 254 /* 255 * This is the timeout's callback for cleaning up the providers and their 256 * probes. 257 */ 258 /*ARGSUSED*/ 259 static void 260 fasttrap_pid_cleanup_cb(void *data) 261 { 262 fasttrap_provider_t **fpp, *fp; 263 fasttrap_bucket_t *bucket; 264 dtrace_provider_id_t provid; 265 int i, later; 266 267 static volatile int in = 0; 268 ASSERT(in == 0); 269 in = 1; 270 271 mutex_enter(&fasttrap_cleanup_mtx); 272 while (fasttrap_cleanup_work) { 273 fasttrap_cleanup_work = 0; 274 mutex_exit(&fasttrap_cleanup_mtx); 275 276 later = 0; 277 278 /* 279 * Iterate over all the providers trying to remove the marked 280 * ones. If a provider is marked but not retired, we just 281 * have to take a crack at removing it -- it's no big deal if 282 * we can't. 283 */ 284 for (i = 0; i < fasttrap_provs.fth_nent; i++) { 285 bucket = &fasttrap_provs.fth_table[i]; 286 mutex_enter(&bucket->ftb_mtx); 287 fpp = (fasttrap_provider_t **)&bucket->ftb_data; 288 289 while ((fp = *fpp) != NULL) { 290 if (!fp->ftp_marked) { 291 fpp = &fp->ftp_next; 292 continue; 293 } 294 295 mutex_enter(&fp->ftp_mtx); 296 297 /* 298 * If this provider is referenced either 299 * because it is a USDT provider or is being 300 * modified, we can't unregister or even 301 * condense. 302 */ 303 if (fp->ftp_ccount != 0) { 304 mutex_exit(&fp->ftp_mtx); 305 fp->ftp_marked = 0; 306 continue; 307 } 308 309 if (!fp->ftp_retired || fp->ftp_rcount != 0) 310 fp->ftp_marked = 0; 311 312 mutex_exit(&fp->ftp_mtx); 313 314 /* 315 * If we successfully unregister this 316 * provider we can remove it from the hash 317 * chain and free the memory. If our attempt 318 * to unregister fails and this is a retired 319 * provider, increment our flag to try again 320 * pretty soon. If we've consumed more than 321 * half of our total permitted number of 322 * probes call dtrace_condense() to try to 323 * clean out the unenabled probes. 324 */ 325 provid = fp->ftp_provid; 326 if (dtrace_unregister(provid) != 0) { 327 if (fasttrap_total > fasttrap_max / 2) 328 (void) dtrace_condense(provid); 329 later += fp->ftp_marked; 330 fpp = &fp->ftp_next; 331 } else { 332 *fpp = fp->ftp_next; 333 fasttrap_provider_free(fp); 334 } 335 } 336 mutex_exit(&bucket->ftb_mtx); 337 } 338 339 mutex_enter(&fasttrap_cleanup_mtx); 340 } 341 342 ASSERT(fasttrap_timeout != 0); 343 344 /* 345 * If we were unable to remove a retired provider, try again after 346 * a second. This situation can occur in certain circumstances where 347 * providers cannot be unregistered even though they have no probes 348 * enabled because of an execution of dtrace -l or something similar. 349 * If the timeout has been disabled (set to 1 because we're trying 350 * to detach), we set fasttrap_cleanup_work to ensure that we'll 351 * get a chance to do that work if and when the timeout is reenabled 352 * (if detach fails). 353 */ 354 if (later > 0 && fasttrap_timeout != (timeout_id_t)1) 355 fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz); 356 else if (later > 0) 357 fasttrap_cleanup_work = 1; 358 else 359 fasttrap_timeout = 0; 360 361 mutex_exit(&fasttrap_cleanup_mtx); 362 in = 0; 363 } 364 365 /* 366 * Activates the asynchronous cleanup mechanism. 367 */ 368 static void 369 fasttrap_pid_cleanup(void) 370 { 371 mutex_enter(&fasttrap_cleanup_mtx); 372 fasttrap_cleanup_work = 1; 373 if (fasttrap_timeout == 0) 374 fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1); 375 mutex_exit(&fasttrap_cleanup_mtx); 376 } 377 378 /* 379 * This is called from cfork() via dtrace_fasttrap_fork(). The child 380 * process's address space is a (roughly) a copy of the parent process's so 381 * we have to remove all the instrumentation we had previously enabled in the 382 * parent. 383 */ 384 static void 385 fasttrap_fork(proc_t *p, proc_t *cp) 386 { 387 pid_t ppid = p->p_pid; 388 int i; 389 390 ASSERT(curproc == p); 391 ASSERT(p->p_proc_flag & P_PR_LOCK); 392 ASSERT(p->p_dtrace_count > 0); 393 ASSERT(cp->p_dtrace_count == 0); 394 395 /* 396 * This would be simpler and faster if we maintained per-process 397 * hash tables of enabled tracepoints. It could, however, potentially 398 * slow down execution of a tracepoint since we'd need to go 399 * through two levels of indirection. In the future, we should 400 * consider either maintaining per-process ancillary lists of 401 * enabled tracepoints or hanging a pointer to a per-process hash 402 * table of enabled tracepoints off the proc structure. 403 */ 404 405 /* 406 * We don't have to worry about the child process disappearing 407 * because we're in fork(). 408 */ 409 mutex_enter(&cp->p_lock); 410 sprlock_proc(cp); 411 mutex_exit(&cp->p_lock); 412 413 /* 414 * Iterate over every tracepoint looking for ones that belong to the 415 * parent process, and remove each from the child process. 416 */ 417 for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 418 fasttrap_tracepoint_t *tp; 419 fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 420 421 mutex_enter(&bucket->ftb_mtx); 422 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 423 if (tp->ftt_pid == ppid && 424 !tp->ftt_proc->ftpc_defunct) { 425 int ret = fasttrap_tracepoint_remove(cp, tp); 426 ASSERT(ret == 0); 427 } 428 } 429 mutex_exit(&bucket->ftb_mtx); 430 } 431 432 mutex_enter(&cp->p_lock); 433 sprunlock(cp); 434 } 435 436 /* 437 * This is called from proc_exit() or from exec_common() if p_dtrace_probes 438 * is set on the proc structure to indicate that there is a pid provider 439 * associated with this process. 440 */ 441 static void 442 fasttrap_exec_exit(proc_t *p) 443 { 444 fasttrap_provider_t *provider; 445 446 ASSERT(p == curproc); 447 ASSERT(MUTEX_HELD(&p->p_lock)); 448 449 mutex_exit(&p->p_lock); 450 451 /* 452 * We clean up the pid provider for this process here; user-land 453 * static probes are handled by the meta-provider remove entry point. 454 */ 455 if ((provider = fasttrap_provider_lookup(p->p_pid, 456 FASTTRAP_PID_NAME, NULL)) != NULL) 457 fasttrap_provider_retire(provider); 458 459 mutex_enter(&p->p_lock); 460 } 461 462 463 /*ARGSUSED*/ 464 static void 465 fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc) 466 { 467 /* 468 * There are no "default" pid probes. 469 */ 470 } 471 472 /*ARGSUSED*/ 473 static void 474 fasttrap_provide(void *arg, const dtrace_probedesc_t *desc) 475 { 476 if (dtrace_probe_lookup(fasttrap_id, NULL, "fasttrap", "fasttrap") == 0) 477 fasttrap_probe_id = dtrace_probe_create(fasttrap_id, NULL, 478 "fasttrap", "fasttrap", FASTTRAP_AFRAMES, NULL); 479 } 480 481 static int 482 fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 483 { 484 fasttrap_tracepoint_t *tp, *new_tp = NULL; 485 fasttrap_bucket_t *bucket; 486 fasttrap_id_t *id; 487 pid_t pid; 488 uintptr_t pc; 489 490 ASSERT(index < probe->ftp_ntps); 491 492 pid = probe->ftp_pid; 493 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 494 id = &probe->ftp_tps[index].fit_id; 495 496 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 497 498 ASSERT(!(p->p_flag & SVFORK)); 499 500 /* 501 * Before we make any modifications, make sure we've imposed a barrier 502 * on the generation in which this probe was last modified. 503 */ 504 fasttrap_mod_barrier(probe->ftp_gen); 505 506 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 507 508 /* 509 * If the tracepoint has already been enabled, just add our id to the 510 * list of interested probes. This may be our second time through 511 * this path in which case we'll have constructed the tracepoint we'd 512 * like to install. If we can't find a match, and have an allocated 513 * tracepoint ready to go, enable that one now. 514 * 515 * A tracepoint whose proc is defunct is also considered defunct. 516 */ 517 again: 518 mutex_enter(&bucket->ftb_mtx); 519 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 520 if (tp->ftt_pid != pid || tp->ftt_pc != pc || 521 tp->ftt_proc->ftpc_defunct) 522 continue; 523 524 /* 525 * Now that we've found a matching tracepoint, it would be 526 * a decent idea to confirm that the tracepoint is still 527 * enabled and the trap instruction hasn't been overwritten. 528 * Since this is a little hairy, we'll punt for now. 529 */ 530 531 /* 532 * This can't be the first interested probe. We don't have 533 * to worry about another thread being in the midst of 534 * deleting this tracepoint (which would be the only valid 535 * reason for a tracepoint to have no interested probes) 536 * since we're holding P_PR_LOCK for this process. 537 */ 538 ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 539 540 if (probe->ftp_type == DTFTP_RETURN || 541 probe->ftp_type == DTFTP_POST_OFFSETS) { 542 id->fti_next = tp->ftt_retids; 543 membar_producer(); 544 tp->ftt_retids = id; 545 membar_producer(); 546 } else { 547 id->fti_next = tp->ftt_ids; 548 membar_producer(); 549 tp->ftt_ids = id; 550 membar_producer(); 551 } 552 553 mutex_exit(&bucket->ftb_mtx); 554 555 if (new_tp != NULL) { 556 new_tp->ftt_ids = NULL; 557 new_tp->ftt_retids = NULL; 558 } 559 560 return (0); 561 } 562 563 /* 564 * If we have a good tracepoint ready to go, install it now while 565 * we have the lock held and no one can screw with us. 566 */ 567 if (new_tp != NULL) { 568 int rc = 0; 569 570 new_tp->ftt_next = bucket->ftb_data; 571 membar_producer(); 572 bucket->ftb_data = new_tp; 573 membar_producer(); 574 mutex_exit(&bucket->ftb_mtx); 575 576 /* 577 * Activate the tracepoint in the ISA-specific manner. 578 * If this fails, we need to report the failure, but 579 * indicate that this tracepoint must still be disabled 580 * by calling fasttrap_tracepoint_disable(). 581 */ 582 if (fasttrap_tracepoint_install(p, new_tp) != 0) 583 rc = FASTTRAP_ENABLE_PARTIAL; 584 585 /* 586 * Increment the count of the number of tracepoints active in 587 * the victim process. 588 */ 589 ASSERT(p->p_proc_flag & P_PR_LOCK); 590 p->p_dtrace_count++; 591 592 return (rc); 593 } 594 595 mutex_exit(&bucket->ftb_mtx); 596 597 /* 598 * Initialize the tracepoint that's been preallocated with the probe. 599 */ 600 new_tp = probe->ftp_tps[index].fit_tp; 601 602 ASSERT(new_tp->ftt_pid == pid); 603 ASSERT(new_tp->ftt_pc == pc); 604 ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 605 ASSERT(new_tp->ftt_ids == NULL); 606 ASSERT(new_tp->ftt_retids == NULL); 607 608 if (probe->ftp_type == DTFTP_RETURN || 609 probe->ftp_type == DTFTP_POST_OFFSETS) { 610 id->fti_next = NULL; 611 new_tp->ftt_retids = id; 612 } else { 613 id->fti_next = NULL; 614 new_tp->ftt_ids = id; 615 } 616 617 /* 618 * If the ISA-dependent initialization goes to plan, go back to the 619 * beginning and try to install this freshly made tracepoint. 620 */ 621 if (fasttrap_tracepoint_init(p, probe, new_tp, pc) == 0) 622 goto again; 623 624 new_tp->ftt_ids = NULL; 625 new_tp->ftt_retids = NULL; 626 627 return (FASTTRAP_ENABLE_FAIL); 628 } 629 630 static void 631 fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 632 { 633 fasttrap_bucket_t *bucket; 634 fasttrap_provider_t *provider = probe->ftp_prov; 635 fasttrap_tracepoint_t **pp, *tp; 636 fasttrap_id_t *id, **idp; 637 pid_t pid; 638 uintptr_t pc; 639 640 ASSERT(index < probe->ftp_ntps); 641 642 pid = probe->ftp_pid; 643 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 644 645 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 646 647 /* 648 * Find the tracepoint and make sure that our id is one of the 649 * ones registered with it. 650 */ 651 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 652 mutex_enter(&bucket->ftb_mtx); 653 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 654 if (tp->ftt_pid == pid && tp->ftt_pc == pc && 655 tp->ftt_proc == provider->ftp_proc) 656 break; 657 } 658 659 /* 660 * If we somehow lost this tracepoint, we're in a world of hurt. 661 */ 662 ASSERT(tp != NULL); 663 664 if (probe->ftp_type == DTFTP_RETURN || 665 probe->ftp_type == DTFTP_POST_OFFSETS) { 666 ASSERT(tp->ftt_retids != NULL); 667 idp = &tp->ftt_retids; 668 } else { 669 ASSERT(tp->ftt_ids != NULL); 670 idp = &tp->ftt_ids; 671 } 672 673 while ((*idp)->fti_probe != probe) { 674 idp = &(*idp)->fti_next; 675 ASSERT(*idp != NULL); 676 } 677 678 id = *idp; 679 *idp = id->fti_next; 680 membar_producer(); 681 682 ASSERT(id->fti_probe == probe); 683 684 /* 685 * If there are other registered enablings of this tracepoint, we're 686 * all done, but if this was the last probe assocated with this 687 * this tracepoint, we need to remove and free it. 688 */ 689 if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 690 691 /* 692 * If the current probe's tracepoint is in use, swap it 693 * for an unused tracepoint. 694 */ 695 if (tp == probe->ftp_tps[index].fit_tp) { 696 fasttrap_probe_t *tmp_probe; 697 fasttrap_tracepoint_t **tmp_tp; 698 uint_t tmp_index; 699 700 if (tp->ftt_ids != NULL) { 701 tmp_probe = tp->ftt_ids->fti_probe; 702 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 703 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 704 } else { 705 tmp_probe = tp->ftt_retids->fti_probe; 706 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 707 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 708 } 709 710 ASSERT(*tmp_tp != NULL); 711 ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 712 ASSERT((*tmp_tp)->ftt_ids == NULL); 713 ASSERT((*tmp_tp)->ftt_retids == NULL); 714 715 probe->ftp_tps[index].fit_tp = *tmp_tp; 716 *tmp_tp = tp; 717 718 } 719 720 mutex_exit(&bucket->ftb_mtx); 721 722 /* 723 * Tag the modified probe with the generation in which it was 724 * changed. 725 */ 726 probe->ftp_gen = fasttrap_mod_gen; 727 return; 728 } 729 730 mutex_exit(&bucket->ftb_mtx); 731 732 /* 733 * We can't safely remove the tracepoint from the set of active 734 * tracepoints until we've actually removed the fasttrap instruction 735 * from the process's text. We can, however, operate on this 736 * tracepoint secure in the knowledge that no other thread is going to 737 * be looking at it since we hold P_PR_LOCK on the process if it's 738 * live or we hold the provider lock on the process if it's dead and 739 * gone. 740 */ 741 742 /* 743 * We only need to remove the actual instruction if we're looking 744 * at an existing process 745 */ 746 if (p != NULL) { 747 /* 748 * If we fail to restore the instruction we need to kill 749 * this process since it's in a completely unrecoverable 750 * state. 751 */ 752 if (fasttrap_tracepoint_remove(p, tp) != 0) 753 fasttrap_sigtrap(p, NULL, pc); 754 755 /* 756 * Decrement the count of the number of tracepoints active 757 * in the victim process. 758 */ 759 ASSERT(p->p_proc_flag & P_PR_LOCK); 760 p->p_dtrace_count--; 761 } 762 763 /* 764 * Remove the probe from the hash table of active tracepoints. 765 */ 766 mutex_enter(&bucket->ftb_mtx); 767 pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 768 ASSERT(*pp != NULL); 769 while (*pp != tp) { 770 pp = &(*pp)->ftt_next; 771 ASSERT(*pp != NULL); 772 } 773 774 *pp = tp->ftt_next; 775 membar_producer(); 776 777 mutex_exit(&bucket->ftb_mtx); 778 779 /* 780 * Tag the modified probe with the generation in which it was changed. 781 */ 782 probe->ftp_gen = fasttrap_mod_gen; 783 } 784 785 typedef int fasttrap_probe_f(struct regs *); 786 787 static void 788 fasttrap_enable_common(int *count, fasttrap_probe_f **fptr, fasttrap_probe_f *f, 789 fasttrap_probe_f **fptr2, fasttrap_probe_f *f2) 790 { 791 /* 792 * We don't have to play the rw lock game here because we're 793 * providing something rather than taking something away -- 794 * we can be sure that no threads have tried to follow this 795 * function pointer yet. 796 */ 797 mutex_enter(&fasttrap_count_mtx); 798 if (*count == 0) { 799 ASSERT(*fptr == NULL); 800 *fptr = f; 801 if (fptr2 != NULL) 802 *fptr2 = f2; 803 } 804 ASSERT(*fptr == f); 805 ASSERT(fptr2 == NULL || *fptr2 == f2); 806 (*count)++; 807 mutex_exit(&fasttrap_count_mtx); 808 } 809 810 static void 811 fasttrap_disable_common(int *count, fasttrap_probe_f **fptr, 812 fasttrap_probe_f **fptr2) 813 { 814 ASSERT(MUTEX_HELD(&cpu_lock)); 815 816 mutex_enter(&fasttrap_count_mtx); 817 (*count)--; 818 ASSERT(*count >= 0); 819 if (*count == 0) { 820 cpu_t *cur, *cpu = CPU; 821 822 for (cur = cpu->cpu_next_onln; cur != cpu; 823 cur = cur->cpu_next_onln) { 824 rw_enter(&cur->cpu_ft_lock, RW_WRITER); 825 } 826 827 *fptr = NULL; 828 if (fptr2 != NULL) 829 *fptr2 = NULL; 830 831 for (cur = cpu->cpu_next_onln; cur != cpu; 832 cur = cur->cpu_next_onln) { 833 rw_exit(&cur->cpu_ft_lock); 834 } 835 } 836 mutex_exit(&fasttrap_count_mtx); 837 } 838 839 /*ARGSUSED*/ 840 static void 841 fasttrap_enable(void *arg, dtrace_id_t id, void *parg) 842 { 843 /* 844 * Enable the probe that corresponds to statically placed trace 845 * points which have not explicitly been placed in the process's text 846 * by the fasttrap provider. 847 */ 848 ASSERT(arg == NULL); 849 ASSERT(id == fasttrap_probe_id); 850 851 fasttrap_enable_common(&fasttrap_count, 852 &dtrace_fasttrap_probe_ptr, fasttrap_probe, NULL, NULL); 853 } 854 855 856 /*ARGSUSED*/ 857 static void 858 fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 859 { 860 fasttrap_probe_t *probe = parg; 861 proc_t *p; 862 int i, rc; 863 864 ASSERT(probe != NULL); 865 ASSERT(!probe->ftp_enabled); 866 ASSERT(id == probe->ftp_id); 867 ASSERT(MUTEX_HELD(&cpu_lock)); 868 869 /* 870 * Increment the count of enabled probes on this probe's provider; 871 * the provider can't go away while the probe still exists. We 872 * must increment this even if we aren't able to properly enable 873 * this probe. 874 */ 875 mutex_enter(&probe->ftp_prov->ftp_mtx); 876 probe->ftp_prov->ftp_rcount++; 877 mutex_exit(&probe->ftp_prov->ftp_mtx); 878 879 /* 880 * Bail out if we can't find the process for this probe or its 881 * provider is retired (meaning it was valid in a previously exec'ed 882 * incarnation of this address space). The provider can't go away 883 * while we're in this code path. 884 */ 885 if (probe->ftp_prov->ftp_retired || 886 (p = sprlock(probe->ftp_pid)) == NULL) 887 return; 888 889 ASSERT(!(p->p_flag & SVFORK)); 890 mutex_exit(&p->p_lock); 891 892 /* 893 * We have to enable the trap entry before any user threads have 894 * the chance to execute the trap instruction we're about to place 895 * in their process's text. 896 */ 897 fasttrap_enable_common(&fasttrap_pid_count, 898 &dtrace_pid_probe_ptr, fasttrap_pid_probe, 899 &dtrace_return_probe_ptr, fasttrap_return_probe); 900 901 /* 902 * Enable all the tracepoints and add this probe's id to each 903 * tracepoint's list of active probes. 904 */ 905 for (i = 0; i < probe->ftp_ntps; i++) { 906 if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 907 /* 908 * If enabling the tracepoint failed completely, 909 * we don't have to disable it; if the failure 910 * was only partial we must disable it. 911 */ 912 if (rc == FASTTRAP_ENABLE_FAIL) 913 i--; 914 else 915 ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 916 917 /* 918 * Back up and pull out all the tracepoints we've 919 * created so far for this probe. 920 */ 921 while (i-- >= 0) { 922 fasttrap_tracepoint_disable(p, probe, i); 923 } 924 925 mutex_enter(&p->p_lock); 926 sprunlock(p); 927 928 /* 929 * Since we're not actually enabling this probe, 930 * drop our reference on the trap table entry. 931 */ 932 fasttrap_disable_common(&fasttrap_pid_count, 933 &dtrace_pid_probe_ptr, &dtrace_return_probe_ptr); 934 return; 935 } 936 } 937 938 mutex_enter(&p->p_lock); 939 sprunlock(p); 940 941 probe->ftp_enabled = 1; 942 } 943 944 945 /*ARGSUSED*/ 946 static void 947 fasttrap_disable(void *arg, dtrace_id_t id, void *parg) 948 { 949 /* 950 * Disable the probe the corresponds to statically placed trace 951 * points. 952 */ 953 ASSERT(arg == NULL); 954 ASSERT(id == fasttrap_probe_id); 955 ASSERT(MUTEX_HELD(&cpu_lock)); 956 fasttrap_disable_common(&fasttrap_count, &dtrace_fasttrap_probe_ptr, 957 NULL); 958 } 959 960 /*ARGSUSED*/ 961 static void 962 fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 963 { 964 fasttrap_probe_t *probe = parg; 965 fasttrap_provider_t *provider = probe->ftp_prov; 966 proc_t *p; 967 int i, whack = 0; 968 969 if (!probe->ftp_enabled) { 970 mutex_enter(&provider->ftp_mtx); 971 provider->ftp_rcount--; 972 ASSERT(provider->ftp_rcount >= 0); 973 mutex_exit(&provider->ftp_mtx); 974 return; 975 } 976 977 ASSERT(id == probe->ftp_id); 978 979 /* 980 * We won't be able to acquire a /proc-esque lock on the process 981 * iff the process is dead and gone. In this case, we rely on the 982 * provider lock as a point of mutual exclusion to prevent other 983 * DTrace consumers from disabling this probe. 984 */ 985 if ((p = sprlock(probe->ftp_pid)) != NULL) { 986 ASSERT(!(p->p_flag & SVFORK)); 987 mutex_exit(&p->p_lock); 988 } 989 990 mutex_enter(&provider->ftp_mtx); 991 992 /* 993 * Disable all the associated tracepoints. 994 */ 995 for (i = 0; i < probe->ftp_ntps; i++) { 996 fasttrap_tracepoint_disable(p, probe, i); 997 } 998 999 ASSERT(provider->ftp_rcount > 0); 1000 provider->ftp_rcount--; 1001 1002 if (p != NULL) { 1003 /* 1004 * Even though we may not be able to remove it entirely, we 1005 * mark this retired provider to get a chance to remove some 1006 * of the associated probes. 1007 */ 1008 if (provider->ftp_retired && !provider->ftp_marked) 1009 whack = provider->ftp_marked = 1; 1010 mutex_exit(&provider->ftp_mtx); 1011 1012 mutex_enter(&p->p_lock); 1013 sprunlock(p); 1014 } else { 1015 /* 1016 * If the process is dead, we're just waiting for the 1017 * last probe to be disabled to be able to free it. 1018 */ 1019 if (provider->ftp_rcount == 0 && !provider->ftp_marked) 1020 whack = provider->ftp_marked = 1; 1021 mutex_exit(&provider->ftp_mtx); 1022 } 1023 1024 if (whack) 1025 fasttrap_pid_cleanup(); 1026 1027 probe->ftp_enabled = 0; 1028 1029 ASSERT(MUTEX_HELD(&cpu_lock)); 1030 fasttrap_disable_common(&fasttrap_pid_count, &dtrace_pid_probe_ptr, 1031 &dtrace_return_probe_ptr); 1032 } 1033 1034 /*ARGSUSED*/ 1035 static void 1036 fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 1037 dtrace_argdesc_t *desc) 1038 { 1039 fasttrap_probe_t *probe = parg; 1040 char *str; 1041 int i; 1042 1043 desc->dtargd_native[0] = '\0'; 1044 desc->dtargd_xlate[0] = '\0'; 1045 1046 if (probe->ftp_prov->ftp_retired != 0 || 1047 desc->dtargd_ndx >= probe->ftp_nargs) { 1048 desc->dtargd_ndx = DTRACE_ARGNONE; 1049 return; 1050 } 1051 1052 /* 1053 * We only need to set this member if the argument is remapped. 1054 */ 1055 if (probe->ftp_argmap != NULL) 1056 desc->dtargd_mapping = probe->ftp_argmap[desc->dtargd_ndx]; 1057 1058 str = probe->ftp_ntypes; 1059 for (i = 0; i < desc->dtargd_mapping; i++) { 1060 str += strlen(str) + 1; 1061 } 1062 1063 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 1064 (void) strcpy(desc->dtargd_native, str); 1065 1066 if (probe->ftp_xtypes == NULL) 1067 return; 1068 1069 str = probe->ftp_xtypes; 1070 for (i = 0; i < desc->dtargd_ndx; i++) { 1071 str += strlen(str) + 1; 1072 } 1073 1074 ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 1075 (void) strcpy(desc->dtargd_xlate, str); 1076 } 1077 1078 /*ARGSUSED*/ 1079 static void 1080 fasttrap_destroy(void *arg, dtrace_id_t id, void *parg) 1081 { 1082 ASSERT(arg == NULL); 1083 ASSERT(id == fasttrap_probe_id); 1084 } 1085 1086 /*ARGSUSED*/ 1087 static void 1088 fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 1089 { 1090 fasttrap_probe_t *probe = parg; 1091 int i; 1092 size_t size; 1093 1094 ASSERT(probe != NULL); 1095 ASSERT(!probe->ftp_enabled); 1096 ASSERT(fasttrap_total >= probe->ftp_ntps); 1097 1098 atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 1099 size = sizeof (fasttrap_probe_t) + 1100 sizeof (probe->ftp_tps[0]) * (probe->ftp_ntps - 1); 1101 1102 if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 1103 fasttrap_mod_barrier(probe->ftp_gen); 1104 1105 for (i = 0; i < probe->ftp_ntps; i++) { 1106 kmem_free(probe->ftp_tps[i].fit_tp, 1107 sizeof (fasttrap_tracepoint_t)); 1108 } 1109 1110 kmem_free(probe, size); 1111 } 1112 1113 1114 static const dtrace_pattr_t fasttrap_attr = { 1115 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1116 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1117 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1118 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1119 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1120 }; 1121 1122 static dtrace_pops_t fasttrap_pops = { 1123 fasttrap_provide, 1124 NULL, 1125 fasttrap_enable, 1126 fasttrap_disable, 1127 NULL, 1128 NULL, 1129 NULL, 1130 fasttrap_getarg, 1131 NULL, 1132 fasttrap_destroy 1133 }; 1134 1135 static const dtrace_pattr_t pid_attr = { 1136 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1137 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1138 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1139 { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1140 { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1141 }; 1142 1143 static dtrace_pops_t pid_pops = { 1144 fasttrap_pid_provide, 1145 NULL, 1146 fasttrap_pid_enable, 1147 fasttrap_pid_disable, 1148 NULL, 1149 NULL, 1150 fasttrap_pid_getargdesc, 1151 fasttrap_getarg, 1152 NULL, 1153 fasttrap_pid_destroy 1154 }; 1155 1156 static dtrace_pops_t usdt_pops = { 1157 fasttrap_pid_provide, 1158 NULL, 1159 fasttrap_pid_enable, 1160 fasttrap_pid_disable, 1161 NULL, 1162 NULL, 1163 fasttrap_pid_getargdesc, 1164 fasttrap_usdt_getarg, 1165 NULL, 1166 fasttrap_pid_destroy 1167 }; 1168 1169 static fasttrap_proc_t * 1170 fasttrap_proc_lookup(pid_t pid) 1171 { 1172 fasttrap_bucket_t *bucket; 1173 fasttrap_proc_t *fprc, *new_fprc; 1174 1175 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1176 mutex_enter(&bucket->ftb_mtx); 1177 1178 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1179 if (fprc->ftpc_pid == pid && !fprc->ftpc_defunct) { 1180 mutex_enter(&fprc->ftpc_mtx); 1181 mutex_exit(&bucket->ftb_mtx); 1182 fprc->ftpc_count++; 1183 mutex_exit(&fprc->ftpc_mtx); 1184 1185 return (fprc); 1186 } 1187 } 1188 1189 /* 1190 * Drop the bucket lock so we don't try to perform a sleeping 1191 * allocation under it. 1192 */ 1193 mutex_exit(&bucket->ftb_mtx); 1194 1195 new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP); 1196 new_fprc->ftpc_pid = pid; 1197 new_fprc->ftpc_count = 1; 1198 1199 mutex_enter(&bucket->ftb_mtx); 1200 1201 /* 1202 * Take another lap through the list to make sure a proc hasn't 1203 * been created for this pid while we weren't under the bucket lock. 1204 */ 1205 for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1206 if (fprc->ftpc_pid == pid && !fprc->ftpc_defunct) { 1207 mutex_enter(&fprc->ftpc_mtx); 1208 mutex_exit(&bucket->ftb_mtx); 1209 fprc->ftpc_count++; 1210 mutex_exit(&fprc->ftpc_mtx); 1211 1212 kmem_free(new_fprc, sizeof (fasttrap_proc_t)); 1213 1214 return (fprc); 1215 } 1216 } 1217 1218 new_fprc->ftpc_next = bucket->ftb_data; 1219 bucket->ftb_data = new_fprc; 1220 1221 mutex_exit(&bucket->ftb_mtx); 1222 1223 return (new_fprc); 1224 } 1225 1226 static void 1227 fasttrap_proc_release(fasttrap_proc_t *proc) 1228 { 1229 fasttrap_bucket_t *bucket; 1230 fasttrap_proc_t *fprc, **fprcp; 1231 pid_t pid = proc->ftpc_pid; 1232 1233 mutex_enter(&proc->ftpc_mtx); 1234 1235 ASSERT(proc->ftpc_count != 0); 1236 1237 if (--proc->ftpc_count != 0) { 1238 mutex_exit(&proc->ftpc_mtx); 1239 return; 1240 } 1241 1242 mutex_exit(&proc->ftpc_mtx); 1243 1244 bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1245 mutex_enter(&bucket->ftb_mtx); 1246 1247 fprcp = (fasttrap_proc_t **)&bucket->ftb_data; 1248 while ((fprc = *fprcp) != NULL) { 1249 if (fprc == proc) 1250 break; 1251 1252 fprcp = &fprc->ftpc_next; 1253 } 1254 1255 /* 1256 * Something strange has happened if we can't find the proc. 1257 */ 1258 ASSERT(fprc != NULL); 1259 1260 *fprcp = fprc->ftpc_next; 1261 1262 mutex_exit(&bucket->ftb_mtx); 1263 1264 kmem_free(fprc, sizeof (fasttrap_proc_t)); 1265 } 1266 1267 /* 1268 * Lookup a fasttrap-managed provider based on its name and associated pid. 1269 * If the pattr argument is non-NULL, this function instantiates the provider 1270 * if it doesn't exist otherwise it returns NULL. The provider is returned 1271 * with its lock held. 1272 */ 1273 static fasttrap_provider_t * 1274 fasttrap_provider_lookup(pid_t pid, const char *name, 1275 const dtrace_pattr_t *pattr) 1276 { 1277 fasttrap_provider_t *fp, *new_fp = NULL; 1278 fasttrap_bucket_t *bucket; 1279 char provname[DTRACE_PROVNAMELEN]; 1280 proc_t *p; 1281 uid_t uid = (uid_t)-1; 1282 1283 ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1284 1285 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1286 mutex_enter(&bucket->ftb_mtx); 1287 1288 /* 1289 * Take a lap through the list and return the match if we find it. 1290 */ 1291 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1292 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1293 !fp->ftp_retired) { 1294 mutex_enter(&fp->ftp_mtx); 1295 mutex_exit(&bucket->ftb_mtx); 1296 return (fp); 1297 } 1298 } 1299 1300 /* 1301 * Drop the bucket lock so we don't try to perform a sleeping 1302 * allocation under it. 1303 */ 1304 mutex_exit(&bucket->ftb_mtx); 1305 1306 /* 1307 * If we didn't want to create a new provider, just return failure. 1308 */ 1309 if (pattr == NULL) 1310 return (NULL); 1311 1312 /* 1313 * Make sure the process exists, isn't a child created as the result 1314 * of a vfork(2), and isn't a zombie (but may be in fork). Record the 1315 * process's uid to pass to dtrace_register(). 1316 */ 1317 mutex_enter(&pidlock); 1318 if ((p = prfind(pid)) == NULL) { 1319 mutex_exit(&pidlock); 1320 return (NULL); 1321 } 1322 mutex_enter(&p->p_lock); 1323 mutex_exit(&pidlock); 1324 if (p->p_flag & (SVFORK | SEXITING)) { 1325 mutex_exit(&p->p_lock); 1326 return (NULL); 1327 } 1328 1329 /* 1330 * Increment p_dtrace_probes so that the process knows to inform us 1331 * when it exits or execs. fasttrap_provider_free() decrements this 1332 * when we're done with this provider. 1333 */ 1334 p->p_dtrace_probes++; 1335 1336 mutex_enter(&p->p_crlock); 1337 uid = crgetruid(p->p_cred); 1338 mutex_exit(&p->p_crlock); 1339 mutex_exit(&p->p_lock); 1340 1341 new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1342 new_fp->ftp_pid = pid; 1343 new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1344 1345 ASSERT(new_fp->ftp_proc != NULL); 1346 1347 mutex_enter(&bucket->ftb_mtx); 1348 1349 /* 1350 * Take another lap through the list to make sure a provider hasn't 1351 * been created for this pid while we weren't under the bucket lock. 1352 */ 1353 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1354 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1355 !fp->ftp_retired) { 1356 mutex_enter(&fp->ftp_mtx); 1357 mutex_exit(&bucket->ftb_mtx); 1358 fasttrap_provider_free(new_fp); 1359 return (fp); 1360 } 1361 } 1362 1363 (void) strcpy(new_fp->ftp_name, name); 1364 1365 /* 1366 * Fail and return NULL if either the provider name is too long 1367 * or we fail to register this new provider with the DTrace 1368 * framework. Note that this is the only place we ever construct 1369 * the full provider name -- we keep it in pieces in the provider 1370 * structure. 1371 */ 1372 if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 1373 sizeof (provname) || 1374 dtrace_register(provname, pattr, 1375 DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER, uid, 1376 pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 1377 &new_fp->ftp_provid) != 0) { 1378 mutex_exit(&bucket->ftb_mtx); 1379 fasttrap_provider_free(new_fp); 1380 return (NULL); 1381 } 1382 1383 new_fp->ftp_next = bucket->ftb_data; 1384 bucket->ftb_data = new_fp; 1385 1386 mutex_enter(&new_fp->ftp_mtx); 1387 mutex_exit(&bucket->ftb_mtx); 1388 1389 return (new_fp); 1390 } 1391 1392 static void 1393 fasttrap_provider_free(fasttrap_provider_t *provider) 1394 { 1395 pid_t pid = provider->ftp_pid; 1396 proc_t *p; 1397 1398 /* 1399 * There need to be no consumers using this provider and no 1400 * associated enabled probes. 1401 */ 1402 ASSERT(provider->ftp_ccount == 0); 1403 ASSERT(provider->ftp_rcount == 0); 1404 1405 fasttrap_proc_release(provider->ftp_proc); 1406 1407 kmem_free(provider, sizeof (fasttrap_provider_t)); 1408 1409 /* 1410 * Decrement p_dtrace_probes on the process whose provider we're 1411 * freeing. We don't have to worry about clobbering somone else's 1412 * modifications to it because we have locked the bucket that 1413 * corresponds to this process's hash chain in the provider hash 1414 * table. Don't sweat it if we can't find the process. 1415 */ 1416 mutex_enter(&pidlock); 1417 if ((p = prfind(pid)) == NULL) { 1418 mutex_exit(&pidlock); 1419 return; 1420 } 1421 1422 mutex_enter(&p->p_lock); 1423 mutex_exit(&pidlock); 1424 1425 p->p_dtrace_probes--; 1426 mutex_exit(&p->p_lock); 1427 } 1428 1429 static void 1430 fasttrap_provider_retire(fasttrap_provider_t *provider) 1431 { 1432 dtrace_provider_id_t provid = provider->ftp_provid; 1433 1434 /* 1435 * Mark the provider to be removed in our post-processing step, 1436 * mark it retired, and mark its proc as defunct (though it may 1437 * already be marked defunct by another provider that shares the 1438 * same proc). Marking it indicates that we should try to remove it; 1439 * setting the retired flag indicates that we're done with this 1440 * provider; setting the proc to be defunct indicates that all 1441 * tracepoints associated with the traced process should be ignored. 1442 */ 1443 provider->ftp_proc->ftpc_defunct = 1; 1444 provider->ftp_retired = 1; 1445 provider->ftp_marked = 1; 1446 mutex_exit(&provider->ftp_mtx); 1447 1448 /* 1449 * We don't have to worry about invalidating the same provider twice 1450 * since fasttrap_provider_lookup() will ignore provider that have 1451 * been marked as retired. 1452 */ 1453 dtrace_invalidate(provid); 1454 1455 fasttrap_pid_cleanup(); 1456 } 1457 1458 static int 1459 fasttrap_add_probe(fasttrap_probe_spec_t *pdata) 1460 { 1461 fasttrap_provider_t *provider; 1462 fasttrap_probe_t *pp; 1463 fasttrap_tracepoint_t *tp; 1464 char *name; 1465 size_t size; 1466 int i, aframes, whack; 1467 1468 switch (pdata->ftps_type) { 1469 case DTFTP_ENTRY: 1470 name = "entry"; 1471 aframes = FASTTRAP_ENTRY_AFRAMES; 1472 break; 1473 case DTFTP_RETURN: 1474 name = "return"; 1475 aframes = FASTTRAP_RETURN_AFRAMES; 1476 break; 1477 case DTFTP_OFFSETS: 1478 name = NULL; 1479 break; 1480 default: 1481 return (EINVAL); 1482 } 1483 1484 if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 1485 FASTTRAP_PID_NAME, &pid_attr)) == NULL) 1486 return (ESRCH); 1487 1488 /* 1489 * Increment this reference count to indicate that a consumer is 1490 * actively adding a new probe associated with this provider. 1491 */ 1492 provider->ftp_ccount++; 1493 mutex_exit(&provider->ftp_mtx); 1494 1495 if (name != NULL) { 1496 if (dtrace_probe_lookup(provider->ftp_provid, 1497 pdata->ftps_mod, pdata->ftps_func, name) != 0) 1498 goto done; 1499 1500 atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 1501 1502 if (fasttrap_total > fasttrap_max) { 1503 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1504 goto no_mem; 1505 } 1506 1507 ASSERT(pdata->ftps_noffs > 0); 1508 size = sizeof (fasttrap_probe_t) + 1509 sizeof (pp->ftp_tps[0]) * (pdata->ftps_noffs - 1); 1510 1511 pp = kmem_zalloc(size, KM_SLEEP); 1512 1513 pp->ftp_prov = provider; 1514 pp->ftp_faddr = pdata->ftps_pc; 1515 pp->ftp_fsize = pdata->ftps_size; 1516 pp->ftp_pid = pdata->ftps_pid; 1517 pp->ftp_ntps = pdata->ftps_noffs; 1518 pp->ftp_type = pdata->ftps_type; 1519 1520 for (i = 0; i < pdata->ftps_noffs; i++) { 1521 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1522 KM_SLEEP); 1523 1524 tp->ftt_proc = provider->ftp_proc; 1525 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1526 tp->ftt_pid = pdata->ftps_pid; 1527 1528 pp->ftp_tps[i].fit_tp = tp; 1529 pp->ftp_tps[i].fit_id.fti_probe = pp; 1530 } 1531 1532 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1533 pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 1534 } else { 1535 for (i = 0; i < pdata->ftps_noffs; i++) { 1536 char name_str[17]; 1537 1538 (void) sprintf(name_str, "%llx", 1539 (unsigned long long)pdata->ftps_offs[i]); 1540 1541 if (dtrace_probe_lookup(provider->ftp_provid, 1542 pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 1543 continue; 1544 1545 atomic_add_32(&fasttrap_total, 1); 1546 1547 if (fasttrap_total > fasttrap_max) { 1548 atomic_add_32(&fasttrap_total, -1); 1549 goto no_mem; 1550 } 1551 1552 pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 1553 1554 pp->ftp_prov = provider; 1555 pp->ftp_faddr = pdata->ftps_pc; 1556 pp->ftp_fsize = pdata->ftps_size; 1557 pp->ftp_pid = pdata->ftps_pid; 1558 pp->ftp_ntps = 1; 1559 pp->ftp_type = pdata->ftps_type; 1560 1561 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1562 KM_SLEEP); 1563 1564 tp->ftt_proc = provider->ftp_proc; 1565 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1566 tp->ftt_pid = pdata->ftps_pid; 1567 1568 pp->ftp_tps[0].fit_tp = tp; 1569 pp->ftp_tps[0].fit_id.fti_probe = pp; 1570 1571 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1572 pdata->ftps_mod, pdata->ftps_func, name_str, 1573 FASTTRAP_OFFSET_AFRAMES, pp); 1574 } 1575 } 1576 1577 done: 1578 /* 1579 * We know that the provider is still valid since we incremented the 1580 * reference count. If someone tried to free this provider while we 1581 * were using it (e.g. because the process called exec(2) or exit(2)), 1582 * take note of that and try to free it now. 1583 */ 1584 mutex_enter(&provider->ftp_mtx); 1585 provider->ftp_ccount--; 1586 whack = provider->ftp_retired; 1587 mutex_exit(&provider->ftp_mtx); 1588 1589 if (whack) 1590 fasttrap_pid_cleanup(); 1591 1592 return (0); 1593 1594 no_mem: 1595 /* 1596 * If we've exhausted the allowable resources, we'll try to remove 1597 * this provider to free some up. This is to cover the case where 1598 * the user has accidentally created many more probes than was 1599 * intended (e.g. pid123:::). 1600 */ 1601 mutex_enter(&provider->ftp_mtx); 1602 provider->ftp_ccount--; 1603 provider->ftp_marked = 1; 1604 mutex_exit(&provider->ftp_mtx); 1605 1606 fasttrap_pid_cleanup(); 1607 1608 return (ENOMEM); 1609 } 1610 1611 /*ARGSUSED*/ 1612 static void * 1613 fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 1614 { 1615 fasttrap_provider_t *provider; 1616 1617 /* 1618 * A 32-bit unsigned integer (like a pid for example) can be 1619 * expressed in 10 or fewer decimal digits. Make sure that we'll 1620 * have enough space for the provider name. 1621 */ 1622 if (strlen(dhpv->dthpv_provname) + 10 >= 1623 sizeof (provider->ftp_name)) { 1624 cmn_err(CE_WARN, "failed to instantiate provider %s: " 1625 "name too long to accomodate pid", dhpv->dthpv_provname); 1626 return (NULL); 1627 } 1628 1629 /* 1630 * Don't let folks spoof the true pid provider. 1631 */ 1632 if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 1633 cmn_err(CE_WARN, "failed to instantiate provider %s: " 1634 "%s is an invalid name", dhpv->dthpv_provname, 1635 FASTTRAP_PID_NAME); 1636 return (NULL); 1637 } 1638 1639 /* 1640 * The highest stability class that fasttrap supports is ISA; cap 1641 * the stability of the new provider accordingly. 1642 */ 1643 if (dhpv->dthpv_pattr.dtpa_provider.dtat_class >= DTRACE_CLASS_COMMON) 1644 dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 1645 if (dhpv->dthpv_pattr.dtpa_mod.dtat_class >= DTRACE_CLASS_COMMON) 1646 dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 1647 if (dhpv->dthpv_pattr.dtpa_func.dtat_class >= DTRACE_CLASS_COMMON) 1648 dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 1649 if (dhpv->dthpv_pattr.dtpa_name.dtat_class >= DTRACE_CLASS_COMMON) 1650 dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 1651 if (dhpv->dthpv_pattr.dtpa_args.dtat_class >= DTRACE_CLASS_COMMON) 1652 dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 1653 1654 if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 1655 &dhpv->dthpv_pattr)) == NULL) { 1656 cmn_err(CE_WARN, "failed to instantiate provider %s for " 1657 "process %u", dhpv->dthpv_provname, (uint_t)pid); 1658 return (NULL); 1659 } 1660 1661 /* 1662 * We elevate the consumer count here to ensure that this provider 1663 * isn't removed until after the meta provider has been told to 1664 * remove it. 1665 */ 1666 provider->ftp_ccount++; 1667 1668 mutex_exit(&provider->ftp_mtx); 1669 1670 return (provider); 1671 } 1672 1673 /*ARGSUSED*/ 1674 static void 1675 fasttrap_meta_create_probe(void *arg, void *parg, 1676 dtrace_helper_probedesc_t *dhpb) 1677 { 1678 fasttrap_provider_t *provider = parg; 1679 fasttrap_probe_t *pp; 1680 fasttrap_tracepoint_t *tp; 1681 size_t size; 1682 int i; 1683 1684 mutex_enter(&provider->ftp_mtx); 1685 1686 if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 1687 dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 1688 mutex_exit(&provider->ftp_mtx); 1689 return; 1690 } 1691 1692 atomic_add_32(&fasttrap_total, dhpb->dthpb_noffs); 1693 1694 if (fasttrap_total > fasttrap_max) { 1695 atomic_add_32(&fasttrap_total, -dhpb->dthpb_noffs); 1696 mutex_exit(&provider->ftp_mtx); 1697 return; 1698 } 1699 1700 size = sizeof (fasttrap_probe_t) + 1701 sizeof (pp->ftp_tps[0]) * (dhpb->dthpb_noffs - 1); 1702 pp = kmem_zalloc(size, KM_SLEEP); 1703 1704 pp->ftp_prov = provider; 1705 pp->ftp_pid = provider->ftp_pid; 1706 pp->ftp_ntps = dhpb->dthpb_noffs; 1707 #ifdef __sparc 1708 pp->ftp_type = DTFTP_POST_OFFSETS; 1709 #else 1710 pp->ftp_type = DTFTP_OFFSETS; 1711 #endif 1712 pp->ftp_nargs = dhpb->dthpb_xargc; 1713 pp->ftp_xtypes = dhpb->dthpb_xtypes; 1714 pp->ftp_ntypes = dhpb->dthpb_ntypes; 1715 1716 for (i = 0; i < pp->ftp_ntps; i++) { 1717 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 1718 1719 tp->ftt_proc = provider->ftp_proc; 1720 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 1721 tp->ftt_pid = provider->ftp_pid; 1722 1723 pp->ftp_tps[i].fit_tp = tp; 1724 pp->ftp_tps[i].fit_id.fti_probe = pp; 1725 } 1726 1727 /* 1728 * If the arguments are shuffled around we set the argument remapping 1729 * table. Later, when the probe fires, we only remap the arguments 1730 * if the table is non-NULL. 1731 */ 1732 for (i = 0; i < dhpb->dthpb_xargc; i++) { 1733 if (dhpb->dthpb_args[i] != i) { 1734 pp->ftp_argmap = dhpb->dthpb_args; 1735 break; 1736 } 1737 } 1738 1739 /* 1740 * The probe is fully constructed -- register it with DTrace. 1741 */ 1742 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 1743 dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 1744 1745 mutex_exit(&provider->ftp_mtx); 1746 } 1747 1748 /*ARGSUSED*/ 1749 static void 1750 fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 1751 { 1752 fasttrap_provider_t *provider; 1753 1754 if ((provider = fasttrap_provider_lookup(pid, 1755 dhpv->dthpv_provname, NULL)) != NULL) { 1756 /* 1757 * Drop the consumer count now that we're done with this 1758 * provider. If there are no other consumers retire it now. 1759 */ 1760 if (--provider->ftp_ccount == 0) 1761 fasttrap_provider_retire(provider); 1762 else 1763 mutex_exit(&provider->ftp_mtx); 1764 } 1765 } 1766 1767 static dtrace_mops_t fasttrap_mops = { 1768 fasttrap_meta_create_probe, 1769 fasttrap_meta_provide, 1770 fasttrap_meta_remove 1771 }; 1772 1773 /*ARGSUSED*/ 1774 static int 1775 fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 1776 { 1777 return (0); 1778 } 1779 1780 /*ARGSUSED*/ 1781 static int 1782 fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 1783 { 1784 if (!dtrace_attached()) 1785 return (EAGAIN); 1786 1787 if (cmd == FASTTRAPIOC_MAKEPROBE) { 1788 fasttrap_probe_spec_t *uprobe = (void *)arg; 1789 fasttrap_probe_spec_t *probe; 1790 uint64_t noffs; 1791 size_t size; 1792 int ret; 1793 char *c; 1794 1795 if (copyin(&uprobe->ftps_noffs, &noffs, 1796 sizeof (uprobe->ftps_noffs))) 1797 return (EFAULT); 1798 1799 /* 1800 * Probes must have at least one tracepoint. 1801 */ 1802 if (noffs == 0) 1803 return (EINVAL); 1804 1805 size = sizeof (fasttrap_probe_spec_t) + 1806 sizeof (probe->ftps_offs[0]) * (noffs - 1); 1807 1808 if (size > 1024 * 1024) 1809 return (ENOMEM); 1810 1811 probe = kmem_alloc(size, KM_SLEEP); 1812 1813 if (copyin(uprobe, probe, size) != 0) { 1814 kmem_free(probe, size); 1815 return (EFAULT); 1816 } 1817 1818 /* 1819 * Verify that the function and module strings contain no 1820 * funny characters. 1821 */ 1822 for (c = &probe->ftps_func[0]; *c != '\0'; c++) { 1823 if (*c < 0x20 || 0x7f <= *c) { 1824 ret = EINVAL; 1825 goto err; 1826 } 1827 } 1828 1829 for (c = &probe->ftps_mod[0]; *c != '\0'; c++) { 1830 if (*c < 0x20 || 0x7f <= *c) { 1831 ret = EINVAL; 1832 goto err; 1833 } 1834 } 1835 1836 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 1837 proc_t *p; 1838 pid_t pid = probe->ftps_pid; 1839 1840 mutex_enter(&pidlock); 1841 /* 1842 * Report an error if the process doesn't exist 1843 * or is actively being birthed. 1844 */ 1845 if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 1846 mutex_exit(&pidlock); 1847 return (ESRCH); 1848 } 1849 mutex_enter(&p->p_lock); 1850 mutex_exit(&pidlock); 1851 1852 if ((ret = priv_proc_cred_perm(cr, p, NULL, 1853 VREAD | VWRITE)) != 0) { 1854 mutex_exit(&p->p_lock); 1855 return (ret); 1856 } 1857 1858 mutex_exit(&p->p_lock); 1859 } 1860 1861 ret = fasttrap_add_probe(probe); 1862 err: 1863 kmem_free(probe, size); 1864 1865 return (ret); 1866 1867 } else if (cmd == FASTTRAPIOC_GETINSTR) { 1868 fasttrap_instr_query_t instr; 1869 fasttrap_tracepoint_t *tp; 1870 uint_t index; 1871 int ret; 1872 1873 if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 1874 return (EFAULT); 1875 1876 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 1877 proc_t *p; 1878 pid_t pid = instr.ftiq_pid; 1879 1880 mutex_enter(&pidlock); 1881 /* 1882 * Report an error if the process doesn't exist 1883 * or is actively being birthed. 1884 */ 1885 if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 1886 mutex_exit(&pidlock); 1887 return (ESRCH); 1888 } 1889 mutex_enter(&p->p_lock); 1890 mutex_exit(&pidlock); 1891 1892 if ((ret = priv_proc_cred_perm(cr, p, NULL, 1893 VREAD)) != 0) { 1894 mutex_exit(&p->p_lock); 1895 return (ret); 1896 } 1897 1898 mutex_exit(&p->p_lock); 1899 } 1900 1901 index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 1902 1903 mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1904 tp = fasttrap_tpoints.fth_table[index].ftb_data; 1905 while (tp != NULL) { 1906 if (instr.ftiq_pid == tp->ftt_pid && 1907 instr.ftiq_pc == tp->ftt_pc && 1908 !tp->ftt_proc->ftpc_defunct) 1909 break; 1910 1911 tp = tp->ftt_next; 1912 } 1913 1914 if (tp == NULL) { 1915 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1916 return (ENOENT); 1917 } 1918 1919 bcopy(&tp->ftt_instr, &instr.ftiq_instr, 1920 sizeof (instr.ftiq_instr)); 1921 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1922 1923 if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 1924 return (EFAULT); 1925 1926 return (0); 1927 } 1928 1929 return (EINVAL); 1930 } 1931 1932 static struct cb_ops fasttrap_cb_ops = { 1933 fasttrap_open, /* open */ 1934 nodev, /* close */ 1935 nulldev, /* strategy */ 1936 nulldev, /* print */ 1937 nodev, /* dump */ 1938 nodev, /* read */ 1939 nodev, /* write */ 1940 fasttrap_ioctl, /* ioctl */ 1941 nodev, /* devmap */ 1942 nodev, /* mmap */ 1943 nodev, /* segmap */ 1944 nochpoll, /* poll */ 1945 ddi_prop_op, /* cb_prop_op */ 1946 0, /* streamtab */ 1947 D_NEW | D_MP /* Driver compatibility flag */ 1948 }; 1949 1950 /*ARGSUSED*/ 1951 static int 1952 fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1953 { 1954 int error; 1955 1956 switch (infocmd) { 1957 case DDI_INFO_DEVT2DEVINFO: 1958 *result = (void *)fasttrap_devi; 1959 error = DDI_SUCCESS; 1960 break; 1961 case DDI_INFO_DEVT2INSTANCE: 1962 *result = (void *)0; 1963 error = DDI_SUCCESS; 1964 break; 1965 default: 1966 error = DDI_FAILURE; 1967 } 1968 return (error); 1969 } 1970 1971 static int 1972 fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1973 { 1974 ulong_t nent; 1975 1976 switch (cmd) { 1977 case DDI_ATTACH: 1978 break; 1979 case DDI_RESUME: 1980 return (DDI_SUCCESS); 1981 default: 1982 return (DDI_FAILURE); 1983 } 1984 1985 if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0, 1986 DDI_PSEUDO, NULL) == DDI_FAILURE || 1987 dtrace_register("fasttrap", &fasttrap_attr, DTRACE_PRIV_USER, 0, 1988 &fasttrap_pops, NULL, &fasttrap_id) != 0) { 1989 ddi_remove_minor_node(devi, NULL); 1990 return (DDI_FAILURE); 1991 } 1992 1993 ddi_report_dev(devi); 1994 fasttrap_devi = devi; 1995 1996 /* 1997 * Install our hooks into fork(2), exec(2), and exit(2). 1998 */ 1999 dtrace_fasttrap_fork_ptr = &fasttrap_fork; 2000 dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit; 2001 dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit; 2002 2003 fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2004 "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 2005 fasttrap_total = 0; 2006 2007 /* 2008 * Conjure up the tracepoints hashtable... 2009 */ 2010 nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2011 "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 2012 2013 if (nent <= 0 || nent > 0x1000000) 2014 nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2015 2016 if ((nent & (nent - 1)) == 0) 2017 fasttrap_tpoints.fth_nent = nent; 2018 else 2019 fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 2020 ASSERT(fasttrap_tpoints.fth_nent > 0); 2021 fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 2022 fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 2023 sizeof (fasttrap_bucket_t), KM_SLEEP); 2024 2025 /* 2026 * ... and the providers hash table... 2027 */ 2028 nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 2029 if ((nent & (nent - 1)) == 0) 2030 fasttrap_provs.fth_nent = nent; 2031 else 2032 fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 2033 ASSERT(fasttrap_provs.fth_nent > 0); 2034 fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2035 fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2036 sizeof (fasttrap_bucket_t), KM_SLEEP); 2037 2038 /* 2039 * ... and the procs hash table. 2040 */ 2041 nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2042 if ((nent & (nent - 1)) == 0) 2043 fasttrap_procs.fth_nent = nent; 2044 else 2045 fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2046 ASSERT(fasttrap_procs.fth_nent > 0); 2047 fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2048 fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 2049 sizeof (fasttrap_bucket_t), KM_SLEEP); 2050 2051 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2052 &fasttrap_meta_id); 2053 2054 return (DDI_SUCCESS); 2055 } 2056 2057 static int 2058 fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 2059 { 2060 int i, fail = 0; 2061 timeout_id_t tmp; 2062 2063 switch (cmd) { 2064 case DDI_DETACH: 2065 break; 2066 case DDI_SUSPEND: 2067 return (DDI_SUCCESS); 2068 default: 2069 return (DDI_FAILURE); 2070 } 2071 2072 /* 2073 * Unregister the meta-provider to make sure no new fasttrap- 2074 * managed providers come along while we're trying to close up 2075 * shop. If we fail to detach, we'll need to re-register as a 2076 * meta-provider. We can fail to unregister as a meta-provider 2077 * if providers we manage still exist. 2078 */ 2079 if (fasttrap_meta_id != DTRACE_METAPROVNONE && 2080 dtrace_meta_unregister(fasttrap_meta_id) != 0) 2081 return (DDI_FAILURE); 2082 2083 /* 2084 * Prevent any new timeouts from running by setting fasttrap_timeout 2085 * to a non-zero value, and wait for the current timeout to complete. 2086 */ 2087 mutex_enter(&fasttrap_cleanup_mtx); 2088 fasttrap_cleanup_work = 0; 2089 2090 while (fasttrap_timeout != (timeout_id_t)1) { 2091 tmp = fasttrap_timeout; 2092 fasttrap_timeout = (timeout_id_t)1; 2093 2094 if (tmp != 0) { 2095 mutex_exit(&fasttrap_cleanup_mtx); 2096 (void) untimeout(tmp); 2097 mutex_enter(&fasttrap_cleanup_mtx); 2098 } 2099 } 2100 2101 fasttrap_cleanup_work = 0; 2102 mutex_exit(&fasttrap_cleanup_mtx); 2103 2104 /* 2105 * Iterate over all of our providers. If there's still a process 2106 * that corresponds to that pid, fail to detach. 2107 */ 2108 for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2109 fasttrap_provider_t **fpp, *fp; 2110 fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 2111 2112 mutex_enter(&bucket->ftb_mtx); 2113 fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2114 while ((fp = *fpp) != NULL) { 2115 /* 2116 * Acquire and release the lock as a simple way of 2117 * waiting for any other consumer to finish with 2118 * this provider. A thread must first acquire the 2119 * bucket lock so there's no chance of another thread 2120 * blocking on the providers lock. 2121 */ 2122 mutex_enter(&fp->ftp_mtx); 2123 mutex_exit(&fp->ftp_mtx); 2124 2125 if (dtrace_unregister(fp->ftp_provid) != 0) { 2126 fail = 1; 2127 fpp = &fp->ftp_next; 2128 } else { 2129 *fpp = fp->ftp_next; 2130 fasttrap_provider_free(fp); 2131 } 2132 } 2133 2134 mutex_exit(&bucket->ftb_mtx); 2135 } 2136 2137 if (fail || dtrace_unregister(fasttrap_id) != 0) { 2138 uint_t work; 2139 /* 2140 * If we're failing to detach, we need to unblock timeouts 2141 * and start a new timeout if any work has accumulated while 2142 * we've been unsuccessfully trying to detach. 2143 */ 2144 mutex_enter(&fasttrap_cleanup_mtx); 2145 fasttrap_timeout = 0; 2146 work = fasttrap_cleanup_work; 2147 mutex_exit(&fasttrap_cleanup_mtx); 2148 2149 if (work) 2150 fasttrap_pid_cleanup(); 2151 2152 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2153 &fasttrap_meta_id); 2154 2155 return (DDI_FAILURE); 2156 } 2157 2158 #ifdef DEBUG 2159 mutex_enter(&fasttrap_count_mtx); 2160 ASSERT(fasttrap_count == 0); 2161 mutex_exit(&fasttrap_count_mtx); 2162 #endif 2163 2164 kmem_free(fasttrap_tpoints.fth_table, 2165 fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 2166 fasttrap_tpoints.fth_nent = 0; 2167 2168 kmem_free(fasttrap_provs.fth_table, 2169 fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 2170 fasttrap_provs.fth_nent = 0; 2171 2172 kmem_free(fasttrap_procs.fth_table, 2173 fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2174 fasttrap_procs.fth_nent = 0; 2175 2176 /* 2177 * We know there are no tracepoints in any process anywhere in 2178 * the system so there is no process which has its p_dtrace_count 2179 * greater than zero, therefore we know that no thread can actively 2180 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes 2181 * and fasttrap_exec() and fasttrap_exit(). 2182 */ 2183 ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork); 2184 dtrace_fasttrap_fork_ptr = NULL; 2185 2186 ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit); 2187 dtrace_fasttrap_exec_ptr = NULL; 2188 2189 ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit); 2190 dtrace_fasttrap_exit_ptr = NULL; 2191 2192 ddi_remove_minor_node(devi, NULL); 2193 2194 return (DDI_SUCCESS); 2195 } 2196 2197 static struct dev_ops fasttrap_ops = { 2198 DEVO_REV, /* devo_rev */ 2199 0, /* refcnt */ 2200 fasttrap_info, /* get_dev_info */ 2201 nulldev, /* identify */ 2202 nulldev, /* probe */ 2203 fasttrap_attach, /* attach */ 2204 fasttrap_detach, /* detach */ 2205 nodev, /* reset */ 2206 &fasttrap_cb_ops, /* driver operations */ 2207 NULL, /* bus operations */ 2208 nodev /* dev power */ 2209 }; 2210 2211 /* 2212 * Module linkage information for the kernel. 2213 */ 2214 static struct modldrv modldrv = { 2215 &mod_driverops, /* module type (this is a pseudo driver) */ 2216 "Fasttrap Tracing", /* name of module */ 2217 &fasttrap_ops, /* driver ops */ 2218 }; 2219 2220 static struct modlinkage modlinkage = { 2221 MODREV_1, 2222 (void *)&modldrv, 2223 NULL 2224 }; 2225 2226 int 2227 _init(void) 2228 { 2229 return (mod_install(&modlinkage)); 2230 } 2231 2232 int 2233 _info(struct modinfo *modinfop) 2234 { 2235 return (mod_info(&modlinkage, modinfop)); 2236 } 2237 2238 int 2239 _fini(void) 2240 { 2241 return (mod_remove(&modlinkage)); 2242 } 2243