1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 * 21 * $FreeBSD: src/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c,v 1.10.2.1 2009/08/03 08:13:06 kensmith Exp $ 22 */ 23 24/* 25 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29/* #pragma ident "%Z%%M% %I% %E% SMI" */ 30 31/* 32 * DTrace - Dynamic Tracing for Solaris 33 * 34 * This is the implementation of the Solaris Dynamic Tracing framework 35 * (DTrace). The user-visible interface to DTrace is described at length in 36 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 37 * library, the in-kernel DTrace framework, and the DTrace providers are 38 * described in the block comments in the <sys/dtrace.h> header file. The 39 * internal architecture of DTrace is described in the block comments in the 40 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 41 * implementation very much assume mastery of all of these sources; if one has 42 * an unanswered question about the implementation, one should consult them 43 * first. 44 * 45 * The functions here are ordered roughly as follows: 46 * 47 * - Probe context functions 48 * - Probe hashing functions 49 * - Non-probe context utility functions 50 * - Matching functions 51 * - Provider-to-Framework API functions 52 * - Probe management functions 53 * - DIF object functions 54 * - Format functions 55 * - Predicate functions 56 * - ECB functions 57 * - Buffer functions 58 * - Enabling functions 59 * - DOF functions 60 * - Anonymous enabling functions 61 * - Consumer state functions 62 * - Helper functions 63 * - Hook functions 64 * - Driver cookbook functions 65 * 66 * Each group of functions begins with a block comment labelled the "DTrace 67 * [Group] Functions", allowing one to find each block by searching forward 68 * on capital-f functions. 69 */ 70#if !defined(sun) 71/* we need internal access to mutexes for state inspection */ 72#define __MUTEX_PRIVATE 73#define __RWLOCK_PRIVATE 74#endif 75 76#include <sys/errno.h> 77#if !defined(sun) 78#include <sys/time.h> 79#endif 80#include <sys/stat.h> 81#include <sys/conf.h> 82#include <sys/systm.h> 83#if defined(sun) 84#include <sys/modctl.h> 85#include <sys/ddi.h> 86#include <sys/sunddi.h> 87#endif 88#include <sys/cpuvar.h> 89#include <sys/kmem.h> 90#if defined(sun) 91#include <sys/strsubr.h> 92#endif 93#include <sys/sysmacros.h> 94#include <sys/dtrace_impl.h> 95#include <sys/atomic.h> 96#include <sys/cmn_err.h> 97#include <sys/mutex_impl.h> 98#include <sys/rwlock_impl.h> 99#include <sys/ctf_api.h> 100#if defined(sun) 101#include <sys/panic.h> 102#include <sys/priv_impl.h> 103#endif 104#include <sys/policy.h> 105#if defined(sun) 106#include <sys/cred_impl.h> 107#include <sys/procfs_isa.h> 108#endif 109#include <sys/taskq.h> 110#if defined(sun) 111#include <sys/mkdev.h> 112#include <sys/kdi.h> 113#endif 114#include <sys/zone.h> 115#include <sys/socket.h> 116#include <netinet/in.h> 117 118/* FreeBSD includes: */ 119#if !defined(sun) 120 121#include <sys/ctype.h> 122#include <sys/limits.h> 123//#include <sys/kdb.h> 124#include <sys/kernel.h> 125#include <sys/malloc.h> 126#include <sys/sysctl.h> 127#include <sys/lock.h> 128#include <sys/mutex.h> 129#include <sys/rwlock.h> 130//#include <sys/sx.h> 131#include <sys/file.h> 132#include <sys/filedesc.h> 133#include <sys/dtrace_bsd.h> 134#include <sys/vmem.h> 135#include <sys/module.h> 136#include <sys/cpu.h> 137#include <netinet/in.h> 138#include "dtrace_cddl.h" 139#include "dtrace_debug.c" 140#endif 141 142#if !defined(sun) 143/* fake module entry for netbsd */ 144module_t *mod_nbsd = NULL; 145#endif 146 147/* 148 * DTrace Tunable Variables 149 * 150 * The following variables may be tuned by adding a line to /etc/system that 151 * includes both the name of the DTrace module ("dtrace") and the name of the 152 * variable. For example: 153 * 154 * set dtrace:dtrace_destructive_disallow = 1 155 * 156 * In general, the only variables that one should be tuning this way are those 157 * that affect system-wide DTrace behavior, and for which the default behavior 158 * is undesirable. Most of these variables are tunable on a per-consumer 159 * basis using DTrace options, and need not be tuned on a system-wide basis. 160 * When tuning these variables, avoid pathological values; while some attempt 161 * is made to verify the integrity of these variables, they are not considered 162 * part of the supported interface to DTrace, and they are therefore not 163 * checked comprehensively. Further, these variables should not be tuned 164 * dynamically via "mdb -kw" or other means; they should only be tuned via 165 * /etc/system. 166 */ 167int dtrace_destructive_disallow = 0; 168dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 169size_t dtrace_difo_maxsize = (256 * 1024); 170dtrace_optval_t dtrace_dof_maxsize = (256 * 1024); 171size_t dtrace_global_maxsize = (16 * 1024); 172size_t dtrace_actions_max = (16 * 1024); 173size_t dtrace_retain_max = 1024; 174dtrace_optval_t dtrace_helper_actions_max = 32; 175dtrace_optval_t dtrace_helper_providers_max = 32; 176dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 177size_t dtrace_strsize_default = 256; 178dtrace_optval_t dtrace_cleanrate_default = 99009900; /* 101 hz */ 179dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 180dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 181dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 182dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 183dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 184dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 185dtrace_optval_t dtrace_nspec_default = 1; 186dtrace_optval_t dtrace_specsize_default = 32 * 1024; 187dtrace_optval_t dtrace_stackframes_default = 20; 188dtrace_optval_t dtrace_ustackframes_default = 20; 189dtrace_optval_t dtrace_jstackframes_default = 50; 190dtrace_optval_t dtrace_jstackstrsize_default = 512; 191int dtrace_msgdsize_max = 128; 192hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 193hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 194int dtrace_devdepth_max = 32; 195int dtrace_err_verbose; 196hrtime_t dtrace_deadman_interval = NANOSEC; 197hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 198hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 199 200/* 201 * DTrace External Variables 202 * 203 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 204 * available to DTrace consumers via the backtick (`) syntax. One of these, 205 * dtrace_zero, is made deliberately so: it is provided as a source of 206 * well-known, zero-filled memory. While this variable is not documented, 207 * it is used by some translators as an implementation detail. 208 */ 209const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 210 211/* 212 * DTrace Internal Variables 213 */ 214#if defined(sun) 215static dev_info_t *dtrace_devi; /* device info */ 216#endif 217static vmem_t *dtrace_arena; /* probe ID arena */ 218#if defined(sun) 219static vmem_t *dtrace_minor; /* minor number arena */ 220static taskq_t *dtrace_taskq; /* task queue */ 221#endif 222static dtrace_probe_t **dtrace_probes; /* array of all probes */ 223int dtrace_probes_size=0; /* size for kmem_free */ 224static int dtrace_nprobes; /* number of probes */ 225static dtrace_provider_t *dtrace_provider; /* provider list */ 226static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 227static int dtrace_opens; /* number of opens */ 228static int dtrace_helpers; /* number of helpers */ 229#if defined(sun) 230static void *dtrace_softstate; /* softstate pointer */ 231#endif 232static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 233static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 234static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 235static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 236static int dtrace_toxranges; /* number of toxic ranges */ 237static int dtrace_toxranges_max; /* size of toxic range array */ 238static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 239static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 240static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 241static kthread_t *dtrace_panicked; /* panicking thread */ 242static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 243static dtrace_genid_t dtrace_probegen; /* current probe generation */ 244static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 245static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 246static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 247#if !defined(sun) 248int dtrace_in_probe; /* non-zero if executing a probe */ 249#if defined(__i386__) || defined(__amd64__) 250uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 251#endif 252#endif 253 254/* 255 * DTrace Locking 256 * DTrace is protected by three (relatively coarse-grained) locks: 257 * 258 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 259 * including enabling state, probes, ECBs, consumer state, helper state, 260 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 261 * probe context is lock-free -- synchronization is handled via the 262 * dtrace_sync() cross call mechanism. 263 * 264 * (2) dtrace_provider_lock is required when manipulating provider state, or 265 * when provider state must be held constant. 266 * 267 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 268 * when meta provider state must be held constant. 269 * 270 * The lock ordering between these three locks is dtrace_meta_lock before 271 * dtrace_provider_lock before dtrace_lock. (In particular, there are 272 * several places where dtrace_provider_lock is held by the framework as it 273 * calls into the providers -- which then call back into the framework, 274 * grabbing dtrace_lock.) 275 * 276 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 277 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 278 * role as a coarse-grained lock; it is acquired before both of these locks. 279 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 280 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 281 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 282 * acquired _between_ dtrace_provider_lock and dtrace_lock. 283 */ 284static kmutex_t dtrace_lock; /* probe state lock */ 285static kmutex_t dtrace_provider_lock; /* provider state lock */ 286static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 287 288#if !defined(sun) 289/* XXX FreeBSD hacks. */ 290static kmutex_t mod_lock; 291 292#define cr_suid cr_svuid 293#define cr_sgid cr_svgid 294#define ipaddr_t in_addr_t 295#define mod_modname pathname 296#define vuprintf vprintf 297#define ttoproc(_a) ((_a)->l_proc) 298#define crgetzoneid(_a) 0 299//#define NCPU MAXCPUS 300#define NCPU ncpu 301#define SNOCD 0 302#define CPU_ON_INTR(_a) 0 303 304#define PRIV_EFFECTIVE (1 << 0) 305#define PRIV_DTRACE_KERNEL (1 << 1) 306#define PRIV_DTRACE_PROC (1 << 2) 307#define PRIV_DTRACE_USER (1 << 3) 308#define PRIV_PROC_OWNER (1 << 4) 309#define PRIV_PROC_ZONE (1 << 5) 310#define PRIV_ALL ~0 311 312//SYSCTL_NODE(_debug, OID_AUTO, dtrace, CTLFLAG_RD, 0, "DTrace Information"); 313#endif 314 315#if defined(sun) 316#define curcpu_id CPU->cpu_id 317#else 318#define curcpu_id cpu_number() 319#endif 320 321 322/* 323 * DTrace Provider Variables 324 * 325 * These are the variables relating to DTrace as a provider (that is, the 326 * provider of the BEGIN, END, and ERROR probes). 327 */ 328static dtrace_pattr_t dtrace_provider_attr = { 329{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 330{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 331{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 332{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 333{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 334}; 335 336static void 337dtrace_nullop(void) 338{} 339 340static int 341dtrace_enable_nullop(void) 342{ 343 return (0); 344} 345 346static dtrace_pops_t dtrace_provider_ops = { 347 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop, 348#if defined(sun) 349 (void (*)(void *, modctl_t *))dtrace_nullop, 350#else 351 (void (*)(void *, dtrace_modctl_t *))dtrace_nullop, 352#endif 353 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop, 354 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 355 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 356 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 357 NULL, 358 NULL, 359 NULL, 360 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 361}; 362 363static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 364static dtrace_id_t dtrace_probeid_end; /* special END probe */ 365dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 366 367/* 368 * DTrace Helper Tracing Variables 369 */ 370uint32_t dtrace_helptrace_next = 0; 371uint32_t dtrace_helptrace_nlocals; 372char *dtrace_helptrace_buffer; 373int dtrace_helptrace_bufsize = 512 * 1024; 374 375#ifdef DEBUG 376int dtrace_helptrace_enabled = 1; 377#else 378int dtrace_helptrace_enabled = 0; 379#endif 380 381/* 382 * DTrace Error Hashing 383 * 384 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 385 * table. This is very useful for checking coverage of tests that are 386 * expected to induce DIF or DOF processing errors, and may be useful for 387 * debugging problems in the DIF code generator or in DOF generation . The 388 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 389 */ 390#ifdef DEBUG 391static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 392static const char *dtrace_errlast; 393static kthread_t *dtrace_errthread; 394static kmutex_t dtrace_errlock; 395#endif 396 397/* 398 * DTrace Macros and Constants 399 * 400 * These are various macros that are useful in various spots in the 401 * implementation, along with a few random constants that have no meaning 402 * outside of the implementation. There is no real structure to this cpp 403 * mishmash -- but is there ever? 404 */ 405#define DTRACE_HASHSTR(hash, probe) \ 406 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 407 408#define DTRACE_HASHNEXT(hash, probe) \ 409 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 410 411#define DTRACE_HASHPREV(hash, probe) \ 412 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 413 414#define DTRACE_HASHEQ(hash, lhs, rhs) \ 415 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 416 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 417 418#define DTRACE_AGGHASHSIZE_SLEW 17 419 420#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 421 422/* 423 * The key for a thread-local variable consists of the lower 61 bits of the 424 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 425 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 426 * equal to a variable identifier. This is necessary (but not sufficient) to 427 * assure that global associative arrays never collide with thread-local 428 * variables. To guarantee that they cannot collide, we must also define the 429 * order for keying dynamic variables. That order is: 430 * 431 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 432 * 433 * Because the variable-key and the tls-key are in orthogonal spaces, there is 434 * no way for a global variable key signature to match a thread-local key 435 * signature. 436 */ 437#if defined(sun) 438#define DTRACE_TLS_THRKEY(where) { \ 439 uint_t intr = 0; \ 440 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 441 for (; actv; actv >>= 1) \ 442 intr++; \ 443 ASSERT(intr < (1 << 3)); \ 444 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 445 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 446} 447#else 448#define DTRACE_TLS_THRKEY(where) { \ 449 uint_t intr = 0; \ 450 (where) = ((curthread->l_lid + (curthread->l_proc->p_pid << 16) + \ 451 DIF_VARIABLE_MAX) & \ 452 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 453} 454#if 0 455#define DTRACE_TLS_THRKEY(where) { \ 456 solaris_cpu_t *_c = &solaris_cpu[curcpu_id]; \ 457 uint_t intr = 0; \ 458 uint_t actv = _c->cpu_intr_actv; \ 459 for (; actv; actv >>= 1) \ 460 intr++; \ 461 ASSERT(intr < (1 << 3)); \ 462 (where) = ((curthread->l_lid + (curthread->l_proc->p_pid << 16) + \ 463 DIF_VARIABLE_MAX) & \ 464 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 465} 466#endif 467#endif 468 469#define DT_BSWAP_8(x) ((x) & 0xff) 470#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 471#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 472#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 473 474#define DT_MASK_LO 0x00000000FFFFFFFFULL 475 476#define DTRACE_STORE(type, tomax, offset, what) \ 477 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 478 479#ifndef __i386 480#define DTRACE_ALIGNCHECK(addr, size, flags) \ 481 if (addr & (size - 1)) { \ 482 *flags |= CPU_DTRACE_BADALIGN; \ 483 cpu_core[curcpu_id].cpuc_dtrace_illval = addr; \ 484 return (0); \ 485 } 486#else 487#define DTRACE_ALIGNCHECK(addr, size, flags) 488#endif 489 490/* 491 * Test whether a range of memory starting at testaddr of size testsz falls 492 * within the range of memory described by addr, sz. We take care to avoid 493 * problems with overflow and underflow of the unsigned quantities, and 494 * disallow all negative sizes. Ranges of size 0 are allowed. 495 */ 496#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 497 ((testaddr) - (baseaddr) < (basesz) && \ 498 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 499 (testaddr) + (testsz) >= (testaddr)) 500 501/* 502 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 503 * alloc_sz on the righthand side of the comparison in order to avoid overflow 504 * or underflow in the comparison with it. This is simpler than the INRANGE 505 * check above, because we know that the dtms_scratch_ptr is valid in the 506 * range. Allocations of size zero are allowed. 507 */ 508#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 509 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 510 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 511 512#define DTRACE_LOADFUNC(bits) \ 513/*CSTYLED*/ \ 514uint##bits##_t \ 515dtrace_load##bits(uintptr_t addr) \ 516{ \ 517 size_t size = bits / NBBY; \ 518 /*CSTYLED*/ \ 519 uint##bits##_t rval; \ 520 int i; \ 521 volatile uint16_t *flags = (volatile uint16_t *) \ 522 &cpu_core[curcpu_id].cpuc_dtrace_flags; \ 523 \ 524 DTRACE_ALIGNCHECK(addr, size, flags); \ 525 \ 526 for (i = 0; i < dtrace_toxranges; i++) { \ 527 if (addr >= dtrace_toxrange[i].dtt_limit) \ 528 continue; \ 529 \ 530 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 531 continue; \ 532 \ 533 /* \ 534 * This address falls within a toxic region; return 0. \ 535 */ \ 536 *flags |= CPU_DTRACE_BADADDR; \ 537 cpu_core[curcpu_id].cpuc_dtrace_illval = addr; \ 538 return (0); \ 539 } \ 540 \ 541 *flags |= CPU_DTRACE_NOFAULT; \ 542 /*CSTYLED*/ \ 543 rval = *((volatile uint##bits##_t *)addr); \ 544 *flags &= ~CPU_DTRACE_NOFAULT; \ 545 \ 546 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 547} 548 549#ifdef _LP64 550#define dtrace_loadptr dtrace_load64 551#else 552#define dtrace_loadptr dtrace_load32 553#endif 554 555#define DTRACE_DYNHASH_FREE 0 556#define DTRACE_DYNHASH_SINK 1 557#define DTRACE_DYNHASH_VALID 2 558 559#define DTRACE_MATCH_FAIL -1 560#define DTRACE_MATCH_NEXT 0 561#define DTRACE_MATCH_DONE 1 562#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 563#define DTRACE_STATE_ALIGN 64 564 565#define DTRACE_FLAGS2FLT(flags) \ 566 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 567 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 568 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 569 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 570 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 571 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 572 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 573 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 574 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 575 DTRACEFLT_UNKNOWN) 576 577#define DTRACEACT_ISSTRING(act) \ 578 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 579 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 580 581/* Function prototype definitions: */ 582static size_t dtrace_strlen(const char *, size_t); 583static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 584static void dtrace_enabling_provide(dtrace_provider_t *); 585static int dtrace_enabling_match(dtrace_enabling_t *, int *); 586static void dtrace_enabling_matchall(void); 587static dtrace_state_t *dtrace_anon_grab(void); 588#if defined(sun) 589static uint64_t dtrace_helper(int, dtrace_mstate_t *, 590 dtrace_state_t *, uint64_t, uint64_t); 591static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 592#endif 593static void dtrace_buffer_drop(dtrace_buffer_t *); 594static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 595 dtrace_state_t *, dtrace_mstate_t *); 596static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 597 dtrace_optval_t); 598static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 599#if defined(sun) 600static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 601#endif 602uint16_t dtrace_load16(uintptr_t); 603uint32_t dtrace_load32(uintptr_t); 604uint64_t dtrace_load64(uintptr_t); 605uint8_t dtrace_load8(uintptr_t); 606void dtrace_dynvar_clean(dtrace_dstate_t *); 607dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 608 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 609uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 610 611/* 612 * DTrace Probe Context Functions 613 * 614 * These functions are called from probe context. Because probe context is 615 * any context in which C may be called, arbitrarily locks may be held, 616 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 617 * As a result, functions called from probe context may only call other DTrace 618 * support functions -- they may not interact at all with the system at large. 619 * (Note that the ASSERT macro is made probe-context safe by redefining it in 620 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 621 * loads are to be performed from probe context, they _must_ be in terms of 622 * the safe dtrace_load*() variants. 623 * 624 * Some functions in this block are not actually called from probe context; 625 * for these functions, there will be a comment above the function reading 626 * "Note: not called from probe context." 627 */ 628void 629dtrace_panic(const char *format, ...) 630{ 631 va_list alist; 632 633 va_start(alist, format); 634 dtrace_vpanic(format, alist); 635 va_end(alist); 636} 637 638int 639dtrace_assfail(const char *a, const char *f, int l) 640{ 641 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 642 643 /* 644 * We just need something here that even the most clever compiler 645 * cannot optimize away. 646 */ 647 return (a[(uintptr_t)f]); 648} 649 650/* 651 * Atomically increment a specified error counter from probe context. 652 */ 653static void 654dtrace_error(uint32_t *counter) 655{ 656 /* 657 * Most counters stored to in probe context are per-CPU counters. 658 * However, there are some error conditions that are sufficiently 659 * arcane that they don't merit per-CPU storage. If these counters 660 * are incremented concurrently on different CPUs, scalability will be 661 * adversely affected -- but we don't expect them to be white-hot in a 662 * correctly constructed enabling... 663 */ 664 uint32_t oval, nval; 665 666 do { 667 oval = *counter; 668 669 if ((nval = oval + 1) == 0) { 670 /* 671 * If the counter would wrap, set it to 1 -- assuring 672 * that the counter is never zero when we have seen 673 * errors. (The counter must be 32-bits because we 674 * aren't guaranteed a 64-bit compare&swap operation.) 675 * To save this code both the infamy of being fingered 676 * by a priggish news story and the indignity of being 677 * the target of a neo-puritan witch trial, we're 678 * carefully avoiding any colorful description of the 679 * likelihood of this condition -- but suffice it to 680 * say that it is only slightly more likely than the 681 * overflow of predicate cache IDs, as discussed in 682 * dtrace_predicate_create(). 683 */ 684 nval = 1; 685 } 686 } while (dtrace_cas32(counter, oval, nval) != oval); 687} 688 689/* 690 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 691 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 692 */ 693DTRACE_LOADFUNC(8) 694DTRACE_LOADFUNC(16) 695DTRACE_LOADFUNC(32) 696DTRACE_LOADFUNC(64) 697 698static int 699dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 700{ 701 if (dest < mstate->dtms_scratch_base) 702 return (0); 703 704 if (dest + size < dest) 705 return (0); 706 707 if (dest + size > mstate->dtms_scratch_ptr) 708 return (0); 709 710 return (1); 711} 712 713static int 714dtrace_canstore_statvar(uint64_t addr, size_t sz, 715 dtrace_statvar_t **svars, int nsvars) 716{ 717 int i; 718 719 for (i = 0; i < nsvars; i++) { 720 dtrace_statvar_t *svar = svars[i]; 721 722 if (svar == NULL || svar->dtsv_size == 0) 723 continue; 724 725 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 726 return (1); 727 } 728 729 return (0); 730} 731 732/* 733 * Check to see if the address is within a memory region to which a store may 734 * be issued. This includes the DTrace scratch areas, and any DTrace variable 735 * region. The caller of dtrace_canstore() is responsible for performing any 736 * alignment checks that are needed before stores are actually executed. 737 */ 738static int 739dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 740 dtrace_vstate_t *vstate) 741{ 742 /* 743 * First, check to see if the address is in scratch space... 744 */ 745 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 746 mstate->dtms_scratch_size)) 747 return (1); 748 749 /* 750 * Now check to see if it's a dynamic variable. This check will pick 751 * up both thread-local variables and any global dynamically-allocated 752 * variables. 753 */ 754 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 755 vstate->dtvs_dynvars.dtds_size)) { 756 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 757 uintptr_t base = (uintptr_t)dstate->dtds_base + 758 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 759 uintptr_t chunkoffs; 760 761 /* 762 * Before we assume that we can store here, we need to make 763 * sure that it isn't in our metadata -- storing to our 764 * dynamic variable metadata would corrupt our state. For 765 * the range to not include any dynamic variable metadata, 766 * it must: 767 * 768 * (1) Start above the hash table that is at the base of 769 * the dynamic variable space 770 * 771 * (2) Have a starting chunk offset that is beyond the 772 * dtrace_dynvar_t that is at the base of every chunk 773 * 774 * (3) Not span a chunk boundary 775 * 776 */ 777 if (addr < base) 778 return (0); 779 780 chunkoffs = (addr - base) % dstate->dtds_chunksize; 781 782 if (chunkoffs < sizeof (dtrace_dynvar_t)) 783 return (0); 784 785 if (chunkoffs + sz > dstate->dtds_chunksize) 786 return (0); 787 788 return (1); 789 } 790 791 /* 792 * Finally, check the static local and global variables. These checks 793 * take the longest, so we perform them last. 794 */ 795 if (dtrace_canstore_statvar(addr, sz, 796 vstate->dtvs_locals, vstate->dtvs_nlocals)) 797 return (1); 798 799 if (dtrace_canstore_statvar(addr, sz, 800 vstate->dtvs_globals, vstate->dtvs_nglobals)) 801 return (1); 802 803 return (0); 804} 805 806 807/* 808 * Convenience routine to check to see if the address is within a memory 809 * region in which a load may be issued given the user's privilege level; 810 * if not, it sets the appropriate error flags and loads 'addr' into the 811 * illegal value slot. 812 * 813 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 814 * appropriate memory access protection. 815 */ 816static int 817dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 818 dtrace_vstate_t *vstate) 819{ 820 volatile uintptr_t *illval = &cpu_core[curcpu_id].cpuc_dtrace_illval; 821 822 /* 823 * If we hold the privilege to read from kernel memory, then 824 * everything is readable. 825 */ 826 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 827 return (1); 828 829 /* 830 * You can obviously read that which you can store. 831 */ 832 if (dtrace_canstore(addr, sz, mstate, vstate)) 833 return (1); 834 835 /* 836 * We're allowed to read from our own string table. 837 */ 838 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 839 mstate->dtms_difo->dtdo_strlen)) 840 return (1); 841 842 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 843 *illval = addr; 844 return (0); 845} 846 847/* 848 * Convenience routine to check to see if a given string is within a memory 849 * region in which a load may be issued given the user's privilege level; 850 * this exists so that we don't need to issue unnecessary dtrace_strlen() 851 * calls in the event that the user has all privileges. 852 */ 853static int 854dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 855 dtrace_vstate_t *vstate) 856{ 857 size_t strsz; 858 859 /* 860 * If we hold the privilege to read from kernel memory, then 861 * everything is readable. 862 */ 863 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 864 return (1); 865 866 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 867 if (dtrace_canload(addr, strsz, mstate, vstate)) 868 return (1); 869 870 return (0); 871} 872 873/* 874 * Convenience routine to check to see if a given variable is within a memory 875 * region in which a load may be issued given the user's privilege level. 876 */ 877static int 878dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 879 dtrace_vstate_t *vstate) 880{ 881 size_t sz; 882 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 883 884 /* 885 * If we hold the privilege to read from kernel memory, then 886 * everything is readable. 887 */ 888 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 889 return (1); 890 891 if (type->dtdt_kind == DIF_TYPE_STRING) 892 sz = dtrace_strlen(src, 893 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 894 else 895 sz = type->dtdt_size; 896 897 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 898} 899 900/* 901 * Compare two strings using safe loads. 902 */ 903static int 904dtrace_strncmp(char *s1, char *s2, size_t limit) 905{ 906 uint8_t c1, c2; 907 volatile uint16_t *flags; 908 909 if (s1 == s2 || limit == 0) 910 return (0); 911 912 flags = (volatile uint16_t *)&cpu_core[curcpu_id].cpuc_dtrace_flags; 913 914 do { 915 if (s1 == NULL) { 916 c1 = '\0'; 917 } else { 918 c1 = dtrace_load8((uintptr_t)s1++); 919 } 920 921 if (s2 == NULL) { 922 c2 = '\0'; 923 } else { 924 c2 = dtrace_load8((uintptr_t)s2++); 925 } 926 927 if (c1 != c2) 928 return (c1 - c2); 929 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 930 931 return (0); 932} 933 934/* 935 * Compute strlen(s) for a string using safe memory accesses. The additional 936 * len parameter is used to specify a maximum length to ensure completion. 937 */ 938static size_t 939dtrace_strlen(const char *s, size_t lim) 940{ 941 uint_t len; 942 943 for (len = 0; len != lim; len++) { 944 if (dtrace_load8((uintptr_t)s++) == '\0') 945 break; 946 } 947 948 return (len); 949} 950 951/* 952 * Check if an address falls within a toxic region. 953 */ 954static int 955dtrace_istoxic(uintptr_t kaddr, size_t size) 956{ 957 uintptr_t taddr, tsize; 958 int i; 959 960 for (i = 0; i < dtrace_toxranges; i++) { 961 taddr = dtrace_toxrange[i].dtt_base; 962 tsize = dtrace_toxrange[i].dtt_limit - taddr; 963 964 if (kaddr - taddr < tsize) { 965 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 966 cpu_core[curcpu_id].cpuc_dtrace_illval = kaddr; 967 return (1); 968 } 969 970 if (taddr - kaddr < size) { 971 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 972 cpu_core[curcpu_id].cpuc_dtrace_illval = taddr; 973 return (1); 974 } 975 } 976 977 return (0); 978} 979 980/* 981 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 982 * memory specified by the DIF program. The dst is assumed to be safe memory 983 * that we can store to directly because it is managed by DTrace. As with 984 * standard bcopy, overlapping copies are handled properly. 985 */ 986static void 987dtrace_bcopy(const void *src, void *dst, size_t len) 988{ 989 if (len != 0) { 990 uint8_t *s1 = dst; 991 const uint8_t *s2 = src; 992 993 if (s1 <= s2) { 994 do { 995 *s1++ = dtrace_load8((uintptr_t)s2++); 996 } while (--len != 0); 997 } else { 998 s2 += len; 999 s1 += len; 1000 1001 do { 1002 *--s1 = dtrace_load8((uintptr_t)--s2); 1003 } while (--len != 0); 1004 } 1005 } 1006} 1007 1008/* 1009 * Copy src to dst using safe memory accesses, up to either the specified 1010 * length, or the point that a nul byte is encountered. The src is assumed to 1011 * be unsafe memory specified by the DIF program. The dst is assumed to be 1012 * safe memory that we can store to directly because it is managed by DTrace. 1013 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1014 */ 1015static void 1016dtrace_strcpy(const void *src, void *dst, size_t len) 1017{ 1018 if (len != 0) { 1019 uint8_t *s1 = dst, c; 1020 const uint8_t *s2 = src; 1021 1022 do { 1023 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1024 } while (--len != 0 && c != '\0'); 1025 } 1026} 1027 1028/* 1029 * Copy src to dst, deriving the size and type from the specified (BYREF) 1030 * variable type. The src is assumed to be unsafe memory specified by the DIF 1031 * program. The dst is assumed to be DTrace variable memory that is of the 1032 * specified type; we assume that we can store to directly. 1033 */ 1034static void 1035dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1036{ 1037 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1038 1039 if (type->dtdt_kind == DIF_TYPE_STRING) { 1040 dtrace_strcpy(src, dst, type->dtdt_size); 1041 } else { 1042 dtrace_bcopy(src, dst, type->dtdt_size); 1043 } 1044} 1045 1046/* 1047 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1048 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1049 * safe memory that we can access directly because it is managed by DTrace. 1050 */ 1051static int 1052dtrace_bcmp(const void *s1, const void *s2, size_t len) 1053{ 1054 volatile uint16_t *flags; 1055 1056 flags = (volatile uint16_t *)&cpu_core[curcpu_id].cpuc_dtrace_flags; 1057 1058 if (s1 == s2) 1059 return (0); 1060 1061 if (s1 == NULL || s2 == NULL) 1062 return (1); 1063 1064 if (s1 != s2 && len != 0) { 1065 const uint8_t *ps1 = s1; 1066 const uint8_t *ps2 = s2; 1067 1068 do { 1069 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1070 return (1); 1071 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1072 } 1073 return (0); 1074} 1075 1076/* 1077 * Zero the specified region using a simple byte-by-byte loop. Note that this 1078 * is for safe DTrace-managed memory only. 1079 */ 1080static void 1081dtrace_bzero(void *dst, size_t len) 1082{ 1083 uchar_t *cp; 1084 1085 for (cp = dst; len != 0; len--) 1086 *cp++ = 0; 1087} 1088 1089static void 1090dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1091{ 1092 uint64_t result[2]; 1093 1094 result[0] = addend1[0] + addend2[0]; 1095 result[1] = addend1[1] + addend2[1] + 1096 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1097 1098 sum[0] = result[0]; 1099 sum[1] = result[1]; 1100} 1101 1102/* 1103 * Shift the 128-bit value in a by b. If b is positive, shift left. 1104 * If b is negative, shift right. 1105 */ 1106static void 1107dtrace_shift_128(uint64_t *a, int b) 1108{ 1109 uint64_t mask; 1110 1111 if (b == 0) 1112 return; 1113 1114 if (b < 0) { 1115 b = -b; 1116 if (b >= 64) { 1117 a[0] = a[1] >> (b - 64); 1118 a[1] = 0; 1119 } else { 1120 a[0] >>= b; 1121 mask = 1LL << (64 - b); 1122 mask -= 1; 1123 a[0] |= ((a[1] & mask) << (64 - b)); 1124 a[1] >>= b; 1125 } 1126 } else { 1127 if (b >= 64) { 1128 a[1] = a[0] << (b - 64); 1129 a[0] = 0; 1130 } else { 1131 a[1] <<= b; 1132 mask = a[0] >> (64 - b); 1133 a[1] |= mask; 1134 a[0] <<= b; 1135 } 1136 } 1137} 1138 1139/* 1140 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1141 * use native multiplication on those, and then re-combine into the 1142 * resulting 128-bit value. 1143 * 1144 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1145 * hi1 * hi2 << 64 + 1146 * hi1 * lo2 << 32 + 1147 * hi2 * lo1 << 32 + 1148 * lo1 * lo2 1149 */ 1150static void 1151dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1152{ 1153 uint64_t hi1, hi2, lo1, lo2; 1154 uint64_t tmp[2]; 1155 1156 hi1 = factor1 >> 32; 1157 hi2 = factor2 >> 32; 1158 1159 lo1 = factor1 & DT_MASK_LO; 1160 lo2 = factor2 & DT_MASK_LO; 1161 1162 product[0] = lo1 * lo2; 1163 product[1] = hi1 * hi2; 1164 1165 tmp[0] = hi1 * lo2; 1166 tmp[1] = 0; 1167 dtrace_shift_128(tmp, 32); 1168 dtrace_add_128(product, tmp, product); 1169 1170 tmp[0] = hi2 * lo1; 1171 tmp[1] = 0; 1172 dtrace_shift_128(tmp, 32); 1173 dtrace_add_128(product, tmp, product); 1174} 1175 1176/* 1177 * This privilege check should be used by actions and subroutines to 1178 * verify that the user credentials of the process that enabled the 1179 * invoking ECB match the target credentials 1180 */ 1181static int 1182dtrace_priv_proc_common_user(dtrace_state_t *state) 1183{ 1184 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1185 1186 /* 1187 * We should always have a non-NULL state cred here, since if cred 1188 * is null (anonymous tracing), we fast-path bypass this routine. 1189 */ 1190 ASSERT(s_cr != NULL); 1191 1192#if defined(sun) 1193 if ((cr = CRED()) != NULL && 1194 s_cr->cr_uid == cr->cr_uid && 1195 s_cr->cr_uid == cr->cr_ruid && 1196 s_cr->cr_uid == cr->cr_suid && 1197 s_cr->cr_gid == cr->cr_gid && 1198 s_cr->cr_gid == cr->cr_rgid && 1199 s_cr->cr_gid == cr->cr_sgid) 1200 return (1); 1201#else 1202 if ((cr = CRED()) != NULL) { 1203 uid_t uid; 1204 gid_t gid; 1205 1206 uid = kauth_cred_getuid(s_cr); 1207 gid = kauth_cred_getgid(s_cr); 1208 1209 if (uid == kauth_cred_getuid(cr) && 1210 uid == kauth_cred_geteuid(cr) && 1211 uid == kauth_cred_getsvuid(cr) && 1212 gid == kauth_cred_getgid(cr) && 1213 gid == kauth_cred_getegid(cr) && 1214 gid == kauth_cred_getsvgid(cr)) { 1215 return 1; 1216 } 1217 } 1218#endif 1219 1220 return (0); 1221} 1222 1223/* 1224 * This privilege check should be used by actions and subroutines to 1225 * verify that the zone of the process that enabled the invoking ECB 1226 * matches the target credentials 1227 */ 1228static int 1229dtrace_priv_proc_common_zone(dtrace_state_t *state) 1230{ 1231#if defined(sun) 1232 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1233 1234 /* 1235 * We should always have a non-NULL state cred here, since if cred 1236 * is null (anonymous tracing), we fast-path bypass this routine. 1237 */ 1238 ASSERT(s_cr != NULL); 1239 1240 if ((cr = CRED()) != NULL && 1241 s_cr->cr_zone == cr->cr_zone) 1242 return (1); 1243 1244 return (0); 1245#else 1246 return (1); 1247#endif 1248} 1249 1250/* 1251 * This privilege check should be used by actions and subroutines to 1252 * verify that the process has not setuid or changed credentials. 1253 */ 1254static int 1255dtrace_priv_proc_common_nocd(void) 1256{ 1257 proc_t *proc; 1258 1259 if ((proc = ttoproc(curthread)) != NULL && 1260 !(proc->p_flag & SNOCD)) 1261 return (1); 1262 1263 return (0); 1264} 1265 1266static int 1267dtrace_priv_proc_destructive(dtrace_state_t *state) 1268{ 1269 int action = state->dts_cred.dcr_action; 1270 1271 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1272 dtrace_priv_proc_common_zone(state) == 0) 1273 goto bad; 1274 1275 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1276 dtrace_priv_proc_common_user(state) == 0) 1277 goto bad; 1278 1279 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1280 dtrace_priv_proc_common_nocd() == 0) 1281 goto bad; 1282 1283 return (1); 1284 1285bad: 1286 cpu_core[curcpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1287 1288 return (0); 1289} 1290 1291static int 1292dtrace_priv_proc_control(dtrace_state_t *state) 1293{ 1294 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1295 return (1); 1296 1297 if (dtrace_priv_proc_common_zone(state) && 1298 dtrace_priv_proc_common_user(state) && 1299 dtrace_priv_proc_common_nocd()) 1300 return (1); 1301 1302 cpu_core[curcpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1303 1304 return (0); 1305} 1306 1307static int 1308dtrace_priv_proc(dtrace_state_t *state) 1309{ 1310 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1311 return (1); 1312 1313 cpu_core[curcpu_id].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1314 1315 return (0); 1316} 1317 1318static int 1319dtrace_priv_kernel(dtrace_state_t *state) 1320{ 1321 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1322 return (1); 1323 1324 cpu_core[curcpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1325 1326 return (0); 1327} 1328 1329static int 1330dtrace_priv_kernel_destructive(dtrace_state_t *state) 1331{ 1332 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1333 return (1); 1334 1335 cpu_core[curcpu_id].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1336 1337 return (0); 1338} 1339 1340/* 1341 * Note: not called from probe context. This function is called 1342 * asynchronously (and at a regular interval) from outside of probe context to 1343 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1344 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1345 */ 1346void 1347dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1348{ 1349 dtrace_dynvar_t *dirty; 1350 dtrace_dstate_percpu_t *dcpu; 1351 int i, work = 0; 1352 1353 for (i = 0; i < NCPU; i++) { 1354 dcpu = &dstate->dtds_percpu[i]; 1355 1356 ASSERT(dcpu->dtdsc_rinsing == NULL); 1357 1358 /* 1359 * If the dirty list is NULL, there is no dirty work to do. 1360 */ 1361 if (dcpu->dtdsc_dirty == NULL) 1362 continue; 1363 1364 /* 1365 * If the clean list is non-NULL, then we're not going to do 1366 * any work for this CPU -- it means that there has not been 1367 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 1368 * since the last time we cleaned house. 1369 */ 1370 if (dcpu->dtdsc_clean != NULL) 1371 continue; 1372 1373 work = 1; 1374 1375 /* 1376 * Atomically move the dirty list aside. 1377 */ 1378 do { 1379 dirty = dcpu->dtdsc_dirty; 1380 1381 /* 1382 * Before we zap the dirty list, set the rinsing list. 1383 * (This allows for a potential assertion in 1384 * dtrace_dynvar(): if a free dynamic variable appears 1385 * on a hash chain, either the dirty list or the 1386 * rinsing list for some CPU must be non-NULL.) 1387 */ 1388 dcpu->dtdsc_rinsing = dirty; 1389 dtrace_membar_producer(); 1390 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1391 dirty, NULL) != dirty); 1392 } 1393 1394 if (!work) { 1395 /* 1396 * We have no work to do; we can simply return. 1397 */ 1398 return; 1399 } 1400 1401 dtrace_sync(); 1402 1403 for (i = 0; i < NCPU; i++) { 1404 dcpu = &dstate->dtds_percpu[i]; 1405 1406 if (dcpu->dtdsc_rinsing == NULL) 1407 continue; 1408 1409 /* 1410 * We are now guaranteed that no hash chain contains a pointer 1411 * into this dirty list; we can make it clean. 1412 */ 1413 ASSERT(dcpu->dtdsc_clean == NULL); 1414 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1415 dcpu->dtdsc_rinsing = NULL; 1416 } 1417 1418 /* 1419 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1420 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1421 * This prevents a race whereby a CPU incorrectly decides that 1422 * the state should be something other than DTRACE_DSTATE_CLEAN 1423 * after dtrace_dynvar_clean() has completed. 1424 */ 1425 dtrace_sync(); 1426 1427 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1428} 1429 1430/* 1431 * Depending on the value of the op parameter, this function looks-up, 1432 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1433 * allocation is requested, this function will return a pointer to a 1434 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1435 * variable can be allocated. If NULL is returned, the appropriate counter 1436 * will be incremented. 1437 */ 1438dtrace_dynvar_t * 1439dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1440 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1441 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1442{ 1443 uint64_t hashval = DTRACE_DYNHASH_VALID; 1444 dtrace_dynhash_t *hash = dstate->dtds_hash; 1445 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1446 processorid_t me = curcpu_id, xcpu = me; 1447 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1448 size_t bucket, ksize; 1449 size_t chunksize = dstate->dtds_chunksize; 1450 uintptr_t kdata, lock, nstate; 1451 uint_t i; 1452 1453 ASSERT(nkeys != 0); 1454 1455 /* 1456 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1457 * algorithm. For the by-value portions, we perform the algorithm in 1458 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1459 * bit, and seems to have only a minute effect on distribution. For 1460 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1461 * over each referenced byte. It's painful to do this, but it's much 1462 * better than pathological hash distribution. The efficacy of the 1463 * hashing algorithm (and a comparison with other algorithms) may be 1464 * found by running the ::dtrace_dynstat MDB dcmd. 1465 */ 1466 for (i = 0; i < nkeys; i++) { 1467 if (key[i].dttk_size == 0) { 1468 uint64_t val = key[i].dttk_value; 1469 1470 hashval += (val >> 48) & 0xffff; 1471 hashval += (hashval << 10); 1472 hashval ^= (hashval >> 6); 1473 1474 hashval += (val >> 32) & 0xffff; 1475 hashval += (hashval << 10); 1476 hashval ^= (hashval >> 6); 1477 1478 hashval += (val >> 16) & 0xffff; 1479 hashval += (hashval << 10); 1480 hashval ^= (hashval >> 6); 1481 1482 hashval += val & 0xffff; 1483 hashval += (hashval << 10); 1484 hashval ^= (hashval >> 6); 1485 } else { 1486 /* 1487 * This is incredibly painful, but it beats the hell 1488 * out of the alternative. 1489 */ 1490 uint64_t j, size = key[i].dttk_size; 1491 uintptr_t base = (uintptr_t)key[i].dttk_value; 1492 1493 if (!dtrace_canload(base, size, mstate, vstate)) 1494 break; 1495 1496 for (j = 0; j < size; j++) { 1497 hashval += dtrace_load8(base + j); 1498 hashval += (hashval << 10); 1499 hashval ^= (hashval >> 6); 1500 } 1501 } 1502 } 1503 1504 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1505 return (NULL); 1506 1507 hashval += (hashval << 3); 1508 hashval ^= (hashval >> 11); 1509 hashval += (hashval << 15); 1510 1511 /* 1512 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1513 * comes out to be one of our two sentinel hash values. If this 1514 * actually happens, we set the hashval to be a value known to be a 1515 * non-sentinel value. 1516 */ 1517 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1518 hashval = DTRACE_DYNHASH_VALID; 1519 1520 /* 1521 * Yes, it's painful to do a divide here. If the cycle count becomes 1522 * important here, tricks can be pulled to reduce it. (However, it's 1523 * critical that hash collisions be kept to an absolute minimum; 1524 * they're much more painful than a divide.) It's better to have a 1525 * solution that generates few collisions and still keeps things 1526 * relatively simple. 1527 */ 1528 bucket = hashval % dstate->dtds_hashsize; 1529 1530 if (op == DTRACE_DYNVAR_DEALLOC) { 1531 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1532 1533 for (;;) { 1534 while ((lock = *lockp) & 1) 1535 continue; 1536 1537 if (dtrace_casptr((volatile void *)lockp, 1538 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1539 break; 1540 } 1541 1542 dtrace_membar_producer(); 1543 } 1544 1545top: 1546 prev = NULL; 1547 lock = hash[bucket].dtdh_lock; 1548 1549 dtrace_membar_consumer(); 1550 1551 start = hash[bucket].dtdh_chain; 1552 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1553 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1554 op != DTRACE_DYNVAR_DEALLOC)); 1555 1556 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1557 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1558 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1559 1560 if (dvar->dtdv_hashval != hashval) { 1561 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1562 /* 1563 * We've reached the sink, and therefore the 1564 * end of the hash chain; we can kick out of 1565 * the loop knowing that we have seen a valid 1566 * snapshot of state. 1567 */ 1568 ASSERT(dvar->dtdv_next == NULL); 1569 ASSERT(dvar == &dtrace_dynhash_sink); 1570 break; 1571 } 1572 1573 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1574 /* 1575 * We've gone off the rails: somewhere along 1576 * the line, one of the members of this hash 1577 * chain was deleted. Note that we could also 1578 * detect this by simply letting this loop run 1579 * to completion, as we would eventually hit 1580 * the end of the dirty list. However, we 1581 * want to avoid running the length of the 1582 * dirty list unnecessarily (it might be quite 1583 * long), so we catch this as early as 1584 * possible by detecting the hash marker. In 1585 * this case, we simply set dvar to NULL and 1586 * break; the conditional after the loop will 1587 * send us back to top. 1588 */ 1589 dvar = NULL; 1590 break; 1591 } 1592 1593 goto next; 1594 } 1595 1596 if (dtuple->dtt_nkeys != nkeys) 1597 goto next; 1598 1599 for (i = 0; i < nkeys; i++, dkey++) { 1600 if (dkey->dttk_size != key[i].dttk_size) 1601 goto next; /* size or type mismatch */ 1602 1603 if (dkey->dttk_size != 0) { 1604 if (dtrace_bcmp( 1605 (void *)(uintptr_t)key[i].dttk_value, 1606 (void *)(uintptr_t)dkey->dttk_value, 1607 dkey->dttk_size)) 1608 goto next; 1609 } else { 1610 if (dkey->dttk_value != key[i].dttk_value) 1611 goto next; 1612 } 1613 } 1614 1615 if (op != DTRACE_DYNVAR_DEALLOC) 1616 return (dvar); 1617 1618 ASSERT(dvar->dtdv_next == NULL || 1619 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1620 1621 if (prev != NULL) { 1622 ASSERT(hash[bucket].dtdh_chain != dvar); 1623 ASSERT(start != dvar); 1624 ASSERT(prev->dtdv_next == dvar); 1625 prev->dtdv_next = dvar->dtdv_next; 1626 } else { 1627 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1628 start, dvar->dtdv_next) != start) { 1629 /* 1630 * We have failed to atomically swing the 1631 * hash table head pointer, presumably because 1632 * of a conflicting allocation on another CPU. 1633 * We need to reread the hash chain and try 1634 * again. 1635 */ 1636 goto top; 1637 } 1638 } 1639 1640 dtrace_membar_producer(); 1641 1642 /* 1643 * Now set the hash value to indicate that it's free. 1644 */ 1645 ASSERT(hash[bucket].dtdh_chain != dvar); 1646 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1647 1648 dtrace_membar_producer(); 1649 1650 /* 1651 * Set the next pointer to point at the dirty list, and 1652 * atomically swing the dirty pointer to the newly freed dvar. 1653 */ 1654 do { 1655 next = dcpu->dtdsc_dirty; 1656 dvar->dtdv_next = next; 1657 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1658 1659 /* 1660 * Finally, unlock this hash bucket. 1661 */ 1662 ASSERT(hash[bucket].dtdh_lock == lock); 1663 ASSERT(lock & 1); 1664 hash[bucket].dtdh_lock++; 1665 1666 return (NULL); 1667next: 1668 prev = dvar; 1669 continue; 1670 } 1671 1672 if (dvar == NULL) { 1673 /* 1674 * If dvar is NULL, it is because we went off the rails: 1675 * one of the elements that we traversed in the hash chain 1676 * was deleted while we were traversing it. In this case, 1677 * we assert that we aren't doing a dealloc (deallocs lock 1678 * the hash bucket to prevent themselves from racing with 1679 * one another), and retry the hash chain traversal. 1680 */ 1681 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1682 goto top; 1683 } 1684 1685 if (op != DTRACE_DYNVAR_ALLOC) { 1686 /* 1687 * If we are not to allocate a new variable, we want to 1688 * return NULL now. Before we return, check that the value 1689 * of the lock word hasn't changed. If it has, we may have 1690 * seen an inconsistent snapshot. 1691 */ 1692 if (op == DTRACE_DYNVAR_NOALLOC) { 1693 if (hash[bucket].dtdh_lock != lock) 1694 goto top; 1695 } else { 1696 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1697 ASSERT(hash[bucket].dtdh_lock == lock); 1698 ASSERT(lock & 1); 1699 hash[bucket].dtdh_lock++; 1700 } 1701 1702 return (NULL); 1703 } 1704 1705 /* 1706 * We need to allocate a new dynamic variable. The size we need is the 1707 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1708 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1709 * the size of any referred-to data (dsize). We then round the final 1710 * size up to the chunksize for allocation. 1711 */ 1712 for (ksize = 0, i = 0; i < nkeys; i++) 1713 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1714 1715 /* 1716 * This should be pretty much impossible, but could happen if, say, 1717 * strange DIF specified the tuple. Ideally, this should be an 1718 * assertion and not an error condition -- but that requires that the 1719 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1720 * bullet-proof. (That is, it must not be able to be fooled by 1721 * malicious DIF.) Given the lack of backwards branches in DIF, 1722 * solving this would presumably not amount to solving the Halting 1723 * Problem -- but it still seems awfully hard. 1724 */ 1725 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1726 ksize + dsize > chunksize) { 1727 dcpu->dtdsc_drops++; 1728 return (NULL); 1729 } 1730 1731 nstate = DTRACE_DSTATE_EMPTY; 1732 1733 do { 1734retry: 1735 free = dcpu->dtdsc_free; 1736 1737 if (free == NULL) { 1738 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1739 void *rval; 1740 1741 if (clean == NULL) { 1742 /* 1743 * We're out of dynamic variable space on 1744 * this CPU. Unless we have tried all CPUs, 1745 * we'll try to allocate from a different 1746 * CPU. 1747 */ 1748 switch (dstate->dtds_state) { 1749 case DTRACE_DSTATE_CLEAN: { 1750 void *sp = &dstate->dtds_state; 1751 1752 if (++xcpu >= NCPU) 1753 xcpu = 0; 1754 1755 if (dcpu->dtdsc_dirty != NULL && 1756 nstate == DTRACE_DSTATE_EMPTY) 1757 nstate = DTRACE_DSTATE_DIRTY; 1758 1759 if (dcpu->dtdsc_rinsing != NULL) 1760 nstate = DTRACE_DSTATE_RINSING; 1761 1762 dcpu = &dstate->dtds_percpu[xcpu]; 1763 1764 if (xcpu != me) 1765 goto retry; 1766 1767 (void) dtrace_cas32(sp, 1768 DTRACE_DSTATE_CLEAN, nstate); 1769 1770 /* 1771 * To increment the correct bean 1772 * counter, take another lap. 1773 */ 1774 goto retry; 1775 } 1776 1777 case DTRACE_DSTATE_DIRTY: 1778 dcpu->dtdsc_dirty_drops++; 1779 break; 1780 1781 case DTRACE_DSTATE_RINSING: 1782 dcpu->dtdsc_rinsing_drops++; 1783 break; 1784 1785 case DTRACE_DSTATE_EMPTY: 1786 dcpu->dtdsc_drops++; 1787 break; 1788 } 1789 1790 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1791 return (NULL); 1792 } 1793 1794 /* 1795 * The clean list appears to be non-empty. We want to 1796 * move the clean list to the free list; we start by 1797 * moving the clean pointer aside. 1798 */ 1799 if (dtrace_casptr(&dcpu->dtdsc_clean, 1800 clean, NULL) != clean) { 1801 /* 1802 * We are in one of two situations: 1803 * 1804 * (a) The clean list was switched to the 1805 * free list by another CPU. 1806 * 1807 * (b) The clean list was added to by the 1808 * cleansing cyclic. 1809 * 1810 * In either of these situations, we can 1811 * just reattempt the free list allocation. 1812 */ 1813 goto retry; 1814 } 1815 1816 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1817 1818 /* 1819 * Now we'll move the clean list to the free list. 1820 * It's impossible for this to fail: the only way 1821 * the free list can be updated is through this 1822 * code path, and only one CPU can own the clean list. 1823 * Thus, it would only be possible for this to fail if 1824 * this code were racing with dtrace_dynvar_clean(). 1825 * (That is, if dtrace_dynvar_clean() updated the clean 1826 * list, and we ended up racing to update the free 1827 * list.) This race is prevented by the dtrace_sync() 1828 * in dtrace_dynvar_clean() -- which flushes the 1829 * owners of the clean lists out before resetting 1830 * the clean lists. 1831 */ 1832 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1833 ASSERT(rval == NULL); 1834 goto retry; 1835 } 1836 1837 dvar = free; 1838 new_free = dvar->dtdv_next; 1839 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1840 1841 /* 1842 * We have now allocated a new chunk. We copy the tuple keys into the 1843 * tuple array and copy any referenced key data into the data space 1844 * following the tuple array. As we do this, we relocate dttk_value 1845 * in the final tuple to point to the key data address in the chunk. 1846 */ 1847 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1848 dvar->dtdv_data = (void *)(kdata + ksize); 1849 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1850 1851 for (i = 0; i < nkeys; i++) { 1852 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1853 size_t kesize = key[i].dttk_size; 1854 1855 if (kesize != 0) { 1856 dtrace_bcopy( 1857 (const void *)(uintptr_t)key[i].dttk_value, 1858 (void *)kdata, kesize); 1859 dkey->dttk_value = kdata; 1860 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1861 } else { 1862 dkey->dttk_value = key[i].dttk_value; 1863 } 1864 1865 dkey->dttk_size = kesize; 1866 } 1867 1868 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1869 dvar->dtdv_hashval = hashval; 1870 dvar->dtdv_next = start; 1871 1872 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1873 return (dvar); 1874 1875 /* 1876 * The cas has failed. Either another CPU is adding an element to 1877 * this hash chain, or another CPU is deleting an element from this 1878 * hash chain. The simplest way to deal with both of these cases 1879 * (though not necessarily the most efficient) is to free our 1880 * allocated block and tail-call ourselves. Note that the free is 1881 * to the dirty list and _not_ to the free list. This is to prevent 1882 * races with allocators, above. 1883 */ 1884 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1885 1886 dtrace_membar_producer(); 1887 1888 do { 1889 free = dcpu->dtdsc_dirty; 1890 dvar->dtdv_next = free; 1891 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1892 1893 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1894} 1895 1896/*ARGSUSED*/ 1897static void 1898dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1899{ 1900 if ((int64_t)nval < (int64_t)*oval) 1901 *oval = nval; 1902} 1903 1904/*ARGSUSED*/ 1905static void 1906dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1907{ 1908 if ((int64_t)nval > (int64_t)*oval) 1909 *oval = nval; 1910} 1911 1912static void 1913dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1914{ 1915 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1916 int64_t val = (int64_t)nval; 1917 1918 if (val < 0) { 1919 for (i = 0; i < zero; i++) { 1920 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1921 quanta[i] += incr; 1922 return; 1923 } 1924 } 1925 } else { 1926 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1927 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1928 quanta[i - 1] += incr; 1929 return; 1930 } 1931 } 1932 1933 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1934 return; 1935 } 1936 1937 ASSERT(0); 1938} 1939 1940static void 1941dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1942{ 1943 uint64_t arg = *lquanta++; 1944 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1945 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1946 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1947 int32_t val = (int32_t)nval, level; 1948 1949 ASSERT(step != 0); 1950 ASSERT(levels != 0); 1951 1952 if (val < base) { 1953 /* 1954 * This is an underflow. 1955 */ 1956 lquanta[0] += incr; 1957 return; 1958 } 1959 1960 level = (val - base) / step; 1961 1962 if (level < levels) { 1963 lquanta[level + 1] += incr; 1964 return; 1965 } 1966 1967 /* 1968 * This is an overflow. 1969 */ 1970 lquanta[levels + 1] += incr; 1971} 1972 1973/*ARGSUSED*/ 1974static void 1975dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1976{ 1977 data[0]++; 1978 data[1] += nval; 1979} 1980 1981/*ARGSUSED*/ 1982static void 1983dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 1984{ 1985 int64_t snval = (int64_t)nval; 1986 uint64_t tmp[2]; 1987 1988 data[0]++; 1989 data[1] += nval; 1990 1991 /* 1992 * What we want to say here is: 1993 * 1994 * data[2] += nval * nval; 1995 * 1996 * But given that nval is 64-bit, we could easily overflow, so 1997 * we do this as 128-bit arithmetic. 1998 */ 1999 if (snval < 0) 2000 snval = -snval; 2001 2002 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2003 dtrace_add_128(data + 2, tmp, data + 2); 2004} 2005 2006/*ARGSUSED*/ 2007static void 2008dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2009{ 2010 *oval = *oval + 1; 2011} 2012 2013/*ARGSUSED*/ 2014static void 2015dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2016{ 2017 *oval += nval; 2018} 2019 2020/* 2021 * Aggregate given the tuple in the principal data buffer, and the aggregating 2022 * action denoted by the specified dtrace_aggregation_t. The aggregation 2023 * buffer is specified as the buf parameter. This routine does not return 2024 * failure; if there is no space in the aggregation buffer, the data will be 2025 * dropped, and a corresponding counter incremented. 2026 */ 2027static void 2028dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2029 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2030{ 2031 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2032 uint32_t i, ndx, size, fsize; 2033 uint32_t align = sizeof (uint64_t) - 1; 2034 dtrace_aggbuffer_t *agb; 2035 dtrace_aggkey_t *key; 2036 uint32_t hashval = 0, limit, isstr; 2037 caddr_t tomax, data, kdata; 2038 dtrace_actkind_t action; 2039 dtrace_action_t *act; 2040 uintptr_t offs; 2041 2042 if (buf == NULL) 2043 return; 2044 2045 if (!agg->dtag_hasarg) { 2046 /* 2047 * Currently, only quantize() and lquantize() take additional 2048 * arguments, and they have the same semantics: an increment 2049 * value that defaults to 1 when not present. If additional 2050 * aggregating actions take arguments, the setting of the 2051 * default argument value will presumably have to become more 2052 * sophisticated... 2053 */ 2054 arg = 1; 2055 } 2056 2057 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2058 size = rec->dtrd_offset - agg->dtag_base; 2059 fsize = size + rec->dtrd_size; 2060 2061 ASSERT(dbuf->dtb_tomax != NULL); 2062 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2063 2064 if ((tomax = buf->dtb_tomax) == NULL) { 2065 dtrace_buffer_drop(buf); 2066 return; 2067 } 2068 2069 /* 2070 * The metastructure is always at the bottom of the buffer. 2071 */ 2072 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2073 sizeof (dtrace_aggbuffer_t)); 2074 2075 if (buf->dtb_offset == 0) { 2076 /* 2077 * We just kludge up approximately 1/8th of the size to be 2078 * buckets. If this guess ends up being routinely 2079 * off-the-mark, we may need to dynamically readjust this 2080 * based on past performance. 2081 */ 2082 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2083 2084 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2085 (uintptr_t)tomax || hashsize == 0) { 2086 /* 2087 * We've been given a ludicrously small buffer; 2088 * increment our drop count and leave. 2089 */ 2090 dtrace_buffer_drop(buf); 2091 return; 2092 } 2093 2094 /* 2095 * And now, a pathetic attempt to try to get a an odd (or 2096 * perchance, a prime) hash size for better hash distribution. 2097 */ 2098 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2099 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2100 2101 agb->dtagb_hashsize = hashsize; 2102 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2103 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2104 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2105 2106 for (i = 0; i < agb->dtagb_hashsize; i++) 2107 agb->dtagb_hash[i] = NULL; 2108 } 2109 2110 ASSERT(agg->dtag_first != NULL); 2111 ASSERT(agg->dtag_first->dta_intuple); 2112 2113 /* 2114 * Calculate the hash value based on the key. Note that we _don't_ 2115 * include the aggid in the hashing (but we will store it as part of 2116 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2117 * algorithm: a simple, quick algorithm that has no known funnels, and 2118 * gets good distribution in practice. The efficacy of the hashing 2119 * algorithm (and a comparison with other algorithms) may be found by 2120 * running the ::dtrace_aggstat MDB dcmd. 2121 */ 2122 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2123 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2124 limit = i + act->dta_rec.dtrd_size; 2125 ASSERT(limit <= size); 2126 isstr = DTRACEACT_ISSTRING(act); 2127 2128 for (; i < limit; i++) { 2129 hashval += data[i]; 2130 hashval += (hashval << 10); 2131 hashval ^= (hashval >> 6); 2132 2133 if (isstr && data[i] == '\0') 2134 break; 2135 } 2136 } 2137 2138 hashval += (hashval << 3); 2139 hashval ^= (hashval >> 11); 2140 hashval += (hashval << 15); 2141 2142 /* 2143 * Yes, the divide here is expensive -- but it's generally the least 2144 * of the performance issues given the amount of data that we iterate 2145 * over to compute hash values, compare data, etc. 2146 */ 2147 ndx = hashval % agb->dtagb_hashsize; 2148 2149 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2150 ASSERT((caddr_t)key >= tomax); 2151 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2152 2153 if (hashval != key->dtak_hashval || key->dtak_size != size) 2154 continue; 2155 2156 kdata = key->dtak_data; 2157 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2158 2159 for (act = agg->dtag_first; act->dta_intuple; 2160 act = act->dta_next) { 2161 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2162 limit = i + act->dta_rec.dtrd_size; 2163 ASSERT(limit <= size); 2164 isstr = DTRACEACT_ISSTRING(act); 2165 2166 for (; i < limit; i++) { 2167 if (kdata[i] != data[i]) 2168 goto next; 2169 2170 if (isstr && data[i] == '\0') 2171 break; 2172 } 2173 } 2174 2175 if (action != key->dtak_action) { 2176 /* 2177 * We are aggregating on the same value in the same 2178 * aggregation with two different aggregating actions. 2179 * (This should have been picked up in the compiler, 2180 * so we may be dealing with errant or devious DIF.) 2181 * This is an error condition; we indicate as much, 2182 * and return. 2183 */ 2184 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2185 return; 2186 } 2187 2188 /* 2189 * This is a hit: we need to apply the aggregator to 2190 * the value at this key. 2191 */ 2192 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2193 return; 2194next: 2195 continue; 2196 } 2197 2198 /* 2199 * We didn't find it. We need to allocate some zero-filled space, 2200 * link it into the hash table appropriately, and apply the aggregator 2201 * to the (zero-filled) value. 2202 */ 2203 offs = buf->dtb_offset; 2204 while (offs & (align - 1)) 2205 offs += sizeof (uint32_t); 2206 2207 /* 2208 * If we don't have enough room to both allocate a new key _and_ 2209 * its associated data, increment the drop count and return. 2210 */ 2211 if ((uintptr_t)tomax + offs + fsize > 2212 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2213 dtrace_buffer_drop(buf); 2214 return; 2215 } 2216 2217 /*CONSTCOND*/ 2218 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2219 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2220 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2221 2222 key->dtak_data = kdata = tomax + offs; 2223 buf->dtb_offset = offs + fsize; 2224 2225 /* 2226 * Now copy the data across. 2227 */ 2228 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2229 2230 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2231 kdata[i] = data[i]; 2232 2233 /* 2234 * Because strings are not zeroed out by default, we need to iterate 2235 * looking for actions that store strings, and we need to explicitly 2236 * pad these strings out with zeroes. 2237 */ 2238 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2239 int nul; 2240 2241 if (!DTRACEACT_ISSTRING(act)) 2242 continue; 2243 2244 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2245 limit = i + act->dta_rec.dtrd_size; 2246 ASSERT(limit <= size); 2247 2248 for (nul = 0; i < limit; i++) { 2249 if (nul) { 2250 kdata[i] = '\0'; 2251 continue; 2252 } 2253 2254 if (data[i] != '\0') 2255 continue; 2256 2257 nul = 1; 2258 } 2259 } 2260 2261 for (i = size; i < fsize; i++) 2262 kdata[i] = 0; 2263 2264 key->dtak_hashval = hashval; 2265 key->dtak_size = size; 2266 key->dtak_action = action; 2267 key->dtak_next = agb->dtagb_hash[ndx]; 2268 agb->dtagb_hash[ndx] = key; 2269 2270 /* 2271 * Finally, apply the aggregator. 2272 */ 2273 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2274 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2275} 2276 2277/* 2278 * Given consumer state, this routine finds a speculation in the INACTIVE 2279 * state and transitions it into the ACTIVE state. If there is no speculation 2280 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2281 * incremented -- it is up to the caller to take appropriate action. 2282 */ 2283static int 2284dtrace_speculation(dtrace_state_t *state) 2285{ 2286 int i = 0; 2287 dtrace_speculation_state_t current; 2288 uint32_t *stat = &state->dts_speculations_unavail, count; 2289 2290 while (i < state->dts_nspeculations) { 2291 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2292 2293 current = spec->dtsp_state; 2294 2295 if (current != DTRACESPEC_INACTIVE) { 2296 if (current == DTRACESPEC_COMMITTINGMANY || 2297 current == DTRACESPEC_COMMITTING || 2298 current == DTRACESPEC_DISCARDING) 2299 stat = &state->dts_speculations_busy; 2300 i++; 2301 continue; 2302 } 2303 2304 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2305 current, DTRACESPEC_ACTIVE) == current) 2306 return (i + 1); 2307 } 2308 2309 /* 2310 * We couldn't find a speculation. If we found as much as a single 2311 * busy speculation buffer, we'll attribute this failure as "busy" 2312 * instead of "unavail". 2313 */ 2314 do { 2315 count = *stat; 2316 } while (dtrace_cas32(stat, count, count + 1) != count); 2317 2318 return (0); 2319} 2320 2321/* 2322 * This routine commits an active speculation. If the specified speculation 2323 * is not in a valid state to perform a commit(), this routine will silently do 2324 * nothing. The state of the specified speculation is transitioned according 2325 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2326 */ 2327static void 2328dtrace_speculation_commit(dtrace_state_t *state, processorid_t xcpu, 2329 dtrace_specid_t which) 2330{ 2331 dtrace_speculation_t *spec; 2332 dtrace_buffer_t *src, *dest; 2333 uintptr_t daddr, saddr, dlimit; 2334 dtrace_speculation_state_t current, new = 0; 2335 intptr_t offs; 2336 2337 if (which == 0) 2338 return; 2339 2340 if (which > state->dts_nspeculations) { 2341 cpu_core[xcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2342 return; 2343 } 2344 2345 spec = &state->dts_speculations[which - 1]; 2346 src = &spec->dtsp_buffer[xcpu]; 2347 dest = &state->dts_buffer[xcpu]; 2348 2349 do { 2350 current = spec->dtsp_state; 2351 2352 if (current == DTRACESPEC_COMMITTINGMANY) 2353 break; 2354 2355 switch (current) { 2356 case DTRACESPEC_INACTIVE: 2357 case DTRACESPEC_DISCARDING: 2358 return; 2359 2360 case DTRACESPEC_COMMITTING: 2361 /* 2362 * This is only possible if we are (a) commit()'ing 2363 * without having done a prior speculate() on this CPU 2364 * and (b) racing with another commit() on a different 2365 * CPU. There's nothing to do -- we just assert that 2366 * our offset is 0. 2367 */ 2368 ASSERT(src->dtb_offset == 0); 2369 return; 2370 2371 case DTRACESPEC_ACTIVE: 2372 new = DTRACESPEC_COMMITTING; 2373 break; 2374 2375 case DTRACESPEC_ACTIVEONE: 2376 /* 2377 * This speculation is active on one CPU. If our 2378 * buffer offset is non-zero, we know that the one CPU 2379 * must be us. Otherwise, we are committing on a 2380 * different CPU from the speculate(), and we must 2381 * rely on being asynchronously cleaned. 2382 */ 2383 if (src->dtb_offset != 0) { 2384 new = DTRACESPEC_COMMITTING; 2385 break; 2386 } 2387 /*FALLTHROUGH*/ 2388 2389 case DTRACESPEC_ACTIVEMANY: 2390 new = DTRACESPEC_COMMITTINGMANY; 2391 break; 2392 2393 default: 2394 ASSERT(0); 2395 } 2396 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2397 current, new) != current); 2398 2399 /* 2400 * We have set the state to indicate that we are committing this 2401 * speculation. Now reserve the necessary space in the destination 2402 * buffer. 2403 */ 2404 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2405 sizeof (uint64_t), state, NULL)) < 0) { 2406 dtrace_buffer_drop(dest); 2407 goto out; 2408 } 2409 2410 /* 2411 * We have the space; copy the buffer across. (Note that this is a 2412 * highly subobtimal bcopy(); in the unlikely event that this becomes 2413 * a serious performance issue, a high-performance DTrace-specific 2414 * bcopy() should obviously be invented.) 2415 */ 2416 daddr = (uintptr_t)dest->dtb_tomax + offs; 2417 dlimit = daddr + src->dtb_offset; 2418 saddr = (uintptr_t)src->dtb_tomax; 2419 2420 /* 2421 * First, the aligned portion. 2422 */ 2423 while (dlimit - daddr >= sizeof (uint64_t)) { 2424 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2425 2426 daddr += sizeof (uint64_t); 2427 saddr += sizeof (uint64_t); 2428 } 2429 2430 /* 2431 * Now any left-over bit... 2432 */ 2433 while (dlimit - daddr) 2434 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2435 2436 /* 2437 * Finally, commit the reserved space in the destination buffer. 2438 */ 2439 dest->dtb_offset = offs + src->dtb_offset; 2440 2441out: 2442 /* 2443 * If we're lucky enough to be the only active CPU on this speculation 2444 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2445 */ 2446 if (current == DTRACESPEC_ACTIVE || 2447 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2448 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2449 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2450 2451 ASSERT(rval == DTRACESPEC_COMMITTING); 2452 } 2453 2454 src->dtb_offset = 0; 2455 src->dtb_xamot_drops += src->dtb_drops; 2456 src->dtb_drops = 0; 2457} 2458 2459/* 2460 * This routine discards an active speculation. If the specified speculation 2461 * is not in a valid state to perform a discard(), this routine will silently 2462 * do nothing. The state of the specified speculation is transitioned 2463 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2464 */ 2465static void 2466dtrace_speculation_discard(dtrace_state_t *state, processorid_t xcpu, 2467 dtrace_specid_t which) 2468{ 2469 dtrace_speculation_t *spec; 2470 dtrace_speculation_state_t current, new = 0; 2471 dtrace_buffer_t *buf; 2472 2473 if (which == 0) 2474 return; 2475 2476 if (which > state->dts_nspeculations) { 2477 cpu_core[xcpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2478 return; 2479 } 2480 2481 spec = &state->dts_speculations[which - 1]; 2482 buf = &spec->dtsp_buffer[xcpu]; 2483 2484 do { 2485 current = spec->dtsp_state; 2486 2487 switch (current) { 2488 case DTRACESPEC_INACTIVE: 2489 case DTRACESPEC_COMMITTINGMANY: 2490 case DTRACESPEC_COMMITTING: 2491 case DTRACESPEC_DISCARDING: 2492 return; 2493 2494 case DTRACESPEC_ACTIVE: 2495 case DTRACESPEC_ACTIVEMANY: 2496 new = DTRACESPEC_DISCARDING; 2497 break; 2498 2499 case DTRACESPEC_ACTIVEONE: 2500 if (buf->dtb_offset != 0) { 2501 new = DTRACESPEC_INACTIVE; 2502 } else { 2503 new = DTRACESPEC_DISCARDING; 2504 } 2505 break; 2506 2507 default: 2508 ASSERT(0); 2509 } 2510 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2511 current, new) != current); 2512 2513 buf->dtb_offset = 0; 2514 buf->dtb_drops = 0; 2515} 2516 2517/* 2518 * Note: not called from probe context. This function is called 2519 * asynchronously from cross call context to clean any speculations that are 2520 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2521 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2522 * speculation. 2523 */ 2524static void 2525dtrace_speculation_clean_here(dtrace_state_t *state) 2526{ 2527 dtrace_icookie_t cookie; 2528 processorid_t xcpu = curcpu_id; 2529 dtrace_buffer_t *dest = &state->dts_buffer[xcpu]; 2530 dtrace_specid_t i; 2531 2532 cookie = dtrace_interrupt_disable(); 2533 2534 if (dest->dtb_tomax == NULL) { 2535 dtrace_interrupt_enable(cookie); 2536 return; 2537 } 2538 2539 for (i = 0; i < state->dts_nspeculations; i++) { 2540 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2541 dtrace_buffer_t *src = &spec->dtsp_buffer[xcpu]; 2542 2543 if (src->dtb_tomax == NULL) 2544 continue; 2545 2546 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2547 src->dtb_offset = 0; 2548 continue; 2549 } 2550 2551 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2552 continue; 2553 2554 if (src->dtb_offset == 0) 2555 continue; 2556 2557 dtrace_speculation_commit(state, xcpu, i + 1); 2558 } 2559 2560 dtrace_interrupt_enable(cookie); 2561} 2562 2563/* 2564 * Note: not called from probe context. This function is called 2565 * asynchronously (and at a regular interval) to clean any speculations that 2566 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2567 * is work to be done, it cross calls all CPUs to perform that work; 2568 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2569 * INACTIVE state until they have been cleaned by all CPUs. 2570 */ 2571static void 2572dtrace_speculation_clean(dtrace_state_t *state) 2573{ 2574 int work = 0, rv; 2575 dtrace_specid_t i; 2576 2577 for (i = 0; i < state->dts_nspeculations; i++) { 2578 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2579 2580 ASSERT(!spec->dtsp_cleaning); 2581 2582 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2583 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2584 continue; 2585 2586 work++; 2587 spec->dtsp_cleaning = 1; 2588 } 2589 2590 if (!work) 2591 return; 2592 2593 dtrace_xcall(DTRACE_CPUALL, 2594 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2595 2596 /* 2597 * We now know that all CPUs have committed or discarded their 2598 * speculation buffers, as appropriate. We can now set the state 2599 * to inactive. 2600 */ 2601 for (i = 0; i < state->dts_nspeculations; i++) { 2602 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2603 dtrace_speculation_state_t current, new; 2604 2605 if (!spec->dtsp_cleaning) 2606 continue; 2607 2608 current = spec->dtsp_state; 2609 ASSERT(current == DTRACESPEC_DISCARDING || 2610 current == DTRACESPEC_COMMITTINGMANY); 2611 2612 new = DTRACESPEC_INACTIVE; 2613 2614 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2615 ASSERT(rv == current); 2616 spec->dtsp_cleaning = 0; 2617 } 2618} 2619 2620/* 2621 * Called as part of a speculate() to get the speculative buffer associated 2622 * with a given speculation. Returns NULL if the specified speculation is not 2623 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2624 * the active CPU is not the specified CPU -- the speculation will be 2625 * atomically transitioned into the ACTIVEMANY state. 2626 */ 2627static dtrace_buffer_t * 2628dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2629 dtrace_specid_t which) 2630{ 2631 dtrace_speculation_t *spec; 2632 dtrace_speculation_state_t current, new = 0; 2633 dtrace_buffer_t *buf; 2634 2635 if (which == 0) 2636 return (NULL); 2637 2638 if (which > state->dts_nspeculations) { 2639 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2640 return (NULL); 2641 } 2642 2643 spec = &state->dts_speculations[which - 1]; 2644 buf = &spec->dtsp_buffer[cpuid]; 2645 2646 do { 2647 current = spec->dtsp_state; 2648 2649 switch (current) { 2650 case DTRACESPEC_INACTIVE: 2651 case DTRACESPEC_COMMITTINGMANY: 2652 case DTRACESPEC_DISCARDING: 2653 return (NULL); 2654 2655 case DTRACESPEC_COMMITTING: 2656 ASSERT(buf->dtb_offset == 0); 2657 return (NULL); 2658 2659 case DTRACESPEC_ACTIVEONE: 2660 /* 2661 * This speculation is currently active on one CPU. 2662 * Check the offset in the buffer; if it's non-zero, 2663 * that CPU must be us (and we leave the state alone). 2664 * If it's zero, assume that we're starting on a new 2665 * CPU -- and change the state to indicate that the 2666 * speculation is active on more than one CPU. 2667 */ 2668 if (buf->dtb_offset != 0) 2669 return (buf); 2670 2671 new = DTRACESPEC_ACTIVEMANY; 2672 break; 2673 2674 case DTRACESPEC_ACTIVEMANY: 2675 return (buf); 2676 2677 case DTRACESPEC_ACTIVE: 2678 new = DTRACESPEC_ACTIVEONE; 2679 break; 2680 2681 default: 2682 ASSERT(0); 2683 } 2684 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2685 current, new) != current); 2686 2687 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2688 return (buf); 2689} 2690 2691/* 2692 * Return a string. In the event that the user lacks the privilege to access 2693 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2694 * don't fail access checking. 2695 * 2696 * dtrace_dif_variable() uses this routine as a helper for various 2697 * builtin values such as 'execname' and 'probefunc.' 2698 */ 2699uintptr_t 2700dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2701 dtrace_mstate_t *mstate) 2702{ 2703 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2704 uintptr_t ret; 2705 size_t strsz; 2706 2707 /* 2708 * The easy case: this probe is allowed to read all of memory, so 2709 * we can just return this as a vanilla pointer. 2710 */ 2711 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2712 return (addr); 2713 2714 /* 2715 * This is the tougher case: we copy the string in question from 2716 * kernel memory into scratch memory and return it that way: this 2717 * ensures that we won't trip up when access checking tests the 2718 * BYREF return value. 2719 */ 2720 strsz = dtrace_strlen((char *)addr, size) + 1; 2721 2722 if (mstate->dtms_scratch_ptr + strsz > 2723 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2724 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2725 return (0); 2726 } 2727 2728 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2729 strsz); 2730 ret = mstate->dtms_scratch_ptr; 2731 mstate->dtms_scratch_ptr += strsz; 2732 return (ret); 2733} 2734 2735#ifdef notyet 2736/* 2737 * Return a string from a memoy address which is known to have one or 2738 * more concatenated, individually zero terminated, sub-strings. 2739 * In the event that the user lacks the privilege to access 2740 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2741 * don't fail access checking. 2742 * 2743 * dtrace_dif_variable() uses this routine as a helper for various 2744 * builtin values such as 'execargs'. 2745 */ 2746static uintptr_t 2747dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 2748 dtrace_mstate_t *mstate) 2749{ 2750 char *p; 2751 size_t i; 2752 uintptr_t ret; 2753 2754 if (mstate->dtms_scratch_ptr + strsz > 2755 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2756 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2757 return (0); 2758 } 2759 2760 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2761 strsz); 2762 2763 /* Replace sub-string termination characters with a space. */ 2764 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 2765 p++, i++) 2766 if (*p == '\0') 2767 *p = ' '; 2768 2769 ret = mstate->dtms_scratch_ptr; 2770 mstate->dtms_scratch_ptr += strsz; 2771 return (ret); 2772} 2773#endif 2774 2775/* 2776 * This function implements the DIF emulator's variable lookups. The emulator 2777 * passes a reserved variable identifier and optional built-in array index. 2778 */ 2779static uint64_t 2780dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2781 uint64_t ndx) 2782{ 2783 /* 2784 * If we're accessing one of the uncached arguments, we'll turn this 2785 * into a reference in the args array. 2786 */ 2787 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2788 ndx = v - DIF_VAR_ARG0; 2789 v = DIF_VAR_ARGS; 2790 } 2791 2792 switch (v) { 2793 case DIF_VAR_ARGS: 2794 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2795 if (ndx >= sizeof (mstate->dtms_arg) / 2796 sizeof (mstate->dtms_arg[0])) { 2797 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2798 dtrace_provider_t *pv; 2799 uint64_t val; 2800 2801 pv = mstate->dtms_probe->dtpr_provider; 2802 if (pv->dtpv_pops.dtps_getargval != NULL) 2803 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2804 mstate->dtms_probe->dtpr_id, 2805 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2806 else 2807 val = dtrace_getarg(ndx, aframes); 2808 2809 /* 2810 * This is regrettably required to keep the compiler 2811 * from tail-optimizing the call to dtrace_getarg(). 2812 * The condition always evaluates to true, but the 2813 * compiler has no way of figuring that out a priori. 2814 * (None of this would be necessary if the compiler 2815 * could be relied upon to _always_ tail-optimize 2816 * the call to dtrace_getarg() -- but it can't.) 2817 */ 2818 if (mstate->dtms_probe != NULL) 2819 return (val); 2820 2821 ASSERT(0); 2822 } 2823 2824 return (mstate->dtms_arg[ndx]); 2825 2826#if defined(sun) 2827 case DIF_VAR_UREGS: { 2828 klwp_t *lwp; 2829 2830 if (!dtrace_priv_proc(state)) 2831 return (0); 2832 2833 if ((lwp = curthread->t_lwp) == NULL) { 2834 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2835 cpu_core[curcpu_id].cpuc_dtrace_illval = NULL; 2836 return (0); 2837 } 2838 2839 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2840 return (0); 2841 } 2842#endif 2843 2844 case DIF_VAR_CURTHREAD: 2845 if (!dtrace_priv_kernel(state)) 2846 return (0); 2847 return ((uint64_t)(uintptr_t)curthread); 2848 2849 case DIF_VAR_TIMESTAMP: 2850 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2851 mstate->dtms_timestamp = dtrace_gethrtime(); 2852 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2853 } 2854 return (mstate->dtms_timestamp); 2855 2856 case DIF_VAR_VTIMESTAMP: 2857 ASSERT(dtrace_vtime_references != 0); 2858 return (curthread->t_dtrace_vtime); 2859 2860 case DIF_VAR_WALLTIMESTAMP: 2861 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2862 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2863 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2864 } 2865 return (mstate->dtms_walltimestamp); 2866 2867#if defined(sun) 2868 case DIF_VAR_IPL: 2869 if (!dtrace_priv_kernel(state)) 2870 return (0); 2871 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2872 mstate->dtms_ipl = dtrace_getipl(); 2873 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2874 } 2875 return (mstate->dtms_ipl); 2876#endif 2877 2878 case DIF_VAR_EPID: 2879 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2880 return (mstate->dtms_epid); 2881 2882 case DIF_VAR_ID: 2883 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2884 return (mstate->dtms_probe->dtpr_id); 2885 2886 case DIF_VAR_STACKDEPTH: 2887 if (!dtrace_priv_kernel(state)) 2888 return (0); 2889 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2890 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2891 2892 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2893 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2894 } 2895 return (mstate->dtms_stackdepth); 2896 2897#if defined(sun) 2898 case DIF_VAR_USTACKDEPTH: 2899 if (!dtrace_priv_proc(state)) 2900 return (0); 2901 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2902 /* 2903 * See comment in DIF_VAR_PID. 2904 */ 2905 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2906 CPU_ON_INTR(CPU)) { 2907 mstate->dtms_ustackdepth = 0; 2908 } else { 2909 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2910 mstate->dtms_ustackdepth = 2911 dtrace_getustackdepth(); 2912 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2913 } 2914 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2915 } 2916 return (mstate->dtms_ustackdepth); 2917#endif 2918 2919 case DIF_VAR_CALLER: 2920 if (!dtrace_priv_kernel(state)) 2921 return (0); 2922 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2923 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2924 2925 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2926 /* 2927 * If this is an unanchored probe, we are 2928 * required to go through the slow path: 2929 * dtrace_caller() only guarantees correct 2930 * results for anchored probes. 2931 */ 2932 pc_t caller[2] = {0, 0}; 2933 2934 dtrace_getpcstack(caller, 2, aframes, 2935 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2936 mstate->dtms_caller = caller[1]; 2937 } else if ((mstate->dtms_caller = 2938 dtrace_caller(aframes)) == -1) { 2939 /* 2940 * We have failed to do this the quick way; 2941 * we must resort to the slower approach of 2942 * calling dtrace_getpcstack(). 2943 */ 2944 pc_t caller = 0; 2945 2946 dtrace_getpcstack(&caller, 1, aframes, NULL); 2947 mstate->dtms_caller = caller; 2948 } 2949 2950 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2951 } 2952 return (mstate->dtms_caller); 2953 2954#if defined(sun) 2955 case DIF_VAR_UCALLER: 2956 if (!dtrace_priv_proc(state)) 2957 return (0); 2958 2959 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2960 uint64_t ustack[3]; 2961 2962 /* 2963 * dtrace_getupcstack() fills in the first uint64_t 2964 * with the current PID. The second uint64_t will 2965 * be the program counter at user-level. The third 2966 * uint64_t will contain the caller, which is what 2967 * we're after. 2968 */ 2969 ustack[2] = 0; 2970 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2971 dtrace_getupcstack(ustack, 3); 2972 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2973 mstate->dtms_ucaller = ustack[2]; 2974 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2975 } 2976 2977 return (mstate->dtms_ucaller); 2978#endif 2979 2980 case DIF_VAR_PROBEPROV: 2981 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2982 return (dtrace_dif_varstr( 2983 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 2984 state, mstate)); 2985 2986 case DIF_VAR_PROBEMOD: 2987 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2988 return (dtrace_dif_varstr( 2989 (uintptr_t)mstate->dtms_probe->dtpr_mod, 2990 state, mstate)); 2991 2992 case DIF_VAR_PROBEFUNC: 2993 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2994 return (dtrace_dif_varstr( 2995 (uintptr_t)mstate->dtms_probe->dtpr_func, 2996 state, mstate)); 2997 2998 case DIF_VAR_PROBENAME: 2999 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3000 return (dtrace_dif_varstr( 3001 (uintptr_t)mstate->dtms_probe->dtpr_name, 3002 state, mstate)); 3003 3004 case DIF_VAR_PID: 3005 if (!dtrace_priv_proc(state)) 3006 return (0); 3007 3008#if defined(sun) 3009 /* 3010 * Note that we are assuming that an unanchored probe is 3011 * always due to a high-level interrupt. (And we're assuming 3012 * that there is only a single high level interrupt.) 3013 */ 3014 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3015 return (pid0.pid_id); 3016 3017 /* 3018 * It is always safe to dereference one's own t_procp pointer: 3019 * it always points to a valid, allocated proc structure. 3020 * Further, it is always safe to dereference the p_pidp member 3021 * of one's own proc structure. (These are truisms becuase 3022 * threads and processes don't clean up their own state -- 3023 * they leave that task to whomever reaps them.) 3024 */ 3025 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3026#else 3027 return ((uint64_t)curproc->p_pid); 3028#endif 3029 3030 case DIF_VAR_PPID: 3031 if (!dtrace_priv_proc(state)) 3032 return (0); 3033 3034#if defined(sun) 3035 /* 3036 * See comment in DIF_VAR_PID. 3037 */ 3038 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3039 return (pid0.pid_id); 3040 3041 /* 3042 * It is always safe to dereference one's own t_procp pointer: 3043 * it always points to a valid, allocated proc structure. 3044 * (This is true because threads don't clean up their own 3045 * state -- they leave that task to whomever reaps them.) 3046 */ 3047 return ((uint64_t)curthread->t_procp->p_ppid); 3048#else 3049 return ((uint64_t)curproc->p_pptr->p_pid); 3050#endif 3051 3052 case DIF_VAR_TID: 3053#if defined(sun) 3054 /* 3055 * See comment in DIF_VAR_PID. 3056 */ 3057 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3058 return (0); 3059#endif 3060 3061 return ((uint64_t)curthread->t_tid); 3062 3063 case DIF_VAR_EXECARGS: { 3064#if 0 3065 struct pargs *p_args = curthread->td_proc->p_args; 3066 3067 if (p_args == NULL) 3068 return(0); 3069 3070 return (dtrace_dif_varstrz( 3071 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3072#endif 3073 /* XXX FreeBSD extension */ 3074 return 0; 3075 } 3076 3077 case DIF_VAR_EXECNAME: 3078#if defined(sun) 3079 if (!dtrace_priv_proc(state)) 3080 return (0); 3081 3082 /* 3083 * See comment in DIF_VAR_PID. 3084 */ 3085 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3086 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3087 3088 /* 3089 * It is always safe to dereference one's own t_procp pointer: 3090 * it always points to a valid, allocated proc structure. 3091 * (This is true because threads don't clean up their own 3092 * state -- they leave that task to whomever reaps them.) 3093 */ 3094 return (dtrace_dif_varstr( 3095 (uintptr_t)curthread->t_procp->p_user.u_comm, 3096 state, mstate)); 3097#else 3098 return (dtrace_dif_varstr( 3099 (uintptr_t) curthread->l_proc->p_comm, state, mstate)); 3100#endif 3101 3102 case DIF_VAR_ZONENAME: 3103#if defined(sun) 3104 if (!dtrace_priv_proc(state)) 3105 return (0); 3106 3107 /* 3108 * See comment in DIF_VAR_PID. 3109 */ 3110 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3111 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3112 3113 /* 3114 * It is always safe to dereference one's own t_procp pointer: 3115 * it always points to a valid, allocated proc structure. 3116 * (This is true because threads don't clean up their own 3117 * state -- they leave that task to whomever reaps them.) 3118 */ 3119 return (dtrace_dif_varstr( 3120 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3121 state, mstate)); 3122#else 3123 return (0); 3124#endif 3125 3126 case DIF_VAR_UID: 3127 if (!dtrace_priv_proc(state)) 3128 return (0); 3129 3130#if defined(sun) 3131 /* 3132 * See comment in DIF_VAR_PID. 3133 */ 3134 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3135 return ((uint64_t)p0.p_cred->cr_uid); 3136 3137 /* 3138 * It is always safe to dereference one's own t_procp pointer: 3139 * it always points to a valid, allocated proc structure. 3140 * (This is true because threads don't clean up their own 3141 * state -- they leave that task to whomever reaps them.) 3142 * 3143 * Additionally, it is safe to dereference one's own process 3144 * credential, since this is never NULL after process birth. 3145 */ 3146 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3147#else 3148 return (uint64_t)kauth_cred_getuid(curthread->t_procp->p_cred); 3149#endif 3150 3151 case DIF_VAR_GID: 3152 if (!dtrace_priv_proc(state)) 3153 return (0); 3154 3155#if defined(sun) 3156 /* 3157 * See comment in DIF_VAR_PID. 3158 */ 3159 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3160 return ((uint64_t)p0.p_cred->cr_gid); 3161 3162 /* 3163 * It is always safe to dereference one's own t_procp pointer: 3164 * it always points to a valid, allocated proc structure. 3165 * (This is true because threads don't clean up their own 3166 * state -- they leave that task to whomever reaps them.) 3167 * 3168 * Additionally, it is safe to dereference one's own process 3169 * credential, since this is never NULL after process birth. 3170 */ 3171 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3172#else 3173 return (uint64_t)kauth_cred_getgid(curthread->t_procp->p_cred); 3174#endif 3175 3176 case DIF_VAR_ERRNO: { 3177#if defined(sun) 3178 klwp_t *lwp; 3179 if (!dtrace_priv_proc(state)) 3180 return (0); 3181 3182 /* 3183 * See comment in DIF_VAR_PID. 3184 */ 3185 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3186 return (0); 3187 3188 /* 3189 * It is always safe to dereference one's own t_lwp pointer in 3190 * the event that this pointer is non-NULL. (This is true 3191 * because threads and lwps don't clean up their own state -- 3192 * they leave that task to whomever reaps them.) 3193 */ 3194 if ((lwp = curthread->t_lwp) == NULL) 3195 return (0); 3196 3197 return ((uint64_t)lwp->lwp_errno); 3198#else 3199#if 0 3200 return (curthread->l_errno); 3201#else 3202 return 0; /* XXX TBD errno support at lwp level? */ 3203#endif 3204#endif 3205 } 3206 default: 3207 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3208 return (0); 3209 } 3210} 3211 3212/* 3213 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3214 * Notice that we don't bother validating the proper number of arguments or 3215 * their types in the tuple stack. This isn't needed because all argument 3216 * interpretation is safe because of our load safety -- the worst that can 3217 * happen is that a bogus program can obtain bogus results. 3218 */ 3219static void 3220dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3221 dtrace_key_t *tupregs, int nargs, 3222 dtrace_mstate_t *mstate, dtrace_state_t *state) 3223{ 3224 volatile uint16_t *flags = &cpu_core[curcpu_id].cpuc_dtrace_flags; 3225 volatile uintptr_t *illval = &cpu_core[curcpu_id].cpuc_dtrace_illval; 3226 dtrace_vstate_t *vstate = &state->dts_vstate; 3227 3228#if defined(sun) 3229 union { 3230 mutex_impl_t mi; 3231 uint64_t mx; 3232 } m; 3233 3234 union { 3235 krwlock_t ri; 3236 uintptr_t rw; 3237 } r; 3238#else 3239 union { 3240 kmutex_t mi; 3241 uint64_t mx; 3242 } m; 3243 3244 union { 3245 krwlock_t ri; 3246 uintptr_t rw; 3247 } r; 3248#endif 3249 3250 switch (subr) { 3251 case DIF_SUBR_RAND: 3252 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3253 break; 3254 3255#if defined(sun) 3256 case DIF_SUBR_MUTEX_OWNED: 3257 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3258 mstate, vstate)) { 3259 regs[rd] = 0; 3260 break; 3261 } 3262 3263 m.mx = dtrace_load64(tupregs[0].dttk_value); 3264 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3265 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3266 else 3267 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3268 break; 3269 3270 case DIF_SUBR_MUTEX_OWNER: 3271 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3272 mstate, vstate)) { 3273 regs[rd] = 0; 3274 break; 3275 } 3276 3277 m.mx = dtrace_load64(tupregs[0].dttk_value); 3278 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3279 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3280 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3281 else 3282 regs[rd] = 0; 3283 break; 3284 3285 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3286 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3287 mstate, vstate)) { 3288 regs[rd] = 0; 3289 break; 3290 } 3291 3292 m.mx = dtrace_load64(tupregs[0].dttk_value); 3293 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3294 break; 3295 3296 case DIF_SUBR_MUTEX_TYPE_SPIN: 3297 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3298 mstate, vstate)) { 3299 regs[rd] = 0; 3300 break; 3301 } 3302 3303 m.mx = dtrace_load64(tupregs[0].dttk_value); 3304 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3305 break; 3306 3307 case DIF_SUBR_RW_READ_HELD: { 3308 uintptr_t tmp; 3309 3310 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3311 mstate, vstate)) { 3312 regs[rd] = 0; 3313 break; 3314 } 3315 3316 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3317 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3318 break; 3319 } 3320 3321 case DIF_SUBR_RW_WRITE_HELD: 3322 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3323 mstate, vstate)) { 3324 regs[rd] = 0; 3325 break; 3326 } 3327 3328 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3329 regs[rd] = _RW_WRITE_HELD(&r.ri); 3330 break; 3331 3332 case DIF_SUBR_RW_ISWRITER: 3333 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3334 mstate, vstate)) { 3335 regs[rd] = 0; 3336 break; 3337 } 3338 3339 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3340 regs[rd] = _RW_ISWRITER(&r.ri); 3341 break; 3342 3343#else 3344 case DIF_SUBR_MUTEX_OWNED: 3345 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3346 mstate, vstate)) { 3347 regs[rd] = 0; 3348 break; 3349 } 3350 3351 m.mx = dtrace_load64(tupregs[0].dttk_value); 3352 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3353 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3354 else 3355 regs[rd] = __SIMPLELOCK_LOCKED_P(&m.mi.mtx_lock); 3356 break; 3357 3358 case DIF_SUBR_MUTEX_OWNER: 3359 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3360 mstate, vstate)) { 3361 regs[rd] = 0; 3362 break; 3363 } 3364 3365 m.mx = dtrace_load64(tupregs[0].dttk_value); 3366 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3367 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3368 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3369 else 3370 regs[rd] = 0; 3371 break; 3372 3373 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3374 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3375 mstate, vstate)) { 3376 regs[rd] = 0; 3377 break; 3378 } 3379 3380 m.mx = dtrace_load64(tupregs[0].dttk_value); 3381 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3382 break; 3383 3384 case DIF_SUBR_MUTEX_TYPE_SPIN: 3385 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3386 mstate, vstate)) { 3387 regs[rd] = 0; 3388 break; 3389 } 3390 3391 m.mx = dtrace_load64(tupregs[0].dttk_value); 3392 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3393 break; 3394 3395 case DIF_SUBR_RW_READ_HELD: { 3396 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3397 mstate, vstate)) { 3398 regs[rd] = 0; 3399 break; 3400 } 3401 3402 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3403 regs[rd] = _RW_READ_HELD(&r.ri); 3404 break; 3405 } 3406 3407 case DIF_SUBR_RW_WRITE_HELD: 3408 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3409 mstate, vstate)) { 3410 regs[rd] = 0; 3411 break; 3412 } 3413 3414 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3415 regs[rd] = _RW_WRITE_HELD(&r.ri); 3416 break; 3417 3418 case DIF_SUBR_RW_ISWRITER: 3419 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3420 mstate, vstate)) { 3421 regs[rd] = 0; 3422 break; 3423 } 3424 3425 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3426 regs[rd] = _RW_ISWRITER(&r.ri); 3427 break; 3428 3429#endif /* ! defined(sun) */ 3430 3431 case DIF_SUBR_BCOPY: { 3432 /* 3433 * We need to be sure that the destination is in the scratch 3434 * region -- no other region is allowed. 3435 */ 3436 uintptr_t src = tupregs[0].dttk_value; 3437 uintptr_t dest = tupregs[1].dttk_value; 3438 size_t size = tupregs[2].dttk_value; 3439 3440 if (!dtrace_inscratch(dest, size, mstate)) { 3441 *flags |= CPU_DTRACE_BADADDR; 3442 *illval = regs[rd]; 3443 break; 3444 } 3445 3446 if (!dtrace_canload(src, size, mstate, vstate)) { 3447 regs[rd] = 0; 3448 break; 3449 } 3450 3451 dtrace_bcopy((void *)src, (void *)dest, size); 3452 break; 3453 } 3454 3455 case DIF_SUBR_ALLOCA: 3456 case DIF_SUBR_COPYIN: { 3457 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3458 uint64_t size = 3459 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3460 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3461 3462 /* 3463 * This action doesn't require any credential checks since 3464 * probes will not activate in user contexts to which the 3465 * enabling user does not have permissions. 3466 */ 3467 3468 /* 3469 * Rounding up the user allocation size could have overflowed 3470 * a large, bogus allocation (like -1ULL) to 0. 3471 */ 3472 if (scratch_size < size || 3473 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3474 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3475 regs[rd] = 0; 3476 break; 3477 } 3478 3479 if (subr == DIF_SUBR_COPYIN) { 3480 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3481 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3482 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3483 } 3484 3485 mstate->dtms_scratch_ptr += scratch_size; 3486 regs[rd] = dest; 3487 break; 3488 } 3489 3490 case DIF_SUBR_COPYINTO: { 3491 uint64_t size = tupregs[1].dttk_value; 3492 uintptr_t dest = tupregs[2].dttk_value; 3493 3494 /* 3495 * This action doesn't require any credential checks since 3496 * probes will not activate in user contexts to which the 3497 * enabling user does not have permissions. 3498 */ 3499 if (!dtrace_inscratch(dest, size, mstate)) { 3500 *flags |= CPU_DTRACE_BADADDR; 3501 *illval = regs[rd]; 3502 break; 3503 } 3504 3505 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3506 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3507 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3508 break; 3509 } 3510 3511 case DIF_SUBR_COPYINSTR: { 3512 uintptr_t dest = mstate->dtms_scratch_ptr; 3513 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3514 3515 if (nargs > 1 && tupregs[1].dttk_value < size) 3516 size = tupregs[1].dttk_value + 1; 3517 3518 /* 3519 * This action doesn't require any credential checks since 3520 * probes will not activate in user contexts to which the 3521 * enabling user does not have permissions. 3522 */ 3523 if (!DTRACE_INSCRATCH(mstate, size)) { 3524 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3525 regs[rd] = 0; 3526 break; 3527 } 3528 3529 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3530 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3531 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3532 3533 ((char *)dest)[size - 1] = '\0'; 3534 mstate->dtms_scratch_ptr += size; 3535 regs[rd] = dest; 3536 break; 3537 } 3538 3539#if defined(sun) 3540 case DIF_SUBR_MSGSIZE: 3541 case DIF_SUBR_MSGDSIZE: { 3542 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3543 uintptr_t wptr, rptr; 3544 size_t count = 0; 3545 int cont = 0; 3546 3547 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 3548 3549 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3550 vstate)) { 3551 regs[rd] = 0; 3552 break; 3553 } 3554 3555 wptr = dtrace_loadptr(baddr + 3556 offsetof(mblk_t, b_wptr)); 3557 3558 rptr = dtrace_loadptr(baddr + 3559 offsetof(mblk_t, b_rptr)); 3560 3561 if (wptr < rptr) { 3562 *flags |= CPU_DTRACE_BADADDR; 3563 *illval = tupregs[0].dttk_value; 3564 break; 3565 } 3566 3567 daddr = dtrace_loadptr(baddr + 3568 offsetof(mblk_t, b_datap)); 3569 3570 baddr = dtrace_loadptr(baddr + 3571 offsetof(mblk_t, b_cont)); 3572 3573 /* 3574 * We want to prevent against denial-of-service here, 3575 * so we're only going to search the list for 3576 * dtrace_msgdsize_max mblks. 3577 */ 3578 if (cont++ > dtrace_msgdsize_max) { 3579 *flags |= CPU_DTRACE_ILLOP; 3580 break; 3581 } 3582 3583 if (subr == DIF_SUBR_MSGDSIZE) { 3584 if (dtrace_load8(daddr + 3585 offsetof(dblk_t, db_type)) != M_DATA) 3586 continue; 3587 } 3588 3589 count += wptr - rptr; 3590 } 3591 3592 if (!(*flags & CPU_DTRACE_FAULT)) 3593 regs[rd] = count; 3594 3595 break; 3596 } 3597#endif 3598 3599 case DIF_SUBR_PROGENYOF: { 3600 pid_t pid = tupregs[0].dttk_value; 3601 proc_t *p; 3602 int rval = 0; 3603 3604 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3605 3606 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3607#if defined(sun) 3608 if (p->p_pidp->pid_id == pid) { 3609#else 3610 if (p->p_pid == pid) { 3611#endif 3612 rval = 1; 3613 break; 3614 } 3615 } 3616 3617 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3618 3619 regs[rd] = rval; 3620 break; 3621 } 3622 3623 case DIF_SUBR_SPECULATION: 3624 regs[rd] = dtrace_speculation(state); 3625 break; 3626 3627 case DIF_SUBR_COPYOUT: { 3628 uintptr_t kaddr = tupregs[0].dttk_value; 3629 uintptr_t uaddr = tupregs[1].dttk_value; 3630 uint64_t size = tupregs[2].dttk_value; 3631 3632 if (!dtrace_destructive_disallow && 3633 dtrace_priv_proc_control(state) && 3634 !dtrace_istoxic(kaddr, size)) { 3635 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3636 dtrace_copyout(kaddr, uaddr, size, flags); 3637 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3638 } 3639 break; 3640 } 3641 3642 case DIF_SUBR_COPYOUTSTR: { 3643 uintptr_t kaddr = tupregs[0].dttk_value; 3644 uintptr_t uaddr = tupregs[1].dttk_value; 3645 uint64_t size = tupregs[2].dttk_value; 3646 3647 if (!dtrace_destructive_disallow && 3648 dtrace_priv_proc_control(state) && 3649 !dtrace_istoxic(kaddr, size)) { 3650 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3651 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3652 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3653 } 3654 break; 3655 } 3656 3657 case DIF_SUBR_STRLEN: { 3658 size_t sz; 3659 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3660 sz = dtrace_strlen((char *)addr, 3661 state->dts_options[DTRACEOPT_STRSIZE]); 3662 3663 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3664 regs[rd] = 0; 3665 break; 3666 } 3667 3668 regs[rd] = sz; 3669 3670 break; 3671 } 3672 3673 case DIF_SUBR_STRCHR: 3674 case DIF_SUBR_STRRCHR: { 3675 /* 3676 * We're going to iterate over the string looking for the 3677 * specified character. We will iterate until we have reached 3678 * the string length or we have found the character. If this 3679 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3680 * of the specified character instead of the first. 3681 */ 3682 uintptr_t saddr = tupregs[0].dttk_value; 3683 uintptr_t addr = tupregs[0].dttk_value; 3684 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3685 char c, target = (char)tupregs[1].dttk_value; 3686 3687 for (regs[rd] = 0; addr < limit; addr++) { 3688 if ((c = dtrace_load8(addr)) == target) { 3689 regs[rd] = addr; 3690 3691 if (subr == DIF_SUBR_STRCHR) 3692 break; 3693 } 3694 3695 if (c == '\0') 3696 break; 3697 } 3698 3699 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3700 regs[rd] = 0; 3701 break; 3702 } 3703 3704 break; 3705 } 3706 3707 case DIF_SUBR_STRSTR: 3708 case DIF_SUBR_INDEX: 3709 case DIF_SUBR_RINDEX: { 3710 /* 3711 * We're going to iterate over the string looking for the 3712 * specified string. We will iterate until we have reached 3713 * the string length or we have found the string. (Yes, this 3714 * is done in the most naive way possible -- but considering 3715 * that the string we're searching for is likely to be 3716 * relatively short, the complexity of Rabin-Karp or similar 3717 * hardly seems merited.) 3718 */ 3719 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3720 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3721 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3722 size_t len = dtrace_strlen(addr, size); 3723 size_t sublen = dtrace_strlen(substr, size); 3724 char *limit = addr + len, *orig = addr; 3725 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3726 int inc = 1; 3727 3728 regs[rd] = notfound; 3729 3730 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3731 regs[rd] = 0; 3732 break; 3733 } 3734 3735 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3736 vstate)) { 3737 regs[rd] = 0; 3738 break; 3739 } 3740 3741 /* 3742 * strstr() and index()/rindex() have similar semantics if 3743 * both strings are the empty string: strstr() returns a 3744 * pointer to the (empty) string, and index() and rindex() 3745 * both return index 0 (regardless of any position argument). 3746 */ 3747 if (sublen == 0 && len == 0) { 3748 if (subr == DIF_SUBR_STRSTR) 3749 regs[rd] = (uintptr_t)addr; 3750 else 3751 regs[rd] = 0; 3752 break; 3753 } 3754 3755 if (subr != DIF_SUBR_STRSTR) { 3756 if (subr == DIF_SUBR_RINDEX) { 3757 limit = orig - 1; 3758 addr += len; 3759 inc = -1; 3760 } 3761 3762 /* 3763 * Both index() and rindex() take an optional position 3764 * argument that denotes the starting position. 3765 */ 3766 if (nargs == 3) { 3767 int64_t pos = (int64_t)tupregs[2].dttk_value; 3768 3769 /* 3770 * If the position argument to index() is 3771 * negative, Perl implicitly clamps it at 3772 * zero. This semantic is a little surprising 3773 * given the special meaning of negative 3774 * positions to similar Perl functions like 3775 * substr(), but it appears to reflect a 3776 * notion that index() can start from a 3777 * negative index and increment its way up to 3778 * the string. Given this notion, Perl's 3779 * rindex() is at least self-consistent in 3780 * that it implicitly clamps positions greater 3781 * than the string length to be the string 3782 * length. Where Perl completely loses 3783 * coherence, however, is when the specified 3784 * substring is the empty string (""). In 3785 * this case, even if the position is 3786 * negative, rindex() returns 0 -- and even if 3787 * the position is greater than the length, 3788 * index() returns the string length. These 3789 * semantics violate the notion that index() 3790 * should never return a value less than the 3791 * specified position and that rindex() should 3792 * never return a value greater than the 3793 * specified position. (One assumes that 3794 * these semantics are artifacts of Perl's 3795 * implementation and not the results of 3796 * deliberate design -- it beggars belief that 3797 * even Larry Wall could desire such oddness.) 3798 * While in the abstract one would wish for 3799 * consistent position semantics across 3800 * substr(), index() and rindex() -- or at the 3801 * very least self-consistent position 3802 * semantics for index() and rindex() -- we 3803 * instead opt to keep with the extant Perl 3804 * semantics, in all their broken glory. (Do 3805 * we have more desire to maintain Perl's 3806 * semantics than Perl does? Probably.) 3807 */ 3808 if (subr == DIF_SUBR_RINDEX) { 3809 if (pos < 0) { 3810 if (sublen == 0) 3811 regs[rd] = 0; 3812 break; 3813 } 3814 3815 if (pos > len) 3816 pos = len; 3817 } else { 3818 if (pos < 0) 3819 pos = 0; 3820 3821 if (pos >= len) { 3822 if (sublen == 0) 3823 regs[rd] = len; 3824 break; 3825 } 3826 } 3827 3828 addr = orig + pos; 3829 } 3830 } 3831 3832 for (regs[rd] = notfound; addr != limit; addr += inc) { 3833 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3834 if (subr != DIF_SUBR_STRSTR) { 3835 /* 3836 * As D index() and rindex() are 3837 * modeled on Perl (and not on awk), 3838 * we return a zero-based (and not a 3839 * one-based) index. (For you Perl 3840 * weenies: no, we're not going to add 3841 * $[ -- and shouldn't you be at a con 3842 * or something?) 3843 */ 3844 regs[rd] = (uintptr_t)(addr - orig); 3845 break; 3846 } 3847 3848 ASSERT(subr == DIF_SUBR_STRSTR); 3849 regs[rd] = (uintptr_t)addr; 3850 break; 3851 } 3852 } 3853 3854 break; 3855 } 3856 3857 case DIF_SUBR_STRTOK: { 3858 uintptr_t addr = tupregs[0].dttk_value; 3859 uintptr_t tokaddr = tupregs[1].dttk_value; 3860 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3861 uintptr_t limit, toklimit = tokaddr + size; 3862 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 3863 char *dest = (char *)mstate->dtms_scratch_ptr; 3864 int i; 3865 3866 /* 3867 * Check both the token buffer and (later) the input buffer, 3868 * since both could be non-scratch addresses. 3869 */ 3870 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3871 regs[rd] = 0; 3872 break; 3873 } 3874 3875 if (!DTRACE_INSCRATCH(mstate, size)) { 3876 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3877 regs[rd] = 0; 3878 break; 3879 } 3880 3881 if (addr == 0) { 3882 /* 3883 * If the address specified is NULL, we use our saved 3884 * strtok pointer from the mstate. Note that this 3885 * means that the saved strtok pointer is _only_ 3886 * valid within multiple enablings of the same probe -- 3887 * it behaves like an implicit clause-local variable. 3888 */ 3889 addr = mstate->dtms_strtok; 3890 } else { 3891 /* 3892 * If the user-specified address is non-NULL we must 3893 * access check it. This is the only time we have 3894 * a chance to do so, since this address may reside 3895 * in the string table of this clause-- future calls 3896 * (when we fetch addr from mstate->dtms_strtok) 3897 * would fail this access check. 3898 */ 3899 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3900 regs[rd] = 0; 3901 break; 3902 } 3903 } 3904 3905 /* 3906 * First, zero the token map, and then process the token 3907 * string -- setting a bit in the map for every character 3908 * found in the token string. 3909 */ 3910 for (i = 0; i < sizeof (tokmap); i++) 3911 tokmap[i] = 0; 3912 3913 for (; tokaddr < toklimit; tokaddr++) { 3914 if ((c = dtrace_load8(tokaddr)) == '\0') 3915 break; 3916 3917 ASSERT((c >> 3) < sizeof (tokmap)); 3918 tokmap[c >> 3] |= (1 << (c & 0x7)); 3919 } 3920 3921 for (limit = addr + size; addr < limit; addr++) { 3922 /* 3923 * We're looking for a character that is _not_ contained 3924 * in the token string. 3925 */ 3926 if ((c = dtrace_load8(addr)) == '\0') 3927 break; 3928 3929 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3930 break; 3931 } 3932 3933 if (c == '\0') { 3934 /* 3935 * We reached the end of the string without finding 3936 * any character that was not in the token string. 3937 * We return NULL in this case, and we set the saved 3938 * address to NULL as well. 3939 */ 3940 regs[rd] = 0; 3941 mstate->dtms_strtok = 0; 3942 break; 3943 } 3944 3945 /* 3946 * From here on, we're copying into the destination string. 3947 */ 3948 for (i = 0; addr < limit && i < size - 1; addr++) { 3949 if ((c = dtrace_load8(addr)) == '\0') 3950 break; 3951 3952 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3953 break; 3954 3955 ASSERT(i < size); 3956 dest[i++] = c; 3957 } 3958 3959 ASSERT(i < size); 3960 dest[i] = '\0'; 3961 regs[rd] = (uintptr_t)dest; 3962 mstate->dtms_scratch_ptr += size; 3963 mstate->dtms_strtok = addr; 3964 break; 3965 } 3966 3967 case DIF_SUBR_SUBSTR: { 3968 uintptr_t s = tupregs[0].dttk_value; 3969 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3970 char *d = (char *)mstate->dtms_scratch_ptr; 3971 int64_t index = (int64_t)tupregs[1].dttk_value; 3972 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3973 size_t len = dtrace_strlen((char *)s, size); 3974 int64_t i = 0; 3975 3976 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3977 regs[rd] = 0; 3978 break; 3979 } 3980 3981 if (!DTRACE_INSCRATCH(mstate, size)) { 3982 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3983 regs[rd] = 0; 3984 break; 3985 } 3986 3987 if (nargs <= 2) 3988 remaining = (int64_t)size; 3989 3990 if (index < 0) { 3991 index += len; 3992 3993 if (index < 0 && index + remaining > 0) { 3994 remaining += index; 3995 index = 0; 3996 } 3997 } 3998 3999 if (index >= len || index < 0) { 4000 remaining = 0; 4001 } else if (remaining < 0) { 4002 remaining += len - index; 4003 } else if (index + remaining > size) { 4004 remaining = size - index; 4005 } 4006 4007 for (i = 0; i < remaining; i++) { 4008 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4009 break; 4010 } 4011 4012 d[i] = '\0'; 4013 4014 mstate->dtms_scratch_ptr += size; 4015 regs[rd] = (uintptr_t)d; 4016 break; 4017 } 4018 4019#if defined(sun) 4020 case DIF_SUBR_GETMAJOR: 4021#ifdef _LP64 4022 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 4023#else 4024 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 4025#endif 4026 break; 4027 4028 case DIF_SUBR_GETMINOR: 4029#ifdef _LP64 4030 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 4031#else 4032 regs[rd] = tupregs[0].dttk_value & MAXMIN; 4033#endif 4034 break; 4035 4036 case DIF_SUBR_DDI_PATHNAME: { 4037 /* 4038 * This one is a galactic mess. We are going to roughly 4039 * emulate ddi_pathname(), but it's made more complicated 4040 * by the fact that we (a) want to include the minor name and 4041 * (b) must proceed iteratively instead of recursively. 4042 */ 4043 uintptr_t dest = mstate->dtms_scratch_ptr; 4044 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4045 char *start = (char *)dest, *end = start + size - 1; 4046 uintptr_t daddr = tupregs[0].dttk_value; 4047 int64_t minor = (int64_t)tupregs[1].dttk_value; 4048 char *s; 4049 int i, len, depth = 0; 4050 4051 /* 4052 * Due to all the pointer jumping we do and context we must 4053 * rely upon, we just mandate that the user must have kernel 4054 * read privileges to use this routine. 4055 */ 4056 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 4057 *flags |= CPU_DTRACE_KPRIV; 4058 *illval = daddr; 4059 regs[rd] = 0; 4060 } 4061 4062 if (!DTRACE_INSCRATCH(mstate, size)) { 4063 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4064 regs[rd] = 0; 4065 break; 4066 } 4067 4068 *end = '\0'; 4069 4070 /* 4071 * We want to have a name for the minor. In order to do this, 4072 * we need to walk the minor list from the devinfo. We want 4073 * to be sure that we don't infinitely walk a circular list, 4074 * so we check for circularity by sending a scout pointer 4075 * ahead two elements for every element that we iterate over; 4076 * if the list is circular, these will ultimately point to the 4077 * same element. You may recognize this little trick as the 4078 * answer to a stupid interview question -- one that always 4079 * seems to be asked by those who had to have it laboriously 4080 * explained to them, and who can't even concisely describe 4081 * the conditions under which one would be forced to resort to 4082 * this technique. Needless to say, those conditions are 4083 * found here -- and probably only here. Is this the only use 4084 * of this infamous trick in shipping, production code? If it 4085 * isn't, it probably should be... 4086 */ 4087 if (minor != -1) { 4088 uintptr_t maddr = dtrace_loadptr(daddr + 4089 offsetof(struct dev_info, devi_minor)); 4090 4091 uintptr_t next = offsetof(struct ddi_minor_data, next); 4092 uintptr_t name = offsetof(struct ddi_minor_data, 4093 d_minor) + offsetof(struct ddi_minor, name); 4094 uintptr_t dev = offsetof(struct ddi_minor_data, 4095 d_minor) + offsetof(struct ddi_minor, dev); 4096 uintptr_t scout; 4097 4098 if (maddr != NULL) 4099 scout = dtrace_loadptr(maddr + next); 4100 4101 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4102 uint64_t m; 4103#ifdef _LP64 4104 m = dtrace_load64(maddr + dev) & MAXMIN64; 4105#else 4106 m = dtrace_load32(maddr + dev) & MAXMIN; 4107#endif 4108 if (m != minor) { 4109 maddr = dtrace_loadptr(maddr + next); 4110 4111 if (scout == NULL) 4112 continue; 4113 4114 scout = dtrace_loadptr(scout + next); 4115 4116 if (scout == NULL) 4117 continue; 4118 4119 scout = dtrace_loadptr(scout + next); 4120 4121 if (scout == NULL) 4122 continue; 4123 4124 if (scout == maddr) { 4125 *flags |= CPU_DTRACE_ILLOP; 4126 break; 4127 } 4128 4129 continue; 4130 } 4131 4132 /* 4133 * We have the minor data. Now we need to 4134 * copy the minor's name into the end of the 4135 * pathname. 4136 */ 4137 s = (char *)dtrace_loadptr(maddr + name); 4138 len = dtrace_strlen(s, size); 4139 4140 if (*flags & CPU_DTRACE_FAULT) 4141 break; 4142 4143 if (len != 0) { 4144 if ((end -= (len + 1)) < start) 4145 break; 4146 4147 *end = ':'; 4148 } 4149 4150 for (i = 1; i <= len; i++) 4151 end[i] = dtrace_load8((uintptr_t)s++); 4152 break; 4153 } 4154 } 4155 4156 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4157 ddi_node_state_t devi_state; 4158 4159 devi_state = dtrace_load32(daddr + 4160 offsetof(struct dev_info, devi_node_state)); 4161 4162 if (*flags & CPU_DTRACE_FAULT) 4163 break; 4164 4165 if (devi_state >= DS_INITIALIZED) { 4166 s = (char *)dtrace_loadptr(daddr + 4167 offsetof(struct dev_info, devi_addr)); 4168 len = dtrace_strlen(s, size); 4169 4170 if (*flags & CPU_DTRACE_FAULT) 4171 break; 4172 4173 if (len != 0) { 4174 if ((end -= (len + 1)) < start) 4175 break; 4176 4177 *end = '@'; 4178 } 4179 4180 for (i = 1; i <= len; i++) 4181 end[i] = dtrace_load8((uintptr_t)s++); 4182 } 4183 4184 /* 4185 * Now for the node name... 4186 */ 4187 s = (char *)dtrace_loadptr(daddr + 4188 offsetof(struct dev_info, devi_node_name)); 4189 4190 daddr = dtrace_loadptr(daddr + 4191 offsetof(struct dev_info, devi_parent)); 4192 4193 /* 4194 * If our parent is NULL (that is, if we're the root 4195 * node), we're going to use the special path 4196 * "devices". 4197 */ 4198 if (daddr == 0) 4199 s = "devices"; 4200 4201 len = dtrace_strlen(s, size); 4202 if (*flags & CPU_DTRACE_FAULT) 4203 break; 4204 4205 if ((end -= (len + 1)) < start) 4206 break; 4207 4208 for (i = 1; i <= len; i++) 4209 end[i] = dtrace_load8((uintptr_t)s++); 4210 *end = '/'; 4211 4212 if (depth++ > dtrace_devdepth_max) { 4213 *flags |= CPU_DTRACE_ILLOP; 4214 break; 4215 } 4216 } 4217 4218 if (end < start) 4219 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4220 4221 if (daddr == 0) { 4222 regs[rd] = (uintptr_t)end; 4223 mstate->dtms_scratch_ptr += size; 4224 } 4225 4226 break; 4227 } 4228#endif 4229 4230 case DIF_SUBR_STRJOIN: { 4231 char *d = (char *)mstate->dtms_scratch_ptr; 4232 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4233 uintptr_t s1 = tupregs[0].dttk_value; 4234 uintptr_t s2 = tupregs[1].dttk_value; 4235 int i = 0; 4236 4237 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4238 !dtrace_strcanload(s2, size, mstate, vstate)) { 4239 regs[rd] = 0; 4240 break; 4241 } 4242 4243 if (!DTRACE_INSCRATCH(mstate, size)) { 4244 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4245 regs[rd] = 0; 4246 break; 4247 } 4248 4249 for (;;) { 4250 if (i >= size) { 4251 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4252 regs[rd] = 0; 4253 break; 4254 } 4255 4256 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4257 i--; 4258 break; 4259 } 4260 } 4261 4262 for (;;) { 4263 if (i >= size) { 4264 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4265 regs[rd] = 0; 4266 break; 4267 } 4268 4269 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4270 break; 4271 } 4272 4273 if (i < size) { 4274 mstate->dtms_scratch_ptr += i; 4275 regs[rd] = (uintptr_t)d; 4276 } 4277 4278 break; 4279 } 4280 4281 case DIF_SUBR_LLTOSTR: { 4282 int64_t i = (int64_t)tupregs[0].dttk_value; 4283 int64_t val = i < 0 ? i * -1 : i; 4284 uint64_t size = 22; /* enough room for 2^64 in decimal */ 4285 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4286 4287 if (!DTRACE_INSCRATCH(mstate, size)) { 4288 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4289 regs[rd] = 0; 4290 break; 4291 } 4292 4293 for (*end-- = '\0'; val; val /= 10) 4294 *end-- = '0' + (val % 10); 4295 4296 if (i == 0) 4297 *end-- = '0'; 4298 4299 if (i < 0) 4300 *end-- = '-'; 4301 4302 regs[rd] = (uintptr_t)end + 1; 4303 mstate->dtms_scratch_ptr += size; 4304 break; 4305 } 4306 4307 case DIF_SUBR_HTONS: 4308 case DIF_SUBR_NTOHS: 4309#if BYTE_ORDER == BIG_ENDIAN 4310 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4311#else 4312 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4313#endif 4314 break; 4315 4316 4317 case DIF_SUBR_HTONL: 4318 case DIF_SUBR_NTOHL: 4319#if BYTE_ORDER == BIG_ENDIAN 4320 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4321#else 4322 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4323#endif 4324 break; 4325 4326 4327 case DIF_SUBR_HTONLL: 4328 case DIF_SUBR_NTOHLL: 4329#if BYTE_ORDER == BIG_ENDIAN 4330 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4331#else 4332 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4333#endif 4334 break; 4335 4336 4337 case DIF_SUBR_DIRNAME: 4338 case DIF_SUBR_BASENAME: { 4339 char *dest = (char *)mstate->dtms_scratch_ptr; 4340 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4341 uintptr_t src = tupregs[0].dttk_value; 4342 int i, j, len = dtrace_strlen((char *)src, size); 4343 int lastbase = -1, firstbase = -1, lastdir = -1; 4344 int start, end; 4345 4346 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4347 regs[rd] = 0; 4348 break; 4349 } 4350 4351 if (!DTRACE_INSCRATCH(mstate, size)) { 4352 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4353 regs[rd] = 0; 4354 break; 4355 } 4356 4357 /* 4358 * The basename and dirname for a zero-length string is 4359 * defined to be "." 4360 */ 4361 if (len == 0) { 4362 len = 1; 4363 src = (uintptr_t)"."; 4364 } 4365 4366 /* 4367 * Start from the back of the string, moving back toward the 4368 * front until we see a character that isn't a slash. That 4369 * character is the last character in the basename. 4370 */ 4371 for (i = len - 1; i >= 0; i--) { 4372 if (dtrace_load8(src + i) != '/') 4373 break; 4374 } 4375 4376 if (i >= 0) 4377 lastbase = i; 4378 4379 /* 4380 * Starting from the last character in the basename, move 4381 * towards the front until we find a slash. The character 4382 * that we processed immediately before that is the first 4383 * character in the basename. 4384 */ 4385 for (; i >= 0; i--) { 4386 if (dtrace_load8(src + i) == '/') 4387 break; 4388 } 4389 4390 if (i >= 0) 4391 firstbase = i + 1; 4392 4393 /* 4394 * Now keep going until we find a non-slash character. That 4395 * character is the last character in the dirname. 4396 */ 4397 for (; i >= 0; i--) { 4398 if (dtrace_load8(src + i) != '/') 4399 break; 4400 } 4401 4402 if (i >= 0) 4403 lastdir = i; 4404 4405 ASSERT(!(lastbase == -1 && firstbase != -1)); 4406 ASSERT(!(firstbase == -1 && lastdir != -1)); 4407 4408 if (lastbase == -1) { 4409 /* 4410 * We didn't find a non-slash character. We know that 4411 * the length is non-zero, so the whole string must be 4412 * slashes. In either the dirname or the basename 4413 * case, we return '/'. 4414 */ 4415 ASSERT(firstbase == -1); 4416 firstbase = lastbase = lastdir = 0; 4417 } 4418 4419 if (firstbase == -1) { 4420 /* 4421 * The entire string consists only of a basename 4422 * component. If we're looking for dirname, we need 4423 * to change our string to be just "."; if we're 4424 * looking for a basename, we'll just set the first 4425 * character of the basename to be 0. 4426 */ 4427 if (subr == DIF_SUBR_DIRNAME) { 4428 ASSERT(lastdir == -1); 4429 src = (uintptr_t)"."; 4430 lastdir = 0; 4431 } else { 4432 firstbase = 0; 4433 } 4434 } 4435 4436 if (subr == DIF_SUBR_DIRNAME) { 4437 if (lastdir == -1) { 4438 /* 4439 * We know that we have a slash in the name -- 4440 * or lastdir would be set to 0, above. And 4441 * because lastdir is -1, we know that this 4442 * slash must be the first character. (That 4443 * is, the full string must be of the form 4444 * "/basename".) In this case, the last 4445 * character of the directory name is 0. 4446 */ 4447 lastdir = 0; 4448 } 4449 4450 start = 0; 4451 end = lastdir; 4452 } else { 4453 ASSERT(subr == DIF_SUBR_BASENAME); 4454 ASSERT(firstbase != -1 && lastbase != -1); 4455 start = firstbase; 4456 end = lastbase; 4457 } 4458 4459 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4460 dest[j] = dtrace_load8(src + i); 4461 4462 dest[j] = '\0'; 4463 regs[rd] = (uintptr_t)dest; 4464 mstate->dtms_scratch_ptr += size; 4465 break; 4466 } 4467 4468 case DIF_SUBR_CLEANPATH: { 4469 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4470 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4471 uintptr_t src = tupregs[0].dttk_value; 4472 int i = 0, j = 0; 4473 4474 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4475 regs[rd] = 0; 4476 break; 4477 } 4478 4479 if (!DTRACE_INSCRATCH(mstate, size)) { 4480 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4481 regs[rd] = 0; 4482 break; 4483 } 4484 4485 /* 4486 * Move forward, loading each character. 4487 */ 4488 do { 4489 c = dtrace_load8(src + i++); 4490next: 4491 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4492 break; 4493 4494 if (c != '/') { 4495 dest[j++] = c; 4496 continue; 4497 } 4498 4499 c = dtrace_load8(src + i++); 4500 4501 if (c == '/') { 4502 /* 4503 * We have two slashes -- we can just advance 4504 * to the next character. 4505 */ 4506 goto next; 4507 } 4508 4509 if (c != '.') { 4510 /* 4511 * This is not "." and it's not ".." -- we can 4512 * just store the "/" and this character and 4513 * drive on. 4514 */ 4515 dest[j++] = '/'; 4516 dest[j++] = c; 4517 continue; 4518 } 4519 4520 c = dtrace_load8(src + i++); 4521 4522 if (c == '/') { 4523 /* 4524 * This is a "/./" component. We're not going 4525 * to store anything in the destination buffer; 4526 * we're just going to go to the next component. 4527 */ 4528 goto next; 4529 } 4530 4531 if (c != '.') { 4532 /* 4533 * This is not ".." -- we can just store the 4534 * "/." and this character and continue 4535 * processing. 4536 */ 4537 dest[j++] = '/'; 4538 dest[j++] = '.'; 4539 dest[j++] = c; 4540 continue; 4541 } 4542 4543 c = dtrace_load8(src + i++); 4544 4545 if (c != '/' && c != '\0') { 4546 /* 4547 * This is not ".." -- it's "..[mumble]". 4548 * We'll store the "/.." and this character 4549 * and continue processing. 4550 */ 4551 dest[j++] = '/'; 4552 dest[j++] = '.'; 4553 dest[j++] = '.'; 4554 dest[j++] = c; 4555 continue; 4556 } 4557 4558 /* 4559 * This is "/../" or "/..\0". We need to back up 4560 * our destination pointer until we find a "/". 4561 */ 4562 i--; 4563 while (j != 0 && dest[--j] != '/') 4564 continue; 4565 4566 if (c == '\0') 4567 dest[++j] = '/'; 4568 } while (c != '\0'); 4569 4570 dest[j] = '\0'; 4571 regs[rd] = (uintptr_t)dest; 4572 mstate->dtms_scratch_ptr += size; 4573 break; 4574 } 4575 4576 case DIF_SUBR_INET_NTOA: 4577 case DIF_SUBR_INET_NTOA6: 4578 case DIF_SUBR_INET_NTOP: { 4579 size_t size; 4580 int af, argi, i; 4581 char *base, *end; 4582 4583 if (subr == DIF_SUBR_INET_NTOP) { 4584 af = (int)tupregs[0].dttk_value; 4585 argi = 1; 4586 } else { 4587 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4588 argi = 0; 4589 } 4590 4591 if (af == AF_INET) { 4592 ipaddr_t ip4; 4593 uint8_t *ptr8, val; 4594 4595 /* 4596 * Safely load the IPv4 address. 4597 */ 4598 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4599 4600 /* 4601 * Check an IPv4 string will fit in scratch. 4602 */ 4603 size = INET_ADDRSTRLEN; 4604 if (!DTRACE_INSCRATCH(mstate, size)) { 4605 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4606 regs[rd] = 0; 4607 break; 4608 } 4609 base = (char *)mstate->dtms_scratch_ptr; 4610 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4611 4612 /* 4613 * Stringify as a dotted decimal quad. 4614 */ 4615 *end-- = '\0'; 4616 ptr8 = (uint8_t *)&ip4; 4617 for (i = 3; i >= 0; i--) { 4618 val = ptr8[i]; 4619 4620 if (val == 0) { 4621 *end-- = '0'; 4622 } else { 4623 for (; val; val /= 10) { 4624 *end-- = '0' + (val % 10); 4625 } 4626 } 4627 4628 if (i > 0) 4629 *end-- = '.'; 4630 } 4631 ASSERT(end + 1 >= base); 4632 4633 } else if (af == AF_INET6) { 4634 struct in6_addr ip6; 4635 int firstzero, tryzero, numzero, v6end; 4636 uint16_t val; 4637 const char digits[] = "0123456789abcdef"; 4638 4639 /* 4640 * Stringify using RFC 1884 convention 2 - 16 bit 4641 * hexadecimal values with a zero-run compression. 4642 * Lower case hexadecimal digits are used. 4643 * eg, fe80::214:4fff:fe0b:76c8. 4644 * The IPv4 embedded form is returned for inet_ntop, 4645 * just the IPv4 string is returned for inet_ntoa6. 4646 */ 4647 4648 /* 4649 * Safely load the IPv6 address. 4650 */ 4651 dtrace_bcopy( 4652 (void *)(uintptr_t)tupregs[argi].dttk_value, 4653 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4654 4655 /* 4656 * Check an IPv6 string will fit in scratch. 4657 */ 4658 size = INET6_ADDRSTRLEN; 4659 if (!DTRACE_INSCRATCH(mstate, size)) { 4660 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4661 regs[rd] = 0; 4662 break; 4663 } 4664 base = (char *)mstate->dtms_scratch_ptr; 4665 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4666 *end-- = '\0'; 4667 4668 /* 4669 * Find the longest run of 16 bit zero values 4670 * for the single allowed zero compression - "::". 4671 */ 4672 firstzero = -1; 4673 tryzero = -1; 4674 numzero = 1; 4675 for (i = 0; i < sizeof (struct in6_addr); i++) { 4676#if defined(sun) 4677 if (ip6._S6_un._S6_u8[i] == 0 && 4678#else 4679 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4680#endif 4681 tryzero == -1 && i % 2 == 0) { 4682 tryzero = i; 4683 continue; 4684 } 4685 4686 if (tryzero != -1 && 4687#if defined(sun) 4688 (ip6._S6_un._S6_u8[i] != 0 || 4689#else 4690 (ip6.__u6_addr.__u6_addr8[i] != 0 || 4691#endif 4692 i == sizeof (struct in6_addr) - 1)) { 4693 4694 if (i - tryzero <= numzero) { 4695 tryzero = -1; 4696 continue; 4697 } 4698 4699 firstzero = tryzero; 4700 numzero = i - i % 2 - tryzero; 4701 tryzero = -1; 4702 4703#if defined(sun) 4704 if (ip6._S6_un._S6_u8[i] == 0 && 4705#else 4706 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4707#endif 4708 i == sizeof (struct in6_addr) - 1) 4709 numzero += 2; 4710 } 4711 } 4712 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4713 4714 /* 4715 * Check for an IPv4 embedded address. 4716 */ 4717 v6end = sizeof (struct in6_addr) - 2; 4718 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4719 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4720 for (i = sizeof (struct in6_addr) - 1; 4721 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4722 ASSERT(end >= base); 4723 4724#if defined(sun) 4725 val = ip6._S6_un._S6_u8[i]; 4726#else 4727 val = ip6.__u6_addr.__u6_addr8[i]; 4728#endif 4729 4730 if (val == 0) { 4731 *end-- = '0'; 4732 } else { 4733 for (; val; val /= 10) { 4734 *end-- = '0' + val % 10; 4735 } 4736 } 4737 4738 if (i > DTRACE_V4MAPPED_OFFSET) 4739 *end-- = '.'; 4740 } 4741 4742 if (subr == DIF_SUBR_INET_NTOA6) 4743 goto inetout; 4744 4745 /* 4746 * Set v6end to skip the IPv4 address that 4747 * we have already stringified. 4748 */ 4749 v6end = 10; 4750 } 4751 4752 /* 4753 * Build the IPv6 string by working through the 4754 * address in reverse. 4755 */ 4756 for (i = v6end; i >= 0; i -= 2) { 4757 ASSERT(end >= base); 4758 4759 if (i == firstzero + numzero - 2) { 4760 *end-- = ':'; 4761 *end-- = ':'; 4762 i -= numzero - 2; 4763 continue; 4764 } 4765 4766 if (i < 14 && i != firstzero - 2) 4767 *end-- = ':'; 4768 4769#if defined(sun) 4770 val = (ip6._S6_un._S6_u8[i] << 8) + 4771 ip6._S6_un._S6_u8[i + 1]; 4772#else 4773 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 4774 ip6.__u6_addr.__u6_addr8[i + 1]; 4775#endif 4776 4777 if (val == 0) { 4778 *end-- = '0'; 4779 } else { 4780 for (; val; val /= 16) { 4781 *end-- = digits[val % 16]; 4782 } 4783 } 4784 } 4785 ASSERT(end + 1 >= base); 4786 4787 } else { 4788 /* 4789 * The user didn't use AH_INET or AH_INET6. 4790 */ 4791 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4792 regs[rd] = 0; 4793 break; 4794 } 4795 4796inetout: regs[rd] = (uintptr_t)end + 1; 4797 mstate->dtms_scratch_ptr += size; 4798 break; 4799 } 4800 4801 case DIF_SUBR_MEMREF: { 4802 uintptr_t size = 2 * sizeof(uintptr_t); 4803 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4804 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 4805 4806 /* address and length */ 4807 memref[0] = tupregs[0].dttk_value; 4808 memref[1] = tupregs[1].dttk_value; 4809 4810 regs[rd] = (uintptr_t) memref; 4811 mstate->dtms_scratch_ptr += scratch_size; 4812 break; 4813 } 4814 4815 case DIF_SUBR_TYPEREF: { 4816 uintptr_t size = 4 * sizeof(uintptr_t); 4817 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4818 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 4819 4820 /* address, num_elements, type_str, type_len */ 4821 typeref[0] = tupregs[0].dttk_value; 4822 typeref[1] = tupregs[1].dttk_value; 4823 typeref[2] = tupregs[2].dttk_value; 4824 typeref[3] = tupregs[3].dttk_value; 4825 4826 regs[rd] = (uintptr_t) typeref; 4827 mstate->dtms_scratch_ptr += scratch_size; 4828 break; 4829 } 4830 } 4831} 4832 4833/* 4834 * Emulate the execution of DTrace IR instructions specified by the given 4835 * DIF object. This function is deliberately void of assertions as all of 4836 * the necessary checks are handled by a call to dtrace_difo_validate(). 4837 */ 4838static uint64_t 4839dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4840 dtrace_vstate_t *vstate, dtrace_state_t *state) 4841{ 4842 const dif_instr_t *text = difo->dtdo_buf; 4843 const uint_t textlen = difo->dtdo_len; 4844 const char *strtab = difo->dtdo_strtab; 4845 const uint64_t *inttab = difo->dtdo_inttab; 4846 4847 uint64_t rval = 0; 4848 dtrace_statvar_t *svar; 4849 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4850 dtrace_difv_t *v; 4851 volatile uint16_t *flags = &cpu_core[curcpu_id].cpuc_dtrace_flags; 4852 volatile uintptr_t *illval = &cpu_core[curcpu_id].cpuc_dtrace_illval; 4853 4854 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4855 uint64_t regs[DIF_DIR_NREGS]; 4856 uint64_t *tmp; 4857 4858 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4859 int64_t cc_r; 4860 uint_t pc = 0, id, opc = 0; 4861 uint8_t ttop = 0; 4862 dif_instr_t instr; 4863 uint_t r1, r2, rd; 4864 4865 /* 4866 * We stash the current DIF object into the machine state: we need it 4867 * for subsequent access checking. 4868 */ 4869 mstate->dtms_difo = difo; 4870 4871 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4872 4873 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4874 opc = pc; 4875 4876 instr = text[pc++]; 4877 r1 = DIF_INSTR_R1(instr); 4878 r2 = DIF_INSTR_R2(instr); 4879 rd = DIF_INSTR_RD(instr); 4880 4881 switch (DIF_INSTR_OP(instr)) { 4882 case DIF_OP_OR: 4883 regs[rd] = regs[r1] | regs[r2]; 4884 break; 4885 case DIF_OP_XOR: 4886 regs[rd] = regs[r1] ^ regs[r2]; 4887 break; 4888 case DIF_OP_AND: 4889 regs[rd] = regs[r1] & regs[r2]; 4890 break; 4891 case DIF_OP_SLL: 4892 regs[rd] = regs[r1] << regs[r2]; 4893 break; 4894 case DIF_OP_SRL: 4895 regs[rd] = regs[r1] >> regs[r2]; 4896 break; 4897 case DIF_OP_SUB: 4898 regs[rd] = regs[r1] - regs[r2]; 4899 break; 4900 case DIF_OP_ADD: 4901 regs[rd] = regs[r1] + regs[r2]; 4902 break; 4903 case DIF_OP_MUL: 4904 regs[rd] = regs[r1] * regs[r2]; 4905 break; 4906 case DIF_OP_SDIV: 4907 if (regs[r2] == 0) { 4908 regs[rd] = 0; 4909 *flags |= CPU_DTRACE_DIVZERO; 4910 } else { 4911 regs[rd] = (int64_t)regs[r1] / 4912 (int64_t)regs[r2]; 4913 } 4914 break; 4915 4916 case DIF_OP_UDIV: 4917 if (regs[r2] == 0) { 4918 regs[rd] = 0; 4919 *flags |= CPU_DTRACE_DIVZERO; 4920 } else { 4921 regs[rd] = regs[r1] / regs[r2]; 4922 } 4923 break; 4924 4925 case DIF_OP_SREM: 4926 if (regs[r2] == 0) { 4927 regs[rd] = 0; 4928 *flags |= CPU_DTRACE_DIVZERO; 4929 } else { 4930 regs[rd] = (int64_t)regs[r1] % 4931 (int64_t)regs[r2]; 4932 } 4933 break; 4934 4935 case DIF_OP_UREM: 4936 if (regs[r2] == 0) { 4937 regs[rd] = 0; 4938 *flags |= CPU_DTRACE_DIVZERO; 4939 } else { 4940 regs[rd] = regs[r1] % regs[r2]; 4941 } 4942 break; 4943 4944 case DIF_OP_NOT: 4945 regs[rd] = ~regs[r1]; 4946 break; 4947 case DIF_OP_MOV: 4948 regs[rd] = regs[r1]; 4949 break; 4950 case DIF_OP_CMP: 4951 cc_r = regs[r1] - regs[r2]; 4952 cc_n = cc_r < 0; 4953 cc_z = cc_r == 0; 4954 cc_v = 0; 4955 cc_c = regs[r1] < regs[r2]; 4956 break; 4957 case DIF_OP_TST: 4958 cc_n = cc_v = cc_c = 0; 4959 cc_z = regs[r1] == 0; 4960 break; 4961 case DIF_OP_BA: 4962 pc = DIF_INSTR_LABEL(instr); 4963 break; 4964 case DIF_OP_BE: 4965 if (cc_z) 4966 pc = DIF_INSTR_LABEL(instr); 4967 break; 4968 case DIF_OP_BNE: 4969 if (cc_z == 0) 4970 pc = DIF_INSTR_LABEL(instr); 4971 break; 4972 case DIF_OP_BG: 4973 if ((cc_z | (cc_n ^ cc_v)) == 0) 4974 pc = DIF_INSTR_LABEL(instr); 4975 break; 4976 case DIF_OP_BGU: 4977 if ((cc_c | cc_z) == 0) 4978 pc = DIF_INSTR_LABEL(instr); 4979 break; 4980 case DIF_OP_BGE: 4981 if ((cc_n ^ cc_v) == 0) 4982 pc = DIF_INSTR_LABEL(instr); 4983 break; 4984 case DIF_OP_BGEU: 4985 if (cc_c == 0) 4986 pc = DIF_INSTR_LABEL(instr); 4987 break; 4988 case DIF_OP_BL: 4989 if (cc_n ^ cc_v) 4990 pc = DIF_INSTR_LABEL(instr); 4991 break; 4992 case DIF_OP_BLU: 4993 if (cc_c) 4994 pc = DIF_INSTR_LABEL(instr); 4995 break; 4996 case DIF_OP_BLE: 4997 if (cc_z | (cc_n ^ cc_v)) 4998 pc = DIF_INSTR_LABEL(instr); 4999 break; 5000 case DIF_OP_BLEU: 5001 if (cc_c | cc_z) 5002 pc = DIF_INSTR_LABEL(instr); 5003 break; 5004 case DIF_OP_RLDSB: 5005 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5006 *flags |= CPU_DTRACE_KPRIV; 5007 *illval = regs[r1]; 5008 break; 5009 } 5010 /*FALLTHROUGH*/ 5011 case DIF_OP_LDSB: 5012 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 5013 break; 5014 case DIF_OP_RLDSH: 5015 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5016 *flags |= CPU_DTRACE_KPRIV; 5017 *illval = regs[r1]; 5018 break; 5019 } 5020 /*FALLTHROUGH*/ 5021 case DIF_OP_LDSH: 5022 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 5023 break; 5024 case DIF_OP_RLDSW: 5025 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5026 *flags |= CPU_DTRACE_KPRIV; 5027 *illval = regs[r1]; 5028 break; 5029 } 5030 /*FALLTHROUGH*/ 5031 case DIF_OP_LDSW: 5032 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 5033 break; 5034 case DIF_OP_RLDUB: 5035 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 5036 *flags |= CPU_DTRACE_KPRIV; 5037 *illval = regs[r1]; 5038 break; 5039 } 5040 /*FALLTHROUGH*/ 5041 case DIF_OP_LDUB: 5042 regs[rd] = dtrace_load8(regs[r1]); 5043 break; 5044 case DIF_OP_RLDUH: 5045 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 5046 *flags |= CPU_DTRACE_KPRIV; 5047 *illval = regs[r1]; 5048 break; 5049 } 5050 /*FALLTHROUGH*/ 5051 case DIF_OP_LDUH: 5052 regs[rd] = dtrace_load16(regs[r1]); 5053 break; 5054 case DIF_OP_RLDUW: 5055 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 5056 *flags |= CPU_DTRACE_KPRIV; 5057 *illval = regs[r1]; 5058 break; 5059 } 5060 /*FALLTHROUGH*/ 5061 case DIF_OP_LDUW: 5062 regs[rd] = dtrace_load32(regs[r1]); 5063 break; 5064 case DIF_OP_RLDX: 5065 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 5066 *flags |= CPU_DTRACE_KPRIV; 5067 *illval = regs[r1]; 5068 break; 5069 } 5070 /*FALLTHROUGH*/ 5071 case DIF_OP_LDX: 5072 regs[rd] = dtrace_load64(regs[r1]); 5073 break; 5074 case DIF_OP_ULDSB: 5075 regs[rd] = (int8_t) 5076 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5077 break; 5078 case DIF_OP_ULDSH: 5079 regs[rd] = (int16_t) 5080 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5081 break; 5082 case DIF_OP_ULDSW: 5083 regs[rd] = (int32_t) 5084 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5085 break; 5086 case DIF_OP_ULDUB: 5087 regs[rd] = 5088 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 5089 break; 5090 case DIF_OP_ULDUH: 5091 regs[rd] = 5092 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5093 break; 5094 case DIF_OP_ULDUW: 5095 regs[rd] = 5096 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5097 break; 5098 case DIF_OP_ULDX: 5099 regs[rd] = 5100 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5101 break; 5102 case DIF_OP_RET: 5103 rval = regs[rd]; 5104 pc = textlen; 5105 break; 5106 case DIF_OP_NOP: 5107 break; 5108 case DIF_OP_SETX: 5109 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5110 break; 5111 case DIF_OP_SETS: 5112 regs[rd] = (uint64_t)(uintptr_t) 5113 (strtab + DIF_INSTR_STRING(instr)); 5114 break; 5115 case DIF_OP_SCMP: { 5116 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5117 uintptr_t s1 = regs[r1]; 5118 uintptr_t s2 = regs[r2]; 5119 5120 if (s1 != 0 && 5121 !dtrace_strcanload(s1, sz, mstate, vstate)) 5122 break; 5123 if (s2 != 0 && 5124 !dtrace_strcanload(s2, sz, mstate, vstate)) 5125 break; 5126 5127 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5128 5129 cc_n = cc_r < 0; 5130 cc_z = cc_r == 0; 5131 cc_v = cc_c = 0; 5132 break; 5133 } 5134 case DIF_OP_LDGA: 5135 regs[rd] = dtrace_dif_variable(mstate, state, 5136 r1, regs[r2]); 5137 break; 5138 case DIF_OP_LDGS: 5139 id = DIF_INSTR_VAR(instr); 5140 5141 if (id >= DIF_VAR_OTHER_UBASE) { 5142 uintptr_t a; 5143 5144 id -= DIF_VAR_OTHER_UBASE; 5145 svar = vstate->dtvs_globals[id]; 5146 ASSERT(svar != NULL); 5147 v = &svar->dtsv_var; 5148 5149 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5150 regs[rd] = svar->dtsv_data; 5151 break; 5152 } 5153 5154 a = (uintptr_t)svar->dtsv_data; 5155 5156 if (*(uint8_t *)a == UINT8_MAX) { 5157 /* 5158 * If the 0th byte is set to UINT8_MAX 5159 * then this is to be treated as a 5160 * reference to a NULL variable. 5161 */ 5162 regs[rd] = 0; 5163 } else { 5164 regs[rd] = a + sizeof (uint64_t); 5165 } 5166 5167 break; 5168 } 5169 5170 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5171 break; 5172 5173 case DIF_OP_STGS: 5174 id = DIF_INSTR_VAR(instr); 5175 5176 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5177 id -= DIF_VAR_OTHER_UBASE; 5178 5179 svar = vstate->dtvs_globals[id]; 5180 ASSERT(svar != NULL); 5181 v = &svar->dtsv_var; 5182 5183 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5184 uintptr_t a = (uintptr_t)svar->dtsv_data; 5185 5186 ASSERT(a != 0); 5187 ASSERT(svar->dtsv_size != 0); 5188 5189 if (regs[rd] == 0) { 5190 *(uint8_t *)a = UINT8_MAX; 5191 break; 5192 } else { 5193 *(uint8_t *)a = 0; 5194 a += sizeof (uint64_t); 5195 } 5196 if (!dtrace_vcanload( 5197 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5198 mstate, vstate)) 5199 break; 5200 5201 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5202 (void *)a, &v->dtdv_type); 5203 break; 5204 } 5205 5206 svar->dtsv_data = regs[rd]; 5207 break; 5208 5209 case DIF_OP_LDTA: 5210 /* 5211 * There are no DTrace built-in thread-local arrays at 5212 * present. This opcode is saved for future work. 5213 */ 5214 *flags |= CPU_DTRACE_ILLOP; 5215 regs[rd] = 0; 5216 break; 5217 5218 case DIF_OP_LDLS: 5219 id = DIF_INSTR_VAR(instr); 5220 5221 if (id < DIF_VAR_OTHER_UBASE) { 5222 /* 5223 * For now, this has no meaning. 5224 */ 5225 regs[rd] = 0; 5226 break; 5227 } 5228 5229 id -= DIF_VAR_OTHER_UBASE; 5230 5231 ASSERT(id < vstate->dtvs_nlocals); 5232 ASSERT(vstate->dtvs_locals != NULL); 5233 5234 svar = vstate->dtvs_locals[id]; 5235 ASSERT(svar != NULL); 5236 v = &svar->dtsv_var; 5237 5238 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5239 uintptr_t a = (uintptr_t)svar->dtsv_data; 5240 size_t sz = v->dtdv_type.dtdt_size; 5241 5242 sz += sizeof (uint64_t); 5243 ASSERT(svar->dtsv_size == NCPU * sz); 5244 a += curcpu_id * sz; 5245 5246 if (*(uint8_t *)a == UINT8_MAX) { 5247 /* 5248 * If the 0th byte is set to UINT8_MAX 5249 * then this is to be treated as a 5250 * reference to a NULL variable. 5251 */ 5252 regs[rd] = 0; 5253 } else { 5254 regs[rd] = a + sizeof (uint64_t); 5255 } 5256 5257 break; 5258 } 5259 5260 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5261 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5262 regs[rd] = tmp[curcpu_id]; 5263 break; 5264 5265 case DIF_OP_STLS: 5266 id = DIF_INSTR_VAR(instr); 5267 5268 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5269 id -= DIF_VAR_OTHER_UBASE; 5270 ASSERT(id < vstate->dtvs_nlocals); 5271 5272 ASSERT(vstate->dtvs_locals != NULL); 5273 svar = vstate->dtvs_locals[id]; 5274 ASSERT(svar != NULL); 5275 v = &svar->dtsv_var; 5276 5277 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5278 uintptr_t a = (uintptr_t)svar->dtsv_data; 5279 size_t sz = v->dtdv_type.dtdt_size; 5280 5281 sz += sizeof (uint64_t); 5282 ASSERT(svar->dtsv_size == NCPU * sz); 5283 a += curcpu_id * sz; 5284 5285 if (regs[rd] == 0) { 5286 *(uint8_t *)a = UINT8_MAX; 5287 break; 5288 } else { 5289 *(uint8_t *)a = 0; 5290 a += sizeof (uint64_t); 5291 } 5292 5293 if (!dtrace_vcanload( 5294 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5295 mstate, vstate)) 5296 break; 5297 5298 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5299 (void *)a, &v->dtdv_type); 5300 break; 5301 } 5302 5303 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5304 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5305 tmp[curcpu_id] = regs[rd]; 5306 break; 5307 5308 case DIF_OP_LDTS: { 5309 dtrace_dynvar_t *dvar; 5310 dtrace_key_t *key; 5311 5312 id = DIF_INSTR_VAR(instr); 5313 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5314 id -= DIF_VAR_OTHER_UBASE; 5315 v = &vstate->dtvs_tlocals[id]; 5316 5317 key = &tupregs[DIF_DTR_NREGS]; 5318 key[0].dttk_value = (uint64_t)id; 5319 key[0].dttk_size = 0; 5320 DTRACE_TLS_THRKEY(key[1].dttk_value); 5321 key[1].dttk_size = 0; 5322 5323 dvar = dtrace_dynvar(dstate, 2, key, 5324 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5325 mstate, vstate); 5326 5327 if (dvar == NULL) { 5328 regs[rd] = 0; 5329 break; 5330 } 5331 5332 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5333 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5334 } else { 5335 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5336 } 5337 5338 break; 5339 } 5340 5341 case DIF_OP_STTS: { 5342 dtrace_dynvar_t *dvar; 5343 dtrace_key_t *key; 5344 5345 id = DIF_INSTR_VAR(instr); 5346 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5347 id -= DIF_VAR_OTHER_UBASE; 5348 5349 key = &tupregs[DIF_DTR_NREGS]; 5350 key[0].dttk_value = (uint64_t)id; 5351 key[0].dttk_size = 0; 5352 DTRACE_TLS_THRKEY(key[1].dttk_value); 5353 key[1].dttk_size = 0; 5354 v = &vstate->dtvs_tlocals[id]; 5355 5356 dvar = dtrace_dynvar(dstate, 2, key, 5357 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5358 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5359 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5360 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5361 5362 /* 5363 * Given that we're storing to thread-local data, 5364 * we need to flush our predicate cache. 5365 */ 5366 curthread->t_predcache = 0; 5367 5368 if (dvar == NULL) 5369 break; 5370 5371 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5372 if (!dtrace_vcanload( 5373 (void *)(uintptr_t)regs[rd], 5374 &v->dtdv_type, mstate, vstate)) 5375 break; 5376 5377 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5378 dvar->dtdv_data, &v->dtdv_type); 5379 } else { 5380 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5381 } 5382 5383 break; 5384 } 5385 5386 case DIF_OP_SRA: 5387 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5388 break; 5389 5390 case DIF_OP_CALL: 5391 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5392 regs, tupregs, ttop, mstate, state); 5393 break; 5394 5395 case DIF_OP_PUSHTR: 5396 if (ttop == DIF_DTR_NREGS) { 5397 *flags |= CPU_DTRACE_TUPOFLOW; 5398 break; 5399 } 5400 5401 if (r1 == DIF_TYPE_STRING) { 5402 /* 5403 * If this is a string type and the size is 0, 5404 * we'll use the system-wide default string 5405 * size. Note that we are _not_ looking at 5406 * the value of the DTRACEOPT_STRSIZE option; 5407 * had this been set, we would expect to have 5408 * a non-zero size value in the "pushtr". 5409 */ 5410 tupregs[ttop].dttk_size = 5411 dtrace_strlen((char *)(uintptr_t)regs[rd], 5412 regs[r2] ? regs[r2] : 5413 dtrace_strsize_default) + 1; 5414 } else { 5415 tupregs[ttop].dttk_size = regs[r2]; 5416 } 5417 5418 tupregs[ttop++].dttk_value = regs[rd]; 5419 break; 5420 5421 case DIF_OP_PUSHTV: 5422 if (ttop == DIF_DTR_NREGS) { 5423 *flags |= CPU_DTRACE_TUPOFLOW; 5424 break; 5425 } 5426 5427 tupregs[ttop].dttk_value = regs[rd]; 5428 tupregs[ttop++].dttk_size = 0; 5429 break; 5430 5431 case DIF_OP_POPTS: 5432 if (ttop != 0) 5433 ttop--; 5434 break; 5435 5436 case DIF_OP_FLUSHTS: 5437 ttop = 0; 5438 break; 5439 5440 case DIF_OP_LDGAA: 5441 case DIF_OP_LDTAA: { 5442 dtrace_dynvar_t *dvar; 5443 dtrace_key_t *key = tupregs; 5444 uint_t nkeys = ttop; 5445 5446 id = DIF_INSTR_VAR(instr); 5447 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5448 id -= DIF_VAR_OTHER_UBASE; 5449 5450 key[nkeys].dttk_value = (uint64_t)id; 5451 key[nkeys++].dttk_size = 0; 5452 5453 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5454 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5455 key[nkeys++].dttk_size = 0; 5456 v = &vstate->dtvs_tlocals[id]; 5457 } else { 5458 v = &vstate->dtvs_globals[id]->dtsv_var; 5459 } 5460 5461 dvar = dtrace_dynvar(dstate, nkeys, key, 5462 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5463 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5464 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5465 5466 if (dvar == NULL) { 5467 regs[rd] = 0; 5468 break; 5469 } 5470 5471 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5472 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5473 } else { 5474 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5475 } 5476 5477 break; 5478 } 5479 5480 case DIF_OP_STGAA: 5481 case DIF_OP_STTAA: { 5482 dtrace_dynvar_t *dvar; 5483 dtrace_key_t *key = tupregs; 5484 uint_t nkeys = ttop; 5485 5486 id = DIF_INSTR_VAR(instr); 5487 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5488 id -= DIF_VAR_OTHER_UBASE; 5489 5490 key[nkeys].dttk_value = (uint64_t)id; 5491 key[nkeys++].dttk_size = 0; 5492 5493 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5494 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5495 key[nkeys++].dttk_size = 0; 5496 v = &vstate->dtvs_tlocals[id]; 5497 } else { 5498 v = &vstate->dtvs_globals[id]->dtsv_var; 5499 } 5500 5501 dvar = dtrace_dynvar(dstate, nkeys, key, 5502 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5503 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5504 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5505 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5506 5507 if (dvar == NULL) 5508 break; 5509 5510 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5511 if (!dtrace_vcanload( 5512 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5513 mstate, vstate)) 5514 break; 5515 5516 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5517 dvar->dtdv_data, &v->dtdv_type); 5518 } else { 5519 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5520 } 5521 5522 break; 5523 } 5524 5525 case DIF_OP_ALLOCS: { 5526 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5527 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5528 5529 /* 5530 * Rounding up the user allocation size could have 5531 * overflowed large, bogus allocations (like -1ULL) to 5532 * 0. 5533 */ 5534 if (size < regs[r1] || 5535 !DTRACE_INSCRATCH(mstate, size)) { 5536 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5537 regs[rd] = 0; 5538 break; 5539 } 5540 5541 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5542 mstate->dtms_scratch_ptr += size; 5543 regs[rd] = ptr; 5544 break; 5545 } 5546 5547 case DIF_OP_COPYS: 5548 if (!dtrace_canstore(regs[rd], regs[r2], 5549 mstate, vstate)) { 5550 *flags |= CPU_DTRACE_BADADDR; 5551 *illval = regs[rd]; 5552 break; 5553 } 5554 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5555 break; 5556 5557 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5558 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5559 break; 5560 5561 case DIF_OP_STB: 5562 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5563 *flags |= CPU_DTRACE_BADADDR; 5564 *illval = regs[rd]; 5565 break; 5566 } 5567 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5568 break; 5569 5570 case DIF_OP_STH: 5571 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5572 *flags |= CPU_DTRACE_BADADDR; 5573 *illval = regs[rd]; 5574 break; 5575 } 5576 if (regs[rd] & 1) { 5577 *flags |= CPU_DTRACE_BADALIGN; 5578 *illval = regs[rd]; 5579 break; 5580 } 5581 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5582 break; 5583 5584 case DIF_OP_STW: 5585 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5586 *flags |= CPU_DTRACE_BADADDR; 5587 *illval = regs[rd]; 5588 break; 5589 } 5590 if (regs[rd] & 3) { 5591 *flags |= CPU_DTRACE_BADALIGN; 5592 *illval = regs[rd]; 5593 break; 5594 } 5595 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5596 break; 5597 5598 case DIF_OP_STX: 5599 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5600 *flags |= CPU_DTRACE_BADADDR; 5601 *illval = regs[rd]; 5602 break; 5603 } 5604 if (regs[rd] & 7) { 5605 *flags |= CPU_DTRACE_BADALIGN; 5606 *illval = regs[rd]; 5607 break; 5608 } 5609 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5610 break; 5611 } 5612 } 5613 5614 if (!(*flags & CPU_DTRACE_FAULT)) 5615 return (rval); 5616 5617 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5618 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5619 5620 return (0); 5621} 5622 5623static void 5624dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5625{ 5626 dtrace_probe_t *probe = ecb->dte_probe; 5627 dtrace_provider_t *prov = probe->dtpr_provider; 5628 char c[DTRACE_FULLNAMELEN + 80], *str; 5629 const char *msg = "dtrace: breakpoint action at probe "; 5630 const char *ecbmsg = " (ecb "; 5631 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5632 uintptr_t val = (uintptr_t)ecb; 5633 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5634 5635 if (dtrace_destructive_disallow) 5636 return; 5637 5638 /* 5639 * It's impossible to be taking action on the NULL probe. 5640 */ 5641 ASSERT(probe != NULL); 5642 5643 /* 5644 * This is a poor man's (destitute man's?) sprintf(): we want to 5645 * print the provider name, module name, function name and name of 5646 * the probe, along with the hex address of the ECB with the breakpoint 5647 * action -- all of which we must place in the character buffer by 5648 * hand. 5649 */ 5650 while (*msg != '\0') 5651 c[i++] = *msg++; 5652 5653 for (str = prov->dtpv_name; *str != '\0'; str++) 5654 c[i++] = *str; 5655 c[i++] = ':'; 5656 5657 for (str = probe->dtpr_mod; *str != '\0'; str++) 5658 c[i++] = *str; 5659 c[i++] = ':'; 5660 5661 for (str = probe->dtpr_func; *str != '\0'; str++) 5662 c[i++] = *str; 5663 c[i++] = ':'; 5664 5665 for (str = probe->dtpr_name; *str != '\0'; str++) 5666 c[i++] = *str; 5667 5668 while (*ecbmsg != '\0') 5669 c[i++] = *ecbmsg++; 5670 5671 while (shift >= 0) { 5672 mask = (uintptr_t)0xf << shift; 5673 5674 if (val >= ((uintptr_t)1 << shift)) 5675 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5676 shift -= 4; 5677 } 5678 5679 c[i++] = ')'; 5680 c[i] = '\0'; 5681 5682#if defined(sun) 5683 debug_enter(c); 5684#else 5685#ifdef DDB 5686 db_printf("%s\n", c); 5687 Debugger(); 5688#else 5689 printf("%s ignored\n", c); 5690#endif /* DDB */ 5691#endif 5692} 5693 5694static void 5695dtrace_action_panic(dtrace_ecb_t *ecb) 5696{ 5697 dtrace_probe_t *probe = ecb->dte_probe; 5698 5699 /* 5700 * It's impossible to be taking action on the NULL probe. 5701 */ 5702 ASSERT(probe != NULL); 5703 5704 if (dtrace_destructive_disallow) 5705 return; 5706 5707 if (dtrace_panicked != NULL) 5708 return; 5709 5710 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5711 return; 5712 5713 /* 5714 * We won the right to panic. (We want to be sure that only one 5715 * thread calls panic() from dtrace_probe(), and that panic() is 5716 * called exactly once.) 5717 */ 5718 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5719 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5720 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5721} 5722 5723static void 5724dtrace_action_raise(uint64_t sig) 5725{ 5726 if (dtrace_destructive_disallow) 5727 return; 5728 5729 if (sig >= NSIG) { 5730 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5731 return; 5732 } 5733 5734#if defined(sun) 5735 /* 5736 * raise() has a queue depth of 1 -- we ignore all subsequent 5737 * invocations of the raise() action. 5738 */ 5739 if (curthread->t_dtrace_sig == 0) 5740 curthread->t_dtrace_sig = (uint8_t)sig; 5741 5742 curthread->t_sig_check = 1; 5743 aston(curthread); 5744#else 5745 struct proc *p = curproc; 5746 mutex_enter(proc_lock); 5747 psignal(p, sig); 5748 mutex_exit(proc_lock); 5749#endif 5750} 5751 5752static void 5753dtrace_action_stop(void) 5754{ 5755 if (dtrace_destructive_disallow) 5756 return; 5757 5758#if defined(sun) 5759 if (!curthread->t_dtrace_stop) { 5760 curthread->t_dtrace_stop = 1; 5761 curthread->t_sig_check = 1; 5762 aston(curthread); 5763 } 5764#else 5765 struct proc *p = curproc; 5766 mutex_enter(proc_lock); 5767 psignal(p, SIGSTOP); 5768 mutex_exit(proc_lock); 5769#endif 5770} 5771 5772static void 5773dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5774{ 5775#if 0 /* XXX TBD - needs solaris_cpu */ 5776 hrtime_t now; 5777 volatile uint16_t *flags; 5778#if defined(sun) 5779 cpu_t *cpu = CPU; 5780#else 5781 cpu_t *cpu = &solaris_cpu[curcpu_id]; 5782#endif 5783 5784 if (dtrace_destructive_disallow) 5785 return; 5786 5787 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5788 5789 now = dtrace_gethrtime(); 5790 5791 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5792 /* 5793 * We need to advance the mark to the current time. 5794 */ 5795 cpu->cpu_dtrace_chillmark = now; 5796 cpu->cpu_dtrace_chilled = 0; 5797 } 5798 5799 /* 5800 * Now check to see if the requested chill time would take us over 5801 * the maximum amount of time allowed in the chill interval. (Or 5802 * worse, if the calculation itself induces overflow.) 5803 */ 5804 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5805 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5806 *flags |= CPU_DTRACE_ILLOP; 5807 return; 5808 } 5809 5810 while (dtrace_gethrtime() - now < val) 5811 continue; 5812 5813 /* 5814 * Normally, we assure that the value of the variable "timestamp" does 5815 * not change within an ECB. The presence of chill() represents an 5816 * exception to this rule, however. 5817 */ 5818 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5819 cpu->cpu_dtrace_chilled += val; 5820#endif 5821} 5822 5823#if defined(sun) 5824static void 5825dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5826 uint64_t *buf, uint64_t arg) 5827{ 5828 int nframes = DTRACE_USTACK_NFRAMES(arg); 5829 int strsize = DTRACE_USTACK_STRSIZE(arg); 5830 uint64_t *pcs = &buf[1], *fps; 5831 char *str = (char *)&pcs[nframes]; 5832 int size, offs = 0, i, j; 5833 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5834 uint16_t *flags = &cpu_core[curcpu_id].cpuc_dtrace_flags; 5835 char *sym; 5836 5837 /* 5838 * Should be taking a faster path if string space has not been 5839 * allocated. 5840 */ 5841 ASSERT(strsize != 0); 5842 5843 /* 5844 * We will first allocate some temporary space for the frame pointers. 5845 */ 5846 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5847 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5848 (nframes * sizeof (uint64_t)); 5849 5850 if (!DTRACE_INSCRATCH(mstate, size)) { 5851 /* 5852 * Not enough room for our frame pointers -- need to indicate 5853 * that we ran out of scratch space. 5854 */ 5855 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5856 return; 5857 } 5858 5859 mstate->dtms_scratch_ptr += size; 5860 saved = mstate->dtms_scratch_ptr; 5861 5862 /* 5863 * Now get a stack with both program counters and frame pointers. 5864 */ 5865 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5866 dtrace_getufpstack(buf, fps, nframes + 1); 5867 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5868 5869 /* 5870 * If that faulted, we're cooked. 5871 */ 5872 if (*flags & CPU_DTRACE_FAULT) 5873 goto out; 5874 5875 /* 5876 * Now we want to walk up the stack, calling the USTACK helper. For 5877 * each iteration, we restore the scratch pointer. 5878 */ 5879 for (i = 0; i < nframes; i++) { 5880 mstate->dtms_scratch_ptr = saved; 5881 5882 if (offs >= strsize) 5883 break; 5884 5885 sym = (char *)(uintptr_t)dtrace_helper( 5886 DTRACE_HELPER_ACTION_USTACK, 5887 mstate, state, pcs[i], fps[i]); 5888 5889 /* 5890 * If we faulted while running the helper, we're going to 5891 * clear the fault and null out the corresponding string. 5892 */ 5893 if (*flags & CPU_DTRACE_FAULT) { 5894 *flags &= ~CPU_DTRACE_FAULT; 5895 str[offs++] = '\0'; 5896 continue; 5897 } 5898 5899 if (sym == NULL) { 5900 str[offs++] = '\0'; 5901 continue; 5902 } 5903 5904 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5905 5906 /* 5907 * Now copy in the string that the helper returned to us. 5908 */ 5909 for (j = 0; offs + j < strsize; j++) { 5910 if ((str[offs + j] = sym[j]) == '\0') 5911 break; 5912 } 5913 5914 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5915 5916 offs += j + 1; 5917 } 5918 5919 if (offs >= strsize) { 5920 /* 5921 * If we didn't have room for all of the strings, we don't 5922 * abort processing -- this needn't be a fatal error -- but we 5923 * still want to increment a counter (dts_stkstroverflows) to 5924 * allow this condition to be warned about. (If this is from 5925 * a jstack() action, it is easily tuned via jstackstrsize.) 5926 */ 5927 dtrace_error(&state->dts_stkstroverflows); 5928 } 5929 5930 while (offs < strsize) 5931 str[offs++] = '\0'; 5932 5933out: 5934 mstate->dtms_scratch_ptr = old; 5935} 5936#endif 5937 5938/* 5939 * If you're looking for the epicenter of DTrace, you just found it. This 5940 * is the function called by the provider to fire a probe -- from which all 5941 * subsequent probe-context DTrace activity emanates. 5942 */ 5943void 5944dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5945 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5946{ 5947 processorid_t cpuid; 5948 dtrace_icookie_t cookie; 5949 dtrace_probe_t *probe; 5950 dtrace_mstate_t mstate; 5951 dtrace_ecb_t *ecb; 5952 dtrace_action_t *act; 5953 intptr_t offs; 5954 size_t size; 5955 int vtime, onintr; 5956 volatile uint16_t *flags; 5957 hrtime_t now; 5958 5959#if defined(sun) 5960 /* 5961 * Kick out immediately if this CPU is still being born (in which case 5962 * curthread will be set to -1) or the current thread can't allow 5963 * probes in its current context. 5964 */ 5965 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 5966 return; 5967#endif 5968 5969 cookie = dtrace_interrupt_disable(); 5970 probe = dtrace_probes[id - 1]; 5971 cpuid = curcpu_id; 5972 onintr = CPU_ON_INTR(CPU); 5973 5974 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 5975 probe->dtpr_predcache == curthread->t_predcache) { 5976 /* 5977 * We have hit in the predicate cache; we know that 5978 * this predicate would evaluate to be false. 5979 */ 5980 dtrace_interrupt_enable(cookie); 5981 return; 5982 } 5983 5984#if defined(sun) 5985 if (panic_quiesce) { 5986#else 5987 if (panicstr != NULL) { 5988#endif 5989 /* 5990 * We don't trace anything if we're panicking. 5991 */ 5992 dtrace_interrupt_enable(cookie); 5993 return; 5994 } 5995 5996 now = dtrace_gethrtime(); 5997 vtime = dtrace_vtime_references != 0; 5998 5999 if (vtime && curthread->t_dtrace_start) 6000 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 6001 6002 mstate.dtms_difo = NULL; 6003 mstate.dtms_probe = probe; 6004 mstate.dtms_strtok = 0; 6005 mstate.dtms_arg[0] = arg0; 6006 mstate.dtms_arg[1] = arg1; 6007 mstate.dtms_arg[2] = arg2; 6008 mstate.dtms_arg[3] = arg3; 6009 mstate.dtms_arg[4] = arg4; 6010 6011 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 6012 6013 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 6014 dtrace_predicate_t *pred = ecb->dte_predicate; 6015 dtrace_state_t *state = ecb->dte_state; 6016 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 6017 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 6018 dtrace_vstate_t *vstate = &state->dts_vstate; 6019 dtrace_provider_t *prov = probe->dtpr_provider; 6020 int committed = 0; 6021 caddr_t tomax; 6022 6023 /* 6024 * A little subtlety with the following (seemingly innocuous) 6025 * declaration of the automatic 'val': by looking at the 6026 * code, you might think that it could be declared in the 6027 * action processing loop, below. (That is, it's only used in 6028 * the action processing loop.) However, it must be declared 6029 * out of that scope because in the case of DIF expression 6030 * arguments to aggregating actions, one iteration of the 6031 * action loop will use the last iteration's value. 6032 */ 6033 uint64_t val = 0; 6034 6035 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 6036 *flags &= ~CPU_DTRACE_ERROR; 6037 6038 if (prov == dtrace_provider) { 6039 /* 6040 * If dtrace itself is the provider of this probe, 6041 * we're only going to continue processing the ECB if 6042 * arg0 (the dtrace_state_t) is equal to the ECB's 6043 * creating state. (This prevents disjoint consumers 6044 * from seeing one another's metaprobes.) 6045 */ 6046 if (arg0 != (uint64_t)(uintptr_t)state) 6047 continue; 6048 } 6049 6050 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 6051 /* 6052 * We're not currently active. If our provider isn't 6053 * the dtrace pseudo provider, we're not interested. 6054 */ 6055 if (prov != dtrace_provider) 6056 continue; 6057 6058 /* 6059 * Now we must further check if we are in the BEGIN 6060 * probe. If we are, we will only continue processing 6061 * if we're still in WARMUP -- if one BEGIN enabling 6062 * has invoked the exit() action, we don't want to 6063 * evaluate subsequent BEGIN enablings. 6064 */ 6065 if (probe->dtpr_id == dtrace_probeid_begin && 6066 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 6067 ASSERT(state->dts_activity == 6068 DTRACE_ACTIVITY_DRAINING); 6069 continue; 6070 } 6071 } 6072 6073 if (ecb->dte_cond) { 6074 /* 6075 * If the dte_cond bits indicate that this 6076 * consumer is only allowed to see user-mode firings 6077 * of this probe, call the provider's dtps_usermode() 6078 * entry point to check that the probe was fired 6079 * while in a user context. Skip this ECB if that's 6080 * not the case. 6081 */ 6082 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 6083 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 6084 probe->dtpr_id, probe->dtpr_arg) == 0) 6085 continue; 6086 6087#if defined(sun) 6088 /* 6089 * This is more subtle than it looks. We have to be 6090 * absolutely certain that CRED() isn't going to 6091 * change out from under us so it's only legit to 6092 * examine that structure if we're in constrained 6093 * situations. Currently, the only times we'll this 6094 * check is if a non-super-user has enabled the 6095 * profile or syscall providers -- providers that 6096 * allow visibility of all processes. For the 6097 * profile case, the check above will ensure that 6098 * we're examining a user context. 6099 */ 6100 if (ecb->dte_cond & DTRACE_COND_OWNER) { 6101 cred_t *cr; 6102 cred_t *s_cr = 6103 ecb->dte_state->dts_cred.dcr_cred; 6104 proc_t *proc; 6105 6106 ASSERT(s_cr != NULL); 6107 6108 if ((cr = CRED()) == NULL || 6109 s_cr->cr_uid != cr->cr_uid || 6110 s_cr->cr_uid != cr->cr_ruid || 6111 s_cr->cr_uid != cr->cr_suid || 6112 s_cr->cr_gid != cr->cr_gid || 6113 s_cr->cr_gid != cr->cr_rgid || 6114 s_cr->cr_gid != cr->cr_sgid || 6115 (proc = ttoproc(curthread)) == NULL || 6116 (proc->p_flag & SNOCD)) 6117 continue; 6118 } 6119 6120 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 6121 cred_t *cr; 6122 cred_t *s_cr = 6123 ecb->dte_state->dts_cred.dcr_cred; 6124 6125 ASSERT(s_cr != NULL); 6126 6127 if ((cr = CRED()) == NULL || 6128 s_cr->cr_zone->zone_id != 6129 cr->cr_zone->zone_id) 6130 continue; 6131 } 6132#endif 6133 } 6134 6135 if (now - state->dts_alive > dtrace_deadman_timeout) { 6136 /* 6137 * We seem to be dead. Unless we (a) have kernel 6138 * destructive permissions (b) have expicitly enabled 6139 * destructive actions and (c) destructive actions have 6140 * not been disabled, we're going to transition into 6141 * the KILLED state, from which no further processing 6142 * on this state will be performed. 6143 */ 6144 if (!dtrace_priv_kernel_destructive(state) || 6145 !state->dts_cred.dcr_destructive || 6146 dtrace_destructive_disallow) { 6147 void *activity = &state->dts_activity; 6148 dtrace_activity_t current; 6149 6150 do { 6151 current = state->dts_activity; 6152 } while (dtrace_cas32(activity, current, 6153 DTRACE_ACTIVITY_KILLED) != current); 6154 6155 continue; 6156 } 6157 } 6158 6159 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6160 ecb->dte_alignment, state, &mstate)) < 0) 6161 continue; 6162 6163 tomax = buf->dtb_tomax; 6164 ASSERT(tomax != NULL); 6165 6166 if (ecb->dte_size != 0) 6167 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 6168 6169 mstate.dtms_epid = ecb->dte_epid; 6170 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6171 6172 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6173 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 6174 else 6175 mstate.dtms_access = 0; 6176 6177 if (pred != NULL) { 6178 dtrace_difo_t *dp = pred->dtp_difo; 6179 int rval; 6180 6181 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6182 6183 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6184 dtrace_cacheid_t cid = probe->dtpr_predcache; 6185 6186 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6187 /* 6188 * Update the predicate cache... 6189 */ 6190 ASSERT(cid == pred->dtp_cacheid); 6191 curthread->t_predcache = cid; 6192 } 6193 6194 continue; 6195 } 6196 } 6197 6198 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6199 act != NULL; act = act->dta_next) { 6200 size_t valoffs; 6201 dtrace_difo_t *dp; 6202 dtrace_recdesc_t *rec = &act->dta_rec; 6203 6204 size = rec->dtrd_size; 6205 valoffs = offs + rec->dtrd_offset; 6206 6207 if (DTRACEACT_ISAGG(act->dta_kind)) { 6208 uint64_t v = 0xbad; 6209 dtrace_aggregation_t *agg; 6210 6211 agg = (dtrace_aggregation_t *)act; 6212 6213 if ((dp = act->dta_difo) != NULL) 6214 v = dtrace_dif_emulate(dp, 6215 &mstate, vstate, state); 6216 6217 if (*flags & CPU_DTRACE_ERROR) 6218 continue; 6219 6220 /* 6221 * Note that we always pass the expression 6222 * value from the previous iteration of the 6223 * action loop. This value will only be used 6224 * if there is an expression argument to the 6225 * aggregating action, denoted by the 6226 * dtag_hasarg field. 6227 */ 6228 dtrace_aggregate(agg, buf, 6229 offs, aggbuf, v, val); 6230 continue; 6231 } 6232 6233 switch (act->dta_kind) { 6234 case DTRACEACT_STOP: 6235 if (dtrace_priv_proc_destructive(state)) 6236 dtrace_action_stop(); 6237 continue; 6238 6239 case DTRACEACT_BREAKPOINT: 6240 if (dtrace_priv_kernel_destructive(state)) 6241 dtrace_action_breakpoint(ecb); 6242 continue; 6243 6244 case DTRACEACT_PANIC: 6245 if (dtrace_priv_kernel_destructive(state)) 6246 dtrace_action_panic(ecb); 6247 continue; 6248 6249 case DTRACEACT_STACK: 6250 if (!dtrace_priv_kernel(state)) 6251 continue; 6252 6253 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6254 size / sizeof (pc_t), probe->dtpr_aframes, 6255 DTRACE_ANCHORED(probe) ? NULL : 6256 (uint32_t *)arg0); 6257 continue; 6258 6259#if defined(sun) 6260 case DTRACEACT_JSTACK: 6261 case DTRACEACT_USTACK: 6262 if (!dtrace_priv_proc(state)) 6263 continue; 6264 6265 /* 6266 * See comment in DIF_VAR_PID. 6267 */ 6268 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6269 CPU_ON_INTR(CPU)) { 6270 int depth = DTRACE_USTACK_NFRAMES( 6271 rec->dtrd_arg) + 1; 6272 6273 dtrace_bzero((void *)(tomax + valoffs), 6274 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6275 + depth * sizeof (uint64_t)); 6276 6277 continue; 6278 } 6279 6280 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6281 curproc->p_dtrace_helpers != NULL) { 6282 /* 6283 * This is the slow path -- we have 6284 * allocated string space, and we're 6285 * getting the stack of a process that 6286 * has helpers. Call into a separate 6287 * routine to perform this processing. 6288 */ 6289 dtrace_action_ustack(&mstate, state, 6290 (uint64_t *)(tomax + valoffs), 6291 rec->dtrd_arg); 6292 continue; 6293 } 6294 6295 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6296 dtrace_getupcstack((uint64_t *) 6297 (tomax + valoffs), 6298 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6299 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6300 continue; 6301#endif 6302 6303 default: 6304 break; 6305 } 6306 6307 dp = act->dta_difo; 6308 ASSERT(dp != NULL); 6309 6310 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6311 6312 if (*flags & CPU_DTRACE_ERROR) 6313 continue; 6314 6315 switch (act->dta_kind) { 6316 case DTRACEACT_SPECULATE: 6317 ASSERT(buf == &state->dts_buffer[cpuid]); 6318 buf = dtrace_speculation_buffer(state, 6319 cpuid, val); 6320 6321 if (buf == NULL) { 6322 *flags |= CPU_DTRACE_DROP; 6323 continue; 6324 } 6325 6326 offs = dtrace_buffer_reserve(buf, 6327 ecb->dte_needed, ecb->dte_alignment, 6328 state, NULL); 6329 6330 if (offs < 0) { 6331 *flags |= CPU_DTRACE_DROP; 6332 continue; 6333 } 6334 6335 tomax = buf->dtb_tomax; 6336 ASSERT(tomax != NULL); 6337 6338 if (ecb->dte_size != 0) 6339 DTRACE_STORE(uint32_t, tomax, offs, 6340 ecb->dte_epid); 6341 continue; 6342 6343 case DTRACEACT_PRINTM: { 6344 /* The DIF returns a 'memref'. */ 6345 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 6346 6347 /* Get the size from the memref. */ 6348 size = memref[1]; 6349 6350 /* 6351 * Check if the size exceeds the allocated 6352 * buffer size. 6353 */ 6354 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6355 /* Flag a drop! */ 6356 *flags |= CPU_DTRACE_DROP; 6357 continue; 6358 } 6359 6360 /* Store the size in the buffer first. */ 6361 DTRACE_STORE(uintptr_t, tomax, 6362 valoffs, size); 6363 6364 /* 6365 * Offset the buffer address to the start 6366 * of the data. 6367 */ 6368 valoffs += sizeof(uintptr_t); 6369 6370 /* 6371 * Reset to the memory address rather than 6372 * the memref array, then let the BYREF 6373 * code below do the work to store the 6374 * memory data in the buffer. 6375 */ 6376 val = memref[0]; 6377 break; 6378 } 6379 6380 case DTRACEACT_PRINTT: { 6381 /* The DIF returns a 'typeref'. */ 6382 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 6383 char c = '\0' + 1; 6384 size_t s; 6385 6386 /* 6387 * Get the type string length and round it 6388 * up so that the data that follows is 6389 * aligned for easy access. 6390 */ 6391 size_t typs = strlen((char *) typeref[2]) + 1; 6392 typs = roundup(typs, sizeof(uintptr_t)); 6393 6394 /* 6395 *Get the size from the typeref using the 6396 * number of elements and the type size. 6397 */ 6398 size = typeref[1] * typeref[3]; 6399 6400 /* 6401 * Check if the size exceeds the allocated 6402 * buffer size. 6403 */ 6404 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6405 /* Flag a drop! */ 6406 *flags |= CPU_DTRACE_DROP; 6407 6408 } 6409 6410 /* Store the size in the buffer first. */ 6411 DTRACE_STORE(uintptr_t, tomax, 6412 valoffs, size); 6413 valoffs += sizeof(uintptr_t); 6414 6415 /* Store the type size in the buffer. */ 6416 DTRACE_STORE(uintptr_t, tomax, 6417 valoffs, typeref[3]); 6418 valoffs += sizeof(uintptr_t); 6419 6420 val = typeref[2]; 6421 6422 for (s = 0; s < typs; s++) { 6423 if (c != '\0') 6424 c = dtrace_load8(val++); 6425 6426 DTRACE_STORE(uint8_t, tomax, 6427 valoffs++, c); 6428 } 6429 6430 /* 6431 * Reset to the memory address rather than 6432 * the typeref array, then let the BYREF 6433 * code below do the work to store the 6434 * memory data in the buffer. 6435 */ 6436 val = typeref[0]; 6437 break; 6438 } 6439 6440 case DTRACEACT_CHILL: 6441 if (dtrace_priv_kernel_destructive(state)) 6442 dtrace_action_chill(&mstate, val); 6443 continue; 6444 6445 case DTRACEACT_RAISE: 6446 if (dtrace_priv_proc_destructive(state)) 6447 dtrace_action_raise(val); 6448 continue; 6449 6450 case DTRACEACT_COMMIT: 6451 ASSERT(!committed); 6452 6453 /* 6454 * We need to commit our buffer state. 6455 */ 6456 if (ecb->dte_size) 6457 buf->dtb_offset = offs + ecb->dte_size; 6458 buf = &state->dts_buffer[cpuid]; 6459 dtrace_speculation_commit(state, cpuid, val); 6460 committed = 1; 6461 continue; 6462 6463 case DTRACEACT_DISCARD: 6464 dtrace_speculation_discard(state, cpuid, val); 6465 continue; 6466 6467 case DTRACEACT_DIFEXPR: 6468 case DTRACEACT_LIBACT: 6469 case DTRACEACT_PRINTF: 6470 case DTRACEACT_PRINTA: 6471 case DTRACEACT_SYSTEM: 6472 case DTRACEACT_FREOPEN: 6473 break; 6474 6475 case DTRACEACT_SYM: 6476 case DTRACEACT_MOD: 6477 if (!dtrace_priv_kernel(state)) 6478 continue; 6479 break; 6480 6481 case DTRACEACT_USYM: 6482 case DTRACEACT_UMOD: 6483 case DTRACEACT_UADDR: { 6484#if defined(sun) 6485 struct pid *pid = curthread->t_procp->p_pidp; 6486#endif 6487 if (!dtrace_priv_proc(state)) 6488 continue; 6489 6490 DTRACE_STORE(uint64_t, tomax, 6491#if defined(sun) 6492 valoffs, (uint64_t)pid->pid_id); 6493#else 6494 valoffs, (uint64_t) curproc->p_pid); 6495#endif 6496 DTRACE_STORE(uint64_t, tomax, 6497 valoffs + sizeof (uint64_t), val); 6498 6499 continue; 6500 } 6501 6502 case DTRACEACT_EXIT: { 6503 /* 6504 * For the exit action, we are going to attempt 6505 * to atomically set our activity to be 6506 * draining. If this fails (either because 6507 * another CPU has beat us to the exit action, 6508 * or because our current activity is something 6509 * other than ACTIVE or WARMUP), we will 6510 * continue. This assures that the exit action 6511 * can be successfully recorded at most once 6512 * when we're in the ACTIVE state. If we're 6513 * encountering the exit() action while in 6514 * COOLDOWN, however, we want to honor the new 6515 * status code. (We know that we're the only 6516 * thread in COOLDOWN, so there is no race.) 6517 */ 6518 void *activity = &state->dts_activity; 6519 dtrace_activity_t current = state->dts_activity; 6520 6521 if (current == DTRACE_ACTIVITY_COOLDOWN) 6522 break; 6523 6524 if (current != DTRACE_ACTIVITY_WARMUP) 6525 current = DTRACE_ACTIVITY_ACTIVE; 6526 6527 if (dtrace_cas32(activity, current, 6528 DTRACE_ACTIVITY_DRAINING) != current) { 6529 *flags |= CPU_DTRACE_DROP; 6530 continue; 6531 } 6532 6533 break; 6534 } 6535 6536 default: 6537 ASSERT(0); 6538 } 6539 6540 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6541 uintptr_t end = valoffs + size; 6542 6543 if (!dtrace_vcanload((void *)(uintptr_t)val, 6544 &dp->dtdo_rtype, &mstate, vstate)) 6545 continue; 6546 6547 /* 6548 * If this is a string, we're going to only 6549 * load until we find the zero byte -- after 6550 * which we'll store zero bytes. 6551 */ 6552 if (dp->dtdo_rtype.dtdt_kind == 6553 DIF_TYPE_STRING) { 6554 char c = '\0' + 1; 6555 int intuple = act->dta_intuple; 6556 size_t s; 6557 6558 for (s = 0; s < size; s++) { 6559 if (c != '\0') 6560 c = dtrace_load8(val++); 6561 6562 DTRACE_STORE(uint8_t, tomax, 6563 valoffs++, c); 6564 6565 if (c == '\0' && intuple) 6566 break; 6567 } 6568 6569 continue; 6570 } 6571 6572 while (valoffs < end) { 6573 DTRACE_STORE(uint8_t, tomax, valoffs++, 6574 dtrace_load8(val++)); 6575 } 6576 6577 continue; 6578 } 6579 6580 switch (size) { 6581 case 0: 6582 break; 6583 6584 case sizeof (uint8_t): 6585 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6586 break; 6587 case sizeof (uint16_t): 6588 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6589 break; 6590 case sizeof (uint32_t): 6591 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6592 break; 6593 case sizeof (uint64_t): 6594 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6595 break; 6596 default: 6597 /* 6598 * Any other size should have been returned by 6599 * reference, not by value. 6600 */ 6601 ASSERT(0); 6602 break; 6603 } 6604 } 6605 6606 if (*flags & CPU_DTRACE_DROP) 6607 continue; 6608 6609 if (*flags & CPU_DTRACE_FAULT) { 6610 int ndx; 6611 dtrace_action_t *err; 6612 6613 buf->dtb_errors++; 6614 6615 if (probe->dtpr_id == dtrace_probeid_error) { 6616 /* 6617 * There's nothing we can do -- we had an 6618 * error on the error probe. We bump an 6619 * error counter to at least indicate that 6620 * this condition happened. 6621 */ 6622 dtrace_error(&state->dts_dblerrors); 6623 continue; 6624 } 6625 6626 if (vtime) { 6627 /* 6628 * Before recursing on dtrace_probe(), we 6629 * need to explicitly clear out our start 6630 * time to prevent it from being accumulated 6631 * into t_dtrace_vtime. 6632 */ 6633 curthread->t_dtrace_start = 0; 6634 } 6635 6636 /* 6637 * Iterate over the actions to figure out which action 6638 * we were processing when we experienced the error. 6639 * Note that act points _past_ the faulting action; if 6640 * act is ecb->dte_action, the fault was in the 6641 * predicate, if it's ecb->dte_action->dta_next it's 6642 * in action #1, and so on. 6643 */ 6644 for (err = ecb->dte_action, ndx = 0; 6645 err != act; err = err->dta_next, ndx++) 6646 continue; 6647 6648 dtrace_probe_error(state, ecb->dte_epid, ndx, 6649 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6650 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6651 cpu_core[cpuid].cpuc_dtrace_illval); 6652 6653 continue; 6654 } 6655 6656 if (!committed) 6657 buf->dtb_offset = offs + ecb->dte_size; 6658 } 6659 6660 if (vtime) 6661 curthread->t_dtrace_start = dtrace_gethrtime(); 6662 6663 dtrace_interrupt_enable(cookie); 6664} 6665 6666/* 6667 * DTrace Probe Hashing Functions 6668 * 6669 * The functions in this section (and indeed, the functions in remaining 6670 * sections) are not _called_ from probe context. (Any exceptions to this are 6671 * marked with a "Note:".) Rather, they are called from elsewhere in the 6672 * DTrace framework to look-up probes in, add probes to and remove probes from 6673 * the DTrace probe hashes. (Each probe is hashed by each element of the 6674 * probe tuple -- allowing for fast lookups, regardless of what was 6675 * specified.) 6676 */ 6677static uint_t 6678dtrace_hash_str(const char *p) 6679{ 6680 unsigned int g; 6681 uint_t hval = 0; 6682 6683 while (*p) { 6684 hval = (hval << 4) + *p++; 6685 if ((g = (hval & 0xf0000000)) != 0) 6686 hval ^= g >> 24; 6687 hval &= ~g; 6688 } 6689 return (hval); 6690} 6691 6692static dtrace_hash_t * 6693dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6694{ 6695 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6696 6697 hash->dth_stroffs = stroffs; 6698 hash->dth_nextoffs = nextoffs; 6699 hash->dth_prevoffs = prevoffs; 6700 6701 hash->dth_size = 1; 6702 hash->dth_mask = hash->dth_size - 1; 6703 6704 hash->dth_tab = kmem_zalloc(hash->dth_size * 6705 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6706 6707 return (hash); 6708} 6709 6710static void 6711dtrace_hash_destroy(dtrace_hash_t *hash) 6712{ 6713#ifdef DEBUG 6714 int i; 6715 6716 for (i = 0; i < hash->dth_size; i++) 6717 ASSERT(hash->dth_tab[i] == NULL); 6718#endif 6719 6720 kmem_free(hash->dth_tab, 6721 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6722 kmem_free(hash, sizeof (dtrace_hash_t)); 6723} 6724 6725static void 6726dtrace_hash_resize(dtrace_hash_t *hash) 6727{ 6728 int size = hash->dth_size, i, ndx; 6729 int new_size = hash->dth_size << 1; 6730 int new_mask = new_size - 1; 6731 dtrace_hashbucket_t **new_tab, *bucket, *next; 6732 6733 ASSERT((new_size & new_mask) == 0); 6734 6735 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6736 6737 for (i = 0; i < size; i++) { 6738 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6739 dtrace_probe_t *probe = bucket->dthb_chain; 6740 6741 ASSERT(probe != NULL); 6742 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6743 6744 next = bucket->dthb_next; 6745 bucket->dthb_next = new_tab[ndx]; 6746 new_tab[ndx] = bucket; 6747 } 6748 } 6749 6750 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6751 hash->dth_tab = new_tab; 6752 hash->dth_size = new_size; 6753 hash->dth_mask = new_mask; 6754} 6755 6756static void 6757dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6758{ 6759 int hashval = DTRACE_HASHSTR(hash, new); 6760 int ndx = hashval & hash->dth_mask; 6761 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6762 dtrace_probe_t **nextp, **prevp; 6763 6764 for (; bucket != NULL; bucket = bucket->dthb_next) { 6765 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6766 goto add; 6767 } 6768 6769 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6770 dtrace_hash_resize(hash); 6771 dtrace_hash_add(hash, new); 6772 return; 6773 } 6774 6775 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6776 bucket->dthb_next = hash->dth_tab[ndx]; 6777 hash->dth_tab[ndx] = bucket; 6778 hash->dth_nbuckets++; 6779 6780add: 6781 nextp = DTRACE_HASHNEXT(hash, new); 6782 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6783 *nextp = bucket->dthb_chain; 6784 6785 if (bucket->dthb_chain != NULL) { 6786 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6787 ASSERT(*prevp == NULL); 6788 *prevp = new; 6789 } 6790 6791 bucket->dthb_chain = new; 6792 bucket->dthb_len++; 6793} 6794 6795static dtrace_probe_t * 6796dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6797{ 6798 int hashval = DTRACE_HASHSTR(hash, template); 6799 int ndx = hashval & hash->dth_mask; 6800 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6801 6802 for (; bucket != NULL; bucket = bucket->dthb_next) { 6803 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6804 return (bucket->dthb_chain); 6805 } 6806 6807 return (NULL); 6808} 6809 6810static int 6811dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6812{ 6813 int hashval = DTRACE_HASHSTR(hash, template); 6814 int ndx = hashval & hash->dth_mask; 6815 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6816 6817 for (; bucket != NULL; bucket = bucket->dthb_next) { 6818 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6819 return (bucket->dthb_len); 6820 } 6821 6822 return (0); 6823} 6824 6825static void 6826dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6827{ 6828 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6829 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6830 6831 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6832 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6833 6834 /* 6835 * Find the bucket that we're removing this probe from. 6836 */ 6837 for (; bucket != NULL; bucket = bucket->dthb_next) { 6838 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6839 break; 6840 } 6841 6842 ASSERT(bucket != NULL); 6843 6844 if (*prevp == NULL) { 6845 if (*nextp == NULL) { 6846 /* 6847 * The removed probe was the only probe on this 6848 * bucket; we need to remove the bucket. 6849 */ 6850 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6851 6852 ASSERT(bucket->dthb_chain == probe); 6853 ASSERT(b != NULL); 6854 6855 if (b == bucket) { 6856 hash->dth_tab[ndx] = bucket->dthb_next; 6857 } else { 6858 while (b->dthb_next != bucket) 6859 b = b->dthb_next; 6860 b->dthb_next = bucket->dthb_next; 6861 } 6862 6863 ASSERT(hash->dth_nbuckets > 0); 6864 hash->dth_nbuckets--; 6865 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6866 return; 6867 } 6868 6869 bucket->dthb_chain = *nextp; 6870 } else { 6871 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6872 } 6873 6874 if (*nextp != NULL) 6875 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6876} 6877 6878/* 6879 * DTrace Utility Functions 6880 * 6881 * These are random utility functions that are _not_ called from probe context. 6882 */ 6883static int 6884dtrace_badattr(const dtrace_attribute_t *a) 6885{ 6886 return (a->dtat_name > DTRACE_STABILITY_MAX || 6887 a->dtat_data > DTRACE_STABILITY_MAX || 6888 a->dtat_class > DTRACE_CLASS_MAX); 6889} 6890 6891/* 6892 * Return a duplicate copy of a string. If the specified string is NULL, 6893 * this function returns a zero-length string. 6894 */ 6895static char * 6896dtrace_strdup(const char *str) 6897{ 6898 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6899 6900 if (str != NULL) 6901 (void) strcpy(new, str); 6902 6903 return (new); 6904} 6905 6906#define DTRACE_ISALPHA(c) \ 6907 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6908 6909static int 6910dtrace_badname(const char *s) 6911{ 6912 char c; 6913 6914 if (s == NULL || (c = *s++) == '\0') 6915 return (0); 6916 6917 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6918 return (1); 6919 6920 while ((c = *s++) != '\0') { 6921 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6922 c != '-' && c != '_' && c != '.' && c != '`') 6923 return (1); 6924 } 6925 6926 return (0); 6927} 6928 6929static void 6930dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6931{ 6932 uint32_t priv; 6933 6934#if defined(sun) 6935 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6936 /* 6937 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter, 6938 * but for GCC they do. 6939 */ 6940 *uidp = 0; 6941 *zoneidp = 0; 6942 6943 priv = DTRACE_PRIV_ALL; 6944 } else { 6945 *uidp = crgetuid(cr); 6946 *zoneidp = crgetzoneid(cr); 6947 6948 priv = 0; 6949 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6950 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6951 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6952 priv |= DTRACE_PRIV_USER; 6953 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6954 priv |= DTRACE_PRIV_PROC; 6955 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6956 priv |= DTRACE_PRIV_OWNER; 6957 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6958 priv |= DTRACE_PRIV_ZONEOWNER; 6959 } 6960#else 6961 priv = DTRACE_PRIV_ALL; 6962 *uidp = 0; 6963 *zoneidp = 0; 6964#endif 6965 6966 *privp = priv; 6967} 6968 6969#ifdef DTRACE_ERRDEBUG 6970static void 6971dtrace_errdebug(const char *str) 6972{ 6973 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 6974 int occupied = 0; 6975 6976 mutex_enter(&dtrace_errlock); 6977 dtrace_errlast = str; 6978 dtrace_errthread = curthread; 6979 6980 while (occupied++ < DTRACE_ERRHASHSZ) { 6981 if (dtrace_errhash[hval].dter_msg == str) { 6982 dtrace_errhash[hval].dter_count++; 6983 goto out; 6984 } 6985 6986 if (dtrace_errhash[hval].dter_msg != NULL) { 6987 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6988 continue; 6989 } 6990 6991 dtrace_errhash[hval].dter_msg = str; 6992 dtrace_errhash[hval].dter_count = 1; 6993 goto out; 6994 } 6995 6996 panic("dtrace: undersized error hash"); 6997out: 6998 mutex_exit(&dtrace_errlock); 6999} 7000#endif 7001 7002/* 7003 * DTrace Matching Functions 7004 * 7005 * These functions are used to match groups of probes, given some elements of 7006 * a probe tuple, or some globbed expressions for elements of a probe tuple. 7007 */ 7008static int 7009dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 7010 zoneid_t zoneid) 7011{ 7012 if (priv != DTRACE_PRIV_ALL) { 7013 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 7014 uint32_t match = priv & ppriv; 7015 7016 /* 7017 * No PRIV_DTRACE_* privileges... 7018 */ 7019 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 7020 DTRACE_PRIV_KERNEL)) == 0) 7021 return (0); 7022 7023 /* 7024 * No matching bits, but there were bits to match... 7025 */ 7026 if (match == 0 && ppriv != 0) 7027 return (0); 7028 7029 /* 7030 * Need to have permissions to the process, but don't... 7031 */ 7032 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 7033 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 7034 return (0); 7035 } 7036 7037 /* 7038 * Need to be in the same zone unless we possess the 7039 * privilege to examine all zones. 7040 */ 7041 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 7042 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 7043 return (0); 7044 } 7045 } 7046 7047 return (1); 7048} 7049 7050/* 7051 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 7052 * consists of input pattern strings and an ops-vector to evaluate them. 7053 * This function returns >0 for match, 0 for no match, and <0 for error. 7054 */ 7055static int 7056dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 7057 uint32_t priv, uid_t uid, zoneid_t zoneid) 7058{ 7059 dtrace_provider_t *pvp = prp->dtpr_provider; 7060 int rv; 7061 7062 if (pvp->dtpv_defunct) 7063 return (0); 7064 7065 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 7066 return (rv); 7067 7068 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 7069 return (rv); 7070 7071 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 7072 return (rv); 7073 7074 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 7075 return (rv); 7076 7077 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 7078 return (0); 7079 7080 return (rv); 7081} 7082 7083/* 7084 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 7085 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 7086 * libc's version, the kernel version only applies to 8-bit ASCII strings. 7087 * In addition, all of the recursion cases except for '*' matching have been 7088 * unwound. For '*', we still implement recursive evaluation, but a depth 7089 * counter is maintained and matching is aborted if we recurse too deep. 7090 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 7091 */ 7092static int 7093dtrace_match_glob(const char *s, const char *p, int depth) 7094{ 7095 const char *olds; 7096 char s1, c; 7097 int gs; 7098 7099 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 7100 return (-1); 7101 7102 if (s == NULL) 7103 s = ""; /* treat NULL as empty string */ 7104 7105top: 7106 olds = s; 7107 s1 = *s++; 7108 7109 if (p == NULL) 7110 return (0); 7111 7112 if ((c = *p++) == '\0') 7113 return (s1 == '\0'); 7114 7115 switch (c) { 7116 case '[': { 7117 int ok = 0, notflag = 0; 7118 char lc = '\0'; 7119 7120 if (s1 == '\0') 7121 return (0); 7122 7123 if (*p == '!') { 7124 notflag = 1; 7125 p++; 7126 } 7127 7128 if ((c = *p++) == '\0') 7129 return (0); 7130 7131 do { 7132 if (c == '-' && lc != '\0' && *p != ']') { 7133 if ((c = *p++) == '\0') 7134 return (0); 7135 if (c == '\\' && (c = *p++) == '\0') 7136 return (0); 7137 7138 if (notflag) { 7139 if (s1 < lc || s1 > c) 7140 ok++; 7141 else 7142 return (0); 7143 } else if (lc <= s1 && s1 <= c) 7144 ok++; 7145 7146 } else if (c == '\\' && (c = *p++) == '\0') 7147 return (0); 7148 7149 lc = c; /* save left-hand 'c' for next iteration */ 7150 7151 if (notflag) { 7152 if (s1 != c) 7153 ok++; 7154 else 7155 return (0); 7156 } else if (s1 == c) 7157 ok++; 7158 7159 if ((c = *p++) == '\0') 7160 return (0); 7161 7162 } while (c != ']'); 7163 7164 if (ok) 7165 goto top; 7166 7167 return (0); 7168 } 7169 7170 case '\\': 7171 if ((c = *p++) == '\0') 7172 return (0); 7173 /*FALLTHRU*/ 7174 7175 default: 7176 if (c != s1) 7177 return (0); 7178 /*FALLTHRU*/ 7179 7180 case '?': 7181 if (s1 != '\0') 7182 goto top; 7183 return (0); 7184 7185 case '*': 7186 while (*p == '*') 7187 p++; /* consecutive *'s are identical to a single one */ 7188 7189 if (*p == '\0') 7190 return (1); 7191 7192 for (s = olds; *s != '\0'; s++) { 7193 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7194 return (gs); 7195 } 7196 7197 return (0); 7198 } 7199} 7200 7201/*ARGSUSED*/ 7202static int 7203dtrace_match_string(const char *s, const char *p, int depth) 7204{ 7205 return (s != NULL && strcmp(s, p) == 0); 7206} 7207 7208/*ARGSUSED*/ 7209static int 7210dtrace_match_nul(const char *s, const char *p, int depth) 7211{ 7212 return (1); /* always match the empty pattern */ 7213} 7214 7215/*ARGSUSED*/ 7216static int 7217dtrace_match_nonzero(const char *s, const char *p, int depth) 7218{ 7219 return (s != NULL && s[0] != '\0'); 7220} 7221 7222static int 7223dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7224 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7225{ 7226 dtrace_probe_t template, *probe; 7227 dtrace_hash_t *hash = NULL; 7228 int len, rc, best = INT_MAX, nmatched = 0; 7229 dtrace_id_t i; 7230 7231 ASSERT(MUTEX_HELD(&dtrace_lock)); 7232 7233 /* 7234 * If the probe ID is specified in the key, just lookup by ID and 7235 * invoke the match callback once if a matching probe is found. 7236 */ 7237 if (pkp->dtpk_id != DTRACE_IDNONE) { 7238 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7239 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7240 if ((*matched)(probe, arg) == DTRACE_MATCH_FAIL) 7241 return (DTRACE_MATCH_FAIL); 7242 nmatched++; 7243 } 7244 return (nmatched); 7245 } 7246 7247 template.dtpr_mod = (char *)pkp->dtpk_mod; 7248 template.dtpr_func = (char *)pkp->dtpk_func; 7249 template.dtpr_name = (char *)pkp->dtpk_name; 7250 7251 /* 7252 * We want to find the most distinct of the module name, function 7253 * name, and name. So for each one that is not a glob pattern or 7254 * empty string, we perform a lookup in the corresponding hash and 7255 * use the hash table with the fewest collisions to do our search. 7256 */ 7257 if (pkp->dtpk_mmatch == &dtrace_match_string && 7258 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7259 best = len; 7260 hash = dtrace_bymod; 7261 } 7262 7263 if (pkp->dtpk_fmatch == &dtrace_match_string && 7264 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7265 best = len; 7266 hash = dtrace_byfunc; 7267 } 7268 7269 if (pkp->dtpk_nmatch == &dtrace_match_string && 7270 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7271 best = len; 7272 hash = dtrace_byname; 7273 } 7274 7275 /* 7276 * If we did not select a hash table, iterate over every probe and 7277 * invoke our callback for each one that matches our input probe key. 7278 */ 7279 if (hash == NULL) { 7280 for (i = 0; i < dtrace_nprobes; i++) { 7281 if ((probe = dtrace_probes[i]) == NULL || 7282 dtrace_match_probe(probe, pkp, priv, uid, 7283 zoneid) <= 0) 7284 continue; 7285 7286 nmatched++; 7287 7288 if ((rc = (*matched)(probe, arg)) != 7289 DTRACE_MATCH_NEXT) { 7290 if (rc == DTRACE_MATCH_FAIL) 7291 return (DTRACE_MATCH_FAIL); 7292 break; 7293 } 7294 } 7295 7296 return (nmatched); 7297 } 7298 7299 /* 7300 * If we selected a hash table, iterate over each probe of the same key 7301 * name and invoke the callback for every probe that matches the other 7302 * attributes of our input probe key. 7303 */ 7304 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7305 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7306 7307 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7308 continue; 7309 7310 nmatched++; 7311 7312 if ((rc = (*matched)(probe, arg)) != DTRACE_MATCH_NEXT) { 7313 if (rc == DTRACE_MATCH_FAIL) 7314 return (DTRACE_MATCH_FAIL); 7315 break; 7316 } 7317 } 7318 7319 return (nmatched); 7320} 7321 7322/* 7323 * Return the function pointer dtrace_probecmp() should use to compare the 7324 * specified pattern with a string. For NULL or empty patterns, we select 7325 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7326 * For non-empty non-glob strings, we use dtrace_match_string(). 7327 */ 7328static dtrace_probekey_f * 7329dtrace_probekey_func(const char *p) 7330{ 7331 char c; 7332 7333 if (p == NULL || *p == '\0') 7334 return (&dtrace_match_nul); 7335 7336 while ((c = *p++) != '\0') { 7337 if (c == '[' || c == '?' || c == '*' || c == '\\') 7338 return (&dtrace_match_glob); 7339 } 7340 7341 return (&dtrace_match_string); 7342} 7343 7344/* 7345 * Build a probe comparison key for use with dtrace_match_probe() from the 7346 * given probe description. By convention, a null key only matches anchored 7347 * probes: if each field is the empty string, reset dtpk_fmatch to 7348 * dtrace_match_nonzero(). 7349 */ 7350static void 7351dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7352{ 7353 pkp->dtpk_prov = pdp->dtpd_provider; 7354 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7355 7356 pkp->dtpk_mod = pdp->dtpd_mod; 7357 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7358 7359 pkp->dtpk_func = pdp->dtpd_func; 7360 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7361 7362 pkp->dtpk_name = pdp->dtpd_name; 7363 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7364 7365 pkp->dtpk_id = pdp->dtpd_id; 7366 7367 if (pkp->dtpk_id == DTRACE_IDNONE && 7368 pkp->dtpk_pmatch == &dtrace_match_nul && 7369 pkp->dtpk_mmatch == &dtrace_match_nul && 7370 pkp->dtpk_fmatch == &dtrace_match_nul && 7371 pkp->dtpk_nmatch == &dtrace_match_nul) 7372 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7373} 7374 7375/* 7376 * DTrace Provider-to-Framework API Functions 7377 * 7378 * These functions implement much of the Provider-to-Framework API, as 7379 * described in <sys/dtrace.h>. The parts of the API not in this section are 7380 * the functions in the API for probe management (found below), and 7381 * dtrace_probe() itself (found above). 7382 */ 7383 7384/* 7385 * Register the calling provider with the DTrace framework. This should 7386 * generally be called by DTrace providers in their attach(9E) entry point. 7387 */ 7388int 7389dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7390 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7391{ 7392 dtrace_provider_t *provider; 7393 7394 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7395 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7396 "arguments", name ? name : "<NULL>"); 7397 return (EINVAL); 7398 } 7399 7400 if (name[0] == '\0' || dtrace_badname(name)) { 7401 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7402 "provider name", name); 7403 return (EINVAL); 7404 } 7405 7406 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7407 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7408 pops->dtps_destroy == NULL || 7409 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7410 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7411 "provider ops", name); 7412 return (EINVAL); 7413 } 7414 7415 if (dtrace_badattr(&pap->dtpa_provider) || 7416 dtrace_badattr(&pap->dtpa_mod) || 7417 dtrace_badattr(&pap->dtpa_func) || 7418 dtrace_badattr(&pap->dtpa_name) || 7419 dtrace_badattr(&pap->dtpa_args)) { 7420 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7421 "provider attributes", name); 7422 return (EINVAL); 7423 } 7424 7425 if (priv & ~DTRACE_PRIV_ALL) { 7426 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7427 "privilege attributes", name); 7428 return (EINVAL); 7429 } 7430 7431 if ((priv & DTRACE_PRIV_KERNEL) && 7432 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7433 pops->dtps_usermode == NULL) { 7434 cmn_err(CE_WARN, "failed to register provider '%s': need " 7435 "dtps_usermode() op for given privilege attributes", name); 7436 return (EINVAL); 7437 } 7438 7439 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7440 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7441 (void) strcpy(provider->dtpv_name, name); 7442 7443 provider->dtpv_attr = *pap; 7444 provider->dtpv_priv.dtpp_flags = priv; 7445 if (cr != NULL) { 7446 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7447 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7448 } 7449 provider->dtpv_pops = *pops; 7450 7451 if (pops->dtps_provide == NULL) { 7452 ASSERT(pops->dtps_provide_module != NULL); 7453 provider->dtpv_pops.dtps_provide = 7454 (void (*)(void *, const dtrace_probedesc_t *))dtrace_nullop; 7455 } 7456 7457 if (pops->dtps_provide_module == NULL) { 7458 ASSERT(pops->dtps_provide != NULL); 7459#if defined(sun) 7460 provider->dtpv_pops.dtps_provide_module = 7461 (void (*)(void *, modctl_t *))dtrace_nullop; 7462#else 7463 provider->dtpv_pops.dtps_provide_module = 7464 (void (*)(void *, dtrace_modctl_t *))dtrace_nullop; 7465#endif 7466 } 7467 7468 if (pops->dtps_suspend == NULL) { 7469 ASSERT(pops->dtps_resume == NULL); 7470 provider->dtpv_pops.dtps_suspend = 7471 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7472 provider->dtpv_pops.dtps_resume = 7473 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7474 } 7475 7476 provider->dtpv_arg = arg; 7477 *idp = (dtrace_provider_id_t)provider; 7478 7479 if (pops == &dtrace_provider_ops) { 7480 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7481 ASSERT(MUTEX_HELD(&dtrace_lock)); 7482 ASSERT(dtrace_anon.dta_enabling == NULL); 7483 7484 /* 7485 * We make sure that the DTrace provider is at the head of 7486 * the provider chain. 7487 */ 7488 provider->dtpv_next = dtrace_provider; 7489 dtrace_provider = provider; 7490 return (0); 7491 } 7492 7493 mutex_enter(&dtrace_provider_lock); 7494 mutex_enter(&dtrace_lock); 7495 7496 /* 7497 * If there is at least one provider registered, we'll add this 7498 * provider after the first provider. 7499 */ 7500 if (dtrace_provider != NULL) { 7501 provider->dtpv_next = dtrace_provider->dtpv_next; 7502 dtrace_provider->dtpv_next = provider; 7503 } else { 7504 dtrace_provider = provider; 7505 } 7506 7507 if (dtrace_retained != NULL) { 7508 dtrace_enabling_provide(provider); 7509 7510 /* 7511 * Now we need to call dtrace_enabling_matchall() -- which 7512 * will acquire cpu_lock and dtrace_lock. We therefore need 7513 * to drop all of our locks before calling into it... 7514 */ 7515 mutex_exit(&dtrace_lock); 7516 mutex_exit(&dtrace_provider_lock); 7517 dtrace_enabling_matchall(); 7518 7519 return (0); 7520 } 7521 7522 mutex_exit(&dtrace_lock); 7523 mutex_exit(&dtrace_provider_lock); 7524 7525 return (0); 7526} 7527 7528/* 7529 * Unregister the specified provider from the DTrace framework. This should 7530 * generally be called by DTrace providers in their detach(9E) entry point. 7531 */ 7532int 7533dtrace_unregister(dtrace_provider_id_t id) 7534{ 7535 dtrace_provider_t *old = (dtrace_provider_t *)id; 7536 dtrace_provider_t *prev = NULL; 7537 int i, self = 0; 7538 dtrace_probe_t *probe, *first = NULL; 7539 7540 if (old->dtpv_pops.dtps_enable == 7541 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop) { 7542 /* 7543 * If DTrace itself is the provider, we're called with locks 7544 * already held. 7545 */ 7546 ASSERT(old == dtrace_provider); 7547#if defined(sun) 7548 ASSERT(dtrace_devi != NULL); 7549#endif 7550 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7551 ASSERT(MUTEX_HELD(&dtrace_lock)); 7552 self = 1; 7553 7554 if (dtrace_provider->dtpv_next != NULL) { 7555 /* 7556 * There's another provider here; return failure. 7557 */ 7558 return (EBUSY); 7559 } 7560 } else { 7561 mutex_enter(&dtrace_provider_lock); 7562 mutex_enter(&mod_lock); 7563 mutex_enter(&dtrace_lock); 7564 } 7565 7566 /* 7567 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7568 * probes, we refuse to let providers slither away, unless this 7569 * provider has already been explicitly invalidated. 7570 */ 7571 if (!old->dtpv_defunct && 7572 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7573 dtrace_anon.dta_state->dts_necbs > 0))) { 7574 if (!self) { 7575 mutex_exit(&dtrace_lock); 7576 mutex_exit(&mod_lock); 7577 mutex_exit(&dtrace_provider_lock); 7578 } 7579 return (EBUSY); 7580 } 7581 7582 /* 7583 * Attempt to destroy the probes associated with this provider. 7584 */ 7585 for (i = 0; i < dtrace_nprobes; i++) { 7586 if ((probe = dtrace_probes[i]) == NULL) 7587 continue; 7588 7589 if (probe->dtpr_provider != old) 7590 continue; 7591 7592 if (probe->dtpr_ecb == NULL) 7593 continue; 7594 7595 /* 7596 * We have at least one ECB; we can't remove this provider. 7597 */ 7598 if (!self) { 7599 mutex_exit(&dtrace_lock); 7600 mutex_exit(&mod_lock); 7601 mutex_exit(&dtrace_provider_lock); 7602 } 7603 return (EBUSY); 7604 } 7605 7606 /* 7607 * All of the probes for this provider are disabled; we can safely 7608 * remove all of them from their hash chains and from the probe array. 7609 */ 7610 for (i = 0; i < dtrace_nprobes; i++) { 7611 if ((probe = dtrace_probes[i]) == NULL) 7612 continue; 7613 7614 if (probe->dtpr_provider != old) 7615 continue; 7616 7617 dtrace_probes[i] = NULL; 7618 7619 dtrace_hash_remove(dtrace_bymod, probe); 7620 dtrace_hash_remove(dtrace_byfunc, probe); 7621 dtrace_hash_remove(dtrace_byname, probe); 7622 7623 if (first == NULL) { 7624 first = probe; 7625 probe->dtpr_nextmod = NULL; 7626 } else { 7627 probe->dtpr_nextmod = first; 7628 first = probe; 7629 } 7630 } 7631 7632 /* 7633 * The provider's probes have been removed from the hash chains and 7634 * from the probe array. Now issue a dtrace_sync() to be sure that 7635 * everyone has cleared out from any probe array processing. 7636 */ 7637 dtrace_sync(); 7638 7639 for (probe = first; probe != NULL; probe = first) { 7640 first = probe->dtpr_nextmod; 7641 7642 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7643 probe->dtpr_arg); 7644 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7645 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7646 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7647#if defined(sun) 7648 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7649#else 7650 vmem_free(dtrace_arena, (uintptr_t)(probe->dtpr_id), 1); 7651#endif 7652 kmem_free(probe, sizeof (dtrace_probe_t)); 7653 } 7654 7655 if ((prev = dtrace_provider) == old) { 7656#if defined(sun) 7657 ASSERT(self || dtrace_devi == NULL); 7658 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7659#endif 7660 dtrace_provider = old->dtpv_next; 7661 } else { 7662 while (prev != NULL && prev->dtpv_next != old) 7663 prev = prev->dtpv_next; 7664 7665 if (prev == NULL) { 7666 panic("attempt to unregister non-existent " 7667 "dtrace provider %p\n", (void *)id); 7668 } 7669 7670 prev->dtpv_next = old->dtpv_next; 7671 } 7672 7673 if (!self) { 7674 mutex_exit(&dtrace_lock); 7675 mutex_exit(&mod_lock); 7676 mutex_exit(&dtrace_provider_lock); 7677 } 7678 7679 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7680 kmem_free(old, sizeof (dtrace_provider_t)); 7681 7682 return (0); 7683} 7684 7685/* 7686 * Invalidate the specified provider. All subsequent probe lookups for the 7687 * specified provider will fail, but its probes will not be removed. 7688 */ 7689void 7690dtrace_invalidate(dtrace_provider_id_t id) 7691{ 7692 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7693 7694 ASSERT(pvp->dtpv_pops.dtps_enable != 7695 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7696 7697 mutex_enter(&dtrace_provider_lock); 7698 mutex_enter(&dtrace_lock); 7699 7700 pvp->dtpv_defunct = 1; 7701 7702 mutex_exit(&dtrace_lock); 7703 mutex_exit(&dtrace_provider_lock); 7704} 7705 7706/* 7707 * Indicate whether or not DTrace has attached. 7708 */ 7709int 7710dtrace_attached(void) 7711{ 7712 /* 7713 * dtrace_provider will be non-NULL iff the DTrace driver has 7714 * attached. (It's non-NULL because DTrace is always itself a 7715 * provider.) 7716 */ 7717 return (dtrace_provider != NULL); 7718} 7719 7720/* 7721 * Remove all the unenabled probes for the given provider. This function is 7722 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7723 * -- just as many of its associated probes as it can. 7724 */ 7725int 7726dtrace_condense(dtrace_provider_id_t id) 7727{ 7728 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7729 int i; 7730 dtrace_probe_t *probe; 7731 7732 /* 7733 * Make sure this isn't the dtrace provider itself. 7734 */ 7735 ASSERT(prov->dtpv_pops.dtps_enable != 7736 (int (*)(void *, dtrace_id_t, void *))dtrace_enable_nullop); 7737 7738 mutex_enter(&dtrace_provider_lock); 7739 mutex_enter(&dtrace_lock); 7740 7741 /* 7742 * Attempt to destroy the probes associated with this provider. 7743 */ 7744 for (i = 0; i < dtrace_nprobes; i++) { 7745 if ((probe = dtrace_probes[i]) == NULL) 7746 continue; 7747 7748 if (probe->dtpr_provider != prov) 7749 continue; 7750 7751 if (probe->dtpr_ecb != NULL) 7752 continue; 7753 7754 dtrace_probes[i] = NULL; 7755 7756 dtrace_hash_remove(dtrace_bymod, probe); 7757 dtrace_hash_remove(dtrace_byfunc, probe); 7758 dtrace_hash_remove(dtrace_byname, probe); 7759 7760 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7761 probe->dtpr_arg); 7762 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7763 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7764 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7765 kmem_free(probe, sizeof (dtrace_probe_t)); 7766#if defined(sun) 7767 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7768#else 7769 vmem_free(dtrace_arena, ((uintptr_t)i + 1), 1); 7770#endif 7771 } 7772 7773 mutex_exit(&dtrace_lock); 7774 mutex_exit(&dtrace_provider_lock); 7775 7776 return (0); 7777} 7778 7779/* 7780 * DTrace Probe Management Functions 7781 * 7782 * The functions in this section perform the DTrace probe management, 7783 * including functions to create probes, look-up probes, and call into the 7784 * providers to request that probes be provided. Some of these functions are 7785 * in the Provider-to-Framework API; these functions can be identified by the 7786 * fact that they are not declared "static". 7787 */ 7788 7789/* 7790 * Create a probe with the specified module name, function name, and name. 7791 */ 7792dtrace_id_t 7793dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7794 const char *func, const char *name, int aframes, void *arg) 7795{ 7796 dtrace_probe_t *probe, **probes; 7797 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7798 dtrace_id_t id; 7799 vmem_addr_t offset; 7800 7801 if (provider == dtrace_provider) { 7802 ASSERT(MUTEX_HELD(&dtrace_lock)); 7803 } else { 7804 mutex_enter(&dtrace_lock); 7805 } 7806 7807 if (vmem_alloc(dtrace_arena, 1, VM_BESTFIT | VM_SLEEP, &offset) != 0) 7808 ASSERT(0); 7809 id = (dtrace_id_t)(uintptr_t)offset; 7810 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7811 7812 probe->dtpr_id = id; 7813 probe->dtpr_gen = dtrace_probegen++; 7814 probe->dtpr_mod = dtrace_strdup(mod); 7815 probe->dtpr_func = dtrace_strdup(func); 7816 probe->dtpr_name = dtrace_strdup(name); 7817 probe->dtpr_arg = arg; 7818 probe->dtpr_aframes = aframes; 7819 probe->dtpr_provider = provider; 7820 7821 dtrace_hash_add(dtrace_bymod, probe); 7822 dtrace_hash_add(dtrace_byfunc, probe); 7823 dtrace_hash_add(dtrace_byname, probe); 7824 7825 if (id - 1 >= dtrace_nprobes) { 7826 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7827 size_t nsize = osize << 1; 7828 7829 if (nsize == 0) { 7830 ASSERT(osize == 0); 7831 ASSERT(dtrace_probes == NULL); 7832 nsize = sizeof (dtrace_probe_t *); 7833 } 7834 7835 probes = kmem_zalloc(nsize, KM_SLEEP); 7836 dtrace_probes_size = nsize; 7837 7838 if (dtrace_probes == NULL) { 7839 ASSERT(osize == 0); 7840 dtrace_probes = probes; 7841 dtrace_nprobes = 1; 7842 } else { 7843 dtrace_probe_t **oprobes = dtrace_probes; 7844 7845 bcopy(oprobes, probes, osize); 7846 dtrace_membar_producer(); 7847 dtrace_probes = probes; 7848 7849 dtrace_sync(); 7850 7851 /* 7852 * All CPUs are now seeing the new probes array; we can 7853 * safely free the old array. 7854 */ 7855 kmem_free(oprobes, osize); 7856 dtrace_nprobes <<= 1; 7857 } 7858 7859 ASSERT(id - 1 < dtrace_nprobes); 7860 } 7861 7862 ASSERT(dtrace_probes[id - 1] == NULL); 7863 dtrace_probes[id - 1] = probe; 7864 7865 if (provider != dtrace_provider) 7866 mutex_exit(&dtrace_lock); 7867 7868 return (id); 7869} 7870 7871static dtrace_probe_t * 7872dtrace_probe_lookup_id(dtrace_id_t id) 7873{ 7874 ASSERT(MUTEX_HELD(&dtrace_lock)); 7875 7876 if (id == 0 || id > dtrace_nprobes) 7877 return (NULL); 7878 7879 return (dtrace_probes[id - 1]); 7880} 7881 7882static int 7883dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7884{ 7885 *((dtrace_id_t *)arg) = probe->dtpr_id; 7886 7887 return (DTRACE_MATCH_DONE); 7888} 7889 7890/* 7891 * Look up a probe based on provider and one or more of module name, function 7892 * name and probe name. 7893 */ 7894dtrace_id_t 7895dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 7896 char *func, char *name) 7897{ 7898 dtrace_probekey_t pkey; 7899 dtrace_id_t id; 7900 int match; 7901 7902 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7903 pkey.dtpk_pmatch = &dtrace_match_string; 7904 pkey.dtpk_mod = mod; 7905 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7906 pkey.dtpk_func = func; 7907 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7908 pkey.dtpk_name = name; 7909 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7910 pkey.dtpk_id = DTRACE_IDNONE; 7911 7912 mutex_enter(&dtrace_lock); 7913 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7914 dtrace_probe_lookup_match, &id); 7915 mutex_exit(&dtrace_lock); 7916 7917 ASSERT(match == 1 || match == 0); 7918 return (match ? id : 0); 7919} 7920 7921/* 7922 * Returns the probe argument associated with the specified probe. 7923 */ 7924void * 7925dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7926{ 7927 dtrace_probe_t *probe; 7928 void *rval = NULL; 7929 7930 mutex_enter(&dtrace_lock); 7931 7932 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7933 probe->dtpr_provider == (dtrace_provider_t *)id) 7934 rval = probe->dtpr_arg; 7935 7936 mutex_exit(&dtrace_lock); 7937 7938 return (rval); 7939} 7940 7941/* 7942 * Copy a probe into a probe description. 7943 */ 7944static void 7945dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7946{ 7947 bzero(pdp, sizeof (dtrace_probedesc_t)); 7948 pdp->dtpd_id = prp->dtpr_id; 7949 7950 (void) strncpy(pdp->dtpd_provider, 7951 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7952 7953 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7954 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7955 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7956} 7957 7958#ifdef notyet /* XXX TBD */ 7959#if !defined(sun) 7960static int 7961dtrace_probe_provide_cb(linker_file_t lf, void *arg) 7962{ 7963 dtrace_provider_t *prv = (dtrace_provider_t *) arg; 7964 7965 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, lf); 7966 7967 return(0); 7968} 7969#endif 7970#endif /* notyet */ 7971 7972 7973/* 7974 * Called to indicate that a probe -- or probes -- should be provided by a 7975 * specfied provider. If the specified description is NULL, the provider will 7976 * be told to provide all of its probes. (This is done whenever a new 7977 * consumer comes along, or whenever a retained enabling is to be matched.) If 7978 * the specified description is non-NULL, the provider is given the 7979 * opportunity to dynamically provide the specified probe, allowing providers 7980 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7981 * probes.) If the provider is NULL, the operations will be applied to all 7982 * providers; if the provider is non-NULL the operations will only be applied 7983 * to the specified provider. The dtrace_provider_lock must be held, and the 7984 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7985 * will need to grab the dtrace_lock when it reenters the framework through 7986 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7987 */ 7988static void 7989dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7990{ 7991#if defined(sun) 7992 modctl_t *ctl; 7993#else 7994 module_t *mod; 7995#endif 7996 int all = 0; 7997 7998 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7999 8000 if (prv == NULL) { 8001 all = 1; 8002 prv = dtrace_provider; 8003 } 8004 8005 do { 8006 /* 8007 * First, call the blanket provide operation. 8008 */ 8009 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 8010 8011 /* 8012 * Now call the per-module provide operation. We will grab 8013 * mod_lock to prevent the list from being modified. Note 8014 * that this also prevents the mod_busy bits from changing. 8015 * (mod_busy can only be changed with mod_lock held.) 8016 */ 8017 mutex_enter(&mod_lock); 8018 8019#if defined(sun) 8020 ctl = &modules; 8021 do { 8022 if (ctl->mod_busy || ctl->mod_mp == NULL) 8023 continue; 8024 8025 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 8026 8027 } while ((ctl = ctl->mod_next) != &modules); 8028#else 8029 8030 /* Fake netbsd module first */ 8031 if (mod_nbsd == NULL) { 8032 mod_nbsd = kmem_zalloc(sizeof(*mod_nbsd), KM_SLEEP); 8033 mod_nbsd->mod_info = kmem_zalloc(sizeof(modinfo_t), KM_SLEEP); 8034 mod_nbsd->mod_refcnt = 1; 8035 *((char **)(intptr_t)&mod_nbsd->mod_info->mi_name) = __UNCONST("netbsd"); 8036 } 8037 8038 kernconfig_lock(); 8039 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, mod_nbsd); 8040 TAILQ_FOREACH(mod, &module_list, mod_chain) { 8041 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, mod); 8042 } 8043 kernconfig_unlock(); 8044#endif 8045 8046 mutex_exit(&mod_lock); 8047 } while (all && (prv = prv->dtpv_next) != NULL); 8048} 8049 8050#if defined(sun) 8051/* 8052 * Iterate over each probe, and call the Framework-to-Provider API function 8053 * denoted by offs. 8054 */ 8055static void 8056dtrace_probe_foreach(uintptr_t offs) 8057{ 8058 dtrace_provider_t *prov; 8059 void (*func)(void *, dtrace_id_t, void *); 8060 dtrace_probe_t *probe; 8061 dtrace_icookie_t cookie; 8062 int i; 8063 8064 /* 8065 * We disable interrupts to walk through the probe array. This is 8066 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 8067 * won't see stale data. 8068 */ 8069 cookie = dtrace_interrupt_disable(); 8070 8071 for (i = 0; i < dtrace_nprobes; i++) { 8072 if ((probe = dtrace_probes[i]) == NULL) 8073 continue; 8074 8075 if (probe->dtpr_ecb == NULL) { 8076 /* 8077 * This probe isn't enabled -- don't call the function. 8078 */ 8079 continue; 8080 } 8081 8082 prov = probe->dtpr_provider; 8083 func = *((void(**)(void *, dtrace_id_t, void *)) 8084 ((uintptr_t)&prov->dtpv_pops + offs)); 8085 8086 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 8087 } 8088 8089 dtrace_interrupt_enable(cookie); 8090} 8091#endif 8092 8093static int 8094dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 8095{ 8096 dtrace_probekey_t pkey; 8097 uint32_t priv; 8098 uid_t uid; 8099 zoneid_t zoneid; 8100 8101 ASSERT(MUTEX_HELD(&dtrace_lock)); 8102 dtrace_ecb_create_cache = NULL; 8103 8104 if (desc == NULL) { 8105 /* 8106 * If we're passed a NULL description, we're being asked to 8107 * create an ECB with a NULL probe. 8108 */ 8109 (void) dtrace_ecb_create_enable(NULL, enab); 8110 return (0); 8111 } 8112 8113 dtrace_probekey(desc, &pkey); 8114 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 8115 &priv, &uid, &zoneid); 8116 8117 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 8118 enab)); 8119} 8120 8121/* 8122 * DTrace Helper Provider Functions 8123 */ 8124static void 8125dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 8126{ 8127 attr->dtat_name = DOF_ATTR_NAME(dofattr); 8128 attr->dtat_data = DOF_ATTR_DATA(dofattr); 8129 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 8130} 8131 8132static void 8133dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 8134 const dof_provider_t *dofprov, char *strtab) 8135{ 8136 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 8137 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 8138 dofprov->dofpv_provattr); 8139 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 8140 dofprov->dofpv_modattr); 8141 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 8142 dofprov->dofpv_funcattr); 8143 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 8144 dofprov->dofpv_nameattr); 8145 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 8146 dofprov->dofpv_argsattr); 8147} 8148 8149static void 8150dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8151{ 8152 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8153 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8154 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 8155 dof_provider_t *provider; 8156 dof_probe_t *probe; 8157 uint32_t *off, *enoff; 8158 uint8_t *arg; 8159 char *strtab; 8160 uint_t i, nprobes; 8161 dtrace_helper_provdesc_t dhpv; 8162 dtrace_helper_probedesc_t dhpb; 8163 dtrace_meta_t *meta = dtrace_meta_pid; 8164 dtrace_mops_t *mops = &meta->dtm_mops; 8165 void *parg; 8166 8167 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8168 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8169 provider->dofpv_strtab * dof->dofh_secsize); 8170 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8171 provider->dofpv_probes * dof->dofh_secsize); 8172 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8173 provider->dofpv_prargs * dof->dofh_secsize); 8174 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8175 provider->dofpv_proffs * dof->dofh_secsize); 8176 8177 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8178 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 8179 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 8180 enoff = NULL; 8181 8182 /* 8183 * See dtrace_helper_provider_validate(). 8184 */ 8185 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 8186 provider->dofpv_prenoffs != DOF_SECT_NONE) { 8187 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8188 provider->dofpv_prenoffs * dof->dofh_secsize); 8189 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 8190 } 8191 8192 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 8193 8194 /* 8195 * Create the provider. 8196 */ 8197 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8198 8199 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8200 return; 8201 8202 meta->dtm_count++; 8203 8204 /* 8205 * Create the probes. 8206 */ 8207 for (i = 0; i < nprobes; i++) { 8208 probe = (dof_probe_t *)(uintptr_t)(daddr + 8209 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8210 8211 dhpb.dthpb_mod = dhp->dofhp_mod; 8212 dhpb.dthpb_func = strtab + probe->dofpr_func; 8213 dhpb.dthpb_name = strtab + probe->dofpr_name; 8214 dhpb.dthpb_base = probe->dofpr_addr; 8215 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8216 dhpb.dthpb_noffs = probe->dofpr_noffs; 8217 if (enoff != NULL) { 8218 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8219 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8220 } else { 8221 dhpb.dthpb_enoffs = NULL; 8222 dhpb.dthpb_nenoffs = 0; 8223 } 8224 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8225 dhpb.dthpb_nargc = probe->dofpr_nargc; 8226 dhpb.dthpb_xargc = probe->dofpr_xargc; 8227 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8228 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8229 8230 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8231 } 8232} 8233 8234static void 8235dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8236{ 8237 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8238 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8239 int i; 8240 8241 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8242 8243 for (i = 0; i < dof->dofh_secnum; i++) { 8244 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8245 dof->dofh_secoff + i * dof->dofh_secsize); 8246 8247 if (sec->dofs_type != DOF_SECT_PROVIDER) 8248 continue; 8249 8250 dtrace_helper_provide_one(dhp, sec, pid); 8251 } 8252 8253 /* 8254 * We may have just created probes, so we must now rematch against 8255 * any retained enablings. Note that this call will acquire both 8256 * cpu_lock and dtrace_lock; the fact that we are holding 8257 * dtrace_meta_lock now is what defines the ordering with respect to 8258 * these three locks. 8259 */ 8260 dtrace_enabling_matchall(); 8261} 8262 8263#if defined(sun) 8264static void 8265dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8266{ 8267 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8268 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8269 dof_sec_t *str_sec; 8270 dof_provider_t *provider; 8271 char *strtab; 8272 dtrace_helper_provdesc_t dhpv; 8273 dtrace_meta_t *meta = dtrace_meta_pid; 8274 dtrace_mops_t *mops = &meta->dtm_mops; 8275 8276 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8277 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8278 provider->dofpv_strtab * dof->dofh_secsize); 8279 8280 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8281 8282 /* 8283 * Create the provider. 8284 */ 8285 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8286 8287 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8288 8289 meta->dtm_count--; 8290} 8291 8292static void 8293dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8294{ 8295 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8296 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8297 int i; 8298 8299 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8300 8301 for (i = 0; i < dof->dofh_secnum; i++) { 8302 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8303 dof->dofh_secoff + i * dof->dofh_secsize); 8304 8305 if (sec->dofs_type != DOF_SECT_PROVIDER) 8306 continue; 8307 8308 dtrace_helper_provider_remove_one(dhp, sec, pid); 8309 } 8310} 8311#endif 8312 8313/* 8314 * DTrace Meta Provider-to-Framework API Functions 8315 * 8316 * These functions implement the Meta Provider-to-Framework API, as described 8317 * in <sys/dtrace.h>. 8318 */ 8319int 8320dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8321 dtrace_meta_provider_id_t *idp) 8322{ 8323 dtrace_meta_t *meta; 8324 dtrace_helpers_t *help, *next; 8325 int i; 8326 8327 *idp = DTRACE_METAPROVNONE; 8328 8329 /* 8330 * We strictly don't need the name, but we hold onto it for 8331 * debuggability. All hail error queues! 8332 */ 8333 if (name == NULL) { 8334 cmn_err(CE_WARN, "failed to register meta-provider: " 8335 "invalid name"); 8336 return (EINVAL); 8337 } 8338 8339 if (mops == NULL || 8340 mops->dtms_create_probe == NULL || 8341 mops->dtms_provide_pid == NULL || 8342 mops->dtms_remove_pid == NULL) { 8343 cmn_err(CE_WARN, "failed to register meta-register %s: " 8344 "invalid ops", name); 8345 return (EINVAL); 8346 } 8347 8348 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8349 meta->dtm_mops = *mops; 8350 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8351 (void) strcpy(meta->dtm_name, name); 8352 meta->dtm_arg = arg; 8353 8354 mutex_enter(&dtrace_meta_lock); 8355 mutex_enter(&dtrace_lock); 8356 8357 if (dtrace_meta_pid != NULL) { 8358 mutex_exit(&dtrace_lock); 8359 mutex_exit(&dtrace_meta_lock); 8360 cmn_err(CE_WARN, "failed to register meta-register %s: " 8361 "user-land meta-provider exists", name); 8362 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8363 kmem_free(meta, sizeof (dtrace_meta_t)); 8364 return (EINVAL); 8365 } 8366 8367 dtrace_meta_pid = meta; 8368 *idp = (dtrace_meta_provider_id_t)meta; 8369 8370 /* 8371 * If there are providers and probes ready to go, pass them 8372 * off to the new meta provider now. 8373 */ 8374 8375 help = dtrace_deferred_pid; 8376 dtrace_deferred_pid = NULL; 8377 8378 mutex_exit(&dtrace_lock); 8379 8380 while (help != NULL) { 8381 for (i = 0; i < help->dthps_nprovs; i++) { 8382 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8383 help->dthps_pid); 8384 } 8385 8386 next = help->dthps_next; 8387 help->dthps_next = NULL; 8388 help->dthps_prev = NULL; 8389 help->dthps_deferred = 0; 8390 help = next; 8391 } 8392 8393 mutex_exit(&dtrace_meta_lock); 8394 8395 return (0); 8396} 8397 8398int 8399dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8400{ 8401 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8402 8403 mutex_enter(&dtrace_meta_lock); 8404 mutex_enter(&dtrace_lock); 8405 8406 if (old == dtrace_meta_pid) { 8407 pp = &dtrace_meta_pid; 8408 } else { 8409 panic("attempt to unregister non-existent " 8410 "dtrace meta-provider %p\n", (void *)old); 8411 } 8412 8413 if (old->dtm_count != 0) { 8414 mutex_exit(&dtrace_lock); 8415 mutex_exit(&dtrace_meta_lock); 8416 return (EBUSY); 8417 } 8418 8419 *pp = NULL; 8420 8421 mutex_exit(&dtrace_lock); 8422 mutex_exit(&dtrace_meta_lock); 8423 8424 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8425 kmem_free(old, sizeof (dtrace_meta_t)); 8426 8427 return (0); 8428} 8429 8430 8431/* 8432 * DTrace DIF Object Functions 8433 */ 8434static int 8435dtrace_difo_err(uint_t pc, const char *format, ...) 8436{ 8437 if (dtrace_err_verbose) { 8438 va_list alist; 8439 8440 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8441 va_start(alist, format); 8442 (void) vuprintf(format, alist); 8443 va_end(alist); 8444 } 8445 8446#ifdef DTRACE_ERRDEBUG 8447 dtrace_errdebug(format); 8448#endif 8449 return (1); 8450} 8451 8452/* 8453 * Validate a DTrace DIF object by checking the IR instructions. The following 8454 * rules are currently enforced by dtrace_difo_validate(): 8455 * 8456 * 1. Each instruction must have a valid opcode 8457 * 2. Each register, string, variable, or subroutine reference must be valid 8458 * 3. No instruction can modify register %r0 (must be zero) 8459 * 4. All instruction reserved bits must be set to zero 8460 * 5. The last instruction must be a "ret" instruction 8461 * 6. All branch targets must reference a valid instruction _after_ the branch 8462 */ 8463static int 8464dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8465 cred_t *cr) 8466{ 8467 int err = 0, i; 8468 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8469 int kcheckload; 8470 uint_t pc; 8471 8472 kcheckload = cr == NULL || 8473 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8474 8475 dp->dtdo_destructive = 0; 8476 8477 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8478 dif_instr_t instr = dp->dtdo_buf[pc]; 8479 8480 uint_t r1 = DIF_INSTR_R1(instr); 8481 uint_t r2 = DIF_INSTR_R2(instr); 8482 uint_t rd = DIF_INSTR_RD(instr); 8483 uint_t rs = DIF_INSTR_RS(instr); 8484 uint_t label = DIF_INSTR_LABEL(instr); 8485 uint_t v = DIF_INSTR_VAR(instr); 8486 uint_t subr = DIF_INSTR_SUBR(instr); 8487 uint_t type = DIF_INSTR_TYPE(instr); 8488 uint_t op = DIF_INSTR_OP(instr); 8489 8490 switch (op) { 8491 case DIF_OP_OR: 8492 case DIF_OP_XOR: 8493 case DIF_OP_AND: 8494 case DIF_OP_SLL: 8495 case DIF_OP_SRL: 8496 case DIF_OP_SRA: 8497 case DIF_OP_SUB: 8498 case DIF_OP_ADD: 8499 case DIF_OP_MUL: 8500 case DIF_OP_SDIV: 8501 case DIF_OP_UDIV: 8502 case DIF_OP_SREM: 8503 case DIF_OP_UREM: 8504 case DIF_OP_COPYS: 8505 if (r1 >= nregs) 8506 err += efunc(pc, "invalid register %u\n", r1); 8507 if (r2 >= nregs) 8508 err += efunc(pc, "invalid register %u\n", r2); 8509 if (rd >= nregs) 8510 err += efunc(pc, "invalid register %u\n", rd); 8511 if (rd == 0) 8512 err += efunc(pc, "cannot write to %r0\n"); 8513 break; 8514 case DIF_OP_NOT: 8515 case DIF_OP_MOV: 8516 case DIF_OP_ALLOCS: 8517 if (r1 >= nregs) 8518 err += efunc(pc, "invalid register %u\n", r1); 8519 if (r2 != 0) 8520 err += efunc(pc, "non-zero reserved bits\n"); 8521 if (rd >= nregs) 8522 err += efunc(pc, "invalid register %u\n", rd); 8523 if (rd == 0) 8524 err += efunc(pc, "cannot write to %r0\n"); 8525 break; 8526 case DIF_OP_LDSB: 8527 case DIF_OP_LDSH: 8528 case DIF_OP_LDSW: 8529 case DIF_OP_LDUB: 8530 case DIF_OP_LDUH: 8531 case DIF_OP_LDUW: 8532 case DIF_OP_LDX: 8533 if (r1 >= nregs) 8534 err += efunc(pc, "invalid register %u\n", r1); 8535 if (r2 != 0) 8536 err += efunc(pc, "non-zero reserved bits\n"); 8537 if (rd >= nregs) 8538 err += efunc(pc, "invalid register %u\n", rd); 8539 if (rd == 0) 8540 err += efunc(pc, "cannot write to %r0\n"); 8541 if (kcheckload) 8542 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8543 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8544 break; 8545 case DIF_OP_RLDSB: 8546 case DIF_OP_RLDSH: 8547 case DIF_OP_RLDSW: 8548 case DIF_OP_RLDUB: 8549 case DIF_OP_RLDUH: 8550 case DIF_OP_RLDUW: 8551 case DIF_OP_RLDX: 8552 if (r1 >= nregs) 8553 err += efunc(pc, "invalid register %u\n", r1); 8554 if (r2 != 0) 8555 err += efunc(pc, "non-zero reserved bits\n"); 8556 if (rd >= nregs) 8557 err += efunc(pc, "invalid register %u\n", rd); 8558 if (rd == 0) 8559 err += efunc(pc, "cannot write to %r0\n"); 8560 break; 8561 case DIF_OP_ULDSB: 8562 case DIF_OP_ULDSH: 8563 case DIF_OP_ULDSW: 8564 case DIF_OP_ULDUB: 8565 case DIF_OP_ULDUH: 8566 case DIF_OP_ULDUW: 8567 case DIF_OP_ULDX: 8568 if (r1 >= nregs) 8569 err += efunc(pc, "invalid register %u\n", r1); 8570 if (r2 != 0) 8571 err += efunc(pc, "non-zero reserved bits\n"); 8572 if (rd >= nregs) 8573 err += efunc(pc, "invalid register %u\n", rd); 8574 if (rd == 0) 8575 err += efunc(pc, "cannot write to %r0\n"); 8576 break; 8577 case DIF_OP_STB: 8578 case DIF_OP_STH: 8579 case DIF_OP_STW: 8580 case DIF_OP_STX: 8581 if (r1 >= nregs) 8582 err += efunc(pc, "invalid register %u\n", r1); 8583 if (r2 != 0) 8584 err += efunc(pc, "non-zero reserved bits\n"); 8585 if (rd >= nregs) 8586 err += efunc(pc, "invalid register %u\n", rd); 8587 if (rd == 0) 8588 err += efunc(pc, "cannot write to 0 address\n"); 8589 break; 8590 case DIF_OP_CMP: 8591 case DIF_OP_SCMP: 8592 if (r1 >= nregs) 8593 err += efunc(pc, "invalid register %u\n", r1); 8594 if (r2 >= nregs) 8595 err += efunc(pc, "invalid register %u\n", r2); 8596 if (rd != 0) 8597 err += efunc(pc, "non-zero reserved bits\n"); 8598 break; 8599 case DIF_OP_TST: 8600 if (r1 >= nregs) 8601 err += efunc(pc, "invalid register %u\n", r1); 8602 if (r2 != 0 || rd != 0) 8603 err += efunc(pc, "non-zero reserved bits\n"); 8604 break; 8605 case DIF_OP_BA: 8606 case DIF_OP_BE: 8607 case DIF_OP_BNE: 8608 case DIF_OP_BG: 8609 case DIF_OP_BGU: 8610 case DIF_OP_BGE: 8611 case DIF_OP_BGEU: 8612 case DIF_OP_BL: 8613 case DIF_OP_BLU: 8614 case DIF_OP_BLE: 8615 case DIF_OP_BLEU: 8616 if (label >= dp->dtdo_len) { 8617 err += efunc(pc, "invalid branch target %u\n", 8618 label); 8619 } 8620 if (label <= pc) { 8621 err += efunc(pc, "backward branch to %u\n", 8622 label); 8623 } 8624 break; 8625 case DIF_OP_RET: 8626 if (r1 != 0 || r2 != 0) 8627 err += efunc(pc, "non-zero reserved bits\n"); 8628 if (rd >= nregs) 8629 err += efunc(pc, "invalid register %u\n", rd); 8630 break; 8631 case DIF_OP_NOP: 8632 case DIF_OP_POPTS: 8633 case DIF_OP_FLUSHTS: 8634 if (r1 != 0 || r2 != 0 || rd != 0) 8635 err += efunc(pc, "non-zero reserved bits\n"); 8636 break; 8637 case DIF_OP_SETX: 8638 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8639 err += efunc(pc, "invalid integer ref %u\n", 8640 DIF_INSTR_INTEGER(instr)); 8641 } 8642 if (rd >= nregs) 8643 err += efunc(pc, "invalid register %u\n", rd); 8644 if (rd == 0) 8645 err += efunc(pc, "cannot write to %r0\n"); 8646 break; 8647 case DIF_OP_SETS: 8648 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8649 err += efunc(pc, "invalid string ref %u\n", 8650 DIF_INSTR_STRING(instr)); 8651 } 8652 if (rd >= nregs) 8653 err += efunc(pc, "invalid register %u\n", rd); 8654 if (rd == 0) 8655 err += efunc(pc, "cannot write to %r0\n"); 8656 break; 8657 case DIF_OP_LDGA: 8658 case DIF_OP_LDTA: 8659 if (r1 > DIF_VAR_ARRAY_MAX) 8660 err += efunc(pc, "invalid array %u\n", r1); 8661 if (r2 >= nregs) 8662 err += efunc(pc, "invalid register %u\n", r2); 8663 if (rd >= nregs) 8664 err += efunc(pc, "invalid register %u\n", rd); 8665 if (rd == 0) 8666 err += efunc(pc, "cannot write to %r0\n"); 8667 break; 8668 case DIF_OP_LDGS: 8669 case DIF_OP_LDTS: 8670 case DIF_OP_LDLS: 8671 case DIF_OP_LDGAA: 8672 case DIF_OP_LDTAA: 8673 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8674 err += efunc(pc, "invalid variable %u\n", v); 8675 if (rd >= nregs) 8676 err += efunc(pc, "invalid register %u\n", rd); 8677 if (rd == 0) 8678 err += efunc(pc, "cannot write to %r0\n"); 8679 break; 8680 case DIF_OP_STGS: 8681 case DIF_OP_STTS: 8682 case DIF_OP_STLS: 8683 case DIF_OP_STGAA: 8684 case DIF_OP_STTAA: 8685 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8686 err += efunc(pc, "invalid variable %u\n", v); 8687 if (rs >= nregs) 8688 err += efunc(pc, "invalid register %u\n", rd); 8689 break; 8690 case DIF_OP_CALL: 8691 if (subr > DIF_SUBR_MAX) 8692 err += efunc(pc, "invalid subr %u\n", subr); 8693 if (rd >= nregs) 8694 err += efunc(pc, "invalid register %u\n", rd); 8695 if (rd == 0) 8696 err += efunc(pc, "cannot write to %r0\n"); 8697 8698 if (subr == DIF_SUBR_COPYOUT || 8699 subr == DIF_SUBR_COPYOUTSTR) { 8700 dp->dtdo_destructive = 1; 8701 } 8702 break; 8703 case DIF_OP_PUSHTR: 8704 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8705 err += efunc(pc, "invalid ref type %u\n", type); 8706 if (r2 >= nregs) 8707 err += efunc(pc, "invalid register %u\n", r2); 8708 if (rs >= nregs) 8709 err += efunc(pc, "invalid register %u\n", rs); 8710 break; 8711 case DIF_OP_PUSHTV: 8712 if (type != DIF_TYPE_CTF) 8713 err += efunc(pc, "invalid val type %u\n", type); 8714 if (r2 >= nregs) 8715 err += efunc(pc, "invalid register %u\n", r2); 8716 if (rs >= nregs) 8717 err += efunc(pc, "invalid register %u\n", rs); 8718 break; 8719 default: 8720 err += efunc(pc, "invalid opcode %u\n", 8721 DIF_INSTR_OP(instr)); 8722 } 8723 } 8724 8725 if (dp->dtdo_len != 0 && 8726 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8727 err += efunc(dp->dtdo_len - 1, 8728 "expected 'ret' as last DIF instruction\n"); 8729 } 8730 8731 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8732 /* 8733 * If we're not returning by reference, the size must be either 8734 * 0 or the size of one of the base types. 8735 */ 8736 switch (dp->dtdo_rtype.dtdt_size) { 8737 case 0: 8738 case sizeof (uint8_t): 8739 case sizeof (uint16_t): 8740 case sizeof (uint32_t): 8741 case sizeof (uint64_t): 8742 break; 8743 8744 default: 8745 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 8746 } 8747 } 8748 8749 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8750 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8751 dtrace_diftype_t *vt, *et; 8752 uint_t id, ndx; 8753 8754 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8755 v->dtdv_scope != DIFV_SCOPE_THREAD && 8756 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8757 err += efunc(i, "unrecognized variable scope %d\n", 8758 v->dtdv_scope); 8759 break; 8760 } 8761 8762 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8763 v->dtdv_kind != DIFV_KIND_SCALAR) { 8764 err += efunc(i, "unrecognized variable type %d\n", 8765 v->dtdv_kind); 8766 break; 8767 } 8768 8769 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8770 err += efunc(i, "%d exceeds variable id limit\n", id); 8771 break; 8772 } 8773 8774 if (id < DIF_VAR_OTHER_UBASE) 8775 continue; 8776 8777 /* 8778 * For user-defined variables, we need to check that this 8779 * definition is identical to any previous definition that we 8780 * encountered. 8781 */ 8782 ndx = id - DIF_VAR_OTHER_UBASE; 8783 8784 switch (v->dtdv_scope) { 8785 case DIFV_SCOPE_GLOBAL: 8786 if (ndx < vstate->dtvs_nglobals) { 8787 dtrace_statvar_t *svar; 8788 8789 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8790 existing = &svar->dtsv_var; 8791 } 8792 8793 break; 8794 8795 case DIFV_SCOPE_THREAD: 8796 if (ndx < vstate->dtvs_ntlocals) 8797 existing = &vstate->dtvs_tlocals[ndx]; 8798 break; 8799 8800 case DIFV_SCOPE_LOCAL: 8801 if (ndx < vstate->dtvs_nlocals) { 8802 dtrace_statvar_t *svar; 8803 8804 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8805 existing = &svar->dtsv_var; 8806 } 8807 8808 break; 8809 } 8810 8811 vt = &v->dtdv_type; 8812 8813 if (vt->dtdt_flags & DIF_TF_BYREF) { 8814 if (vt->dtdt_size == 0) { 8815 err += efunc(i, "zero-sized variable\n"); 8816 break; 8817 } 8818 8819 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8820 vt->dtdt_size > dtrace_global_maxsize) { 8821 err += efunc(i, "oversized by-ref global\n"); 8822 break; 8823 } 8824 } 8825 8826 if (existing == NULL || existing->dtdv_id == 0) 8827 continue; 8828 8829 ASSERT(existing->dtdv_id == v->dtdv_id); 8830 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8831 8832 if (existing->dtdv_kind != v->dtdv_kind) 8833 err += efunc(i, "%d changed variable kind\n", id); 8834 8835 et = &existing->dtdv_type; 8836 8837 if (vt->dtdt_flags != et->dtdt_flags) { 8838 err += efunc(i, "%d changed variable type flags\n", id); 8839 break; 8840 } 8841 8842 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8843 err += efunc(i, "%d changed variable type size\n", id); 8844 break; 8845 } 8846 } 8847 8848 return (err); 8849} 8850 8851#if defined(sun) 8852/* 8853 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8854 * are much more constrained than normal DIFOs. Specifically, they may 8855 * not: 8856 * 8857 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8858 * miscellaneous string routines 8859 * 2. Access DTrace variables other than the args[] array, and the 8860 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8861 * 3. Have thread-local variables. 8862 * 4. Have dynamic variables. 8863 */ 8864static int 8865dtrace_difo_validate_helper(dtrace_difo_t *dp) 8866{ 8867 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8868 int err = 0; 8869 uint_t pc; 8870 8871 for (pc = 0; pc < dp->dtdo_len; pc++) { 8872 dif_instr_t instr = dp->dtdo_buf[pc]; 8873 8874 uint_t v = DIF_INSTR_VAR(instr); 8875 uint_t subr = DIF_INSTR_SUBR(instr); 8876 uint_t op = DIF_INSTR_OP(instr); 8877 8878 switch (op) { 8879 case DIF_OP_OR: 8880 case DIF_OP_XOR: 8881 case DIF_OP_AND: 8882 case DIF_OP_SLL: 8883 case DIF_OP_SRL: 8884 case DIF_OP_SRA: 8885 case DIF_OP_SUB: 8886 case DIF_OP_ADD: 8887 case DIF_OP_MUL: 8888 case DIF_OP_SDIV: 8889 case DIF_OP_UDIV: 8890 case DIF_OP_SREM: 8891 case DIF_OP_UREM: 8892 case DIF_OP_COPYS: 8893 case DIF_OP_NOT: 8894 case DIF_OP_MOV: 8895 case DIF_OP_RLDSB: 8896 case DIF_OP_RLDSH: 8897 case DIF_OP_RLDSW: 8898 case DIF_OP_RLDUB: 8899 case DIF_OP_RLDUH: 8900 case DIF_OP_RLDUW: 8901 case DIF_OP_RLDX: 8902 case DIF_OP_ULDSB: 8903 case DIF_OP_ULDSH: 8904 case DIF_OP_ULDSW: 8905 case DIF_OP_ULDUB: 8906 case DIF_OP_ULDUH: 8907 case DIF_OP_ULDUW: 8908 case DIF_OP_ULDX: 8909 case DIF_OP_STB: 8910 case DIF_OP_STH: 8911 case DIF_OP_STW: 8912 case DIF_OP_STX: 8913 case DIF_OP_ALLOCS: 8914 case DIF_OP_CMP: 8915 case DIF_OP_SCMP: 8916 case DIF_OP_TST: 8917 case DIF_OP_BA: 8918 case DIF_OP_BE: 8919 case DIF_OP_BNE: 8920 case DIF_OP_BG: 8921 case DIF_OP_BGU: 8922 case DIF_OP_BGE: 8923 case DIF_OP_BGEU: 8924 case DIF_OP_BL: 8925 case DIF_OP_BLU: 8926 case DIF_OP_BLE: 8927 case DIF_OP_BLEU: 8928 case DIF_OP_RET: 8929 case DIF_OP_NOP: 8930 case DIF_OP_POPTS: 8931 case DIF_OP_FLUSHTS: 8932 case DIF_OP_SETX: 8933 case DIF_OP_SETS: 8934 case DIF_OP_LDGA: 8935 case DIF_OP_LDLS: 8936 case DIF_OP_STGS: 8937 case DIF_OP_STLS: 8938 case DIF_OP_PUSHTR: 8939 case DIF_OP_PUSHTV: 8940 break; 8941 8942 case DIF_OP_LDGS: 8943 if (v >= DIF_VAR_OTHER_UBASE) 8944 break; 8945 8946 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8947 break; 8948 8949 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8950 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8951 v == DIF_VAR_EXECARGS || 8952 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8953 v == DIF_VAR_UID || v == DIF_VAR_GID) 8954 break; 8955 8956 err += efunc(pc, "illegal variable %u\n", v); 8957 break; 8958 8959 case DIF_OP_LDTA: 8960 case DIF_OP_LDTS: 8961 case DIF_OP_LDGAA: 8962 case DIF_OP_LDTAA: 8963 err += efunc(pc, "illegal dynamic variable load\n"); 8964 break; 8965 8966 case DIF_OP_STTS: 8967 case DIF_OP_STGAA: 8968 case DIF_OP_STTAA: 8969 err += efunc(pc, "illegal dynamic variable store\n"); 8970 break; 8971 8972 case DIF_OP_CALL: 8973 if (subr == DIF_SUBR_ALLOCA || 8974 subr == DIF_SUBR_BCOPY || 8975 subr == DIF_SUBR_COPYIN || 8976 subr == DIF_SUBR_COPYINTO || 8977 subr == DIF_SUBR_COPYINSTR || 8978 subr == DIF_SUBR_INDEX || 8979 subr == DIF_SUBR_INET_NTOA || 8980 subr == DIF_SUBR_INET_NTOA6 || 8981 subr == DIF_SUBR_INET_NTOP || 8982 subr == DIF_SUBR_LLTOSTR || 8983 subr == DIF_SUBR_RINDEX || 8984 subr == DIF_SUBR_STRCHR || 8985 subr == DIF_SUBR_STRJOIN || 8986 subr == DIF_SUBR_STRRCHR || 8987 subr == DIF_SUBR_STRSTR || 8988 subr == DIF_SUBR_HTONS || 8989 subr == DIF_SUBR_HTONL || 8990 subr == DIF_SUBR_HTONLL || 8991 subr == DIF_SUBR_NTOHS || 8992 subr == DIF_SUBR_NTOHL || 8993 subr == DIF_SUBR_NTOHLL || 8994 subr == DIF_SUBR_MEMREF || 8995 subr == DIF_SUBR_TYPEREF) 8996 break; 8997 8998 err += efunc(pc, "invalid subr %u\n", subr); 8999 break; 9000 9001 default: 9002 err += efunc(pc, "invalid opcode %u\n", 9003 DIF_INSTR_OP(instr)); 9004 } 9005 } 9006 9007 return (err); 9008} 9009#endif 9010 9011/* 9012 * Returns 1 if the expression in the DIF object can be cached on a per-thread 9013 * basis; 0 if not. 9014 */ 9015static int 9016dtrace_difo_cacheable(dtrace_difo_t *dp) 9017{ 9018 int i; 9019 9020 if (dp == NULL) 9021 return (0); 9022 9023 for (i = 0; i < dp->dtdo_varlen; i++) { 9024 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9025 9026 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 9027 continue; 9028 9029 switch (v->dtdv_id) { 9030 case DIF_VAR_CURTHREAD: 9031 case DIF_VAR_PID: 9032 case DIF_VAR_TID: 9033 case DIF_VAR_EXECARGS: 9034 case DIF_VAR_EXECNAME: 9035 case DIF_VAR_ZONENAME: 9036 break; 9037 9038 default: 9039 return (0); 9040 } 9041 } 9042 9043 /* 9044 * This DIF object may be cacheable. Now we need to look for any 9045 * array loading instructions, any memory loading instructions, or 9046 * any stores to thread-local variables. 9047 */ 9048 for (i = 0; i < dp->dtdo_len; i++) { 9049 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 9050 9051 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 9052 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 9053 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 9054 op == DIF_OP_LDGA || op == DIF_OP_STTS) 9055 return (0); 9056 } 9057 9058 return (1); 9059} 9060 9061static void 9062dtrace_difo_hold(dtrace_difo_t *dp) 9063{ 9064 int i; 9065 9066 ASSERT(MUTEX_HELD(&dtrace_lock)); 9067 9068 dp->dtdo_refcnt++; 9069 ASSERT(dp->dtdo_refcnt != 0); 9070 9071 /* 9072 * We need to check this DIF object for references to the variable 9073 * DIF_VAR_VTIMESTAMP. 9074 */ 9075 for (i = 0; i < dp->dtdo_varlen; i++) { 9076 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9077 9078 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9079 continue; 9080 9081 if (dtrace_vtime_references++ == 0) 9082 dtrace_vtime_enable(); 9083 } 9084} 9085 9086/* 9087 * This routine calculates the dynamic variable chunksize for a given DIF 9088 * object. The calculation is not fool-proof, and can probably be tricked by 9089 * malicious DIF -- but it works for all compiler-generated DIF. Because this 9090 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 9091 * if a dynamic variable size exceeds the chunksize. 9092 */ 9093static void 9094dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9095{ 9096 uint64_t sval = 0; 9097 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 9098 const dif_instr_t *text = dp->dtdo_buf; 9099 uint_t pc, srd = 0; 9100 uint_t ttop = 0; 9101 size_t size, ksize; 9102 uint_t id, i; 9103 9104 for (pc = 0; pc < dp->dtdo_len; pc++) { 9105 dif_instr_t instr = text[pc]; 9106 uint_t op = DIF_INSTR_OP(instr); 9107 uint_t rd = DIF_INSTR_RD(instr); 9108 uint_t r1 = DIF_INSTR_R1(instr); 9109 uint_t nkeys = 0; 9110 uchar_t scope = 0; 9111 9112 dtrace_key_t *key = tupregs; 9113 9114 switch (op) { 9115 case DIF_OP_SETX: 9116 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 9117 srd = rd; 9118 continue; 9119 9120 case DIF_OP_STTS: 9121 key = &tupregs[DIF_DTR_NREGS]; 9122 key[0].dttk_size = 0; 9123 key[1].dttk_size = 0; 9124 nkeys = 2; 9125 scope = DIFV_SCOPE_THREAD; 9126 break; 9127 9128 case DIF_OP_STGAA: 9129 case DIF_OP_STTAA: 9130 nkeys = ttop; 9131 9132 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 9133 key[nkeys++].dttk_size = 0; 9134 9135 key[nkeys++].dttk_size = 0; 9136 9137 if (op == DIF_OP_STTAA) { 9138 scope = DIFV_SCOPE_THREAD; 9139 } else { 9140 scope = DIFV_SCOPE_GLOBAL; 9141 } 9142 9143 break; 9144 9145 case DIF_OP_PUSHTR: 9146 if (ttop == DIF_DTR_NREGS) 9147 return; 9148 9149 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 9150 /* 9151 * If the register for the size of the "pushtr" 9152 * is %r0 (or the value is 0) and the type is 9153 * a string, we'll use the system-wide default 9154 * string size. 9155 */ 9156 tupregs[ttop++].dttk_size = 9157 dtrace_strsize_default; 9158 } else { 9159 if (srd == 0) 9160 return; 9161 9162 tupregs[ttop++].dttk_size = sval; 9163 } 9164 9165 break; 9166 9167 case DIF_OP_PUSHTV: 9168 if (ttop == DIF_DTR_NREGS) 9169 return; 9170 9171 tupregs[ttop++].dttk_size = 0; 9172 break; 9173 9174 case DIF_OP_FLUSHTS: 9175 ttop = 0; 9176 break; 9177 9178 case DIF_OP_POPTS: 9179 if (ttop != 0) 9180 ttop--; 9181 break; 9182 } 9183 9184 sval = 0; 9185 srd = 0; 9186 9187 if (nkeys == 0) 9188 continue; 9189 9190 /* 9191 * We have a dynamic variable allocation; calculate its size. 9192 */ 9193 for (ksize = 0, i = 0; i < nkeys; i++) 9194 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9195 9196 size = sizeof (dtrace_dynvar_t); 9197 size += sizeof (dtrace_key_t) * (nkeys - 1); 9198 size += ksize; 9199 9200 /* 9201 * Now we need to determine the size of the stored data. 9202 */ 9203 id = DIF_INSTR_VAR(instr); 9204 9205 for (i = 0; i < dp->dtdo_varlen; i++) { 9206 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9207 9208 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9209 size += v->dtdv_type.dtdt_size; 9210 break; 9211 } 9212 } 9213 9214 if (i == dp->dtdo_varlen) 9215 return; 9216 9217 /* 9218 * We have the size. If this is larger than the chunk size 9219 * for our dynamic variable state, reset the chunk size. 9220 */ 9221 size = P2ROUNDUP(size, sizeof (uint64_t)); 9222 9223 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9224 vstate->dtvs_dynvars.dtds_chunksize = size; 9225 } 9226} 9227 9228static void 9229dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9230{ 9231 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9232 uint_t id; 9233 9234 ASSERT(MUTEX_HELD(&dtrace_lock)); 9235 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9236 9237 for (i = 0; i < dp->dtdo_varlen; i++) { 9238 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9239 dtrace_statvar_t *svar, ***svarp = NULL; 9240 size_t dsize = 0; 9241 uint8_t scope = v->dtdv_scope; 9242 int *np = NULL; 9243 9244 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9245 continue; 9246 9247 id -= DIF_VAR_OTHER_UBASE; 9248 9249 switch (scope) { 9250 case DIFV_SCOPE_THREAD: 9251 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9252 dtrace_difv_t *tlocals; 9253 9254 if ((ntlocals = (otlocals << 1)) == 0) 9255 ntlocals = 1; 9256 9257 osz = otlocals * sizeof (dtrace_difv_t); 9258 nsz = ntlocals * sizeof (dtrace_difv_t); 9259 9260 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9261 9262 if (osz != 0) { 9263 bcopy(vstate->dtvs_tlocals, 9264 tlocals, osz); 9265 kmem_free(vstate->dtvs_tlocals, osz); 9266 } 9267 9268 vstate->dtvs_tlocals = tlocals; 9269 vstate->dtvs_ntlocals = ntlocals; 9270 } 9271 9272 vstate->dtvs_tlocals[id] = *v; 9273 continue; 9274 9275 case DIFV_SCOPE_LOCAL: 9276 np = &vstate->dtvs_nlocals; 9277 svarp = &vstate->dtvs_locals; 9278 9279 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9280 dsize = NCPU * (v->dtdv_type.dtdt_size + 9281 sizeof (uint64_t)); 9282 else 9283 dsize = NCPU * sizeof (uint64_t); 9284 9285 break; 9286 9287 case DIFV_SCOPE_GLOBAL: 9288 np = &vstate->dtvs_nglobals; 9289 svarp = &vstate->dtvs_globals; 9290 9291 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9292 dsize = v->dtdv_type.dtdt_size + 9293 sizeof (uint64_t); 9294 9295 break; 9296 9297 default: 9298 ASSERT(0); 9299 } 9300 9301 while (id >= (oldsvars = *np)) { 9302 dtrace_statvar_t **statics; 9303 int newsvars, oldsize, newsize; 9304 9305 if ((newsvars = (oldsvars << 1)) == 0) 9306 newsvars = 1; 9307 9308 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9309 newsize = newsvars * sizeof (dtrace_statvar_t *); 9310 9311 statics = kmem_zalloc(newsize, KM_SLEEP); 9312 9313 if (oldsize != 0) { 9314 bcopy(*svarp, statics, oldsize); 9315 kmem_free(*svarp, oldsize); 9316 } 9317 9318 *svarp = statics; 9319 *np = newsvars; 9320 } 9321 9322 if ((svar = (*svarp)[id]) == NULL) { 9323 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9324 svar->dtsv_var = *v; 9325 9326 if ((svar->dtsv_size = dsize) != 0) { 9327 svar->dtsv_data = (uint64_t)(uintptr_t) 9328 kmem_zalloc(dsize, KM_SLEEP); 9329 } 9330 9331 (*svarp)[id] = svar; 9332 } 9333 9334 svar->dtsv_refcnt++; 9335 } 9336 9337 dtrace_difo_chunksize(dp, vstate); 9338 dtrace_difo_hold(dp); 9339} 9340 9341#if defined(sun) 9342static dtrace_difo_t * 9343dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9344{ 9345 dtrace_difo_t *new; 9346 size_t sz; 9347 9348 ASSERT(dp->dtdo_buf != NULL); 9349 ASSERT(dp->dtdo_refcnt != 0); 9350 9351 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9352 9353 ASSERT(dp->dtdo_buf != NULL); 9354 sz = dp->dtdo_len * sizeof (dif_instr_t); 9355 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9356 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9357 new->dtdo_len = dp->dtdo_len; 9358 9359 if (dp->dtdo_strtab != NULL) { 9360 ASSERT(dp->dtdo_strlen != 0); 9361 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9362 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9363 new->dtdo_strlen = dp->dtdo_strlen; 9364 } 9365 9366 if (dp->dtdo_inttab != NULL) { 9367 ASSERT(dp->dtdo_intlen != 0); 9368 sz = dp->dtdo_intlen * sizeof (uint64_t); 9369 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9370 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9371 new->dtdo_intlen = dp->dtdo_intlen; 9372 } 9373 9374 if (dp->dtdo_vartab != NULL) { 9375 ASSERT(dp->dtdo_varlen != 0); 9376 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9377 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9378 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9379 new->dtdo_varlen = dp->dtdo_varlen; 9380 } 9381 9382 dtrace_difo_init(new, vstate); 9383 return (new); 9384} 9385#endif 9386 9387static void 9388dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9389{ 9390 int i; 9391 9392 ASSERT(dp->dtdo_refcnt == 0); 9393 9394 for (i = 0; i < dp->dtdo_varlen; i++) { 9395 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9396 dtrace_statvar_t *svar, **svarp = NULL; 9397 uint_t id; 9398 uint8_t scope = v->dtdv_scope; 9399 int *np = NULL; 9400 9401 switch (scope) { 9402 case DIFV_SCOPE_THREAD: 9403 continue; 9404 9405 case DIFV_SCOPE_LOCAL: 9406 np = &vstate->dtvs_nlocals; 9407 svarp = vstate->dtvs_locals; 9408 break; 9409 9410 case DIFV_SCOPE_GLOBAL: 9411 np = &vstate->dtvs_nglobals; 9412 svarp = vstate->dtvs_globals; 9413 break; 9414 9415 default: 9416 ASSERT(0); 9417 } 9418 9419 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9420 continue; 9421 9422 id -= DIF_VAR_OTHER_UBASE; 9423 ASSERT(id < *np); 9424 9425 svar = svarp[id]; 9426 ASSERT(svar != NULL); 9427 ASSERT(svar->dtsv_refcnt > 0); 9428 9429 if (--svar->dtsv_refcnt > 0) 9430 continue; 9431 9432 if (svar->dtsv_size != 0) { 9433 ASSERT(svar->dtsv_data != 0); 9434 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9435 svar->dtsv_size); 9436 } 9437 9438 kmem_free(svar, sizeof (dtrace_statvar_t)); 9439 svarp[id] = NULL; 9440 } 9441 9442 if (dp->dtdo_buf != NULL) 9443 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9444 if (dp->dtdo_inttab != NULL) 9445 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9446 if (dp->dtdo_strtab != NULL) 9447 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9448 if (dp->dtdo_vartab != NULL) 9449 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9450 9451 kmem_free(dp, sizeof (dtrace_difo_t)); 9452} 9453 9454static void 9455dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9456{ 9457 int i; 9458 9459 ASSERT(MUTEX_HELD(&dtrace_lock)); 9460 ASSERT(dp->dtdo_refcnt != 0); 9461 9462 for (i = 0; i < dp->dtdo_varlen; i++) { 9463 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9464 9465 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9466 continue; 9467 9468 ASSERT(dtrace_vtime_references > 0); 9469 if (--dtrace_vtime_references == 0) 9470 dtrace_vtime_disable(); 9471 } 9472 9473 if (--dp->dtdo_refcnt == 0) 9474 dtrace_difo_destroy(dp, vstate); 9475} 9476 9477/* 9478 * DTrace Format Functions 9479 */ 9480static uint16_t 9481dtrace_format_add(dtrace_state_t *state, char *str) 9482{ 9483 char *fmt, **new; 9484 uint16_t ndx, len = strlen(str) + 1; 9485 9486 fmt = kmem_zalloc(len, KM_SLEEP); 9487 bcopy(str, fmt, len); 9488 9489 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9490 if (state->dts_formats[ndx] == NULL) { 9491 state->dts_formats[ndx] = fmt; 9492 return (ndx + 1); 9493 } 9494 } 9495 9496 if (state->dts_nformats == USHRT_MAX) { 9497 /* 9498 * This is only likely if a denial-of-service attack is being 9499 * attempted. As such, it's okay to fail silently here. 9500 */ 9501 kmem_free(fmt, len); 9502 return (0); 9503 } 9504 9505 /* 9506 * For simplicity, we always resize the formats array to be exactly the 9507 * number of formats. 9508 */ 9509 ndx = state->dts_nformats++; 9510 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9511 9512 if (state->dts_formats != NULL) { 9513 ASSERT(ndx != 0); 9514 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9515 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9516 } 9517 9518 state->dts_formats = new; 9519 state->dts_formats[ndx] = fmt; 9520 9521 return (ndx + 1); 9522} 9523 9524static void 9525dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9526{ 9527 char *fmt; 9528 9529 ASSERT(state->dts_formats != NULL); 9530 ASSERT(format <= state->dts_nformats); 9531 ASSERT(state->dts_formats[format - 1] != NULL); 9532 9533 fmt = state->dts_formats[format - 1]; 9534 kmem_free(fmt, strlen(fmt) + 1); 9535 state->dts_formats[format - 1] = NULL; 9536} 9537 9538static void 9539dtrace_format_destroy(dtrace_state_t *state) 9540{ 9541 int i; 9542 9543 if (state->dts_nformats == 0) { 9544 ASSERT(state->dts_formats == NULL); 9545 return; 9546 } 9547 9548 ASSERT(state->dts_formats != NULL); 9549 9550 for (i = 0; i < state->dts_nformats; i++) { 9551 char *fmt = state->dts_formats[i]; 9552 9553 if (fmt == NULL) 9554 continue; 9555 9556 kmem_free(fmt, strlen(fmt) + 1); 9557 } 9558 9559 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9560 state->dts_nformats = 0; 9561 state->dts_formats = NULL; 9562} 9563 9564/* 9565 * DTrace Predicate Functions 9566 */ 9567static dtrace_predicate_t * 9568dtrace_predicate_create(dtrace_difo_t *dp) 9569{ 9570 dtrace_predicate_t *pred; 9571 9572 ASSERT(MUTEX_HELD(&dtrace_lock)); 9573 ASSERT(dp->dtdo_refcnt != 0); 9574 9575 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9576 pred->dtp_difo = dp; 9577 pred->dtp_refcnt = 1; 9578 9579 if (!dtrace_difo_cacheable(dp)) 9580 return (pred); 9581 9582 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9583 /* 9584 * This is only theoretically possible -- we have had 2^32 9585 * cacheable predicates on this machine. We cannot allow any 9586 * more predicates to become cacheable: as unlikely as it is, 9587 * there may be a thread caching a (now stale) predicate cache 9588 * ID. (N.B.: the temptation is being successfully resisted to 9589 * have this cmn_err() "Holy shit -- we executed this code!") 9590 */ 9591 return (pred); 9592 } 9593 9594 pred->dtp_cacheid = dtrace_predcache_id++; 9595 9596 return (pred); 9597} 9598 9599static void 9600dtrace_predicate_hold(dtrace_predicate_t *pred) 9601{ 9602 ASSERT(MUTEX_HELD(&dtrace_lock)); 9603 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9604 ASSERT(pred->dtp_refcnt > 0); 9605 9606 pred->dtp_refcnt++; 9607} 9608 9609static void 9610dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9611{ 9612 dtrace_difo_t *dp = pred->dtp_difo; 9613 9614 ASSERT(MUTEX_HELD(&dtrace_lock)); 9615 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9616 ASSERT(pred->dtp_refcnt > 0); 9617 9618 if (--pred->dtp_refcnt == 0) { 9619 dtrace_difo_release(pred->dtp_difo, vstate); 9620 kmem_free(pred, sizeof (dtrace_predicate_t)); 9621 } 9622} 9623 9624/* 9625 * DTrace Action Description Functions 9626 */ 9627static dtrace_actdesc_t * 9628dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9629 uint64_t uarg, uint64_t arg) 9630{ 9631 dtrace_actdesc_t *act; 9632 9633#if defined(sun) 9634 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9635 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9636#endif 9637 9638 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9639 act->dtad_kind = kind; 9640 act->dtad_ntuple = ntuple; 9641 act->dtad_uarg = uarg; 9642 act->dtad_arg = arg; 9643 act->dtad_refcnt = 1; 9644 9645 return (act); 9646} 9647 9648static void 9649dtrace_actdesc_hold(dtrace_actdesc_t *act) 9650{ 9651 ASSERT(act->dtad_refcnt >= 1); 9652 act->dtad_refcnt++; 9653} 9654 9655static void 9656dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9657{ 9658 dtrace_actkind_t kind = act->dtad_kind; 9659 dtrace_difo_t *dp; 9660 9661 ASSERT(act->dtad_refcnt >= 1); 9662 9663 if (--act->dtad_refcnt != 0) 9664 return; 9665 9666 if ((dp = act->dtad_difo) != NULL) 9667 dtrace_difo_release(dp, vstate); 9668 9669 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9670 char *str = (char *)(uintptr_t)act->dtad_arg; 9671 9672#if defined(sun) 9673 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9674 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9675#endif 9676 9677 if (str != NULL) 9678 kmem_free(str, strlen(str) + 1); 9679 } 9680 9681 kmem_free(act, sizeof (dtrace_actdesc_t)); 9682} 9683 9684/* 9685 * DTrace ECB Functions 9686 */ 9687static dtrace_ecb_t * 9688dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9689{ 9690 dtrace_ecb_t *ecb; 9691 dtrace_epid_t epid; 9692 9693 ASSERT(MUTEX_HELD(&dtrace_lock)); 9694 9695 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9696 ecb->dte_predicate = NULL; 9697 ecb->dte_probe = probe; 9698 9699 /* 9700 * The default size is the size of the default action: recording 9701 * the epid. 9702 */ 9703 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9704 ecb->dte_alignment = sizeof (dtrace_epid_t); 9705 9706 epid = state->dts_epid++; 9707 9708 if (epid - 1 >= state->dts_necbs) { 9709 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9710 int necbs = state->dts_necbs << 1; 9711 9712 ASSERT(epid == state->dts_necbs + 1); 9713 9714 if (necbs == 0) { 9715 ASSERT(oecbs == NULL); 9716 necbs = 1; 9717 } 9718 9719 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9720 9721 if (oecbs != NULL) 9722 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9723 9724 dtrace_membar_producer(); 9725 state->dts_ecbs = ecbs; 9726 9727 if (oecbs != NULL) { 9728 /* 9729 * If this state is active, we must dtrace_sync() 9730 * before we can free the old dts_ecbs array: we're 9731 * coming in hot, and there may be active ring 9732 * buffer processing (which indexes into the dts_ecbs 9733 * array) on another CPU. 9734 */ 9735 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9736 dtrace_sync(); 9737 9738 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9739 } 9740 9741 dtrace_membar_producer(); 9742 state->dts_necbs = necbs; 9743 } 9744 9745 ecb->dte_state = state; 9746 9747 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9748 dtrace_membar_producer(); 9749 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9750 9751 return (ecb); 9752} 9753 9754static int 9755dtrace_ecb_enable(dtrace_ecb_t *ecb) 9756{ 9757 dtrace_probe_t *probe = ecb->dte_probe; 9758 9759 ASSERT(MUTEX_HELD(&cpu_lock)); 9760 ASSERT(MUTEX_HELD(&dtrace_lock)); 9761 ASSERT(ecb->dte_next == NULL); 9762 9763 if (probe == NULL) { 9764 /* 9765 * This is the NULL probe -- there's nothing to do. 9766 */ 9767 return (0); 9768 } 9769 9770 if (probe->dtpr_ecb == NULL) { 9771 dtrace_provider_t *prov = probe->dtpr_provider; 9772 9773 /* 9774 * We're the first ECB on this probe. 9775 */ 9776 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9777 9778 if (ecb->dte_predicate != NULL) 9779 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9780 9781 return (prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9782 probe->dtpr_id, probe->dtpr_arg)); 9783 } else { 9784 /* 9785 * This probe is already active. Swing the last pointer to 9786 * point to the new ECB, and issue a dtrace_sync() to assure 9787 * that all CPUs have seen the change. 9788 */ 9789 ASSERT(probe->dtpr_ecb_last != NULL); 9790 probe->dtpr_ecb_last->dte_next = ecb; 9791 probe->dtpr_ecb_last = ecb; 9792 probe->dtpr_predcache = 0; 9793 9794 dtrace_sync(); 9795 return (0); 9796 } 9797} 9798 9799static void 9800dtrace_ecb_resize(dtrace_ecb_t *ecb) 9801{ 9802 uint32_t maxalign = sizeof (dtrace_epid_t); 9803 uint32_t align = sizeof (uint8_t), offs, diff; 9804 dtrace_action_t *act; 9805 int wastuple = 0; 9806 uint32_t aggbase = UINT32_MAX; 9807 dtrace_state_t *state = ecb->dte_state; 9808 9809 /* 9810 * If we record anything, we always record the epid. (And we always 9811 * record it first.) 9812 */ 9813 offs = sizeof (dtrace_epid_t); 9814 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9815 9816 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9817 dtrace_recdesc_t *rec = &act->dta_rec; 9818 9819 if ((align = rec->dtrd_alignment) > maxalign) 9820 maxalign = align; 9821 9822 if (!wastuple && act->dta_intuple) { 9823 /* 9824 * This is the first record in a tuple. Align the 9825 * offset to be at offset 4 in an 8-byte aligned 9826 * block. 9827 */ 9828 diff = offs + sizeof (dtrace_aggid_t); 9829 9830 if ((diff = (diff & (sizeof (uint64_t) - 1)))) 9831 offs += sizeof (uint64_t) - diff; 9832 9833 aggbase = offs - sizeof (dtrace_aggid_t); 9834 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 9835 } 9836 9837 /*LINTED*/ 9838 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 9839 /* 9840 * The current offset is not properly aligned; align it. 9841 */ 9842 offs += align - diff; 9843 } 9844 9845 rec->dtrd_offset = offs; 9846 9847 if (offs + rec->dtrd_size > ecb->dte_needed) { 9848 ecb->dte_needed = offs + rec->dtrd_size; 9849 9850 if (ecb->dte_needed > state->dts_needed) 9851 state->dts_needed = ecb->dte_needed; 9852 } 9853 9854 if (DTRACEACT_ISAGG(act->dta_kind)) { 9855 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9856 dtrace_action_t *first = agg->dtag_first, *prev; 9857 9858 ASSERT(rec->dtrd_size != 0 && first != NULL); 9859 ASSERT(wastuple); 9860 ASSERT(aggbase != UINT32_MAX); 9861 9862 agg->dtag_base = aggbase; 9863 9864 while ((prev = first->dta_prev) != NULL && 9865 DTRACEACT_ISAGG(prev->dta_kind)) { 9866 agg = (dtrace_aggregation_t *)prev; 9867 first = agg->dtag_first; 9868 } 9869 9870 if (prev != NULL) { 9871 offs = prev->dta_rec.dtrd_offset + 9872 prev->dta_rec.dtrd_size; 9873 } else { 9874 offs = sizeof (dtrace_epid_t); 9875 } 9876 wastuple = 0; 9877 } else { 9878 if (!act->dta_intuple) 9879 ecb->dte_size = offs + rec->dtrd_size; 9880 9881 offs += rec->dtrd_size; 9882 } 9883 9884 wastuple = act->dta_intuple; 9885 } 9886 9887 if ((act = ecb->dte_action) != NULL && 9888 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9889 ecb->dte_size == sizeof (dtrace_epid_t)) { 9890 /* 9891 * If the size is still sizeof (dtrace_epid_t), then all 9892 * actions store no data; set the size to 0. 9893 */ 9894 ecb->dte_alignment = maxalign; 9895 ecb->dte_size = 0; 9896 9897 /* 9898 * If the needed space is still sizeof (dtrace_epid_t), then 9899 * all actions need no additional space; set the needed 9900 * size to 0. 9901 */ 9902 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 9903 ecb->dte_needed = 0; 9904 9905 return; 9906 } 9907 9908 /* 9909 * Set our alignment, and make sure that the dte_size and dte_needed 9910 * are aligned to the size of an EPID. 9911 */ 9912 ecb->dte_alignment = maxalign; 9913 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 9914 ~(sizeof (dtrace_epid_t) - 1); 9915 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 9916 ~(sizeof (dtrace_epid_t) - 1); 9917 ASSERT(ecb->dte_size <= ecb->dte_needed); 9918} 9919 9920static dtrace_action_t * 9921dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9922{ 9923 dtrace_aggregation_t *agg; 9924 size_t size = sizeof (uint64_t); 9925 int ntuple = desc->dtad_ntuple; 9926 dtrace_action_t *act; 9927 dtrace_recdesc_t *frec; 9928 dtrace_aggid_t aggid; 9929 dtrace_state_t *state = ecb->dte_state; 9930 vmem_addr_t offset; 9931 9932 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9933 agg->dtag_ecb = ecb; 9934 9935 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9936 9937 switch (desc->dtad_kind) { 9938 case DTRACEAGG_MIN: 9939 agg->dtag_initial = INT64_MAX; 9940 agg->dtag_aggregate = dtrace_aggregate_min; 9941 break; 9942 9943 case DTRACEAGG_MAX: 9944 agg->dtag_initial = INT64_MIN; 9945 agg->dtag_aggregate = dtrace_aggregate_max; 9946 break; 9947 9948 case DTRACEAGG_COUNT: 9949 agg->dtag_aggregate = dtrace_aggregate_count; 9950 break; 9951 9952 case DTRACEAGG_QUANTIZE: 9953 agg->dtag_aggregate = dtrace_aggregate_quantize; 9954 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9955 sizeof (uint64_t); 9956 break; 9957 9958 case DTRACEAGG_LQUANTIZE: { 9959 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9960 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9961 9962 agg->dtag_initial = desc->dtad_arg; 9963 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9964 9965 if (step == 0 || levels == 0) 9966 goto err; 9967 9968 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9969 break; 9970 } 9971 9972 case DTRACEAGG_AVG: 9973 agg->dtag_aggregate = dtrace_aggregate_avg; 9974 size = sizeof (uint64_t) * 2; 9975 break; 9976 9977 case DTRACEAGG_STDDEV: 9978 agg->dtag_aggregate = dtrace_aggregate_stddev; 9979 size = sizeof (uint64_t) * 4; 9980 break; 9981 9982 case DTRACEAGG_SUM: 9983 agg->dtag_aggregate = dtrace_aggregate_sum; 9984 break; 9985 9986 default: 9987 goto err; 9988 } 9989 9990 agg->dtag_action.dta_rec.dtrd_size = size; 9991 9992 if (ntuple == 0) 9993 goto err; 9994 9995 /* 9996 * We must make sure that we have enough actions for the n-tuple. 9997 */ 9998 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9999 if (DTRACEACT_ISAGG(act->dta_kind)) 10000 break; 10001 10002 if (--ntuple == 0) { 10003 /* 10004 * This is the action with which our n-tuple begins. 10005 */ 10006 agg->dtag_first = act; 10007 goto success; 10008 } 10009 } 10010 10011 /* 10012 * This n-tuple is short by ntuple elements. Return failure. 10013 */ 10014 ASSERT(ntuple != 0); 10015err: 10016 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10017 return (NULL); 10018 10019success: 10020 /* 10021 * If the last action in the tuple has a size of zero, it's actually 10022 * an expression argument for the aggregating action. 10023 */ 10024 ASSERT(ecb->dte_action_last != NULL); 10025 act = ecb->dte_action_last; 10026 10027 if (act->dta_kind == DTRACEACT_DIFEXPR) { 10028 ASSERT(act->dta_difo != NULL); 10029 10030 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 10031 agg->dtag_hasarg = 1; 10032 } 10033 10034 /* 10035 * We need to allocate an id for this aggregation. 10036 */ 10037 if (vmem_alloc(state->dts_aggid_arena, 1, VM_BESTFIT | VM_SLEEP, 10038 &offset) != 0) 10039 ASSERT(0); 10040 aggid = (dtrace_aggid_t)(uintptr_t)offset; 10041 10042 10043 if (aggid - 1 >= state->dts_naggregations) { 10044 dtrace_aggregation_t **oaggs = state->dts_aggregations; 10045 dtrace_aggregation_t **aggs; 10046 int naggs = state->dts_naggregations << 1; 10047 int onaggs = state->dts_naggregations; 10048 10049 ASSERT(aggid == state->dts_naggregations + 1); 10050 10051 if (naggs == 0) { 10052 ASSERT(oaggs == NULL); 10053 naggs = 1; 10054 } 10055 10056 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 10057 10058 if (oaggs != NULL) { 10059 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 10060 kmem_free(oaggs, onaggs * sizeof (*aggs)); 10061 } 10062 10063 state->dts_aggregations = aggs; 10064 state->dts_naggregations = naggs; 10065 } 10066 10067 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 10068 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 10069 10070 frec = &agg->dtag_first->dta_rec; 10071 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 10072 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 10073 10074 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 10075 ASSERT(!act->dta_intuple); 10076 act->dta_intuple = 1; 10077 } 10078 10079 return (&agg->dtag_action); 10080} 10081 10082static void 10083dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 10084{ 10085 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 10086 dtrace_state_t *state = ecb->dte_state; 10087 dtrace_aggid_t aggid = agg->dtag_id; 10088 10089 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 10090#if defined(sun) 10091 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 10092#else 10093 vmem_free(state->dts_aggid_arena, (uintptr_t)aggid, 1); 10094#endif 10095 10096 ASSERT(state->dts_aggregations[aggid - 1] == agg); 10097 state->dts_aggregations[aggid - 1] = NULL; 10098 10099 kmem_free(agg, sizeof (dtrace_aggregation_t)); 10100} 10101 10102static int 10103dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 10104{ 10105 dtrace_action_t *action, *last; 10106 dtrace_difo_t *dp = desc->dtad_difo; 10107 uint32_t size = 0, align = sizeof (uint8_t), mask; 10108 uint16_t format = 0; 10109 dtrace_recdesc_t *rec; 10110 dtrace_state_t *state = ecb->dte_state; 10111 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 10112 uint64_t arg = desc->dtad_arg; 10113 10114 ASSERT(MUTEX_HELD(&dtrace_lock)); 10115 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 10116 10117 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 10118 /* 10119 * If this is an aggregating action, there must be neither 10120 * a speculate nor a commit on the action chain. 10121 */ 10122 dtrace_action_t *act; 10123 10124 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 10125 if (act->dta_kind == DTRACEACT_COMMIT) 10126 return (EINVAL); 10127 10128 if (act->dta_kind == DTRACEACT_SPECULATE) 10129 return (EINVAL); 10130 } 10131 10132 action = dtrace_ecb_aggregation_create(ecb, desc); 10133 10134 if (action == NULL) 10135 return (EINVAL); 10136 } else { 10137 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 10138 (desc->dtad_kind == DTRACEACT_DIFEXPR && 10139 dp != NULL && dp->dtdo_destructive)) { 10140 state->dts_destructive = 1; 10141 } 10142 10143 switch (desc->dtad_kind) { 10144 case DTRACEACT_PRINTF: 10145 case DTRACEACT_PRINTA: 10146 case DTRACEACT_SYSTEM: 10147 case DTRACEACT_FREOPEN: 10148 /* 10149 * We know that our arg is a string -- turn it into a 10150 * format. 10151 */ 10152 if (arg == 0) { 10153 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 10154 format = 0; 10155 } else { 10156 ASSERT(arg != 0); 10157#if defined(sun) 10158 ASSERT(arg > KERNELBASE); 10159#endif 10160 format = dtrace_format_add(state, 10161 (char *)(uintptr_t)arg); 10162 } 10163 10164 /*FALLTHROUGH*/ 10165 case DTRACEACT_LIBACT: 10166 case DTRACEACT_DIFEXPR: 10167 if (dp == NULL) 10168 return (EINVAL); 10169 10170 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 10171 break; 10172 10173 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 10174 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10175 return (EINVAL); 10176 10177 size = opt[DTRACEOPT_STRSIZE]; 10178 } 10179 10180 break; 10181 10182 case DTRACEACT_STACK: 10183 if ((nframes = arg) == 0) { 10184 nframes = opt[DTRACEOPT_STACKFRAMES]; 10185 ASSERT(nframes > 0); 10186 arg = nframes; 10187 } 10188 10189 size = nframes * sizeof (pc_t); 10190 break; 10191 10192 case DTRACEACT_JSTACK: 10193 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10194 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10195 10196 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10197 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10198 10199 arg = DTRACE_USTACK_ARG(nframes, strsize); 10200 10201 /*FALLTHROUGH*/ 10202 case DTRACEACT_USTACK: 10203 if (desc->dtad_kind != DTRACEACT_JSTACK && 10204 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10205 strsize = DTRACE_USTACK_STRSIZE(arg); 10206 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10207 ASSERT(nframes > 0); 10208 arg = DTRACE_USTACK_ARG(nframes, strsize); 10209 } 10210 10211 /* 10212 * Save a slot for the pid. 10213 */ 10214 size = (nframes + 1) * sizeof (uint64_t); 10215 size += DTRACE_USTACK_STRSIZE(arg); 10216 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10217 10218 break; 10219 10220 case DTRACEACT_SYM: 10221 case DTRACEACT_MOD: 10222 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10223 sizeof (uint64_t)) || 10224 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10225 return (EINVAL); 10226 break; 10227 10228 case DTRACEACT_USYM: 10229 case DTRACEACT_UMOD: 10230 case DTRACEACT_UADDR: 10231 if (dp == NULL || 10232 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10233 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10234 return (EINVAL); 10235 10236 /* 10237 * We have a slot for the pid, plus a slot for the 10238 * argument. To keep things simple (aligned with 10239 * bitness-neutral sizing), we store each as a 64-bit 10240 * quantity. 10241 */ 10242 size = 2 * sizeof (uint64_t); 10243 break; 10244 10245 case DTRACEACT_STOP: 10246 case DTRACEACT_BREAKPOINT: 10247 case DTRACEACT_PANIC: 10248 break; 10249 10250 case DTRACEACT_CHILL: 10251 case DTRACEACT_DISCARD: 10252 case DTRACEACT_RAISE: 10253 if (dp == NULL) 10254 return (EINVAL); 10255 break; 10256 10257 case DTRACEACT_EXIT: 10258 if (dp == NULL || 10259 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10260 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10261 return (EINVAL); 10262 break; 10263 10264 case DTRACEACT_SPECULATE: 10265 if (ecb->dte_size > sizeof (dtrace_epid_t)) 10266 return (EINVAL); 10267 10268 if (dp == NULL) 10269 return (EINVAL); 10270 10271 state->dts_speculates = 1; 10272 break; 10273 10274 case DTRACEACT_PRINTM: 10275 size = dp->dtdo_rtype.dtdt_size; 10276 break; 10277 10278 case DTRACEACT_PRINTT: 10279 size = dp->dtdo_rtype.dtdt_size; 10280 break; 10281 10282 case DTRACEACT_COMMIT: { 10283 dtrace_action_t *act = ecb->dte_action; 10284 10285 for (; act != NULL; act = act->dta_next) { 10286 if (act->dta_kind == DTRACEACT_COMMIT) 10287 return (EINVAL); 10288 } 10289 10290 if (dp == NULL) 10291 return (EINVAL); 10292 break; 10293 } 10294 10295 default: 10296 return (EINVAL); 10297 } 10298 10299 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10300 /* 10301 * If this is a data-storing action or a speculate, 10302 * we must be sure that there isn't a commit on the 10303 * action chain. 10304 */ 10305 dtrace_action_t *act = ecb->dte_action; 10306 10307 for (; act != NULL; act = act->dta_next) { 10308 if (act->dta_kind == DTRACEACT_COMMIT) 10309 return (EINVAL); 10310 } 10311 } 10312 10313 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10314 action->dta_rec.dtrd_size = size; 10315 } 10316 10317 action->dta_refcnt = 1; 10318 rec = &action->dta_rec; 10319 size = rec->dtrd_size; 10320 10321 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10322 if (!(size & mask)) { 10323 align = mask + 1; 10324 break; 10325 } 10326 } 10327 10328 action->dta_kind = desc->dtad_kind; 10329 10330 if ((action->dta_difo = dp) != NULL) 10331 dtrace_difo_hold(dp); 10332 10333 rec->dtrd_action = action->dta_kind; 10334 rec->dtrd_arg = arg; 10335 rec->dtrd_uarg = desc->dtad_uarg; 10336 rec->dtrd_alignment = (uint16_t)align; 10337 rec->dtrd_format = format; 10338 10339 if ((last = ecb->dte_action_last) != NULL) { 10340 ASSERT(ecb->dte_action != NULL); 10341 action->dta_prev = last; 10342 last->dta_next = action; 10343 } else { 10344 ASSERT(ecb->dte_action == NULL); 10345 ecb->dte_action = action; 10346 } 10347 10348 ecb->dte_action_last = action; 10349 10350 return (0); 10351} 10352 10353static void 10354dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10355{ 10356 dtrace_action_t *act = ecb->dte_action, *next; 10357 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10358 dtrace_difo_t *dp; 10359 uint16_t format; 10360 10361 if (act != NULL && act->dta_refcnt > 1) { 10362 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10363 act->dta_refcnt--; 10364 } else { 10365 for (; act != NULL; act = next) { 10366 next = act->dta_next; 10367 ASSERT(next != NULL || act == ecb->dte_action_last); 10368 ASSERT(act->dta_refcnt == 1); 10369 10370 if ((format = act->dta_rec.dtrd_format) != 0) 10371 dtrace_format_remove(ecb->dte_state, format); 10372 10373 if ((dp = act->dta_difo) != NULL) 10374 dtrace_difo_release(dp, vstate); 10375 10376 if (DTRACEACT_ISAGG(act->dta_kind)) { 10377 dtrace_ecb_aggregation_destroy(ecb, act); 10378 } else { 10379 kmem_free(act, sizeof (dtrace_action_t)); 10380 } 10381 } 10382 } 10383 10384 ecb->dte_action = NULL; 10385 ecb->dte_action_last = NULL; 10386 ecb->dte_size = sizeof (dtrace_epid_t); 10387} 10388 10389static void 10390dtrace_ecb_disable(dtrace_ecb_t *ecb) 10391{ 10392 /* 10393 * We disable the ECB by removing it from its probe. 10394 */ 10395 dtrace_ecb_t *pecb, *prev = NULL; 10396 dtrace_probe_t *probe = ecb->dte_probe; 10397 10398 ASSERT(MUTEX_HELD(&dtrace_lock)); 10399 10400 if (probe == NULL) { 10401 /* 10402 * This is the NULL probe; there is nothing to disable. 10403 */ 10404 return; 10405 } 10406 10407 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10408 if (pecb == ecb) 10409 break; 10410 prev = pecb; 10411 } 10412 10413 ASSERT(pecb != NULL); 10414 10415 if (prev == NULL) { 10416 probe->dtpr_ecb = ecb->dte_next; 10417 } else { 10418 prev->dte_next = ecb->dte_next; 10419 } 10420 10421 if (ecb == probe->dtpr_ecb_last) { 10422 ASSERT(ecb->dte_next == NULL); 10423 probe->dtpr_ecb_last = prev; 10424 } 10425 10426 /* 10427 * The ECB has been disconnected from the probe; now sync to assure 10428 * that all CPUs have seen the change before returning. 10429 */ 10430 dtrace_sync(); 10431 10432 if (probe->dtpr_ecb == NULL) { 10433 /* 10434 * That was the last ECB on the probe; clear the predicate 10435 * cache ID for the probe, disable it and sync one more time 10436 * to assure that we'll never hit it again. 10437 */ 10438 dtrace_provider_t *prov = probe->dtpr_provider; 10439 10440 ASSERT(ecb->dte_next == NULL); 10441 ASSERT(probe->dtpr_ecb_last == NULL); 10442 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10443 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10444 probe->dtpr_id, probe->dtpr_arg); 10445 dtrace_sync(); 10446 } else { 10447 /* 10448 * There is at least one ECB remaining on the probe. If there 10449 * is _exactly_ one, set the probe's predicate cache ID to be 10450 * the predicate cache ID of the remaining ECB. 10451 */ 10452 ASSERT(probe->dtpr_ecb_last != NULL); 10453 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10454 10455 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10456 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10457 10458 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10459 10460 if (p != NULL) 10461 probe->dtpr_predcache = p->dtp_cacheid; 10462 } 10463 10464 ecb->dte_next = NULL; 10465 } 10466} 10467 10468static void 10469dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10470{ 10471 dtrace_state_t *state = ecb->dte_state; 10472 dtrace_vstate_t *vstate = &state->dts_vstate; 10473 dtrace_predicate_t *pred; 10474 dtrace_epid_t epid = ecb->dte_epid; 10475 10476 ASSERT(MUTEX_HELD(&dtrace_lock)); 10477 ASSERT(ecb->dte_next == NULL); 10478 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10479 10480 if ((pred = ecb->dte_predicate) != NULL) 10481 dtrace_predicate_release(pred, vstate); 10482 10483 dtrace_ecb_action_remove(ecb); 10484 10485 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10486 state->dts_ecbs[epid - 1] = NULL; 10487 10488 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10489} 10490 10491static dtrace_ecb_t * 10492dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10493 dtrace_enabling_t *enab) 10494{ 10495 dtrace_ecb_t *ecb; 10496 dtrace_predicate_t *pred; 10497 dtrace_actdesc_t *act; 10498 dtrace_provider_t *prov; 10499 dtrace_ecbdesc_t *desc = enab->dten_current; 10500 10501 ASSERT(MUTEX_HELD(&dtrace_lock)); 10502 ASSERT(state != NULL); 10503 10504 ecb = dtrace_ecb_add(state, probe); 10505 ecb->dte_uarg = desc->dted_uarg; 10506 10507 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10508 dtrace_predicate_hold(pred); 10509 ecb->dte_predicate = pred; 10510 } 10511 10512 if (probe != NULL) { 10513 /* 10514 * If the provider shows more leg than the consumer is old 10515 * enough to see, we need to enable the appropriate implicit 10516 * predicate bits to prevent the ecb from activating at 10517 * revealing times. 10518 * 10519 * Providers specifying DTRACE_PRIV_USER at register time 10520 * are stating that they need the /proc-style privilege 10521 * model to be enforced, and this is what DTRACE_COND_OWNER 10522 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10523 */ 10524 prov = probe->dtpr_provider; 10525 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10526 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10527 ecb->dte_cond |= DTRACE_COND_OWNER; 10528 10529 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10530 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10531 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10532 10533 /* 10534 * If the provider shows us kernel innards and the user 10535 * is lacking sufficient privilege, enable the 10536 * DTRACE_COND_USERMODE implicit predicate. 10537 */ 10538 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10539 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10540 ecb->dte_cond |= DTRACE_COND_USERMODE; 10541 } 10542 10543 if (dtrace_ecb_create_cache != NULL) { 10544 /* 10545 * If we have a cached ecb, we'll use its action list instead 10546 * of creating our own (saving both time and space). 10547 */ 10548 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10549 dtrace_action_t *xact = cached->dte_action; 10550 10551 if (xact != NULL) { 10552 ASSERT(xact->dta_refcnt > 0); 10553 xact->dta_refcnt++; 10554 ecb->dte_action = xact; 10555 ecb->dte_action_last = cached->dte_action_last; 10556 ecb->dte_needed = cached->dte_needed; 10557 ecb->dte_size = cached->dte_size; 10558 ecb->dte_alignment = cached->dte_alignment; 10559 } 10560 10561 return (ecb); 10562 } 10563 10564 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10565 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10566 dtrace_ecb_destroy(ecb); 10567 return (NULL); 10568 } 10569 } 10570 10571 dtrace_ecb_resize(ecb); 10572 10573 return (dtrace_ecb_create_cache = ecb); 10574} 10575 10576static int 10577dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10578{ 10579 dtrace_ecb_t *ecb; 10580 dtrace_enabling_t *enab = arg; 10581 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10582 10583 ASSERT(state != NULL); 10584 10585 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10586 /* 10587 * This probe was created in a generation for which this 10588 * enabling has previously created ECBs; we don't want to 10589 * enable it again, so just kick out. 10590 */ 10591 return (DTRACE_MATCH_NEXT); 10592 } 10593 10594 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10595 return (DTRACE_MATCH_DONE); 10596 10597 if (dtrace_ecb_enable(ecb) < 0) 10598 return (DTRACE_MATCH_FAIL); 10599 10600 return (DTRACE_MATCH_NEXT); 10601} 10602 10603static dtrace_ecb_t * 10604dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10605{ 10606 dtrace_ecb_t *ecb; 10607 10608 ASSERT(MUTEX_HELD(&dtrace_lock)); 10609 10610 if (id == 0 || id > state->dts_necbs) 10611 return (NULL); 10612 10613 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10614 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10615 10616 return (state->dts_ecbs[id - 1]); 10617} 10618 10619static dtrace_aggregation_t * 10620dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10621{ 10622 dtrace_aggregation_t *agg; 10623 10624 ASSERT(MUTEX_HELD(&dtrace_lock)); 10625 10626 if (id == 0 || id > state->dts_naggregations) 10627 return (NULL); 10628 10629 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10630 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10631 agg->dtag_id == id); 10632 10633 return (state->dts_aggregations[id - 1]); 10634} 10635 10636/* 10637 * DTrace Buffer Functions 10638 * 10639 * The following functions manipulate DTrace buffers. Most of these functions 10640 * are called in the context of establishing or processing consumer state; 10641 * exceptions are explicitly noted. 10642 */ 10643 10644/* 10645 * Note: called from cross call context. This function switches the two 10646 * buffers on a given CPU. The atomicity of this operation is assured by 10647 * disabling interrupts while the actual switch takes place; the disabling of 10648 * interrupts serializes the execution with any execution of dtrace_probe() on 10649 * the same CPU. 10650 */ 10651static void 10652dtrace_buffer_switch(dtrace_buffer_t *buf) 10653{ 10654 caddr_t tomax = buf->dtb_tomax; 10655 caddr_t xamot = buf->dtb_xamot; 10656 dtrace_icookie_t cookie; 10657 10658 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10659 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10660 10661 cookie = dtrace_interrupt_disable(); 10662 buf->dtb_tomax = xamot; 10663 buf->dtb_xamot = tomax; 10664 buf->dtb_xamot_drops = buf->dtb_drops; 10665 buf->dtb_xamot_offset = buf->dtb_offset; 10666 buf->dtb_xamot_errors = buf->dtb_errors; 10667 buf->dtb_xamot_flags = buf->dtb_flags; 10668 buf->dtb_offset = 0; 10669 buf->dtb_drops = 0; 10670 buf->dtb_errors = 0; 10671 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10672 dtrace_interrupt_enable(cookie); 10673} 10674 10675/* 10676 * Note: called from cross call context. This function activates a buffer 10677 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10678 * is guaranteed by the disabling of interrupts. 10679 */ 10680static void 10681dtrace_buffer_activate(dtrace_state_t *state) 10682{ 10683 dtrace_buffer_t *buf; 10684 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10685 10686 buf = &state->dts_buffer[curcpu_id]; 10687 10688 if (buf->dtb_tomax != NULL) { 10689 /* 10690 * We might like to assert that the buffer is marked inactive, 10691 * but this isn't necessarily true: the buffer for the CPU 10692 * that processes the BEGIN probe has its buffer activated 10693 * manually. In this case, we take the (harmless) action 10694 * re-clearing the bit INACTIVE bit. 10695 */ 10696 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10697 } 10698 10699 dtrace_interrupt_enable(cookie); 10700} 10701 10702static int 10703dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10704 processorid_t xcpu) 10705{ 10706#if defined(sun) 10707 cpu_t *cp; 10708#else 10709 CPU_INFO_ITERATOR cpuind; 10710 struct cpu_info *cinfo; 10711#endif 10712 dtrace_buffer_t *buf; 10713 10714#if defined(sun) 10715 ASSERT(MUTEX_HELD(&cpu_lock)); 10716 ASSERT(MUTEX_HELD(&dtrace_lock)); 10717 10718 if (size > dtrace_nonroot_maxsize && 10719 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10720 return (EFBIG); 10721 10722 cp = cpu_list; 10723 10724 do { 10725 if (xcpu != DTRACE_CPUALL && xcpu != cp->cpu_id) 10726 continue; 10727 10728 buf = &bufs[cp->cpu_id]; 10729 10730 /* 10731 * If there is already a buffer allocated for this CPU, it 10732 * is only possible that this is a DR event. In this case, 10733 */ 10734 if (buf->dtb_tomax != NULL) { 10735 ASSERT(buf->dtb_size == size); 10736 continue; 10737 } 10738 10739 ASSERT(buf->dtb_xamot == NULL); 10740 10741 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10742 goto err; 10743 10744 buf->dtb_size = size; 10745 buf->dtb_flags = flags; 10746 buf->dtb_offset = 0; 10747 buf->dtb_drops = 0; 10748 10749 if (flags & DTRACEBUF_NOSWITCH) 10750 continue; 10751 10752 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10753 goto err; 10754 } while ((cp = cp->cpu_next) != cpu_list); 10755 10756 return (0); 10757 10758err: 10759 cp = cpu_list; 10760 10761 do { 10762 if (xcpu != DTRACE_CPUALL && xcpu != cp->cpu_id) 10763 continue; 10764 10765 buf = &bufs[cp->cpu_id]; 10766 10767 if (buf->dtb_xamot != NULL) { 10768 ASSERT(buf->dtb_tomax != NULL); 10769 ASSERT(buf->dtb_size == size); 10770 kmem_free(buf->dtb_xamot, size); 10771 } 10772 10773 if (buf->dtb_tomax != NULL) { 10774 ASSERT(buf->dtb_size == size); 10775 kmem_free(buf->dtb_tomax, size); 10776 } 10777 10778 buf->dtb_tomax = NULL; 10779 buf->dtb_xamot = NULL; 10780 buf->dtb_size = 0; 10781 } while ((cp = cp->cpu_next) != cpu_list); 10782 10783 return (ENOMEM); 10784#else 10785 10786#if defined(__amd64__) 10787 /* 10788 * FreeBSD isn't good at limiting the amount of memory we 10789 * ask to malloc, so let's place a limit here before trying 10790 * to do something that might well end in tears at bedtime. 10791 */ 10792 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 10793 return(ENOMEM); 10794#endif 10795 10796 ASSERT(MUTEX_HELD(&dtrace_lock)); 10797 for (CPU_INFO_FOREACH(cpuind, cinfo)) { 10798 if (xcpu != DTRACE_CPUALL && xcpu != cpu_index(cinfo)) 10799 continue; 10800 10801 buf = &bufs[cpu_index(cinfo)]; 10802 10803 /* 10804 * If there is already a buffer allocated for this CPU, it 10805 * is only possible that this is a DR event. In this case, 10806 * the buffer size must match our specified size. 10807 */ 10808 if (buf->dtb_tomax != NULL) { 10809 ASSERT(buf->dtb_size == size); 10810 continue; 10811 } 10812 10813 ASSERT(buf->dtb_xamot == NULL); 10814 10815 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10816 goto err; 10817 10818 buf->dtb_size = size; 10819 buf->dtb_flags = flags; 10820 buf->dtb_offset = 0; 10821 buf->dtb_drops = 0; 10822 10823 if (flags & DTRACEBUF_NOSWITCH) 10824 continue; 10825 10826 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10827 goto err; 10828 } 10829 10830 return (0); 10831 10832err: 10833 /* 10834 * Error allocating memory, so free the buffers that were 10835 * allocated before the failed allocation. 10836 */ 10837 for (CPU_INFO_FOREACH(cpuind, cinfo)) { 10838 if (xcpu != DTRACE_CPUALL && xcpu != cpu_index(cinfo)) 10839 continue; 10840 10841 buf = &bufs[cpu_index(cinfo)]; 10842 10843 if (buf->dtb_xamot != NULL) { 10844 ASSERT(buf->dtb_tomax != NULL); 10845 ASSERT(buf->dtb_size == size); 10846 kmem_free(buf->dtb_xamot, size); 10847 } 10848 10849 if (buf->dtb_tomax != NULL) { 10850 ASSERT(buf->dtb_size == size); 10851 kmem_free(buf->dtb_tomax, size); 10852 } 10853 10854 buf->dtb_tomax = NULL; 10855 buf->dtb_xamot = NULL; 10856 buf->dtb_size = 0; 10857 10858 } 10859 10860 return (ENOMEM); 10861#endif 10862} 10863 10864/* 10865 * Note: called from probe context. This function just increments the drop 10866 * count on a buffer. It has been made a function to allow for the 10867 * possibility of understanding the source of mysterious drop counts. (A 10868 * problem for which one may be particularly disappointed that DTrace cannot 10869 * be used to understand DTrace.) 10870 */ 10871static void 10872dtrace_buffer_drop(dtrace_buffer_t *buf) 10873{ 10874 buf->dtb_drops++; 10875} 10876 10877/* 10878 * Note: called from probe context. This function is called to reserve space 10879 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10880 * mstate. Returns the new offset in the buffer, or a negative value if an 10881 * error has occurred. 10882 */ 10883static intptr_t 10884dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10885 dtrace_state_t *state, dtrace_mstate_t *mstate) 10886{ 10887 intptr_t offs = buf->dtb_offset, soffs; 10888 intptr_t woffs; 10889 caddr_t tomax; 10890 size_t total; 10891 10892 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10893 return (-1); 10894 10895 if ((tomax = buf->dtb_tomax) == NULL) { 10896 dtrace_buffer_drop(buf); 10897 return (-1); 10898 } 10899 10900 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10901 while (offs & (align - 1)) { 10902 /* 10903 * Assert that our alignment is off by a number which 10904 * is itself sizeof (uint32_t) aligned. 10905 */ 10906 ASSERT(!((align - (offs & (align - 1))) & 10907 (sizeof (uint32_t) - 1))); 10908 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10909 offs += sizeof (uint32_t); 10910 } 10911 10912 if ((soffs = offs + needed) > buf->dtb_size) { 10913 dtrace_buffer_drop(buf); 10914 return (-1); 10915 } 10916 10917 if (mstate == NULL) 10918 return (offs); 10919 10920 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10921 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10922 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10923 10924 return (offs); 10925 } 10926 10927 if (buf->dtb_flags & DTRACEBUF_FILL) { 10928 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10929 (buf->dtb_flags & DTRACEBUF_FULL)) 10930 return (-1); 10931 goto out; 10932 } 10933 10934 total = needed + (offs & (align - 1)); 10935 10936 /* 10937 * For a ring buffer, life is quite a bit more complicated. Before 10938 * we can store any padding, we need to adjust our wrapping offset. 10939 * (If we've never before wrapped or we're not about to, no adjustment 10940 * is required.) 10941 */ 10942 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10943 offs + total > buf->dtb_size) { 10944 woffs = buf->dtb_xamot_offset; 10945 10946 if (offs + total > buf->dtb_size) { 10947 /* 10948 * We can't fit in the end of the buffer. First, a 10949 * sanity check that we can fit in the buffer at all. 10950 */ 10951 if (total > buf->dtb_size) { 10952 dtrace_buffer_drop(buf); 10953 return (-1); 10954 } 10955 10956 /* 10957 * We're going to be storing at the top of the buffer, 10958 * so now we need to deal with the wrapped offset. We 10959 * only reset our wrapped offset to 0 if it is 10960 * currently greater than the current offset. If it 10961 * is less than the current offset, it is because a 10962 * previous allocation induced a wrap -- but the 10963 * allocation didn't subsequently take the space due 10964 * to an error or false predicate evaluation. In this 10965 * case, we'll just leave the wrapped offset alone: if 10966 * the wrapped offset hasn't been advanced far enough 10967 * for this allocation, it will be adjusted in the 10968 * lower loop. 10969 */ 10970 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10971 if (woffs >= offs) 10972 woffs = 0; 10973 } else { 10974 woffs = 0; 10975 } 10976 10977 /* 10978 * Now we know that we're going to be storing to the 10979 * top of the buffer and that there is room for us 10980 * there. We need to clear the buffer from the current 10981 * offset to the end (there may be old gunk there). 10982 */ 10983 while (offs < buf->dtb_size) 10984 tomax[offs++] = 0; 10985 10986 /* 10987 * We need to set our offset to zero. And because we 10988 * are wrapping, we need to set the bit indicating as 10989 * much. We can also adjust our needed space back 10990 * down to the space required by the ECB -- we know 10991 * that the top of the buffer is aligned. 10992 */ 10993 offs = 0; 10994 total = needed; 10995 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10996 } else { 10997 /* 10998 * There is room for us in the buffer, so we simply 10999 * need to check the wrapped offset. 11000 */ 11001 if (woffs < offs) { 11002 /* 11003 * The wrapped offset is less than the offset. 11004 * This can happen if we allocated buffer space 11005 * that induced a wrap, but then we didn't 11006 * subsequently take the space due to an error 11007 * or false predicate evaluation. This is 11008 * okay; we know that _this_ allocation isn't 11009 * going to induce a wrap. We still can't 11010 * reset the wrapped offset to be zero, 11011 * however: the space may have been trashed in 11012 * the previous failed probe attempt. But at 11013 * least the wrapped offset doesn't need to 11014 * be adjusted at all... 11015 */ 11016 goto out; 11017 } 11018 } 11019 11020 while (offs + total > woffs) { 11021 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 11022 size_t size; 11023 11024 if (epid == DTRACE_EPIDNONE) { 11025 size = sizeof (uint32_t); 11026 } else { 11027 ASSERT(epid <= state->dts_necbs); 11028 ASSERT(state->dts_ecbs[epid - 1] != NULL); 11029 11030 size = state->dts_ecbs[epid - 1]->dte_size; 11031 } 11032 11033 ASSERT(woffs + size <= buf->dtb_size); 11034 ASSERT(size != 0); 11035 11036 if (woffs + size == buf->dtb_size) { 11037 /* 11038 * We've reached the end of the buffer; we want 11039 * to set the wrapped offset to 0 and break 11040 * out. However, if the offs is 0, then we're 11041 * in a strange edge-condition: the amount of 11042 * space that we want to reserve plus the size 11043 * of the record that we're overwriting is 11044 * greater than the size of the buffer. This 11045 * is problematic because if we reserve the 11046 * space but subsequently don't consume it (due 11047 * to a failed predicate or error) the wrapped 11048 * offset will be 0 -- yet the EPID at offset 0 11049 * will not be committed. This situation is 11050 * relatively easy to deal with: if we're in 11051 * this case, the buffer is indistinguishable 11052 * from one that hasn't wrapped; we need only 11053 * finish the job by clearing the wrapped bit, 11054 * explicitly setting the offset to be 0, and 11055 * zero'ing out the old data in the buffer. 11056 */ 11057 if (offs == 0) { 11058 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 11059 buf->dtb_offset = 0; 11060 woffs = total; 11061 11062 while (woffs < buf->dtb_size) 11063 tomax[woffs++] = 0; 11064 } 11065 11066 woffs = 0; 11067 break; 11068 } 11069 11070 woffs += size; 11071 } 11072 11073 /* 11074 * We have a wrapped offset. It may be that the wrapped offset 11075 * has become zero -- that's okay. 11076 */ 11077 buf->dtb_xamot_offset = woffs; 11078 } 11079 11080out: 11081 /* 11082 * Now we can plow the buffer with any necessary padding. 11083 */ 11084 while (offs & (align - 1)) { 11085 /* 11086 * Assert that our alignment is off by a number which 11087 * is itself sizeof (uint32_t) aligned. 11088 */ 11089 ASSERT(!((align - (offs & (align - 1))) & 11090 (sizeof (uint32_t) - 1))); 11091 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 11092 offs += sizeof (uint32_t); 11093 } 11094 11095 if (buf->dtb_flags & DTRACEBUF_FILL) { 11096 if (offs + needed > buf->dtb_size - state->dts_reserve) { 11097 buf->dtb_flags |= DTRACEBUF_FULL; 11098 return (-1); 11099 } 11100 } 11101 11102 if (mstate == NULL) 11103 return (offs); 11104 11105 /* 11106 * For ring buffers and fill buffers, the scratch space is always 11107 * the inactive buffer. 11108 */ 11109 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 11110 mstate->dtms_scratch_size = buf->dtb_size; 11111 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 11112 11113 return (offs); 11114} 11115 11116static void 11117dtrace_buffer_polish(dtrace_buffer_t *buf) 11118{ 11119 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 11120 ASSERT(MUTEX_HELD(&dtrace_lock)); 11121 11122 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 11123 return; 11124 11125 /* 11126 * We need to polish the ring buffer. There are three cases: 11127 * 11128 * - The first (and presumably most common) is that there is no gap 11129 * between the buffer offset and the wrapped offset. In this case, 11130 * there is nothing in the buffer that isn't valid data; we can 11131 * mark the buffer as polished and return. 11132 * 11133 * - The second (less common than the first but still more common 11134 * than the third) is that there is a gap between the buffer offset 11135 * and the wrapped offset, and the wrapped offset is larger than the 11136 * buffer offset. This can happen because of an alignment issue, or 11137 * can happen because of a call to dtrace_buffer_reserve() that 11138 * didn't subsequently consume the buffer space. In this case, 11139 * we need to zero the data from the buffer offset to the wrapped 11140 * offset. 11141 * 11142 * - The third (and least common) is that there is a gap between the 11143 * buffer offset and the wrapped offset, but the wrapped offset is 11144 * _less_ than the buffer offset. This can only happen because a 11145 * call to dtrace_buffer_reserve() induced a wrap, but the space 11146 * was not subsequently consumed. In this case, we need to zero the 11147 * space from the offset to the end of the buffer _and_ from the 11148 * top of the buffer to the wrapped offset. 11149 */ 11150 if (buf->dtb_offset < buf->dtb_xamot_offset) { 11151 bzero(buf->dtb_tomax + buf->dtb_offset, 11152 buf->dtb_xamot_offset - buf->dtb_offset); 11153 } 11154 11155 if (buf->dtb_offset > buf->dtb_xamot_offset) { 11156 bzero(buf->dtb_tomax + buf->dtb_offset, 11157 buf->dtb_size - buf->dtb_offset); 11158 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 11159 } 11160} 11161 11162static void 11163dtrace_buffer_free(dtrace_buffer_t *bufs) 11164{ 11165 int i; 11166 11167 for (i = 0; i < NCPU; i++) { 11168 dtrace_buffer_t *buf = &bufs[i]; 11169 11170 if (buf->dtb_tomax == NULL) { 11171 ASSERT(buf->dtb_xamot == NULL); 11172 ASSERT(buf->dtb_size == 0); 11173 continue; 11174 } 11175 11176 if (buf->dtb_xamot != NULL) { 11177 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11178 kmem_free(buf->dtb_xamot, buf->dtb_size); 11179 } 11180 11181 kmem_free(buf->dtb_tomax, buf->dtb_size); 11182 buf->dtb_size = 0; 11183 buf->dtb_tomax = NULL; 11184 buf->dtb_xamot = NULL; 11185 } 11186} 11187 11188/* 11189 * DTrace Enabling Functions 11190 */ 11191static dtrace_enabling_t * 11192dtrace_enabling_create(dtrace_vstate_t *vstate) 11193{ 11194 dtrace_enabling_t *enab; 11195 11196 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 11197 enab->dten_vstate = vstate; 11198 11199 return (enab); 11200} 11201 11202static void 11203dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 11204{ 11205 dtrace_ecbdesc_t **ndesc; 11206 size_t osize, nsize; 11207 11208 /* 11209 * We can't add to enablings after we've enabled them, or after we've 11210 * retained them. 11211 */ 11212 ASSERT(enab->dten_probegen == 0); 11213 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11214 11215 if (enab->dten_ndesc < enab->dten_maxdesc) { 11216 enab->dten_desc[enab->dten_ndesc++] = ecb; 11217 return; 11218 } 11219 11220 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11221 11222 if (enab->dten_maxdesc == 0) { 11223 enab->dten_maxdesc = 1; 11224 } else { 11225 enab->dten_maxdesc <<= 1; 11226 } 11227 11228 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11229 11230 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11231 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11232 bcopy(enab->dten_desc, ndesc, osize); 11233 if (enab->dten_desc != NULL) 11234 kmem_free(enab->dten_desc, osize); 11235 11236 enab->dten_desc = ndesc; 11237 enab->dten_desc[enab->dten_ndesc++] = ecb; 11238} 11239 11240static void 11241dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11242 dtrace_probedesc_t *pd) 11243{ 11244 dtrace_ecbdesc_t *new; 11245 dtrace_predicate_t *pred; 11246 dtrace_actdesc_t *act; 11247 11248 /* 11249 * We're going to create a new ECB description that matches the 11250 * specified ECB in every way, but has the specified probe description. 11251 */ 11252 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11253 11254 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11255 dtrace_predicate_hold(pred); 11256 11257 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11258 dtrace_actdesc_hold(act); 11259 11260 new->dted_action = ecb->dted_action; 11261 new->dted_pred = ecb->dted_pred; 11262 new->dted_probe = *pd; 11263 new->dted_uarg = ecb->dted_uarg; 11264 11265 dtrace_enabling_add(enab, new); 11266} 11267 11268static void 11269dtrace_enabling_dump(dtrace_enabling_t *enab) 11270{ 11271 int i; 11272 11273 for (i = 0; i < enab->dten_ndesc; i++) { 11274 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11275 11276 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11277 desc->dtpd_provider, desc->dtpd_mod, 11278 desc->dtpd_func, desc->dtpd_name); 11279 } 11280} 11281 11282static void 11283dtrace_enabling_destroy(dtrace_enabling_t *enab) 11284{ 11285 int i; 11286 dtrace_ecbdesc_t *ep; 11287 dtrace_vstate_t *vstate = enab->dten_vstate; 11288 11289 ASSERT(MUTEX_HELD(&dtrace_lock)); 11290 11291 for (i = 0; i < enab->dten_ndesc; i++) { 11292 dtrace_actdesc_t *act, *next; 11293 dtrace_predicate_t *pred; 11294 11295 ep = enab->dten_desc[i]; 11296 11297 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11298 dtrace_predicate_release(pred, vstate); 11299 11300 for (act = ep->dted_action; act != NULL; act = next) { 11301 next = act->dtad_next; 11302 dtrace_actdesc_release(act, vstate); 11303 } 11304 11305 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11306 } 11307 11308 if (enab->dten_desc != NULL) 11309 kmem_free(enab->dten_desc, 11310 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11311 11312 /* 11313 * If this was a retained enabling, decrement the dts_nretained count 11314 * and take it off of the dtrace_retained list. 11315 */ 11316 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11317 dtrace_retained == enab) { 11318 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11319 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11320 enab->dten_vstate->dtvs_state->dts_nretained--; 11321 } 11322 11323 if (enab->dten_prev == NULL) { 11324 if (dtrace_retained == enab) { 11325 dtrace_retained = enab->dten_next; 11326 11327 if (dtrace_retained != NULL) 11328 dtrace_retained->dten_prev = NULL; 11329 } 11330 } else { 11331 ASSERT(enab != dtrace_retained); 11332 ASSERT(dtrace_retained != NULL); 11333 enab->dten_prev->dten_next = enab->dten_next; 11334 } 11335 11336 if (enab->dten_next != NULL) { 11337 ASSERT(dtrace_retained != NULL); 11338 enab->dten_next->dten_prev = enab->dten_prev; 11339 } 11340 11341 kmem_free(enab, sizeof (dtrace_enabling_t)); 11342} 11343 11344static int 11345dtrace_enabling_retain(dtrace_enabling_t *enab) 11346{ 11347 dtrace_state_t *state; 11348 11349 ASSERT(MUTEX_HELD(&dtrace_lock)); 11350 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11351 ASSERT(enab->dten_vstate != NULL); 11352 11353 state = enab->dten_vstate->dtvs_state; 11354 ASSERT(state != NULL); 11355 11356 /* 11357 * We only allow each state to retain dtrace_retain_max enablings. 11358 */ 11359 if (state->dts_nretained >= dtrace_retain_max) 11360 return (ENOSPC); 11361 11362 state->dts_nretained++; 11363 11364 if (dtrace_retained == NULL) { 11365 dtrace_retained = enab; 11366 return (0); 11367 } 11368 11369 enab->dten_next = dtrace_retained; 11370 dtrace_retained->dten_prev = enab; 11371 dtrace_retained = enab; 11372 11373 return (0); 11374} 11375 11376static int 11377dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11378 dtrace_probedesc_t *create) 11379{ 11380 dtrace_enabling_t *new, *enab; 11381 int found = 0, err = ENOENT; 11382 11383 ASSERT(MUTEX_HELD(&dtrace_lock)); 11384 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11385 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11386 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11387 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11388 11389 new = dtrace_enabling_create(&state->dts_vstate); 11390 11391 /* 11392 * Iterate over all retained enablings, looking for enablings that 11393 * match the specified state. 11394 */ 11395 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11396 int i; 11397 11398 /* 11399 * dtvs_state can only be NULL for helper enablings -- and 11400 * helper enablings can't be retained. 11401 */ 11402 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11403 11404 if (enab->dten_vstate->dtvs_state != state) 11405 continue; 11406 11407 /* 11408 * Now iterate over each probe description; we're looking for 11409 * an exact match to the specified probe description. 11410 */ 11411 for (i = 0; i < enab->dten_ndesc; i++) { 11412 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11413 dtrace_probedesc_t *pd = &ep->dted_probe; 11414 11415 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11416 continue; 11417 11418 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11419 continue; 11420 11421 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11422 continue; 11423 11424 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11425 continue; 11426 11427 /* 11428 * We have a winning probe! Add it to our growing 11429 * enabling. 11430 */ 11431 found = 1; 11432 dtrace_enabling_addlike(new, ep, create); 11433 } 11434 } 11435 11436 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11437 dtrace_enabling_destroy(new); 11438 return (err); 11439 } 11440 11441 return (0); 11442} 11443 11444static void 11445dtrace_enabling_retract(dtrace_state_t *state) 11446{ 11447 dtrace_enabling_t *enab, *next; 11448 11449 ASSERT(MUTEX_HELD(&dtrace_lock)); 11450 11451 /* 11452 * Iterate over all retained enablings, destroy the enablings retained 11453 * for the specified state. 11454 */ 11455 for (enab = dtrace_retained; enab != NULL; enab = next) { 11456 next = enab->dten_next; 11457 11458 /* 11459 * dtvs_state can only be NULL for helper enablings -- and 11460 * helper enablings can't be retained. 11461 */ 11462 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11463 11464 if (enab->dten_vstate->dtvs_state == state) { 11465 ASSERT(state->dts_nretained > 0); 11466 dtrace_enabling_destroy(enab); 11467 } 11468 } 11469 11470 ASSERT(state->dts_nretained == 0); 11471} 11472 11473static int 11474dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11475{ 11476 int i = 0; 11477 int total_matched = 0, matched = 0; 11478 11479 ASSERT(MUTEX_HELD(&cpu_lock)); 11480 ASSERT(MUTEX_HELD(&dtrace_lock)); 11481 11482 for (i = 0; i < enab->dten_ndesc; i++) { 11483 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11484 11485 enab->dten_current = ep; 11486 enab->dten_error = 0; 11487 11488 /* 11489 * If a provider failed to enable a probe then get out and 11490 * let the consumer know we failed. 11491 */ 11492 if ((matched = dtrace_probe_enable(&ep->dted_probe, enab)) < 0) 11493 return (EBUSY); 11494 11495 total_matched += matched; 11496 11497 if (enab->dten_error != 0) { 11498 /* 11499 * If we get an error half-way through enabling the 11500 * probes, we kick out -- perhaps with some number of 11501 * them enabled. Leaving enabled probes enabled may 11502 * be slightly confusing for user-level, but we expect 11503 * that no one will attempt to actually drive on in 11504 * the face of such errors. If this is an anonymous 11505 * enabling (indicated with a NULL nmatched pointer), 11506 * we cmn_err() a message. We aren't expecting to 11507 * get such an error -- such as it can exist at all, 11508 * it would be a result of corrupted DOF in the driver 11509 * properties. 11510 */ 11511 if (nmatched == NULL) { 11512 cmn_err(CE_WARN, "dtrace_enabling_match() " 11513 "error on %p: %d", (void *)ep, 11514 enab->dten_error); 11515 } 11516 11517 return (enab->dten_error); 11518 } 11519 } 11520 11521 enab->dten_probegen = dtrace_probegen; 11522 if (nmatched != NULL) 11523 *nmatched = total_matched; 11524 11525 return (0); 11526} 11527 11528static void 11529dtrace_enabling_matchall(void) 11530{ 11531 dtrace_enabling_t *enab; 11532 11533 mutex_enter(&cpu_lock); 11534 mutex_enter(&dtrace_lock); 11535 11536 /* 11537 * Iterate over all retained enablings to see if any probes match 11538 * against them. We only perform this operation on enablings for which 11539 * we have sufficient permissions by virtue of being in the global zone 11540 * or in the same zone as the DTrace client. Because we can be called 11541 * after dtrace_detach() has been called, we cannot assert that there 11542 * are retained enablings. We can safely load from dtrace_retained, 11543 * however: the taskq_destroy() at the end of dtrace_detach() will 11544 * block pending our completion. 11545 */ 11546 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11547#if defined(sun) 11548 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 11549 11550 if (INGLOBALZONE(curproc) || getzoneid() == crgetzoneid(cr)) 11551#endif 11552 (void) dtrace_enabling_match(enab, NULL); 11553 } 11554 11555 mutex_exit(&dtrace_lock); 11556 mutex_exit(&cpu_lock); 11557} 11558 11559/* 11560 * If an enabling is to be enabled without having matched probes (that is, if 11561 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11562 * enabling must be _primed_ by creating an ECB for every ECB description. 11563 * This must be done to assure that we know the number of speculations, the 11564 * number of aggregations, the minimum buffer size needed, etc. before we 11565 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11566 * enabling any probes, we create ECBs for every ECB decription, but with a 11567 * NULL probe -- which is exactly what this function does. 11568 */ 11569static void 11570dtrace_enabling_prime(dtrace_state_t *state) 11571{ 11572 dtrace_enabling_t *enab; 11573 int i; 11574 11575 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11576 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11577 11578 if (enab->dten_vstate->dtvs_state != state) 11579 continue; 11580 11581 /* 11582 * We don't want to prime an enabling more than once, lest 11583 * we allow a malicious user to induce resource exhaustion. 11584 * (The ECBs that result from priming an enabling aren't 11585 * leaked -- but they also aren't deallocated until the 11586 * consumer state is destroyed.) 11587 */ 11588 if (enab->dten_primed) 11589 continue; 11590 11591 for (i = 0; i < enab->dten_ndesc; i++) { 11592 enab->dten_current = enab->dten_desc[i]; 11593 (void) dtrace_probe_enable(NULL, enab); 11594 } 11595 11596 enab->dten_primed = 1; 11597 } 11598} 11599 11600/* 11601 * Called to indicate that probes should be provided due to retained 11602 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11603 * must take an initial lap through the enabling calling the dtps_provide() 11604 * entry point explicitly to allow for autocreated probes. 11605 */ 11606static void 11607dtrace_enabling_provide(dtrace_provider_t *prv) 11608{ 11609 int i, all = 0; 11610 dtrace_probedesc_t desc; 11611 11612 ASSERT(MUTEX_HELD(&dtrace_lock)); 11613 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11614 11615 if (prv == NULL) { 11616 all = 1; 11617 prv = dtrace_provider; 11618 } 11619 11620 do { 11621 dtrace_enabling_t *enab = dtrace_retained; 11622 void *parg = prv->dtpv_arg; 11623 11624 for (; enab != NULL; enab = enab->dten_next) { 11625 for (i = 0; i < enab->dten_ndesc; i++) { 11626 desc = enab->dten_desc[i]->dted_probe; 11627 mutex_exit(&dtrace_lock); 11628 prv->dtpv_pops.dtps_provide(parg, &desc); 11629 mutex_enter(&dtrace_lock); 11630 } 11631 } 11632 } while (all && (prv = prv->dtpv_next) != NULL); 11633 11634 mutex_exit(&dtrace_lock); 11635 dtrace_probe_provide(NULL, all ? NULL : prv); 11636 mutex_enter(&dtrace_lock); 11637} 11638 11639/* 11640 * DTrace DOF Functions 11641 */ 11642/*ARGSUSED*/ 11643static void 11644dtrace_dof_error(dof_hdr_t *dof, const char *str) 11645{ 11646 if (dtrace_err_verbose) 11647 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11648 11649#ifdef DTRACE_ERRDEBUG 11650 dtrace_errdebug(str); 11651#endif 11652} 11653 11654/* 11655 * Create DOF out of a currently enabled state. Right now, we only create 11656 * DOF containing the run-time options -- but this could be expanded to create 11657 * complete DOF representing the enabled state. 11658 */ 11659static dof_hdr_t * 11660dtrace_dof_create(dtrace_state_t *state) 11661{ 11662 dof_hdr_t *dof; 11663 dof_sec_t *sec; 11664 dof_optdesc_t *opt; 11665 int i, len = sizeof (dof_hdr_t) + 11666 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11667 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11668 11669 ASSERT(MUTEX_HELD(&dtrace_lock)); 11670 11671 dof = kmem_zalloc(len, KM_SLEEP); 11672 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11673 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11674 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11675 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11676 11677 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11678 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11679 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11680 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11681 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11682 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11683 11684 dof->dofh_flags = 0; 11685 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11686 dof->dofh_secsize = sizeof (dof_sec_t); 11687 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11688 dof->dofh_secoff = sizeof (dof_hdr_t); 11689 dof->dofh_loadsz = len; 11690 dof->dofh_filesz = len; 11691 dof->dofh_pad = 0; 11692 11693 /* 11694 * Fill in the option section header... 11695 */ 11696 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11697 sec->dofs_type = DOF_SECT_OPTDESC; 11698 sec->dofs_align = sizeof (uint64_t); 11699 sec->dofs_flags = DOF_SECF_LOAD; 11700 sec->dofs_entsize = sizeof (dof_optdesc_t); 11701 11702 opt = (dof_optdesc_t *)((uintptr_t)sec + 11703 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11704 11705 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11706 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11707 11708 for (i = 0; i < DTRACEOPT_MAX; i++) { 11709 opt[i].dofo_option = i; 11710 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11711 opt[i].dofo_value = state->dts_options[i]; 11712 } 11713 11714 return (dof); 11715} 11716 11717static dof_hdr_t * 11718dtrace_dof_copyin(uintptr_t uarg, int *errp) 11719{ 11720 dof_hdr_t hdr, *dof; 11721 11722 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11723 11724 /* 11725 * First, we're going to copyin() the sizeof (dof_hdr_t). 11726 */ 11727 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11728 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11729 *errp = EFAULT; 11730 return (NULL); 11731 } 11732 11733 /* 11734 * Now we'll allocate the entire DOF and copy it in -- provided 11735 * that the length isn't outrageous. 11736 */ 11737 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11738 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11739 *errp = E2BIG; 11740 return (NULL); 11741 } 11742 11743 if (hdr.dofh_loadsz < sizeof (hdr)) { 11744 dtrace_dof_error(&hdr, "invalid load size"); 11745 *errp = EINVAL; 11746 return (NULL); 11747 } 11748 11749 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11750 11751 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 11752 dof->dofh_loadsz != hdr.dofh_loadsz) { 11753 kmem_free(dof, hdr.dofh_loadsz); 11754 *errp = EFAULT; 11755 return (NULL); 11756 } 11757 11758 return (dof); 11759} 11760 11761#if !defined(sun) 11762static __inline uchar_t 11763dtrace_dof_char(char c) { 11764 switch (c) { 11765 case '0': 11766 case '1': 11767 case '2': 11768 case '3': 11769 case '4': 11770 case '5': 11771 case '6': 11772 case '7': 11773 case '8': 11774 case '9': 11775 return (c - '0'); 11776 case 'A': 11777 case 'B': 11778 case 'C': 11779 case 'D': 11780 case 'E': 11781 case 'F': 11782 return (c - 'A' + 10); 11783 case 'a': 11784 case 'b': 11785 case 'c': 11786 case 'd': 11787 case 'e': 11788 case 'f': 11789 return (c - 'a' + 10); 11790 } 11791 /* Should not reach here. */ 11792 return (0); 11793} 11794#endif 11795 11796static dof_hdr_t * 11797dtrace_dof_property(const char *name) 11798{ 11799 dof_hdr_t *dof = NULL; 11800#if defined(sun) 11801 uchar_t *buf; 11802 uint64_t loadsz; 11803 unsigned int len, i; 11804 11805 /* 11806 * Unfortunately, array of values in .conf files are always (and 11807 * only) interpreted to be integer arrays. We must read our DOF 11808 * as an integer array, and then squeeze it into a byte array. 11809 */ 11810 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11811 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11812 return (NULL); 11813 11814 for (i = 0; i < len; i++) 11815 buf[i] = (uchar_t)(((int *)buf)[i]); 11816 11817 if (len < sizeof (dof_hdr_t)) { 11818 ddi_prop_free(buf); 11819 dtrace_dof_error(NULL, "truncated header"); 11820 return (NULL); 11821 } 11822 11823 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11824 ddi_prop_free(buf); 11825 dtrace_dof_error(NULL, "truncated DOF"); 11826 return (NULL); 11827 } 11828 11829 if (loadsz >= dtrace_dof_maxsize) { 11830 ddi_prop_free(buf); 11831 dtrace_dof_error(NULL, "oversized DOF"); 11832 return (NULL); 11833 } 11834 11835 dof = kmem_alloc(loadsz, KM_SLEEP); 11836 bcopy(buf, dof, loadsz); 11837 ddi_prop_free(buf); 11838#else 11839 printf("dtrace: XXX %s not implemented (name=%s)\n", __func__, name); 11840#if 0 /* XXX TBD dtrace_dof_provide */ 11841 char *p; 11842 char *p_env; 11843 11844 if ((p_env = getenv(name)) == NULL) 11845 return (NULL); 11846 11847 len = strlen(p_env) / 2; 11848 11849 buf = kmem_alloc(len, KM_SLEEP); 11850 11851 dof = (dof_hdr_t *) buf; 11852 11853 p = p_env; 11854 11855 for (i = 0; i < len; i++) { 11856 buf[i] = (dtrace_dof_char(p[0]) << 4) | 11857 dtrace_dof_char(p[1]); 11858 p += 2; 11859 } 11860 11861 freeenv(p_env); 11862 11863 if (len < sizeof (dof_hdr_t)) { 11864 kmem_free(buf, len); 11865 dtrace_dof_error(NULL, "truncated header"); 11866 return (NULL); 11867 } 11868 11869 if (len < (loadsz = dof->dofh_loadsz)) { 11870 kmem_free(buf, len); 11871 dtrace_dof_error(NULL, "truncated DOF"); 11872 return (NULL); 11873 } 11874 11875 if (loadsz >= dtrace_dof_maxsize) { 11876 kmem_free(buf, len); 11877 dtrace_dof_error(NULL, "oversized DOF"); 11878 return (NULL); 11879 } 11880#endif 11881#endif 11882 11883 return (dof); 11884} 11885 11886static void 11887dtrace_dof_destroy(dof_hdr_t *dof) 11888{ 11889 kmem_free(dof, dof->dofh_loadsz); 11890} 11891 11892/* 11893 * Return the dof_sec_t pointer corresponding to a given section index. If the 11894 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11895 * a type other than DOF_SECT_NONE is specified, the header is checked against 11896 * this type and NULL is returned if the types do not match. 11897 */ 11898static dof_sec_t * 11899dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11900{ 11901 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11902 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11903 11904 if (i >= dof->dofh_secnum) { 11905 dtrace_dof_error(dof, "referenced section index is invalid"); 11906 return (NULL); 11907 } 11908 11909 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11910 dtrace_dof_error(dof, "referenced section is not loadable"); 11911 return (NULL); 11912 } 11913 11914 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11915 dtrace_dof_error(dof, "referenced section is the wrong type"); 11916 return (NULL); 11917 } 11918 11919 return (sec); 11920} 11921 11922static dtrace_probedesc_t * 11923dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11924{ 11925 dof_probedesc_t *probe; 11926 dof_sec_t *strtab; 11927 uintptr_t daddr = (uintptr_t)dof; 11928 uintptr_t str; 11929 size_t size; 11930 11931 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11932 dtrace_dof_error(dof, "invalid probe section"); 11933 return (NULL); 11934 } 11935 11936 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11937 dtrace_dof_error(dof, "bad alignment in probe description"); 11938 return (NULL); 11939 } 11940 11941 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11942 dtrace_dof_error(dof, "truncated probe description"); 11943 return (NULL); 11944 } 11945 11946 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11947 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11948 11949 if (strtab == NULL) 11950 return (NULL); 11951 11952 str = daddr + strtab->dofs_offset; 11953 size = strtab->dofs_size; 11954 11955 if (probe->dofp_provider >= strtab->dofs_size) { 11956 dtrace_dof_error(dof, "corrupt probe provider"); 11957 return (NULL); 11958 } 11959 11960 (void) strncpy(desc->dtpd_provider, 11961 (char *)(str + probe->dofp_provider), 11962 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11963 11964 if (probe->dofp_mod >= strtab->dofs_size) { 11965 dtrace_dof_error(dof, "corrupt probe module"); 11966 return (NULL); 11967 } 11968 11969 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11970 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11971 11972 if (probe->dofp_func >= strtab->dofs_size) { 11973 dtrace_dof_error(dof, "corrupt probe function"); 11974 return (NULL); 11975 } 11976 11977 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11978 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11979 11980 if (probe->dofp_name >= strtab->dofs_size) { 11981 dtrace_dof_error(dof, "corrupt probe name"); 11982 return (NULL); 11983 } 11984 11985 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11986 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11987 11988 return (desc); 11989} 11990 11991static dtrace_difo_t * 11992dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11993 cred_t *cr) 11994{ 11995 dtrace_difo_t *dp; 11996 size_t ttl = 0; 11997 dof_difohdr_t *dofd; 11998 uintptr_t daddr = (uintptr_t)dof; 11999 size_t maxx = dtrace_difo_maxsize; 12000 int i, l, n; 12001 12002 static const struct { 12003 int section; 12004 int bufoffs; 12005 int lenoffs; 12006 int entsize; 12007 int align; 12008 const char *msg; 12009 } difo[] = { 12010 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 12011 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 12012 sizeof (dif_instr_t), "multiple DIF sections" }, 12013 12014 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 12015 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 12016 sizeof (uint64_t), "multiple integer tables" }, 12017 12018 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 12019 offsetof(dtrace_difo_t, dtdo_strlen), 0, 12020 sizeof (char), "multiple string tables" }, 12021 12022 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 12023 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 12024 sizeof (uint_t), "multiple variable tables" }, 12025 12026 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 12027 }; 12028 12029 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 12030 dtrace_dof_error(dof, "invalid DIFO header section"); 12031 return (NULL); 12032 } 12033 12034 if (sec->dofs_align != sizeof (dof_secidx_t)) { 12035 dtrace_dof_error(dof, "bad alignment in DIFO header"); 12036 return (NULL); 12037 } 12038 12039 if (sec->dofs_size < sizeof (dof_difohdr_t) || 12040 sec->dofs_size % sizeof (dof_secidx_t)) { 12041 dtrace_dof_error(dof, "bad size in DIFO header"); 12042 return (NULL); 12043 } 12044 12045 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12046 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 12047 12048 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 12049 dp->dtdo_rtype = dofd->dofd_rtype; 12050 12051 for (l = 0; l < n; l++) { 12052 dof_sec_t *subsec; 12053 void **bufp; 12054 uint32_t *lenp; 12055 12056 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 12057 dofd->dofd_links[l])) == NULL) 12058 goto err; /* invalid section link */ 12059 12060 if (ttl + subsec->dofs_size > maxx) { 12061 dtrace_dof_error(dof, "exceeds maximum size"); 12062 goto err; 12063 } 12064 12065 ttl += subsec->dofs_size; 12066 12067 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 12068 if (subsec->dofs_type != difo[i].section) 12069 continue; 12070 12071 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 12072 dtrace_dof_error(dof, "section not loaded"); 12073 goto err; 12074 } 12075 12076 if (subsec->dofs_align != difo[i].align) { 12077 dtrace_dof_error(dof, "bad alignment"); 12078 goto err; 12079 } 12080 12081 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 12082 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 12083 12084 if (*bufp != NULL) { 12085 dtrace_dof_error(dof, difo[i].msg); 12086 goto err; 12087 } 12088 12089 if (difo[i].entsize != subsec->dofs_entsize) { 12090 dtrace_dof_error(dof, "entry size mismatch"); 12091 goto err; 12092 } 12093 12094 if (subsec->dofs_entsize != 0 && 12095 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 12096 dtrace_dof_error(dof, "corrupt entry size"); 12097 goto err; 12098 } 12099 12100 *lenp = subsec->dofs_size; 12101 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 12102 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 12103 *bufp, subsec->dofs_size); 12104 12105 if (subsec->dofs_entsize != 0) 12106 *lenp /= subsec->dofs_entsize; 12107 12108 break; 12109 } 12110 12111 /* 12112 * If we encounter a loadable DIFO sub-section that is not 12113 * known to us, assume this is a broken program and fail. 12114 */ 12115 if (difo[i].section == DOF_SECT_NONE && 12116 (subsec->dofs_flags & DOF_SECF_LOAD)) { 12117 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 12118 goto err; 12119 } 12120 } 12121 12122 if (dp->dtdo_buf == NULL) { 12123 /* 12124 * We can't have a DIF object without DIF text. 12125 */ 12126 dtrace_dof_error(dof, "missing DIF text"); 12127 goto err; 12128 } 12129 12130 /* 12131 * Before we validate the DIF object, run through the variable table 12132 * looking for the strings -- if any of their size are under, we'll set 12133 * their size to be the system-wide default string size. Note that 12134 * this should _not_ happen if the "strsize" option has been set -- 12135 * in this case, the compiler should have set the size to reflect the 12136 * setting of the option. 12137 */ 12138 for (i = 0; i < dp->dtdo_varlen; i++) { 12139 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 12140 dtrace_diftype_t *t = &v->dtdv_type; 12141 12142 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 12143 continue; 12144 12145 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 12146 t->dtdt_size = dtrace_strsize_default; 12147 } 12148 12149 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 12150 goto err; 12151 12152 dtrace_difo_init(dp, vstate); 12153 return (dp); 12154 12155err: 12156 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 12157 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 12158 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 12159 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 12160 12161 kmem_free(dp, sizeof (dtrace_difo_t)); 12162 return (NULL); 12163} 12164 12165static dtrace_predicate_t * 12166dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12167 cred_t *cr) 12168{ 12169 dtrace_difo_t *dp; 12170 12171 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 12172 return (NULL); 12173 12174 return (dtrace_predicate_create(dp)); 12175} 12176 12177static dtrace_actdesc_t * 12178dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12179 cred_t *cr) 12180{ 12181 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 12182 dof_actdesc_t *desc; 12183 dof_sec_t *difosec; 12184 size_t offs; 12185 uintptr_t daddr = (uintptr_t)dof; 12186 uint64_t arg; 12187 dtrace_actkind_t kind; 12188 12189 if (sec->dofs_type != DOF_SECT_ACTDESC) { 12190 dtrace_dof_error(dof, "invalid action section"); 12191 return (NULL); 12192 } 12193 12194 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 12195 dtrace_dof_error(dof, "truncated action description"); 12196 return (NULL); 12197 } 12198 12199 if (sec->dofs_align != sizeof (uint64_t)) { 12200 dtrace_dof_error(dof, "bad alignment in action description"); 12201 return (NULL); 12202 } 12203 12204 if (sec->dofs_size < sec->dofs_entsize) { 12205 dtrace_dof_error(dof, "section entry size exceeds total size"); 12206 return (NULL); 12207 } 12208 12209 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 12210 dtrace_dof_error(dof, "bad entry size in action description"); 12211 return (NULL); 12212 } 12213 12214 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 12215 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12216 return (NULL); 12217 } 12218 12219 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12220 desc = (dof_actdesc_t *)(daddr + 12221 (uintptr_t)sec->dofs_offset + offs); 12222 kind = (dtrace_actkind_t)desc->dofa_kind; 12223 12224 if (DTRACEACT_ISPRINTFLIKE(kind) && 12225 (kind != DTRACEACT_PRINTA || 12226 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12227 dof_sec_t *strtab; 12228 char *str, *fmt; 12229 uint64_t i; 12230 12231 /* 12232 * printf()-like actions must have a format string. 12233 */ 12234 if ((strtab = dtrace_dof_sect(dof, 12235 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12236 goto err; 12237 12238 str = (char *)((uintptr_t)dof + 12239 (uintptr_t)strtab->dofs_offset); 12240 12241 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12242 if (str[i] == '\0') 12243 break; 12244 } 12245 12246 if (i >= strtab->dofs_size) { 12247 dtrace_dof_error(dof, "bogus format string"); 12248 goto err; 12249 } 12250 12251 if (i == desc->dofa_arg) { 12252 dtrace_dof_error(dof, "empty format string"); 12253 goto err; 12254 } 12255 12256 i -= desc->dofa_arg; 12257 fmt = kmem_alloc(i + 1, KM_SLEEP); 12258 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12259 arg = (uint64_t)(uintptr_t)fmt; 12260 } else { 12261 if (kind == DTRACEACT_PRINTA) { 12262 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12263 arg = 0; 12264 } else { 12265 arg = desc->dofa_arg; 12266 } 12267 } 12268 12269 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12270 desc->dofa_uarg, arg); 12271 12272 if (last != NULL) { 12273 last->dtad_next = act; 12274 } else { 12275 first = act; 12276 } 12277 12278 last = act; 12279 12280 if (desc->dofa_difo == DOF_SECIDX_NONE) 12281 continue; 12282 12283 if ((difosec = dtrace_dof_sect(dof, 12284 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12285 goto err; 12286 12287 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12288 12289 if (act->dtad_difo == NULL) 12290 goto err; 12291 } 12292 12293 ASSERT(first != NULL); 12294 return (first); 12295 12296err: 12297 for (act = first; act != NULL; act = next) { 12298 next = act->dtad_next; 12299 dtrace_actdesc_release(act, vstate); 12300 } 12301 12302 return (NULL); 12303} 12304 12305static dtrace_ecbdesc_t * 12306dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12307 cred_t *cr) 12308{ 12309 dtrace_ecbdesc_t *ep; 12310 dof_ecbdesc_t *ecb; 12311 dtrace_probedesc_t *desc; 12312 dtrace_predicate_t *pred = NULL; 12313 12314 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12315 dtrace_dof_error(dof, "truncated ECB description"); 12316 return (NULL); 12317 } 12318 12319 if (sec->dofs_align != sizeof (uint64_t)) { 12320 dtrace_dof_error(dof, "bad alignment in ECB description"); 12321 return (NULL); 12322 } 12323 12324 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12325 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12326 12327 if (sec == NULL) 12328 return (NULL); 12329 12330 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12331 ep->dted_uarg = ecb->dofe_uarg; 12332 desc = &ep->dted_probe; 12333 12334 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12335 goto err; 12336 12337 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12338 if ((sec = dtrace_dof_sect(dof, 12339 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12340 goto err; 12341 12342 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12343 goto err; 12344 12345 ep->dted_pred.dtpdd_predicate = pred; 12346 } 12347 12348 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12349 if ((sec = dtrace_dof_sect(dof, 12350 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12351 goto err; 12352 12353 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12354 12355 if (ep->dted_action == NULL) 12356 goto err; 12357 } 12358 12359 return (ep); 12360 12361err: 12362 if (pred != NULL) 12363 dtrace_predicate_release(pred, vstate); 12364 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12365 return (NULL); 12366} 12367 12368/* 12369 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12370 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12371 * site of any user SETX relocations to account for load object base address. 12372 * In the future, if we need other relocations, this function can be extended. 12373 */ 12374static int 12375dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12376{ 12377 uintptr_t daddr = (uintptr_t)dof; 12378 dof_relohdr_t *dofr = 12379 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12380 dof_sec_t *ss, *rs, *ts; 12381 dof_relodesc_t *r; 12382 uint_t i, n; 12383 12384 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12385 sec->dofs_align != sizeof (dof_secidx_t)) { 12386 dtrace_dof_error(dof, "invalid relocation header"); 12387 return (-1); 12388 } 12389 12390 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12391 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12392 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12393 12394 if (ss == NULL || rs == NULL || ts == NULL) 12395 return (-1); /* dtrace_dof_error() has been called already */ 12396 12397 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12398 rs->dofs_align != sizeof (uint64_t)) { 12399 dtrace_dof_error(dof, "invalid relocation section"); 12400 return (-1); 12401 } 12402 12403 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12404 n = rs->dofs_size / rs->dofs_entsize; 12405 12406 for (i = 0; i < n; i++) { 12407 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12408 12409 switch (r->dofr_type) { 12410 case DOF_RELO_NONE: 12411 break; 12412 case DOF_RELO_SETX: 12413 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12414 sizeof (uint64_t) > ts->dofs_size) { 12415 dtrace_dof_error(dof, "bad relocation offset"); 12416 return (-1); 12417 } 12418 12419 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12420 dtrace_dof_error(dof, "misaligned setx relo"); 12421 return (-1); 12422 } 12423 12424 *(uint64_t *)taddr += ubase; 12425 break; 12426 default: 12427 dtrace_dof_error(dof, "invalid relocation type"); 12428 return (-1); 12429 } 12430 12431 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12432 } 12433 12434 return (0); 12435} 12436 12437/* 12438 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12439 * header: it should be at the front of a memory region that is at least 12440 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12441 * size. It need not be validated in any other way. 12442 */ 12443static int 12444dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12445 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12446{ 12447 uint64_t len = dof->dofh_loadsz, seclen; 12448 uintptr_t daddr = (uintptr_t)dof; 12449 dtrace_ecbdesc_t *ep; 12450 dtrace_enabling_t *enab; 12451 uint_t i; 12452 12453 ASSERT(MUTEX_HELD(&dtrace_lock)); 12454 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12455 12456 /* 12457 * Check the DOF header identification bytes. In addition to checking 12458 * valid settings, we also verify that unused bits/bytes are zeroed so 12459 * we can use them later without fear of regressing existing binaries. 12460 */ 12461 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12462 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12463 dtrace_dof_error(dof, "DOF magic string mismatch"); 12464 return (-1); 12465 } 12466 12467 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12468 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12469 dtrace_dof_error(dof, "DOF has invalid data model"); 12470 return (-1); 12471 } 12472 12473 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12474 dtrace_dof_error(dof, "DOF encoding mismatch"); 12475 return (-1); 12476 } 12477 12478 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12479 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12480 dtrace_dof_error(dof, "DOF version mismatch"); 12481 return (-1); 12482 } 12483 12484 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12485 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12486 return (-1); 12487 } 12488 12489 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12490 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12491 return (-1); 12492 } 12493 12494 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12495 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12496 return (-1); 12497 } 12498 12499 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12500 if (dof->dofh_ident[i] != 0) { 12501 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12502 return (-1); 12503 } 12504 } 12505 12506 if (dof->dofh_flags & ~DOF_FL_VALID) { 12507 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12508 return (-1); 12509 } 12510 12511 if (dof->dofh_secsize == 0) { 12512 dtrace_dof_error(dof, "zero section header size"); 12513 return (-1); 12514 } 12515 12516 /* 12517 * Check that the section headers don't exceed the amount of DOF 12518 * data. Note that we cast the section size and number of sections 12519 * to uint64_t's to prevent possible overflow in the multiplication. 12520 */ 12521 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12522 12523 if (dof->dofh_secoff > len || seclen > len || 12524 dof->dofh_secoff + seclen > len) { 12525 dtrace_dof_error(dof, "truncated section headers"); 12526 return (-1); 12527 } 12528 12529 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12530 dtrace_dof_error(dof, "misaligned section headers"); 12531 return (-1); 12532 } 12533 12534 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12535 dtrace_dof_error(dof, "misaligned section size"); 12536 return (-1); 12537 } 12538 12539 /* 12540 * Take an initial pass through the section headers to be sure that 12541 * the headers don't have stray offsets. If the 'noprobes' flag is 12542 * set, do not permit sections relating to providers, probes, or args. 12543 */ 12544 for (i = 0; i < dof->dofh_secnum; i++) { 12545 dof_sec_t *sec = (dof_sec_t *)(daddr + 12546 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12547 12548 if (noprobes) { 12549 switch (sec->dofs_type) { 12550 case DOF_SECT_PROVIDER: 12551 case DOF_SECT_PROBES: 12552 case DOF_SECT_PRARGS: 12553 case DOF_SECT_PROFFS: 12554 dtrace_dof_error(dof, "illegal sections " 12555 "for enabling"); 12556 return (-1); 12557 } 12558 } 12559 12560 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 12561 !(sec->dofs_flags & DOF_SECF_LOAD)) { 12562 dtrace_dof_error(dof, "loadable section with load " 12563 "flag unset"); 12564 return (-1); 12565 } 12566 12567 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12568 continue; /* just ignore non-loadable sections */ 12569 12570 if (sec->dofs_align & (sec->dofs_align - 1)) { 12571 dtrace_dof_error(dof, "bad section alignment"); 12572 return (-1); 12573 } 12574 12575 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12576 dtrace_dof_error(dof, "misaligned section"); 12577 return (-1); 12578 } 12579 12580 if (sec->dofs_offset > len || sec->dofs_size > len || 12581 sec->dofs_offset + sec->dofs_size > len) { 12582 dtrace_dof_error(dof, "corrupt section header"); 12583 return (-1); 12584 } 12585 12586 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12587 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12588 dtrace_dof_error(dof, "non-terminating string table"); 12589 return (-1); 12590 } 12591 } 12592 12593 /* 12594 * Take a second pass through the sections and locate and perform any 12595 * relocations that are present. We do this after the first pass to 12596 * be sure that all sections have had their headers validated. 12597 */ 12598 for (i = 0; i < dof->dofh_secnum; i++) { 12599 dof_sec_t *sec = (dof_sec_t *)(daddr + 12600 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12601 12602 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12603 continue; /* skip sections that are not loadable */ 12604 12605 switch (sec->dofs_type) { 12606 case DOF_SECT_URELHDR: 12607 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12608 return (-1); 12609 break; 12610 } 12611 } 12612 12613 if ((enab = *enabp) == NULL) 12614 enab = *enabp = dtrace_enabling_create(vstate); 12615 12616 for (i = 0; i < dof->dofh_secnum; i++) { 12617 dof_sec_t *sec = (dof_sec_t *)(daddr + 12618 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12619 12620 if (sec->dofs_type != DOF_SECT_ECBDESC) 12621 continue; 12622 12623 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12624 dtrace_enabling_destroy(enab); 12625 *enabp = NULL; 12626 return (-1); 12627 } 12628 12629 dtrace_enabling_add(enab, ep); 12630 } 12631 12632 return (0); 12633} 12634 12635/* 12636 * Process DOF for any options. This routine assumes that the DOF has been 12637 * at least processed by dtrace_dof_slurp(). 12638 */ 12639static int 12640dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12641{ 12642 int i, rval; 12643 uint32_t entsize; 12644 size_t offs; 12645 dof_optdesc_t *desc; 12646 12647 for (i = 0; i < dof->dofh_secnum; i++) { 12648 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12649 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12650 12651 if (sec->dofs_type != DOF_SECT_OPTDESC) 12652 continue; 12653 12654 if (sec->dofs_align != sizeof (uint64_t)) { 12655 dtrace_dof_error(dof, "bad alignment in " 12656 "option description"); 12657 return (EINVAL); 12658 } 12659 12660 if ((entsize = sec->dofs_entsize) == 0) { 12661 dtrace_dof_error(dof, "zeroed option entry size"); 12662 return (EINVAL); 12663 } 12664 12665 if (entsize < sizeof (dof_optdesc_t)) { 12666 dtrace_dof_error(dof, "bad option entry size"); 12667 return (EINVAL); 12668 } 12669 12670 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12671 desc = (dof_optdesc_t *)((uintptr_t)dof + 12672 (uintptr_t)sec->dofs_offset + offs); 12673 12674 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12675 dtrace_dof_error(dof, "non-zero option string"); 12676 return (EINVAL); 12677 } 12678 12679 if (desc->dofo_value == DTRACEOPT_UNSET) { 12680 dtrace_dof_error(dof, "unset option"); 12681 return (EINVAL); 12682 } 12683 12684 if ((rval = dtrace_state_option(state, 12685 desc->dofo_option, desc->dofo_value)) != 0) { 12686 dtrace_dof_error(dof, "rejected option"); 12687 return (rval); 12688 } 12689 } 12690 } 12691 12692 return (0); 12693} 12694 12695/* 12696 * DTrace Consumer State Functions 12697 */ 12698static int 12699dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12700{ 12701 size_t hashsize, maxper, minn, chunksize = dstate->dtds_chunksize; 12702 void *base; 12703 uintptr_t limit; 12704 dtrace_dynvar_t *dvar, *next, *start; 12705 int i; 12706 12707 ASSERT(MUTEX_HELD(&dtrace_lock)); 12708 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12709 12710 bzero(dstate, sizeof (dtrace_dstate_t)); 12711 12712 if ((dstate->dtds_chunksize = chunksize) == 0) 12713 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12714 12715 if (size < (minn = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12716 size = minn; 12717 12718 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 12719 return (ENOMEM); 12720 12721 dstate->dtds_size = size; 12722 dstate->dtds_base = base; 12723 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12724 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12725 12726 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12727 12728 if (hashsize != 1 && (hashsize & 1)) 12729 hashsize--; 12730 12731 dstate->dtds_hashsize = hashsize; 12732 dstate->dtds_hash = dstate->dtds_base; 12733 12734 /* 12735 * Set all of our hash buckets to point to the single sink, and (if 12736 * it hasn't already been set), set the sink's hash value to be the 12737 * sink sentinel value. The sink is needed for dynamic variable 12738 * lookups to know that they have iterated over an entire, valid hash 12739 * chain. 12740 */ 12741 for (i = 0; i < hashsize; i++) 12742 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12743 12744 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12745 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12746 12747 /* 12748 * Determine number of active CPUs. Divide free list evenly among 12749 * active CPUs. 12750 */ 12751 start = (dtrace_dynvar_t *) 12752 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12753 limit = (uintptr_t)base + size; 12754 12755 maxper = (limit - (uintptr_t)start) / NCPU; 12756 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12757 12758 for (i = 0; i < NCPU; i++) { 12759 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12760 12761 /* 12762 * If we don't even have enough chunks to make it once through 12763 * NCPUs, we're just going to allocate everything to the first 12764 * CPU. And if we're on the last CPU, we're going to allocate 12765 * whatever is left over. In either case, we set the limit to 12766 * be the limit of the dynamic variable space. 12767 */ 12768 if (maxper == 0 || i == NCPU - 1) { 12769 limit = (uintptr_t)base + size; 12770 start = NULL; 12771 } else { 12772 limit = (uintptr_t)start + maxper; 12773 start = (dtrace_dynvar_t *)limit; 12774 } 12775 12776 ASSERT(limit <= (uintptr_t)base + size); 12777 12778 for (;;) { 12779 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12780 dstate->dtds_chunksize); 12781 12782 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12783 break; 12784 12785 dvar->dtdv_next = next; 12786 dvar = next; 12787 } 12788 12789 if (maxper == 0) 12790 break; 12791 } 12792 12793 return (0); 12794} 12795 12796static void 12797dtrace_dstate_fini(dtrace_dstate_t *dstate) 12798{ 12799 ASSERT(MUTEX_HELD(&cpu_lock)); 12800 12801 if (dstate->dtds_base == NULL) 12802 return; 12803 12804 kmem_free(dstate->dtds_base, dstate->dtds_size); 12805 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12806} 12807 12808static void 12809dtrace_vstate_fini(dtrace_vstate_t *vstate) 12810{ 12811 /* 12812 * Logical XOR, where are you? 12813 */ 12814 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12815 12816 if (vstate->dtvs_nglobals > 0) { 12817 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12818 sizeof (dtrace_statvar_t *)); 12819 } 12820 12821 if (vstate->dtvs_ntlocals > 0) { 12822 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12823 sizeof (dtrace_difv_t)); 12824 } 12825 12826 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12827 12828 if (vstate->dtvs_nlocals > 0) { 12829 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12830 sizeof (dtrace_statvar_t *)); 12831 } 12832} 12833 12834static void 12835dtrace_state_clean(dtrace_state_t *state) 12836{ 12837 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12838 return; 12839 12840 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12841 dtrace_speculation_clean(state); 12842} 12843 12844static void 12845dtrace_state_deadman(dtrace_state_t *state) 12846{ 12847 hrtime_t now; 12848 12849 dtrace_sync(); 12850 12851 now = dtrace_gethrtime(); 12852 12853 if (state != dtrace_anon.dta_state && 12854 now - state->dts_laststatus >= dtrace_deadman_user) 12855 return; 12856 12857 /* 12858 * We must be sure that dts_alive never appears to be less than the 12859 * value upon entry to dtrace_state_deadman(), and because we lack a 12860 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12861 * store INT64_MAX to it, followed by a memory barrier, followed by 12862 * the new value. This assures that dts_alive never appears to be 12863 * less than its true value, regardless of the order in which the 12864 * stores to the underlying storage are issued. 12865 */ 12866 state->dts_alive = INT64_MAX; 12867 dtrace_membar_producer(); 12868 state->dts_alive = now; 12869} 12870 12871#if !defined(sun) 12872struct dtrace_state_worker *dtrace_state_worker_add(void (*)(dtrace_state_t *), 12873 dtrace_state_t *, hrtime_t); 12874void dtrace_state_worker_remove(struct dtrace_state_worker *); 12875#endif 12876 12877static dtrace_state_t * 12878#if defined(sun) 12879dtrace_state_create(dev_t *devp, cred_t *cr) 12880#else 12881dtrace_state_create(dev_t dev, cred_t *cr) 12882#endif 12883{ 12884#if defined(sun) 12885 minor_t minor; 12886 major_t major; 12887#else 12888 int m = 0; 12889#endif 12890 char c[30]; 12891 dtrace_state_t *state; 12892 dtrace_optval_t *opt; 12893 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12894 12895 ASSERT(MUTEX_HELD(&dtrace_lock)); 12896 ASSERT(MUTEX_HELD(&cpu_lock)); 12897 12898#if defined(sun) 12899 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12900 VM_BESTFIT | VM_SLEEP); 12901 12902 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12903 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12904 return (NULL); 12905 } 12906 12907 state = ddi_get_soft_state(dtrace_softstate, minor); 12908#else 12909 m = minor(dev) & 0x0F; 12910 12911 /* Allocate memory for the state. */ 12912 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 12913#endif 12914 12915 state->dts_epid = DTRACE_EPIDNONE + 1; 12916 12917 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 12918#if defined(sun) 12919 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12920 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12921 12922 if (devp != NULL) { 12923 major = getemajor(*devp); 12924 } else { 12925 major = ddi_driver_major(dtrace_devi); 12926 } 12927 12928 state->dts_dev = makedevice(major, minor); 12929 12930 if (devp != NULL) 12931 *devp = state->dts_dev; 12932#else 12933 state->dts_aggid_arena = vmem_create(c, 1, INT_MAX, 1, 12934 NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE); 12935 state->dts_dev = dev; 12936#endif 12937 12938 /* 12939 * We allocate NCPU buffers. On the one hand, this can be quite 12940 * a bit of memory per instance (nearly 36K on a Starcat). On the 12941 * other hand, it saves an additional memory reference in the probe 12942 * path. 12943 */ 12944 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12945 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12946 12947#if defined(sun) 12948 state->dts_cleaner = CYCLIC_NONE; 12949 state->dts_deadman = CYCLIC_NONE; 12950#else 12951 state->dts_cleaner = NULL; 12952 state->dts_deadman = NULL; 12953#endif 12954 state->dts_vstate.dtvs_state = state; 12955 12956 for (i = 0; i < DTRACEOPT_MAX; i++) 12957 state->dts_options[i] = DTRACEOPT_UNSET; 12958 12959 /* 12960 * Set the default options. 12961 */ 12962 opt = state->dts_options; 12963 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12964 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12965 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12966 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12967 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12968 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12969 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12970 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12971 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12972 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12973 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12974 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12975 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12976 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12977 12978 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12979 12980 /* 12981 * Depending on the user credentials, we set flag bits which alter probe 12982 * visibility or the amount of destructiveness allowed. In the case of 12983 * actual anonymous tracing, or the possession of all privileges, all of 12984 * the normal checks are bypassed. 12985 */ 12986 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12987 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12988 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12989 } else { 12990 /* 12991 * Set up the credentials for this instantiation. We take a 12992 * hold on the credential to prevent it from disappearing on 12993 * us; this in turn prevents the zone_t referenced by this 12994 * credential from disappearing. This means that we can 12995 * examine the credential and the zone from probe context. 12996 */ 12997#if defined(sun) 12998 crhold(cr); 12999#else 13000 kauth_cred_hold(cr); 13001#endif 13002 state->dts_cred.dcr_cred = cr; 13003 13004 /* 13005 * CRA_PROC means "we have *some* privilege for dtrace" and 13006 * unlocks the use of variables like pid, zonename, etc. 13007 */ 13008 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 13009 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13010 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 13011 } 13012 13013 /* 13014 * dtrace_user allows use of syscall and profile providers. 13015 * If the user also has proc_owner and/or proc_zone, we 13016 * extend the scope to include additional visibility and 13017 * destructive power. 13018 */ 13019 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 13020 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 13021 state->dts_cred.dcr_visible |= 13022 DTRACE_CRV_ALLPROC; 13023 13024 state->dts_cred.dcr_action |= 13025 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13026 } 13027 13028 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 13029 state->dts_cred.dcr_visible |= 13030 DTRACE_CRV_ALLZONE; 13031 13032 state->dts_cred.dcr_action |= 13033 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13034 } 13035 13036 /* 13037 * If we have all privs in whatever zone this is, 13038 * we can do destructive things to processes which 13039 * have altered credentials. 13040 */ 13041#if defined(sun) 13042 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13043 cr->cr_zone->zone_privset)) { 13044 state->dts_cred.dcr_action |= 13045 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13046 } 13047#endif 13048 } 13049 13050 /* 13051 * Holding the dtrace_kernel privilege also implies that 13052 * the user has the dtrace_user privilege from a visibility 13053 * perspective. But without further privileges, some 13054 * destructive actions are not available. 13055 */ 13056 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 13057 /* 13058 * Make all probes in all zones visible. However, 13059 * this doesn't mean that all actions become available 13060 * to all zones. 13061 */ 13062 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 13063 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 13064 13065 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 13066 DTRACE_CRA_PROC; 13067 /* 13068 * Holding proc_owner means that destructive actions 13069 * for *this* zone are allowed. 13070 */ 13071 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13072 state->dts_cred.dcr_action |= 13073 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13074 13075 /* 13076 * Holding proc_zone means that destructive actions 13077 * for this user/group ID in all zones is allowed. 13078 */ 13079 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13080 state->dts_cred.dcr_action |= 13081 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13082 13083#if defined(sun) 13084 /* 13085 * If we have all privs in whatever zone this is, 13086 * we can do destructive things to processes which 13087 * have altered credentials. 13088 */ 13089 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 13090 cr->cr_zone->zone_privset)) { 13091 state->dts_cred.dcr_action |= 13092 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 13093 } 13094#endif 13095 } 13096 13097 /* 13098 * Holding the dtrace_proc privilege gives control over fasttrap 13099 * and pid providers. We need to grant wider destructive 13100 * privileges in the event that the user has proc_owner and/or 13101 * proc_zone. 13102 */ 13103 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 13104 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 13105 state->dts_cred.dcr_action |= 13106 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 13107 13108 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 13109 state->dts_cred.dcr_action |= 13110 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 13111 } 13112 } 13113 13114 return (state); 13115} 13116 13117static int 13118dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 13119{ 13120 dtrace_optval_t *opt = state->dts_options, size; 13121 processorid_t xcpu = 0;; 13122 int flags = 0, rval; 13123 13124 ASSERT(MUTEX_HELD(&dtrace_lock)); 13125 ASSERT(MUTEX_HELD(&cpu_lock)); 13126 ASSERT(which < DTRACEOPT_MAX); 13127 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 13128 (state == dtrace_anon.dta_state && 13129 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 13130 13131 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 13132 return (0); 13133 13134 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 13135 xcpu = opt[DTRACEOPT_CPU]; 13136 13137 if (which == DTRACEOPT_SPECSIZE) 13138 flags |= DTRACEBUF_NOSWITCH; 13139 13140 if (which == DTRACEOPT_BUFSIZE) { 13141 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 13142 flags |= DTRACEBUF_RING; 13143 13144 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 13145 flags |= DTRACEBUF_FILL; 13146 13147 if (state != dtrace_anon.dta_state || 13148 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 13149 flags |= DTRACEBUF_INACTIVE; 13150 } 13151 13152 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 13153 /* 13154 * The size must be 8-byte aligned. If the size is not 8-byte 13155 * aligned, drop it down by the difference. 13156 */ 13157 if (size & (sizeof (uint64_t) - 1)) 13158 size -= size & (sizeof (uint64_t) - 1); 13159 13160 if (size < state->dts_reserve) { 13161 /* 13162 * Buffers always must be large enough to accommodate 13163 * their prereserved space. We return E2BIG instead 13164 * of ENOMEM in this case to allow for user-level 13165 * software to differentiate the cases. 13166 */ 13167 return (E2BIG); 13168 } 13169 13170 rval = dtrace_buffer_alloc(buf, size, flags, xcpu); 13171 13172 if (rval != ENOMEM) { 13173 opt[which] = size; 13174 return (rval); 13175 } 13176 13177 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13178 return (rval); 13179 } 13180 13181 return (ENOMEM); 13182} 13183 13184static int 13185dtrace_state_buffers(dtrace_state_t *state) 13186{ 13187 dtrace_speculation_t *spec = state->dts_speculations; 13188 int rval, i; 13189 13190 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 13191 DTRACEOPT_BUFSIZE)) != 0) 13192 return (rval); 13193 13194 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 13195 DTRACEOPT_AGGSIZE)) != 0) 13196 return (rval); 13197 13198 for (i = 0; i < state->dts_nspeculations; i++) { 13199 if ((rval = dtrace_state_buffer(state, 13200 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 13201 return (rval); 13202 } 13203 13204 return (0); 13205} 13206 13207static void 13208dtrace_state_prereserve(dtrace_state_t *state) 13209{ 13210 dtrace_ecb_t *ecb; 13211 dtrace_probe_t *probe; 13212 13213 state->dts_reserve = 0; 13214 13215 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 13216 return; 13217 13218 /* 13219 * If our buffer policy is a "fill" buffer policy, we need to set the 13220 * prereserved space to be the space required by the END probes. 13221 */ 13222 probe = dtrace_probes[dtrace_probeid_end - 1]; 13223 ASSERT(probe != NULL); 13224 13225 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 13226 if (ecb->dte_state != state) 13227 continue; 13228 13229 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 13230 } 13231} 13232 13233static int 13234dtrace_state_go(dtrace_state_t *state, processorid_t *xcpu) 13235{ 13236 dtrace_optval_t *opt = state->dts_options, sz, nspec; 13237 dtrace_speculation_t *spec; 13238 dtrace_buffer_t *buf; 13239#if defined(sun) 13240 cyc_handler_t hdlr; 13241 cyc_time_t when; 13242#endif 13243 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13244 dtrace_icookie_t cookie; 13245 13246 mutex_enter(&cpu_lock); 13247 mutex_enter(&dtrace_lock); 13248 13249 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13250 rval = EBUSY; 13251 goto out; 13252 } 13253 13254 /* 13255 * Before we can perform any checks, we must prime all of the 13256 * retained enablings that correspond to this state. 13257 */ 13258 dtrace_enabling_prime(state); 13259 13260 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13261 rval = EACCES; 13262 goto out; 13263 } 13264 13265 dtrace_state_prereserve(state); 13266 13267 /* 13268 * Now we want to do is try to allocate our speculations. 13269 * We do not automatically resize the number of speculations; if 13270 * this fails, we will fail the operation. 13271 */ 13272 nspec = opt[DTRACEOPT_NSPEC]; 13273 ASSERT(nspec != DTRACEOPT_UNSET); 13274 13275 if (nspec > INT_MAX) { 13276 rval = ENOMEM; 13277 goto out; 13278 } 13279 13280 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 13281 13282 if (spec == NULL) { 13283 rval = ENOMEM; 13284 goto out; 13285 } 13286 13287 state->dts_speculations = spec; 13288 state->dts_nspeculations = (int)nspec; 13289 13290 for (i = 0; i < nspec; i++) { 13291 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 13292 rval = ENOMEM; 13293 goto err; 13294 } 13295 13296 spec[i].dtsp_buffer = buf; 13297 } 13298 13299 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13300 if (dtrace_anon.dta_state == NULL) { 13301 rval = ENOENT; 13302 goto out; 13303 } 13304 13305 if (state->dts_necbs != 0) { 13306 rval = EALREADY; 13307 goto out; 13308 } 13309 13310 state->dts_anon = dtrace_anon_grab(); 13311 ASSERT(state->dts_anon != NULL); 13312 state = state->dts_anon; 13313 13314 /* 13315 * We want "grabanon" to be set in the grabbed state, so we'll 13316 * copy that option value from the grabbing state into the 13317 * grabbed state. 13318 */ 13319 state->dts_options[DTRACEOPT_GRABANON] = 13320 opt[DTRACEOPT_GRABANON]; 13321 13322 *xcpu = dtrace_anon.dta_beganon; 13323 13324 /* 13325 * If the anonymous state is active (as it almost certainly 13326 * is if the anonymous enabling ultimately matched anything), 13327 * we don't allow any further option processing -- but we 13328 * don't return failure. 13329 */ 13330 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13331 goto out; 13332 } 13333 13334 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13335 opt[DTRACEOPT_AGGSIZE] != 0) { 13336 if (state->dts_aggregations == NULL) { 13337 /* 13338 * We're not going to create an aggregation buffer 13339 * because we don't have any ECBs that contain 13340 * aggregations -- set this option to 0. 13341 */ 13342 opt[DTRACEOPT_AGGSIZE] = 0; 13343 } else { 13344 /* 13345 * If we have an aggregation buffer, we must also have 13346 * a buffer to use as scratch. 13347 */ 13348 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13349 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13350 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13351 } 13352 } 13353 } 13354 13355 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13356 opt[DTRACEOPT_SPECSIZE] != 0) { 13357 if (!state->dts_speculates) { 13358 /* 13359 * We're not going to create speculation buffers 13360 * because we don't have any ECBs that actually 13361 * speculate -- set the speculation size to 0. 13362 */ 13363 opt[DTRACEOPT_SPECSIZE] = 0; 13364 } 13365 } 13366 13367 /* 13368 * The bare minimum size for any buffer that we're actually going to 13369 * do anything to is sizeof (uint64_t). 13370 */ 13371 sz = sizeof (uint64_t); 13372 13373 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13374 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13375 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13376 /* 13377 * A buffer size has been explicitly set to 0 (or to a size 13378 * that will be adjusted to 0) and we need the space -- we 13379 * need to return failure. We return ENOSPC to differentiate 13380 * it from failing to allocate a buffer due to failure to meet 13381 * the reserve (for which we return E2BIG). 13382 */ 13383 rval = ENOSPC; 13384 goto out; 13385 } 13386 13387 if ((rval = dtrace_state_buffers(state)) != 0) 13388 goto err; 13389 13390 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13391 sz = dtrace_dstate_defsize; 13392 13393 do { 13394 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13395 13396 if (rval == 0) 13397 break; 13398 13399 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13400 goto err; 13401 } while (sz >>= 1); 13402 13403 opt[DTRACEOPT_DYNVARSIZE] = sz; 13404 13405 if (rval != 0) 13406 goto err; 13407 13408 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13409 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13410 13411 if (opt[DTRACEOPT_CLEANRATE] == 0) 13412 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13413 13414 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13415 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13416 13417 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13418 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13419 13420 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13421#if defined(sun) 13422 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13423 hdlr.cyh_arg = state; 13424 hdlr.cyh_level = CY_LOW_LEVEL; 13425 13426 when.cyt_when = 0; 13427 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13428 13429 state->dts_cleaner = cyclic_add(&hdlr, &when); 13430 13431 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13432 hdlr.cyh_arg = state; 13433 hdlr.cyh_level = CY_LOW_LEVEL; 13434 13435 when.cyt_when = 0; 13436 when.cyt_interval = dtrace_deadman_interval; 13437 13438 state->dts_deadman = cyclic_add(&hdlr, &when); 13439#else 13440 state->dts_cleaner = dtrace_state_worker_add( 13441 dtrace_state_clean, state, opt[DTRACEOPT_CLEANRATE]); 13442 state->dts_deadman = dtrace_state_worker_add( 13443 dtrace_state_deadman, state, dtrace_deadman_interval); 13444#endif 13445 13446 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13447 13448 /* 13449 * Now it's time to actually fire the BEGIN probe. We need to disable 13450 * interrupts here both to record the CPU on which we fired the BEGIN 13451 * probe (the data from this CPU will be processed first at user 13452 * level) and to manually activate the buffer for this CPU. 13453 */ 13454 cookie = dtrace_interrupt_disable(); 13455 *xcpu = curcpu_id; 13456 ASSERT(state->dts_buffer[*xcpu].dtb_flags & DTRACEBUF_INACTIVE); 13457 state->dts_buffer[*xcpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13458 13459 dtrace_probe(dtrace_probeid_begin, 13460 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13461 dtrace_interrupt_enable(cookie); 13462 /* 13463 * We may have had an exit action from a BEGIN probe; only change our 13464 * state to ACTIVE if we're still in WARMUP. 13465 */ 13466 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13467 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13468 13469 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13470 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13471 13472 /* 13473 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13474 * want each CPU to transition its principal buffer out of the 13475 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13476 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13477 * atomically transition from processing none of a state's ECBs to 13478 * processing all of them. 13479 */ 13480 dtrace_xcall(DTRACE_CPUALL, 13481 (dtrace_xcall_t)dtrace_buffer_activate, state); 13482 goto out; 13483 13484err: 13485 dtrace_buffer_free(state->dts_buffer); 13486 dtrace_buffer_free(state->dts_aggbuffer); 13487 13488 if ((nspec = state->dts_nspeculations) == 0) { 13489 ASSERT(state->dts_speculations == NULL); 13490 goto out; 13491 } 13492 13493 spec = state->dts_speculations; 13494 ASSERT(spec != NULL); 13495 13496 for (i = 0; i < state->dts_nspeculations; i++) { 13497 if ((buf = spec[i].dtsp_buffer) == NULL) 13498 break; 13499 13500 dtrace_buffer_free(buf); 13501 kmem_free(buf, bufsize); 13502 } 13503 13504 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13505 state->dts_nspeculations = 0; 13506 state->dts_speculations = NULL; 13507 13508out: 13509 mutex_exit(&dtrace_lock); 13510 mutex_exit(&cpu_lock); 13511 13512 return (rval); 13513} 13514 13515static int 13516dtrace_state_stop(dtrace_state_t *state, processorid_t *xcpu) 13517{ 13518 dtrace_icookie_t cookie; 13519 13520 ASSERT(MUTEX_HELD(&dtrace_lock)); 13521 13522 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13523 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13524 return (EINVAL); 13525 13526 /* 13527 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13528 * to be sure that every CPU has seen it. See below for the details 13529 * on why this is done. 13530 */ 13531 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13532 dtrace_sync(); 13533 13534 /* 13535 * By this point, it is impossible for any CPU to be still processing 13536 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13537 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13538 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13539 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13540 * iff we're in the END probe. 13541 */ 13542 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13543 dtrace_sync(); 13544 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13545 13546 /* 13547 * Finally, we can release the reserve and call the END probe. We 13548 * disable interrupts across calling the END probe to allow us to 13549 * return the CPU on which we actually called the END probe. This 13550 * allows user-land to be sure that this CPU's principal buffer is 13551 * processed last. 13552 */ 13553 state->dts_reserve = 0; 13554 13555 cookie = dtrace_interrupt_disable(); 13556 *xcpu = curcpu_id; 13557 dtrace_probe(dtrace_probeid_end, 13558 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13559 dtrace_interrupt_enable(cookie); 13560 13561 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13562 dtrace_sync(); 13563 13564 return (0); 13565} 13566 13567static int 13568dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13569 dtrace_optval_t val) 13570{ 13571 ASSERT(MUTEX_HELD(&dtrace_lock)); 13572 13573 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13574 return (EBUSY); 13575 13576 if (option >= DTRACEOPT_MAX) 13577 return (EINVAL); 13578 13579 if (option != DTRACEOPT_CPU && val < 0) 13580 return (EINVAL); 13581 13582 switch (option) { 13583 case DTRACEOPT_DESTRUCTIVE: 13584 if (dtrace_destructive_disallow) 13585 return (EACCES); 13586 13587 state->dts_cred.dcr_destructive = 1; 13588 break; 13589 13590 case DTRACEOPT_BUFSIZE: 13591 case DTRACEOPT_DYNVARSIZE: 13592 case DTRACEOPT_AGGSIZE: 13593 case DTRACEOPT_SPECSIZE: 13594 case DTRACEOPT_STRSIZE: 13595 if (val < 0) 13596 return (EINVAL); 13597 13598 if (val >= LONG_MAX) { 13599 /* 13600 * If this is an otherwise negative value, set it to 13601 * the highest multiple of 128m less than LONG_MAX. 13602 * Technically, we're adjusting the size without 13603 * regard to the buffer resizing policy, but in fact, 13604 * this has no effect -- if we set the buffer size to 13605 * ~LONG_MAX and the buffer policy is ultimately set to 13606 * be "manual", the buffer allocation is guaranteed to 13607 * fail, if only because the allocation requires two 13608 * buffers. (We set the the size to the highest 13609 * multiple of 128m because it ensures that the size 13610 * will remain a multiple of a megabyte when 13611 * repeatedly halved -- all the way down to 15m.) 13612 */ 13613 val = LONG_MAX - (1 << 27) + 1; 13614 } 13615 } 13616 13617 state->dts_options[option] = val; 13618 13619 return (0); 13620} 13621 13622static void 13623dtrace_state_destroy(dtrace_state_t *state) 13624{ 13625 dtrace_ecb_t *ecb; 13626 dtrace_vstate_t *vstate = &state->dts_vstate; 13627#if defined(sun) 13628 minor_t minor = getminor(state->dts_dev); 13629#endif 13630 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13631 dtrace_speculation_t *spec = state->dts_speculations; 13632 int nspec = state->dts_nspeculations; 13633 uint32_t match; 13634 13635 ASSERT(MUTEX_HELD(&dtrace_lock)); 13636 ASSERT(MUTEX_HELD(&cpu_lock)); 13637 13638 /* 13639 * First, retract any retained enablings for this state. 13640 */ 13641 dtrace_enabling_retract(state); 13642 ASSERT(state->dts_nretained == 0); 13643 13644 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13645 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13646 /* 13647 * We have managed to come into dtrace_state_destroy() on a 13648 * hot enabling -- almost certainly because of a disorderly 13649 * shutdown of a consumer. (That is, a consumer that is 13650 * exiting without having called dtrace_stop().) In this case, 13651 * we're going to set our activity to be KILLED, and then 13652 * issue a sync to be sure that everyone is out of probe 13653 * context before we start blowing away ECBs. 13654 */ 13655 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13656 dtrace_sync(); 13657 } 13658 13659 /* 13660 * Release the credential hold we took in dtrace_state_create(). 13661 */ 13662 if (state->dts_cred.dcr_cred != NULL) { 13663#if defined(sun) 13664 crfree(state->dts_cred.dcr_cred); 13665#else 13666 kauth_cred_free(state->dts_cred.dcr_cred); 13667#endif 13668 } 13669 13670 /* 13671 * Now we can safely disable and destroy any enabled probes. Because 13672 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13673 * (especially if they're all enabled), we take two passes through the 13674 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13675 * in the second we disable whatever is left over. 13676 */ 13677 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13678 for (i = 0; i < state->dts_necbs; i++) { 13679 if ((ecb = state->dts_ecbs[i]) == NULL) 13680 continue; 13681 13682 if (match && ecb->dte_probe != NULL) { 13683 dtrace_probe_t *probe = ecb->dte_probe; 13684 dtrace_provider_t *prov = probe->dtpr_provider; 13685 13686 if (!(prov->dtpv_priv.dtpp_flags & match)) 13687 continue; 13688 } 13689 13690 dtrace_ecb_disable(ecb); 13691 dtrace_ecb_destroy(ecb); 13692 } 13693 13694 if (!match) 13695 break; 13696 } 13697 13698 /* 13699 * Before we free the buffers, perform one more sync to assure that 13700 * every CPU is out of probe context. 13701 */ 13702 dtrace_sync(); 13703 13704 dtrace_buffer_free(state->dts_buffer); 13705 dtrace_buffer_free(state->dts_aggbuffer); 13706 13707 for (i = 0; i < nspec; i++) 13708 dtrace_buffer_free(spec[i].dtsp_buffer); 13709 13710#if defined(sun) 13711 if (state->dts_cleaner != CYCLIC_NONE) 13712 cyclic_remove(state->dts_cleaner); 13713 13714 if (state->dts_deadman != CYCLIC_NONE) 13715 cyclic_remove(state->dts_deadman); 13716#else 13717 if (state->dts_cleaner != NULL) 13718 dtrace_state_worker_remove(state->dts_cleaner); 13719 13720 if (state->dts_deadman != NULL) 13721 dtrace_state_worker_remove(state->dts_deadman); 13722#endif 13723 13724 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13725 dtrace_vstate_fini(vstate); 13726 if (state->dts_ecbs != NULL) 13727 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13728 13729 if (state->dts_aggregations != NULL) { 13730#ifdef DEBUG 13731 for (i = 0; i < state->dts_naggregations; i++) 13732 ASSERT(state->dts_aggregations[i] == NULL); 13733#endif 13734 ASSERT(state->dts_naggregations > 0); 13735 kmem_free(state->dts_aggregations, 13736 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13737 } 13738 13739 kmem_free(state->dts_buffer, bufsize); 13740 kmem_free(state->dts_aggbuffer, bufsize); 13741 13742 for (i = 0; i < nspec; i++) 13743 kmem_free(spec[i].dtsp_buffer, bufsize); 13744 13745 if (spec != NULL) 13746 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13747 13748 dtrace_format_destroy(state); 13749 13750 if (state->dts_aggid_arena != NULL) { 13751 vmem_destroy(state->dts_aggid_arena); 13752 state->dts_aggid_arena = NULL; 13753 } 13754#if defined(sun) 13755 ddi_soft_state_free(dtrace_softstate, minor); 13756 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13757#else 13758 kmem_free(state, sizeof(dtrace_state_t)); 13759#endif 13760} 13761 13762/* 13763 * DTrace Anonymous Enabling Functions 13764 */ 13765static dtrace_state_t * 13766dtrace_anon_grab(void) 13767{ 13768 dtrace_state_t *state; 13769 13770 ASSERT(MUTEX_HELD(&dtrace_lock)); 13771 13772 if ((state = dtrace_anon.dta_state) == NULL) { 13773 ASSERT(dtrace_anon.dta_enabling == NULL); 13774 return (NULL); 13775 } 13776 13777 ASSERT(dtrace_anon.dta_enabling != NULL); 13778 ASSERT(dtrace_retained != NULL); 13779 13780 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 13781 dtrace_anon.dta_enabling = NULL; 13782 dtrace_anon.dta_state = NULL; 13783 13784 return (state); 13785} 13786 13787static void 13788dtrace_anon_property(void) 13789{ 13790 int i, rv; 13791 dtrace_state_t *state; 13792 dof_hdr_t *dof; 13793 char c[32]; /* enough for "dof-data-" + digits */ 13794 13795 ASSERT(MUTEX_HELD(&dtrace_lock)); 13796 ASSERT(MUTEX_HELD(&cpu_lock)); 13797 13798 for (i = 0; ; i++) { 13799 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 13800 13801 dtrace_err_verbose = 1; 13802 13803 if ((dof = dtrace_dof_property(c)) == NULL) { 13804 dtrace_err_verbose = 0; 13805 break; 13806 } 13807 13808#if defined(sun) 13809 /* 13810 * We want to create anonymous state, so we need to transition 13811 * the kernel debugger to indicate that DTrace is active. If 13812 * this fails (e.g. because the debugger has modified text in 13813 * some way), we won't continue with the processing. 13814 */ 13815 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13816 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 13817 "enabling ignored."); 13818 dtrace_dof_destroy(dof); 13819 break; 13820 } 13821#endif 13822 13823 /* 13824 * If we haven't allocated an anonymous state, we'll do so now. 13825 */ 13826 if ((state = dtrace_anon.dta_state) == NULL) { 13827#if defined(sun) 13828 state = dtrace_state_create(NULL, NULL); 13829#endif 13830 dtrace_anon.dta_state = state; 13831 13832 if (state == NULL) { 13833 /* 13834 * This basically shouldn't happen: the only 13835 * failure mode from dtrace_state_create() is a 13836 * failure of ddi_soft_state_zalloc() that 13837 * itself should never happen. Still, the 13838 * interface allows for a failure mode, and 13839 * we want to fail as gracefully as possible: 13840 * we'll emit an error message and cease 13841 * processing anonymous state in this case. 13842 */ 13843 cmn_err(CE_WARN, "failed to create " 13844 "anonymous state"); 13845 dtrace_dof_destroy(dof); 13846 break; 13847 } 13848 } 13849 13850 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13851 &dtrace_anon.dta_enabling, 0, B_TRUE); 13852 13853 if (rv == 0) 13854 rv = dtrace_dof_options(dof, state); 13855 13856 dtrace_err_verbose = 0; 13857 dtrace_dof_destroy(dof); 13858 13859 if (rv != 0) { 13860 /* 13861 * This is malformed DOF; chuck any anonymous state 13862 * that we created. 13863 */ 13864 ASSERT(dtrace_anon.dta_enabling == NULL); 13865 dtrace_state_destroy(state); 13866 dtrace_anon.dta_state = NULL; 13867 break; 13868 } 13869 13870 ASSERT(dtrace_anon.dta_enabling != NULL); 13871 } 13872 13873 if (dtrace_anon.dta_enabling != NULL) { 13874 int rval; 13875 13876 /* 13877 * dtrace_enabling_retain() can only fail because we are 13878 * trying to retain more enablings than are allowed -- but 13879 * we only have one anonymous enabling, and we are guaranteed 13880 * to be allowed at least one retained enabling; we assert 13881 * that dtrace_enabling_retain() returns success. 13882 */ 13883 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13884 ASSERT(rval == 0); 13885 13886 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13887 } 13888} 13889 13890#if defined(sun) 13891/* 13892 * DTrace Helper Functions 13893 */ 13894static void 13895dtrace_helper_trace(dtrace_helper_action_t *helper, 13896 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13897{ 13898 uint32_t size, next, nnext, i; 13899 dtrace_helptrace_t *ent; 13900 uint16_t flags = cpu_core[curcpu_id].cpuc_dtrace_flags; 13901 13902 if (!dtrace_helptrace_enabled) 13903 return; 13904 13905 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13906 13907 /* 13908 * What would a tracing framework be without its own tracing 13909 * framework? (Well, a hell of a lot simpler, for starters...) 13910 */ 13911 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13912 sizeof (uint64_t) - sizeof (uint64_t); 13913 13914 /* 13915 * Iterate until we can allocate a slot in the trace buffer. 13916 */ 13917 do { 13918 next = dtrace_helptrace_next; 13919 13920 if (next + size < dtrace_helptrace_bufsize) { 13921 nnext = next + size; 13922 } else { 13923 nnext = size; 13924 } 13925 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13926 13927 /* 13928 * We have our slot; fill it in. 13929 */ 13930 if (nnext == size) 13931 next = 0; 13932 13933 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 13934 ent->dtht_helper = helper; 13935 ent->dtht_where = where; 13936 ent->dtht_nlocals = vstate->dtvs_nlocals; 13937 13938 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13939 mstate->dtms_fltoffs : -1; 13940 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13941 ent->dtht_illval = cpu_core[curcpu_id].cpuc_dtrace_illval; 13942 13943 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13944 dtrace_statvar_t *svar; 13945 13946 if ((svar = vstate->dtvs_locals[i]) == NULL) 13947 continue; 13948 13949 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13950 ent->dtht_locals[i] = 13951 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu_id]; 13952 } 13953} 13954#endif 13955 13956#if defined(sun) 13957static uint64_t 13958dtrace_helper(int which, dtrace_mstate_t *mstate, 13959 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13960{ 13961 uint16_t *flags = &cpu_core[curcpu_id].cpuc_dtrace_flags; 13962 uint64_t sarg0 = mstate->dtms_arg[0]; 13963 uint64_t sarg1 = mstate->dtms_arg[1]; 13964 uint64_t rval; 13965 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13966 dtrace_helper_action_t *helper; 13967 dtrace_vstate_t *vstate; 13968 dtrace_difo_t *pred; 13969 int i, trace = dtrace_helptrace_enabled; 13970 13971 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13972 13973 if (helpers == NULL) 13974 return (0); 13975 13976 if ((helper = helpers->dthps_actions[which]) == NULL) 13977 return (0); 13978 13979 vstate = &helpers->dthps_vstate; 13980 mstate->dtms_arg[0] = arg0; 13981 mstate->dtms_arg[1] = arg1; 13982 13983 /* 13984 * Now iterate over each helper. If its predicate evaluates to 'true', 13985 * we'll call the corresponding actions. Note that the below calls 13986 * to dtrace_dif_emulate() may set faults in machine state. This is 13987 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13988 * the stored DIF offset with its own (which is the desired behavior). 13989 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13990 * from machine state; this is okay, too. 13991 */ 13992 for (; helper != NULL; helper = helper->dtha_next) { 13993 if ((pred = helper->dtha_predicate) != NULL) { 13994 if (trace) 13995 dtrace_helper_trace(helper, mstate, vstate, 0); 13996 13997 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13998 goto next; 13999 14000 if (*flags & CPU_DTRACE_FAULT) 14001 goto err; 14002 } 14003 14004 for (i = 0; i < helper->dtha_nactions; i++) { 14005 if (trace) 14006 dtrace_helper_trace(helper, 14007 mstate, vstate, i + 1); 14008 14009 rval = dtrace_dif_emulate(helper->dtha_actions[i], 14010 mstate, vstate, state); 14011 14012 if (*flags & CPU_DTRACE_FAULT) 14013 goto err; 14014 } 14015 14016next: 14017 if (trace) 14018 dtrace_helper_trace(helper, mstate, vstate, 14019 DTRACE_HELPTRACE_NEXT); 14020 } 14021 14022 if (trace) 14023 dtrace_helper_trace(helper, mstate, vstate, 14024 DTRACE_HELPTRACE_DONE); 14025 14026 /* 14027 * Restore the arg0 that we saved upon entry. 14028 */ 14029 mstate->dtms_arg[0] = sarg0; 14030 mstate->dtms_arg[1] = sarg1; 14031 14032 return (rval); 14033 14034err: 14035 if (trace) 14036 dtrace_helper_trace(helper, mstate, vstate, 14037 DTRACE_HELPTRACE_ERR); 14038 14039 /* 14040 * Restore the arg0 that we saved upon entry. 14041 */ 14042 mstate->dtms_arg[0] = sarg0; 14043 mstate->dtms_arg[1] = sarg1; 14044 14045 return (0); 14046} 14047 14048static void 14049dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 14050 dtrace_vstate_t *vstate) 14051{ 14052 int i; 14053 14054 if (helper->dtha_predicate != NULL) 14055 dtrace_difo_release(helper->dtha_predicate, vstate); 14056 14057 for (i = 0; i < helper->dtha_nactions; i++) { 14058 ASSERT(helper->dtha_actions[i] != NULL); 14059 dtrace_difo_release(helper->dtha_actions[i], vstate); 14060 } 14061 14062 kmem_free(helper->dtha_actions, 14063 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 14064 kmem_free(helper, sizeof (dtrace_helper_action_t)); 14065} 14066 14067static int 14068dtrace_helper_destroygen(int gen) 14069{ 14070 proc_t *p = curproc; 14071 dtrace_helpers_t *help = p->p_dtrace_helpers; 14072 dtrace_vstate_t *vstate; 14073 int i; 14074 14075 ASSERT(MUTEX_HELD(&dtrace_lock)); 14076 14077 if (help == NULL || gen > help->dthps_generation) 14078 return (EINVAL); 14079 14080 vstate = &help->dthps_vstate; 14081 14082 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14083 dtrace_helper_action_t *last = NULL, *h, *next; 14084 14085 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14086 next = h->dtha_next; 14087 14088 if (h->dtha_generation == gen) { 14089 if (last != NULL) { 14090 last->dtha_next = next; 14091 } else { 14092 help->dthps_actions[i] = next; 14093 } 14094 14095 dtrace_helper_action_destroy(h, vstate); 14096 } else { 14097 last = h; 14098 } 14099 } 14100 } 14101 14102 /* 14103 * Interate until we've cleared out all helper providers with the 14104 * given generation number. 14105 */ 14106 for (;;) { 14107 dtrace_helper_provider_t *prov; 14108 14109 /* 14110 * Look for a helper provider with the right generation. We 14111 * have to start back at the beginning of the list each time 14112 * because we drop dtrace_lock. It's unlikely that we'll make 14113 * more than two passes. 14114 */ 14115 for (i = 0; i < help->dthps_nprovs; i++) { 14116 prov = help->dthps_provs[i]; 14117 14118 if (prov->dthp_generation == gen) 14119 break; 14120 } 14121 14122 /* 14123 * If there were no matches, we're done. 14124 */ 14125 if (i == help->dthps_nprovs) 14126 break; 14127 14128 /* 14129 * Move the last helper provider into this slot. 14130 */ 14131 help->dthps_nprovs--; 14132 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 14133 help->dthps_provs[help->dthps_nprovs] = NULL; 14134 14135 mutex_exit(&dtrace_lock); 14136 14137 /* 14138 * If we have a meta provider, remove this helper provider. 14139 */ 14140 mutex_enter(&dtrace_meta_lock); 14141 if (dtrace_meta_pid != NULL) { 14142 ASSERT(dtrace_deferred_pid == NULL); 14143 dtrace_helper_provider_remove(&prov->dthp_prov, 14144 p->p_pid); 14145 } 14146 mutex_exit(&dtrace_meta_lock); 14147 14148 dtrace_helper_provider_destroy(prov); 14149 14150 mutex_enter(&dtrace_lock); 14151 } 14152 14153 return (0); 14154} 14155#endif 14156 14157#if defined(sun) 14158static int 14159dtrace_helper_validate(dtrace_helper_action_t *helper) 14160{ 14161 int err = 0, i; 14162 dtrace_difo_t *dp; 14163 14164 if ((dp = helper->dtha_predicate) != NULL) 14165 err += dtrace_difo_validate_helper(dp); 14166 14167 for (i = 0; i < helper->dtha_nactions; i++) 14168 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 14169 14170 return (err == 0); 14171} 14172#endif 14173 14174#if defined(sun) 14175static int 14176dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 14177{ 14178 dtrace_helpers_t *help; 14179 dtrace_helper_action_t *helper, *last; 14180 dtrace_actdesc_t *act; 14181 dtrace_vstate_t *vstate; 14182 dtrace_predicate_t *pred; 14183 int count = 0, nactions = 0, i; 14184 14185 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 14186 return (EINVAL); 14187 14188 help = curproc->p_dtrace_helpers; 14189 last = help->dthps_actions[which]; 14190 vstate = &help->dthps_vstate; 14191 14192 for (count = 0; last != NULL; last = last->dtha_next) { 14193 count++; 14194 if (last->dtha_next == NULL) 14195 break; 14196 } 14197 14198 /* 14199 * If we already have dtrace_helper_actions_max helper actions for this 14200 * helper action type, we'll refuse to add a new one. 14201 */ 14202 if (count >= dtrace_helper_actions_max) 14203 return (ENOSPC); 14204 14205 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 14206 helper->dtha_generation = help->dthps_generation; 14207 14208 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 14209 ASSERT(pred->dtp_difo != NULL); 14210 dtrace_difo_hold(pred->dtp_difo); 14211 helper->dtha_predicate = pred->dtp_difo; 14212 } 14213 14214 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 14215 if (act->dtad_kind != DTRACEACT_DIFEXPR) 14216 goto err; 14217 14218 if (act->dtad_difo == NULL) 14219 goto err; 14220 14221 nactions++; 14222 } 14223 14224 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 14225 (helper->dtha_nactions = nactions), KM_SLEEP); 14226 14227 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 14228 dtrace_difo_hold(act->dtad_difo); 14229 helper->dtha_actions[i++] = act->dtad_difo; 14230 } 14231 14232 if (!dtrace_helper_validate(helper)) 14233 goto err; 14234 14235 if (last == NULL) { 14236 help->dthps_actions[which] = helper; 14237 } else { 14238 last->dtha_next = helper; 14239 } 14240 14241 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 14242 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 14243 dtrace_helptrace_next = 0; 14244 } 14245 14246 return (0); 14247err: 14248 dtrace_helper_action_destroy(helper, vstate); 14249 return (EINVAL); 14250} 14251 14252static void 14253dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 14254 dof_helper_t *dofhp) 14255{ 14256 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 14257 14258 mutex_enter(&dtrace_meta_lock); 14259 mutex_enter(&dtrace_lock); 14260 14261 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14262 /* 14263 * If the dtrace module is loaded but not attached, or if 14264 * there aren't isn't a meta provider registered to deal with 14265 * these provider descriptions, we need to postpone creating 14266 * the actual providers until later. 14267 */ 14268 14269 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14270 dtrace_deferred_pid != help) { 14271 help->dthps_deferred = 1; 14272 help->dthps_pid = p->p_pid; 14273 help->dthps_next = dtrace_deferred_pid; 14274 help->dthps_prev = NULL; 14275 if (dtrace_deferred_pid != NULL) 14276 dtrace_deferred_pid->dthps_prev = help; 14277 dtrace_deferred_pid = help; 14278 } 14279 14280 mutex_exit(&dtrace_lock); 14281 14282 } else if (dofhp != NULL) { 14283 /* 14284 * If the dtrace module is loaded and we have a particular 14285 * helper provider description, pass that off to the 14286 * meta provider. 14287 */ 14288 14289 mutex_exit(&dtrace_lock); 14290 14291 dtrace_helper_provide(dofhp, p->p_pid); 14292 14293 } else { 14294 /* 14295 * Otherwise, just pass all the helper provider descriptions 14296 * off to the meta provider. 14297 */ 14298 14299 int i; 14300 mutex_exit(&dtrace_lock); 14301 14302 for (i = 0; i < help->dthps_nprovs; i++) { 14303 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14304 p->p_pid); 14305 } 14306 } 14307 14308 mutex_exit(&dtrace_meta_lock); 14309} 14310 14311static int 14312dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14313{ 14314 dtrace_helpers_t *help; 14315 dtrace_helper_provider_t *hprov, **tmp_provs; 14316 uint_t tmp_maxprovs, i; 14317 14318 ASSERT(MUTEX_HELD(&dtrace_lock)); 14319 14320 help = curproc->p_dtrace_helpers; 14321 ASSERT(help != NULL); 14322 14323 /* 14324 * If we already have dtrace_helper_providers_max helper providers, 14325 * we're refuse to add a new one. 14326 */ 14327 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14328 return (ENOSPC); 14329 14330 /* 14331 * Check to make sure this isn't a duplicate. 14332 */ 14333 for (i = 0; i < help->dthps_nprovs; i++) { 14334 if (dofhp->dofhp_addr == 14335 help->dthps_provs[i]->dthp_prov.dofhp_addr) 14336 return (EALREADY); 14337 } 14338 14339 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14340 hprov->dthp_prov = *dofhp; 14341 hprov->dthp_ref = 1; 14342 hprov->dthp_generation = gen; 14343 14344 /* 14345 * Allocate a bigger table for helper providers if it's already full. 14346 */ 14347 if (help->dthps_maxprovs == help->dthps_nprovs) { 14348 tmp_maxprovs = help->dthps_maxprovs; 14349 tmp_provs = help->dthps_provs; 14350 14351 if (help->dthps_maxprovs == 0) 14352 help->dthps_maxprovs = 2; 14353 else 14354 help->dthps_maxprovs *= 2; 14355 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14356 help->dthps_maxprovs = dtrace_helper_providers_max; 14357 14358 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14359 14360 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14361 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14362 14363 if (tmp_provs != NULL) { 14364 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14365 sizeof (dtrace_helper_provider_t *)); 14366 kmem_free(tmp_provs, tmp_maxprovs * 14367 sizeof (dtrace_helper_provider_t *)); 14368 } 14369 } 14370 14371 help->dthps_provs[help->dthps_nprovs] = hprov; 14372 help->dthps_nprovs++; 14373 14374 return (0); 14375} 14376 14377static void 14378dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14379{ 14380 mutex_enter(&dtrace_lock); 14381 14382 if (--hprov->dthp_ref == 0) { 14383 dof_hdr_t *dof; 14384 mutex_exit(&dtrace_lock); 14385 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14386 dtrace_dof_destroy(dof); 14387 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14388 } else { 14389 mutex_exit(&dtrace_lock); 14390 } 14391} 14392 14393static int 14394dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14395{ 14396 uintptr_t daddr = (uintptr_t)dof; 14397 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14398 dof_provider_t *provider; 14399 dof_probe_t *probe; 14400 uint8_t *arg; 14401 char *strtab, *typestr; 14402 dof_stridx_t typeidx; 14403 size_t typesz; 14404 uint_t nprobes, j, k; 14405 14406 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14407 14408 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14409 dtrace_dof_error(dof, "misaligned section offset"); 14410 return (-1); 14411 } 14412 14413 /* 14414 * The section needs to be large enough to contain the DOF provider 14415 * structure appropriate for the given version. 14416 */ 14417 if (sec->dofs_size < 14418 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14419 offsetof(dof_provider_t, dofpv_prenoffs) : 14420 sizeof (dof_provider_t))) { 14421 dtrace_dof_error(dof, "provider section too small"); 14422 return (-1); 14423 } 14424 14425 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14426 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14427 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14428 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14429 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14430 14431 if (str_sec == NULL || prb_sec == NULL || 14432 arg_sec == NULL || off_sec == NULL) 14433 return (-1); 14434 14435 enoff_sec = NULL; 14436 14437 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14438 provider->dofpv_prenoffs != DOF_SECT_NONE && 14439 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14440 provider->dofpv_prenoffs)) == NULL) 14441 return (-1); 14442 14443 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14444 14445 if (provider->dofpv_name >= str_sec->dofs_size || 14446 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14447 dtrace_dof_error(dof, "invalid provider name"); 14448 return (-1); 14449 } 14450 14451 if (prb_sec->dofs_entsize == 0 || 14452 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14453 dtrace_dof_error(dof, "invalid entry size"); 14454 return (-1); 14455 } 14456 14457 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14458 dtrace_dof_error(dof, "misaligned entry size"); 14459 return (-1); 14460 } 14461 14462 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14463 dtrace_dof_error(dof, "invalid entry size"); 14464 return (-1); 14465 } 14466 14467 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14468 dtrace_dof_error(dof, "misaligned section offset"); 14469 return (-1); 14470 } 14471 14472 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14473 dtrace_dof_error(dof, "invalid entry size"); 14474 return (-1); 14475 } 14476 14477 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14478 14479 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14480 14481 /* 14482 * Take a pass through the probes to check for errors. 14483 */ 14484 for (j = 0; j < nprobes; j++) { 14485 probe = (dof_probe_t *)(uintptr_t)(daddr + 14486 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14487 14488 if (probe->dofpr_func >= str_sec->dofs_size) { 14489 dtrace_dof_error(dof, "invalid function name"); 14490 return (-1); 14491 } 14492 14493 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14494 dtrace_dof_error(dof, "function name too long"); 14495 return (-1); 14496 } 14497 14498 if (probe->dofpr_name >= str_sec->dofs_size || 14499 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14500 dtrace_dof_error(dof, "invalid probe name"); 14501 return (-1); 14502 } 14503 14504 /* 14505 * The offset count must not wrap the index, and the offsets 14506 * must also not overflow the section's data. 14507 */ 14508 if (probe->dofpr_offidx + probe->dofpr_noffs < 14509 probe->dofpr_offidx || 14510 (probe->dofpr_offidx + probe->dofpr_noffs) * 14511 off_sec->dofs_entsize > off_sec->dofs_size) { 14512 dtrace_dof_error(dof, "invalid probe offset"); 14513 return (-1); 14514 } 14515 14516 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14517 /* 14518 * If there's no is-enabled offset section, make sure 14519 * there aren't any is-enabled offsets. Otherwise 14520 * perform the same checks as for probe offsets 14521 * (immediately above). 14522 */ 14523 if (enoff_sec == NULL) { 14524 if (probe->dofpr_enoffidx != 0 || 14525 probe->dofpr_nenoffs != 0) { 14526 dtrace_dof_error(dof, "is-enabled " 14527 "offsets with null section"); 14528 return (-1); 14529 } 14530 } else if (probe->dofpr_enoffidx + 14531 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14532 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14533 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14534 dtrace_dof_error(dof, "invalid is-enabled " 14535 "offset"); 14536 return (-1); 14537 } 14538 14539 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14540 dtrace_dof_error(dof, "zero probe and " 14541 "is-enabled offsets"); 14542 return (-1); 14543 } 14544 } else if (probe->dofpr_noffs == 0) { 14545 dtrace_dof_error(dof, "zero probe offsets"); 14546 return (-1); 14547 } 14548 14549 if (probe->dofpr_argidx + probe->dofpr_xargc < 14550 probe->dofpr_argidx || 14551 (probe->dofpr_argidx + probe->dofpr_xargc) * 14552 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14553 dtrace_dof_error(dof, "invalid args"); 14554 return (-1); 14555 } 14556 14557 typeidx = probe->dofpr_nargv; 14558 typestr = strtab + probe->dofpr_nargv; 14559 for (k = 0; k < probe->dofpr_nargc; k++) { 14560 if (typeidx >= str_sec->dofs_size) { 14561 dtrace_dof_error(dof, "bad " 14562 "native argument type"); 14563 return (-1); 14564 } 14565 14566 typesz = strlen(typestr) + 1; 14567 if (typesz > DTRACE_ARGTYPELEN) { 14568 dtrace_dof_error(dof, "native " 14569 "argument type too long"); 14570 return (-1); 14571 } 14572 typeidx += typesz; 14573 typestr += typesz; 14574 } 14575 14576 typeidx = probe->dofpr_xargv; 14577 typestr = strtab + probe->dofpr_xargv; 14578 for (k = 0; k < probe->dofpr_xargc; k++) { 14579 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14580 dtrace_dof_error(dof, "bad " 14581 "native argument index"); 14582 return (-1); 14583 } 14584 14585 if (typeidx >= str_sec->dofs_size) { 14586 dtrace_dof_error(dof, "bad " 14587 "translated argument type"); 14588 return (-1); 14589 } 14590 14591 typesz = strlen(typestr) + 1; 14592 if (typesz > DTRACE_ARGTYPELEN) { 14593 dtrace_dof_error(dof, "translated argument " 14594 "type too long"); 14595 return (-1); 14596 } 14597 14598 typeidx += typesz; 14599 typestr += typesz; 14600 } 14601 } 14602 14603 return (0); 14604} 14605 14606static int 14607dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14608{ 14609 dtrace_helpers_t *help; 14610 dtrace_vstate_t *vstate; 14611 dtrace_enabling_t *enab = NULL; 14612 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14613 uintptr_t daddr = (uintptr_t)dof; 14614 14615 ASSERT(MUTEX_HELD(&dtrace_lock)); 14616 14617 if ((help = curproc->p_dtrace_helpers) == NULL) 14618 help = dtrace_helpers_create(curproc); 14619 14620 vstate = &help->dthps_vstate; 14621 14622 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14623 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14624 dtrace_dof_destroy(dof); 14625 return (rv); 14626 } 14627 14628 /* 14629 * Look for helper providers and validate their descriptions. 14630 */ 14631 if (dhp != NULL) { 14632 for (i = 0; i < dof->dofh_secnum; i++) { 14633 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14634 dof->dofh_secoff + i * dof->dofh_secsize); 14635 14636 if (sec->dofs_type != DOF_SECT_PROVIDER) 14637 continue; 14638 14639 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14640 dtrace_enabling_destroy(enab); 14641 dtrace_dof_destroy(dof); 14642 return (-1); 14643 } 14644 14645 nprovs++; 14646 } 14647 } 14648 14649 /* 14650 * Now we need to walk through the ECB descriptions in the enabling. 14651 */ 14652 for (i = 0; i < enab->dten_ndesc; i++) { 14653 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14654 dtrace_probedesc_t *desc = &ep->dted_probe; 14655 14656 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14657 continue; 14658 14659 if (strcmp(desc->dtpd_mod, "helper") != 0) 14660 continue; 14661 14662 if (strcmp(desc->dtpd_func, "ustack") != 0) 14663 continue; 14664 14665 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14666 ep)) != 0) { 14667 /* 14668 * Adding this helper action failed -- we are now going 14669 * to rip out the entire generation and return failure. 14670 */ 14671 (void) dtrace_helper_destroygen(help->dthps_generation); 14672 dtrace_enabling_destroy(enab); 14673 dtrace_dof_destroy(dof); 14674 return (-1); 14675 } 14676 14677 nhelpers++; 14678 } 14679 14680 if (nhelpers < enab->dten_ndesc) 14681 dtrace_dof_error(dof, "unmatched helpers"); 14682 14683 gen = help->dthps_generation++; 14684 dtrace_enabling_destroy(enab); 14685 14686 if (dhp != NULL && nprovs > 0) { 14687 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14688 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14689 mutex_exit(&dtrace_lock); 14690 dtrace_helper_provider_register(curproc, help, dhp); 14691 mutex_enter(&dtrace_lock); 14692 14693 destroy = 0; 14694 } 14695 } 14696 14697 if (destroy) 14698 dtrace_dof_destroy(dof); 14699 14700 return (gen); 14701} 14702 14703static dtrace_helpers_t * 14704dtrace_helpers_create(proc_t *p) 14705{ 14706 dtrace_helpers_t *help; 14707 14708 ASSERT(MUTEX_HELD(&dtrace_lock)); 14709 ASSERT(p->p_dtrace_helpers == NULL); 14710 14711 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14712 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14713 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14714 14715 p->p_dtrace_helpers = help; 14716 dtrace_helpers++; 14717 14718 return (help); 14719} 14720 14721static void 14722dtrace_helpers_destroy(void) 14723{ 14724 dtrace_helpers_t *help; 14725 dtrace_vstate_t *vstate; 14726 proc_t *p = curproc; 14727 int i; 14728 14729 mutex_enter(&dtrace_lock); 14730 14731 ASSERT(p->p_dtrace_helpers != NULL); 14732 ASSERT(dtrace_helpers > 0); 14733 14734 help = p->p_dtrace_helpers; 14735 vstate = &help->dthps_vstate; 14736 14737 /* 14738 * We're now going to lose the help from this process. 14739 */ 14740 p->p_dtrace_helpers = NULL; 14741 dtrace_sync(); 14742 14743 /* 14744 * Destory the helper actions. 14745 */ 14746 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14747 dtrace_helper_action_t *h, *next; 14748 14749 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14750 next = h->dtha_next; 14751 dtrace_helper_action_destroy(h, vstate); 14752 h = next; 14753 } 14754 } 14755 14756 mutex_exit(&dtrace_lock); 14757 14758 /* 14759 * Destroy the helper providers. 14760 */ 14761 if (help->dthps_maxprovs > 0) { 14762 mutex_enter(&dtrace_meta_lock); 14763 if (dtrace_meta_pid != NULL) { 14764 ASSERT(dtrace_deferred_pid == NULL); 14765 14766 for (i = 0; i < help->dthps_nprovs; i++) { 14767 dtrace_helper_provider_remove( 14768 &help->dthps_provs[i]->dthp_prov, p->p_pid); 14769 } 14770 } else { 14771 mutex_enter(&dtrace_lock); 14772 ASSERT(help->dthps_deferred == 0 || 14773 help->dthps_next != NULL || 14774 help->dthps_prev != NULL || 14775 help == dtrace_deferred_pid); 14776 14777 /* 14778 * Remove the helper from the deferred list. 14779 */ 14780 if (help->dthps_next != NULL) 14781 help->dthps_next->dthps_prev = help->dthps_prev; 14782 if (help->dthps_prev != NULL) 14783 help->dthps_prev->dthps_next = help->dthps_next; 14784 if (dtrace_deferred_pid == help) { 14785 dtrace_deferred_pid = help->dthps_next; 14786 ASSERT(help->dthps_prev == NULL); 14787 } 14788 14789 mutex_exit(&dtrace_lock); 14790 } 14791 14792 mutex_exit(&dtrace_meta_lock); 14793 14794 for (i = 0; i < help->dthps_nprovs; i++) { 14795 dtrace_helper_provider_destroy(help->dthps_provs[i]); 14796 } 14797 14798 kmem_free(help->dthps_provs, help->dthps_maxprovs * 14799 sizeof (dtrace_helper_provider_t *)); 14800 } 14801 14802 mutex_enter(&dtrace_lock); 14803 14804 dtrace_vstate_fini(&help->dthps_vstate); 14805 kmem_free(help->dthps_actions, 14806 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 14807 kmem_free(help, sizeof (dtrace_helpers_t)); 14808 14809 --dtrace_helpers; 14810 mutex_exit(&dtrace_lock); 14811} 14812 14813static void 14814dtrace_helpers_duplicate(proc_t *from, proc_t *to) 14815{ 14816 dtrace_helpers_t *help, *newhelp; 14817 dtrace_helper_action_t *helper, *new, *last; 14818 dtrace_difo_t *dp; 14819 dtrace_vstate_t *vstate; 14820 int i, j, sz, hasprovs = 0; 14821 14822 mutex_enter(&dtrace_lock); 14823 ASSERT(from->p_dtrace_helpers != NULL); 14824 ASSERT(dtrace_helpers > 0); 14825 14826 help = from->p_dtrace_helpers; 14827 newhelp = dtrace_helpers_create(to); 14828 ASSERT(to->p_dtrace_helpers != NULL); 14829 14830 newhelp->dthps_generation = help->dthps_generation; 14831 vstate = &newhelp->dthps_vstate; 14832 14833 /* 14834 * Duplicate the helper actions. 14835 */ 14836 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14837 if ((helper = help->dthps_actions[i]) == NULL) 14838 continue; 14839 14840 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 14841 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 14842 KM_SLEEP); 14843 new->dtha_generation = helper->dtha_generation; 14844 14845 if ((dp = helper->dtha_predicate) != NULL) { 14846 dp = dtrace_difo_duplicate(dp, vstate); 14847 new->dtha_predicate = dp; 14848 } 14849 14850 new->dtha_nactions = helper->dtha_nactions; 14851 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14852 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14853 14854 for (j = 0; j < new->dtha_nactions; j++) { 14855 dtrace_difo_t *dp = helper->dtha_actions[j]; 14856 14857 ASSERT(dp != NULL); 14858 dp = dtrace_difo_duplicate(dp, vstate); 14859 new->dtha_actions[j] = dp; 14860 } 14861 14862 if (last != NULL) { 14863 last->dtha_next = new; 14864 } else { 14865 newhelp->dthps_actions[i] = new; 14866 } 14867 14868 last = new; 14869 } 14870 } 14871 14872 /* 14873 * Duplicate the helper providers and register them with the 14874 * DTrace framework. 14875 */ 14876 if (help->dthps_nprovs > 0) { 14877 newhelp->dthps_nprovs = help->dthps_nprovs; 14878 newhelp->dthps_maxprovs = help->dthps_nprovs; 14879 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14880 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14881 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14882 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14883 newhelp->dthps_provs[i]->dthp_ref++; 14884 } 14885 14886 hasprovs = 1; 14887 } 14888 14889 mutex_exit(&dtrace_lock); 14890 14891 if (hasprovs) 14892 dtrace_helper_provider_register(to, newhelp, NULL); 14893} 14894#endif 14895 14896#if defined(sun) 14897/* 14898 * DTrace Hook Functions 14899 */ 14900static void 14901dtrace_module_loaded(modctl_t *ctl) 14902{ 14903 dtrace_provider_t *prv; 14904 14905 mutex_enter(&dtrace_provider_lock); 14906 mutex_enter(&mod_lock); 14907 14908 ASSERT(ctl->mod_busy); 14909 14910 /* 14911 * We're going to call each providers per-module provide operation 14912 * specifying only this module. 14913 */ 14914 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14915 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14916 14917 mutex_exit(&mod_lock); 14918 mutex_exit(&dtrace_provider_lock); 14919 14920 /* 14921 * If we have any retained enablings, we need to match against them. 14922 * Enabling probes requires that cpu_lock be held, and we cannot hold 14923 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14924 * module. (In particular, this happens when loading scheduling 14925 * classes.) So if we have any retained enablings, we need to dispatch 14926 * our task queue to do the match for us. 14927 */ 14928 mutex_enter(&dtrace_lock); 14929 14930 if (dtrace_retained == NULL) { 14931 mutex_exit(&dtrace_lock); 14932 return; 14933 } 14934 14935 (void) taskq_dispatch(dtrace_taskq, 14936 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14937 14938 mutex_exit(&dtrace_lock); 14939 14940 /* 14941 * And now, for a little heuristic sleaze: in general, we want to 14942 * match modules as soon as they load. However, we cannot guarantee 14943 * this, because it would lead us to the lock ordering violation 14944 * outlined above. The common case, of course, is that cpu_lock is 14945 * _not_ held -- so we delay here for a clock tick, hoping that that's 14946 * long enough for the task queue to do its work. If it's not, it's 14947 * not a serious problem -- it just means that the module that we 14948 * just loaded may not be immediately instrumentable. 14949 */ 14950 delay(1); 14951} 14952 14953static void 14954dtrace_module_unloaded(modctl_t *ctl) 14955{ 14956 dtrace_probe_t template, *probe, *first, *next; 14957 dtrace_provider_t *prov; 14958 14959 template.dtpr_mod = ctl->mod_modname; 14960 14961 mutex_enter(&dtrace_provider_lock); 14962 mutex_enter(&mod_lock); 14963 mutex_enter(&dtrace_lock); 14964 14965 if (dtrace_bymod == NULL) { 14966 /* 14967 * The DTrace module is loaded (obviously) but not attached; 14968 * we don't have any work to do. 14969 */ 14970 mutex_exit(&dtrace_provider_lock); 14971 mutex_exit(&mod_lock); 14972 mutex_exit(&dtrace_lock); 14973 return; 14974 } 14975 14976 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14977 probe != NULL; probe = probe->dtpr_nextmod) { 14978 if (probe->dtpr_ecb != NULL) { 14979 mutex_exit(&dtrace_provider_lock); 14980 mutex_exit(&mod_lock); 14981 mutex_exit(&dtrace_lock); 14982 14983 /* 14984 * This shouldn't _actually_ be possible -- we're 14985 * unloading a module that has an enabled probe in it. 14986 * (It's normally up to the provider to make sure that 14987 * this can't happen.) However, because dtps_enable() 14988 * doesn't have a failure mode, there can be an 14989 * enable/unload race. Upshot: we don't want to 14990 * assert, but we're not going to disable the 14991 * probe, either. 14992 */ 14993 if (dtrace_err_verbose) { 14994 cmn_err(CE_WARN, "unloaded module '%s' had " 14995 "enabled probes", ctl->mod_modname); 14996 } 14997 14998 return; 14999 } 15000 } 15001 15002 probe = first; 15003 15004 for (first = NULL; probe != NULL; probe = next) { 15005 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 15006 15007 dtrace_probes[probe->dtpr_id - 1] = NULL; 15008 15009 next = probe->dtpr_nextmod; 15010 dtrace_hash_remove(dtrace_bymod, probe); 15011 dtrace_hash_remove(dtrace_byfunc, probe); 15012 dtrace_hash_remove(dtrace_byname, probe); 15013 15014 if (first == NULL) { 15015 first = probe; 15016 probe->dtpr_nextmod = NULL; 15017 } else { 15018 probe->dtpr_nextmod = first; 15019 first = probe; 15020 } 15021 } 15022 15023 /* 15024 * We've removed all of the module's probes from the hash chains and 15025 * from the probe array. Now issue a dtrace_sync() to be sure that 15026 * everyone has cleared out from any probe array processing. 15027 */ 15028 dtrace_sync(); 15029 15030 for (probe = first; probe != NULL; probe = first) { 15031 first = probe->dtpr_nextmod; 15032 prov = probe->dtpr_provider; 15033 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 15034 probe->dtpr_arg); 15035 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 15036 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 15037 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 15038 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 15039 kmem_free(probe, sizeof (dtrace_probe_t)); 15040 } 15041 15042 mutex_exit(&dtrace_lock); 15043 mutex_exit(&mod_lock); 15044 mutex_exit(&dtrace_provider_lock); 15045} 15046 15047static void 15048dtrace_suspend(void) 15049{ 15050 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 15051} 15052 15053static void 15054dtrace_resume(void) 15055{ 15056 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 15057} 15058#endif 15059 15060static int 15061dtrace_cpu_setup(cpu_setup_t what, processorid_t xcpu) 15062{ 15063 ASSERT(MUTEX_HELD(&cpu_lock)); 15064 mutex_enter(&dtrace_lock); 15065 15066 switch (what) { 15067 case CPU_CONFIG: { 15068 dtrace_state_t *state; 15069 dtrace_optval_t *opt, rs, c; 15070 15071 /* 15072 * For now, we only allocate a new buffer for anonymous state. 15073 */ 15074 if ((state = dtrace_anon.dta_state) == NULL) 15075 break; 15076 15077 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 15078 break; 15079 15080 opt = state->dts_options; 15081 c = opt[DTRACEOPT_CPU]; 15082 15083 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 15084 break; 15085 15086 /* 15087 * Regardless of what the actual policy is, we're going to 15088 * temporarily set our resize policy to be manual. We're 15089 * also going to temporarily set our CPU option to denote 15090 * the newly configured CPU. 15091 */ 15092 rs = opt[DTRACEOPT_BUFRESIZE]; 15093 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 15094 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 15095 15096 (void) dtrace_state_buffers(state); 15097 15098 opt[DTRACEOPT_BUFRESIZE] = rs; 15099 opt[DTRACEOPT_CPU] = c; 15100 15101 break; 15102 } 15103 15104 case CPU_UNCONFIG: 15105 /* 15106 * We don't free the buffer in the CPU_UNCONFIG case. (The 15107 * buffer will be freed when the consumer exits.) 15108 */ 15109 break; 15110 15111 default: 15112 break; 15113 } 15114 15115 mutex_exit(&dtrace_lock); 15116 return (0); 15117} 15118 15119#if defined(sun) 15120static void 15121dtrace_cpu_setup_initial(processorid_t cpu) 15122{ 15123 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 15124} 15125#endif 15126 15127static void 15128dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 15129{ 15130 if (dtrace_toxranges >= dtrace_toxranges_max) { 15131 int osize, nsize; 15132 dtrace_toxrange_t *range; 15133 15134 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15135 15136 if (osize == 0) { 15137 ASSERT(dtrace_toxrange == NULL); 15138 ASSERT(dtrace_toxranges_max == 0); 15139 dtrace_toxranges_max = 1; 15140 } else { 15141 dtrace_toxranges_max <<= 1; 15142 } 15143 15144 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 15145 range = kmem_zalloc(nsize, KM_SLEEP); 15146 15147 if (dtrace_toxrange != NULL) { 15148 ASSERT(osize != 0); 15149 bcopy(dtrace_toxrange, range, osize); 15150 kmem_free(dtrace_toxrange, osize); 15151 } 15152 15153 dtrace_toxrange = range; 15154 } 15155 15156 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 15157 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 15158 15159 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 15160 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 15161 dtrace_toxranges++; 15162} 15163 15164/* 15165 * DTrace Driver Cookbook Functions 15166 */ 15167#if defined(sun) 15168/*ARGSUSED*/ 15169static int 15170dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 15171{ 15172 dtrace_provider_id_t id; 15173 dtrace_state_t *state = NULL; 15174 dtrace_enabling_t *enab; 15175 15176 mutex_enter(&cpu_lock); 15177 mutex_enter(&dtrace_provider_lock); 15178 mutex_enter(&dtrace_lock); 15179 15180 if (ddi_soft_state_init(&dtrace_softstate, 15181 sizeof (dtrace_state_t), 0) != 0) { 15182 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 15183 mutex_exit(&cpu_lock); 15184 mutex_exit(&dtrace_provider_lock); 15185 mutex_exit(&dtrace_lock); 15186 return (DDI_FAILURE); 15187 } 15188 15189 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 15190 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 15191 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 15192 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 15193 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 15194 ddi_remove_minor_node(devi, NULL); 15195 ddi_soft_state_fini(&dtrace_softstate); 15196 mutex_exit(&cpu_lock); 15197 mutex_exit(&dtrace_provider_lock); 15198 mutex_exit(&dtrace_lock); 15199 return (DDI_FAILURE); 15200 } 15201 15202 ddi_report_dev(devi); 15203 dtrace_devi = devi; 15204 15205 dtrace_modload = dtrace_module_loaded; 15206 dtrace_modunload = dtrace_module_unloaded; 15207 dtrace_cpu_init = dtrace_cpu_setup_initial; 15208 dtrace_helpers_cleanup = dtrace_helpers_destroy; 15209 dtrace_helpers_fork = dtrace_helpers_duplicate; 15210 dtrace_cpustart_init = dtrace_suspend; 15211 dtrace_cpustart_fini = dtrace_resume; 15212 dtrace_debugger_init = dtrace_suspend; 15213 dtrace_debugger_fini = dtrace_resume; 15214 15215 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15216 15217 ASSERT(MUTEX_HELD(&cpu_lock)); 15218 15219 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 15220 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 15221 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 15222 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 15223 VM_SLEEP | VMC_IDENTIFIER); 15224 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15225 1, INT_MAX, 0); 15226 15227 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 15228 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 15229 NULL, NULL, NULL, NULL, NULL, 0); 15230 15231 ASSERT(MUTEX_HELD(&cpu_lock)); 15232 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 15233 offsetof(dtrace_probe_t, dtpr_nextmod), 15234 offsetof(dtrace_probe_t, dtpr_prevmod)); 15235 15236 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 15237 offsetof(dtrace_probe_t, dtpr_nextfunc), 15238 offsetof(dtrace_probe_t, dtpr_prevfunc)); 15239 15240 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 15241 offsetof(dtrace_probe_t, dtpr_nextname), 15242 offsetof(dtrace_probe_t, dtpr_prevname)); 15243 15244 if (dtrace_retain_max < 1) { 15245 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 15246 "setting to 1", dtrace_retain_max); 15247 dtrace_retain_max = 1; 15248 } 15249 15250 /* 15251 * Now discover our toxic ranges. 15252 */ 15253 dtrace_toxic_ranges(dtrace_toxrange_add); 15254 15255 /* 15256 * Before we register ourselves as a provider to our own framework, 15257 * we would like to assert that dtrace_provider is NULL -- but that's 15258 * not true if we were loaded as a dependency of a DTrace provider. 15259 * Once we've registered, we can assert that dtrace_provider is our 15260 * pseudo provider. 15261 */ 15262 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15263 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15264 15265 ASSERT(dtrace_provider != NULL); 15266 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15267 15268 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15269 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15270 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15271 dtrace_provider, NULL, NULL, "END", 0, NULL); 15272 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15273 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15274 15275 dtrace_anon_property(); 15276 mutex_exit(&cpu_lock); 15277 15278 /* 15279 * If DTrace helper tracing is enabled, we need to allocate the 15280 * trace buffer and initialize the values. 15281 */ 15282 if (dtrace_helptrace_enabled) { 15283 ASSERT(dtrace_helptrace_buffer == NULL); 15284 dtrace_helptrace_buffer = 15285 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15286 dtrace_helptrace_next = 0; 15287 } 15288 15289 /* 15290 * If there are already providers, we must ask them to provide their 15291 * probes, and then match any anonymous enabling against them. Note 15292 * that there should be no other retained enablings at this time: 15293 * the only retained enablings at this time should be the anonymous 15294 * enabling. 15295 */ 15296 if (dtrace_anon.dta_enabling != NULL) { 15297 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15298 15299 dtrace_enabling_provide(NULL); 15300 state = dtrace_anon.dta_state; 15301 15302 /* 15303 * We couldn't hold cpu_lock across the above call to 15304 * dtrace_enabling_provide(), but we must hold it to actually 15305 * enable the probes. We have to drop all of our locks, pick 15306 * up cpu_lock, and regain our locks before matching the 15307 * retained anonymous enabling. 15308 */ 15309 mutex_exit(&dtrace_lock); 15310 mutex_exit(&dtrace_provider_lock); 15311 15312 mutex_enter(&cpu_lock); 15313 mutex_enter(&dtrace_provider_lock); 15314 mutex_enter(&dtrace_lock); 15315 15316 if ((enab = dtrace_anon.dta_enabling) != NULL) 15317 (void) dtrace_enabling_match(enab, NULL); 15318 15319 mutex_exit(&cpu_lock); 15320 } 15321 15322 mutex_exit(&dtrace_lock); 15323 mutex_exit(&dtrace_provider_lock); 15324 15325 if (state != NULL) { 15326 /* 15327 * If we created any anonymous state, set it going now. 15328 */ 15329 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15330 } 15331 15332 return (DDI_SUCCESS); 15333} 15334#endif 15335 15336#if !defined(sun) 15337#if __FreeBSD_version >= 800039 15338static void 15339dtrace_dtr(void *data __unused) 15340{ 15341} 15342#endif 15343#endif 15344 15345#if !defined(sun) 15346static dev_type_open(dtrace_open); 15347 15348/* Pseudo Device Entry points */ 15349/* Just opens, clones to the fileops below */ 15350const struct cdevsw dtrace_cdevsw = { 15351 dtrace_open, noclose, noread, nowrite, noioctl, 15352 nostop, notty, nopoll, nommap, nokqfilter, 15353 D_OTHER | D_MPSAFE 15354}; 15355 15356static int dtrace_ioctl(struct file *fp, u_long cmd, void *data); 15357static int dtrace_close(struct file *fp); 15358 15359static const struct fileops dtrace_fileops = { 15360 .fo_read = fbadop_read, 15361 .fo_write = fbadop_write, 15362 .fo_ioctl = dtrace_ioctl, 15363 .fo_fcntl = fnullop_fcntl, 15364 .fo_poll = fnullop_poll, 15365 .fo_stat = fbadop_stat, 15366 .fo_close = dtrace_close, 15367 .fo_kqfilter = fnullop_kqfilter, 15368}; 15369#endif 15370 15371/*ARGSUSED*/ 15372static int 15373#if defined(sun) 15374dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15375#else 15376dtrace_open(dev_t dev, int flags, int mode, struct lwp *l) 15377#endif 15378{ 15379 dtrace_state_t *state; 15380 uint32_t priv; 15381 uid_t uid; 15382 zoneid_t zoneid; 15383 15384#if defined(sun) 15385 if (getminor(*devp) == DTRACEMNRN_HELPER) 15386 return (0); 15387 15388 /* 15389 * If this wasn't an open with the "helper" minor, then it must be 15390 * the "dtrace" minor. 15391 */ 15392 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 15393#else 15394 cred_t *cred_p = NULL; 15395 struct file *fp; 15396 int fd; 15397 int res; 15398 15399 if ((res = fd_allocfile(&fp, &fd)) != 0) 15400 return res; 15401#if 0 15402#if __FreeBSD_version < 800039 15403 /* 15404 * The first minor device is the one that is cloned so there is 15405 * nothing more to do here. 15406 */ 15407 if (dev2unit(dev) == 0) 15408 return 0; 15409 15410 /* 15411 * Devices are cloned, so if the DTrace state has already 15412 * been allocated, that means this device belongs to a 15413 * different client. Each client should open '/dev/dtrace' 15414 * to get a cloned device. 15415 */ 15416 if (dev->si_drv1 != NULL) 15417 return (EBUSY); 15418#endif 15419 15420 cred_p = dev->si_cred; 15421#endif 15422 cred_p = l->l_cred; 15423#endif 15424 15425 /* 15426 * If no DTRACE_PRIV_* bits are set in the credential, then the 15427 * caller lacks sufficient permission to do anything with DTrace. 15428 */ 15429 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15430 if (priv == DTRACE_PRIV_NONE) { 15431 return (EACCES); 15432 } 15433 15434 /* 15435 * Ask all providers to provide all their probes. 15436 */ 15437 mutex_enter(&dtrace_provider_lock); 15438 dtrace_probe_provide(NULL, NULL); 15439 mutex_exit(&dtrace_provider_lock); 15440 15441 mutex_enter(&cpu_lock); 15442 mutex_enter(&dtrace_lock); 15443 dtrace_opens++; 15444 dtrace_membar_producer(); 15445 15446#if defined(sun) 15447 /* 15448 * If the kernel debugger is active (that is, if the kernel debugger 15449 * modified text in some way), we won't allow the open. 15450 */ 15451 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15452 dtrace_opens--; 15453 mutex_exit(&cpu_lock); 15454 mutex_exit(&dtrace_lock); 15455 return (EBUSY); 15456 } 15457 15458 state = dtrace_state_create(devp, cred_p); 15459#else 15460 state = dtrace_state_create(dev, cred_p); 15461#endif 15462 15463 mutex_exit(&cpu_lock); 15464 15465 if (state == NULL) { 15466#if defined(sun) 15467 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15468 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15469#else 15470 --dtrace_opens; 15471#endif 15472 mutex_exit(&dtrace_lock); 15473 return (EAGAIN); 15474 } 15475 15476 mutex_exit(&dtrace_lock); 15477 15478#if defined(sun) 15479 return (0); 15480#else 15481 return fd_clone(fp, fd, flags, &dtrace_fileops, state); 15482#endif 15483} 15484 15485/*ARGSUSED*/ 15486static int 15487#if defined(sun) 15488dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15489#else 15490dtrace_close(struct file *fp) 15491#endif 15492{ 15493#if defined(sun) 15494 minor_t minor = getminor(dev); 15495 dtrace_state_t *state; 15496 15497 if (minor == DTRACEMNRN_HELPER) 15498 return (0); 15499 15500 state = ddi_get_soft_state(dtrace_softstate, minor); 15501#else 15502 dtrace_state_t *state = (dtrace_state_t *)fp->f_data; 15503#endif 15504 15505 mutex_enter(&cpu_lock); 15506 mutex_enter(&dtrace_lock); 15507 15508 if (state != NULL) { 15509 if (state->dts_anon) { 15510 /* 15511 * There is anonymous state. Destroy that first. 15512 */ 15513 ASSERT(dtrace_anon.dta_state == NULL); 15514 dtrace_state_destroy(state->dts_anon); 15515 } 15516 15517 dtrace_state_destroy(state); 15518 15519#if !defined(sun) 15520 fp->f_data = NULL; 15521#endif 15522 } 15523 15524 ASSERT(dtrace_opens > 0); 15525#if defined(sun) 15526 /* 15527 * Only relinquish control of the kernel debugger interface when there 15528 * are no consumers and no anonymous enablings. 15529 */ 15530 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 15531 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15532#else 15533 --dtrace_opens; 15534#endif 15535 15536 mutex_exit(&dtrace_lock); 15537 mutex_exit(&cpu_lock); 15538 15539 return (0); 15540} 15541 15542#if defined(sun) 15543/*ARGSUSED*/ 15544static int 15545dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15546{ 15547 int rval; 15548 dof_helper_t help, *dhp = NULL; 15549 15550 switch (cmd) { 15551 case DTRACEHIOC_ADDDOF: 15552 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15553 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15554 return (EFAULT); 15555 } 15556 15557 dhp = &help; 15558 arg = (intptr_t)help.dofhp_dof; 15559 /*FALLTHROUGH*/ 15560 15561 case DTRACEHIOC_ADD: { 15562 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15563 15564 if (dof == NULL) 15565 return (rval); 15566 15567 mutex_enter(&dtrace_lock); 15568 15569 /* 15570 * dtrace_helper_slurp() takes responsibility for the dof -- 15571 * it may free it now or it may save it and free it later. 15572 */ 15573 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15574 *rv = rval; 15575 rval = 0; 15576 } else { 15577 rval = EINVAL; 15578 } 15579 15580 mutex_exit(&dtrace_lock); 15581 return (rval); 15582 } 15583 15584 case DTRACEHIOC_REMOVE: { 15585 mutex_enter(&dtrace_lock); 15586 rval = dtrace_helper_destroygen(arg); 15587 mutex_exit(&dtrace_lock); 15588 15589 return (rval); 15590 } 15591 15592 default: 15593 break; 15594 } 15595 15596 return (ENOTTY); 15597} 15598 15599/*ARGSUSED*/ 15600static int 15601dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15602{ 15603 minor_t minor = getminor(dev); 15604 dtrace_state_t *state; 15605 int rval; 15606 15607 if (minor == DTRACEMNRN_HELPER) 15608 return (dtrace_ioctl_helper(cmd, arg, rv)); 15609 15610 state = ddi_get_soft_state(dtrace_softstate, minor); 15611 15612 if (state->dts_anon) { 15613 ASSERT(dtrace_anon.dta_state == NULL); 15614 state = state->dts_anon; 15615 } 15616 15617 switch (cmd) { 15618 case DTRACEIOC_PROVIDER: { 15619 dtrace_providerdesc_t pvd; 15620 dtrace_provider_t *pvp; 15621 15622 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15623 return (EFAULT); 15624 15625 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15626 mutex_enter(&dtrace_provider_lock); 15627 15628 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15629 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15630 break; 15631 } 15632 15633 mutex_exit(&dtrace_provider_lock); 15634 15635 if (pvp == NULL) 15636 return (ESRCH); 15637 15638 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15639 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15640 15641 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15642 return (EFAULT); 15643 15644 return (0); 15645 } 15646 15647 case DTRACEIOC_EPROBE: { 15648 dtrace_eprobedesc_t epdesc; 15649 dtrace_ecb_t *ecb; 15650 dtrace_action_t *act; 15651 void *buf; 15652 size_t size; 15653 uintptr_t dest; 15654 int nrecs; 15655 15656 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15657 return (EFAULT); 15658 15659 mutex_enter(&dtrace_lock); 15660 15661 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15662 mutex_exit(&dtrace_lock); 15663 return (EINVAL); 15664 } 15665 15666 if (ecb->dte_probe == NULL) { 15667 mutex_exit(&dtrace_lock); 15668 return (EINVAL); 15669 } 15670 15671 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15672 epdesc.dtepd_uarg = ecb->dte_uarg; 15673 epdesc.dtepd_size = ecb->dte_size; 15674 15675 nrecs = epdesc.dtepd_nrecs; 15676 epdesc.dtepd_nrecs = 0; 15677 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15678 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15679 continue; 15680 15681 epdesc.dtepd_nrecs++; 15682 } 15683 15684 /* 15685 * Now that we have the size, we need to allocate a temporary 15686 * buffer in which to store the complete description. We need 15687 * the temporary buffer to be able to drop dtrace_lock() 15688 * across the copyout(), below. 15689 */ 15690 size = sizeof (dtrace_eprobedesc_t) + 15691 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15692 15693 buf = kmem_alloc(size, KM_SLEEP); 15694 dest = (uintptr_t)buf; 15695 15696 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15697 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15698 15699 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15700 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15701 continue; 15702 15703 if (nrecs-- == 0) 15704 break; 15705 15706 bcopy(&act->dta_rec, (void *)dest, 15707 sizeof (dtrace_recdesc_t)); 15708 dest += sizeof (dtrace_recdesc_t); 15709 } 15710 15711 mutex_exit(&dtrace_lock); 15712 15713 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15714 kmem_free(buf, size); 15715 return (EFAULT); 15716 } 15717 15718 kmem_free(buf, size); 15719 return (0); 15720 } 15721 15722 case DTRACEIOC_AGGDESC: { 15723 dtrace_aggdesc_t aggdesc; 15724 dtrace_action_t *act; 15725 dtrace_aggregation_t *agg; 15726 int nrecs; 15727 uint32_t offs; 15728 dtrace_recdesc_t *lrec; 15729 void *buf; 15730 size_t size; 15731 uintptr_t dest; 15732 15733 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15734 return (EFAULT); 15735 15736 mutex_enter(&dtrace_lock); 15737 15738 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15739 mutex_exit(&dtrace_lock); 15740 return (EINVAL); 15741 } 15742 15743 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 15744 15745 nrecs = aggdesc.dtagd_nrecs; 15746 aggdesc.dtagd_nrecs = 0; 15747 15748 offs = agg->dtag_base; 15749 lrec = &agg->dtag_action.dta_rec; 15750 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 15751 15752 for (act = agg->dtag_first; ; act = act->dta_next) { 15753 ASSERT(act->dta_intuple || 15754 DTRACEACT_ISAGG(act->dta_kind)); 15755 15756 /* 15757 * If this action has a record size of zero, it 15758 * denotes an argument to the aggregating action. 15759 * Because the presence of this record doesn't (or 15760 * shouldn't) affect the way the data is interpreted, 15761 * we don't copy it out to save user-level the 15762 * confusion of dealing with a zero-length record. 15763 */ 15764 if (act->dta_rec.dtrd_size == 0) { 15765 ASSERT(agg->dtag_hasarg); 15766 continue; 15767 } 15768 15769 aggdesc.dtagd_nrecs++; 15770 15771 if (act == &agg->dtag_action) 15772 break; 15773 } 15774 15775 /* 15776 * Now that we have the size, we need to allocate a temporary 15777 * buffer in which to store the complete description. We need 15778 * the temporary buffer to be able to drop dtrace_lock() 15779 * across the copyout(), below. 15780 */ 15781 size = sizeof (dtrace_aggdesc_t) + 15782 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 15783 15784 buf = kmem_alloc(size, KM_SLEEP); 15785 dest = (uintptr_t)buf; 15786 15787 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 15788 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 15789 15790 for (act = agg->dtag_first; ; act = act->dta_next) { 15791 dtrace_recdesc_t rec = act->dta_rec; 15792 15793 /* 15794 * See the comment in the above loop for why we pass 15795 * over zero-length records. 15796 */ 15797 if (rec.dtrd_size == 0) { 15798 ASSERT(agg->dtag_hasarg); 15799 continue; 15800 } 15801 15802 if (nrecs-- == 0) 15803 break; 15804 15805 rec.dtrd_offset -= offs; 15806 bcopy(&rec, (void *)dest, sizeof (rec)); 15807 dest += sizeof (dtrace_recdesc_t); 15808 15809 if (act == &agg->dtag_action) 15810 break; 15811 } 15812 15813 mutex_exit(&dtrace_lock); 15814 15815 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15816 kmem_free(buf, size); 15817 return (EFAULT); 15818 } 15819 15820 kmem_free(buf, size); 15821 return (0); 15822 } 15823 15824 case DTRACEIOC_ENABLE: { 15825 dof_hdr_t *dof; 15826 dtrace_enabling_t *enab = NULL; 15827 dtrace_vstate_t *vstate; 15828 int err = 0; 15829 15830 *rv = 0; 15831 15832 /* 15833 * If a NULL argument has been passed, we take this as our 15834 * cue to reevaluate our enablings. 15835 */ 15836 if (arg == NULL) { 15837 dtrace_enabling_matchall(); 15838 15839 return (0); 15840 } 15841 15842 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 15843 return (rval); 15844 15845 mutex_enter(&cpu_lock); 15846 mutex_enter(&dtrace_lock); 15847 vstate = &state->dts_vstate; 15848 15849 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 15850 mutex_exit(&dtrace_lock); 15851 mutex_exit(&cpu_lock); 15852 dtrace_dof_destroy(dof); 15853 return (EBUSY); 15854 } 15855 15856 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 15857 mutex_exit(&dtrace_lock); 15858 mutex_exit(&cpu_lock); 15859 dtrace_dof_destroy(dof); 15860 return (EINVAL); 15861 } 15862 15863 if ((rval = dtrace_dof_options(dof, state)) != 0) { 15864 dtrace_enabling_destroy(enab); 15865 mutex_exit(&dtrace_lock); 15866 mutex_exit(&cpu_lock); 15867 dtrace_dof_destroy(dof); 15868 return (rval); 15869 } 15870 15871 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 15872 err = dtrace_enabling_retain(enab); 15873 } else { 15874 dtrace_enabling_destroy(enab); 15875 } 15876 15877 mutex_exit(&cpu_lock); 15878 mutex_exit(&dtrace_lock); 15879 dtrace_dof_destroy(dof); 15880 15881 return (err); 15882 } 15883 15884 case DTRACEIOC_REPLICATE: { 15885 dtrace_repldesc_t desc; 15886 dtrace_probedesc_t *match = &desc.dtrpd_match; 15887 dtrace_probedesc_t *create = &desc.dtrpd_create; 15888 int err; 15889 15890 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15891 return (EFAULT); 15892 15893 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15894 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15895 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15896 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15897 15898 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15899 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15900 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15901 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15902 15903 mutex_enter(&dtrace_lock); 15904 err = dtrace_enabling_replicate(state, match, create); 15905 mutex_exit(&dtrace_lock); 15906 15907 return (err); 15908 } 15909 15910 case DTRACEIOC_PROBEMATCH: 15911 case DTRACEIOC_PROBES: { 15912 dtrace_probe_t *probe = NULL; 15913 dtrace_probedesc_t desc; 15914 dtrace_probekey_t pkey; 15915 dtrace_id_t i; 15916 int m = 0; 15917 uint32_t priv; 15918 uid_t uid; 15919 zoneid_t zoneid; 15920 15921 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15922 return (EFAULT); 15923 15924 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15925 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15926 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15927 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15928 15929 /* 15930 * Before we attempt to match this probe, we want to give 15931 * all providers the opportunity to provide it. 15932 */ 15933 if (desc.dtpd_id == DTRACE_IDNONE) { 15934 mutex_enter(&dtrace_provider_lock); 15935 dtrace_probe_provide(&desc, NULL); 15936 mutex_exit(&dtrace_provider_lock); 15937 desc.dtpd_id++; 15938 } 15939 15940 if (cmd == DTRACEIOC_PROBEMATCH) { 15941 dtrace_probekey(&desc, &pkey); 15942 pkey.dtpk_id = DTRACE_IDNONE; 15943 } 15944 15945 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 15946 15947 mutex_enter(&dtrace_lock); 15948 15949 if (cmd == DTRACEIOC_PROBEMATCH) { 15950 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15951 if ((probe = dtrace_probes[i - 1]) != NULL && 15952 (m = dtrace_match_probe(probe, &pkey, 15953 priv, uid, zoneid)) != 0) 15954 break; 15955 } 15956 15957 if (m < 0) { 15958 mutex_exit(&dtrace_lock); 15959 return (EINVAL); 15960 } 15961 15962 } else { 15963 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15964 if ((probe = dtrace_probes[i - 1]) != NULL && 15965 dtrace_match_priv(probe, priv, uid, zoneid)) 15966 break; 15967 } 15968 } 15969 15970 if (probe == NULL) { 15971 mutex_exit(&dtrace_lock); 15972 return (ESRCH); 15973 } 15974 15975 dtrace_probe_description(probe, &desc); 15976 mutex_exit(&dtrace_lock); 15977 15978 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15979 return (EFAULT); 15980 15981 return (0); 15982 } 15983 15984 case DTRACEIOC_PROBEARG: { 15985 dtrace_argdesc_t desc; 15986 dtrace_probe_t *probe; 15987 dtrace_provider_t *prov; 15988 15989 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15990 return (EFAULT); 15991 15992 if (desc.dtargd_id == DTRACE_IDNONE) 15993 return (EINVAL); 15994 15995 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15996 return (EINVAL); 15997 15998 mutex_enter(&dtrace_provider_lock); 15999 mutex_enter(&mod_lock); 16000 mutex_enter(&dtrace_lock); 16001 16002 if (desc.dtargd_id > dtrace_nprobes) { 16003 mutex_exit(&dtrace_lock); 16004 mutex_exit(&mod_lock); 16005 mutex_exit(&dtrace_provider_lock); 16006 return (EINVAL); 16007 } 16008 16009 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 16010 mutex_exit(&dtrace_lock); 16011 mutex_exit(&mod_lock); 16012 mutex_exit(&dtrace_provider_lock); 16013 return (EINVAL); 16014 } 16015 16016 mutex_exit(&dtrace_lock); 16017 16018 prov = probe->dtpr_provider; 16019 16020 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 16021 /* 16022 * There isn't any typed information for this probe. 16023 * Set the argument number to DTRACE_ARGNONE. 16024 */ 16025 desc.dtargd_ndx = DTRACE_ARGNONE; 16026 } else { 16027 desc.dtargd_native[0] = '\0'; 16028 desc.dtargd_xlate[0] = '\0'; 16029 desc.dtargd_mapping = desc.dtargd_ndx; 16030 16031 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 16032 probe->dtpr_id, probe->dtpr_arg, &desc); 16033 } 16034 16035 mutex_exit(&mod_lock); 16036 mutex_exit(&dtrace_provider_lock); 16037 16038 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16039 return (EFAULT); 16040 16041 return (0); 16042 } 16043 16044 case DTRACEIOC_GO: { 16045 processorid_t cpuid; 16046 rval = dtrace_state_go(state, &cpuid); 16047 16048 if (rval != 0) 16049 return (rval); 16050 16051 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16052 return (EFAULT); 16053 16054 return (0); 16055 } 16056 16057 case DTRACEIOC_STOP: { 16058 processorid_t cpuid; 16059 16060 mutex_enter(&dtrace_lock); 16061 rval = dtrace_state_stop(state, &cpuid); 16062 mutex_exit(&dtrace_lock); 16063 16064 if (rval != 0) 16065 return (rval); 16066 16067 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 16068 return (EFAULT); 16069 16070 return (0); 16071 } 16072 16073 case DTRACEIOC_DOFGET: { 16074 dof_hdr_t hdr, *dof; 16075 uint64_t len; 16076 16077 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 16078 return (EFAULT); 16079 16080 mutex_enter(&dtrace_lock); 16081 dof = dtrace_dof_create(state); 16082 mutex_exit(&dtrace_lock); 16083 16084 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 16085 rval = copyout(dof, (void *)arg, len); 16086 dtrace_dof_destroy(dof); 16087 16088 return (rval == 0 ? 0 : EFAULT); 16089 } 16090 16091 case DTRACEIOC_AGGSNAP: 16092 case DTRACEIOC_BUFSNAP: { 16093 dtrace_bufdesc_t desc; 16094 caddr_t cached; 16095 dtrace_buffer_t *buf; 16096 16097 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 16098 return (EFAULT); 16099 16100 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 16101 return (EINVAL); 16102 16103 mutex_enter(&dtrace_lock); 16104 16105 if (cmd == DTRACEIOC_BUFSNAP) { 16106 buf = &state->dts_buffer[desc.dtbd_cpu]; 16107 } else { 16108 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 16109 } 16110 16111 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 16112 size_t sz = buf->dtb_offset; 16113 16114 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 16115 mutex_exit(&dtrace_lock); 16116 return (EBUSY); 16117 } 16118 16119 /* 16120 * If this buffer has already been consumed, we're 16121 * going to indicate that there's nothing left here 16122 * to consume. 16123 */ 16124 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 16125 mutex_exit(&dtrace_lock); 16126 16127 desc.dtbd_size = 0; 16128 desc.dtbd_drops = 0; 16129 desc.dtbd_errors = 0; 16130 desc.dtbd_oldest = 0; 16131 sz = sizeof (desc); 16132 16133 if (copyout(&desc, (void *)arg, sz) != 0) 16134 return (EFAULT); 16135 16136 return (0); 16137 } 16138 16139 /* 16140 * If this is a ring buffer that has wrapped, we want 16141 * to copy the whole thing out. 16142 */ 16143 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 16144 dtrace_buffer_polish(buf); 16145 sz = buf->dtb_size; 16146 } 16147 16148 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 16149 mutex_exit(&dtrace_lock); 16150 return (EFAULT); 16151 } 16152 16153 desc.dtbd_size = sz; 16154 desc.dtbd_drops = buf->dtb_drops; 16155 desc.dtbd_errors = buf->dtb_errors; 16156 desc.dtbd_oldest = buf->dtb_xamot_offset; 16157 16158 mutex_exit(&dtrace_lock); 16159 16160 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16161 return (EFAULT); 16162 16163 buf->dtb_flags |= DTRACEBUF_CONSUMED; 16164 16165 return (0); 16166 } 16167 16168 if (buf->dtb_tomax == NULL) { 16169 ASSERT(buf->dtb_xamot == NULL); 16170 mutex_exit(&dtrace_lock); 16171 return (ENOENT); 16172 } 16173 16174 cached = buf->dtb_tomax; 16175 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 16176 16177 dtrace_xcall(desc.dtbd_cpu, 16178 (dtrace_xcall_t)dtrace_buffer_switch, buf); 16179 16180 state->dts_errors += buf->dtb_xamot_errors; 16181 16182 /* 16183 * If the buffers did not actually switch, then the cross call 16184 * did not take place -- presumably because the given CPU is 16185 * not in the ready set. If this is the case, we'll return 16186 * ENOENT. 16187 */ 16188 if (buf->dtb_tomax == cached) { 16189 ASSERT(buf->dtb_xamot != cached); 16190 mutex_exit(&dtrace_lock); 16191 return (ENOENT); 16192 } 16193 16194 ASSERT(cached == buf->dtb_xamot); 16195 16196 /* 16197 * We have our snapshot; now copy it out. 16198 */ 16199 if (copyout(buf->dtb_xamot, desc.dtbd_data, 16200 buf->dtb_xamot_offset) != 0) { 16201 mutex_exit(&dtrace_lock); 16202 return (EFAULT); 16203 } 16204 16205 desc.dtbd_size = buf->dtb_xamot_offset; 16206 desc.dtbd_drops = buf->dtb_xamot_drops; 16207 desc.dtbd_errors = buf->dtb_xamot_errors; 16208 desc.dtbd_oldest = 0; 16209 16210 mutex_exit(&dtrace_lock); 16211 16212 /* 16213 * Finally, copy out the buffer description. 16214 */ 16215 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16216 return (EFAULT); 16217 16218 return (0); 16219 } 16220 16221 case DTRACEIOC_CONF: { 16222 dtrace_conf_t conf; 16223 16224 bzero(&conf, sizeof (conf)); 16225 conf.dtc_difversion = DIF_VERSION; 16226 conf.dtc_difintregs = DIF_DIR_NREGS; 16227 conf.dtc_diftupregs = DIF_DTR_NREGS; 16228 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 16229 16230 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 16231 return (EFAULT); 16232 16233 return (0); 16234 } 16235 16236 case DTRACEIOC_STATUS: { 16237 dtrace_status_t stat; 16238 dtrace_dstate_t *dstate; 16239 int i, j; 16240 uint64_t nerrs; 16241 16242 /* 16243 * See the comment in dtrace_state_deadman() for the reason 16244 * for setting dts_laststatus to INT64_MAX before setting 16245 * it to the correct value. 16246 */ 16247 state->dts_laststatus = INT64_MAX; 16248 dtrace_membar_producer(); 16249 state->dts_laststatus = dtrace_gethrtime(); 16250 16251 bzero(&stat, sizeof (stat)); 16252 16253 mutex_enter(&dtrace_lock); 16254 16255 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 16256 mutex_exit(&dtrace_lock); 16257 return (ENOENT); 16258 } 16259 16260 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 16261 stat.dtst_exiting = 1; 16262 16263 nerrs = state->dts_errors; 16264 dstate = &state->dts_vstate.dtvs_dynvars; 16265 16266 for (i = 0; i < NCPU; i++) { 16267 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 16268 16269 stat.dtst_dyndrops += dcpu->dtdsc_drops; 16270 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 16271 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 16272 16273 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 16274 stat.dtst_filled++; 16275 16276 nerrs += state->dts_buffer[i].dtb_errors; 16277 16278 for (j = 0; j < state->dts_nspeculations; j++) { 16279 dtrace_speculation_t *spec; 16280 dtrace_buffer_t *buf; 16281 16282 spec = &state->dts_speculations[j]; 16283 buf = &spec->dtsp_buffer[i]; 16284 stat.dtst_specdrops += buf->dtb_xamot_drops; 16285 } 16286 } 16287 16288 stat.dtst_specdrops_busy = state->dts_speculations_busy; 16289 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 16290 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 16291 stat.dtst_dblerrors = state->dts_dblerrors; 16292 stat.dtst_killed = 16293 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 16294 stat.dtst_errors = nerrs; 16295 16296 mutex_exit(&dtrace_lock); 16297 16298 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 16299 return (EFAULT); 16300 16301 return (0); 16302 } 16303 16304 case DTRACEIOC_FORMAT: { 16305 dtrace_fmtdesc_t fmt; 16306 char *str; 16307 int len; 16308 16309 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 16310 return (EFAULT); 16311 16312 mutex_enter(&dtrace_lock); 16313 16314 if (fmt.dtfd_format == 0 || 16315 fmt.dtfd_format > state->dts_nformats) { 16316 mutex_exit(&dtrace_lock); 16317 return (EINVAL); 16318 } 16319 16320 /* 16321 * Format strings are allocated contiguously and they are 16322 * never freed; if a format index is less than the number 16323 * of formats, we can assert that the format map is non-NULL 16324 * and that the format for the specified index is non-NULL. 16325 */ 16326 ASSERT(state->dts_formats != NULL); 16327 str = state->dts_formats[fmt.dtfd_format - 1]; 16328 ASSERT(str != NULL); 16329 16330 len = strlen(str) + 1; 16331 16332 if (len > fmt.dtfd_length) { 16333 fmt.dtfd_length = len; 16334 16335 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 16336 mutex_exit(&dtrace_lock); 16337 return (EINVAL); 16338 } 16339 } else { 16340 if (copyout(str, fmt.dtfd_string, len) != 0) { 16341 mutex_exit(&dtrace_lock); 16342 return (EINVAL); 16343 } 16344 } 16345 16346 mutex_exit(&dtrace_lock); 16347 return (0); 16348 } 16349 16350 default: 16351 break; 16352 } 16353 16354 return (ENOTTY); 16355} 16356 16357/*ARGSUSED*/ 16358static int 16359dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16360{ 16361 dtrace_state_t *state; 16362 16363 switch (cmd) { 16364 case DDI_DETACH: 16365 break; 16366 16367 case DDI_SUSPEND: 16368 return (DDI_SUCCESS); 16369 16370 default: 16371 return (DDI_FAILURE); 16372 } 16373 16374 mutex_enter(&cpu_lock); 16375 mutex_enter(&dtrace_provider_lock); 16376 mutex_enter(&dtrace_lock); 16377 16378 ASSERT(dtrace_opens == 0); 16379 16380 if (dtrace_helpers > 0) { 16381 mutex_exit(&dtrace_provider_lock); 16382 mutex_exit(&dtrace_lock); 16383 mutex_exit(&cpu_lock); 16384 return (DDI_FAILURE); 16385 } 16386 16387 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16388 mutex_exit(&dtrace_provider_lock); 16389 mutex_exit(&dtrace_lock); 16390 mutex_exit(&cpu_lock); 16391 return (DDI_FAILURE); 16392 } 16393 16394 dtrace_provider = NULL; 16395 16396 if ((state = dtrace_anon_grab()) != NULL) { 16397 /* 16398 * If there were ECBs on this state, the provider should 16399 * have not been allowed to detach; assert that there is 16400 * none. 16401 */ 16402 ASSERT(state->dts_necbs == 0); 16403 dtrace_state_destroy(state); 16404 16405 /* 16406 * If we're being detached with anonymous state, we need to 16407 * indicate to the kernel debugger that DTrace is now inactive. 16408 */ 16409 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16410 } 16411 16412 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16413 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16414 dtrace_cpu_init = NULL; 16415 dtrace_helpers_cleanup = NULL; 16416 dtrace_helpers_fork = NULL; 16417 dtrace_cpustart_init = NULL; 16418 dtrace_cpustart_fini = NULL; 16419 dtrace_debugger_init = NULL; 16420 dtrace_debugger_fini = NULL; 16421 dtrace_modload = NULL; 16422 dtrace_modunload = NULL; 16423 16424 mutex_exit(&cpu_lock); 16425 16426 if (dtrace_helptrace_enabled) { 16427 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 16428 dtrace_helptrace_buffer = NULL; 16429 } 16430 16431 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16432 dtrace_probes = NULL; 16433 dtrace_nprobes = 0; 16434 16435 dtrace_hash_destroy(dtrace_bymod); 16436 dtrace_hash_destroy(dtrace_byfunc); 16437 dtrace_hash_destroy(dtrace_byname); 16438 dtrace_bymod = NULL; 16439 dtrace_byfunc = NULL; 16440 dtrace_byname = NULL; 16441 16442 kmem_cache_destroy(dtrace_state_cache); 16443 vmem_destroy(dtrace_minor); 16444 vmem_destroy(dtrace_arena); 16445 16446 if (dtrace_toxrange != NULL) { 16447 kmem_free(dtrace_toxrange, 16448 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16449 dtrace_toxrange = NULL; 16450 dtrace_toxranges = 0; 16451 dtrace_toxranges_max = 0; 16452 } 16453 16454 ddi_remove_minor_node(dtrace_devi, NULL); 16455 dtrace_devi = NULL; 16456 16457 ddi_soft_state_fini(&dtrace_softstate); 16458 16459 ASSERT(dtrace_vtime_references == 0); 16460 ASSERT(dtrace_opens == 0); 16461 ASSERT(dtrace_retained == NULL); 16462 16463 mutex_exit(&dtrace_lock); 16464 mutex_exit(&dtrace_provider_lock); 16465 16466 /* 16467 * We don't destroy the task queue until after we have dropped our 16468 * locks (taskq_destroy() may block on running tasks). To prevent 16469 * attempting to do work after we have effectively detached but before 16470 * the task queue has been destroyed, all tasks dispatched via the 16471 * task queue must check that DTrace is still attached before 16472 * performing any operation. 16473 */ 16474 taskq_destroy(dtrace_taskq); 16475 dtrace_taskq = NULL; 16476 16477 return (DDI_SUCCESS); 16478} 16479#endif 16480 16481#if defined(sun) 16482/*ARGSUSED*/ 16483static int 16484dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16485{ 16486 int error; 16487 16488 switch (infocmd) { 16489 case DDI_INFO_DEVT2DEVINFO: 16490 *result = (void *)dtrace_devi; 16491 error = DDI_SUCCESS; 16492 break; 16493 case DDI_INFO_DEVT2INSTANCE: 16494 *result = (void *)0; 16495 error = DDI_SUCCESS; 16496 break; 16497 default: 16498 error = DDI_FAILURE; 16499 } 16500 return (error); 16501} 16502#endif 16503 16504#if defined(sun) 16505static struct cb_ops dtrace_cb_ops = { 16506 dtrace_open, /* open */ 16507 dtrace_close, /* close */ 16508 nulldev, /* strategy */ 16509 nulldev, /* print */ 16510 nodev, /* dump */ 16511 nodev, /* read */ 16512 nodev, /* write */ 16513 dtrace_ioctl, /* ioctl */ 16514 nodev, /* devmap */ 16515 nodev, /* mmap */ 16516 nodev, /* segmap */ 16517 nochpoll, /* poll */ 16518 ddi_prop_op, /* cb_prop_op */ 16519 0, /* streamtab */ 16520 D_NEW | D_MP /* Driver compatibility flag */ 16521}; 16522 16523static struct dev_ops dtrace_ops = { 16524 DEVO_REV, /* devo_rev */ 16525 0, /* refcnt */ 16526 dtrace_info, /* get_dev_info */ 16527 nulldev, /* identify */ 16528 nulldev, /* probe */ 16529 dtrace_attach, /* attach */ 16530 dtrace_detach, /* detach */ 16531 nodev, /* reset */ 16532 &dtrace_cb_ops, /* driver operations */ 16533 NULL, /* bus operations */ 16534 nodev /* dev power */ 16535}; 16536 16537static struct modldrv modldrv = { 16538 &mod_driverops, /* module type (this is a pseudo driver) */ 16539 "Dynamic Tracing", /* name of module */ 16540 &dtrace_ops, /* driver ops */ 16541}; 16542 16543static struct modlinkage modlinkage = { 16544 MODREV_1, 16545 (void *)&modldrv, 16546 NULL 16547}; 16548 16549int 16550_init(void) 16551{ 16552 return (mod_install(&modlinkage)); 16553} 16554 16555int 16556_info(struct modinfo *modinfop) 16557{ 16558 return (mod_info(&modlinkage, modinfop)); 16559} 16560 16561int 16562_fini(void) 16563{ 16564 return (mod_remove(&modlinkage)); 16565} 16566#else 16567 16568#if 0 16569static d_ioctl_t dtrace_ioctl; 16570static void dtrace_load(void *); 16571static int dtrace_unload(void); 16572#if __FreeBSD_version < 800039 16573static void dtrace_clone(void *, struct ucred *, char *, int , struct cdev **); 16574static struct clonedevs *dtrace_clones; /* Ptr to the array of cloned devices. */ 16575static eventhandler_tag eh_tag; /* Event handler tag. */ 16576#else 16577static struct cdev *dtrace_dev; 16578#endif 16579 16580void dtrace_invop_init(void); 16581void dtrace_invop_uninit(void); 16582 16583static struct cdevsw dtrace_cdevsw = { 16584 .d_version = D_VERSION, 16585 .d_flags = D_TRACKCLOSE | D_NEEDMINOR, 16586 .d_close = dtrace_close, 16587 .d_ioctl = dtrace_ioctl, 16588 .d_open = dtrace_open, 16589 .d_name = "dtrace", 16590}; 16591#endif 16592void dtrace_invop_init(void); 16593void dtrace_invop_uninit(void); 16594 16595static void dtrace_load(void *); 16596static int dtrace_unload(void); 16597 16598#include <dtrace_anon.c> 16599#include <dtrace_ioctl.c> 16600#include <dtrace_load.c> 16601#include <dtrace_modevent.c> 16602#include <dtrace_sysctl.c> 16603#include <dtrace_unload.c> 16604#include <dtrace_vtime.c> 16605#include <dtrace_hacks.c> 16606#include <dtrace_isa.c> 16607 16608MODULE(MODULE_CLASS_MISC, dtrace, "solaris"); 16609 16610#if 0 16611DEV_MODULE(dtrace, dtrace_modevent, NULL); 16612MODULE_VERSION(dtrace, 1); 16613MODULE_DEPEND(dtrace, cyclic, 1, 1, 1); 16614MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 16615#endif 16616#endif 16617 16618#if !defined(sun) 16619#undef mutex_init 16620 16621struct dtrace_state_worker { 16622 kmutex_t lock; 16623 kcondvar_t cv; 16624 void (*fn)(dtrace_state_t *); 16625 dtrace_state_t *state; 16626 int interval; 16627 lwp_t *lwp; 16628 bool exiting; 16629}; 16630 16631static void 16632dtrace_state_worker_thread(void *vp) 16633{ 16634 struct dtrace_state_worker *w = vp; 16635 16636 mutex_enter(&w->lock); 16637 while (!w->exiting) { 16638 int error; 16639 16640 error = cv_timedwait(&w->cv, &w->lock, w->interval); 16641 if (error == EWOULDBLOCK) { 16642 mutex_exit(&w->lock); 16643 w->fn(w->state); 16644 mutex_enter(&w->lock); 16645 } 16646 } 16647 mutex_exit(&w->lock); 16648 kthread_exit(0); 16649} 16650 16651struct dtrace_state_worker * 16652dtrace_state_worker_add(void (*fn)(dtrace_state_t *), dtrace_state_t *state, 16653 hrtime_t interval) 16654{ 16655 struct dtrace_state_worker *w; 16656 int error; 16657 16658 w = kmem_alloc(sizeof(*w), KM_SLEEP); 16659 mutex_init(&w->lock, MUTEX_DEFAULT, IPL_NONE); 16660 cv_init(&w->cv, "dtrace"); 16661 w->interval = ((uintmax_t)hz * interval) / NANOSEC, 16662 w->fn = fn; 16663 w->state = state; 16664 w->exiting = false; 16665 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE|KTHREAD_MUSTJOIN, NULL, 16666 dtrace_state_worker_thread, w, &w->lwp, "dtrace-state-worker"); 16667 KASSERT(error == 0); /* XXX */ 16668 return w; 16669} 16670 16671void 16672dtrace_state_worker_remove(struct dtrace_state_worker *w) 16673{ 16674 int error; 16675 16676 KASSERT(!w->exiting); 16677 mutex_enter(&w->lock); 16678 w->exiting = true; 16679 cv_signal(&w->cv); 16680 mutex_exit(&w->lock); 16681 error = kthread_join(w->lwp); 16682 KASSERT(error == 0); 16683 cv_destroy(&w->cv); 16684 mutex_destroy(&w->lock); 16685 kmem_free(w, sizeof(*w)); 16686} 16687#endif 16688