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