xref: /onnv-gate/usr/src/uts/common/dtrace/fasttrap.c (revision 7656:2621e50fdf4a)
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 /*
236390Sahl  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/atomic.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/stat.h>
310Sstevel@tonic-gate #include <sys/modctl.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/ddi.h>
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/cpuvar.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/strsubr.h>
390Sstevel@tonic-gate #include <sys/fasttrap.h>
400Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
410Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
420Sstevel@tonic-gate #include <sys/dtrace.h>
430Sstevel@tonic-gate #include <sys/dtrace_impl.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/proc.h>
460Sstevel@tonic-gate #include <sys/priv.h>
470Sstevel@tonic-gate #include <sys/policy.h>
483944Sahl #include <util/qsort.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * User-Land Trap-Based Tracing
520Sstevel@tonic-gate  * ----------------------------
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * The fasttrap provider allows DTrace consumers to instrument any user-level
550Sstevel@tonic-gate  * instruction to gather data; this includes probes with semantic
560Sstevel@tonic-gate  * signifigance like entry and return as well as simple offsets into the
570Sstevel@tonic-gate  * function. While the specific techniques used are very ISA specific, the
580Sstevel@tonic-gate  * methodology is generalizable to any architecture.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * The General Methodology
620Sstevel@tonic-gate  * -----------------------
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * With the primary goal of tracing every user-land instruction and the
650Sstevel@tonic-gate  * limitation that we can't trust user space so don't want to rely on much
660Sstevel@tonic-gate  * information there, we begin by replacing the instructions we want to trace
670Sstevel@tonic-gate  * with trap instructions. Each instruction we overwrite is saved into a hash
680Sstevel@tonic-gate  * table keyed by process ID and pc address. When we enter the kernel due to
690Sstevel@tonic-gate  * this trap instruction, we need the effects of the replaced instruction to
700Sstevel@tonic-gate  * appear to have occurred before we proceed with the user thread's
710Sstevel@tonic-gate  * execution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * Each user level thread is represented by a ulwp_t structure which is
740Sstevel@tonic-gate  * always easily accessible through a register. The most basic way to produce
750Sstevel@tonic-gate  * the effects of the instruction we replaced is to copy that instruction out
760Sstevel@tonic-gate  * to a bit of scratch space reserved in the user thread's ulwp_t structure
770Sstevel@tonic-gate  * (a sort of kernel-private thread local storage), set the PC to that
780Sstevel@tonic-gate  * scratch space and single step. When we reenter the kernel after single
790Sstevel@tonic-gate  * stepping the instruction we must then adjust the PC to point to what would
800Sstevel@tonic-gate  * normally be the next instruction. Of course, special care must be taken
810Sstevel@tonic-gate  * for branches and jumps, but these represent such a small fraction of any
820Sstevel@tonic-gate  * instruction set that writing the code to emulate these in the kernel is
830Sstevel@tonic-gate  * not too difficult.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  * Return probes may require several tracepoints to trace every return site,
860Sstevel@tonic-gate  * and, conversely, each tracepoint may activate several probes (the entry
870Sstevel@tonic-gate  * and offset 0 probes, for example). To solve this muliplexing problem,
880Sstevel@tonic-gate  * tracepoints contain lists of probes to activate and probes contain lists
890Sstevel@tonic-gate  * of tracepoints to enable. If a probe is activated, it adds its ID to
900Sstevel@tonic-gate  * existing tracepoints or creates new ones as necessary.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Most probes are activated _before_ the instruction is executed, but return
930Sstevel@tonic-gate  * probes are activated _after_ the effects of the last instruction of the
940Sstevel@tonic-gate  * function are visible. Return probes must be fired _after_ we have
950Sstevel@tonic-gate  * single-stepped the instruction whereas all other probes are fired
960Sstevel@tonic-gate  * beforehand.
972179Sahl  *
982179Sahl  *
992179Sahl  * Lock Ordering
1002179Sahl  * -------------
1012179Sahl  *
1022179Sahl  * The lock ordering below -- both internally and with respect to the DTrace
1032179Sahl  * framework -- is a little tricky and bears some explanation. Each provider
1042179Sahl  * has a lock (ftp_mtx) that protects its members including reference counts
1052179Sahl  * for enabled probes (ftp_rcount), consumers actively creating probes
1062179Sahl  * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
1072179Sahl  * from being freed. A provider is looked up by taking the bucket lock for the
1082179Sahl  * provider hash table, and is returned with its lock held. The provider lock
1092179Sahl  * may be taken in functions invoked by the DTrace framework, but may not be
1102179Sahl  * held while calling functions in the DTrace framework.
1112179Sahl  *
1122179Sahl  * To ensure consistency over multiple calls to the DTrace framework, the
1132179Sahl  * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
1142179Sahl  * not be taken when holding the provider lock as that would create a cyclic
1152179Sahl  * lock ordering. In situations where one would naturally take the provider
1162179Sahl  * lock and then the creation lock, we instead up a reference count to prevent
1172179Sahl  * the provider from disappearing, drop the provider lock, and acquire the
1182179Sahl  * creation lock.
1192179Sahl  *
1202179Sahl  * Briefly:
1212179Sahl  * 	bucket lock before provider lock
1222179Sahl  *	DTrace before provider lock
1232179Sahl  *	creation lock before DTrace
1242179Sahl  *	never hold the provider lock and creation lock simultaneously
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static dev_info_t *fasttrap_devi;
1280Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static timeout_id_t fasttrap_timeout;
1310Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx;
1320Sstevel@tonic-gate static uint_t fasttrap_cleanup_work;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Generation count on modifications to the global tracepoint lookup table.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate  * When the fasttrap provider is loaded, fasttrap_max is set to either
1410Sstevel@tonic-gate  * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
1420Sstevel@tonic-gate  * fasttrap.conf file. Each time a probe is created, fasttrap_total is
1430Sstevel@tonic-gate  * incremented by the number of tracepoints that may be associated with that
1440Sstevel@tonic-gate  * probe; fasttrap_total is capped at fasttrap_max.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate #define	FASTTRAP_MAX_DEFAULT		250000
1470Sstevel@tonic-gate static uint32_t fasttrap_max;
1480Sstevel@tonic-gate static uint32_t fasttrap_total;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
1520Sstevel@tonic-gate #define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
153532Sahl #define	FASTTRAP_PROCS_DEFAULT_SIZE	0x100
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #define	FASTTRAP_PID_NAME		"pid"
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate fasttrap_hash_t			fasttrap_tpoints;
1580Sstevel@tonic-gate static fasttrap_hash_t		fasttrap_provs;
159532Sahl static fasttrap_hash_t		fasttrap_procs;
1600Sstevel@tonic-gate 
1612179Sahl static uint64_t			fasttrap_pid_count;	/* pid ref count */
1620Sstevel@tonic-gate static kmutex_t			fasttrap_count_mtx;	/* lock on ref count */
1630Sstevel@tonic-gate 
164315Sahl #define	FASTTRAP_ENABLE_FAIL	1
165315Sahl #define	FASTTRAP_ENABLE_PARTIAL	2
166315Sahl 
1670Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
1680Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
1710Sstevel@tonic-gate     const dtrace_pattr_t *);
172935Sahl static void fasttrap_provider_retire(pid_t, const char *, int);
1730Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *);
1740Sstevel@tonic-gate 
175532Sahl static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
176532Sahl static void fasttrap_proc_release(fasttrap_proc_t *);
177532Sahl 
1780Sstevel@tonic-gate #define	FASTTRAP_PROVS_INDEX(pid, name) \
1790Sstevel@tonic-gate 	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
1800Sstevel@tonic-gate 
181532Sahl #define	FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
182532Sahl 
1830Sstevel@tonic-gate static int
1840Sstevel@tonic-gate fasttrap_highbit(ulong_t i)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	int h = 1;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (i == 0)
1890Sstevel@tonic-gate 		return (0);
1900Sstevel@tonic-gate #ifdef _LP64
1910Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
1920Sstevel@tonic-gate 		h += 32; i >>= 32;
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate 	if (i & 0xffff0000) {
1960Sstevel@tonic-gate 		h += 16; i >>= 16;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	if (i & 0xff00) {
1990Sstevel@tonic-gate 		h += 8; i >>= 8;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	if (i & 0xf0) {
2020Sstevel@tonic-gate 		h += 4; i >>= 4;
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	if (i & 0xc) {
2050Sstevel@tonic-gate 		h += 2; i >>= 2;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	if (i & 0x2) {
2080Sstevel@tonic-gate 		h += 1;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	return (h);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static uint_t
2140Sstevel@tonic-gate fasttrap_hash_str(const char *p)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	unsigned int g;
2170Sstevel@tonic-gate 	uint_t hval = 0;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	while (*p) {
2200Sstevel@tonic-gate 		hval = (hval << 4) + *p++;
2210Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
2220Sstevel@tonic-gate 			hval ^= g >> 24;
2230Sstevel@tonic-gate 		hval &= ~g;
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	return (hval);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate void
2290Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGTRAP;
2340Sstevel@tonic-gate 	sqp->sq_info.si_code = TRAP_DTRACE;
2350Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)pc;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2380Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
2390Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	if (t != NULL)
2420Sstevel@tonic-gate 		aston(t);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * This function ensures that no threads are actively using the memory
2470Sstevel@tonic-gate  * associated with probes that were formerly live.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate static void
2500Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate 	int i;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (gen < fasttrap_mod_gen)
2550Sstevel@tonic-gate 		return;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	fasttrap_mod_gen++;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
2600Sstevel@tonic-gate 		mutex_enter(&cpu_core[i].cpuc_pid_lock);
2610Sstevel@tonic-gate 		mutex_exit(&cpu_core[i].cpuc_pid_lock);
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate  * This is the timeout's callback for cleaning up the providers and their
2670Sstevel@tonic-gate  * probes.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate /*ARGSUSED*/
2700Sstevel@tonic-gate static void
2710Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	fasttrap_provider_t **fpp, *fp;
2740Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
2750Sstevel@tonic-gate 	dtrace_provider_id_t provid;
2760Sstevel@tonic-gate 	int i, later;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	static volatile int in = 0;
2790Sstevel@tonic-gate 	ASSERT(in == 0);
2800Sstevel@tonic-gate 	in = 1;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
2830Sstevel@tonic-gate 	while (fasttrap_cleanup_work) {
2840Sstevel@tonic-gate 		fasttrap_cleanup_work = 0;
2850Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		later = 0;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * Iterate over all the providers trying to remove the marked
291532Sahl 		 * ones. If a provider is marked but not retired, we just
2920Sstevel@tonic-gate 		 * have to take a crack at removing it -- it's no big deal if
2930Sstevel@tonic-gate 		 * we can't.
2940Sstevel@tonic-gate 		 */
2950Sstevel@tonic-gate 		for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2960Sstevel@tonic-gate 			bucket = &fasttrap_provs.fth_table[i];
2970Sstevel@tonic-gate 			mutex_enter(&bucket->ftb_mtx);
2980Sstevel@tonic-gate 			fpp = (fasttrap_provider_t **)&bucket->ftb_data;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 			while ((fp = *fpp) != NULL) {
3010Sstevel@tonic-gate 				if (!fp->ftp_marked) {
3020Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3030Sstevel@tonic-gate 					continue;
3040Sstevel@tonic-gate 				}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 				mutex_enter(&fp->ftp_mtx);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 				/*
3092179Sahl 				 * If this provider has consumers actively
3102179Sahl 				 * creating probes (ftp_ccount) or is a USDT
3112179Sahl 				 * provider (ftp_mcount), we can't unregister
3122179Sahl 				 * or even condense.
3130Sstevel@tonic-gate 				 */
3141880Sahl 				if (fp->ftp_ccount != 0 ||
3151880Sahl 				    fp->ftp_mcount != 0) {
3160Sstevel@tonic-gate 					mutex_exit(&fp->ftp_mtx);
3170Sstevel@tonic-gate 					fp->ftp_marked = 0;
3180Sstevel@tonic-gate 					continue;
3190Sstevel@tonic-gate 				}
3200Sstevel@tonic-gate 
321532Sahl 				if (!fp->ftp_retired || fp->ftp_rcount != 0)
3220Sstevel@tonic-gate 					fp->ftp_marked = 0;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 				mutex_exit(&fp->ftp_mtx);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 				/*
3270Sstevel@tonic-gate 				 * If we successfully unregister this
3280Sstevel@tonic-gate 				 * provider we can remove it from the hash
3290Sstevel@tonic-gate 				 * chain and free the memory. If our attempt
330532Sahl 				 * to unregister fails and this is a retired
3310Sstevel@tonic-gate 				 * provider, increment our flag to try again
3320Sstevel@tonic-gate 				 * pretty soon. If we've consumed more than
3330Sstevel@tonic-gate 				 * half of our total permitted number of
3340Sstevel@tonic-gate 				 * probes call dtrace_condense() to try to
3350Sstevel@tonic-gate 				 * clean out the unenabled probes.
3360Sstevel@tonic-gate 				 */
3370Sstevel@tonic-gate 				provid = fp->ftp_provid;
3380Sstevel@tonic-gate 				if (dtrace_unregister(provid) != 0) {
3390Sstevel@tonic-gate 					if (fasttrap_total > fasttrap_max / 2)
3400Sstevel@tonic-gate 						(void) dtrace_condense(provid);
3410Sstevel@tonic-gate 					later += fp->ftp_marked;
3420Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3430Sstevel@tonic-gate 				} else {
3440Sstevel@tonic-gate 					*fpp = fp->ftp_next;
3450Sstevel@tonic-gate 					fasttrap_provider_free(fp);
3460Sstevel@tonic-gate 				}
3470Sstevel@tonic-gate 			}
3480Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
3490Sstevel@tonic-gate 		}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	ASSERT(fasttrap_timeout != 0);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
357532Sahl 	 * If we were unable to remove a retired provider, try again after
3580Sstevel@tonic-gate 	 * a second. This situation can occur in certain circumstances where
3590Sstevel@tonic-gate 	 * providers cannot be unregistered even though they have no probes
3600Sstevel@tonic-gate 	 * enabled because of an execution of dtrace -l or something similar.
3610Sstevel@tonic-gate 	 * If the timeout has been disabled (set to 1 because we're trying
3620Sstevel@tonic-gate 	 * to detach), we set fasttrap_cleanup_work to ensure that we'll
3630Sstevel@tonic-gate 	 * get a chance to do that work if and when the timeout is reenabled
3640Sstevel@tonic-gate 	 * (if detach fails).
3650Sstevel@tonic-gate 	 */
3660Sstevel@tonic-gate 	if (later > 0 && fasttrap_timeout != (timeout_id_t)1)
3670Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz);
3680Sstevel@tonic-gate 	else if (later > 0)
3690Sstevel@tonic-gate 		fasttrap_cleanup_work = 1;
3700Sstevel@tonic-gate 	else
3710Sstevel@tonic-gate 		fasttrap_timeout = 0;
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3740Sstevel@tonic-gate 	in = 0;
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate /*
3780Sstevel@tonic-gate  * Activates the asynchronous cleanup mechanism.
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate static void
3810Sstevel@tonic-gate fasttrap_pid_cleanup(void)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
3840Sstevel@tonic-gate 	fasttrap_cleanup_work = 1;
3850Sstevel@tonic-gate 	if (fasttrap_timeout == 0)
3860Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1);
3870Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * This is called from cfork() via dtrace_fasttrap_fork(). The child
3926390Sahl  * process's address space is (roughly) a copy of the parent process's so
3930Sstevel@tonic-gate  * we have to remove all the instrumentation we had previously enabled in the
3940Sstevel@tonic-gate  * parent.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate static void
3970Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	pid_t ppid = p->p_pid;
4000Sstevel@tonic-gate 	int i;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	ASSERT(curproc == p);
4030Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
4040Sstevel@tonic-gate 	ASSERT(p->p_dtrace_count > 0);
4050Sstevel@tonic-gate 	ASSERT(cp->p_dtrace_count == 0);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * This would be simpler and faster if we maintained per-process
4090Sstevel@tonic-gate 	 * hash tables of enabled tracepoints. It could, however, potentially
4100Sstevel@tonic-gate 	 * slow down execution of a tracepoint since we'd need to go
4110Sstevel@tonic-gate 	 * through two levels of indirection. In the future, we should
4120Sstevel@tonic-gate 	 * consider either maintaining per-process ancillary lists of
4130Sstevel@tonic-gate 	 * enabled tracepoints or hanging a pointer to a per-process hash
4140Sstevel@tonic-gate 	 * table of enabled tracepoints off the proc structure.
4150Sstevel@tonic-gate 	 */
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * We don't have to worry about the child process disappearing
4190Sstevel@tonic-gate 	 * because we're in fork().
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4220Sstevel@tonic-gate 	sprlock_proc(cp);
4230Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * Iterate over every tracepoint looking for ones that belong to the
4270Sstevel@tonic-gate 	 * parent process, and remove each from the child process.
4280Sstevel@tonic-gate 	 */
4290Sstevel@tonic-gate 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
4300Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
4310Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
4340Sstevel@tonic-gate 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
435532Sahl 			if (tp->ftt_pid == ppid &&
4364821Sahl 			    tp->ftt_proc->ftpc_acount != 0) {
4370Sstevel@tonic-gate 				int ret = fasttrap_tracepoint_remove(cp, tp);
4380Sstevel@tonic-gate 				ASSERT(ret == 0);
4396390Sahl 
4406390Sahl 				/*
4416390Sahl 				 * The count of active providers can only be
4426390Sahl 				 * decremented (i.e. to zero) during exec,
4436390Sahl 				 * exit, and removal of a meta provider so it
4446390Sahl 				 * should be impossible to drop the count
4456390Sahl 				 * mid-fork.
4466390Sahl 				 */
4476390Sahl 				ASSERT(tp->ftt_proc->ftpc_acount != 0);
4480Sstevel@tonic-gate 			}
4490Sstevel@tonic-gate 		}
4500Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4540Sstevel@tonic-gate 	sprunlock(cp);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * This is called from proc_exit() or from exec_common() if p_dtrace_probes
4590Sstevel@tonic-gate  * is set on the proc structure to indicate that there is a pid provider
4600Sstevel@tonic-gate  * associated with this process.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate static void
4630Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	ASSERT(p == curproc);
4660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * We clean up the pid provider for this process here; user-land
4720Sstevel@tonic-gate 	 * static probes are handled by the meta-provider remove entry point.
4730Sstevel@tonic-gate 	 */
474935Sahl 	fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4770Sstevel@tonic-gate }
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*ARGSUSED*/
4810Sstevel@tonic-gate static void
4820Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	/*
4850Sstevel@tonic-gate 	 * There are no "default" pid probes.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate static int
4900Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
4910Sstevel@tonic-gate {
4920Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, *new_tp = NULL;
4930Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
4940Sstevel@tonic-gate 	fasttrap_id_t *id;
4950Sstevel@tonic-gate 	pid_t pid;
4960Sstevel@tonic-gate 	uintptr_t pc;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	pid = probe->ftp_pid;
5010Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
5020Sstevel@tonic-gate 	id = &probe->ftp_tps[index].fit_id;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * Before we make any modifications, make sure we've imposed a barrier
5100Sstevel@tonic-gate 	 * on the generation in which this probe was last modified.
5110Sstevel@tonic-gate 	 */
5120Sstevel@tonic-gate 	fasttrap_mod_barrier(probe->ftp_gen);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * If the tracepoint has already been enabled, just add our id to the
5180Sstevel@tonic-gate 	 * list of interested probes. This may be our second time through
5190Sstevel@tonic-gate 	 * this path in which case we'll have constructed the tracepoint we'd
5200Sstevel@tonic-gate 	 * like to install. If we can't find a match, and have an allocated
5210Sstevel@tonic-gate 	 * tracepoint ready to go, enable that one now.
5220Sstevel@tonic-gate 	 *
5231710Sahl 	 * A tracepoint whose process is defunct is also considered defunct.
5240Sstevel@tonic-gate 	 */
5250Sstevel@tonic-gate again:
5260Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
5270Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
5286390Sahl 		/*
5296390Sahl 		 * Note that it's safe to access the active count on the
5306390Sahl 		 * associated proc structure because we know that at least one
5316390Sahl 		 * provider (this one) will still be around throughout this
5326390Sahl 		 * operation.
5336390Sahl 		 */
5340Sstevel@tonic-gate 		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
5354821Sahl 		    tp->ftt_proc->ftpc_acount == 0)
5360Sstevel@tonic-gate 			continue;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		/*
5390Sstevel@tonic-gate 		 * Now that we've found a matching tracepoint, it would be
5400Sstevel@tonic-gate 		 * a decent idea to confirm that the tracepoint is still
5410Sstevel@tonic-gate 		 * enabled and the trap instruction hasn't been overwritten.
5420Sstevel@tonic-gate 		 * Since this is a little hairy, we'll punt for now.
5430Sstevel@tonic-gate 		 */
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		/*
5460Sstevel@tonic-gate 		 * This can't be the first interested probe. We don't have
5470Sstevel@tonic-gate 		 * to worry about another thread being in the midst of
5480Sstevel@tonic-gate 		 * deleting this tracepoint (which would be the only valid
5490Sstevel@tonic-gate 		 * reason for a tracepoint to have no interested probes)
5500Sstevel@tonic-gate 		 * since we're holding P_PR_LOCK for this process.
5510Sstevel@tonic-gate 		 */
5520Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
5530Sstevel@tonic-gate 
5541710Sahl 		switch (id->fti_ptype) {
5551710Sahl 		case DTFTP_ENTRY:
5561710Sahl 		case DTFTP_OFFSETS:
5571710Sahl 		case DTFTP_IS_ENABLED:
5581710Sahl 			id->fti_next = tp->ftt_ids;
5591710Sahl 			membar_producer();
5601710Sahl 			tp->ftt_ids = id;
5611710Sahl 			membar_producer();
5621710Sahl 			break;
5631710Sahl 
5641710Sahl 		case DTFTP_RETURN:
5651710Sahl 		case DTFTP_POST_OFFSETS:
5660Sstevel@tonic-gate 			id->fti_next = tp->ftt_retids;
5670Sstevel@tonic-gate 			membar_producer();
5680Sstevel@tonic-gate 			tp->ftt_retids = id;
5690Sstevel@tonic-gate 			membar_producer();
5701710Sahl 			break;
5711710Sahl 
5721710Sahl 		default:
5731710Sahl 			ASSERT(0);
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		if (new_tp != NULL) {
5790Sstevel@tonic-gate 			new_tp->ftt_ids = NULL;
5800Sstevel@tonic-gate 			new_tp->ftt_retids = NULL;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		return (0);
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * If we have a good tracepoint ready to go, install it now while
5880Sstevel@tonic-gate 	 * we have the lock held and no one can screw with us.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	if (new_tp != NULL) {
591315Sahl 		int rc = 0;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		new_tp->ftt_next = bucket->ftb_data;
5940Sstevel@tonic-gate 		membar_producer();
5950Sstevel@tonic-gate 		bucket->ftb_data = new_tp;
5960Sstevel@tonic-gate 		membar_producer();
5970Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		/*
600315Sahl 		 * Activate the tracepoint in the ISA-specific manner.
601315Sahl 		 * If this fails, we need to report the failure, but
602315Sahl 		 * indicate that this tracepoint must still be disabled
603315Sahl 		 * by calling fasttrap_tracepoint_disable().
6040Sstevel@tonic-gate 		 */
605315Sahl 		if (fasttrap_tracepoint_install(p, new_tp) != 0)
606315Sahl 			rc = FASTTRAP_ENABLE_PARTIAL;
607315Sahl 
608315Sahl 		/*
609315Sahl 		 * Increment the count of the number of tracepoints active in
610315Sahl 		 * the victim process.
611315Sahl 		 */
612315Sahl 		ASSERT(p->p_proc_flag & P_PR_LOCK);
613315Sahl 		p->p_dtrace_count++;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		return (rc);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Initialize the tracepoint that's been preallocated with the probe.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	new_tp = probe->ftp_tps[index].fit_tp;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pid == pid);
6260Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pc == pc);
627532Sahl 	ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
6280Sstevel@tonic-gate 	ASSERT(new_tp->ftt_ids == NULL);
6290Sstevel@tonic-gate 	ASSERT(new_tp->ftt_retids == NULL);
6300Sstevel@tonic-gate 
6311710Sahl 	switch (id->fti_ptype) {
6321710Sahl 	case DTFTP_ENTRY:
6331710Sahl 	case DTFTP_OFFSETS:
6341710Sahl 	case DTFTP_IS_ENABLED:
6351710Sahl 		id->fti_next = NULL;
6361710Sahl 		new_tp->ftt_ids = id;
6371710Sahl 		break;
6381710Sahl 
6391710Sahl 	case DTFTP_RETURN:
6401710Sahl 	case DTFTP_POST_OFFSETS:
6410Sstevel@tonic-gate 		id->fti_next = NULL;
6420Sstevel@tonic-gate 		new_tp->ftt_retids = id;
6431710Sahl 		break;
6441710Sahl 
6451710Sahl 	default:
6461710Sahl 		ASSERT(0);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/*
650315Sahl 	 * If the ISA-dependent initialization goes to plan, go back to the
6510Sstevel@tonic-gate 	 * beginning and try to install this freshly made tracepoint.
6520Sstevel@tonic-gate 	 */
6531710Sahl 	if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
6540Sstevel@tonic-gate 		goto again;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	new_tp->ftt_ids = NULL;
6570Sstevel@tonic-gate 	new_tp->ftt_retids = NULL;
6580Sstevel@tonic-gate 
659315Sahl 	return (FASTTRAP_ENABLE_FAIL);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate static void
6630Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
6660Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
6670Sstevel@tonic-gate 	fasttrap_tracepoint_t **pp, *tp;
6680Sstevel@tonic-gate 	fasttrap_id_t *id, **idp;
6690Sstevel@tonic-gate 	pid_t pid;
6700Sstevel@tonic-gate 	uintptr_t pc;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	pid = probe->ftp_pid;
6750Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
6761710Sahl 	id = &probe->ftp_tps[index].fit_id;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * Find the tracepoint and make sure that our id is one of the
6820Sstevel@tonic-gate 	 * ones registered with it.
6830Sstevel@tonic-gate 	 */
6840Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6850Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
6860Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6870Sstevel@tonic-gate 		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
688532Sahl 		    tp->ftt_proc == provider->ftp_proc)
6890Sstevel@tonic-gate 			break;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * If we somehow lost this tracepoint, we're in a world of hurt.
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	ASSERT(tp != NULL);
6960Sstevel@tonic-gate 
6971710Sahl 	switch (id->fti_ptype) {
6981710Sahl 	case DTFTP_ENTRY:
6991710Sahl 	case DTFTP_OFFSETS:
7001710Sahl 	case DTFTP_IS_ENABLED:
7011710Sahl 		ASSERT(tp->ftt_ids != NULL);
7021710Sahl 		idp = &tp->ftt_ids;
7031710Sahl 		break;
7041710Sahl 
7051710Sahl 	case DTFTP_RETURN:
7061710Sahl 	case DTFTP_POST_OFFSETS:
7070Sstevel@tonic-gate 		ASSERT(tp->ftt_retids != NULL);
7080Sstevel@tonic-gate 		idp = &tp->ftt_retids;
7091710Sahl 		break;
7101710Sahl 
7111710Sahl 	default:
7121710Sahl 		ASSERT(0);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	while ((*idp)->fti_probe != probe) {
7160Sstevel@tonic-gate 		idp = &(*idp)->fti_next;
7170Sstevel@tonic-gate 		ASSERT(*idp != NULL);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	id = *idp;
7210Sstevel@tonic-gate 	*idp = id->fti_next;
7220Sstevel@tonic-gate 	membar_producer();
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	ASSERT(id->fti_probe == probe);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * If there are other registered enablings of this tracepoint, we're
7280Sstevel@tonic-gate 	 * all done, but if this was the last probe assocated with this
7290Sstevel@tonic-gate 	 * this tracepoint, we need to remove and free it.
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		/*
7340Sstevel@tonic-gate 		 * If the current probe's tracepoint is in use, swap it
7350Sstevel@tonic-gate 		 * for an unused tracepoint.
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 		if (tp == probe->ftp_tps[index].fit_tp) {
7380Sstevel@tonic-gate 			fasttrap_probe_t *tmp_probe;
7390Sstevel@tonic-gate 			fasttrap_tracepoint_t **tmp_tp;
7400Sstevel@tonic-gate 			uint_t tmp_index;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 			if (tp->ftt_ids != NULL) {
7430Sstevel@tonic-gate 				tmp_probe = tp->ftt_ids->fti_probe;
7443944Sahl 				/* LINTED - alignment */
7450Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
7460Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7470Sstevel@tonic-gate 			} else {
7480Sstevel@tonic-gate 				tmp_probe = tp->ftt_retids->fti_probe;
7493944Sahl 				/* LINTED - alignment */
7500Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
7510Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 			ASSERT(*tmp_tp != NULL);
7550Sstevel@tonic-gate 			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
7560Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_ids == NULL);
7570Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_retids == NULL);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 			probe->ftp_tps[index].fit_tp = *tmp_tp;
7600Sstevel@tonic-gate 			*tmp_tp = tp;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 		/*
7660Sstevel@tonic-gate 		 * Tag the modified probe with the generation in which it was
7670Sstevel@tonic-gate 		 * changed.
7680Sstevel@tonic-gate 		 */
7690Sstevel@tonic-gate 		probe->ftp_gen = fasttrap_mod_gen;
7700Sstevel@tonic-gate 		return;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * We can't safely remove the tracepoint from the set of active
7770Sstevel@tonic-gate 	 * tracepoints until we've actually removed the fasttrap instruction
7780Sstevel@tonic-gate 	 * from the process's text. We can, however, operate on this
7790Sstevel@tonic-gate 	 * tracepoint secure in the knowledge that no other thread is going to
7800Sstevel@tonic-gate 	 * be looking at it since we hold P_PR_LOCK on the process if it's
7810Sstevel@tonic-gate 	 * live or we hold the provider lock on the process if it's dead and
7820Sstevel@tonic-gate 	 * gone.
7830Sstevel@tonic-gate 	 */
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * We only need to remove the actual instruction if we're looking
7870Sstevel@tonic-gate 	 * at an existing process
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	if (p != NULL) {
7900Sstevel@tonic-gate 		/*
7910Sstevel@tonic-gate 		 * If we fail to restore the instruction we need to kill
7920Sstevel@tonic-gate 		 * this process since it's in a completely unrecoverable
7930Sstevel@tonic-gate 		 * state.
7940Sstevel@tonic-gate 		 */
7950Sstevel@tonic-gate 		if (fasttrap_tracepoint_remove(p, tp) != 0)
7960Sstevel@tonic-gate 			fasttrap_sigtrap(p, NULL, pc);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 		/*
7990Sstevel@tonic-gate 		 * Decrement the count of the number of tracepoints active
8000Sstevel@tonic-gate 		 * in the victim process.
8010Sstevel@tonic-gate 		 */
8020Sstevel@tonic-gate 		ASSERT(p->p_proc_flag & P_PR_LOCK);
8030Sstevel@tonic-gate 		p->p_dtrace_count--;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * Remove the probe from the hash table of active tracepoints.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
8100Sstevel@tonic-gate 	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
8110Sstevel@tonic-gate 	ASSERT(*pp != NULL);
8120Sstevel@tonic-gate 	while (*pp != tp) {
8130Sstevel@tonic-gate 		pp = &(*pp)->ftt_next;
8140Sstevel@tonic-gate 		ASSERT(*pp != NULL);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	*pp = tp->ftt_next;
8180Sstevel@tonic-gate 	membar_producer();
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * Tag the modified probe with the generation in which it was changed.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	probe->ftp_gen = fasttrap_mod_gen;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate static void
8292179Sahl fasttrap_enable_callbacks(void)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * We don't have to play the rw lock game here because we're
8330Sstevel@tonic-gate 	 * providing something rather than taking something away --
8340Sstevel@tonic-gate 	 * we can be sure that no threads have tried to follow this
8350Sstevel@tonic-gate 	 * function pointer yet.
8360Sstevel@tonic-gate 	 */
8370Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
8382179Sahl 	if (fasttrap_pid_count == 0) {
8392179Sahl 		ASSERT(dtrace_pid_probe_ptr == NULL);
8402179Sahl 		ASSERT(dtrace_return_probe_ptr == NULL);
8412179Sahl 		dtrace_pid_probe_ptr = &fasttrap_pid_probe;
8422179Sahl 		dtrace_return_probe_ptr = &fasttrap_return_probe;
8430Sstevel@tonic-gate 	}
8442179Sahl 	ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
8452179Sahl 	ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
8462179Sahl 	fasttrap_pid_count++;
8470Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate static void
8512179Sahl fasttrap_disable_callbacks(void)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
8562179Sahl 	ASSERT(fasttrap_pid_count > 0);
8572179Sahl 	fasttrap_pid_count--;
8582179Sahl 	if (fasttrap_pid_count == 0) {
8590Sstevel@tonic-gate 		cpu_t *cur, *cpu = CPU;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8624568Sahl 		    cur = cur->cpu_next_onln) {
8630Sstevel@tonic-gate 			rw_enter(&cur->cpu_ft_lock, RW_WRITER);
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 
8662179Sahl 		dtrace_pid_probe_ptr = NULL;
8672179Sahl 		dtrace_return_probe_ptr = NULL;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8704568Sahl 		    cur = cur->cpu_next_onln) {
8710Sstevel@tonic-gate 			rw_exit(&cur->cpu_ft_lock);
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8750Sstevel@tonic-gate }
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate /*ARGSUSED*/
8780Sstevel@tonic-gate static void
8790Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
8820Sstevel@tonic-gate 	proc_t *p;
883315Sahl 	int i, rc;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	ASSERT(probe != NULL);
8860Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
8870Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
8880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	/*
8910Sstevel@tonic-gate 	 * Increment the count of enabled probes on this probe's provider;
8920Sstevel@tonic-gate 	 * the provider can't go away while the probe still exists. We
8930Sstevel@tonic-gate 	 * must increment this even if we aren't able to properly enable
8940Sstevel@tonic-gate 	 * this probe.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	mutex_enter(&probe->ftp_prov->ftp_mtx);
8970Sstevel@tonic-gate 	probe->ftp_prov->ftp_rcount++;
8980Sstevel@tonic-gate 	mutex_exit(&probe->ftp_prov->ftp_mtx);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/*
9011880Sahl 	 * If this probe's provider is retired (meaning it was valid in a
9021880Sahl 	 * previously exec'ed incarnation of this address space), bail out. The
9031880Sahl 	 * provider can't go away while we're in this code path.
9041880Sahl 	 */
9051880Sahl 	if (probe->ftp_prov->ftp_retired)
9061880Sahl 		return;
9071880Sahl 
9081880Sahl 	/*
9091880Sahl 	 * If we can't find the process, it may be that we're in the context of
9101880Sahl 	 * a fork in which the traced process is being born and we're copying
9111880Sahl 	 * USDT probes. Otherwise, the process is gone so bail.
9120Sstevel@tonic-gate 	 */
9131880Sahl 	if ((p = sprlock(probe->ftp_pid)) == NULL) {
9141880Sahl 		if ((curproc->p_flag & SFORKING) == 0)
9151880Sahl 			return;
9161880Sahl 
9171880Sahl 		mutex_enter(&pidlock);
9181880Sahl 		p = prfind(probe->ftp_pid);
9191880Sahl 
9201880Sahl 		/*
9211880Sahl 		 * Confirm that curproc is indeed forking the process in which
9221880Sahl 		 * we're trying to enable probes.
9231880Sahl 		 */
9241880Sahl 		ASSERT(p != NULL);
9251880Sahl 		ASSERT(p->p_parent == curproc);
9261880Sahl 		ASSERT(p->p_stat == SIDL);
9271880Sahl 
9281880Sahl 		mutex_enter(&p->p_lock);
9291880Sahl 		mutex_exit(&pidlock);
9301880Sahl 
9311880Sahl 		sprlock_proc(p);
9321880Sahl 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
9350Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	/*
9382179Sahl 	 * We have to enable the trap entry point before any user threads have
9390Sstevel@tonic-gate 	 * the chance to execute the trap instruction we're about to place
9400Sstevel@tonic-gate 	 * in their process's text.
9410Sstevel@tonic-gate 	 */
9422179Sahl 	fasttrap_enable_callbacks();
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	/*
9450Sstevel@tonic-gate 	 * Enable all the tracepoints and add this probe's id to each
9460Sstevel@tonic-gate 	 * tracepoint's list of active probes.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
949315Sahl 		if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
950315Sahl 			/*
951315Sahl 			 * If enabling the tracepoint failed completely,
952315Sahl 			 * we don't have to disable it; if the failure
953315Sahl 			 * was only partial we must disable it.
954315Sahl 			 */
955315Sahl 			if (rc == FASTTRAP_ENABLE_FAIL)
956315Sahl 				i--;
957315Sahl 			else
958315Sahl 				ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
959315Sahl 
9600Sstevel@tonic-gate 			/*
9610Sstevel@tonic-gate 			 * Back up and pull out all the tracepoints we've
9620Sstevel@tonic-gate 			 * created so far for this probe.
9630Sstevel@tonic-gate 			 */
964665Sahl 			while (i >= 0) {
9650Sstevel@tonic-gate 				fasttrap_tracepoint_disable(p, probe, i);
966665Sahl 				i--;
9670Sstevel@tonic-gate 			}
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9700Sstevel@tonic-gate 			sprunlock(p);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 			/*
9730Sstevel@tonic-gate 			 * Since we're not actually enabling this probe,
9740Sstevel@tonic-gate 			 * drop our reference on the trap table entry.
9750Sstevel@tonic-gate 			 */
9762179Sahl 			fasttrap_disable_callbacks();
9770Sstevel@tonic-gate 			return;
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 	}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
9820Sstevel@tonic-gate 	sprunlock(p);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	probe->ftp_enabled = 1;
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate /*ARGSUSED*/
9880Sstevel@tonic-gate static void
9890Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
9920Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
9930Sstevel@tonic-gate 	proc_t *p;
9940Sstevel@tonic-gate 	int i, whack = 0;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * We won't be able to acquire a /proc-esque lock on the process
10000Sstevel@tonic-gate 	 * iff the process is dead and gone. In this case, we rely on the
10010Sstevel@tonic-gate 	 * provider lock as a point of mutual exclusion to prevent other
10020Sstevel@tonic-gate 	 * DTrace consumers from disabling this probe.
10030Sstevel@tonic-gate 	 */
10040Sstevel@tonic-gate 	if ((p = sprlock(probe->ftp_pid)) != NULL) {
10050Sstevel@tonic-gate 		ASSERT(!(p->p_flag & SVFORK));
10060Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/*
10123944Sahl 	 * Disable all the associated tracepoints (for fully enabled probes).
10130Sstevel@tonic-gate 	 */
10143944Sahl 	if (probe->ftp_enabled) {
10153944Sahl 		for (i = 0; i < probe->ftp_ntps; i++) {
10163944Sahl 			fasttrap_tracepoint_disable(p, probe, i);
10173944Sahl 		}
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount > 0);
10210Sstevel@tonic-gate 	provider->ftp_rcount--;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if (p != NULL) {
10240Sstevel@tonic-gate 		/*
10250Sstevel@tonic-gate 		 * Even though we may not be able to remove it entirely, we
1026532Sahl 		 * mark this retired provider to get a chance to remove some
10270Sstevel@tonic-gate 		 * of the associated probes.
10280Sstevel@tonic-gate 		 */
1029532Sahl 		if (provider->ftp_retired && !provider->ftp_marked)
10300Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10310Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
10340Sstevel@tonic-gate 		sprunlock(p);
10350Sstevel@tonic-gate 	} else {
10360Sstevel@tonic-gate 		/*
10370Sstevel@tonic-gate 		 * If the process is dead, we're just waiting for the
10380Sstevel@tonic-gate 		 * last probe to be disabled to be able to free it.
10390Sstevel@tonic-gate 		 */
10400Sstevel@tonic-gate 		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
10410Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10420Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	if (whack)
10460Sstevel@tonic-gate 		fasttrap_pid_cleanup();
10470Sstevel@tonic-gate 
10483944Sahl 	if (!probe->ftp_enabled)
10493944Sahl 		return;
10503944Sahl 
10510Sstevel@tonic-gate 	probe->ftp_enabled = 0;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
10542179Sahl 	fasttrap_disable_callbacks();
10550Sstevel@tonic-gate }
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate /*ARGSUSED*/
10580Sstevel@tonic-gate static void
10590Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
10600Sstevel@tonic-gate     dtrace_argdesc_t *desc)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
10630Sstevel@tonic-gate 	char *str;
10644568Sahl 	int i, ndx;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	desc->dtargd_native[0] = '\0';
10670Sstevel@tonic-gate 	desc->dtargd_xlate[0] = '\0';
10680Sstevel@tonic-gate 
1069532Sahl 	if (probe->ftp_prov->ftp_retired != 0 ||
10700Sstevel@tonic-gate 	    desc->dtargd_ndx >= probe->ftp_nargs) {
10710Sstevel@tonic-gate 		desc->dtargd_ndx = DTRACE_ARGNONE;
10720Sstevel@tonic-gate 		return;
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10754568Sahl 	ndx = (probe->ftp_argmap != NULL) ?
10764568Sahl 	    probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	str = probe->ftp_ntypes;
10794568Sahl 	for (i = 0; i < ndx; i++) {
10800Sstevel@tonic-gate 		str += strlen(str) + 1;
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
10840Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_native, str);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	if (probe->ftp_xtypes == NULL)
10870Sstevel@tonic-gate 		return;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	str = probe->ftp_xtypes;
10900Sstevel@tonic-gate 	for (i = 0; i < desc->dtargd_ndx; i++) {
10910Sstevel@tonic-gate 		str += strlen(str) + 1;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
10950Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_xlate, str);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*ARGSUSED*/
10990Sstevel@tonic-gate static void
11000Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
11030Sstevel@tonic-gate 	int i;
11040Sstevel@tonic-gate 	size_t size;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	ASSERT(probe != NULL);
11070Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
11080Sstevel@tonic-gate 	ASSERT(fasttrap_total >= probe->ftp_ntps);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
11111880Sahl 	size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
11140Sstevel@tonic-gate 		fasttrap_mod_barrier(probe->ftp_gen);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
11170Sstevel@tonic-gate 		kmem_free(probe->ftp_tps[i].fit_tp,
11180Sstevel@tonic-gate 		    sizeof (fasttrap_tracepoint_t));
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	kmem_free(probe, size);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = {
11260Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11270Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11280Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11290Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11300Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11310Sstevel@tonic-gate };
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate static dtrace_pops_t pid_pops = {
11340Sstevel@tonic-gate 	fasttrap_pid_provide,
11350Sstevel@tonic-gate 	NULL,
11360Sstevel@tonic-gate 	fasttrap_pid_enable,
11370Sstevel@tonic-gate 	fasttrap_pid_disable,
11380Sstevel@tonic-gate 	NULL,
11390Sstevel@tonic-gate 	NULL,
11400Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
11412179Sahl 	fasttrap_pid_getarg,
11420Sstevel@tonic-gate 	NULL,
11430Sstevel@tonic-gate 	fasttrap_pid_destroy
11440Sstevel@tonic-gate };
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate static dtrace_pops_t usdt_pops = {
11470Sstevel@tonic-gate 	fasttrap_pid_provide,
11480Sstevel@tonic-gate 	NULL,
11490Sstevel@tonic-gate 	fasttrap_pid_enable,
11500Sstevel@tonic-gate 	fasttrap_pid_disable,
11510Sstevel@tonic-gate 	NULL,
11520Sstevel@tonic-gate 	NULL,
11530Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
11540Sstevel@tonic-gate 	fasttrap_usdt_getarg,
11550Sstevel@tonic-gate 	NULL,
11560Sstevel@tonic-gate 	fasttrap_pid_destroy
11570Sstevel@tonic-gate };
11580Sstevel@tonic-gate 
1159532Sahl static fasttrap_proc_t *
1160532Sahl fasttrap_proc_lookup(pid_t pid)
1161532Sahl {
1162532Sahl 	fasttrap_bucket_t *bucket;
1163532Sahl 	fasttrap_proc_t *fprc, *new_fprc;
1164532Sahl 
1165532Sahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1166532Sahl 	mutex_enter(&bucket->ftb_mtx);
1167532Sahl 
1168532Sahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
11694821Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1170532Sahl 			mutex_enter(&fprc->ftpc_mtx);
1171532Sahl 			mutex_exit(&bucket->ftb_mtx);
11724821Sahl 			fprc->ftpc_rcount++;
11734821Sahl 			atomic_add_64(&fprc->ftpc_acount, 1);
11746390Sahl 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1175532Sahl 			mutex_exit(&fprc->ftpc_mtx);
1176532Sahl 
1177532Sahl 			return (fprc);
1178532Sahl 		}
1179532Sahl 	}
1180532Sahl 
1181532Sahl 	/*
1182532Sahl 	 * Drop the bucket lock so we don't try to perform a sleeping
1183532Sahl 	 * allocation under it.
1184532Sahl 	 */
1185532Sahl 	mutex_exit(&bucket->ftb_mtx);
1186532Sahl 
1187532Sahl 	new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1188532Sahl 	new_fprc->ftpc_pid = pid;
11894821Sahl 	new_fprc->ftpc_rcount = 1;
11904821Sahl 	new_fprc->ftpc_acount = 1;
1191532Sahl 
1192532Sahl 	mutex_enter(&bucket->ftb_mtx);
1193532Sahl 
1194532Sahl 	/*
1195532Sahl 	 * Take another lap through the list to make sure a proc hasn't
1196532Sahl 	 * been created for this pid while we weren't under the bucket lock.
1197532Sahl 	 */
1198532Sahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
11994821Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1200532Sahl 			mutex_enter(&fprc->ftpc_mtx);
1201532Sahl 			mutex_exit(&bucket->ftb_mtx);
12024821Sahl 			fprc->ftpc_rcount++;
12034821Sahl 			atomic_add_64(&fprc->ftpc_acount, 1);
12046390Sahl 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1205532Sahl 			mutex_exit(&fprc->ftpc_mtx);
1206532Sahl 
1207532Sahl 			kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1208532Sahl 
1209532Sahl 			return (fprc);
1210532Sahl 		}
1211532Sahl 	}
1212532Sahl 
1213532Sahl 	new_fprc->ftpc_next = bucket->ftb_data;
1214532Sahl 	bucket->ftb_data = new_fprc;
1215532Sahl 
1216532Sahl 	mutex_exit(&bucket->ftb_mtx);
1217532Sahl 
1218532Sahl 	return (new_fprc);
1219532Sahl }
1220532Sahl 
1221532Sahl static void
1222532Sahl fasttrap_proc_release(fasttrap_proc_t *proc)
1223532Sahl {
1224532Sahl 	fasttrap_bucket_t *bucket;
1225532Sahl 	fasttrap_proc_t *fprc, **fprcp;
1226532Sahl 	pid_t pid = proc->ftpc_pid;
1227532Sahl 
1228532Sahl 	mutex_enter(&proc->ftpc_mtx);
1229532Sahl 
12304821Sahl 	ASSERT(proc->ftpc_rcount != 0);
12316390Sahl 	ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1232532Sahl 
12334821Sahl 	if (--proc->ftpc_rcount != 0) {
1234532Sahl 		mutex_exit(&proc->ftpc_mtx);
1235532Sahl 		return;
1236532Sahl 	}
1237532Sahl 
1238532Sahl 	mutex_exit(&proc->ftpc_mtx);
1239532Sahl 
12404821Sahl 	/*
12414821Sahl 	 * There should definitely be no live providers associated with this
12424821Sahl 	 * process at this point.
12434821Sahl 	 */
12444821Sahl 	ASSERT(proc->ftpc_acount == 0);
12454821Sahl 
1246532Sahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1247532Sahl 	mutex_enter(&bucket->ftb_mtx);
1248532Sahl 
1249532Sahl 	fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1250532Sahl 	while ((fprc = *fprcp) != NULL) {
1251532Sahl 		if (fprc == proc)
1252532Sahl 			break;
1253532Sahl 
1254532Sahl 		fprcp = &fprc->ftpc_next;
1255532Sahl 	}
1256532Sahl 
1257532Sahl 	/*
1258532Sahl 	 * Something strange has happened if we can't find the proc.
1259532Sahl 	 */
1260532Sahl 	ASSERT(fprc != NULL);
1261532Sahl 
1262532Sahl 	*fprcp = fprc->ftpc_next;
1263532Sahl 
1264532Sahl 	mutex_exit(&bucket->ftb_mtx);
1265532Sahl 
1266532Sahl 	kmem_free(fprc, sizeof (fasttrap_proc_t));
1267532Sahl }
1268532Sahl 
12690Sstevel@tonic-gate /*
12700Sstevel@tonic-gate  * Lookup a fasttrap-managed provider based on its name and associated pid.
12710Sstevel@tonic-gate  * If the pattr argument is non-NULL, this function instantiates the provider
12720Sstevel@tonic-gate  * if it doesn't exist otherwise it returns NULL. The provider is returned
12730Sstevel@tonic-gate  * with its lock held.
12740Sstevel@tonic-gate  */
12750Sstevel@tonic-gate static fasttrap_provider_t *
12760Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name,
12770Sstevel@tonic-gate     const dtrace_pattr_t *pattr)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	fasttrap_provider_t *fp, *new_fp = NULL;
12800Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
12810Sstevel@tonic-gate 	char provname[DTRACE_PROVNAMELEN];
12820Sstevel@tonic-gate 	proc_t *p;
12831677Sdp 	cred_t *cred;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1286935Sahl 	ASSERT(pattr != NULL);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
12890Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	/*
12920Sstevel@tonic-gate 	 * Take a lap through the list and return the match if we find it.
12930Sstevel@tonic-gate 	 */
12940Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
12950Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1296532Sahl 		    !fp->ftp_retired) {
12970Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
12980Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
12990Sstevel@tonic-gate 			return (fp);
13000Sstevel@tonic-gate 		}
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * Drop the bucket lock so we don't try to perform a sleeping
13050Sstevel@tonic-gate 	 * allocation under it.
13060Sstevel@tonic-gate 	 */
13070Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
13080Sstevel@tonic-gate 
1309532Sahl 	/*
13100Sstevel@tonic-gate 	 * Make sure the process exists, isn't a child created as the result
13111677Sdp 	 * of a vfork(2), and isn't a zombie (but may be in fork).
13120Sstevel@tonic-gate 	 */
13130Sstevel@tonic-gate 	mutex_enter(&pidlock);
1314390Sraf 	if ((p = prfind(pid)) == NULL) {
13150Sstevel@tonic-gate 		mutex_exit(&pidlock);
13160Sstevel@tonic-gate 		return (NULL);
13170Sstevel@tonic-gate 	}
13180Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
13190Sstevel@tonic-gate 	mutex_exit(&pidlock);
1320390Sraf 	if (p->p_flag & (SVFORK | SEXITING)) {
1321390Sraf 		mutex_exit(&p->p_lock);
1322390Sraf 		return (NULL);
1323390Sraf 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/*
13260Sstevel@tonic-gate 	 * Increment p_dtrace_probes so that the process knows to inform us
13270Sstevel@tonic-gate 	 * when it exits or execs. fasttrap_provider_free() decrements this
13280Sstevel@tonic-gate 	 * when we're done with this provider.
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	p->p_dtrace_probes++;
13310Sstevel@tonic-gate 
13321677Sdp 	/*
13331677Sdp 	 * Grab the credentials for this process so we have
13341677Sdp 	 * something to pass to dtrace_register().
13351677Sdp 	 */
13360Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
13371677Sdp 	crhold(p->p_cred);
13381677Sdp 	cred = p->p_cred;
13390Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
13400Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1343532Sahl 	new_fp->ftp_pid = pid;
1344532Sahl 	new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1345532Sahl 
1346532Sahl 	ASSERT(new_fp->ftp_proc != NULL);
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	/*
13510Sstevel@tonic-gate 	 * Take another lap through the list to make sure a provider hasn't
13520Sstevel@tonic-gate 	 * been created for this pid while we weren't under the bucket lock.
13530Sstevel@tonic-gate 	 */
13540Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
13550Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1356532Sahl 		    !fp->ftp_retired) {
13570Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
13580Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
13590Sstevel@tonic-gate 			fasttrap_provider_free(new_fp);
13601677Sdp 			crfree(cred);
13610Sstevel@tonic-gate 			return (fp);
13620Sstevel@tonic-gate 		}
13630Sstevel@tonic-gate 	}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	(void) strcpy(new_fp->ftp_name, name);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/*
13680Sstevel@tonic-gate 	 * Fail and return NULL if either the provider name is too long
13690Sstevel@tonic-gate 	 * or we fail to register this new provider with the DTrace
13700Sstevel@tonic-gate 	 * framework. Note that this is the only place we ever construct
13710Sstevel@tonic-gate 	 * the full provider name -- we keep it in pieces in the provider
13720Sstevel@tonic-gate 	 * structure.
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
13750Sstevel@tonic-gate 	    sizeof (provname) ||
13760Sstevel@tonic-gate 	    dtrace_register(provname, pattr,
13771677Sdp 	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
13780Sstevel@tonic-gate 	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
13790Sstevel@tonic-gate 	    &new_fp->ftp_provid) != 0) {
13800Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
13810Sstevel@tonic-gate 		fasttrap_provider_free(new_fp);
13821677Sdp 		crfree(cred);
13830Sstevel@tonic-gate 		return (NULL);
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	new_fp->ftp_next = bucket->ftb_data;
13870Sstevel@tonic-gate 	bucket->ftb_data = new_fp;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	mutex_enter(&new_fp->ftp_mtx);
13900Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
13910Sstevel@tonic-gate 
13921677Sdp 	crfree(cred);
13930Sstevel@tonic-gate 	return (new_fp);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate static void
13970Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	pid_t pid = provider->ftp_pid;
14000Sstevel@tonic-gate 	proc_t *p;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	/*
14031880Sahl 	 * There need to be no associated enabled probes, no consumers
14041880Sahl 	 * creating probes, and no meta providers referencing this provider.
14050Sstevel@tonic-gate 	 */
14061880Sahl 	ASSERT(provider->ftp_rcount == 0);
14070Sstevel@tonic-gate 	ASSERT(provider->ftp_ccount == 0);
14081880Sahl 	ASSERT(provider->ftp_mcount == 0);
14090Sstevel@tonic-gate 
14106390Sahl 	/*
14116390Sahl 	 * If this provider hasn't been retired, we need to explicitly drop the
14126390Sahl 	 * count of active providers on the associated process structure.
14136390Sahl 	 */
14146390Sahl 	if (!provider->ftp_retired) {
14156390Sahl 		atomic_add_64(&provider->ftp_proc->ftpc_acount, -1);
14166390Sahl 		ASSERT(provider->ftp_proc->ftpc_acount <
14176390Sahl 		    provider->ftp_proc->ftpc_rcount);
14186390Sahl 	}
14196390Sahl 
1420532Sahl 	fasttrap_proc_release(provider->ftp_proc);
1421532Sahl 
14220Sstevel@tonic-gate 	kmem_free(provider, sizeof (fasttrap_provider_t));
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	/*
14250Sstevel@tonic-gate 	 * Decrement p_dtrace_probes on the process whose provider we're
14260Sstevel@tonic-gate 	 * freeing. We don't have to worry about clobbering somone else's
14270Sstevel@tonic-gate 	 * modifications to it because we have locked the bucket that
14280Sstevel@tonic-gate 	 * corresponds to this process's hash chain in the provider hash
14290Sstevel@tonic-gate 	 * table. Don't sweat it if we can't find the process.
14300Sstevel@tonic-gate 	 */
14310Sstevel@tonic-gate 	mutex_enter(&pidlock);
14320Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
14330Sstevel@tonic-gate 		mutex_exit(&pidlock);
14340Sstevel@tonic-gate 		return;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
14380Sstevel@tonic-gate 	mutex_exit(&pidlock);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	p->p_dtrace_probes--;
14410Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate static void
14451880Sahl fasttrap_provider_retire(pid_t pid, const char *name, int mprov)
14460Sstevel@tonic-gate {
1447935Sahl 	fasttrap_provider_t *fp;
1448935Sahl 	fasttrap_bucket_t *bucket;
1449935Sahl 	dtrace_provider_id_t provid;
1450935Sahl 
1451935Sahl 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1452935Sahl 
1453935Sahl 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1454935Sahl 	mutex_enter(&bucket->ftb_mtx);
1455935Sahl 
1456935Sahl 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1457935Sahl 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1458935Sahl 		    !fp->ftp_retired)
1459935Sahl 			break;
1460935Sahl 	}
1461935Sahl 
1462935Sahl 	if (fp == NULL) {
1463935Sahl 		mutex_exit(&bucket->ftb_mtx);
1464935Sahl 		return;
1465935Sahl 	}
14660Sstevel@tonic-gate 
14671880Sahl 	mutex_enter(&fp->ftp_mtx);
14681880Sahl 	ASSERT(!mprov || fp->ftp_mcount > 0);
14691880Sahl 	if (mprov && --fp->ftp_mcount != 0)  {
14701880Sahl 		mutex_exit(&fp->ftp_mtx);
14711880Sahl 		mutex_exit(&bucket->ftb_mtx);
14721880Sahl 		return;
14731880Sahl 	}
14741880Sahl 
14750Sstevel@tonic-gate 	/*
14764821Sahl 	 * Mark the provider to be removed in our post-processing step, mark it
14774821Sahl 	 * retired, and drop the active count on its proc. Marking it indicates
14784821Sahl 	 * that we should try to remove it; setting the retired flag indicates
14794821Sahl 	 * that we're done with this provider; dropping the active the proc
14804821Sahl 	 * releases our hold, and when this reaches zero (as it will during
14814821Sahl 	 * exit or exec) the proc and associated providers become defunct.
1482935Sahl 	 *
1483935Sahl 	 * We obviously need to take the bucket lock before the provider lock
1484935Sahl 	 * to perform the lookup, but we need to drop the provider lock
1485935Sahl 	 * before calling into the DTrace framework since we acquire the
1486935Sahl 	 * provider lock in callbacks invoked from the DTrace framework. The
1487935Sahl 	 * bucket lock therefore protects the integrity of the provider hash
1488935Sahl 	 * table.
14890Sstevel@tonic-gate 	 */
14904821Sahl 	atomic_add_64(&fp->ftp_proc->ftpc_acount, -1);
14916390Sahl 	ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
14926390Sahl 
1493935Sahl 	fp->ftp_retired = 1;
1494935Sahl 	fp->ftp_marked = 1;
1495935Sahl 	provid = fp->ftp_provid;
1496935Sahl 	mutex_exit(&fp->ftp_mtx);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/*
14990Sstevel@tonic-gate 	 * We don't have to worry about invalidating the same provider twice
15000Sstevel@tonic-gate 	 * since fasttrap_provider_lookup() will ignore provider that have
1501532Sahl 	 * been marked as retired.
15020Sstevel@tonic-gate 	 */
15030Sstevel@tonic-gate 	dtrace_invalidate(provid);
15040Sstevel@tonic-gate 
1505935Sahl 	mutex_exit(&bucket->ftb_mtx);
1506935Sahl 
15070Sstevel@tonic-gate 	fasttrap_pid_cleanup();
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate static int
15113944Sahl fasttrap_uint32_cmp(const void *ap, const void *bp)
15123944Sahl {
15133944Sahl 	return (*(const uint32_t *)ap - *(const uint32_t *)bp);
15143944Sahl }
15153944Sahl 
15163944Sahl static int
15173944Sahl fasttrap_uint64_cmp(const void *ap, const void *bp)
15183944Sahl {
15193944Sahl 	return (*(const uint64_t *)ap - *(const uint64_t *)bp);
15203944Sahl }
15213944Sahl 
15223944Sahl static int
15230Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	fasttrap_provider_t *provider;
15260Sstevel@tonic-gate 	fasttrap_probe_t *pp;
15270Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
15280Sstevel@tonic-gate 	char *name;
15290Sstevel@tonic-gate 	int i, aframes, whack;
15300Sstevel@tonic-gate 
15313944Sahl 	/*
15323944Sahl 	 * There needs to be at least one desired trace point.
15333944Sahl 	 */
15343944Sahl 	if (pdata->ftps_noffs == 0)
15353944Sahl 		return (EINVAL);
15363944Sahl 
15370Sstevel@tonic-gate 	switch (pdata->ftps_type) {
15380Sstevel@tonic-gate 	case DTFTP_ENTRY:
15390Sstevel@tonic-gate 		name = "entry";
15400Sstevel@tonic-gate 		aframes = FASTTRAP_ENTRY_AFRAMES;
15410Sstevel@tonic-gate 		break;
15420Sstevel@tonic-gate 	case DTFTP_RETURN:
15430Sstevel@tonic-gate 		name = "return";
15440Sstevel@tonic-gate 		aframes = FASTTRAP_RETURN_AFRAMES;
15450Sstevel@tonic-gate 		break;
15460Sstevel@tonic-gate 	case DTFTP_OFFSETS:
15470Sstevel@tonic-gate 		name = NULL;
15480Sstevel@tonic-gate 		break;
15490Sstevel@tonic-gate 	default:
15500Sstevel@tonic-gate 		return (EINVAL);
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
15540Sstevel@tonic-gate 	    FASTTRAP_PID_NAME, &pid_attr)) == NULL)
15550Sstevel@tonic-gate 		return (ESRCH);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	/*
15580Sstevel@tonic-gate 	 * Increment this reference count to indicate that a consumer is
15592179Sahl 	 * actively adding a new probe associated with this provider. This
15602179Sahl 	 * prevents the provider from being deleted -- we'll need to check
15612179Sahl 	 * for pending deletions when we drop this reference count.
15620Sstevel@tonic-gate 	 */
15630Sstevel@tonic-gate 	provider->ftp_ccount++;
15640Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
15650Sstevel@tonic-gate 
15662179Sahl 	/*
15672179Sahl 	 * Grab the creation lock to ensure consistency between calls to
15682179Sahl 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
15692179Sahl 	 * other threads creating probes. We must drop the provider lock
15702179Sahl 	 * before taking this lock to avoid a three-way deadlock with the
15712179Sahl 	 * DTrace framework.
15722179Sahl 	 */
15732179Sahl 	mutex_enter(&provider->ftp_cmtx);
15740Sstevel@tonic-gate 
15752179Sahl 	if (name == NULL) {
15760Sstevel@tonic-gate 		for (i = 0; i < pdata->ftps_noffs; i++) {
15770Sstevel@tonic-gate 			char name_str[17];
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 			(void) sprintf(name_str, "%llx",
15800Sstevel@tonic-gate 			    (unsigned long long)pdata->ftps_offs[i]);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 			if (dtrace_probe_lookup(provider->ftp_provid,
15830Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
15840Sstevel@tonic-gate 				continue;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 			atomic_add_32(&fasttrap_total, 1);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 			if (fasttrap_total > fasttrap_max) {
15890Sstevel@tonic-gate 				atomic_add_32(&fasttrap_total, -1);
15900Sstevel@tonic-gate 				goto no_mem;
15910Sstevel@tonic-gate 			}
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 			pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 			pp->ftp_prov = provider;
15960Sstevel@tonic-gate 			pp->ftp_faddr = pdata->ftps_pc;
15970Sstevel@tonic-gate 			pp->ftp_fsize = pdata->ftps_size;
15980Sstevel@tonic-gate 			pp->ftp_pid = pdata->ftps_pid;
15990Sstevel@tonic-gate 			pp->ftp_ntps = 1;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
16020Sstevel@tonic-gate 			    KM_SLEEP);
16030Sstevel@tonic-gate 
1604532Sahl 			tp->ftt_proc = provider->ftp_proc;
16050Sstevel@tonic-gate 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
16060Sstevel@tonic-gate 			tp->ftt_pid = pdata->ftps_pid;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 			pp->ftp_tps[0].fit_tp = tp;
16090Sstevel@tonic-gate 			pp->ftp_tps[0].fit_id.fti_probe = pp;
16101710Sahl 			pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
16130Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str,
16140Sstevel@tonic-gate 			    FASTTRAP_OFFSET_AFRAMES, pp);
16150Sstevel@tonic-gate 		}
16162179Sahl 
16172179Sahl 	} else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
16182179Sahl 	    pdata->ftps_func, name) == 0) {
16192179Sahl 		atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
16202179Sahl 
16212179Sahl 		if (fasttrap_total > fasttrap_max) {
16222179Sahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
16232179Sahl 			goto no_mem;
16242179Sahl 		}
16252179Sahl 
16263944Sahl 		/*
16273944Sahl 		 * Make sure all tracepoint program counter values are unique.
16283944Sahl 		 * We later assume that each probe has exactly one tracepoint
16293944Sahl 		 * for a given pc.
16303944Sahl 		 */
16313944Sahl 		qsort(pdata->ftps_offs, pdata->ftps_noffs,
16323944Sahl 		    sizeof (uint64_t), fasttrap_uint64_cmp);
16333944Sahl 		for (i = 1; i < pdata->ftps_noffs; i++) {
16343944Sahl 			if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
16353944Sahl 				continue;
16363944Sahl 
16373944Sahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
16383944Sahl 			goto no_mem;
16393944Sahl 		}
16403944Sahl 
16412179Sahl 		ASSERT(pdata->ftps_noffs > 0);
16422179Sahl 		pp = kmem_zalloc(offsetof(fasttrap_probe_t,
16432179Sahl 		    ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
16442179Sahl 
16452179Sahl 		pp->ftp_prov = provider;
16462179Sahl 		pp->ftp_faddr = pdata->ftps_pc;
16472179Sahl 		pp->ftp_fsize = pdata->ftps_size;
16482179Sahl 		pp->ftp_pid = pdata->ftps_pid;
16492179Sahl 		pp->ftp_ntps = pdata->ftps_noffs;
16502179Sahl 
16512179Sahl 		for (i = 0; i < pdata->ftps_noffs; i++) {
16522179Sahl 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
16532179Sahl 			    KM_SLEEP);
16542179Sahl 
16552179Sahl 			tp->ftt_proc = provider->ftp_proc;
16562179Sahl 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
16572179Sahl 			tp->ftt_pid = pdata->ftps_pid;
16582179Sahl 
16592179Sahl 			pp->ftp_tps[i].fit_tp = tp;
16602179Sahl 			pp->ftp_tps[i].fit_id.fti_probe = pp;
16612179Sahl 			pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
16622179Sahl 		}
16632179Sahl 
16642179Sahl 		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
16652179Sahl 		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 
16682179Sahl 	mutex_exit(&provider->ftp_cmtx);
16692179Sahl 
16700Sstevel@tonic-gate 	/*
16710Sstevel@tonic-gate 	 * We know that the provider is still valid since we incremented the
16722179Sahl 	 * creation reference count. If someone tried to clean up this provider
16732179Sahl 	 * while we were using it (e.g. because the process called exec(2) or
16742179Sahl 	 * exit(2)), take note of that and try to clean it up now.
16750Sstevel@tonic-gate 	 */
16760Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
16770Sstevel@tonic-gate 	provider->ftp_ccount--;
1678532Sahl 	whack = provider->ftp_retired;
16790Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (whack)
16820Sstevel@tonic-gate 		fasttrap_pid_cleanup();
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	return (0);
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate no_mem:
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * If we've exhausted the allowable resources, we'll try to remove
16890Sstevel@tonic-gate 	 * this provider to free some up. This is to cover the case where
16900Sstevel@tonic-gate 	 * the user has accidentally created many more probes than was
16910Sstevel@tonic-gate 	 * intended (e.g. pid123:::).
16920Sstevel@tonic-gate 	 */
16932179Sahl 	mutex_exit(&provider->ftp_cmtx);
16940Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
16950Sstevel@tonic-gate 	provider->ftp_ccount--;
16960Sstevel@tonic-gate 	provider->ftp_marked = 1;
16970Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	fasttrap_pid_cleanup();
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 	return (ENOMEM);
17020Sstevel@tonic-gate }
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate /*ARGSUSED*/
17050Sstevel@tonic-gate static void *
17060Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	fasttrap_provider_t *provider;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	/*
17110Sstevel@tonic-gate 	 * A 32-bit unsigned integer (like a pid for example) can be
17120Sstevel@tonic-gate 	 * expressed in 10 or fewer decimal digits. Make sure that we'll
17130Sstevel@tonic-gate 	 * have enough space for the provider name.
17140Sstevel@tonic-gate 	 */
17150Sstevel@tonic-gate 	if (strlen(dhpv->dthpv_provname) + 10 >=
17160Sstevel@tonic-gate 	    sizeof (provider->ftp_name)) {
17170Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
17180Sstevel@tonic-gate 		    "name too long to accomodate pid", dhpv->dthpv_provname);
17190Sstevel@tonic-gate 		return (NULL);
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 	/*
17230Sstevel@tonic-gate 	 * Don't let folks spoof the true pid provider.
17240Sstevel@tonic-gate 	 */
17250Sstevel@tonic-gate 	if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
17260Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
17270Sstevel@tonic-gate 		    "%s is an invalid name", dhpv->dthpv_provname,
17280Sstevel@tonic-gate 		    FASTTRAP_PID_NAME);
17290Sstevel@tonic-gate 		return (NULL);
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	/*
17330Sstevel@tonic-gate 	 * The highest stability class that fasttrap supports is ISA; cap
17340Sstevel@tonic-gate 	 * the stability of the new provider accordingly.
17350Sstevel@tonic-gate 	 */
17364821Sahl 	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
17370Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
17384821Sahl 	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
17390Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
17404821Sahl 	if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
17410Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
17424821Sahl 	if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
17430Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
17444821Sahl 	if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
17450Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
17480Sstevel@tonic-gate 	    &dhpv->dthpv_pattr)) == NULL) {
17490Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s for "
17500Sstevel@tonic-gate 		    "process %u",  dhpv->dthpv_provname, (uint_t)pid);
17510Sstevel@tonic-gate 		return (NULL);
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/*
17551880Sahl 	 * Up the meta provider count so this provider isn't removed until
17561880Sahl 	 * the meta provider has been told to remove it.
17570Sstevel@tonic-gate 	 */
17581880Sahl 	provider->ftp_mcount++;
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	return (provider);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate /*ARGSUSED*/
17660Sstevel@tonic-gate static void
17670Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg,
17680Sstevel@tonic-gate     dtrace_helper_probedesc_t *dhpb)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate 	fasttrap_provider_t *provider = parg;
17710Sstevel@tonic-gate 	fasttrap_probe_t *pp;
17720Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
17731710Sahl 	int i, j;
17741710Sahl 	uint32_t ntps;
17750Sstevel@tonic-gate 
17762179Sahl 	/*
17772179Sahl 	 * Since the meta provider count is non-zero we don't have to worry
17782179Sahl 	 * about this provider disappearing.
17792179Sahl 	 */
17802179Sahl 	ASSERT(provider->ftp_mcount > 0);
17812179Sahl 
17822179Sahl 	/*
17833944Sahl 	 * The offsets must be unique.
17843944Sahl 	 */
17853944Sahl 	qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
17863944Sahl 	    fasttrap_uint32_cmp);
17873944Sahl 	for (i = 1; i < dhpb->dthpb_noffs; i++) {
17883944Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
17893944Sahl 		    dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
17903944Sahl 			return;
17913944Sahl 	}
17923944Sahl 
17933944Sahl 	qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
17943944Sahl 	    fasttrap_uint32_cmp);
17953944Sahl 	for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
17963944Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
17973944Sahl 		    dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
17983944Sahl 			return;
17993944Sahl 	}
18003944Sahl 
18013944Sahl 	/*
18022179Sahl 	 * Grab the creation lock to ensure consistency between calls to
18032179Sahl 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
18042179Sahl 	 * other threads creating probes.
18052179Sahl 	 */
18062179Sahl 	mutex_enter(&provider->ftp_cmtx);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
18090Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
18102179Sahl 		mutex_exit(&provider->ftp_cmtx);
18110Sstevel@tonic-gate 		return;
18120Sstevel@tonic-gate 	}
18130Sstevel@tonic-gate 
18141710Sahl 	ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
18151880Sahl 	ASSERT(ntps > 0);
18161710Sahl 
18171710Sahl 	atomic_add_32(&fasttrap_total, ntps);
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	if (fasttrap_total > fasttrap_max) {
18201710Sahl 		atomic_add_32(&fasttrap_total, -ntps);
18212179Sahl 		mutex_exit(&provider->ftp_cmtx);
18220Sstevel@tonic-gate 		return;
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 
18251880Sahl 	pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	pp->ftp_prov = provider;
18280Sstevel@tonic-gate 	pp->ftp_pid = provider->ftp_pid;
18291710Sahl 	pp->ftp_ntps = ntps;
18300Sstevel@tonic-gate 	pp->ftp_nargs = dhpb->dthpb_xargc;
18310Sstevel@tonic-gate 	pp->ftp_xtypes = dhpb->dthpb_xtypes;
18320Sstevel@tonic-gate 	pp->ftp_ntypes = dhpb->dthpb_ntypes;
18330Sstevel@tonic-gate 
18341710Sahl 	/*
18351710Sahl 	 * First create a tracepoint for each actual point of interest.
18361710Sahl 	 */
18371710Sahl 	for (i = 0; i < dhpb->dthpb_noffs; i++) {
18380Sstevel@tonic-gate 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18390Sstevel@tonic-gate 
1840532Sahl 		tp->ftt_proc = provider->ftp_proc;
18410Sstevel@tonic-gate 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
18420Sstevel@tonic-gate 		tp->ftt_pid = provider->ftp_pid;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		pp->ftp_tps[i].fit_tp = tp;
18450Sstevel@tonic-gate 		pp->ftp_tps[i].fit_id.fti_probe = pp;
18461710Sahl #ifdef __sparc
18471710Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
18481710Sahl #else
18491710Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
18501710Sahl #endif
18511710Sahl 	}
18521710Sahl 
18531710Sahl 	/*
18541710Sahl 	 * Then create a tracepoint for each is-enabled point.
18551710Sahl 	 */
18561710Sahl 	for (j = 0; i < ntps; i++, j++) {
18571710Sahl 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18581710Sahl 
18591710Sahl 		tp->ftt_proc = provider->ftp_proc;
18601710Sahl 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
18611710Sahl 		tp->ftt_pid = provider->ftp_pid;
18621710Sahl 
18631710Sahl 		pp->ftp_tps[i].fit_tp = tp;
18641710Sahl 		pp->ftp_tps[i].fit_id.fti_probe = pp;
18651710Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
18660Sstevel@tonic-gate 	}
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 	/*
18690Sstevel@tonic-gate 	 * If the arguments are shuffled around we set the argument remapping
18700Sstevel@tonic-gate 	 * table. Later, when the probe fires, we only remap the arguments
18710Sstevel@tonic-gate 	 * if the table is non-NULL.
18720Sstevel@tonic-gate 	 */
18730Sstevel@tonic-gate 	for (i = 0; i < dhpb->dthpb_xargc; i++) {
18740Sstevel@tonic-gate 		if (dhpb->dthpb_args[i] != i) {
18750Sstevel@tonic-gate 			pp->ftp_argmap = dhpb->dthpb_args;
18760Sstevel@tonic-gate 			break;
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	/*
18810Sstevel@tonic-gate 	 * The probe is fully constructed -- register it with DTrace.
18820Sstevel@tonic-gate 	 */
18830Sstevel@tonic-gate 	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
18840Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
18850Sstevel@tonic-gate 
18862179Sahl 	mutex_exit(&provider->ftp_cmtx);
18870Sstevel@tonic-gate }
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate /*ARGSUSED*/
18900Sstevel@tonic-gate static void
18910Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
18920Sstevel@tonic-gate {
1893935Sahl 	/*
1894935Sahl 	 * Clean up the USDT provider. There may be active consumers of the
1895935Sahl 	 * provider busy adding probes, no damage will actually befall the
1896935Sahl 	 * provider until that count has dropped to zero. This just puts
1897935Sahl 	 * the provider on death row.
1898935Sahl 	 */
1899935Sahl 	fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = {
19030Sstevel@tonic-gate 	fasttrap_meta_create_probe,
19040Sstevel@tonic-gate 	fasttrap_meta_provide,
19050Sstevel@tonic-gate 	fasttrap_meta_remove
19060Sstevel@tonic-gate };
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate /*ARGSUSED*/
19090Sstevel@tonic-gate static int
19100Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
19110Sstevel@tonic-gate {
19120Sstevel@tonic-gate 	return (0);
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate /*ARGSUSED*/
19160Sstevel@tonic-gate static int
19170Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	if (!dtrace_attached())
19200Sstevel@tonic-gate 		return (EAGAIN);
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate 	if (cmd == FASTTRAPIOC_MAKEPROBE) {
19230Sstevel@tonic-gate 		fasttrap_probe_spec_t *uprobe = (void *)arg;
19240Sstevel@tonic-gate 		fasttrap_probe_spec_t *probe;
19250Sstevel@tonic-gate 		uint64_t noffs;
19260Sstevel@tonic-gate 		size_t size;
19270Sstevel@tonic-gate 		int ret;
19280Sstevel@tonic-gate 		char *c;
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 		if (copyin(&uprobe->ftps_noffs, &noffs,
19310Sstevel@tonic-gate 		    sizeof (uprobe->ftps_noffs)))
19320Sstevel@tonic-gate 			return (EFAULT);
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 		/*
19350Sstevel@tonic-gate 		 * Probes must have at least one tracepoint.
19360Sstevel@tonic-gate 		 */
19370Sstevel@tonic-gate 		if (noffs == 0)
19380Sstevel@tonic-gate 			return (EINVAL);
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 		size = sizeof (fasttrap_probe_spec_t) +
19410Sstevel@tonic-gate 		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		if (size > 1024 * 1024)
19440Sstevel@tonic-gate 			return (ENOMEM);
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 		probe = kmem_alloc(size, KM_SLEEP);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 		if (copyin(uprobe, probe, size) != 0) {
19490Sstevel@tonic-gate 			kmem_free(probe, size);
19500Sstevel@tonic-gate 			return (EFAULT);
19510Sstevel@tonic-gate 		}
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 		/*
19540Sstevel@tonic-gate 		 * Verify that the function and module strings contain no
19550Sstevel@tonic-gate 		 * funny characters.
19560Sstevel@tonic-gate 		 */
19570Sstevel@tonic-gate 		for (c = &probe->ftps_func[0]; *c != '\0'; c++) {
19580Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
19590Sstevel@tonic-gate 				ret = EINVAL;
19600Sstevel@tonic-gate 				goto err;
19610Sstevel@tonic-gate 			}
19620Sstevel@tonic-gate 		}
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 		for (c = &probe->ftps_mod[0]; *c != '\0'; c++) {
19650Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
19660Sstevel@tonic-gate 				ret = EINVAL;
19670Sstevel@tonic-gate 				goto err;
19680Sstevel@tonic-gate 			}
19690Sstevel@tonic-gate 		}
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 = probe->ftps_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 | VWRITE)) != 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 		ret = fasttrap_add_probe(probe);
19970Sstevel@tonic-gate err:
19980Sstevel@tonic-gate 		kmem_free(probe, size);
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		return (ret);
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	} else if (cmd == FASTTRAPIOC_GETINSTR) {
20030Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
20040Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
20050Sstevel@tonic-gate 		uint_t index;
20060Sstevel@tonic-gate 		int ret;
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 		if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
20090Sstevel@tonic-gate 			return (EFAULT);
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
20120Sstevel@tonic-gate 			proc_t *p;
20130Sstevel@tonic-gate 			pid_t pid = instr.ftiq_pid;
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 			mutex_enter(&pidlock);
20160Sstevel@tonic-gate 			/*
20170Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
20180Sstevel@tonic-gate 			 * or is actively being birthed.
20190Sstevel@tonic-gate 			 */
20200Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
20210Sstevel@tonic-gate 				mutex_exit(&pidlock);
20220Sstevel@tonic-gate 				return (ESRCH);
20230Sstevel@tonic-gate 			}
20240Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
20250Sstevel@tonic-gate 			mutex_exit(&pidlock);
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
20280Sstevel@tonic-gate 			    VREAD)) != 0) {
20290Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
20300Sstevel@tonic-gate 				return (ret);
20310Sstevel@tonic-gate 			}
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
20340Sstevel@tonic-gate 		}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 		mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20390Sstevel@tonic-gate 		tp = fasttrap_tpoints.fth_table[index].ftb_data;
20400Sstevel@tonic-gate 		while (tp != NULL) {
20410Sstevel@tonic-gate 			if (instr.ftiq_pid == tp->ftt_pid &&
20420Sstevel@tonic-gate 			    instr.ftiq_pc == tp->ftt_pc &&
20434821Sahl 			    tp->ftt_proc->ftpc_acount != 0)
20440Sstevel@tonic-gate 				break;
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 			tp = tp->ftt_next;
20470Sstevel@tonic-gate 		}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 		if (tp == NULL) {
20500Sstevel@tonic-gate 			mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20510Sstevel@tonic-gate 			return (ENOENT);
20520Sstevel@tonic-gate 		}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
20550Sstevel@tonic-gate 		    sizeof (instr.ftiq_instr));
20560Sstevel@tonic-gate 		mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 		if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
20590Sstevel@tonic-gate 			return (EFAULT);
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 		return (0);
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 	return (EINVAL);
20650Sstevel@tonic-gate }
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = {
20680Sstevel@tonic-gate 	fasttrap_open,		/* open */
20690Sstevel@tonic-gate 	nodev,			/* close */
20700Sstevel@tonic-gate 	nulldev,		/* strategy */
20710Sstevel@tonic-gate 	nulldev,		/* print */
20720Sstevel@tonic-gate 	nodev,			/* dump */
20730Sstevel@tonic-gate 	nodev,			/* read */
20740Sstevel@tonic-gate 	nodev,			/* write */
20750Sstevel@tonic-gate 	fasttrap_ioctl,		/* ioctl */
20760Sstevel@tonic-gate 	nodev,			/* devmap */
20770Sstevel@tonic-gate 	nodev,			/* mmap */
20780Sstevel@tonic-gate 	nodev,			/* segmap */
20790Sstevel@tonic-gate 	nochpoll,		/* poll */
20800Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
20810Sstevel@tonic-gate 	0,			/* streamtab  */
20820Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
20830Sstevel@tonic-gate };
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate /*ARGSUSED*/
20860Sstevel@tonic-gate static int
20870Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
20880Sstevel@tonic-gate {
20890Sstevel@tonic-gate 	int error;
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	switch (infocmd) {
20920Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
20930Sstevel@tonic-gate 		*result = (void *)fasttrap_devi;
20940Sstevel@tonic-gate 		error = DDI_SUCCESS;
20950Sstevel@tonic-gate 		break;
20960Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
20970Sstevel@tonic-gate 		*result = (void *)0;
20980Sstevel@tonic-gate 		error = DDI_SUCCESS;
20990Sstevel@tonic-gate 		break;
21000Sstevel@tonic-gate 	default:
21010Sstevel@tonic-gate 		error = DDI_FAILURE;
21020Sstevel@tonic-gate 	}
21030Sstevel@tonic-gate 	return (error);
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate static int
21070Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
21080Sstevel@tonic-gate {
21090Sstevel@tonic-gate 	ulong_t nent;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	switch (cmd) {
21120Sstevel@tonic-gate 	case DDI_ATTACH:
21130Sstevel@tonic-gate 		break;
21140Sstevel@tonic-gate 	case DDI_RESUME:
21150Sstevel@tonic-gate 		return (DDI_SUCCESS);
21160Sstevel@tonic-gate 	default:
21170Sstevel@tonic-gate 		return (DDI_FAILURE);
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0,
21212179Sahl 	    DDI_PSEUDO, NULL) == DDI_FAILURE) {
21220Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
21230Sstevel@tonic-gate 		return (DDI_FAILURE);
21240Sstevel@tonic-gate 	}
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	ddi_report_dev(devi);
21270Sstevel@tonic-gate 	fasttrap_devi = devi;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	/*
21300Sstevel@tonic-gate 	 * Install our hooks into fork(2), exec(2), and exit(2).
21310Sstevel@tonic-gate 	 */
21320Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = &fasttrap_fork;
21330Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
21340Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21370Sstevel@tonic-gate 	    "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
21380Sstevel@tonic-gate 	fasttrap_total = 0;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	/*
21410Sstevel@tonic-gate 	 * Conjure up the tracepoints hashtable...
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21440Sstevel@tonic-gate 	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
21450Sstevel@tonic-gate 
21463944Sahl 	if (nent == 0 || nent > 0x1000000)
21470Sstevel@tonic-gate 		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
21500Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = nent;
21510Sstevel@tonic-gate 	else
21520Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
21530Sstevel@tonic-gate 	ASSERT(fasttrap_tpoints.fth_nent > 0);
21540Sstevel@tonic-gate 	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
21550Sstevel@tonic-gate 	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
21560Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	/*
2159532Sahl 	 * ... and the providers hash table...
21600Sstevel@tonic-gate 	 */
21610Sstevel@tonic-gate 	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
21620Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
21630Sstevel@tonic-gate 		fasttrap_provs.fth_nent = nent;
21640Sstevel@tonic-gate 	else
21650Sstevel@tonic-gate 		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
21660Sstevel@tonic-gate 	ASSERT(fasttrap_provs.fth_nent > 0);
21670Sstevel@tonic-gate 	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
2168532Sahl 	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
2169532Sahl 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21700Sstevel@tonic-gate 
2171532Sahl 	/*
2172532Sahl 	 * ... and the procs hash table.
2173532Sahl 	 */
2174532Sahl 	nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2175532Sahl 	if ((nent & (nent - 1)) == 0)
2176532Sahl 		fasttrap_procs.fth_nent = nent;
2177532Sahl 	else
2178532Sahl 		fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2179532Sahl 	ASSERT(fasttrap_procs.fth_nent > 0);
2180532Sahl 	fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2181532Sahl 	fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
21820Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
21850Sstevel@tonic-gate 	    &fasttrap_meta_id);
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 	return (DDI_SUCCESS);
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate static int
21910Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
21920Sstevel@tonic-gate {
21930Sstevel@tonic-gate 	int i, fail = 0;
21940Sstevel@tonic-gate 	timeout_id_t tmp;
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	switch (cmd) {
21970Sstevel@tonic-gate 	case DDI_DETACH:
21980Sstevel@tonic-gate 		break;
21990Sstevel@tonic-gate 	case DDI_SUSPEND:
22000Sstevel@tonic-gate 		return (DDI_SUCCESS);
22010Sstevel@tonic-gate 	default:
22020Sstevel@tonic-gate 		return (DDI_FAILURE);
22030Sstevel@tonic-gate 	}
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	/*
22060Sstevel@tonic-gate 	 * Unregister the meta-provider to make sure no new fasttrap-
22070Sstevel@tonic-gate 	 * managed providers come along while we're trying to close up
22080Sstevel@tonic-gate 	 * shop. If we fail to detach, we'll need to re-register as a
22090Sstevel@tonic-gate 	 * meta-provider. We can fail to unregister as a meta-provider
22100Sstevel@tonic-gate 	 * if providers we manage still exist.
22110Sstevel@tonic-gate 	 */
22120Sstevel@tonic-gate 	if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
22130Sstevel@tonic-gate 	    dtrace_meta_unregister(fasttrap_meta_id) != 0)
22140Sstevel@tonic-gate 		return (DDI_FAILURE);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 	/*
22170Sstevel@tonic-gate 	 * Prevent any new timeouts from running by setting fasttrap_timeout
22180Sstevel@tonic-gate 	 * to a non-zero value, and wait for the current timeout to complete.
22190Sstevel@tonic-gate 	 */
22200Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
22210Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	while (fasttrap_timeout != (timeout_id_t)1) {
22240Sstevel@tonic-gate 		tmp = fasttrap_timeout;
22250Sstevel@tonic-gate 		fasttrap_timeout = (timeout_id_t)1;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 		if (tmp != 0) {
22280Sstevel@tonic-gate 			mutex_exit(&fasttrap_cleanup_mtx);
22290Sstevel@tonic-gate 			(void) untimeout(tmp);
22300Sstevel@tonic-gate 			mutex_enter(&fasttrap_cleanup_mtx);
22310Sstevel@tonic-gate 		}
22320Sstevel@tonic-gate 	}
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
22350Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * Iterate over all of our providers. If there's still a process
22390Sstevel@tonic-gate 	 * that corresponds to that pid, fail to detach.
22400Sstevel@tonic-gate 	 */
22410Sstevel@tonic-gate 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
22420Sstevel@tonic-gate 		fasttrap_provider_t **fpp, *fp;
22430Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
22460Sstevel@tonic-gate 		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
22470Sstevel@tonic-gate 		while ((fp = *fpp) != NULL) {
22480Sstevel@tonic-gate 			/*
22490Sstevel@tonic-gate 			 * Acquire and release the lock as a simple way of
22500Sstevel@tonic-gate 			 * waiting for any other consumer to finish with
22510Sstevel@tonic-gate 			 * this provider. A thread must first acquire the
22520Sstevel@tonic-gate 			 * bucket lock so there's no chance of another thread
2253935Sahl 			 * blocking on the provider's lock.
22540Sstevel@tonic-gate 			 */
22550Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
22560Sstevel@tonic-gate 			mutex_exit(&fp->ftp_mtx);
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 			if (dtrace_unregister(fp->ftp_provid) != 0) {
22590Sstevel@tonic-gate 				fail = 1;
22600Sstevel@tonic-gate 				fpp = &fp->ftp_next;
22610Sstevel@tonic-gate 			} else {
22620Sstevel@tonic-gate 				*fpp = fp->ftp_next;
22630Sstevel@tonic-gate 				fasttrap_provider_free(fp);
22640Sstevel@tonic-gate 			}
22650Sstevel@tonic-gate 		}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22702179Sahl 	if (fail) {
22710Sstevel@tonic-gate 		uint_t work;
22720Sstevel@tonic-gate 		/*
22730Sstevel@tonic-gate 		 * If we're failing to detach, we need to unblock timeouts
22740Sstevel@tonic-gate 		 * and start a new timeout if any work has accumulated while
22750Sstevel@tonic-gate 		 * we've been unsuccessfully trying to detach.
22760Sstevel@tonic-gate 		 */
22770Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
22780Sstevel@tonic-gate 		fasttrap_timeout = 0;
22790Sstevel@tonic-gate 		work = fasttrap_cleanup_work;
22800Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 		if (work)
22830Sstevel@tonic-gate 			fasttrap_pid_cleanup();
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
22860Sstevel@tonic-gate 		    &fasttrap_meta_id);
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 		return (DDI_FAILURE);
22890Sstevel@tonic-gate 	}
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate #ifdef DEBUG
22920Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
22932179Sahl 	ASSERT(fasttrap_pid_count == 0);
22940Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
22950Sstevel@tonic-gate #endif
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	kmem_free(fasttrap_tpoints.fth_table,
22980Sstevel@tonic-gate 	    fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
22990Sstevel@tonic-gate 	fasttrap_tpoints.fth_nent = 0;
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 	kmem_free(fasttrap_provs.fth_table,
23020Sstevel@tonic-gate 	    fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
23030Sstevel@tonic-gate 	fasttrap_provs.fth_nent = 0;
23040Sstevel@tonic-gate 
2305532Sahl 	kmem_free(fasttrap_procs.fth_table,
2306532Sahl 	    fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2307532Sahl 	fasttrap_procs.fth_nent = 0;
2308532Sahl 
23090Sstevel@tonic-gate 	/*
23100Sstevel@tonic-gate 	 * We know there are no tracepoints in any process anywhere in
23110Sstevel@tonic-gate 	 * the system so there is no process which has its p_dtrace_count
23120Sstevel@tonic-gate 	 * greater than zero, therefore we know that no thread can actively
23130Sstevel@tonic-gate 	 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes
23140Sstevel@tonic-gate 	 * and fasttrap_exec() and fasttrap_exit().
23150Sstevel@tonic-gate 	 */
23160Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork);
23170Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = NULL;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit);
23200Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = NULL;
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit);
23230Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = NULL;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 	return (DDI_SUCCESS);
23280Sstevel@tonic-gate }
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate static struct dev_ops fasttrap_ops = {
23310Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
23320Sstevel@tonic-gate 	0,			/* refcnt */
23330Sstevel@tonic-gate 	fasttrap_info,		/* get_dev_info */
23340Sstevel@tonic-gate 	nulldev,		/* identify */
23350Sstevel@tonic-gate 	nulldev,		/* probe */
23360Sstevel@tonic-gate 	fasttrap_attach,	/* attach */
23370Sstevel@tonic-gate 	fasttrap_detach,	/* detach */
23380Sstevel@tonic-gate 	nodev,			/* reset */
23390Sstevel@tonic-gate 	&fasttrap_cb_ops,	/* driver operations */
23400Sstevel@tonic-gate 	NULL,			/* bus operations */
2341*7656SSherry.Moore@Sun.COM 	nodev,			/* dev power */
2342*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
23430Sstevel@tonic-gate };
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate /*
23460Sstevel@tonic-gate  * Module linkage information for the kernel.
23470Sstevel@tonic-gate  */
23480Sstevel@tonic-gate static struct modldrv modldrv = {
23490Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
23500Sstevel@tonic-gate 	"Fasttrap Tracing",	/* name of module */
23510Sstevel@tonic-gate 	&fasttrap_ops,		/* driver ops */
23520Sstevel@tonic-gate };
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate static struct modlinkage modlinkage = {
23550Sstevel@tonic-gate 	MODREV_1,
23560Sstevel@tonic-gate 	(void *)&modldrv,
23570Sstevel@tonic-gate 	NULL
23580Sstevel@tonic-gate };
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate int
23610Sstevel@tonic-gate _init(void)
23620Sstevel@tonic-gate {
23630Sstevel@tonic-gate 	return (mod_install(&modlinkage));
23640Sstevel@tonic-gate }
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate int
23670Sstevel@tonic-gate _info(struct modinfo *modinfop)
23680Sstevel@tonic-gate {
23690Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate int
23730Sstevel@tonic-gate _fini(void)
23740Sstevel@tonic-gate {
23750Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
23760Sstevel@tonic-gate }
2377