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