xref: /titanic_54/usr/src/uts/common/dtrace/fasttrap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/fasttrap.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/fasttrap_impl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/fasttrap_isa.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/dtrace_impl.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/frame.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/priv.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/ontrap.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
57*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
58*7c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
59*7c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
60*7c478bd9Sstevel@tonic-gate #include <vm/seg_spt.h>
61*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * User-Land Trap-Based Tracing
65*7c478bd9Sstevel@tonic-gate  * ----------------------------
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  * The fasttrap provider allows DTrace consumers to instrument any user-level
68*7c478bd9Sstevel@tonic-gate  * instruction to gather data; this includes probes with semantic
69*7c478bd9Sstevel@tonic-gate  * signifigance like entry and return as well as simple offsets into the
70*7c478bd9Sstevel@tonic-gate  * function. While the specific techniques used are very ISA specific, the
71*7c478bd9Sstevel@tonic-gate  * methodology is generalizable to any architecture.
72*7c478bd9Sstevel@tonic-gate  *
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  * The General Methodology
75*7c478bd9Sstevel@tonic-gate  * -----------------------
76*7c478bd9Sstevel@tonic-gate  *
77*7c478bd9Sstevel@tonic-gate  * With the primary goal of tracing every user-land instruction and the
78*7c478bd9Sstevel@tonic-gate  * limitation that we can't trust user space so don't want to rely on much
79*7c478bd9Sstevel@tonic-gate  * information there, we begin by replacing the instructions we want to trace
80*7c478bd9Sstevel@tonic-gate  * with trap instructions. Each instruction we overwrite is saved into a hash
81*7c478bd9Sstevel@tonic-gate  * table keyed by process ID and pc address. When we enter the kernel due to
82*7c478bd9Sstevel@tonic-gate  * this trap instruction, we need the effects of the replaced instruction to
83*7c478bd9Sstevel@tonic-gate  * appear to have occurred before we proceed with the user thread's
84*7c478bd9Sstevel@tonic-gate  * execution.
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  * Each user level thread is represented by a ulwp_t structure which is
87*7c478bd9Sstevel@tonic-gate  * always easily accessible through a register. The most basic way to produce
88*7c478bd9Sstevel@tonic-gate  * the effects of the instruction we replaced is to copy that instruction out
89*7c478bd9Sstevel@tonic-gate  * to a bit of scratch space reserved in the user thread's ulwp_t structure
90*7c478bd9Sstevel@tonic-gate  * (a sort of kernel-private thread local storage), set the PC to that
91*7c478bd9Sstevel@tonic-gate  * scratch space and single step. When we reenter the kernel after single
92*7c478bd9Sstevel@tonic-gate  * stepping the instruction we must then adjust the PC to point to what would
93*7c478bd9Sstevel@tonic-gate  * normally be the next instruction. Of course, special care must be taken
94*7c478bd9Sstevel@tonic-gate  * for branches and jumps, but these represent such a small fraction of any
95*7c478bd9Sstevel@tonic-gate  * instruction set that writing the code to emulate these in the kernel is
96*7c478bd9Sstevel@tonic-gate  * not too difficult.
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  * Return probes may require several tracepoints to trace every return site,
99*7c478bd9Sstevel@tonic-gate  * and, conversely, each tracepoint may activate several probes (the entry
100*7c478bd9Sstevel@tonic-gate  * and offset 0 probes, for example). To solve this muliplexing problem,
101*7c478bd9Sstevel@tonic-gate  * tracepoints contain lists of probes to activate and probes contain lists
102*7c478bd9Sstevel@tonic-gate  * of tracepoints to enable. If a probe is activated, it adds its ID to
103*7c478bd9Sstevel@tonic-gate  * existing tracepoints or creates new ones as necessary.
104*7c478bd9Sstevel@tonic-gate  *
105*7c478bd9Sstevel@tonic-gate  * Most probes are activated _before_ the instruction is executed, but return
106*7c478bd9Sstevel@tonic-gate  * probes are activated _after_ the effects of the last instruction of the
107*7c478bd9Sstevel@tonic-gate  * function are visible. Return probes must be fired _after_ we have
108*7c478bd9Sstevel@tonic-gate  * single-stepped the instruction whereas all other probes are fired
109*7c478bd9Sstevel@tonic-gate  * beforehand.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate static dev_info_t *fasttrap_devi;
113*7c478bd9Sstevel@tonic-gate static dtrace_provider_id_t fasttrap_id;
114*7c478bd9Sstevel@tonic-gate static dtrace_meta_provider_id_t fasttrap_meta_id;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate static timeout_id_t fasttrap_timeout;
117*7c478bd9Sstevel@tonic-gate static kmutex_t fasttrap_cleanup_mtx;
118*7c478bd9Sstevel@tonic-gate static uint_t fasttrap_cleanup_work;
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate /*
121*7c478bd9Sstevel@tonic-gate  * Generation count on modifications to the global tracepoint lookup table.
122*7c478bd9Sstevel@tonic-gate  */
123*7c478bd9Sstevel@tonic-gate static volatile uint64_t fasttrap_mod_gen;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate  * When the fasttrap provider is loaded, fasttrap_max is set to either
127*7c478bd9Sstevel@tonic-gate  * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the
128*7c478bd9Sstevel@tonic-gate  * fasttrap.conf file. Each time a probe is created, fasttrap_total is
129*7c478bd9Sstevel@tonic-gate  * incremented by the number of tracepoints that may be associated with that
130*7c478bd9Sstevel@tonic-gate  * probe; fasttrap_total is capped at fasttrap_max.
131*7c478bd9Sstevel@tonic-gate  */
132*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_MAX_DEFAULT		250000
133*7c478bd9Sstevel@tonic-gate static uint32_t fasttrap_max;
134*7c478bd9Sstevel@tonic-gate static uint32_t fasttrap_total;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_TPOINTS_DEFAULT_SIZE	0x4000
138*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVIDERS_DEFAULT_SIZE	0x100
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_PID_NAME		"pid"
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate fasttrap_hash_t			fasttrap_tpoints;
143*7c478bd9Sstevel@tonic-gate static fasttrap_hash_t		fasttrap_provs;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate dtrace_id_t			fasttrap_probe_id;
146*7c478bd9Sstevel@tonic-gate static int			fasttrap_count;		/* ref count */
147*7c478bd9Sstevel@tonic-gate static int			fasttrap_pid_count;	/* pid ref count */
148*7c478bd9Sstevel@tonic-gate static kmutex_t			fasttrap_count_mtx;	/* lock on ref count */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate static int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t);
151*7c478bd9Sstevel@tonic-gate static void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *,
154*7c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *);
155*7c478bd9Sstevel@tonic-gate static void fasttrap_provider_free(fasttrap_provider_t *);
156*7c478bd9Sstevel@tonic-gate static void fasttrap_provider_retire(fasttrap_provider_t *);
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate #define	FASTTRAP_PROVS_INDEX(pid, name) \
159*7c478bd9Sstevel@tonic-gate 	((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask)
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate static int
162*7c478bd9Sstevel@tonic-gate fasttrap_highbit(ulong_t i)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	int h = 1;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	if (i == 0)
167*7c478bd9Sstevel@tonic-gate 		return (0);
168*7c478bd9Sstevel@tonic-gate #ifdef _LP64
169*7c478bd9Sstevel@tonic-gate 	if (i & 0xffffffff00000000ul) {
170*7c478bd9Sstevel@tonic-gate 		h += 32; i >>= 32;
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate #endif
173*7c478bd9Sstevel@tonic-gate 	if (i & 0xffff0000) {
174*7c478bd9Sstevel@tonic-gate 		h += 16; i >>= 16;
175*7c478bd9Sstevel@tonic-gate 	}
176*7c478bd9Sstevel@tonic-gate 	if (i & 0xff00) {
177*7c478bd9Sstevel@tonic-gate 		h += 8; i >>= 8;
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 	if (i & 0xf0) {
180*7c478bd9Sstevel@tonic-gate 		h += 4; i >>= 4;
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 	if (i & 0xc) {
183*7c478bd9Sstevel@tonic-gate 		h += 2; i >>= 2;
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 	if (i & 0x2) {
186*7c478bd9Sstevel@tonic-gate 		h += 1;
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	return (h);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate static uint_t
192*7c478bd9Sstevel@tonic-gate fasttrap_hash_str(const char *p)
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	unsigned int g;
195*7c478bd9Sstevel@tonic-gate 	uint_t hval = 0;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	while (*p) {
198*7c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *p++;
199*7c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
200*7c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
201*7c478bd9Sstevel@tonic-gate 		hval &= ~g;
202*7c478bd9Sstevel@tonic-gate 	}
203*7c478bd9Sstevel@tonic-gate 	return (hval);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate void
207*7c478bd9Sstevel@tonic-gate fasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc)
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_signo = SIGTRAP;
212*7c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_code = TRAP_DTRACE;
213*7c478bd9Sstevel@tonic-gate 	sqp->sq_info.si_addr = (caddr_t)pc;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
216*7c478bd9Sstevel@tonic-gate 	sigaddqa(p, t, sqp);
217*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	if (t != NULL)
220*7c478bd9Sstevel@tonic-gate 		aston(t);
221*7c478bd9Sstevel@tonic-gate }
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate /*
224*7c478bd9Sstevel@tonic-gate  * This function ensures that no threads are actively using the memory
225*7c478bd9Sstevel@tonic-gate  * associated with probes that were formerly live.
226*7c478bd9Sstevel@tonic-gate  */
227*7c478bd9Sstevel@tonic-gate static void
228*7c478bd9Sstevel@tonic-gate fasttrap_mod_barrier(uint64_t gen)
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	int i;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	if (gen < fasttrap_mod_gen)
233*7c478bd9Sstevel@tonic-gate 		return;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	fasttrap_mod_gen++;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCPU; i++) {
238*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cpu_core[i].cpuc_pid_lock);
239*7c478bd9Sstevel@tonic-gate 		mutex_exit(&cpu_core[i].cpuc_pid_lock);
240*7c478bd9Sstevel@tonic-gate 	}
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate /*
244*7c478bd9Sstevel@tonic-gate  * This is the timeout's callback for cleaning up the providers and their
245*7c478bd9Sstevel@tonic-gate  * probes.
246*7c478bd9Sstevel@tonic-gate  */
247*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
248*7c478bd9Sstevel@tonic-gate static void
249*7c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup_cb(void *data)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t **fpp, *fp;
252*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
253*7c478bd9Sstevel@tonic-gate 	dtrace_provider_id_t provid;
254*7c478bd9Sstevel@tonic-gate 	int i, later;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	static volatile int in = 0;
257*7c478bd9Sstevel@tonic-gate 	ASSERT(in == 0);
258*7c478bd9Sstevel@tonic-gate 	in = 1;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
261*7c478bd9Sstevel@tonic-gate 	while (fasttrap_cleanup_work) {
262*7c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 0;
263*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 		later = 0;
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 		/*
268*7c478bd9Sstevel@tonic-gate 		 * Iterate over all the providers trying to remove the marked
269*7c478bd9Sstevel@tonic-gate 		 * ones.  If a provider is marked, but not defunct, we just
270*7c478bd9Sstevel@tonic-gate 		 * have to take a crack at removing it -- it's no big deal if
271*7c478bd9Sstevel@tonic-gate 		 * we can't.
272*7c478bd9Sstevel@tonic-gate 		 */
273*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < fasttrap_provs.fth_nent; i++) {
274*7c478bd9Sstevel@tonic-gate 			bucket = &fasttrap_provs.fth_table[i];
275*7c478bd9Sstevel@tonic-gate 			mutex_enter(&bucket->ftb_mtx);
276*7c478bd9Sstevel@tonic-gate 			fpp = (fasttrap_provider_t **)&bucket->ftb_data;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 			while ((fp = *fpp) != NULL) {
279*7c478bd9Sstevel@tonic-gate 				if (!fp->ftp_marked) {
280*7c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
281*7c478bd9Sstevel@tonic-gate 					continue;
282*7c478bd9Sstevel@tonic-gate 				}
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 				mutex_enter(&fp->ftp_mtx);
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 				/*
287*7c478bd9Sstevel@tonic-gate 				 * If this provider is referenced either
288*7c478bd9Sstevel@tonic-gate 				 * because it is a USDT provider or is being
289*7c478bd9Sstevel@tonic-gate 				 * modified, we can't unregister or even
290*7c478bd9Sstevel@tonic-gate 				 * condense.
291*7c478bd9Sstevel@tonic-gate 				 */
292*7c478bd9Sstevel@tonic-gate 				if (fp->ftp_ccount != 0) {
293*7c478bd9Sstevel@tonic-gate 					mutex_exit(&fp->ftp_mtx);
294*7c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
295*7c478bd9Sstevel@tonic-gate 					continue;
296*7c478bd9Sstevel@tonic-gate 				}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 				if (!fp->ftp_defunct || fp->ftp_rcount != 0)
299*7c478bd9Sstevel@tonic-gate 					fp->ftp_marked = 0;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 				mutex_exit(&fp->ftp_mtx);
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 				/*
304*7c478bd9Sstevel@tonic-gate 				 * If we successfully unregister this
305*7c478bd9Sstevel@tonic-gate 				 * provider we can remove it from the hash
306*7c478bd9Sstevel@tonic-gate 				 * chain and free the memory. If our attempt
307*7c478bd9Sstevel@tonic-gate 				 * to unregister fails and this is a defunct
308*7c478bd9Sstevel@tonic-gate 				 * provider, increment our flag to try again
309*7c478bd9Sstevel@tonic-gate 				 * pretty soon. If we've consumed more than
310*7c478bd9Sstevel@tonic-gate 				 * half of our total permitted number of
311*7c478bd9Sstevel@tonic-gate 				 * probes call dtrace_condense() to try to
312*7c478bd9Sstevel@tonic-gate 				 * clean out the unenabled probes.
313*7c478bd9Sstevel@tonic-gate 				 */
314*7c478bd9Sstevel@tonic-gate 				provid = fp->ftp_provid;
315*7c478bd9Sstevel@tonic-gate 				if (dtrace_unregister(provid) != 0) {
316*7c478bd9Sstevel@tonic-gate 					if (fasttrap_total > fasttrap_max / 2)
317*7c478bd9Sstevel@tonic-gate 						(void) dtrace_condense(provid);
318*7c478bd9Sstevel@tonic-gate 					later += fp->ftp_marked;
319*7c478bd9Sstevel@tonic-gate 					fpp = &fp->ftp_next;
320*7c478bd9Sstevel@tonic-gate 				} else {
321*7c478bd9Sstevel@tonic-gate 					*fpp = fp->ftp_next;
322*7c478bd9Sstevel@tonic-gate 					fasttrap_provider_free(fp);
323*7c478bd9Sstevel@tonic-gate 				}
324*7c478bd9Sstevel@tonic-gate 			}
325*7c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
329*7c478bd9Sstevel@tonic-gate 	}
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_timeout != 0);
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/*
334*7c478bd9Sstevel@tonic-gate 	 * If we were unable to remove a defunct provider, try again after
335*7c478bd9Sstevel@tonic-gate 	 * a second. This situation can occur in certain circumstances where
336*7c478bd9Sstevel@tonic-gate 	 * providers cannot be unregistered even though they have no probes
337*7c478bd9Sstevel@tonic-gate 	 * enabled because of an execution of dtrace -l or something similar.
338*7c478bd9Sstevel@tonic-gate 	 * If the timeout has been disabled (set to 1 because we're trying
339*7c478bd9Sstevel@tonic-gate 	 * to detach), we set fasttrap_cleanup_work to ensure that we'll
340*7c478bd9Sstevel@tonic-gate 	 * get a chance to do that work if and when the timeout is reenabled
341*7c478bd9Sstevel@tonic-gate 	 * (if detach fails).
342*7c478bd9Sstevel@tonic-gate 	 */
343*7c478bd9Sstevel@tonic-gate 	if (later > 0 && fasttrap_timeout != (timeout_id_t)1)
344*7c478bd9Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, hz);
345*7c478bd9Sstevel@tonic-gate 	else if (later > 0)
346*7c478bd9Sstevel@tonic-gate 		fasttrap_cleanup_work = 1;
347*7c478bd9Sstevel@tonic-gate 	else
348*7c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
351*7c478bd9Sstevel@tonic-gate 	in = 0;
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate /*
355*7c478bd9Sstevel@tonic-gate  * Activates the asynchronous cleanup mechanism.
356*7c478bd9Sstevel@tonic-gate  */
357*7c478bd9Sstevel@tonic-gate static void
358*7c478bd9Sstevel@tonic-gate fasttrap_pid_cleanup(void)
359*7c478bd9Sstevel@tonic-gate {
360*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
361*7c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 1;
362*7c478bd9Sstevel@tonic-gate 	if (fasttrap_timeout == 0)
363*7c478bd9Sstevel@tonic-gate 		fasttrap_timeout = timeout(&fasttrap_pid_cleanup_cb, NULL, 1);
364*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
365*7c478bd9Sstevel@tonic-gate }
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate /*
368*7c478bd9Sstevel@tonic-gate  * This is called from cfork() via dtrace_fasttrap_fork(). The child
369*7c478bd9Sstevel@tonic-gate  * process's address space is a (roughly) a copy of the parent process's so
370*7c478bd9Sstevel@tonic-gate  * we have to remove all the instrumentation we had previously enabled in the
371*7c478bd9Sstevel@tonic-gate  * parent.
372*7c478bd9Sstevel@tonic-gate  */
373*7c478bd9Sstevel@tonic-gate static void
374*7c478bd9Sstevel@tonic-gate fasttrap_fork(proc_t *p, proc_t *cp)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	pid_t ppid = p->p_pid;
377*7c478bd9Sstevel@tonic-gate 	int i;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	ASSERT(curproc == p);
380*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
381*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_dtrace_count > 0);
382*7c478bd9Sstevel@tonic-gate 	ASSERT(cp->p_dtrace_count == 0);
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	/*
385*7c478bd9Sstevel@tonic-gate 	 * This would be simpler and faster if we maintained per-process
386*7c478bd9Sstevel@tonic-gate 	 * hash tables of enabled tracepoints. It could, however, potentially
387*7c478bd9Sstevel@tonic-gate 	 * slow down execution of a tracepoint since we'd need to go
388*7c478bd9Sstevel@tonic-gate 	 * through two levels of indirection. In the future, we should
389*7c478bd9Sstevel@tonic-gate 	 * consider either maintaining per-process ancillary lists of
390*7c478bd9Sstevel@tonic-gate 	 * enabled tracepoints or hanging a pointer to a per-process hash
391*7c478bd9Sstevel@tonic-gate 	 * table of enabled tracepoints off the proc structure.
392*7c478bd9Sstevel@tonic-gate 	 */
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	/*
395*7c478bd9Sstevel@tonic-gate 	 * We don't have to worry about the child process disappearing
396*7c478bd9Sstevel@tonic-gate 	 * because we're in fork().
397*7c478bd9Sstevel@tonic-gate 	 */
398*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
399*7c478bd9Sstevel@tonic-gate 	sprlock_proc(cp);
400*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate 	/*
403*7c478bd9Sstevel@tonic-gate 	 * Iterate over every tracepoint looking for ones that belong to the
404*7c478bd9Sstevel@tonic-gate 	 * parent process, and remove each from the child process.
405*7c478bd9Sstevel@tonic-gate 	 */
406*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_tpoints.fth_nent; i++) {
407*7c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
408*7c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i];
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
411*7c478bd9Sstevel@tonic-gate 		for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
412*7c478bd9Sstevel@tonic-gate 			if (tp->ftt_pid == ppid && !tp->ftt_prov->ftp_defunct) {
413*7c478bd9Sstevel@tonic-gate 				int ret = fasttrap_tracepoint_remove(cp, tp);
414*7c478bd9Sstevel@tonic-gate 				ASSERT(ret == 0);
415*7c478bd9Sstevel@tonic-gate 			}
416*7c478bd9Sstevel@tonic-gate 		}
417*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
421*7c478bd9Sstevel@tonic-gate 	sprunlock(cp);
422*7c478bd9Sstevel@tonic-gate }
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate /*
425*7c478bd9Sstevel@tonic-gate  * This is called from proc_exit() or from exec_common() if p_dtrace_probes
426*7c478bd9Sstevel@tonic-gate  * is set on the proc structure to indicate that there is a pid provider
427*7c478bd9Sstevel@tonic-gate  * associated with this process.
428*7c478bd9Sstevel@tonic-gate  */
429*7c478bd9Sstevel@tonic-gate static void
430*7c478bd9Sstevel@tonic-gate fasttrap_exec_exit(proc_t *p)
431*7c478bd9Sstevel@tonic-gate {
432*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
433*7c478bd9Sstevel@tonic-gate 
434*7c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);
435*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	/*
440*7c478bd9Sstevel@tonic-gate 	 * We clean up the pid provider for this process here; user-land
441*7c478bd9Sstevel@tonic-gate 	 * static probes are handled by the meta-provider remove entry point.
442*7c478bd9Sstevel@tonic-gate 	 */
443*7c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(p->p_pid,
444*7c478bd9Sstevel@tonic-gate 	    FASTTRAP_PID_NAME, NULL)) != NULL)
445*7c478bd9Sstevel@tonic-gate 		fasttrap_provider_retire(provider);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
452*7c478bd9Sstevel@tonic-gate static void
453*7c478bd9Sstevel@tonic-gate fasttrap_pid_provide(void *arg, const dtrace_probedesc_t *desc)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	/*
456*7c478bd9Sstevel@tonic-gate 	 * There are no "default" pid probes.
457*7c478bd9Sstevel@tonic-gate 	 */
458*7c478bd9Sstevel@tonic-gate }
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
461*7c478bd9Sstevel@tonic-gate static void
462*7c478bd9Sstevel@tonic-gate fasttrap_provide(void *arg, const dtrace_probedesc_t *desc)
463*7c478bd9Sstevel@tonic-gate {
464*7c478bd9Sstevel@tonic-gate 	if (dtrace_probe_lookup(fasttrap_id, NULL, "fasttrap", "fasttrap") == 0)
465*7c478bd9Sstevel@tonic-gate 		fasttrap_probe_id = dtrace_probe_create(fasttrap_id, NULL,
466*7c478bd9Sstevel@tonic-gate 		    "fasttrap", "fasttrap", FASTTRAP_AFRAMES, NULL);
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate static int
470*7c478bd9Sstevel@tonic-gate fasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
471*7c478bd9Sstevel@tonic-gate {
472*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp, *new_tp = NULL;
473*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
474*7c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id;
475*7c478bd9Sstevel@tonic-gate 	pid_t pid;
476*7c478bd9Sstevel@tonic-gate 	uintptr_t pc;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
481*7c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
482*7c478bd9Sstevel@tonic-gate 	id = &probe->ftp_tps[index].fit_id;
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	/*
489*7c478bd9Sstevel@tonic-gate 	 * Before we make any modifications, make sure we've imposed a barrier
490*7c478bd9Sstevel@tonic-gate 	 * on the generation in which this probe was last modified.
491*7c478bd9Sstevel@tonic-gate 	 */
492*7c478bd9Sstevel@tonic-gate 	fasttrap_mod_barrier(probe->ftp_gen);
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	/*
497*7c478bd9Sstevel@tonic-gate 	 * If the tracepoint has already been enabled, just add our id to the
498*7c478bd9Sstevel@tonic-gate 	 * list of interested probes. This may be our second time through
499*7c478bd9Sstevel@tonic-gate 	 * this path in which case we'll have constructed the tracepoint we'd
500*7c478bd9Sstevel@tonic-gate 	 * like to install. If we can't find a match, and have an allocated
501*7c478bd9Sstevel@tonic-gate 	 * tracepoint ready to go, enable that one now.
502*7c478bd9Sstevel@tonic-gate 	 *
503*7c478bd9Sstevel@tonic-gate 	 * Tracepoints whose provider is now defunct are also considered
504*7c478bd9Sstevel@tonic-gate 	 * defunct.
505*7c478bd9Sstevel@tonic-gate 	 */
506*7c478bd9Sstevel@tonic-gate again:
507*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
508*7c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
509*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid != pid || tp->ftt_pc != pc ||
510*7c478bd9Sstevel@tonic-gate 		    tp->ftt_prov->ftp_defunct)
511*7c478bd9Sstevel@tonic-gate 			continue;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 		/*
514*7c478bd9Sstevel@tonic-gate 		 * Now that we've found a matching tracepoint, it would be
515*7c478bd9Sstevel@tonic-gate 		 * a decent idea to confirm that the tracepoint is still
516*7c478bd9Sstevel@tonic-gate 		 * enabled and the trap instruction hasn't been overwritten.
517*7c478bd9Sstevel@tonic-gate 		 * Since this is a little hairy, we'll punt for now.
518*7c478bd9Sstevel@tonic-gate 		 */
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 		/*
521*7c478bd9Sstevel@tonic-gate 		 * This can't be the first interested probe. We don't have
522*7c478bd9Sstevel@tonic-gate 		 * to worry about another thread being in the midst of
523*7c478bd9Sstevel@tonic-gate 		 * deleting this tracepoint (which would be the only valid
524*7c478bd9Sstevel@tonic-gate 		 * reason for a tracepoint to have no interested probes)
525*7c478bd9Sstevel@tonic-gate 		 * since we're holding P_PR_LOCK for this process.
526*7c478bd9Sstevel@tonic-gate 		 */
527*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL);
528*7c478bd9Sstevel@tonic-gate 
529*7c478bd9Sstevel@tonic-gate 		if (probe->ftp_type == DTFTP_RETURN ||
530*7c478bd9Sstevel@tonic-gate 		    probe->ftp_type == DTFTP_POST_OFFSETS) {
531*7c478bd9Sstevel@tonic-gate 			id->fti_next = tp->ftt_retids;
532*7c478bd9Sstevel@tonic-gate 			membar_producer();
533*7c478bd9Sstevel@tonic-gate 			tp->ftt_retids = id;
534*7c478bd9Sstevel@tonic-gate 			membar_producer();
535*7c478bd9Sstevel@tonic-gate 		} else {
536*7c478bd9Sstevel@tonic-gate 			id->fti_next = tp->ftt_ids;
537*7c478bd9Sstevel@tonic-gate 			membar_producer();
538*7c478bd9Sstevel@tonic-gate 			tp->ftt_ids = id;
539*7c478bd9Sstevel@tonic-gate 			membar_producer();
540*7c478bd9Sstevel@tonic-gate 		}
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 		if (new_tp != NULL) {
545*7c478bd9Sstevel@tonic-gate 			new_tp->ftt_ids = NULL;
546*7c478bd9Sstevel@tonic-gate 			new_tp->ftt_retids = NULL;
547*7c478bd9Sstevel@tonic-gate 		}
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 		return (0);
550*7c478bd9Sstevel@tonic-gate 	}
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 	/*
553*7c478bd9Sstevel@tonic-gate 	 * If we have a good tracepoint ready to go, install it now while
554*7c478bd9Sstevel@tonic-gate 	 * we have the lock held and no one can screw with us.
555*7c478bd9Sstevel@tonic-gate 	 */
556*7c478bd9Sstevel@tonic-gate 	if (new_tp != NULL) {
557*7c478bd9Sstevel@tonic-gate 		int rc;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 		new_tp->ftt_next = bucket->ftb_data;
560*7c478bd9Sstevel@tonic-gate 		membar_producer();
561*7c478bd9Sstevel@tonic-gate 		bucket->ftb_data = new_tp;
562*7c478bd9Sstevel@tonic-gate 		membar_producer();
563*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 		/*
566*7c478bd9Sstevel@tonic-gate 		 * Activate the tracepoint in the isa-specific manner.
567*7c478bd9Sstevel@tonic-gate 		 */
568*7c478bd9Sstevel@tonic-gate 		if (fasttrap_tracepoint_install(p, new_tp) != 0) {
569*7c478bd9Sstevel@tonic-gate 			rc = 1;
570*7c478bd9Sstevel@tonic-gate 		} else {
571*7c478bd9Sstevel@tonic-gate 			/*
572*7c478bd9Sstevel@tonic-gate 			 * Increment the count of the number of tracepoints
573*7c478bd9Sstevel@tonic-gate 			 * active in the victim process.
574*7c478bd9Sstevel@tonic-gate 			 */
575*7c478bd9Sstevel@tonic-gate 			ASSERT(p->p_proc_flag & P_PR_LOCK);
576*7c478bd9Sstevel@tonic-gate 			p->p_dtrace_count++;
577*7c478bd9Sstevel@tonic-gate 			rc = 0;
578*7c478bd9Sstevel@tonic-gate 		}
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 		return (rc);
581*7c478bd9Sstevel@tonic-gate 	}
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
584*7c478bd9Sstevel@tonic-gate 
585*7c478bd9Sstevel@tonic-gate 	/*
586*7c478bd9Sstevel@tonic-gate 	 * Initialize the tracepoint that's been preallocated with the probe.
587*7c478bd9Sstevel@tonic-gate 	 */
588*7c478bd9Sstevel@tonic-gate 	new_tp = probe->ftp_tps[index].fit_tp;
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pid == pid);
591*7c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_pc == pc);
592*7c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_prov == probe->ftp_prov);
593*7c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_ids == NULL);
594*7c478bd9Sstevel@tonic-gate 	ASSERT(new_tp->ftt_retids == NULL);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_type == DTFTP_RETURN ||
597*7c478bd9Sstevel@tonic-gate 	    probe->ftp_type == DTFTP_POST_OFFSETS) {
598*7c478bd9Sstevel@tonic-gate 		id->fti_next = NULL;
599*7c478bd9Sstevel@tonic-gate 		new_tp->ftt_retids = id;
600*7c478bd9Sstevel@tonic-gate 	} else {
601*7c478bd9Sstevel@tonic-gate 		id->fti_next = NULL;
602*7c478bd9Sstevel@tonic-gate 		new_tp->ftt_ids = id;
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	/*
606*7c478bd9Sstevel@tonic-gate 	 * If the isa-dependent initialization goes to plan, go back to the
607*7c478bd9Sstevel@tonic-gate 	 * beginning and try to install this freshly made tracepoint.
608*7c478bd9Sstevel@tonic-gate 	 */
609*7c478bd9Sstevel@tonic-gate 	if (fasttrap_tracepoint_init(p, probe, new_tp, pc) == 0)
610*7c478bd9Sstevel@tonic-gate 		goto again;
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	new_tp->ftt_ids = NULL;
613*7c478bd9Sstevel@tonic-gate 	new_tp->ftt_retids = NULL;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	return (1);
616*7c478bd9Sstevel@tonic-gate }
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate static void
619*7c478bd9Sstevel@tonic-gate fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index)
620*7c478bd9Sstevel@tonic-gate {
621*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
622*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
623*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t **pp, *tp;
624*7c478bd9Sstevel@tonic-gate 	fasttrap_id_t *id, **idp;
625*7c478bd9Sstevel@tonic-gate 	pid_t pid;
626*7c478bd9Sstevel@tonic-gate 	uintptr_t pc;
627*7c478bd9Sstevel@tonic-gate 
628*7c478bd9Sstevel@tonic-gate 	ASSERT(index < probe->ftp_ntps);
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 	pid = probe->ftp_pid;
631*7c478bd9Sstevel@tonic-gate 	pc = probe->ftp_tps[index].fit_tp->ftt_pc;
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid);
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	/*
636*7c478bd9Sstevel@tonic-gate 	 * Find the tracepoint and make sure that our id is one of the
637*7c478bd9Sstevel@tonic-gate 	 * ones registered with it.
638*7c478bd9Sstevel@tonic-gate 	 */
639*7c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)];
640*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
641*7c478bd9Sstevel@tonic-gate 	for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) {
642*7c478bd9Sstevel@tonic-gate 		if (tp->ftt_pid == pid && tp->ftt_pc == pc &&
643*7c478bd9Sstevel@tonic-gate 		    tp->ftt_prov == provider)
644*7c478bd9Sstevel@tonic-gate 			break;
645*7c478bd9Sstevel@tonic-gate 	}
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	/*
648*7c478bd9Sstevel@tonic-gate 	 * If we somehow lost this tracepoint, we're in a world of hurt.
649*7c478bd9Sstevel@tonic-gate 	 */
650*7c478bd9Sstevel@tonic-gate 	ASSERT(tp != NULL);
651*7c478bd9Sstevel@tonic-gate 
652*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_type == DTFTP_RETURN ||
653*7c478bd9Sstevel@tonic-gate 	    probe->ftp_type == DTFTP_POST_OFFSETS) {
654*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_retids != NULL);
655*7c478bd9Sstevel@tonic-gate 		idp = &tp->ftt_retids;
656*7c478bd9Sstevel@tonic-gate 	} else {
657*7c478bd9Sstevel@tonic-gate 		ASSERT(tp->ftt_ids != NULL);
658*7c478bd9Sstevel@tonic-gate 		idp = &tp->ftt_ids;
659*7c478bd9Sstevel@tonic-gate 	}
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate 	while ((*idp)->fti_probe != probe) {
662*7c478bd9Sstevel@tonic-gate 		idp = &(*idp)->fti_next;
663*7c478bd9Sstevel@tonic-gate 		ASSERT(*idp != NULL);
664*7c478bd9Sstevel@tonic-gate 	}
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	id = *idp;
667*7c478bd9Sstevel@tonic-gate 	*idp = id->fti_next;
668*7c478bd9Sstevel@tonic-gate 	membar_producer();
669*7c478bd9Sstevel@tonic-gate 
670*7c478bd9Sstevel@tonic-gate 	ASSERT(id->fti_probe == probe);
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	/*
673*7c478bd9Sstevel@tonic-gate 	 * If there are other registered enablings of this tracepoint, we're
674*7c478bd9Sstevel@tonic-gate 	 * all done, but if this was the last probe assocated with this
675*7c478bd9Sstevel@tonic-gate 	 * this tracepoint, we need to remove and free it.
676*7c478bd9Sstevel@tonic-gate 	 */
677*7c478bd9Sstevel@tonic-gate 	if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) {
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate 		/*
680*7c478bd9Sstevel@tonic-gate 		 * If the current probe's tracepoint is in use, swap it
681*7c478bd9Sstevel@tonic-gate 		 * for an unused tracepoint.
682*7c478bd9Sstevel@tonic-gate 		 */
683*7c478bd9Sstevel@tonic-gate 		if (tp == probe->ftp_tps[index].fit_tp) {
684*7c478bd9Sstevel@tonic-gate 			fasttrap_probe_t *tmp_probe;
685*7c478bd9Sstevel@tonic-gate 			fasttrap_tracepoint_t **tmp_tp;
686*7c478bd9Sstevel@tonic-gate 			uint_t tmp_index;
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 			if (tp->ftt_ids != NULL) {
689*7c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_ids->fti_probe;
690*7c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids);
691*7c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
692*7c478bd9Sstevel@tonic-gate 			} else {
693*7c478bd9Sstevel@tonic-gate 				tmp_probe = tp->ftt_retids->fti_probe;
694*7c478bd9Sstevel@tonic-gate 				tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids);
695*7c478bd9Sstevel@tonic-gate 				tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp;
696*7c478bd9Sstevel@tonic-gate 			}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != NULL);
699*7c478bd9Sstevel@tonic-gate 			ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp);
700*7c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_ids == NULL);
701*7c478bd9Sstevel@tonic-gate 			ASSERT((*tmp_tp)->ftt_retids == NULL);
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 			probe->ftp_tps[index].fit_tp = *tmp_tp;
704*7c478bd9Sstevel@tonic-gate 			*tmp_tp = tp;
705*7c478bd9Sstevel@tonic-gate 
706*7c478bd9Sstevel@tonic-gate 		}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 		/*
711*7c478bd9Sstevel@tonic-gate 		 * Tag the modified probe with the generation in which it was
712*7c478bd9Sstevel@tonic-gate 		 * changed.
713*7c478bd9Sstevel@tonic-gate 		 */
714*7c478bd9Sstevel@tonic-gate 		probe->ftp_gen = fasttrap_mod_gen;
715*7c478bd9Sstevel@tonic-gate 		return;
716*7c478bd9Sstevel@tonic-gate 	}
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	/*
721*7c478bd9Sstevel@tonic-gate 	 * We can't safely remove the tracepoint from the set of active
722*7c478bd9Sstevel@tonic-gate 	 * tracepoints until we've actually removed the fasttrap instruction
723*7c478bd9Sstevel@tonic-gate 	 * from the process's text. We can, however, operate on this
724*7c478bd9Sstevel@tonic-gate 	 * tracepoint secure in the knowledge that no other thread is going to
725*7c478bd9Sstevel@tonic-gate 	 * be looking at it since we hold P_PR_LOCK on the process if it's
726*7c478bd9Sstevel@tonic-gate 	 * live or we hold the provider lock on the process if it's dead and
727*7c478bd9Sstevel@tonic-gate 	 * gone.
728*7c478bd9Sstevel@tonic-gate 	 */
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	/*
731*7c478bd9Sstevel@tonic-gate 	 * We only need to remove the actual instruction if we're looking
732*7c478bd9Sstevel@tonic-gate 	 * at an existing process
733*7c478bd9Sstevel@tonic-gate 	 */
734*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
735*7c478bd9Sstevel@tonic-gate 		/*
736*7c478bd9Sstevel@tonic-gate 		 * If we fail to restore the instruction we need to kill
737*7c478bd9Sstevel@tonic-gate 		 * this process since it's in a completely unrecoverable
738*7c478bd9Sstevel@tonic-gate 		 * state.
739*7c478bd9Sstevel@tonic-gate 		 */
740*7c478bd9Sstevel@tonic-gate 		if (fasttrap_tracepoint_remove(p, tp) != 0)
741*7c478bd9Sstevel@tonic-gate 			fasttrap_sigtrap(p, NULL, pc);
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 		/*
744*7c478bd9Sstevel@tonic-gate 		 * Decrement the count of the number of tracepoints active
745*7c478bd9Sstevel@tonic-gate 		 * in the victim process.
746*7c478bd9Sstevel@tonic-gate 		 */
747*7c478bd9Sstevel@tonic-gate 		ASSERT(p->p_proc_flag & P_PR_LOCK);
748*7c478bd9Sstevel@tonic-gate 		p->p_dtrace_count--;
749*7c478bd9Sstevel@tonic-gate 	}
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	/*
752*7c478bd9Sstevel@tonic-gate 	 * Remove the probe from the hash table of active tracepoints.
753*7c478bd9Sstevel@tonic-gate 	 */
754*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
755*7c478bd9Sstevel@tonic-gate 	pp = (fasttrap_tracepoint_t **)&bucket->ftb_data;
756*7c478bd9Sstevel@tonic-gate 	ASSERT(*pp != NULL);
757*7c478bd9Sstevel@tonic-gate 	while (*pp != tp) {
758*7c478bd9Sstevel@tonic-gate 		pp = &(*pp)->ftt_next;
759*7c478bd9Sstevel@tonic-gate 		ASSERT(*pp != NULL);
760*7c478bd9Sstevel@tonic-gate 	}
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 	*pp = tp->ftt_next;
763*7c478bd9Sstevel@tonic-gate 	membar_producer();
764*7c478bd9Sstevel@tonic-gate 
765*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	/*
768*7c478bd9Sstevel@tonic-gate 	 * Tag the modified probe with the generation in which it was changed.
769*7c478bd9Sstevel@tonic-gate 	 */
770*7c478bd9Sstevel@tonic-gate 	probe->ftp_gen = fasttrap_mod_gen;
771*7c478bd9Sstevel@tonic-gate }
772*7c478bd9Sstevel@tonic-gate 
773*7c478bd9Sstevel@tonic-gate typedef int fasttrap_probe_f(struct regs *);
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate static void
776*7c478bd9Sstevel@tonic-gate fasttrap_enable_common(int *count, fasttrap_probe_f **fptr, fasttrap_probe_f *f,
777*7c478bd9Sstevel@tonic-gate     fasttrap_probe_f **fptr2, fasttrap_probe_f *f2)
778*7c478bd9Sstevel@tonic-gate {
779*7c478bd9Sstevel@tonic-gate 	/*
780*7c478bd9Sstevel@tonic-gate 	 * We don't have to play the rw lock game here because we're
781*7c478bd9Sstevel@tonic-gate 	 * providing something rather than taking something away --
782*7c478bd9Sstevel@tonic-gate 	 * we can be sure that no threads have tried to follow this
783*7c478bd9Sstevel@tonic-gate 	 * function pointer yet.
784*7c478bd9Sstevel@tonic-gate 	 */
785*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
786*7c478bd9Sstevel@tonic-gate 	if (*count == 0) {
787*7c478bd9Sstevel@tonic-gate 		ASSERT(*fptr == NULL);
788*7c478bd9Sstevel@tonic-gate 		*fptr = f;
789*7c478bd9Sstevel@tonic-gate 		if (fptr2 != NULL)
790*7c478bd9Sstevel@tonic-gate 			*fptr2 = f2;
791*7c478bd9Sstevel@tonic-gate 	}
792*7c478bd9Sstevel@tonic-gate 	ASSERT(*fptr == f);
793*7c478bd9Sstevel@tonic-gate 	ASSERT(fptr2 == NULL || *fptr2 == f2);
794*7c478bd9Sstevel@tonic-gate 	(*count)++;
795*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
796*7c478bd9Sstevel@tonic-gate }
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate static void
799*7c478bd9Sstevel@tonic-gate fasttrap_disable_common(int *count, fasttrap_probe_f **fptr,
800*7c478bd9Sstevel@tonic-gate     fasttrap_probe_f **fptr2)
801*7c478bd9Sstevel@tonic-gate {
802*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
803*7c478bd9Sstevel@tonic-gate 
804*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
805*7c478bd9Sstevel@tonic-gate 	(*count)--;
806*7c478bd9Sstevel@tonic-gate 	ASSERT(*count >= 0);
807*7c478bd9Sstevel@tonic-gate 	if (*count == 0) {
808*7c478bd9Sstevel@tonic-gate 		cpu_t *cur, *cpu = CPU;
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
811*7c478bd9Sstevel@tonic-gate 			cur = cur->cpu_next_onln) {
812*7c478bd9Sstevel@tonic-gate 			rw_enter(&cur->cpu_ft_lock, RW_WRITER);
813*7c478bd9Sstevel@tonic-gate 		}
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 		*fptr = NULL;
816*7c478bd9Sstevel@tonic-gate 		if (fptr2 != NULL)
817*7c478bd9Sstevel@tonic-gate 			*fptr2 = NULL;
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate 		for (cur = cpu->cpu_next_onln; cur != cpu;
820*7c478bd9Sstevel@tonic-gate 			cur = cur->cpu_next_onln) {
821*7c478bd9Sstevel@tonic-gate 			rw_exit(&cur->cpu_ft_lock);
822*7c478bd9Sstevel@tonic-gate 		}
823*7c478bd9Sstevel@tonic-gate 	}
824*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
825*7c478bd9Sstevel@tonic-gate }
826*7c478bd9Sstevel@tonic-gate 
827*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
828*7c478bd9Sstevel@tonic-gate static void
829*7c478bd9Sstevel@tonic-gate fasttrap_enable(void *arg, dtrace_id_t id, void *parg)
830*7c478bd9Sstevel@tonic-gate {
831*7c478bd9Sstevel@tonic-gate 	/*
832*7c478bd9Sstevel@tonic-gate 	 * Enable the probe that corresponds to statically placed trace
833*7c478bd9Sstevel@tonic-gate 	 * points which have not explicitly been placed in the process's text
834*7c478bd9Sstevel@tonic-gate 	 * by the fasttrap provider.
835*7c478bd9Sstevel@tonic-gate 	 */
836*7c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
837*7c478bd9Sstevel@tonic-gate 	ASSERT(id == fasttrap_probe_id);
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 	fasttrap_enable_common(&fasttrap_count,
840*7c478bd9Sstevel@tonic-gate 	    &dtrace_fasttrap_probe_ptr, fasttrap_probe, NULL, NULL);
841*7c478bd9Sstevel@tonic-gate }
842*7c478bd9Sstevel@tonic-gate 
843*7c478bd9Sstevel@tonic-gate 
844*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
845*7c478bd9Sstevel@tonic-gate static void
846*7c478bd9Sstevel@tonic-gate fasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg)
847*7c478bd9Sstevel@tonic-gate {
848*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
849*7c478bd9Sstevel@tonic-gate 	proc_t *p;
850*7c478bd9Sstevel@tonic-gate 	int i;
851*7c478bd9Sstevel@tonic-gate 
852*7c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
853*7c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
854*7c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
855*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
856*7c478bd9Sstevel@tonic-gate 
857*7c478bd9Sstevel@tonic-gate 	/*
858*7c478bd9Sstevel@tonic-gate 	 * Increment the count of enabled probes on this probe's provider;
859*7c478bd9Sstevel@tonic-gate 	 * the provider can't go away while the probe still exists. We
860*7c478bd9Sstevel@tonic-gate 	 * must increment this even if we aren't able to properly enable
861*7c478bd9Sstevel@tonic-gate 	 * this probe.
862*7c478bd9Sstevel@tonic-gate 	 */
863*7c478bd9Sstevel@tonic-gate 	mutex_enter(&probe->ftp_prov->ftp_mtx);
864*7c478bd9Sstevel@tonic-gate 	probe->ftp_prov->ftp_rcount++;
865*7c478bd9Sstevel@tonic-gate 	mutex_exit(&probe->ftp_prov->ftp_mtx);
866*7c478bd9Sstevel@tonic-gate 
867*7c478bd9Sstevel@tonic-gate 	/*
868*7c478bd9Sstevel@tonic-gate 	 * Bail out if we can't find the process for this probe or its
869*7c478bd9Sstevel@tonic-gate 	 * provider is defunct (meaning it was valid in a previously exec'ed
870*7c478bd9Sstevel@tonic-gate 	 * incarnation of this address space). The provider can't go away
871*7c478bd9Sstevel@tonic-gate 	 * while we're in this code path.
872*7c478bd9Sstevel@tonic-gate 	 */
873*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_prov->ftp_defunct ||
874*7c478bd9Sstevel@tonic-gate 	    (p = sprlock(probe->ftp_pid)) == NULL)
875*7c478bd9Sstevel@tonic-gate 		return;
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 	ASSERT(!(p->p_flag & SVFORK));
878*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
879*7c478bd9Sstevel@tonic-gate 
880*7c478bd9Sstevel@tonic-gate 	/*
881*7c478bd9Sstevel@tonic-gate 	 * We have to enable the trap entry before any user threads have
882*7c478bd9Sstevel@tonic-gate 	 * the chance to execute the trap instruction we're about to place
883*7c478bd9Sstevel@tonic-gate 	 * in their process's text.
884*7c478bd9Sstevel@tonic-gate 	 */
885*7c478bd9Sstevel@tonic-gate 	fasttrap_enable_common(&fasttrap_pid_count,
886*7c478bd9Sstevel@tonic-gate 	    &dtrace_pid_probe_ptr, fasttrap_pid_probe,
887*7c478bd9Sstevel@tonic-gate 	    &dtrace_return_probe_ptr, fasttrap_return_probe);
888*7c478bd9Sstevel@tonic-gate 
889*7c478bd9Sstevel@tonic-gate 	/*
890*7c478bd9Sstevel@tonic-gate 	 * Enable all the tracepoints and add this probe's id to each
891*7c478bd9Sstevel@tonic-gate 	 * tracepoint's list of active probes.
892*7c478bd9Sstevel@tonic-gate 	 */
893*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
894*7c478bd9Sstevel@tonic-gate 		if (fasttrap_tracepoint_enable(p, probe, i) != 0) {
895*7c478bd9Sstevel@tonic-gate 			/*
896*7c478bd9Sstevel@tonic-gate 			 * Back up and pull out all the tracepoints we've
897*7c478bd9Sstevel@tonic-gate 			 * created so far for this probe.
898*7c478bd9Sstevel@tonic-gate 			 */
899*7c478bd9Sstevel@tonic-gate 			while (--i >= 0) {
900*7c478bd9Sstevel@tonic-gate 				fasttrap_tracepoint_disable(p, probe, i);
901*7c478bd9Sstevel@tonic-gate 			}
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
904*7c478bd9Sstevel@tonic-gate 			sprunlock(p);
905*7c478bd9Sstevel@tonic-gate 
906*7c478bd9Sstevel@tonic-gate 			/*
907*7c478bd9Sstevel@tonic-gate 			 * Since we're not actually enabling this probe,
908*7c478bd9Sstevel@tonic-gate 			 * drop our reference on the trap table entry.
909*7c478bd9Sstevel@tonic-gate 			 */
910*7c478bd9Sstevel@tonic-gate 			fasttrap_disable_common(&fasttrap_pid_count,
911*7c478bd9Sstevel@tonic-gate 			    &dtrace_pid_probe_ptr, &dtrace_return_probe_ptr);
912*7c478bd9Sstevel@tonic-gate 			return;
913*7c478bd9Sstevel@tonic-gate 		}
914*7c478bd9Sstevel@tonic-gate 	}
915*7c478bd9Sstevel@tonic-gate 
916*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
917*7c478bd9Sstevel@tonic-gate 	sprunlock(p);
918*7c478bd9Sstevel@tonic-gate 
919*7c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 1;
920*7c478bd9Sstevel@tonic-gate }
921*7c478bd9Sstevel@tonic-gate 
922*7c478bd9Sstevel@tonic-gate 
923*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
924*7c478bd9Sstevel@tonic-gate static void
925*7c478bd9Sstevel@tonic-gate fasttrap_disable(void *arg, dtrace_id_t id, void *parg)
926*7c478bd9Sstevel@tonic-gate {
927*7c478bd9Sstevel@tonic-gate 	/*
928*7c478bd9Sstevel@tonic-gate 	 * Disable the probe the corresponds to statically placed trace
929*7c478bd9Sstevel@tonic-gate 	 * points.
930*7c478bd9Sstevel@tonic-gate 	 */
931*7c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
932*7c478bd9Sstevel@tonic-gate 	ASSERT(id == fasttrap_probe_id);
933*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
934*7c478bd9Sstevel@tonic-gate 	fasttrap_disable_common(&fasttrap_count, &dtrace_fasttrap_probe_ptr,
935*7c478bd9Sstevel@tonic-gate 	    NULL);
936*7c478bd9Sstevel@tonic-gate }
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
939*7c478bd9Sstevel@tonic-gate static void
940*7c478bd9Sstevel@tonic-gate fasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg)
941*7c478bd9Sstevel@tonic-gate {
942*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
943*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = probe->ftp_prov;
944*7c478bd9Sstevel@tonic-gate 	proc_t *p;
945*7c478bd9Sstevel@tonic-gate 	int i, whack = 0;
946*7c478bd9Sstevel@tonic-gate 
947*7c478bd9Sstevel@tonic-gate 	if (!probe->ftp_enabled) {
948*7c478bd9Sstevel@tonic-gate 		mutex_enter(&provider->ftp_mtx);
949*7c478bd9Sstevel@tonic-gate 		provider->ftp_rcount--;
950*7c478bd9Sstevel@tonic-gate 		ASSERT(provider->ftp_rcount >= 0);
951*7c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
952*7c478bd9Sstevel@tonic-gate 		return;
953*7c478bd9Sstevel@tonic-gate 	}
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate 	ASSERT(id == probe->ftp_id);
956*7c478bd9Sstevel@tonic-gate 
957*7c478bd9Sstevel@tonic-gate 	/*
958*7c478bd9Sstevel@tonic-gate 	 * We won't be able to acquire a /proc-esque lock on the process
959*7c478bd9Sstevel@tonic-gate 	 * iff the process is dead and gone. In this case, we rely on the
960*7c478bd9Sstevel@tonic-gate 	 * provider lock as a point of mutual exclusion to prevent other
961*7c478bd9Sstevel@tonic-gate 	 * DTrace consumers from disabling this probe.
962*7c478bd9Sstevel@tonic-gate 	 */
963*7c478bd9Sstevel@tonic-gate 	if ((p = sprlock(probe->ftp_pid)) != NULL) {
964*7c478bd9Sstevel@tonic-gate 		ASSERT(!(p->p_flag & SVFORK));
965*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
966*7c478bd9Sstevel@tonic-gate 	}
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
969*7c478bd9Sstevel@tonic-gate 
970*7c478bd9Sstevel@tonic-gate 	/*
971*7c478bd9Sstevel@tonic-gate 	 * Disable all the associated tracepoints.
972*7c478bd9Sstevel@tonic-gate 	 */
973*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
974*7c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_disable(p, probe, i);
975*7c478bd9Sstevel@tonic-gate 	}
976*7c478bd9Sstevel@tonic-gate 
977*7c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount > 0);
978*7c478bd9Sstevel@tonic-gate 	provider->ftp_rcount--;
979*7c478bd9Sstevel@tonic-gate 
980*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
981*7c478bd9Sstevel@tonic-gate 		/*
982*7c478bd9Sstevel@tonic-gate 		 * Even though we may not be able to remove it entirely, we
983*7c478bd9Sstevel@tonic-gate 		 * mark this defunct provider to get a chance to remove some
984*7c478bd9Sstevel@tonic-gate 		 * of the associated probes.
985*7c478bd9Sstevel@tonic-gate 		 */
986*7c478bd9Sstevel@tonic-gate 		if (provider->ftp_defunct && !provider->ftp_marked)
987*7c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
988*7c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
989*7c478bd9Sstevel@tonic-gate 
990*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
991*7c478bd9Sstevel@tonic-gate 		sprunlock(p);
992*7c478bd9Sstevel@tonic-gate 	} else {
993*7c478bd9Sstevel@tonic-gate 		/*
994*7c478bd9Sstevel@tonic-gate 		 * If the process is dead, we're just waiting for the
995*7c478bd9Sstevel@tonic-gate 		 * last probe to be disabled to be able to free it.
996*7c478bd9Sstevel@tonic-gate 		 */
997*7c478bd9Sstevel@tonic-gate 		if (provider->ftp_rcount == 0 && !provider->ftp_marked)
998*7c478bd9Sstevel@tonic-gate 			whack = provider->ftp_marked = 1;
999*7c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
1000*7c478bd9Sstevel@tonic-gate 	}
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate 	if (whack)
1003*7c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
1004*7c478bd9Sstevel@tonic-gate 
1005*7c478bd9Sstevel@tonic-gate 	probe->ftp_enabled = 0;
1006*7c478bd9Sstevel@tonic-gate 
1007*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
1008*7c478bd9Sstevel@tonic-gate 	fasttrap_disable_common(&fasttrap_pid_count, &dtrace_pid_probe_ptr,
1009*7c478bd9Sstevel@tonic-gate 	    &dtrace_return_probe_ptr);
1010*7c478bd9Sstevel@tonic-gate }
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1013*7c478bd9Sstevel@tonic-gate static void
1014*7c478bd9Sstevel@tonic-gate fasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg,
1015*7c478bd9Sstevel@tonic-gate     dtrace_argdesc_t *desc)
1016*7c478bd9Sstevel@tonic-gate {
1017*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
1018*7c478bd9Sstevel@tonic-gate 	char *str;
1019*7c478bd9Sstevel@tonic-gate 	int i;
1020*7c478bd9Sstevel@tonic-gate 
1021*7c478bd9Sstevel@tonic-gate 	desc->dtargd_native[0] = '\0';
1022*7c478bd9Sstevel@tonic-gate 	desc->dtargd_xlate[0] = '\0';
1023*7c478bd9Sstevel@tonic-gate 
1024*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_prov->ftp_defunct != 0 ||
1025*7c478bd9Sstevel@tonic-gate 	    desc->dtargd_ndx >= probe->ftp_nargs) {
1026*7c478bd9Sstevel@tonic-gate 		desc->dtargd_ndx = DTRACE_ARGNONE;
1027*7c478bd9Sstevel@tonic-gate 		return;
1028*7c478bd9Sstevel@tonic-gate 	}
1029*7c478bd9Sstevel@tonic-gate 
1030*7c478bd9Sstevel@tonic-gate 	/*
1031*7c478bd9Sstevel@tonic-gate 	 * We only need to set this member if the argument is remapped.
1032*7c478bd9Sstevel@tonic-gate 	 */
1033*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_argmap != NULL)
1034*7c478bd9Sstevel@tonic-gate 		desc->dtargd_mapping = probe->ftp_argmap[desc->dtargd_ndx];
1035*7c478bd9Sstevel@tonic-gate 
1036*7c478bd9Sstevel@tonic-gate 	str = probe->ftp_ntypes;
1037*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < desc->dtargd_mapping; i++) {
1038*7c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
1039*7c478bd9Sstevel@tonic-gate 	}
1040*7c478bd9Sstevel@tonic-gate 
1041*7c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native));
1042*7c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_native, str);
1043*7c478bd9Sstevel@tonic-gate 
1044*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_xtypes == NULL)
1045*7c478bd9Sstevel@tonic-gate 		return;
1046*7c478bd9Sstevel@tonic-gate 
1047*7c478bd9Sstevel@tonic-gate 	str = probe->ftp_xtypes;
1048*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < desc->dtargd_ndx; i++) {
1049*7c478bd9Sstevel@tonic-gate 		str += strlen(str) + 1;
1050*7c478bd9Sstevel@tonic-gate 	}
1051*7c478bd9Sstevel@tonic-gate 
1052*7c478bd9Sstevel@tonic-gate 	ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate));
1053*7c478bd9Sstevel@tonic-gate 	(void) strcpy(desc->dtargd_xlate, str);
1054*7c478bd9Sstevel@tonic-gate }
1055*7c478bd9Sstevel@tonic-gate 
1056*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1057*7c478bd9Sstevel@tonic-gate static void
1058*7c478bd9Sstevel@tonic-gate fasttrap_destroy(void *arg, dtrace_id_t id, void *parg)
1059*7c478bd9Sstevel@tonic-gate {
1060*7c478bd9Sstevel@tonic-gate 	ASSERT(arg == NULL);
1061*7c478bd9Sstevel@tonic-gate 	ASSERT(id == fasttrap_probe_id);
1062*7c478bd9Sstevel@tonic-gate }
1063*7c478bd9Sstevel@tonic-gate 
1064*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1065*7c478bd9Sstevel@tonic-gate static void
1066*7c478bd9Sstevel@tonic-gate fasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg)
1067*7c478bd9Sstevel@tonic-gate {
1068*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *probe = parg;
1069*7c478bd9Sstevel@tonic-gate 	int i;
1070*7c478bd9Sstevel@tonic-gate 	size_t size;
1071*7c478bd9Sstevel@tonic-gate 
1072*7c478bd9Sstevel@tonic-gate 	ASSERT(probe != NULL);
1073*7c478bd9Sstevel@tonic-gate 	ASSERT(!probe->ftp_enabled);
1074*7c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_total >= probe->ftp_ntps);
1075*7c478bd9Sstevel@tonic-gate 
1076*7c478bd9Sstevel@tonic-gate 	atomic_add_32(&fasttrap_total, -probe->ftp_ntps);
1077*7c478bd9Sstevel@tonic-gate 	size = sizeof (fasttrap_probe_t) +
1078*7c478bd9Sstevel@tonic-gate 	    sizeof (probe->ftp_tps[0]) * (probe->ftp_ntps - 1);
1079*7c478bd9Sstevel@tonic-gate 
1080*7c478bd9Sstevel@tonic-gate 	if (probe->ftp_gen + 1 >= fasttrap_mod_gen)
1081*7c478bd9Sstevel@tonic-gate 		fasttrap_mod_barrier(probe->ftp_gen);
1082*7c478bd9Sstevel@tonic-gate 
1083*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < probe->ftp_ntps; i++) {
1084*7c478bd9Sstevel@tonic-gate 		kmem_free(probe->ftp_tps[i].fit_tp,
1085*7c478bd9Sstevel@tonic-gate 		    sizeof (fasttrap_tracepoint_t));
1086*7c478bd9Sstevel@tonic-gate 	}
1087*7c478bd9Sstevel@tonic-gate 
1088*7c478bd9Sstevel@tonic-gate 	kmem_free(probe, size);
1089*7c478bd9Sstevel@tonic-gate }
1090*7c478bd9Sstevel@tonic-gate 
1091*7c478bd9Sstevel@tonic-gate 
1092*7c478bd9Sstevel@tonic-gate static const dtrace_pattr_t fasttrap_attr = {
1093*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1094*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1095*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1096*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1097*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1098*7c478bd9Sstevel@tonic-gate };
1099*7c478bd9Sstevel@tonic-gate 
1100*7c478bd9Sstevel@tonic-gate static dtrace_pops_t fasttrap_pops = {
1101*7c478bd9Sstevel@tonic-gate 	fasttrap_provide,
1102*7c478bd9Sstevel@tonic-gate 	NULL,
1103*7c478bd9Sstevel@tonic-gate 	fasttrap_enable,
1104*7c478bd9Sstevel@tonic-gate 	fasttrap_disable,
1105*7c478bd9Sstevel@tonic-gate 	NULL,
1106*7c478bd9Sstevel@tonic-gate 	NULL,
1107*7c478bd9Sstevel@tonic-gate 	NULL,
1108*7c478bd9Sstevel@tonic-gate 	fasttrap_getarg,
1109*7c478bd9Sstevel@tonic-gate 	NULL,
1110*7c478bd9Sstevel@tonic-gate 	fasttrap_destroy
1111*7c478bd9Sstevel@tonic-gate };
1112*7c478bd9Sstevel@tonic-gate 
1113*7c478bd9Sstevel@tonic-gate static const dtrace_pattr_t pid_attr = {
1114*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1115*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1116*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1117*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA },
1118*7c478bd9Sstevel@tonic-gate { DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
1119*7c478bd9Sstevel@tonic-gate };
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate static dtrace_pops_t pid_pops = {
1122*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
1123*7c478bd9Sstevel@tonic-gate 	NULL,
1124*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
1125*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
1126*7c478bd9Sstevel@tonic-gate 	NULL,
1127*7c478bd9Sstevel@tonic-gate 	NULL,
1128*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
1129*7c478bd9Sstevel@tonic-gate 	fasttrap_getarg,
1130*7c478bd9Sstevel@tonic-gate 	NULL,
1131*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
1132*7c478bd9Sstevel@tonic-gate };
1133*7c478bd9Sstevel@tonic-gate 
1134*7c478bd9Sstevel@tonic-gate static dtrace_pops_t usdt_pops = {
1135*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_provide,
1136*7c478bd9Sstevel@tonic-gate 	NULL,
1137*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_enable,
1138*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_disable,
1139*7c478bd9Sstevel@tonic-gate 	NULL,
1140*7c478bd9Sstevel@tonic-gate 	NULL,
1141*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_getargdesc,
1142*7c478bd9Sstevel@tonic-gate 	fasttrap_usdt_getarg,
1143*7c478bd9Sstevel@tonic-gate 	NULL,
1144*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_destroy
1145*7c478bd9Sstevel@tonic-gate };
1146*7c478bd9Sstevel@tonic-gate 
1147*7c478bd9Sstevel@tonic-gate /*
1148*7c478bd9Sstevel@tonic-gate  * Lookup a fasttrap-managed provider based on its name and associated pid.
1149*7c478bd9Sstevel@tonic-gate  * If the pattr argument is non-NULL, this function instantiates the provider
1150*7c478bd9Sstevel@tonic-gate  * if it doesn't exist otherwise it returns NULL. The provider is returned
1151*7c478bd9Sstevel@tonic-gate  * with its lock held.
1152*7c478bd9Sstevel@tonic-gate  */
1153*7c478bd9Sstevel@tonic-gate static fasttrap_provider_t *
1154*7c478bd9Sstevel@tonic-gate fasttrap_provider_lookup(pid_t pid, const char *name,
1155*7c478bd9Sstevel@tonic-gate     const dtrace_pattr_t *pattr)
1156*7c478bd9Sstevel@tonic-gate {
1157*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *fp, *new_fp = NULL;
1158*7c478bd9Sstevel@tonic-gate 	fasttrap_bucket_t *bucket;
1159*7c478bd9Sstevel@tonic-gate 	char provname[DTRACE_PROVNAMELEN];
1160*7c478bd9Sstevel@tonic-gate 	proc_t *p;
1161*7c478bd9Sstevel@tonic-gate 	uid_t uid = (uid_t)-1;
1162*7c478bd9Sstevel@tonic-gate 
1163*7c478bd9Sstevel@tonic-gate 	ASSERT(strlen(name) < sizeof (fp->ftp_name));
1164*7c478bd9Sstevel@tonic-gate 
1165*7c478bd9Sstevel@tonic-gate 	bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)];
1166*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
1167*7c478bd9Sstevel@tonic-gate 
1168*7c478bd9Sstevel@tonic-gate 	/*
1169*7c478bd9Sstevel@tonic-gate 	 * Take a lap through the list and return the match if we find it.
1170*7c478bd9Sstevel@tonic-gate 	 */
1171*7c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1172*7c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1173*7c478bd9Sstevel@tonic-gate 		    !fp->ftp_defunct) {
1174*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
1175*7c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
1176*7c478bd9Sstevel@tonic-gate 			return (fp);
1177*7c478bd9Sstevel@tonic-gate 		}
1178*7c478bd9Sstevel@tonic-gate 	}
1179*7c478bd9Sstevel@tonic-gate 
1180*7c478bd9Sstevel@tonic-gate 	/*
1181*7c478bd9Sstevel@tonic-gate 	 * Drop the bucket lock so we don't try to perform a sleeping
1182*7c478bd9Sstevel@tonic-gate 	 * allocation under it.
1183*7c478bd9Sstevel@tonic-gate 	 */
1184*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
1185*7c478bd9Sstevel@tonic-gate 
1186*7c478bd9Sstevel@tonic-gate 	if (pattr == NULL)
1187*7c478bd9Sstevel@tonic-gate 		return (NULL);
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 	/*
1190*7c478bd9Sstevel@tonic-gate 	 * Make sure the process exists, isn't a child created as the result
1191*7c478bd9Sstevel@tonic-gate 	 * of a vfork(2), and isn't a zombie (but may be in fork). Record the
1192*7c478bd9Sstevel@tonic-gate 	 * process's uid to pass to dtrace_register().
1193*7c478bd9Sstevel@tonic-gate 	 */
1194*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
1195*7c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL ||
1196*7c478bd9Sstevel@tonic-gate 	    (p->p_flag & (SVFORK | SEXITLWPS)) ||
1197*7c478bd9Sstevel@tonic-gate 	    (p->p_lwpcnt == 0 && p->p_lwpdir != NULL)) {
1198*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1199*7c478bd9Sstevel@tonic-gate 		return (NULL);
1200*7c478bd9Sstevel@tonic-gate 	}
1201*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1202*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1203*7c478bd9Sstevel@tonic-gate 
1204*7c478bd9Sstevel@tonic-gate 	/*
1205*7c478bd9Sstevel@tonic-gate 	 * Increment p_dtrace_probes so that the process knows to inform us
1206*7c478bd9Sstevel@tonic-gate 	 * when it exits or execs. fasttrap_provider_free() decrements this
1207*7c478bd9Sstevel@tonic-gate 	 * when we're done with this provider.
1208*7c478bd9Sstevel@tonic-gate 	 */
1209*7c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes++;
1210*7c478bd9Sstevel@tonic-gate 
1211*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
1212*7c478bd9Sstevel@tonic-gate 	uid = crgetruid(p->p_cred);
1213*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
1214*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1215*7c478bd9Sstevel@tonic-gate 
1216*7c478bd9Sstevel@tonic-gate 	new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP);
1217*7c478bd9Sstevel@tonic-gate 
1218*7c478bd9Sstevel@tonic-gate 	mutex_enter(&bucket->ftb_mtx);
1219*7c478bd9Sstevel@tonic-gate 
1220*7c478bd9Sstevel@tonic-gate 	/*
1221*7c478bd9Sstevel@tonic-gate 	 * Take another lap through the list to make sure a provider hasn't
1222*7c478bd9Sstevel@tonic-gate 	 * been created for this pid while we weren't under the bucket lock.
1223*7c478bd9Sstevel@tonic-gate 	 */
1224*7c478bd9Sstevel@tonic-gate 	for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) {
1225*7c478bd9Sstevel@tonic-gate 		if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 &&
1226*7c478bd9Sstevel@tonic-gate 		    !fp->ftp_defunct) {
1227*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
1228*7c478bd9Sstevel@tonic-gate 			mutex_exit(&bucket->ftb_mtx);
1229*7c478bd9Sstevel@tonic-gate 			fasttrap_provider_free(new_fp);
1230*7c478bd9Sstevel@tonic-gate 			return (fp);
1231*7c478bd9Sstevel@tonic-gate 		}
1232*7c478bd9Sstevel@tonic-gate 	}
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate 	new_fp->ftp_pid = pid;
1235*7c478bd9Sstevel@tonic-gate 	(void) strcpy(new_fp->ftp_name, name);
1236*7c478bd9Sstevel@tonic-gate 
1237*7c478bd9Sstevel@tonic-gate 	/*
1238*7c478bd9Sstevel@tonic-gate 	 * Fail and return NULL if either the provider name is too long
1239*7c478bd9Sstevel@tonic-gate 	 * or we fail to register this new provider with the DTrace
1240*7c478bd9Sstevel@tonic-gate 	 * framework. Note that this is the only place we ever construct
1241*7c478bd9Sstevel@tonic-gate 	 * the full provider name -- we keep it in pieces in the provider
1242*7c478bd9Sstevel@tonic-gate 	 * structure.
1243*7c478bd9Sstevel@tonic-gate 	 */
1244*7c478bd9Sstevel@tonic-gate 	if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >=
1245*7c478bd9Sstevel@tonic-gate 	    sizeof (provname) ||
1246*7c478bd9Sstevel@tonic-gate 	    dtrace_register(provname, pattr,
1247*7c478bd9Sstevel@tonic-gate 	    DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER, uid,
1248*7c478bd9Sstevel@tonic-gate 	    pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp,
1249*7c478bd9Sstevel@tonic-gate 	    &new_fp->ftp_provid) != 0) {
1250*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
1251*7c478bd9Sstevel@tonic-gate 		fasttrap_provider_free(new_fp);
1252*7c478bd9Sstevel@tonic-gate 		return (NULL);
1253*7c478bd9Sstevel@tonic-gate 	}
1254*7c478bd9Sstevel@tonic-gate 
1255*7c478bd9Sstevel@tonic-gate 	new_fp->ftp_next = bucket->ftb_data;
1256*7c478bd9Sstevel@tonic-gate 	bucket->ftb_data = new_fp;
1257*7c478bd9Sstevel@tonic-gate 
1258*7c478bd9Sstevel@tonic-gate 	mutex_enter(&new_fp->ftp_mtx);
1259*7c478bd9Sstevel@tonic-gate 	mutex_exit(&bucket->ftb_mtx);
1260*7c478bd9Sstevel@tonic-gate 
1261*7c478bd9Sstevel@tonic-gate 	return (new_fp);
1262*7c478bd9Sstevel@tonic-gate }
1263*7c478bd9Sstevel@tonic-gate 
1264*7c478bd9Sstevel@tonic-gate static void
1265*7c478bd9Sstevel@tonic-gate fasttrap_provider_free(fasttrap_provider_t *provider)
1266*7c478bd9Sstevel@tonic-gate {
1267*7c478bd9Sstevel@tonic-gate 	pid_t pid = provider->ftp_pid;
1268*7c478bd9Sstevel@tonic-gate 	proc_t *p;
1269*7c478bd9Sstevel@tonic-gate 
1270*7c478bd9Sstevel@tonic-gate 	/*
1271*7c478bd9Sstevel@tonic-gate 	 * There need to be no consumers using this provider and no
1272*7c478bd9Sstevel@tonic-gate 	 * associated enabled probes.
1273*7c478bd9Sstevel@tonic-gate 	 */
1274*7c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_ccount == 0);
1275*7c478bd9Sstevel@tonic-gate 	ASSERT(provider->ftp_rcount == 0);
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 	kmem_free(provider, sizeof (fasttrap_provider_t));
1278*7c478bd9Sstevel@tonic-gate 
1279*7c478bd9Sstevel@tonic-gate 	/*
1280*7c478bd9Sstevel@tonic-gate 	 * Decrement p_dtrace_probes on the process whose provider we're
1281*7c478bd9Sstevel@tonic-gate 	 * freeing. We don't have to worry about clobbering somone else's
1282*7c478bd9Sstevel@tonic-gate 	 * modifications to it because we have locked the bucket that
1283*7c478bd9Sstevel@tonic-gate 	 * corresponds to this process's hash chain in the provider hash
1284*7c478bd9Sstevel@tonic-gate 	 * table. Don't sweat it if we can't find the process.
1285*7c478bd9Sstevel@tonic-gate 	 */
1286*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
1287*7c478bd9Sstevel@tonic-gate 	if ((p = prfind(pid)) == NULL) {
1288*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
1289*7c478bd9Sstevel@tonic-gate 		return;
1290*7c478bd9Sstevel@tonic-gate 	}
1291*7c478bd9Sstevel@tonic-gate 
1292*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1293*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate 	p->p_dtrace_probes--;
1296*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1297*7c478bd9Sstevel@tonic-gate }
1298*7c478bd9Sstevel@tonic-gate 
1299*7c478bd9Sstevel@tonic-gate static void
1300*7c478bd9Sstevel@tonic-gate fasttrap_provider_retire(fasttrap_provider_t *provider)
1301*7c478bd9Sstevel@tonic-gate {
1302*7c478bd9Sstevel@tonic-gate 	dtrace_provider_id_t provid = provider->ftp_provid;
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate 	/*
1305*7c478bd9Sstevel@tonic-gate 	 * Mark the provider to be removed in our post-processing step
1306*7c478bd9Sstevel@tonic-gate 	 * and mark it as defunct. The former indicates that we should try
1307*7c478bd9Sstevel@tonic-gate 	 * to remove it, the latter indicates that even if we were unable
1308*7c478bd9Sstevel@tonic-gate 	 * to remove it, this provider shouldn't be used to create probes
1309*7c478bd9Sstevel@tonic-gate 	 * in the future.
1310*7c478bd9Sstevel@tonic-gate 	 */
1311*7c478bd9Sstevel@tonic-gate 	provider->ftp_defunct = 1;
1312*7c478bd9Sstevel@tonic-gate 	provider->ftp_marked = 1;
1313*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1314*7c478bd9Sstevel@tonic-gate 
1315*7c478bd9Sstevel@tonic-gate 	/*
1316*7c478bd9Sstevel@tonic-gate 	 * We don't have to worry about invalidating the same provider twice
1317*7c478bd9Sstevel@tonic-gate 	 * since fasttrap_provider_lookup() will ignore provider that have
1318*7c478bd9Sstevel@tonic-gate 	 * been marked as defunct.
1319*7c478bd9Sstevel@tonic-gate 	 */
1320*7c478bd9Sstevel@tonic-gate 	dtrace_invalidate(provid);
1321*7c478bd9Sstevel@tonic-gate 
1322*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
1323*7c478bd9Sstevel@tonic-gate }
1324*7c478bd9Sstevel@tonic-gate 
1325*7c478bd9Sstevel@tonic-gate static int
1326*7c478bd9Sstevel@tonic-gate fasttrap_add_probe(fasttrap_probe_spec_t *pdata)
1327*7c478bd9Sstevel@tonic-gate {
1328*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
1329*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
1330*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
1331*7c478bd9Sstevel@tonic-gate 	char *name;
1332*7c478bd9Sstevel@tonic-gate 	size_t size;
1333*7c478bd9Sstevel@tonic-gate 	int i, aframes, whack;
1334*7c478bd9Sstevel@tonic-gate 
1335*7c478bd9Sstevel@tonic-gate 	switch (pdata->ftps_type) {
1336*7c478bd9Sstevel@tonic-gate 	case DTFTP_ENTRY:
1337*7c478bd9Sstevel@tonic-gate 		name = "entry";
1338*7c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_ENTRY_AFRAMES;
1339*7c478bd9Sstevel@tonic-gate 		break;
1340*7c478bd9Sstevel@tonic-gate 	case DTFTP_RETURN:
1341*7c478bd9Sstevel@tonic-gate 		name = "return";
1342*7c478bd9Sstevel@tonic-gate 		aframes = FASTTRAP_RETURN_AFRAMES;
1343*7c478bd9Sstevel@tonic-gate 		break;
1344*7c478bd9Sstevel@tonic-gate 	case DTFTP_OFFSETS:
1345*7c478bd9Sstevel@tonic-gate 		name = NULL;
1346*7c478bd9Sstevel@tonic-gate 		break;
1347*7c478bd9Sstevel@tonic-gate 	default:
1348*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
1349*7c478bd9Sstevel@tonic-gate 	}
1350*7c478bd9Sstevel@tonic-gate 
1351*7c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pdata->ftps_pid,
1352*7c478bd9Sstevel@tonic-gate 	    FASTTRAP_PID_NAME, &pid_attr)) == NULL)
1353*7c478bd9Sstevel@tonic-gate 		return (ESRCH);
1354*7c478bd9Sstevel@tonic-gate 
1355*7c478bd9Sstevel@tonic-gate 	/*
1356*7c478bd9Sstevel@tonic-gate 	 * Increment this reference count to indicate that a consumer is
1357*7c478bd9Sstevel@tonic-gate 	 * actively adding a new probe associated with this provider.
1358*7c478bd9Sstevel@tonic-gate 	 */
1359*7c478bd9Sstevel@tonic-gate 	provider->ftp_ccount++;
1360*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1361*7c478bd9Sstevel@tonic-gate 
1362*7c478bd9Sstevel@tonic-gate 	if (name != NULL) {
1363*7c478bd9Sstevel@tonic-gate 		if (dtrace_probe_lookup(provider->ftp_provid,
1364*7c478bd9Sstevel@tonic-gate 		    pdata->ftps_mod, pdata->ftps_func, name) != 0)
1365*7c478bd9Sstevel@tonic-gate 			goto done;
1366*7c478bd9Sstevel@tonic-gate 
1367*7c478bd9Sstevel@tonic-gate 		atomic_add_32(&fasttrap_total, pdata->ftps_noffs);
1368*7c478bd9Sstevel@tonic-gate 
1369*7c478bd9Sstevel@tonic-gate 		if (fasttrap_total > fasttrap_max) {
1370*7c478bd9Sstevel@tonic-gate 			atomic_add_32(&fasttrap_total, -pdata->ftps_noffs);
1371*7c478bd9Sstevel@tonic-gate 			goto no_mem;
1372*7c478bd9Sstevel@tonic-gate 		}
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 		ASSERT(pdata->ftps_noffs > 0);
1375*7c478bd9Sstevel@tonic-gate 		size = sizeof (fasttrap_probe_t) +
1376*7c478bd9Sstevel@tonic-gate 		    sizeof (pp->ftp_tps[0]) * (pdata->ftps_noffs - 1);
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate 		pp = kmem_zalloc(size, KM_SLEEP);
1379*7c478bd9Sstevel@tonic-gate 
1380*7c478bd9Sstevel@tonic-gate 		pp->ftp_prov = provider;
1381*7c478bd9Sstevel@tonic-gate 		pp->ftp_faddr = pdata->ftps_pc;
1382*7c478bd9Sstevel@tonic-gate 		pp->ftp_fsize = pdata->ftps_size;
1383*7c478bd9Sstevel@tonic-gate 		pp->ftp_pid = pdata->ftps_pid;
1384*7c478bd9Sstevel@tonic-gate 		pp->ftp_ntps = pdata->ftps_noffs;
1385*7c478bd9Sstevel@tonic-gate 		pp->ftp_type = pdata->ftps_type;
1386*7c478bd9Sstevel@tonic-gate 
1387*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < pdata->ftps_noffs; i++) {
1388*7c478bd9Sstevel@tonic-gate 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1389*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
1390*7c478bd9Sstevel@tonic-gate 
1391*7c478bd9Sstevel@tonic-gate 			tp->ftt_prov = provider;
1392*7c478bd9Sstevel@tonic-gate 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1393*7c478bd9Sstevel@tonic-gate 			tp->ftt_pid = pdata->ftps_pid;
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 			pp->ftp_tps[i].fit_tp = tp;
1396*7c478bd9Sstevel@tonic-gate 			pp->ftp_tps[i].fit_id.fti_probe = pp;
1397*7c478bd9Sstevel@tonic-gate 		}
1398*7c478bd9Sstevel@tonic-gate 
1399*7c478bd9Sstevel@tonic-gate 		pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1400*7c478bd9Sstevel@tonic-gate 		    pdata->ftps_mod, pdata->ftps_func, name, aframes, pp);
1401*7c478bd9Sstevel@tonic-gate 	} else {
1402*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < pdata->ftps_noffs; i++) {
1403*7c478bd9Sstevel@tonic-gate 			char name_str[17];
1404*7c478bd9Sstevel@tonic-gate 
1405*7c478bd9Sstevel@tonic-gate 			(void) sprintf(name_str, "%llx",
1406*7c478bd9Sstevel@tonic-gate 			    (unsigned long long)pdata->ftps_offs[i]);
1407*7c478bd9Sstevel@tonic-gate 
1408*7c478bd9Sstevel@tonic-gate 			if (dtrace_probe_lookup(provider->ftp_provid,
1409*7c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str) != 0)
1410*7c478bd9Sstevel@tonic-gate 				continue;
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 			atomic_add_32(&fasttrap_total, 1);
1413*7c478bd9Sstevel@tonic-gate 
1414*7c478bd9Sstevel@tonic-gate 			if (fasttrap_total > fasttrap_max) {
1415*7c478bd9Sstevel@tonic-gate 				atomic_add_32(&fasttrap_total, -1);
1416*7c478bd9Sstevel@tonic-gate 				goto no_mem;
1417*7c478bd9Sstevel@tonic-gate 			}
1418*7c478bd9Sstevel@tonic-gate 
1419*7c478bd9Sstevel@tonic-gate 			pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP);
1420*7c478bd9Sstevel@tonic-gate 
1421*7c478bd9Sstevel@tonic-gate 			pp->ftp_prov = provider;
1422*7c478bd9Sstevel@tonic-gate 			pp->ftp_faddr = pdata->ftps_pc;
1423*7c478bd9Sstevel@tonic-gate 			pp->ftp_fsize = pdata->ftps_size;
1424*7c478bd9Sstevel@tonic-gate 			pp->ftp_pid = pdata->ftps_pid;
1425*7c478bd9Sstevel@tonic-gate 			pp->ftp_ntps = 1;
1426*7c478bd9Sstevel@tonic-gate 			pp->ftp_type = pdata->ftps_type;
1427*7c478bd9Sstevel@tonic-gate 
1428*7c478bd9Sstevel@tonic-gate 			tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t),
1429*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
1430*7c478bd9Sstevel@tonic-gate 
1431*7c478bd9Sstevel@tonic-gate 			tp->ftt_prov = provider;
1432*7c478bd9Sstevel@tonic-gate 			tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc;
1433*7c478bd9Sstevel@tonic-gate 			tp->ftt_pid = pdata->ftps_pid;
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_tp = tp;
1436*7c478bd9Sstevel@tonic-gate 			pp->ftp_tps[0].fit_id.fti_probe = pp;
1437*7c478bd9Sstevel@tonic-gate 
1438*7c478bd9Sstevel@tonic-gate 			pp->ftp_id = dtrace_probe_create(provider->ftp_provid,
1439*7c478bd9Sstevel@tonic-gate 			    pdata->ftps_mod, pdata->ftps_func, name_str,
1440*7c478bd9Sstevel@tonic-gate 			    FASTTRAP_OFFSET_AFRAMES, pp);
1441*7c478bd9Sstevel@tonic-gate 		}
1442*7c478bd9Sstevel@tonic-gate 	}
1443*7c478bd9Sstevel@tonic-gate 
1444*7c478bd9Sstevel@tonic-gate done:
1445*7c478bd9Sstevel@tonic-gate 	/*
1446*7c478bd9Sstevel@tonic-gate 	 * We know that the provider is still valid since we incremented the
1447*7c478bd9Sstevel@tonic-gate 	 * reference count. If someone tried to free this provider while we
1448*7c478bd9Sstevel@tonic-gate 	 * were using it (e.g. because the process called exec(2) or exit(2)),
1449*7c478bd9Sstevel@tonic-gate 	 * take note of that and try to free it now.
1450*7c478bd9Sstevel@tonic-gate 	 */
1451*7c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
1452*7c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
1453*7c478bd9Sstevel@tonic-gate 	whack = provider->ftp_defunct;
1454*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1455*7c478bd9Sstevel@tonic-gate 
1456*7c478bd9Sstevel@tonic-gate 	if (whack)
1457*7c478bd9Sstevel@tonic-gate 		fasttrap_pid_cleanup();
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate 	return (0);
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate no_mem:
1462*7c478bd9Sstevel@tonic-gate 	/*
1463*7c478bd9Sstevel@tonic-gate 	 * If we've exhausted the allowable resources, we'll try to remove
1464*7c478bd9Sstevel@tonic-gate 	 * this provider to free some up. This is to cover the case where
1465*7c478bd9Sstevel@tonic-gate 	 * the user has accidentally created many more probes than was
1466*7c478bd9Sstevel@tonic-gate 	 * intended (e.g. pid123:::).
1467*7c478bd9Sstevel@tonic-gate 	 */
1468*7c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
1469*7c478bd9Sstevel@tonic-gate 	provider->ftp_ccount--;
1470*7c478bd9Sstevel@tonic-gate 	provider->ftp_marked = 1;
1471*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1472*7c478bd9Sstevel@tonic-gate 
1473*7c478bd9Sstevel@tonic-gate 	fasttrap_pid_cleanup();
1474*7c478bd9Sstevel@tonic-gate 
1475*7c478bd9Sstevel@tonic-gate 	return (ENOMEM);
1476*7c478bd9Sstevel@tonic-gate }
1477*7c478bd9Sstevel@tonic-gate 
1478*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1479*7c478bd9Sstevel@tonic-gate static void *
1480*7c478bd9Sstevel@tonic-gate fasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
1481*7c478bd9Sstevel@tonic-gate {
1482*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
1483*7c478bd9Sstevel@tonic-gate 
1484*7c478bd9Sstevel@tonic-gate 	/*
1485*7c478bd9Sstevel@tonic-gate 	 * A 32-bit unsigned integer (like a pid for example) can be
1486*7c478bd9Sstevel@tonic-gate 	 * expressed in 10 or fewer decimal digits. Make sure that we'll
1487*7c478bd9Sstevel@tonic-gate 	 * have enough space for the provider name.
1488*7c478bd9Sstevel@tonic-gate 	 */
1489*7c478bd9Sstevel@tonic-gate 	if (strlen(dhpv->dthpv_provname) + 10 >=
1490*7c478bd9Sstevel@tonic-gate 	    sizeof (provider->ftp_name)) {
1491*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
1492*7c478bd9Sstevel@tonic-gate 		    "name too long to accomodate pid", dhpv->dthpv_provname);
1493*7c478bd9Sstevel@tonic-gate 		return (NULL);
1494*7c478bd9Sstevel@tonic-gate 	}
1495*7c478bd9Sstevel@tonic-gate 
1496*7c478bd9Sstevel@tonic-gate 	/*
1497*7c478bd9Sstevel@tonic-gate 	 * Don't let folks spoof the true pid provider.
1498*7c478bd9Sstevel@tonic-gate 	 */
1499*7c478bd9Sstevel@tonic-gate 	if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) {
1500*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s: "
1501*7c478bd9Sstevel@tonic-gate 		    "%s is an invalid name", dhpv->dthpv_provname,
1502*7c478bd9Sstevel@tonic-gate 		    FASTTRAP_PID_NAME);
1503*7c478bd9Sstevel@tonic-gate 		return (NULL);
1504*7c478bd9Sstevel@tonic-gate 	}
1505*7c478bd9Sstevel@tonic-gate 
1506*7c478bd9Sstevel@tonic-gate 	/*
1507*7c478bd9Sstevel@tonic-gate 	 * The highest stability class that fasttrap supports is ISA; cap
1508*7c478bd9Sstevel@tonic-gate 	 * the stability of the new provider accordingly.
1509*7c478bd9Sstevel@tonic-gate 	 */
1510*7c478bd9Sstevel@tonic-gate 	if (dhpv->dthpv_pattr.dtpa_provider.dtat_class >= DTRACE_CLASS_COMMON)
1511*7c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA;
1512*7c478bd9Sstevel@tonic-gate 	if (dhpv->dthpv_pattr.dtpa_mod.dtat_class >= DTRACE_CLASS_COMMON)
1513*7c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA;
1514*7c478bd9Sstevel@tonic-gate 	if (dhpv->dthpv_pattr.dtpa_func.dtat_class >= DTRACE_CLASS_COMMON)
1515*7c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA;
1516*7c478bd9Sstevel@tonic-gate 	if (dhpv->dthpv_pattr.dtpa_name.dtat_class >= DTRACE_CLASS_COMMON)
1517*7c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA;
1518*7c478bd9Sstevel@tonic-gate 	if (dhpv->dthpv_pattr.dtpa_args.dtat_class >= DTRACE_CLASS_COMMON)
1519*7c478bd9Sstevel@tonic-gate 		dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA;
1520*7c478bd9Sstevel@tonic-gate 
1521*7c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname,
1522*7c478bd9Sstevel@tonic-gate 	    &dhpv->dthpv_pattr)) == NULL) {
1523*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "failed to instantiate provider %s for "
1524*7c478bd9Sstevel@tonic-gate 		    "process %u",  dhpv->dthpv_provname, (uint_t)pid);
1525*7c478bd9Sstevel@tonic-gate 		return (NULL);
1526*7c478bd9Sstevel@tonic-gate 	}
1527*7c478bd9Sstevel@tonic-gate 
1528*7c478bd9Sstevel@tonic-gate 	/*
1529*7c478bd9Sstevel@tonic-gate 	 * We elevate the consumer count here to ensure that this provider
1530*7c478bd9Sstevel@tonic-gate 	 * isn't removed until after the meta provider has been told to
1531*7c478bd9Sstevel@tonic-gate 	 * remove it.
1532*7c478bd9Sstevel@tonic-gate 	 */
1533*7c478bd9Sstevel@tonic-gate 	provider->ftp_ccount++;
1534*7c478bd9Sstevel@tonic-gate 
1535*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1536*7c478bd9Sstevel@tonic-gate 
1537*7c478bd9Sstevel@tonic-gate 	return (provider);
1538*7c478bd9Sstevel@tonic-gate }
1539*7c478bd9Sstevel@tonic-gate 
1540*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1541*7c478bd9Sstevel@tonic-gate static void
1542*7c478bd9Sstevel@tonic-gate fasttrap_meta_create_probe(void *arg, void *parg,
1543*7c478bd9Sstevel@tonic-gate     dtrace_helper_probedesc_t *dhpb)
1544*7c478bd9Sstevel@tonic-gate {
1545*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider = parg;
1546*7c478bd9Sstevel@tonic-gate 	fasttrap_probe_t *pp;
1547*7c478bd9Sstevel@tonic-gate 	fasttrap_tracepoint_t *tp;
1548*7c478bd9Sstevel@tonic-gate 	size_t size;
1549*7c478bd9Sstevel@tonic-gate 	int i;
1550*7c478bd9Sstevel@tonic-gate 
1551*7c478bd9Sstevel@tonic-gate 	mutex_enter(&provider->ftp_mtx);
1552*7c478bd9Sstevel@tonic-gate 
1553*7c478bd9Sstevel@tonic-gate 	if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod,
1554*7c478bd9Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name) != 0) {
1555*7c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
1556*7c478bd9Sstevel@tonic-gate 		return;
1557*7c478bd9Sstevel@tonic-gate 	}
1558*7c478bd9Sstevel@tonic-gate 
1559*7c478bd9Sstevel@tonic-gate 	atomic_add_32(&fasttrap_total, dhpb->dthpb_noffs);
1560*7c478bd9Sstevel@tonic-gate 
1561*7c478bd9Sstevel@tonic-gate 	if (fasttrap_total > fasttrap_max) {
1562*7c478bd9Sstevel@tonic-gate 		atomic_add_32(&fasttrap_total, -dhpb->dthpb_noffs);
1563*7c478bd9Sstevel@tonic-gate 		mutex_exit(&provider->ftp_mtx);
1564*7c478bd9Sstevel@tonic-gate 		return;
1565*7c478bd9Sstevel@tonic-gate 	}
1566*7c478bd9Sstevel@tonic-gate 
1567*7c478bd9Sstevel@tonic-gate 	size = sizeof (fasttrap_probe_t) +
1568*7c478bd9Sstevel@tonic-gate 	    sizeof (pp->ftp_tps[0]) * (dhpb->dthpb_noffs - 1);
1569*7c478bd9Sstevel@tonic-gate 	pp = kmem_zalloc(size, KM_SLEEP);
1570*7c478bd9Sstevel@tonic-gate 
1571*7c478bd9Sstevel@tonic-gate 	pp->ftp_prov = provider;
1572*7c478bd9Sstevel@tonic-gate 	pp->ftp_pid = provider->ftp_pid;
1573*7c478bd9Sstevel@tonic-gate 	pp->ftp_ntps = dhpb->dthpb_noffs;
1574*7c478bd9Sstevel@tonic-gate #ifdef __sparc
1575*7c478bd9Sstevel@tonic-gate 	pp->ftp_type = DTFTP_POST_OFFSETS;
1576*7c478bd9Sstevel@tonic-gate #else
1577*7c478bd9Sstevel@tonic-gate 	pp->ftp_type = DTFTP_OFFSETS;
1578*7c478bd9Sstevel@tonic-gate #endif
1579*7c478bd9Sstevel@tonic-gate 	pp->ftp_nargs = dhpb->dthpb_xargc;
1580*7c478bd9Sstevel@tonic-gate 	pp->ftp_xtypes = dhpb->dthpb_xtypes;
1581*7c478bd9Sstevel@tonic-gate 	pp->ftp_ntypes = dhpb->dthpb_ntypes;
1582*7c478bd9Sstevel@tonic-gate 
1583*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < pp->ftp_ntps; i++) {
1584*7c478bd9Sstevel@tonic-gate 		tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP);
1585*7c478bd9Sstevel@tonic-gate 
1586*7c478bd9Sstevel@tonic-gate 		tp->ftt_prov = provider;
1587*7c478bd9Sstevel@tonic-gate 		tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i];
1588*7c478bd9Sstevel@tonic-gate 		tp->ftt_pid = provider->ftp_pid;
1589*7c478bd9Sstevel@tonic-gate 
1590*7c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_tp = tp;
1591*7c478bd9Sstevel@tonic-gate 		pp->ftp_tps[i].fit_id.fti_probe = pp;
1592*7c478bd9Sstevel@tonic-gate 	}
1593*7c478bd9Sstevel@tonic-gate 
1594*7c478bd9Sstevel@tonic-gate 	/*
1595*7c478bd9Sstevel@tonic-gate 	 * If the arguments are shuffled around we set the argument remapping
1596*7c478bd9Sstevel@tonic-gate 	 * table. Later, when the probe fires, we only remap the arguments
1597*7c478bd9Sstevel@tonic-gate 	 * if the table is non-NULL.
1598*7c478bd9Sstevel@tonic-gate 	 */
1599*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < dhpb->dthpb_xargc; i++) {
1600*7c478bd9Sstevel@tonic-gate 		if (dhpb->dthpb_args[i] != i) {
1601*7c478bd9Sstevel@tonic-gate 			pp->ftp_argmap = dhpb->dthpb_args;
1602*7c478bd9Sstevel@tonic-gate 			break;
1603*7c478bd9Sstevel@tonic-gate 		}
1604*7c478bd9Sstevel@tonic-gate 	}
1605*7c478bd9Sstevel@tonic-gate 
1606*7c478bd9Sstevel@tonic-gate 	/*
1607*7c478bd9Sstevel@tonic-gate 	 * The probe is fully constructed -- register it with DTrace.
1608*7c478bd9Sstevel@tonic-gate 	 */
1609*7c478bd9Sstevel@tonic-gate 	pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod,
1610*7c478bd9Sstevel@tonic-gate 	    dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp);
1611*7c478bd9Sstevel@tonic-gate 
1612*7c478bd9Sstevel@tonic-gate 	mutex_exit(&provider->ftp_mtx);
1613*7c478bd9Sstevel@tonic-gate }
1614*7c478bd9Sstevel@tonic-gate 
1615*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1616*7c478bd9Sstevel@tonic-gate static void
1617*7c478bd9Sstevel@tonic-gate fasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid)
1618*7c478bd9Sstevel@tonic-gate {
1619*7c478bd9Sstevel@tonic-gate 	fasttrap_provider_t *provider;
1620*7c478bd9Sstevel@tonic-gate 
1621*7c478bd9Sstevel@tonic-gate 	if ((provider = fasttrap_provider_lookup(pid,
1622*7c478bd9Sstevel@tonic-gate 	    dhpv->dthpv_provname, NULL)) != NULL) {
1623*7c478bd9Sstevel@tonic-gate 		/*
1624*7c478bd9Sstevel@tonic-gate 		 * Drop the consumer count now that we're done with this
1625*7c478bd9Sstevel@tonic-gate 		 * provider. If there are no other consumers retire it now.
1626*7c478bd9Sstevel@tonic-gate 		 */
1627*7c478bd9Sstevel@tonic-gate 		if (--provider->ftp_ccount == 0)
1628*7c478bd9Sstevel@tonic-gate 			fasttrap_provider_retire(provider);
1629*7c478bd9Sstevel@tonic-gate 		else
1630*7c478bd9Sstevel@tonic-gate 			mutex_exit(&provider->ftp_mtx);
1631*7c478bd9Sstevel@tonic-gate 	}
1632*7c478bd9Sstevel@tonic-gate }
1633*7c478bd9Sstevel@tonic-gate 
1634*7c478bd9Sstevel@tonic-gate static dtrace_mops_t fasttrap_mops = {
1635*7c478bd9Sstevel@tonic-gate 	fasttrap_meta_create_probe,
1636*7c478bd9Sstevel@tonic-gate 	fasttrap_meta_provide,
1637*7c478bd9Sstevel@tonic-gate 	fasttrap_meta_remove
1638*7c478bd9Sstevel@tonic-gate };
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1641*7c478bd9Sstevel@tonic-gate static int
1642*7c478bd9Sstevel@tonic-gate fasttrap_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1643*7c478bd9Sstevel@tonic-gate {
1644*7c478bd9Sstevel@tonic-gate 	return (0);
1645*7c478bd9Sstevel@tonic-gate }
1646*7c478bd9Sstevel@tonic-gate 
1647*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1648*7c478bd9Sstevel@tonic-gate static int
1649*7c478bd9Sstevel@tonic-gate fasttrap_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv)
1650*7c478bd9Sstevel@tonic-gate {
1651*7c478bd9Sstevel@tonic-gate 	if (!dtrace_attached())
1652*7c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate 	if (cmd == FASTTRAPIOC_MAKEPROBE) {
1655*7c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *uprobe = (void *)arg;
1656*7c478bd9Sstevel@tonic-gate 		fasttrap_probe_spec_t *probe;
1657*7c478bd9Sstevel@tonic-gate 		uint64_t noffs;
1658*7c478bd9Sstevel@tonic-gate 		size_t size;
1659*7c478bd9Sstevel@tonic-gate 		int ret;
1660*7c478bd9Sstevel@tonic-gate 		char *c;
1661*7c478bd9Sstevel@tonic-gate 
1662*7c478bd9Sstevel@tonic-gate 		if (copyin(&uprobe->ftps_noffs, &noffs,
1663*7c478bd9Sstevel@tonic-gate 		    sizeof (uprobe->ftps_noffs)))
1664*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate 		/*
1667*7c478bd9Sstevel@tonic-gate 		 * Probes must have at least one tracepoint.
1668*7c478bd9Sstevel@tonic-gate 		 */
1669*7c478bd9Sstevel@tonic-gate 		if (noffs == 0)
1670*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
1671*7c478bd9Sstevel@tonic-gate 
1672*7c478bd9Sstevel@tonic-gate 		size = sizeof (fasttrap_probe_spec_t) +
1673*7c478bd9Sstevel@tonic-gate 		    sizeof (probe->ftps_offs[0]) * (noffs - 1);
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate 		if (size > 1024 * 1024)
1676*7c478bd9Sstevel@tonic-gate 			return (ENOMEM);
1677*7c478bd9Sstevel@tonic-gate 
1678*7c478bd9Sstevel@tonic-gate 		probe = kmem_alloc(size, KM_SLEEP);
1679*7c478bd9Sstevel@tonic-gate 
1680*7c478bd9Sstevel@tonic-gate 		if (copyin(uprobe, probe, size) != 0) {
1681*7c478bd9Sstevel@tonic-gate 			kmem_free(probe, size);
1682*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
1683*7c478bd9Sstevel@tonic-gate 		}
1684*7c478bd9Sstevel@tonic-gate 
1685*7c478bd9Sstevel@tonic-gate 		/*
1686*7c478bd9Sstevel@tonic-gate 		 * Verify that the function and module strings contain no
1687*7c478bd9Sstevel@tonic-gate 		 * funny characters.
1688*7c478bd9Sstevel@tonic-gate 		 */
1689*7c478bd9Sstevel@tonic-gate 		for (c = &probe->ftps_func[0]; *c != '\0'; c++) {
1690*7c478bd9Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
1691*7c478bd9Sstevel@tonic-gate 				ret = EINVAL;
1692*7c478bd9Sstevel@tonic-gate 				goto err;
1693*7c478bd9Sstevel@tonic-gate 			}
1694*7c478bd9Sstevel@tonic-gate 		}
1695*7c478bd9Sstevel@tonic-gate 
1696*7c478bd9Sstevel@tonic-gate 		for (c = &probe->ftps_mod[0]; *c != '\0'; c++) {
1697*7c478bd9Sstevel@tonic-gate 			if (*c < 0x20 || 0x7f <= *c) {
1698*7c478bd9Sstevel@tonic-gate 				ret = EINVAL;
1699*7c478bd9Sstevel@tonic-gate 				goto err;
1700*7c478bd9Sstevel@tonic-gate 			}
1701*7c478bd9Sstevel@tonic-gate 		}
1702*7c478bd9Sstevel@tonic-gate 
1703*7c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
1704*7c478bd9Sstevel@tonic-gate 			proc_t *p;
1705*7c478bd9Sstevel@tonic-gate 			pid_t pid = probe->ftps_pid;
1706*7c478bd9Sstevel@tonic-gate 
1707*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
1708*7c478bd9Sstevel@tonic-gate 			/*
1709*7c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
1710*7c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
1711*7c478bd9Sstevel@tonic-gate 			 */
1712*7c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
1713*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
1714*7c478bd9Sstevel@tonic-gate 				return (ESRCH);
1715*7c478bd9Sstevel@tonic-gate 			}
1716*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1717*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1718*7c478bd9Sstevel@tonic-gate 
1719*7c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
1720*7c478bd9Sstevel@tonic-gate 			    VREAD | VWRITE)) != 0) {
1721*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1722*7c478bd9Sstevel@tonic-gate 				return (ret);
1723*7c478bd9Sstevel@tonic-gate 			}
1724*7c478bd9Sstevel@tonic-gate 
1725*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1726*7c478bd9Sstevel@tonic-gate 		}
1727*7c478bd9Sstevel@tonic-gate 
1728*7c478bd9Sstevel@tonic-gate 		ret = fasttrap_add_probe(probe);
1729*7c478bd9Sstevel@tonic-gate err:
1730*7c478bd9Sstevel@tonic-gate 		kmem_free(probe, size);
1731*7c478bd9Sstevel@tonic-gate 
1732*7c478bd9Sstevel@tonic-gate 		return (ret);
1733*7c478bd9Sstevel@tonic-gate 
1734*7c478bd9Sstevel@tonic-gate 	} else if (cmd == FASTTRAPIOC_GETINSTR) {
1735*7c478bd9Sstevel@tonic-gate 		fasttrap_instr_query_t instr;
1736*7c478bd9Sstevel@tonic-gate 		fasttrap_tracepoint_t *tp;
1737*7c478bd9Sstevel@tonic-gate 		uint_t index;
1738*7c478bd9Sstevel@tonic-gate 		int ret;
1739*7c478bd9Sstevel@tonic-gate 
1740*7c478bd9Sstevel@tonic-gate 		if (copyin((void *)arg, &instr, sizeof (instr)) != 0)
1741*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
1742*7c478bd9Sstevel@tonic-gate 
1743*7c478bd9Sstevel@tonic-gate 		if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) {
1744*7c478bd9Sstevel@tonic-gate 			proc_t *p;
1745*7c478bd9Sstevel@tonic-gate 			pid_t pid = instr.ftiq_pid;
1746*7c478bd9Sstevel@tonic-gate 
1747*7c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
1748*7c478bd9Sstevel@tonic-gate 			/*
1749*7c478bd9Sstevel@tonic-gate 			 * Report an error if the process doesn't exist
1750*7c478bd9Sstevel@tonic-gate 			 * or is actively being birthed.
1751*7c478bd9Sstevel@tonic-gate 			 */
1752*7c478bd9Sstevel@tonic-gate 			if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
1753*7c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
1754*7c478bd9Sstevel@tonic-gate 				return (ESRCH);
1755*7c478bd9Sstevel@tonic-gate 			}
1756*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1757*7c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
1758*7c478bd9Sstevel@tonic-gate 
1759*7c478bd9Sstevel@tonic-gate 			if ((ret = priv_proc_cred_perm(cr, p, NULL,
1760*7c478bd9Sstevel@tonic-gate 			    VREAD)) != 0) {
1761*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1762*7c478bd9Sstevel@tonic-gate 				return (ret);
1763*7c478bd9Sstevel@tonic-gate 			}
1764*7c478bd9Sstevel@tonic-gate 
1765*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1766*7c478bd9Sstevel@tonic-gate 		}
1767*7c478bd9Sstevel@tonic-gate 
1768*7c478bd9Sstevel@tonic-gate 		index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc);
1769*7c478bd9Sstevel@tonic-gate 
1770*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx);
1771*7c478bd9Sstevel@tonic-gate 		tp = fasttrap_tpoints.fth_table[index].ftb_data;
1772*7c478bd9Sstevel@tonic-gate 		while (tp != NULL) {
1773*7c478bd9Sstevel@tonic-gate 			if (instr.ftiq_pid == tp->ftt_pid &&
1774*7c478bd9Sstevel@tonic-gate 			    instr.ftiq_pc == tp->ftt_pc &&
1775*7c478bd9Sstevel@tonic-gate 			    !tp->ftt_prov->ftp_defunct)
1776*7c478bd9Sstevel@tonic-gate 				break;
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate 			tp = tp->ftt_next;
1779*7c478bd9Sstevel@tonic-gate 		}
1780*7c478bd9Sstevel@tonic-gate 
1781*7c478bd9Sstevel@tonic-gate 		if (tp == NULL) {
1782*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
1783*7c478bd9Sstevel@tonic-gate 			return (ENOENT);
1784*7c478bd9Sstevel@tonic-gate 		}
1785*7c478bd9Sstevel@tonic-gate 
1786*7c478bd9Sstevel@tonic-gate 		bcopy(&tp->ftt_instr, &instr.ftiq_instr,
1787*7c478bd9Sstevel@tonic-gate 		    sizeof (instr.ftiq_instr));
1788*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx);
1789*7c478bd9Sstevel@tonic-gate 
1790*7c478bd9Sstevel@tonic-gate 		if (copyout(&instr, (void *)arg, sizeof (instr)) != 0)
1791*7c478bd9Sstevel@tonic-gate 			return (EFAULT);
1792*7c478bd9Sstevel@tonic-gate 
1793*7c478bd9Sstevel@tonic-gate 		return (0);
1794*7c478bd9Sstevel@tonic-gate 	}
1795*7c478bd9Sstevel@tonic-gate 
1796*7c478bd9Sstevel@tonic-gate 	return (EINVAL);
1797*7c478bd9Sstevel@tonic-gate }
1798*7c478bd9Sstevel@tonic-gate 
1799*7c478bd9Sstevel@tonic-gate static struct cb_ops fasttrap_cb_ops = {
1800*7c478bd9Sstevel@tonic-gate 	fasttrap_open,		/* open */
1801*7c478bd9Sstevel@tonic-gate 	nodev,			/* close */
1802*7c478bd9Sstevel@tonic-gate 	nulldev,		/* strategy */
1803*7c478bd9Sstevel@tonic-gate 	nulldev,		/* print */
1804*7c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
1805*7c478bd9Sstevel@tonic-gate 	nodev,			/* read */
1806*7c478bd9Sstevel@tonic-gate 	nodev,			/* write */
1807*7c478bd9Sstevel@tonic-gate 	fasttrap_ioctl,		/* ioctl */
1808*7c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
1809*7c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
1810*7c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
1811*7c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
1812*7c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
1813*7c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
1814*7c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
1815*7c478bd9Sstevel@tonic-gate };
1816*7c478bd9Sstevel@tonic-gate 
1817*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1818*7c478bd9Sstevel@tonic-gate static int
1819*7c478bd9Sstevel@tonic-gate fasttrap_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1820*7c478bd9Sstevel@tonic-gate {
1821*7c478bd9Sstevel@tonic-gate 	int error;
1822*7c478bd9Sstevel@tonic-gate 
1823*7c478bd9Sstevel@tonic-gate 	switch (infocmd) {
1824*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1825*7c478bd9Sstevel@tonic-gate 		*result = (void *)fasttrap_devi;
1826*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1827*7c478bd9Sstevel@tonic-gate 		break;
1828*7c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1829*7c478bd9Sstevel@tonic-gate 		*result = (void *)0;
1830*7c478bd9Sstevel@tonic-gate 		error = DDI_SUCCESS;
1831*7c478bd9Sstevel@tonic-gate 		break;
1832*7c478bd9Sstevel@tonic-gate 	default:
1833*7c478bd9Sstevel@tonic-gate 		error = DDI_FAILURE;
1834*7c478bd9Sstevel@tonic-gate 	}
1835*7c478bd9Sstevel@tonic-gate 	return (error);
1836*7c478bd9Sstevel@tonic-gate }
1837*7c478bd9Sstevel@tonic-gate 
1838*7c478bd9Sstevel@tonic-gate static int
1839*7c478bd9Sstevel@tonic-gate fasttrap_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
1840*7c478bd9Sstevel@tonic-gate {
1841*7c478bd9Sstevel@tonic-gate 	ulong_t nent;
1842*7c478bd9Sstevel@tonic-gate 
1843*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
1844*7c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
1845*7c478bd9Sstevel@tonic-gate 		break;
1846*7c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
1847*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1848*7c478bd9Sstevel@tonic-gate 	default:
1849*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1850*7c478bd9Sstevel@tonic-gate 	}
1851*7c478bd9Sstevel@tonic-gate 
1852*7c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "fasttrap", S_IFCHR, 0,
1853*7c478bd9Sstevel@tonic-gate 	    DDI_PSEUDO, NULL) == DDI_FAILURE ||
1854*7c478bd9Sstevel@tonic-gate 	    dtrace_register("fasttrap", &fasttrap_attr, DTRACE_PRIV_USER, 0,
1855*7c478bd9Sstevel@tonic-gate 	    &fasttrap_pops, NULL, &fasttrap_id) != 0) {
1856*7c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
1857*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1858*7c478bd9Sstevel@tonic-gate 	}
1859*7c478bd9Sstevel@tonic-gate 
1860*7c478bd9Sstevel@tonic-gate 	ddi_report_dev(devi);
1861*7c478bd9Sstevel@tonic-gate 	fasttrap_devi = devi;
1862*7c478bd9Sstevel@tonic-gate 
1863*7c478bd9Sstevel@tonic-gate 	/*
1864*7c478bd9Sstevel@tonic-gate 	 * Install our hooks into fork(2), exec(2), and exit(2).
1865*7c478bd9Sstevel@tonic-gate 	 */
1866*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = &fasttrap_fork;
1867*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = &fasttrap_exec_exit;
1868*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = &fasttrap_exec_exit;
1869*7c478bd9Sstevel@tonic-gate 
1870*7c478bd9Sstevel@tonic-gate 	fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1871*7c478bd9Sstevel@tonic-gate 	    "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT);
1872*7c478bd9Sstevel@tonic-gate 	fasttrap_total = 0;
1873*7c478bd9Sstevel@tonic-gate 
1874*7c478bd9Sstevel@tonic-gate 	/*
1875*7c478bd9Sstevel@tonic-gate 	 * Conjure up the tracepoints hashtable...
1876*7c478bd9Sstevel@tonic-gate 	 */
1877*7c478bd9Sstevel@tonic-gate 	nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1878*7c478bd9Sstevel@tonic-gate 	    "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE);
1879*7c478bd9Sstevel@tonic-gate 
1880*7c478bd9Sstevel@tonic-gate 	if (nent <= 0 || nent > 0x1000000)
1881*7c478bd9Sstevel@tonic-gate 		nent = FASTTRAP_TPOINTS_DEFAULT_SIZE;
1882*7c478bd9Sstevel@tonic-gate 
1883*7c478bd9Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
1884*7c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = nent;
1885*7c478bd9Sstevel@tonic-gate 	else
1886*7c478bd9Sstevel@tonic-gate 		fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent);
1887*7c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_tpoints.fth_nent > 0);
1888*7c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1;
1889*7c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent *
1890*7c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
1891*7c478bd9Sstevel@tonic-gate 
1892*7c478bd9Sstevel@tonic-gate 	/*
1893*7c478bd9Sstevel@tonic-gate 	 * ... and the providers hash table.
1894*7c478bd9Sstevel@tonic-gate 	 */
1895*7c478bd9Sstevel@tonic-gate 	nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE;
1896*7c478bd9Sstevel@tonic-gate 	if ((nent & (nent - 1)) == 0)
1897*7c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = nent;
1898*7c478bd9Sstevel@tonic-gate 	else
1899*7c478bd9Sstevel@tonic-gate 		fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent);
1900*7c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_provs.fth_nent > 0);
1901*7c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1;
1902*7c478bd9Sstevel@tonic-gate 
1903*7c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent *
1904*7c478bd9Sstevel@tonic-gate 	    sizeof (fasttrap_bucket_t), KM_SLEEP);
1905*7c478bd9Sstevel@tonic-gate 
1906*7c478bd9Sstevel@tonic-gate 	(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
1907*7c478bd9Sstevel@tonic-gate 	    &fasttrap_meta_id);
1908*7c478bd9Sstevel@tonic-gate 
1909*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
1910*7c478bd9Sstevel@tonic-gate }
1911*7c478bd9Sstevel@tonic-gate 
1912*7c478bd9Sstevel@tonic-gate static int
1913*7c478bd9Sstevel@tonic-gate fasttrap_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
1914*7c478bd9Sstevel@tonic-gate {
1915*7c478bd9Sstevel@tonic-gate 	int i, fail = 0;
1916*7c478bd9Sstevel@tonic-gate 	timeout_id_t tmp;
1917*7c478bd9Sstevel@tonic-gate 
1918*7c478bd9Sstevel@tonic-gate 	switch (cmd) {
1919*7c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
1920*7c478bd9Sstevel@tonic-gate 		break;
1921*7c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
1922*7c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
1923*7c478bd9Sstevel@tonic-gate 	default:
1924*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1925*7c478bd9Sstevel@tonic-gate 	}
1926*7c478bd9Sstevel@tonic-gate 
1927*7c478bd9Sstevel@tonic-gate 	/*
1928*7c478bd9Sstevel@tonic-gate 	 * Unregister the meta-provider to make sure no new fasttrap-
1929*7c478bd9Sstevel@tonic-gate 	 * managed providers come along while we're trying to close up
1930*7c478bd9Sstevel@tonic-gate 	 * shop. If we fail to detach, we'll need to re-register as a
1931*7c478bd9Sstevel@tonic-gate 	 * meta-provider. We can fail to unregister as a meta-provider
1932*7c478bd9Sstevel@tonic-gate 	 * if providers we manage still exist.
1933*7c478bd9Sstevel@tonic-gate 	 */
1934*7c478bd9Sstevel@tonic-gate 	if (fasttrap_meta_id != DTRACE_METAPROVNONE &&
1935*7c478bd9Sstevel@tonic-gate 	    dtrace_meta_unregister(fasttrap_meta_id) != 0)
1936*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
1937*7c478bd9Sstevel@tonic-gate 
1938*7c478bd9Sstevel@tonic-gate 	/*
1939*7c478bd9Sstevel@tonic-gate 	 * Prevent any new timeouts from running by setting fasttrap_timeout
1940*7c478bd9Sstevel@tonic-gate 	 * to a non-zero value, and wait for the current timeout to complete.
1941*7c478bd9Sstevel@tonic-gate 	 */
1942*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_cleanup_mtx);
1943*7c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
1944*7c478bd9Sstevel@tonic-gate 
1945*7c478bd9Sstevel@tonic-gate 	while (fasttrap_timeout != (timeout_id_t)1) {
1946*7c478bd9Sstevel@tonic-gate 		tmp = fasttrap_timeout;
1947*7c478bd9Sstevel@tonic-gate 		fasttrap_timeout = (timeout_id_t)1;
1948*7c478bd9Sstevel@tonic-gate 
1949*7c478bd9Sstevel@tonic-gate 		if (tmp != 0) {
1950*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fasttrap_cleanup_mtx);
1951*7c478bd9Sstevel@tonic-gate 			(void) untimeout(tmp);
1952*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fasttrap_cleanup_mtx);
1953*7c478bd9Sstevel@tonic-gate 		}
1954*7c478bd9Sstevel@tonic-gate 	}
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate 	fasttrap_cleanup_work = 0;
1957*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_cleanup_mtx);
1958*7c478bd9Sstevel@tonic-gate 
1959*7c478bd9Sstevel@tonic-gate 	/*
1960*7c478bd9Sstevel@tonic-gate 	 * Iterate over all of our providers. If there's still a process
1961*7c478bd9Sstevel@tonic-gate 	 * that corresponds to that pid, fail to detach.
1962*7c478bd9Sstevel@tonic-gate 	 */
1963*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < fasttrap_provs.fth_nent; i++) {
1964*7c478bd9Sstevel@tonic-gate 		fasttrap_provider_t **fpp, *fp;
1965*7c478bd9Sstevel@tonic-gate 		fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i];
1966*7c478bd9Sstevel@tonic-gate 
1967*7c478bd9Sstevel@tonic-gate 		mutex_enter(&bucket->ftb_mtx);
1968*7c478bd9Sstevel@tonic-gate 		fpp = (fasttrap_provider_t **)&bucket->ftb_data;
1969*7c478bd9Sstevel@tonic-gate 		while ((fp = *fpp) != NULL) {
1970*7c478bd9Sstevel@tonic-gate 			/*
1971*7c478bd9Sstevel@tonic-gate 			 * Acquire and release the lock as a simple way of
1972*7c478bd9Sstevel@tonic-gate 			 * waiting for any other consumer to finish with
1973*7c478bd9Sstevel@tonic-gate 			 * this provider. A thread must first acquire the
1974*7c478bd9Sstevel@tonic-gate 			 * bucket lock so there's no chance of another thread
1975*7c478bd9Sstevel@tonic-gate 			 * blocking on the providers lock.
1976*7c478bd9Sstevel@tonic-gate 			 */
1977*7c478bd9Sstevel@tonic-gate 			mutex_enter(&fp->ftp_mtx);
1978*7c478bd9Sstevel@tonic-gate 			mutex_exit(&fp->ftp_mtx);
1979*7c478bd9Sstevel@tonic-gate 
1980*7c478bd9Sstevel@tonic-gate 			if (dtrace_unregister(fp->ftp_provid) != 0) {
1981*7c478bd9Sstevel@tonic-gate 				fail = 1;
1982*7c478bd9Sstevel@tonic-gate 				fpp = &fp->ftp_next;
1983*7c478bd9Sstevel@tonic-gate 			} else {
1984*7c478bd9Sstevel@tonic-gate 				*fpp = fp->ftp_next;
1985*7c478bd9Sstevel@tonic-gate 				fasttrap_provider_free(fp);
1986*7c478bd9Sstevel@tonic-gate 			}
1987*7c478bd9Sstevel@tonic-gate 		}
1988*7c478bd9Sstevel@tonic-gate 
1989*7c478bd9Sstevel@tonic-gate 		mutex_exit(&bucket->ftb_mtx);
1990*7c478bd9Sstevel@tonic-gate 	}
1991*7c478bd9Sstevel@tonic-gate 
1992*7c478bd9Sstevel@tonic-gate 	if (fail || dtrace_unregister(fasttrap_id) != 0) {
1993*7c478bd9Sstevel@tonic-gate 		uint_t work;
1994*7c478bd9Sstevel@tonic-gate 		/*
1995*7c478bd9Sstevel@tonic-gate 		 * If we're failing to detach, we need to unblock timeouts
1996*7c478bd9Sstevel@tonic-gate 		 * and start a new timeout if any work has accumulated while
1997*7c478bd9Sstevel@tonic-gate 		 * we've been unsuccessfully trying to detach.
1998*7c478bd9Sstevel@tonic-gate 		 */
1999*7c478bd9Sstevel@tonic-gate 		mutex_enter(&fasttrap_cleanup_mtx);
2000*7c478bd9Sstevel@tonic-gate 		fasttrap_timeout = 0;
2001*7c478bd9Sstevel@tonic-gate 		work = fasttrap_cleanup_work;
2002*7c478bd9Sstevel@tonic-gate 		mutex_exit(&fasttrap_cleanup_mtx);
2003*7c478bd9Sstevel@tonic-gate 
2004*7c478bd9Sstevel@tonic-gate 		if (work)
2005*7c478bd9Sstevel@tonic-gate 			fasttrap_pid_cleanup();
2006*7c478bd9Sstevel@tonic-gate 
2007*7c478bd9Sstevel@tonic-gate 		(void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL,
2008*7c478bd9Sstevel@tonic-gate 		    &fasttrap_meta_id);
2009*7c478bd9Sstevel@tonic-gate 
2010*7c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2011*7c478bd9Sstevel@tonic-gate 	}
2012*7c478bd9Sstevel@tonic-gate 
2013*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
2014*7c478bd9Sstevel@tonic-gate 	mutex_enter(&fasttrap_count_mtx);
2015*7c478bd9Sstevel@tonic-gate 	ASSERT(fasttrap_count == 0);
2016*7c478bd9Sstevel@tonic-gate 	mutex_exit(&fasttrap_count_mtx);
2017*7c478bd9Sstevel@tonic-gate #endif
2018*7c478bd9Sstevel@tonic-gate 
2019*7c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_tpoints.fth_table,
2020*7c478bd9Sstevel@tonic-gate 	    fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t));
2021*7c478bd9Sstevel@tonic-gate 	fasttrap_tpoints.fth_nent = 0;
2022*7c478bd9Sstevel@tonic-gate 
2023*7c478bd9Sstevel@tonic-gate 	kmem_free(fasttrap_provs.fth_table,
2024*7c478bd9Sstevel@tonic-gate 	    fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t));
2025*7c478bd9Sstevel@tonic-gate 	fasttrap_provs.fth_nent = 0;
2026*7c478bd9Sstevel@tonic-gate 
2027*7c478bd9Sstevel@tonic-gate 	/*
2028*7c478bd9Sstevel@tonic-gate 	 * We know there are no tracepoints in any process anywhere in
2029*7c478bd9Sstevel@tonic-gate 	 * the system so there is no process which has its p_dtrace_count
2030*7c478bd9Sstevel@tonic-gate 	 * greater than zero, therefore we know that no thread can actively
2031*7c478bd9Sstevel@tonic-gate 	 * be executing code in fasttrap_fork(). Similarly for p_dtrace_probes
2032*7c478bd9Sstevel@tonic-gate 	 * and fasttrap_exec() and fasttrap_exit().
2033*7c478bd9Sstevel@tonic-gate 	 */
2034*7c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_fork_ptr == &fasttrap_fork);
2035*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_fork_ptr = NULL;
2036*7c478bd9Sstevel@tonic-gate 
2037*7c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exec_ptr == &fasttrap_exec_exit);
2038*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exec_ptr = NULL;
2039*7c478bd9Sstevel@tonic-gate 
2040*7c478bd9Sstevel@tonic-gate 	ASSERT(dtrace_fasttrap_exit_ptr == &fasttrap_exec_exit);
2041*7c478bd9Sstevel@tonic-gate 	dtrace_fasttrap_exit_ptr = NULL;
2042*7c478bd9Sstevel@tonic-gate 
2043*7c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
2044*7c478bd9Sstevel@tonic-gate 
2045*7c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2046*7c478bd9Sstevel@tonic-gate }
2047*7c478bd9Sstevel@tonic-gate 
2048*7c478bd9Sstevel@tonic-gate static struct dev_ops fasttrap_ops = {
2049*7c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
2050*7c478bd9Sstevel@tonic-gate 	0,			/* refcnt */
2051*7c478bd9Sstevel@tonic-gate 	fasttrap_info,		/* get_dev_info */
2052*7c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
2053*7c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
2054*7c478bd9Sstevel@tonic-gate 	fasttrap_attach,	/* attach */
2055*7c478bd9Sstevel@tonic-gate 	fasttrap_detach,	/* detach */
2056*7c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
2057*7c478bd9Sstevel@tonic-gate 	&fasttrap_cb_ops,	/* driver operations */
2058*7c478bd9Sstevel@tonic-gate 	NULL,			/* bus operations */
2059*7c478bd9Sstevel@tonic-gate 	nodev			/* dev power */
2060*7c478bd9Sstevel@tonic-gate };
2061*7c478bd9Sstevel@tonic-gate 
2062*7c478bd9Sstevel@tonic-gate /*
2063*7c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
2064*7c478bd9Sstevel@tonic-gate  */
2065*7c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
2066*7c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* module type (this is a pseudo driver) */
2067*7c478bd9Sstevel@tonic-gate 	"Fasttrap Tracing",	/* name of module */
2068*7c478bd9Sstevel@tonic-gate 	&fasttrap_ops,		/* driver ops */
2069*7c478bd9Sstevel@tonic-gate };
2070*7c478bd9Sstevel@tonic-gate 
2071*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
2072*7c478bd9Sstevel@tonic-gate 	MODREV_1,
2073*7c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
2074*7c478bd9Sstevel@tonic-gate 	NULL
2075*7c478bd9Sstevel@tonic-gate };
2076*7c478bd9Sstevel@tonic-gate 
2077*7c478bd9Sstevel@tonic-gate int
2078*7c478bd9Sstevel@tonic-gate _init(void)
2079*7c478bd9Sstevel@tonic-gate {
2080*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
2081*7c478bd9Sstevel@tonic-gate }
2082*7c478bd9Sstevel@tonic-gate 
2083*7c478bd9Sstevel@tonic-gate int
2084*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2085*7c478bd9Sstevel@tonic-gate {
2086*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2087*7c478bd9Sstevel@tonic-gate }
2088*7c478bd9Sstevel@tonic-gate 
2089*7c478bd9Sstevel@tonic-gate int
2090*7c478bd9Sstevel@tonic-gate _fini(void)
2091*7c478bd9Sstevel@tonic-gate {
2092*7c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
2093*7c478bd9Sstevel@tonic-gate }
2094