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 51677Sdp * Common Development and Distribution License (the "License"). 61677Sdp * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21390Sraf 220Sstevel@tonic-gate /* 231677Sdp * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/atomic.h> 300Sstevel@tonic-gate #include <sys/errno.h> 310Sstevel@tonic-gate #include <sys/stat.h> 320Sstevel@tonic-gate #include <sys/modctl.h> 330Sstevel@tonic-gate #include <sys/conf.h> 340Sstevel@tonic-gate #include <sys/systm.h> 350Sstevel@tonic-gate #include <sys/ddi.h> 360Sstevel@tonic-gate #include <sys/sunddi.h> 370Sstevel@tonic-gate #include <sys/cpuvar.h> 380Sstevel@tonic-gate #include <sys/kmem.h> 390Sstevel@tonic-gate #include <sys/strsubr.h> 400Sstevel@tonic-gate #include <sys/fasttrap.h> 410Sstevel@tonic-gate #include <sys/fasttrap_impl.h> 420Sstevel@tonic-gate #include <sys/fasttrap_isa.h> 430Sstevel@tonic-gate #include <sys/dtrace.h> 440Sstevel@tonic-gate #include <sys/dtrace_impl.h> 450Sstevel@tonic-gate #include <sys/sysmacros.h> 460Sstevel@tonic-gate #include <sys/frame.h> 470Sstevel@tonic-gate #include <sys/stack.h> 480Sstevel@tonic-gate #include <sys/proc.h> 490Sstevel@tonic-gate #include <sys/priv.h> 500Sstevel@tonic-gate #include <sys/policy.h> 510Sstevel@tonic-gate #include <sys/ontrap.h> 520Sstevel@tonic-gate #include <sys/vmsystm.h> 530Sstevel@tonic-gate #include <sys/prsystm.h> 540Sstevel@tonic-gate 550Sstevel@tonic-gate #include <vm/as.h> 560Sstevel@tonic-gate #include <vm/seg.h> 570Sstevel@tonic-gate #include <vm/seg_dev.h> 580Sstevel@tonic-gate #include <vm/seg_vn.h> 590Sstevel@tonic-gate #include <vm/seg_spt.h> 600Sstevel@tonic-gate #include <vm/seg_kmem.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * User-Land Trap-Based Tracing 640Sstevel@tonic-gate * ---------------------------- 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * The fasttrap provider allows DTrace consumers to instrument any user-level 670Sstevel@tonic-gate * instruction to gather data; this includes probes with semantic 680Sstevel@tonic-gate * signifigance like entry and return as well as simple offsets into the 690Sstevel@tonic-gate * function. While the specific techniques used are very ISA specific, the 700Sstevel@tonic-gate * methodology is generalizable to any architecture. 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * The General Methodology 740Sstevel@tonic-gate * ----------------------- 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * With the primary goal of tracing every user-land instruction and the 770Sstevel@tonic-gate * limitation that we can't trust user space so don't want to rely on much 780Sstevel@tonic-gate * information there, we begin by replacing the instructions we want to trace 790Sstevel@tonic-gate * with trap instructions. Each instruction we overwrite is saved into a hash 800Sstevel@tonic-gate * table keyed by process ID and pc address. When we enter the kernel due to 810Sstevel@tonic-gate * this trap instruction, we need the effects of the replaced instruction to 820Sstevel@tonic-gate * appear to have occurred before we proceed with the user thread's 830Sstevel@tonic-gate * execution. 840Sstevel@tonic-gate * 850Sstevel@tonic-gate * Each user level thread is represented by a ulwp_t structure which is 860Sstevel@tonic-gate * always easily accessible through a register. The most basic way to produce 870Sstevel@tonic-gate * the effects of the instruction we replaced is to copy that instruction out 880Sstevel@tonic-gate * to a bit of scratch space reserved in the user thread's ulwp_t structure 890Sstevel@tonic-gate * (a sort of kernel-private thread local storage), set the PC to that 900Sstevel@tonic-gate * scratch space and single step. When we reenter the kernel after single 910Sstevel@tonic-gate * stepping the instruction we must then adjust the PC to point to what would 920Sstevel@tonic-gate * normally be the next instruction. Of course, special care must be taken 930Sstevel@tonic-gate * for branches and jumps, but these represent such a small fraction of any 940Sstevel@tonic-gate * instruction set that writing the code to emulate these in the kernel is 950Sstevel@tonic-gate * not too difficult. 960Sstevel@tonic-gate * 970Sstevel@tonic-gate * Return probes may require several tracepoints to trace every return site, 980Sstevel@tonic-gate * and, conversely, each tracepoint may activate several probes (the entry 990Sstevel@tonic-gate * and offset 0 probes, for example). To solve this muliplexing problem, 1000Sstevel@tonic-gate * tracepoints contain lists of probes to activate and probes contain lists 1010Sstevel@tonic-gate * of tracepoints to enable. If a probe is activated, it adds its ID to 1020Sstevel@tonic-gate * existing tracepoints or creates new ones as necessary. 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * Most probes are activated _before_ the instruction is executed, but return 1050Sstevel@tonic-gate * probes are activated _after_ the effects of the last instruction of the 1060Sstevel@tonic-gate * function are visible. Return probes must be fired _after_ we have 1070Sstevel@tonic-gate * single-stepped the instruction whereas all other probes are fired 1080Sstevel@tonic-gate * beforehand. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate static dev_info_t *fasttrap_devi; 1120Sstevel@tonic-gate static dtrace_provider_id_t fasttrap_id; 1130Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id; 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate static timeout_id_t fasttrap_timeout; 1160Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx; 1170Sstevel@tonic-gate static uint_t fasttrap_cleanup_work; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * Generation count on modifications to the global tracepoint lookup table. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate /* 1250Sstevel@tonic-gate * When the fasttrap provider is loaded, fasttrap_max is set to either 1260Sstevel@tonic-gate * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the 1270Sstevel@tonic-gate * fasttrap.conf file. Each time a probe is created, fasttrap_total is 1280Sstevel@tonic-gate * incremented by the number of tracepoints that may be associated with that 1290Sstevel@tonic-gate * probe; fasttrap_total is capped at fasttrap_max. 1300Sstevel@tonic-gate */ 1310Sstevel@tonic-gate #define FASTTRAP_MAX_DEFAULT 250000 1320Sstevel@tonic-gate static uint32_t fasttrap_max; 1330Sstevel@tonic-gate static uint32_t fasttrap_total; 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 1370Sstevel@tonic-gate #define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 138532Sahl #define FASTTRAP_PROCS_DEFAULT_SIZE 0x100 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate #define FASTTRAP_PID_NAME "pid" 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate fasttrap_hash_t fasttrap_tpoints; 1430Sstevel@tonic-gate static fasttrap_hash_t fasttrap_provs; 144532Sahl static fasttrap_hash_t fasttrap_procs; 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 *); 159935Sahl static void fasttrap_provider_retire(pid_t, const char *, int); 1600Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *); 1610Sstevel@tonic-gate 162532Sahl static fasttrap_proc_t *fasttrap_proc_lookup(pid_t); 163532Sahl static void fasttrap_proc_release(fasttrap_proc_t *); 164532Sahl 1650Sstevel@tonic-gate #define FASTTRAP_PROVS_INDEX(pid, name) \ 1660Sstevel@tonic-gate ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 1670Sstevel@tonic-gate 168532Sahl #define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask) 169532Sahl 1700Sstevel@tonic-gate static int 1710Sstevel@tonic-gate fasttrap_highbit(ulong_t i) 1720Sstevel@tonic-gate { 1730Sstevel@tonic-gate int h = 1; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate if (i == 0) 1760Sstevel@tonic-gate return (0); 1770Sstevel@tonic-gate #ifdef _LP64 1780Sstevel@tonic-gate if (i & 0xffffffff00000000ul) { 1790Sstevel@tonic-gate h += 32; i >>= 32; 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate #endif 1820Sstevel@tonic-gate if (i & 0xffff0000) { 1830Sstevel@tonic-gate h += 16; i >>= 16; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate if (i & 0xff00) { 1860Sstevel@tonic-gate h += 8; i >>= 8; 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate if (i & 0xf0) { 1890Sstevel@tonic-gate h += 4; i >>= 4; 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate if (i & 0xc) { 1920Sstevel@tonic-gate h += 2; i >>= 2; 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate if (i & 0x2) { 1950Sstevel@tonic-gate h += 1; 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate return (h); 1980Sstevel@tonic-gate } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate static uint_t 2010Sstevel@tonic-gate fasttrap_hash_str(const char *p) 2020Sstevel@tonic-gate { 2030Sstevel@tonic-gate unsigned int g; 2040Sstevel@tonic-gate uint_t hval = 0; 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate while (*p) { 2070Sstevel@tonic-gate hval = (hval << 4) + *p++; 2080Sstevel@tonic-gate if ((g = (hval & 0xf0000000)) != 0) 2090Sstevel@tonic-gate hval ^= g >> 24; 2100Sstevel@tonic-gate hval &= ~g; 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate return (hval); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate void 2160Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate sqp->sq_info.si_signo = SIGTRAP; 2210Sstevel@tonic-gate sqp->sq_info.si_code = TRAP_DTRACE; 2220Sstevel@tonic-gate sqp->sq_info.si_addr = (caddr_t)pc; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate mutex_enter(&p->p_lock); 2250Sstevel@tonic-gate sigaddqa(p, t, sqp); 2260Sstevel@tonic-gate mutex_exit(&p->p_lock); 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate if (t != NULL) 2290Sstevel@tonic-gate aston(t); 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * This function ensures that no threads are actively using the memory 2340Sstevel@tonic-gate * associated with probes that were formerly live. 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate static void 2370Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate int i; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate if (gen < fasttrap_mod_gen) 2420Sstevel@tonic-gate return; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate fasttrap_mod_gen++; 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate for (i = 0; i < NCPU; i++) { 2470Sstevel@tonic-gate mutex_enter(&cpu_core[i].cpuc_pid_lock); 2480Sstevel@tonic-gate mutex_exit(&cpu_core[i].cpuc_pid_lock); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * This is the timeout's callback for cleaning up the providers and their 2540Sstevel@tonic-gate * probes. 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate /*ARGSUSED*/ 2570Sstevel@tonic-gate static void 2580Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data) 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp; 2610Sstevel@tonic-gate fasttrap_bucket_t *bucket; 2620Sstevel@tonic-gate dtrace_provider_id_t provid; 2630Sstevel@tonic-gate int i, later; 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate static volatile int in = 0; 2660Sstevel@tonic-gate ASSERT(in == 0); 2670Sstevel@tonic-gate in = 1; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 2700Sstevel@tonic-gate while (fasttrap_cleanup_work) { 2710Sstevel@tonic-gate fasttrap_cleanup_work = 0; 2720Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate later = 0; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * Iterate over all the providers trying to remove the marked 278532Sahl * ones. If a provider is marked but not retired, we just 2790Sstevel@tonic-gate * have to take a crack at removing it -- it's no big deal if 2800Sstevel@tonic-gate * we can't. 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2830Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[i]; 2840Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 2850Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate while ((fp = *fpp) != NULL) { 2880Sstevel@tonic-gate if (!fp->ftp_marked) { 2890Sstevel@tonic-gate fpp = &fp->ftp_next; 2900Sstevel@tonic-gate continue; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * If this provider is referenced either 2970Sstevel@tonic-gate * because it is a USDT provider or is being 2980Sstevel@tonic-gate * modified, we can't unregister or even 2990Sstevel@tonic-gate * condense. 3000Sstevel@tonic-gate */ 301*1880Sahl if (fp->ftp_ccount != 0 || 302*1880Sahl fp->ftp_mcount != 0) { 3030Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 3040Sstevel@tonic-gate fp->ftp_marked = 0; 3050Sstevel@tonic-gate continue; 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 308532Sahl if (!fp->ftp_retired || fp->ftp_rcount != 0) 3090Sstevel@tonic-gate fp->ftp_marked = 0; 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate /* 3140Sstevel@tonic-gate * If we successfully unregister this 3150Sstevel@tonic-gate * provider we can remove it from the hash 3160Sstevel@tonic-gate * chain and free the memory. If our attempt 317532Sahl * to unregister fails and this is a retired 3180Sstevel@tonic-gate * provider, increment our flag to try again 3190Sstevel@tonic-gate * pretty soon. If we've consumed more than 3200Sstevel@tonic-gate * half of our total permitted number of 3210Sstevel@tonic-gate * probes call dtrace_condense() to try to 3220Sstevel@tonic-gate * clean out the unenabled probes. 3230Sstevel@tonic-gate */ 3240Sstevel@tonic-gate provid = fp->ftp_provid; 3250Sstevel@tonic-gate if (dtrace_unregister(provid) != 0) { 3260Sstevel@tonic-gate if (fasttrap_total > fasttrap_max / 2) 3270Sstevel@tonic-gate (void) dtrace_condense(provid); 3280Sstevel@tonic-gate later += fp->ftp_marked; 3290Sstevel@tonic-gate fpp = &fp->ftp_next; 3300Sstevel@tonic-gate } else { 3310Sstevel@tonic-gate *fpp = fp->ftp_next; 3320Sstevel@tonic-gate fasttrap_provider_free(fp); 3330Sstevel@tonic-gate } 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate ASSERT(fasttrap_timeout != 0); 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate /* 344532Sahl * If we were unable to remove a retired provider, try again after 3450Sstevel@tonic-gate * a second. This situation can occur in certain circumstances where 3460Sstevel@tonic-gate * providers cannot be unregistered even though they have no probes 3470Sstevel@tonic-gate * enabled because of an execution of dtrace -l or something similar. 3480Sstevel@tonic-gate * If the timeout has been disabled (set to 1 because we're trying 3490Sstevel@tonic-gate * to detach), we set fasttrap_cleanup_work to ensure that we'll 3500Sstevel@tonic-gate * get a chance to do that work if and when the timeout is reenabled 3510Sstevel@tonic-gate * (if detach fails). 3520Sstevel@tonic-gate */ 3530Sstevel@tonic-gate if (later > 0 && fasttrap_timeout != (timeout_id_t)1) 3540Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz); 3550Sstevel@tonic-gate else if (later > 0) 3560Sstevel@tonic-gate fasttrap_cleanup_work = 1; 3570Sstevel@tonic-gate else 3580Sstevel@tonic-gate fasttrap_timeout = 0; 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 3610Sstevel@tonic-gate in = 0; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * Activates the asynchronous cleanup mechanism. 3660Sstevel@tonic-gate */ 3670Sstevel@tonic-gate static void 3680Sstevel@tonic-gate fasttrap_pid_cleanup(void) 3690Sstevel@tonic-gate { 3700Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 3710Sstevel@tonic-gate fasttrap_cleanup_work = 1; 3720Sstevel@tonic-gate if (fasttrap_timeout == 0) 3730Sstevel@tonic-gate fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1); 3740Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 3750Sstevel@tonic-gate } 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate /* 3780Sstevel@tonic-gate * This is called from cfork() via dtrace_fasttrap_fork(). The child 3790Sstevel@tonic-gate * process's address space is a (roughly) a copy of the parent process's so 3800Sstevel@tonic-gate * we have to remove all the instrumentation we had previously enabled in the 3810Sstevel@tonic-gate * parent. 3820Sstevel@tonic-gate */ 3830Sstevel@tonic-gate static void 3840Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp) 3850Sstevel@tonic-gate { 3860Sstevel@tonic-gate pid_t ppid = p->p_pid; 3870Sstevel@tonic-gate int i; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate ASSERT(curproc == p); 3900Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK); 3910Sstevel@tonic-gate ASSERT(p->p_dtrace_count > 0); 3920Sstevel@tonic-gate ASSERT(cp->p_dtrace_count == 0); 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate /* 3950Sstevel@tonic-gate * This would be simpler and faster if we maintained per-process 3960Sstevel@tonic-gate * hash tables of enabled tracepoints. It could, however, potentially 3970Sstevel@tonic-gate * slow down execution of a tracepoint since we'd need to go 3980Sstevel@tonic-gate * through two levels of indirection. In the future, we should 3990Sstevel@tonic-gate * consider either maintaining per-process ancillary lists of 4000Sstevel@tonic-gate * enabled tracepoints or hanging a pointer to a per-process hash 4010Sstevel@tonic-gate * table of enabled tracepoints off the proc structure. 4020Sstevel@tonic-gate */ 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * We don't have to worry about the child process disappearing 4060Sstevel@tonic-gate * because we're in fork(). 4070Sstevel@tonic-gate */ 4080Sstevel@tonic-gate mutex_enter(&cp->p_lock); 4090Sstevel@tonic-gate sprlock_proc(cp); 4100Sstevel@tonic-gate mutex_exit(&cp->p_lock); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * Iterate over every tracepoint looking for ones that belong to the 4140Sstevel@tonic-gate * parent process, and remove each from the child process. 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 4170Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 4180Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 4210Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 422532Sahl if (tp->ftt_pid == ppid && 423532Sahl !tp->ftt_proc->ftpc_defunct) { 4240Sstevel@tonic-gate int ret = fasttrap_tracepoint_remove(cp, tp); 4250Sstevel@tonic-gate ASSERT(ret == 0); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate mutex_enter(&cp->p_lock); 4320Sstevel@tonic-gate sprunlock(cp); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * This is called from proc_exit() or from exec_common() if p_dtrace_probes 4370Sstevel@tonic-gate * is set on the proc structure to indicate that there is a pid provider 4380Sstevel@tonic-gate * associated with this process. 4390Sstevel@tonic-gate */ 4400Sstevel@tonic-gate static void 4410Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p) 4420Sstevel@tonic-gate { 4430Sstevel@tonic-gate ASSERT(p == curproc); 4440Sstevel@tonic-gate ASSERT(MUTEX_HELD(&p->p_lock)); 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate mutex_exit(&p->p_lock); 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * We clean up the pid provider for this process here; user-land 4500Sstevel@tonic-gate * static probes are handled by the meta-provider remove entry point. 4510Sstevel@tonic-gate */ 452935Sahl fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0); 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate mutex_enter(&p->p_lock); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /*ARGSUSED*/ 4590Sstevel@tonic-gate static void 4600Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc) 4610Sstevel@tonic-gate { 4620Sstevel@tonic-gate /* 4630Sstevel@tonic-gate * There are no "default" pid probes. 4640Sstevel@tonic-gate */ 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /*ARGSUSED*/ 4680Sstevel@tonic-gate static void 4690Sstevel@tonic-gate fasttrap_provide(void *arg, const dtrace_probedesc_t *desc) 4700Sstevel@tonic-gate { 4710Sstevel@tonic-gate if (dtrace_probe_lookup(fasttrap_id, NULL, "fasttrap", "fasttrap") == 0) 4720Sstevel@tonic-gate fasttrap_probe_id = dtrace_probe_create(fasttrap_id, NULL, 4730Sstevel@tonic-gate "fasttrap", "fasttrap", FASTTRAP_AFRAMES, NULL); 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate static int 4770Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate fasttrap_tracepoint_t *tp, *new_tp = NULL; 4800Sstevel@tonic-gate fasttrap_bucket_t *bucket; 4810Sstevel@tonic-gate fasttrap_id_t *id; 4820Sstevel@tonic-gate pid_t pid; 4830Sstevel@tonic-gate uintptr_t pc; 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps); 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate pid = probe->ftp_pid; 4880Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc; 4890Sstevel@tonic-gate id = &probe->ftp_tps[index].fit_id; 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate /* 4960Sstevel@tonic-gate * Before we make any modifications, make sure we've imposed a barrier 4970Sstevel@tonic-gate * on the generation in which this probe was last modified. 4980Sstevel@tonic-gate */ 4990Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen); 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * If the tracepoint has already been enabled, just add our id to the 5050Sstevel@tonic-gate * list of interested probes. This may be our second time through 5060Sstevel@tonic-gate * this path in which case we'll have constructed the tracepoint we'd 5070Sstevel@tonic-gate * like to install. If we can't find a match, and have an allocated 5080Sstevel@tonic-gate * tracepoint ready to go, enable that one now. 5090Sstevel@tonic-gate * 5101710Sahl * A tracepoint whose process is defunct is also considered defunct. 5110Sstevel@tonic-gate */ 5120Sstevel@tonic-gate again: 5130Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 5140Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 5150Sstevel@tonic-gate if (tp->ftt_pid != pid || tp->ftt_pc != pc || 516532Sahl tp->ftt_proc->ftpc_defunct) 5170Sstevel@tonic-gate continue; 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate /* 5200Sstevel@tonic-gate * Now that we've found a matching tracepoint, it would be 5210Sstevel@tonic-gate * a decent idea to confirm that the tracepoint is still 5220Sstevel@tonic-gate * enabled and the trap instruction hasn't been overwritten. 5230Sstevel@tonic-gate * Since this is a little hairy, we'll punt for now. 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * This can't be the first interested probe. We don't have 5280Sstevel@tonic-gate * to worry about another thread being in the midst of 5290Sstevel@tonic-gate * deleting this tracepoint (which would be the only valid 5300Sstevel@tonic-gate * reason for a tracepoint to have no interested probes) 5310Sstevel@tonic-gate * since we're holding P_PR_LOCK for this process. 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 5340Sstevel@tonic-gate 5351710Sahl switch (id->fti_ptype) { 5361710Sahl case DTFTP_ENTRY: 5371710Sahl case DTFTP_OFFSETS: 5381710Sahl case DTFTP_IS_ENABLED: 5391710Sahl id->fti_next = tp->ftt_ids; 5401710Sahl membar_producer(); 5411710Sahl tp->ftt_ids = id; 5421710Sahl membar_producer(); 5431710Sahl break; 5441710Sahl 5451710Sahl case DTFTP_RETURN: 5461710Sahl case DTFTP_POST_OFFSETS: 5470Sstevel@tonic-gate id->fti_next = tp->ftt_retids; 5480Sstevel@tonic-gate membar_producer(); 5490Sstevel@tonic-gate tp->ftt_retids = id; 5500Sstevel@tonic-gate membar_producer(); 5511710Sahl break; 5521710Sahl 5531710Sahl default: 5541710Sahl ASSERT(0); 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate if (new_tp != NULL) { 5600Sstevel@tonic-gate new_tp->ftt_ids = NULL; 5610Sstevel@tonic-gate new_tp->ftt_retids = NULL; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate return (0); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * If we have a good tracepoint ready to go, install it now while 5690Sstevel@tonic-gate * we have the lock held and no one can screw with us. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (new_tp != NULL) { 572315Sahl int rc = 0; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate new_tp->ftt_next = bucket->ftb_data; 5750Sstevel@tonic-gate membar_producer(); 5760Sstevel@tonic-gate bucket->ftb_data = new_tp; 5770Sstevel@tonic-gate membar_producer(); 5780Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate /* 581315Sahl * Activate the tracepoint in the ISA-specific manner. 582315Sahl * If this fails, we need to report the failure, but 583315Sahl * indicate that this tracepoint must still be disabled 584315Sahl * by calling fasttrap_tracepoint_disable(). 5850Sstevel@tonic-gate */ 586315Sahl if (fasttrap_tracepoint_install(p, new_tp) != 0) 587315Sahl rc = FASTTRAP_ENABLE_PARTIAL; 588315Sahl 589315Sahl /* 590315Sahl * Increment the count of the number of tracepoints active in 591315Sahl * the victim process. 592315Sahl */ 593315Sahl ASSERT(p->p_proc_flag & P_PR_LOCK); 594315Sahl p->p_dtrace_count++; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate return (rc); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate /* 6020Sstevel@tonic-gate * Initialize the tracepoint that's been preallocated with the probe. 6030Sstevel@tonic-gate */ 6040Sstevel@tonic-gate new_tp = probe->ftp_tps[index].fit_tp; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate ASSERT(new_tp->ftt_pid == pid); 6070Sstevel@tonic-gate ASSERT(new_tp->ftt_pc == pc); 608532Sahl ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 6090Sstevel@tonic-gate ASSERT(new_tp->ftt_ids == NULL); 6100Sstevel@tonic-gate ASSERT(new_tp->ftt_retids == NULL); 6110Sstevel@tonic-gate 6121710Sahl switch (id->fti_ptype) { 6131710Sahl case DTFTP_ENTRY: 6141710Sahl case DTFTP_OFFSETS: 6151710Sahl case DTFTP_IS_ENABLED: 6161710Sahl id->fti_next = NULL; 6171710Sahl new_tp->ftt_ids = id; 6181710Sahl break; 6191710Sahl 6201710Sahl case DTFTP_RETURN: 6211710Sahl case DTFTP_POST_OFFSETS: 6220Sstevel@tonic-gate id->fti_next = NULL; 6230Sstevel@tonic-gate new_tp->ftt_retids = id; 6241710Sahl break; 6251710Sahl 6261710Sahl default: 6271710Sahl ASSERT(0); 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 631315Sahl * If the ISA-dependent initialization goes to plan, go back to the 6320Sstevel@tonic-gate * beginning and try to install this freshly made tracepoint. 6330Sstevel@tonic-gate */ 6341710Sahl if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0) 6350Sstevel@tonic-gate goto again; 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate new_tp->ftt_ids = NULL; 6380Sstevel@tonic-gate new_tp->ftt_retids = NULL; 6390Sstevel@tonic-gate 640315Sahl return (FASTTRAP_ENABLE_FAIL); 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate static void 6440Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 6450Sstevel@tonic-gate { 6460Sstevel@tonic-gate fasttrap_bucket_t *bucket; 6470Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov; 6480Sstevel@tonic-gate fasttrap_tracepoint_t **pp, *tp; 6490Sstevel@tonic-gate fasttrap_id_t *id, **idp; 6500Sstevel@tonic-gate pid_t pid; 6510Sstevel@tonic-gate uintptr_t pc; 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate ASSERT(index < probe->ftp_ntps); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate pid = probe->ftp_pid; 6560Sstevel@tonic-gate pc = probe->ftp_tps[index].fit_tp->ftt_pc; 6571710Sahl id = &probe->ftp_tps[index].fit_id; 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* 6620Sstevel@tonic-gate * Find the tracepoint and make sure that our id is one of the 6630Sstevel@tonic-gate * ones registered with it. 6640Sstevel@tonic-gate */ 6650Sstevel@tonic-gate bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 6660Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 6670Sstevel@tonic-gate for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 6680Sstevel@tonic-gate if (tp->ftt_pid == pid && tp->ftt_pc == pc && 669532Sahl tp->ftt_proc == provider->ftp_proc) 6700Sstevel@tonic-gate break; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * If we somehow lost this tracepoint, we're in a world of hurt. 6750Sstevel@tonic-gate */ 6760Sstevel@tonic-gate ASSERT(tp != NULL); 6770Sstevel@tonic-gate 6781710Sahl switch (id->fti_ptype) { 6791710Sahl case DTFTP_ENTRY: 6801710Sahl case DTFTP_OFFSETS: 6811710Sahl case DTFTP_IS_ENABLED: 6821710Sahl ASSERT(tp->ftt_ids != NULL); 6831710Sahl idp = &tp->ftt_ids; 6841710Sahl break; 6851710Sahl 6861710Sahl case DTFTP_RETURN: 6871710Sahl case DTFTP_POST_OFFSETS: 6880Sstevel@tonic-gate ASSERT(tp->ftt_retids != NULL); 6890Sstevel@tonic-gate idp = &tp->ftt_retids; 6901710Sahl break; 6911710Sahl 6921710Sahl default: 6931710Sahl ASSERT(0); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate while ((*idp)->fti_probe != probe) { 6970Sstevel@tonic-gate idp = &(*idp)->fti_next; 6980Sstevel@tonic-gate ASSERT(*idp != NULL); 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate id = *idp; 7020Sstevel@tonic-gate *idp = id->fti_next; 7030Sstevel@tonic-gate membar_producer(); 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate ASSERT(id->fti_probe == probe); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate /* 7080Sstevel@tonic-gate * If there are other registered enablings of this tracepoint, we're 7090Sstevel@tonic-gate * all done, but if this was the last probe assocated with this 7100Sstevel@tonic-gate * this tracepoint, we need to remove and free it. 7110Sstevel@tonic-gate */ 7120Sstevel@tonic-gate if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * If the current probe's tracepoint is in use, swap it 7160Sstevel@tonic-gate * for an unused tracepoint. 7170Sstevel@tonic-gate */ 7180Sstevel@tonic-gate if (tp == probe->ftp_tps[index].fit_tp) { 7190Sstevel@tonic-gate fasttrap_probe_t *tmp_probe; 7200Sstevel@tonic-gate fasttrap_tracepoint_t **tmp_tp; 7210Sstevel@tonic-gate uint_t tmp_index; 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate if (tp->ftt_ids != NULL) { 7240Sstevel@tonic-gate tmp_probe = tp->ftt_ids->fti_probe; 7250Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 7260Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 7270Sstevel@tonic-gate } else { 7280Sstevel@tonic-gate tmp_probe = tp->ftt_retids->fti_probe; 7290Sstevel@tonic-gate tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 7300Sstevel@tonic-gate tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate ASSERT(*tmp_tp != NULL); 7340Sstevel@tonic-gate ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 7350Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_ids == NULL); 7360Sstevel@tonic-gate ASSERT((*tmp_tp)->ftt_retids == NULL); 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate probe->ftp_tps[index].fit_tp = *tmp_tp; 7390Sstevel@tonic-gate *tmp_tp = tp; 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * Tag the modified probe with the generation in which it was 7470Sstevel@tonic-gate * changed. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen; 7500Sstevel@tonic-gate return; 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate /* 7560Sstevel@tonic-gate * We can't safely remove the tracepoint from the set of active 7570Sstevel@tonic-gate * tracepoints until we've actually removed the fasttrap instruction 7580Sstevel@tonic-gate * from the process's text. We can, however, operate on this 7590Sstevel@tonic-gate * tracepoint secure in the knowledge that no other thread is going to 7600Sstevel@tonic-gate * be looking at it since we hold P_PR_LOCK on the process if it's 7610Sstevel@tonic-gate * live or we hold the provider lock on the process if it's dead and 7620Sstevel@tonic-gate * gone. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* 7660Sstevel@tonic-gate * We only need to remove the actual instruction if we're looking 7670Sstevel@tonic-gate * at an existing process 7680Sstevel@tonic-gate */ 7690Sstevel@tonic-gate if (p != NULL) { 7700Sstevel@tonic-gate /* 7710Sstevel@tonic-gate * If we fail to restore the instruction we need to kill 7720Sstevel@tonic-gate * this process since it's in a completely unrecoverable 7730Sstevel@tonic-gate * state. 7740Sstevel@tonic-gate */ 7750Sstevel@tonic-gate if (fasttrap_tracepoint_remove(p, tp) != 0) 7760Sstevel@tonic-gate fasttrap_sigtrap(p, NULL, pc); 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate /* 7790Sstevel@tonic-gate * Decrement the count of the number of tracepoints active 7800Sstevel@tonic-gate * in the victim process. 7810Sstevel@tonic-gate */ 7820Sstevel@tonic-gate ASSERT(p->p_proc_flag & P_PR_LOCK); 7830Sstevel@tonic-gate p->p_dtrace_count--; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate 7860Sstevel@tonic-gate /* 7870Sstevel@tonic-gate * Remove the probe from the hash table of active tracepoints. 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 7900Sstevel@tonic-gate pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 7910Sstevel@tonic-gate ASSERT(*pp != NULL); 7920Sstevel@tonic-gate while (*pp != tp) { 7930Sstevel@tonic-gate pp = &(*pp)->ftt_next; 7940Sstevel@tonic-gate ASSERT(*pp != NULL); 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate *pp = tp->ftt_next; 7980Sstevel@tonic-gate membar_producer(); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate /* 8030Sstevel@tonic-gate * Tag the modified probe with the generation in which it was changed. 8040Sstevel@tonic-gate */ 8050Sstevel@tonic-gate probe->ftp_gen = fasttrap_mod_gen; 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate typedef int fasttrap_probe_f(struct regs *); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate static void 8110Sstevel@tonic-gate fasttrap_enable_common(int *count, fasttrap_probe_f **fptr, fasttrap_probe_f *f, 8120Sstevel@tonic-gate fasttrap_probe_f **fptr2, fasttrap_probe_f *f2) 8130Sstevel@tonic-gate { 8140Sstevel@tonic-gate /* 8150Sstevel@tonic-gate * We don't have to play the rw lock game here because we're 8160Sstevel@tonic-gate * providing something rather than taking something away -- 8170Sstevel@tonic-gate * we can be sure that no threads have tried to follow this 8180Sstevel@tonic-gate * function pointer yet. 8190Sstevel@tonic-gate */ 8200Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 8210Sstevel@tonic-gate if (*count == 0) { 8220Sstevel@tonic-gate ASSERT(*fptr == NULL); 8230Sstevel@tonic-gate *fptr = f; 8240Sstevel@tonic-gate if (fptr2 != NULL) 8250Sstevel@tonic-gate *fptr2 = f2; 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate ASSERT(*fptr == f); 8280Sstevel@tonic-gate ASSERT(fptr2 == NULL || *fptr2 == f2); 8290Sstevel@tonic-gate (*count)++; 8300Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 8310Sstevel@tonic-gate } 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate static void 8340Sstevel@tonic-gate fasttrap_disable_common(int *count, fasttrap_probe_f **fptr, 8350Sstevel@tonic-gate fasttrap_probe_f **fptr2) 8360Sstevel@tonic-gate { 8370Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 8400Sstevel@tonic-gate (*count)--; 8410Sstevel@tonic-gate ASSERT(*count >= 0); 8420Sstevel@tonic-gate if (*count == 0) { 8430Sstevel@tonic-gate cpu_t *cur, *cpu = CPU; 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu; 8460Sstevel@tonic-gate cur = cur->cpu_next_onln) { 8470Sstevel@tonic-gate rw_enter(&cur->cpu_ft_lock, RW_WRITER); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate *fptr = NULL; 8510Sstevel@tonic-gate if (fptr2 != NULL) 8520Sstevel@tonic-gate *fptr2 = NULL; 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate for (cur = cpu->cpu_next_onln; cur != cpu; 8550Sstevel@tonic-gate cur = cur->cpu_next_onln) { 8560Sstevel@tonic-gate rw_exit(&cur->cpu_ft_lock); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate /*ARGSUSED*/ 8630Sstevel@tonic-gate static void 8640Sstevel@tonic-gate fasttrap_enable(void *arg, dtrace_id_t id, void *parg) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate /* 8670Sstevel@tonic-gate * Enable the probe that corresponds to statically placed trace 8680Sstevel@tonic-gate * points which have not explicitly been placed in the process's text 8690Sstevel@tonic-gate * by the fasttrap provider. 8700Sstevel@tonic-gate */ 8710Sstevel@tonic-gate ASSERT(arg == NULL); 8720Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate fasttrap_enable_common(&fasttrap_count, 8750Sstevel@tonic-gate &dtrace_fasttrap_probe_ptr, fasttrap_probe, NULL, NULL); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /*ARGSUSED*/ 8800Sstevel@tonic-gate static void 8810Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 8820Sstevel@tonic-gate { 8830Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 8840Sstevel@tonic-gate proc_t *p; 885315Sahl int i, rc; 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate ASSERT(probe != NULL); 8880Sstevel@tonic-gate ASSERT(!probe->ftp_enabled); 8890Sstevel@tonic-gate ASSERT(id == probe->ftp_id); 8900Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 8910Sstevel@tonic-gate 8920Sstevel@tonic-gate /* 8930Sstevel@tonic-gate * Increment the count of enabled probes on this probe's provider; 8940Sstevel@tonic-gate * the provider can't go away while the probe still exists. We 8950Sstevel@tonic-gate * must increment this even if we aren't able to properly enable 8960Sstevel@tonic-gate * this probe. 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate mutex_enter(&probe->ftp_prov->ftp_mtx); 8990Sstevel@tonic-gate probe->ftp_prov->ftp_rcount++; 9000Sstevel@tonic-gate mutex_exit(&probe->ftp_prov->ftp_mtx); 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate /* 903*1880Sahl * If this probe's provider is retired (meaning it was valid in a 904*1880Sahl * previously exec'ed incarnation of this address space), bail out. The 905*1880Sahl * provider can't go away while we're in this code path. 906*1880Sahl */ 907*1880Sahl if (probe->ftp_prov->ftp_retired) 908*1880Sahl return; 909*1880Sahl 910*1880Sahl /* 911*1880Sahl * If we can't find the process, it may be that we're in the context of 912*1880Sahl * a fork in which the traced process is being born and we're copying 913*1880Sahl * USDT probes. Otherwise, the process is gone so bail. 9140Sstevel@tonic-gate */ 915*1880Sahl if ((p = sprlock(probe->ftp_pid)) == NULL) { 916*1880Sahl if ((curproc->p_flag & SFORKING) == 0) 917*1880Sahl return; 918*1880Sahl 919*1880Sahl mutex_enter(&pidlock); 920*1880Sahl p = prfind(probe->ftp_pid); 921*1880Sahl 922*1880Sahl /* 923*1880Sahl * Confirm that curproc is indeed forking the process in which 924*1880Sahl * we're trying to enable probes. 925*1880Sahl */ 926*1880Sahl ASSERT(p != NULL); 927*1880Sahl ASSERT(p->p_parent == curproc); 928*1880Sahl ASSERT(p->p_stat == SIDL); 929*1880Sahl 930*1880Sahl mutex_enter(&p->p_lock); 931*1880Sahl mutex_exit(&pidlock); 932*1880Sahl 933*1880Sahl sprlock_proc(p); 934*1880Sahl } 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 9370Sstevel@tonic-gate mutex_exit(&p->p_lock); 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * We have to enable the trap entry before any user threads have 9410Sstevel@tonic-gate * the chance to execute the trap instruction we're about to place 9420Sstevel@tonic-gate * in their process's text. 9430Sstevel@tonic-gate */ 9440Sstevel@tonic-gate fasttrap_enable_common(&fasttrap_pid_count, 9450Sstevel@tonic-gate &dtrace_pid_probe_ptr, fasttrap_pid_probe, 9460Sstevel@tonic-gate &dtrace_return_probe_ptr, fasttrap_return_probe); 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate /* 9490Sstevel@tonic-gate * Enable all the tracepoints and add this probe's id to each 9500Sstevel@tonic-gate * tracepoint's list of active probes. 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 953315Sahl if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 954315Sahl /* 955315Sahl * If enabling the tracepoint failed completely, 956315Sahl * we don't have to disable it; if the failure 957315Sahl * was only partial we must disable it. 958315Sahl */ 959315Sahl if (rc == FASTTRAP_ENABLE_FAIL) 960315Sahl i--; 961315Sahl else 962315Sahl ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 963315Sahl 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * Back up and pull out all the tracepoints we've 9660Sstevel@tonic-gate * created so far for this probe. 9670Sstevel@tonic-gate */ 968665Sahl while (i >= 0) { 9690Sstevel@tonic-gate fasttrap_tracepoint_disable(p, probe, i); 970665Sahl i--; 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate mutex_enter(&p->p_lock); 9740Sstevel@tonic-gate sprunlock(p); 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate /* 9770Sstevel@tonic-gate * Since we're not actually enabling this probe, 9780Sstevel@tonic-gate * drop our reference on the trap table entry. 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_pid_count, 9810Sstevel@tonic-gate &dtrace_pid_probe_ptr, &dtrace_return_probe_ptr); 9820Sstevel@tonic-gate return; 9830Sstevel@tonic-gate } 9840Sstevel@tonic-gate } 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate mutex_enter(&p->p_lock); 9870Sstevel@tonic-gate sprunlock(p); 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate probe->ftp_enabled = 1; 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate /*ARGSUSED*/ 9940Sstevel@tonic-gate static void 9950Sstevel@tonic-gate fasttrap_disable(void *arg, dtrace_id_t id, void *parg) 9960Sstevel@tonic-gate { 9970Sstevel@tonic-gate /* 9980Sstevel@tonic-gate * Disable the probe the corresponds to statically placed trace 9990Sstevel@tonic-gate * points. 10000Sstevel@tonic-gate */ 10010Sstevel@tonic-gate ASSERT(arg == NULL); 10020Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 10030Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10040Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_count, &dtrace_fasttrap_probe_ptr, 10050Sstevel@tonic-gate NULL); 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate /*ARGSUSED*/ 10090Sstevel@tonic-gate static void 10100Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 10110Sstevel@tonic-gate { 10120Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 10130Sstevel@tonic-gate fasttrap_provider_t *provider = probe->ftp_prov; 10140Sstevel@tonic-gate proc_t *p; 10150Sstevel@tonic-gate int i, whack = 0; 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate if (!probe->ftp_enabled) { 10180Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 10190Sstevel@tonic-gate provider->ftp_rcount--; 10200Sstevel@tonic-gate ASSERT(provider->ftp_rcount >= 0); 10210Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 10220Sstevel@tonic-gate return; 10230Sstevel@tonic-gate } 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate ASSERT(id == probe->ftp_id); 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* 10280Sstevel@tonic-gate * We won't be able to acquire a /proc-esque lock on the process 10290Sstevel@tonic-gate * iff the process is dead and gone. In this case, we rely on the 10300Sstevel@tonic-gate * provider lock as a point of mutual exclusion to prevent other 10310Sstevel@tonic-gate * DTrace consumers from disabling this probe. 10320Sstevel@tonic-gate */ 10330Sstevel@tonic-gate if ((p = sprlock(probe->ftp_pid)) != NULL) { 10340Sstevel@tonic-gate ASSERT(!(p->p_flag & SVFORK)); 10350Sstevel@tonic-gate mutex_exit(&p->p_lock); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate /* 10410Sstevel@tonic-gate * Disable all the associated tracepoints. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 10440Sstevel@tonic-gate fasttrap_tracepoint_disable(p, probe, i); 10450Sstevel@tonic-gate } 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate ASSERT(provider->ftp_rcount > 0); 10480Sstevel@tonic-gate provider->ftp_rcount--; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if (p != NULL) { 10510Sstevel@tonic-gate /* 10520Sstevel@tonic-gate * Even though we may not be able to remove it entirely, we 1053532Sahl * mark this retired provider to get a chance to remove some 10540Sstevel@tonic-gate * of the associated probes. 10550Sstevel@tonic-gate */ 1056532Sahl if (provider->ftp_retired && !provider->ftp_marked) 10570Sstevel@tonic-gate whack = provider->ftp_marked = 1; 10580Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 10590Sstevel@tonic-gate 10600Sstevel@tonic-gate mutex_enter(&p->p_lock); 10610Sstevel@tonic-gate sprunlock(p); 10620Sstevel@tonic-gate } else { 10630Sstevel@tonic-gate /* 10640Sstevel@tonic-gate * If the process is dead, we're just waiting for the 10650Sstevel@tonic-gate * last probe to be disabled to be able to free it. 10660Sstevel@tonic-gate */ 10670Sstevel@tonic-gate if (provider->ftp_rcount == 0 && !provider->ftp_marked) 10680Sstevel@tonic-gate whack = provider->ftp_marked = 1; 10690Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate if (whack) 10730Sstevel@tonic-gate fasttrap_pid_cleanup(); 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate probe->ftp_enabled = 0; 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate ASSERT(MUTEX_HELD(&cpu_lock)); 10780Sstevel@tonic-gate fasttrap_disable_common(&fasttrap_pid_count, &dtrace_pid_probe_ptr, 10790Sstevel@tonic-gate &dtrace_return_probe_ptr); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate /*ARGSUSED*/ 10830Sstevel@tonic-gate static void 10840Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 10850Sstevel@tonic-gate dtrace_argdesc_t *desc) 10860Sstevel@tonic-gate { 10870Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 10880Sstevel@tonic-gate char *str; 10890Sstevel@tonic-gate int i; 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate desc->dtargd_native[0] = '\0'; 10920Sstevel@tonic-gate desc->dtargd_xlate[0] = '\0'; 10930Sstevel@tonic-gate 1094532Sahl if (probe->ftp_prov->ftp_retired != 0 || 10950Sstevel@tonic-gate desc->dtargd_ndx >= probe->ftp_nargs) { 10960Sstevel@tonic-gate desc->dtargd_ndx = DTRACE_ARGNONE; 10970Sstevel@tonic-gate return; 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate /* 11010Sstevel@tonic-gate * We only need to set this member if the argument is remapped. 11020Sstevel@tonic-gate */ 11030Sstevel@tonic-gate if (probe->ftp_argmap != NULL) 11040Sstevel@tonic-gate desc->dtargd_mapping = probe->ftp_argmap[desc->dtargd_ndx]; 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate str = probe->ftp_ntypes; 11070Sstevel@tonic-gate for (i = 0; i < desc->dtargd_mapping; i++) { 11080Sstevel@tonic-gate str += strlen(str) + 1; 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 11120Sstevel@tonic-gate (void) strcpy(desc->dtargd_native, str); 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate if (probe->ftp_xtypes == NULL) 11150Sstevel@tonic-gate return; 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate str = probe->ftp_xtypes; 11180Sstevel@tonic-gate for (i = 0; i < desc->dtargd_ndx; i++) { 11190Sstevel@tonic-gate str += strlen(str) + 1; 11200Sstevel@tonic-gate } 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 11230Sstevel@tonic-gate (void) strcpy(desc->dtargd_xlate, str); 11240Sstevel@tonic-gate } 11250Sstevel@tonic-gate 11260Sstevel@tonic-gate /*ARGSUSED*/ 11270Sstevel@tonic-gate static void 11280Sstevel@tonic-gate fasttrap_destroy(void *arg, dtrace_id_t id, void *parg) 11290Sstevel@tonic-gate { 11300Sstevel@tonic-gate ASSERT(arg == NULL); 11310Sstevel@tonic-gate ASSERT(id == fasttrap_probe_id); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate /*ARGSUSED*/ 11350Sstevel@tonic-gate static void 11360Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 11370Sstevel@tonic-gate { 11380Sstevel@tonic-gate fasttrap_probe_t *probe = parg; 11390Sstevel@tonic-gate int i; 11400Sstevel@tonic-gate size_t size; 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate ASSERT(probe != NULL); 11430Sstevel@tonic-gate ASSERT(!probe->ftp_enabled); 11440Sstevel@tonic-gate ASSERT(fasttrap_total >= probe->ftp_ntps); 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 1147*1880Sahl size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]); 11480Sstevel@tonic-gate 11490Sstevel@tonic-gate if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 11500Sstevel@tonic-gate fasttrap_mod_barrier(probe->ftp_gen); 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate for (i = 0; i < probe->ftp_ntps; i++) { 11530Sstevel@tonic-gate kmem_free(probe->ftp_tps[i].fit_tp, 11540Sstevel@tonic-gate sizeof (fasttrap_tracepoint_t)); 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate kmem_free(probe, size); 11580Sstevel@tonic-gate } 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate 11610Sstevel@tonic-gate static const dtrace_pattr_t fasttrap_attr = { 11620Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11630Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11640Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11650Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11660Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11670Sstevel@tonic-gate }; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate static dtrace_pops_t fasttrap_pops = { 11700Sstevel@tonic-gate fasttrap_provide, 11710Sstevel@tonic-gate NULL, 11720Sstevel@tonic-gate fasttrap_enable, 11730Sstevel@tonic-gate fasttrap_disable, 11740Sstevel@tonic-gate NULL, 11750Sstevel@tonic-gate NULL, 11760Sstevel@tonic-gate NULL, 11770Sstevel@tonic-gate fasttrap_getarg, 11780Sstevel@tonic-gate NULL, 11790Sstevel@tonic-gate fasttrap_destroy 11800Sstevel@tonic-gate }; 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = { 11830Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11840Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11850Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11860Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 11870Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 11880Sstevel@tonic-gate }; 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate static dtrace_pops_t pid_pops = { 11910Sstevel@tonic-gate fasttrap_pid_provide, 11920Sstevel@tonic-gate NULL, 11930Sstevel@tonic-gate fasttrap_pid_enable, 11940Sstevel@tonic-gate fasttrap_pid_disable, 11950Sstevel@tonic-gate NULL, 11960Sstevel@tonic-gate NULL, 11970Sstevel@tonic-gate fasttrap_pid_getargdesc, 11980Sstevel@tonic-gate fasttrap_getarg, 11990Sstevel@tonic-gate NULL, 12000Sstevel@tonic-gate fasttrap_pid_destroy 12010Sstevel@tonic-gate }; 12020Sstevel@tonic-gate 12030Sstevel@tonic-gate static dtrace_pops_t usdt_pops = { 12040Sstevel@tonic-gate fasttrap_pid_provide, 12050Sstevel@tonic-gate NULL, 12060Sstevel@tonic-gate fasttrap_pid_enable, 12070Sstevel@tonic-gate fasttrap_pid_disable, 12080Sstevel@tonic-gate NULL, 12090Sstevel@tonic-gate NULL, 12100Sstevel@tonic-gate fasttrap_pid_getargdesc, 12110Sstevel@tonic-gate fasttrap_usdt_getarg, 12120Sstevel@tonic-gate NULL, 12130Sstevel@tonic-gate fasttrap_pid_destroy 12140Sstevel@tonic-gate }; 12150Sstevel@tonic-gate 1216532Sahl static fasttrap_proc_t * 1217532Sahl fasttrap_proc_lookup(pid_t pid) 1218532Sahl { 1219532Sahl fasttrap_bucket_t *bucket; 1220532Sahl fasttrap_proc_t *fprc, *new_fprc; 1221532Sahl 1222532Sahl bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1223532Sahl mutex_enter(&bucket->ftb_mtx); 1224532Sahl 1225532Sahl for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1226532Sahl if (fprc->ftpc_pid == pid && !fprc->ftpc_defunct) { 1227532Sahl mutex_enter(&fprc->ftpc_mtx); 1228532Sahl mutex_exit(&bucket->ftb_mtx); 1229532Sahl fprc->ftpc_count++; 1230532Sahl mutex_exit(&fprc->ftpc_mtx); 1231532Sahl 1232532Sahl return (fprc); 1233532Sahl } 1234532Sahl } 1235532Sahl 1236532Sahl /* 1237532Sahl * Drop the bucket lock so we don't try to perform a sleeping 1238532Sahl * allocation under it. 1239532Sahl */ 1240532Sahl mutex_exit(&bucket->ftb_mtx); 1241532Sahl 1242532Sahl new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP); 1243532Sahl new_fprc->ftpc_pid = pid; 1244532Sahl new_fprc->ftpc_count = 1; 1245532Sahl 1246532Sahl mutex_enter(&bucket->ftb_mtx); 1247532Sahl 1248532Sahl /* 1249532Sahl * Take another lap through the list to make sure a proc hasn't 1250532Sahl * been created for this pid while we weren't under the bucket lock. 1251532Sahl */ 1252532Sahl for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1253532Sahl if (fprc->ftpc_pid == pid && !fprc->ftpc_defunct) { 1254532Sahl mutex_enter(&fprc->ftpc_mtx); 1255532Sahl mutex_exit(&bucket->ftb_mtx); 1256532Sahl fprc->ftpc_count++; 1257532Sahl mutex_exit(&fprc->ftpc_mtx); 1258532Sahl 1259532Sahl kmem_free(new_fprc, sizeof (fasttrap_proc_t)); 1260532Sahl 1261532Sahl return (fprc); 1262532Sahl } 1263532Sahl } 1264532Sahl 1265532Sahl new_fprc->ftpc_next = bucket->ftb_data; 1266532Sahl bucket->ftb_data = new_fprc; 1267532Sahl 1268532Sahl mutex_exit(&bucket->ftb_mtx); 1269532Sahl 1270532Sahl return (new_fprc); 1271532Sahl } 1272532Sahl 1273532Sahl static void 1274532Sahl fasttrap_proc_release(fasttrap_proc_t *proc) 1275532Sahl { 1276532Sahl fasttrap_bucket_t *bucket; 1277532Sahl fasttrap_proc_t *fprc, **fprcp; 1278532Sahl pid_t pid = proc->ftpc_pid; 1279532Sahl 1280532Sahl mutex_enter(&proc->ftpc_mtx); 1281532Sahl 1282532Sahl ASSERT(proc->ftpc_count != 0); 1283532Sahl 1284532Sahl if (--proc->ftpc_count != 0) { 1285532Sahl mutex_exit(&proc->ftpc_mtx); 1286532Sahl return; 1287532Sahl } 1288532Sahl 1289532Sahl mutex_exit(&proc->ftpc_mtx); 1290532Sahl 1291532Sahl bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1292532Sahl mutex_enter(&bucket->ftb_mtx); 1293532Sahl 1294532Sahl fprcp = (fasttrap_proc_t **)&bucket->ftb_data; 1295532Sahl while ((fprc = *fprcp) != NULL) { 1296532Sahl if (fprc == proc) 1297532Sahl break; 1298532Sahl 1299532Sahl fprcp = &fprc->ftpc_next; 1300532Sahl } 1301532Sahl 1302532Sahl /* 1303532Sahl * Something strange has happened if we can't find the proc. 1304532Sahl */ 1305532Sahl ASSERT(fprc != NULL); 1306532Sahl 1307532Sahl *fprcp = fprc->ftpc_next; 1308532Sahl 1309532Sahl mutex_exit(&bucket->ftb_mtx); 1310532Sahl 1311532Sahl kmem_free(fprc, sizeof (fasttrap_proc_t)); 1312532Sahl } 1313532Sahl 13140Sstevel@tonic-gate /* 13150Sstevel@tonic-gate * Lookup a fasttrap-managed provider based on its name and associated pid. 13160Sstevel@tonic-gate * If the pattr argument is non-NULL, this function instantiates the provider 13170Sstevel@tonic-gate * if it doesn't exist otherwise it returns NULL. The provider is returned 13180Sstevel@tonic-gate * with its lock held. 13190Sstevel@tonic-gate */ 13200Sstevel@tonic-gate static fasttrap_provider_t * 13210Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name, 13220Sstevel@tonic-gate const dtrace_pattr_t *pattr) 13230Sstevel@tonic-gate { 13240Sstevel@tonic-gate fasttrap_provider_t *fp, *new_fp = NULL; 13250Sstevel@tonic-gate fasttrap_bucket_t *bucket; 13260Sstevel@tonic-gate char provname[DTRACE_PROVNAMELEN]; 13270Sstevel@tonic-gate proc_t *p; 13281677Sdp cred_t *cred; 13290Sstevel@tonic-gate 13300Sstevel@tonic-gate ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1331935Sahl ASSERT(pattr != NULL); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 13340Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate /* 13370Sstevel@tonic-gate * Take a lap through the list and return the match if we find it. 13380Sstevel@tonic-gate */ 13390Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 13400Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1341532Sahl !fp->ftp_retired) { 13420Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 13430Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 13440Sstevel@tonic-gate return (fp); 13450Sstevel@tonic-gate } 13460Sstevel@tonic-gate } 13470Sstevel@tonic-gate 13480Sstevel@tonic-gate /* 13490Sstevel@tonic-gate * Drop the bucket lock so we don't try to perform a sleeping 13500Sstevel@tonic-gate * allocation under it. 13510Sstevel@tonic-gate */ 13520Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 13530Sstevel@tonic-gate 1354532Sahl /* 13550Sstevel@tonic-gate * Make sure the process exists, isn't a child created as the result 13561677Sdp * of a vfork(2), and isn't a zombie (but may be in fork). 13570Sstevel@tonic-gate */ 13580Sstevel@tonic-gate mutex_enter(&pidlock); 1359390Sraf if ((p = prfind(pid)) == NULL) { 13600Sstevel@tonic-gate mutex_exit(&pidlock); 13610Sstevel@tonic-gate return (NULL); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate mutex_enter(&p->p_lock); 13640Sstevel@tonic-gate mutex_exit(&pidlock); 1365390Sraf if (p->p_flag & (SVFORK | SEXITING)) { 1366390Sraf mutex_exit(&p->p_lock); 1367390Sraf return (NULL); 1368390Sraf } 13690Sstevel@tonic-gate 13700Sstevel@tonic-gate /* 13710Sstevel@tonic-gate * Increment p_dtrace_probes so that the process knows to inform us 13720Sstevel@tonic-gate * when it exits or execs. fasttrap_provider_free() decrements this 13730Sstevel@tonic-gate * when we're done with this provider. 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate p->p_dtrace_probes++; 13760Sstevel@tonic-gate 13771677Sdp /* 13781677Sdp * Grab the credentials for this process so we have 13791677Sdp * something to pass to dtrace_register(). 13801677Sdp */ 13810Sstevel@tonic-gate mutex_enter(&p->p_crlock); 13821677Sdp crhold(p->p_cred); 13831677Sdp cred = p->p_cred; 13840Sstevel@tonic-gate mutex_exit(&p->p_crlock); 13850Sstevel@tonic-gate mutex_exit(&p->p_lock); 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1388532Sahl new_fp->ftp_pid = pid; 1389532Sahl new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1390532Sahl 1391532Sahl ASSERT(new_fp->ftp_proc != NULL); 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate /* 13960Sstevel@tonic-gate * Take another lap through the list to make sure a provider hasn't 13970Sstevel@tonic-gate * been created for this pid while we weren't under the bucket lock. 13980Sstevel@tonic-gate */ 13990Sstevel@tonic-gate for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 14000Sstevel@tonic-gate if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1401532Sahl !fp->ftp_retired) { 14020Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 14030Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 14040Sstevel@tonic-gate fasttrap_provider_free(new_fp); 14051677Sdp crfree(cred); 14060Sstevel@tonic-gate return (fp); 14070Sstevel@tonic-gate } 14080Sstevel@tonic-gate } 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate (void) strcpy(new_fp->ftp_name, name); 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate /* 14130Sstevel@tonic-gate * Fail and return NULL if either the provider name is too long 14140Sstevel@tonic-gate * or we fail to register this new provider with the DTrace 14150Sstevel@tonic-gate * framework. Note that this is the only place we ever construct 14160Sstevel@tonic-gate * the full provider name -- we keep it in pieces in the provider 14170Sstevel@tonic-gate * structure. 14180Sstevel@tonic-gate */ 14190Sstevel@tonic-gate if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 14200Sstevel@tonic-gate sizeof (provname) || 14210Sstevel@tonic-gate dtrace_register(provname, pattr, 14221677Sdp DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred, 14230Sstevel@tonic-gate pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 14240Sstevel@tonic-gate &new_fp->ftp_provid) != 0) { 14250Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 14260Sstevel@tonic-gate fasttrap_provider_free(new_fp); 14271677Sdp crfree(cred); 14280Sstevel@tonic-gate return (NULL); 14290Sstevel@tonic-gate } 14300Sstevel@tonic-gate 14310Sstevel@tonic-gate new_fp->ftp_next = bucket->ftb_data; 14320Sstevel@tonic-gate bucket->ftb_data = new_fp; 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate mutex_enter(&new_fp->ftp_mtx); 14350Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 14360Sstevel@tonic-gate 14371677Sdp crfree(cred); 14380Sstevel@tonic-gate return (new_fp); 14390Sstevel@tonic-gate } 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate static void 14420Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider) 14430Sstevel@tonic-gate { 14440Sstevel@tonic-gate pid_t pid = provider->ftp_pid; 14450Sstevel@tonic-gate proc_t *p; 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate /* 1448*1880Sahl * There need to be no associated enabled probes, no consumers 1449*1880Sahl * creating probes, and no meta providers referencing this provider. 14500Sstevel@tonic-gate */ 1451*1880Sahl ASSERT(provider->ftp_rcount == 0); 14520Sstevel@tonic-gate ASSERT(provider->ftp_ccount == 0); 1453*1880Sahl ASSERT(provider->ftp_mcount == 0); 14540Sstevel@tonic-gate 1455532Sahl fasttrap_proc_release(provider->ftp_proc); 1456532Sahl 14570Sstevel@tonic-gate kmem_free(provider, sizeof (fasttrap_provider_t)); 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate /* 14600Sstevel@tonic-gate * Decrement p_dtrace_probes on the process whose provider we're 14610Sstevel@tonic-gate * freeing. We don't have to worry about clobbering somone else's 14620Sstevel@tonic-gate * modifications to it because we have locked the bucket that 14630Sstevel@tonic-gate * corresponds to this process's hash chain in the provider hash 14640Sstevel@tonic-gate * table. Don't sweat it if we can't find the process. 14650Sstevel@tonic-gate */ 14660Sstevel@tonic-gate mutex_enter(&pidlock); 14670Sstevel@tonic-gate if ((p = prfind(pid)) == NULL) { 14680Sstevel@tonic-gate mutex_exit(&pidlock); 14690Sstevel@tonic-gate return; 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate 14720Sstevel@tonic-gate mutex_enter(&p->p_lock); 14730Sstevel@tonic-gate mutex_exit(&pidlock); 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate p->p_dtrace_probes--; 14760Sstevel@tonic-gate mutex_exit(&p->p_lock); 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate 14790Sstevel@tonic-gate static void 1480*1880Sahl fasttrap_provider_retire(pid_t pid, const char *name, int mprov) 14810Sstevel@tonic-gate { 1482935Sahl fasttrap_provider_t *fp; 1483935Sahl fasttrap_bucket_t *bucket; 1484935Sahl dtrace_provider_id_t provid; 1485935Sahl 1486935Sahl ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1487935Sahl 1488935Sahl bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1489935Sahl mutex_enter(&bucket->ftb_mtx); 1490935Sahl 1491935Sahl for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1492935Sahl if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1493935Sahl !fp->ftp_retired) 1494935Sahl break; 1495935Sahl } 1496935Sahl 1497935Sahl if (fp == NULL) { 1498935Sahl mutex_exit(&bucket->ftb_mtx); 1499935Sahl return; 1500935Sahl } 15010Sstevel@tonic-gate 1502*1880Sahl mutex_enter(&fp->ftp_mtx); 1503*1880Sahl ASSERT(!mprov || fp->ftp_mcount > 0); 1504*1880Sahl if (mprov && --fp->ftp_mcount != 0) { 1505*1880Sahl mutex_exit(&fp->ftp_mtx); 1506*1880Sahl mutex_exit(&bucket->ftb_mtx); 1507*1880Sahl return; 1508*1880Sahl } 1509*1880Sahl 15100Sstevel@tonic-gate /* 1511532Sahl * Mark the provider to be removed in our post-processing step, 1512532Sahl * mark it retired, and mark its proc as defunct (though it may 1513532Sahl * already be marked defunct by another provider that shares the 1514532Sahl * same proc). Marking it indicates that we should try to remove it; 1515532Sahl * setting the retired flag indicates that we're done with this 1516532Sahl * provider; setting the proc to be defunct indicates that all 1517532Sahl * tracepoints associated with the traced process should be ignored. 1518935Sahl * 1519935Sahl * We obviously need to take the bucket lock before the provider lock 1520935Sahl * to perform the lookup, but we need to drop the provider lock 1521935Sahl * before calling into the DTrace framework since we acquire the 1522935Sahl * provider lock in callbacks invoked from the DTrace framework. The 1523935Sahl * bucket lock therefore protects the integrity of the provider hash 1524935Sahl * table. 15250Sstevel@tonic-gate */ 1526935Sahl fp->ftp_proc->ftpc_defunct = 1; 1527935Sahl fp->ftp_retired = 1; 1528935Sahl fp->ftp_marked = 1; 1529935Sahl provid = fp->ftp_provid; 1530935Sahl mutex_exit(&fp->ftp_mtx); 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate /* 15330Sstevel@tonic-gate * We don't have to worry about invalidating the same provider twice 15340Sstevel@tonic-gate * since fasttrap_provider_lookup() will ignore provider that have 1535532Sahl * been marked as retired. 15360Sstevel@tonic-gate */ 15370Sstevel@tonic-gate dtrace_invalidate(provid); 15380Sstevel@tonic-gate 1539935Sahl mutex_exit(&bucket->ftb_mtx); 1540935Sahl 15410Sstevel@tonic-gate fasttrap_pid_cleanup(); 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate static int 15450Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata) 15460Sstevel@tonic-gate { 15470Sstevel@tonic-gate fasttrap_provider_t *provider; 15480Sstevel@tonic-gate fasttrap_probe_t *pp; 15490Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 15500Sstevel@tonic-gate char *name; 15510Sstevel@tonic-gate int i, aframes, whack; 15520Sstevel@tonic-gate 15530Sstevel@tonic-gate switch (pdata->ftps_type) { 15540Sstevel@tonic-gate case DTFTP_ENTRY: 15550Sstevel@tonic-gate name = "entry"; 15560Sstevel@tonic-gate aframes = FASTTRAP_ENTRY_AFRAMES; 15570Sstevel@tonic-gate break; 15580Sstevel@tonic-gate case DTFTP_RETURN: 15590Sstevel@tonic-gate name = "return"; 15600Sstevel@tonic-gate aframes = FASTTRAP_RETURN_AFRAMES; 15610Sstevel@tonic-gate break; 15620Sstevel@tonic-gate case DTFTP_OFFSETS: 15630Sstevel@tonic-gate name = NULL; 15640Sstevel@tonic-gate break; 15650Sstevel@tonic-gate default: 15660Sstevel@tonic-gate return (EINVAL); 15670Sstevel@tonic-gate } 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 15700Sstevel@tonic-gate FASTTRAP_PID_NAME, &pid_attr)) == NULL) 15710Sstevel@tonic-gate return (ESRCH); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * Increment this reference count to indicate that a consumer is 15750Sstevel@tonic-gate * actively adding a new probe associated with this provider. 15760Sstevel@tonic-gate */ 15770Sstevel@tonic-gate provider->ftp_ccount++; 15780Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 15790Sstevel@tonic-gate 15800Sstevel@tonic-gate if (name != NULL) { 15810Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, 15820Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name) != 0) 15830Sstevel@tonic-gate goto done; 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 15880Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 15890Sstevel@tonic-gate goto no_mem; 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate ASSERT(pdata->ftps_noffs > 0); 1593*1880Sahl pp = kmem_zalloc(offsetof(fasttrap_probe_t, 1594*1880Sahl ftp_tps[pdata->ftps_noffs]), KM_SLEEP); 15950Sstevel@tonic-gate 15960Sstevel@tonic-gate pp->ftp_prov = provider; 15970Sstevel@tonic-gate pp->ftp_faddr = pdata->ftps_pc; 15980Sstevel@tonic-gate pp->ftp_fsize = pdata->ftps_size; 15990Sstevel@tonic-gate pp->ftp_pid = pdata->ftps_pid; 16000Sstevel@tonic-gate pp->ftp_ntps = pdata->ftps_noffs; 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate for (i = 0; i < pdata->ftps_noffs; i++) { 16030Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 16040Sstevel@tonic-gate KM_SLEEP); 16050Sstevel@tonic-gate 1606532Sahl tp->ftt_proc = provider->ftp_proc; 16070Sstevel@tonic-gate tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 16080Sstevel@tonic-gate tp->ftt_pid = pdata->ftps_pid; 16090Sstevel@tonic-gate 16100Sstevel@tonic-gate pp->ftp_tps[i].fit_tp = tp; 16110Sstevel@tonic-gate pp->ftp_tps[i].fit_id.fti_probe = pp; 16121710Sahl pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type; 16130Sstevel@tonic-gate } 16140Sstevel@tonic-gate 16150Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 16160Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 16170Sstevel@tonic-gate } else { 16180Sstevel@tonic-gate for (i = 0; i < pdata->ftps_noffs; i++) { 16190Sstevel@tonic-gate char name_str[17]; 16200Sstevel@tonic-gate 16210Sstevel@tonic-gate (void) sprintf(name_str, "%llx", 16220Sstevel@tonic-gate (unsigned long long)pdata->ftps_offs[i]); 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, 16250Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 16260Sstevel@tonic-gate continue; 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate atomic_add_32(&fasttrap_total, 1); 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 16310Sstevel@tonic-gate atomic_add_32(&fasttrap_total, -1); 16320Sstevel@tonic-gate goto no_mem; 16330Sstevel@tonic-gate } 16340Sstevel@tonic-gate 16350Sstevel@tonic-gate pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate pp->ftp_prov = provider; 16380Sstevel@tonic-gate pp->ftp_faddr = pdata->ftps_pc; 16390Sstevel@tonic-gate pp->ftp_fsize = pdata->ftps_size; 16400Sstevel@tonic-gate pp->ftp_pid = pdata->ftps_pid; 16410Sstevel@tonic-gate pp->ftp_ntps = 1; 16420Sstevel@tonic-gate 16430Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 16440Sstevel@tonic-gate KM_SLEEP); 16450Sstevel@tonic-gate 1646532Sahl tp->ftt_proc = provider->ftp_proc; 16470Sstevel@tonic-gate tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 16480Sstevel@tonic-gate tp->ftt_pid = pdata->ftps_pid; 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate pp->ftp_tps[0].fit_tp = tp; 16510Sstevel@tonic-gate pp->ftp_tps[0].fit_id.fti_probe = pp; 16521710Sahl pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type; 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 16550Sstevel@tonic-gate pdata->ftps_mod, pdata->ftps_func, name_str, 16560Sstevel@tonic-gate FASTTRAP_OFFSET_AFRAMES, pp); 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate } 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate done: 16610Sstevel@tonic-gate /* 16620Sstevel@tonic-gate * We know that the provider is still valid since we incremented the 16630Sstevel@tonic-gate * reference count. If someone tried to free this provider while we 16640Sstevel@tonic-gate * were using it (e.g. because the process called exec(2) or exit(2)), 16650Sstevel@tonic-gate * take note of that and try to free it now. 16660Sstevel@tonic-gate */ 16670Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 16680Sstevel@tonic-gate provider->ftp_ccount--; 1669532Sahl whack = provider->ftp_retired; 16700Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate if (whack) 16730Sstevel@tonic-gate fasttrap_pid_cleanup(); 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate return (0); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate no_mem: 16780Sstevel@tonic-gate /* 16790Sstevel@tonic-gate * If we've exhausted the allowable resources, we'll try to remove 16800Sstevel@tonic-gate * this provider to free some up. This is to cover the case where 16810Sstevel@tonic-gate * the user has accidentally created many more probes than was 16820Sstevel@tonic-gate * intended (e.g. pid123:::). 16830Sstevel@tonic-gate */ 16840Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 16850Sstevel@tonic-gate provider->ftp_ccount--; 16860Sstevel@tonic-gate provider->ftp_marked = 1; 16870Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate fasttrap_pid_cleanup(); 16900Sstevel@tonic-gate 16910Sstevel@tonic-gate return (ENOMEM); 16920Sstevel@tonic-gate } 16930Sstevel@tonic-gate 16940Sstevel@tonic-gate /*ARGSUSED*/ 16950Sstevel@tonic-gate static void * 16960Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 16970Sstevel@tonic-gate { 16980Sstevel@tonic-gate fasttrap_provider_t *provider; 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate /* 17010Sstevel@tonic-gate * A 32-bit unsigned integer (like a pid for example) can be 17020Sstevel@tonic-gate * expressed in 10 or fewer decimal digits. Make sure that we'll 17030Sstevel@tonic-gate * have enough space for the provider name. 17040Sstevel@tonic-gate */ 17050Sstevel@tonic-gate if (strlen(dhpv->dthpv_provname) + 10 >= 17060Sstevel@tonic-gate sizeof (provider->ftp_name)) { 17070Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: " 17080Sstevel@tonic-gate "name too long to accomodate pid", dhpv->dthpv_provname); 17090Sstevel@tonic-gate return (NULL); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate /* 17130Sstevel@tonic-gate * Don't let folks spoof the true pid provider. 17140Sstevel@tonic-gate */ 17150Sstevel@tonic-gate if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 17160Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s: " 17170Sstevel@tonic-gate "%s is an invalid name", dhpv->dthpv_provname, 17180Sstevel@tonic-gate FASTTRAP_PID_NAME); 17190Sstevel@tonic-gate return (NULL); 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate /* 17230Sstevel@tonic-gate * The highest stability class that fasttrap supports is ISA; cap 17240Sstevel@tonic-gate * the stability of the new provider accordingly. 17250Sstevel@tonic-gate */ 17260Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_provider.dtat_class >= DTRACE_CLASS_COMMON) 17270Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 17280Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_mod.dtat_class >= DTRACE_CLASS_COMMON) 17290Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 17300Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_func.dtat_class >= DTRACE_CLASS_COMMON) 17310Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 17320Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_name.dtat_class >= DTRACE_CLASS_COMMON) 17330Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 17340Sstevel@tonic-gate if (dhpv->dthpv_pattr.dtpa_args.dtat_class >= DTRACE_CLASS_COMMON) 17350Sstevel@tonic-gate dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 17360Sstevel@tonic-gate 17370Sstevel@tonic-gate if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 17380Sstevel@tonic-gate &dhpv->dthpv_pattr)) == NULL) { 17390Sstevel@tonic-gate cmn_err(CE_WARN, "failed to instantiate provider %s for " 17400Sstevel@tonic-gate "process %u", dhpv->dthpv_provname, (uint_t)pid); 17410Sstevel@tonic-gate return (NULL); 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate /* 1745*1880Sahl * Up the meta provider count so this provider isn't removed until 1746*1880Sahl * the meta provider has been told to remove it. 17470Sstevel@tonic-gate */ 1748*1880Sahl provider->ftp_mcount++; 17490Sstevel@tonic-gate 17500Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate return (provider); 17530Sstevel@tonic-gate } 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate /*ARGSUSED*/ 17560Sstevel@tonic-gate static void 17570Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg, 17580Sstevel@tonic-gate dtrace_helper_probedesc_t *dhpb) 17590Sstevel@tonic-gate { 17600Sstevel@tonic-gate fasttrap_provider_t *provider = parg; 17610Sstevel@tonic-gate fasttrap_probe_t *pp; 17620Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 17631710Sahl int i, j; 17641710Sahl uint32_t ntps; 17650Sstevel@tonic-gate 17660Sstevel@tonic-gate mutex_enter(&provider->ftp_mtx); 17670Sstevel@tonic-gate 17680Sstevel@tonic-gate if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 17690Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 17700Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 17710Sstevel@tonic-gate return; 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate 17741710Sahl ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs; 1775*1880Sahl ASSERT(ntps > 0); 17761710Sahl 17771710Sahl atomic_add_32(&fasttrap_total, ntps); 17780Sstevel@tonic-gate 17790Sstevel@tonic-gate if (fasttrap_total > fasttrap_max) { 17801710Sahl atomic_add_32(&fasttrap_total, -ntps); 17810Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 17820Sstevel@tonic-gate return; 17830Sstevel@tonic-gate } 17840Sstevel@tonic-gate 1785*1880Sahl pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP); 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate pp->ftp_prov = provider; 17880Sstevel@tonic-gate pp->ftp_pid = provider->ftp_pid; 17891710Sahl pp->ftp_ntps = ntps; 17900Sstevel@tonic-gate pp->ftp_nargs = dhpb->dthpb_xargc; 17910Sstevel@tonic-gate pp->ftp_xtypes = dhpb->dthpb_xtypes; 17920Sstevel@tonic-gate pp->ftp_ntypes = dhpb->dthpb_ntypes; 17930Sstevel@tonic-gate 17941710Sahl /* 17951710Sahl * First create a tracepoint for each actual point of interest. 17961710Sahl */ 17971710Sahl for (i = 0; i < dhpb->dthpb_noffs; i++) { 17980Sstevel@tonic-gate tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 17990Sstevel@tonic-gate 1800532Sahl tp->ftt_proc = provider->ftp_proc; 18010Sstevel@tonic-gate tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 18020Sstevel@tonic-gate tp->ftt_pid = provider->ftp_pid; 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate pp->ftp_tps[i].fit_tp = tp; 18050Sstevel@tonic-gate pp->ftp_tps[i].fit_id.fti_probe = pp; 18061710Sahl #ifdef __sparc 18071710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS; 18081710Sahl #else 18091710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS; 18101710Sahl #endif 18111710Sahl } 18121710Sahl 18131710Sahl /* 18141710Sahl * Then create a tracepoint for each is-enabled point. 18151710Sahl */ 18161710Sahl for (j = 0; i < ntps; i++, j++) { 18171710Sahl tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 18181710Sahl 18191710Sahl tp->ftt_proc = provider->ftp_proc; 18201710Sahl tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j]; 18211710Sahl tp->ftt_pid = provider->ftp_pid; 18221710Sahl 18231710Sahl pp->ftp_tps[i].fit_tp = tp; 18241710Sahl pp->ftp_tps[i].fit_id.fti_probe = pp; 18251710Sahl pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED; 18260Sstevel@tonic-gate } 18270Sstevel@tonic-gate 18280Sstevel@tonic-gate /* 18290Sstevel@tonic-gate * If the arguments are shuffled around we set the argument remapping 18300Sstevel@tonic-gate * table. Later, when the probe fires, we only remap the arguments 18310Sstevel@tonic-gate * if the table is non-NULL. 18320Sstevel@tonic-gate */ 18330Sstevel@tonic-gate for (i = 0; i < dhpb->dthpb_xargc; i++) { 18340Sstevel@tonic-gate if (dhpb->dthpb_args[i] != i) { 18350Sstevel@tonic-gate pp->ftp_argmap = dhpb->dthpb_args; 18360Sstevel@tonic-gate break; 18370Sstevel@tonic-gate } 18380Sstevel@tonic-gate } 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate /* 18410Sstevel@tonic-gate * The probe is fully constructed -- register it with DTrace. 18420Sstevel@tonic-gate */ 18430Sstevel@tonic-gate pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 18440Sstevel@tonic-gate dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate mutex_exit(&provider->ftp_mtx); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate /*ARGSUSED*/ 18500Sstevel@tonic-gate static void 18510Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 18520Sstevel@tonic-gate { 1853935Sahl /* 1854935Sahl * Clean up the USDT provider. There may be active consumers of the 1855935Sahl * provider busy adding probes, no damage will actually befall the 1856935Sahl * provider until that count has dropped to zero. This just puts 1857935Sahl * the provider on death row. 1858935Sahl */ 1859935Sahl fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1); 18600Sstevel@tonic-gate } 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = { 18630Sstevel@tonic-gate fasttrap_meta_create_probe, 18640Sstevel@tonic-gate fasttrap_meta_provide, 18650Sstevel@tonic-gate fasttrap_meta_remove 18660Sstevel@tonic-gate }; 18670Sstevel@tonic-gate 18680Sstevel@tonic-gate /*ARGSUSED*/ 18690Sstevel@tonic-gate static int 18700Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 18710Sstevel@tonic-gate { 18720Sstevel@tonic-gate return (0); 18730Sstevel@tonic-gate } 18740Sstevel@tonic-gate 18750Sstevel@tonic-gate /*ARGSUSED*/ 18760Sstevel@tonic-gate static int 18770Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 18780Sstevel@tonic-gate { 18790Sstevel@tonic-gate if (!dtrace_attached()) 18800Sstevel@tonic-gate return (EAGAIN); 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate if (cmd == FASTTRAPIOC_MAKEPROBE) { 18830Sstevel@tonic-gate fasttrap_probe_spec_t *uprobe = (void *)arg; 18840Sstevel@tonic-gate fasttrap_probe_spec_t *probe; 18850Sstevel@tonic-gate uint64_t noffs; 18860Sstevel@tonic-gate size_t size; 18870Sstevel@tonic-gate int ret; 18880Sstevel@tonic-gate char *c; 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate if (copyin(&uprobe->ftps_noffs, &noffs, 18910Sstevel@tonic-gate sizeof (uprobe->ftps_noffs))) 18920Sstevel@tonic-gate return (EFAULT); 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * Probes must have at least one tracepoint. 18960Sstevel@tonic-gate */ 18970Sstevel@tonic-gate if (noffs == 0) 18980Sstevel@tonic-gate return (EINVAL); 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate size = sizeof (fasttrap_probe_spec_t) + 19010Sstevel@tonic-gate sizeof (probe->ftps_offs[0]) * (noffs - 1); 19020Sstevel@tonic-gate 19030Sstevel@tonic-gate if (size > 1024 * 1024) 19040Sstevel@tonic-gate return (ENOMEM); 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate probe = kmem_alloc(size, KM_SLEEP); 19070Sstevel@tonic-gate 19080Sstevel@tonic-gate if (copyin(uprobe, probe, size) != 0) { 19090Sstevel@tonic-gate kmem_free(probe, size); 19100Sstevel@tonic-gate return (EFAULT); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate 19130Sstevel@tonic-gate /* 19140Sstevel@tonic-gate * Verify that the function and module strings contain no 19150Sstevel@tonic-gate * funny characters. 19160Sstevel@tonic-gate */ 19170Sstevel@tonic-gate for (c = &probe->ftps_func[0]; *c != '\0'; c++) { 19180Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) { 19190Sstevel@tonic-gate ret = EINVAL; 19200Sstevel@tonic-gate goto err; 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate } 19230Sstevel@tonic-gate 19240Sstevel@tonic-gate for (c = &probe->ftps_mod[0]; *c != '\0'; c++) { 19250Sstevel@tonic-gate if (*c < 0x20 || 0x7f <= *c) { 19260Sstevel@tonic-gate ret = EINVAL; 19270Sstevel@tonic-gate goto err; 19280Sstevel@tonic-gate } 19290Sstevel@tonic-gate } 19300Sstevel@tonic-gate 19310Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 19320Sstevel@tonic-gate proc_t *p; 19330Sstevel@tonic-gate pid_t pid = probe->ftps_pid; 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate mutex_enter(&pidlock); 19360Sstevel@tonic-gate /* 19370Sstevel@tonic-gate * Report an error if the process doesn't exist 19380Sstevel@tonic-gate * or is actively being birthed. 19390Sstevel@tonic-gate */ 19400Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 19410Sstevel@tonic-gate mutex_exit(&pidlock); 19420Sstevel@tonic-gate return (ESRCH); 19430Sstevel@tonic-gate } 19440Sstevel@tonic-gate mutex_enter(&p->p_lock); 19450Sstevel@tonic-gate mutex_exit(&pidlock); 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL, 19480Sstevel@tonic-gate VREAD | VWRITE)) != 0) { 19490Sstevel@tonic-gate mutex_exit(&p->p_lock); 19500Sstevel@tonic-gate return (ret); 19510Sstevel@tonic-gate } 19520Sstevel@tonic-gate 19530Sstevel@tonic-gate mutex_exit(&p->p_lock); 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate ret = fasttrap_add_probe(probe); 19570Sstevel@tonic-gate err: 19580Sstevel@tonic-gate kmem_free(probe, size); 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate return (ret); 19610Sstevel@tonic-gate 19620Sstevel@tonic-gate } else if (cmd == FASTTRAPIOC_GETINSTR) { 19630Sstevel@tonic-gate fasttrap_instr_query_t instr; 19640Sstevel@tonic-gate fasttrap_tracepoint_t *tp; 19650Sstevel@tonic-gate uint_t index; 19660Sstevel@tonic-gate int ret; 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 19690Sstevel@tonic-gate return (EFAULT); 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 19720Sstevel@tonic-gate proc_t *p; 19730Sstevel@tonic-gate pid_t pid = instr.ftiq_pid; 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate mutex_enter(&pidlock); 19760Sstevel@tonic-gate /* 19770Sstevel@tonic-gate * Report an error if the process doesn't exist 19780Sstevel@tonic-gate * or is actively being birthed. 19790Sstevel@tonic-gate */ 19800Sstevel@tonic-gate if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) { 19810Sstevel@tonic-gate mutex_exit(&pidlock); 19820Sstevel@tonic-gate return (ESRCH); 19830Sstevel@tonic-gate } 19840Sstevel@tonic-gate mutex_enter(&p->p_lock); 19850Sstevel@tonic-gate mutex_exit(&pidlock); 19860Sstevel@tonic-gate 19870Sstevel@tonic-gate if ((ret = priv_proc_cred_perm(cr, p, NULL, 19880Sstevel@tonic-gate VREAD)) != 0) { 19890Sstevel@tonic-gate mutex_exit(&p->p_lock); 19900Sstevel@tonic-gate return (ret); 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate 19930Sstevel@tonic-gate mutex_exit(&p->p_lock); 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 19970Sstevel@tonic-gate 19980Sstevel@tonic-gate mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 19990Sstevel@tonic-gate tp = fasttrap_tpoints.fth_table[index].ftb_data; 20000Sstevel@tonic-gate while (tp != NULL) { 20010Sstevel@tonic-gate if (instr.ftiq_pid == tp->ftt_pid && 20020Sstevel@tonic-gate instr.ftiq_pc == tp->ftt_pc && 2003532Sahl !tp->ftt_proc->ftpc_defunct) 20040Sstevel@tonic-gate break; 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate tp = tp->ftt_next; 20070Sstevel@tonic-gate } 20080Sstevel@tonic-gate 20090Sstevel@tonic-gate if (tp == NULL) { 20100Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 20110Sstevel@tonic-gate return (ENOENT); 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate bcopy(&tp->ftt_instr, &instr.ftiq_instr, 20150Sstevel@tonic-gate sizeof (instr.ftiq_instr)); 20160Sstevel@tonic-gate mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 20190Sstevel@tonic-gate return (EFAULT); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate return (0); 20220Sstevel@tonic-gate } 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate return (EINVAL); 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate 20270Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = { 20280Sstevel@tonic-gate fasttrap_open, /* open */ 20290Sstevel@tonic-gate nodev, /* close */ 20300Sstevel@tonic-gate nulldev, /* strategy */ 20310Sstevel@tonic-gate nulldev, /* print */ 20320Sstevel@tonic-gate nodev, /* dump */ 20330Sstevel@tonic-gate nodev, /* read */ 20340Sstevel@tonic-gate nodev, /* write */ 20350Sstevel@tonic-gate fasttrap_ioctl, /* ioctl */ 20360Sstevel@tonic-gate nodev, /* devmap */ 20370Sstevel@tonic-gate nodev, /* mmap */ 20380Sstevel@tonic-gate nodev, /* segmap */ 20390Sstevel@tonic-gate nochpoll, /* poll */ 20400Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 20410Sstevel@tonic-gate 0, /* streamtab */ 20420Sstevel@tonic-gate D_NEW | D_MP /* Driver compatibility flag */ 20430Sstevel@tonic-gate }; 20440Sstevel@tonic-gate 20450Sstevel@tonic-gate /*ARGSUSED*/ 20460Sstevel@tonic-gate static int 20470Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 20480Sstevel@tonic-gate { 20490Sstevel@tonic-gate int error; 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate switch (infocmd) { 20520Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 20530Sstevel@tonic-gate *result = (void *)fasttrap_devi; 20540Sstevel@tonic-gate error = DDI_SUCCESS; 20550Sstevel@tonic-gate break; 20560Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 20570Sstevel@tonic-gate *result = (void *)0; 20580Sstevel@tonic-gate error = DDI_SUCCESS; 20590Sstevel@tonic-gate break; 20600Sstevel@tonic-gate default: 20610Sstevel@tonic-gate error = DDI_FAILURE; 20620Sstevel@tonic-gate } 20630Sstevel@tonic-gate return (error); 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate static int 20670Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 20680Sstevel@tonic-gate { 20690Sstevel@tonic-gate ulong_t nent; 20700Sstevel@tonic-gate 20710Sstevel@tonic-gate switch (cmd) { 20720Sstevel@tonic-gate case DDI_ATTACH: 20730Sstevel@tonic-gate break; 20740Sstevel@tonic-gate case DDI_RESUME: 20750Sstevel@tonic-gate return (DDI_SUCCESS); 20760Sstevel@tonic-gate default: 20770Sstevel@tonic-gate return (DDI_FAILURE); 20780Sstevel@tonic-gate } 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0, 20810Sstevel@tonic-gate DDI_PSEUDO, NULL) == DDI_FAILURE || 20821677Sdp dtrace_register("fasttrap", &fasttrap_attr, DTRACE_PRIV_USER, NULL, 20830Sstevel@tonic-gate &fasttrap_pops, NULL, &fasttrap_id) != 0) { 20840Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 20850Sstevel@tonic-gate return (DDI_FAILURE); 20860Sstevel@tonic-gate } 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate ddi_report_dev(devi); 20890Sstevel@tonic-gate fasttrap_devi = devi; 20900Sstevel@tonic-gate 20910Sstevel@tonic-gate /* 20920Sstevel@tonic-gate * Install our hooks into fork(2), exec(2), and exit(2). 20930Sstevel@tonic-gate */ 20940Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = &fasttrap_fork; 20950Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit; 20960Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit; 20970Sstevel@tonic-gate 20980Sstevel@tonic-gate fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 20990Sstevel@tonic-gate "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 21000Sstevel@tonic-gate fasttrap_total = 0; 21010Sstevel@tonic-gate 21020Sstevel@tonic-gate /* 21030Sstevel@tonic-gate * Conjure up the tracepoints hashtable... 21040Sstevel@tonic-gate */ 21050Sstevel@tonic-gate nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 21060Sstevel@tonic-gate "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate if (nent <= 0 || nent > 0x1000000) 21090Sstevel@tonic-gate nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate if ((nent & (nent - 1)) == 0) 21120Sstevel@tonic-gate fasttrap_tpoints.fth_nent = nent; 21130Sstevel@tonic-gate else 21140Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 21150Sstevel@tonic-gate ASSERT(fasttrap_tpoints.fth_nent > 0); 21160Sstevel@tonic-gate fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 21170Sstevel@tonic-gate fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 21180Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP); 21190Sstevel@tonic-gate 21200Sstevel@tonic-gate /* 2121532Sahl * ... and the providers hash table... 21220Sstevel@tonic-gate */ 21230Sstevel@tonic-gate nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 21240Sstevel@tonic-gate if ((nent & (nent - 1)) == 0) 21250Sstevel@tonic-gate fasttrap_provs.fth_nent = nent; 21260Sstevel@tonic-gate else 21270Sstevel@tonic-gate fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 21280Sstevel@tonic-gate ASSERT(fasttrap_provs.fth_nent > 0); 21290Sstevel@tonic-gate fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2130532Sahl fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2131532Sahl sizeof (fasttrap_bucket_t), KM_SLEEP); 21320Sstevel@tonic-gate 2133532Sahl /* 2134532Sahl * ... and the procs hash table. 2135532Sahl */ 2136532Sahl nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2137532Sahl if ((nent & (nent - 1)) == 0) 2138532Sahl fasttrap_procs.fth_nent = nent; 2139532Sahl else 2140532Sahl fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2141532Sahl ASSERT(fasttrap_procs.fth_nent > 0); 2142532Sahl fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2143532Sahl fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 21440Sstevel@tonic-gate sizeof (fasttrap_bucket_t), KM_SLEEP); 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 21470Sstevel@tonic-gate &fasttrap_meta_id); 21480Sstevel@tonic-gate 21490Sstevel@tonic-gate return (DDI_SUCCESS); 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate 21520Sstevel@tonic-gate static int 21530Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd) 21540Sstevel@tonic-gate { 21550Sstevel@tonic-gate int i, fail = 0; 21560Sstevel@tonic-gate timeout_id_t tmp; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate switch (cmd) { 21590Sstevel@tonic-gate case DDI_DETACH: 21600Sstevel@tonic-gate break; 21610Sstevel@tonic-gate case DDI_SUSPEND: 21620Sstevel@tonic-gate return (DDI_SUCCESS); 21630Sstevel@tonic-gate default: 21640Sstevel@tonic-gate return (DDI_FAILURE); 21650Sstevel@tonic-gate } 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate /* 21680Sstevel@tonic-gate * Unregister the meta-provider to make sure no new fasttrap- 21690Sstevel@tonic-gate * managed providers come along while we're trying to close up 21700Sstevel@tonic-gate * shop. If we fail to detach, we'll need to re-register as a 21710Sstevel@tonic-gate * meta-provider. We can fail to unregister as a meta-provider 21720Sstevel@tonic-gate * if providers we manage still exist. 21730Sstevel@tonic-gate */ 21740Sstevel@tonic-gate if (fasttrap_meta_id != DTRACE_METAPROVNONE && 21750Sstevel@tonic-gate dtrace_meta_unregister(fasttrap_meta_id) != 0) 21760Sstevel@tonic-gate return (DDI_FAILURE); 21770Sstevel@tonic-gate 21780Sstevel@tonic-gate /* 21790Sstevel@tonic-gate * Prevent any new timeouts from running by setting fasttrap_timeout 21800Sstevel@tonic-gate * to a non-zero value, and wait for the current timeout to complete. 21810Sstevel@tonic-gate */ 21820Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 21830Sstevel@tonic-gate fasttrap_cleanup_work = 0; 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate while (fasttrap_timeout != (timeout_id_t)1) { 21860Sstevel@tonic-gate tmp = fasttrap_timeout; 21870Sstevel@tonic-gate fasttrap_timeout = (timeout_id_t)1; 21880Sstevel@tonic-gate 21890Sstevel@tonic-gate if (tmp != 0) { 21900Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 21910Sstevel@tonic-gate (void) untimeout(tmp); 21920Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 21930Sstevel@tonic-gate } 21940Sstevel@tonic-gate } 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate fasttrap_cleanup_work = 0; 21970Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate /* 22000Sstevel@tonic-gate * Iterate over all of our providers. If there's still a process 22010Sstevel@tonic-gate * that corresponds to that pid, fail to detach. 22020Sstevel@tonic-gate */ 22030Sstevel@tonic-gate for (i = 0; i < fasttrap_provs.fth_nent; i++) { 22040Sstevel@tonic-gate fasttrap_provider_t **fpp, *fp; 22050Sstevel@tonic-gate fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate mutex_enter(&bucket->ftb_mtx); 22080Sstevel@tonic-gate fpp = (fasttrap_provider_t **)&bucket->ftb_data; 22090Sstevel@tonic-gate while ((fp = *fpp) != NULL) { 22100Sstevel@tonic-gate /* 22110Sstevel@tonic-gate * Acquire and release the lock as a simple way of 22120Sstevel@tonic-gate * waiting for any other consumer to finish with 22130Sstevel@tonic-gate * this provider. A thread must first acquire the 22140Sstevel@tonic-gate * bucket lock so there's no chance of another thread 2215935Sahl * blocking on the provider's lock. 22160Sstevel@tonic-gate */ 22170Sstevel@tonic-gate mutex_enter(&fp->ftp_mtx); 22180Sstevel@tonic-gate mutex_exit(&fp->ftp_mtx); 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate if (dtrace_unregister(fp->ftp_provid) != 0) { 22210Sstevel@tonic-gate fail = 1; 22220Sstevel@tonic-gate fpp = &fp->ftp_next; 22230Sstevel@tonic-gate } else { 22240Sstevel@tonic-gate *fpp = fp->ftp_next; 22250Sstevel@tonic-gate fasttrap_provider_free(fp); 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate } 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate mutex_exit(&bucket->ftb_mtx); 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate if (fail || dtrace_unregister(fasttrap_id) != 0) { 22330Sstevel@tonic-gate uint_t work; 22340Sstevel@tonic-gate /* 22350Sstevel@tonic-gate * If we're failing to detach, we need to unblock timeouts 22360Sstevel@tonic-gate * and start a new timeout if any work has accumulated while 22370Sstevel@tonic-gate * we've been unsuccessfully trying to detach. 22380Sstevel@tonic-gate */ 22390Sstevel@tonic-gate mutex_enter(&fasttrap_cleanup_mtx); 22400Sstevel@tonic-gate fasttrap_timeout = 0; 22410Sstevel@tonic-gate work = fasttrap_cleanup_work; 22420Sstevel@tonic-gate mutex_exit(&fasttrap_cleanup_mtx); 22430Sstevel@tonic-gate 22440Sstevel@tonic-gate if (work) 22450Sstevel@tonic-gate fasttrap_pid_cleanup(); 22460Sstevel@tonic-gate 22470Sstevel@tonic-gate (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 22480Sstevel@tonic-gate &fasttrap_meta_id); 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate return (DDI_FAILURE); 22510Sstevel@tonic-gate } 22520Sstevel@tonic-gate 22530Sstevel@tonic-gate #ifdef DEBUG 22540Sstevel@tonic-gate mutex_enter(&fasttrap_count_mtx); 22550Sstevel@tonic-gate ASSERT(fasttrap_count == 0); 22560Sstevel@tonic-gate mutex_exit(&fasttrap_count_mtx); 22570Sstevel@tonic-gate #endif 22580Sstevel@tonic-gate 22590Sstevel@tonic-gate kmem_free(fasttrap_tpoints.fth_table, 22600Sstevel@tonic-gate fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 22610Sstevel@tonic-gate fasttrap_tpoints.fth_nent = 0; 22620Sstevel@tonic-gate 22630Sstevel@tonic-gate kmem_free(fasttrap_provs.fth_table, 22640Sstevel@tonic-gate fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 22650Sstevel@tonic-gate fasttrap_provs.fth_nent = 0; 22660Sstevel@tonic-gate 2267532Sahl kmem_free(fasttrap_procs.fth_table, 2268532Sahl fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2269532Sahl fasttrap_procs.fth_nent = 0; 2270532Sahl 22710Sstevel@tonic-gate /* 22720Sstevel@tonic-gate * We know there are no tracepoints in any process anywhere in 22730Sstevel@tonic-gate * the system so there is no process which has its p_dtrace_count 22740Sstevel@tonic-gate * greater than zero, therefore we know that no thread can actively 22750Sstevel@tonic-gate * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes 22760Sstevel@tonic-gate * and fasttrap_exec() and fasttrap_exit(). 22770Sstevel@tonic-gate */ 22780Sstevel@tonic-gate ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork); 22790Sstevel@tonic-gate dtrace_fasttrap_fork_ptr = NULL; 22800Sstevel@tonic-gate 22810Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit); 22820Sstevel@tonic-gate dtrace_fasttrap_exec_ptr = NULL; 22830Sstevel@tonic-gate 22840Sstevel@tonic-gate ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit); 22850Sstevel@tonic-gate dtrace_fasttrap_exit_ptr = NULL; 22860Sstevel@tonic-gate 22870Sstevel@tonic-gate ddi_remove_minor_node(devi, NULL); 22880Sstevel@tonic-gate 22890Sstevel@tonic-gate return (DDI_SUCCESS); 22900Sstevel@tonic-gate } 22910Sstevel@tonic-gate 22920Sstevel@tonic-gate static struct dev_ops fasttrap_ops = { 22930Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 22940Sstevel@tonic-gate 0, /* refcnt */ 22950Sstevel@tonic-gate fasttrap_info, /* get_dev_info */ 22960Sstevel@tonic-gate nulldev, /* identify */ 22970Sstevel@tonic-gate nulldev, /* probe */ 22980Sstevel@tonic-gate fasttrap_attach, /* attach */ 22990Sstevel@tonic-gate fasttrap_detach, /* detach */ 23000Sstevel@tonic-gate nodev, /* reset */ 23010Sstevel@tonic-gate &fasttrap_cb_ops, /* driver operations */ 23020Sstevel@tonic-gate NULL, /* bus operations */ 23030Sstevel@tonic-gate nodev /* dev power */ 23040Sstevel@tonic-gate }; 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate /* 23070Sstevel@tonic-gate * Module linkage information for the kernel. 23080Sstevel@tonic-gate */ 23090Sstevel@tonic-gate static struct modldrv modldrv = { 23100Sstevel@tonic-gate &mod_driverops, /* module type (this is a pseudo driver) */ 23110Sstevel@tonic-gate "Fasttrap Tracing", /* name of module */ 23120Sstevel@tonic-gate &fasttrap_ops, /* driver ops */ 23130Sstevel@tonic-gate }; 23140Sstevel@tonic-gate 23150Sstevel@tonic-gate static struct modlinkage modlinkage = { 23160Sstevel@tonic-gate MODREV_1, 23170Sstevel@tonic-gate (void *)&modldrv, 23180Sstevel@tonic-gate NULL 23190Sstevel@tonic-gate }; 23200Sstevel@tonic-gate 23210Sstevel@tonic-gate int 23220Sstevel@tonic-gate _init(void) 23230Sstevel@tonic-gate { 23240Sstevel@tonic-gate return (mod_install(&modlinkage)); 23250Sstevel@tonic-gate } 23260Sstevel@tonic-gate 23270Sstevel@tonic-gate int 23280Sstevel@tonic-gate _info(struct modinfo *modinfop) 23290Sstevel@tonic-gate { 23300Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 23310Sstevel@tonic-gate } 23320Sstevel@tonic-gate 23330Sstevel@tonic-gate int 23340Sstevel@tonic-gate _fini(void) 23350Sstevel@tonic-gate { 23360Sstevel@tonic-gate return (mod_remove(&modlinkage)); 23370Sstevel@tonic-gate } 2338