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 /*
23b9e93c10SJonathan Haslam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27f484800dSBryan Cantrill /*
289df7e4e1SBryan Cantrill  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
29f484800dSBryan Cantrill  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
327c478bd9Sstevel@tonic-gate #include <sys/errno.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
417c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
427c478bd9Sstevel@tonic-gate #include <sys/fasttrap.h>
437c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
447c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
457c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
467c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
477c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
487c478bd9Sstevel@tonic-gate #include <sys/proc.h>
497c478bd9Sstevel@tonic-gate #include <sys/priv.h>
507c478bd9Sstevel@tonic-gate #include <sys/policy.h>
5173427c57Sahl #include <util/qsort.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * User-Land Trap-Based Tracing
557c478bd9Sstevel@tonic-gate  * ----------------------------
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * The fasttrap provider allows DTrace consumers to instrument any user-level
587c478bd9Sstevel@tonic-gate  * instruction to gather data; this includes probes with semantic
597c478bd9Sstevel@tonic-gate  * signifigance like entry and return as well as simple offsets into the
607c478bd9Sstevel@tonic-gate  * function. While the specific techniques used are very ISA specific, the
617c478bd9Sstevel@tonic-gate  * methodology is generalizable to any architecture.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * The General Methodology
657c478bd9Sstevel@tonic-gate  * -----------------------
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * With the primary goal of tracing every user-land instruction and the
687c478bd9Sstevel@tonic-gate  * limitation that we can't trust user space so don't want to rely on much
697c478bd9Sstevel@tonic-gate  * information there, we begin by replacing the instructions we want to trace
707c478bd9Sstevel@tonic-gate  * with trap instructions. Each instruction we overwrite is saved into a hash
717c478bd9Sstevel@tonic-gate  * table keyed by process ID and pc address. When we enter the kernel due to
727c478bd9Sstevel@tonic-gate  * this trap instruction, we need the effects of the replaced instruction to
737c478bd9Sstevel@tonic-gate  * appear to have occurred before we proceed with the user thread's
747c478bd9Sstevel@tonic-gate  * execution.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * Each user level thread is represented by a ulwp_t structure which is
777c478bd9Sstevel@tonic-gate  * always easily accessible through a register. The most basic way to produce
787c478bd9Sstevel@tonic-gate  * the effects of the instruction we replaced is to copy that instruction out
797c478bd9Sstevel@tonic-gate  * to a bit of scratch space reserved in the user thread's ulwp_t structure
807c478bd9Sstevel@tonic-gate  * (a sort of kernel-private thread local storage), set the PC to that
817c478bd9Sstevel@tonic-gate  * scratch space and single step. When we reenter the kernel after single
827c478bd9Sstevel@tonic-gate  * stepping the instruction we must then adjust the PC to point to what would
837c478bd9Sstevel@tonic-gate  * normally be the next instruction. Of course, special care must be taken
847c478bd9Sstevel@tonic-gate  * for branches and jumps, but these represent such a small fraction of any
857c478bd9Sstevel@tonic-gate  * instruction set that writing the code to emulate these in the kernel is
867c478bd9Sstevel@tonic-gate  * not too difficult.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Return probes may require several tracepoints to trace every return site,
897c478bd9Sstevel@tonic-gate  * and, conversely, each tracepoint may activate several probes (the entry
907c478bd9Sstevel@tonic-gate  * and offset 0 probes, for example). To solve this muliplexing problem,
917c478bd9Sstevel@tonic-gate  * tracepoints contain lists of probes to activate and probes contain lists
927c478bd9Sstevel@tonic-gate  * of tracepoints to enable. If a probe is activated, it adds its ID to
937c478bd9Sstevel@tonic-gate  * existing tracepoints or creates new ones as necessary.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Most probes are activated _before_ the instruction is executed, but return
967c478bd9Sstevel@tonic-gate  * probes are activated _after_ the effects of the last instruction of the
977c478bd9Sstevel@tonic-gate  * function are visible. Return probes must be fired _after_ we have
987c478bd9Sstevel@tonic-gate  * single-stepped the instruction whereas all other probes are fired
997c478bd9Sstevel@tonic-gate  * beforehand.
100f498645aSahl  *
101f498645aSahl  *
102f498645aSahl  * Lock Ordering
103f498645aSahl  * -------------
104f498645aSahl  *
105f498645aSahl  * The lock ordering below -- both internally and with respect to the DTrace
106f498645aSahl  * framework -- is a little tricky and bears some explanation. Each provider
107f498645aSahl  * has a lock (ftp_mtx) that protects its members including reference counts
108f498645aSahl  * for enabled probes (ftp_rcount), consumers actively creating probes
109f498645aSahl  * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider
110f498645aSahl  * from being freed. A provider is looked up by taking the bucket lock for the
111f498645aSahl  * provider hash table, and is returned with its lock held. The provider lock
112f498645aSahl  * may be taken in functions invoked by the DTrace framework, but may not be
113f498645aSahl  * held while calling functions in the DTrace framework.
114f498645aSahl  *
115f498645aSahl  * To ensure consistency over multiple calls to the DTrace framework, the
116f498645aSahl  * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may
117f498645aSahl  * not be taken when holding the provider lock as that would create a cyclic
118f498645aSahl  * lock ordering. In situations where one would naturally take the provider
119f498645aSahl  * lock and then the creation lock, we instead up a reference count to prevent
120f498645aSahl  * the provider from disappearing, drop the provider lock, and acquire the
121f498645aSahl  * creation lock.
122f498645aSahl  *
123f498645aSahl  * Briefly:
124*ef623aa1SToomas Soome  *	bucket lock before provider lock
125f498645aSahl  *	DTrace before provider lock
126f498645aSahl  *	creation lock before DTrace
127f498645aSahl  *	never hold the provider lock and creation lock simultaneously
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static dev_info_t *fasttrap_devi;
1317c478bd9Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static timeout_id_t fasttrap_timeout;
1347c478bd9Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx;
1357c478bd9Sstevel@tonic-gate static uint_t fasttrap_cleanup_work;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Generation count on modifications to the global tracepoint lookup table.
1397c478bd9Sstevel@tonic-gate  */
1407c478bd9Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * When the fasttrap provider is loaded, fasttrap_max is set to either
1447c478bd9Sstevel@tonic-gate  * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
1457c478bd9Sstevel@tonic-gate  * fasttrap.conf file. Each time a probe is created, fasttrap_total is
1467c478bd9Sstevel@tonic-gate  * incremented by the number of tracepoints that may be associated with that
1477c478bd9Sstevel@tonic-gate  * probe; fasttrap_total is capped at fasttrap_max.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate #define	FASTTRAP_MAX_DEFAULT		250000
1507c478bd9Sstevel@tonic-gate static uint32_t fasttrap_max;
1517c478bd9Sstevel@tonic-gate static uint32_t fasttrap_total;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
1557c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
156b096140dSahl #define	FASTTRAP_PROCS_DEFAULT_SIZE	0x100
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #define	FASTTRAP_PID_NAME		"pid"
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate fasttrap_hash_t			fasttrap_tpoints;
1617c478bd9Sstevel@tonic-gate static fasttrap_hash_t		fasttrap_provs;
162b096140dSahl static fasttrap_hash_t		fasttrap_procs;
1637c478bd9Sstevel@tonic-gate 
164f498645aSahl static uint64_t			fasttrap_pid_count;	/* pid ref count */
1657c478bd9Sstevel@tonic-gate static kmutex_t			fasttrap_count_mtx;	/* lock on ref count */
1667c478bd9Sstevel@tonic-gate 
1678f5db3e7Sahl #define	FASTTRAP_ENABLE_FAIL	1
1688f5db3e7Sahl #define	FASTTRAP_ENABLE_PARTIAL	2
1698f5db3e7Sahl 
1707c478bd9Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
1717c478bd9Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
1747c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *);
175dafb5540Sahl static void fasttrap_provider_retire(pid_t, const char *, int);
1767c478bd9Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *);
1777c478bd9Sstevel@tonic-gate 
178b096140dSahl static fasttrap_proc_t *fasttrap_proc_lookup(pid_t);
179b096140dSahl static void fasttrap_proc_release(fasttrap_proc_t *);
180b096140dSahl 
1817c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVS_INDEX(pid, name) \
1827c478bd9Sstevel@tonic-gate 	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
1837c478bd9Sstevel@tonic-gate 
184b096140dSahl #define	FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask)
185b096140dSahl 
1867c478bd9Sstevel@tonic-gate static int
fasttrap_highbit(ulong_t i)1877c478bd9Sstevel@tonic-gate fasttrap_highbit(ulong_t i)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int h = 1;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (i == 0)
1927c478bd9Sstevel@tonic-gate 		return (0);
1937c478bd9Sstevel@tonic-gate #ifdef _LP64
1947c478bd9Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
1957c478bd9Sstevel@tonic-gate 		h += 32; i >>= 32;
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000) {
1997c478bd9Sstevel@tonic-gate 		h += 16; i >>= 16;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	if (i & 0xff00) {
2027c478bd9Sstevel@tonic-gate 		h += 8; i >>= 8;
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	if (i & 0xf0) {
2057c478bd9Sstevel@tonic-gate 		h += 4; i >>= 4;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	if (i & 0xc) {
2087c478bd9Sstevel@tonic-gate 		h += 2; i >>= 2;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	if (i & 0x2) {
2117c478bd9Sstevel@tonic-gate 		h += 1;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	return (h);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static uint_t
fasttrap_hash_str(const char * p)2177c478bd9Sstevel@tonic-gate fasttrap_hash_str(const char *p)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	unsigned int g;
2207c478bd9Sstevel@tonic-gate 	uint_t hval = 0;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	while (*p) {
2237c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *p++;
2247c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
2257c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
2267c478bd9Sstevel@tonic-gate 		hval &= ~g;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	return (hval);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate void
fasttrap_sigtrap(proc_t * p,kthread_t * t,uintptr_t pc)2327c478bd9Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGTRAP;
2377c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_code = TRAP_DTRACE;
2387c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)pc;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2417c478bd9Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
2427c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (t != NULL)
2457c478bd9Sstevel@tonic-gate 		aston(t);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * This function ensures that no threads are actively using the memory
2507c478bd9Sstevel@tonic-gate  * associated with probes that were formerly live.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static void
fasttrap_mod_barrier(uint64_t gen)2537c478bd9Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	int i;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (gen < fasttrap_mod_gen)
2587c478bd9Sstevel@tonic-gate 		return;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	fasttrap_mod_gen++;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
2637c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_core[i].cpuc_pid_lock);
2647c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_core[i].cpuc_pid_lock);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * This is the timeout's callback for cleaning up the providers and their
2707c478bd9Sstevel@tonic-gate  * probes.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2737c478bd9Sstevel@tonic-gate static void
fasttrap_pid_cleanup_cb(void * data)2747c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	fasttrap_provider_t **fpp, *fp;
2777c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
2787c478bd9Sstevel@tonic-gate 	dtrace_provider_id_t provid;
279f484800dSBryan Cantrill 	int i, later, rval;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	static volatile int in = 0;
2827c478bd9Sstevel@tonic-gate 	ASSERT(in == 0);
2837c478bd9Sstevel@tonic-gate 	in = 1;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
2867c478bd9Sstevel@tonic-gate 	while (fasttrap_cleanup_work) {
2877c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 0;
2887c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		later = 0;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		/*
2937c478bd9Sstevel@tonic-gate 		 * Iterate over all the providers trying to remove the marked
294b096140dSahl 		 * ones. If a provider is marked but not retired, we just
2957c478bd9Sstevel@tonic-gate 		 * have to take a crack at removing it -- it's no big deal if
2967c478bd9Sstevel@tonic-gate 		 * we can't.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 		for (i = 0; i < fasttrap_provs.fth_nent; i++) {
2997c478bd9Sstevel@tonic-gate 			bucket = &fasttrap_provs.fth_table[i];
3007c478bd9Sstevel@tonic-gate 			mutex_enter(&bucket->ftb_mtx);
3017c478bd9Sstevel@tonic-gate 			fpp = (fasttrap_provider_t **)&bucket->ftb_data;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			while ((fp = *fpp) != NULL) {
3047c478bd9Sstevel@tonic-gate 				if (!fp->ftp_marked) {
3057c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3067c478bd9Sstevel@tonic-gate 					continue;
3077c478bd9Sstevel@tonic-gate 				}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 				mutex_enter(&fp->ftp_mtx);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 				/*
312f498645aSahl 				 * If this provider has consumers actively
313f498645aSahl 				 * creating probes (ftp_ccount) or is a USDT
314f498645aSahl 				 * provider (ftp_mcount), we can't unregister
315f498645aSahl 				 * or even condense.
3167c478bd9Sstevel@tonic-gate 				 */
317ab9a77c7Sahl 				if (fp->ftp_ccount != 0 ||
318ab9a77c7Sahl 				    fp->ftp_mcount != 0) {
3197c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->ftp_mtx);
3207c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
3217c478bd9Sstevel@tonic-gate 					continue;
3227c478bd9Sstevel@tonic-gate 				}
3237c478bd9Sstevel@tonic-gate 
324b096140dSahl 				if (!fp->ftp_retired || fp->ftp_rcount != 0)
3257c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 				mutex_exit(&fp->ftp_mtx);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 				/*
3307c478bd9Sstevel@tonic-gate 				 * If we successfully unregister this
3317c478bd9Sstevel@tonic-gate 				 * provider we can remove it from the hash
3327c478bd9Sstevel@tonic-gate 				 * chain and free the memory. If our attempt
333b096140dSahl 				 * to unregister fails and this is a retired
3347c478bd9Sstevel@tonic-gate 				 * provider, increment our flag to try again
3357c478bd9Sstevel@tonic-gate 				 * pretty soon. If we've consumed more than
3367c478bd9Sstevel@tonic-gate 				 * half of our total permitted number of
3377c478bd9Sstevel@tonic-gate 				 * probes call dtrace_condense() to try to
3387c478bd9Sstevel@tonic-gate 				 * clean out the unenabled probes.
3397c478bd9Sstevel@tonic-gate 				 */
3407c478bd9Sstevel@tonic-gate 				provid = fp->ftp_provid;
341f484800dSBryan Cantrill 				if ((rval = dtrace_unregister(provid)) != 0) {
3427c478bd9Sstevel@tonic-gate 					if (fasttrap_total > fasttrap_max / 2)
3437c478bd9Sstevel@tonic-gate 						(void) dtrace_condense(provid);
344f484800dSBryan Cantrill 
345f484800dSBryan Cantrill 					if (rval == EAGAIN)
346f484800dSBryan Cantrill 						fp->ftp_marked = 1;
347f484800dSBryan Cantrill 
3487c478bd9Sstevel@tonic-gate 					later += fp->ftp_marked;
3497c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
3507c478bd9Sstevel@tonic-gate 				} else {
3517c478bd9Sstevel@tonic-gate 					*fpp = fp->ftp_next;
3527c478bd9Sstevel@tonic-gate 					fasttrap_provider_free(fp);
3537c478bd9Sstevel@tonic-gate 				}
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_timeout != 0);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
364b096140dSahl 	 * If we were unable to remove a retired provider, try again after
3657c478bd9Sstevel@tonic-gate 	 * a second. This situation can occur in certain circumstances where
3667c478bd9Sstevel@tonic-gate 	 * providers cannot be unregistered even though they have no probes
3677c478bd9Sstevel@tonic-gate 	 * enabled because of an execution of dtrace -l or something similar.
3687c478bd9Sstevel@tonic-gate 	 * If the timeout has been disabled (set to 1 because we're trying
3697c478bd9Sstevel@tonic-gate 	 * to detach), we set fasttrap_cleanup_work to ensure that we'll
3707c478bd9Sstevel@tonic-gate 	 * get a chance to do that work if and when the timeout is reenabled
3717c478bd9Sstevel@tonic-gate 	 * (if detach fails).
3727c478bd9Sstevel@tonic-gate 	 */
373f484800dSBryan Cantrill 	if (later > 0) {
374f484800dSBryan Cantrill 		if (fasttrap_timeout != (timeout_id_t)1) {
375f484800dSBryan Cantrill 			fasttrap_timeout =
376f484800dSBryan Cantrill 			    timeout(&fasttrap_pid_cleanup_cb, NULL, hz);
377f484800dSBryan Cantrill 		}
378f484800dSBryan Cantrill 
3797c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 1;
380f484800dSBryan Cantrill 	} else {
3817c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
382f484800dSBryan Cantrill 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3857c478bd9Sstevel@tonic-gate 	in = 0;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * Activates the asynchronous cleanup mechanism.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate static void
fasttrap_pid_cleanup(void)3927c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup(void)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
3957c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 1;
3967c478bd9Sstevel@tonic-gate 	if (fasttrap_timeout == 0)
3977c478bd9Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1);
3987c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * This is called from cfork() via dtrace_fasttrap_fork(). The child
4036009dbc6Sahl  * process's address space is (roughly) a copy of the parent process's so
4047c478bd9Sstevel@tonic-gate  * we have to remove all the instrumentation we had previously enabled in the
4057c478bd9Sstevel@tonic-gate  * parent.
4067c478bd9Sstevel@tonic-gate  */
4077c478bd9Sstevel@tonic-gate static void
fasttrap_fork(proc_t * p,proc_t * cp)4087c478bd9Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	pid_t ppid = p->p_pid;
4117c478bd9Sstevel@tonic-gate 	int i;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	ASSERT(curproc == p);
4147c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
4157c478bd9Sstevel@tonic-gate 	ASSERT(p->p_dtrace_count > 0);
4167c478bd9Sstevel@tonic-gate 	ASSERT(cp->p_dtrace_count == 0);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	/*
4197c478bd9Sstevel@tonic-gate 	 * This would be simpler and faster if we maintained per-process
4207c478bd9Sstevel@tonic-gate 	 * hash tables of enabled tracepoints. It could, however, potentially
4217c478bd9Sstevel@tonic-gate 	 * slow down execution of a tracepoint since we'd need to go
4227c478bd9Sstevel@tonic-gate 	 * through two levels of indirection. In the future, we should
4237c478bd9Sstevel@tonic-gate 	 * consider either maintaining per-process ancillary lists of
4247c478bd9Sstevel@tonic-gate 	 * enabled tracepoints or hanging a pointer to a per-process hash
4257c478bd9Sstevel@tonic-gate 	 * table of enabled tracepoints off the proc structure.
4267c478bd9Sstevel@tonic-gate 	 */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * We don't have to worry about the child process disappearing
4307c478bd9Sstevel@tonic-gate 	 * because we're in fork().
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4337c478bd9Sstevel@tonic-gate 	sprlock_proc(cp);
4347c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * Iterate over every tracepoint looking for ones that belong to the
4387c478bd9Sstevel@tonic-gate 	 * parent process, and remove each from the child process.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
4417c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
4427c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
4457c478bd9Sstevel@tonic-gate 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
446b096140dSahl 			if (tp->ftt_pid == ppid &&
447038dc6b3Sahl 			    tp->ftt_proc->ftpc_acount != 0) {
4487c478bd9Sstevel@tonic-gate 				int ret = fasttrap_tracepoint_remove(cp, tp);
4497c478bd9Sstevel@tonic-gate 				ASSERT(ret == 0);
4506009dbc6Sahl 
4516009dbc6Sahl 				/*
4526009dbc6Sahl 				 * The count of active providers can only be
4536009dbc6Sahl 				 * decremented (i.e. to zero) during exec,
4546009dbc6Sahl 				 * exit, and removal of a meta provider so it
4556009dbc6Sahl 				 * should be impossible to drop the count
4566009dbc6Sahl 				 * mid-fork.
4576009dbc6Sahl 				 */
4586009dbc6Sahl 				ASSERT(tp->ftt_proc->ftpc_acount != 0);
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
4657c478bd9Sstevel@tonic-gate 	sprunlock(cp);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * This is called from proc_exit() or from exec_common() if p_dtrace_probes
4707c478bd9Sstevel@tonic-gate  * is set on the proc structure to indicate that there is a pid provider
4717c478bd9Sstevel@tonic-gate  * associated with this process.
4727c478bd9Sstevel@tonic-gate  */
4737c478bd9Sstevel@tonic-gate static void
fasttrap_exec_exit(proc_t * p)4747c478bd9Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p)
4757c478bd9Sstevel@tonic-gate {
4767c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);
4777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * We clean up the pid provider for this process here; user-land
4837c478bd9Sstevel@tonic-gate 	 * static probes are handled by the meta-provider remove entry point.
4847c478bd9Sstevel@tonic-gate 	 */
485dafb5540Sahl 	fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4927c478bd9Sstevel@tonic-gate static void
fasttrap_pid_provide(void * arg,const dtrace_probedesc_t * desc)4937c478bd9Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * There are no "default" pid probes.
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static int
fasttrap_tracepoint_enable(proc_t * p,fasttrap_probe_t * probe,uint_t index)5017c478bd9Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, *new_tp = NULL;
5047c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
5057c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
5067c478bd9Sstevel@tonic-gate 	pid_t pid;
5077c478bd9Sstevel@tonic-gate 	uintptr_t pc;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
5127c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
5137c478bd9Sstevel@tonic-gate 	id = &probe->ftp_tps[index].fit_id;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Before we make any modifications, make sure we've imposed a barrier
5217c478bd9Sstevel@tonic-gate 	 * on the generation in which this probe was last modified.
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	fasttrap_mod_barrier(probe->ftp_gen);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * If the tracepoint has already been enabled, just add our id to the
5297c478bd9Sstevel@tonic-gate 	 * list of interested probes. This may be our second time through
5307c478bd9Sstevel@tonic-gate 	 * this path in which case we'll have constructed the tracepoint we'd
5317c478bd9Sstevel@tonic-gate 	 * like to install. If we can't find a match, and have an allocated
5327c478bd9Sstevel@tonic-gate 	 * tracepoint ready to go, enable that one now.
5337c478bd9Sstevel@tonic-gate 	 *
534ac448965Sahl 	 * A tracepoint whose process is defunct is also considered defunct.
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate again:
5377c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
5387c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
5396009dbc6Sahl 		/*
5406009dbc6Sahl 		 * Note that it's safe to access the active count on the
5416009dbc6Sahl 		 * associated proc structure because we know that at least one
5426009dbc6Sahl 		 * provider (this one) will still be around throughout this
5436009dbc6Sahl 		 * operation.
5446009dbc6Sahl 		 */
5457c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
546038dc6b3Sahl 		    tp->ftt_proc->ftpc_acount == 0)
5477c478bd9Sstevel@tonic-gate 			continue;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		/*
5507c478bd9Sstevel@tonic-gate 		 * Now that we've found a matching tracepoint, it would be
5517c478bd9Sstevel@tonic-gate 		 * a decent idea to confirm that the tracepoint is still
5527c478bd9Sstevel@tonic-gate 		 * enabled and the trap instruction hasn't been overwritten.
5537c478bd9Sstevel@tonic-gate 		 * Since this is a little hairy, we'll punt for now.
5547c478bd9Sstevel@tonic-gate 		 */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * This can't be the first interested probe. We don't have
5587c478bd9Sstevel@tonic-gate 		 * to worry about another thread being in the midst of
5597c478bd9Sstevel@tonic-gate 		 * deleting this tracepoint (which would be the only valid
5607c478bd9Sstevel@tonic-gate 		 * reason for a tracepoint to have no interested probes)
5617c478bd9Sstevel@tonic-gate 		 * since we're holding P_PR_LOCK for this process.
5627c478bd9Sstevel@tonic-gate 		 */
5637c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
5647c478bd9Sstevel@tonic-gate 
565ac448965Sahl 		switch (id->fti_ptype) {
566ac448965Sahl 		case DTFTP_ENTRY:
567ac448965Sahl 		case DTFTP_OFFSETS:
568ac448965Sahl 		case DTFTP_IS_ENABLED:
5697c478bd9Sstevel@tonic-gate 			id->fti_next = tp->ftt_ids;
5707c478bd9Sstevel@tonic-gate 			membar_producer();
5717c478bd9Sstevel@tonic-gate 			tp->ftt_ids = id;
5727c478bd9Sstevel@tonic-gate 			membar_producer();
573ac448965Sahl 			break;
574ac448965Sahl 
575ac448965Sahl 		case DTFTP_RETURN:
576ac448965Sahl 		case DTFTP_POST_OFFSETS:
577ac448965Sahl 			id->fti_next = tp->ftt_retids;
578ac448965Sahl 			membar_producer();
579ac448965Sahl 			tp->ftt_retids = id;
580ac448965Sahl 			membar_producer();
581ac448965Sahl 			break;
582ac448965Sahl 
583ac448965Sahl 		default:
584ac448965Sahl 			ASSERT(0);
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		if (new_tp != NULL) {
5907c478bd9Sstevel@tonic-gate 			new_tp->ftt_ids = NULL;
5917c478bd9Sstevel@tonic-gate 			new_tp->ftt_retids = NULL;
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		return (0);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * If we have a good tracepoint ready to go, install it now while
5997c478bd9Sstevel@tonic-gate 	 * we have the lock held and no one can screw with us.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	if (new_tp != NULL) {
6028f5db3e7Sahl 		int rc = 0;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		new_tp->ftt_next = bucket->ftb_data;
6057c478bd9Sstevel@tonic-gate 		membar_producer();
6067c478bd9Sstevel@tonic-gate 		bucket->ftb_data = new_tp;
6077c478bd9Sstevel@tonic-gate 		membar_producer();
6087c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		/*
6118f5db3e7Sahl 		 * Activate the tracepoint in the ISA-specific manner.
6128f5db3e7Sahl 		 * If this fails, we need to report the failure, but
6138f5db3e7Sahl 		 * indicate that this tracepoint must still be disabled
6148f5db3e7Sahl 		 * by calling fasttrap_tracepoint_disable().
6157c478bd9Sstevel@tonic-gate 		 */
6168f5db3e7Sahl 		if (fasttrap_tracepoint_install(p, new_tp) != 0)
6178f5db3e7Sahl 			rc = FASTTRAP_ENABLE_PARTIAL;
6188f5db3e7Sahl 
6198f5db3e7Sahl 		/*
6208f5db3e7Sahl 		 * Increment the count of the number of tracepoints active in
6218f5db3e7Sahl 		 * the victim process.
6228f5db3e7Sahl 		 */
6238f5db3e7Sahl 		ASSERT(p->p_proc_flag & P_PR_LOCK);
6248f5db3e7Sahl 		p->p_dtrace_count++;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		return (rc);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * Initialize the tracepoint that's been preallocated with the probe.
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	new_tp = probe->ftp_tps[index].fit_tp;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pid == pid);
6377c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pc == pc);
638b096140dSahl 	ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc);
6397c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_ids == NULL);
6407c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_retids == NULL);
6417c478bd9Sstevel@tonic-gate 
642ac448965Sahl 	switch (id->fti_ptype) {
643ac448965Sahl 	case DTFTP_ENTRY:
644ac448965Sahl 	case DTFTP_OFFSETS:
645ac448965Sahl 	case DTFTP_IS_ENABLED:
6467c478bd9Sstevel@tonic-gate 		id->fti_next = NULL;
6477c478bd9Sstevel@tonic-gate 		new_tp->ftt_ids = id;
648ac448965Sahl 		break;
649ac448965Sahl 
650ac448965Sahl 	case DTFTP_RETURN:
651ac448965Sahl 	case DTFTP_POST_OFFSETS:
652ac448965Sahl 		id->fti_next = NULL;
653ac448965Sahl 		new_tp->ftt_retids = id;
654ac448965Sahl 		break;
655ac448965Sahl 
656ac448965Sahl 	default:
657ac448965Sahl 		ASSERT(0);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6618f5db3e7Sahl 	 * If the ISA-dependent initialization goes to plan, go back to the
6627c478bd9Sstevel@tonic-gate 	 * beginning and try to install this freshly made tracepoint.
6637c478bd9Sstevel@tonic-gate 	 */
664ac448965Sahl 	if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0)
6657c478bd9Sstevel@tonic-gate 		goto again;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	new_tp->ftt_ids = NULL;
6687c478bd9Sstevel@tonic-gate 	new_tp->ftt_retids = NULL;
6697c478bd9Sstevel@tonic-gate 
6708f5db3e7Sahl 	return (FASTTRAP_ENABLE_FAIL);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static void
fasttrap_tracepoint_disable(proc_t * p,fasttrap_probe_t * probe,uint_t index)6747c478bd9Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
6777c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
6787c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t **pp, *tp;
6797c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id, **idp;
6807c478bd9Sstevel@tonic-gate 	pid_t pid;
6817c478bd9Sstevel@tonic-gate 	uintptr_t pc;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
6867c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
687ac448965Sahl 	id = &probe->ftp_tps[index].fit_id;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Find the tracepoint and make sure that our id is one of the
6937c478bd9Sstevel@tonic-gate 	 * ones registered with it.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
6967c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
6977c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
6987c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
699b096140dSahl 		    tp->ftt_proc == provider->ftp_proc)
7007c478bd9Sstevel@tonic-gate 			break;
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * If we somehow lost this tracepoint, we're in a world of hurt.
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
7077c478bd9Sstevel@tonic-gate 
708ac448965Sahl 	switch (id->fti_ptype) {
709ac448965Sahl 	case DTFTP_ENTRY:
710ac448965Sahl 	case DTFTP_OFFSETS:
711ac448965Sahl 	case DTFTP_IS_ENABLED:
7127c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL);
7137c478bd9Sstevel@tonic-gate 		idp = &tp->ftt_ids;
714ac448965Sahl 		break;
715ac448965Sahl 
716ac448965Sahl 	case DTFTP_RETURN:
717ac448965Sahl 	case DTFTP_POST_OFFSETS:
718ac448965Sahl 		ASSERT(tp->ftt_retids != NULL);
719ac448965Sahl 		idp = &tp->ftt_retids;
720ac448965Sahl 		break;
721ac448965Sahl 
722ac448965Sahl 	default:
723ac448965Sahl 		ASSERT(0);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	while ((*idp)->fti_probe != probe) {
7277c478bd9Sstevel@tonic-gate 		idp = &(*idp)->fti_next;
7287c478bd9Sstevel@tonic-gate 		ASSERT(*idp != NULL);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	id = *idp;
7327c478bd9Sstevel@tonic-gate 	*idp = id->fti_next;
7337c478bd9Sstevel@tonic-gate 	membar_producer();
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	ASSERT(id->fti_probe == probe);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/*
7387c478bd9Sstevel@tonic-gate 	 * If there are other registered enablings of this tracepoint, we're
7397c478bd9Sstevel@tonic-gate 	 * all done, but if this was the last probe assocated with this
7407c478bd9Sstevel@tonic-gate 	 * this tracepoint, we need to remove and free it.
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		/*
7457c478bd9Sstevel@tonic-gate 		 * If the current probe's tracepoint is in use, swap it
7467c478bd9Sstevel@tonic-gate 		 * for an unused tracepoint.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		if (tp == probe->ftp_tps[index].fit_tp) {
7497c478bd9Sstevel@tonic-gate 			fasttrap_probe_t *tmp_probe;
7507c478bd9Sstevel@tonic-gate 			fasttrap_tracepoint_t **tmp_tp;
7517c478bd9Sstevel@tonic-gate 			uint_t tmp_index;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			if (tp->ftt_ids != NULL) {
7547c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_ids->fti_probe;
75573427c57Sahl 				/* LINTED - alignment */
7567c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
7577c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7587c478bd9Sstevel@tonic-gate 			} else {
7597c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_retids->fti_probe;
76073427c57Sahl 				/* LINTED - alignment */
7617c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
7627c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
7637c478bd9Sstevel@tonic-gate 			}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != NULL);
7667c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
7677c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_ids == NULL);
7687c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_retids == NULL);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			probe->ftp_tps[index].fit_tp = *tmp_tp;
7717c478bd9Sstevel@tonic-gate 			*tmp_tp = tp;
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		/*
7777c478bd9Sstevel@tonic-gate 		 * Tag the modified probe with the generation in which it was
7787c478bd9Sstevel@tonic-gate 		 * changed.
7797c478bd9Sstevel@tonic-gate 		 */
7807c478bd9Sstevel@tonic-gate 		probe->ftp_gen = fasttrap_mod_gen;
7817c478bd9Sstevel@tonic-gate 		return;
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	/*
7877c478bd9Sstevel@tonic-gate 	 * We can't safely remove the tracepoint from the set of active
7887c478bd9Sstevel@tonic-gate 	 * tracepoints until we've actually removed the fasttrap instruction
7897c478bd9Sstevel@tonic-gate 	 * from the process's text. We can, however, operate on this
7907c478bd9Sstevel@tonic-gate 	 * tracepoint secure in the knowledge that no other thread is going to
7917c478bd9Sstevel@tonic-gate 	 * be looking at it since we hold P_PR_LOCK on the process if it's
7927c478bd9Sstevel@tonic-gate 	 * live or we hold the provider lock on the process if it's dead and
7937c478bd9Sstevel@tonic-gate 	 * gone.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * We only need to remove the actual instruction if we're looking
7987c478bd9Sstevel@tonic-gate 	 * at an existing process
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	if (p != NULL) {
8017c478bd9Sstevel@tonic-gate 		/*
8027c478bd9Sstevel@tonic-gate 		 * If we fail to restore the instruction we need to kill
8037c478bd9Sstevel@tonic-gate 		 * this process since it's in a completely unrecoverable
8047c478bd9Sstevel@tonic-gate 		 * state.
8057c478bd9Sstevel@tonic-gate 		 */
8067c478bd9Sstevel@tonic-gate 		if (fasttrap_tracepoint_remove(p, tp) != 0)
8077c478bd9Sstevel@tonic-gate 			fasttrap_sigtrap(p, NULL, pc);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		/*
8107c478bd9Sstevel@tonic-gate 		 * Decrement the count of the number of tracepoints active
8117c478bd9Sstevel@tonic-gate 		 * in the victim process.
8127c478bd9Sstevel@tonic-gate 		 */
8137c478bd9Sstevel@tonic-gate 		ASSERT(p->p_proc_flag & P_PR_LOCK);
8147c478bd9Sstevel@tonic-gate 		p->p_dtrace_count--;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * Remove the probe from the hash table of active tracepoints.
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
8217c478bd9Sstevel@tonic-gate 	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
8227c478bd9Sstevel@tonic-gate 	ASSERT(*pp != NULL);
8237c478bd9Sstevel@tonic-gate 	while (*pp != tp) {
8247c478bd9Sstevel@tonic-gate 		pp = &(*pp)->ftt_next;
8257c478bd9Sstevel@tonic-gate 		ASSERT(*pp != NULL);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	*pp = tp->ftt_next;
8297c478bd9Sstevel@tonic-gate 	membar_producer();
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/*
8347c478bd9Sstevel@tonic-gate 	 * Tag the modified probe with the generation in which it was changed.
8357c478bd9Sstevel@tonic-gate 	 */
8367c478bd9Sstevel@tonic-gate 	probe->ftp_gen = fasttrap_mod_gen;
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate static void
fasttrap_enable_callbacks(void)840f498645aSahl fasttrap_enable_callbacks(void)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * We don't have to play the rw lock game here because we're
8447c478bd9Sstevel@tonic-gate 	 * providing something rather than taking something away --
8457c478bd9Sstevel@tonic-gate 	 * we can be sure that no threads have tried to follow this
8467c478bd9Sstevel@tonic-gate 	 * function pointer yet.
8477c478bd9Sstevel@tonic-gate 	 */
8487c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
849f498645aSahl 	if (fasttrap_pid_count == 0) {
850f498645aSahl 		ASSERT(dtrace_pid_probe_ptr == NULL);
851f498645aSahl 		ASSERT(dtrace_return_probe_ptr == NULL);
852f498645aSahl 		dtrace_pid_probe_ptr = &fasttrap_pid_probe;
853f498645aSahl 		dtrace_return_probe_ptr = &fasttrap_return_probe;
8547c478bd9Sstevel@tonic-gate 	}
855f498645aSahl 	ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe);
856f498645aSahl 	ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe);
857f498645aSahl 	fasttrap_pid_count++;
8587c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate static void
fasttrap_disable_callbacks(void)862f498645aSahl fasttrap_disable_callbacks(void)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
867f498645aSahl 	ASSERT(fasttrap_pid_count > 0);
868f498645aSahl 	fasttrap_pid_count--;
869f498645aSahl 	if (fasttrap_pid_count == 0) {
8707c478bd9Sstevel@tonic-gate 		cpu_t *cur, *cpu = CPU;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8739e8164f5Sahl 		    cur = cur->cpu_next_onln) {
8747c478bd9Sstevel@tonic-gate 			rw_enter(&cur->cpu_ft_lock, RW_WRITER);
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 
877f498645aSahl 		dtrace_pid_probe_ptr = NULL;
878f498645aSahl 		dtrace_return_probe_ptr = NULL;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
8819e8164f5Sahl 		    cur = cur->cpu_next_onln) {
8827c478bd9Sstevel@tonic-gate 			rw_exit(&cur->cpu_ft_lock);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate /*ARGSUSED*/
889b9e93c10SJonathan Haslam static int
fasttrap_pid_enable(void * arg,dtrace_id_t id,void * parg)8907c478bd9Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
8937c478bd9Sstevel@tonic-gate 	proc_t *p;
8948f5db3e7Sahl 	int i, rc;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
8977c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
8987c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
8997c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * Increment the count of enabled probes on this probe's provider;
9037c478bd9Sstevel@tonic-gate 	 * the provider can't go away while the probe still exists. We
9047c478bd9Sstevel@tonic-gate 	 * must increment this even if we aren't able to properly enable
9057c478bd9Sstevel@tonic-gate 	 * this probe.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	mutex_enter(&probe->ftp_prov->ftp_mtx);
9087c478bd9Sstevel@tonic-gate 	probe->ftp_prov->ftp_rcount++;
9097c478bd9Sstevel@tonic-gate 	mutex_exit(&probe->ftp_prov->ftp_mtx);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	/*
912ab9a77c7Sahl 	 * If this probe's provider is retired (meaning it was valid in a
913ab9a77c7Sahl 	 * previously exec'ed incarnation of this address space), bail out. The
914ab9a77c7Sahl 	 * provider can't go away while we're in this code path.
9157c478bd9Sstevel@tonic-gate 	 */
916ab9a77c7Sahl 	if (probe->ftp_prov->ftp_retired)
917b9e93c10SJonathan Haslam 		return (0);
9187c478bd9Sstevel@tonic-gate 
919ab9a77c7Sahl 	/*
920ab9a77c7Sahl 	 * If we can't find the process, it may be that we're in the context of
921ab9a77c7Sahl 	 * a fork in which the traced process is being born and we're copying
922ab9a77c7Sahl 	 * USDT probes. Otherwise, the process is gone so bail.
923ab9a77c7Sahl 	 */
924ab9a77c7Sahl 	if ((p = sprlock(probe->ftp_pid)) == NULL) {
925ab9a77c7Sahl 		if ((curproc->p_flag & SFORKING) == 0)
926b9e93c10SJonathan Haslam 			return (0);
927ab9a77c7Sahl 
928ab9a77c7Sahl 		mutex_enter(&pidlock);
929ab9a77c7Sahl 		p = prfind(probe->ftp_pid);
930ab9a77c7Sahl 
9319df7e4e1SBryan Cantrill 		if (p == NULL) {
9329df7e4e1SBryan Cantrill 			/*
9339df7e4e1SBryan Cantrill 			 * So it's not that the target process is being born,
9349df7e4e1SBryan Cantrill 			 * it's that it isn't there at all (and we simply
9359df7e4e1SBryan Cantrill 			 * happen to be forking).  Anyway, we know that the
9369df7e4e1SBryan Cantrill 			 * target is definitely gone, so bail out.
9379df7e4e1SBryan Cantrill 			 */
9389df7e4e1SBryan Cantrill 			mutex_exit(&pidlock);
9399df7e4e1SBryan Cantrill 			return (0);
9409df7e4e1SBryan Cantrill 		}
9419df7e4e1SBryan Cantrill 
942ab9a77c7Sahl 		/*
943ab9a77c7Sahl 		 * Confirm that curproc is indeed forking the process in which
944ab9a77c7Sahl 		 * we're trying to enable probes.
945ab9a77c7Sahl 		 */
946ab9a77c7Sahl 		ASSERT(p->p_parent == curproc);
947ab9a77c7Sahl 		ASSERT(p->p_stat == SIDL);
948ab9a77c7Sahl 
949ab9a77c7Sahl 		mutex_enter(&p->p_lock);
950ab9a77c7Sahl 		mutex_exit(&pidlock);
951ab9a77c7Sahl 
952ab9a77c7Sahl 		sprlock_proc(p);
953ab9a77c7Sahl 	}
954ab9a77c7Sahl 
9557c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
9567c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/*
959f498645aSahl 	 * We have to enable the trap entry point before any user threads have
9607c478bd9Sstevel@tonic-gate 	 * the chance to execute the trap instruction we're about to place
9617c478bd9Sstevel@tonic-gate 	 * in their process's text.
9627c478bd9Sstevel@tonic-gate 	 */
963f498645aSahl 	fasttrap_enable_callbacks();
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * Enable all the tracepoints and add this probe's id to each
9677c478bd9Sstevel@tonic-gate 	 * tracepoint's list of active probes.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
9708f5db3e7Sahl 		if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) {
9718f5db3e7Sahl 			/*
9728f5db3e7Sahl 			 * If enabling the tracepoint failed completely,
9738f5db3e7Sahl 			 * we don't have to disable it; if the failure
9748f5db3e7Sahl 			 * was only partial we must disable it.
9758f5db3e7Sahl 			 */
9768f5db3e7Sahl 			if (rc == FASTTRAP_ENABLE_FAIL)
9778f5db3e7Sahl 				i--;
9788f5db3e7Sahl 			else
9798f5db3e7Sahl 				ASSERT(rc == FASTTRAP_ENABLE_PARTIAL);
9808f5db3e7Sahl 
9817c478bd9Sstevel@tonic-gate 			/*
9827c478bd9Sstevel@tonic-gate 			 * Back up and pull out all the tracepoints we've
9837c478bd9Sstevel@tonic-gate 			 * created so far for this probe.
9847c478bd9Sstevel@tonic-gate 			 */
9858c642573Sahl 			while (i >= 0) {
9867c478bd9Sstevel@tonic-gate 				fasttrap_tracepoint_disable(p, probe, i);
9878c642573Sahl 				i--;
9887c478bd9Sstevel@tonic-gate 			}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
9917c478bd9Sstevel@tonic-gate 			sprunlock(p);
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 			/*
9947c478bd9Sstevel@tonic-gate 			 * Since we're not actually enabling this probe,
9957c478bd9Sstevel@tonic-gate 			 * drop our reference on the trap table entry.
9967c478bd9Sstevel@tonic-gate 			 */
997f498645aSahl 			fasttrap_disable_callbacks();
998b9e93c10SJonathan Haslam 			return (0);
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
10037c478bd9Sstevel@tonic-gate 	sprunlock(p);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 1;
1006b9e93c10SJonathan Haslam 	return (0);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10107c478bd9Sstevel@tonic-gate static void
fasttrap_pid_disable(void * arg,dtrace_id_t id,void * parg)10117c478bd9Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
10147c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
10157c478bd9Sstevel@tonic-gate 	proc_t *p;
10167c478bd9Sstevel@tonic-gate 	int i, whack = 0;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/*
10217c478bd9Sstevel@tonic-gate 	 * We won't be able to acquire a /proc-esque lock on the process
10227c478bd9Sstevel@tonic-gate 	 * iff the process is dead and gone. In this case, we rely on the
10237c478bd9Sstevel@tonic-gate 	 * provider lock as a point of mutual exclusion to prevent other
10247c478bd9Sstevel@tonic-gate 	 * DTrace consumers from disabling this probe.
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	if ((p = sprlock(probe->ftp_pid)) != NULL) {
10277c478bd9Sstevel@tonic-gate 		ASSERT(!(p->p_flag & SVFORK));
10287c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/*
103473427c57Sahl 	 * Disable all the associated tracepoints (for fully enabled probes).
10357c478bd9Sstevel@tonic-gate 	 */
103673427c57Sahl 	if (probe->ftp_enabled) {
103773427c57Sahl 		for (i = 0; i < probe->ftp_ntps; i++) {
103873427c57Sahl 			fasttrap_tracepoint_disable(p, probe, i);
103973427c57Sahl 		}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount > 0);
10437c478bd9Sstevel@tonic-gate 	provider->ftp_rcount--;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (p != NULL) {
10467c478bd9Sstevel@tonic-gate 		/*
10477c478bd9Sstevel@tonic-gate 		 * Even though we may not be able to remove it entirely, we
1048b096140dSahl 		 * mark this retired provider to get a chance to remove some
10497c478bd9Sstevel@tonic-gate 		 * of the associated probes.
10507c478bd9Sstevel@tonic-gate 		 */
1051b096140dSahl 		if (provider->ftp_retired && !provider->ftp_marked)
10527c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10537c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
10567c478bd9Sstevel@tonic-gate 		sprunlock(p);
10577c478bd9Sstevel@tonic-gate 	} else {
10587c478bd9Sstevel@tonic-gate 		/*
10597c478bd9Sstevel@tonic-gate 		 * If the process is dead, we're just waiting for the
10607c478bd9Sstevel@tonic-gate 		 * last probe to be disabled to be able to free it.
10617c478bd9Sstevel@tonic-gate 		 */
10627c478bd9Sstevel@tonic-gate 		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
10637c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
10647c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
10657c478bd9Sstevel@tonic-gate 	}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (whack)
10687c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
10697c478bd9Sstevel@tonic-gate 
107073427c57Sahl 	if (!probe->ftp_enabled)
107173427c57Sahl 		return;
107273427c57Sahl 
10737c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 0;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1076f498645aSahl 	fasttrap_disable_callbacks();
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10807c478bd9Sstevel@tonic-gate static void
fasttrap_pid_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)10817c478bd9Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
10827c478bd9Sstevel@tonic-gate     dtrace_argdesc_t *desc)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
10857c478bd9Sstevel@tonic-gate 	char *str;
10869e8164f5Sahl 	int i, ndx;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	desc->dtargd_native[0] = '\0';
10897c478bd9Sstevel@tonic-gate 	desc->dtargd_xlate[0] = '\0';
10907c478bd9Sstevel@tonic-gate 
1091b096140dSahl 	if (probe->ftp_prov->ftp_retired != 0 ||
10927c478bd9Sstevel@tonic-gate 	    desc->dtargd_ndx >= probe->ftp_nargs) {
10937c478bd9Sstevel@tonic-gate 		desc->dtargd_ndx = DTRACE_ARGNONE;
10947c478bd9Sstevel@tonic-gate 		return;
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10979e8164f5Sahl 	ndx = (probe->ftp_argmap != NULL) ?
10989e8164f5Sahl 	    probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	str = probe->ftp_ntypes;
11019e8164f5Sahl 	for (i = 0; i < ndx; i++) {
11027c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
11067c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_native, str);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (probe->ftp_xtypes == NULL)
11097c478bd9Sstevel@tonic-gate 		return;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	str = probe->ftp_xtypes;
11127c478bd9Sstevel@tonic-gate 	for (i = 0; i < desc->dtargd_ndx; i++) {
11137c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
11177c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_xlate, str);
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11217c478bd9Sstevel@tonic-gate static void
fasttrap_pid_destroy(void * arg,dtrace_id_t id,void * parg)11227c478bd9Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
11257c478bd9Sstevel@tonic-gate 	int i;
11267c478bd9Sstevel@tonic-gate 	size_t size;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
11297c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
11307c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_total >= probe->ftp_ntps);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
1133ab9a77c7Sahl 	size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
11367c478bd9Sstevel@tonic-gate 		fasttrap_mod_barrier(probe->ftp_gen);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
11397c478bd9Sstevel@tonic-gate 		kmem_free(probe->ftp_tps[i].fit_tp,
11407c478bd9Sstevel@tonic-gate 		    sizeof (fasttrap_tracepoint_t));
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	kmem_free(probe, size);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = {
11487c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11497c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11507c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11517c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
11527c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
11537c478bd9Sstevel@tonic-gate };
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate static dtrace_pops_t pid_pops = {
11567c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
11577c478bd9Sstevel@tonic-gate 	NULL,
11587c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
11597c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
11607c478bd9Sstevel@tonic-gate 	NULL,
11617c478bd9Sstevel@tonic-gate 	NULL,
11627c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
1163f498645aSahl 	fasttrap_pid_getarg,
11647c478bd9Sstevel@tonic-gate 	NULL,
11657c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
11667c478bd9Sstevel@tonic-gate };
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate static dtrace_pops_t usdt_pops = {
11697c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
11707c478bd9Sstevel@tonic-gate 	NULL,
11717c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
11727c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
11737c478bd9Sstevel@tonic-gate 	NULL,
11747c478bd9Sstevel@tonic-gate 	NULL,
11757c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
11767c478bd9Sstevel@tonic-gate 	fasttrap_usdt_getarg,
11777c478bd9Sstevel@tonic-gate 	NULL,
11787c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
11797c478bd9Sstevel@tonic-gate };
11807c478bd9Sstevel@tonic-gate 
1181b096140dSahl static fasttrap_proc_t *
fasttrap_proc_lookup(pid_t pid)1182b096140dSahl fasttrap_proc_lookup(pid_t pid)
1183b096140dSahl {
1184b096140dSahl 	fasttrap_bucket_t *bucket;
1185b096140dSahl 	fasttrap_proc_t *fprc, *new_fprc;
1186b096140dSahl 
1187b096140dSahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1188b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1189b096140dSahl 
1190b096140dSahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1191038dc6b3Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1192b096140dSahl 			mutex_enter(&fprc->ftpc_mtx);
1193b096140dSahl 			mutex_exit(&bucket->ftb_mtx);
1194038dc6b3Sahl 			fprc->ftpc_rcount++;
11951a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&fprc->ftpc_acount);
11966009dbc6Sahl 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1197b096140dSahl 			mutex_exit(&fprc->ftpc_mtx);
1198b096140dSahl 
1199b096140dSahl 			return (fprc);
1200b096140dSahl 		}
1201b096140dSahl 	}
1202b096140dSahl 
1203b096140dSahl 	/*
1204b096140dSahl 	 * Drop the bucket lock so we don't try to perform a sleeping
1205b096140dSahl 	 * allocation under it.
1206b096140dSahl 	 */
1207b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1208b096140dSahl 
1209b096140dSahl 	new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP);
1210b096140dSahl 	new_fprc->ftpc_pid = pid;
1211038dc6b3Sahl 	new_fprc->ftpc_rcount = 1;
1212038dc6b3Sahl 	new_fprc->ftpc_acount = 1;
1213b096140dSahl 
1214b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1215b096140dSahl 
1216b096140dSahl 	/*
1217b096140dSahl 	 * Take another lap through the list to make sure a proc hasn't
1218b096140dSahl 	 * been created for this pid while we weren't under the bucket lock.
1219b096140dSahl 	 */
1220b096140dSahl 	for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) {
1221038dc6b3Sahl 		if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) {
1222b096140dSahl 			mutex_enter(&fprc->ftpc_mtx);
1223b096140dSahl 			mutex_exit(&bucket->ftb_mtx);
1224038dc6b3Sahl 			fprc->ftpc_rcount++;
12251a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(&fprc->ftpc_acount);
12266009dbc6Sahl 			ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount);
1227b096140dSahl 			mutex_exit(&fprc->ftpc_mtx);
1228b096140dSahl 
1229b096140dSahl 			kmem_free(new_fprc, sizeof (fasttrap_proc_t));
1230b096140dSahl 
1231b096140dSahl 			return (fprc);
1232b096140dSahl 		}
1233b096140dSahl 	}
1234b096140dSahl 
1235b096140dSahl 	new_fprc->ftpc_next = bucket->ftb_data;
1236b096140dSahl 	bucket->ftb_data = new_fprc;
1237b096140dSahl 
1238b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1239b096140dSahl 
1240b096140dSahl 	return (new_fprc);
1241b096140dSahl }
1242b096140dSahl 
1243b096140dSahl static void
fasttrap_proc_release(fasttrap_proc_t * proc)1244b096140dSahl fasttrap_proc_release(fasttrap_proc_t *proc)
1245b096140dSahl {
1246b096140dSahl 	fasttrap_bucket_t *bucket;
1247b096140dSahl 	fasttrap_proc_t *fprc, **fprcp;
1248b096140dSahl 	pid_t pid = proc->ftpc_pid;
1249b096140dSahl 
1250b096140dSahl 	mutex_enter(&proc->ftpc_mtx);
1251b096140dSahl 
1252038dc6b3Sahl 	ASSERT(proc->ftpc_rcount != 0);
12536009dbc6Sahl 	ASSERT(proc->ftpc_acount <= proc->ftpc_rcount);
1254b096140dSahl 
1255038dc6b3Sahl 	if (--proc->ftpc_rcount != 0) {
1256b096140dSahl 		mutex_exit(&proc->ftpc_mtx);
1257b096140dSahl 		return;
1258b096140dSahl 	}
1259b096140dSahl 
1260b096140dSahl 	mutex_exit(&proc->ftpc_mtx);
1261b096140dSahl 
1262038dc6b3Sahl 	/*
1263038dc6b3Sahl 	 * There should definitely be no live providers associated with this
1264038dc6b3Sahl 	 * process at this point.
1265038dc6b3Sahl 	 */
1266038dc6b3Sahl 	ASSERT(proc->ftpc_acount == 0);
1267038dc6b3Sahl 
1268b096140dSahl 	bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)];
1269b096140dSahl 	mutex_enter(&bucket->ftb_mtx);
1270b096140dSahl 
1271b096140dSahl 	fprcp = (fasttrap_proc_t **)&bucket->ftb_data;
1272b096140dSahl 	while ((fprc = *fprcp) != NULL) {
1273b096140dSahl 		if (fprc == proc)
1274b096140dSahl 			break;
1275b096140dSahl 
1276b096140dSahl 		fprcp = &fprc->ftpc_next;
1277b096140dSahl 	}
1278b096140dSahl 
1279b096140dSahl 	/*
1280b096140dSahl 	 * Something strange has happened if we can't find the proc.
1281b096140dSahl 	 */
1282b096140dSahl 	ASSERT(fprc != NULL);
1283b096140dSahl 
1284b096140dSahl 	*fprcp = fprc->ftpc_next;
1285b096140dSahl 
1286b096140dSahl 	mutex_exit(&bucket->ftb_mtx);
1287b096140dSahl 
1288b096140dSahl 	kmem_free(fprc, sizeof (fasttrap_proc_t));
1289b096140dSahl }
1290b096140dSahl 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Lookup a fasttrap-managed provider based on its name and associated pid.
12937c478bd9Sstevel@tonic-gate  * If the pattr argument is non-NULL, this function instantiates the provider
12947c478bd9Sstevel@tonic-gate  * if it doesn't exist otherwise it returns NULL. The provider is returned
12957c478bd9Sstevel@tonic-gate  * with its lock held.
12967c478bd9Sstevel@tonic-gate  */
12977c478bd9Sstevel@tonic-gate static fasttrap_provider_t *
fasttrap_provider_lookup(pid_t pid,const char * name,const dtrace_pattr_t * pattr)12987c478bd9Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name,
12997c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *pattr)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *fp, *new_fp = NULL;
13027c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
13037c478bd9Sstevel@tonic-gate 	char provname[DTRACE_PROVNAMELEN];
13047c478bd9Sstevel@tonic-gate 	proc_t *p;
1305ad4023c4Sdp 	cred_t *cred;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1308dafb5540Sahl 	ASSERT(pattr != NULL);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
13117c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Take a lap through the list and return the match if we find it.
13157c478bd9Sstevel@tonic-gate 	 */
13167c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
13177c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1318b096140dSahl 		    !fp->ftp_retired) {
13197c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
13207c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
13217c478bd9Sstevel@tonic-gate 			return (fp);
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	/*
13267c478bd9Sstevel@tonic-gate 	 * Drop the bucket lock so we don't try to perform a sleeping
13277c478bd9Sstevel@tonic-gate 	 * allocation under it.
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * Make sure the process exists, isn't a child created as the result
1333ad4023c4Sdp 	 * of a vfork(2), and isn't a zombie (but may be in fork).
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
133697eda132Sraf 	if ((p = prfind(pid)) == NULL) {
13377c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
13387c478bd9Sstevel@tonic-gate 		return (NULL);
13397c478bd9Sstevel@tonic-gate 	}
13407c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
13417c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
134297eda132Sraf 	if (p->p_flag & (SVFORK | SEXITING)) {
134397eda132Sraf 		mutex_exit(&p->p_lock);
134497eda132Sraf 		return (NULL);
134597eda132Sraf 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * Increment p_dtrace_probes so that the process knows to inform us
13497c478bd9Sstevel@tonic-gate 	 * when it exits or execs. fasttrap_provider_free() decrements this
13507c478bd9Sstevel@tonic-gate 	 * when we're done with this provider.
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes++;
13537c478bd9Sstevel@tonic-gate 
1354ad4023c4Sdp 	/*
1355ad4023c4Sdp 	 * Grab the credentials for this process so we have
1356ad4023c4Sdp 	 * something to pass to dtrace_register().
1357ad4023c4Sdp 	 */
13587c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
1359ad4023c4Sdp 	crhold(p->p_cred);
1360ad4023c4Sdp 	cred = p->p_cred;
13617c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
13627c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1365b096140dSahl 	new_fp->ftp_pid = pid;
1366b096140dSahl 	new_fp->ftp_proc = fasttrap_proc_lookup(pid);
1367b096140dSahl 
1368b096140dSahl 	ASSERT(new_fp->ftp_proc != NULL);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/*
13737c478bd9Sstevel@tonic-gate 	 * Take another lap through the list to make sure a provider hasn't
13747c478bd9Sstevel@tonic-gate 	 * been created for this pid while we weren't under the bucket lock.
13757c478bd9Sstevel@tonic-gate 	 */
13767c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
13777c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1378b096140dSahl 		    !fp->ftp_retired) {
13797c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
13807c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
13817c478bd9Sstevel@tonic-gate 			fasttrap_provider_free(new_fp);
1382ad4023c4Sdp 			crfree(cred);
13837c478bd9Sstevel@tonic-gate 			return (fp);
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	(void) strcpy(new_fp->ftp_name, name);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Fail and return NULL if either the provider name is too long
13917c478bd9Sstevel@tonic-gate 	 * or we fail to register this new provider with the DTrace
13927c478bd9Sstevel@tonic-gate 	 * framework. Note that this is the only place we ever construct
13937c478bd9Sstevel@tonic-gate 	 * the full provider name -- we keep it in pieces in the provider
13947c478bd9Sstevel@tonic-gate 	 * structure.
13957c478bd9Sstevel@tonic-gate 	 */
13967c478bd9Sstevel@tonic-gate 	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
13977c478bd9Sstevel@tonic-gate 	    sizeof (provname) ||
13987c478bd9Sstevel@tonic-gate 	    dtrace_register(provname, pattr,
1399ad4023c4Sdp 	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred,
14007c478bd9Sstevel@tonic-gate 	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
14017c478bd9Sstevel@tonic-gate 	    &new_fp->ftp_provid) != 0) {
14027c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
14037c478bd9Sstevel@tonic-gate 		fasttrap_provider_free(new_fp);
1404ad4023c4Sdp 		crfree(cred);
14057c478bd9Sstevel@tonic-gate 		return (NULL);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	new_fp->ftp_next = bucket->ftb_data;
14097c478bd9Sstevel@tonic-gate 	bucket->ftb_data = new_fp;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	mutex_enter(&new_fp->ftp_mtx);
14127c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
14137c478bd9Sstevel@tonic-gate 
1414ad4023c4Sdp 	crfree(cred);
14157c478bd9Sstevel@tonic-gate 	return (new_fp);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate static void
fasttrap_provider_free(fasttrap_provider_t * provider)14197c478bd9Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	pid_t pid = provider->ftp_pid;
14227c478bd9Sstevel@tonic-gate 	proc_t *p;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	/*
1425ab9a77c7Sahl 	 * There need to be no associated enabled probes, no consumers
1426ab9a77c7Sahl 	 * creating probes, and no meta providers referencing this provider.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount == 0);
1429ab9a77c7Sahl 	ASSERT(provider->ftp_ccount == 0);
1430ab9a77c7Sahl 	ASSERT(provider->ftp_mcount == 0);
14317c478bd9Sstevel@tonic-gate 
14326009dbc6Sahl 	/*
14336009dbc6Sahl 	 * If this provider hasn't been retired, we need to explicitly drop the
14346009dbc6Sahl 	 * count of active providers on the associated process structure.
14356009dbc6Sahl 	 */
14366009dbc6Sahl 	if (!provider->ftp_retired) {
14371a5e258fSJosef 'Jeff' Sipek 		atomic_dec_64(&provider->ftp_proc->ftpc_acount);
14386009dbc6Sahl 		ASSERT(provider->ftp_proc->ftpc_acount <
14396009dbc6Sahl 		    provider->ftp_proc->ftpc_rcount);
14406009dbc6Sahl 	}
14416009dbc6Sahl 
1442b096140dSahl 	fasttrap_proc_release(provider->ftp_proc);
1443b096140dSahl 
14447c478bd9Sstevel@tonic-gate 	kmem_free(provider, sizeof (fasttrap_provider_t));
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/*
14477c478bd9Sstevel@tonic-gate 	 * Decrement p_dtrace_probes on the process whose provider we're
14487c478bd9Sstevel@tonic-gate 	 * freeing. We don't have to worry about clobbering somone else's
14497c478bd9Sstevel@tonic-gate 	 * modifications to it because we have locked the bucket that
14507c478bd9Sstevel@tonic-gate 	 * corresponds to this process's hash chain in the provider hash
14517c478bd9Sstevel@tonic-gate 	 * table. Don't sweat it if we can't find the process.
14527c478bd9Sstevel@tonic-gate 	 */
14537c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
14547c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
14557c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
14567c478bd9Sstevel@tonic-gate 		return;
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
14607c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes--;
14637c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate static void
fasttrap_provider_retire(pid_t pid,const char * name,int mprov)1467ab9a77c7Sahl fasttrap_provider_retire(pid_t pid, const char *name, int mprov)
14687c478bd9Sstevel@tonic-gate {
1469dafb5540Sahl 	fasttrap_provider_t *fp;
1470dafb5540Sahl 	fasttrap_bucket_t *bucket;
1471dafb5540Sahl 	dtrace_provider_id_t provid;
1472dafb5540Sahl 
1473dafb5540Sahl 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1474dafb5540Sahl 
1475dafb5540Sahl 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1476dafb5540Sahl 	mutex_enter(&bucket->ftb_mtx);
1477dafb5540Sahl 
1478dafb5540Sahl 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1479dafb5540Sahl 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1480dafb5540Sahl 		    !fp->ftp_retired)
1481dafb5540Sahl 			break;
1482dafb5540Sahl 	}
1483dafb5540Sahl 
1484dafb5540Sahl 	if (fp == NULL) {
1485dafb5540Sahl 		mutex_exit(&bucket->ftb_mtx);
1486dafb5540Sahl 		return;
1487dafb5540Sahl 	}
14887c478bd9Sstevel@tonic-gate 
1489ab9a77c7Sahl 	mutex_enter(&fp->ftp_mtx);
1490ab9a77c7Sahl 	ASSERT(!mprov || fp->ftp_mcount > 0);
1491ab9a77c7Sahl 	if (mprov && --fp->ftp_mcount != 0)  {
1492ab9a77c7Sahl 		mutex_exit(&fp->ftp_mtx);
1493ab9a77c7Sahl 		mutex_exit(&bucket->ftb_mtx);
1494ab9a77c7Sahl 		return;
1495ab9a77c7Sahl 	}
1496ab9a77c7Sahl 
14977c478bd9Sstevel@tonic-gate 	/*
1498038dc6b3Sahl 	 * Mark the provider to be removed in our post-processing step, mark it
1499038dc6b3Sahl 	 * retired, and drop the active count on its proc. Marking it indicates
1500038dc6b3Sahl 	 * that we should try to remove it; setting the retired flag indicates
1501038dc6b3Sahl 	 * that we're done with this provider; dropping the active the proc
1502038dc6b3Sahl 	 * releases our hold, and when this reaches zero (as it will during
1503038dc6b3Sahl 	 * exit or exec) the proc and associated providers become defunct.
1504dafb5540Sahl 	 *
1505dafb5540Sahl 	 * We obviously need to take the bucket lock before the provider lock
1506dafb5540Sahl 	 * to perform the lookup, but we need to drop the provider lock
1507dafb5540Sahl 	 * before calling into the DTrace framework since we acquire the
1508dafb5540Sahl 	 * provider lock in callbacks invoked from the DTrace framework. The
1509dafb5540Sahl 	 * bucket lock therefore protects the integrity of the provider hash
1510dafb5540Sahl 	 * table.
15117c478bd9Sstevel@tonic-gate 	 */
15121a5e258fSJosef 'Jeff' Sipek 	atomic_dec_64(&fp->ftp_proc->ftpc_acount);
15136009dbc6Sahl 	ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount);
15146009dbc6Sahl 
1515dafb5540Sahl 	fp->ftp_retired = 1;
1516dafb5540Sahl 	fp->ftp_marked = 1;
1517dafb5540Sahl 	provid = fp->ftp_provid;
1518dafb5540Sahl 	mutex_exit(&fp->ftp_mtx);
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/*
15217c478bd9Sstevel@tonic-gate 	 * We don't have to worry about invalidating the same provider twice
15227c478bd9Sstevel@tonic-gate 	 * since fasttrap_provider_lookup() will ignore provider that have
1523b096140dSahl 	 * been marked as retired.
15247c478bd9Sstevel@tonic-gate 	 */
15257c478bd9Sstevel@tonic-gate 	dtrace_invalidate(provid);
15267c478bd9Sstevel@tonic-gate 
1527dafb5540Sahl 	mutex_exit(&bucket->ftb_mtx);
1528dafb5540Sahl 
15297c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
153273427c57Sahl static int
fasttrap_uint32_cmp(const void * ap,const void * bp)153373427c57Sahl fasttrap_uint32_cmp(const void *ap, const void *bp)
153473427c57Sahl {
153573427c57Sahl 	return (*(const uint32_t *)ap - *(const uint32_t *)bp);
153673427c57Sahl }
153773427c57Sahl 
153873427c57Sahl static int
fasttrap_uint64_cmp(const void * ap,const void * bp)153973427c57Sahl fasttrap_uint64_cmp(const void *ap, const void *bp)
154073427c57Sahl {
154173427c57Sahl 	return (*(const uint64_t *)ap - *(const uint64_t *)bp);
154273427c57Sahl }
154373427c57Sahl 
15447c478bd9Sstevel@tonic-gate static int
fasttrap_add_probe(fasttrap_probe_spec_t * pdata)15457c478bd9Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
15487c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
15497c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
15507c478bd9Sstevel@tonic-gate 	char *name;
15517c478bd9Sstevel@tonic-gate 	int i, aframes, whack;
15527c478bd9Sstevel@tonic-gate 
155373427c57Sahl 	/*
155473427c57Sahl 	 * There needs to be at least one desired trace point.
155573427c57Sahl 	 */
155673427c57Sahl 	if (pdata->ftps_noffs == 0)
155773427c57Sahl 		return (EINVAL);
155873427c57Sahl 
15597c478bd9Sstevel@tonic-gate 	switch (pdata->ftps_type) {
15607c478bd9Sstevel@tonic-gate 	case DTFTP_ENTRY:
15617c478bd9Sstevel@tonic-gate 		name = "entry";
15627c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_ENTRY_AFRAMES;
15637c478bd9Sstevel@tonic-gate 		break;
15647c478bd9Sstevel@tonic-gate 	case DTFTP_RETURN:
15657c478bd9Sstevel@tonic-gate 		name = "return";
15667c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_RETURN_AFRAMES;
15677c478bd9Sstevel@tonic-gate 		break;
15687c478bd9Sstevel@tonic-gate 	case DTFTP_OFFSETS:
15697c478bd9Sstevel@tonic-gate 		name = NULL;
15707c478bd9Sstevel@tonic-gate 		break;
15717c478bd9Sstevel@tonic-gate 	default:
15727c478bd9Sstevel@tonic-gate 		return (EINVAL);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
15767c478bd9Sstevel@tonic-gate 	    FASTTRAP_PID_NAME, &pid_attr)) == NULL)
15777c478bd9Sstevel@tonic-gate 		return (ESRCH);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/*
15807c478bd9Sstevel@tonic-gate 	 * Increment this reference count to indicate that a consumer is
1581f498645aSahl 	 * actively adding a new probe associated with this provider. This
1582f498645aSahl 	 * prevents the provider from being deleted -- we'll need to check
1583f498645aSahl 	 * for pending deletions when we drop this reference count.
15847c478bd9Sstevel@tonic-gate 	 */
15857c478bd9Sstevel@tonic-gate 	provider->ftp_ccount++;
15867c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
15877c478bd9Sstevel@tonic-gate 
1588f498645aSahl 	/*
1589f498645aSahl 	 * Grab the creation lock to ensure consistency between calls to
1590f498645aSahl 	 * dtrace_probe_lookup() and dtrace_probe_create() in the face of
1591f498645aSahl 	 * other threads creating probes. We must drop the provider lock
1592f498645aSahl 	 * before taking this lock to avoid a three-way deadlock with the
1593f498645aSahl 	 * DTrace framework.
1594f498645aSahl 	 */
1595f498645aSahl 	mutex_enter(&provider->ftp_cmtx);
15967c478bd9Sstevel@tonic-gate 
1597f498645aSahl 	if (name == NULL) {
15987c478bd9Sstevel@tonic-gate 		for (i = 0; i < pdata->ftps_noffs; i++) {
15997c478bd9Sstevel@tonic-gate 			char name_str[17];
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 			(void) sprintf(name_str, "%llx",
16027c478bd9Sstevel@tonic-gate 			    (unsigned long long)pdata->ftps_offs[i]);
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 			if (dtrace_probe_lookup(provider->ftp_provid,
16057c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
16067c478bd9Sstevel@tonic-gate 				continue;
16077c478bd9Sstevel@tonic-gate 
16081a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&fasttrap_total);
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 			if (fasttrap_total > fasttrap_max) {
16111a5e258fSJosef 'Jeff' Sipek 				atomic_dec_32(&fasttrap_total);
16127c478bd9Sstevel@tonic-gate 				goto no_mem;
16137c478bd9Sstevel@tonic-gate 			}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 			pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 			pp->ftp_prov = provider;
16187c478bd9Sstevel@tonic-gate 			pp->ftp_faddr = pdata->ftps_pc;
16197c478bd9Sstevel@tonic-gate 			pp->ftp_fsize = pdata->ftps_size;
16207c478bd9Sstevel@tonic-gate 			pp->ftp_pid = pdata->ftps_pid;
16217c478bd9Sstevel@tonic-gate 			pp->ftp_ntps = 1;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
16247c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
16257c478bd9Sstevel@tonic-gate 
1626b096140dSahl 			tp->ftt_proc = provider->ftp_proc;
16277c478bd9Sstevel@tonic-gate 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
16287c478bd9Sstevel@tonic-gate 			tp->ftt_pid = pdata->ftps_pid;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_tp = tp;
16317c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_id.fti_probe = pp;
1632ac448965Sahl 			pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
16357c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str,
16367c478bd9Sstevel@tonic-gate 			    FASTTRAP_OFFSET_AFRAMES, pp);
16377c478bd9Sstevel@tonic-gate 		}
1638f498645aSahl 
1639f498645aSahl 	} else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod,
1640f498645aSahl 	    pdata->ftps_func, name) == 0) {
1641f498645aSahl 		atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
1642f498645aSahl 
1643f498645aSahl 		if (fasttrap_total > fasttrap_max) {
1644f498645aSahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1645f498645aSahl 			goto no_mem;
1646f498645aSahl 		}
1647f498645aSahl 
164873427c57Sahl 		/*
164973427c57Sahl 		 * Make sure all tracepoint program counter values are unique.
165073427c57Sahl 		 * We later assume that each probe has exactly one tracepoint
165173427c57Sahl 		 * for a given pc.
165273427c57Sahl 		 */
165373427c57Sahl 		qsort(pdata->ftps_offs, pdata->ftps_noffs,
165473427c57Sahl 		    sizeof (uint64_t), fasttrap_uint64_cmp);
165573427c57Sahl 		for (i = 1; i < pdata->ftps_noffs; i++) {
165673427c57Sahl 			if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1])
165773427c57Sahl 				continue;
165873427c57Sahl 
165973427c57Sahl 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
166073427c57Sahl 			goto no_mem;
166173427c57Sahl 		}
166273427c57Sahl 
1663f498645aSahl 		ASSERT(pdata->ftps_noffs > 0);
1664f498645aSahl 		pp = kmem_zalloc(offsetof(fasttrap_probe_t,
1665f498645aSahl 		    ftp_tps[pdata->ftps_noffs]), KM_SLEEP);
1666f498645aSahl 
1667f498645aSahl 		pp->ftp_prov = provider;
1668f498645aSahl 		pp->ftp_faddr = pdata->ftps_pc;
1669f498645aSahl 		pp->ftp_fsize = pdata->ftps_size;
1670f498645aSahl 		pp->ftp_pid = pdata->ftps_pid;
1671f498645aSahl 		pp->ftp_ntps = pdata->ftps_noffs;
1672f498645aSahl 
1673f498645aSahl 		for (i = 0; i < pdata->ftps_noffs; i++) {
1674f498645aSahl 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1675f498645aSahl 			    KM_SLEEP);
1676f498645aSahl 
1677f498645aSahl 			tp->ftt_proc = provider->ftp_proc;
1678f498645aSahl 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1679f498645aSahl 			tp->ftt_pid = pdata->ftps_pid;
1680f498645aSahl 
1681f498645aSahl 			pp->ftp_tps[i].fit_tp = tp;
1682f498645aSahl 			pp->ftp_tps[i].fit_id.fti_probe = pp;
1683f498645aSahl 			pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type;
1684f498645aSahl 		}
1685f498645aSahl 
1686f498645aSahl 		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1687f498645aSahl 		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
1690f498645aSahl 	mutex_exit(&provider->ftp_cmtx);
1691f498645aSahl 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * We know that the provider is still valid since we incremented the
1694f498645aSahl 	 * creation reference count. If someone tried to clean up this provider
1695f498645aSahl 	 * while we were using it (e.g. because the process called exec(2) or
1696f498645aSahl 	 * exit(2)), take note of that and try to clean it up now.
16977c478bd9Sstevel@tonic-gate 	 */
16987c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
16997c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
1700b096140dSahl 	whack = provider->ftp_retired;
17017c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (whack)
17047c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	return (0);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate no_mem:
17097c478bd9Sstevel@tonic-gate 	/*
17107c478bd9Sstevel@tonic-gate 	 * If we've exhausted the allowable resources, we'll try to remove
17117c478bd9Sstevel@tonic-gate 	 * this provider to free some up. This is to cover the case where
17127c478bd9Sstevel@tonic-gate 	 * the user has accidentally created many more probes than was
17137c478bd9Sstevel@tonic-gate 	 * intended (e.g. pid123:::).
17147c478bd9Sstevel@tonic-gate 	 */
1715f498645aSahl 	mutex_exit(&provider->ftp_cmtx);
17167c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
17177c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
17187c478bd9Sstevel@tonic-gate 	provider->ftp_marked = 1;
17197c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	return (ENOMEM);
17247c478bd9Sstevel@tonic-gate }
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17277c478bd9Sstevel@tonic-gate static void *
fasttrap_meta_provide(void * arg,dtrace_helper_provdesc_t * dhpv,pid_t pid)17287c478bd9Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	/*
17337c478bd9Sstevel@tonic-gate 	 * A 32-bit unsigned integer (like a pid for example) can be
17347c478bd9Sstevel@tonic-gate 	 * expressed in 10 or fewer decimal digits. Make sure that we'll
17357c478bd9Sstevel@tonic-gate 	 * have enough space for the provider name.
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	if (strlen(dhpv->dthpv_provname) + 10 >=
17387c478bd9Sstevel@tonic-gate 	    sizeof (provider->ftp_name)) {
17397c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
17407c478bd9Sstevel@tonic-gate 		    "name too long to accomodate pid", dhpv->dthpv_provname);
17417c478bd9Sstevel@tonic-gate 		return (NULL);
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	/*
17457c478bd9Sstevel@tonic-gate 	 * Don't let folks spoof the true pid provider.
17467c478bd9Sstevel@tonic-gate 	 */
17477c478bd9Sstevel@tonic-gate 	if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
17487c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
17497c478bd9Sstevel@tonic-gate 		    "%s is an invalid name", dhpv->dthpv_provname,
17507c478bd9Sstevel@tonic-gate 		    FASTTRAP_PID_NAME);
17517c478bd9Sstevel@tonic-gate 		return (NULL);
17527c478bd9Sstevel@tonic-gate 	}
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	/*
17557c478bd9Sstevel@tonic-gate 	 * The highest stability class that fasttrap supports is ISA; cap
17567c478bd9Sstevel@tonic-gate 	 * the stability of the new provider accordingly.
17577c478bd9Sstevel@tonic-gate 	 */
1758038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA)
17597c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
1760038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA)
17617c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
1762038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA)
17637c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
1764038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA)
17657c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
1766038dc6b3Sahl 	if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA)
17677c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
17707c478bd9Sstevel@tonic-gate 	    &dhpv->dthpv_pattr)) == NULL) {
17717c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s for "
17727c478bd9Sstevel@tonic-gate 		    "process %u",  dhpv->dthpv_provname, (uint_t)pid);
17737c478bd9Sstevel@tonic-gate 		return (NULL);
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/*
1777ab9a77c7Sahl 	 * Up the meta provider count so this provider isn't removed until
1778ab9a77c7Sahl 	 * the meta provider has been told to remove it.
17797c478bd9Sstevel@tonic-gate 	 */
1780ab9a77c7Sahl 	provider->ftp_mcount++;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	return (provider);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877bd3c1d1SBryan Cantrill /*
17887bd3c1d1SBryan Cantrill  * We know a few things about our context here:  we know that the probe being
17897bd3c1d1SBryan Cantrill  * created doesn't already exist (DTrace won't load DOF at the same address
17907bd3c1d1SBryan Cantrill  * twice, even if explicitly told to do so) and we know that we are
17917bd3c1d1SBryan Cantrill  * single-threaded with respect to the meta provider machinery. Knowing that
17927bd3c1d1SBryan Cantrill  * this is a new probe and that there is no way for us to race with another
17937bd3c1d1SBryan Cantrill  * operation on this provider allows us an important optimization: we need not
17947bd3c1d1SBryan Cantrill  * lookup a probe before adding it.  Saving this lookup is important because
17957bd3c1d1SBryan Cantrill  * this code is in the fork path for processes with USDT probes, and lookups
17967bd3c1d1SBryan Cantrill  * here are potentially very expensive because of long hash conflicts on
17977bd3c1d1SBryan Cantrill  * module, function and name (DTrace doesn't hash on provider name).
17987bd3c1d1SBryan Cantrill  */
17997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18007c478bd9Sstevel@tonic-gate static void
fasttrap_meta_create_probe(void * arg,void * parg,dtrace_helper_probedesc_t * dhpb)18017c478bd9Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg,
18027c478bd9Sstevel@tonic-gate     dtrace_helper_probedesc_t *dhpb)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = parg;
18057c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
18067c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
1807ac448965Sahl 	int i, j;
1808ac448965Sahl 	uint32_t ntps;
18097c478bd9Sstevel@tonic-gate 
1810f498645aSahl 	/*
1811f498645aSahl 	 * Since the meta provider count is non-zero we don't have to worry
1812f498645aSahl 	 * about this provider disappearing.
1813f498645aSahl 	 */
1814f498645aSahl 	ASSERT(provider->ftp_mcount > 0);
1815f498645aSahl 
181673427c57Sahl 	/*
181773427c57Sahl 	 * The offsets must be unique.
181873427c57Sahl 	 */
181973427c57Sahl 	qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t),
182073427c57Sahl 	    fasttrap_uint32_cmp);
182173427c57Sahl 	for (i = 1; i < dhpb->dthpb_noffs; i++) {
182273427c57Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <=
182373427c57Sahl 		    dhpb->dthpb_base + dhpb->dthpb_offs[i - 1])
182473427c57Sahl 			return;
182573427c57Sahl 	}
182673427c57Sahl 
182773427c57Sahl 	qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t),
182873427c57Sahl 	    fasttrap_uint32_cmp);
182973427c57Sahl 	for (i = 1; i < dhpb->dthpb_nenoffs; i++) {
183073427c57Sahl 		if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <=
183173427c57Sahl 		    dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1])
183273427c57Sahl 			return;
183373427c57Sahl 	}
183473427c57Sahl 
1835ac448965Sahl 	ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs;
1836ab9a77c7Sahl 	ASSERT(ntps > 0);
1837ac448965Sahl 
1838ac448965Sahl 	atomic_add_32(&fasttrap_total, ntps);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (fasttrap_total > fasttrap_max) {
1841ac448965Sahl 		atomic_add_32(&fasttrap_total, -ntps);
18427c478bd9Sstevel@tonic-gate 		return;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
1845ab9a77c7Sahl 	pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	pp->ftp_prov = provider;
18487c478bd9Sstevel@tonic-gate 	pp->ftp_pid = provider->ftp_pid;
1849ac448965Sahl 	pp->ftp_ntps = ntps;
18507c478bd9Sstevel@tonic-gate 	pp->ftp_nargs = dhpb->dthpb_xargc;
18517c478bd9Sstevel@tonic-gate 	pp->ftp_xtypes = dhpb->dthpb_xtypes;
18527c478bd9Sstevel@tonic-gate 	pp->ftp_ntypes = dhpb->dthpb_ntypes;
18537c478bd9Sstevel@tonic-gate 
1854ac448965Sahl 	/*
1855ac448965Sahl 	 * First create a tracepoint for each actual point of interest.
1856ac448965Sahl 	 */
1857ac448965Sahl 	for (i = 0; i < dhpb->dthpb_noffs; i++) {
18587c478bd9Sstevel@tonic-gate 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
18597c478bd9Sstevel@tonic-gate 
1860b096140dSahl 		tp->ftt_proc = provider->ftp_proc;
18617c478bd9Sstevel@tonic-gate 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
18627c478bd9Sstevel@tonic-gate 		tp->ftt_pid = provider->ftp_pid;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_tp = tp;
18657c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_id.fti_probe = pp;
1866ac448965Sahl #ifdef __sparc
1867ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS;
1868ac448965Sahl #else
1869ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS;
1870ac448965Sahl #endif
1871ac448965Sahl 	}
1872ac448965Sahl 
1873ac448965Sahl 	/*
1874ac448965Sahl 	 * Then create a tracepoint for each is-enabled point.
1875ac448965Sahl 	 */
1876ac448965Sahl 	for (j = 0; i < ntps; i++, j++) {
1877ac448965Sahl 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
1878ac448965Sahl 
1879ac448965Sahl 		tp->ftt_proc = provider->ftp_proc;
1880ac448965Sahl 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j];
1881ac448965Sahl 		tp->ftt_pid = provider->ftp_pid;
1882ac448965Sahl 
1883ac448965Sahl 		pp->ftp_tps[i].fit_tp = tp;
1884ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_probe = pp;
1885ac448965Sahl 		pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED;
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * If the arguments are shuffled around we set the argument remapping
18907c478bd9Sstevel@tonic-gate 	 * table. Later, when the probe fires, we only remap the arguments
18917c478bd9Sstevel@tonic-gate 	 * if the table is non-NULL.
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhpb->dthpb_xargc; i++) {
18947c478bd9Sstevel@tonic-gate 		if (dhpb->dthpb_args[i] != i) {
18957c478bd9Sstevel@tonic-gate 			pp->ftp_argmap = dhpb->dthpb_args;
18967c478bd9Sstevel@tonic-gate 			break;
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 	}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	/*
19017c478bd9Sstevel@tonic-gate 	 * The probe is fully constructed -- register it with DTrace.
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
19047c478bd9Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19087c478bd9Sstevel@tonic-gate static void
fasttrap_meta_remove(void * arg,dtrace_helper_provdesc_t * dhpv,pid_t pid)19097c478bd9Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
19107c478bd9Sstevel@tonic-gate {
1911dafb5540Sahl 	/*
1912dafb5540Sahl 	 * Clean up the USDT provider. There may be active consumers of the
1913dafb5540Sahl 	 * provider busy adding probes, no damage will actually befall the
1914dafb5540Sahl 	 * provider until that count has dropped to zero. This just puts
1915dafb5540Sahl 	 * the provider on death row.
1916dafb5540Sahl 	 */
1917dafb5540Sahl 	fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1);
19187c478bd9Sstevel@tonic-gate }
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = {
19217c478bd9Sstevel@tonic-gate 	fasttrap_meta_create_probe,
19227c478bd9Sstevel@tonic-gate 	fasttrap_meta_provide,
19237c478bd9Sstevel@tonic-gate 	fasttrap_meta_remove
19247c478bd9Sstevel@tonic-gate };
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19277c478bd9Sstevel@tonic-gate static int
fasttrap_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)19287c478bd9Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
19297c478bd9Sstevel@tonic-gate {
19307c478bd9Sstevel@tonic-gate 	return (0);
19317c478bd9Sstevel@tonic-gate }
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19347c478bd9Sstevel@tonic-gate static int
fasttrap_ioctl(dev_t dev,int cmd,intptr_t arg,int md,cred_t * cr,int * rv)19357c478bd9Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
19367c478bd9Sstevel@tonic-gate {
19377c478bd9Sstevel@tonic-gate 	if (!dtrace_attached())
19387c478bd9Sstevel@tonic-gate 		return (EAGAIN);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	if (cmd == FASTTRAPIOC_MAKEPROBE) {
19417c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *uprobe = (void *)arg;
19427c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *probe;
19437c478bd9Sstevel@tonic-gate 		uint64_t noffs;
19447c478bd9Sstevel@tonic-gate 		size_t size;
19451444d846SBryan Cantrill 		int ret, err;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		if (copyin(&uprobe->ftps_noffs, &noffs,
19487c478bd9Sstevel@tonic-gate 		    sizeof (uprobe->ftps_noffs)))
19497c478bd9Sstevel@tonic-gate 			return (EFAULT);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 		/*
19527c478bd9Sstevel@tonic-gate 		 * Probes must have at least one tracepoint.
19537c478bd9Sstevel@tonic-gate 		 */
19547c478bd9Sstevel@tonic-gate 		if (noffs == 0)
19557c478bd9Sstevel@tonic-gate 			return (EINVAL);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		size = sizeof (fasttrap_probe_spec_t) +
19587c478bd9Sstevel@tonic-gate 		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		if (size > 1024 * 1024)
19617c478bd9Sstevel@tonic-gate 			return (ENOMEM);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		probe = kmem_alloc(size, KM_SLEEP);
19647c478bd9Sstevel@tonic-gate 
1965721f47f6SJonathan Haslam 		if (copyin(uprobe, probe, size) != 0 ||
1966721f47f6SJonathan Haslam 		    probe->ftps_noffs != noffs) {
19677c478bd9Sstevel@tonic-gate 			kmem_free(probe, size);
19687c478bd9Sstevel@tonic-gate 			return (EFAULT);
19697c478bd9Sstevel@tonic-gate 		}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		/*
19727c478bd9Sstevel@tonic-gate 		 * Verify that the function and module strings contain no
19737c478bd9Sstevel@tonic-gate 		 * funny characters.
19747c478bd9Sstevel@tonic-gate 		 */
19751444d846SBryan Cantrill 		if (u8_validate(probe->ftps_func, strlen(probe->ftps_func),
19761444d846SBryan Cantrill 		    NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
19771444d846SBryan Cantrill 			ret = EINVAL;
19781444d846SBryan Cantrill 			goto err;
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 
19811444d846SBryan Cantrill 		if (u8_validate(probe->ftps_mod, strlen(probe->ftps_mod),
19821444d846SBryan Cantrill 		    NULL, U8_VALIDATE_ENTIRE, &err) < 0) {
19831444d846SBryan Cantrill 			ret = EINVAL;
19841444d846SBryan Cantrill 			goto err;
19857c478bd9Sstevel@tonic-gate 		}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
19887c478bd9Sstevel@tonic-gate 			proc_t *p;
19897c478bd9Sstevel@tonic-gate 			pid_t pid = probe->ftps_pid;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
19927c478bd9Sstevel@tonic-gate 			/*
19937c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
19947c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
19957c478bd9Sstevel@tonic-gate 			 */
19967c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
19977c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
19987c478bd9Sstevel@tonic-gate 				return (ESRCH);
19997c478bd9Sstevel@tonic-gate 			}
20007c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
20017c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
20047c478bd9Sstevel@tonic-gate 			    VREAD | VWRITE)) != 0) {
20057c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
20067c478bd9Sstevel@tonic-gate 				return (ret);
20077c478bd9Sstevel@tonic-gate 			}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
20107c478bd9Sstevel@tonic-gate 		}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 		ret = fasttrap_add_probe(probe);
20137c478bd9Sstevel@tonic-gate err:
20147c478bd9Sstevel@tonic-gate 		kmem_free(probe, size);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 		return (ret);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	} else if (cmd == FASTTRAPIOC_GETINSTR) {
20197c478bd9Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
20207c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
20217c478bd9Sstevel@tonic-gate 		uint_t index;
20227c478bd9Sstevel@tonic-gate 		int ret;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
20257c478bd9Sstevel@tonic-gate 			return (EFAULT);
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
20287c478bd9Sstevel@tonic-gate 			proc_t *p;
20297c478bd9Sstevel@tonic-gate 			pid_t pid = instr.ftiq_pid;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
20327c478bd9Sstevel@tonic-gate 			/*
20337c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
20347c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
20357c478bd9Sstevel@tonic-gate 			 */
20367c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
20377c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
20387c478bd9Sstevel@tonic-gate 				return (ESRCH);
20397c478bd9Sstevel@tonic-gate 			}
20407c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
20417c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
20447c478bd9Sstevel@tonic-gate 			    VREAD)) != 0) {
20457c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
20467c478bd9Sstevel@tonic-gate 				return (ret);
20477c478bd9Sstevel@tonic-gate 			}
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
20507c478bd9Sstevel@tonic-gate 		}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20557c478bd9Sstevel@tonic-gate 		tp = fasttrap_tpoints.fth_table[index].ftb_data;
20567c478bd9Sstevel@tonic-gate 		while (tp != NULL) {
20577c478bd9Sstevel@tonic-gate 			if (instr.ftiq_pid == tp->ftt_pid &&
20587c478bd9Sstevel@tonic-gate 			    instr.ftiq_pc == tp->ftt_pc &&
2059038dc6b3Sahl 			    tp->ftt_proc->ftpc_acount != 0)
20607c478bd9Sstevel@tonic-gate 				break;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 			tp = tp->ftt_next;
20637c478bd9Sstevel@tonic-gate 		}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
20667c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20677c478bd9Sstevel@tonic-gate 			return (ENOENT);
20687c478bd9Sstevel@tonic-gate 		}
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
20717c478bd9Sstevel@tonic-gate 		    sizeof (instr.ftiq_instr));
20727c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 		if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
20757c478bd9Sstevel@tonic-gate 			return (EFAULT);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 		return (0);
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	return (EINVAL);
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = {
20847c478bd9Sstevel@tonic-gate 	fasttrap_open,		/* open */
20857c478bd9Sstevel@tonic-gate 	nodev,			/* close */
20867c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
20877c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
20887c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
20897c478bd9Sstevel@tonic-gate 	nodev,			/* read */
20907c478bd9Sstevel@tonic-gate 	nodev,			/* write */
20917c478bd9Sstevel@tonic-gate 	fasttrap_ioctl,		/* ioctl */
20927c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
20937c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
20947c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
20957c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
20967c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
20977c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
20987c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
20997c478bd9Sstevel@tonic-gate };
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21027c478bd9Sstevel@tonic-gate static int
fasttrap_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)21037c478bd9Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
21047c478bd9Sstevel@tonic-gate {
21057c478bd9Sstevel@tonic-gate 	int error;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	switch (infocmd) {
21087c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
21097c478bd9Sstevel@tonic-gate 		*result = (void *)fasttrap_devi;
21107c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
21117c478bd9Sstevel@tonic-gate 		break;
21127c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
21137c478bd9Sstevel@tonic-gate 		*result = (void *)0;
21147c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
21157c478bd9Sstevel@tonic-gate 		break;
21167c478bd9Sstevel@tonic-gate 	default:
21177c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 	return (error);
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate static int
fasttrap_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)21237c478bd9Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	ulong_t nent;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	switch (cmd) {
21287c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
21297c478bd9Sstevel@tonic-gate 		break;
21307c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
21317c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
21327c478bd9Sstevel@tonic-gate 	default:
21337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21347c478bd9Sstevel@tonic-gate 	}
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0,
2137*ef623aa1SToomas Soome 	    DDI_PSEUDO, 0) == DDI_FAILURE) {
21387c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
21397c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
21407c478bd9Sstevel@tonic-gate 	}
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
21437c478bd9Sstevel@tonic-gate 	fasttrap_devi = devi;
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	/*
21467c478bd9Sstevel@tonic-gate 	 * Install our hooks into fork(2), exec(2), and exit(2).
21477c478bd9Sstevel@tonic-gate 	 */
21487c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = &fasttrap_fork;
21497c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
21507c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 	fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21537c478bd9Sstevel@tonic-gate 	    "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
21547c478bd9Sstevel@tonic-gate 	fasttrap_total = 0;
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * Conjure up the tracepoints hashtable...
21587c478bd9Sstevel@tonic-gate 	 */
21597c478bd9Sstevel@tonic-gate 	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
21607c478bd9Sstevel@tonic-gate 	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
21617c478bd9Sstevel@tonic-gate 
216273427c57Sahl 	if (nent == 0 || nent > 0x1000000)
21637c478bd9Sstevel@tonic-gate 		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
21647c478bd9Sstevel@tonic-gate 
2165de710d24SJosef 'Jeff' Sipek 	if (ISP2(nent))
21667c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = nent;
21677c478bd9Sstevel@tonic-gate 	else
21687c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
21697c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_tpoints.fth_nent > 0);
21707c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
21717c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
21727c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	/*
2175b096140dSahl 	 * ... and the providers hash table...
21767c478bd9Sstevel@tonic-gate 	 */
21777c478bd9Sstevel@tonic-gate 	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
2178de710d24SJosef 'Jeff' Sipek 	if (ISP2(nent))
21797c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = nent;
21807c478bd9Sstevel@tonic-gate 	else
21817c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
21827c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_provs.fth_nent > 0);
21837c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
21847c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
21857c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
21867c478bd9Sstevel@tonic-gate 
2187b096140dSahl 	/*
2188b096140dSahl 	 * ... and the procs hash table.
2189b096140dSahl 	 */
2190b096140dSahl 	nent = FASTTRAP_PROCS_DEFAULT_SIZE;
2191de710d24SJosef 'Jeff' Sipek 	if (ISP2(nent))
2192b096140dSahl 		fasttrap_procs.fth_nent = nent;
2193b096140dSahl 	else
2194b096140dSahl 		fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent);
2195b096140dSahl 	ASSERT(fasttrap_procs.fth_nent > 0);
2196b096140dSahl 	fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1;
2197b096140dSahl 	fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent *
2198b096140dSahl 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
2199b096140dSahl 
22007c478bd9Sstevel@tonic-gate 	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
22017c478bd9Sstevel@tonic-gate 	    &fasttrap_meta_id);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
22047c478bd9Sstevel@tonic-gate }
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate static int
fasttrap_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)22077c478bd9Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
22087c478bd9Sstevel@tonic-gate {
22097c478bd9Sstevel@tonic-gate 	int i, fail = 0;
22107c478bd9Sstevel@tonic-gate 	timeout_id_t tmp;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	switch (cmd) {
22137c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
22147c478bd9Sstevel@tonic-gate 		break;
22157c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
22167c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
22177c478bd9Sstevel@tonic-gate 	default:
22187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22197c478bd9Sstevel@tonic-gate 	}
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	/*
22227c478bd9Sstevel@tonic-gate 	 * Unregister the meta-provider to make sure no new fasttrap-
22237c478bd9Sstevel@tonic-gate 	 * managed providers come along while we're trying to close up
22247c478bd9Sstevel@tonic-gate 	 * shop. If we fail to detach, we'll need to re-register as a
22257c478bd9Sstevel@tonic-gate 	 * meta-provider. We can fail to unregister as a meta-provider
22267c478bd9Sstevel@tonic-gate 	 * if providers we manage still exist.
22277c478bd9Sstevel@tonic-gate 	 */
22287c478bd9Sstevel@tonic-gate 	if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
22297c478bd9Sstevel@tonic-gate 	    dtrace_meta_unregister(fasttrap_meta_id) != 0)
22307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	/*
22337c478bd9Sstevel@tonic-gate 	 * Prevent any new timeouts from running by setting fasttrap_timeout
22347c478bd9Sstevel@tonic-gate 	 * to a non-zero value, and wait for the current timeout to complete.
22357c478bd9Sstevel@tonic-gate 	 */
22367c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
22377c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	while (fasttrap_timeout != (timeout_id_t)1) {
22407c478bd9Sstevel@tonic-gate 		tmp = fasttrap_timeout;
22417c478bd9Sstevel@tonic-gate 		fasttrap_timeout = (timeout_id_t)1;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 		if (tmp != 0) {
22447c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_cleanup_mtx);
22457c478bd9Sstevel@tonic-gate 			(void) untimeout(tmp);
22467c478bd9Sstevel@tonic-gate 			mutex_enter(&fasttrap_cleanup_mtx);
22477c478bd9Sstevel@tonic-gate 		}
22487c478bd9Sstevel@tonic-gate 	}
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
22517c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	/*
22547c478bd9Sstevel@tonic-gate 	 * Iterate over all of our providers. If there's still a process
22557c478bd9Sstevel@tonic-gate 	 * that corresponds to that pid, fail to detach.
22567c478bd9Sstevel@tonic-gate 	 */
22577c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
22587c478bd9Sstevel@tonic-gate 		fasttrap_provider_t **fpp, *fp;
22597c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
22627c478bd9Sstevel@tonic-gate 		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
22637c478bd9Sstevel@tonic-gate 		while ((fp = *fpp) != NULL) {
22647c478bd9Sstevel@tonic-gate 			/*
22657c478bd9Sstevel@tonic-gate 			 * Acquire and release the lock as a simple way of
22667c478bd9Sstevel@tonic-gate 			 * waiting for any other consumer to finish with
22677c478bd9Sstevel@tonic-gate 			 * this provider. A thread must first acquire the
22687c478bd9Sstevel@tonic-gate 			 * bucket lock so there's no chance of another thread
2269dafb5540Sahl 			 * blocking on the provider's lock.
22707c478bd9Sstevel@tonic-gate 			 */
22717c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
22727c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->ftp_mtx);
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 			if (dtrace_unregister(fp->ftp_provid) != 0) {
22757c478bd9Sstevel@tonic-gate 				fail = 1;
22767c478bd9Sstevel@tonic-gate 				fpp = &fp->ftp_next;
22777c478bd9Sstevel@tonic-gate 			} else {
22787c478bd9Sstevel@tonic-gate 				*fpp = fp->ftp_next;
22797c478bd9Sstevel@tonic-gate 				fasttrap_provider_free(fp);
22807c478bd9Sstevel@tonic-gate 			}
22817c478bd9Sstevel@tonic-gate 		}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
2286f498645aSahl 	if (fail) {
22877c478bd9Sstevel@tonic-gate 		uint_t work;
22887c478bd9Sstevel@tonic-gate 		/*
22897c478bd9Sstevel@tonic-gate 		 * If we're failing to detach, we need to unblock timeouts
22907c478bd9Sstevel@tonic-gate 		 * and start a new timeout if any work has accumulated while
22917c478bd9Sstevel@tonic-gate 		 * we've been unsuccessfully trying to detach.
22927c478bd9Sstevel@tonic-gate 		 */
22937c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
22947c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
22957c478bd9Sstevel@tonic-gate 		work = fasttrap_cleanup_work;
22967c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		if (work)
22997c478bd9Sstevel@tonic-gate 			fasttrap_pid_cleanup();
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
23027c478bd9Sstevel@tonic-gate 		    &fasttrap_meta_id);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate #ifdef DEBUG
23087c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
2309f498645aSahl 	ASSERT(fasttrap_pid_count == 0);
23107c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
23117c478bd9Sstevel@tonic-gate #endif
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_tpoints.fth_table,
23147c478bd9Sstevel@tonic-gate 	    fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
23157c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_nent = 0;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_provs.fth_table,
23187c478bd9Sstevel@tonic-gate 	    fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
23197c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_nent = 0;
23207c478bd9Sstevel@tonic-gate 
2321b096140dSahl 	kmem_free(fasttrap_procs.fth_table,
2322b096140dSahl 	    fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t));
2323b096140dSahl 	fasttrap_procs.fth_nent = 0;
2324b096140dSahl 
23257c478bd9Sstevel@tonic-gate 	/*
23267c478bd9Sstevel@tonic-gate 	 * We know there are no tracepoints in any process anywhere in
23277c478bd9Sstevel@tonic-gate 	 * the system so there is no process which has its p_dtrace_count
23287c478bd9Sstevel@tonic-gate 	 * greater than zero, therefore we know that no thread can actively
23297c478bd9Sstevel@tonic-gate 	 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes
23307c478bd9Sstevel@tonic-gate 	 * and fasttrap_exec() and fasttrap_exit().
23317c478bd9Sstevel@tonic-gate 	 */
23327c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork);
23337c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = NULL;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit);
23367c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = NULL;
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit);
23397c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = NULL;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
23447c478bd9Sstevel@tonic-gate }
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate static struct dev_ops fasttrap_ops = {
23477c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
23487c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
23497c478bd9Sstevel@tonic-gate 	fasttrap_info,		/* get_dev_info */
23507c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
23517c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
23527c478bd9Sstevel@tonic-gate 	fasttrap_attach,	/* attach */
23537c478bd9Sstevel@tonic-gate 	fasttrap_detach,	/* detach */
23547c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
23557c478bd9Sstevel@tonic-gate 	&fasttrap_cb_ops,	/* driver operations */
23567c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
235719397407SSherry Moore 	nodev,			/* dev power */
235819397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
23597c478bd9Sstevel@tonic-gate };
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate /*
23627c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
23637c478bd9Sstevel@tonic-gate  */
23647c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
23657c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
23667c478bd9Sstevel@tonic-gate 	"Fasttrap Tracing",	/* name of module */
23677c478bd9Sstevel@tonic-gate 	&fasttrap_ops,		/* driver ops */
23687c478bd9Sstevel@tonic-gate };
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
23717c478bd9Sstevel@tonic-gate 	MODREV_1,
23727c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
23737c478bd9Sstevel@tonic-gate 	NULL
23747c478bd9Sstevel@tonic-gate };
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate int
_init(void)23777c478bd9Sstevel@tonic-gate _init(void)
23787c478bd9Sstevel@tonic-gate {
23797c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
23807c478bd9Sstevel@tonic-gate }
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)23837c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
23847c478bd9Sstevel@tonic-gate {
23857c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
23867c478bd9Sstevel@tonic-gate }
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate int
_fini(void)23897c478bd9Sstevel@tonic-gate _fini(void)
23907c478bd9Sstevel@tonic-gate {
23917c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
23927c478bd9Sstevel@tonic-gate }
2393