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_retire(pid_t, const char *, int); 162 static void fasttrap_provider_free(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 ASSERT(p == curproc); 445 ASSERT(MUTEX_HELD(&p->p_lock)); 446 447 mutex_exit(&p->p_lock); 448 449 /* 450 * We clean up the pid provider for this process here; user-land 451 * static probes are handled by the meta-provider remove entry point. 452 * Note that the consumer count is not artificially elevated on the 453 * pid provider as it is on USDT providers so there's no need to drop 454 * it here. 455 */ 456 fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0); 457 458 mutex_enter(&p->p_lock); 459 } 460 461 462 /*ARGSUSED*/ 463 static void 464 fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc) 465 { 466 /* 467 * There are no "default" pid probes. 468 */ 469 } 470 471 /*ARGSUSED*/ 472 static void 473 fasttrap_provide(void *arg, const dtrace_probedesc_t *desc) 474 { 475 if (dtrace_probe_lookup(fasttrap_id, NULL, "fasttrap", "fasttrap") == 0) 476 fasttrap_probe_id = dtrace_probe_create(fasttrap_id, NULL, 477 "fasttrap", "fasttrap", FASTTRAP_AFRAMES, NULL); 478 } 479 480 static int 481 fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 482 { 483 fasttrap_tracepoint_t *tp, *new_tp = NULL; 484 fasttrap_bucket_t *bucket; 485 fasttrap_id_t *id; 486 pid_t pid; 487 uintptr_t pc; 488 489 ASSERT(index < probe->ftp_ntps); 490 491 pid = probe->ftp_pid; 492 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 493 id = &probe->ftp_tps[index].fit_id; 494 495 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 496 497 ASSERT(!(p->p_flag & SVFORK)); 498 499 /* 500 * Before we make any modifications, make sure we've imposed a barrier 501 * on the generation in which this probe was last modified. 502 */ 503 fasttrap_mod_barrier(probe->ftp_gen); 504 505 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 506 507 /* 508 * If the tracepoint has already been enabled, just add our id to the 509 * list of interested probes. This may be our second time through 510 * this path in which case we'll have constructed the tracepoint we'd 511 * like to install. If we can't find a match, and have an allocated 512 * tracepoint ready to go, enable that one now. 513 * 514 * A tracepoint whose proc is defunct is also considered defunct. 515 */ 516 again: 517 mutex_enter(&bucket->ftb_mtx); 518 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 519 if (tp->ftt_pid != pid || tp->ftt_pc != pc || 520 tp->ftt_proc->ftpc_defunct) 521 continue; 522 523 /* 524 * Now that we've found a matching tracepoint, it would be 525 * a decent idea to confirm that the tracepoint is still 526 * enabled and the trap instruction hasn't been overwritten. 527 * Since this is a little hairy, we'll punt for now. 528 */ 529 530 /* 531 * This can't be the first interested probe. We don't have 532 * to worry about another thread being in the midst of 533 * deleting this tracepoint (which would be the only valid 534 * reason for a tracepoint to have no interested probes) 535 * since we're holding P_PR_LOCK for this process. 536 */ 537 ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 538 539 if (probe->ftp_type == DTFTP_RETURN || 540 probe->ftp_type == DTFTP_POST_OFFSETS) { 541 id->fti_next = tp->ftt_retids; 542 membar_producer(); 543 tp->ftt_retids = id; 544 membar_producer(); 545 } else { 546 id->fti_next = tp->ftt_ids; 547 membar_producer(); 548 tp->ftt_ids = id; 549 membar_producer(); 550 } 551 552 mutex_exit(&bucket->ftb_mtx); 553 554 if (new_tp != NULL) { 555 new_tp->ftt_ids = NULL; 556 new_tp->ftt_retids = NULL; 557 } 558 559 return (0); 560 } 561 562 /* 563 * If we have a good tracepoint ready to go, install it now while 564 * we have the lock held and no one can screw with us. 565 */ 566 if (new_tp != NULL) { 567 int rc = 0; 568 569 new_tp->ftt_next = bucket->ftb_data; 570 membar_producer(); 571 bucket->ftb_data = new_tp; 572 membar_producer(); 573 mutex_exit(&bucket->ftb_mtx); 574 575 /* 576 * Activate the tracepoint in the ISA-specific manner. 577 * If this fails, we need to report the failure, but 578 * indicate that this tracepoint must still be disabled 579 * by calling fasttrap_tracepoint_disable(). 580 */ 581 if (fasttrap_tracepoint_install(p, new_tp) != 0) 582 rc = FASTTRAP_ENABLE_PARTIAL; 583 584 /* 585 * Increment the count of the number of tracepoints active in 586 * the victim process. 587 */ 588 ASSERT(p->p_proc_flag & P_PR_LOCK); 589 p->p_dtrace_count++; 590 591 return (rc); 592 } 593 594 mutex_exit(&bucket->ftb_mtx); 595 596 /* 597 * Initialize the tracepoint that's been preallocated with the probe. 598 */ 599 new_tp = probe->ftp_tps[index].fit_tp; 600 601 ASSERT(new_tp->ftt_pid == pid); 602 ASSERT(new_tp->ftt_pc == pc); 603 ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 604 ASSERT(new_tp->ftt_ids == NULL); 605 ASSERT(new_tp->ftt_retids == NULL); 606 607 if (probe->ftp_type == DTFTP_RETURN || 608 probe->ftp_type == DTFTP_POST_OFFSETS) { 609 id->fti_next = NULL; 610 new_tp->ftt_retids = id; 611 } else { 612 id->fti_next = NULL; 613 new_tp->ftt_ids = id; 614 } 615 616 /* 617 * If the ISA-dependent initialization goes to plan, go back to the 618 * beginning and try to install this freshly made tracepoint. 619 */ 620 if (fasttrap_tracepoint_init(p, probe, new_tp, pc) == 0) 621 goto again; 622 623 new_tp->ftt_ids = NULL; 624 new_tp->ftt_retids = NULL; 625 626 return (FASTTRAP_ENABLE_FAIL); 627 } 628 629 static void 630 fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 631 { 632 fasttrap_bucket_t *bucket; 633 fasttrap_provider_t *provider = probe->ftp_prov; 634 fasttrap_tracepoint_t **pp, *tp; 635 fasttrap_id_t *id, **idp; 636 pid_t pid; 637 uintptr_t pc; 638 639 ASSERT(index < probe->ftp_ntps); 640 641 pid = probe->ftp_pid; 642 pc = probe->ftp_tps[index].fit_tp->ftt_pc; 643 644 ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 645 646 /* 647 * Find the tracepoint and make sure that our id is one of the 648 * ones registered with it. 649 */ 650 bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 651 mutex_enter(&bucket->ftb_mtx); 652 for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 653 if (tp->ftt_pid == pid && tp->ftt_pc == pc && 654 tp->ftt_proc == provider->ftp_proc) 655 break; 656 } 657 658 /* 659 * If we somehow lost this tracepoint, we're in a world of hurt. 660 */ 661 ASSERT(tp != NULL); 662 663 if (probe->ftp_type == DTFTP_RETURN || 664 probe->ftp_type == DTFTP_POST_OFFSETS) { 665 ASSERT(tp->ftt_retids != NULL); 666 idp = &tp->ftt_retids; 667 } else { 668 ASSERT(tp->ftt_ids != NULL); 669 idp = &tp->ftt_ids; 670 } 671 672 while ((*idp)->fti_probe != probe) { 673 idp = &(*idp)->fti_next; 674 ASSERT(*idp != NULL); 675 } 676 677 id = *idp; 678 *idp = id->fti_next; 679 membar_producer(); 680 681 ASSERT(id->fti_probe == probe); 682 683 /* 684 * If there are other registered enablings of this tracepoint, we're 685 * all done, but if this was the last probe assocated with this 686 * this tracepoint, we need to remove and free it. 687 */ 688 if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 689 690 /* 691 * If the current probe's tracepoint is in use, swap it 692 * for an unused tracepoint. 693 */ 694 if (tp == probe->ftp_tps[index].fit_tp) { 695 fasttrap_probe_t *tmp_probe; 696 fasttrap_tracepoint_t **tmp_tp; 697 uint_t tmp_index; 698 699 if (tp->ftt_ids != NULL) { 700 tmp_probe = tp->ftt_ids->fti_probe; 701 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 702 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 703 } else { 704 tmp_probe = tp->ftt_retids->fti_probe; 705 tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 706 tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 707 } 708 709 ASSERT(*tmp_tp != NULL); 710 ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 711 ASSERT((*tmp_tp)->ftt_ids == NULL); 712 ASSERT((*tmp_tp)->ftt_retids == NULL); 713 714 probe->ftp_tps[index].fit_tp = *tmp_tp; 715 *tmp_tp = tp; 716 717 } 718 719 mutex_exit(&bucket->ftb_mtx); 720 721 /* 722 * Tag the modified probe with the generation in which it was 723 * changed. 724 */ 725 probe->ftp_gen = fasttrap_mod_gen; 726 return; 727 } 728 729 mutex_exit(&bucket->ftb_mtx); 730 731 /* 732 * We can't safely remove the tracepoint from the set of active 733 * tracepoints until we've actually removed the fasttrap instruction 734 * from the process's text. We can, however, operate on this 735 * tracepoint secure in the knowledge that no other thread is going to 736 * be looking at it since we hold P_PR_LOCK on the process if it's 737 * live or we hold the provider lock on the process if it's dead and 738 * gone. 739 */ 740 741 /* 742 * We only need to remove the actual instruction if we're looking 743 * at an existing process 744 */ 745 if (p != NULL) { 746 /* 747 * If we fail to restore the instruction we need to kill 748 * this process since it's in a completely unrecoverable 749 * state. 750 */ 751 if (fasttrap_tracepoint_remove(p, tp) != 0) 752 fasttrap_sigtrap(p, NULL, pc); 753 754 /* 755 * Decrement the count of the number of tracepoints active 756 * in the victim process. 757 */ 758 ASSERT(p->p_proc_flag & P_PR_LOCK); 759 p->p_dtrace_count--; 760 } 761 762 /* 763 * Remove the probe from the hash table of active tracepoints. 764 */ 765 mutex_enter(&bucket->ftb_mtx); 766 pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 767 ASSERT(*pp != NULL); 768 while (*pp != tp) { 769 pp = &(*pp)->ftt_next; 770 ASSERT(*pp != NULL); 771 } 772 773 *pp = tp->ftt_next; 774 membar_producer(); 775 776 mutex_exit(&bucket->ftb_mtx); 777 778 /* 779 * Tag the modified probe with the generation in which it was changed. 780 */ 781 probe->ftp_gen = fasttrap_mod_gen; 782 } 783 784 typedef int fasttrap_probe_f(struct regs *); 785 786 static void 787 fasttrap_enable_common(int *count, fasttrap_probe_f **fptr, fasttrap_probe_f *f, 788 fasttrap_probe_f **fptr2, fasttrap_probe_f *f2) 789 { 790 /* 791 * We don't have to play the rw lock game here because we're 792 * providing something rather than taking something away -- 793 * we can be sure that no threads have tried to follow this 794 * function pointer yet. 795 */ 796 mutex_enter(&fasttrap_count_mtx); 797 if (*count == 0) { 798 ASSERT(*fptr == NULL); 799 *fptr = f; 800 if (fptr2 != NULL) 801 *fptr2 = f2; 802 } 803 ASSERT(*fptr == f); 804 ASSERT(fptr2 == NULL || *fptr2 == f2); 805 (*count)++; 806 mutex_exit(&fasttrap_count_mtx); 807 } 808 809 static void 810 fasttrap_disable_common(int *count, fasttrap_probe_f **fptr, 811 fasttrap_probe_f **fptr2) 812 { 813 ASSERT(MUTEX_HELD(&cpu_lock)); 814 815 mutex_enter(&fasttrap_count_mtx); 816 (*count)--; 817 ASSERT(*count >= 0); 818 if (*count == 0) { 819 cpu_t *cur, *cpu = CPU; 820 821 for (cur = cpu->cpu_next_onln; cur != cpu; 822 cur = cur->cpu_next_onln) { 823 rw_enter(&cur->cpu_ft_lock, RW_WRITER); 824 } 825 826 *fptr = NULL; 827 if (fptr2 != NULL) 828 *fptr2 = NULL; 829 830 for (cur = cpu->cpu_next_onln; cur != cpu; 831 cur = cur->cpu_next_onln) { 832 rw_exit(&cur->cpu_ft_lock); 833 } 834 } 835 mutex_exit(&fasttrap_count_mtx); 836 } 837 838 /*ARGSUSED*/ 839 static void 840 fasttrap_enable(void *arg, dtrace_id_t id, void *parg) 841 { 842 /* 843 * Enable the probe that corresponds to statically placed trace 844 * points which have not explicitly been placed in the process's text 845 * by the fasttrap provider. 846 */ 847 ASSERT(arg == NULL); 848 ASSERT(id == fasttrap_probe_id); 849 850 fasttrap_enable_common(&fasttrap_count, 851 &dtrace_fasttrap_probe_ptr, fasttrap_probe, NULL, NULL); 852 } 853 854 855 /*ARGSUSED*/ 856 static void 857 fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 858 { 859 fasttrap_probe_t *probe = parg; 860 proc_t *p; 861 int i, rc; 862 863 ASSERT(probe != NULL); 864 ASSERT(!probe->ftp_enabled); 865 ASSERT(id == probe->ftp_id); 866 ASSERT(MUTEX_HELD(&cpu_lock)); 867 868 /* 869 * Increment the count of enabled probes on this probe's provider; 870 * the provider can't go away while the probe still exists. We 871 * must increment this even if we aren't able to properly enable 872 * this probe. 873 */ 874 mutex_enter(&probe->ftp_prov->ftp_mtx); 875 probe->ftp_prov->ftp_rcount++; 876 mutex_exit(&probe->ftp_prov->ftp_mtx); 877 878 /* 879 * Bail out if we can't find the process for this probe or its 880 * provider is retired (meaning it was valid in a previously exec'ed 881 * incarnation of this address space). The provider can't go away 882 * while we're in this code path. 883 */ 884 if (probe->ftp_prov->ftp_retired || 885 (p = sprlock(probe->ftp_pid)) == NULL) 886 return; 887 888 ASSERT(!(p->p_flag & SVFORK)); 889 mutex_exit(&p->p_lock); 890 891 /* 892 * We have to enable the trap entry before any user threads have 893 * the chance to execute the trap instruction we're about to place 894 * in their process's text. 895 */ 896 fasttrap_enable_common(&fasttrap_pid_count, 897 &dtrace_pid_probe_ptr, fasttrap_pid_probe, 898 &dtrace_return_probe_ptr, fasttrap_return_probe); 899 900 /* 901 * Enable all the tracepoints and add this probe's id to each 902 * tracepoint's list of active probes. 903 */ 904 for (i = 0; i < probe->ftp_ntps; i++) { 905 if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 906 /* 907 * If enabling the tracepoint failed completely, 908 * we don't have to disable it; if the failure 909 * was only partial we must disable it. 910 */ 911 if (rc == FASTTRAP_ENABLE_FAIL) 912 i--; 913 else 914 ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 915 916 /* 917 * Back up and pull out all the tracepoints we've 918 * created so far for this probe. 919 */ 920 while (i >= 0) { 921 fasttrap_tracepoint_disable(p, probe, i); 922 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 ASSERT(pattr != NULL); 1285 1286 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1287 mutex_enter(&bucket->ftb_mtx); 1288 1289 /* 1290 * Take a lap through the list and return the match if we find it. 1291 */ 1292 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1293 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1294 !fp->ftp_retired) { 1295 mutex_enter(&fp->ftp_mtx); 1296 mutex_exit(&bucket->ftb_mtx); 1297 return (fp); 1298 } 1299 } 1300 1301 /* 1302 * Drop the bucket lock so we don't try to perform a sleeping 1303 * allocation under it. 1304 */ 1305 mutex_exit(&bucket->ftb_mtx); 1306 1307 /* 1308 * Make sure the process exists, isn't a child created as the result 1309 * of a vfork(2), and isn't a zombie (but may be in fork). Record the 1310 * process's uid to pass to dtrace_register(). 1311 */ 1312 mutex_enter(&pidlock); 1313 if ((p = prfind(pid)) == NULL) { 1314 mutex_exit(&pidlock); 1315 return (NULL); 1316 } 1317 mutex_enter(&p->p_lock); 1318 mutex_exit(&pidlock); 1319 if (p->p_flag & (SVFORK | SEXITING)) { 1320 mutex_exit(&p->p_lock); 1321 return (NULL); 1322 } 1323 1324 /* 1325 * Increment p_dtrace_probes so that the process knows to inform us 1326 * when it exits or execs. fasttrap_provider_free() decrements this 1327 * when we're done with this provider. 1328 */ 1329 p->p_dtrace_probes++; 1330 1331 mutex_enter(&p->p_crlock); 1332 uid = crgetruid(p->p_cred); 1333 mutex_exit(&p->p_crlock); 1334 mutex_exit(&p->p_lock); 1335 1336 new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1337 new_fp->ftp_pid = pid; 1338 new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1339 1340 ASSERT(new_fp->ftp_proc != NULL); 1341 1342 mutex_enter(&bucket->ftb_mtx); 1343 1344 /* 1345 * Take another lap through the list to make sure a provider hasn't 1346 * been created for this pid while we weren't under the bucket lock. 1347 */ 1348 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1349 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1350 !fp->ftp_retired) { 1351 mutex_enter(&fp->ftp_mtx); 1352 mutex_exit(&bucket->ftb_mtx); 1353 fasttrap_provider_free(new_fp); 1354 return (fp); 1355 } 1356 } 1357 1358 (void) strcpy(new_fp->ftp_name, name); 1359 1360 /* 1361 * Fail and return NULL if either the provider name is too long 1362 * or we fail to register this new provider with the DTrace 1363 * framework. Note that this is the only place we ever construct 1364 * the full provider name -- we keep it in pieces in the provider 1365 * structure. 1366 */ 1367 if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 1368 sizeof (provname) || 1369 dtrace_register(provname, pattr, 1370 DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER, uid, 1371 pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 1372 &new_fp->ftp_provid) != 0) { 1373 mutex_exit(&bucket->ftb_mtx); 1374 fasttrap_provider_free(new_fp); 1375 return (NULL); 1376 } 1377 1378 new_fp->ftp_next = bucket->ftb_data; 1379 bucket->ftb_data = new_fp; 1380 1381 mutex_enter(&new_fp->ftp_mtx); 1382 mutex_exit(&bucket->ftb_mtx); 1383 1384 return (new_fp); 1385 } 1386 1387 static void 1388 fasttrap_provider_free(fasttrap_provider_t *provider) 1389 { 1390 pid_t pid = provider->ftp_pid; 1391 proc_t *p; 1392 1393 /* 1394 * There need to be no consumers using this provider and no 1395 * associated enabled probes. 1396 */ 1397 ASSERT(provider->ftp_ccount == 0); 1398 ASSERT(provider->ftp_rcount == 0); 1399 1400 fasttrap_proc_release(provider->ftp_proc); 1401 1402 kmem_free(provider, sizeof (fasttrap_provider_t)); 1403 1404 /* 1405 * Decrement p_dtrace_probes on the process whose provider we're 1406 * freeing. We don't have to worry about clobbering somone else's 1407 * modifications to it because we have locked the bucket that 1408 * corresponds to this process's hash chain in the provider hash 1409 * table. Don't sweat it if we can't find the process. 1410 */ 1411 mutex_enter(&pidlock); 1412 if ((p = prfind(pid)) == NULL) { 1413 mutex_exit(&pidlock); 1414 return; 1415 } 1416 1417 mutex_enter(&p->p_lock); 1418 mutex_exit(&pidlock); 1419 1420 p->p_dtrace_probes--; 1421 mutex_exit(&p->p_lock); 1422 } 1423 1424 static void 1425 fasttrap_provider_retire(pid_t pid, const char *name, int ccount) 1426 { 1427 fasttrap_provider_t *fp; 1428 fasttrap_bucket_t *bucket; 1429 dtrace_provider_id_t provid; 1430 1431 ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1432 ASSERT(ccount == 0 || ccount == 1); 1433 1434 bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1435 mutex_enter(&bucket->ftb_mtx); 1436 1437 for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1438 if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1439 !fp->ftp_retired) 1440 break; 1441 } 1442 1443 if (fp == NULL) { 1444 mutex_exit(&bucket->ftb_mtx); 1445 return; 1446 } 1447 1448 /* 1449 * Mark the provider to be removed in our post-processing step, 1450 * mark it retired, and mark its proc as defunct (though it may 1451 * already be marked defunct by another provider that shares the 1452 * same proc). Marking it indicates that we should try to remove it; 1453 * setting the retired flag indicates that we're done with this 1454 * provider; setting the proc to be defunct indicates that all 1455 * tracepoints associated with the traced process should be ignored. 1456 * We also drop the consumer count here by the amount specified. 1457 * 1458 * We obviously need to take the bucket lock before the provider lock 1459 * to perform the lookup, but we need to drop the provider lock 1460 * before calling into the DTrace framework since we acquire the 1461 * provider lock in callbacks invoked from the DTrace framework. The 1462 * bucket lock therefore protects the integrity of the provider hash 1463 * table. 1464 */ 1465 mutex_enter(&fp->ftp_mtx); 1466 fp->ftp_proc->ftpc_defunct = 1; 1467 fp->ftp_retired = 1; 1468 fp->ftp_marked = 1; 1469 fp->ftp_ccount -= ccount; 1470 provid = fp->ftp_provid; 1471 mutex_exit(&fp->ftp_mtx); 1472 1473 /* 1474 * We don't have to worry about invalidating the same provider twice 1475 * since fasttrap_provider_lookup() will ignore provider that have 1476 * been marked as retired. 1477 */ 1478 dtrace_invalidate(provid); 1479 1480 mutex_exit(&bucket->ftb_mtx); 1481 1482 fasttrap_pid_cleanup(); 1483 } 1484 1485 static int 1486 fasttrap_add_probe(fasttrap_probe_spec_t *pdata) 1487 { 1488 fasttrap_provider_t *provider; 1489 fasttrap_probe_t *pp; 1490 fasttrap_tracepoint_t *tp; 1491 char *name; 1492 size_t size; 1493 int i, aframes, whack; 1494 1495 switch (pdata->ftps_type) { 1496 case DTFTP_ENTRY: 1497 name = "entry"; 1498 aframes = FASTTRAP_ENTRY_AFRAMES; 1499 break; 1500 case DTFTP_RETURN: 1501 name = "return"; 1502 aframes = FASTTRAP_RETURN_AFRAMES; 1503 break; 1504 case DTFTP_OFFSETS: 1505 name = NULL; 1506 break; 1507 default: 1508 return (EINVAL); 1509 } 1510 1511 if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 1512 FASTTRAP_PID_NAME, &pid_attr)) == NULL) 1513 return (ESRCH); 1514 1515 /* 1516 * Increment this reference count to indicate that a consumer is 1517 * actively adding a new probe associated with this provider. 1518 */ 1519 provider->ftp_ccount++; 1520 mutex_exit(&provider->ftp_mtx); 1521 1522 if (name != NULL) { 1523 if (dtrace_probe_lookup(provider->ftp_provid, 1524 pdata->ftps_mod, pdata->ftps_func, name) != 0) 1525 goto done; 1526 1527 atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 1528 1529 if (fasttrap_total > fasttrap_max) { 1530 atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1531 goto no_mem; 1532 } 1533 1534 ASSERT(pdata->ftps_noffs > 0); 1535 size = sizeof (fasttrap_probe_t) + 1536 sizeof (pp->ftp_tps[0]) * (pdata->ftps_noffs - 1); 1537 1538 pp = kmem_zalloc(size, KM_SLEEP); 1539 1540 pp->ftp_prov = provider; 1541 pp->ftp_faddr = pdata->ftps_pc; 1542 pp->ftp_fsize = pdata->ftps_size; 1543 pp->ftp_pid = pdata->ftps_pid; 1544 pp->ftp_ntps = pdata->ftps_noffs; 1545 pp->ftp_type = pdata->ftps_type; 1546 1547 for (i = 0; i < pdata->ftps_noffs; i++) { 1548 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1549 KM_SLEEP); 1550 1551 tp->ftt_proc = provider->ftp_proc; 1552 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1553 tp->ftt_pid = pdata->ftps_pid; 1554 1555 pp->ftp_tps[i].fit_tp = tp; 1556 pp->ftp_tps[i].fit_id.fti_probe = pp; 1557 } 1558 1559 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1560 pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 1561 } else { 1562 for (i = 0; i < pdata->ftps_noffs; i++) { 1563 char name_str[17]; 1564 1565 (void) sprintf(name_str, "%llx", 1566 (unsigned long long)pdata->ftps_offs[i]); 1567 1568 if (dtrace_probe_lookup(provider->ftp_provid, 1569 pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 1570 continue; 1571 1572 atomic_add_32(&fasttrap_total, 1); 1573 1574 if (fasttrap_total > fasttrap_max) { 1575 atomic_add_32(&fasttrap_total, -1); 1576 goto no_mem; 1577 } 1578 1579 pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 1580 1581 pp->ftp_prov = provider; 1582 pp->ftp_faddr = pdata->ftps_pc; 1583 pp->ftp_fsize = pdata->ftps_size; 1584 pp->ftp_pid = pdata->ftps_pid; 1585 pp->ftp_ntps = 1; 1586 pp->ftp_type = pdata->ftps_type; 1587 1588 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1589 KM_SLEEP); 1590 1591 tp->ftt_proc = provider->ftp_proc; 1592 tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1593 tp->ftt_pid = pdata->ftps_pid; 1594 1595 pp->ftp_tps[0].fit_tp = tp; 1596 pp->ftp_tps[0].fit_id.fti_probe = pp; 1597 1598 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1599 pdata->ftps_mod, pdata->ftps_func, name_str, 1600 FASTTRAP_OFFSET_AFRAMES, pp); 1601 } 1602 } 1603 1604 done: 1605 /* 1606 * We know that the provider is still valid since we incremented the 1607 * reference count. If someone tried to free this provider while we 1608 * were using it (e.g. because the process called exec(2) or exit(2)), 1609 * take note of that and try to free it now. 1610 */ 1611 mutex_enter(&provider->ftp_mtx); 1612 provider->ftp_ccount--; 1613 whack = provider->ftp_retired; 1614 mutex_exit(&provider->ftp_mtx); 1615 1616 if (whack) 1617 fasttrap_pid_cleanup(); 1618 1619 return (0); 1620 1621 no_mem: 1622 /* 1623 * If we've exhausted the allowable resources, we'll try to remove 1624 * this provider to free some up. This is to cover the case where 1625 * the user has accidentally created many more probes than was 1626 * intended (e.g. pid123:::). 1627 */ 1628 mutex_enter(&provider->ftp_mtx); 1629 provider->ftp_ccount--; 1630 provider->ftp_marked = 1; 1631 mutex_exit(&provider->ftp_mtx); 1632 1633 fasttrap_pid_cleanup(); 1634 1635 return (ENOMEM); 1636 } 1637 1638 /*ARGSUSED*/ 1639 static void * 1640 fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 1641 { 1642 fasttrap_provider_t *provider; 1643 1644 /* 1645 * A 32-bit unsigned integer (like a pid for example) can be 1646 * expressed in 10 or fewer decimal digits. Make sure that we'll 1647 * have enough space for the provider name. 1648 */ 1649 if (strlen(dhpv->dthpv_provname) + 10 >= 1650 sizeof (provider->ftp_name)) { 1651 cmn_err(CE_WARN, "failed to instantiate provider %s: " 1652 "name too long to accomodate pid", dhpv->dthpv_provname); 1653 return (NULL); 1654 } 1655 1656 /* 1657 * Don't let folks spoof the true pid provider. 1658 */ 1659 if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 1660 cmn_err(CE_WARN, "failed to instantiate provider %s: " 1661 "%s is an invalid name", dhpv->dthpv_provname, 1662 FASTTRAP_PID_NAME); 1663 return (NULL); 1664 } 1665 1666 /* 1667 * The highest stability class that fasttrap supports is ISA; cap 1668 * the stability of the new provider accordingly. 1669 */ 1670 if (dhpv->dthpv_pattr.dtpa_provider.dtat_class >= DTRACE_CLASS_COMMON) 1671 dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 1672 if (dhpv->dthpv_pattr.dtpa_mod.dtat_class >= DTRACE_CLASS_COMMON) 1673 dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 1674 if (dhpv->dthpv_pattr.dtpa_func.dtat_class >= DTRACE_CLASS_COMMON) 1675 dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 1676 if (dhpv->dthpv_pattr.dtpa_name.dtat_class >= DTRACE_CLASS_COMMON) 1677 dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 1678 if (dhpv->dthpv_pattr.dtpa_args.dtat_class >= DTRACE_CLASS_COMMON) 1679 dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 1680 1681 if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 1682 &dhpv->dthpv_pattr)) == NULL) { 1683 cmn_err(CE_WARN, "failed to instantiate provider %s for " 1684 "process %u", dhpv->dthpv_provname, (uint_t)pid); 1685 return (NULL); 1686 } 1687 1688 /* 1689 * We elevate the consumer count here to ensure that this provider 1690 * isn't removed until after the meta provider has been told to 1691 * remove it. 1692 */ 1693 provider->ftp_ccount++; 1694 1695 mutex_exit(&provider->ftp_mtx); 1696 1697 return (provider); 1698 } 1699 1700 /*ARGSUSED*/ 1701 static void 1702 fasttrap_meta_create_probe(void *arg, void *parg, 1703 dtrace_helper_probedesc_t *dhpb) 1704 { 1705 fasttrap_provider_t *provider = parg; 1706 fasttrap_probe_t *pp; 1707 fasttrap_tracepoint_t *tp; 1708 size_t size; 1709 int i; 1710 1711 mutex_enter(&provider->ftp_mtx); 1712 1713 if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 1714 dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 1715 mutex_exit(&provider->ftp_mtx); 1716 return; 1717 } 1718 1719 atomic_add_32(&fasttrap_total, dhpb->dthpb_noffs); 1720 1721 if (fasttrap_total > fasttrap_max) { 1722 atomic_add_32(&fasttrap_total, -dhpb->dthpb_noffs); 1723 mutex_exit(&provider->ftp_mtx); 1724 return; 1725 } 1726 1727 size = sizeof (fasttrap_probe_t) + 1728 sizeof (pp->ftp_tps[0]) * (dhpb->dthpb_noffs - 1); 1729 pp = kmem_zalloc(size, KM_SLEEP); 1730 1731 pp->ftp_prov = provider; 1732 pp->ftp_pid = provider->ftp_pid; 1733 pp->ftp_ntps = dhpb->dthpb_noffs; 1734 #ifdef __sparc 1735 pp->ftp_type = DTFTP_POST_OFFSETS; 1736 #else 1737 pp->ftp_type = DTFTP_OFFSETS; 1738 #endif 1739 pp->ftp_nargs = dhpb->dthpb_xargc; 1740 pp->ftp_xtypes = dhpb->dthpb_xtypes; 1741 pp->ftp_ntypes = dhpb->dthpb_ntypes; 1742 1743 for (i = 0; i < pp->ftp_ntps; i++) { 1744 tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 1745 1746 tp->ftt_proc = provider->ftp_proc; 1747 tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 1748 tp->ftt_pid = provider->ftp_pid; 1749 1750 pp->ftp_tps[i].fit_tp = tp; 1751 pp->ftp_tps[i].fit_id.fti_probe = pp; 1752 } 1753 1754 /* 1755 * If the arguments are shuffled around we set the argument remapping 1756 * table. Later, when the probe fires, we only remap the arguments 1757 * if the table is non-NULL. 1758 */ 1759 for (i = 0; i < dhpb->dthpb_xargc; i++) { 1760 if (dhpb->dthpb_args[i] != i) { 1761 pp->ftp_argmap = dhpb->dthpb_args; 1762 break; 1763 } 1764 } 1765 1766 /* 1767 * The probe is fully constructed -- register it with DTrace. 1768 */ 1769 pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 1770 dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 1771 1772 mutex_exit(&provider->ftp_mtx); 1773 } 1774 1775 /*ARGSUSED*/ 1776 static void 1777 fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 1778 { 1779 /* 1780 * Clean up the USDT provider. There may be active consumers of the 1781 * provider busy adding probes, no damage will actually befall the 1782 * provider until that count has dropped to zero. This just puts 1783 * the provider on death row. 1784 */ 1785 fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1); 1786 } 1787 1788 static dtrace_mops_t fasttrap_mops = { 1789 fasttrap_meta_create_probe, 1790 fasttrap_meta_provide, 1791 fasttrap_meta_remove 1792 }; 1793 1794 /*ARGSUSED*/ 1795 static int 1796 fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 1797 { 1798 return (0); 1799 } 1800 1801 /*ARGSUSED*/ 1802 static int 1803 fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 1804 { 1805 if (!dtrace_attached()) 1806 return (EAGAIN); 1807 1808 if (cmd == FASTTRAPIOC_MAKEPROBE) { 1809 fasttrap_probe_spec_t *uprobe = (void *)arg; 1810 fasttrap_probe_spec_t *probe; 1811 uint64_t noffs; 1812 size_t size; 1813 int ret; 1814 char *c; 1815 1816 if (copyin(&uprobe->ftps_noffs, &noffs, 1817 sizeof (uprobe->ftps_noffs))) 1818 return (EFAULT); 1819 1820 /* 1821 * Probes must have at least one tracepoint. 1822 */ 1823 if (noffs == 0) 1824 return (EINVAL); 1825 1826 size = sizeof (fasttrap_probe_spec_t) + 1827 sizeof (probe->ftps_offs[0]) * (noffs - 1); 1828 1829 if (size > 1024 * 1024) 1830 return (ENOMEM); 1831 1832 probe = kmem_alloc(size, KM_SLEEP); 1833 1834 if (copyin(uprobe, probe, size) != 0) { 1835 kmem_free(probe, size); 1836 return (EFAULT); 1837 } 1838 1839 /* 1840 * Verify that the function and module strings contain no 1841 * funny characters. 1842 */ 1843 for (c = &probe->ftps_func[0]; *c != '\0'; c++) { 1844 if (*c < 0x20 || 0x7f <= *c) { 1845 ret = EINVAL; 1846 goto err; 1847 } 1848 } 1849 1850 for (c = &probe->ftps_mod[0]; *c != '\0'; c++) { 1851 if (*c < 0x20 || 0x7f <= *c) { 1852 ret = EINVAL; 1853 goto err; 1854 } 1855 } 1856 1857 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 1858 proc_t *p; 1859 pid_t pid = probe->ftps_pid; 1860 1861 mutex_enter(&pidlock); 1862 /* 1863 * Report an error if the process doesn't exist 1864 * or is actively being birthed. 1865 */ 1866 if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 1867 mutex_exit(&pidlock); 1868 return (ESRCH); 1869 } 1870 mutex_enter(&p->p_lock); 1871 mutex_exit(&pidlock); 1872 1873 if ((ret = priv_proc_cred_perm(cr, p, NULL, 1874 VREAD | VWRITE)) != 0) { 1875 mutex_exit(&p->p_lock); 1876 return (ret); 1877 } 1878 1879 mutex_exit(&p->p_lock); 1880 } 1881 1882 ret = fasttrap_add_probe(probe); 1883 err: 1884 kmem_free(probe, size); 1885 1886 return (ret); 1887 1888 } else if (cmd == FASTTRAPIOC_GETINSTR) { 1889 fasttrap_instr_query_t instr; 1890 fasttrap_tracepoint_t *tp; 1891 uint_t index; 1892 int ret; 1893 1894 if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 1895 return (EFAULT); 1896 1897 if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 1898 proc_t *p; 1899 pid_t pid = instr.ftiq_pid; 1900 1901 mutex_enter(&pidlock); 1902 /* 1903 * Report an error if the process doesn't exist 1904 * or is actively being birthed. 1905 */ 1906 if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 1907 mutex_exit(&pidlock); 1908 return (ESRCH); 1909 } 1910 mutex_enter(&p->p_lock); 1911 mutex_exit(&pidlock); 1912 1913 if ((ret = priv_proc_cred_perm(cr, p, NULL, 1914 VREAD)) != 0) { 1915 mutex_exit(&p->p_lock); 1916 return (ret); 1917 } 1918 1919 mutex_exit(&p->p_lock); 1920 } 1921 1922 index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 1923 1924 mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1925 tp = fasttrap_tpoints.fth_table[index].ftb_data; 1926 while (tp != NULL) { 1927 if (instr.ftiq_pid == tp->ftt_pid && 1928 instr.ftiq_pc == tp->ftt_pc && 1929 !tp->ftt_proc->ftpc_defunct) 1930 break; 1931 1932 tp = tp->ftt_next; 1933 } 1934 1935 if (tp == NULL) { 1936 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1937 return (ENOENT); 1938 } 1939 1940 bcopy(&tp->ftt_instr, &instr.ftiq_instr, 1941 sizeof (instr.ftiq_instr)); 1942 mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 1943 1944 if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 1945 return (EFAULT); 1946 1947 return (0); 1948 } 1949 1950 return (EINVAL); 1951 } 1952 1953 static struct cb_ops fasttrap_cb_ops = { 1954 fasttrap_open, /* open */ 1955 nodev, /* close */ 1956 nulldev, /* strategy */ 1957 nulldev, /* print */ 1958 nodev, /* dump */ 1959 nodev, /* read */ 1960 nodev, /* write */ 1961 fasttrap_ioctl, /* ioctl */ 1962 nodev, /* devmap */ 1963 nodev, /* mmap */ 1964 nodev, /* segmap */ 1965 nochpoll, /* poll */ 1966 ddi_prop_op, /* cb_prop_op */ 1967 0, /* streamtab */ 1968 D_NEW | D_MP /* Driver compatibility flag */ 1969 }; 1970 1971 /*ARGSUSED*/ 1972 static int 1973 fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 1974 { 1975 int error; 1976 1977 switch (infocmd) { 1978 case DDI_INFO_DEVT2DEVINFO: 1979 *result = (void *)fasttrap_devi; 1980 error = DDI_SUCCESS; 1981 break; 1982 case DDI_INFO_DEVT2INSTANCE: 1983 *result = (void *)0; 1984 error = DDI_SUCCESS; 1985 break; 1986 default: 1987 error = DDI_FAILURE; 1988 } 1989 return (error); 1990 } 1991 1992 static int 1993 fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 1994 { 1995 ulong_t nent; 1996 1997 switch (cmd) { 1998 case DDI_ATTACH: 1999 break; 2000 case DDI_RESUME: 2001 return (DDI_SUCCESS); 2002 default: 2003 return (DDI_FAILURE); 2004 } 2005 2006 if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0, 2007 DDI_PSEUDO, NULL) == DDI_FAILURE || 2008 dtrace_register("fasttrap", &fasttrap_attr, DTRACE_PRIV_USER, 0, 2009 &fasttrap_pops, NULL, &fasttrap_id) != 0) { 2010 ddi_remove_minor_node(devi, NULL); 2011 return (DDI_FAILURE); 2012 } 2013 2014 ddi_report_dev(devi); 2015 fasttrap_devi = devi; 2016 2017 /* 2018 * Install our hooks into fork(2), exec(2), and exit(2). 2019 */ 2020 dtrace_fasttrap_fork_ptr = &fasttrap_fork; 2021 dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit; 2022 dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit; 2023 2024 fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2025 "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 2026 fasttrap_total = 0; 2027 2028 /* 2029 * Conjure up the tracepoints hashtable... 2030 */ 2031 nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2032 "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 2033 2034 if (nent <= 0 || nent > 0x1000000) 2035 nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2036 2037 if ((nent & (nent - 1)) == 0) 2038 fasttrap_tpoints.fth_nent = nent; 2039 else 2040 fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 2041 ASSERT(fasttrap_tpoints.fth_nent > 0); 2042 fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 2043 fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 2044 sizeof (fasttrap_bucket_t), KM_SLEEP); 2045 2046 /* 2047 * ... and the providers hash table... 2048 */ 2049 nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 2050 if ((nent & (nent - 1)) == 0) 2051 fasttrap_provs.fth_nent = nent; 2052 else 2053 fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 2054 ASSERT(fasttrap_provs.fth_nent > 0); 2055 fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2056 fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2057 sizeof (fasttrap_bucket_t), KM_SLEEP); 2058 2059 /* 2060 * ... and the procs hash table. 2061 */ 2062 nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2063 if ((nent & (nent - 1)) == 0) 2064 fasttrap_procs.fth_nent = nent; 2065 else 2066 fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2067 ASSERT(fasttrap_procs.fth_nent > 0); 2068 fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2069 fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 2070 sizeof (fasttrap_bucket_t), KM_SLEEP); 2071 2072 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2073 &fasttrap_meta_id); 2074 2075 return (DDI_SUCCESS); 2076 } 2077 2078 static int 2079 fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 2080 { 2081 int i, fail = 0; 2082 timeout_id_t tmp; 2083 2084 switch (cmd) { 2085 case DDI_DETACH: 2086 break; 2087 case DDI_SUSPEND: 2088 return (DDI_SUCCESS); 2089 default: 2090 return (DDI_FAILURE); 2091 } 2092 2093 /* 2094 * Unregister the meta-provider to make sure no new fasttrap- 2095 * managed providers come along while we're trying to close up 2096 * shop. If we fail to detach, we'll need to re-register as a 2097 * meta-provider. We can fail to unregister as a meta-provider 2098 * if providers we manage still exist. 2099 */ 2100 if (fasttrap_meta_id != DTRACE_METAPROVNONE && 2101 dtrace_meta_unregister(fasttrap_meta_id) != 0) 2102 return (DDI_FAILURE); 2103 2104 /* 2105 * Prevent any new timeouts from running by setting fasttrap_timeout 2106 * to a non-zero value, and wait for the current timeout to complete. 2107 */ 2108 mutex_enter(&fasttrap_cleanup_mtx); 2109 fasttrap_cleanup_work = 0; 2110 2111 while (fasttrap_timeout != (timeout_id_t)1) { 2112 tmp = fasttrap_timeout; 2113 fasttrap_timeout = (timeout_id_t)1; 2114 2115 if (tmp != 0) { 2116 mutex_exit(&fasttrap_cleanup_mtx); 2117 (void) untimeout(tmp); 2118 mutex_enter(&fasttrap_cleanup_mtx); 2119 } 2120 } 2121 2122 fasttrap_cleanup_work = 0; 2123 mutex_exit(&fasttrap_cleanup_mtx); 2124 2125 /* 2126 * Iterate over all of our providers. If there's still a process 2127 * that corresponds to that pid, fail to detach. 2128 */ 2129 for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2130 fasttrap_provider_t **fpp, *fp; 2131 fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 2132 2133 mutex_enter(&bucket->ftb_mtx); 2134 fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2135 while ((fp = *fpp) != NULL) { 2136 /* 2137 * Acquire and release the lock as a simple way of 2138 * waiting for any other consumer to finish with 2139 * this provider. A thread must first acquire the 2140 * bucket lock so there's no chance of another thread 2141 * blocking on the provider's lock. 2142 */ 2143 mutex_enter(&fp->ftp_mtx); 2144 mutex_exit(&fp->ftp_mtx); 2145 2146 if (dtrace_unregister(fp->ftp_provid) != 0) { 2147 fail = 1; 2148 fpp = &fp->ftp_next; 2149 } else { 2150 *fpp = fp->ftp_next; 2151 fasttrap_provider_free(fp); 2152 } 2153 } 2154 2155 mutex_exit(&bucket->ftb_mtx); 2156 } 2157 2158 if (fail || dtrace_unregister(fasttrap_id) != 0) { 2159 uint_t work; 2160 /* 2161 * If we're failing to detach, we need to unblock timeouts 2162 * and start a new timeout if any work has accumulated while 2163 * we've been unsuccessfully trying to detach. 2164 */ 2165 mutex_enter(&fasttrap_cleanup_mtx); 2166 fasttrap_timeout = 0; 2167 work = fasttrap_cleanup_work; 2168 mutex_exit(&fasttrap_cleanup_mtx); 2169 2170 if (work) 2171 fasttrap_pid_cleanup(); 2172 2173 (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2174 &fasttrap_meta_id); 2175 2176 return (DDI_FAILURE); 2177 } 2178 2179 #ifdef DEBUG 2180 mutex_enter(&fasttrap_count_mtx); 2181 ASSERT(fasttrap_count == 0); 2182 mutex_exit(&fasttrap_count_mtx); 2183 #endif 2184 2185 kmem_free(fasttrap_tpoints.fth_table, 2186 fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 2187 fasttrap_tpoints.fth_nent = 0; 2188 2189 kmem_free(fasttrap_provs.fth_table, 2190 fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 2191 fasttrap_provs.fth_nent = 0; 2192 2193 kmem_free(fasttrap_procs.fth_table, 2194 fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2195 fasttrap_procs.fth_nent = 0; 2196 2197 /* 2198 * We know there are no tracepoints in any process anywhere in 2199 * the system so there is no process which has its p_dtrace_count 2200 * greater than zero, therefore we know that no thread can actively 2201 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes 2202 * and fasttrap_exec() and fasttrap_exit(). 2203 */ 2204 ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork); 2205 dtrace_fasttrap_fork_ptr = NULL; 2206 2207 ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit); 2208 dtrace_fasttrap_exec_ptr = NULL; 2209 2210 ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit); 2211 dtrace_fasttrap_exit_ptr = NULL; 2212 2213 ddi_remove_minor_node(devi, NULL); 2214 2215 return (DDI_SUCCESS); 2216 } 2217 2218 static struct dev_ops fasttrap_ops = { 2219 DEVO_REV, /* devo_rev */ 2220 0, /* refcnt */ 2221 fasttrap_info, /* get_dev_info */ 2222 nulldev, /* identify */ 2223 nulldev, /* probe */ 2224 fasttrap_attach, /* attach */ 2225 fasttrap_detach, /* detach */ 2226 nodev, /* reset */ 2227 &fasttrap_cb_ops, /* driver operations */ 2228 NULL, /* bus operations */ 2229 nodev /* dev power */ 2230 }; 2231 2232 /* 2233 * Module linkage information for the kernel. 2234 */ 2235 static struct modldrv modldrv = { 2236 &mod_driverops, /* module type (this is a pseudo driver) */ 2237 "Fasttrap Tracing", /* name of module */ 2238 &fasttrap_ops, /* driver ops */ 2239 }; 2240 2241 static struct modlinkage modlinkage = { 2242 MODREV_1, 2243 (void *)&modldrv, 2244 NULL 2245 }; 2246 2247 int 2248 _init(void) 2249 { 2250 return (mod_install(&modlinkage)); 2251 } 2252 2253 int 2254 _info(struct modinfo *modinfop) 2255 { 2256 return (mod_info(&modlinkage, modinfop)); 2257 } 2258 2259 int 2260 _fini(void) 2261 { 2262 return (mod_remove(&modlinkage)); 2263 } 2264