xref: /titanic_44/usr/src/uts/common/dtrace/fasttrap.c (revision 038dc6b3d39a14038605fbb3fc6a9bb76dd27fa8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ad4023c4Sdp  * Common Development and Distribution License (the "License").
6ad4023c4Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
2373427c57Sahl  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/stat.h>
327c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
407c478bd9Sstevel@tonic-gate #include <sys/fasttrap.h>
417c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
437c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
447c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
467c478bd9Sstevel@tonic-gate #include <sys/proc.h>
477c478bd9Sstevel@tonic-gate #include <sys/priv.h>
487c478bd9Sstevel@tonic-gate #include <sys/policy.h>
4973427c57Sahl #include <util/qsort.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * User-Land Trap-Based Tracing
537c478bd9Sstevel@tonic-gate  * ----------------------------
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * The fasttrap provider allows DTrace consumers to instrument any user-level
567c478bd9Sstevel@tonic-gate  * instruction to gather data; this includes probes with semantic
577c478bd9Sstevel@tonic-gate  * signifigance like entry and return as well as simple offsets into the
587c478bd9Sstevel@tonic-gate  * function. While the specific techniques used are very ISA specific, the
597c478bd9Sstevel@tonic-gate  * methodology is generalizable to any architecture.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * The General Methodology
637c478bd9Sstevel@tonic-gate  * -----------------------
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * With the primary goal of tracing every user-land instruction and the
667c478bd9Sstevel@tonic-gate  * limitation that we can't trust user space so don't want to rely on much
677c478bd9Sstevel@tonic-gate  * information there, we begin by replacing the instructions we want to trace
687c478bd9Sstevel@tonic-gate  * with trap instructions. Each instruction we overwrite is saved into a hash
697c478bd9Sstevel@tonic-gate  * table keyed by process ID and pc address. When we enter the kernel due to
707c478bd9Sstevel@tonic-gate  * this trap instruction, we need the effects of the replaced instruction to
717c478bd9Sstevel@tonic-gate  * appear to have occurred before we proceed with the user thread's
727c478bd9Sstevel@tonic-gate  * execution.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Each user level thread is represented by a ulwp_t structure which is
757c478bd9Sstevel@tonic-gate  * always easily accessible through a register. The most basic way to produce
767c478bd9Sstevel@tonic-gate  * the effects of the instruction we replaced is to copy that instruction out
777c478bd9Sstevel@tonic-gate  * to a bit of scratch space reserved in the user thread's ulwp_t structure
787c478bd9Sstevel@tonic-gate  * (a sort of kernel-private thread local storage), set the PC to that
797c478bd9Sstevel@tonic-gate  * scratch space and single step. When we reenter the kernel after single
807c478bd9Sstevel@tonic-gate  * stepping the instruction we must then adjust the PC to point to what would
817c478bd9Sstevel@tonic-gate  * normally be the next instruction. Of course, special care must be taken
827c478bd9Sstevel@tonic-gate  * for branches and jumps, but these represent such a small fraction of any
837c478bd9Sstevel@tonic-gate  * instruction set that writing the code to emulate these in the kernel is
847c478bd9Sstevel@tonic-gate  * not too difficult.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * Return probes may require several tracepoints to trace every return site,
877c478bd9Sstevel@tonic-gate  * and, conversely, each tracepoint may activate several probes (the entry
887c478bd9Sstevel@tonic-gate  * and offset 0 probes, for example). To solve this muliplexing problem,
897c478bd9Sstevel@tonic-gate  * tracepoints contain lists of probes to activate and probes contain lists
907c478bd9Sstevel@tonic-gate  * of tracepoints to enable. If a probe is activated, it adds its ID to
917c478bd9Sstevel@tonic-gate  * existing tracepoints or creates new ones as necessary.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * Most probes are activated _before_ the instruction is executed, but return
947c478bd9Sstevel@tonic-gate  * probes are activated _after_ the effects of the last instruction of the
957c478bd9Sstevel@tonic-gate  * function are visible. Return probes must be fired _after_ we have
967c478bd9Sstevel@tonic-gate  * single-stepped the instruction whereas all other probes are fired
977c478bd9Sstevel@tonic-gate  * beforehand.
98f498645aSahl  *
99f498645aSahl  *
100f498645aSahl  * Lock Ordering
101f498645aSahl  * -------------
102f498645aSahl  *
103f498645aSahl  * The lock ordering below -- both internally and with respect to the DTrace
104f498645aSahl  * framework -- is a little tricky and bears some explanation. Each provider
105f498645aSahl  * has a lock (ftp_mtx) that protects its members including reference counts
106f498645aSahl  * for enabled probes (ftp_rcount), consumers actively creating probes
107f498645aSahl  * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
108f498645aSahl  * from being freed. A provider is looked up by taking the bucket lock for the
109f498645aSahl  * provider hash table, and is returned with its lock held. The provider lock
110f498645aSahl  * may be taken in functions invoked by the DTrace framework, but may not be
111f498645aSahl  * held while calling functions in the DTrace framework.
112f498645aSahl  *
113f498645aSahl  * To ensure consistency over multiple calls to the DTrace framework, the
114f498645aSahl  * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
115f498645aSahl  * not be taken when holding the provider lock as that would create a cyclic
116f498645aSahl  * lock ordering. In situations where one would naturally take the provider
117f498645aSahl  * lock and then the creation lock, we instead up a reference count to prevent
118f498645aSahl  * the provider from disappearing, drop the provider lock, and acquire the
119f498645aSahl  * creation lock.
120f498645aSahl  *
121f498645aSahl  * Briefly:
122f498645aSahl  * 	bucket lock before provider lock
123f498645aSahl  *	DTrace before provider lock
124f498645aSahl  *	creation lock before DTrace
125f498645aSahl  *	never hold the provider lock and creation lock simultaneously
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static dev_info_t *fasttrap_devi;
1297c478bd9Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static timeout_id_t fasttrap_timeout;
1327c478bd9Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx;
1337c478bd9Sstevel@tonic-gate static uint_t fasttrap_cleanup_work;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Generation count on modifications to the global tracepoint lookup table.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * When the fasttrap provider is loaded, fasttrap_max is set to either
1427c478bd9Sstevel@tonic-gate  * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
1437c478bd9Sstevel@tonic-gate  * fasttrap.conf file. Each time a probe is created, fasttrap_total is
1447c478bd9Sstevel@tonic-gate  * incremented by the number of tracepoints that may be associated with that
1457c478bd9Sstevel@tonic-gate  * probe; fasttrap_total is capped at fasttrap_max.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate #define	FASTTRAP_MAX_DEFAULT		250000
1487c478bd9Sstevel@tonic-gate static uint32_t fasttrap_max;
1497c478bd9Sstevel@tonic-gate static uint32_t fasttrap_total;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
1537c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
154b096140dSahl #define	FASTTRAP_PROCS_DEFAULT_SIZE	0x100
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	FASTTRAP_PID_NAME		"pid"
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate fasttrap_hash_t			fasttrap_tpoints;
1597c478bd9Sstevel@tonic-gate static fasttrap_hash_t		fasttrap_provs;
160b096140dSahl static fasttrap_hash_t		fasttrap_procs;
1617c478bd9Sstevel@tonic-gate 
162f498645aSahl static uint64_t			fasttrap_pid_count;	/* pid ref count */
1637c478bd9Sstevel@tonic-gate static kmutex_t			fasttrap_count_mtx;	/* lock on ref count */
1647c478bd9Sstevel@tonic-gate 
1658f5db3e7Sahl #define	FASTTRAP_ENABLE_FAIL	1
1668f5db3e7Sahl #define	FASTTRAP_ENABLE_PARTIAL	2
1678f5db3e7Sahl 
1687c478bd9Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
1697c478bd9Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
1727c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *);
173dafb5540Sahl static void fasttrap_provider_retire(pid_t, const char *, int);
1747c478bd9Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *);
1757c478bd9Sstevel@tonic-gate 
176b096140dSahl static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
177b096140dSahl static void fasttrap_proc_release(fasttrap_proc_t *);
178b096140dSahl 
1797c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVS_INDEX(pid, name) \
1807c478bd9Sstevel@tonic-gate 	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
1817c478bd9Sstevel@tonic-gate 
182b096140dSahl #define	FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
183b096140dSahl 
1847c478bd9Sstevel@tonic-gate static int
1857c478bd9Sstevel@tonic-gate fasttrap_highbit(ulong_t i)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	int h = 1;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (i == 0)
1907c478bd9Sstevel@tonic-gate 		return (0);
1917c478bd9Sstevel@tonic-gate #ifdef _LP64
1927c478bd9Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
1937c478bd9Sstevel@tonic-gate 		h += 32; i >>= 32;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate #endif
1967c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000) {
1977c478bd9Sstevel@tonic-gate 		h += 16; i >>= 16;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	if (i & 0xff00) {
2007c478bd9Sstevel@tonic-gate 		h += 8; i >>= 8;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	if (i & 0xf0) {
2037c478bd9Sstevel@tonic-gate 		h += 4; i >>= 4;
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	if (i & 0xc) {
2067c478bd9Sstevel@tonic-gate 		h += 2; i >>= 2;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	if (i & 0x2) {
2097c478bd9Sstevel@tonic-gate 		h += 1;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (h);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static uint_t
2157c478bd9Sstevel@tonic-gate fasttrap_hash_str(const char *p)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	unsigned int g;
2187c478bd9Sstevel@tonic-gate 	uint_t hval = 0;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	while (*p) {
2217c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *p++;
2227c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
2237c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
2247c478bd9Sstevel@tonic-gate 		hval &= ~g;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	return (hval);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate void
2307c478bd9Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGTRAP;
2357c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_code = TRAP_DTRACE;
2367c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)pc;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2397c478bd9Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
2407c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (t != NULL)
2437c478bd9Sstevel@tonic-gate 		aston(t);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * This function ensures that no threads are actively using the memory
2487c478bd9Sstevel@tonic-gate  * associated with probes that were formerly live.
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate static void
2517c478bd9Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	int i;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (gen < fasttrap_mod_gen)
2567c478bd9Sstevel@tonic-gate 		return;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	fasttrap_mod_gen++;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
2617c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_core[i].cpuc_pid_lock);
2627c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_core[i].cpuc_pid_lock);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * This is the timeout's callback for cleaning up the providers and their
2687c478bd9Sstevel@tonic-gate  * probes.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2717c478bd9Sstevel@tonic-gate static void
2727c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	fasttrap_provider_t **fpp, *fp;
2757c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
2767c478bd9Sstevel@tonic-gate 	dtrace_provider_id_t provid;
2777c478bd9Sstevel@tonic-gate 	int i, later;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	static volatile int in = 0;
2807c478bd9Sstevel@tonic-gate 	ASSERT(in == 0);
2817c478bd9Sstevel@tonic-gate 	in = 1;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
2847c478bd9Sstevel@tonic-gate 	while (fasttrap_cleanup_work) {
2857c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 0;
2867c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		later = 0;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		/*
2917c478bd9Sstevel@tonic-gate 		 * Iterate over all the providers trying to remove the marked
292b096140dSahl 		 * ones. If a provider is marked but not retired, we just
2937c478bd9Sstevel@tonic-gate 		 * have to take a crack at removing it -- it's no big deal if
2947c478bd9Sstevel@tonic-gate 		 * we can't.
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 		for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2977c478bd9Sstevel@tonic-gate 			bucket = &fasttrap_provs.fth_table[i];
2987c478bd9Sstevel@tonic-gate 			mutex_enter(&bucket->ftb_mtx);
2997c478bd9Sstevel@tonic-gate 			fpp = (fasttrap_provider_t **)&bucket->ftb_data;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			while ((fp = *fpp) != NULL) {
3027c478bd9Sstevel@tonic-gate 				if (!fp->ftp_marked) {
3037c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3047c478bd9Sstevel@tonic-gate 					continue;
3057c478bd9Sstevel@tonic-gate 				}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 				mutex_enter(&fp->ftp_mtx);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 				/*
310f498645aSahl 				 * If this provider has consumers actively
311f498645aSahl 				 * creating probes (ftp_ccount) or is a USDT
312f498645aSahl 				 * provider (ftp_mcount), we can't unregister
313f498645aSahl 				 * or even condense.
3147c478bd9Sstevel@tonic-gate 				 */
315ab9a77c7Sahl 				if (fp->ftp_ccount != 0 ||
316ab9a77c7Sahl 				    fp->ftp_mcount != 0) {
3177c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->ftp_mtx);
3187c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
3197c478bd9Sstevel@tonic-gate 					continue;
3207c478bd9Sstevel@tonic-gate 				}
3217c478bd9Sstevel@tonic-gate 
322b096140dSahl 				if (!fp->ftp_retired || fp->ftp_rcount != 0)
3237c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 				mutex_exit(&fp->ftp_mtx);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 				/*
3287c478bd9Sstevel@tonic-gate 				 * If we successfully unregister this
3297c478bd9Sstevel@tonic-gate 				 * provider we can remove it from the hash
3307c478bd9Sstevel@tonic-gate 				 * chain and free the memory. If our attempt
331b096140dSahl 				 * to unregister fails and this is a retired
3327c478bd9Sstevel@tonic-gate 				 * provider, increment our flag to try again
3337c478bd9Sstevel@tonic-gate 				 * pretty soon. If we've consumed more than
3347c478bd9Sstevel@tonic-gate 				 * half of our total permitted number of
3357c478bd9Sstevel@tonic-gate 				 * probes call dtrace_condense() to try to
3367c478bd9Sstevel@tonic-gate 				 * clean out the unenabled probes.
3377c478bd9Sstevel@tonic-gate 				 */
3387c478bd9Sstevel@tonic-gate 				provid = fp->ftp_provid;
3397c478bd9Sstevel@tonic-gate 				if (dtrace_unregister(provid) != 0) {
3407c478bd9Sstevel@tonic-gate 					if (fasttrap_total > fasttrap_max / 2)
3417c478bd9Sstevel@tonic-gate 						(void) dtrace_condense(provid);
3427c478bd9Sstevel@tonic-gate 					later += fp->ftp_marked;
3437c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3447c478bd9Sstevel@tonic-gate 				} else {
3457c478bd9Sstevel@tonic-gate 					*fpp = fp->ftp_next;
3467c478bd9Sstevel@tonic-gate 					fasttrap_provider_free(fp);
3477c478bd9Sstevel@tonic-gate 				}
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_timeout != 0);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	/*
358b096140dSahl 	 * If we were unable to remove a retired provider, try again after
3597c478bd9Sstevel@tonic-gate 	 * a second. This situation can occur in certain circumstances where
3607c478bd9Sstevel@tonic-gate 	 * providers cannot be unregistered even though they have no probes
3617c478bd9Sstevel@tonic-gate 	 * enabled because of an execution of dtrace -l or something similar.
3627c478bd9Sstevel@tonic-gate 	 * If the timeout has been disabled (set to 1 because we're trying
3637c478bd9Sstevel@tonic-gate 	 * to detach), we set fasttrap_cleanup_work to ensure that we'll
3647c478bd9Sstevel@tonic-gate 	 * get a chance to do that work if and when the timeout is reenabled
3657c478bd9Sstevel@tonic-gate 	 * (if detach fails).
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (later > 0 && fasttrap_timeout != (timeout_id_t)1)
3687c478bd9Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz);
3697c478bd9Sstevel@tonic-gate 	else if (later > 0)
3707c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 1;
3717c478bd9Sstevel@tonic-gate 	else
3727c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3757c478bd9Sstevel@tonic-gate 	in = 0;
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * Activates the asynchronous cleanup mechanism.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate static void
3827c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup(void)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
3857c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 1;
3867c478bd9Sstevel@tonic-gate 	if (fasttrap_timeout == 0)
3877c478bd9Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1);
3887c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * This is called from cfork() via dtrace_fasttrap_fork(). The child
3937c478bd9Sstevel@tonic-gate  * process's address space is a (roughly) a copy of the parent process's so
3947c478bd9Sstevel@tonic-gate  * we have to remove all the instrumentation we had previously enabled in the
3957c478bd9Sstevel@tonic-gate  * parent.
3967c478bd9Sstevel@tonic-gate  */
3977c478bd9Sstevel@tonic-gate static void
3987c478bd9Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	pid_t ppid = p->p_pid;
4017c478bd9Sstevel@tonic-gate 	int i;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	ASSERT(curproc == p);
4047c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
4057c478bd9Sstevel@tonic-gate 	ASSERT(p->p_dtrace_count > 0);
4067c478bd9Sstevel@tonic-gate 	ASSERT(cp->p_dtrace_count == 0);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * This would be simpler and faster if we maintained per-process
4107c478bd9Sstevel@tonic-gate 	 * hash tables of enabled tracepoints. It could, however, potentially
4117c478bd9Sstevel@tonic-gate 	 * slow down execution of a tracepoint since we'd need to go
4127c478bd9Sstevel@tonic-gate 	 * through two levels of indirection. In the future, we should
4137c478bd9Sstevel@tonic-gate 	 * consider either maintaining per-process ancillary lists of
4147c478bd9Sstevel@tonic-gate 	 * enabled tracepoints or hanging a pointer to a per-process hash
4157c478bd9Sstevel@tonic-gate 	 * table of enabled tracepoints off the proc structure.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * We don't have to worry about the child process disappearing
4207c478bd9Sstevel@tonic-gate 	 * because we're in fork().
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4237c478bd9Sstevel@tonic-gate 	sprlock_proc(cp);
4247c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * Iterate over every tracepoint looking for ones that belong to the
4287c478bd9Sstevel@tonic-gate 	 * parent process, and remove each from the child process.
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
4317c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
4327c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
4357c478bd9Sstevel@tonic-gate 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
436b096140dSahl 			if (tp->ftt_pid == ppid &&
437*038dc6b3Sahl 			    tp->ftt_proc->ftpc_acount != 0) {
4387c478bd9Sstevel@tonic-gate 				int ret = fasttrap_tracepoint_remove(cp, tp);
4397c478bd9Sstevel@tonic-gate 				ASSERT(ret == 0);
4407c478bd9Sstevel@tonic-gate 			}
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4467c478bd9Sstevel@tonic-gate 	sprunlock(cp);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * This is called from proc_exit() or from exec_common() if p_dtrace_probes
4517c478bd9Sstevel@tonic-gate  * is set on the proc structure to indicate that there is a pid provider
4527c478bd9Sstevel@tonic-gate  * associated with this process.
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate static void
4557c478bd9Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);
4587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * We clean up the pid provider for this process here; user-land
4647c478bd9Sstevel@tonic-gate 	 * static probes are handled by the meta-provider remove entry point.
4657c478bd9Sstevel@tonic-gate 	 */
466dafb5540Sahl 	fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4737c478bd9Sstevel@tonic-gate static void
4747c478bd9Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * There are no "default" pid probes.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate static int
4827c478bd9Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, *new_tp = NULL;
4857c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
4867c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
4877c478bd9Sstevel@tonic-gate 	pid_t pid;
4887c478bd9Sstevel@tonic-gate 	uintptr_t pc;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
4937c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
4947c478bd9Sstevel@tonic-gate 	id = &probe->ftp_tps[index].fit_id;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * Before we make any modifications, make sure we've imposed a barrier
5027c478bd9Sstevel@tonic-gate 	 * on the generation in which this probe was last modified.
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	fasttrap_mod_barrier(probe->ftp_gen);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * If the tracepoint has already been enabled, just add our id to the
5107c478bd9Sstevel@tonic-gate 	 * list of interested probes. This may be our second time through
5117c478bd9Sstevel@tonic-gate 	 * this path in which case we'll have constructed the tracepoint we'd
5127c478bd9Sstevel@tonic-gate 	 * like to install. If we can't find a match, and have an allocated
5137c478bd9Sstevel@tonic-gate 	 * tracepoint ready to go, enable that one now.
5147c478bd9Sstevel@tonic-gate 	 *
515ac448965Sahl 	 * A tracepoint whose process is defunct is also considered defunct.
5167c478bd9Sstevel@tonic-gate 	 */
5177c478bd9Sstevel@tonic-gate again:
5187c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
5197c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
5207c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
521*038dc6b3Sahl 		    tp->ftt_proc->ftpc_acount == 0)
5227c478bd9Sstevel@tonic-gate 			continue;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Now that we've found a matching tracepoint, it would be
5267c478bd9Sstevel@tonic-gate 		 * a decent idea to confirm that the tracepoint is still
5277c478bd9Sstevel@tonic-gate 		 * enabled and the trap instruction hasn't been overwritten.
5287c478bd9Sstevel@tonic-gate 		 * Since this is a little hairy, we'll punt for now.
5297c478bd9Sstevel@tonic-gate 		 */
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		/*
5327c478bd9Sstevel@tonic-gate 		 * This can't be the first interested probe. We don't have
5337c478bd9Sstevel@tonic-gate 		 * to worry about another thread being in the midst of
5347c478bd9Sstevel@tonic-gate 		 * deleting this tracepoint (which would be the only valid
5357c478bd9Sstevel@tonic-gate 		 * reason for a tracepoint to have no interested probes)
5367c478bd9Sstevel@tonic-gate 		 * since we're holding P_PR_LOCK for this process.
5377c478bd9Sstevel@tonic-gate 		 */
5387c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
5397c478bd9Sstevel@tonic-gate 
540ac448965Sahl 		switch (id->fti_ptype) {
541ac448965Sahl 		case DTFTP_ENTRY:
542ac448965Sahl 		case DTFTP_OFFSETS:
543ac448965Sahl 		case DTFTP_IS_ENABLED:
5447c478bd9Sstevel@tonic-gate 			id->fti_next = tp->ftt_ids;
5457c478bd9Sstevel@tonic-gate 			membar_producer();
5467c478bd9Sstevel@tonic-gate 			tp->ftt_ids = id;
5477c478bd9Sstevel@tonic-gate 			membar_producer();
548ac448965Sahl 			break;
549ac448965Sahl 
550ac448965Sahl 		case DTFTP_RETURN:
551ac448965Sahl 		case DTFTP_POST_OFFSETS:
552ac448965Sahl 			id->fti_next = tp->ftt_retids;
553ac448965Sahl 			membar_producer();
554ac448965Sahl 			tp->ftt_retids = id;
555ac448965Sahl 			membar_producer();
556ac448965Sahl 			break;
557ac448965Sahl 
558ac448965Sahl 		default:
559ac448965Sahl 			ASSERT(0);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		if (new_tp != NULL) {
5657c478bd9Sstevel@tonic-gate 			new_tp->ftt_ids = NULL;
5667c478bd9Sstevel@tonic-gate 			new_tp->ftt_retids = NULL;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		return (0);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * If we have a good tracepoint ready to go, install it now while
5747c478bd9Sstevel@tonic-gate 	 * we have the lock held and no one can screw with us.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	if (new_tp != NULL) {
5778f5db3e7Sahl 		int rc = 0;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		new_tp->ftt_next = bucket->ftb_data;
5807c478bd9Sstevel@tonic-gate 		membar_producer();
5817c478bd9Sstevel@tonic-gate 		bucket->ftb_data = new_tp;
5827c478bd9Sstevel@tonic-gate 		membar_producer();
5837c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		/*
5868f5db3e7Sahl 		 * Activate the tracepoint in the ISA-specific manner.
5878f5db3e7Sahl 		 * If this fails, we need to report the failure, but
5888f5db3e7Sahl 		 * indicate that this tracepoint must still be disabled
5898f5db3e7Sahl 		 * by calling fasttrap_tracepoint_disable().
5907c478bd9Sstevel@tonic-gate 		 */
5918f5db3e7Sahl 		if (fasttrap_tracepoint_install(p, new_tp) != 0)
5928f5db3e7Sahl 			rc = FASTTRAP_ENABLE_PARTIAL;
5938f5db3e7Sahl 
5947c478bd9Sstevel@tonic-gate 		/*
5958f5db3e7Sahl 		 * Increment the count of the number of tracepoints active in
5968f5db3e7Sahl 		 * the victim process.
5977c478bd9Sstevel@tonic-gate 		 */
5987c478bd9Sstevel@tonic-gate 		ASSERT(p->p_proc_flag & P_PR_LOCK);
5997c478bd9Sstevel@tonic-gate 		p->p_dtrace_count++;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		return (rc);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/*
6077c478bd9Sstevel@tonic-gate 	 * Initialize the tracepoint that's been preallocated with the probe.
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	new_tp = probe->ftp_tps[index].fit_tp;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pid == pid);
6127c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pc == pc);
613b096140dSahl 	ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
6147c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_ids == NULL);
6157c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_retids == NULL);
6167c478bd9Sstevel@tonic-gate 
617ac448965Sahl 	switch (id->fti_ptype) {
618ac448965Sahl 	case DTFTP_ENTRY:
619ac448965Sahl 	case DTFTP_OFFSETS:
620ac448965Sahl 	case DTFTP_IS_ENABLED:
6217c478bd9Sstevel@tonic-gate 		id->fti_next = NULL;
6227c478bd9Sstevel@tonic-gate 		new_tp->ftt_ids = id;
623ac448965Sahl 		break;
624ac448965Sahl 
625ac448965Sahl 	case DTFTP_RETURN:
626ac448965Sahl 	case DTFTP_POST_OFFSETS:
627ac448965Sahl 		id->fti_next = NULL;
628ac448965Sahl 		new_tp->ftt_retids = id;
629ac448965Sahl 		break;
630ac448965Sahl 
631ac448965Sahl 	default:
632ac448965Sahl 		ASSERT(0);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6368f5db3e7Sahl 	 * If the ISA-dependent initialization goes to plan, go back to the
6377c478bd9Sstevel@tonic-gate 	 * beginning and try to install this freshly made tracepoint.
6387c478bd9Sstevel@tonic-gate 	 */
639ac448965Sahl 	if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
6407c478bd9Sstevel@tonic-gate 		goto again;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	new_tp->ftt_ids = NULL;
6437c478bd9Sstevel@tonic-gate 	new_tp->ftt_retids = NULL;
6447c478bd9Sstevel@tonic-gate 
6458f5db3e7Sahl 	return (FASTTRAP_ENABLE_FAIL);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static void
6497c478bd9Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
6527c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
6537c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t **pp, *tp;
6547c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id, **idp;
6557c478bd9Sstevel@tonic-gate 	pid_t pid;
6567c478bd9Sstevel@tonic-gate 	uintptr_t pc;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
6617c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
662ac448965Sahl 	id = &probe->ftp_tps[index].fit_id;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * Find the tracepoint and make sure that our id is one of the
6687c478bd9Sstevel@tonic-gate 	 * ones registered with it.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6717c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
6727c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6737c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
674b096140dSahl 		    tp->ftt_proc == provider->ftp_proc)
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * If we somehow lost this tracepoint, we're in a world of hurt.
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
6827c478bd9Sstevel@tonic-gate 
683ac448965Sahl 	switch (id->fti_ptype) {
684ac448965Sahl 	case DTFTP_ENTRY:
685ac448965Sahl 	case DTFTP_OFFSETS:
686ac448965Sahl 	case DTFTP_IS_ENABLED:
6877c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL);
6887c478bd9Sstevel@tonic-gate 		idp = &tp->ftt_ids;
689ac448965Sahl 		break;
690ac448965Sahl 
691ac448965Sahl 	case DTFTP_RETURN:
692ac448965Sahl 	case DTFTP_POST_OFFSETS:
693ac448965Sahl 		ASSERT(tp->ftt_retids != NULL);
694ac448965Sahl 		idp = &tp->ftt_retids;
695ac448965Sahl 		break;
696ac448965Sahl 
697ac448965Sahl 	default:
698ac448965Sahl 		ASSERT(0);
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	while ((*idp)->fti_probe != probe) {
7027c478bd9Sstevel@tonic-gate 		idp = &(*idp)->fti_next;
7037c478bd9Sstevel@tonic-gate 		ASSERT(*idp != NULL);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	id = *idp;
7077c478bd9Sstevel@tonic-gate 	*idp = id->fti_next;
7087c478bd9Sstevel@tonic-gate 	membar_producer();
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	ASSERT(id->fti_probe == probe);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	/*
7137c478bd9Sstevel@tonic-gate 	 * If there are other registered enablings of this tracepoint, we're
7147c478bd9Sstevel@tonic-gate 	 * all done, but if this was the last probe assocated with this
7157c478bd9Sstevel@tonic-gate 	 * this tracepoint, we need to remove and free it.
7167c478bd9Sstevel@tonic-gate 	 */
7177c478bd9Sstevel@tonic-gate 	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * If the current probe's tracepoint is in use, swap it
7217c478bd9Sstevel@tonic-gate 		 * for an unused tracepoint.
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		if (tp == probe->ftp_tps[index].fit_tp) {
7247c478bd9Sstevel@tonic-gate 			fasttrap_probe_t *tmp_probe;
7257c478bd9Sstevel@tonic-gate 			fasttrap_tracepoint_t **tmp_tp;
7267c478bd9Sstevel@tonic-gate 			uint_t tmp_index;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 			if (tp->ftt_ids != NULL) {
7297c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_ids->fti_probe;
73073427c57Sahl 				/* LINTED - alignment */
7317c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
7327c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7337c478bd9Sstevel@tonic-gate 			} else {
7347c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_retids->fti_probe;
73573427c57Sahl 				/* LINTED - alignment */
7367c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
7377c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != NULL);
7417c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
7427c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_ids == NULL);
7437c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_retids == NULL);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 			probe->ftp_tps[index].fit_tp = *tmp_tp;
7467c478bd9Sstevel@tonic-gate 			*tmp_tp = tp;
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * Tag the modified probe with the generation in which it was
7537c478bd9Sstevel@tonic-gate 		 * changed.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		probe->ftp_gen = fasttrap_mod_gen;
7567c478bd9Sstevel@tonic-gate 		return;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	/*
7627c478bd9Sstevel@tonic-gate 	 * We can't safely remove the tracepoint from the set of active
7637c478bd9Sstevel@tonic-gate 	 * tracepoints until we've actually removed the fasttrap instruction
7647c478bd9Sstevel@tonic-gate 	 * from the process's text. We can, however, operate on this
7657c478bd9Sstevel@tonic-gate 	 * tracepoint secure in the knowledge that no other thread is going to
7667c478bd9Sstevel@tonic-gate 	 * be looking at it since we hold P_PR_LOCK on the process if it's
7677c478bd9Sstevel@tonic-gate 	 * live or we hold the provider lock on the process if it's dead and
7687c478bd9Sstevel@tonic-gate 	 * gone.
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/*
7727c478bd9Sstevel@tonic-gate 	 * We only need to remove the actual instruction if we're looking
7737c478bd9Sstevel@tonic-gate 	 * at an existing process
7747c478bd9Sstevel@tonic-gate 	 */
7757c478bd9Sstevel@tonic-gate 	if (p != NULL) {
7767c478bd9Sstevel@tonic-gate 		/*
7777c478bd9Sstevel@tonic-gate 		 * If we fail to restore the instruction we need to kill
7787c478bd9Sstevel@tonic-gate 		 * this process since it's in a completely unrecoverable
7797c478bd9Sstevel@tonic-gate 		 * state.
7807c478bd9Sstevel@tonic-gate 		 */
7817c478bd9Sstevel@tonic-gate 		if (fasttrap_tracepoint_remove(p, tp) != 0)
7827c478bd9Sstevel@tonic-gate 			fasttrap_sigtrap(p, NULL, pc);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/*
7857c478bd9Sstevel@tonic-gate 		 * Decrement the count of the number of tracepoints active
7867c478bd9Sstevel@tonic-gate 		 * in the victim process.
7877c478bd9Sstevel@tonic-gate 		 */
7887c478bd9Sstevel@tonic-gate 		ASSERT(p->p_proc_flag & P_PR_LOCK);
7897c478bd9Sstevel@tonic-gate 		p->p_dtrace_count--;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/*
7937c478bd9Sstevel@tonic-gate 	 * Remove the probe from the hash table of active tracepoints.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
7967c478bd9Sstevel@tonic-gate 	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
7977c478bd9Sstevel@tonic-gate 	ASSERT(*pp != NULL);
7987c478bd9Sstevel@tonic-gate 	while (*pp != tp) {
7997c478bd9Sstevel@tonic-gate 		pp = &(*pp)->ftt_next;
8007c478bd9Sstevel@tonic-gate 		ASSERT(*pp != NULL);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	*pp = tp->ftt_next;
8047c478bd9Sstevel@tonic-gate 	membar_producer();
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * Tag the modified probe with the generation in which it was changed.
8107c478bd9Sstevel@tonic-gate 	 */
8117c478bd9Sstevel@tonic-gate 	probe->ftp_gen = fasttrap_mod_gen;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static void
815f498645aSahl fasttrap_enable_callbacks(void)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * We don't have to play the rw lock game here because we're
8197c478bd9Sstevel@tonic-gate 	 * providing something rather than taking something away --
8207c478bd9Sstevel@tonic-gate 	 * we can be sure that no threads have tried to follow this
8217c478bd9Sstevel@tonic-gate 	 * function pointer yet.
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
824f498645aSahl 	if (fasttrap_pid_count == 0) {
825f498645aSahl 		ASSERT(dtrace_pid_probe_ptr == NULL);
826f498645aSahl 		ASSERT(dtrace_return_probe_ptr == NULL);
827f498645aSahl 		dtrace_pid_probe_ptr = &fasttrap_pid_probe;
828f498645aSahl 		dtrace_return_probe_ptr = &fasttrap_return_probe;
8297c478bd9Sstevel@tonic-gate 	}
830f498645aSahl 	ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
831f498645aSahl 	ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
832f498645aSahl 	fasttrap_pid_count++;
8337c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate static void
837f498645aSahl fasttrap_disable_callbacks(void)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
842f498645aSahl 	ASSERT(fasttrap_pid_count > 0);
843f498645aSahl 	fasttrap_pid_count--;
844f498645aSahl 	if (fasttrap_pid_count == 0) {
8457c478bd9Sstevel@tonic-gate 		cpu_t *cur, *cpu = CPU;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8487c478bd9Sstevel@tonic-gate 		    cur = cur->cpu_next_onln) {
8497c478bd9Sstevel@tonic-gate 			rw_enter(&cur->cpu_ft_lock, RW_WRITER);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 
852f498645aSahl 		dtrace_pid_probe_ptr = NULL;
853f498645aSahl 		dtrace_return_probe_ptr = NULL;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8567c478bd9Sstevel@tonic-gate 		    cur = cur->cpu_next_onln) {
8577c478bd9Sstevel@tonic-gate 			rw_exit(&cur->cpu_ft_lock);
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8647c478bd9Sstevel@tonic-gate static void
8657c478bd9Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
8687c478bd9Sstevel@tonic-gate 	proc_t *p;
8698f5db3e7Sahl 	int i, rc;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
8727c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
8737c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
8747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * Increment the count of enabled probes on this probe's provider;
8787c478bd9Sstevel@tonic-gate 	 * the provider can't go away while the probe still exists. We
8797c478bd9Sstevel@tonic-gate 	 * must increment this even if we aren't able to properly enable
8807c478bd9Sstevel@tonic-gate 	 * this probe.
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	mutex_enter(&probe->ftp_prov->ftp_mtx);
8837c478bd9Sstevel@tonic-gate 	probe->ftp_prov->ftp_rcount++;
8847c478bd9Sstevel@tonic-gate 	mutex_exit(&probe->ftp_prov->ftp_mtx);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
887ab9a77c7Sahl 	 * If this probe's provider is retired (meaning it was valid in a
888ab9a77c7Sahl 	 * previously exec'ed incarnation of this address space), bail out. The
889ab9a77c7Sahl 	 * provider can't go away while we're in this code path.
8907c478bd9Sstevel@tonic-gate 	 */
891ab9a77c7Sahl 	if (probe->ftp_prov->ftp_retired)
8927c478bd9Sstevel@tonic-gate 		return;
8937c478bd9Sstevel@tonic-gate 
894ab9a77c7Sahl 	/*
895ab9a77c7Sahl 	 * If we can't find the process, it may be that we're in the context of
896ab9a77c7Sahl 	 * a fork in which the traced process is being born and we're copying
897ab9a77c7Sahl 	 * USDT probes. Otherwise, the process is gone so bail.
898ab9a77c7Sahl 	 */
899ab9a77c7Sahl 	if ((p = sprlock(probe->ftp_pid)) == NULL) {
900ab9a77c7Sahl 		if ((curproc->p_flag & SFORKING) == 0)
901ab9a77c7Sahl 			return;
902ab9a77c7Sahl 
903ab9a77c7Sahl 		mutex_enter(&pidlock);
904ab9a77c7Sahl 		p = prfind(probe->ftp_pid);
905ab9a77c7Sahl 
906ab9a77c7Sahl 		/*
907ab9a77c7Sahl 		 * Confirm that curproc is indeed forking the process in which
908ab9a77c7Sahl 		 * we're trying to enable probes.
909ab9a77c7Sahl 		 */
910ab9a77c7Sahl 		ASSERT(p != NULL);
911ab9a77c7Sahl 		ASSERT(p->p_parent == curproc);
912ab9a77c7Sahl 		ASSERT(p->p_stat == SIDL);
913ab9a77c7Sahl 
914ab9a77c7Sahl 		mutex_enter(&p->p_lock);
915ab9a77c7Sahl 		mutex_exit(&pidlock);
916ab9a77c7Sahl 
917ab9a77c7Sahl 		sprlock_proc(p);
918ab9a77c7Sahl 	}
919ab9a77c7Sahl 
9207c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
9217c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
924f498645aSahl 	 * We have to enable the trap entry point before any user threads have
9257c478bd9Sstevel@tonic-gate 	 * the chance to execute the trap instruction we're about to place
9267c478bd9Sstevel@tonic-gate 	 * in their process's text.
9277c478bd9Sstevel@tonic-gate 	 */
928f498645aSahl 	fasttrap_enable_callbacks();
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * Enable all the tracepoints and add this probe's id to each
9327c478bd9Sstevel@tonic-gate 	 * tracepoint's list of active probes.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
9358f5db3e7Sahl 		if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
9368f5db3e7Sahl 			/*
9378f5db3e7Sahl 			 * If enabling the tracepoint failed completely,
9388f5db3e7Sahl 			 * we don't have to disable it; if the failure
9398f5db3e7Sahl 			 * was only partial we must disable it.
9408f5db3e7Sahl 			 */
9418f5db3e7Sahl 			if (rc == FASTTRAP_ENABLE_FAIL)
9428f5db3e7Sahl 				i--;
9438f5db3e7Sahl 			else
9448f5db3e7Sahl 				ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
9458f5db3e7Sahl 
9467c478bd9Sstevel@tonic-gate 			/*
9477c478bd9Sstevel@tonic-gate 			 * Back up and pull out all the tracepoints we've
9487c478bd9Sstevel@tonic-gate 			 * created so far for this probe.
9497c478bd9Sstevel@tonic-gate 			 */
9508c642573Sahl 			while (i >= 0) {
9517c478bd9Sstevel@tonic-gate 				fasttrap_tracepoint_disable(p, probe, i);
9528c642573Sahl 				i--;
9537c478bd9Sstevel@tonic-gate 			}
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9567c478bd9Sstevel@tonic-gate 			sprunlock(p);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			/*
9597c478bd9Sstevel@tonic-gate 			 * Since we're not actually enabling this probe,
9607c478bd9Sstevel@tonic-gate 			 * drop our reference on the trap table entry.
9617c478bd9Sstevel@tonic-gate 			 */
962f498645aSahl 			fasttrap_disable_callbacks();
9637c478bd9Sstevel@tonic-gate 			return;
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
9687c478bd9Sstevel@tonic-gate 	sprunlock(p);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 1;
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9747c478bd9Sstevel@tonic-gate static void
9757c478bd9Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
9787c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
9797c478bd9Sstevel@tonic-gate 	proc_t *p;
9807c478bd9Sstevel@tonic-gate 	int i, whack = 0;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * We won't be able to acquire a /proc-esque lock on the process
9867c478bd9Sstevel@tonic-gate 	 * iff the process is dead and gone. In this case, we rely on the
9877c478bd9Sstevel@tonic-gate 	 * provider lock as a point of mutual exclusion to prevent other
9887c478bd9Sstevel@tonic-gate 	 * DTrace consumers from disabling this probe.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	if ((p = sprlock(probe->ftp_pid)) != NULL) {
9917c478bd9Sstevel@tonic-gate 		ASSERT(!(p->p_flag & SVFORK));
9927c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/*
99873427c57Sahl 	 * Disable all the associated tracepoints (for fully enabled probes).
9997c478bd9Sstevel@tonic-gate 	 */
100073427c57Sahl 	if (probe->ftp_enabled) {
10017c478bd9Sstevel@tonic-gate 		for (i = 0; i < probe->ftp_ntps; i++) {
10027c478bd9Sstevel@tonic-gate 			fasttrap_tracepoint_disable(p, probe, i);
10037c478bd9Sstevel@tonic-gate 		}
100473427c57Sahl 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount > 0);
10077c478bd9Sstevel@tonic-gate 	provider->ftp_rcount--;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (p != NULL) {
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * Even though we may not be able to remove it entirely, we
1012b096140dSahl 		 * mark this retired provider to get a chance to remove some
10137c478bd9Sstevel@tonic-gate 		 * of the associated probes.
10147c478bd9Sstevel@tonic-gate 		 */
1015b096140dSahl 		if (provider->ftp_retired && !provider->ftp_marked)
10167c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10177c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
10207c478bd9Sstevel@tonic-gate 		sprunlock(p);
10217c478bd9Sstevel@tonic-gate 	} else {
10227c478bd9Sstevel@tonic-gate 		/*
10237c478bd9Sstevel@tonic-gate 		 * If the process is dead, we're just waiting for the
10247c478bd9Sstevel@tonic-gate 		 * last probe to be disabled to be able to free it.
10257c478bd9Sstevel@tonic-gate 		 */
10267c478bd9Sstevel@tonic-gate 		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
10277c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10287c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (whack)
10327c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
10337c478bd9Sstevel@tonic-gate 
103473427c57Sahl 	if (!probe->ftp_enabled)
103573427c57Sahl 		return;
103673427c57Sahl 
10377c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 0;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1040f498645aSahl 	fasttrap_disable_callbacks();
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10447c478bd9Sstevel@tonic-gate static void
10457c478bd9Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
10467c478bd9Sstevel@tonic-gate     dtrace_argdesc_t *desc)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
10497c478bd9Sstevel@tonic-gate 	char *str;
10509e8164f5Sahl 	int i, ndx;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	desc->dtargd_native[0] = '\0';
10537c478bd9Sstevel@tonic-gate 	desc->dtargd_xlate[0] = '\0';
10547c478bd9Sstevel@tonic-gate 
1055b096140dSahl 	if (probe->ftp_prov->ftp_retired != 0 ||
10567c478bd9Sstevel@tonic-gate 	    desc->dtargd_ndx >= probe->ftp_nargs) {
10577c478bd9Sstevel@tonic-gate 		desc->dtargd_ndx = DTRACE_ARGNONE;
10587c478bd9Sstevel@tonic-gate 		return;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10619e8164f5Sahl 	ndx = (probe->ftp_argmap != NULL) ?
10629e8164f5Sahl 	    probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	str = probe->ftp_ntypes;
10659e8164f5Sahl 	for (i = 0; i < ndx; i++) {
10667c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
10707c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_native, str);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	if (probe->ftp_xtypes == NULL)
10737c478bd9Sstevel@tonic-gate 		return;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	str = probe->ftp_xtypes;
10767c478bd9Sstevel@tonic-gate 	for (i = 0; i < desc->dtargd_ndx; i++) {
10777c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
10817c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_xlate, str);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10857c478bd9Sstevel@tonic-gate static void
10867c478bd9Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
10897c478bd9Sstevel@tonic-gate 	int i;
10907c478bd9Sstevel@tonic-gate 	size_t size;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
10937c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
10947c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_total >= probe->ftp_ntps);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
1097ab9a77c7Sahl 	size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
11007c478bd9Sstevel@tonic-gate 		fasttrap_mod_barrier(probe->ftp_gen);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
11037c478bd9Sstevel@tonic-gate 		kmem_free(probe->ftp_tps[i].fit_tp,
11047c478bd9Sstevel@tonic-gate 		    sizeof (fasttrap_tracepoint_t));
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	kmem_free(probe, size);
11087c478bd9Sstevel@tonic-gate }
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = {
11127c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11137c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11147c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11157c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11167c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11177c478bd9Sstevel@tonic-gate };
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate static dtrace_pops_t pid_pops = {
11207c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
11217c478bd9Sstevel@tonic-gate 	NULL,
11227c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
11237c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
11247c478bd9Sstevel@tonic-gate 	NULL,
11257c478bd9Sstevel@tonic-gate 	NULL,
11267c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
1127f498645aSahl 	fasttrap_pid_getarg,
11287c478bd9Sstevel@tonic-gate 	NULL,
11297c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
11307c478bd9Sstevel@tonic-gate };
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate static dtrace_pops_t usdt_pops = {
11337c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
11347c478bd9Sstevel@tonic-gate 	NULL,
11357c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
11367c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
11377c478bd9Sstevel@tonic-gate 	NULL,
11387c478bd9Sstevel@tonic-gate 	NULL,
11397c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
11407c478bd9Sstevel@tonic-gate 	fasttrap_usdt_getarg,
11417c478bd9Sstevel@tonic-gate 	NULL,
11427c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
11437c478bd9Sstevel@tonic-gate };
11447c478bd9Sstevel@tonic-gate 
1145b096140dSahl static fasttrap_proc_t *
1146b096140dSahl fasttrap_proc_lookup(pid_t pid)
1147b096140dSahl {
1148b096140dSahl 	fasttrap_bucket_t *bucket;
1149b096140dSahl 	fasttrap_proc_t *fprc, *new_fprc;
1150b096140dSahl 
1151b096140dSahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1152b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1153b096140dSahl 
1154b096140dSahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1155*038dc6b3Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1156b096140dSahl 			mutex_enter(&fprc->ftpc_mtx);
1157b096140dSahl 			mutex_exit(&bucket->ftb_mtx);
1158*038dc6b3Sahl 			fprc->ftpc_rcount++;
1159*038dc6b3Sahl 			atomic_add_64(&fprc->ftpc_acount, 1);
1160b096140dSahl 			mutex_exit(&fprc->ftpc_mtx);
1161b096140dSahl 
1162b096140dSahl 			return (fprc);
1163b096140dSahl 		}
1164b096140dSahl 	}
1165b096140dSahl 
1166b096140dSahl 	/*
1167b096140dSahl 	 * Drop the bucket lock so we don't try to perform a sleeping
1168b096140dSahl 	 * allocation under it.
1169b096140dSahl 	 */
1170b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1171b096140dSahl 
1172b096140dSahl 	new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1173b096140dSahl 	new_fprc->ftpc_pid = pid;
1174*038dc6b3Sahl 	new_fprc->ftpc_rcount = 1;
1175*038dc6b3Sahl 	new_fprc->ftpc_acount = 1;
1176b096140dSahl 
1177b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1178b096140dSahl 
1179b096140dSahl 	/*
1180b096140dSahl 	 * Take another lap through the list to make sure a proc hasn't
1181b096140dSahl 	 * been created for this pid while we weren't under the bucket lock.
1182b096140dSahl 	 */
1183b096140dSahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1184*038dc6b3Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1185b096140dSahl 			mutex_enter(&fprc->ftpc_mtx);
1186b096140dSahl 			mutex_exit(&bucket->ftb_mtx);
1187*038dc6b3Sahl 			fprc->ftpc_rcount++;
1188*038dc6b3Sahl 			atomic_add_64(&fprc->ftpc_acount, 1);
1189b096140dSahl 			mutex_exit(&fprc->ftpc_mtx);
1190b096140dSahl 
1191b096140dSahl 			kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1192b096140dSahl 
1193b096140dSahl 			return (fprc);
1194b096140dSahl 		}
1195b096140dSahl 	}
1196b096140dSahl 
1197b096140dSahl 	new_fprc->ftpc_next = bucket->ftb_data;
1198b096140dSahl 	bucket->ftb_data = new_fprc;
1199b096140dSahl 
1200b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1201b096140dSahl 
1202b096140dSahl 	return (new_fprc);
1203b096140dSahl }
1204b096140dSahl 
1205b096140dSahl static void
1206b096140dSahl fasttrap_proc_release(fasttrap_proc_t *proc)
1207b096140dSahl {
1208b096140dSahl 	fasttrap_bucket_t *bucket;
1209b096140dSahl 	fasttrap_proc_t *fprc, **fprcp;
1210b096140dSahl 	pid_t pid = proc->ftpc_pid;
1211b096140dSahl 
1212b096140dSahl 	mutex_enter(&proc->ftpc_mtx);
1213b096140dSahl 
1214*038dc6b3Sahl 	ASSERT(proc->ftpc_rcount != 0);
1215b096140dSahl 
1216*038dc6b3Sahl 	if (--proc->ftpc_rcount != 0) {
1217b096140dSahl 		mutex_exit(&proc->ftpc_mtx);
1218b096140dSahl 		return;
1219b096140dSahl 	}
1220b096140dSahl 
1221b096140dSahl 	mutex_exit(&proc->ftpc_mtx);
1222b096140dSahl 
1223*038dc6b3Sahl 	/*
1224*038dc6b3Sahl 	 * There should definitely be no live providers associated with this
1225*038dc6b3Sahl 	 * process at this point.
1226*038dc6b3Sahl 	 */
1227*038dc6b3Sahl 	ASSERT(proc->ftpc_acount == 0);
1228*038dc6b3Sahl 
1229b096140dSahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1230b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1231b096140dSahl 
1232b096140dSahl 	fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1233b096140dSahl 	while ((fprc = *fprcp) != NULL) {
1234b096140dSahl 		if (fprc == proc)
1235b096140dSahl 			break;
1236b096140dSahl 
1237b096140dSahl 		fprcp = &fprc->ftpc_next;
1238b096140dSahl 	}
1239b096140dSahl 
1240b096140dSahl 	/*
1241b096140dSahl 	 * Something strange has happened if we can't find the proc.
1242b096140dSahl 	 */
1243b096140dSahl 	ASSERT(fprc != NULL);
1244b096140dSahl 
1245b096140dSahl 	*fprcp = fprc->ftpc_next;
1246b096140dSahl 
1247b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1248b096140dSahl 
1249b096140dSahl 	kmem_free(fprc, sizeof (fasttrap_proc_t));
1250b096140dSahl }
1251b096140dSahl 
12527c478bd9Sstevel@tonic-gate /*
12537c478bd9Sstevel@tonic-gate  * Lookup a fasttrap-managed provider based on its name and associated pid.
12547c478bd9Sstevel@tonic-gate  * If the pattr argument is non-NULL, this function instantiates the provider
12557c478bd9Sstevel@tonic-gate  * if it doesn't exist otherwise it returns NULL. The provider is returned
12567c478bd9Sstevel@tonic-gate  * with its lock held.
12577c478bd9Sstevel@tonic-gate  */
12587c478bd9Sstevel@tonic-gate static fasttrap_provider_t *
12597c478bd9Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name,
12607c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *pattr)
12617c478bd9Sstevel@tonic-gate {
12627c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *fp, *new_fp = NULL;
12637c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
12647c478bd9Sstevel@tonic-gate 	char provname[DTRACE_PROVNAMELEN];
12657c478bd9Sstevel@tonic-gate 	proc_t *p;
1266ad4023c4Sdp 	cred_t *cred;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1269dafb5540Sahl 	ASSERT(pattr != NULL);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
12727c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * Take a lap through the list and return the match if we find it.
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
12787c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1279b096140dSahl 		    !fp->ftp_retired) {
12807c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
12817c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
12827c478bd9Sstevel@tonic-gate 			return (fp);
12837c478bd9Sstevel@tonic-gate 		}
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	/*
12877c478bd9Sstevel@tonic-gate 	 * Drop the bucket lock so we don't try to perform a sleeping
12887c478bd9Sstevel@tonic-gate 	 * allocation under it.
12897c478bd9Sstevel@tonic-gate 	 */
12907c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
12917c478bd9Sstevel@tonic-gate 
1292b096140dSahl 	/*
12937c478bd9Sstevel@tonic-gate 	 * Make sure the process exists, isn't a child created as the result
1294ad4023c4Sdp 	 * of a vfork(2), and isn't a zombie (but may be in fork).
12957c478bd9Sstevel@tonic-gate 	 */
12967c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
129797eda132Sraf 	if ((p = prfind(pid)) == NULL) {
12987c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
12997c478bd9Sstevel@tonic-gate 		return (NULL);
13007c478bd9Sstevel@tonic-gate 	}
13017c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
13027c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
130397eda132Sraf 	if (p->p_flag & (SVFORK | SEXITING)) {
130497eda132Sraf 		mutex_exit(&p->p_lock);
130597eda132Sraf 		return (NULL);
130697eda132Sraf 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Increment p_dtrace_probes so that the process knows to inform us
13107c478bd9Sstevel@tonic-gate 	 * when it exits or execs. fasttrap_provider_free() decrements this
13117c478bd9Sstevel@tonic-gate 	 * when we're done with this provider.
13127c478bd9Sstevel@tonic-gate 	 */
13137c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes++;
13147c478bd9Sstevel@tonic-gate 
1315ad4023c4Sdp 	/*
1316ad4023c4Sdp 	 * Grab the credentials for this process so we have
1317ad4023c4Sdp 	 * something to pass to dtrace_register().
1318ad4023c4Sdp 	 */
13197c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
1320ad4023c4Sdp 	crhold(p->p_cred);
1321ad4023c4Sdp 	cred = p->p_cred;
13227c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
13237c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1326b096140dSahl 	new_fp->ftp_pid = pid;
1327b096140dSahl 	new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1328b096140dSahl 
1329b096140dSahl 	ASSERT(new_fp->ftp_proc != NULL);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	/*
13347c478bd9Sstevel@tonic-gate 	 * Take another lap through the list to make sure a provider hasn't
13357c478bd9Sstevel@tonic-gate 	 * been created for this pid while we weren't under the bucket lock.
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
13387c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1339b096140dSahl 		    !fp->ftp_retired) {
13407c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
13417c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
13427c478bd9Sstevel@tonic-gate 			fasttrap_provider_free(new_fp);
1343ad4023c4Sdp 			crfree(cred);
13447c478bd9Sstevel@tonic-gate 			return (fp);
13457c478bd9Sstevel@tonic-gate 		}
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	(void) strcpy(new_fp->ftp_name, name);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * Fail and return NULL if either the provider name is too long
13527c478bd9Sstevel@tonic-gate 	 * or we fail to register this new provider with the DTrace
13537c478bd9Sstevel@tonic-gate 	 * framework. Note that this is the only place we ever construct
13547c478bd9Sstevel@tonic-gate 	 * the full provider name -- we keep it in pieces in the provider
13557c478bd9Sstevel@tonic-gate 	 * structure.
13567c478bd9Sstevel@tonic-gate 	 */
13577c478bd9Sstevel@tonic-gate 	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
13587c478bd9Sstevel@tonic-gate 	    sizeof (provname) ||
13597c478bd9Sstevel@tonic-gate 	    dtrace_register(provname, pattr,
1360ad4023c4Sdp 	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
13617c478bd9Sstevel@tonic-gate 	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
13627c478bd9Sstevel@tonic-gate 	    &new_fp->ftp_provid) != 0) {
13637c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
13647c478bd9Sstevel@tonic-gate 		fasttrap_provider_free(new_fp);
1365ad4023c4Sdp 		crfree(cred);
13667c478bd9Sstevel@tonic-gate 		return (NULL);
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	new_fp->ftp_next = bucket->ftb_data;
13707c478bd9Sstevel@tonic-gate 	bucket->ftb_data = new_fp;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	mutex_enter(&new_fp->ftp_mtx);
13737c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
13747c478bd9Sstevel@tonic-gate 
1375ad4023c4Sdp 	crfree(cred);
13767c478bd9Sstevel@tonic-gate 	return (new_fp);
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate static void
13807c478bd9Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider)
13817c478bd9Sstevel@tonic-gate {
13827c478bd9Sstevel@tonic-gate 	pid_t pid = provider->ftp_pid;
13837c478bd9Sstevel@tonic-gate 	proc_t *p;
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/*
1386ab9a77c7Sahl 	 * There need to be no associated enabled probes, no consumers
1387ab9a77c7Sahl 	 * creating probes, and no meta providers referencing this provider.
13887c478bd9Sstevel@tonic-gate 	 */
13897c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount == 0);
1390ab9a77c7Sahl 	ASSERT(provider->ftp_ccount == 0);
1391ab9a77c7Sahl 	ASSERT(provider->ftp_mcount == 0);
13927c478bd9Sstevel@tonic-gate 
1393b096140dSahl 	fasttrap_proc_release(provider->ftp_proc);
1394b096140dSahl 
13957c478bd9Sstevel@tonic-gate 	kmem_free(provider, sizeof (fasttrap_provider_t));
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	/*
13987c478bd9Sstevel@tonic-gate 	 * Decrement p_dtrace_probes on the process whose provider we're
13997c478bd9Sstevel@tonic-gate 	 * freeing. We don't have to worry about clobbering somone else's
14007c478bd9Sstevel@tonic-gate 	 * modifications to it because we have locked the bucket that
14017c478bd9Sstevel@tonic-gate 	 * corresponds to this process's hash chain in the provider hash
14027c478bd9Sstevel@tonic-gate 	 * table. Don't sweat it if we can't find the process.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
14057c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
14067c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
14077c478bd9Sstevel@tonic-gate 		return;
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
14117c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes--;
14147c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate static void
1418ab9a77c7Sahl fasttrap_provider_retire(pid_t pid, const char *name, int mprov)
14197c478bd9Sstevel@tonic-gate {
1420dafb5540Sahl 	fasttrap_provider_t *fp;
1421dafb5540Sahl 	fasttrap_bucket_t *bucket;
1422dafb5540Sahl 	dtrace_provider_id_t provid;
1423dafb5540Sahl 
1424dafb5540Sahl 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1425dafb5540Sahl 
1426dafb5540Sahl 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1427dafb5540Sahl 	mutex_enter(&bucket->ftb_mtx);
1428dafb5540Sahl 
1429dafb5540Sahl 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1430dafb5540Sahl 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1431dafb5540Sahl 		    !fp->ftp_retired)
1432dafb5540Sahl 			break;
1433dafb5540Sahl 	}
1434dafb5540Sahl 
1435dafb5540Sahl 	if (fp == NULL) {
1436dafb5540Sahl 		mutex_exit(&bucket->ftb_mtx);
1437dafb5540Sahl 		return;
1438dafb5540Sahl 	}
14397c478bd9Sstevel@tonic-gate 
1440ab9a77c7Sahl 	mutex_enter(&fp->ftp_mtx);
1441ab9a77c7Sahl 	ASSERT(!mprov || fp->ftp_mcount > 0);
1442ab9a77c7Sahl 	if (mprov && --fp->ftp_mcount != 0)  {
1443ab9a77c7Sahl 		mutex_exit(&fp->ftp_mtx);
1444ab9a77c7Sahl 		mutex_exit(&bucket->ftb_mtx);
1445ab9a77c7Sahl 		return;
1446ab9a77c7Sahl 	}
1447ab9a77c7Sahl 
14487c478bd9Sstevel@tonic-gate 	/*
1449*038dc6b3Sahl 	 * Mark the provider to be removed in our post-processing step, mark it
1450*038dc6b3Sahl 	 * retired, and drop the active count on its proc. Marking it indicates
1451*038dc6b3Sahl 	 * that we should try to remove it; setting the retired flag indicates
1452*038dc6b3Sahl 	 * that we're done with this provider; dropping the active the proc
1453*038dc6b3Sahl 	 * releases our hold, and when this reaches zero (as it will during
1454*038dc6b3Sahl 	 * exit or exec) the proc and associated providers become defunct.
1455dafb5540Sahl 	 *
1456dafb5540Sahl 	 * We obviously need to take the bucket lock before the provider lock
1457dafb5540Sahl 	 * to perform the lookup, but we need to drop the provider lock
1458dafb5540Sahl 	 * before calling into the DTrace framework since we acquire the
1459dafb5540Sahl 	 * provider lock in callbacks invoked from the DTrace framework. The
1460dafb5540Sahl 	 * bucket lock therefore protects the integrity of the provider hash
1461dafb5540Sahl 	 * table.
14627c478bd9Sstevel@tonic-gate 	 */
1463*038dc6b3Sahl 	atomic_add_64(&fp->ftp_proc->ftpc_acount, -1);
1464dafb5540Sahl 	fp->ftp_retired = 1;
1465dafb5540Sahl 	fp->ftp_marked = 1;
1466dafb5540Sahl 	provid = fp->ftp_provid;
1467dafb5540Sahl 	mutex_exit(&fp->ftp_mtx);
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	/*
14707c478bd9Sstevel@tonic-gate 	 * We don't have to worry about invalidating the same provider twice
14717c478bd9Sstevel@tonic-gate 	 * since fasttrap_provider_lookup() will ignore provider that have
1472b096140dSahl 	 * been marked as retired.
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	dtrace_invalidate(provid);
14757c478bd9Sstevel@tonic-gate 
1476dafb5540Sahl 	mutex_exit(&bucket->ftb_mtx);
1477dafb5540Sahl 
14787c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
14797c478bd9Sstevel@tonic-gate }
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate static int
148273427c57Sahl fasttrap_uint32_cmp(const void *ap, const void *bp)
148373427c57Sahl {
148473427c57Sahl 	return (*(const uint32_t *)ap - *(const uint32_t *)bp);
148573427c57Sahl }
148673427c57Sahl 
148773427c57Sahl static int
148873427c57Sahl fasttrap_uint64_cmp(const void *ap, const void *bp)
148973427c57Sahl {
149073427c57Sahl 	return (*(const uint64_t *)ap - *(const uint64_t *)bp);
149173427c57Sahl }
149273427c57Sahl 
149373427c57Sahl static int
14947c478bd9Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
14977c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
14987c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
14997c478bd9Sstevel@tonic-gate 	char *name;
15007c478bd9Sstevel@tonic-gate 	int i, aframes, whack;
15017c478bd9Sstevel@tonic-gate 
150273427c57Sahl 	/*
150373427c57Sahl 	 * There needs to be at least one desired trace point.
150473427c57Sahl 	 */
150573427c57Sahl 	if (pdata->ftps_noffs == 0)
150673427c57Sahl 		return (EINVAL);
150773427c57Sahl 
15087c478bd9Sstevel@tonic-gate 	switch (pdata->ftps_type) {
15097c478bd9Sstevel@tonic-gate 	case DTFTP_ENTRY:
15107c478bd9Sstevel@tonic-gate 		name = "entry";
15117c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_ENTRY_AFRAMES;
15127c478bd9Sstevel@tonic-gate 		break;
15137c478bd9Sstevel@tonic-gate 	case DTFTP_RETURN:
15147c478bd9Sstevel@tonic-gate 		name = "return";
15157c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_RETURN_AFRAMES;
15167c478bd9Sstevel@tonic-gate 		break;
15177c478bd9Sstevel@tonic-gate 	case DTFTP_OFFSETS:
15187c478bd9Sstevel@tonic-gate 		name = NULL;
15197c478bd9Sstevel@tonic-gate 		break;
15207c478bd9Sstevel@tonic-gate 	default:
15217c478bd9Sstevel@tonic-gate 		return (EINVAL);
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
15257c478bd9Sstevel@tonic-gate 	    FASTTRAP_PID_NAME, &pid_attr)) == NULL)
15267c478bd9Sstevel@tonic-gate 		return (ESRCH);
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	/*
15297c478bd9Sstevel@tonic-gate 	 * Increment this reference count to indicate that a consumer is
1530f498645aSahl 	 * actively adding a new probe associated with this provider. This
1531f498645aSahl 	 * prevents the provider from being deleted -- we'll need to check
1532f498645aSahl 	 * for pending deletions when we drop this reference count.
15337c478bd9Sstevel@tonic-gate 	 */
15347c478bd9Sstevel@tonic-gate 	provider->ftp_ccount++;
15357c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
15367c478bd9Sstevel@tonic-gate 
1537f498645aSahl 	/*
1538f498645aSahl 	 * Grab the creation lock to ensure consistency between calls to
1539f498645aSahl 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1540f498645aSahl 	 * other threads creating probes. We must drop the provider lock
1541f498645aSahl 	 * before taking this lock to avoid a three-way deadlock with the
1542f498645aSahl 	 * DTrace framework.
1543f498645aSahl 	 */
1544f498645aSahl 	mutex_enter(&provider->ftp_cmtx);
15457c478bd9Sstevel@tonic-gate 
1546f498645aSahl 	if (name == NULL) {
15477c478bd9Sstevel@tonic-gate 		for (i = 0; i < pdata->ftps_noffs; i++) {
15487c478bd9Sstevel@tonic-gate 			char name_str[17];
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 			(void) sprintf(name_str, "%llx",
15517c478bd9Sstevel@tonic-gate 			    (unsigned long long)pdata->ftps_offs[i]);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 			if (dtrace_probe_lookup(provider->ftp_provid,
15547c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
15557c478bd9Sstevel@tonic-gate 				continue;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 			atomic_add_32(&fasttrap_total, 1);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 			if (fasttrap_total > fasttrap_max) {
15607c478bd9Sstevel@tonic-gate 				atomic_add_32(&fasttrap_total, -1);
15617c478bd9Sstevel@tonic-gate 				goto no_mem;
15627c478bd9Sstevel@tonic-gate 			}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 			pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 			pp->ftp_prov = provider;
15677c478bd9Sstevel@tonic-gate 			pp->ftp_faddr = pdata->ftps_pc;
15687c478bd9Sstevel@tonic-gate 			pp->ftp_fsize = pdata->ftps_size;
15697c478bd9Sstevel@tonic-gate 			pp->ftp_pid = pdata->ftps_pid;
15707c478bd9Sstevel@tonic-gate 			pp->ftp_ntps = 1;
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
15737c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
15747c478bd9Sstevel@tonic-gate 
1575b096140dSahl 			tp->ftt_proc = provider->ftp_proc;
15767c478bd9Sstevel@tonic-gate 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
15777c478bd9Sstevel@tonic-gate 			tp->ftt_pid = pdata->ftps_pid;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_tp = tp;
15807c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_id.fti_probe = pp;
1581ac448965Sahl 			pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
15847c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str,
15857c478bd9Sstevel@tonic-gate 			    FASTTRAP_OFFSET_AFRAMES, pp);
15867c478bd9Sstevel@tonic-gate 		}
1587f498645aSahl 
1588f498645aSahl 	} else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
1589f498645aSahl 	    pdata->ftps_func, name) == 0) {
1590f498645aSahl 		atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
1591f498645aSahl 
1592f498645aSahl 		if (fasttrap_total > fasttrap_max) {
1593f498645aSahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1594f498645aSahl 			goto no_mem;
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 
159773427c57Sahl 		/*
159873427c57Sahl 		 * Make sure all tracepoint program counter values are unique.
159973427c57Sahl 		 * We later assume that each probe has exactly one tracepoint
160073427c57Sahl 		 * for a given pc.
160173427c57Sahl 		 */
160273427c57Sahl 		qsort(pdata->ftps_offs, pdata->ftps_noffs,
160373427c57Sahl 		    sizeof (uint64_t), fasttrap_uint64_cmp);
160473427c57Sahl 		for (i = 1; i < pdata->ftps_noffs; i++) {
160573427c57Sahl 			if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
160673427c57Sahl 				continue;
160773427c57Sahl 
160873427c57Sahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
160973427c57Sahl 			goto no_mem;
161073427c57Sahl 		}
161173427c57Sahl 
1612f498645aSahl 		ASSERT(pdata->ftps_noffs > 0);
1613f498645aSahl 		pp = kmem_zalloc(offsetof(fasttrap_probe_t,
1614f498645aSahl 		    ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
1615f498645aSahl 
1616f498645aSahl 		pp->ftp_prov = provider;
1617f498645aSahl 		pp->ftp_faddr = pdata->ftps_pc;
1618f498645aSahl 		pp->ftp_fsize = pdata->ftps_size;
1619f498645aSahl 		pp->ftp_pid = pdata->ftps_pid;
1620f498645aSahl 		pp->ftp_ntps = pdata->ftps_noffs;
1621f498645aSahl 
1622f498645aSahl 		for (i = 0; i < pdata->ftps_noffs; i++) {
1623f498645aSahl 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1624f498645aSahl 			    KM_SLEEP);
1625f498645aSahl 
1626f498645aSahl 			tp->ftt_proc = provider->ftp_proc;
1627f498645aSahl 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1628f498645aSahl 			tp->ftt_pid = pdata->ftps_pid;
1629f498645aSahl 
1630f498645aSahl 			pp->ftp_tps[i].fit_tp = tp;
1631f498645aSahl 			pp->ftp_tps[i].fit_id.fti_probe = pp;
1632f498645aSahl 			pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
1633f498645aSahl 		}
1634f498645aSahl 
1635f498645aSahl 		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1636f498645aSahl 		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
1637f498645aSahl 	}
1638f498645aSahl 
1639f498645aSahl 	mutex_exit(&provider->ftp_cmtx);
1640f498645aSahl 
16417c478bd9Sstevel@tonic-gate 	/*
16427c478bd9Sstevel@tonic-gate 	 * We know that the provider is still valid since we incremented the
1643f498645aSahl 	 * creation reference count. If someone tried to clean up this provider
1644f498645aSahl 	 * while we were using it (e.g. because the process called exec(2) or
1645f498645aSahl 	 * exit(2)), take note of that and try to clean it up now.
16467c478bd9Sstevel@tonic-gate 	 */
16477c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
16487c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
1649b096140dSahl 	whack = provider->ftp_retired;
16507c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if (whack)
16537c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	return (0);
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate no_mem:
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * If we've exhausted the allowable resources, we'll try to remove
16607c478bd9Sstevel@tonic-gate 	 * this provider to free some up. This is to cover the case where
16617c478bd9Sstevel@tonic-gate 	 * the user has accidentally created many more probes than was
16627c478bd9Sstevel@tonic-gate 	 * intended (e.g. pid123:::).
16637c478bd9Sstevel@tonic-gate 	 */
1664f498645aSahl 	mutex_exit(&provider->ftp_cmtx);
16657c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
16667c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
16677c478bd9Sstevel@tonic-gate 	provider->ftp_marked = 1;
16687c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	return (ENOMEM);
16737c478bd9Sstevel@tonic-gate }
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16767c478bd9Sstevel@tonic-gate static void *
16777c478bd9Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * A 32-bit unsigned integer (like a pid for example) can be
16837c478bd9Sstevel@tonic-gate 	 * expressed in 10 or fewer decimal digits. Make sure that we'll
16847c478bd9Sstevel@tonic-gate 	 * have enough space for the provider name.
16857c478bd9Sstevel@tonic-gate 	 */
16867c478bd9Sstevel@tonic-gate 	if (strlen(dhpv->dthpv_provname) + 10 >=
16877c478bd9Sstevel@tonic-gate 	    sizeof (provider->ftp_name)) {
16887c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
16897c478bd9Sstevel@tonic-gate 		    "name too long to accomodate pid", dhpv->dthpv_provname);
16907c478bd9Sstevel@tonic-gate 		return (NULL);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * Don't let folks spoof the true pid provider.
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 	if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
16977c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
16987c478bd9Sstevel@tonic-gate 		    "%s is an invalid name", dhpv->dthpv_provname,
16997c478bd9Sstevel@tonic-gate 		    FASTTRAP_PID_NAME);
17007c478bd9Sstevel@tonic-gate 		return (NULL);
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	/*
17047c478bd9Sstevel@tonic-gate 	 * The highest stability class that fasttrap supports is ISA; cap
17057c478bd9Sstevel@tonic-gate 	 * the stability of the new provider accordingly.
17067c478bd9Sstevel@tonic-gate 	 */
1707*038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
17087c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
1709*038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
17107c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
1711*038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
17127c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
1713*038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
17147c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
1715*038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
17167c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
17197c478bd9Sstevel@tonic-gate 	    &dhpv->dthpv_pattr)) == NULL) {
17207c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s for "
17217c478bd9Sstevel@tonic-gate 		    "process %u",  dhpv->dthpv_provname, (uint_t)pid);
17227c478bd9Sstevel@tonic-gate 		return (NULL);
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	/*
1726ab9a77c7Sahl 	 * Up the meta provider count so this provider isn't removed until
1727ab9a77c7Sahl 	 * the meta provider has been told to remove it.
17287c478bd9Sstevel@tonic-gate 	 */
1729ab9a77c7Sahl 	provider->ftp_mcount++;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	return (provider);
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17377c478bd9Sstevel@tonic-gate static void
17387c478bd9Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg,
17397c478bd9Sstevel@tonic-gate     dtrace_helper_probedesc_t *dhpb)
17407c478bd9Sstevel@tonic-gate {
17417c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = parg;
17427c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
17437c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
1744ac448965Sahl 	int i, j;
1745ac448965Sahl 	uint32_t ntps;
17467c478bd9Sstevel@tonic-gate 
1747f498645aSahl 	/*
1748f498645aSahl 	 * Since the meta provider count is non-zero we don't have to worry
1749f498645aSahl 	 * about this provider disappearing.
1750f498645aSahl 	 */
1751f498645aSahl 	ASSERT(provider->ftp_mcount > 0);
1752f498645aSahl 
1753f498645aSahl 	/*
175473427c57Sahl 	 * The offsets must be unique.
175573427c57Sahl 	 */
175673427c57Sahl 	qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
175773427c57Sahl 	    fasttrap_uint32_cmp);
175873427c57Sahl 	for (i = 1; i < dhpb->dthpb_noffs; i++) {
175973427c57Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
176073427c57Sahl 		    dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
176173427c57Sahl 			return;
176273427c57Sahl 	}
176373427c57Sahl 
176473427c57Sahl 	qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
176573427c57Sahl 	    fasttrap_uint32_cmp);
176673427c57Sahl 	for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
176773427c57Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
176873427c57Sahl 		    dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
176973427c57Sahl 			return;
177073427c57Sahl 	}
177173427c57Sahl 
177273427c57Sahl 	/*
1773f498645aSahl 	 * Grab the creation lock to ensure consistency between calls to
1774f498645aSahl 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1775f498645aSahl 	 * other threads creating probes.
1776f498645aSahl 	 */
1777f498645aSahl 	mutex_enter(&provider->ftp_cmtx);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
17807c478bd9Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
1781f498645aSahl 		mutex_exit(&provider->ftp_cmtx);
17827c478bd9Sstevel@tonic-gate 		return;
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
1785ac448965Sahl 	ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
1786ab9a77c7Sahl 	ASSERT(ntps > 0);
1787ac448965Sahl 
1788ac448965Sahl 	atomic_add_32(&fasttrap_total, ntps);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (fasttrap_total > fasttrap_max) {
1791ac448965Sahl 		atomic_add_32(&fasttrap_total, -ntps);
1792f498645aSahl 		mutex_exit(&provider->ftp_cmtx);
17937c478bd9Sstevel@tonic-gate 		return;
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate 
1796ab9a77c7Sahl 	pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	pp->ftp_prov = provider;
17997c478bd9Sstevel@tonic-gate 	pp->ftp_pid = provider->ftp_pid;
1800ac448965Sahl 	pp->ftp_ntps = ntps;
18017c478bd9Sstevel@tonic-gate 	pp->ftp_nargs = dhpb->dthpb_xargc;
18027c478bd9Sstevel@tonic-gate 	pp->ftp_xtypes = dhpb->dthpb_xtypes;
18037c478bd9Sstevel@tonic-gate 	pp->ftp_ntypes = dhpb->dthpb_ntypes;
18047c478bd9Sstevel@tonic-gate 
1805ac448965Sahl 	/*
1806ac448965Sahl 	 * First create a tracepoint for each actual point of interest.
1807ac448965Sahl 	 */
1808ac448965Sahl 	for (i = 0; i < dhpb->dthpb_noffs; i++) {
18097c478bd9Sstevel@tonic-gate 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18107c478bd9Sstevel@tonic-gate 
1811b096140dSahl 		tp->ftt_proc = provider->ftp_proc;
18127c478bd9Sstevel@tonic-gate 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
18137c478bd9Sstevel@tonic-gate 		tp->ftt_pid = provider->ftp_pid;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_tp = tp;
18167c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_id.fti_probe = pp;
1817ac448965Sahl #ifdef __sparc
1818ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
1819ac448965Sahl #else
1820ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
1821ac448965Sahl #endif
1822ac448965Sahl 	}
1823ac448965Sahl 
1824ac448965Sahl 	/*
1825ac448965Sahl 	 * Then create a tracepoint for each is-enabled point.
1826ac448965Sahl 	 */
1827ac448965Sahl 	for (j = 0; i < ntps; i++, j++) {
1828ac448965Sahl 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
1829ac448965Sahl 
1830ac448965Sahl 		tp->ftt_proc = provider->ftp_proc;
1831ac448965Sahl 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
1832ac448965Sahl 		tp->ftt_pid = provider->ftp_pid;
1833ac448965Sahl 
1834ac448965Sahl 		pp->ftp_tps[i].fit_tp = tp;
1835ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_probe = pp;
1836ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/*
18407c478bd9Sstevel@tonic-gate 	 * If the arguments are shuffled around we set the argument remapping
18417c478bd9Sstevel@tonic-gate 	 * table. Later, when the probe fires, we only remap the arguments
18427c478bd9Sstevel@tonic-gate 	 * if the table is non-NULL.
18437c478bd9Sstevel@tonic-gate 	 */
18447c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhpb->dthpb_xargc; i++) {
18457c478bd9Sstevel@tonic-gate 		if (dhpb->dthpb_args[i] != i) {
18467c478bd9Sstevel@tonic-gate 			pp->ftp_argmap = dhpb->dthpb_args;
18477c478bd9Sstevel@tonic-gate 			break;
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 	}
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	/*
18527c478bd9Sstevel@tonic-gate 	 * The probe is fully constructed -- register it with DTrace.
18537c478bd9Sstevel@tonic-gate 	 */
18547c478bd9Sstevel@tonic-gate 	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
18557c478bd9Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
18567c478bd9Sstevel@tonic-gate 
1857f498645aSahl 	mutex_exit(&provider->ftp_cmtx);
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18617c478bd9Sstevel@tonic-gate static void
18627c478bd9Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
18637c478bd9Sstevel@tonic-gate {
18647c478bd9Sstevel@tonic-gate 	/*
1865dafb5540Sahl 	 * Clean up the USDT provider. There may be active consumers of the
1866dafb5540Sahl 	 * provider busy adding probes, no damage will actually befall the
1867dafb5540Sahl 	 * provider until that count has dropped to zero. This just puts
1868dafb5540Sahl 	 * the provider on death row.
18697c478bd9Sstevel@tonic-gate 	 */
1870dafb5540Sahl 	fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = {
18747c478bd9Sstevel@tonic-gate 	fasttrap_meta_create_probe,
18757c478bd9Sstevel@tonic-gate 	fasttrap_meta_provide,
18767c478bd9Sstevel@tonic-gate 	fasttrap_meta_remove
18777c478bd9Sstevel@tonic-gate };
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18807c478bd9Sstevel@tonic-gate static int
18817c478bd9Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	return (0);
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18877c478bd9Sstevel@tonic-gate static int
18887c478bd9Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
18897c478bd9Sstevel@tonic-gate {
18907c478bd9Sstevel@tonic-gate 	if (!dtrace_attached())
18917c478bd9Sstevel@tonic-gate 		return (EAGAIN);
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	if (cmd == FASTTRAPIOC_MAKEPROBE) {
18947c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *uprobe = (void *)arg;
18957c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *probe;
18967c478bd9Sstevel@tonic-gate 		uint64_t noffs;
18977c478bd9Sstevel@tonic-gate 		size_t size;
18987c478bd9Sstevel@tonic-gate 		int ret;
18997c478bd9Sstevel@tonic-gate 		char *c;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 		if (copyin(&uprobe->ftps_noffs, &noffs,
19027c478bd9Sstevel@tonic-gate 		    sizeof (uprobe->ftps_noffs)))
19037c478bd9Sstevel@tonic-gate 			return (EFAULT);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 		/*
19067c478bd9Sstevel@tonic-gate 		 * Probes must have at least one tracepoint.
19077c478bd9Sstevel@tonic-gate 		 */
19087c478bd9Sstevel@tonic-gate 		if (noffs == 0)
19097c478bd9Sstevel@tonic-gate 			return (EINVAL);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		size = sizeof (fasttrap_probe_spec_t) +
19127c478bd9Sstevel@tonic-gate 		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		if (size > 1024 * 1024)
19157c478bd9Sstevel@tonic-gate 			return (ENOMEM);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 		probe = kmem_alloc(size, KM_SLEEP);
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 		if (copyin(uprobe, probe, size) != 0) {
19207c478bd9Sstevel@tonic-gate 			kmem_free(probe, size);
19217c478bd9Sstevel@tonic-gate 			return (EFAULT);
19227c478bd9Sstevel@tonic-gate 		}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 		/*
19257c478bd9Sstevel@tonic-gate 		 * Verify that the function and module strings contain no
19267c478bd9Sstevel@tonic-gate 		 * funny characters.
19277c478bd9Sstevel@tonic-gate 		 */
19287c478bd9Sstevel@tonic-gate 		for (c = &probe->ftps_func[0]; *c != '\0'; c++) {
19297c478bd9Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
19307c478bd9Sstevel@tonic-gate 				ret = EINVAL;
19317c478bd9Sstevel@tonic-gate 				goto err;
19327c478bd9Sstevel@tonic-gate 			}
19337c478bd9Sstevel@tonic-gate 		}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		for (c = &probe->ftps_mod[0]; *c != '\0'; c++) {
19367c478bd9Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
19377c478bd9Sstevel@tonic-gate 				ret = EINVAL;
19387c478bd9Sstevel@tonic-gate 				goto err;
19397c478bd9Sstevel@tonic-gate 			}
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
19437c478bd9Sstevel@tonic-gate 			proc_t *p;
19447c478bd9Sstevel@tonic-gate 			pid_t pid = probe->ftps_pid;
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
19477c478bd9Sstevel@tonic-gate 			/*
19487c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
19497c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
19507c478bd9Sstevel@tonic-gate 			 */
19517c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
19527c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
19537c478bd9Sstevel@tonic-gate 				return (ESRCH);
19547c478bd9Sstevel@tonic-gate 			}
19557c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
19567c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
19597c478bd9Sstevel@tonic-gate 			    VREAD | VWRITE)) != 0) {
19607c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
19617c478bd9Sstevel@tonic-gate 				return (ret);
19627c478bd9Sstevel@tonic-gate 			}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		ret = fasttrap_add_probe(probe);
19687c478bd9Sstevel@tonic-gate err:
19697c478bd9Sstevel@tonic-gate 		kmem_free(probe, size);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		return (ret);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	} else if (cmd == FASTTRAPIOC_GETINSTR) {
19747c478bd9Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
19757c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
19767c478bd9Sstevel@tonic-gate 		uint_t index;
19777c478bd9Sstevel@tonic-gate 		int ret;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
19807c478bd9Sstevel@tonic-gate 			return (EFAULT);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
19837c478bd9Sstevel@tonic-gate 			proc_t *p;
19847c478bd9Sstevel@tonic-gate 			pid_t pid = instr.ftiq_pid;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
19877c478bd9Sstevel@tonic-gate 			/*
19887c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
19897c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
19907c478bd9Sstevel@tonic-gate 			 */
19917c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
19927c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
19937c478bd9Sstevel@tonic-gate 				return (ESRCH);
19947c478bd9Sstevel@tonic-gate 			}
19957c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
19967c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
19997c478bd9Sstevel@tonic-gate 			    VREAD)) != 0) {
20007c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
20017c478bd9Sstevel@tonic-gate 				return (ret);
20027c478bd9Sstevel@tonic-gate 			}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
20057c478bd9Sstevel@tonic-gate 		}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20107c478bd9Sstevel@tonic-gate 		tp = fasttrap_tpoints.fth_table[index].ftb_data;
20117c478bd9Sstevel@tonic-gate 		while (tp != NULL) {
20127c478bd9Sstevel@tonic-gate 			if (instr.ftiq_pid == tp->ftt_pid &&
20137c478bd9Sstevel@tonic-gate 			    instr.ftiq_pc == tp->ftt_pc &&
2014*038dc6b3Sahl 			    tp->ftt_proc->ftpc_acount != 0)
20157c478bd9Sstevel@tonic-gate 				break;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 			tp = tp->ftt_next;
20187c478bd9Sstevel@tonic-gate 		}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
20217c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20227c478bd9Sstevel@tonic-gate 			return (ENOENT);
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
20267c478bd9Sstevel@tonic-gate 		    sizeof (instr.ftiq_instr));
20277c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 		if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
20307c478bd9Sstevel@tonic-gate 			return (EFAULT);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 		return (0);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	return (EINVAL);
20367c478bd9Sstevel@tonic-gate }
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = {
20397c478bd9Sstevel@tonic-gate 	fasttrap_open,		/* open */
20407c478bd9Sstevel@tonic-gate 	nodev,			/* close */
20417c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
20427c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
20437c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
20447c478bd9Sstevel@tonic-gate 	nodev,			/* read */
20457c478bd9Sstevel@tonic-gate 	nodev,			/* write */
20467c478bd9Sstevel@tonic-gate 	fasttrap_ioctl,		/* ioctl */
20477c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
20487c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
20497c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
20507c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
20517c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
20527c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
20537c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
20547c478bd9Sstevel@tonic-gate };
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20577c478bd9Sstevel@tonic-gate static int
20587c478bd9Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
20597c478bd9Sstevel@tonic-gate {
20607c478bd9Sstevel@tonic-gate 	int error;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	switch (infocmd) {
20637c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
20647c478bd9Sstevel@tonic-gate 		*result = (void *)fasttrap_devi;
20657c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
20667c478bd9Sstevel@tonic-gate 		break;
20677c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
20687c478bd9Sstevel@tonic-gate 		*result = (void *)0;
20697c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
20707c478bd9Sstevel@tonic-gate 		break;
20717c478bd9Sstevel@tonic-gate 	default:
20727c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 	return (error);
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate static int
20787c478bd9Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	ulong_t nent;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	switch (cmd) {
20837c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
20847c478bd9Sstevel@tonic-gate 		break;
20857c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
20867c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
20877c478bd9Sstevel@tonic-gate 	default:
20887c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0,
2092f498645aSahl 	    DDI_PSEUDO, NULL) == DDI_FAILURE) {
20937c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
20947c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
20957c478bd9Sstevel@tonic-gate 	}
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
20987c478bd9Sstevel@tonic-gate 	fasttrap_devi = devi;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	/*
21017c478bd9Sstevel@tonic-gate 	 * Install our hooks into fork(2), exec(2), and exit(2).
21027c478bd9Sstevel@tonic-gate 	 */
21037c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = &fasttrap_fork;
21047c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
21057c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21087c478bd9Sstevel@tonic-gate 	    "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
21097c478bd9Sstevel@tonic-gate 	fasttrap_total = 0;
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	/*
21127c478bd9Sstevel@tonic-gate 	 * Conjure up the tracepoints hashtable...
21137c478bd9Sstevel@tonic-gate 	 */
21147c478bd9Sstevel@tonic-gate 	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21157c478bd9Sstevel@tonic-gate 	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
21167c478bd9Sstevel@tonic-gate 
211773427c57Sahl 	if (nent == 0 || nent > 0x1000000)
21187c478bd9Sstevel@tonic-gate 		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
21217c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = nent;
21227c478bd9Sstevel@tonic-gate 	else
21237c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
21247c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_tpoints.fth_nent > 0);
21257c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
21267c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
21277c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	/*
2130b096140dSahl 	 * ... and the providers hash table...
21317c478bd9Sstevel@tonic-gate 	 */
21327c478bd9Sstevel@tonic-gate 	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
21337c478bd9Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
21347c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = nent;
21357c478bd9Sstevel@tonic-gate 	else
21367c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
21377c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_provs.fth_nent > 0);
21387c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
21397c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
21407c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21417c478bd9Sstevel@tonic-gate 
2142b096140dSahl 	/*
2143b096140dSahl 	 * ... and the procs hash table.
2144b096140dSahl 	 */
2145b096140dSahl 	nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2146b096140dSahl 	if ((nent & (nent - 1)) == 0)
2147b096140dSahl 		fasttrap_procs.fth_nent = nent;
2148b096140dSahl 	else
2149b096140dSahl 		fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2150b096140dSahl 	ASSERT(fasttrap_procs.fth_nent > 0);
2151b096140dSahl 	fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2152b096140dSahl 	fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
2153b096140dSahl 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2154b096140dSahl 
21557c478bd9Sstevel@tonic-gate 	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
21567c478bd9Sstevel@tonic-gate 	    &fasttrap_meta_id);
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
21597c478bd9Sstevel@tonic-gate }
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate static int
21627c478bd9Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	int i, fail = 0;
21657c478bd9Sstevel@tonic-gate 	timeout_id_t tmp;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	switch (cmd) {
21687c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
21697c478bd9Sstevel@tonic-gate 		break;
21707c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
21717c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
21727c478bd9Sstevel@tonic-gate 	default:
21737c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21747c478bd9Sstevel@tonic-gate 	}
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/*
21777c478bd9Sstevel@tonic-gate 	 * Unregister the meta-provider to make sure no new fasttrap-
21787c478bd9Sstevel@tonic-gate 	 * managed providers come along while we're trying to close up
21797c478bd9Sstevel@tonic-gate 	 * shop. If we fail to detach, we'll need to re-register as a
21807c478bd9Sstevel@tonic-gate 	 * meta-provider. We can fail to unregister as a meta-provider
21817c478bd9Sstevel@tonic-gate 	 * if providers we manage still exist.
21827c478bd9Sstevel@tonic-gate 	 */
21837c478bd9Sstevel@tonic-gate 	if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
21847c478bd9Sstevel@tonic-gate 	    dtrace_meta_unregister(fasttrap_meta_id) != 0)
21857c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	/*
21887c478bd9Sstevel@tonic-gate 	 * Prevent any new timeouts from running by setting fasttrap_timeout
21897c478bd9Sstevel@tonic-gate 	 * to a non-zero value, and wait for the current timeout to complete.
21907c478bd9Sstevel@tonic-gate 	 */
21917c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
21927c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	while (fasttrap_timeout != (timeout_id_t)1) {
21957c478bd9Sstevel@tonic-gate 		tmp = fasttrap_timeout;
21967c478bd9Sstevel@tonic-gate 		fasttrap_timeout = (timeout_id_t)1;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 		if (tmp != 0) {
21997c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_cleanup_mtx);
22007c478bd9Sstevel@tonic-gate 			(void) untimeout(tmp);
22017c478bd9Sstevel@tonic-gate 			mutex_enter(&fasttrap_cleanup_mtx);
22027c478bd9Sstevel@tonic-gate 		}
22037c478bd9Sstevel@tonic-gate 	}
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
22067c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	/*
22097c478bd9Sstevel@tonic-gate 	 * Iterate over all of our providers. If there's still a process
22107c478bd9Sstevel@tonic-gate 	 * that corresponds to that pid, fail to detach.
22117c478bd9Sstevel@tonic-gate 	 */
22127c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
22137c478bd9Sstevel@tonic-gate 		fasttrap_provider_t **fpp, *fp;
22147c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
22177c478bd9Sstevel@tonic-gate 		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
22187c478bd9Sstevel@tonic-gate 		while ((fp = *fpp) != NULL) {
22197c478bd9Sstevel@tonic-gate 			/*
22207c478bd9Sstevel@tonic-gate 			 * Acquire and release the lock as a simple way of
22217c478bd9Sstevel@tonic-gate 			 * waiting for any other consumer to finish with
22227c478bd9Sstevel@tonic-gate 			 * this provider. A thread must first acquire the
22237c478bd9Sstevel@tonic-gate 			 * bucket lock so there's no chance of another thread
2224dafb5540Sahl 			 * blocking on the provider's lock.
22257c478bd9Sstevel@tonic-gate 			 */
22267c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
22277c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->ftp_mtx);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 			if (dtrace_unregister(fp->ftp_provid) != 0) {
22307c478bd9Sstevel@tonic-gate 				fail = 1;
22317c478bd9Sstevel@tonic-gate 				fpp = &fp->ftp_next;
22327c478bd9Sstevel@tonic-gate 			} else {
22337c478bd9Sstevel@tonic-gate 				*fpp = fp->ftp_next;
22347c478bd9Sstevel@tonic-gate 				fasttrap_provider_free(fp);
22357c478bd9Sstevel@tonic-gate 			}
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
22397c478bd9Sstevel@tonic-gate 	}
22407c478bd9Sstevel@tonic-gate 
2241f498645aSahl 	if (fail) {
22427c478bd9Sstevel@tonic-gate 		uint_t work;
22437c478bd9Sstevel@tonic-gate 		/*
22447c478bd9Sstevel@tonic-gate 		 * If we're failing to detach, we need to unblock timeouts
22457c478bd9Sstevel@tonic-gate 		 * and start a new timeout if any work has accumulated while
22467c478bd9Sstevel@tonic-gate 		 * we've been unsuccessfully trying to detach.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
22497c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
22507c478bd9Sstevel@tonic-gate 		work = fasttrap_cleanup_work;
22517c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 		if (work)
22547c478bd9Sstevel@tonic-gate 			fasttrap_pid_cleanup();
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 		(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
22577c478bd9Sstevel@tonic-gate 		    &fasttrap_meta_id);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22607c478bd9Sstevel@tonic-gate 	}
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate #ifdef DEBUG
22637c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
2264f498645aSahl 	ASSERT(fasttrap_pid_count == 0);
22657c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
22667c478bd9Sstevel@tonic-gate #endif
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_tpoints.fth_table,
22697c478bd9Sstevel@tonic-gate 	    fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
22707c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_nent = 0;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_provs.fth_table,
22737c478bd9Sstevel@tonic-gate 	    fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
22747c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_nent = 0;
22757c478bd9Sstevel@tonic-gate 
2276b096140dSahl 	kmem_free(fasttrap_procs.fth_table,
2277b096140dSahl 	    fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2278b096140dSahl 	fasttrap_procs.fth_nent = 0;
2279b096140dSahl 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * We know there are no tracepoints in any process anywhere in
22827c478bd9Sstevel@tonic-gate 	 * the system so there is no process which has its p_dtrace_count
22837c478bd9Sstevel@tonic-gate 	 * greater than zero, therefore we know that no thread can actively
22847c478bd9Sstevel@tonic-gate 	 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes
22857c478bd9Sstevel@tonic-gate 	 * and fasttrap_exec() and fasttrap_exit().
22867c478bd9Sstevel@tonic-gate 	 */
22877c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork);
22887c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = NULL;
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit);
22917c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = NULL;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit);
22947c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = NULL;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate static struct dev_ops fasttrap_ops = {
23027c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
23037c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
23047c478bd9Sstevel@tonic-gate 	fasttrap_info,		/* get_dev_info */
23057c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
23067c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
23077c478bd9Sstevel@tonic-gate 	fasttrap_attach,	/* attach */
23087c478bd9Sstevel@tonic-gate 	fasttrap_detach,	/* detach */
23097c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
23107c478bd9Sstevel@tonic-gate 	&fasttrap_cb_ops,	/* driver operations */
23117c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
23127c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
23137c478bd9Sstevel@tonic-gate };
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate /*
23167c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
23177c478bd9Sstevel@tonic-gate  */
23187c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
23197c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
23207c478bd9Sstevel@tonic-gate 	"Fasttrap Tracing",	/* name of module */
23217c478bd9Sstevel@tonic-gate 	&fasttrap_ops,		/* driver ops */
23227c478bd9Sstevel@tonic-gate };
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
23257c478bd9Sstevel@tonic-gate 	MODREV_1,
23267c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
23277c478bd9Sstevel@tonic-gate 	NULL
23287c478bd9Sstevel@tonic-gate };
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate int
23317c478bd9Sstevel@tonic-gate _init(void)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate int
23377c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
23387c478bd9Sstevel@tonic-gate {
23397c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
23407c478bd9Sstevel@tonic-gate }
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate int
23437c478bd9Sstevel@tonic-gate _fini(void)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
23467c478bd9Sstevel@tonic-gate }
2347