1179193Sjb/* 2179193Sjb * CDDL HEADER START 3179193Sjb * 4179193Sjb * The contents of this file are subject to the terms of the 5179193Sjb * Common Development and Distribution License (the "License"). 6179193Sjb * You may not use this file except in compliance with the License. 7179193Sjb * 8179193Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9179193Sjb * or http://www.opensolaris.org/os/licensing. 10179193Sjb * See the License for the specific language governing permissions 11179193Sjb * and limitations under the License. 12179193Sjb * 13179193Sjb * When distributing Covered Code, include this CDDL HEADER in each 14179193Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15179193Sjb * If applicable, add the following below this CDDL HEADER, with the 16179193Sjb * fields enclosed by brackets "[]" replaced with your own identifying 17179193Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 18179193Sjb * 19179193Sjb * CDDL HEADER END 20211738Srpaulo * 21211738Srpaulo * Portions Copyright 2010 The FreeBSD Foundation 22211738Srpaulo * 23211738Srpaulo * $FreeBSD$ 24179193Sjb */ 25179193Sjb 26179193Sjb/* 27179198Sjb * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 28179193Sjb * Use is subject to license terms. 29179193Sjb */ 30179193Sjb 31268734Spfg/* 32268734Spfg * Copyright (c) 2013, Joyent, Inc. All rights reserved. 33268734Spfg */ 34179193Sjb 35179193Sjb#include <sys/atomic.h> 36179193Sjb#include <sys/errno.h> 37179193Sjb#include <sys/stat.h> 38179193Sjb#include <sys/modctl.h> 39179193Sjb#include <sys/conf.h> 40179193Sjb#include <sys/systm.h> 41211738Srpaulo#if defined(sun) 42179193Sjb#include <sys/ddi.h> 43211738Srpaulo#endif 44179193Sjb#include <sys/sunddi.h> 45179193Sjb#include <sys/cpuvar.h> 46179193Sjb#include <sys/kmem.h> 47211738Srpaulo#if defined(sun) 48179193Sjb#include <sys/strsubr.h> 49211738Srpaulo#endif 50179193Sjb#include <sys/fasttrap.h> 51179193Sjb#include <sys/fasttrap_impl.h> 52179193Sjb#include <sys/fasttrap_isa.h> 53179193Sjb#include <sys/dtrace.h> 54179193Sjb#include <sys/dtrace_impl.h> 55179193Sjb#include <sys/sysmacros.h> 56179193Sjb#include <sys/proc.h> 57179193Sjb#include <sys/policy.h> 58211738Srpaulo#if defined(sun) 59179193Sjb#include <util/qsort.h> 60211738Srpaulo#endif 61211738Srpaulo#include <sys/mutex.h> 62211738Srpaulo#include <sys/kernel.h> 63211738Srpaulo#if !defined(sun) 64269342Smarkj#include <sys/dtrace_bsd.h> 65269342Smarkj#include <sys/eventhandler.h> 66211738Srpaulo#include <sys/user.h> 67269342Smarkj#include <vm/vm.h> 68269342Smarkj#include <vm/pmap.h> 69269342Smarkj#include <vm/vm_map.h> 70269342Smarkj#include <vm/vm_param.h> 71268734Spfg#include <sys/u8_textprep.h> 72211738Srpaulo#include <cddl/dev/dtrace/dtrace_cddl.h> 73211738Srpaulo#endif 74179193Sjb 75179193Sjb/* 76179193Sjb * User-Land Trap-Based Tracing 77179193Sjb * ---------------------------- 78179193Sjb * 79179193Sjb * The fasttrap provider allows DTrace consumers to instrument any user-level 80179193Sjb * instruction to gather data; this includes probes with semantic 81179193Sjb * signifigance like entry and return as well as simple offsets into the 82179193Sjb * function. While the specific techniques used are very ISA specific, the 83179193Sjb * methodology is generalizable to any architecture. 84179193Sjb * 85179193Sjb * 86179193Sjb * The General Methodology 87179193Sjb * ----------------------- 88179193Sjb * 89179193Sjb * With the primary goal of tracing every user-land instruction and the 90179193Sjb * limitation that we can't trust user space so don't want to rely on much 91179193Sjb * information there, we begin by replacing the instructions we want to trace 92179193Sjb * with trap instructions. Each instruction we overwrite is saved into a hash 93179193Sjb * table keyed by process ID and pc address. When we enter the kernel due to 94179193Sjb * this trap instruction, we need the effects of the replaced instruction to 95179193Sjb * appear to have occurred before we proceed with the user thread's 96179193Sjb * execution. 97179193Sjb * 98179193Sjb * Each user level thread is represented by a ulwp_t structure which is 99179193Sjb * always easily accessible through a register. The most basic way to produce 100179193Sjb * the effects of the instruction we replaced is to copy that instruction out 101179193Sjb * to a bit of scratch space reserved in the user thread's ulwp_t structure 102179193Sjb * (a sort of kernel-private thread local storage), set the PC to that 103179193Sjb * scratch space and single step. When we reenter the kernel after single 104179193Sjb * stepping the instruction we must then adjust the PC to point to what would 105179193Sjb * normally be the next instruction. Of course, special care must be taken 106179193Sjb * for branches and jumps, but these represent such a small fraction of any 107179193Sjb * instruction set that writing the code to emulate these in the kernel is 108179193Sjb * not too difficult. 109179193Sjb * 110179193Sjb * Return probes may require several tracepoints to trace every return site, 111179193Sjb * and, conversely, each tracepoint may activate several probes (the entry 112179193Sjb * and offset 0 probes, for example). To solve this muliplexing problem, 113179193Sjb * tracepoints contain lists of probes to activate and probes contain lists 114179193Sjb * of tracepoints to enable. If a probe is activated, it adds its ID to 115179193Sjb * existing tracepoints or creates new ones as necessary. 116179193Sjb * 117179193Sjb * Most probes are activated _before_ the instruction is executed, but return 118179193Sjb * probes are activated _after_ the effects of the last instruction of the 119179193Sjb * function are visible. Return probes must be fired _after_ we have 120179193Sjb * single-stepped the instruction whereas all other probes are fired 121179193Sjb * beforehand. 122179193Sjb * 123179193Sjb * 124179193Sjb * Lock Ordering 125179193Sjb * ------------- 126179193Sjb * 127179193Sjb * The lock ordering below -- both internally and with respect to the DTrace 128179193Sjb * framework -- is a little tricky and bears some explanation. Each provider 129179193Sjb * has a lock (ftp_mtx) that protects its members including reference counts 130179193Sjb * for enabled probes (ftp_rcount), consumers actively creating probes 131179193Sjb * (ftp_ccount) and USDT consumers (ftp_mcount); all three prevent a provider 132179193Sjb * from being freed. A provider is looked up by taking the bucket lock for the 133179193Sjb * provider hash table, and is returned with its lock held. The provider lock 134179193Sjb * may be taken in functions invoked by the DTrace framework, but may not be 135179193Sjb * held while calling functions in the DTrace framework. 136179193Sjb * 137179193Sjb * To ensure consistency over multiple calls to the DTrace framework, the 138179193Sjb * creation lock (ftp_cmtx) should be held. Naturally, the creation lock may 139179193Sjb * not be taken when holding the provider lock as that would create a cyclic 140179193Sjb * lock ordering. In situations where one would naturally take the provider 141179193Sjb * lock and then the creation lock, we instead up a reference count to prevent 142179193Sjb * the provider from disappearing, drop the provider lock, and acquire the 143179193Sjb * creation lock. 144179193Sjb * 145179193Sjb * Briefly: 146179193Sjb * bucket lock before provider lock 147179193Sjb * DTrace before provider lock 148179193Sjb * creation lock before DTrace 149179193Sjb * never hold the provider lock and creation lock simultaneously 150179193Sjb */ 151179193Sjb 152211738Srpaulostatic d_open_t fasttrap_open; 153211738Srpaulostatic d_ioctl_t fasttrap_ioctl; 154211738Srpaulo 155211738Srpaulostatic struct cdevsw fasttrap_cdevsw = { 156211738Srpaulo .d_version = D_VERSION, 157211738Srpaulo .d_open = fasttrap_open, 158211738Srpaulo .d_ioctl = fasttrap_ioctl, 159211738Srpaulo .d_name = "fasttrap", 160211738Srpaulo}; 161211738Srpaulostatic struct cdev *fasttrap_cdev; 162179193Sjbstatic dtrace_meta_provider_id_t fasttrap_meta_id; 163179193Sjb 164250953Smarkjstatic struct proc *fasttrap_cleanup_proc; 165211738Srpaulostatic struct mtx fasttrap_cleanup_mtx; 166250953Smarkjstatic uint_t fasttrap_cleanup_work, fasttrap_cleanup_drain, fasttrap_cleanup_cv; 167179193Sjb 168179193Sjb/* 169179193Sjb * Generation count on modifications to the global tracepoint lookup table. 170179193Sjb */ 171179193Sjbstatic volatile uint64_t fasttrap_mod_gen; 172179193Sjb 173179193Sjb/* 174179193Sjb * When the fasttrap provider is loaded, fasttrap_max is set to either 175179193Sjb * FASTTRAP_MAX_DEFAULT or the value for fasttrap-max-probes in the 176179193Sjb * fasttrap.conf file. Each time a probe is created, fasttrap_total is 177179193Sjb * incremented by the number of tracepoints that may be associated with that 178179193Sjb * probe; fasttrap_total is capped at fasttrap_max. 179179193Sjb */ 180179193Sjb#define FASTTRAP_MAX_DEFAULT 250000 181179193Sjbstatic uint32_t fasttrap_max; 182179193Sjbstatic uint32_t fasttrap_total; 183179193Sjb 184248983Spfg/* 185248983Spfg * Copyright (c) 2011, Joyent, Inc. All rights reserved. 186248983Spfg */ 187179193Sjb 188179193Sjb#define FASTTRAP_TPOINTS_DEFAULT_SIZE 0x4000 189179193Sjb#define FASTTRAP_PROVIDERS_DEFAULT_SIZE 0x100 190179193Sjb#define FASTTRAP_PROCS_DEFAULT_SIZE 0x100 191179193Sjb 192179193Sjb#define FASTTRAP_PID_NAME "pid" 193179193Sjb 194179193Sjbfasttrap_hash_t fasttrap_tpoints; 195179193Sjbstatic fasttrap_hash_t fasttrap_provs; 196179193Sjbstatic fasttrap_hash_t fasttrap_procs; 197179193Sjb 198179193Sjbstatic uint64_t fasttrap_pid_count; /* pid ref count */ 199179193Sjbstatic kmutex_t fasttrap_count_mtx; /* lock on ref count */ 200179193Sjb 201179193Sjb#define FASTTRAP_ENABLE_FAIL 1 202179193Sjb#define FASTTRAP_ENABLE_PARTIAL 2 203179193Sjb 204179193Sjbstatic int fasttrap_tracepoint_enable(proc_t *, fasttrap_probe_t *, uint_t); 205179193Sjbstatic void fasttrap_tracepoint_disable(proc_t *, fasttrap_probe_t *, uint_t); 206179193Sjb 207179193Sjbstatic fasttrap_provider_t *fasttrap_provider_lookup(pid_t, const char *, 208179193Sjb const dtrace_pattr_t *); 209179193Sjbstatic void fasttrap_provider_retire(pid_t, const char *, int); 210179193Sjbstatic void fasttrap_provider_free(fasttrap_provider_t *); 211179193Sjb 212179193Sjbstatic fasttrap_proc_t *fasttrap_proc_lookup(pid_t); 213179193Sjbstatic void fasttrap_proc_release(fasttrap_proc_t *); 214179193Sjb 215269342Smarkj#if !defined(sun) 216269342Smarkjstatic void fasttrap_thread_dtor(void *, struct thread *); 217269342Smarkj#endif 218269342Smarkj 219179193Sjb#define FASTTRAP_PROVS_INDEX(pid, name) \ 220179193Sjb ((fasttrap_hash_str(name) + (pid)) & fasttrap_provs.fth_mask) 221179193Sjb 222179193Sjb#define FASTTRAP_PROCS_INDEX(pid) ((pid) & fasttrap_procs.fth_mask) 223179193Sjb 224211925Srpaulo#if !defined(sun) 225211925Srpaulostatic kmutex_t fasttrap_cpuc_pid_lock[MAXCPU]; 226269342Smarkjstatic eventhandler_tag fasttrap_thread_dtor_tag; 227211925Srpaulo#endif 228211925Srpaulo 229179193Sjbstatic int 230179193Sjbfasttrap_highbit(ulong_t i) 231179193Sjb{ 232179193Sjb int h = 1; 233179193Sjb 234179193Sjb if (i == 0) 235179193Sjb return (0); 236179193Sjb#ifdef _LP64 237179193Sjb if (i & 0xffffffff00000000ul) { 238179193Sjb h += 32; i >>= 32; 239179193Sjb } 240179193Sjb#endif 241179193Sjb if (i & 0xffff0000) { 242179193Sjb h += 16; i >>= 16; 243179193Sjb } 244179193Sjb if (i & 0xff00) { 245179193Sjb h += 8; i >>= 8; 246179193Sjb } 247179193Sjb if (i & 0xf0) { 248179193Sjb h += 4; i >>= 4; 249179193Sjb } 250179193Sjb if (i & 0xc) { 251179193Sjb h += 2; i >>= 2; 252179193Sjb } 253179193Sjb if (i & 0x2) { 254179193Sjb h += 1; 255179193Sjb } 256179193Sjb return (h); 257179193Sjb} 258179193Sjb 259179193Sjbstatic uint_t 260179193Sjbfasttrap_hash_str(const char *p) 261179193Sjb{ 262179193Sjb unsigned int g; 263179193Sjb uint_t hval = 0; 264179193Sjb 265179193Sjb while (*p) { 266179193Sjb hval = (hval << 4) + *p++; 267179193Sjb if ((g = (hval & 0xf0000000)) != 0) 268179193Sjb hval ^= g >> 24; 269179193Sjb hval &= ~g; 270179193Sjb } 271179193Sjb return (hval); 272179193Sjb} 273179193Sjb 274179193Sjbvoid 275179193Sjbfasttrap_sigtrap(proc_t *p, kthread_t *t, uintptr_t pc) 276179193Sjb{ 277211738Srpaulo#if defined(sun) 278179193Sjb sigqueue_t *sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP); 279179193Sjb 280179193Sjb sqp->sq_info.si_signo = SIGTRAP; 281179193Sjb sqp->sq_info.si_code = TRAP_DTRACE; 282179193Sjb sqp->sq_info.si_addr = (caddr_t)pc; 283179193Sjb 284179193Sjb mutex_enter(&p->p_lock); 285179193Sjb sigaddqa(p, t, sqp); 286179193Sjb mutex_exit(&p->p_lock); 287179193Sjb 288179193Sjb if (t != NULL) 289179193Sjb aston(t); 290211738Srpaulo#else 291211738Srpaulo ksiginfo_t *ksi = kmem_zalloc(sizeof (ksiginfo_t), KM_SLEEP); 292211738Srpaulo 293211738Srpaulo ksiginfo_init(ksi); 294211738Srpaulo ksi->ksi_signo = SIGTRAP; 295211738Srpaulo ksi->ksi_code = TRAP_DTRACE; 296211738Srpaulo ksi->ksi_addr = (caddr_t)pc; 297211738Srpaulo PROC_LOCK(p); 298211745Srpaulo (void) tdksignal(t, SIGTRAP, ksi); 299211738Srpaulo PROC_UNLOCK(p); 300211738Srpaulo#endif 301179193Sjb} 302179193Sjb 303269342Smarkj#if !defined(sun) 304179193Sjb/* 305269342Smarkj * Obtain a chunk of scratch space in the address space of the target process. 306269342Smarkj */ 307269342Smarkjfasttrap_scrspace_t * 308269342Smarkjfasttrap_scraddr(struct thread *td, fasttrap_proc_t *fprc) 309269342Smarkj{ 310269342Smarkj fasttrap_scrblock_t *scrblk; 311269342Smarkj fasttrap_scrspace_t *scrspc; 312269342Smarkj struct proc *p; 313269342Smarkj vm_offset_t addr; 314269342Smarkj int error, i; 315269342Smarkj 316269342Smarkj scrspc = NULL; 317269342Smarkj if (td->t_dtrace_sscr != NULL) { 318269342Smarkj /* If the thread already has scratch space, we're done. */ 319269342Smarkj scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr; 320269342Smarkj return (scrspc); 321269342Smarkj } 322269342Smarkj 323269342Smarkj p = td->td_proc; 324269342Smarkj 325269342Smarkj mutex_enter(&fprc->ftpc_mtx); 326269342Smarkj if (LIST_EMPTY(&fprc->ftpc_fscr)) { 327269342Smarkj /* 328269342Smarkj * No scratch space is available, so we'll map a new scratch 329269342Smarkj * space block into the traced process' address space. 330269342Smarkj */ 331269342Smarkj addr = 0; 332269342Smarkj error = vm_map_find(&p->p_vmspace->vm_map, NULL, 0, &addr, 333269342Smarkj FASTTRAP_SCRBLOCK_SIZE, 0, VMFS_ANY_SPACE, VM_PROT_ALL, 334269342Smarkj VM_PROT_ALL, 0); 335269342Smarkj if (error != KERN_SUCCESS) 336269342Smarkj goto done; 337269342Smarkj 338269342Smarkj scrblk = malloc(sizeof(*scrblk), M_SOLARIS, M_WAITOK); 339269342Smarkj scrblk->ftsb_addr = addr; 340269342Smarkj LIST_INSERT_HEAD(&fprc->ftpc_scrblks, scrblk, ftsb_next); 341269342Smarkj 342269342Smarkj /* 343269342Smarkj * Carve the block up into chunks and put them on the free list. 344269342Smarkj */ 345269342Smarkj for (i = 0; 346269342Smarkj i < FASTTRAP_SCRBLOCK_SIZE / FASTTRAP_SCRSPACE_SIZE; i++) { 347269342Smarkj scrspc = malloc(sizeof(*scrspc), M_SOLARIS, M_WAITOK); 348269342Smarkj scrspc->ftss_addr = addr + 349269342Smarkj i * FASTTRAP_SCRSPACE_SIZE; 350269342Smarkj LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, 351269342Smarkj ftss_next); 352269342Smarkj } 353269342Smarkj } 354269342Smarkj 355269342Smarkj /* 356269342Smarkj * Take the first scratch chunk off the free list, put it on the 357269342Smarkj * allocated list, and return its address. 358269342Smarkj */ 359269342Smarkj scrspc = LIST_FIRST(&fprc->ftpc_fscr); 360269342Smarkj LIST_REMOVE(scrspc, ftss_next); 361269342Smarkj LIST_INSERT_HEAD(&fprc->ftpc_ascr, scrspc, ftss_next); 362269342Smarkj 363269342Smarkj /* 364269342Smarkj * This scratch space is reserved for use by td until the thread exits. 365269342Smarkj */ 366269342Smarkj td->t_dtrace_sscr = scrspc; 367269342Smarkj 368269342Smarkjdone: 369269342Smarkj mutex_exit(&fprc->ftpc_mtx); 370269342Smarkj 371269342Smarkj return (scrspc); 372269342Smarkj} 373269342Smarkj 374269342Smarkj/* 375269342Smarkj * Return any allocated per-thread scratch space chunks back to the process' 376269342Smarkj * free list. 377269342Smarkj */ 378269342Smarkjstatic void 379269342Smarkjfasttrap_thread_dtor(void *arg __unused, struct thread *td) 380269342Smarkj{ 381269342Smarkj fasttrap_bucket_t *bucket; 382269342Smarkj fasttrap_proc_t *fprc; 383269342Smarkj fasttrap_scrspace_t *scrspc; 384269342Smarkj pid_t pid; 385269342Smarkj 386269342Smarkj if (td->t_dtrace_sscr == NULL) 387269342Smarkj return; 388269342Smarkj 389269342Smarkj pid = td->td_proc->p_pid; 390269342Smarkj bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 391269342Smarkj fprc = NULL; 392269342Smarkj 393269342Smarkj /* Look up the fasttrap process handle for this process. */ 394269342Smarkj mutex_enter(&bucket->ftb_mtx); 395269342Smarkj for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 396269342Smarkj if (fprc->ftpc_pid == pid) { 397269342Smarkj mutex_enter(&fprc->ftpc_mtx); 398269342Smarkj mutex_exit(&bucket->ftb_mtx); 399269342Smarkj break; 400269342Smarkj } 401269342Smarkj } 402269342Smarkj if (fprc == NULL) { 403269342Smarkj mutex_exit(&bucket->ftb_mtx); 404269342Smarkj return; 405269342Smarkj } 406269342Smarkj 407269342Smarkj scrspc = (fasttrap_scrspace_t *)td->t_dtrace_sscr; 408269342Smarkj LIST_REMOVE(scrspc, ftss_next); 409269342Smarkj LIST_INSERT_HEAD(&fprc->ftpc_fscr, scrspc, ftss_next); 410269342Smarkj 411269342Smarkj mutex_exit(&fprc->ftpc_mtx); 412269342Smarkj} 413269342Smarkj#endif 414269342Smarkj 415269342Smarkj/* 416179193Sjb * This function ensures that no threads are actively using the memory 417179193Sjb * associated with probes that were formerly live. 418179193Sjb */ 419179193Sjbstatic void 420179193Sjbfasttrap_mod_barrier(uint64_t gen) 421179193Sjb{ 422179193Sjb int i; 423179193Sjb 424179193Sjb if (gen < fasttrap_mod_gen) 425179193Sjb return; 426179193Sjb 427179193Sjb fasttrap_mod_gen++; 428179193Sjb 429211925Srpaulo CPU_FOREACH(i) { 430211925Srpaulo mutex_enter(&fasttrap_cpuc_pid_lock[i]); 431211925Srpaulo mutex_exit(&fasttrap_cpuc_pid_lock[i]); 432179193Sjb } 433179193Sjb} 434179193Sjb 435179193Sjb/* 436250953Smarkj * This function performs asynchronous cleanup of fasttrap providers. The 437250953Smarkj * Solaris implementation of this mechanism use a timeout that's activated in 438250953Smarkj * fasttrap_pid_cleanup(), but this doesn't work in FreeBSD: one may sleep while 439250953Smarkj * holding the DTrace mutexes, but it is unsafe to sleep in a callout handler. 440250953Smarkj * Thus we use a dedicated process to perform the cleanup when requested. 441179193Sjb */ 442179193Sjb/*ARGSUSED*/ 443179193Sjbstatic void 444179193Sjbfasttrap_pid_cleanup_cb(void *data) 445179193Sjb{ 446179193Sjb fasttrap_provider_t **fpp, *fp; 447179193Sjb fasttrap_bucket_t *bucket; 448179193Sjb dtrace_provider_id_t provid; 449248983Spfg int i, later = 0, rval; 450179193Sjb 451250953Smarkj mtx_lock(&fasttrap_cleanup_mtx); 452250953Smarkj while (!fasttrap_cleanup_drain || later > 0) { 453179193Sjb fasttrap_cleanup_work = 0; 454211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 455179193Sjb 456179193Sjb later = 0; 457179193Sjb 458179193Sjb /* 459179193Sjb * Iterate over all the providers trying to remove the marked 460179193Sjb * ones. If a provider is marked but not retired, we just 461179193Sjb * have to take a crack at removing it -- it's no big deal if 462179193Sjb * we can't. 463179193Sjb */ 464179193Sjb for (i = 0; i < fasttrap_provs.fth_nent; i++) { 465179193Sjb bucket = &fasttrap_provs.fth_table[i]; 466179193Sjb mutex_enter(&bucket->ftb_mtx); 467179193Sjb fpp = (fasttrap_provider_t **)&bucket->ftb_data; 468179193Sjb 469179193Sjb while ((fp = *fpp) != NULL) { 470179193Sjb if (!fp->ftp_marked) { 471179193Sjb fpp = &fp->ftp_next; 472179193Sjb continue; 473179193Sjb } 474179193Sjb 475179193Sjb mutex_enter(&fp->ftp_mtx); 476179193Sjb 477179193Sjb /* 478179193Sjb * If this provider has consumers actively 479179193Sjb * creating probes (ftp_ccount) or is a USDT 480179193Sjb * provider (ftp_mcount), we can't unregister 481179193Sjb * or even condense. 482179193Sjb */ 483179193Sjb if (fp->ftp_ccount != 0 || 484179193Sjb fp->ftp_mcount != 0) { 485179193Sjb mutex_exit(&fp->ftp_mtx); 486179193Sjb fp->ftp_marked = 0; 487179193Sjb continue; 488179193Sjb } 489179193Sjb 490179193Sjb if (!fp->ftp_retired || fp->ftp_rcount != 0) 491179193Sjb fp->ftp_marked = 0; 492179193Sjb 493179193Sjb mutex_exit(&fp->ftp_mtx); 494179193Sjb 495179193Sjb /* 496179193Sjb * If we successfully unregister this 497179193Sjb * provider we can remove it from the hash 498179193Sjb * chain and free the memory. If our attempt 499179193Sjb * to unregister fails and this is a retired 500179193Sjb * provider, increment our flag to try again 501179193Sjb * pretty soon. If we've consumed more than 502179193Sjb * half of our total permitted number of 503179193Sjb * probes call dtrace_condense() to try to 504179193Sjb * clean out the unenabled probes. 505179193Sjb */ 506179193Sjb provid = fp->ftp_provid; 507248983Spfg if ((rval = dtrace_unregister(provid)) != 0) { 508179193Sjb if (fasttrap_total > fasttrap_max / 2) 509179193Sjb (void) dtrace_condense(provid); 510248983Spfg 511248983Spfg if (rval == EAGAIN) 512248983Spfg fp->ftp_marked = 1; 513248983Spfg 514179193Sjb later += fp->ftp_marked; 515179193Sjb fpp = &fp->ftp_next; 516179193Sjb } else { 517179193Sjb *fpp = fp->ftp_next; 518179193Sjb fasttrap_provider_free(fp); 519179193Sjb } 520179193Sjb } 521179193Sjb mutex_exit(&bucket->ftb_mtx); 522179193Sjb } 523250953Smarkj mtx_lock(&fasttrap_cleanup_mtx); 524179193Sjb 525250953Smarkj /* 526250953Smarkj * If we were unable to retire a provider, try again after a 527250953Smarkj * second. This situation can occur in certain circumstances 528250953Smarkj * where providers cannot be unregistered even though they have 529250953Smarkj * no probes enabled because of an execution of dtrace -l or 530250953Smarkj * something similar. 531250953Smarkj */ 532250953Smarkj if (later > 0 || fasttrap_cleanup_work || 533250953Smarkj fasttrap_cleanup_drain) { 534250953Smarkj mtx_unlock(&fasttrap_cleanup_mtx); 535250953Smarkj pause("ftclean", hz); 536250953Smarkj mtx_lock(&fasttrap_cleanup_mtx); 537250953Smarkj } else 538250953Smarkj mtx_sleep(&fasttrap_cleanup_cv, &fasttrap_cleanup_mtx, 539250953Smarkj 0, "ftcl", 0); 540179193Sjb } 541179193Sjb 542179193Sjb /* 543250953Smarkj * Wake up the thread in fasttrap_unload() now that we're done. 544179193Sjb */ 545250953Smarkj wakeup(&fasttrap_cleanup_drain); 546250953Smarkj mtx_unlock(&fasttrap_cleanup_mtx); 547179193Sjb 548250953Smarkj kthread_exit(); 549179193Sjb} 550179193Sjb 551179193Sjb/* 552179193Sjb * Activates the asynchronous cleanup mechanism. 553179193Sjb */ 554179193Sjbstatic void 555179193Sjbfasttrap_pid_cleanup(void) 556179193Sjb{ 557211738Srpaulo 558211738Srpaulo mtx_lock(&fasttrap_cleanup_mtx); 559250953Smarkj if (!fasttrap_cleanup_work) { 560250953Smarkj fasttrap_cleanup_work = 1; 561250953Smarkj wakeup(&fasttrap_cleanup_cv); 562250953Smarkj } 563211738Srpaulo mtx_unlock(&fasttrap_cleanup_mtx); 564179193Sjb} 565179193Sjb 566179193Sjb/* 567179193Sjb * This is called from cfork() via dtrace_fasttrap_fork(). The child 568179198Sjb * process's address space is (roughly) a copy of the parent process's so 569179193Sjb * we have to remove all the instrumentation we had previously enabled in the 570179193Sjb * parent. 571179193Sjb */ 572179193Sjbstatic void 573179193Sjbfasttrap_fork(proc_t *p, proc_t *cp) 574179193Sjb{ 575269342Smarkj#if !defined(sun) 576269342Smarkj fasttrap_scrblock_t *scrblk; 577269342Smarkj fasttrap_proc_t *fprc = NULL; 578269342Smarkj#endif 579179193Sjb pid_t ppid = p->p_pid; 580179193Sjb int i; 581179193Sjb 582211738Srpaulo#if defined(sun) 583179193Sjb ASSERT(curproc == p); 584179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 585211738Srpaulo#else 586211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 587211738Srpaulo#endif 588211738Srpaulo#if defined(sun) 589179193Sjb ASSERT(p->p_dtrace_count > 0); 590211738Srpaulo#else 591212357Srpaulo if (p->p_dtrace_helpers) { 592212357Srpaulo /* 593212357Srpaulo * dtrace_helpers_duplicate() allocates memory. 594212357Srpaulo */ 595212494Srpaulo _PHOLD(cp); 596212357Srpaulo PROC_UNLOCK(p); 597212357Srpaulo PROC_UNLOCK(cp); 598212357Srpaulo dtrace_helpers_duplicate(p, cp); 599212357Srpaulo PROC_LOCK(cp); 600212357Srpaulo PROC_LOCK(p); 601212494Srpaulo _PRELE(cp); 602212357Srpaulo } 603211738Srpaulo /* 604211738Srpaulo * This check is purposely here instead of in kern_fork.c because, 605211738Srpaulo * for legal resons, we cannot include the dtrace_cddl.h header 606211738Srpaulo * inside kern_fork.c and insert if-clause there. 607211738Srpaulo */ 608211738Srpaulo if (p->p_dtrace_count == 0) 609211738Srpaulo return; 610211738Srpaulo#endif 611179193Sjb ASSERT(cp->p_dtrace_count == 0); 612179193Sjb 613179193Sjb /* 614179193Sjb * This would be simpler and faster if we maintained per-process 615179193Sjb * hash tables of enabled tracepoints. It could, however, potentially 616179193Sjb * slow down execution of a tracepoint since we'd need to go 617179193Sjb * through two levels of indirection. In the future, we should 618179193Sjb * consider either maintaining per-process ancillary lists of 619179193Sjb * enabled tracepoints or hanging a pointer to a per-process hash 620179193Sjb * table of enabled tracepoints off the proc structure. 621179193Sjb */ 622179193Sjb 623179193Sjb /* 624179193Sjb * We don't have to worry about the child process disappearing 625179193Sjb * because we're in fork(). 626179193Sjb */ 627211738Srpaulo#if defined(sun) 628211738Srpaulo mtx_lock_spin(&cp->p_slock); 629179193Sjb sprlock_proc(cp); 630211738Srpaulo mtx_unlock_spin(&cp->p_slock); 631212494Srpaulo#else 632254198Srpaulo /* 633254198Srpaulo * fasttrap_tracepoint_remove() expects the child process to be 634254198Srpaulo * unlocked and the VM then expects curproc to be unlocked. 635254198Srpaulo */ 636212494Srpaulo _PHOLD(cp); 637254198Srpaulo PROC_UNLOCK(cp); 638254198Srpaulo PROC_UNLOCK(p); 639211738Srpaulo#endif 640179193Sjb 641179193Sjb /* 642179193Sjb * Iterate over every tracepoint looking for ones that belong to the 643179193Sjb * parent process, and remove each from the child process. 644179193Sjb */ 645179193Sjb for (i = 0; i < fasttrap_tpoints.fth_nent; i++) { 646179193Sjb fasttrap_tracepoint_t *tp; 647179193Sjb fasttrap_bucket_t *bucket = &fasttrap_tpoints.fth_table[i]; 648179193Sjb 649179193Sjb mutex_enter(&bucket->ftb_mtx); 650179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 651179193Sjb if (tp->ftt_pid == ppid && 652179193Sjb tp->ftt_proc->ftpc_acount != 0) { 653179193Sjb int ret = fasttrap_tracepoint_remove(cp, tp); 654179193Sjb ASSERT(ret == 0); 655179198Sjb 656179198Sjb /* 657179198Sjb * The count of active providers can only be 658179198Sjb * decremented (i.e. to zero) during exec, 659179198Sjb * exit, and removal of a meta provider so it 660179198Sjb * should be impossible to drop the count 661179198Sjb * mid-fork. 662179198Sjb */ 663179198Sjb ASSERT(tp->ftt_proc->ftpc_acount != 0); 664269342Smarkj#if !defined(sun) 665269342Smarkj fprc = tp->ftt_proc; 666269342Smarkj#endif 667179193Sjb } 668179193Sjb } 669179193Sjb mutex_exit(&bucket->ftb_mtx); 670269342Smarkj 671269342Smarkj#if !defined(sun) 672269342Smarkj /* 673269342Smarkj * Unmap any scratch space inherited from the parent's address 674269342Smarkj * space. 675269342Smarkj */ 676269342Smarkj if (fprc != NULL) { 677269342Smarkj mutex_enter(&fprc->ftpc_mtx); 678269342Smarkj LIST_FOREACH(scrblk, &fprc->ftpc_scrblks, ftsb_next) { 679269342Smarkj vm_map_remove(&cp->p_vmspace->vm_map, 680269342Smarkj scrblk->ftsb_addr, 681269342Smarkj scrblk->ftsb_addr + FASTTRAP_SCRBLOCK_SIZE); 682269342Smarkj } 683269342Smarkj mutex_exit(&fprc->ftpc_mtx); 684269342Smarkj } 685269342Smarkj#endif 686179193Sjb } 687179193Sjb 688211738Srpaulo#if defined(sun) 689179193Sjb mutex_enter(&cp->p_lock); 690179193Sjb sprunlock(cp); 691212494Srpaulo#else 692254198Srpaulo PROC_LOCK(p); 693254198Srpaulo PROC_LOCK(cp); 694212494Srpaulo _PRELE(cp); 695211738Srpaulo#endif 696179193Sjb} 697179193Sjb 698179193Sjb/* 699179193Sjb * This is called from proc_exit() or from exec_common() if p_dtrace_probes 700179193Sjb * is set on the proc structure to indicate that there is a pid provider 701179193Sjb * associated with this process. 702179193Sjb */ 703179193Sjbstatic void 704179193Sjbfasttrap_exec_exit(proc_t *p) 705179193Sjb{ 706269342Smarkj#if !defined(sun) 707269342Smarkj struct thread *td; 708269342Smarkj#endif 709269342Smarkj 710211738Srpaulo#if defined(sun) 711179193Sjb ASSERT(p == curproc); 712269342Smarkj#else 713211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 714212494Srpaulo _PHOLD(p); 715269342Smarkj /* 716269342Smarkj * Since struct threads may be recycled, we cannot rely on t_dtrace_sscr 717269342Smarkj * fields to be zeroed by kdtrace_thread_ctor. Thus we must zero it 718269342Smarkj * ourselves when a process exits. 719269342Smarkj */ 720269342Smarkj FOREACH_THREAD_IN_PROC(p, td) 721269342Smarkj td->t_dtrace_sscr = NULL; 722211738Srpaulo PROC_UNLOCK(p); 723269342Smarkj#endif 724179193Sjb 725179193Sjb /* 726179193Sjb * We clean up the pid provider for this process here; user-land 727179193Sjb * static probes are handled by the meta-provider remove entry point. 728179193Sjb */ 729179193Sjb fasttrap_provider_retire(p->p_pid, FASTTRAP_PID_NAME, 0); 730212357Srpaulo#if !defined(sun) 731212357Srpaulo if (p->p_dtrace_helpers) 732212357Srpaulo dtrace_helpers_destroy(p); 733211738Srpaulo PROC_LOCK(p); 734212494Srpaulo _PRELE(p); 735269342Smarkj#endif 736179193Sjb} 737179193Sjb 738179193Sjb 739179193Sjb/*ARGSUSED*/ 740179193Sjbstatic void 741211738Srpaulofasttrap_pid_provide(void *arg, dtrace_probedesc_t *desc) 742179193Sjb{ 743179193Sjb /* 744179193Sjb * There are no "default" pid probes. 745179193Sjb */ 746179193Sjb} 747179193Sjb 748179193Sjbstatic int 749179193Sjbfasttrap_tracepoint_enable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 750179193Sjb{ 751179193Sjb fasttrap_tracepoint_t *tp, *new_tp = NULL; 752179193Sjb fasttrap_bucket_t *bucket; 753179193Sjb fasttrap_id_t *id; 754179193Sjb pid_t pid; 755179193Sjb uintptr_t pc; 756179193Sjb 757179193Sjb ASSERT(index < probe->ftp_ntps); 758179193Sjb 759179193Sjb pid = probe->ftp_pid; 760179193Sjb pc = probe->ftp_tps[index].fit_tp->ftt_pc; 761179193Sjb id = &probe->ftp_tps[index].fit_id; 762179193Sjb 763179193Sjb ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 764179193Sjb 765211738Srpaulo#if defined(sun) 766179193Sjb ASSERT(!(p->p_flag & SVFORK)); 767211738Srpaulo#endif 768179193Sjb 769179193Sjb /* 770179193Sjb * Before we make any modifications, make sure we've imposed a barrier 771179193Sjb * on the generation in which this probe was last modified. 772179193Sjb */ 773179193Sjb fasttrap_mod_barrier(probe->ftp_gen); 774179193Sjb 775179193Sjb bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 776179193Sjb 777179193Sjb /* 778179193Sjb * If the tracepoint has already been enabled, just add our id to the 779179193Sjb * list of interested probes. This may be our second time through 780179193Sjb * this path in which case we'll have constructed the tracepoint we'd 781179193Sjb * like to install. If we can't find a match, and have an allocated 782179193Sjb * tracepoint ready to go, enable that one now. 783179193Sjb * 784179193Sjb * A tracepoint whose process is defunct is also considered defunct. 785179193Sjb */ 786179193Sjbagain: 787179193Sjb mutex_enter(&bucket->ftb_mtx); 788179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 789179198Sjb /* 790179198Sjb * Note that it's safe to access the active count on the 791179198Sjb * associated proc structure because we know that at least one 792179198Sjb * provider (this one) will still be around throughout this 793179198Sjb * operation. 794179198Sjb */ 795179193Sjb if (tp->ftt_pid != pid || tp->ftt_pc != pc || 796179193Sjb tp->ftt_proc->ftpc_acount == 0) 797179193Sjb continue; 798179193Sjb 799179193Sjb /* 800179193Sjb * Now that we've found a matching tracepoint, it would be 801179193Sjb * a decent idea to confirm that the tracepoint is still 802179193Sjb * enabled and the trap instruction hasn't been overwritten. 803179193Sjb * Since this is a little hairy, we'll punt for now. 804179193Sjb */ 805179193Sjb 806179193Sjb /* 807179193Sjb * This can't be the first interested probe. We don't have 808179193Sjb * to worry about another thread being in the midst of 809179193Sjb * deleting this tracepoint (which would be the only valid 810179193Sjb * reason for a tracepoint to have no interested probes) 811179193Sjb * since we're holding P_PR_LOCK for this process. 812179193Sjb */ 813179193Sjb ASSERT(tp->ftt_ids != NULL || tp->ftt_retids != NULL); 814179193Sjb 815179193Sjb switch (id->fti_ptype) { 816179193Sjb case DTFTP_ENTRY: 817179193Sjb case DTFTP_OFFSETS: 818179193Sjb case DTFTP_IS_ENABLED: 819179193Sjb id->fti_next = tp->ftt_ids; 820179193Sjb membar_producer(); 821179193Sjb tp->ftt_ids = id; 822179193Sjb membar_producer(); 823179193Sjb break; 824179193Sjb 825179193Sjb case DTFTP_RETURN: 826179193Sjb case DTFTP_POST_OFFSETS: 827179193Sjb id->fti_next = tp->ftt_retids; 828179193Sjb membar_producer(); 829179193Sjb tp->ftt_retids = id; 830179193Sjb membar_producer(); 831179193Sjb break; 832179193Sjb 833179193Sjb default: 834179193Sjb ASSERT(0); 835179193Sjb } 836179193Sjb 837179193Sjb mutex_exit(&bucket->ftb_mtx); 838179193Sjb 839179193Sjb if (new_tp != NULL) { 840179193Sjb new_tp->ftt_ids = NULL; 841179193Sjb new_tp->ftt_retids = NULL; 842179193Sjb } 843179193Sjb 844179193Sjb return (0); 845179193Sjb } 846179193Sjb 847179193Sjb /* 848179193Sjb * If we have a good tracepoint ready to go, install it now while 849179193Sjb * we have the lock held and no one can screw with us. 850179193Sjb */ 851179193Sjb if (new_tp != NULL) { 852179193Sjb int rc = 0; 853179193Sjb 854179193Sjb new_tp->ftt_next = bucket->ftb_data; 855179193Sjb membar_producer(); 856179193Sjb bucket->ftb_data = new_tp; 857179193Sjb membar_producer(); 858179193Sjb mutex_exit(&bucket->ftb_mtx); 859179193Sjb 860179193Sjb /* 861179193Sjb * Activate the tracepoint in the ISA-specific manner. 862179193Sjb * If this fails, we need to report the failure, but 863179193Sjb * indicate that this tracepoint must still be disabled 864179193Sjb * by calling fasttrap_tracepoint_disable(). 865179193Sjb */ 866179193Sjb if (fasttrap_tracepoint_install(p, new_tp) != 0) 867179193Sjb rc = FASTTRAP_ENABLE_PARTIAL; 868179193Sjb 869179193Sjb /* 870179193Sjb * Increment the count of the number of tracepoints active in 871179193Sjb * the victim process. 872179193Sjb */ 873211738Srpaulo#if defined(sun) 874179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 875211738Srpaulo#endif 876179193Sjb p->p_dtrace_count++; 877179193Sjb 878179193Sjb return (rc); 879179193Sjb } 880179193Sjb 881179193Sjb mutex_exit(&bucket->ftb_mtx); 882179193Sjb 883179193Sjb /* 884179193Sjb * Initialize the tracepoint that's been preallocated with the probe. 885179193Sjb */ 886179193Sjb new_tp = probe->ftp_tps[index].fit_tp; 887179193Sjb 888179193Sjb ASSERT(new_tp->ftt_pid == pid); 889179193Sjb ASSERT(new_tp->ftt_pc == pc); 890179193Sjb ASSERT(new_tp->ftt_proc == probe->ftp_prov->ftp_proc); 891179193Sjb ASSERT(new_tp->ftt_ids == NULL); 892179193Sjb ASSERT(new_tp->ftt_retids == NULL); 893179193Sjb 894179193Sjb switch (id->fti_ptype) { 895179193Sjb case DTFTP_ENTRY: 896179193Sjb case DTFTP_OFFSETS: 897179193Sjb case DTFTP_IS_ENABLED: 898179193Sjb id->fti_next = NULL; 899179193Sjb new_tp->ftt_ids = id; 900179193Sjb break; 901179193Sjb 902179193Sjb case DTFTP_RETURN: 903179193Sjb case DTFTP_POST_OFFSETS: 904179193Sjb id->fti_next = NULL; 905179193Sjb new_tp->ftt_retids = id; 906179193Sjb break; 907179193Sjb 908179193Sjb default: 909179193Sjb ASSERT(0); 910179193Sjb } 911179193Sjb 912179193Sjb /* 913179193Sjb * If the ISA-dependent initialization goes to plan, go back to the 914179193Sjb * beginning and try to install this freshly made tracepoint. 915179193Sjb */ 916179193Sjb if (fasttrap_tracepoint_init(p, new_tp, pc, id->fti_ptype) == 0) 917179193Sjb goto again; 918179193Sjb 919179193Sjb new_tp->ftt_ids = NULL; 920179193Sjb new_tp->ftt_retids = NULL; 921179193Sjb 922179193Sjb return (FASTTRAP_ENABLE_FAIL); 923179193Sjb} 924179193Sjb 925179193Sjbstatic void 926179193Sjbfasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_t *probe, uint_t index) 927179193Sjb{ 928179193Sjb fasttrap_bucket_t *bucket; 929179193Sjb fasttrap_provider_t *provider = probe->ftp_prov; 930179193Sjb fasttrap_tracepoint_t **pp, *tp; 931211738Srpaulo fasttrap_id_t *id, **idp = NULL; 932179193Sjb pid_t pid; 933179193Sjb uintptr_t pc; 934179193Sjb 935179193Sjb ASSERT(index < probe->ftp_ntps); 936179193Sjb 937179193Sjb pid = probe->ftp_pid; 938179193Sjb pc = probe->ftp_tps[index].fit_tp->ftt_pc; 939179193Sjb id = &probe->ftp_tps[index].fit_id; 940179193Sjb 941179193Sjb ASSERT(probe->ftp_tps[index].fit_tp->ftt_pid == pid); 942179193Sjb 943179193Sjb /* 944179193Sjb * Find the tracepoint and make sure that our id is one of the 945179193Sjb * ones registered with it. 946179193Sjb */ 947179193Sjb bucket = &fasttrap_tpoints.fth_table[FASTTRAP_TPOINTS_INDEX(pid, pc)]; 948179193Sjb mutex_enter(&bucket->ftb_mtx); 949179193Sjb for (tp = bucket->ftb_data; tp != NULL; tp = tp->ftt_next) { 950179193Sjb if (tp->ftt_pid == pid && tp->ftt_pc == pc && 951179193Sjb tp->ftt_proc == provider->ftp_proc) 952179193Sjb break; 953179193Sjb } 954179193Sjb 955179193Sjb /* 956179193Sjb * If we somehow lost this tracepoint, we're in a world of hurt. 957179193Sjb */ 958179193Sjb ASSERT(tp != NULL); 959179193Sjb 960179193Sjb switch (id->fti_ptype) { 961179193Sjb case DTFTP_ENTRY: 962179193Sjb case DTFTP_OFFSETS: 963179193Sjb case DTFTP_IS_ENABLED: 964179193Sjb ASSERT(tp->ftt_ids != NULL); 965179193Sjb idp = &tp->ftt_ids; 966179193Sjb break; 967179193Sjb 968179193Sjb case DTFTP_RETURN: 969179193Sjb case DTFTP_POST_OFFSETS: 970179193Sjb ASSERT(tp->ftt_retids != NULL); 971179193Sjb idp = &tp->ftt_retids; 972179193Sjb break; 973179193Sjb 974179193Sjb default: 975179193Sjb ASSERT(0); 976179193Sjb } 977179193Sjb 978179193Sjb while ((*idp)->fti_probe != probe) { 979179193Sjb idp = &(*idp)->fti_next; 980179193Sjb ASSERT(*idp != NULL); 981179193Sjb } 982179193Sjb 983179193Sjb id = *idp; 984179193Sjb *idp = id->fti_next; 985179193Sjb membar_producer(); 986179193Sjb 987179193Sjb ASSERT(id->fti_probe == probe); 988179193Sjb 989179193Sjb /* 990179193Sjb * If there are other registered enablings of this tracepoint, we're 991179193Sjb * all done, but if this was the last probe assocated with this 992179193Sjb * this tracepoint, we need to remove and free it. 993179193Sjb */ 994179193Sjb if (tp->ftt_ids != NULL || tp->ftt_retids != NULL) { 995179193Sjb 996179193Sjb /* 997179193Sjb * If the current probe's tracepoint is in use, swap it 998179193Sjb * for an unused tracepoint. 999179193Sjb */ 1000179193Sjb if (tp == probe->ftp_tps[index].fit_tp) { 1001179193Sjb fasttrap_probe_t *tmp_probe; 1002179193Sjb fasttrap_tracepoint_t **tmp_tp; 1003179193Sjb uint_t tmp_index; 1004179193Sjb 1005179193Sjb if (tp->ftt_ids != NULL) { 1006179193Sjb tmp_probe = tp->ftt_ids->fti_probe; 1007179193Sjb /* LINTED - alignment */ 1008179193Sjb tmp_index = FASTTRAP_ID_INDEX(tp->ftt_ids); 1009179193Sjb tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 1010179193Sjb } else { 1011179193Sjb tmp_probe = tp->ftt_retids->fti_probe; 1012179193Sjb /* LINTED - alignment */ 1013179193Sjb tmp_index = FASTTRAP_ID_INDEX(tp->ftt_retids); 1014179193Sjb tmp_tp = &tmp_probe->ftp_tps[tmp_index].fit_tp; 1015179193Sjb } 1016179193Sjb 1017179193Sjb ASSERT(*tmp_tp != NULL); 1018179193Sjb ASSERT(*tmp_tp != probe->ftp_tps[index].fit_tp); 1019179193Sjb ASSERT((*tmp_tp)->ftt_ids == NULL); 1020179193Sjb ASSERT((*tmp_tp)->ftt_retids == NULL); 1021179193Sjb 1022179193Sjb probe->ftp_tps[index].fit_tp = *tmp_tp; 1023179193Sjb *tmp_tp = tp; 1024179193Sjb } 1025179193Sjb 1026179193Sjb mutex_exit(&bucket->ftb_mtx); 1027179193Sjb 1028179193Sjb /* 1029179193Sjb * Tag the modified probe with the generation in which it was 1030179193Sjb * changed. 1031179193Sjb */ 1032179193Sjb probe->ftp_gen = fasttrap_mod_gen; 1033179193Sjb return; 1034179193Sjb } 1035179193Sjb 1036179193Sjb mutex_exit(&bucket->ftb_mtx); 1037179193Sjb 1038179193Sjb /* 1039179193Sjb * We can't safely remove the tracepoint from the set of active 1040179193Sjb * tracepoints until we've actually removed the fasttrap instruction 1041179193Sjb * from the process's text. We can, however, operate on this 1042179193Sjb * tracepoint secure in the knowledge that no other thread is going to 1043179193Sjb * be looking at it since we hold P_PR_LOCK on the process if it's 1044179193Sjb * live or we hold the provider lock on the process if it's dead and 1045179193Sjb * gone. 1046179193Sjb */ 1047179193Sjb 1048179193Sjb /* 1049179193Sjb * We only need to remove the actual instruction if we're looking 1050179193Sjb * at an existing process 1051179193Sjb */ 1052179193Sjb if (p != NULL) { 1053179193Sjb /* 1054179193Sjb * If we fail to restore the instruction we need to kill 1055179193Sjb * this process since it's in a completely unrecoverable 1056179193Sjb * state. 1057179193Sjb */ 1058179193Sjb if (fasttrap_tracepoint_remove(p, tp) != 0) 1059179193Sjb fasttrap_sigtrap(p, NULL, pc); 1060179193Sjb 1061179193Sjb /* 1062179193Sjb * Decrement the count of the number of tracepoints active 1063179193Sjb * in the victim process. 1064179193Sjb */ 1065211738Srpaulo#if defined(sun) 1066179193Sjb ASSERT(p->p_proc_flag & P_PR_LOCK); 1067211738Srpaulo#endif 1068179193Sjb p->p_dtrace_count--; 1069179193Sjb } 1070179193Sjb 1071179193Sjb /* 1072179193Sjb * Remove the probe from the hash table of active tracepoints. 1073179193Sjb */ 1074179193Sjb mutex_enter(&bucket->ftb_mtx); 1075179193Sjb pp = (fasttrap_tracepoint_t **)&bucket->ftb_data; 1076179193Sjb ASSERT(*pp != NULL); 1077179193Sjb while (*pp != tp) { 1078179193Sjb pp = &(*pp)->ftt_next; 1079179193Sjb ASSERT(*pp != NULL); 1080179193Sjb } 1081179193Sjb 1082179193Sjb *pp = tp->ftt_next; 1083179193Sjb membar_producer(); 1084179193Sjb 1085179193Sjb mutex_exit(&bucket->ftb_mtx); 1086179193Sjb 1087179193Sjb /* 1088179193Sjb * Tag the modified probe with the generation in which it was changed. 1089179193Sjb */ 1090179193Sjb probe->ftp_gen = fasttrap_mod_gen; 1091179193Sjb} 1092179193Sjb 1093179193Sjbstatic void 1094179193Sjbfasttrap_enable_callbacks(void) 1095179193Sjb{ 1096179193Sjb /* 1097179193Sjb * We don't have to play the rw lock game here because we're 1098179193Sjb * providing something rather than taking something away -- 1099179193Sjb * we can be sure that no threads have tried to follow this 1100179193Sjb * function pointer yet. 1101179193Sjb */ 1102179193Sjb mutex_enter(&fasttrap_count_mtx); 1103179193Sjb if (fasttrap_pid_count == 0) { 1104179193Sjb ASSERT(dtrace_pid_probe_ptr == NULL); 1105179193Sjb ASSERT(dtrace_return_probe_ptr == NULL); 1106179193Sjb dtrace_pid_probe_ptr = &fasttrap_pid_probe; 1107179193Sjb dtrace_return_probe_ptr = &fasttrap_return_probe; 1108179193Sjb } 1109179193Sjb ASSERT(dtrace_pid_probe_ptr == &fasttrap_pid_probe); 1110179193Sjb ASSERT(dtrace_return_probe_ptr == &fasttrap_return_probe); 1111179193Sjb fasttrap_pid_count++; 1112179193Sjb mutex_exit(&fasttrap_count_mtx); 1113179193Sjb} 1114179193Sjb 1115179193Sjbstatic void 1116179193Sjbfasttrap_disable_callbacks(void) 1117179193Sjb{ 1118211738Srpaulo#if defined(sun) 1119179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 1120211738Srpaulo#endif 1121179193Sjb 1122211738Srpaulo 1123179193Sjb mutex_enter(&fasttrap_count_mtx); 1124179193Sjb ASSERT(fasttrap_pid_count > 0); 1125179193Sjb fasttrap_pid_count--; 1126179193Sjb if (fasttrap_pid_count == 0) { 1127211738Srpaulo#if defined(sun) 1128179193Sjb cpu_t *cur, *cpu = CPU; 1129179193Sjb 1130179193Sjb for (cur = cpu->cpu_next_onln; cur != cpu; 1131179193Sjb cur = cur->cpu_next_onln) { 1132179193Sjb rw_enter(&cur->cpu_ft_lock, RW_WRITER); 1133179193Sjb } 1134211738Srpaulo#endif 1135179193Sjb dtrace_pid_probe_ptr = NULL; 1136179193Sjb dtrace_return_probe_ptr = NULL; 1137211738Srpaulo#if defined(sun) 1138179193Sjb for (cur = cpu->cpu_next_onln; cur != cpu; 1139179193Sjb cur = cur->cpu_next_onln) { 1140179193Sjb rw_exit(&cur->cpu_ft_lock); 1141179193Sjb } 1142211738Srpaulo#endif 1143179193Sjb } 1144179193Sjb mutex_exit(&fasttrap_count_mtx); 1145179193Sjb} 1146179193Sjb 1147179193Sjb/*ARGSUSED*/ 1148179193Sjbstatic void 1149179193Sjbfasttrap_pid_enable(void *arg, dtrace_id_t id, void *parg) 1150179193Sjb{ 1151179193Sjb fasttrap_probe_t *probe = parg; 1152211738Srpaulo proc_t *p = NULL; 1153179193Sjb int i, rc; 1154179193Sjb 1155179193Sjb ASSERT(probe != NULL); 1156179193Sjb ASSERT(!probe->ftp_enabled); 1157179193Sjb ASSERT(id == probe->ftp_id); 1158211738Srpaulo#if defined(sun) 1159179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 1160211738Srpaulo#endif 1161179193Sjb 1162179193Sjb /* 1163179193Sjb * Increment the count of enabled probes on this probe's provider; 1164179193Sjb * the provider can't go away while the probe still exists. We 1165179193Sjb * must increment this even if we aren't able to properly enable 1166179193Sjb * this probe. 1167179193Sjb */ 1168179193Sjb mutex_enter(&probe->ftp_prov->ftp_mtx); 1169179193Sjb probe->ftp_prov->ftp_rcount++; 1170179193Sjb mutex_exit(&probe->ftp_prov->ftp_mtx); 1171179193Sjb 1172179193Sjb /* 1173179193Sjb * If this probe's provider is retired (meaning it was valid in a 1174179193Sjb * previously exec'ed incarnation of this address space), bail out. The 1175179193Sjb * provider can't go away while we're in this code path. 1176179193Sjb */ 1177179193Sjb if (probe->ftp_prov->ftp_retired) 1178179193Sjb return; 1179179193Sjb 1180179193Sjb /* 1181179193Sjb * If we can't find the process, it may be that we're in the context of 1182179193Sjb * a fork in which the traced process is being born and we're copying 1183179193Sjb * USDT probes. Otherwise, the process is gone so bail. 1184179193Sjb */ 1185211738Srpaulo#if defined(sun) 1186179193Sjb if ((p = sprlock(probe->ftp_pid)) == NULL) { 1187179193Sjb if ((curproc->p_flag & SFORKING) == 0) 1188179193Sjb return; 1189179193Sjb 1190179193Sjb mutex_enter(&pidlock); 1191179193Sjb p = prfind(probe->ftp_pid); 1192179193Sjb 1193179193Sjb /* 1194179193Sjb * Confirm that curproc is indeed forking the process in which 1195179193Sjb * we're trying to enable probes. 1196179193Sjb */ 1197179193Sjb ASSERT(p != NULL); 1198179193Sjb ASSERT(p->p_parent == curproc); 1199179193Sjb ASSERT(p->p_stat == SIDL); 1200179193Sjb 1201179193Sjb mutex_enter(&p->p_lock); 1202179193Sjb mutex_exit(&pidlock); 1203179193Sjb 1204179193Sjb sprlock_proc(p); 1205179193Sjb } 1206179193Sjb 1207179193Sjb ASSERT(!(p->p_flag & SVFORK)); 1208179193Sjb mutex_exit(&p->p_lock); 1209211738Srpaulo#else 1210211738Srpaulo if ((p = pfind(probe->ftp_pid)) == NULL) 1211211738Srpaulo return; 1212211738Srpaulo#endif 1213179193Sjb 1214179193Sjb /* 1215179193Sjb * We have to enable the trap entry point before any user threads have 1216179193Sjb * the chance to execute the trap instruction we're about to place 1217179193Sjb * in their process's text. 1218179193Sjb */ 1219212494Srpaulo#ifdef __FreeBSD__ 1220212494Srpaulo /* 1221212494Srpaulo * pfind() returns a locked process. 1222212494Srpaulo */ 1223212494Srpaulo _PHOLD(p); 1224211738Srpaulo PROC_UNLOCK(p); 1225212494Srpaulo#endif 1226179193Sjb fasttrap_enable_callbacks(); 1227179193Sjb 1228179193Sjb /* 1229179193Sjb * Enable all the tracepoints and add this probe's id to each 1230179193Sjb * tracepoint's list of active probes. 1231179193Sjb */ 1232179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1233179193Sjb if ((rc = fasttrap_tracepoint_enable(p, probe, i)) != 0) { 1234179193Sjb /* 1235179193Sjb * If enabling the tracepoint failed completely, 1236179193Sjb * we don't have to disable it; if the failure 1237179193Sjb * was only partial we must disable it. 1238179193Sjb */ 1239179193Sjb if (rc == FASTTRAP_ENABLE_FAIL) 1240179193Sjb i--; 1241179193Sjb else 1242179193Sjb ASSERT(rc == FASTTRAP_ENABLE_PARTIAL); 1243179193Sjb 1244179193Sjb /* 1245179193Sjb * Back up and pull out all the tracepoints we've 1246179193Sjb * created so far for this probe. 1247179193Sjb */ 1248179193Sjb while (i >= 0) { 1249179193Sjb fasttrap_tracepoint_disable(p, probe, i); 1250179193Sjb i--; 1251179193Sjb } 1252179193Sjb 1253211738Srpaulo#if defined(sun) 1254179193Sjb mutex_enter(&p->p_lock); 1255179193Sjb sprunlock(p); 1256211738Srpaulo#else 1257212494Srpaulo PRELE(p); 1258211738Srpaulo#endif 1259179193Sjb 1260179193Sjb /* 1261179193Sjb * Since we're not actually enabling this probe, 1262179193Sjb * drop our reference on the trap table entry. 1263179193Sjb */ 1264179193Sjb fasttrap_disable_callbacks(); 1265179193Sjb return; 1266179193Sjb } 1267179193Sjb } 1268211738Srpaulo#if defined(sun) 1269179193Sjb mutex_enter(&p->p_lock); 1270179193Sjb sprunlock(p); 1271211738Srpaulo#else 1272212494Srpaulo PRELE(p); 1273211738Srpaulo#endif 1274179193Sjb 1275179193Sjb probe->ftp_enabled = 1; 1276179193Sjb} 1277179193Sjb 1278179193Sjb/*ARGSUSED*/ 1279179193Sjbstatic void 1280179193Sjbfasttrap_pid_disable(void *arg, dtrace_id_t id, void *parg) 1281179193Sjb{ 1282179193Sjb fasttrap_probe_t *probe = parg; 1283179193Sjb fasttrap_provider_t *provider = probe->ftp_prov; 1284179193Sjb proc_t *p; 1285179193Sjb int i, whack = 0; 1286179193Sjb 1287179193Sjb ASSERT(id == probe->ftp_id); 1288179193Sjb 1289211738Srpaulo mutex_enter(&provider->ftp_mtx); 1290211738Srpaulo 1291179193Sjb /* 1292179193Sjb * We won't be able to acquire a /proc-esque lock on the process 1293179193Sjb * iff the process is dead and gone. In this case, we rely on the 1294179193Sjb * provider lock as a point of mutual exclusion to prevent other 1295179193Sjb * DTrace consumers from disabling this probe. 1296179193Sjb */ 1297247049Sgibbs if ((p = pfind(probe->ftp_pid)) != NULL) { 1298212494Srpaulo#ifdef __FreeBSD__ 1299247049Sgibbs _PHOLD(p); 1300247049Sgibbs PROC_UNLOCK(p); 1301212494Srpaulo#endif 1302247049Sgibbs } 1303179193Sjb 1304179193Sjb /* 1305179193Sjb * Disable all the associated tracepoints (for fully enabled probes). 1306179193Sjb */ 1307179193Sjb if (probe->ftp_enabled) { 1308179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1309179193Sjb fasttrap_tracepoint_disable(p, probe, i); 1310179193Sjb } 1311179193Sjb } 1312179193Sjb 1313179193Sjb ASSERT(provider->ftp_rcount > 0); 1314179193Sjb provider->ftp_rcount--; 1315179193Sjb 1316179193Sjb if (p != NULL) { 1317179193Sjb /* 1318179193Sjb * Even though we may not be able to remove it entirely, we 1319179193Sjb * mark this retired provider to get a chance to remove some 1320179193Sjb * of the associated probes. 1321179193Sjb */ 1322179193Sjb if (provider->ftp_retired && !provider->ftp_marked) 1323179193Sjb whack = provider->ftp_marked = 1; 1324179193Sjb mutex_exit(&provider->ftp_mtx); 1325179193Sjb } else { 1326179193Sjb /* 1327179193Sjb * If the process is dead, we're just waiting for the 1328179193Sjb * last probe to be disabled to be able to free it. 1329179193Sjb */ 1330179193Sjb if (provider->ftp_rcount == 0 && !provider->ftp_marked) 1331179193Sjb whack = provider->ftp_marked = 1; 1332179193Sjb mutex_exit(&provider->ftp_mtx); 1333179193Sjb } 1334179193Sjb 1335179193Sjb if (whack) 1336179193Sjb fasttrap_pid_cleanup(); 1337179193Sjb 1338212494Srpaulo#ifdef __FreeBSD__ 1339247049Sgibbs if (p != NULL) 1340247049Sgibbs PRELE(p); 1341212494Srpaulo#endif 1342179193Sjb if (!probe->ftp_enabled) 1343179193Sjb return; 1344179193Sjb 1345179193Sjb probe->ftp_enabled = 0; 1346179193Sjb 1347211738Srpaulo#if defined(sun) 1348179193Sjb ASSERT(MUTEX_HELD(&cpu_lock)); 1349211738Srpaulo#endif 1350179193Sjb fasttrap_disable_callbacks(); 1351179193Sjb} 1352179193Sjb 1353179193Sjb/*ARGSUSED*/ 1354179193Sjbstatic void 1355179193Sjbfasttrap_pid_getargdesc(void *arg, dtrace_id_t id, void *parg, 1356179193Sjb dtrace_argdesc_t *desc) 1357179193Sjb{ 1358179193Sjb fasttrap_probe_t *probe = parg; 1359179193Sjb char *str; 1360179193Sjb int i, ndx; 1361179193Sjb 1362179193Sjb desc->dtargd_native[0] = '\0'; 1363179193Sjb desc->dtargd_xlate[0] = '\0'; 1364179193Sjb 1365179193Sjb if (probe->ftp_prov->ftp_retired != 0 || 1366179193Sjb desc->dtargd_ndx >= probe->ftp_nargs) { 1367179193Sjb desc->dtargd_ndx = DTRACE_ARGNONE; 1368179193Sjb return; 1369179193Sjb } 1370179193Sjb 1371179193Sjb ndx = (probe->ftp_argmap != NULL) ? 1372179193Sjb probe->ftp_argmap[desc->dtargd_ndx] : desc->dtargd_ndx; 1373179193Sjb 1374179193Sjb str = probe->ftp_ntypes; 1375179193Sjb for (i = 0; i < ndx; i++) { 1376179193Sjb str += strlen(str) + 1; 1377179193Sjb } 1378179193Sjb 1379179193Sjb ASSERT(strlen(str + 1) < sizeof (desc->dtargd_native)); 1380179193Sjb (void) strcpy(desc->dtargd_native, str); 1381179193Sjb 1382179193Sjb if (probe->ftp_xtypes == NULL) 1383179193Sjb return; 1384179193Sjb 1385179193Sjb str = probe->ftp_xtypes; 1386179193Sjb for (i = 0; i < desc->dtargd_ndx; i++) { 1387179193Sjb str += strlen(str) + 1; 1388179193Sjb } 1389179193Sjb 1390179193Sjb ASSERT(strlen(str + 1) < sizeof (desc->dtargd_xlate)); 1391179193Sjb (void) strcpy(desc->dtargd_xlate, str); 1392179193Sjb} 1393179193Sjb 1394179193Sjb/*ARGSUSED*/ 1395179193Sjbstatic void 1396179193Sjbfasttrap_pid_destroy(void *arg, dtrace_id_t id, void *parg) 1397179193Sjb{ 1398179193Sjb fasttrap_probe_t *probe = parg; 1399179193Sjb int i; 1400179193Sjb size_t size; 1401179193Sjb 1402179193Sjb ASSERT(probe != NULL); 1403179193Sjb ASSERT(!probe->ftp_enabled); 1404179193Sjb ASSERT(fasttrap_total >= probe->ftp_ntps); 1405179193Sjb 1406179193Sjb atomic_add_32(&fasttrap_total, -probe->ftp_ntps); 1407179193Sjb size = offsetof(fasttrap_probe_t, ftp_tps[probe->ftp_ntps]); 1408179193Sjb 1409179193Sjb if (probe->ftp_gen + 1 >= fasttrap_mod_gen) 1410179193Sjb fasttrap_mod_barrier(probe->ftp_gen); 1411179193Sjb 1412179193Sjb for (i = 0; i < probe->ftp_ntps; i++) { 1413179193Sjb kmem_free(probe->ftp_tps[i].fit_tp, 1414179193Sjb sizeof (fasttrap_tracepoint_t)); 1415179193Sjb } 1416179193Sjb 1417179193Sjb kmem_free(probe, size); 1418179193Sjb} 1419179193Sjb 1420179193Sjb 1421179193Sjbstatic const dtrace_pattr_t pid_attr = { 1422179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1423179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1424179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1425179193Sjb{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_ISA }, 1426179193Sjb{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 1427179193Sjb}; 1428179193Sjb 1429179193Sjbstatic dtrace_pops_t pid_pops = { 1430179193Sjb fasttrap_pid_provide, 1431179193Sjb NULL, 1432179193Sjb fasttrap_pid_enable, 1433179193Sjb fasttrap_pid_disable, 1434179193Sjb NULL, 1435179193Sjb NULL, 1436179193Sjb fasttrap_pid_getargdesc, 1437179193Sjb fasttrap_pid_getarg, 1438179193Sjb NULL, 1439179193Sjb fasttrap_pid_destroy 1440179193Sjb}; 1441179193Sjb 1442179193Sjbstatic dtrace_pops_t usdt_pops = { 1443179193Sjb fasttrap_pid_provide, 1444179193Sjb NULL, 1445179193Sjb fasttrap_pid_enable, 1446179193Sjb fasttrap_pid_disable, 1447179193Sjb NULL, 1448179193Sjb NULL, 1449179193Sjb fasttrap_pid_getargdesc, 1450179193Sjb fasttrap_usdt_getarg, 1451179193Sjb NULL, 1452179193Sjb fasttrap_pid_destroy 1453179193Sjb}; 1454179193Sjb 1455179193Sjbstatic fasttrap_proc_t * 1456179193Sjbfasttrap_proc_lookup(pid_t pid) 1457179193Sjb{ 1458179193Sjb fasttrap_bucket_t *bucket; 1459179193Sjb fasttrap_proc_t *fprc, *new_fprc; 1460179193Sjb 1461211738Srpaulo 1462179193Sjb bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1463179193Sjb mutex_enter(&bucket->ftb_mtx); 1464179193Sjb 1465179193Sjb for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1466179193Sjb if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1467179193Sjb mutex_enter(&fprc->ftpc_mtx); 1468179193Sjb mutex_exit(&bucket->ftb_mtx); 1469179193Sjb fprc->ftpc_rcount++; 1470271001Sdelphij atomic_inc_64(&fprc->ftpc_acount); 1471179198Sjb ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1472179193Sjb mutex_exit(&fprc->ftpc_mtx); 1473179193Sjb 1474179193Sjb return (fprc); 1475179193Sjb } 1476179193Sjb } 1477179193Sjb 1478179193Sjb /* 1479179193Sjb * Drop the bucket lock so we don't try to perform a sleeping 1480179193Sjb * allocation under it. 1481179193Sjb */ 1482179193Sjb mutex_exit(&bucket->ftb_mtx); 1483179193Sjb 1484179193Sjb new_fprc = kmem_zalloc(sizeof (fasttrap_proc_t), KM_SLEEP); 1485179193Sjb new_fprc->ftpc_pid = pid; 1486179193Sjb new_fprc->ftpc_rcount = 1; 1487179193Sjb new_fprc->ftpc_acount = 1; 1488211738Srpaulo#if !defined(sun) 1489211738Srpaulo mutex_init(&new_fprc->ftpc_mtx, "fasttrap proc mtx", MUTEX_DEFAULT, 1490211738Srpaulo NULL); 1491211738Srpaulo#endif 1492179193Sjb 1493179193Sjb mutex_enter(&bucket->ftb_mtx); 1494179193Sjb 1495179193Sjb /* 1496179193Sjb * Take another lap through the list to make sure a proc hasn't 1497179193Sjb * been created for this pid while we weren't under the bucket lock. 1498179193Sjb */ 1499179193Sjb for (fprc = bucket->ftb_data; fprc != NULL; fprc = fprc->ftpc_next) { 1500179193Sjb if (fprc->ftpc_pid == pid && fprc->ftpc_acount != 0) { 1501179193Sjb mutex_enter(&fprc->ftpc_mtx); 1502179193Sjb mutex_exit(&bucket->ftb_mtx); 1503179193Sjb fprc->ftpc_rcount++; 1504271001Sdelphij atomic_inc_64(&fprc->ftpc_acount); 1505179198Sjb ASSERT(fprc->ftpc_acount <= fprc->ftpc_rcount); 1506179193Sjb mutex_exit(&fprc->ftpc_mtx); 1507179193Sjb 1508179193Sjb kmem_free(new_fprc, sizeof (fasttrap_proc_t)); 1509179193Sjb 1510179193Sjb return (fprc); 1511179193Sjb } 1512179193Sjb } 1513179193Sjb 1514179193Sjb new_fprc->ftpc_next = bucket->ftb_data; 1515179193Sjb bucket->ftb_data = new_fprc; 1516179193Sjb 1517179193Sjb mutex_exit(&bucket->ftb_mtx); 1518179193Sjb 1519179193Sjb return (new_fprc); 1520179193Sjb} 1521179193Sjb 1522179193Sjbstatic void 1523179193Sjbfasttrap_proc_release(fasttrap_proc_t *proc) 1524179193Sjb{ 1525179193Sjb fasttrap_bucket_t *bucket; 1526179193Sjb fasttrap_proc_t *fprc, **fprcp; 1527179193Sjb pid_t pid = proc->ftpc_pid; 1528269342Smarkj#if !defined(sun) 1529269342Smarkj fasttrap_scrblock_t *scrblk, *scrblktmp; 1530269342Smarkj fasttrap_scrspace_t *scrspc, *scrspctmp; 1531269342Smarkj struct proc *p; 1532269342Smarkj struct thread *td; 1533269342Smarkj#endif 1534179193Sjb 1535179193Sjb mutex_enter(&proc->ftpc_mtx); 1536179193Sjb 1537179193Sjb ASSERT(proc->ftpc_rcount != 0); 1538179198Sjb ASSERT(proc->ftpc_acount <= proc->ftpc_rcount); 1539179193Sjb 1540179193Sjb if (--proc->ftpc_rcount != 0) { 1541179193Sjb mutex_exit(&proc->ftpc_mtx); 1542179193Sjb return; 1543179193Sjb } 1544179193Sjb 1545269342Smarkj#if !defined(sun) 1546269342Smarkj /* 1547269342Smarkj * Free all structures used to manage per-thread scratch space. 1548269342Smarkj */ 1549269342Smarkj LIST_FOREACH_SAFE(scrblk, &proc->ftpc_scrblks, ftsb_next, 1550269342Smarkj scrblktmp) { 1551269342Smarkj LIST_REMOVE(scrblk, ftsb_next); 1552269342Smarkj free(scrblk, M_SOLARIS); 1553269342Smarkj } 1554269342Smarkj LIST_FOREACH_SAFE(scrspc, &proc->ftpc_fscr, ftss_next, scrspctmp) { 1555269342Smarkj LIST_REMOVE(scrspc, ftss_next); 1556269342Smarkj free(scrspc, M_SOLARIS); 1557269342Smarkj } 1558269342Smarkj LIST_FOREACH_SAFE(scrspc, &proc->ftpc_ascr, ftss_next, scrspctmp) { 1559269342Smarkj LIST_REMOVE(scrspc, ftss_next); 1560269342Smarkj free(scrspc, M_SOLARIS); 1561269342Smarkj } 1562269342Smarkj 1563269342Smarkj if ((p = pfind(pid)) != NULL) { 1564269342Smarkj FOREACH_THREAD_IN_PROC(p, td) 1565269342Smarkj td->t_dtrace_sscr = NULL; 1566269342Smarkj PROC_UNLOCK(p); 1567269342Smarkj } 1568269342Smarkj#endif 1569269342Smarkj 1570179193Sjb mutex_exit(&proc->ftpc_mtx); 1571179193Sjb 1572179193Sjb /* 1573179193Sjb * There should definitely be no live providers associated with this 1574179193Sjb * process at this point. 1575179193Sjb */ 1576179193Sjb ASSERT(proc->ftpc_acount == 0); 1577179193Sjb 1578179193Sjb bucket = &fasttrap_procs.fth_table[FASTTRAP_PROCS_INDEX(pid)]; 1579179193Sjb mutex_enter(&bucket->ftb_mtx); 1580179193Sjb 1581179193Sjb fprcp = (fasttrap_proc_t **)&bucket->ftb_data; 1582179193Sjb while ((fprc = *fprcp) != NULL) { 1583179193Sjb if (fprc == proc) 1584179193Sjb break; 1585179193Sjb 1586179193Sjb fprcp = &fprc->ftpc_next; 1587179193Sjb } 1588179193Sjb 1589179193Sjb /* 1590179193Sjb * Something strange has happened if we can't find the proc. 1591179193Sjb */ 1592179193Sjb ASSERT(fprc != NULL); 1593179193Sjb 1594179193Sjb *fprcp = fprc->ftpc_next; 1595179193Sjb 1596179193Sjb mutex_exit(&bucket->ftb_mtx); 1597179193Sjb 1598179193Sjb kmem_free(fprc, sizeof (fasttrap_proc_t)); 1599179193Sjb} 1600179193Sjb 1601179193Sjb/* 1602179193Sjb * Lookup a fasttrap-managed provider based on its name and associated pid. 1603179193Sjb * If the pattr argument is non-NULL, this function instantiates the provider 1604179193Sjb * if it doesn't exist otherwise it returns NULL. The provider is returned 1605179193Sjb * with its lock held. 1606179193Sjb */ 1607179193Sjbstatic fasttrap_provider_t * 1608179193Sjbfasttrap_provider_lookup(pid_t pid, const char *name, 1609179193Sjb const dtrace_pattr_t *pattr) 1610179193Sjb{ 1611179193Sjb fasttrap_provider_t *fp, *new_fp = NULL; 1612179193Sjb fasttrap_bucket_t *bucket; 1613179193Sjb char provname[DTRACE_PROVNAMELEN]; 1614179193Sjb proc_t *p; 1615179193Sjb cred_t *cred; 1616179193Sjb 1617179193Sjb ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1618179193Sjb ASSERT(pattr != NULL); 1619179193Sjb 1620179193Sjb bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1621179193Sjb mutex_enter(&bucket->ftb_mtx); 1622179193Sjb 1623179193Sjb /* 1624179193Sjb * Take a lap through the list and return the match if we find it. 1625179193Sjb */ 1626179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1627179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1628179193Sjb !fp->ftp_retired) { 1629179193Sjb mutex_enter(&fp->ftp_mtx); 1630179193Sjb mutex_exit(&bucket->ftb_mtx); 1631179193Sjb return (fp); 1632179193Sjb } 1633179193Sjb } 1634179193Sjb 1635179193Sjb /* 1636179193Sjb * Drop the bucket lock so we don't try to perform a sleeping 1637179193Sjb * allocation under it. 1638179193Sjb */ 1639179193Sjb mutex_exit(&bucket->ftb_mtx); 1640179193Sjb 1641179193Sjb /* 1642179193Sjb * Make sure the process exists, isn't a child created as the result 1643179193Sjb * of a vfork(2), and isn't a zombie (but may be in fork). 1644179193Sjb */ 1645211738Srpaulo if ((p = pfind(pid)) == NULL) 1646179193Sjb return (NULL); 1647179193Sjb 1648179193Sjb /* 1649179193Sjb * Increment p_dtrace_probes so that the process knows to inform us 1650179193Sjb * when it exits or execs. fasttrap_provider_free() decrements this 1651179193Sjb * when we're done with this provider. 1652179193Sjb */ 1653179193Sjb p->p_dtrace_probes++; 1654179193Sjb 1655179193Sjb /* 1656179193Sjb * Grab the credentials for this process so we have 1657179193Sjb * something to pass to dtrace_register(). 1658179193Sjb */ 1659211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 1660211738Srpaulo crhold(p->p_ucred); 1661211738Srpaulo cred = p->p_ucred; 1662211738Srpaulo PROC_UNLOCK(p); 1663179193Sjb 1664179193Sjb new_fp = kmem_zalloc(sizeof (fasttrap_provider_t), KM_SLEEP); 1665179193Sjb new_fp->ftp_pid = pid; 1666179193Sjb new_fp->ftp_proc = fasttrap_proc_lookup(pid); 1667211738Srpaulo#if !defined(sun) 1668211738Srpaulo mutex_init(&new_fp->ftp_mtx, "provider mtx", MUTEX_DEFAULT, NULL); 1669211738Srpaulo mutex_init(&new_fp->ftp_cmtx, "lock on creating", MUTEX_DEFAULT, NULL); 1670211738Srpaulo#endif 1671179193Sjb 1672179193Sjb ASSERT(new_fp->ftp_proc != NULL); 1673179193Sjb 1674179193Sjb mutex_enter(&bucket->ftb_mtx); 1675179193Sjb 1676179193Sjb /* 1677179193Sjb * Take another lap through the list to make sure a provider hasn't 1678179193Sjb * been created for this pid while we weren't under the bucket lock. 1679179193Sjb */ 1680179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1681179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1682179193Sjb !fp->ftp_retired) { 1683179193Sjb mutex_enter(&fp->ftp_mtx); 1684179193Sjb mutex_exit(&bucket->ftb_mtx); 1685179193Sjb fasttrap_provider_free(new_fp); 1686179193Sjb crfree(cred); 1687179193Sjb return (fp); 1688179193Sjb } 1689179193Sjb } 1690179193Sjb 1691179193Sjb (void) strcpy(new_fp->ftp_name, name); 1692179193Sjb 1693179193Sjb /* 1694179193Sjb * Fail and return NULL if either the provider name is too long 1695179193Sjb * or we fail to register this new provider with the DTrace 1696179193Sjb * framework. Note that this is the only place we ever construct 1697179193Sjb * the full provider name -- we keep it in pieces in the provider 1698179193Sjb * structure. 1699179193Sjb */ 1700179193Sjb if (snprintf(provname, sizeof (provname), "%s%u", name, (uint_t)pid) >= 1701179193Sjb sizeof (provname) || 1702179193Sjb dtrace_register(provname, pattr, 1703179193Sjb DTRACE_PRIV_PROC | DTRACE_PRIV_OWNER | DTRACE_PRIV_ZONEOWNER, cred, 1704179193Sjb pattr == &pid_attr ? &pid_pops : &usdt_pops, new_fp, 1705179193Sjb &new_fp->ftp_provid) != 0) { 1706179193Sjb mutex_exit(&bucket->ftb_mtx); 1707179193Sjb fasttrap_provider_free(new_fp); 1708179193Sjb crfree(cred); 1709179193Sjb return (NULL); 1710179193Sjb } 1711179193Sjb 1712179193Sjb new_fp->ftp_next = bucket->ftb_data; 1713179193Sjb bucket->ftb_data = new_fp; 1714179193Sjb 1715179193Sjb mutex_enter(&new_fp->ftp_mtx); 1716179193Sjb mutex_exit(&bucket->ftb_mtx); 1717179193Sjb 1718179193Sjb crfree(cred); 1719179193Sjb return (new_fp); 1720179193Sjb} 1721179193Sjb 1722179193Sjbstatic void 1723179193Sjbfasttrap_provider_free(fasttrap_provider_t *provider) 1724179193Sjb{ 1725179193Sjb pid_t pid = provider->ftp_pid; 1726179193Sjb proc_t *p; 1727179193Sjb 1728179193Sjb /* 1729179193Sjb * There need to be no associated enabled probes, no consumers 1730179193Sjb * creating probes, and no meta providers referencing this provider. 1731179193Sjb */ 1732179193Sjb ASSERT(provider->ftp_rcount == 0); 1733179193Sjb ASSERT(provider->ftp_ccount == 0); 1734179193Sjb ASSERT(provider->ftp_mcount == 0); 1735179193Sjb 1736179198Sjb /* 1737179198Sjb * If this provider hasn't been retired, we need to explicitly drop the 1738179198Sjb * count of active providers on the associated process structure. 1739179198Sjb */ 1740179198Sjb if (!provider->ftp_retired) { 1741271001Sdelphij atomic_dec_64(&provider->ftp_proc->ftpc_acount); 1742179198Sjb ASSERT(provider->ftp_proc->ftpc_acount < 1743179198Sjb provider->ftp_proc->ftpc_rcount); 1744179198Sjb } 1745179198Sjb 1746179193Sjb fasttrap_proc_release(provider->ftp_proc); 1747179193Sjb 1748211738Srpaulo#if !defined(sun) 1749211738Srpaulo mutex_destroy(&provider->ftp_mtx); 1750211738Srpaulo mutex_destroy(&provider->ftp_cmtx); 1751211738Srpaulo#endif 1752179193Sjb kmem_free(provider, sizeof (fasttrap_provider_t)); 1753179193Sjb 1754179193Sjb /* 1755179193Sjb * Decrement p_dtrace_probes on the process whose provider we're 1756179193Sjb * freeing. We don't have to worry about clobbering somone else's 1757179193Sjb * modifications to it because we have locked the bucket that 1758179193Sjb * corresponds to this process's hash chain in the provider hash 1759179193Sjb * table. Don't sweat it if we can't find the process. 1760179193Sjb */ 1761211738Srpaulo if ((p = pfind(pid)) == NULL) { 1762179193Sjb return; 1763179193Sjb } 1764179193Sjb 1765179193Sjb p->p_dtrace_probes--; 1766211738Srpaulo#if !defined(sun) 1767211738Srpaulo PROC_UNLOCK(p); 1768211738Srpaulo#endif 1769179193Sjb} 1770179193Sjb 1771179193Sjbstatic void 1772179193Sjbfasttrap_provider_retire(pid_t pid, const char *name, int mprov) 1773179193Sjb{ 1774179193Sjb fasttrap_provider_t *fp; 1775179193Sjb fasttrap_bucket_t *bucket; 1776179193Sjb dtrace_provider_id_t provid; 1777179193Sjb 1778179193Sjb ASSERT(strlen(name) < sizeof (fp->ftp_name)); 1779179193Sjb 1780179193Sjb bucket = &fasttrap_provs.fth_table[FASTTRAP_PROVS_INDEX(pid, name)]; 1781179193Sjb mutex_enter(&bucket->ftb_mtx); 1782179193Sjb 1783179193Sjb for (fp = bucket->ftb_data; fp != NULL; fp = fp->ftp_next) { 1784179193Sjb if (fp->ftp_pid == pid && strcmp(fp->ftp_name, name) == 0 && 1785179193Sjb !fp->ftp_retired) 1786179193Sjb break; 1787179193Sjb } 1788179193Sjb 1789179193Sjb if (fp == NULL) { 1790179193Sjb mutex_exit(&bucket->ftb_mtx); 1791179193Sjb return; 1792179193Sjb } 1793179193Sjb 1794179193Sjb mutex_enter(&fp->ftp_mtx); 1795179193Sjb ASSERT(!mprov || fp->ftp_mcount > 0); 1796179193Sjb if (mprov && --fp->ftp_mcount != 0) { 1797179193Sjb mutex_exit(&fp->ftp_mtx); 1798179193Sjb mutex_exit(&bucket->ftb_mtx); 1799179193Sjb return; 1800179193Sjb } 1801179193Sjb 1802179193Sjb /* 1803179193Sjb * Mark the provider to be removed in our post-processing step, mark it 1804179193Sjb * retired, and drop the active count on its proc. Marking it indicates 1805179193Sjb * that we should try to remove it; setting the retired flag indicates 1806179193Sjb * that we're done with this provider; dropping the active the proc 1807179193Sjb * releases our hold, and when this reaches zero (as it will during 1808179193Sjb * exit or exec) the proc and associated providers become defunct. 1809179193Sjb * 1810179193Sjb * We obviously need to take the bucket lock before the provider lock 1811179193Sjb * to perform the lookup, but we need to drop the provider lock 1812179193Sjb * before calling into the DTrace framework since we acquire the 1813179193Sjb * provider lock in callbacks invoked from the DTrace framework. The 1814179193Sjb * bucket lock therefore protects the integrity of the provider hash 1815179193Sjb * table. 1816179193Sjb */ 1817271001Sdelphij atomic_dec_64(&fp->ftp_proc->ftpc_acount); 1818179198Sjb ASSERT(fp->ftp_proc->ftpc_acount < fp->ftp_proc->ftpc_rcount); 1819179198Sjb 1820179193Sjb fp->ftp_retired = 1; 1821179193Sjb fp->ftp_marked = 1; 1822179193Sjb provid = fp->ftp_provid; 1823179193Sjb mutex_exit(&fp->ftp_mtx); 1824179193Sjb 1825179193Sjb /* 1826179193Sjb * We don't have to worry about invalidating the same provider twice 1827179193Sjb * since fasttrap_provider_lookup() will ignore provider that have 1828179193Sjb * been marked as retired. 1829179193Sjb */ 1830179193Sjb dtrace_invalidate(provid); 1831179193Sjb 1832179193Sjb mutex_exit(&bucket->ftb_mtx); 1833179193Sjb 1834179193Sjb fasttrap_pid_cleanup(); 1835179193Sjb} 1836179193Sjb 1837179193Sjbstatic int 1838179193Sjbfasttrap_uint32_cmp(const void *ap, const void *bp) 1839179193Sjb{ 1840179193Sjb return (*(const uint32_t *)ap - *(const uint32_t *)bp); 1841179193Sjb} 1842179193Sjb 1843179193Sjbstatic int 1844179193Sjbfasttrap_uint64_cmp(const void *ap, const void *bp) 1845179193Sjb{ 1846179193Sjb return (*(const uint64_t *)ap - *(const uint64_t *)bp); 1847179193Sjb} 1848179193Sjb 1849179193Sjbstatic int 1850179193Sjbfasttrap_add_probe(fasttrap_probe_spec_t *pdata) 1851179193Sjb{ 1852179193Sjb fasttrap_provider_t *provider; 1853179193Sjb fasttrap_probe_t *pp; 1854179193Sjb fasttrap_tracepoint_t *tp; 1855179193Sjb char *name; 1856211738Srpaulo int i, aframes = 0, whack; 1857179193Sjb 1858179193Sjb /* 1859179193Sjb * There needs to be at least one desired trace point. 1860179193Sjb */ 1861179193Sjb if (pdata->ftps_noffs == 0) 1862179193Sjb return (EINVAL); 1863179193Sjb 1864179193Sjb switch (pdata->ftps_type) { 1865179193Sjb case DTFTP_ENTRY: 1866179193Sjb name = "entry"; 1867179193Sjb aframes = FASTTRAP_ENTRY_AFRAMES; 1868179193Sjb break; 1869179193Sjb case DTFTP_RETURN: 1870179193Sjb name = "return"; 1871179193Sjb aframes = FASTTRAP_RETURN_AFRAMES; 1872179193Sjb break; 1873179193Sjb case DTFTP_OFFSETS: 1874179193Sjb name = NULL; 1875179193Sjb break; 1876179193Sjb default: 1877179193Sjb return (EINVAL); 1878179193Sjb } 1879179193Sjb 1880179193Sjb if ((provider = fasttrap_provider_lookup(pdata->ftps_pid, 1881179193Sjb FASTTRAP_PID_NAME, &pid_attr)) == NULL) 1882179193Sjb return (ESRCH); 1883179193Sjb 1884179193Sjb /* 1885179193Sjb * Increment this reference count to indicate that a consumer is 1886179193Sjb * actively adding a new probe associated with this provider. This 1887179193Sjb * prevents the provider from being deleted -- we'll need to check 1888179193Sjb * for pending deletions when we drop this reference count. 1889179193Sjb */ 1890179193Sjb provider->ftp_ccount++; 1891179193Sjb mutex_exit(&provider->ftp_mtx); 1892179193Sjb 1893179193Sjb /* 1894179193Sjb * Grab the creation lock to ensure consistency between calls to 1895179193Sjb * dtrace_probe_lookup() and dtrace_probe_create() in the face of 1896179193Sjb * other threads creating probes. We must drop the provider lock 1897179193Sjb * before taking this lock to avoid a three-way deadlock with the 1898179193Sjb * DTrace framework. 1899179193Sjb */ 1900179193Sjb mutex_enter(&provider->ftp_cmtx); 1901179193Sjb 1902179193Sjb if (name == NULL) { 1903179193Sjb for (i = 0; i < pdata->ftps_noffs; i++) { 1904179193Sjb char name_str[17]; 1905179193Sjb 1906179193Sjb (void) sprintf(name_str, "%llx", 1907179193Sjb (unsigned long long)pdata->ftps_offs[i]); 1908179193Sjb 1909179193Sjb if (dtrace_probe_lookup(provider->ftp_provid, 1910179193Sjb pdata->ftps_mod, pdata->ftps_func, name_str) != 0) 1911179193Sjb continue; 1912179193Sjb 1913271001Sdelphij atomic_inc_32(&fasttrap_total); 1914179193Sjb 1915179193Sjb if (fasttrap_total > fasttrap_max) { 1916271001Sdelphij atomic_dec_32(&fasttrap_total); 1917179193Sjb goto no_mem; 1918179193Sjb } 1919179193Sjb 1920179193Sjb pp = kmem_zalloc(sizeof (fasttrap_probe_t), KM_SLEEP); 1921179193Sjb 1922179193Sjb pp->ftp_prov = provider; 1923179193Sjb pp->ftp_faddr = pdata->ftps_pc; 1924179193Sjb pp->ftp_fsize = pdata->ftps_size; 1925179193Sjb pp->ftp_pid = pdata->ftps_pid; 1926179193Sjb pp->ftp_ntps = 1; 1927179193Sjb 1928179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1929179193Sjb KM_SLEEP); 1930179193Sjb 1931179193Sjb tp->ftt_proc = provider->ftp_proc; 1932179193Sjb tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1933179193Sjb tp->ftt_pid = pdata->ftps_pid; 1934179193Sjb 1935179193Sjb pp->ftp_tps[0].fit_tp = tp; 1936179193Sjb pp->ftp_tps[0].fit_id.fti_probe = pp; 1937179193Sjb pp->ftp_tps[0].fit_id.fti_ptype = pdata->ftps_type; 1938179193Sjb 1939179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1940179193Sjb pdata->ftps_mod, pdata->ftps_func, name_str, 1941179193Sjb FASTTRAP_OFFSET_AFRAMES, pp); 1942179193Sjb } 1943179193Sjb 1944179193Sjb } else if (dtrace_probe_lookup(provider->ftp_provid, pdata->ftps_mod, 1945179193Sjb pdata->ftps_func, name) == 0) { 1946179193Sjb atomic_add_32(&fasttrap_total, pdata->ftps_noffs); 1947179193Sjb 1948179193Sjb if (fasttrap_total > fasttrap_max) { 1949179193Sjb atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1950179193Sjb goto no_mem; 1951179193Sjb } 1952179193Sjb 1953179193Sjb /* 1954179193Sjb * Make sure all tracepoint program counter values are unique. 1955179193Sjb * We later assume that each probe has exactly one tracepoint 1956179193Sjb * for a given pc. 1957179193Sjb */ 1958179193Sjb qsort(pdata->ftps_offs, pdata->ftps_noffs, 1959179193Sjb sizeof (uint64_t), fasttrap_uint64_cmp); 1960179193Sjb for (i = 1; i < pdata->ftps_noffs; i++) { 1961179193Sjb if (pdata->ftps_offs[i] > pdata->ftps_offs[i - 1]) 1962179193Sjb continue; 1963179193Sjb 1964179193Sjb atomic_add_32(&fasttrap_total, -pdata->ftps_noffs); 1965179193Sjb goto no_mem; 1966179193Sjb } 1967179193Sjb 1968179193Sjb ASSERT(pdata->ftps_noffs > 0); 1969179193Sjb pp = kmem_zalloc(offsetof(fasttrap_probe_t, 1970179193Sjb ftp_tps[pdata->ftps_noffs]), KM_SLEEP); 1971179193Sjb 1972179193Sjb pp->ftp_prov = provider; 1973179193Sjb pp->ftp_faddr = pdata->ftps_pc; 1974179193Sjb pp->ftp_fsize = pdata->ftps_size; 1975179193Sjb pp->ftp_pid = pdata->ftps_pid; 1976179193Sjb pp->ftp_ntps = pdata->ftps_noffs; 1977179193Sjb 1978179193Sjb for (i = 0; i < pdata->ftps_noffs; i++) { 1979179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), 1980179193Sjb KM_SLEEP); 1981179193Sjb 1982179193Sjb tp->ftt_proc = provider->ftp_proc; 1983179193Sjb tp->ftt_pc = pdata->ftps_offs[i] + pdata->ftps_pc; 1984179193Sjb tp->ftt_pid = pdata->ftps_pid; 1985179193Sjb 1986179193Sjb pp->ftp_tps[i].fit_tp = tp; 1987179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 1988179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = pdata->ftps_type; 1989179193Sjb } 1990179193Sjb 1991179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, 1992179193Sjb pdata->ftps_mod, pdata->ftps_func, name, aframes, pp); 1993179193Sjb } 1994179193Sjb 1995179193Sjb mutex_exit(&provider->ftp_cmtx); 1996179193Sjb 1997179193Sjb /* 1998179193Sjb * We know that the provider is still valid since we incremented the 1999179193Sjb * creation reference count. If someone tried to clean up this provider 2000179193Sjb * while we were using it (e.g. because the process called exec(2) or 2001179193Sjb * exit(2)), take note of that and try to clean it up now. 2002179193Sjb */ 2003179193Sjb mutex_enter(&provider->ftp_mtx); 2004179193Sjb provider->ftp_ccount--; 2005179193Sjb whack = provider->ftp_retired; 2006179193Sjb mutex_exit(&provider->ftp_mtx); 2007179193Sjb 2008179193Sjb if (whack) 2009179193Sjb fasttrap_pid_cleanup(); 2010179193Sjb 2011179193Sjb return (0); 2012179193Sjb 2013179193Sjbno_mem: 2014179193Sjb /* 2015179193Sjb * If we've exhausted the allowable resources, we'll try to remove 2016179193Sjb * this provider to free some up. This is to cover the case where 2017179193Sjb * the user has accidentally created many more probes than was 2018179193Sjb * intended (e.g. pid123:::). 2019179193Sjb */ 2020179193Sjb mutex_exit(&provider->ftp_cmtx); 2021179193Sjb mutex_enter(&provider->ftp_mtx); 2022179193Sjb provider->ftp_ccount--; 2023179193Sjb provider->ftp_marked = 1; 2024179193Sjb mutex_exit(&provider->ftp_mtx); 2025179193Sjb 2026179193Sjb fasttrap_pid_cleanup(); 2027179193Sjb 2028179193Sjb return (ENOMEM); 2029179193Sjb} 2030179193Sjb 2031179193Sjb/*ARGSUSED*/ 2032179193Sjbstatic void * 2033179193Sjbfasttrap_meta_provide(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 2034179193Sjb{ 2035179193Sjb fasttrap_provider_t *provider; 2036179193Sjb 2037179193Sjb /* 2038179193Sjb * A 32-bit unsigned integer (like a pid for example) can be 2039179193Sjb * expressed in 10 or fewer decimal digits. Make sure that we'll 2040179193Sjb * have enough space for the provider name. 2041179193Sjb */ 2042179193Sjb if (strlen(dhpv->dthpv_provname) + 10 >= 2043179193Sjb sizeof (provider->ftp_name)) { 2044211738Srpaulo printf("failed to instantiate provider %s: " 2045179193Sjb "name too long to accomodate pid", dhpv->dthpv_provname); 2046179193Sjb return (NULL); 2047179193Sjb } 2048179193Sjb 2049179193Sjb /* 2050179193Sjb * Don't let folks spoof the true pid provider. 2051179193Sjb */ 2052179193Sjb if (strcmp(dhpv->dthpv_provname, FASTTRAP_PID_NAME) == 0) { 2053211738Srpaulo printf("failed to instantiate provider %s: " 2054179193Sjb "%s is an invalid name", dhpv->dthpv_provname, 2055179193Sjb FASTTRAP_PID_NAME); 2056179193Sjb return (NULL); 2057179193Sjb } 2058179193Sjb 2059179193Sjb /* 2060179193Sjb * The highest stability class that fasttrap supports is ISA; cap 2061179193Sjb * the stability of the new provider accordingly. 2062179193Sjb */ 2063179193Sjb if (dhpv->dthpv_pattr.dtpa_provider.dtat_class > DTRACE_CLASS_ISA) 2064179193Sjb dhpv->dthpv_pattr.dtpa_provider.dtat_class = DTRACE_CLASS_ISA; 2065179193Sjb if (dhpv->dthpv_pattr.dtpa_mod.dtat_class > DTRACE_CLASS_ISA) 2066179193Sjb dhpv->dthpv_pattr.dtpa_mod.dtat_class = DTRACE_CLASS_ISA; 2067179193Sjb if (dhpv->dthpv_pattr.dtpa_func.dtat_class > DTRACE_CLASS_ISA) 2068179193Sjb dhpv->dthpv_pattr.dtpa_func.dtat_class = DTRACE_CLASS_ISA; 2069179193Sjb if (dhpv->dthpv_pattr.dtpa_name.dtat_class > DTRACE_CLASS_ISA) 2070179193Sjb dhpv->dthpv_pattr.dtpa_name.dtat_class = DTRACE_CLASS_ISA; 2071179193Sjb if (dhpv->dthpv_pattr.dtpa_args.dtat_class > DTRACE_CLASS_ISA) 2072179193Sjb dhpv->dthpv_pattr.dtpa_args.dtat_class = DTRACE_CLASS_ISA; 2073179193Sjb 2074179193Sjb if ((provider = fasttrap_provider_lookup(pid, dhpv->dthpv_provname, 2075179193Sjb &dhpv->dthpv_pattr)) == NULL) { 2076211738Srpaulo printf("failed to instantiate provider %s for " 2077179193Sjb "process %u", dhpv->dthpv_provname, (uint_t)pid); 2078179193Sjb return (NULL); 2079179193Sjb } 2080179193Sjb 2081179193Sjb /* 2082179193Sjb * Up the meta provider count so this provider isn't removed until 2083179193Sjb * the meta provider has been told to remove it. 2084179193Sjb */ 2085179193Sjb provider->ftp_mcount++; 2086179193Sjb 2087179193Sjb mutex_exit(&provider->ftp_mtx); 2088179193Sjb 2089179193Sjb return (provider); 2090179193Sjb} 2091179193Sjb 2092179193Sjb/*ARGSUSED*/ 2093179193Sjbstatic void 2094179193Sjbfasttrap_meta_create_probe(void *arg, void *parg, 2095179193Sjb dtrace_helper_probedesc_t *dhpb) 2096179193Sjb{ 2097179193Sjb fasttrap_provider_t *provider = parg; 2098179193Sjb fasttrap_probe_t *pp; 2099179193Sjb fasttrap_tracepoint_t *tp; 2100179193Sjb int i, j; 2101179193Sjb uint32_t ntps; 2102179193Sjb 2103179193Sjb /* 2104179193Sjb * Since the meta provider count is non-zero we don't have to worry 2105179193Sjb * about this provider disappearing. 2106179193Sjb */ 2107179193Sjb ASSERT(provider->ftp_mcount > 0); 2108179193Sjb 2109179193Sjb /* 2110179193Sjb * The offsets must be unique. 2111179193Sjb */ 2112179193Sjb qsort(dhpb->dthpb_offs, dhpb->dthpb_noffs, sizeof (uint32_t), 2113179193Sjb fasttrap_uint32_cmp); 2114179193Sjb for (i = 1; i < dhpb->dthpb_noffs; i++) { 2115179193Sjb if (dhpb->dthpb_base + dhpb->dthpb_offs[i] <= 2116179193Sjb dhpb->dthpb_base + dhpb->dthpb_offs[i - 1]) 2117179193Sjb return; 2118179193Sjb } 2119179193Sjb 2120179193Sjb qsort(dhpb->dthpb_enoffs, dhpb->dthpb_nenoffs, sizeof (uint32_t), 2121179193Sjb fasttrap_uint32_cmp); 2122179193Sjb for (i = 1; i < dhpb->dthpb_nenoffs; i++) { 2123179193Sjb if (dhpb->dthpb_base + dhpb->dthpb_enoffs[i] <= 2124179193Sjb dhpb->dthpb_base + dhpb->dthpb_enoffs[i - 1]) 2125179193Sjb return; 2126179193Sjb } 2127179193Sjb 2128179193Sjb /* 2129179193Sjb * Grab the creation lock to ensure consistency between calls to 2130179193Sjb * dtrace_probe_lookup() and dtrace_probe_create() in the face of 2131179193Sjb * other threads creating probes. 2132179193Sjb */ 2133179193Sjb mutex_enter(&provider->ftp_cmtx); 2134179193Sjb 2135179193Sjb if (dtrace_probe_lookup(provider->ftp_provid, dhpb->dthpb_mod, 2136179193Sjb dhpb->dthpb_func, dhpb->dthpb_name) != 0) { 2137179193Sjb mutex_exit(&provider->ftp_cmtx); 2138179193Sjb return; 2139179193Sjb } 2140179193Sjb 2141179193Sjb ntps = dhpb->dthpb_noffs + dhpb->dthpb_nenoffs; 2142179193Sjb ASSERT(ntps > 0); 2143179193Sjb 2144179193Sjb atomic_add_32(&fasttrap_total, ntps); 2145179193Sjb 2146179193Sjb if (fasttrap_total > fasttrap_max) { 2147179193Sjb atomic_add_32(&fasttrap_total, -ntps); 2148179193Sjb mutex_exit(&provider->ftp_cmtx); 2149179193Sjb return; 2150179193Sjb } 2151179193Sjb 2152179193Sjb pp = kmem_zalloc(offsetof(fasttrap_probe_t, ftp_tps[ntps]), KM_SLEEP); 2153179193Sjb 2154179193Sjb pp->ftp_prov = provider; 2155179193Sjb pp->ftp_pid = provider->ftp_pid; 2156179193Sjb pp->ftp_ntps = ntps; 2157179193Sjb pp->ftp_nargs = dhpb->dthpb_xargc; 2158179193Sjb pp->ftp_xtypes = dhpb->dthpb_xtypes; 2159179193Sjb pp->ftp_ntypes = dhpb->dthpb_ntypes; 2160179193Sjb 2161179193Sjb /* 2162179193Sjb * First create a tracepoint for each actual point of interest. 2163179193Sjb */ 2164179193Sjb for (i = 0; i < dhpb->dthpb_noffs; i++) { 2165179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 2166179193Sjb 2167179193Sjb tp->ftt_proc = provider->ftp_proc; 2168179193Sjb tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_offs[i]; 2169179193Sjb tp->ftt_pid = provider->ftp_pid; 2170179193Sjb 2171179193Sjb pp->ftp_tps[i].fit_tp = tp; 2172179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 2173179193Sjb#ifdef __sparc 2174179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_POST_OFFSETS; 2175179193Sjb#else 2176179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_OFFSETS; 2177179193Sjb#endif 2178179193Sjb } 2179179193Sjb 2180179193Sjb /* 2181179193Sjb * Then create a tracepoint for each is-enabled point. 2182179193Sjb */ 2183179193Sjb for (j = 0; i < ntps; i++, j++) { 2184179193Sjb tp = kmem_zalloc(sizeof (fasttrap_tracepoint_t), KM_SLEEP); 2185179193Sjb 2186179193Sjb tp->ftt_proc = provider->ftp_proc; 2187179193Sjb tp->ftt_pc = dhpb->dthpb_base + dhpb->dthpb_enoffs[j]; 2188179193Sjb tp->ftt_pid = provider->ftp_pid; 2189179193Sjb 2190179193Sjb pp->ftp_tps[i].fit_tp = tp; 2191179193Sjb pp->ftp_tps[i].fit_id.fti_probe = pp; 2192179193Sjb pp->ftp_tps[i].fit_id.fti_ptype = DTFTP_IS_ENABLED; 2193179193Sjb } 2194179193Sjb 2195179193Sjb /* 2196179193Sjb * If the arguments are shuffled around we set the argument remapping 2197179193Sjb * table. Later, when the probe fires, we only remap the arguments 2198179193Sjb * if the table is non-NULL. 2199179193Sjb */ 2200179193Sjb for (i = 0; i < dhpb->dthpb_xargc; i++) { 2201179193Sjb if (dhpb->dthpb_args[i] != i) { 2202179193Sjb pp->ftp_argmap = dhpb->dthpb_args; 2203179193Sjb break; 2204179193Sjb } 2205179193Sjb } 2206179193Sjb 2207179193Sjb /* 2208179193Sjb * The probe is fully constructed -- register it with DTrace. 2209179193Sjb */ 2210179193Sjb pp->ftp_id = dtrace_probe_create(provider->ftp_provid, dhpb->dthpb_mod, 2211179193Sjb dhpb->dthpb_func, dhpb->dthpb_name, FASTTRAP_OFFSET_AFRAMES, pp); 2212179193Sjb 2213179193Sjb mutex_exit(&provider->ftp_cmtx); 2214179193Sjb} 2215179193Sjb 2216179193Sjb/*ARGSUSED*/ 2217179193Sjbstatic void 2218179193Sjbfasttrap_meta_remove(void *arg, dtrace_helper_provdesc_t *dhpv, pid_t pid) 2219179193Sjb{ 2220179193Sjb /* 2221179193Sjb * Clean up the USDT provider. There may be active consumers of the 2222179193Sjb * provider busy adding probes, no damage will actually befall the 2223179193Sjb * provider until that count has dropped to zero. This just puts 2224179193Sjb * the provider on death row. 2225179193Sjb */ 2226179193Sjb fasttrap_provider_retire(pid, dhpv->dthpv_provname, 1); 2227179193Sjb} 2228179193Sjb 2229179193Sjbstatic dtrace_mops_t fasttrap_mops = { 2230179193Sjb fasttrap_meta_create_probe, 2231179193Sjb fasttrap_meta_provide, 2232179193Sjb fasttrap_meta_remove 2233179193Sjb}; 2234179193Sjb 2235179193Sjb/*ARGSUSED*/ 2236179193Sjbstatic int 2237211738Srpaulofasttrap_open(struct cdev *dev __unused, int oflags __unused, 2238211738Srpaulo int devtype __unused, struct thread *td __unused) 2239179193Sjb{ 2240179193Sjb return (0); 2241179193Sjb} 2242179193Sjb 2243179193Sjb/*ARGSUSED*/ 2244179193Sjbstatic int 2245211738Srpaulofasttrap_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int fflag, 2246211738Srpaulo struct thread *td) 2247179193Sjb{ 2248211738Srpaulo#ifdef notyet 2249211738Srpaulo struct kinfo_proc kp; 2250211738Srpaulo const cred_t *cr = td->td_ucred; 2251211738Srpaulo#endif 2252179193Sjb if (!dtrace_attached()) 2253179193Sjb return (EAGAIN); 2254179193Sjb 2255179193Sjb if (cmd == FASTTRAPIOC_MAKEPROBE) { 2256262048Savg fasttrap_probe_spec_t *uprobe = *(fasttrap_probe_spec_t **)arg; 2257179193Sjb fasttrap_probe_spec_t *probe; 2258179193Sjb uint64_t noffs; 2259179193Sjb size_t size; 2260268734Spfg int ret, err; 2261179193Sjb 2262179193Sjb if (copyin(&uprobe->ftps_noffs, &noffs, 2263179193Sjb sizeof (uprobe->ftps_noffs))) 2264179193Sjb return (EFAULT); 2265179193Sjb 2266179193Sjb /* 2267179193Sjb * Probes must have at least one tracepoint. 2268179193Sjb */ 2269179193Sjb if (noffs == 0) 2270179193Sjb return (EINVAL); 2271179193Sjb 2272179193Sjb size = sizeof (fasttrap_probe_spec_t) + 2273179193Sjb sizeof (probe->ftps_offs[0]) * (noffs - 1); 2274179193Sjb 2275179193Sjb if (size > 1024 * 1024) 2276179193Sjb return (ENOMEM); 2277179193Sjb 2278179193Sjb probe = kmem_alloc(size, KM_SLEEP); 2279179193Sjb 2280268572Spfg if (copyin(uprobe, probe, size) != 0 || 2281268572Spfg probe->ftps_noffs != noffs) { 2282179193Sjb kmem_free(probe, size); 2283179193Sjb return (EFAULT); 2284179193Sjb } 2285179193Sjb 2286179193Sjb /* 2287179193Sjb * Verify that the function and module strings contain no 2288179193Sjb * funny characters. 2289179193Sjb */ 2290268734Spfg if (u8_validate(probe->ftps_func, strlen(probe->ftps_func), 2291268734Spfg NULL, U8_VALIDATE_ENTIRE, &err) < 0) { 2292268734Spfg ret = EINVAL; 2293268734Spfg goto err; 2294179193Sjb } 2295179193Sjb 2296268734Spfg if (u8_validate(probe->ftps_mod, strlen(probe->ftps_mod), 2297268734Spfg NULL, U8_VALIDATE_ENTIRE, &err) < 0) { 2298268734Spfg ret = EINVAL; 2299268734Spfg goto err; 2300179193Sjb } 2301179193Sjb 2302211738Srpaulo#ifdef notyet 2303179193Sjb if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2304179193Sjb proc_t *p; 2305179193Sjb pid_t pid = probe->ftps_pid; 2306179193Sjb 2307211738Srpaulo#if defined(sun) 2308179193Sjb mutex_enter(&pidlock); 2309211738Srpaulo#endif 2310179193Sjb /* 2311179193Sjb * Report an error if the process doesn't exist 2312179193Sjb * or is actively being birthed. 2313179193Sjb */ 2314211738Srpaulo p = pfind(pid); 2315211738Srpaulo if (p) 2316211738Srpaulo fill_kinfo_proc(p, &kp); 2317211738Srpaulo if (p == NULL || kp.ki_stat == SIDL) { 2318211738Srpaulo#if defined(sun) 2319179193Sjb mutex_exit(&pidlock); 2320211738Srpaulo#endif 2321179193Sjb return (ESRCH); 2322179193Sjb } 2323211738Srpaulo#if defined(sun) 2324179193Sjb mutex_enter(&p->p_lock); 2325179193Sjb mutex_exit(&pidlock); 2326211738Srpaulo#else 2327211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 2328211738Srpaulo#endif 2329179193Sjb 2330211738Srpaulo#ifdef notyet 2331179193Sjb if ((ret = priv_proc_cred_perm(cr, p, NULL, 2332179193Sjb VREAD | VWRITE)) != 0) { 2333211738Srpaulo#if defined(sun) 2334179193Sjb mutex_exit(&p->p_lock); 2335211738Srpaulo#else 2336211738Srpaulo PROC_UNLOCK(p); 2337211738Srpaulo#endif 2338179193Sjb return (ret); 2339179193Sjb } 2340211738Srpaulo#endif /* notyet */ 2341211738Srpaulo#if defined(sun) 2342179193Sjb mutex_exit(&p->p_lock); 2343211738Srpaulo#else 2344211738Srpaulo PROC_UNLOCK(p); 2345211738Srpaulo#endif 2346179193Sjb } 2347211738Srpaulo#endif /* notyet */ 2348179193Sjb 2349179193Sjb ret = fasttrap_add_probe(probe); 2350179193Sjberr: 2351179193Sjb kmem_free(probe, size); 2352179193Sjb 2353179193Sjb return (ret); 2354179193Sjb 2355179193Sjb } else if (cmd == FASTTRAPIOC_GETINSTR) { 2356179193Sjb fasttrap_instr_query_t instr; 2357179193Sjb fasttrap_tracepoint_t *tp; 2358179193Sjb uint_t index; 2359211738Srpaulo#if defined(sun) 2360179193Sjb int ret; 2361211738Srpaulo#endif 2362179193Sjb 2363211738Srpaulo#if defined(sun) 2364179193Sjb if (copyin((void *)arg, &instr, sizeof (instr)) != 0) 2365179193Sjb return (EFAULT); 2366211738Srpaulo#endif 2367179193Sjb 2368211738Srpaulo#ifdef notyet 2369179193Sjb if (!PRIV_POLICY_CHOICE(cr, PRIV_ALL, B_FALSE)) { 2370179193Sjb proc_t *p; 2371179193Sjb pid_t pid = instr.ftiq_pid; 2372179193Sjb 2373211738Srpaulo#if defined(sun) 2374179193Sjb mutex_enter(&pidlock); 2375211738Srpaulo#endif 2376179193Sjb /* 2377179193Sjb * Report an error if the process doesn't exist 2378179193Sjb * or is actively being birthed. 2379179193Sjb */ 2380211738Srpaulo p = pfind(pid); 2381211738Srpaulo if (p) 2382211738Srpaulo fill_kinfo_proc(p, &kp); 2383211738Srpaulo if (p == NULL || kp.ki_stat == SIDL) { 2384211738Srpaulo#if defined(sun) 2385179193Sjb mutex_exit(&pidlock); 2386211738Srpaulo#endif 2387179193Sjb return (ESRCH); 2388179193Sjb } 2389211738Srpaulo#if defined(sun) 2390179193Sjb mutex_enter(&p->p_lock); 2391179193Sjb mutex_exit(&pidlock); 2392211738Srpaulo#else 2393211738Srpaulo PROC_LOCK_ASSERT(p, MA_OWNED); 2394211738Srpaulo#endif 2395179193Sjb 2396211738Srpaulo#ifdef notyet 2397179193Sjb if ((ret = priv_proc_cred_perm(cr, p, NULL, 2398179193Sjb VREAD)) != 0) { 2399211738Srpaulo#if defined(sun) 2400179193Sjb mutex_exit(&p->p_lock); 2401211738Srpaulo#else 2402211738Srpaulo PROC_UNLOCK(p); 2403211738Srpaulo#endif 2404179193Sjb return (ret); 2405179193Sjb } 2406211738Srpaulo#endif /* notyet */ 2407179193Sjb 2408211738Srpaulo#if defined(sun) 2409179193Sjb mutex_exit(&p->p_lock); 2410211738Srpaulo#else 2411211738Srpaulo PROC_UNLOCK(p); 2412211738Srpaulo#endif 2413179193Sjb } 2414211738Srpaulo#endif /* notyet */ 2415179193Sjb 2416179193Sjb index = FASTTRAP_TPOINTS_INDEX(instr.ftiq_pid, instr.ftiq_pc); 2417179193Sjb 2418179193Sjb mutex_enter(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2419179193Sjb tp = fasttrap_tpoints.fth_table[index].ftb_data; 2420179193Sjb while (tp != NULL) { 2421179193Sjb if (instr.ftiq_pid == tp->ftt_pid && 2422179193Sjb instr.ftiq_pc == tp->ftt_pc && 2423179193Sjb tp->ftt_proc->ftpc_acount != 0) 2424179193Sjb break; 2425179193Sjb 2426179193Sjb tp = tp->ftt_next; 2427179193Sjb } 2428179193Sjb 2429179193Sjb if (tp == NULL) { 2430179193Sjb mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2431179193Sjb return (ENOENT); 2432179193Sjb } 2433179193Sjb 2434179193Sjb bcopy(&tp->ftt_instr, &instr.ftiq_instr, 2435179193Sjb sizeof (instr.ftiq_instr)); 2436179193Sjb mutex_exit(&fasttrap_tpoints.fth_table[index].ftb_mtx); 2437179193Sjb 2438179193Sjb if (copyout(&instr, (void *)arg, sizeof (instr)) != 0) 2439179193Sjb return (EFAULT); 2440179193Sjb 2441179193Sjb return (0); 2442179193Sjb } 2443179193Sjb 2444179193Sjb return (EINVAL); 2445179193Sjb} 2446179193Sjb 2447179193Sjbstatic int 2448211738Srpaulofasttrap_load(void) 2449179193Sjb{ 2450179193Sjb ulong_t nent; 2451250953Smarkj int i, ret; 2452179193Sjb 2453211738Srpaulo /* Create the /dev/dtrace/fasttrap entry. */ 2454211738Srpaulo fasttrap_cdev = make_dev(&fasttrap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 2455211738Srpaulo "dtrace/fasttrap"); 2456179193Sjb 2457211738Srpaulo mtx_init(&fasttrap_cleanup_mtx, "fasttrap clean", "dtrace", MTX_DEF); 2458211738Srpaulo mutex_init(&fasttrap_count_mtx, "fasttrap count mtx", MUTEX_DEFAULT, 2459211738Srpaulo NULL); 2460179193Sjb 2461211738Srpaulo#if defined(sun) 2462179193Sjb fasttrap_max = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2463179193Sjb "fasttrap-max-probes", FASTTRAP_MAX_DEFAULT); 2464211738Srpaulo#else 2465211738Srpaulo fasttrap_max = FASTTRAP_MAX_DEFAULT; 2466211738Srpaulo#endif 2467179193Sjb fasttrap_total = 0; 2468179193Sjb 2469179193Sjb /* 2470179193Sjb * Conjure up the tracepoints hashtable... 2471179193Sjb */ 2472211738Srpaulo#if defined(sun) 2473179193Sjb nent = ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS, 2474179193Sjb "fasttrap-hash-size", FASTTRAP_TPOINTS_DEFAULT_SIZE); 2475211738Srpaulo#else 2476211738Srpaulo nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2477211738Srpaulo#endif 2478179193Sjb 2479179193Sjb if (nent == 0 || nent > 0x1000000) 2480179193Sjb nent = FASTTRAP_TPOINTS_DEFAULT_SIZE; 2481179193Sjb 2482179193Sjb if ((nent & (nent - 1)) == 0) 2483179193Sjb fasttrap_tpoints.fth_nent = nent; 2484179193Sjb else 2485179193Sjb fasttrap_tpoints.fth_nent = 1 << fasttrap_highbit(nent); 2486179193Sjb ASSERT(fasttrap_tpoints.fth_nent > 0); 2487179193Sjb fasttrap_tpoints.fth_mask = fasttrap_tpoints.fth_nent - 1; 2488179193Sjb fasttrap_tpoints.fth_table = kmem_zalloc(fasttrap_tpoints.fth_nent * 2489179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2490211738Srpaulo#if !defined(sun) 2491211738Srpaulo for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2492211738Srpaulo mutex_init(&fasttrap_tpoints.fth_table[i].ftb_mtx, 2493211738Srpaulo "tracepoints bucket mtx", MUTEX_DEFAULT, NULL); 2494211738Srpaulo#endif 2495179193Sjb 2496179193Sjb /* 2497179193Sjb * ... and the providers hash table... 2498179193Sjb */ 2499179193Sjb nent = FASTTRAP_PROVIDERS_DEFAULT_SIZE; 2500179193Sjb if ((nent & (nent - 1)) == 0) 2501179193Sjb fasttrap_provs.fth_nent = nent; 2502179193Sjb else 2503179193Sjb fasttrap_provs.fth_nent = 1 << fasttrap_highbit(nent); 2504179193Sjb ASSERT(fasttrap_provs.fth_nent > 0); 2505179193Sjb fasttrap_provs.fth_mask = fasttrap_provs.fth_nent - 1; 2506179193Sjb fasttrap_provs.fth_table = kmem_zalloc(fasttrap_provs.fth_nent * 2507179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2508211738Srpaulo#if !defined(sun) 2509211738Srpaulo for (i = 0; i < fasttrap_provs.fth_nent; i++) 2510211738Srpaulo mutex_init(&fasttrap_provs.fth_table[i].ftb_mtx, 2511211738Srpaulo "providers bucket mtx", MUTEX_DEFAULT, NULL); 2512211738Srpaulo#endif 2513179193Sjb 2514259483Sasomers ret = kproc_create(fasttrap_pid_cleanup_cb, NULL, 2515259483Sasomers &fasttrap_cleanup_proc, 0, 0, "ftcleanup"); 2516259483Sasomers if (ret != 0) { 2517259483Sasomers destroy_dev(fasttrap_cdev); 2518259483Sasomers#if !defined(sun) 2519259483Sasomers for (i = 0; i < fasttrap_provs.fth_nent; i++) 2520259483Sasomers mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx); 2521259483Sasomers for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2522259483Sasomers mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx); 2523259483Sasomers#endif 2524259483Sasomers kmem_free(fasttrap_provs.fth_table, fasttrap_provs.fth_nent * 2525259483Sasomers sizeof (fasttrap_bucket_t)); 2526259483Sasomers mtx_destroy(&fasttrap_cleanup_mtx); 2527259483Sasomers mutex_destroy(&fasttrap_count_mtx); 2528259483Sasomers return (ret); 2529259483Sasomers } 2530259483Sasomers 2531259483Sasomers 2532179193Sjb /* 2533179193Sjb * ... and the procs hash table. 2534179193Sjb */ 2535179193Sjb nent = FASTTRAP_PROCS_DEFAULT_SIZE; 2536179193Sjb if ((nent & (nent - 1)) == 0) 2537179193Sjb fasttrap_procs.fth_nent = nent; 2538179193Sjb else 2539179193Sjb fasttrap_procs.fth_nent = 1 << fasttrap_highbit(nent); 2540179193Sjb ASSERT(fasttrap_procs.fth_nent > 0); 2541179193Sjb fasttrap_procs.fth_mask = fasttrap_procs.fth_nent - 1; 2542179193Sjb fasttrap_procs.fth_table = kmem_zalloc(fasttrap_procs.fth_nent * 2543179193Sjb sizeof (fasttrap_bucket_t), KM_SLEEP); 2544211738Srpaulo#if !defined(sun) 2545211738Srpaulo for (i = 0; i < fasttrap_procs.fth_nent; i++) 2546211738Srpaulo mutex_init(&fasttrap_procs.fth_table[i].ftb_mtx, 2547211738Srpaulo "processes bucket mtx", MUTEX_DEFAULT, NULL); 2548211925Srpaulo 2549211925Srpaulo CPU_FOREACH(i) { 2550211925Srpaulo mutex_init(&fasttrap_cpuc_pid_lock[i], "fasttrap barrier", 2551211925Srpaulo MUTEX_DEFAULT, NULL); 2552211925Srpaulo } 2553269342Smarkj 2554269342Smarkj /* 2555269342Smarkj * This event handler must run before kdtrace_thread_dtor() since it 2556269342Smarkj * accesses the thread's struct kdtrace_thread. 2557269342Smarkj */ 2558269342Smarkj fasttrap_thread_dtor_tag = EVENTHANDLER_REGISTER(thread_dtor, 2559269342Smarkj fasttrap_thread_dtor, NULL, EVENTHANDLER_PRI_FIRST); 2560211738Srpaulo#endif 2561179193Sjb 2562253079Savg /* 2563253079Savg * Install our hooks into fork(2), exec(2), and exit(2). 2564253079Savg */ 2565253079Savg dtrace_fasttrap_fork = &fasttrap_fork; 2566253079Savg dtrace_fasttrap_exit = &fasttrap_exec_exit; 2567253079Savg dtrace_fasttrap_exec = &fasttrap_exec_exit; 2568253079Savg 2569179193Sjb (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2570179193Sjb &fasttrap_meta_id); 2571179193Sjb 2572211738Srpaulo return (0); 2573179193Sjb} 2574179193Sjb 2575179193Sjbstatic int 2576211738Srpaulofasttrap_unload(void) 2577179193Sjb{ 2578179193Sjb int i, fail = 0; 2579179193Sjb 2580179193Sjb /* 2581179193Sjb * Unregister the meta-provider to make sure no new fasttrap- 2582179193Sjb * managed providers come along while we're trying to close up 2583179193Sjb * shop. If we fail to detach, we'll need to re-register as a 2584179193Sjb * meta-provider. We can fail to unregister as a meta-provider 2585179193Sjb * if providers we manage still exist. 2586179193Sjb */ 2587179193Sjb if (fasttrap_meta_id != DTRACE_METAPROVNONE && 2588179193Sjb dtrace_meta_unregister(fasttrap_meta_id) != 0) 2589211738Srpaulo return (-1); 2590179193Sjb 2591179193Sjb /* 2592179193Sjb * Iterate over all of our providers. If there's still a process 2593179193Sjb * that corresponds to that pid, fail to detach. 2594179193Sjb */ 2595179193Sjb for (i = 0; i < fasttrap_provs.fth_nent; i++) { 2596179193Sjb fasttrap_provider_t **fpp, *fp; 2597179193Sjb fasttrap_bucket_t *bucket = &fasttrap_provs.fth_table[i]; 2598179193Sjb 2599179193Sjb mutex_enter(&bucket->ftb_mtx); 2600179193Sjb fpp = (fasttrap_provider_t **)&bucket->ftb_data; 2601179193Sjb while ((fp = *fpp) != NULL) { 2602179193Sjb /* 2603179193Sjb * Acquire and release the lock as a simple way of 2604179193Sjb * waiting for any other consumer to finish with 2605179193Sjb * this provider. A thread must first acquire the 2606179193Sjb * bucket lock so there's no chance of another thread 2607179193Sjb * blocking on the provider's lock. 2608179193Sjb */ 2609179193Sjb mutex_enter(&fp->ftp_mtx); 2610179193Sjb mutex_exit(&fp->ftp_mtx); 2611179193Sjb 2612179193Sjb if (dtrace_unregister(fp->ftp_provid) != 0) { 2613179193Sjb fail = 1; 2614179193Sjb fpp = &fp->ftp_next; 2615179193Sjb } else { 2616179193Sjb *fpp = fp->ftp_next; 2617179193Sjb fasttrap_provider_free(fp); 2618179193Sjb } 2619179193Sjb } 2620179193Sjb 2621179193Sjb mutex_exit(&bucket->ftb_mtx); 2622179193Sjb } 2623179193Sjb 2624179193Sjb if (fail) { 2625179193Sjb (void) dtrace_meta_register("fasttrap", &fasttrap_mops, NULL, 2626179193Sjb &fasttrap_meta_id); 2627179193Sjb 2628211738Srpaulo return (-1); 2629179193Sjb } 2630179193Sjb 2631259483Sasomers /* 2632259483Sasomers * Stop new processes from entering these hooks now, before the 2633259483Sasomers * fasttrap_cleanup thread runs. That way all processes will hopefully 2634259483Sasomers * be out of these hooks before we free fasttrap_provs.fth_table 2635259483Sasomers */ 2636259483Sasomers ASSERT(dtrace_fasttrap_fork == &fasttrap_fork); 2637259483Sasomers dtrace_fasttrap_fork = NULL; 2638259483Sasomers 2639259483Sasomers ASSERT(dtrace_fasttrap_exec == &fasttrap_exec_exit); 2640259483Sasomers dtrace_fasttrap_exec = NULL; 2641259483Sasomers 2642259483Sasomers ASSERT(dtrace_fasttrap_exit == &fasttrap_exec_exit); 2643259483Sasomers dtrace_fasttrap_exit = NULL; 2644259483Sasomers 2645250953Smarkj mtx_lock(&fasttrap_cleanup_mtx); 2646250953Smarkj fasttrap_cleanup_drain = 1; 2647250953Smarkj /* Wait for the cleanup thread to finish up and signal us. */ 2648250953Smarkj wakeup(&fasttrap_cleanup_cv); 2649250953Smarkj mtx_sleep(&fasttrap_cleanup_drain, &fasttrap_cleanup_mtx, 0, "ftcld", 2650250953Smarkj 0); 2651250953Smarkj fasttrap_cleanup_proc = NULL; 2652252493Smarkj mtx_destroy(&fasttrap_cleanup_mtx); 2653250953Smarkj 2654179193Sjb#ifdef DEBUG 2655179193Sjb mutex_enter(&fasttrap_count_mtx); 2656179193Sjb ASSERT(fasttrap_pid_count == 0); 2657179193Sjb mutex_exit(&fasttrap_count_mtx); 2658179193Sjb#endif 2659179193Sjb 2660259483Sasomers#if !defined(sun) 2661269342Smarkj EVENTHANDLER_DEREGISTER(thread_dtor, fasttrap_thread_dtor_tag); 2662269342Smarkj 2663259483Sasomers for (i = 0; i < fasttrap_tpoints.fth_nent; i++) 2664259483Sasomers mutex_destroy(&fasttrap_tpoints.fth_table[i].ftb_mtx); 2665259483Sasomers for (i = 0; i < fasttrap_provs.fth_nent; i++) 2666259483Sasomers mutex_destroy(&fasttrap_provs.fth_table[i].ftb_mtx); 2667259483Sasomers for (i = 0; i < fasttrap_procs.fth_nent; i++) 2668259483Sasomers mutex_destroy(&fasttrap_procs.fth_table[i].ftb_mtx); 2669259483Sasomers#endif 2670179193Sjb kmem_free(fasttrap_tpoints.fth_table, 2671179193Sjb fasttrap_tpoints.fth_nent * sizeof (fasttrap_bucket_t)); 2672179193Sjb fasttrap_tpoints.fth_nent = 0; 2673179193Sjb 2674179193Sjb kmem_free(fasttrap_provs.fth_table, 2675179193Sjb fasttrap_provs.fth_nent * sizeof (fasttrap_bucket_t)); 2676179193Sjb fasttrap_provs.fth_nent = 0; 2677179193Sjb 2678179193Sjb kmem_free(fasttrap_procs.fth_table, 2679179193Sjb fasttrap_procs.fth_nent * sizeof (fasttrap_bucket_t)); 2680179193Sjb fasttrap_procs.fth_nent = 0; 2681179193Sjb 2682211738Srpaulo#if !defined(sun) 2683211738Srpaulo destroy_dev(fasttrap_cdev); 2684211738Srpaulo mutex_destroy(&fasttrap_count_mtx); 2685211925Srpaulo CPU_FOREACH(i) { 2686211925Srpaulo mutex_destroy(&fasttrap_cpuc_pid_lock[i]); 2687211925Srpaulo } 2688211738Srpaulo#endif 2689179193Sjb 2690211738Srpaulo return (0); 2691179193Sjb} 2692179193Sjb 2693211738Srpaulo/* ARGSUSED */ 2694211738Srpaulostatic int 2695211738Srpaulofasttrap_modevent(module_t mod __unused, int type, void *data __unused) 2696211738Srpaulo{ 2697211738Srpaulo int error = 0; 2698179193Sjb 2699211738Srpaulo switch (type) { 2700211738Srpaulo case MOD_LOAD: 2701211738Srpaulo break; 2702179193Sjb 2703211738Srpaulo case MOD_UNLOAD: 2704211738Srpaulo break; 2705179193Sjb 2706211738Srpaulo case MOD_SHUTDOWN: 2707211738Srpaulo break; 2708179193Sjb 2709211738Srpaulo default: 2710211738Srpaulo error = EOPNOTSUPP; 2711211738Srpaulo break; 2712211738Srpaulo } 2713211738Srpaulo return (error); 2714179193Sjb} 2715179193Sjb 2716211738SrpauloSYSINIT(fasttrap_load, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, fasttrap_load, 2717211738Srpaulo NULL); 2718211738SrpauloSYSUNINIT(fasttrap_unload, SI_SUB_DTRACE_PROVIDER, SI_ORDER_ANY, 2719211738Srpaulo fasttrap_unload, NULL); 2720211738Srpaulo 2721211738SrpauloDEV_MODULE(fasttrap, fasttrap_modevent, NULL); 2722211738SrpauloMODULE_VERSION(fasttrap, 1); 2723211738SrpauloMODULE_DEPEND(fasttrap, dtrace, 1, 1, 1); 2724211738SrpauloMODULE_DEPEND(fasttrap, opensolaris, 1, 1, 1); 2725