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