10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 22*390Sraf 230Sstevel@tonic-gate /* 240Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/atomic.h> 310Sstevel@tonic-gate #include <sys/errno.h> 320Sstevel@tonic-gate #include <sys/stat.h> 330Sstevel@tonic-gate #include <sys/modctl.h> 340Sstevel@tonic-gate #include <sys/conf.h> 350Sstevel@tonic-gate #include <sys/systm.h> 360Sstevel@tonic-gate #include <sys/ddi.h> 370Sstevel@tonic-gate #include <sys/sunddi.h> 380Sstevel@tonic-gate #include <sys/cpuvar.h> 390Sstevel@tonic-gate #include <sys/kmem.h> 400Sstevel@tonic-gate #include <sys/strsubr.h> 410Sstevel@tonic-gate #include <sys/fasttrap.h> 420Sstevel@tonic-gate #include <sys/fasttrap_impl.h> 430Sstevel@tonic-gate #include <sys/fasttrap_isa.h> 440Sstevel@tonic-gate #include <sys/dtrace.h> 450Sstevel@tonic-gate #include <sys/dtrace_impl.h> 460Sstevel@tonic-gate #include <sys/sysmacros.h> 470Sstevel@tonic-gate #include <sys/frame.h> 480Sstevel@tonic-gate #include <sys/stack.h> 490Sstevel@tonic-gate #include <sys/proc.h> 500Sstevel@tonic-gate #include <sys/priv.h> 510Sstevel@tonic-gate #include <sys/policy.h> 520Sstevel@tonic-gate #include <sys/ontrap.h> 530Sstevel@tonic-gate #include <sys/vmsystm.h> 540Sstevel@tonic-gate #include <sys/prsystm.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <vm/as.h> 580Sstevel@tonic-gate #include <vm/seg.h> 590Sstevel@tonic-gate #include <vm/seg_dev.h> 600Sstevel@tonic-gate #include <vm/seg_vn.h> 610Sstevel@tonic-gate #include <vm/seg_spt.h> 620Sstevel@tonic-gate #include <vm/seg_kmem.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * User-Land Trap-Based Tracing 660Sstevel@tonic-gate * ---------------------------- 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * The fasttrap provider allows DTrace consumers to instrument any user-level 690Sstevel@tonic-gate * instruction to gather data; this includes probes with semantic 700Sstevel@tonic-gate * signifigance like entry and return as well as simple offsets into the 710Sstevel@tonic-gate * function. While the specific techniques used are very ISA specific, the 720Sstevel@tonic-gate * methodology is generalizable to any architecture. 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * The General Methodology 760Sstevel@tonic-gate * ----------------------- 770Sstevel@tonic-gate * 780Sstevel@tonic-gate * With the primary goal of tracing every user-land instruction and the 790Sstevel@tonic-gate * limitation that we can't trust user space so don't want to rely on much 800Sstevel@tonic-gate * information there, we begin by replacing the instructions we want to trace 810Sstevel@tonic-gate * with trap instructions. Each instruction we overwrite is saved into a hash 820Sstevel@tonic-gate * table keyed by process ID and pc address. When we enter the kernel due to 830Sstevel@tonic-gate * this trap instruction, we need the effects of the replaced instruction to 840Sstevel@tonic-gate * appear to have occurred before we proceed with the user thread's 850Sstevel@tonic-gate * execution. 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * Each user level thread is represented by a ulwp_t structure which is 880Sstevel@tonic-gate * always easily accessible through a register. The most basic way to produce 890Sstevel@tonic-gate * the effects of the instruction we replaced is to copy that instruction out 900Sstevel@tonic-gate * to a bit of scratch space reserved in the user thread's ulwp_t structure 910Sstevel@tonic-gate * (a sort of kernel-private thread local storage), set the PC to that 920Sstevel@tonic-gate * scratch space and single step. When we reenter the kernel after single 930Sstevel@tonic-gate * stepping the instruction we must then adjust the PC to point to what would 940Sstevel@tonic-gate * normally be the next instruction. Of course, special care must be taken 950Sstevel@tonic-gate * for branches and jumps, but these represent such a small fraction of any 960Sstevel@tonic-gate * instruction set that writing the code to emulate these in the kernel is 970Sstevel@tonic-gate * not too difficult. 980Sstevel@tonic-gate * 990Sstevel@tonic-gate * Return probes may require several tracepoints to trace every return site, 1000Sstevel@tonic-gate * and, conversely, each tracepoint may activate several probes (the entry 1010Sstevel@tonic-gate * and offset 0 probes, for example). To solve this muliplexing problem, 1020Sstevel@tonic-gate * tracepoints contain lists of probes to activate and probes contain lists 1030Sstevel@tonic-gate * of tracepoints to enable. If a probe is activated, it adds its ID to 1040Sstevel@tonic-gate * existing tracepoints or creates new ones as necessary. 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * Most probes are activated _before_ the instruction is executed, but return 1070Sstevel@tonic-gate * probes are activated _after_ the effects of the last instruction of the 1080Sstevel@tonic-gate * function are visible. Return probes must be fired _after_ we have 1090Sstevel@tonic-gate * single-stepped the instruction whereas all other probes are fired 1100Sstevel@tonic-gate * beforehand. 1110Sstevel@tonic-gate */ 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate static dev_info_t *fasttrap_devi; 1140Sstevel@tonic-gate static dtrace_provider_id_t fasttrap_id; 1150Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate static timeout_id_t fasttrap_timeout; 1180Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx; 1190Sstevel@tonic-gate static uint_t fasttrap_cleanup_work; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * Generation count on modifications to the global tracepoint lookup table. 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * When the fasttrap provider is loaded, fasttrap_max is set to either 1280Sstevel@tonic-gate * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the 1290Sstevel@tonic-gate * fasttrap.conf file. Each time a probe is created, fasttrap_total is 1300Sstevel@tonic-gate * incremented by the number of tracepoints that may be associated with that 1310Sstevel@tonic-gate * probe; fasttrap_total is capped at fasttrap_max. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate #define FASTTRAP_MAX_DEFAULT 250000 1340Sstevel@tonic-gate static uint32_t fasttrap_max; 1350Sstevel@tonic-gate static uint32_t fasttrap_total; 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 1390Sstevel@tonic-gate #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate #define FASTTRAP_PID_NAME "pid" 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate fasttrap_hash_t fasttrap_tpoints; 1440Sstevel@tonic-gate static fasttrap_hash_t fasttrap_provs; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate dtrace_id_t fasttrap_probe_id; 1470Sstevel@tonic-gate static int fasttrap_count; /* ref count */ 1480Sstevel@tonic-gate static int fasttrap_pid_count; /* pid ref count */ 1490Sstevel@tonic-gate static kmutex_t fasttrap_count_mtx; /* lock on ref count */ 1500Sstevel@tonic-gate 151315Sahl #define FASTTRAP_ENABLE_FAIL 1 152315Sahl #define FASTTRAP_ENABLE_PARTIAL 2 153315Sahl 1540Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t); 1550Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t); 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *, 1580Sstevel@tonic-gate const dtrace_pattr_t *); 1590Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *); 1600Sstevel@tonic-gate static void fasttrap_provider_retire(fasttrap_provider_t *); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate #define FASTTRAP_PROVS_INDEX(pid, name) \ 1630Sstevel@tonic-gate ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate static int 1660Sstevel@tonic-gate fasttrap_highbit(ulong_t i) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate int h = 1; 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate if (i == 0) 1710Sstevel@tonic-gate return (0); 1720Sstevel@tonic-gate #ifdef _LP64 1730Sstevel@tonic-gate if (i & 0xffffffff00000000ul) { 1740Sstevel@tonic-gate h += 32; i >>= 32; 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate #endif 1770Sstevel@tonic-gate if (i & 0xffff0000) { 1780Sstevel@tonic-gate h += 16; i >>= 16; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate if (i & 0xff00) { 1810Sstevel@tonic-gate h += 8; i >>= 8; 1820Sstevel@tonic-gate } 1830Sstevel@tonic-gate if (i & 0xf0) { 1840Sstevel@tonic-gate h += 4; i >>= 4; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate if (i & 0xc) { 1870Sstevel@tonic-gate h += 2; i >>= 2; 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate if (i & 0x2) { 1900Sstevel@tonic-gate h += 1; 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate return (h); 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate static uint_t 1960Sstevel@tonic-gate fasttrap_hash_str(const char *p) 1970Sstevel@tonic-gate { 1980Sstevel@tonic-gate unsigned int g; 1990Sstevel@tonic-gate uint_t hval = 0; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate while (*p) { 2020Sstevel@tonic-gate hval = (hval << 4) + *p++; 2030Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0) 2040Sstevel@tonic-gate hval ^= g >> 24; 2050Sstevel@tonic-gate hval &= ~g; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate return (hval); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate void 2110Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 2120Sstevel@tonic-gate { 2130Sstevel@tonic-gate sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate sqp->sq_info.si_signo = SIGTRAP; 2160Sstevel@tonic-gate sqp->sq_info.si_code = TRAP_DTRACE; 2170Sstevel@tonic-gate sqp->sq_info.si_addr = (caddr_t)pc; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate mutex_enter(&p->p_lock); 2200Sstevel@tonic-gate sigaddqa(p, t, sqp); 2210Sstevel@tonic-gate mutex_exit(&p->p_lock); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate if (t != NULL) 2240Sstevel@tonic-gate aston(t); 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate /* 2280Sstevel@tonic-gate * This function ensures that no threads are actively using the memory 2290Sstevel@tonic-gate * associated with probes that were formerly live. 2300Sstevel@tonic-gate */ 2310Sstevel@tonic-gate static void 2320Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen) 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate int i; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate if (gen < fasttrap_mod_gen) 2370Sstevel@tonic-gate return; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate fasttrap_mod_gen++; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 2420Sstevel@tonic-gate mutex_enter(&cpu_core[i].cpuc_pid_lock); 2430Sstevel@tonic-gate mutex_exit(&cpu_core[i].cpuc_pid_lock); 2440Sstevel@tonic-gate } 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate /* 2480Sstevel@tonic-gate * This is the timeout's callback for cleaning up the providers and their 2490Sstevel@tonic-gate * probes. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate /*ARGSUSED*/ 2520Sstevel@tonic-gate static void 2530Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data) 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp; 2560Sstevel@tonic-gate fasttrap_bucket_t *bucket; 2570Sstevel@tonic-gate dtrace_provider_id_t provid; 2580Sstevel@tonic-gate int i, later; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate static volatile int in = 0; 2610Sstevel@tonic-gate ASSERT(in == 0); 2620Sstevel@tonic-gate in = 1; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 2650Sstevel@tonic-gate while (fasttrap_cleanup_work) { 2660Sstevel@tonic-gate fasttrap_cleanup_work = 0; 2670Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate later = 0; 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate /* 2720Sstevel@tonic-gate * Iterate over all the providers trying to remove the marked 2730Sstevel@tonic-gate * ones. If a provider is marked, but not defunct, we just 2740Sstevel@tonic-gate * have to take a crack at removing it -- it's no big deal if 2750Sstevel@tonic-gate * we can't. 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2780Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[i]; 2790Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 2800Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate while ((fp = *fpp) != NULL) { 2830Sstevel@tonic-gate if (!fp->ftp_marked) { 2840Sstevel@tonic-gate fpp = &fp->ftp_next; 2850Sstevel@tonic-gate continue; 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* 2910Sstevel@tonic-gate * If this provider is referenced either 2920Sstevel@tonic-gate * because it is a USDT provider or is being 2930Sstevel@tonic-gate * modified, we can't unregister or even 2940Sstevel@tonic-gate * condense. 2950Sstevel@tonic-gate */ 2960Sstevel@tonic-gate if (fp->ftp_ccount != 0) { 2970Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 2980Sstevel@tonic-gate fp->ftp_marked = 0; 2990Sstevel@tonic-gate continue; 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate if (!fp->ftp_defunct || fp->ftp_rcount != 0) 3030Sstevel@tonic-gate fp->ftp_marked = 0; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * If we successfully unregister this 3090Sstevel@tonic-gate * provider we can remove it from the hash 3100Sstevel@tonic-gate * chain and free the memory. If our attempt 3110Sstevel@tonic-gate * to unregister fails and this is a defunct 3120Sstevel@tonic-gate * provider, increment our flag to try again 3130Sstevel@tonic-gate * pretty soon. If we've consumed more than 3140Sstevel@tonic-gate * half of our total permitted number of 3150Sstevel@tonic-gate * probes call dtrace_condense() to try to 3160Sstevel@tonic-gate * clean out the unenabled probes. 3170Sstevel@tonic-gate */ 3180Sstevel@tonic-gate provid = fp->ftp_provid; 3190Sstevel@tonic-gate if (dtrace_unregister(provid) != 0) { 3200Sstevel@tonic-gate if (fasttrap_total > fasttrap_max / 2) 3210Sstevel@tonic-gate (void) dtrace_condense(provid); 3220Sstevel@tonic-gate later += fp->ftp_marked; 3230Sstevel@tonic-gate fpp = &fp->ftp_next; 3240Sstevel@tonic-gate } else { 3250Sstevel@tonic-gate *fpp = fp->ftp_next; 3260Sstevel@tonic-gate fasttrap_provider_free(fp); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate } 3290Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate ASSERT(fasttrap_timeout != 0); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* 3380Sstevel@tonic-gate * If we were unable to remove a defunct provider, try again after 3390Sstevel@tonic-gate * a second. This situation can occur in certain circumstances where 3400Sstevel@tonic-gate * providers cannot be unregistered even though they have no probes 3410Sstevel@tonic-gate * enabled because of an execution of dtrace -l or something similar. 3420Sstevel@tonic-gate * If the timeout has been disabled (set to 1 because we're trying 3430Sstevel@tonic-gate * to detach), we set fasttrap_cleanup_work to ensure that we'll 3440Sstevel@tonic-gate * get a chance to do that work if and when the timeout is reenabled 3450Sstevel@tonic-gate * (if detach fails). 3460Sstevel@tonic-gate */ 3470Sstevel@tonic-gate if (later > 0 && fasttrap_timeout != (timeout_id_t)1) 3480Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz); 3490Sstevel@tonic-gate else if (later > 0) 3500Sstevel@tonic-gate fasttrap_cleanup_work = 1; 3510Sstevel@tonic-gate else 3520Sstevel@tonic-gate fasttrap_timeout = 0; 3530Sstevel@tonic-gate 3540Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 3550Sstevel@tonic-gate in = 0; 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate 3580Sstevel@tonic-gate /* 3590Sstevel@tonic-gate * Activates the asynchronous cleanup mechanism. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate static void 3620Sstevel@tonic-gate fasttrap_pid_cleanup(void) 3630Sstevel@tonic-gate { 3640Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 3650Sstevel@tonic-gate fasttrap_cleanup_work = 1; 3660Sstevel@tonic-gate if (fasttrap_timeout == 0) 3670Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1); 3680Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * This is called from cfork() via dtrace_fasttrap_fork(). The child 3730Sstevel@tonic-gate * process's address space is a (roughly) a copy of the parent process's so 3740Sstevel@tonic-gate * we have to remove all the instrumentation we had previously enabled in the 3750Sstevel@tonic-gate * parent. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate static void 3780Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp) 3790Sstevel@tonic-gate { 3800Sstevel@tonic-gate pid_t ppid = p->p_pid; 3810Sstevel@tonic-gate int i; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate ASSERT(curproc == p); 3840Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK); 3850Sstevel@tonic-gate ASSERT(p->p_dtrace_count > 0); 3860Sstevel@tonic-gate ASSERT(cp->p_dtrace_count == 0); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * This would be simpler and faster if we maintained per-process 3900Sstevel@tonic-gate * hash tables of enabled tracepoints. It could, however, potentially 3910Sstevel@tonic-gate * slow down execution of a tracepoint since we'd need to go 3920Sstevel@tonic-gate * through two levels of indirection. In the future, we should 3930Sstevel@tonic-gate * consider either maintaining per-process ancillary lists of 3940Sstevel@tonic-gate * enabled tracepoints or hanging a pointer to a per-process hash 3950Sstevel@tonic-gate * table of enabled tracepoints off the proc structure. 3960Sstevel@tonic-gate */ 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate /* 3990Sstevel@tonic-gate * We don't have to worry about the child process disappearing 4000Sstevel@tonic-gate * because we're in fork(). 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate mutex_enter(&cp->p_lock); 4030Sstevel@tonic-gate sprlock_proc(cp); 4040Sstevel@tonic-gate mutex_exit(&cp->p_lock); 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate /* 4070Sstevel@tonic-gate * Iterate over every tracepoint looking for ones that belong to the 4080Sstevel@tonic-gate * parent process, and remove each from the child process. 4090Sstevel@tonic-gate */ 4100Sstevel@tonic-gate for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 4110Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 4120Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 4150Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 4160Sstevel@tonic-gate if (tp->ftt_pid == ppid && !tp->ftt_prov->ftp_defunct) { 4170Sstevel@tonic-gate int ret = fasttrap_tracepoint_remove(cp, tp); 4180Sstevel@tonic-gate ASSERT(ret == 0); 4190Sstevel@tonic-gate } 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate mutex_enter(&cp->p_lock); 4250Sstevel@tonic-gate sprunlock(cp); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate /* 4290Sstevel@tonic-gate * This is called from proc_exit() or from exec_common() if p_dtrace_probes 4300Sstevel@tonic-gate * is set on the proc structure to indicate that there is a pid provider 4310Sstevel@tonic-gate * associated with this process. 4320Sstevel@tonic-gate */ 4330Sstevel@tonic-gate static void 4340Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p) 4350Sstevel@tonic-gate { 4360Sstevel@tonic-gate fasttrap_provider_t *provider; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate ASSERT(p == curproc); 4390Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate mutex_exit(&p->p_lock); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * We clean up the pid provider for this process here; user-land 4450Sstevel@tonic-gate * static probes are handled by the meta-provider remove entry point. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(p->p_pid, 4480Sstevel@tonic-gate FASTTRAP_PID_NAME, NULL)) != NULL) 4490Sstevel@tonic-gate fasttrap_provider_retire(provider); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate mutex_enter(&p->p_lock); 4520Sstevel@tonic-gate } 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate /*ARGSUSED*/ 4560Sstevel@tonic-gate static void 4570Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc) 4580Sstevel@tonic-gate { 4590Sstevel@tonic-gate /* 4600Sstevel@tonic-gate * There are no "default" pid probes. 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /*ARGSUSED*/ 4650Sstevel@tonic-gate static void 4660Sstevel@tonic-gate fasttrap_provide(void *arg, const dtrace_probedesc_t *desc) 4670Sstevel@tonic-gate { 4680Sstevel@tonic-gate if (dtrace_probe_lookup(fasttrap_id, NULL, "fasttrap", "fasttrap") == 0) 4690Sstevel@tonic-gate fasttrap_probe_id = dtrace_probe_create(fasttrap_id, NULL, 4700Sstevel@tonic-gate "fasttrap", "fasttrap", FASTTRAP_AFRAMES, NULL); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate static int 4740Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate fasttrap_tracepoint_t *tp, *new_tp = NULL; 4770Sstevel@tonic-gate fasttrap_bucket_t *bucket; 4780Sstevel@tonic-gate fasttrap_id_t *id; 4790Sstevel@tonic-gate pid_t pid; 4800Sstevel@tonic-gate uintptr_t pc; 4810Sstevel@tonic-gate 4820Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps); 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate pid = probe->ftp_pid; 4850Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc; 4860Sstevel@tonic-gate id = &probe->ftp_tps[index].fit_id; 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate /* 4930Sstevel@tonic-gate * Before we make any modifications, make sure we've imposed a barrier 4940Sstevel@tonic-gate * on the generation in which this probe was last modified. 4950Sstevel@tonic-gate */ 4960Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen); 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate /* 5010Sstevel@tonic-gate * If the tracepoint has already been enabled, just add our id to the 5020Sstevel@tonic-gate * list of interested probes. This may be our second time through 5030Sstevel@tonic-gate * this path in which case we'll have constructed the tracepoint we'd 5040Sstevel@tonic-gate * like to install. If we can't find a match, and have an allocated 5050Sstevel@tonic-gate * tracepoint ready to go, enable that one now. 5060Sstevel@tonic-gate * 5070Sstevel@tonic-gate * Tracepoints whose provider is now defunct are also considered 5080Sstevel@tonic-gate * defunct. 5090Sstevel@tonic-gate */ 5100Sstevel@tonic-gate again: 5110Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 5120Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 5130Sstevel@tonic-gate if (tp->ftt_pid != pid || tp->ftt_pc != pc || 5140Sstevel@tonic-gate tp->ftt_prov->ftp_defunct) 5150Sstevel@tonic-gate continue; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate /* 5180Sstevel@tonic-gate * Now that we've found a matching tracepoint, it would be 5190Sstevel@tonic-gate * a decent idea to confirm that the tracepoint is still 5200Sstevel@tonic-gate * enabled and the trap instruction hasn't been overwritten. 5210Sstevel@tonic-gate * Since this is a little hairy, we'll punt for now. 5220Sstevel@tonic-gate */ 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * This can't be the first interested probe. We don't have 5260Sstevel@tonic-gate * to worry about another thread being in the midst of 5270Sstevel@tonic-gate * deleting this tracepoint (which would be the only valid 5280Sstevel@tonic-gate * reason for a tracepoint to have no interested probes) 5290Sstevel@tonic-gate * since we're holding P_PR_LOCK for this process. 5300Sstevel@tonic-gate */ 5310Sstevel@tonic-gate ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (probe->ftp_type == DTFTP_RETURN || 5340Sstevel@tonic-gate probe->ftp_type == DTFTP_POST_OFFSETS) { 5350Sstevel@tonic-gate id->fti_next = tp->ftt_retids; 5360Sstevel@tonic-gate membar_producer(); 5370Sstevel@tonic-gate tp->ftt_retids = id; 5380Sstevel@tonic-gate membar_producer(); 5390Sstevel@tonic-gate } else { 5400Sstevel@tonic-gate id->fti_next = tp->ftt_ids; 5410Sstevel@tonic-gate membar_producer(); 5420Sstevel@tonic-gate tp->ftt_ids = id; 5430Sstevel@tonic-gate membar_producer(); 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate if (new_tp != NULL) { 5490Sstevel@tonic-gate new_tp->ftt_ids = NULL; 5500Sstevel@tonic-gate new_tp->ftt_retids = NULL; 5510Sstevel@tonic-gate } 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate return (0); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate /* 5570Sstevel@tonic-gate * If we have a good tracepoint ready to go, install it now while 5580Sstevel@tonic-gate * we have the lock held and no one can screw with us. 5590Sstevel@tonic-gate */ 5600Sstevel@tonic-gate if (new_tp != NULL) { 561315Sahl int rc = 0; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate new_tp->ftt_next = bucket->ftb_data; 5640Sstevel@tonic-gate membar_producer(); 5650Sstevel@tonic-gate bucket->ftb_data = new_tp; 5660Sstevel@tonic-gate membar_producer(); 5670Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate /* 570315Sahl * Activate the tracepoint in the ISA-specific manner. 571315Sahl * If this fails, we need to report the failure, but 572315Sahl * indicate that this tracepoint must still be disabled 573315Sahl * by calling fasttrap_tracepoint_disable(). 5740Sstevel@tonic-gate */ 575315Sahl if (fasttrap_tracepoint_install(p, new_tp) != 0) 576315Sahl rc = FASTTRAP_ENABLE_PARTIAL; 577315Sahl 578315Sahl /* 579315Sahl * Increment the count of the number of tracepoints active in 580315Sahl * the victim process. 581315Sahl */ 582315Sahl ASSERT(p->p_proc_flag & P_PR_LOCK); 583315Sahl p->p_dtrace_count++; 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate return (rc); 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* 5910Sstevel@tonic-gate * Initialize the tracepoint that's been preallocated with the probe. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate new_tp = probe->ftp_tps[index].fit_tp; 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate ASSERT(new_tp->ftt_pid == pid); 5960Sstevel@tonic-gate ASSERT(new_tp->ftt_pc == pc); 5970Sstevel@tonic-gate ASSERT(new_tp->ftt_prov == probe->ftp_prov); 5980Sstevel@tonic-gate ASSERT(new_tp->ftt_ids == NULL); 5990Sstevel@tonic-gate ASSERT(new_tp->ftt_retids == NULL); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (probe->ftp_type == DTFTP_RETURN || 6020Sstevel@tonic-gate probe->ftp_type == DTFTP_POST_OFFSETS) { 6030Sstevel@tonic-gate id->fti_next = NULL; 6040Sstevel@tonic-gate new_tp->ftt_retids = id; 6050Sstevel@tonic-gate } else { 6060Sstevel@tonic-gate id->fti_next = NULL; 6070Sstevel@tonic-gate new_tp->ftt_ids = id; 6080Sstevel@tonic-gate } 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate /* 611315Sahl * If the ISA-dependent initialization goes to plan, go back to the 6120Sstevel@tonic-gate * beginning and try to install this freshly made tracepoint. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate if (fasttrap_tracepoint_init(p, probe, new_tp, pc) == 0) 6150Sstevel@tonic-gate goto again; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate new_tp->ftt_ids = NULL; 6180Sstevel@tonic-gate new_tp->ftt_retids = NULL; 6190Sstevel@tonic-gate 620315Sahl return (FASTTRAP_ENABLE_FAIL); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate static void 6240Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 6250Sstevel@tonic-gate { 6260Sstevel@tonic-gate fasttrap_bucket_t *bucket; 6270Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov; 6280Sstevel@tonic-gate fasttrap_tracepoint_t **pp, *tp; 6290Sstevel@tonic-gate fasttrap_id_t *id, **idp; 6300Sstevel@tonic-gate pid_t pid; 6310Sstevel@tonic-gate uintptr_t pc; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate pid = probe->ftp_pid; 6360Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc; 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* 6410Sstevel@tonic-gate * Find the tracepoint and make sure that our id is one of the 6420Sstevel@tonic-gate * ones registered with it. 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 6450Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 6460Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 6470Sstevel@tonic-gate if (tp->ftt_pid == pid && tp->ftt_pc == pc && 6480Sstevel@tonic-gate tp->ftt_prov == provider) 6490Sstevel@tonic-gate break; 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate /* 6530Sstevel@tonic-gate * If we somehow lost this tracepoint, we're in a world of hurt. 6540Sstevel@tonic-gate */ 6550Sstevel@tonic-gate ASSERT(tp != NULL); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate if (probe->ftp_type == DTFTP_RETURN || 6580Sstevel@tonic-gate probe->ftp_type == DTFTP_POST_OFFSETS) { 6590Sstevel@tonic-gate ASSERT(tp->ftt_retids != NULL); 6600Sstevel@tonic-gate idp = &tp->ftt_retids; 6610Sstevel@tonic-gate } else { 6620Sstevel@tonic-gate ASSERT(tp->ftt_ids != NULL); 6630Sstevel@tonic-gate idp = &tp->ftt_ids; 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate while ((*idp)->fti_probe != probe) { 6670Sstevel@tonic-gate idp = &(*idp)->fti_next; 6680Sstevel@tonic-gate ASSERT(*idp != NULL); 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate id = *idp; 6720Sstevel@tonic-gate *idp = id->fti_next; 6730Sstevel@tonic-gate membar_producer(); 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate ASSERT(id->fti_probe == probe); 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate /* 6780Sstevel@tonic-gate * If there are other registered enablings of this tracepoint, we're 6790Sstevel@tonic-gate * all done, but if this was the last probe assocated with this 6800Sstevel@tonic-gate * this tracepoint, we need to remove and free it. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate /* 6850Sstevel@tonic-gate * If the current probe's tracepoint is in use, swap it 6860Sstevel@tonic-gate * for an unused tracepoint. 6870Sstevel@tonic-gate */ 6880Sstevel@tonic-gate if (tp == probe->ftp_tps[index].fit_tp) { 6890Sstevel@tonic-gate fasttrap_probe_t *tmp_probe; 6900Sstevel@tonic-gate fasttrap_tracepoint_t **tmp_tp; 6910Sstevel@tonic-gate uint_t tmp_index; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate if (tp->ftt_ids != NULL) { 6940Sstevel@tonic-gate tmp_probe = tp->ftt_ids->fti_probe; 6950Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 6960Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 6970Sstevel@tonic-gate } else { 6980Sstevel@tonic-gate tmp_probe = tp->ftt_retids->fti_probe; 6990Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 7000Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate ASSERT(*tmp_tp != NULL); 7040Sstevel@tonic-gate ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 7050Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_ids == NULL); 7060Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_retids == NULL); 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate probe->ftp_tps[index].fit_tp = *tmp_tp; 7090Sstevel@tonic-gate *tmp_tp = tp; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate } 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate /* 7160Sstevel@tonic-gate * Tag the modified probe with the generation in which it was 7170Sstevel@tonic-gate * changed. 7180Sstevel@tonic-gate */ 7190Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen; 7200Sstevel@tonic-gate return; 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate /* 7260Sstevel@tonic-gate * We can't safely remove the tracepoint from the set of active 7270Sstevel@tonic-gate * tracepoints until we've actually removed the fasttrap instruction 7280Sstevel@tonic-gate * from the process's text. We can, however, operate on this 7290Sstevel@tonic-gate * tracepoint secure in the knowledge that no other thread is going to 7300Sstevel@tonic-gate * be looking at it since we hold P_PR_LOCK on the process if it's 7310Sstevel@tonic-gate * live or we hold the provider lock on the process if it's dead and 7320Sstevel@tonic-gate * gone. 7330Sstevel@tonic-gate */ 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate /* 7360Sstevel@tonic-gate * We only need to remove the actual instruction if we're looking 7370Sstevel@tonic-gate * at an existing process 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate if (p != NULL) { 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * If we fail to restore the instruction we need to kill 7420Sstevel@tonic-gate * this process since it's in a completely unrecoverable 7430Sstevel@tonic-gate * state. 7440Sstevel@tonic-gate */ 7450Sstevel@tonic-gate if (fasttrap_tracepoint_remove(p, tp) != 0) 7460Sstevel@tonic-gate fasttrap_sigtrap(p, NULL, pc); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate /* 7490Sstevel@tonic-gate * Decrement the count of the number of tracepoints active 7500Sstevel@tonic-gate * in the victim process. 7510Sstevel@tonic-gate */ 7520Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK); 7530Sstevel@tonic-gate p->p_dtrace_count--; 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate 7560Sstevel@tonic-gate /* 7570Sstevel@tonic-gate * Remove the probe from the hash table of active tracepoints. 7580Sstevel@tonic-gate */ 7590Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 7600Sstevel@tonic-gate pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 7610Sstevel@tonic-gate ASSERT(*pp != NULL); 7620Sstevel@tonic-gate while (*pp != tp) { 7630Sstevel@tonic-gate pp = &(*pp)->ftt_next; 7640Sstevel@tonic-gate ASSERT(*pp != NULL); 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate *pp = tp->ftt_next; 7680Sstevel@tonic-gate membar_producer(); 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Tag the modified probe with the generation in which it was changed. 7740Sstevel@tonic-gate */ 7750Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen; 7760Sstevel@tonic-gate } 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate typedef int fasttrap_probe_f(struct regs *); 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate static void 7810Sstevel@tonic-gate fasttrap_enable_common(int *count, fasttrap_probe_f **fptr, fasttrap_probe_f *f, 7820Sstevel@tonic-gate fasttrap_probe_f **fptr2, fasttrap_probe_f *f2) 7830Sstevel@tonic-gate { 7840Sstevel@tonic-gate /* 7850Sstevel@tonic-gate * We don't have to play the rw lock game here because we're 7860Sstevel@tonic-gate * providing something rather than taking something away -- 7870Sstevel@tonic-gate * we can be sure that no threads have tried to follow this 7880Sstevel@tonic-gate * function pointer yet. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 7910Sstevel@tonic-gate if (*count == 0) { 7920Sstevel@tonic-gate ASSERT(*fptr == NULL); 7930Sstevel@tonic-gate *fptr = f; 7940Sstevel@tonic-gate if (fptr2 != NULL) 7950Sstevel@tonic-gate *fptr2 = f2; 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate ASSERT(*fptr == f); 7980Sstevel@tonic-gate ASSERT(fptr2 == NULL || *fptr2 == f2); 7990Sstevel@tonic-gate (*count)++; 8000Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate static void 8040Sstevel@tonic-gate fasttrap_disable_common(int *count, fasttrap_probe_f **fptr, 8050Sstevel@tonic-gate fasttrap_probe_f **fptr2) 8060Sstevel@tonic-gate { 8070Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 8100Sstevel@tonic-gate (*count)--; 8110Sstevel@tonic-gate ASSERT(*count >= 0); 8120Sstevel@tonic-gate if (*count == 0) { 8130Sstevel@tonic-gate cpu_t *cur, *cpu = CPU; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu; 8160Sstevel@tonic-gate cur = cur->cpu_next_onln) { 8170Sstevel@tonic-gate rw_enter(&cur->cpu_ft_lock, RW_WRITER); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate *fptr = NULL; 8210Sstevel@tonic-gate if (fptr2 != NULL) 8220Sstevel@tonic-gate *fptr2 = NULL; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu; 8250Sstevel@tonic-gate cur = cur->cpu_next_onln) { 8260Sstevel@tonic-gate rw_exit(&cur->cpu_ft_lock); 8270Sstevel@tonic-gate } 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate /*ARGSUSED*/ 8330Sstevel@tonic-gate static void 8340Sstevel@tonic-gate fasttrap_enable(void *arg, dtrace_id_t id, void *parg) 8350Sstevel@tonic-gate { 8360Sstevel@tonic-gate /* 8370Sstevel@tonic-gate * Enable the probe that corresponds to statically placed trace 8380Sstevel@tonic-gate * points which have not explicitly been placed in the process's text 8390Sstevel@tonic-gate * by the fasttrap provider. 8400Sstevel@tonic-gate */ 8410Sstevel@tonic-gate ASSERT(arg == NULL); 8420Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate fasttrap_enable_common(&fasttrap_count, 8450Sstevel@tonic-gate &dtrace_fasttrap_probe_ptr, fasttrap_probe, NULL, NULL); 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate /*ARGSUSED*/ 8500Sstevel@tonic-gate static void 8510Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 8520Sstevel@tonic-gate { 8530Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 8540Sstevel@tonic-gate proc_t *p; 855315Sahl int i, rc; 8560Sstevel@tonic-gate 8570Sstevel@tonic-gate ASSERT(probe != NULL); 8580Sstevel@tonic-gate ASSERT(!probe->ftp_enabled); 8590Sstevel@tonic-gate ASSERT(id == probe->ftp_id); 8600Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate /* 8630Sstevel@tonic-gate * Increment the count of enabled probes on this probe's provider; 8640Sstevel@tonic-gate * the provider can't go away while the probe still exists. We 8650Sstevel@tonic-gate * must increment this even if we aren't able to properly enable 8660Sstevel@tonic-gate * this probe. 8670Sstevel@tonic-gate */ 8680Sstevel@tonic-gate mutex_enter(&probe->ftp_prov->ftp_mtx); 8690Sstevel@tonic-gate probe->ftp_prov->ftp_rcount++; 8700Sstevel@tonic-gate mutex_exit(&probe->ftp_prov->ftp_mtx); 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate /* 8730Sstevel@tonic-gate * Bail out if we can't find the process for this probe or its 8740Sstevel@tonic-gate * provider is defunct (meaning it was valid in a previously exec'ed 8750Sstevel@tonic-gate * incarnation of this address space). The provider can't go away 8760Sstevel@tonic-gate * while we're in this code path. 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate if (probe->ftp_prov->ftp_defunct || 8790Sstevel@tonic-gate (p = sprlock(probe->ftp_pid)) == NULL) 8800Sstevel@tonic-gate return; 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 8830Sstevel@tonic-gate mutex_exit(&p->p_lock); 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate /* 8860Sstevel@tonic-gate * We have to enable the trap entry before any user threads have 8870Sstevel@tonic-gate * the chance to execute the trap instruction we're about to place 8880Sstevel@tonic-gate * in their process's text. 8890Sstevel@tonic-gate */ 8900Sstevel@tonic-gate fasttrap_enable_common(&fasttrap_pid_count, 8910Sstevel@tonic-gate &dtrace_pid_probe_ptr, fasttrap_pid_probe, 8920Sstevel@tonic-gate &dtrace_return_probe_ptr, fasttrap_return_probe); 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * Enable all the tracepoints and add this probe's id to each 8960Sstevel@tonic-gate * tracepoint's list of active probes. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 899315Sahl if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 900315Sahl /* 901315Sahl * If enabling the tracepoint failed completely, 902315Sahl * we don't have to disable it; if the failure 903315Sahl * was only partial we must disable it. 904315Sahl */ 905315Sahl if (rc == FASTTRAP_ENABLE_FAIL) 906315Sahl i--; 907315Sahl else 908315Sahl ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 909315Sahl 9100Sstevel@tonic-gate /* 9110Sstevel@tonic-gate * Back up and pull out all the tracepoints we've 9120Sstevel@tonic-gate * created so far for this probe. 9130Sstevel@tonic-gate */ 914315Sahl while (i-- >= 0) { 9150Sstevel@tonic-gate fasttrap_tracepoint_disable(p, probe, i); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate mutex_enter(&p->p_lock); 9190Sstevel@tonic-gate sprunlock(p); 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate /* 9220Sstevel@tonic-gate * Since we're not actually enabling this probe, 9230Sstevel@tonic-gate * drop our reference on the trap table entry. 9240Sstevel@tonic-gate */ 9250Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_pid_count, 9260Sstevel@tonic-gate &dtrace_pid_probe_ptr, &dtrace_return_probe_ptr); 9270Sstevel@tonic-gate return; 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate } 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate mutex_enter(&p->p_lock); 9320Sstevel@tonic-gate sprunlock(p); 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate probe->ftp_enabled = 1; 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate /*ARGSUSED*/ 9390Sstevel@tonic-gate static void 9400Sstevel@tonic-gate fasttrap_disable(void *arg, dtrace_id_t id, void *parg) 9410Sstevel@tonic-gate { 9420Sstevel@tonic-gate /* 9430Sstevel@tonic-gate * Disable the probe the corresponds to statically placed trace 9440Sstevel@tonic-gate * points. 9450Sstevel@tonic-gate */ 9460Sstevel@tonic-gate ASSERT(arg == NULL); 9470Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 9480Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 9490Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_count, &dtrace_fasttrap_probe_ptr, 9500Sstevel@tonic-gate NULL); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate /*ARGSUSED*/ 9540Sstevel@tonic-gate static void 9550Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 9560Sstevel@tonic-gate { 9570Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 9580Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov; 9590Sstevel@tonic-gate proc_t *p; 9600Sstevel@tonic-gate int i, whack = 0; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate if (!probe->ftp_enabled) { 9630Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 9640Sstevel@tonic-gate provider->ftp_rcount--; 9650Sstevel@tonic-gate ASSERT(provider->ftp_rcount >= 0); 9660Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 9670Sstevel@tonic-gate return; 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate ASSERT(id == probe->ftp_id); 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate /* 9730Sstevel@tonic-gate * We won't be able to acquire a /proc-esque lock on the process 9740Sstevel@tonic-gate * iff the process is dead and gone. In this case, we rely on the 9750Sstevel@tonic-gate * provider lock as a point of mutual exclusion to prevent other 9760Sstevel@tonic-gate * DTrace consumers from disabling this probe. 9770Sstevel@tonic-gate */ 9780Sstevel@tonic-gate if ((p = sprlock(probe->ftp_pid)) != NULL) { 9790Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 9800Sstevel@tonic-gate mutex_exit(&p->p_lock); 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 9840Sstevel@tonic-gate 9850Sstevel@tonic-gate /* 9860Sstevel@tonic-gate * Disable all the associated tracepoints. 9870Sstevel@tonic-gate */ 9880Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 9890Sstevel@tonic-gate fasttrap_tracepoint_disable(p, probe, i); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate ASSERT(provider->ftp_rcount > 0); 9930Sstevel@tonic-gate provider->ftp_rcount--; 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate if (p != NULL) { 9960Sstevel@tonic-gate /* 9970Sstevel@tonic-gate * Even though we may not be able to remove it entirely, we 9980Sstevel@tonic-gate * mark this defunct provider to get a chance to remove some 9990Sstevel@tonic-gate * of the associated probes. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate if (provider->ftp_defunct && !provider->ftp_marked) 10020Sstevel@tonic-gate whack = provider->ftp_marked = 1; 10030Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 10040Sstevel@tonic-gate 10050Sstevel@tonic-gate mutex_enter(&p->p_lock); 10060Sstevel@tonic-gate sprunlock(p); 10070Sstevel@tonic-gate } else { 10080Sstevel@tonic-gate /* 10090Sstevel@tonic-gate * If the process is dead, we're just waiting for the 10100Sstevel@tonic-gate * last probe to be disabled to be able to free it. 10110Sstevel@tonic-gate */ 10120Sstevel@tonic-gate if (provider->ftp_rcount == 0 && !provider->ftp_marked) 10130Sstevel@tonic-gate whack = provider->ftp_marked = 1; 10140Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate if (whack) 10180Sstevel@tonic-gate fasttrap_pid_cleanup(); 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate probe->ftp_enabled = 0; 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10230Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_pid_count, &dtrace_pid_probe_ptr, 10240Sstevel@tonic-gate &dtrace_return_probe_ptr); 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /*ARGSUSED*/ 10280Sstevel@tonic-gate static void 10290Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 10300Sstevel@tonic-gate dtrace_argdesc_t *desc) 10310Sstevel@tonic-gate { 10320Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 10330Sstevel@tonic-gate char *str; 10340Sstevel@tonic-gate int i; 10350Sstevel@tonic-gate 10360Sstevel@tonic-gate desc->dtargd_native[0] = '\0'; 10370Sstevel@tonic-gate desc->dtargd_xlate[0] = '\0'; 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate if (probe->ftp_prov->ftp_defunct != 0 || 10400Sstevel@tonic-gate desc->dtargd_ndx >= probe->ftp_nargs) { 10410Sstevel@tonic-gate desc->dtargd_ndx = DTRACE_ARGNONE; 10420Sstevel@tonic-gate return; 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * We only need to set this member if the argument is remapped. 10470Sstevel@tonic-gate */ 10480Sstevel@tonic-gate if (probe->ftp_argmap != NULL) 10490Sstevel@tonic-gate desc->dtargd_mapping = probe->ftp_argmap[desc->dtargd_ndx]; 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate str = probe->ftp_ntypes; 10520Sstevel@tonic-gate for (i = 0; i < desc->dtargd_mapping; i++) { 10530Sstevel@tonic-gate str += strlen(str) + 1; 10540Sstevel@tonic-gate } 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 10570Sstevel@tonic-gate (void) strcpy(desc->dtargd_native, str); 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate if (probe->ftp_xtypes == NULL) 10600Sstevel@tonic-gate return; 10610Sstevel@tonic-gate 10620Sstevel@tonic-gate str = probe->ftp_xtypes; 10630Sstevel@tonic-gate for (i = 0; i < desc->dtargd_ndx; i++) { 10640Sstevel@tonic-gate str += strlen(str) + 1; 10650Sstevel@tonic-gate } 10660Sstevel@tonic-gate 10670Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 10680Sstevel@tonic-gate (void) strcpy(desc->dtargd_xlate, str); 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate 10710Sstevel@tonic-gate /*ARGSUSED*/ 10720Sstevel@tonic-gate static void 10730Sstevel@tonic-gate fasttrap_destroy(void *arg, dtrace_id_t id, void *parg) 10740Sstevel@tonic-gate { 10750Sstevel@tonic-gate ASSERT(arg == NULL); 10760Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 10770Sstevel@tonic-gate } 10780Sstevel@tonic-gate 10790Sstevel@tonic-gate /*ARGSUSED*/ 10800Sstevel@tonic-gate static void 10810Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 10820Sstevel@tonic-gate { 10830Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 10840Sstevel@tonic-gate int i; 10850Sstevel@tonic-gate size_t size; 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate ASSERT(probe != NULL); 10880Sstevel@tonic-gate ASSERT(!probe->ftp_enabled); 10890Sstevel@tonic-gate ASSERT(fasttrap_total >= probe->ftp_ntps); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 10920Sstevel@tonic-gate size = sizeof (fasttrap_probe_t) + 10930Sstevel@tonic-gate sizeof (probe->ftp_tps[0]) * (probe->ftp_ntps - 1); 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 10960Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen); 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 10990Sstevel@tonic-gate kmem_free(probe->ftp_tps[i].fit_tp, 11000Sstevel@tonic-gate sizeof (fasttrap_tracepoint_t)); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate kmem_free(probe, size); 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate static const dtrace_pattr_t fasttrap_attr = { 11080Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11090Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11100Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11110Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11120Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11130Sstevel@tonic-gate }; 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate static dtrace_pops_t fasttrap_pops = { 11160Sstevel@tonic-gate fasttrap_provide, 11170Sstevel@tonic-gate NULL, 11180Sstevel@tonic-gate fasttrap_enable, 11190Sstevel@tonic-gate fasttrap_disable, 11200Sstevel@tonic-gate NULL, 11210Sstevel@tonic-gate NULL, 11220Sstevel@tonic-gate NULL, 11230Sstevel@tonic-gate fasttrap_getarg, 11240Sstevel@tonic-gate NULL, 11250Sstevel@tonic-gate fasttrap_destroy 11260Sstevel@tonic-gate }; 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = { 11290Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11300Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11310Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11320Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11330Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11340Sstevel@tonic-gate }; 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate static dtrace_pops_t pid_pops = { 11370Sstevel@tonic-gate fasttrap_pid_provide, 11380Sstevel@tonic-gate NULL, 11390Sstevel@tonic-gate fasttrap_pid_enable, 11400Sstevel@tonic-gate fasttrap_pid_disable, 11410Sstevel@tonic-gate NULL, 11420Sstevel@tonic-gate NULL, 11430Sstevel@tonic-gate fasttrap_pid_getargdesc, 11440Sstevel@tonic-gate fasttrap_getarg, 11450Sstevel@tonic-gate NULL, 11460Sstevel@tonic-gate fasttrap_pid_destroy 11470Sstevel@tonic-gate }; 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate static dtrace_pops_t usdt_pops = { 11500Sstevel@tonic-gate fasttrap_pid_provide, 11510Sstevel@tonic-gate NULL, 11520Sstevel@tonic-gate fasttrap_pid_enable, 11530Sstevel@tonic-gate fasttrap_pid_disable, 11540Sstevel@tonic-gate NULL, 11550Sstevel@tonic-gate NULL, 11560Sstevel@tonic-gate fasttrap_pid_getargdesc, 11570Sstevel@tonic-gate fasttrap_usdt_getarg, 11580Sstevel@tonic-gate NULL, 11590Sstevel@tonic-gate fasttrap_pid_destroy 11600Sstevel@tonic-gate }; 11610Sstevel@tonic-gate 11620Sstevel@tonic-gate /* 11630Sstevel@tonic-gate * Lookup a fasttrap-managed provider based on its name and associated pid. 11640Sstevel@tonic-gate * If the pattr argument is non-NULL, this function instantiates the provider 11650Sstevel@tonic-gate * if it doesn't exist otherwise it returns NULL. The provider is returned 11660Sstevel@tonic-gate * with its lock held. 11670Sstevel@tonic-gate */ 11680Sstevel@tonic-gate static fasttrap_provider_t * 11690Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name, 11700Sstevel@tonic-gate const dtrace_pattr_t *pattr) 11710Sstevel@tonic-gate { 11720Sstevel@tonic-gate fasttrap_provider_t *fp, *new_fp = NULL; 11730Sstevel@tonic-gate fasttrap_bucket_t *bucket; 11740Sstevel@tonic-gate char provname[DTRACE_PROVNAMELEN]; 11750Sstevel@tonic-gate proc_t *p; 11760Sstevel@tonic-gate uid_t uid = (uid_t)-1; 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate ASSERT(strlen(name) < sizeof (fp->ftp_name)); 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 11810Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate /* 11840Sstevel@tonic-gate * Take a lap through the list and return the match if we find it. 11850Sstevel@tonic-gate */ 11860Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 11870Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 11880Sstevel@tonic-gate !fp->ftp_defunct) { 11890Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 11900Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 11910Sstevel@tonic-gate return (fp); 11920Sstevel@tonic-gate } 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate 11950Sstevel@tonic-gate /* 11960Sstevel@tonic-gate * Drop the bucket lock so we don't try to perform a sleeping 11970Sstevel@tonic-gate * allocation under it. 11980Sstevel@tonic-gate */ 11990Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 12000Sstevel@tonic-gate 12010Sstevel@tonic-gate if (pattr == NULL) 12020Sstevel@tonic-gate return (NULL); 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate /* 12050Sstevel@tonic-gate * Make sure the process exists, isn't a child created as the result 12060Sstevel@tonic-gate * of a vfork(2), and isn't a zombie (but may be in fork). Record the 12070Sstevel@tonic-gate * process's uid to pass to dtrace_register(). 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate mutex_enter(&pidlock); 1210*390Sraf if ((p = prfind(pid)) == NULL) { 12110Sstevel@tonic-gate mutex_exit(&pidlock); 12120Sstevel@tonic-gate return (NULL); 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate mutex_enter(&p->p_lock); 12150Sstevel@tonic-gate mutex_exit(&pidlock); 1216*390Sraf if (p->p_flag & (SVFORK | SEXITING)) { 1217*390Sraf mutex_exit(&p->p_lock); 1218*390Sraf return (NULL); 1219*390Sraf } 12200Sstevel@tonic-gate 12210Sstevel@tonic-gate /* 12220Sstevel@tonic-gate * Increment p_dtrace_probes so that the process knows to inform us 12230Sstevel@tonic-gate * when it exits or execs. fasttrap_provider_free() decrements this 12240Sstevel@tonic-gate * when we're done with this provider. 12250Sstevel@tonic-gate */ 12260Sstevel@tonic-gate p->p_dtrace_probes++; 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate mutex_enter(&p->p_crlock); 12290Sstevel@tonic-gate uid = crgetruid(p->p_cred); 12300Sstevel@tonic-gate mutex_exit(&p->p_crlock); 12310Sstevel@tonic-gate mutex_exit(&p->p_lock); 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate /* 12380Sstevel@tonic-gate * Take another lap through the list to make sure a provider hasn't 12390Sstevel@tonic-gate * been created for this pid while we weren't under the bucket lock. 12400Sstevel@tonic-gate */ 12410Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 12420Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 12430Sstevel@tonic-gate !fp->ftp_defunct) { 12440Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 12450Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 12460Sstevel@tonic-gate fasttrap_provider_free(new_fp); 12470Sstevel@tonic-gate return (fp); 12480Sstevel@tonic-gate } 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate new_fp->ftp_pid = pid; 12520Sstevel@tonic-gate (void) strcpy(new_fp->ftp_name, name); 12530Sstevel@tonic-gate 12540Sstevel@tonic-gate /* 12550Sstevel@tonic-gate * Fail and return NULL if either the provider name is too long 12560Sstevel@tonic-gate * or we fail to register this new provider with the DTrace 12570Sstevel@tonic-gate * framework. Note that this is the only place we ever construct 12580Sstevel@tonic-gate * the full provider name -- we keep it in pieces in the provider 12590Sstevel@tonic-gate * structure. 12600Sstevel@tonic-gate */ 12610Sstevel@tonic-gate if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 12620Sstevel@tonic-gate sizeof (provname) || 12630Sstevel@tonic-gate dtrace_register(provname, pattr, 12640Sstevel@tonic-gate DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER, uid, 12650Sstevel@tonic-gate pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 12660Sstevel@tonic-gate &new_fp->ftp_provid) != 0) { 12670Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 12680Sstevel@tonic-gate fasttrap_provider_free(new_fp); 12690Sstevel@tonic-gate return (NULL); 12700Sstevel@tonic-gate } 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate new_fp->ftp_next = bucket->ftb_data; 12730Sstevel@tonic-gate bucket->ftb_data = new_fp; 12740Sstevel@tonic-gate 12750Sstevel@tonic-gate mutex_enter(&new_fp->ftp_mtx); 12760Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate return (new_fp); 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate static void 12820Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider) 12830Sstevel@tonic-gate { 12840Sstevel@tonic-gate pid_t pid = provider->ftp_pid; 12850Sstevel@tonic-gate proc_t *p; 12860Sstevel@tonic-gate 12870Sstevel@tonic-gate /* 12880Sstevel@tonic-gate * There need to be no consumers using this provider and no 12890Sstevel@tonic-gate * associated enabled probes. 12900Sstevel@tonic-gate */ 12910Sstevel@tonic-gate ASSERT(provider->ftp_ccount == 0); 12920Sstevel@tonic-gate ASSERT(provider->ftp_rcount == 0); 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate kmem_free(provider, sizeof (fasttrap_provider_t)); 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate /* 12970Sstevel@tonic-gate * Decrement p_dtrace_probes on the process whose provider we're 12980Sstevel@tonic-gate * freeing. We don't have to worry about clobbering somone else's 12990Sstevel@tonic-gate * modifications to it because we have locked the bucket that 13000Sstevel@tonic-gate * corresponds to this process's hash chain in the provider hash 13010Sstevel@tonic-gate * table. Don't sweat it if we can't find the process. 13020Sstevel@tonic-gate */ 13030Sstevel@tonic-gate mutex_enter(&pidlock); 13040Sstevel@tonic-gate if ((p = prfind(pid)) == NULL) { 13050Sstevel@tonic-gate mutex_exit(&pidlock); 13060Sstevel@tonic-gate return; 13070Sstevel@tonic-gate } 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate mutex_enter(&p->p_lock); 13100Sstevel@tonic-gate mutex_exit(&pidlock); 13110Sstevel@tonic-gate 13120Sstevel@tonic-gate p->p_dtrace_probes--; 13130Sstevel@tonic-gate mutex_exit(&p->p_lock); 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate static void 13170Sstevel@tonic-gate fasttrap_provider_retire(fasttrap_provider_t *provider) 13180Sstevel@tonic-gate { 13190Sstevel@tonic-gate dtrace_provider_id_t provid = provider->ftp_provid; 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * Mark the provider to be removed in our post-processing step 13230Sstevel@tonic-gate * and mark it as defunct. The former indicates that we should try 13240Sstevel@tonic-gate * to remove it, the latter indicates that even if we were unable 13250Sstevel@tonic-gate * to remove it, this provider shouldn't be used to create probes 13260Sstevel@tonic-gate * in the future. 13270Sstevel@tonic-gate */ 13280Sstevel@tonic-gate provider->ftp_defunct = 1; 13290Sstevel@tonic-gate provider->ftp_marked = 1; 13300Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate /* 13330Sstevel@tonic-gate * We don't have to worry about invalidating the same provider twice 13340Sstevel@tonic-gate * since fasttrap_provider_lookup() will ignore provider that have 13350Sstevel@tonic-gate * been marked as defunct. 13360Sstevel@tonic-gate */ 13370Sstevel@tonic-gate dtrace_invalidate(provid); 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate fasttrap_pid_cleanup(); 13400Sstevel@tonic-gate } 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate static int 13430Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata) 13440Sstevel@tonic-gate { 13450Sstevel@tonic-gate fasttrap_provider_t *provider; 13460Sstevel@tonic-gate fasttrap_probe_t *pp; 13470Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 13480Sstevel@tonic-gate char *name; 13490Sstevel@tonic-gate size_t size; 13500Sstevel@tonic-gate int i, aframes, whack; 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate switch (pdata->ftps_type) { 13530Sstevel@tonic-gate case DTFTP_ENTRY: 13540Sstevel@tonic-gate name = "entry"; 13550Sstevel@tonic-gate aframes = FASTTRAP_ENTRY_AFRAMES; 13560Sstevel@tonic-gate break; 13570Sstevel@tonic-gate case DTFTP_RETURN: 13580Sstevel@tonic-gate name = "return"; 13590Sstevel@tonic-gate aframes = FASTTRAP_RETURN_AFRAMES; 13600Sstevel@tonic-gate break; 13610Sstevel@tonic-gate case DTFTP_OFFSETS: 13620Sstevel@tonic-gate name = NULL; 13630Sstevel@tonic-gate break; 13640Sstevel@tonic-gate default: 13650Sstevel@tonic-gate return (EINVAL); 13660Sstevel@tonic-gate } 13670Sstevel@tonic-gate 13680Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 13690Sstevel@tonic-gate FASTTRAP_PID_NAME, &pid_attr)) == NULL) 13700Sstevel@tonic-gate return (ESRCH); 13710Sstevel@tonic-gate 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * Increment this reference count to indicate that a consumer is 13740Sstevel@tonic-gate * actively adding a new probe associated with this provider. 13750Sstevel@tonic-gate */ 13760Sstevel@tonic-gate provider->ftp_ccount++; 13770Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate if (name != NULL) { 13800Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, 13810Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name) != 0) 13820Sstevel@tonic-gate goto done; 13830Sstevel@tonic-gate 13840Sstevel@tonic-gate atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 13870Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 13880Sstevel@tonic-gate goto no_mem; 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate ASSERT(pdata->ftps_noffs > 0); 13920Sstevel@tonic-gate size = sizeof (fasttrap_probe_t) + 13930Sstevel@tonic-gate sizeof (pp->ftp_tps[0]) * (pdata->ftps_noffs - 1); 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate pp = kmem_zalloc(size, KM_SLEEP); 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate pp->ftp_prov = provider; 13980Sstevel@tonic-gate pp->ftp_faddr = pdata->ftps_pc; 13990Sstevel@tonic-gate pp->ftp_fsize = pdata->ftps_size; 14000Sstevel@tonic-gate pp->ftp_pid = pdata->ftps_pid; 14010Sstevel@tonic-gate pp->ftp_ntps = pdata->ftps_noffs; 14020Sstevel@tonic-gate pp->ftp_type = pdata->ftps_type; 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate for (i = 0; i < pdata->ftps_noffs; i++) { 14050Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 14060Sstevel@tonic-gate KM_SLEEP); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate tp->ftt_prov = provider; 14090Sstevel@tonic-gate tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 14100Sstevel@tonic-gate tp->ftt_pid = pdata->ftps_pid; 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate pp->ftp_tps[i].fit_tp = tp; 14130Sstevel@tonic-gate pp->ftp_tps[i].fit_id.fti_probe = pp; 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 14170Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 14180Sstevel@tonic-gate } else { 14190Sstevel@tonic-gate for (i = 0; i < pdata->ftps_noffs; i++) { 14200Sstevel@tonic-gate char name_str[17]; 14210Sstevel@tonic-gate 14220Sstevel@tonic-gate (void) sprintf(name_str, "%llx", 14230Sstevel@tonic-gate (unsigned long long)pdata->ftps_offs[i]); 14240Sstevel@tonic-gate 14250Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, 14260Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 14270Sstevel@tonic-gate continue; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate atomic_add_32(&fasttrap_total, 1); 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 14320Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -1); 14330Sstevel@tonic-gate goto no_mem; 14340Sstevel@tonic-gate } 14350Sstevel@tonic-gate 14360Sstevel@tonic-gate pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate pp->ftp_prov = provider; 14390Sstevel@tonic-gate pp->ftp_faddr = pdata->ftps_pc; 14400Sstevel@tonic-gate pp->ftp_fsize = pdata->ftps_size; 14410Sstevel@tonic-gate pp->ftp_pid = pdata->ftps_pid; 14420Sstevel@tonic-gate pp->ftp_ntps = 1; 14430Sstevel@tonic-gate pp->ftp_type = pdata->ftps_type; 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 14460Sstevel@tonic-gate KM_SLEEP); 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate tp->ftt_prov = provider; 14490Sstevel@tonic-gate tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 14500Sstevel@tonic-gate tp->ftt_pid = pdata->ftps_pid; 14510Sstevel@tonic-gate 14520Sstevel@tonic-gate pp->ftp_tps[0].fit_tp = tp; 14530Sstevel@tonic-gate pp->ftp_tps[0].fit_id.fti_probe = pp; 14540Sstevel@tonic-gate 14550Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 14560Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str, 14570Sstevel@tonic-gate FASTTRAP_OFFSET_AFRAMES, pp); 14580Sstevel@tonic-gate } 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate 14610Sstevel@tonic-gate done: 14620Sstevel@tonic-gate /* 14630Sstevel@tonic-gate * We know that the provider is still valid since we incremented the 14640Sstevel@tonic-gate * reference count. If someone tried to free this provider while we 14650Sstevel@tonic-gate * were using it (e.g. because the process called exec(2) or exit(2)), 14660Sstevel@tonic-gate * take note of that and try to free it now. 14670Sstevel@tonic-gate */ 14680Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 14690Sstevel@tonic-gate provider->ftp_ccount--; 14700Sstevel@tonic-gate whack = provider->ftp_defunct; 14710Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate if (whack) 14740Sstevel@tonic-gate fasttrap_pid_cleanup(); 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate return (0); 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate no_mem: 14790Sstevel@tonic-gate /* 14800Sstevel@tonic-gate * If we've exhausted the allowable resources, we'll try to remove 14810Sstevel@tonic-gate * this provider to free some up. This is to cover the case where 14820Sstevel@tonic-gate * the user has accidentally created many more probes than was 14830Sstevel@tonic-gate * intended (e.g. pid123:::). 14840Sstevel@tonic-gate */ 14850Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 14860Sstevel@tonic-gate provider->ftp_ccount--; 14870Sstevel@tonic-gate provider->ftp_marked = 1; 14880Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate fasttrap_pid_cleanup(); 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate return (ENOMEM); 14930Sstevel@tonic-gate } 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate /*ARGSUSED*/ 14960Sstevel@tonic-gate static void * 14970Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 14980Sstevel@tonic-gate { 14990Sstevel@tonic-gate fasttrap_provider_t *provider; 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate /* 15020Sstevel@tonic-gate * A 32-bit unsigned integer (like a pid for example) can be 15030Sstevel@tonic-gate * expressed in 10 or fewer decimal digits. Make sure that we'll 15040Sstevel@tonic-gate * have enough space for the provider name. 15050Sstevel@tonic-gate */ 15060Sstevel@tonic-gate if (strlen(dhpv->dthpv_provname) + 10 >= 15070Sstevel@tonic-gate sizeof (provider->ftp_name)) { 15080Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: " 15090Sstevel@tonic-gate "name too long to accomodate pid", dhpv->dthpv_provname); 15100Sstevel@tonic-gate return (NULL); 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate /* 15140Sstevel@tonic-gate * Don't let folks spoof the true pid provider. 15150Sstevel@tonic-gate */ 15160Sstevel@tonic-gate if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 15170Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: " 15180Sstevel@tonic-gate "%s is an invalid name", dhpv->dthpv_provname, 15190Sstevel@tonic-gate FASTTRAP_PID_NAME); 15200Sstevel@tonic-gate return (NULL); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate /* 15240Sstevel@tonic-gate * The highest stability class that fasttrap supports is ISA; cap 15250Sstevel@tonic-gate * the stability of the new provider accordingly. 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_provider.dtat_class >= DTRACE_CLASS_COMMON) 15280Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 15290Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_mod.dtat_class >= DTRACE_CLASS_COMMON) 15300Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 15310Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_func.dtat_class >= DTRACE_CLASS_COMMON) 15320Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 15330Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_name.dtat_class >= DTRACE_CLASS_COMMON) 15340Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 15350Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_args.dtat_class >= DTRACE_CLASS_COMMON) 15360Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 15390Sstevel@tonic-gate &dhpv->dthpv_pattr)) == NULL) { 15400Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s for " 15410Sstevel@tonic-gate "process %u", dhpv->dthpv_provname, (uint_t)pid); 15420Sstevel@tonic-gate return (NULL); 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate 15450Sstevel@tonic-gate /* 15460Sstevel@tonic-gate * We elevate the consumer count here to ensure that this provider 15470Sstevel@tonic-gate * isn't removed until after the meta provider has been told to 15480Sstevel@tonic-gate * remove it. 15490Sstevel@tonic-gate */ 15500Sstevel@tonic-gate provider->ftp_ccount++; 15510Sstevel@tonic-gate 15520Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 15530Sstevel@tonic-gate 15540Sstevel@tonic-gate return (provider); 15550Sstevel@tonic-gate } 15560Sstevel@tonic-gate 15570Sstevel@tonic-gate /*ARGSUSED*/ 15580Sstevel@tonic-gate static void 15590Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg, 15600Sstevel@tonic-gate dtrace_helper_probedesc_t *dhpb) 15610Sstevel@tonic-gate { 15620Sstevel@tonic-gate fasttrap_provider_t *provider = parg; 15630Sstevel@tonic-gate fasttrap_probe_t *pp; 15640Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 15650Sstevel@tonic-gate size_t size; 15660Sstevel@tonic-gate int i; 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 15710Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 15720Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 15730Sstevel@tonic-gate return; 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate atomic_add_32(&fasttrap_total, dhpb->dthpb_noffs); 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 15790Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -dhpb->dthpb_noffs); 15800Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 15810Sstevel@tonic-gate return; 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate 15840Sstevel@tonic-gate size = sizeof (fasttrap_probe_t) + 15850Sstevel@tonic-gate sizeof (pp->ftp_tps[0]) * (dhpb->dthpb_noffs - 1); 15860Sstevel@tonic-gate pp = kmem_zalloc(size, KM_SLEEP); 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate pp->ftp_prov = provider; 15890Sstevel@tonic-gate pp->ftp_pid = provider->ftp_pid; 15900Sstevel@tonic-gate pp->ftp_ntps = dhpb->dthpb_noffs; 15910Sstevel@tonic-gate #ifdef __sparc 15920Sstevel@tonic-gate pp->ftp_type = DTFTP_POST_OFFSETS; 15930Sstevel@tonic-gate #else 15940Sstevel@tonic-gate pp->ftp_type = DTFTP_OFFSETS; 15950Sstevel@tonic-gate #endif 15960Sstevel@tonic-gate pp->ftp_nargs = dhpb->dthpb_xargc; 15970Sstevel@tonic-gate pp->ftp_xtypes = dhpb->dthpb_xtypes; 15980Sstevel@tonic-gate pp->ftp_ntypes = dhpb->dthpb_ntypes; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate for (i = 0; i < pp->ftp_ntps; i++) { 16010Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate tp->ftt_prov = provider; 16040Sstevel@tonic-gate tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 16050Sstevel@tonic-gate tp->ftt_pid = provider->ftp_pid; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate pp->ftp_tps[i].fit_tp = tp; 16080Sstevel@tonic-gate pp->ftp_tps[i].fit_id.fti_probe = pp; 16090Sstevel@tonic-gate } 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate /* 16120Sstevel@tonic-gate * If the arguments are shuffled around we set the argument remapping 16130Sstevel@tonic-gate * table. Later, when the probe fires, we only remap the arguments 16140Sstevel@tonic-gate * if the table is non-NULL. 16150Sstevel@tonic-gate */ 16160Sstevel@tonic-gate for (i = 0; i < dhpb->dthpb_xargc; i++) { 16170Sstevel@tonic-gate if (dhpb->dthpb_args[i] != i) { 16180Sstevel@tonic-gate pp->ftp_argmap = dhpb->dthpb_args; 16190Sstevel@tonic-gate break; 16200Sstevel@tonic-gate } 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate /* 16240Sstevel@tonic-gate * The probe is fully constructed -- register it with DTrace. 16250Sstevel@tonic-gate */ 16260Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 16270Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 16300Sstevel@tonic-gate } 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate /*ARGSUSED*/ 16330Sstevel@tonic-gate static void 16340Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 16350Sstevel@tonic-gate { 16360Sstevel@tonic-gate fasttrap_provider_t *provider; 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pid, 16390Sstevel@tonic-gate dhpv->dthpv_provname, NULL)) != NULL) { 16400Sstevel@tonic-gate /* 16410Sstevel@tonic-gate * Drop the consumer count now that we're done with this 16420Sstevel@tonic-gate * provider. If there are no other consumers retire it now. 16430Sstevel@tonic-gate */ 16440Sstevel@tonic-gate if (--provider->ftp_ccount == 0) 16450Sstevel@tonic-gate fasttrap_provider_retire(provider); 16460Sstevel@tonic-gate else 16470Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = { 16520Sstevel@tonic-gate fasttrap_meta_create_probe, 16530Sstevel@tonic-gate fasttrap_meta_provide, 16540Sstevel@tonic-gate fasttrap_meta_remove 16550Sstevel@tonic-gate }; 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate /*ARGSUSED*/ 16580Sstevel@tonic-gate static int 16590Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 16600Sstevel@tonic-gate { 16610Sstevel@tonic-gate return (0); 16620Sstevel@tonic-gate } 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate /*ARGSUSED*/ 16650Sstevel@tonic-gate static int 16660Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 16670Sstevel@tonic-gate { 16680Sstevel@tonic-gate if (!dtrace_attached()) 16690Sstevel@tonic-gate return (EAGAIN); 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate if (cmd == FASTTRAPIOC_MAKEPROBE) { 16720Sstevel@tonic-gate fasttrap_probe_spec_t *uprobe = (void *)arg; 16730Sstevel@tonic-gate fasttrap_probe_spec_t *probe; 16740Sstevel@tonic-gate uint64_t noffs; 16750Sstevel@tonic-gate size_t size; 16760Sstevel@tonic-gate int ret; 16770Sstevel@tonic-gate char *c; 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate if (copyin(&uprobe->ftps_noffs, &noffs, 16800Sstevel@tonic-gate sizeof (uprobe->ftps_noffs))) 16810Sstevel@tonic-gate return (EFAULT); 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate /* 16840Sstevel@tonic-gate * Probes must have at least one tracepoint. 16850Sstevel@tonic-gate */ 16860Sstevel@tonic-gate if (noffs == 0) 16870Sstevel@tonic-gate return (EINVAL); 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate size = sizeof (fasttrap_probe_spec_t) + 16900Sstevel@tonic-gate sizeof (probe->ftps_offs[0]) * (noffs - 1); 16910Sstevel@tonic-gate 16920Sstevel@tonic-gate if (size > 1024 * 1024) 16930Sstevel@tonic-gate return (ENOMEM); 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate probe = kmem_alloc(size, KM_SLEEP); 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate if (copyin(uprobe, probe, size) != 0) { 16980Sstevel@tonic-gate kmem_free(probe, size); 16990Sstevel@tonic-gate return (EFAULT); 17000Sstevel@tonic-gate } 17010Sstevel@tonic-gate 17020Sstevel@tonic-gate /* 17030Sstevel@tonic-gate * Verify that the function and module strings contain no 17040Sstevel@tonic-gate * funny characters. 17050Sstevel@tonic-gate */ 17060Sstevel@tonic-gate for (c = &probe->ftps_func[0]; *c != '\0'; c++) { 17070Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) { 17080Sstevel@tonic-gate ret = EINVAL; 17090Sstevel@tonic-gate goto err; 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate 17130Sstevel@tonic-gate for (c = &probe->ftps_mod[0]; *c != '\0'; c++) { 17140Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) { 17150Sstevel@tonic-gate ret = EINVAL; 17160Sstevel@tonic-gate goto err; 17170Sstevel@tonic-gate } 17180Sstevel@tonic-gate } 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 17210Sstevel@tonic-gate proc_t *p; 17220Sstevel@tonic-gate pid_t pid = probe->ftps_pid; 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate mutex_enter(&pidlock); 17250Sstevel@tonic-gate /* 17260Sstevel@tonic-gate * Report an error if the process doesn't exist 17270Sstevel@tonic-gate * or is actively being birthed. 17280Sstevel@tonic-gate */ 17290Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 17300Sstevel@tonic-gate mutex_exit(&pidlock); 17310Sstevel@tonic-gate return (ESRCH); 17320Sstevel@tonic-gate } 17330Sstevel@tonic-gate mutex_enter(&p->p_lock); 17340Sstevel@tonic-gate mutex_exit(&pidlock); 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL, 17370Sstevel@tonic-gate VREAD | VWRITE)) != 0) { 17380Sstevel@tonic-gate mutex_exit(&p->p_lock); 17390Sstevel@tonic-gate return (ret); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate mutex_exit(&p->p_lock); 17430Sstevel@tonic-gate } 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate ret = fasttrap_add_probe(probe); 17460Sstevel@tonic-gate err: 17470Sstevel@tonic-gate kmem_free(probe, size); 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate return (ret); 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate } else if (cmd == FASTTRAPIOC_GETINSTR) { 17520Sstevel@tonic-gate fasttrap_instr_query_t instr; 17530Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 17540Sstevel@tonic-gate uint_t index; 17550Sstevel@tonic-gate int ret; 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 17580Sstevel@tonic-gate return (EFAULT); 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 17610Sstevel@tonic-gate proc_t *p; 17620Sstevel@tonic-gate pid_t pid = instr.ftiq_pid; 17630Sstevel@tonic-gate 17640Sstevel@tonic-gate mutex_enter(&pidlock); 17650Sstevel@tonic-gate /* 17660Sstevel@tonic-gate * Report an error if the process doesn't exist 17670Sstevel@tonic-gate * or is actively being birthed. 17680Sstevel@tonic-gate */ 17690Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 17700Sstevel@tonic-gate mutex_exit(&pidlock); 17710Sstevel@tonic-gate return (ESRCH); 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate mutex_enter(&p->p_lock); 17740Sstevel@tonic-gate mutex_exit(&pidlock); 17750Sstevel@tonic-gate 17760Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL, 17770Sstevel@tonic-gate VREAD)) != 0) { 17780Sstevel@tonic-gate mutex_exit(&p->p_lock); 17790Sstevel@tonic-gate return (ret); 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate mutex_exit(&p->p_lock); 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate 17850Sstevel@tonic-gate index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 17880Sstevel@tonic-gate tp = fasttrap_tpoints.fth_table[index].ftb_data; 17890Sstevel@tonic-gate while (tp != NULL) { 17900Sstevel@tonic-gate if (instr.ftiq_pid == tp->ftt_pid && 17910Sstevel@tonic-gate instr.ftiq_pc == tp->ftt_pc && 17920Sstevel@tonic-gate !tp->ftt_prov->ftp_defunct) 17930Sstevel@tonic-gate break; 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate tp = tp->ftt_next; 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate 17980Sstevel@tonic-gate if (tp == NULL) { 17990Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 18000Sstevel@tonic-gate return (ENOENT); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate bcopy(&tp->ftt_instr, &instr.ftiq_instr, 18040Sstevel@tonic-gate sizeof (instr.ftiq_instr)); 18050Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 18080Sstevel@tonic-gate return (EFAULT); 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate return (0); 18110Sstevel@tonic-gate } 18120Sstevel@tonic-gate 18130Sstevel@tonic-gate return (EINVAL); 18140Sstevel@tonic-gate } 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = { 18170Sstevel@tonic-gate fasttrap_open, /* open */ 18180Sstevel@tonic-gate nodev, /* close */ 18190Sstevel@tonic-gate nulldev, /* strategy */ 18200Sstevel@tonic-gate nulldev, /* print */ 18210Sstevel@tonic-gate nodev, /* dump */ 18220Sstevel@tonic-gate nodev, /* read */ 18230Sstevel@tonic-gate nodev, /* write */ 18240Sstevel@tonic-gate fasttrap_ioctl, /* ioctl */ 18250Sstevel@tonic-gate nodev, /* devmap */ 18260Sstevel@tonic-gate nodev, /* mmap */ 18270Sstevel@tonic-gate nodev, /* segmap */ 18280Sstevel@tonic-gate nochpoll, /* poll */ 18290Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 18300Sstevel@tonic-gate 0, /* streamtab */ 18310Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 18320Sstevel@tonic-gate }; 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate /*ARGSUSED*/ 18350Sstevel@tonic-gate static int 18360Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 18370Sstevel@tonic-gate { 18380Sstevel@tonic-gate int error; 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate switch (infocmd) { 18410Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 18420Sstevel@tonic-gate *result = (void *)fasttrap_devi; 18430Sstevel@tonic-gate error = DDI_SUCCESS; 18440Sstevel@tonic-gate break; 18450Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 18460Sstevel@tonic-gate *result = (void *)0; 18470Sstevel@tonic-gate error = DDI_SUCCESS; 18480Sstevel@tonic-gate break; 18490Sstevel@tonic-gate default: 18500Sstevel@tonic-gate error = DDI_FAILURE; 18510Sstevel@tonic-gate } 18520Sstevel@tonic-gate return (error); 18530Sstevel@tonic-gate } 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate static int 18560Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 18570Sstevel@tonic-gate { 18580Sstevel@tonic-gate ulong_t nent; 18590Sstevel@tonic-gate 18600Sstevel@tonic-gate switch (cmd) { 18610Sstevel@tonic-gate case DDI_ATTACH: 18620Sstevel@tonic-gate break; 18630Sstevel@tonic-gate case DDI_RESUME: 18640Sstevel@tonic-gate return (DDI_SUCCESS); 18650Sstevel@tonic-gate default: 18660Sstevel@tonic-gate return (DDI_FAILURE); 18670Sstevel@tonic-gate } 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0, 18700Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE || 18710Sstevel@tonic-gate dtrace_register("fasttrap", &fasttrap_attr, DTRACE_PRIV_USER, 0, 18720Sstevel@tonic-gate &fasttrap_pops, NULL, &fasttrap_id) != 0) { 18730Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 18740Sstevel@tonic-gate return (DDI_FAILURE); 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate 18770Sstevel@tonic-gate ddi_report_dev(devi); 18780Sstevel@tonic-gate fasttrap_devi = devi; 18790Sstevel@tonic-gate 18800Sstevel@tonic-gate /* 18810Sstevel@tonic-gate * Install our hooks into fork(2), exec(2), and exit(2). 18820Sstevel@tonic-gate */ 18830Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = &fasttrap_fork; 18840Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit; 18850Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit; 18860Sstevel@tonic-gate 18870Sstevel@tonic-gate fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 18880Sstevel@tonic-gate "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 18890Sstevel@tonic-gate fasttrap_total = 0; 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Conjure up the tracepoints hashtable... 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 18950Sstevel@tonic-gate "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 18960Sstevel@tonic-gate 18970Sstevel@tonic-gate if (nent <= 0 || nent > 0x1000000) 18980Sstevel@tonic-gate nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate if ((nent & (nent - 1)) == 0) 19010Sstevel@tonic-gate fasttrap_tpoints.fth_nent = nent; 19020Sstevel@tonic-gate else 19030Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 19040Sstevel@tonic-gate ASSERT(fasttrap_tpoints.fth_nent > 0); 19050Sstevel@tonic-gate fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 19060Sstevel@tonic-gate fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 19070Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP); 19080Sstevel@tonic-gate 19090Sstevel@tonic-gate /* 19100Sstevel@tonic-gate * ... and the providers hash table. 19110Sstevel@tonic-gate */ 19120Sstevel@tonic-gate nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 19130Sstevel@tonic-gate if ((nent & (nent - 1)) == 0) 19140Sstevel@tonic-gate fasttrap_provs.fth_nent = nent; 19150Sstevel@tonic-gate else 19160Sstevel@tonic-gate fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 19170Sstevel@tonic-gate ASSERT(fasttrap_provs.fth_nent > 0); 19180Sstevel@tonic-gate fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 19190Sstevel@tonic-gate 19200Sstevel@tonic-gate fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 19210Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP); 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 19240Sstevel@tonic-gate &fasttrap_meta_id); 19250Sstevel@tonic-gate 19260Sstevel@tonic-gate return (DDI_SUCCESS); 19270Sstevel@tonic-gate } 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate static int 19300Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 19310Sstevel@tonic-gate { 19320Sstevel@tonic-gate int i, fail = 0; 19330Sstevel@tonic-gate timeout_id_t tmp; 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate switch (cmd) { 19360Sstevel@tonic-gate case DDI_DETACH: 19370Sstevel@tonic-gate break; 19380Sstevel@tonic-gate case DDI_SUSPEND: 19390Sstevel@tonic-gate return (DDI_SUCCESS); 19400Sstevel@tonic-gate default: 19410Sstevel@tonic-gate return (DDI_FAILURE); 19420Sstevel@tonic-gate } 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate /* 19450Sstevel@tonic-gate * Unregister the meta-provider to make sure no new fasttrap- 19460Sstevel@tonic-gate * managed providers come along while we're trying to close up 19470Sstevel@tonic-gate * shop. If we fail to detach, we'll need to re-register as a 19480Sstevel@tonic-gate * meta-provider. We can fail to unregister as a meta-provider 19490Sstevel@tonic-gate * if providers we manage still exist. 19500Sstevel@tonic-gate */ 19510Sstevel@tonic-gate if (fasttrap_meta_id != DTRACE_METAPROVNONE && 19520Sstevel@tonic-gate dtrace_meta_unregister(fasttrap_meta_id) != 0) 19530Sstevel@tonic-gate return (DDI_FAILURE); 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate /* 19560Sstevel@tonic-gate * Prevent any new timeouts from running by setting fasttrap_timeout 19570Sstevel@tonic-gate * to a non-zero value, and wait for the current timeout to complete. 19580Sstevel@tonic-gate */ 19590Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 19600Sstevel@tonic-gate fasttrap_cleanup_work = 0; 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate while (fasttrap_timeout != (timeout_id_t)1) { 19630Sstevel@tonic-gate tmp = fasttrap_timeout; 19640Sstevel@tonic-gate fasttrap_timeout = (timeout_id_t)1; 19650Sstevel@tonic-gate 19660Sstevel@tonic-gate if (tmp != 0) { 19670Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 19680Sstevel@tonic-gate (void) untimeout(tmp); 19690Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 19700Sstevel@tonic-gate } 19710Sstevel@tonic-gate } 19720Sstevel@tonic-gate 19730Sstevel@tonic-gate fasttrap_cleanup_work = 0; 19740Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 19750Sstevel@tonic-gate 19760Sstevel@tonic-gate /* 19770Sstevel@tonic-gate * Iterate over all of our providers. If there's still a process 19780Sstevel@tonic-gate * that corresponds to that pid, fail to detach. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) { 19810Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp; 19820Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 19850Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data; 19860Sstevel@tonic-gate while ((fp = *fpp) != NULL) { 19870Sstevel@tonic-gate /* 19880Sstevel@tonic-gate * Acquire and release the lock as a simple way of 19890Sstevel@tonic-gate * waiting for any other consumer to finish with 19900Sstevel@tonic-gate * this provider. A thread must first acquire the 19910Sstevel@tonic-gate * bucket lock so there's no chance of another thread 19920Sstevel@tonic-gate * blocking on the providers lock. 19930Sstevel@tonic-gate */ 19940Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 19950Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 19960Sstevel@tonic-gate 19970Sstevel@tonic-gate if (dtrace_unregister(fp->ftp_provid) != 0) { 19980Sstevel@tonic-gate fail = 1; 19990Sstevel@tonic-gate fpp = &fp->ftp_next; 20000Sstevel@tonic-gate } else { 20010Sstevel@tonic-gate *fpp = fp->ftp_next; 20020Sstevel@tonic-gate fasttrap_provider_free(fp); 20030Sstevel@tonic-gate } 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate if (fail || dtrace_unregister(fasttrap_id) != 0) { 20100Sstevel@tonic-gate uint_t work; 20110Sstevel@tonic-gate /* 20120Sstevel@tonic-gate * If we're failing to detach, we need to unblock timeouts 20130Sstevel@tonic-gate * and start a new timeout if any work has accumulated while 20140Sstevel@tonic-gate * we've been unsuccessfully trying to detach. 20150Sstevel@tonic-gate */ 20160Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 20170Sstevel@tonic-gate fasttrap_timeout = 0; 20180Sstevel@tonic-gate work = fasttrap_cleanup_work; 20190Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate if (work) 20220Sstevel@tonic-gate fasttrap_pid_cleanup(); 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 20250Sstevel@tonic-gate &fasttrap_meta_id); 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate return (DDI_FAILURE); 20280Sstevel@tonic-gate } 20290Sstevel@tonic-gate 20300Sstevel@tonic-gate #ifdef DEBUG 20310Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 20320Sstevel@tonic-gate ASSERT(fasttrap_count == 0); 20330Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 20340Sstevel@tonic-gate #endif 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate kmem_free(fasttrap_tpoints.fth_table, 20370Sstevel@tonic-gate fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 20380Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 0; 20390Sstevel@tonic-gate 20400Sstevel@tonic-gate kmem_free(fasttrap_provs.fth_table, 20410Sstevel@tonic-gate fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 20420Sstevel@tonic-gate fasttrap_provs.fth_nent = 0; 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* 20450Sstevel@tonic-gate * We know there are no tracepoints in any process anywhere in 20460Sstevel@tonic-gate * the system so there is no process which has its p_dtrace_count 20470Sstevel@tonic-gate * greater than zero, therefore we know that no thread can actively 20480Sstevel@tonic-gate * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes 20490Sstevel@tonic-gate * and fasttrap_exec() and fasttrap_exit(). 20500Sstevel@tonic-gate */ 20510Sstevel@tonic-gate ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork); 20520Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = NULL; 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit); 20550Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = NULL; 20560Sstevel@tonic-gate 20570Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit); 20580Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = NULL; 20590Sstevel@tonic-gate 20600Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate return (DDI_SUCCESS); 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate 20650Sstevel@tonic-gate static struct dev_ops fasttrap_ops = { 20660Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 20670Sstevel@tonic-gate 0, /* refcnt */ 20680Sstevel@tonic-gate fasttrap_info, /* get_dev_info */ 20690Sstevel@tonic-gate nulldev, /* identify */ 20700Sstevel@tonic-gate nulldev, /* probe */ 20710Sstevel@tonic-gate fasttrap_attach, /* attach */ 20720Sstevel@tonic-gate fasttrap_detach, /* detach */ 20730Sstevel@tonic-gate nodev, /* reset */ 20740Sstevel@tonic-gate &fasttrap_cb_ops, /* driver operations */ 20750Sstevel@tonic-gate NULL, /* bus operations */ 20760Sstevel@tonic-gate nodev /* dev power */ 20770Sstevel@tonic-gate }; 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate /* 20800Sstevel@tonic-gate * Module linkage information for the kernel. 20810Sstevel@tonic-gate */ 20820Sstevel@tonic-gate static struct modldrv modldrv = { 20830Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 20840Sstevel@tonic-gate "Fasttrap Tracing", /* name of module */ 20850Sstevel@tonic-gate &fasttrap_ops, /* driver ops */ 20860Sstevel@tonic-gate }; 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate static struct modlinkage modlinkage = { 20890Sstevel@tonic-gate MODREV_1, 20900Sstevel@tonic-gate (void *)&modldrv, 20910Sstevel@tonic-gate NULL 20920Sstevel@tonic-gate }; 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate int 20950Sstevel@tonic-gate _init(void) 20960Sstevel@tonic-gate { 20970Sstevel@tonic-gate return (mod_install(&modlinkage)); 20980Sstevel@tonic-gate } 20990Sstevel@tonic-gate 21000Sstevel@tonic-gate int 21010Sstevel@tonic-gate _info(struct modinfo *modinfop) 21020Sstevel@tonic-gate { 21030Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate int 21070Sstevel@tonic-gate _fini(void) 21080Sstevel@tonic-gate { 21090Sstevel@tonic-gate return (mod_remove(&modlinkage)); 21100Sstevel@tonic-gate } 2111