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: stable/11/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c 361088 2020-05-15 20:03:57Z dim $ 22 */ 23 24/* 25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 26 * Copyright (c) 2016, Joyent, Inc. All rights reserved. 27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved. 28 */ 29 30/* 31 * DTrace - Dynamic Tracing for Solaris 32 * 33 * This is the implementation of the Solaris Dynamic Tracing framework 34 * (DTrace). The user-visible interface to DTrace is described at length in 35 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 36 * library, the in-kernel DTrace framework, and the DTrace providers are 37 * described in the block comments in the <sys/dtrace.h> header file. The 38 * internal architecture of DTrace is described in the block comments in the 39 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 40 * implementation very much assume mastery of all of these sources; if one has 41 * an unanswered question about the implementation, one should consult them 42 * first. 43 * 44 * The functions here are ordered roughly as follows: 45 * 46 * - Probe context functions 47 * - Probe hashing functions 48 * - Non-probe context utility functions 49 * - Matching functions 50 * - Provider-to-Framework API functions 51 * - Probe management functions 52 * - DIF object functions 53 * - Format functions 54 * - Predicate functions 55 * - ECB functions 56 * - Buffer functions 57 * - Enabling functions 58 * - DOF functions 59 * - Anonymous enabling functions 60 * - Consumer state functions 61 * - Helper functions 62 * - Hook functions 63 * - Driver cookbook functions 64 * 65 * Each group of functions begins with a block comment labelled the "DTrace 66 * [Group] Functions", allowing one to find each block by searching forward 67 * on capital-f functions. 68 */ 69#include <sys/errno.h> 70#ifndef illumos 71#include <sys/time.h> 72#endif 73#include <sys/stat.h> 74#include <sys/modctl.h> 75#include <sys/conf.h> 76#include <sys/systm.h> 77#ifdef illumos 78#include <sys/ddi.h> 79#include <sys/sunddi.h> 80#endif 81#include <sys/cpuvar.h> 82#include <sys/kmem.h> 83#ifdef illumos 84#include <sys/strsubr.h> 85#endif 86#include <sys/sysmacros.h> 87#include <sys/dtrace_impl.h> 88#include <sys/atomic.h> 89#include <sys/cmn_err.h> 90#ifdef illumos 91#include <sys/mutex_impl.h> 92#include <sys/rwlock_impl.h> 93#endif 94#include <sys/ctf_api.h> 95#ifdef illumos 96#include <sys/panic.h> 97#include <sys/priv_impl.h> 98#endif 99#include <sys/policy.h> 100#ifdef illumos 101#include <sys/cred_impl.h> 102#include <sys/procfs_isa.h> 103#endif 104#include <sys/taskq.h> 105#ifdef illumos 106#include <sys/mkdev.h> 107#include <sys/kdi.h> 108#endif 109#include <sys/zone.h> 110#include <sys/socket.h> 111#include <netinet/in.h> 112#include "strtolctype.h" 113 114/* FreeBSD includes: */ 115#ifndef illumos 116#include <sys/callout.h> 117#include <sys/ctype.h> 118#include <sys/eventhandler.h> 119#include <sys/limits.h> 120#include <sys/linker.h> 121#include <sys/kdb.h> 122#include <sys/kernel.h> 123#include <sys/malloc.h> 124#include <sys/lock.h> 125#include <sys/mutex.h> 126#include <sys/ptrace.h> 127#include <sys/random.h> 128#include <sys/rwlock.h> 129#include <sys/sx.h> 130#include <sys/sysctl.h> 131 132#include <sys/dtrace_bsd.h> 133 134#include <netinet/in.h> 135 136#include "dtrace_cddl.h" 137#include "dtrace_debug.c" 138#endif 139 140#include "dtrace_xoroshiro128_plus.h" 141 142/* 143 * DTrace Tunable Variables 144 * 145 * The following variables may be tuned by adding a line to /etc/system that 146 * includes both the name of the DTrace module ("dtrace") and the name of the 147 * variable. For example: 148 * 149 * set dtrace:dtrace_destructive_disallow = 1 150 * 151 * In general, the only variables that one should be tuning this way are those 152 * that affect system-wide DTrace behavior, and for which the default behavior 153 * is undesirable. Most of these variables are tunable on a per-consumer 154 * basis using DTrace options, and need not be tuned on a system-wide basis. 155 * When tuning these variables, avoid pathological values; while some attempt 156 * is made to verify the integrity of these variables, they are not considered 157 * part of the supported interface to DTrace, and they are therefore not 158 * checked comprehensively. Further, these variables should not be tuned 159 * dynamically via "mdb -kw" or other means; they should only be tuned via 160 * /etc/system. 161 */ 162int dtrace_destructive_disallow = 0; 163#ifndef illumos 164/* Positive logic version of dtrace_destructive_disallow for loader tunable */ 165int dtrace_allow_destructive = 1; 166#endif 167dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 168size_t dtrace_difo_maxsize = (256 * 1024); 169dtrace_optval_t dtrace_dof_maxsize = (8 * 1024 * 1024); 170size_t dtrace_statvar_maxsize = (16 * 1024); 171size_t dtrace_actions_max = (16 * 1024); 172size_t dtrace_retain_max = 1024; 173dtrace_optval_t dtrace_helper_actions_max = 128; 174dtrace_optval_t dtrace_helper_providers_max = 32; 175dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 176size_t dtrace_strsize_default = 256; 177dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 178dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 179dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 180dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 181dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 182dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 183dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 184dtrace_optval_t dtrace_nspec_default = 1; 185dtrace_optval_t dtrace_specsize_default = 32 * 1024; 186dtrace_optval_t dtrace_stackframes_default = 20; 187dtrace_optval_t dtrace_ustackframes_default = 20; 188dtrace_optval_t dtrace_jstackframes_default = 50; 189dtrace_optval_t dtrace_jstackstrsize_default = 512; 190int dtrace_msgdsize_max = 128; 191hrtime_t dtrace_chill_max = MSEC2NSEC(500); /* 500 ms */ 192hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 193int dtrace_devdepth_max = 32; 194int dtrace_err_verbose; 195hrtime_t dtrace_deadman_interval = NANOSEC; 196hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 197hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 198hrtime_t dtrace_unregister_defunct_reap = (hrtime_t)60 * NANOSEC; 199#ifndef illumos 200int dtrace_memstr_max = 4096; 201#endif 202 203/* 204 * DTrace External Variables 205 * 206 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 207 * available to DTrace consumers via the backtick (`) syntax. One of these, 208 * dtrace_zero, is made deliberately so: it is provided as a source of 209 * well-known, zero-filled memory. While this variable is not documented, 210 * it is used by some translators as an implementation detail. 211 */ 212const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 213 214/* 215 * DTrace Internal Variables 216 */ 217#ifdef illumos 218static dev_info_t *dtrace_devi; /* device info */ 219#endif 220#ifdef illumos 221static vmem_t *dtrace_arena; /* probe ID arena */ 222static vmem_t *dtrace_minor; /* minor number arena */ 223#else 224static taskq_t *dtrace_taskq; /* task queue */ 225static struct unrhdr *dtrace_arena; /* Probe ID number. */ 226#endif 227static dtrace_probe_t **dtrace_probes; /* array of all probes */ 228static int dtrace_nprobes; /* number of probes */ 229static dtrace_provider_t *dtrace_provider; /* provider list */ 230static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 231static int dtrace_opens; /* number of opens */ 232static int dtrace_helpers; /* number of helpers */ 233static int dtrace_getf; /* number of unpriv getf()s */ 234#ifdef illumos 235static void *dtrace_softstate; /* softstate pointer */ 236#endif 237static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 238static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 239static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 240static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 241static int dtrace_toxranges; /* number of toxic ranges */ 242static int dtrace_toxranges_max; /* size of toxic range array */ 243static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 244static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 245static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 246static kthread_t *dtrace_panicked; /* panicking thread */ 247static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 248static dtrace_genid_t dtrace_probegen; /* current probe generation */ 249static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 250static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 251static dtrace_genid_t dtrace_retained_gen; /* current retained enab gen */ 252static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 253static int dtrace_dynvar_failclean; /* dynvars failed to clean */ 254#ifndef illumos 255static struct mtx dtrace_unr_mtx; 256MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 257static eventhandler_tag dtrace_kld_load_tag; 258static eventhandler_tag dtrace_kld_unload_try_tag; 259#endif 260 261/* 262 * DTrace Locking 263 * DTrace is protected by three (relatively coarse-grained) locks: 264 * 265 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 266 * including enabling state, probes, ECBs, consumer state, helper state, 267 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 268 * probe context is lock-free -- synchronization is handled via the 269 * dtrace_sync() cross call mechanism. 270 * 271 * (2) dtrace_provider_lock is required when manipulating provider state, or 272 * when provider state must be held constant. 273 * 274 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 275 * when meta provider state must be held constant. 276 * 277 * The lock ordering between these three locks is dtrace_meta_lock before 278 * dtrace_provider_lock before dtrace_lock. (In particular, there are 279 * several places where dtrace_provider_lock is held by the framework as it 280 * calls into the providers -- which then call back into the framework, 281 * grabbing dtrace_lock.) 282 * 283 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 284 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 285 * role as a coarse-grained lock; it is acquired before both of these locks. 286 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 287 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 288 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 289 * acquired _between_ dtrace_provider_lock and dtrace_lock. 290 */ 291static kmutex_t dtrace_lock; /* probe state lock */ 292static kmutex_t dtrace_provider_lock; /* provider state lock */ 293static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 294 295#ifndef illumos 296/* XXX FreeBSD hacks. */ 297#define cr_suid cr_svuid 298#define cr_sgid cr_svgid 299#define ipaddr_t in_addr_t 300#define mod_modname pathname 301#define vuprintf vprintf 302#define ttoproc(_a) ((_a)->td_proc) 303#define crgetzoneid(_a) 0 304#define SNOCD 0 305#define CPU_ON_INTR(_a) 0 306 307#define PRIV_EFFECTIVE (1 << 0) 308#define PRIV_DTRACE_KERNEL (1 << 1) 309#define PRIV_DTRACE_PROC (1 << 2) 310#define PRIV_DTRACE_USER (1 << 3) 311#define PRIV_PROC_OWNER (1 << 4) 312#define PRIV_PROC_ZONE (1 << 5) 313#define PRIV_ALL ~0 314 315SYSCTL_DECL(_debug_dtrace); 316SYSCTL_DECL(_kern_dtrace); 317#endif 318 319#ifdef illumos 320#define curcpu CPU->cpu_id 321#endif 322 323 324/* 325 * DTrace Provider Variables 326 * 327 * These are the variables relating to DTrace as a provider (that is, the 328 * provider of the BEGIN, END, and ERROR probes). 329 */ 330static dtrace_pattr_t dtrace_provider_attr = { 331{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 332{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 333{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 334{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 335{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 336}; 337 338static void 339dtrace_nullop(void) 340{} 341 342static dtrace_pops_t dtrace_provider_ops = { 343 .dtps_provide = (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 344 .dtps_provide_module = (void (*)(void *, modctl_t *))dtrace_nullop, 345 .dtps_enable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 346 .dtps_disable = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 347 .dtps_suspend = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 348 .dtps_resume = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 349 .dtps_getargdesc = NULL, 350 .dtps_getargval = NULL, 351 .dtps_usermode = NULL, 352 .dtps_destroy = (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 353}; 354 355static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 356static dtrace_id_t dtrace_probeid_end; /* special END probe */ 357dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 358 359/* 360 * DTrace Helper Tracing Variables 361 * 362 * These variables should be set dynamically to enable helper tracing. The 363 * only variables that should be set are dtrace_helptrace_enable (which should 364 * be set to a non-zero value to allocate helper tracing buffers on the next 365 * open of /dev/dtrace) and dtrace_helptrace_disable (which should be set to a 366 * non-zero value to deallocate helper tracing buffers on the next close of 367 * /dev/dtrace). When (and only when) helper tracing is disabled, the 368 * buffer size may also be set via dtrace_helptrace_bufsize. 369 */ 370int dtrace_helptrace_enable = 0; 371int dtrace_helptrace_disable = 0; 372int dtrace_helptrace_bufsize = 16 * 1024 * 1024; 373uint32_t dtrace_helptrace_nlocals; 374static dtrace_helptrace_t *dtrace_helptrace_buffer; 375static uint32_t dtrace_helptrace_next = 0; 376static int dtrace_helptrace_wrapped = 0; 377 378/* 379 * DTrace Error Hashing 380 * 381 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 382 * table. This is very useful for checking coverage of tests that are 383 * expected to induce DIF or DOF processing errors, and may be useful for 384 * debugging problems in the DIF code generator or in DOF generation . The 385 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 386 */ 387#ifdef DEBUG 388static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 389static const char *dtrace_errlast; 390static kthread_t *dtrace_errthread; 391static kmutex_t dtrace_errlock; 392#endif 393 394/* 395 * DTrace Macros and Constants 396 * 397 * These are various macros that are useful in various spots in the 398 * implementation, along with a few random constants that have no meaning 399 * outside of the implementation. There is no real structure to this cpp 400 * mishmash -- but is there ever? 401 */ 402#define DTRACE_HASHSTR(hash, probe) \ 403 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 404 405#define DTRACE_HASHNEXT(hash, probe) \ 406 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 407 408#define DTRACE_HASHPREV(hash, probe) \ 409 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 410 411#define DTRACE_HASHEQ(hash, lhs, rhs) \ 412 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 413 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 414 415#define DTRACE_AGGHASHSIZE_SLEW 17 416 417#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 418 419/* 420 * The key for a thread-local variable consists of the lower 61 bits of the 421 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 422 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 423 * equal to a variable identifier. This is necessary (but not sufficient) to 424 * assure that global associative arrays never collide with thread-local 425 * variables. To guarantee that they cannot collide, we must also define the 426 * order for keying dynamic variables. That order is: 427 * 428 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 429 * 430 * Because the variable-key and the tls-key are in orthogonal spaces, there is 431 * no way for a global variable key signature to match a thread-local key 432 * signature. 433 */ 434#ifdef illumos 435#define DTRACE_TLS_THRKEY(where) { \ 436 uint_t intr = 0; \ 437 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 438 for (; actv; actv >>= 1) \ 439 intr++; \ 440 ASSERT(intr < (1 << 3)); \ 441 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 442 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 443} 444#else 445#define DTRACE_TLS_THRKEY(where) { \ 446 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 447 uint_t intr = 0; \ 448 uint_t actv = _c->cpu_intr_actv; \ 449 for (; actv; actv >>= 1) \ 450 intr++; \ 451 ASSERT(intr < (1 << 3)); \ 452 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 453 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 454} 455#endif 456 457#define DT_BSWAP_8(x) ((x) & 0xff) 458#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 459#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 460#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 461 462#define DT_MASK_LO 0x00000000FFFFFFFFULL 463 464#define DTRACE_STORE(type, tomax, offset, what) \ 465 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 466 467#ifndef __x86 468#define DTRACE_ALIGNCHECK(addr, size, flags) \ 469 if (addr & (size - 1)) { \ 470 *flags |= CPU_DTRACE_BADALIGN; \ 471 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 472 return (0); \ 473 } 474#else 475#define DTRACE_ALIGNCHECK(addr, size, flags) 476#endif 477 478/* 479 * Test whether a range of memory starting at testaddr of size testsz falls 480 * within the range of memory described by addr, sz. We take care to avoid 481 * problems with overflow and underflow of the unsigned quantities, and 482 * disallow all negative sizes. Ranges of size 0 are allowed. 483 */ 484#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 485 ((testaddr) - (uintptr_t)(baseaddr) < (basesz) && \ 486 (testaddr) + (testsz) - (uintptr_t)(baseaddr) <= (basesz) && \ 487 (testaddr) + (testsz) >= (testaddr)) 488 489/* 490 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 491 * alloc_sz on the righthand side of the comparison in order to avoid overflow 492 * or underflow in the comparison with it. This is simpler than the INRANGE 493 * check above, because we know that the dtms_scratch_ptr is valid in the 494 * range. Allocations of size zero are allowed. 495 */ 496#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 497 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 498 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 499 500#define DTRACE_LOADFUNC(bits) \ 501/*CSTYLED*/ \ 502uint##bits##_t \ 503dtrace_load##bits(uintptr_t addr) \ 504{ \ 505 size_t size = bits / NBBY; \ 506 /*CSTYLED*/ \ 507 uint##bits##_t rval; \ 508 int i; \ 509 volatile uint16_t *flags = (volatile uint16_t *) \ 510 &cpu_core[curcpu].cpuc_dtrace_flags; \ 511 \ 512 DTRACE_ALIGNCHECK(addr, size, flags); \ 513 \ 514 for (i = 0; i < dtrace_toxranges; i++) { \ 515 if (addr >= dtrace_toxrange[i].dtt_limit) \ 516 continue; \ 517 \ 518 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 519 continue; \ 520 \ 521 /* \ 522 * This address falls within a toxic region; return 0. \ 523 */ \ 524 *flags |= CPU_DTRACE_BADADDR; \ 525 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 526 return (0); \ 527 } \ 528 \ 529 *flags |= CPU_DTRACE_NOFAULT; \ 530 /*CSTYLED*/ \ 531 rval = *((volatile uint##bits##_t *)addr); \ 532 *flags &= ~CPU_DTRACE_NOFAULT; \ 533 \ 534 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 535} 536 537#ifdef _LP64 538#define dtrace_loadptr dtrace_load64 539#else 540#define dtrace_loadptr dtrace_load32 541#endif 542 543#define DTRACE_DYNHASH_FREE 0 544#define DTRACE_DYNHASH_SINK 1 545#define DTRACE_DYNHASH_VALID 2 546 547#define DTRACE_MATCH_NEXT 0 548#define DTRACE_MATCH_DONE 1 549#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 550#define DTRACE_STATE_ALIGN 64 551 552#define DTRACE_FLAGS2FLT(flags) \ 553 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 554 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 555 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 556 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 557 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 558 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 559 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 560 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 561 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 562 DTRACEFLT_UNKNOWN) 563 564#define DTRACEACT_ISSTRING(act) \ 565 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 566 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 567 568/* Function prototype definitions: */ 569static size_t dtrace_strlen(const char *, size_t); 570static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 571static void dtrace_enabling_provide(dtrace_provider_t *); 572static int dtrace_enabling_match(dtrace_enabling_t *, int *); 573static void dtrace_enabling_matchall(void); 574static void dtrace_enabling_reap(void); 575static dtrace_state_t *dtrace_anon_grab(void); 576static uint64_t dtrace_helper(int, dtrace_mstate_t *, 577 dtrace_state_t *, uint64_t, uint64_t); 578static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 579static void dtrace_buffer_drop(dtrace_buffer_t *); 580static int dtrace_buffer_consumed(dtrace_buffer_t *, hrtime_t when); 581static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 582 dtrace_state_t *, dtrace_mstate_t *); 583static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 584 dtrace_optval_t); 585static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 586static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 587uint16_t dtrace_load16(uintptr_t); 588uint32_t dtrace_load32(uintptr_t); 589uint64_t dtrace_load64(uintptr_t); 590uint8_t dtrace_load8(uintptr_t); 591void dtrace_dynvar_clean(dtrace_dstate_t *); 592dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 593 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 594uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 595static int dtrace_priv_proc(dtrace_state_t *); 596static void dtrace_getf_barrier(void); 597 598/* 599 * DTrace Probe Context Functions 600 * 601 * These functions are called from probe context. Because probe context is 602 * any context in which C may be called, arbitrarily locks may be held, 603 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 604 * As a result, functions called from probe context may only call other DTrace 605 * support functions -- they may not interact at all with the system at large. 606 * (Note that the ASSERT macro is made probe-context safe by redefining it in 607 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 608 * loads are to be performed from probe context, they _must_ be in terms of 609 * the safe dtrace_load*() variants. 610 * 611 * Some functions in this block are not actually called from probe context; 612 * for these functions, there will be a comment above the function reading 613 * "Note: not called from probe context." 614 */ 615void 616dtrace_panic(const char *format, ...) 617{ 618 va_list alist; 619 620 va_start(alist, format); 621#ifdef __FreeBSD__ 622 vpanic(format, alist); 623#else 624 dtrace_vpanic(format, alist); 625#endif 626 va_end(alist); 627} 628 629int 630dtrace_assfail(const char *a, const char *f, int l) 631{ 632 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 633 634 /* 635 * We just need something here that even the most clever compiler 636 * cannot optimize away. 637 */ 638 return (a[(uintptr_t)f]); 639} 640 641/* 642 * Atomically increment a specified error counter from probe context. 643 */ 644static void 645dtrace_error(uint32_t *counter) 646{ 647 /* 648 * Most counters stored to in probe context are per-CPU counters. 649 * However, there are some error conditions that are sufficiently 650 * arcane that they don't merit per-CPU storage. If these counters 651 * are incremented concurrently on different CPUs, scalability will be 652 * adversely affected -- but we don't expect them to be white-hot in a 653 * correctly constructed enabling... 654 */ 655 uint32_t oval, nval; 656 657 do { 658 oval = *counter; 659 660 if ((nval = oval + 1) == 0) { 661 /* 662 * If the counter would wrap, set it to 1 -- assuring 663 * that the counter is never zero when we have seen 664 * errors. (The counter must be 32-bits because we 665 * aren't guaranteed a 64-bit compare&swap operation.) 666 * To save this code both the infamy of being fingered 667 * by a priggish news story and the indignity of being 668 * the target of a neo-puritan witch trial, we're 669 * carefully avoiding any colorful description of the 670 * likelihood of this condition -- but suffice it to 671 * say that it is only slightly more likely than the 672 * overflow of predicate cache IDs, as discussed in 673 * dtrace_predicate_create(). 674 */ 675 nval = 1; 676 } 677 } while (dtrace_cas32(counter, oval, nval) != oval); 678} 679 680/* 681 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 682 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 683 */ 684/* BEGIN CSTYLED */ 685DTRACE_LOADFUNC(8) 686DTRACE_LOADFUNC(16) 687DTRACE_LOADFUNC(32) 688DTRACE_LOADFUNC(64) 689/* END CSTYLED */ 690 691static int 692dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 693{ 694 if (dest < mstate->dtms_scratch_base) 695 return (0); 696 697 if (dest + size < dest) 698 return (0); 699 700 if (dest + size > mstate->dtms_scratch_ptr) 701 return (0); 702 703 return (1); 704} 705 706static int 707dtrace_canstore_statvar(uint64_t addr, size_t sz, 708 dtrace_statvar_t **svars, int nsvars) 709{ 710 int i; 711 size_t maxglobalsize, maxlocalsize; 712 713 if (nsvars == 0) 714 return (0); 715 716 maxglobalsize = dtrace_statvar_maxsize; 717 maxlocalsize = (maxglobalsize + sizeof (uint64_t)) * NCPU; 718 719 for (i = 0; i < nsvars; i++) { 720 dtrace_statvar_t *svar = svars[i]; 721 uint8_t scope; 722 size_t size; 723 724 if (svar == NULL || (size = svar->dtsv_size) == 0) 725 continue; 726 727 scope = svar->dtsv_var.dtdv_scope; 728 729 /* 730 * We verify that our size is valid in the spirit of providing 731 * defense in depth: we want to prevent attackers from using 732 * DTrace to escalate an orthogonal kernel heap corruption bug 733 * into the ability to store to arbitrary locations in memory. 734 */ 735 VERIFY((scope == DIFV_SCOPE_GLOBAL && size < maxglobalsize) || 736 (scope == DIFV_SCOPE_LOCAL && size < maxlocalsize)); 737 738 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 739 return (1); 740 } 741 742 return (0); 743} 744 745/* 746 * Check to see if the address is within a memory region to which a store may 747 * be issued. This includes the DTrace scratch areas, and any DTrace variable 748 * region. The caller of dtrace_canstore() is responsible for performing any 749 * alignment checks that are needed before stores are actually executed. 750 */ 751static int 752dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 753 dtrace_vstate_t *vstate) 754{ 755 /* 756 * First, check to see if the address is in scratch space... 757 */ 758 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 759 mstate->dtms_scratch_size)) 760 return (1); 761 762 /* 763 * Now check to see if it's a dynamic variable. This check will pick 764 * up both thread-local variables and any global dynamically-allocated 765 * variables. 766 */ 767 if (DTRACE_INRANGE(addr, sz, vstate->dtvs_dynvars.dtds_base, 768 vstate->dtvs_dynvars.dtds_size)) { 769 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 770 uintptr_t base = (uintptr_t)dstate->dtds_base + 771 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 772 uintptr_t chunkoffs; 773 dtrace_dynvar_t *dvar; 774 775 /* 776 * Before we assume that we can store here, we need to make 777 * sure that it isn't in our metadata -- storing to our 778 * dynamic variable metadata would corrupt our state. For 779 * the range to not include any dynamic variable metadata, 780 * it must: 781 * 782 * (1) Start above the hash table that is at the base of 783 * the dynamic variable space 784 * 785 * (2) Have a starting chunk offset that is beyond the 786 * dtrace_dynvar_t that is at the base of every chunk 787 * 788 * (3) Not span a chunk boundary 789 * 790 * (4) Not be in the tuple space of a dynamic variable 791 * 792 */ 793 if (addr < base) 794 return (0); 795 796 chunkoffs = (addr - base) % dstate->dtds_chunksize; 797 798 if (chunkoffs < sizeof (dtrace_dynvar_t)) 799 return (0); 800 801 if (chunkoffs + sz > dstate->dtds_chunksize) 802 return (0); 803 804 dvar = (dtrace_dynvar_t *)((uintptr_t)addr - chunkoffs); 805 806 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) 807 return (0); 808 809 if (chunkoffs < sizeof (dtrace_dynvar_t) + 810 ((dvar->dtdv_tuple.dtt_nkeys - 1) * sizeof (dtrace_key_t))) 811 return (0); 812 813 return (1); 814 } 815 816 /* 817 * Finally, check the static local and global variables. These checks 818 * take the longest, so we perform them last. 819 */ 820 if (dtrace_canstore_statvar(addr, sz, 821 vstate->dtvs_locals, vstate->dtvs_nlocals)) 822 return (1); 823 824 if (dtrace_canstore_statvar(addr, sz, 825 vstate->dtvs_globals, vstate->dtvs_nglobals)) 826 return (1); 827 828 return (0); 829} 830 831 832/* 833 * Convenience routine to check to see if the address is within a memory 834 * region in which a load may be issued given the user's privilege level; 835 * if not, it sets the appropriate error flags and loads 'addr' into the 836 * illegal value slot. 837 * 838 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 839 * appropriate memory access protection. 840 */ 841static int 842dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 843 dtrace_vstate_t *vstate) 844{ 845 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 846 file_t *fp; 847 848 /* 849 * If we hold the privilege to read from kernel memory, then 850 * everything is readable. 851 */ 852 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 853 return (1); 854 855 /* 856 * You can obviously read that which you can store. 857 */ 858 if (dtrace_canstore(addr, sz, mstate, vstate)) 859 return (1); 860 861 /* 862 * We're allowed to read from our own string table. 863 */ 864 if (DTRACE_INRANGE(addr, sz, mstate->dtms_difo->dtdo_strtab, 865 mstate->dtms_difo->dtdo_strlen)) 866 return (1); 867 868 if (vstate->dtvs_state != NULL && 869 dtrace_priv_proc(vstate->dtvs_state)) { 870 proc_t *p; 871 872 /* 873 * When we have privileges to the current process, there are 874 * several context-related kernel structures that are safe to 875 * read, even absent the privilege to read from kernel memory. 876 * These reads are safe because these structures contain only 877 * state that (1) we're permitted to read, (2) is harmless or 878 * (3) contains pointers to additional kernel state that we're 879 * not permitted to read (and as such, do not present an 880 * opportunity for privilege escalation). Finally (and 881 * critically), because of the nature of their relation with 882 * the current thread context, the memory associated with these 883 * structures cannot change over the duration of probe context, 884 * and it is therefore impossible for this memory to be 885 * deallocated and reallocated as something else while it's 886 * being operated upon. 887 */ 888 if (DTRACE_INRANGE(addr, sz, curthread, sizeof (kthread_t))) 889 return (1); 890 891 if ((p = curthread->t_procp) != NULL && DTRACE_INRANGE(addr, 892 sz, curthread->t_procp, sizeof (proc_t))) { 893 return (1); 894 } 895 896 if (curthread->t_cred != NULL && DTRACE_INRANGE(addr, sz, 897 curthread->t_cred, sizeof (cred_t))) { 898 return (1); 899 } 900 901#ifdef illumos 902 if (p != NULL && p->p_pidp != NULL && DTRACE_INRANGE(addr, sz, 903 &(p->p_pidp->pid_id), sizeof (pid_t))) { 904 return (1); 905 } 906 907 if (curthread->t_cpu != NULL && DTRACE_INRANGE(addr, sz, 908 curthread->t_cpu, offsetof(cpu_t, cpu_pause_thread))) { 909 return (1); 910 } 911#endif 912 } 913 914 if ((fp = mstate->dtms_getf) != NULL) { 915 uintptr_t psz = sizeof (void *); 916 vnode_t *vp; 917 vnodeops_t *op; 918 919 /* 920 * When getf() returns a file_t, the enabling is implicitly 921 * granted the (transient) right to read the returned file_t 922 * as well as the v_path and v_op->vnop_name of the underlying 923 * vnode. These accesses are allowed after a successful 924 * getf() because the members that they refer to cannot change 925 * once set -- and the barrier logic in the kernel's closef() 926 * path assures that the file_t and its referenced vode_t 927 * cannot themselves be stale (that is, it impossible for 928 * either dtms_getf itself or its f_vnode member to reference 929 * freed memory). 930 */ 931 if (DTRACE_INRANGE(addr, sz, fp, sizeof (file_t))) 932 return (1); 933 934 if ((vp = fp->f_vnode) != NULL) { 935#ifdef illumos 936 if (DTRACE_INRANGE(addr, sz, &vp->v_path, psz)) 937 return (1); 938 if (vp->v_path != NULL && DTRACE_INRANGE(addr, sz, 939 vp->v_path, strlen(vp->v_path) + 1)) { 940 return (1); 941 } 942#endif 943 944 if (DTRACE_INRANGE(addr, sz, &vp->v_op, psz)) 945 return (1); 946 947#ifdef illumos 948 if ((op = vp->v_op) != NULL && 949 DTRACE_INRANGE(addr, sz, &op->vnop_name, psz)) { 950 return (1); 951 } 952 953 if (op != NULL && op->vnop_name != NULL && 954 DTRACE_INRANGE(addr, sz, op->vnop_name, 955 strlen(op->vnop_name) + 1)) { 956 return (1); 957 } 958#endif 959 } 960 } 961 962 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 963 *illval = addr; 964 return (0); 965} 966 967/* 968 * Convenience routine to check to see if a given string is within a memory 969 * region in which a load may be issued given the user's privilege level; 970 * this exists so that we don't need to issue unnecessary dtrace_strlen() 971 * calls in the event that the user has all privileges. 972 */ 973static int 974dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 975 dtrace_vstate_t *vstate) 976{ 977 size_t strsz; 978 979 /* 980 * If we hold the privilege to read from kernel memory, then 981 * everything is readable. 982 */ 983 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 984 return (1); 985 986 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 987 if (dtrace_canload(addr, strsz, mstate, vstate)) 988 return (1); 989 990 return (0); 991} 992 993/* 994 * Convenience routine to check to see if a given variable is within a memory 995 * region in which a load may be issued given the user's privilege level. 996 */ 997static int 998dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 999 dtrace_vstate_t *vstate) 1000{ 1001 size_t sz; 1002 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1003 1004 /* 1005 * If we hold the privilege to read from kernel memory, then 1006 * everything is readable. 1007 */ 1008 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 1009 return (1); 1010 1011 if (type->dtdt_kind == DIF_TYPE_STRING) 1012 sz = dtrace_strlen(src, 1013 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 1014 else 1015 sz = type->dtdt_size; 1016 1017 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 1018} 1019 1020/* 1021 * Convert a string to a signed integer using safe loads. 1022 * 1023 * NOTE: This function uses various macros from strtolctype.h to manipulate 1024 * digit values, etc -- these have all been checked to ensure they make 1025 * no additional function calls. 1026 */ 1027static int64_t 1028dtrace_strtoll(char *input, int base, size_t limit) 1029{ 1030 uintptr_t pos = (uintptr_t)input; 1031 int64_t val = 0; 1032 int x; 1033 boolean_t neg = B_FALSE; 1034 char c, cc, ccc; 1035 uintptr_t end = pos + limit; 1036 1037 /* 1038 * Consume any whitespace preceding digits. 1039 */ 1040 while ((c = dtrace_load8(pos)) == ' ' || c == '\t') 1041 pos++; 1042 1043 /* 1044 * Handle an explicit sign if one is present. 1045 */ 1046 if (c == '-' || c == '+') { 1047 if (c == '-') 1048 neg = B_TRUE; 1049 c = dtrace_load8(++pos); 1050 } 1051 1052 /* 1053 * Check for an explicit hexadecimal prefix ("0x" or "0X") and skip it 1054 * if present. 1055 */ 1056 if (base == 16 && c == '0' && ((cc = dtrace_load8(pos + 1)) == 'x' || 1057 cc == 'X') && isxdigit(ccc = dtrace_load8(pos + 2))) { 1058 pos += 2; 1059 c = ccc; 1060 } 1061 1062 /* 1063 * Read in contiguous digits until the first non-digit character. 1064 */ 1065 for (; pos < end && c != '\0' && lisalnum(c) && (x = DIGIT(c)) < base; 1066 c = dtrace_load8(++pos)) 1067 val = val * base + x; 1068 1069 return (neg ? -val : val); 1070} 1071 1072/* 1073 * Compare two strings using safe loads. 1074 */ 1075static int 1076dtrace_strncmp(char *s1, char *s2, size_t limit) 1077{ 1078 uint8_t c1, c2; 1079 volatile uint16_t *flags; 1080 1081 if (s1 == s2 || limit == 0) 1082 return (0); 1083 1084 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1085 1086 do { 1087 if (s1 == NULL) { 1088 c1 = '\0'; 1089 } else { 1090 c1 = dtrace_load8((uintptr_t)s1++); 1091 } 1092 1093 if (s2 == NULL) { 1094 c2 = '\0'; 1095 } else { 1096 c2 = dtrace_load8((uintptr_t)s2++); 1097 } 1098 1099 if (c1 != c2) 1100 return (c1 - c2); 1101 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 1102 1103 return (0); 1104} 1105 1106/* 1107 * Compute strlen(s) for a string using safe memory accesses. The additional 1108 * len parameter is used to specify a maximum length to ensure completion. 1109 */ 1110static size_t 1111dtrace_strlen(const char *s, size_t lim) 1112{ 1113 uint_t len; 1114 1115 for (len = 0; len != lim; len++) { 1116 if (dtrace_load8((uintptr_t)s++) == '\0') 1117 break; 1118 } 1119 1120 return (len); 1121} 1122 1123/* 1124 * Check if an address falls within a toxic region. 1125 */ 1126static int 1127dtrace_istoxic(uintptr_t kaddr, size_t size) 1128{ 1129 uintptr_t taddr, tsize; 1130 int i; 1131 1132 for (i = 0; i < dtrace_toxranges; i++) { 1133 taddr = dtrace_toxrange[i].dtt_base; 1134 tsize = dtrace_toxrange[i].dtt_limit - taddr; 1135 1136 if (kaddr - taddr < tsize) { 1137 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1138 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 1139 return (1); 1140 } 1141 1142 if (taddr - kaddr < size) { 1143 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 1144 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 1145 return (1); 1146 } 1147 } 1148 1149 return (0); 1150} 1151 1152/* 1153 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 1154 * memory specified by the DIF program. The dst is assumed to be safe memory 1155 * that we can store to directly because it is managed by DTrace. As with 1156 * standard bcopy, overlapping copies are handled properly. 1157 */ 1158static void 1159dtrace_bcopy(const void *src, void *dst, size_t len) 1160{ 1161 if (len != 0) { 1162 uint8_t *s1 = dst; 1163 const uint8_t *s2 = src; 1164 1165 if (s1 <= s2) { 1166 do { 1167 *s1++ = dtrace_load8((uintptr_t)s2++); 1168 } while (--len != 0); 1169 } else { 1170 s2 += len; 1171 s1 += len; 1172 1173 do { 1174 *--s1 = dtrace_load8((uintptr_t)--s2); 1175 } while (--len != 0); 1176 } 1177 } 1178} 1179 1180/* 1181 * Copy src to dst using safe memory accesses, up to either the specified 1182 * length, or the point that a nul byte is encountered. The src is assumed to 1183 * be unsafe memory specified by the DIF program. The dst is assumed to be 1184 * safe memory that we can store to directly because it is managed by DTrace. 1185 * Unlike dtrace_bcopy(), overlapping regions are not handled. 1186 */ 1187static void 1188dtrace_strcpy(const void *src, void *dst, size_t len) 1189{ 1190 if (len != 0) { 1191 uint8_t *s1 = dst, c; 1192 const uint8_t *s2 = src; 1193 1194 do { 1195 *s1++ = c = dtrace_load8((uintptr_t)s2++); 1196 } while (--len != 0 && c != '\0'); 1197 } 1198} 1199 1200/* 1201 * Copy src to dst, deriving the size and type from the specified (BYREF) 1202 * variable type. The src is assumed to be unsafe memory specified by the DIF 1203 * program. The dst is assumed to be DTrace variable memory that is of the 1204 * specified type; we assume that we can store to directly. 1205 */ 1206static void 1207dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 1208{ 1209 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1210 1211 if (type->dtdt_kind == DIF_TYPE_STRING) { 1212 dtrace_strcpy(src, dst, type->dtdt_size); 1213 } else { 1214 dtrace_bcopy(src, dst, type->dtdt_size); 1215 } 1216} 1217 1218/* 1219 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1220 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1221 * safe memory that we can access directly because it is managed by DTrace. 1222 */ 1223static int 1224dtrace_bcmp(const void *s1, const void *s2, size_t len) 1225{ 1226 volatile uint16_t *flags; 1227 1228 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1229 1230 if (s1 == s2) 1231 return (0); 1232 1233 if (s1 == NULL || s2 == NULL) 1234 return (1); 1235 1236 if (s1 != s2 && len != 0) { 1237 const uint8_t *ps1 = s1; 1238 const uint8_t *ps2 = s2; 1239 1240 do { 1241 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1242 return (1); 1243 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1244 } 1245 return (0); 1246} 1247 1248/* 1249 * Zero the specified region using a simple byte-by-byte loop. Note that this 1250 * is for safe DTrace-managed memory only. 1251 */ 1252static void 1253dtrace_bzero(void *dst, size_t len) 1254{ 1255 uchar_t *cp; 1256 1257 for (cp = dst; len != 0; len--) 1258 *cp++ = 0; 1259} 1260 1261static void 1262dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1263{ 1264 uint64_t result[2]; 1265 1266 result[0] = addend1[0] + addend2[0]; 1267 result[1] = addend1[1] + addend2[1] + 1268 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1269 1270 sum[0] = result[0]; 1271 sum[1] = result[1]; 1272} 1273 1274/* 1275 * Shift the 128-bit value in a by b. If b is positive, shift left. 1276 * If b is negative, shift right. 1277 */ 1278static void 1279dtrace_shift_128(uint64_t *a, int b) 1280{ 1281 uint64_t mask; 1282 1283 if (b == 0) 1284 return; 1285 1286 if (b < 0) { 1287 b = -b; 1288 if (b >= 64) { 1289 a[0] = a[1] >> (b - 64); 1290 a[1] = 0; 1291 } else { 1292 a[0] >>= b; 1293 mask = 1LL << (64 - b); 1294 mask -= 1; 1295 a[0] |= ((a[1] & mask) << (64 - b)); 1296 a[1] >>= b; 1297 } 1298 } else { 1299 if (b >= 64) { 1300 a[1] = a[0] << (b - 64); 1301 a[0] = 0; 1302 } else { 1303 a[1] <<= b; 1304 mask = a[0] >> (64 - b); 1305 a[1] |= mask; 1306 a[0] <<= b; 1307 } 1308 } 1309} 1310 1311/* 1312 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1313 * use native multiplication on those, and then re-combine into the 1314 * resulting 128-bit value. 1315 * 1316 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1317 * hi1 * hi2 << 64 + 1318 * hi1 * lo2 << 32 + 1319 * hi2 * lo1 << 32 + 1320 * lo1 * lo2 1321 */ 1322static void 1323dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1324{ 1325 uint64_t hi1, hi2, lo1, lo2; 1326 uint64_t tmp[2]; 1327 1328 hi1 = factor1 >> 32; 1329 hi2 = factor2 >> 32; 1330 1331 lo1 = factor1 & DT_MASK_LO; 1332 lo2 = factor2 & DT_MASK_LO; 1333 1334 product[0] = lo1 * lo2; 1335 product[1] = hi1 * hi2; 1336 1337 tmp[0] = hi1 * lo2; 1338 tmp[1] = 0; 1339 dtrace_shift_128(tmp, 32); 1340 dtrace_add_128(product, tmp, product); 1341 1342 tmp[0] = hi2 * lo1; 1343 tmp[1] = 0; 1344 dtrace_shift_128(tmp, 32); 1345 dtrace_add_128(product, tmp, product); 1346} 1347 1348/* 1349 * This privilege check should be used by actions and subroutines to 1350 * verify that the user credentials of the process that enabled the 1351 * invoking ECB match the target credentials 1352 */ 1353static int 1354dtrace_priv_proc_common_user(dtrace_state_t *state) 1355{ 1356 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1357 1358 /* 1359 * We should always have a non-NULL state cred here, since if cred 1360 * is null (anonymous tracing), we fast-path bypass this routine. 1361 */ 1362 ASSERT(s_cr != NULL); 1363 1364 if ((cr = CRED()) != NULL && 1365 s_cr->cr_uid == cr->cr_uid && 1366 s_cr->cr_uid == cr->cr_ruid && 1367 s_cr->cr_uid == cr->cr_suid && 1368 s_cr->cr_gid == cr->cr_gid && 1369 s_cr->cr_gid == cr->cr_rgid && 1370 s_cr->cr_gid == cr->cr_sgid) 1371 return (1); 1372 1373 return (0); 1374} 1375 1376/* 1377 * This privilege check should be used by actions and subroutines to 1378 * verify that the zone of the process that enabled the invoking ECB 1379 * matches the target credentials 1380 */ 1381static int 1382dtrace_priv_proc_common_zone(dtrace_state_t *state) 1383{ 1384#ifdef illumos 1385 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1386 1387 /* 1388 * We should always have a non-NULL state cred here, since if cred 1389 * is null (anonymous tracing), we fast-path bypass this routine. 1390 */ 1391 ASSERT(s_cr != NULL); 1392 1393 if ((cr = CRED()) != NULL && s_cr->cr_zone == cr->cr_zone) 1394 return (1); 1395 1396 return (0); 1397#else 1398 return (1); 1399#endif 1400} 1401 1402/* 1403 * This privilege check should be used by actions and subroutines to 1404 * verify that the process has not setuid or changed credentials. 1405 */ 1406static int 1407dtrace_priv_proc_common_nocd(void) 1408{ 1409 proc_t *proc; 1410 1411 if ((proc = ttoproc(curthread)) != NULL && 1412 !(proc->p_flag & SNOCD)) 1413 return (1); 1414 1415 return (0); 1416} 1417 1418static int 1419dtrace_priv_proc_destructive(dtrace_state_t *state) 1420{ 1421 int action = state->dts_cred.dcr_action; 1422 1423 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1424 dtrace_priv_proc_common_zone(state) == 0) 1425 goto bad; 1426 1427 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1428 dtrace_priv_proc_common_user(state) == 0) 1429 goto bad; 1430 1431 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1432 dtrace_priv_proc_common_nocd() == 0) 1433 goto bad; 1434 1435 return (1); 1436 1437bad: 1438 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1439 1440 return (0); 1441} 1442 1443static int 1444dtrace_priv_proc_control(dtrace_state_t *state) 1445{ 1446 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1447 return (1); 1448 1449 if (dtrace_priv_proc_common_zone(state) && 1450 dtrace_priv_proc_common_user(state) && 1451 dtrace_priv_proc_common_nocd()) 1452 return (1); 1453 1454 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1455 1456 return (0); 1457} 1458 1459static int 1460dtrace_priv_proc(dtrace_state_t *state) 1461{ 1462 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1463 return (1); 1464 1465 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1466 1467 return (0); 1468} 1469 1470static int 1471dtrace_priv_kernel(dtrace_state_t *state) 1472{ 1473 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1474 return (1); 1475 1476 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1477 1478 return (0); 1479} 1480 1481static int 1482dtrace_priv_kernel_destructive(dtrace_state_t *state) 1483{ 1484 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1485 return (1); 1486 1487 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1488 1489 return (0); 1490} 1491 1492/* 1493 * Determine if the dte_cond of the specified ECB allows for processing of 1494 * the current probe to continue. Note that this routine may allow continued 1495 * processing, but with access(es) stripped from the mstate's dtms_access 1496 * field. 1497 */ 1498static int 1499dtrace_priv_probe(dtrace_state_t *state, dtrace_mstate_t *mstate, 1500 dtrace_ecb_t *ecb) 1501{ 1502 dtrace_probe_t *probe = ecb->dte_probe; 1503 dtrace_provider_t *prov = probe->dtpr_provider; 1504 dtrace_pops_t *pops = &prov->dtpv_pops; 1505 int mode = DTRACE_MODE_NOPRIV_DROP; 1506 1507 ASSERT(ecb->dte_cond); 1508 1509#ifdef illumos 1510 if (pops->dtps_mode != NULL) { 1511 mode = pops->dtps_mode(prov->dtpv_arg, 1512 probe->dtpr_id, probe->dtpr_arg); 1513 1514 ASSERT((mode & DTRACE_MODE_USER) || 1515 (mode & DTRACE_MODE_KERNEL)); 1516 ASSERT((mode & DTRACE_MODE_NOPRIV_RESTRICT) || 1517 (mode & DTRACE_MODE_NOPRIV_DROP)); 1518 } 1519 1520 /* 1521 * If the dte_cond bits indicate that this consumer is only allowed to 1522 * see user-mode firings of this probe, call the provider's dtps_mode() 1523 * entry point to check that the probe was fired while in a user 1524 * context. If that's not the case, use the policy specified by the 1525 * provider to determine if we drop the probe or merely restrict 1526 * operation. 1527 */ 1528 if (ecb->dte_cond & DTRACE_COND_USERMODE) { 1529 ASSERT(mode != DTRACE_MODE_NOPRIV_DROP); 1530 1531 if (!(mode & DTRACE_MODE_USER)) { 1532 if (mode & DTRACE_MODE_NOPRIV_DROP) 1533 return (0); 1534 1535 mstate->dtms_access &= ~DTRACE_ACCESS_ARGS; 1536 } 1537 } 1538#endif 1539 1540 /* 1541 * This is more subtle than it looks. We have to be absolutely certain 1542 * that CRED() isn't going to change out from under us so it's only 1543 * legit to examine that structure if we're in constrained situations. 1544 * Currently, the only times we'll this check is if a non-super-user 1545 * has enabled the profile or syscall providers -- providers that 1546 * allow visibility of all processes. For the profile case, the check 1547 * above will ensure that we're examining a user context. 1548 */ 1549 if (ecb->dte_cond & DTRACE_COND_OWNER) { 1550 cred_t *cr; 1551 cred_t *s_cr = state->dts_cred.dcr_cred; 1552 proc_t *proc; 1553 1554 ASSERT(s_cr != NULL); 1555 1556 if ((cr = CRED()) == NULL || 1557 s_cr->cr_uid != cr->cr_uid || 1558 s_cr->cr_uid != cr->cr_ruid || 1559 s_cr->cr_uid != cr->cr_suid || 1560 s_cr->cr_gid != cr->cr_gid || 1561 s_cr->cr_gid != cr->cr_rgid || 1562 s_cr->cr_gid != cr->cr_sgid || 1563 (proc = ttoproc(curthread)) == NULL || 1564 (proc->p_flag & SNOCD)) { 1565 if (mode & DTRACE_MODE_NOPRIV_DROP) 1566 return (0); 1567 1568#ifdef illumos 1569 mstate->dtms_access &= ~DTRACE_ACCESS_PROC; 1570#endif 1571 } 1572 } 1573 1574#ifdef illumos 1575 /* 1576 * If our dte_cond is set to DTRACE_COND_ZONEOWNER and we are not 1577 * in our zone, check to see if our mode policy is to restrict rather 1578 * than to drop; if to restrict, strip away both DTRACE_ACCESS_PROC 1579 * and DTRACE_ACCESS_ARGS 1580 */ 1581 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 1582 cred_t *cr; 1583 cred_t *s_cr = state->dts_cred.dcr_cred; 1584 1585 ASSERT(s_cr != NULL); 1586 1587 if ((cr = CRED()) == NULL || 1588 s_cr->cr_zone->zone_id != cr->cr_zone->zone_id) { 1589 if (mode & DTRACE_MODE_NOPRIV_DROP) 1590 return (0); 1591 1592 mstate->dtms_access &= 1593 ~(DTRACE_ACCESS_PROC | DTRACE_ACCESS_ARGS); 1594 } 1595 } 1596#endif 1597 1598 return (1); 1599} 1600 1601/* 1602 * Note: not called from probe context. This function is called 1603 * asynchronously (and at a regular interval) from outside of probe context to 1604 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1605 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1606 */ 1607void 1608dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1609{ 1610 dtrace_dynvar_t *dirty; 1611 dtrace_dstate_percpu_t *dcpu; 1612 dtrace_dynvar_t **rinsep; 1613 int i, j, work = 0; 1614 1615 for (i = 0; i < NCPU; i++) { 1616 dcpu = &dstate->dtds_percpu[i]; 1617 rinsep = &dcpu->dtdsc_rinsing; 1618 1619 /* 1620 * If the dirty list is NULL, there is no dirty work to do. 1621 */ 1622 if (dcpu->dtdsc_dirty == NULL) 1623 continue; 1624 1625 if (dcpu->dtdsc_rinsing != NULL) { 1626 /* 1627 * If the rinsing list is non-NULL, then it is because 1628 * this CPU was selected to accept another CPU's 1629 * dirty list -- and since that time, dirty buffers 1630 * have accumulated. This is a highly unlikely 1631 * condition, but we choose to ignore the dirty 1632 * buffers -- they'll be picked up a future cleanse. 1633 */ 1634 continue; 1635 } 1636 1637 if (dcpu->dtdsc_clean != NULL) { 1638 /* 1639 * If the clean list is non-NULL, then we're in a 1640 * situation where a CPU has done deallocations (we 1641 * have a non-NULL dirty list) but no allocations (we 1642 * also have a non-NULL clean list). We can't simply 1643 * move the dirty list into the clean list on this 1644 * CPU, yet we also don't want to allow this condition 1645 * to persist, lest a short clean list prevent a 1646 * massive dirty list from being cleaned (which in 1647 * turn could lead to otherwise avoidable dynamic 1648 * drops). To deal with this, we look for some CPU 1649 * with a NULL clean list, NULL dirty list, and NULL 1650 * rinsing list -- and then we borrow this CPU to 1651 * rinse our dirty list. 1652 */ 1653 for (j = 0; j < NCPU; j++) { 1654 dtrace_dstate_percpu_t *rinser; 1655 1656 rinser = &dstate->dtds_percpu[j]; 1657 1658 if (rinser->dtdsc_rinsing != NULL) 1659 continue; 1660 1661 if (rinser->dtdsc_dirty != NULL) 1662 continue; 1663 1664 if (rinser->dtdsc_clean != NULL) 1665 continue; 1666 1667 rinsep = &rinser->dtdsc_rinsing; 1668 break; 1669 } 1670 1671 if (j == NCPU) { 1672 /* 1673 * We were unable to find another CPU that 1674 * could accept this dirty list -- we are 1675 * therefore unable to clean it now. 1676 */ 1677 dtrace_dynvar_failclean++; 1678 continue; 1679 } 1680 } 1681 1682 work = 1; 1683 1684 /* 1685 * Atomically move the dirty list aside. 1686 */ 1687 do { 1688 dirty = dcpu->dtdsc_dirty; 1689 1690 /* 1691 * Before we zap the dirty list, set the rinsing list. 1692 * (This allows for a potential assertion in 1693 * dtrace_dynvar(): if a free dynamic variable appears 1694 * on a hash chain, either the dirty list or the 1695 * rinsing list for some CPU must be non-NULL.) 1696 */ 1697 *rinsep = dirty; 1698 dtrace_membar_producer(); 1699 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1700 dirty, NULL) != dirty); 1701 } 1702 1703 if (!work) { 1704 /* 1705 * We have no work to do; we can simply return. 1706 */ 1707 return; 1708 } 1709 1710 dtrace_sync(); 1711 1712 for (i = 0; i < NCPU; i++) { 1713 dcpu = &dstate->dtds_percpu[i]; 1714 1715 if (dcpu->dtdsc_rinsing == NULL) 1716 continue; 1717 1718 /* 1719 * We are now guaranteed that no hash chain contains a pointer 1720 * into this dirty list; we can make it clean. 1721 */ 1722 ASSERT(dcpu->dtdsc_clean == NULL); 1723 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1724 dcpu->dtdsc_rinsing = NULL; 1725 } 1726 1727 /* 1728 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1729 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1730 * This prevents a race whereby a CPU incorrectly decides that 1731 * the state should be something other than DTRACE_DSTATE_CLEAN 1732 * after dtrace_dynvar_clean() has completed. 1733 */ 1734 dtrace_sync(); 1735 1736 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1737} 1738 1739/* 1740 * Depending on the value of the op parameter, this function looks-up, 1741 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1742 * allocation is requested, this function will return a pointer to a 1743 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1744 * variable can be allocated. If NULL is returned, the appropriate counter 1745 * will be incremented. 1746 */ 1747dtrace_dynvar_t * 1748dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1749 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1750 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1751{ 1752 uint64_t hashval = DTRACE_DYNHASH_VALID; 1753 dtrace_dynhash_t *hash = dstate->dtds_hash; 1754 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1755 processorid_t me = curcpu, cpu = me; 1756 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1757 size_t bucket, ksize; 1758 size_t chunksize = dstate->dtds_chunksize; 1759 uintptr_t kdata, lock, nstate; 1760 uint_t i; 1761 1762 ASSERT(nkeys != 0); 1763 1764 /* 1765 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1766 * algorithm. For the by-value portions, we perform the algorithm in 1767 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1768 * bit, and seems to have only a minute effect on distribution. For 1769 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1770 * over each referenced byte. It's painful to do this, but it's much 1771 * better than pathological hash distribution. The efficacy of the 1772 * hashing algorithm (and a comparison with other algorithms) may be 1773 * found by running the ::dtrace_dynstat MDB dcmd. 1774 */ 1775 for (i = 0; i < nkeys; i++) { 1776 if (key[i].dttk_size == 0) { 1777 uint64_t val = key[i].dttk_value; 1778 1779 hashval += (val >> 48) & 0xffff; 1780 hashval += (hashval << 10); 1781 hashval ^= (hashval >> 6); 1782 1783 hashval += (val >> 32) & 0xffff; 1784 hashval += (hashval << 10); 1785 hashval ^= (hashval >> 6); 1786 1787 hashval += (val >> 16) & 0xffff; 1788 hashval += (hashval << 10); 1789 hashval ^= (hashval >> 6); 1790 1791 hashval += val & 0xffff; 1792 hashval += (hashval << 10); 1793 hashval ^= (hashval >> 6); 1794 } else { 1795 /* 1796 * This is incredibly painful, but it beats the hell 1797 * out of the alternative. 1798 */ 1799 uint64_t j, size = key[i].dttk_size; 1800 uintptr_t base = (uintptr_t)key[i].dttk_value; 1801 1802 if (!dtrace_canload(base, size, mstate, vstate)) 1803 break; 1804 1805 for (j = 0; j < size; j++) { 1806 hashval += dtrace_load8(base + j); 1807 hashval += (hashval << 10); 1808 hashval ^= (hashval >> 6); 1809 } 1810 } 1811 } 1812 1813 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1814 return (NULL); 1815 1816 hashval += (hashval << 3); 1817 hashval ^= (hashval >> 11); 1818 hashval += (hashval << 15); 1819 1820 /* 1821 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1822 * comes out to be one of our two sentinel hash values. If this 1823 * actually happens, we set the hashval to be a value known to be a 1824 * non-sentinel value. 1825 */ 1826 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1827 hashval = DTRACE_DYNHASH_VALID; 1828 1829 /* 1830 * Yes, it's painful to do a divide here. If the cycle count becomes 1831 * important here, tricks can be pulled to reduce it. (However, it's 1832 * critical that hash collisions be kept to an absolute minimum; 1833 * they're much more painful than a divide.) It's better to have a 1834 * solution that generates few collisions and still keeps things 1835 * relatively simple. 1836 */ 1837 bucket = hashval % dstate->dtds_hashsize; 1838 1839 if (op == DTRACE_DYNVAR_DEALLOC) { 1840 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1841 1842 for (;;) { 1843 while ((lock = *lockp) & 1) 1844 continue; 1845 1846 if (dtrace_casptr((volatile void *)lockp, 1847 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1848 break; 1849 } 1850 1851 dtrace_membar_producer(); 1852 } 1853 1854top: 1855 prev = NULL; 1856 lock = hash[bucket].dtdh_lock; 1857 1858 dtrace_membar_consumer(); 1859 1860 start = hash[bucket].dtdh_chain; 1861 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1862 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1863 op != DTRACE_DYNVAR_DEALLOC)); 1864 1865 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1866 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1867 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1868 1869 if (dvar->dtdv_hashval != hashval) { 1870 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1871 /* 1872 * We've reached the sink, and therefore the 1873 * end of the hash chain; we can kick out of 1874 * the loop knowing that we have seen a valid 1875 * snapshot of state. 1876 */ 1877 ASSERT(dvar->dtdv_next == NULL); 1878 ASSERT(dvar == &dtrace_dynhash_sink); 1879 break; 1880 } 1881 1882 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1883 /* 1884 * We've gone off the rails: somewhere along 1885 * the line, one of the members of this hash 1886 * chain was deleted. Note that we could also 1887 * detect this by simply letting this loop run 1888 * to completion, as we would eventually hit 1889 * the end of the dirty list. However, we 1890 * want to avoid running the length of the 1891 * dirty list unnecessarily (it might be quite 1892 * long), so we catch this as early as 1893 * possible by detecting the hash marker. In 1894 * this case, we simply set dvar to NULL and 1895 * break; the conditional after the loop will 1896 * send us back to top. 1897 */ 1898 dvar = NULL; 1899 break; 1900 } 1901 1902 goto next; 1903 } 1904 1905 if (dtuple->dtt_nkeys != nkeys) 1906 goto next; 1907 1908 for (i = 0; i < nkeys; i++, dkey++) { 1909 if (dkey->dttk_size != key[i].dttk_size) 1910 goto next; /* size or type mismatch */ 1911 1912 if (dkey->dttk_size != 0) { 1913 if (dtrace_bcmp( 1914 (void *)(uintptr_t)key[i].dttk_value, 1915 (void *)(uintptr_t)dkey->dttk_value, 1916 dkey->dttk_size)) 1917 goto next; 1918 } else { 1919 if (dkey->dttk_value != key[i].dttk_value) 1920 goto next; 1921 } 1922 } 1923 1924 if (op != DTRACE_DYNVAR_DEALLOC) 1925 return (dvar); 1926 1927 ASSERT(dvar->dtdv_next == NULL || 1928 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1929 1930 if (prev != NULL) { 1931 ASSERT(hash[bucket].dtdh_chain != dvar); 1932 ASSERT(start != dvar); 1933 ASSERT(prev->dtdv_next == dvar); 1934 prev->dtdv_next = dvar->dtdv_next; 1935 } else { 1936 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1937 start, dvar->dtdv_next) != start) { 1938 /* 1939 * We have failed to atomically swing the 1940 * hash table head pointer, presumably because 1941 * of a conflicting allocation on another CPU. 1942 * We need to reread the hash chain and try 1943 * again. 1944 */ 1945 goto top; 1946 } 1947 } 1948 1949 dtrace_membar_producer(); 1950 1951 /* 1952 * Now set the hash value to indicate that it's free. 1953 */ 1954 ASSERT(hash[bucket].dtdh_chain != dvar); 1955 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1956 1957 dtrace_membar_producer(); 1958 1959 /* 1960 * Set the next pointer to point at the dirty list, and 1961 * atomically swing the dirty pointer to the newly freed dvar. 1962 */ 1963 do { 1964 next = dcpu->dtdsc_dirty; 1965 dvar->dtdv_next = next; 1966 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1967 1968 /* 1969 * Finally, unlock this hash bucket. 1970 */ 1971 ASSERT(hash[bucket].dtdh_lock == lock); 1972 ASSERT(lock & 1); 1973 hash[bucket].dtdh_lock++; 1974 1975 return (NULL); 1976next: 1977 prev = dvar; 1978 continue; 1979 } 1980 1981 if (dvar == NULL) { 1982 /* 1983 * If dvar is NULL, it is because we went off the rails: 1984 * one of the elements that we traversed in the hash chain 1985 * was deleted while we were traversing it. In this case, 1986 * we assert that we aren't doing a dealloc (deallocs lock 1987 * the hash bucket to prevent themselves from racing with 1988 * one another), and retry the hash chain traversal. 1989 */ 1990 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1991 goto top; 1992 } 1993 1994 if (op != DTRACE_DYNVAR_ALLOC) { 1995 /* 1996 * If we are not to allocate a new variable, we want to 1997 * return NULL now. Before we return, check that the value 1998 * of the lock word hasn't changed. If it has, we may have 1999 * seen an inconsistent snapshot. 2000 */ 2001 if (op == DTRACE_DYNVAR_NOALLOC) { 2002 if (hash[bucket].dtdh_lock != lock) 2003 goto top; 2004 } else { 2005 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 2006 ASSERT(hash[bucket].dtdh_lock == lock); 2007 ASSERT(lock & 1); 2008 hash[bucket].dtdh_lock++; 2009 } 2010 2011 return (NULL); 2012 } 2013 2014 /* 2015 * We need to allocate a new dynamic variable. The size we need is the 2016 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 2017 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 2018 * the size of any referred-to data (dsize). We then round the final 2019 * size up to the chunksize for allocation. 2020 */ 2021 for (ksize = 0, i = 0; i < nkeys; i++) 2022 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 2023 2024 /* 2025 * This should be pretty much impossible, but could happen if, say, 2026 * strange DIF specified the tuple. Ideally, this should be an 2027 * assertion and not an error condition -- but that requires that the 2028 * chunksize calculation in dtrace_difo_chunksize() be absolutely 2029 * bullet-proof. (That is, it must not be able to be fooled by 2030 * malicious DIF.) Given the lack of backwards branches in DIF, 2031 * solving this would presumably not amount to solving the Halting 2032 * Problem -- but it still seems awfully hard. 2033 */ 2034 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 2035 ksize + dsize > chunksize) { 2036 dcpu->dtdsc_drops++; 2037 return (NULL); 2038 } 2039 2040 nstate = DTRACE_DSTATE_EMPTY; 2041 2042 do { 2043retry: 2044 free = dcpu->dtdsc_free; 2045 2046 if (free == NULL) { 2047 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 2048 void *rval; 2049 2050 if (clean == NULL) { 2051 /* 2052 * We're out of dynamic variable space on 2053 * this CPU. Unless we have tried all CPUs, 2054 * we'll try to allocate from a different 2055 * CPU. 2056 */ 2057 switch (dstate->dtds_state) { 2058 case DTRACE_DSTATE_CLEAN: { 2059 void *sp = &dstate->dtds_state; 2060 2061 if (++cpu >= NCPU) 2062 cpu = 0; 2063 2064 if (dcpu->dtdsc_dirty != NULL && 2065 nstate == DTRACE_DSTATE_EMPTY) 2066 nstate = DTRACE_DSTATE_DIRTY; 2067 2068 if (dcpu->dtdsc_rinsing != NULL) 2069 nstate = DTRACE_DSTATE_RINSING; 2070 2071 dcpu = &dstate->dtds_percpu[cpu]; 2072 2073 if (cpu != me) 2074 goto retry; 2075 2076 (void) dtrace_cas32(sp, 2077 DTRACE_DSTATE_CLEAN, nstate); 2078 2079 /* 2080 * To increment the correct bean 2081 * counter, take another lap. 2082 */ 2083 goto retry; 2084 } 2085 2086 case DTRACE_DSTATE_DIRTY: 2087 dcpu->dtdsc_dirty_drops++; 2088 break; 2089 2090 case DTRACE_DSTATE_RINSING: 2091 dcpu->dtdsc_rinsing_drops++; 2092 break; 2093 2094 case DTRACE_DSTATE_EMPTY: 2095 dcpu->dtdsc_drops++; 2096 break; 2097 } 2098 2099 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 2100 return (NULL); 2101 } 2102 2103 /* 2104 * The clean list appears to be non-empty. We want to 2105 * move the clean list to the free list; we start by 2106 * moving the clean pointer aside. 2107 */ 2108 if (dtrace_casptr(&dcpu->dtdsc_clean, 2109 clean, NULL) != clean) { 2110 /* 2111 * We are in one of two situations: 2112 * 2113 * (a) The clean list was switched to the 2114 * free list by another CPU. 2115 * 2116 * (b) The clean list was added to by the 2117 * cleansing cyclic. 2118 * 2119 * In either of these situations, we can 2120 * just reattempt the free list allocation. 2121 */ 2122 goto retry; 2123 } 2124 2125 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 2126 2127 /* 2128 * Now we'll move the clean list to our free list. 2129 * It's impossible for this to fail: the only way 2130 * the free list can be updated is through this 2131 * code path, and only one CPU can own the clean list. 2132 * Thus, it would only be possible for this to fail if 2133 * this code were racing with dtrace_dynvar_clean(). 2134 * (That is, if dtrace_dynvar_clean() updated the clean 2135 * list, and we ended up racing to update the free 2136 * list.) This race is prevented by the dtrace_sync() 2137 * in dtrace_dynvar_clean() -- which flushes the 2138 * owners of the clean lists out before resetting 2139 * the clean lists. 2140 */ 2141 dcpu = &dstate->dtds_percpu[me]; 2142 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 2143 ASSERT(rval == NULL); 2144 goto retry; 2145 } 2146 2147 dvar = free; 2148 new_free = dvar->dtdv_next; 2149 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 2150 2151 /* 2152 * We have now allocated a new chunk. We copy the tuple keys into the 2153 * tuple array and copy any referenced key data into the data space 2154 * following the tuple array. As we do this, we relocate dttk_value 2155 * in the final tuple to point to the key data address in the chunk. 2156 */ 2157 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 2158 dvar->dtdv_data = (void *)(kdata + ksize); 2159 dvar->dtdv_tuple.dtt_nkeys = nkeys; 2160 2161 for (i = 0; i < nkeys; i++) { 2162 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 2163 size_t kesize = key[i].dttk_size; 2164 2165 if (kesize != 0) { 2166 dtrace_bcopy( 2167 (const void *)(uintptr_t)key[i].dttk_value, 2168 (void *)kdata, kesize); 2169 dkey->dttk_value = kdata; 2170 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 2171 } else { 2172 dkey->dttk_value = key[i].dttk_value; 2173 } 2174 2175 dkey->dttk_size = kesize; 2176 } 2177 2178 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 2179 dvar->dtdv_hashval = hashval; 2180 dvar->dtdv_next = start; 2181 2182 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 2183 return (dvar); 2184 2185 /* 2186 * The cas has failed. Either another CPU is adding an element to 2187 * this hash chain, or another CPU is deleting an element from this 2188 * hash chain. The simplest way to deal with both of these cases 2189 * (though not necessarily the most efficient) is to free our 2190 * allocated block and re-attempt it all. Note that the free is 2191 * to the dirty list and _not_ to the free list. This is to prevent 2192 * races with allocators, above. 2193 */ 2194 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 2195 2196 dtrace_membar_producer(); 2197 2198 do { 2199 free = dcpu->dtdsc_dirty; 2200 dvar->dtdv_next = free; 2201 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 2202 2203 goto top; 2204} 2205 2206/*ARGSUSED*/ 2207static void 2208dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 2209{ 2210 if ((int64_t)nval < (int64_t)*oval) 2211 *oval = nval; 2212} 2213 2214/*ARGSUSED*/ 2215static void 2216dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 2217{ 2218 if ((int64_t)nval > (int64_t)*oval) 2219 *oval = nval; 2220} 2221 2222static void 2223dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 2224{ 2225 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 2226 int64_t val = (int64_t)nval; 2227 2228 if (val < 0) { 2229 for (i = 0; i < zero; i++) { 2230 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 2231 quanta[i] += incr; 2232 return; 2233 } 2234 } 2235 } else { 2236 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 2237 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 2238 quanta[i - 1] += incr; 2239 return; 2240 } 2241 } 2242 2243 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 2244 return; 2245 } 2246 2247 ASSERT(0); 2248} 2249 2250static void 2251dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 2252{ 2253 uint64_t arg = *lquanta++; 2254 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 2255 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 2256 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 2257 int32_t val = (int32_t)nval, level; 2258 2259 ASSERT(step != 0); 2260 ASSERT(levels != 0); 2261 2262 if (val < base) { 2263 /* 2264 * This is an underflow. 2265 */ 2266 lquanta[0] += incr; 2267 return; 2268 } 2269 2270 level = (val - base) / step; 2271 2272 if (level < levels) { 2273 lquanta[level + 1] += incr; 2274 return; 2275 } 2276 2277 /* 2278 * This is an overflow. 2279 */ 2280 lquanta[levels + 1] += incr; 2281} 2282 2283static int 2284dtrace_aggregate_llquantize_bucket(uint16_t factor, uint16_t low, 2285 uint16_t high, uint16_t nsteps, int64_t value) 2286{ 2287 int64_t this = 1, last, next; 2288 int base = 1, order; 2289 2290 ASSERT(factor <= nsteps); 2291 ASSERT(nsteps % factor == 0); 2292 2293 for (order = 0; order < low; order++) 2294 this *= factor; 2295 2296 /* 2297 * If our value is less than our factor taken to the power of the 2298 * low order of magnitude, it goes into the zeroth bucket. 2299 */ 2300 if (value < (last = this)) 2301 return (0); 2302 2303 for (this *= factor; order <= high; order++) { 2304 int nbuckets = this > nsteps ? nsteps : this; 2305 2306 if ((next = this * factor) < this) { 2307 /* 2308 * We should not generally get log/linear quantizations 2309 * with a high magnitude that allows 64-bits to 2310 * overflow, but we nonetheless protect against this 2311 * by explicitly checking for overflow, and clamping 2312 * our value accordingly. 2313 */ 2314 value = this - 1; 2315 } 2316 2317 if (value < this) { 2318 /* 2319 * If our value lies within this order of magnitude, 2320 * determine its position by taking the offset within 2321 * the order of magnitude, dividing by the bucket 2322 * width, and adding to our (accumulated) base. 2323 */ 2324 return (base + (value - last) / (this / nbuckets)); 2325 } 2326 2327 base += nbuckets - (nbuckets / factor); 2328 last = this; 2329 this = next; 2330 } 2331 2332 /* 2333 * Our value is greater than or equal to our factor taken to the 2334 * power of one plus the high magnitude -- return the top bucket. 2335 */ 2336 return (base); 2337} 2338 2339static void 2340dtrace_aggregate_llquantize(uint64_t *llquanta, uint64_t nval, uint64_t incr) 2341{ 2342 uint64_t arg = *llquanta++; 2343 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(arg); 2344 uint16_t low = DTRACE_LLQUANTIZE_LOW(arg); 2345 uint16_t high = DTRACE_LLQUANTIZE_HIGH(arg); 2346 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(arg); 2347 2348 llquanta[dtrace_aggregate_llquantize_bucket(factor, 2349 low, high, nsteps, nval)] += incr; 2350} 2351 2352/*ARGSUSED*/ 2353static void 2354dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 2355{ 2356 data[0]++; 2357 data[1] += nval; 2358} 2359 2360/*ARGSUSED*/ 2361static void 2362dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 2363{ 2364 int64_t snval = (int64_t)nval; 2365 uint64_t tmp[2]; 2366 2367 data[0]++; 2368 data[1] += nval; 2369 2370 /* 2371 * What we want to say here is: 2372 * 2373 * data[2] += nval * nval; 2374 * 2375 * But given that nval is 64-bit, we could easily overflow, so 2376 * we do this as 128-bit arithmetic. 2377 */ 2378 if (snval < 0) 2379 snval = -snval; 2380 2381 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 2382 dtrace_add_128(data + 2, tmp, data + 2); 2383} 2384 2385/*ARGSUSED*/ 2386static void 2387dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 2388{ 2389 *oval = *oval + 1; 2390} 2391 2392/*ARGSUSED*/ 2393static void 2394dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 2395{ 2396 *oval += nval; 2397} 2398 2399/* 2400 * Aggregate given the tuple in the principal data buffer, and the aggregating 2401 * action denoted by the specified dtrace_aggregation_t. The aggregation 2402 * buffer is specified as the buf parameter. This routine does not return 2403 * failure; if there is no space in the aggregation buffer, the data will be 2404 * dropped, and a corresponding counter incremented. 2405 */ 2406static void 2407dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 2408 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 2409{ 2410 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 2411 uint32_t i, ndx, size, fsize; 2412 uint32_t align = sizeof (uint64_t) - 1; 2413 dtrace_aggbuffer_t *agb; 2414 dtrace_aggkey_t *key; 2415 uint32_t hashval = 0, limit, isstr; 2416 caddr_t tomax, data, kdata; 2417 dtrace_actkind_t action; 2418 dtrace_action_t *act; 2419 uintptr_t offs; 2420 2421 if (buf == NULL) 2422 return; 2423 2424 if (!agg->dtag_hasarg) { 2425 /* 2426 * Currently, only quantize() and lquantize() take additional 2427 * arguments, and they have the same semantics: an increment 2428 * value that defaults to 1 when not present. If additional 2429 * aggregating actions take arguments, the setting of the 2430 * default argument value will presumably have to become more 2431 * sophisticated... 2432 */ 2433 arg = 1; 2434 } 2435 2436 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2437 size = rec->dtrd_offset - agg->dtag_base; 2438 fsize = size + rec->dtrd_size; 2439 2440 ASSERT(dbuf->dtb_tomax != NULL); 2441 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2442 2443 if ((tomax = buf->dtb_tomax) == NULL) { 2444 dtrace_buffer_drop(buf); 2445 return; 2446 } 2447 2448 /* 2449 * The metastructure is always at the bottom of the buffer. 2450 */ 2451 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2452 sizeof (dtrace_aggbuffer_t)); 2453 2454 if (buf->dtb_offset == 0) { 2455 /* 2456 * We just kludge up approximately 1/8th of the size to be 2457 * buckets. If this guess ends up being routinely 2458 * off-the-mark, we may need to dynamically readjust this 2459 * based on past performance. 2460 */ 2461 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2462 2463 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2464 (uintptr_t)tomax || hashsize == 0) { 2465 /* 2466 * We've been given a ludicrously small buffer; 2467 * increment our drop count and leave. 2468 */ 2469 dtrace_buffer_drop(buf); 2470 return; 2471 } 2472 2473 /* 2474 * And now, a pathetic attempt to try to get a an odd (or 2475 * perchance, a prime) hash size for better hash distribution. 2476 */ 2477 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2478 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2479 2480 agb->dtagb_hashsize = hashsize; 2481 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2482 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2483 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2484 2485 for (i = 0; i < agb->dtagb_hashsize; i++) 2486 agb->dtagb_hash[i] = NULL; 2487 } 2488 2489 ASSERT(agg->dtag_first != NULL); 2490 ASSERT(agg->dtag_first->dta_intuple); 2491 2492 /* 2493 * Calculate the hash value based on the key. Note that we _don't_ 2494 * include the aggid in the hashing (but we will store it as part of 2495 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2496 * algorithm: a simple, quick algorithm that has no known funnels, and 2497 * gets good distribution in practice. The efficacy of the hashing 2498 * algorithm (and a comparison with other algorithms) may be found by 2499 * running the ::dtrace_aggstat MDB dcmd. 2500 */ 2501 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2502 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2503 limit = i + act->dta_rec.dtrd_size; 2504 ASSERT(limit <= size); 2505 isstr = DTRACEACT_ISSTRING(act); 2506 2507 for (; i < limit; i++) { 2508 hashval += data[i]; 2509 hashval += (hashval << 10); 2510 hashval ^= (hashval >> 6); 2511 2512 if (isstr && data[i] == '\0') 2513 break; 2514 } 2515 } 2516 2517 hashval += (hashval << 3); 2518 hashval ^= (hashval >> 11); 2519 hashval += (hashval << 15); 2520 2521 /* 2522 * Yes, the divide here is expensive -- but it's generally the least 2523 * of the performance issues given the amount of data that we iterate 2524 * over to compute hash values, compare data, etc. 2525 */ 2526 ndx = hashval % agb->dtagb_hashsize; 2527 2528 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2529 ASSERT((caddr_t)key >= tomax); 2530 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2531 2532 if (hashval != key->dtak_hashval || key->dtak_size != size) 2533 continue; 2534 2535 kdata = key->dtak_data; 2536 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2537 2538 for (act = agg->dtag_first; act->dta_intuple; 2539 act = act->dta_next) { 2540 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2541 limit = i + act->dta_rec.dtrd_size; 2542 ASSERT(limit <= size); 2543 isstr = DTRACEACT_ISSTRING(act); 2544 2545 for (; i < limit; i++) { 2546 if (kdata[i] != data[i]) 2547 goto next; 2548 2549 if (isstr && data[i] == '\0') 2550 break; 2551 } 2552 } 2553 2554 if (action != key->dtak_action) { 2555 /* 2556 * We are aggregating on the same value in the same 2557 * aggregation with two different aggregating actions. 2558 * (This should have been picked up in the compiler, 2559 * so we may be dealing with errant or devious DIF.) 2560 * This is an error condition; we indicate as much, 2561 * and return. 2562 */ 2563 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2564 return; 2565 } 2566 2567 /* 2568 * This is a hit: we need to apply the aggregator to 2569 * the value at this key. 2570 */ 2571 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2572 return; 2573next: 2574 continue; 2575 } 2576 2577 /* 2578 * We didn't find it. We need to allocate some zero-filled space, 2579 * link it into the hash table appropriately, and apply the aggregator 2580 * to the (zero-filled) value. 2581 */ 2582 offs = buf->dtb_offset; 2583 while (offs & (align - 1)) 2584 offs += sizeof (uint32_t); 2585 2586 /* 2587 * If we don't have enough room to both allocate a new key _and_ 2588 * its associated data, increment the drop count and return. 2589 */ 2590 if ((uintptr_t)tomax + offs + fsize > 2591 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2592 dtrace_buffer_drop(buf); 2593 return; 2594 } 2595 2596 /*CONSTCOND*/ 2597 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2598 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2599 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2600 2601 key->dtak_data = kdata = tomax + offs; 2602 buf->dtb_offset = offs + fsize; 2603 2604 /* 2605 * Now copy the data across. 2606 */ 2607 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2608 2609 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2610 kdata[i] = data[i]; 2611 2612 /* 2613 * Because strings are not zeroed out by default, we need to iterate 2614 * looking for actions that store strings, and we need to explicitly 2615 * pad these strings out with zeroes. 2616 */ 2617 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2618 int nul; 2619 2620 if (!DTRACEACT_ISSTRING(act)) 2621 continue; 2622 2623 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2624 limit = i + act->dta_rec.dtrd_size; 2625 ASSERT(limit <= size); 2626 2627 for (nul = 0; i < limit; i++) { 2628 if (nul) { 2629 kdata[i] = '\0'; 2630 continue; 2631 } 2632 2633 if (data[i] != '\0') 2634 continue; 2635 2636 nul = 1; 2637 } 2638 } 2639 2640 for (i = size; i < fsize; i++) 2641 kdata[i] = 0; 2642 2643 key->dtak_hashval = hashval; 2644 key->dtak_size = size; 2645 key->dtak_action = action; 2646 key->dtak_next = agb->dtagb_hash[ndx]; 2647 agb->dtagb_hash[ndx] = key; 2648 2649 /* 2650 * Finally, apply the aggregator. 2651 */ 2652 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2653 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2654} 2655 2656/* 2657 * Given consumer state, this routine finds a speculation in the INACTIVE 2658 * state and transitions it into the ACTIVE state. If there is no speculation 2659 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2660 * incremented -- it is up to the caller to take appropriate action. 2661 */ 2662static int 2663dtrace_speculation(dtrace_state_t *state) 2664{ 2665 int i = 0; 2666 dtrace_speculation_state_t current; 2667 uint32_t *stat = &state->dts_speculations_unavail, count; 2668 2669 while (i < state->dts_nspeculations) { 2670 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2671 2672 current = spec->dtsp_state; 2673 2674 if (current != DTRACESPEC_INACTIVE) { 2675 if (current == DTRACESPEC_COMMITTINGMANY || 2676 current == DTRACESPEC_COMMITTING || 2677 current == DTRACESPEC_DISCARDING) 2678 stat = &state->dts_speculations_busy; 2679 i++; 2680 continue; 2681 } 2682 2683 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2684 current, DTRACESPEC_ACTIVE) == current) 2685 return (i + 1); 2686 } 2687 2688 /* 2689 * We couldn't find a speculation. If we found as much as a single 2690 * busy speculation buffer, we'll attribute this failure as "busy" 2691 * instead of "unavail". 2692 */ 2693 do { 2694 count = *stat; 2695 } while (dtrace_cas32(stat, count, count + 1) != count); 2696 2697 return (0); 2698} 2699 2700/* 2701 * This routine commits an active speculation. If the specified speculation 2702 * is not in a valid state to perform a commit(), this routine will silently do 2703 * nothing. The state of the specified speculation is transitioned according 2704 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2705 */ 2706static void 2707dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2708 dtrace_specid_t which) 2709{ 2710 dtrace_speculation_t *spec; 2711 dtrace_buffer_t *src, *dest; 2712 uintptr_t daddr, saddr, dlimit, slimit; 2713 dtrace_speculation_state_t current, new = 0; 2714 intptr_t offs; 2715 uint64_t timestamp; 2716 2717 if (which == 0) 2718 return; 2719 2720 if (which > state->dts_nspeculations) { 2721 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2722 return; 2723 } 2724 2725 spec = &state->dts_speculations[which - 1]; 2726 src = &spec->dtsp_buffer[cpu]; 2727 dest = &state->dts_buffer[cpu]; 2728 2729 do { 2730 current = spec->dtsp_state; 2731 2732 if (current == DTRACESPEC_COMMITTINGMANY) 2733 break; 2734 2735 switch (current) { 2736 case DTRACESPEC_INACTIVE: 2737 case DTRACESPEC_DISCARDING: 2738 return; 2739 2740 case DTRACESPEC_COMMITTING: 2741 /* 2742 * This is only possible if we are (a) commit()'ing 2743 * without having done a prior speculate() on this CPU 2744 * and (b) racing with another commit() on a different 2745 * CPU. There's nothing to do -- we just assert that 2746 * our offset is 0. 2747 */ 2748 ASSERT(src->dtb_offset == 0); 2749 return; 2750 2751 case DTRACESPEC_ACTIVE: 2752 new = DTRACESPEC_COMMITTING; 2753 break; 2754 2755 case DTRACESPEC_ACTIVEONE: 2756 /* 2757 * This speculation is active on one CPU. If our 2758 * buffer offset is non-zero, we know that the one CPU 2759 * must be us. Otherwise, we are committing on a 2760 * different CPU from the speculate(), and we must 2761 * rely on being asynchronously cleaned. 2762 */ 2763 if (src->dtb_offset != 0) { 2764 new = DTRACESPEC_COMMITTING; 2765 break; 2766 } 2767 /*FALLTHROUGH*/ 2768 2769 case DTRACESPEC_ACTIVEMANY: 2770 new = DTRACESPEC_COMMITTINGMANY; 2771 break; 2772 2773 default: 2774 ASSERT(0); 2775 } 2776 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2777 current, new) != current); 2778 2779 /* 2780 * We have set the state to indicate that we are committing this 2781 * speculation. Now reserve the necessary space in the destination 2782 * buffer. 2783 */ 2784 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2785 sizeof (uint64_t), state, NULL)) < 0) { 2786 dtrace_buffer_drop(dest); 2787 goto out; 2788 } 2789 2790 /* 2791 * We have sufficient space to copy the speculative buffer into the 2792 * primary buffer. First, modify the speculative buffer, filling 2793 * in the timestamp of all entries with the current time. The data 2794 * must have the commit() time rather than the time it was traced, 2795 * so that all entries in the primary buffer are in timestamp order. 2796 */ 2797 timestamp = dtrace_gethrtime(); 2798 saddr = (uintptr_t)src->dtb_tomax; 2799 slimit = saddr + src->dtb_offset; 2800 while (saddr < slimit) { 2801 size_t size; 2802 dtrace_rechdr_t *dtrh = (dtrace_rechdr_t *)saddr; 2803 2804 if (dtrh->dtrh_epid == DTRACE_EPIDNONE) { 2805 saddr += sizeof (dtrace_epid_t); 2806 continue; 2807 } 2808 ASSERT3U(dtrh->dtrh_epid, <=, state->dts_necbs); 2809 size = state->dts_ecbs[dtrh->dtrh_epid - 1]->dte_size; 2810 2811 ASSERT3U(saddr + size, <=, slimit); 2812 ASSERT3U(size, >=, sizeof (dtrace_rechdr_t)); 2813 ASSERT3U(DTRACE_RECORD_LOAD_TIMESTAMP(dtrh), ==, UINT64_MAX); 2814 2815 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, timestamp); 2816 2817 saddr += size; 2818 } 2819 2820 /* 2821 * Copy the buffer across. (Note that this is a 2822 * highly subobtimal bcopy(); in the unlikely event that this becomes 2823 * a serious performance issue, a high-performance DTrace-specific 2824 * bcopy() should obviously be invented.) 2825 */ 2826 daddr = (uintptr_t)dest->dtb_tomax + offs; 2827 dlimit = daddr + src->dtb_offset; 2828 saddr = (uintptr_t)src->dtb_tomax; 2829 2830 /* 2831 * First, the aligned portion. 2832 */ 2833 while (dlimit - daddr >= sizeof (uint64_t)) { 2834 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2835 2836 daddr += sizeof (uint64_t); 2837 saddr += sizeof (uint64_t); 2838 } 2839 2840 /* 2841 * Now any left-over bit... 2842 */ 2843 while (dlimit - daddr) 2844 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2845 2846 /* 2847 * Finally, commit the reserved space in the destination buffer. 2848 */ 2849 dest->dtb_offset = offs + src->dtb_offset; 2850 2851out: 2852 /* 2853 * If we're lucky enough to be the only active CPU on this speculation 2854 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2855 */ 2856 if (current == DTRACESPEC_ACTIVE || 2857 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2858 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2859 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2860 2861 ASSERT(rval == DTRACESPEC_COMMITTING); 2862 } 2863 2864 src->dtb_offset = 0; 2865 src->dtb_xamot_drops += src->dtb_drops; 2866 src->dtb_drops = 0; 2867} 2868 2869/* 2870 * This routine discards an active speculation. If the specified speculation 2871 * is not in a valid state to perform a discard(), this routine will silently 2872 * do nothing. The state of the specified speculation is transitioned 2873 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2874 */ 2875static void 2876dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2877 dtrace_specid_t which) 2878{ 2879 dtrace_speculation_t *spec; 2880 dtrace_speculation_state_t current, new = 0; 2881 dtrace_buffer_t *buf; 2882 2883 if (which == 0) 2884 return; 2885 2886 if (which > state->dts_nspeculations) { 2887 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2888 return; 2889 } 2890 2891 spec = &state->dts_speculations[which - 1]; 2892 buf = &spec->dtsp_buffer[cpu]; 2893 2894 do { 2895 current = spec->dtsp_state; 2896 2897 switch (current) { 2898 case DTRACESPEC_INACTIVE: 2899 case DTRACESPEC_COMMITTINGMANY: 2900 case DTRACESPEC_COMMITTING: 2901 case DTRACESPEC_DISCARDING: 2902 return; 2903 2904 case DTRACESPEC_ACTIVE: 2905 case DTRACESPEC_ACTIVEMANY: 2906 new = DTRACESPEC_DISCARDING; 2907 break; 2908 2909 case DTRACESPEC_ACTIVEONE: 2910 if (buf->dtb_offset != 0) { 2911 new = DTRACESPEC_INACTIVE; 2912 } else { 2913 new = DTRACESPEC_DISCARDING; 2914 } 2915 break; 2916 2917 default: 2918 ASSERT(0); 2919 } 2920 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2921 current, new) != current); 2922 2923 buf->dtb_offset = 0; 2924 buf->dtb_drops = 0; 2925} 2926 2927/* 2928 * Note: not called from probe context. This function is called 2929 * asynchronously from cross call context to clean any speculations that are 2930 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2931 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2932 * speculation. 2933 */ 2934static void 2935dtrace_speculation_clean_here(dtrace_state_t *state) 2936{ 2937 dtrace_icookie_t cookie; 2938 processorid_t cpu = curcpu; 2939 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2940 dtrace_specid_t i; 2941 2942 cookie = dtrace_interrupt_disable(); 2943 2944 if (dest->dtb_tomax == NULL) { 2945 dtrace_interrupt_enable(cookie); 2946 return; 2947 } 2948 2949 for (i = 0; i < state->dts_nspeculations; i++) { 2950 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2951 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2952 2953 if (src->dtb_tomax == NULL) 2954 continue; 2955 2956 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2957 src->dtb_offset = 0; 2958 continue; 2959 } 2960 2961 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2962 continue; 2963 2964 if (src->dtb_offset == 0) 2965 continue; 2966 2967 dtrace_speculation_commit(state, cpu, i + 1); 2968 } 2969 2970 dtrace_interrupt_enable(cookie); 2971} 2972 2973/* 2974 * Note: not called from probe context. This function is called 2975 * asynchronously (and at a regular interval) to clean any speculations that 2976 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2977 * is work to be done, it cross calls all CPUs to perform that work; 2978 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2979 * INACTIVE state until they have been cleaned by all CPUs. 2980 */ 2981static void 2982dtrace_speculation_clean(dtrace_state_t *state) 2983{ 2984 int work = 0, rv; 2985 dtrace_specid_t i; 2986 2987 for (i = 0; i < state->dts_nspeculations; i++) { 2988 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2989 2990 ASSERT(!spec->dtsp_cleaning); 2991 2992 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2993 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2994 continue; 2995 2996 work++; 2997 spec->dtsp_cleaning = 1; 2998 } 2999 3000 if (!work) 3001 return; 3002 3003 dtrace_xcall(DTRACE_CPUALL, 3004 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 3005 3006 /* 3007 * We now know that all CPUs have committed or discarded their 3008 * speculation buffers, as appropriate. We can now set the state 3009 * to inactive. 3010 */ 3011 for (i = 0; i < state->dts_nspeculations; i++) { 3012 dtrace_speculation_t *spec = &state->dts_speculations[i]; 3013 dtrace_speculation_state_t current, new; 3014 3015 if (!spec->dtsp_cleaning) 3016 continue; 3017 3018 current = spec->dtsp_state; 3019 ASSERT(current == DTRACESPEC_DISCARDING || 3020 current == DTRACESPEC_COMMITTINGMANY); 3021 3022 new = DTRACESPEC_INACTIVE; 3023 3024 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 3025 ASSERT(rv == current); 3026 spec->dtsp_cleaning = 0; 3027 } 3028} 3029 3030/* 3031 * Called as part of a speculate() to get the speculative buffer associated 3032 * with a given speculation. Returns NULL if the specified speculation is not 3033 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 3034 * the active CPU is not the specified CPU -- the speculation will be 3035 * atomically transitioned into the ACTIVEMANY state. 3036 */ 3037static dtrace_buffer_t * 3038dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 3039 dtrace_specid_t which) 3040{ 3041 dtrace_speculation_t *spec; 3042 dtrace_speculation_state_t current, new = 0; 3043 dtrace_buffer_t *buf; 3044 3045 if (which == 0) 3046 return (NULL); 3047 3048 if (which > state->dts_nspeculations) { 3049 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 3050 return (NULL); 3051 } 3052 3053 spec = &state->dts_speculations[which - 1]; 3054 buf = &spec->dtsp_buffer[cpuid]; 3055 3056 do { 3057 current = spec->dtsp_state; 3058 3059 switch (current) { 3060 case DTRACESPEC_INACTIVE: 3061 case DTRACESPEC_COMMITTINGMANY: 3062 case DTRACESPEC_DISCARDING: 3063 return (NULL); 3064 3065 case DTRACESPEC_COMMITTING: 3066 ASSERT(buf->dtb_offset == 0); 3067 return (NULL); 3068 3069 case DTRACESPEC_ACTIVEONE: 3070 /* 3071 * This speculation is currently active on one CPU. 3072 * Check the offset in the buffer; if it's non-zero, 3073 * that CPU must be us (and we leave the state alone). 3074 * If it's zero, assume that we're starting on a new 3075 * CPU -- and change the state to indicate that the 3076 * speculation is active on more than one CPU. 3077 */ 3078 if (buf->dtb_offset != 0) 3079 return (buf); 3080 3081 new = DTRACESPEC_ACTIVEMANY; 3082 break; 3083 3084 case DTRACESPEC_ACTIVEMANY: 3085 return (buf); 3086 3087 case DTRACESPEC_ACTIVE: 3088 new = DTRACESPEC_ACTIVEONE; 3089 break; 3090 3091 default: 3092 ASSERT(0); 3093 } 3094 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 3095 current, new) != current); 3096 3097 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 3098 return (buf); 3099} 3100 3101/* 3102 * Return a string. In the event that the user lacks the privilege to access 3103 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3104 * don't fail access checking. 3105 * 3106 * dtrace_dif_variable() uses this routine as a helper for various 3107 * builtin values such as 'execname' and 'probefunc.' 3108 */ 3109uintptr_t 3110dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 3111 dtrace_mstate_t *mstate) 3112{ 3113 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3114 uintptr_t ret; 3115 size_t strsz; 3116 3117 /* 3118 * The easy case: this probe is allowed to read all of memory, so 3119 * we can just return this as a vanilla pointer. 3120 */ 3121 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 3122 return (addr); 3123 3124 /* 3125 * This is the tougher case: we copy the string in question from 3126 * kernel memory into scratch memory and return it that way: this 3127 * ensures that we won't trip up when access checking tests the 3128 * BYREF return value. 3129 */ 3130 strsz = dtrace_strlen((char *)addr, size) + 1; 3131 3132 if (mstate->dtms_scratch_ptr + strsz > 3133 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3134 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3135 return (0); 3136 } 3137 3138 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3139 strsz); 3140 ret = mstate->dtms_scratch_ptr; 3141 mstate->dtms_scratch_ptr += strsz; 3142 return (ret); 3143} 3144 3145/* 3146 * Return a string from a memoy address which is known to have one or 3147 * more concatenated, individually zero terminated, sub-strings. 3148 * In the event that the user lacks the privilege to access 3149 * arbitrary kernel memory, we copy the string out to scratch memory so that we 3150 * don't fail access checking. 3151 * 3152 * dtrace_dif_variable() uses this routine as a helper for various 3153 * builtin values such as 'execargs'. 3154 */ 3155static uintptr_t 3156dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 3157 dtrace_mstate_t *mstate) 3158{ 3159 char *p; 3160 size_t i; 3161 uintptr_t ret; 3162 3163 if (mstate->dtms_scratch_ptr + strsz > 3164 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 3165 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3166 return (0); 3167 } 3168 3169 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 3170 strsz); 3171 3172 /* Replace sub-string termination characters with a space. */ 3173 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 3174 p++, i++) 3175 if (*p == '\0') 3176 *p = ' '; 3177 3178 ret = mstate->dtms_scratch_ptr; 3179 mstate->dtms_scratch_ptr += strsz; 3180 return (ret); 3181} 3182 3183/* 3184 * This function implements the DIF emulator's variable lookups. The emulator 3185 * passes a reserved variable identifier and optional built-in array index. 3186 */ 3187static uint64_t 3188dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 3189 uint64_t ndx) 3190{ 3191 /* 3192 * If we're accessing one of the uncached arguments, we'll turn this 3193 * into a reference in the args array. 3194 */ 3195 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 3196 ndx = v - DIF_VAR_ARG0; 3197 v = DIF_VAR_ARGS; 3198 } 3199 3200 switch (v) { 3201 case DIF_VAR_ARGS: 3202 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 3203 if (ndx >= sizeof (mstate->dtms_arg) / 3204 sizeof (mstate->dtms_arg[0])) { 3205 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3206 dtrace_provider_t *pv; 3207 uint64_t val; 3208 3209 pv = mstate->dtms_probe->dtpr_provider; 3210 if (pv->dtpv_pops.dtps_getargval != NULL) 3211 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 3212 mstate->dtms_probe->dtpr_id, 3213 mstate->dtms_probe->dtpr_arg, ndx, aframes); 3214 else 3215 val = dtrace_getarg(ndx, aframes); 3216 3217 /* 3218 * This is regrettably required to keep the compiler 3219 * from tail-optimizing the call to dtrace_getarg(). 3220 * The condition always evaluates to true, but the 3221 * compiler has no way of figuring that out a priori. 3222 * (None of this would be necessary if the compiler 3223 * could be relied upon to _always_ tail-optimize 3224 * the call to dtrace_getarg() -- but it can't.) 3225 */ 3226 if (mstate->dtms_probe != NULL) 3227 return (val); 3228 3229 ASSERT(0); 3230 } 3231 3232 return (mstate->dtms_arg[ndx]); 3233 3234#ifdef illumos 3235 case DIF_VAR_UREGS: { 3236 klwp_t *lwp; 3237 3238 if (!dtrace_priv_proc(state)) 3239 return (0); 3240 3241 if ((lwp = curthread->t_lwp) == NULL) { 3242 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3243 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 3244 return (0); 3245 } 3246 3247 return (dtrace_getreg(lwp->lwp_regs, ndx)); 3248 return (0); 3249 } 3250#else 3251 case DIF_VAR_UREGS: { 3252 struct trapframe *tframe; 3253 3254 if (!dtrace_priv_proc(state)) 3255 return (0); 3256 3257 if ((tframe = curthread->td_frame) == NULL) { 3258 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 3259 cpu_core[curcpu].cpuc_dtrace_illval = 0; 3260 return (0); 3261 } 3262 3263 return (dtrace_getreg(tframe, ndx)); 3264 } 3265#endif 3266 3267 case DIF_VAR_CURTHREAD: 3268 if (!dtrace_priv_proc(state)) 3269 return (0); 3270 return ((uint64_t)(uintptr_t)curthread); 3271 3272 case DIF_VAR_TIMESTAMP: 3273 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 3274 mstate->dtms_timestamp = dtrace_gethrtime(); 3275 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 3276 } 3277 return (mstate->dtms_timestamp); 3278 3279 case DIF_VAR_VTIMESTAMP: 3280 ASSERT(dtrace_vtime_references != 0); 3281 return (curthread->t_dtrace_vtime); 3282 3283 case DIF_VAR_WALLTIMESTAMP: 3284 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 3285 mstate->dtms_walltimestamp = dtrace_gethrestime(); 3286 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 3287 } 3288 return (mstate->dtms_walltimestamp); 3289 3290#ifdef illumos 3291 case DIF_VAR_IPL: 3292 if (!dtrace_priv_kernel(state)) 3293 return (0); 3294 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 3295 mstate->dtms_ipl = dtrace_getipl(); 3296 mstate->dtms_present |= DTRACE_MSTATE_IPL; 3297 } 3298 return (mstate->dtms_ipl); 3299#endif 3300 3301 case DIF_VAR_EPID: 3302 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 3303 return (mstate->dtms_epid); 3304 3305 case DIF_VAR_ID: 3306 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3307 return (mstate->dtms_probe->dtpr_id); 3308 3309 case DIF_VAR_STACKDEPTH: 3310 if (!dtrace_priv_kernel(state)) 3311 return (0); 3312 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 3313 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3314 3315 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 3316 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 3317 } 3318 return (mstate->dtms_stackdepth); 3319 3320 case DIF_VAR_USTACKDEPTH: 3321 if (!dtrace_priv_proc(state)) 3322 return (0); 3323 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 3324 /* 3325 * See comment in DIF_VAR_PID. 3326 */ 3327 if (DTRACE_ANCHORED(mstate->dtms_probe) && 3328 CPU_ON_INTR(CPU)) { 3329 mstate->dtms_ustackdepth = 0; 3330 } else { 3331 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3332 mstate->dtms_ustackdepth = 3333 dtrace_getustackdepth(); 3334 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3335 } 3336 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 3337 } 3338 return (mstate->dtms_ustackdepth); 3339 3340 case DIF_VAR_CALLER: 3341 if (!dtrace_priv_kernel(state)) 3342 return (0); 3343 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 3344 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 3345 3346 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 3347 /* 3348 * If this is an unanchored probe, we are 3349 * required to go through the slow path: 3350 * dtrace_caller() only guarantees correct 3351 * results for anchored probes. 3352 */ 3353 pc_t caller[2] = {0, 0}; 3354 3355 dtrace_getpcstack(caller, 2, aframes, 3356 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 3357 mstate->dtms_caller = caller[1]; 3358 } else if ((mstate->dtms_caller = 3359 dtrace_caller(aframes)) == -1) { 3360 /* 3361 * We have failed to do this the quick way; 3362 * we must resort to the slower approach of 3363 * calling dtrace_getpcstack(). 3364 */ 3365 pc_t caller = 0; 3366 3367 dtrace_getpcstack(&caller, 1, aframes, NULL); 3368 mstate->dtms_caller = caller; 3369 } 3370 3371 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 3372 } 3373 return (mstate->dtms_caller); 3374 3375 case DIF_VAR_UCALLER: 3376 if (!dtrace_priv_proc(state)) 3377 return (0); 3378 3379 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 3380 uint64_t ustack[3]; 3381 3382 /* 3383 * dtrace_getupcstack() fills in the first uint64_t 3384 * with the current PID. The second uint64_t will 3385 * be the program counter at user-level. The third 3386 * uint64_t will contain the caller, which is what 3387 * we're after. 3388 */ 3389 ustack[2] = 0; 3390 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3391 dtrace_getupcstack(ustack, 3); 3392 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3393 mstate->dtms_ucaller = ustack[2]; 3394 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 3395 } 3396 3397 return (mstate->dtms_ucaller); 3398 3399 case DIF_VAR_PROBEPROV: 3400 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3401 return (dtrace_dif_varstr( 3402 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 3403 state, mstate)); 3404 3405 case DIF_VAR_PROBEMOD: 3406 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3407 return (dtrace_dif_varstr( 3408 (uintptr_t)mstate->dtms_probe->dtpr_mod, 3409 state, mstate)); 3410 3411 case DIF_VAR_PROBEFUNC: 3412 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3413 return (dtrace_dif_varstr( 3414 (uintptr_t)mstate->dtms_probe->dtpr_func, 3415 state, mstate)); 3416 3417 case DIF_VAR_PROBENAME: 3418 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 3419 return (dtrace_dif_varstr( 3420 (uintptr_t)mstate->dtms_probe->dtpr_name, 3421 state, mstate)); 3422 3423 case DIF_VAR_PID: 3424 if (!dtrace_priv_proc(state)) 3425 return (0); 3426 3427#ifdef illumos 3428 /* 3429 * Note that we are assuming that an unanchored probe is 3430 * always due to a high-level interrupt. (And we're assuming 3431 * that there is only a single high level interrupt.) 3432 */ 3433 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3434 return (pid0.pid_id); 3435 3436 /* 3437 * It is always safe to dereference one's own t_procp pointer: 3438 * it always points to a valid, allocated proc structure. 3439 * Further, it is always safe to dereference the p_pidp member 3440 * of one's own proc structure. (These are truisms becuase 3441 * threads and processes don't clean up their own state -- 3442 * they leave that task to whomever reaps them.) 3443 */ 3444 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 3445#else 3446 return ((uint64_t)curproc->p_pid); 3447#endif 3448 3449 case DIF_VAR_PPID: 3450 if (!dtrace_priv_proc(state)) 3451 return (0); 3452 3453#ifdef illumos 3454 /* 3455 * See comment in DIF_VAR_PID. 3456 */ 3457 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3458 return (pid0.pid_id); 3459 3460 /* 3461 * It is always safe to dereference one's own t_procp pointer: 3462 * it always points to a valid, allocated proc structure. 3463 * (This is true because threads don't clean up their own 3464 * state -- they leave that task to whomever reaps them.) 3465 */ 3466 return ((uint64_t)curthread->t_procp->p_ppid); 3467#else 3468 if (curproc->p_pid == proc0.p_pid) 3469 return (curproc->p_pid); 3470 else 3471 return (curproc->p_pptr->p_pid); 3472#endif 3473 3474 case DIF_VAR_TID: 3475#ifdef illumos 3476 /* 3477 * See comment in DIF_VAR_PID. 3478 */ 3479 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3480 return (0); 3481#endif 3482 3483 return ((uint64_t)curthread->t_tid); 3484 3485 case DIF_VAR_EXECARGS: { 3486 struct pargs *p_args = curthread->td_proc->p_args; 3487 3488 if (p_args == NULL) 3489 return(0); 3490 3491 return (dtrace_dif_varstrz( 3492 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3493 } 3494 3495 case DIF_VAR_EXECNAME: 3496#ifdef illumos 3497 if (!dtrace_priv_proc(state)) 3498 return (0); 3499 3500 /* 3501 * See comment in DIF_VAR_PID. 3502 */ 3503 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3504 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3505 3506 /* 3507 * It is always safe to dereference one's own t_procp pointer: 3508 * it always points to a valid, allocated proc structure. 3509 * (This is true because threads don't clean up their own 3510 * state -- they leave that task to whomever reaps them.) 3511 */ 3512 return (dtrace_dif_varstr( 3513 (uintptr_t)curthread->t_procp->p_user.u_comm, 3514 state, mstate)); 3515#else 3516 return (dtrace_dif_varstr( 3517 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3518#endif 3519 3520 case DIF_VAR_ZONENAME: 3521#ifdef illumos 3522 if (!dtrace_priv_proc(state)) 3523 return (0); 3524 3525 /* 3526 * See comment in DIF_VAR_PID. 3527 */ 3528 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3529 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3530 3531 /* 3532 * It is always safe to dereference one's own t_procp pointer: 3533 * it always points to a valid, allocated proc structure. 3534 * (This is true because threads don't clean up their own 3535 * state -- they leave that task to whomever reaps them.) 3536 */ 3537 return (dtrace_dif_varstr( 3538 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3539 state, mstate)); 3540#elif defined(__FreeBSD__) 3541 /* 3542 * On FreeBSD, we introduce compatibility to zonename by falling through 3543 * into jailname. 3544 */ 3545 case DIF_VAR_JAILNAME: 3546 if (!dtrace_priv_kernel(state)) 3547 return (0); 3548 3549 return (dtrace_dif_varstr( 3550 (uintptr_t)curthread->td_ucred->cr_prison->pr_name, 3551 state, mstate)); 3552 3553 case DIF_VAR_JID: 3554 if (!dtrace_priv_kernel(state)) 3555 return (0); 3556 3557 return ((uint64_t)curthread->td_ucred->cr_prison->pr_id); 3558#else 3559 return (0); 3560#endif 3561 3562 case DIF_VAR_UID: 3563 if (!dtrace_priv_proc(state)) 3564 return (0); 3565 3566#ifdef illumos 3567 /* 3568 * See comment in DIF_VAR_PID. 3569 */ 3570 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3571 return ((uint64_t)p0.p_cred->cr_uid); 3572 3573 /* 3574 * It is always safe to dereference one's own t_procp pointer: 3575 * it always points to a valid, allocated proc structure. 3576 * (This is true because threads don't clean up their own 3577 * state -- they leave that task to whomever reaps them.) 3578 * 3579 * Additionally, it is safe to dereference one's own process 3580 * credential, since this is never NULL after process birth. 3581 */ 3582 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3583#else 3584 return ((uint64_t)curthread->td_ucred->cr_uid); 3585#endif 3586 3587 case DIF_VAR_GID: 3588 if (!dtrace_priv_proc(state)) 3589 return (0); 3590 3591#ifdef illumos 3592 /* 3593 * See comment in DIF_VAR_PID. 3594 */ 3595 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3596 return ((uint64_t)p0.p_cred->cr_gid); 3597 3598 /* 3599 * It is always safe to dereference one's own t_procp pointer: 3600 * it always points to a valid, allocated proc structure. 3601 * (This is true because threads don't clean up their own 3602 * state -- they leave that task to whomever reaps them.) 3603 * 3604 * Additionally, it is safe to dereference one's own process 3605 * credential, since this is never NULL after process birth. 3606 */ 3607 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3608#else 3609 return ((uint64_t)curthread->td_ucred->cr_gid); 3610#endif 3611 3612 case DIF_VAR_ERRNO: { 3613#ifdef illumos 3614 klwp_t *lwp; 3615 if (!dtrace_priv_proc(state)) 3616 return (0); 3617 3618 /* 3619 * See comment in DIF_VAR_PID. 3620 */ 3621 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3622 return (0); 3623 3624 /* 3625 * It is always safe to dereference one's own t_lwp pointer in 3626 * the event that this pointer is non-NULL. (This is true 3627 * because threads and lwps don't clean up their own state -- 3628 * they leave that task to whomever reaps them.) 3629 */ 3630 if ((lwp = curthread->t_lwp) == NULL) 3631 return (0); 3632 3633 return ((uint64_t)lwp->lwp_errno); 3634#else 3635 return (curthread->td_errno); 3636#endif 3637 } 3638#ifndef illumos 3639 case DIF_VAR_CPU: { 3640 return curcpu; 3641 } 3642#endif 3643 default: 3644 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3645 return (0); 3646 } 3647} 3648 3649 3650typedef enum dtrace_json_state { 3651 DTRACE_JSON_REST = 1, 3652 DTRACE_JSON_OBJECT, 3653 DTRACE_JSON_STRING, 3654 DTRACE_JSON_STRING_ESCAPE, 3655 DTRACE_JSON_STRING_ESCAPE_UNICODE, 3656 DTRACE_JSON_COLON, 3657 DTRACE_JSON_COMMA, 3658 DTRACE_JSON_VALUE, 3659 DTRACE_JSON_IDENTIFIER, 3660 DTRACE_JSON_NUMBER, 3661 DTRACE_JSON_NUMBER_FRAC, 3662 DTRACE_JSON_NUMBER_EXP, 3663 DTRACE_JSON_COLLECT_OBJECT 3664} dtrace_json_state_t; 3665 3666/* 3667 * This function possesses just enough knowledge about JSON to extract a single 3668 * value from a JSON string and store it in the scratch buffer. It is able 3669 * to extract nested object values, and members of arrays by index. 3670 * 3671 * elemlist is a list of JSON keys, stored as packed NUL-terminated strings, to 3672 * be looked up as we descend into the object tree. e.g. 3673 * 3674 * foo[0].bar.baz[32] --> "foo" NUL "0" NUL "bar" NUL "baz" NUL "32" NUL 3675 * with nelems = 5. 3676 * 3677 * The run time of this function must be bounded above by strsize to limit the 3678 * amount of work done in probe context. As such, it is implemented as a 3679 * simple state machine, reading one character at a time using safe loads 3680 * until we find the requested element, hit a parsing error or run off the 3681 * end of the object or string. 3682 * 3683 * As there is no way for a subroutine to return an error without interrupting 3684 * clause execution, we simply return NULL in the event of a missing key or any 3685 * other error condition. Each NULL return in this function is commented with 3686 * the error condition it represents -- parsing or otherwise. 3687 * 3688 * The set of states for the state machine closely matches the JSON 3689 * specification (http://json.org/). Briefly: 3690 * 3691 * DTRACE_JSON_REST: 3692 * Skip whitespace until we find either a top-level Object, moving 3693 * to DTRACE_JSON_OBJECT; or an Array, moving to DTRACE_JSON_VALUE. 3694 * 3695 * DTRACE_JSON_OBJECT: 3696 * Locate the next key String in an Object. Sets a flag to denote 3697 * the next String as a key string and moves to DTRACE_JSON_STRING. 3698 * 3699 * DTRACE_JSON_COLON: 3700 * Skip whitespace until we find the colon that separates key Strings 3701 * from their values. Once found, move to DTRACE_JSON_VALUE. 3702 * 3703 * DTRACE_JSON_VALUE: 3704 * Detects the type of the next value (String, Number, Identifier, Object 3705 * or Array) and routes to the states that process that type. Here we also 3706 * deal with the element selector list if we are requested to traverse down 3707 * into the object tree. 3708 * 3709 * DTRACE_JSON_COMMA: 3710 * Skip whitespace until we find the comma that separates key-value pairs 3711 * in Objects (returning to DTRACE_JSON_OBJECT) or values in Arrays 3712 * (similarly DTRACE_JSON_VALUE). All following literal value processing 3713 * states return to this state at the end of their value, unless otherwise 3714 * noted. 3715 * 3716 * DTRACE_JSON_NUMBER, DTRACE_JSON_NUMBER_FRAC, DTRACE_JSON_NUMBER_EXP: 3717 * Processes a Number literal from the JSON, including any exponent 3718 * component that may be present. Numbers are returned as strings, which 3719 * may be passed to strtoll() if an integer is required. 3720 * 3721 * DTRACE_JSON_IDENTIFIER: 3722 * Processes a "true", "false" or "null" literal in the JSON. 3723 * 3724 * DTRACE_JSON_STRING, DTRACE_JSON_STRING_ESCAPE, 3725 * DTRACE_JSON_STRING_ESCAPE_UNICODE: 3726 * Processes a String literal from the JSON, whether the String denotes 3727 * a key, a value or part of a larger Object. Handles all escape sequences 3728 * present in the specification, including four-digit unicode characters, 3729 * but merely includes the escape sequence without converting it to the 3730 * actual escaped character. If the String is flagged as a key, we 3731 * move to DTRACE_JSON_COLON rather than DTRACE_JSON_COMMA. 3732 * 3733 * DTRACE_JSON_COLLECT_OBJECT: 3734 * This state collects an entire Object (or Array), correctly handling 3735 * embedded strings. If the full element selector list matches this nested 3736 * object, we return the Object in full as a string. If not, we use this 3737 * state to skip to the next value at this level and continue processing. 3738 * 3739 * NOTE: This function uses various macros from strtolctype.h to manipulate 3740 * digit values, etc -- these have all been checked to ensure they make 3741 * no additional function calls. 3742 */ 3743static char * 3744dtrace_json(uint64_t size, uintptr_t json, char *elemlist, int nelems, 3745 char *dest) 3746{ 3747 dtrace_json_state_t state = DTRACE_JSON_REST; 3748 int64_t array_elem = INT64_MIN; 3749 int64_t array_pos = 0; 3750 uint8_t escape_unicount = 0; 3751 boolean_t string_is_key = B_FALSE; 3752 boolean_t collect_object = B_FALSE; 3753 boolean_t found_key = B_FALSE; 3754 boolean_t in_array = B_FALSE; 3755 uint32_t braces = 0, brackets = 0; 3756 char *elem = elemlist; 3757 char *dd = dest; 3758 uintptr_t cur; 3759 3760 for (cur = json; cur < json + size; cur++) { 3761 char cc = dtrace_load8(cur); 3762 if (cc == '\0') 3763 return (NULL); 3764 3765 switch (state) { 3766 case DTRACE_JSON_REST: 3767 if (isspace(cc)) 3768 break; 3769 3770 if (cc == '{') { 3771 state = DTRACE_JSON_OBJECT; 3772 break; 3773 } 3774 3775 if (cc == '[') { 3776 in_array = B_TRUE; 3777 array_pos = 0; 3778 array_elem = dtrace_strtoll(elem, 10, size); 3779 found_key = array_elem == 0 ? B_TRUE : B_FALSE; 3780 state = DTRACE_JSON_VALUE; 3781 break; 3782 } 3783 3784 /* 3785 * ERROR: expected to find a top-level object or array. 3786 */ 3787 return (NULL); 3788 case DTRACE_JSON_OBJECT: 3789 if (isspace(cc)) 3790 break; 3791 3792 if (cc == '"') { 3793 state = DTRACE_JSON_STRING; 3794 string_is_key = B_TRUE; 3795 break; 3796 } 3797 3798 /* 3799 * ERROR: either the object did not start with a key 3800 * string, or we've run off the end of the object 3801 * without finding the requested key. 3802 */ 3803 return (NULL); 3804 case DTRACE_JSON_STRING: 3805 if (cc == '\\') { 3806 *dd++ = '\\'; 3807 state = DTRACE_JSON_STRING_ESCAPE; 3808 break; 3809 } 3810 3811 if (cc == '"') { 3812 if (collect_object) { 3813 /* 3814 * We don't reset the dest here, as 3815 * the string is part of a larger 3816 * object being collected. 3817 */ 3818 *dd++ = cc; 3819 collect_object = B_FALSE; 3820 state = DTRACE_JSON_COLLECT_OBJECT; 3821 break; 3822 } 3823 *dd = '\0'; 3824 dd = dest; /* reset string buffer */ 3825 if (string_is_key) { 3826 if (dtrace_strncmp(dest, elem, 3827 size) == 0) 3828 found_key = B_TRUE; 3829 } else if (found_key) { 3830 if (nelems > 1) { 3831 /* 3832 * We expected an object, not 3833 * this string. 3834 */ 3835 return (NULL); 3836 } 3837 return (dest); 3838 } 3839 state = string_is_key ? DTRACE_JSON_COLON : 3840 DTRACE_JSON_COMMA; 3841 string_is_key = B_FALSE; 3842 break; 3843 } 3844 3845 *dd++ = cc; 3846 break; 3847 case DTRACE_JSON_STRING_ESCAPE: 3848 *dd++ = cc; 3849 if (cc == 'u') { 3850 escape_unicount = 0; 3851 state = DTRACE_JSON_STRING_ESCAPE_UNICODE; 3852 } else { 3853 state = DTRACE_JSON_STRING; 3854 } 3855 break; 3856 case DTRACE_JSON_STRING_ESCAPE_UNICODE: 3857 if (!isxdigit(cc)) { 3858 /* 3859 * ERROR: invalid unicode escape, expected 3860 * four valid hexidecimal digits. 3861 */ 3862 return (NULL); 3863 } 3864 3865 *dd++ = cc; 3866 if (++escape_unicount == 4) 3867 state = DTRACE_JSON_STRING; 3868 break; 3869 case DTRACE_JSON_COLON: 3870 if (isspace(cc)) 3871 break; 3872 3873 if (cc == ':') { 3874 state = DTRACE_JSON_VALUE; 3875 break; 3876 } 3877 3878 /* 3879 * ERROR: expected a colon. 3880 */ 3881 return (NULL); 3882 case DTRACE_JSON_COMMA: 3883 if (isspace(cc)) 3884 break; 3885 3886 if (cc == ',') { 3887 if (in_array) { 3888 state = DTRACE_JSON_VALUE; 3889 if (++array_pos == array_elem) 3890 found_key = B_TRUE; 3891 } else { 3892 state = DTRACE_JSON_OBJECT; 3893 } 3894 break; 3895 } 3896 3897 /* 3898 * ERROR: either we hit an unexpected character, or 3899 * we reached the end of the object or array without 3900 * finding the requested key. 3901 */ 3902 return (NULL); 3903 case DTRACE_JSON_IDENTIFIER: 3904 if (islower(cc)) { 3905 *dd++ = cc; 3906 break; 3907 } 3908 3909 *dd = '\0'; 3910 dd = dest; /* reset string buffer */ 3911 3912 if (dtrace_strncmp(dest, "true", 5) == 0 || 3913 dtrace_strncmp(dest, "false", 6) == 0 || 3914 dtrace_strncmp(dest, "null", 5) == 0) { 3915 if (found_key) { 3916 if (nelems > 1) { 3917 /* 3918 * ERROR: We expected an object, 3919 * not this identifier. 3920 */ 3921 return (NULL); 3922 } 3923 return (dest); 3924 } else { 3925 cur--; 3926 state = DTRACE_JSON_COMMA; 3927 break; 3928 } 3929 } 3930 3931 /* 3932 * ERROR: we did not recognise the identifier as one 3933 * of those in the JSON specification. 3934 */ 3935 return (NULL); 3936 case DTRACE_JSON_NUMBER: 3937 if (cc == '.') { 3938 *dd++ = cc; 3939 state = DTRACE_JSON_NUMBER_FRAC; 3940 break; 3941 } 3942 3943 if (cc == 'x' || cc == 'X') { 3944 /* 3945 * ERROR: specification explicitly excludes 3946 * hexidecimal or octal numbers. 3947 */ 3948 return (NULL); 3949 } 3950 3951 /* FALLTHRU */ 3952 case DTRACE_JSON_NUMBER_FRAC: 3953 if (cc == 'e' || cc == 'E') { 3954 *dd++ = cc; 3955 state = DTRACE_JSON_NUMBER_EXP; 3956 break; 3957 } 3958 3959 if (cc == '+' || cc == '-') { 3960 /* 3961 * ERROR: expect sign as part of exponent only. 3962 */ 3963 return (NULL); 3964 } 3965 /* FALLTHRU */ 3966 case DTRACE_JSON_NUMBER_EXP: 3967 if (isdigit(cc) || cc == '+' || cc == '-') { 3968 *dd++ = cc; 3969 break; 3970 } 3971 3972 *dd = '\0'; 3973 dd = dest; /* reset string buffer */ 3974 if (found_key) { 3975 if (nelems > 1) { 3976 /* 3977 * ERROR: We expected an object, not 3978 * this number. 3979 */ 3980 return (NULL); 3981 } 3982 return (dest); 3983 } 3984 3985 cur--; 3986 state = DTRACE_JSON_COMMA; 3987 break; 3988 case DTRACE_JSON_VALUE: 3989 if (isspace(cc)) 3990 break; 3991 3992 if (cc == '{' || cc == '[') { 3993 if (nelems > 1 && found_key) { 3994 in_array = cc == '[' ? B_TRUE : B_FALSE; 3995 /* 3996 * If our element selector directs us 3997 * to descend into this nested object, 3998 * then move to the next selector 3999 * element in the list and restart the 4000 * state machine. 4001 */ 4002 while (*elem != '\0') 4003 elem++; 4004 elem++; /* skip the inter-element NUL */ 4005 nelems--; 4006 dd = dest; 4007 if (in_array) { 4008 state = DTRACE_JSON_VALUE; 4009 array_pos = 0; 4010 array_elem = dtrace_strtoll( 4011 elem, 10, size); 4012 found_key = array_elem == 0 ? 4013 B_TRUE : B_FALSE; 4014 } else { 4015 found_key = B_FALSE; 4016 state = DTRACE_JSON_OBJECT; 4017 } 4018 break; 4019 } 4020 4021 /* 4022 * Otherwise, we wish to either skip this 4023 * nested object or return it in full. 4024 */ 4025 if (cc == '[') 4026 brackets = 1; 4027 else 4028 braces = 1; 4029 *dd++ = cc; 4030 state = DTRACE_JSON_COLLECT_OBJECT; 4031 break; 4032 } 4033 4034 if (cc == '"') { 4035 state = DTRACE_JSON_STRING; 4036 break; 4037 } 4038 4039 if (islower(cc)) { 4040 /* 4041 * Here we deal with true, false and null. 4042 */ 4043 *dd++ = cc; 4044 state = DTRACE_JSON_IDENTIFIER; 4045 break; 4046 } 4047 4048 if (cc == '-' || isdigit(cc)) { 4049 *dd++ = cc; 4050 state = DTRACE_JSON_NUMBER; 4051 break; 4052 } 4053 4054 /* 4055 * ERROR: unexpected character at start of value. 4056 */ 4057 return (NULL); 4058 case DTRACE_JSON_COLLECT_OBJECT: 4059 if (cc == '\0') 4060 /* 4061 * ERROR: unexpected end of input. 4062 */ 4063 return (NULL); 4064 4065 *dd++ = cc; 4066 if (cc == '"') { 4067 collect_object = B_TRUE; 4068 state = DTRACE_JSON_STRING; 4069 break; 4070 } 4071 4072 if (cc == ']') { 4073 if (brackets-- == 0) { 4074 /* 4075 * ERROR: unbalanced brackets. 4076 */ 4077 return (NULL); 4078 } 4079 } else if (cc == '}') { 4080 if (braces-- == 0) { 4081 /* 4082 * ERROR: unbalanced braces. 4083 */ 4084 return (NULL); 4085 } 4086 } else if (cc == '{') { 4087 braces++; 4088 } else if (cc == '[') { 4089 brackets++; 4090 } 4091 4092 if (brackets == 0 && braces == 0) { 4093 if (found_key) { 4094 *dd = '\0'; 4095 return (dest); 4096 } 4097 dd = dest; /* reset string buffer */ 4098 state = DTRACE_JSON_COMMA; 4099 } 4100 break; 4101 } 4102 } 4103 return (NULL); 4104} 4105 4106/* 4107 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 4108 * Notice that we don't bother validating the proper number of arguments or 4109 * their types in the tuple stack. This isn't needed because all argument 4110 * interpretation is safe because of our load safety -- the worst that can 4111 * happen is that a bogus program can obtain bogus results. 4112 */ 4113static void 4114dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 4115 dtrace_key_t *tupregs, int nargs, 4116 dtrace_mstate_t *mstate, dtrace_state_t *state) 4117{ 4118 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4119 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4120 dtrace_vstate_t *vstate = &state->dts_vstate; 4121 4122#ifdef illumos 4123 union { 4124 mutex_impl_t mi; 4125 uint64_t mx; 4126 } m; 4127 4128 union { 4129 krwlock_t ri; 4130 uintptr_t rw; 4131 } r; 4132#else 4133 struct thread *lowner; 4134 union { 4135 struct lock_object *li; 4136 uintptr_t lx; 4137 } l; 4138#endif 4139 4140 switch (subr) { 4141 case DIF_SUBR_RAND: 4142 regs[rd] = dtrace_xoroshiro128_plus_next( 4143 state->dts_rstate[curcpu]); 4144 break; 4145 4146#ifdef illumos 4147 case DIF_SUBR_MUTEX_OWNED: 4148 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4149 mstate, vstate)) { 4150 regs[rd] = 0; 4151 break; 4152 } 4153 4154 m.mx = dtrace_load64(tupregs[0].dttk_value); 4155 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 4156 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 4157 else 4158 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 4159 break; 4160 4161 case DIF_SUBR_MUTEX_OWNER: 4162 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4163 mstate, vstate)) { 4164 regs[rd] = 0; 4165 break; 4166 } 4167 4168 m.mx = dtrace_load64(tupregs[0].dttk_value); 4169 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 4170 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 4171 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 4172 else 4173 regs[rd] = 0; 4174 break; 4175 4176 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4177 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4178 mstate, vstate)) { 4179 regs[rd] = 0; 4180 break; 4181 } 4182 4183 m.mx = dtrace_load64(tupregs[0].dttk_value); 4184 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 4185 break; 4186 4187 case DIF_SUBR_MUTEX_TYPE_SPIN: 4188 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 4189 mstate, vstate)) { 4190 regs[rd] = 0; 4191 break; 4192 } 4193 4194 m.mx = dtrace_load64(tupregs[0].dttk_value); 4195 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 4196 break; 4197 4198 case DIF_SUBR_RW_READ_HELD: { 4199 uintptr_t tmp; 4200 4201 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4202 mstate, vstate)) { 4203 regs[rd] = 0; 4204 break; 4205 } 4206 4207 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4208 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 4209 break; 4210 } 4211 4212 case DIF_SUBR_RW_WRITE_HELD: 4213 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4214 mstate, vstate)) { 4215 regs[rd] = 0; 4216 break; 4217 } 4218 4219 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4220 regs[rd] = _RW_WRITE_HELD(&r.ri); 4221 break; 4222 4223 case DIF_SUBR_RW_ISWRITER: 4224 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 4225 mstate, vstate)) { 4226 regs[rd] = 0; 4227 break; 4228 } 4229 4230 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 4231 regs[rd] = _RW_ISWRITER(&r.ri); 4232 break; 4233 4234#else /* !illumos */ 4235 case DIF_SUBR_MUTEX_OWNED: 4236 if (!dtrace_canload(tupregs[0].dttk_value, 4237 sizeof (struct lock_object), mstate, vstate)) { 4238 regs[rd] = 0; 4239 break; 4240 } 4241 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4242 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4243 break; 4244 4245 case DIF_SUBR_MUTEX_OWNER: 4246 if (!dtrace_canload(tupregs[0].dttk_value, 4247 sizeof (struct lock_object), mstate, vstate)) { 4248 regs[rd] = 0; 4249 break; 4250 } 4251 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4252 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4253 regs[rd] = (uintptr_t)lowner; 4254 break; 4255 4256 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 4257 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4258 mstate, vstate)) { 4259 regs[rd] = 0; 4260 break; 4261 } 4262 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4263 /* XXX - should be only LC_SLEEPABLE? */ 4264 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & 4265 (LC_SLEEPLOCK | LC_SLEEPABLE)) != 0; 4266 break; 4267 4268 case DIF_SUBR_MUTEX_TYPE_SPIN: 4269 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (struct mtx), 4270 mstate, vstate)) { 4271 regs[rd] = 0; 4272 break; 4273 } 4274 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4275 regs[rd] = (LOCK_CLASS(l.li)->lc_flags & LC_SPINLOCK) != 0; 4276 break; 4277 4278 case DIF_SUBR_RW_READ_HELD: 4279 case DIF_SUBR_SX_SHARED_HELD: 4280 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4281 mstate, vstate)) { 4282 regs[rd] = 0; 4283 break; 4284 } 4285 l.lx = dtrace_loadptr((uintptr_t)&tupregs[0].dttk_value); 4286 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4287 lowner == NULL; 4288 break; 4289 4290 case DIF_SUBR_RW_WRITE_HELD: 4291 case DIF_SUBR_SX_EXCLUSIVE_HELD: 4292 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4293 mstate, vstate)) { 4294 regs[rd] = 0; 4295 break; 4296 } 4297 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4298 regs[rd] = LOCK_CLASS(l.li)->lc_owner(l.li, &lowner) && 4299 lowner != NULL; 4300 break; 4301 4302 case DIF_SUBR_RW_ISWRITER: 4303 case DIF_SUBR_SX_ISEXCLUSIVE: 4304 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 4305 mstate, vstate)) { 4306 regs[rd] = 0; 4307 break; 4308 } 4309 l.lx = dtrace_loadptr(tupregs[0].dttk_value); 4310 LOCK_CLASS(l.li)->lc_owner(l.li, &lowner); 4311 regs[rd] = (lowner == curthread); 4312 break; 4313#endif /* illumos */ 4314 4315 case DIF_SUBR_BCOPY: { 4316 /* 4317 * We need to be sure that the destination is in the scratch 4318 * region -- no other region is allowed. 4319 */ 4320 uintptr_t src = tupregs[0].dttk_value; 4321 uintptr_t dest = tupregs[1].dttk_value; 4322 size_t size = tupregs[2].dttk_value; 4323 4324 if (!dtrace_inscratch(dest, size, mstate)) { 4325 *flags |= CPU_DTRACE_BADADDR; 4326 *illval = regs[rd]; 4327 break; 4328 } 4329 4330 if (!dtrace_canload(src, size, mstate, vstate)) { 4331 regs[rd] = 0; 4332 break; 4333 } 4334 4335 dtrace_bcopy((void *)src, (void *)dest, size); 4336 break; 4337 } 4338 4339 case DIF_SUBR_ALLOCA: 4340 case DIF_SUBR_COPYIN: { 4341 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 4342 uint64_t size = 4343 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 4344 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 4345 4346 /* 4347 * This action doesn't require any credential checks since 4348 * probes will not activate in user contexts to which the 4349 * enabling user does not have permissions. 4350 */ 4351 4352 /* 4353 * Rounding up the user allocation size could have overflowed 4354 * a large, bogus allocation (like -1ULL) to 0. 4355 */ 4356 if (scratch_size < size || 4357 !DTRACE_INSCRATCH(mstate, scratch_size)) { 4358 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4359 regs[rd] = 0; 4360 break; 4361 } 4362 4363 if (subr == DIF_SUBR_COPYIN) { 4364 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4365 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4366 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4367 } 4368 4369 mstate->dtms_scratch_ptr += scratch_size; 4370 regs[rd] = dest; 4371 break; 4372 } 4373 4374 case DIF_SUBR_COPYINTO: { 4375 uint64_t size = tupregs[1].dttk_value; 4376 uintptr_t dest = tupregs[2].dttk_value; 4377 4378 /* 4379 * This action doesn't require any credential checks since 4380 * probes will not activate in user contexts to which the 4381 * enabling user does not have permissions. 4382 */ 4383 if (!dtrace_inscratch(dest, size, mstate)) { 4384 *flags |= CPU_DTRACE_BADADDR; 4385 *illval = regs[rd]; 4386 break; 4387 } 4388 4389 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4390 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 4391 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4392 break; 4393 } 4394 4395 case DIF_SUBR_COPYINSTR: { 4396 uintptr_t dest = mstate->dtms_scratch_ptr; 4397 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4398 4399 if (nargs > 1 && tupregs[1].dttk_value < size) 4400 size = tupregs[1].dttk_value + 1; 4401 4402 /* 4403 * This action doesn't require any credential checks since 4404 * probes will not activate in user contexts to which the 4405 * enabling user does not have permissions. 4406 */ 4407 if (!DTRACE_INSCRATCH(mstate, size)) { 4408 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4409 regs[rd] = 0; 4410 break; 4411 } 4412 4413 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4414 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 4415 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4416 4417 ((char *)dest)[size - 1] = '\0'; 4418 mstate->dtms_scratch_ptr += size; 4419 regs[rd] = dest; 4420 break; 4421 } 4422 4423#ifdef illumos 4424 case DIF_SUBR_MSGSIZE: 4425 case DIF_SUBR_MSGDSIZE: { 4426 uintptr_t baddr = tupregs[0].dttk_value, daddr; 4427 uintptr_t wptr, rptr; 4428 size_t count = 0; 4429 int cont = 0; 4430 4431 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 4432 4433 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 4434 vstate)) { 4435 regs[rd] = 0; 4436 break; 4437 } 4438 4439 wptr = dtrace_loadptr(baddr + 4440 offsetof(mblk_t, b_wptr)); 4441 4442 rptr = dtrace_loadptr(baddr + 4443 offsetof(mblk_t, b_rptr)); 4444 4445 if (wptr < rptr) { 4446 *flags |= CPU_DTRACE_BADADDR; 4447 *illval = tupregs[0].dttk_value; 4448 break; 4449 } 4450 4451 daddr = dtrace_loadptr(baddr + 4452 offsetof(mblk_t, b_datap)); 4453 4454 baddr = dtrace_loadptr(baddr + 4455 offsetof(mblk_t, b_cont)); 4456 4457 /* 4458 * We want to prevent against denial-of-service here, 4459 * so we're only going to search the list for 4460 * dtrace_msgdsize_max mblks. 4461 */ 4462 if (cont++ > dtrace_msgdsize_max) { 4463 *flags |= CPU_DTRACE_ILLOP; 4464 break; 4465 } 4466 4467 if (subr == DIF_SUBR_MSGDSIZE) { 4468 if (dtrace_load8(daddr + 4469 offsetof(dblk_t, db_type)) != M_DATA) 4470 continue; 4471 } 4472 4473 count += wptr - rptr; 4474 } 4475 4476 if (!(*flags & CPU_DTRACE_FAULT)) 4477 regs[rd] = count; 4478 4479 break; 4480 } 4481#endif 4482 4483 case DIF_SUBR_PROGENYOF: { 4484 pid_t pid = tupregs[0].dttk_value; 4485 proc_t *p; 4486 int rval = 0; 4487 4488 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4489 4490 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 4491#ifdef illumos 4492 if (p->p_pidp->pid_id == pid) { 4493#else 4494 if (p->p_pid == pid) { 4495#endif 4496 rval = 1; 4497 break; 4498 } 4499 } 4500 4501 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4502 4503 regs[rd] = rval; 4504 break; 4505 } 4506 4507 case DIF_SUBR_SPECULATION: 4508 regs[rd] = dtrace_speculation(state); 4509 break; 4510 4511 case DIF_SUBR_COPYOUT: { 4512 uintptr_t kaddr = tupregs[0].dttk_value; 4513 uintptr_t uaddr = tupregs[1].dttk_value; 4514 uint64_t size = tupregs[2].dttk_value; 4515 4516 if (!dtrace_destructive_disallow && 4517 dtrace_priv_proc_control(state) && 4518 !dtrace_istoxic(kaddr, size) && 4519 dtrace_canload(kaddr, size, mstate, vstate)) { 4520 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4521 dtrace_copyout(kaddr, uaddr, size, flags); 4522 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4523 } 4524 break; 4525 } 4526 4527 case DIF_SUBR_COPYOUTSTR: { 4528 uintptr_t kaddr = tupregs[0].dttk_value; 4529 uintptr_t uaddr = tupregs[1].dttk_value; 4530 uint64_t size = tupregs[2].dttk_value; 4531 4532 if (!dtrace_destructive_disallow && 4533 dtrace_priv_proc_control(state) && 4534 !dtrace_istoxic(kaddr, size) && 4535 dtrace_strcanload(kaddr, size, mstate, vstate)) { 4536 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 4537 dtrace_copyoutstr(kaddr, uaddr, size, flags); 4538 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 4539 } 4540 break; 4541 } 4542 4543 case DIF_SUBR_STRLEN: { 4544 size_t sz; 4545 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 4546 sz = dtrace_strlen((char *)addr, 4547 state->dts_options[DTRACEOPT_STRSIZE]); 4548 4549 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 4550 regs[rd] = 0; 4551 break; 4552 } 4553 4554 regs[rd] = sz; 4555 4556 break; 4557 } 4558 4559 case DIF_SUBR_STRCHR: 4560 case DIF_SUBR_STRRCHR: { 4561 /* 4562 * We're going to iterate over the string looking for the 4563 * specified character. We will iterate until we have reached 4564 * the string length or we have found the character. If this 4565 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 4566 * of the specified character instead of the first. 4567 */ 4568 uintptr_t saddr = tupregs[0].dttk_value; 4569 uintptr_t addr = tupregs[0].dttk_value; 4570 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 4571 char c, target = (char)tupregs[1].dttk_value; 4572 4573 for (regs[rd] = 0; addr < limit; addr++) { 4574 if ((c = dtrace_load8(addr)) == target) { 4575 regs[rd] = addr; 4576 4577 if (subr == DIF_SUBR_STRCHR) 4578 break; 4579 } 4580 4581 if (c == '\0') 4582 break; 4583 } 4584 4585 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 4586 regs[rd] = 0; 4587 break; 4588 } 4589 4590 break; 4591 } 4592 4593 case DIF_SUBR_STRSTR: 4594 case DIF_SUBR_INDEX: 4595 case DIF_SUBR_RINDEX: { 4596 /* 4597 * We're going to iterate over the string looking for the 4598 * specified string. We will iterate until we have reached 4599 * the string length or we have found the string. (Yes, this 4600 * is done in the most naive way possible -- but considering 4601 * that the string we're searching for is likely to be 4602 * relatively short, the complexity of Rabin-Karp or similar 4603 * hardly seems merited.) 4604 */ 4605 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 4606 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 4607 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4608 size_t len = dtrace_strlen(addr, size); 4609 size_t sublen = dtrace_strlen(substr, size); 4610 char *limit = addr + len, *orig = addr; 4611 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 4612 int inc = 1; 4613 4614 regs[rd] = notfound; 4615 4616 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 4617 regs[rd] = 0; 4618 break; 4619 } 4620 4621 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 4622 vstate)) { 4623 regs[rd] = 0; 4624 break; 4625 } 4626 4627 /* 4628 * strstr() and index()/rindex() have similar semantics if 4629 * both strings are the empty string: strstr() returns a 4630 * pointer to the (empty) string, and index() and rindex() 4631 * both return index 0 (regardless of any position argument). 4632 */ 4633 if (sublen == 0 && len == 0) { 4634 if (subr == DIF_SUBR_STRSTR) 4635 regs[rd] = (uintptr_t)addr; 4636 else 4637 regs[rd] = 0; 4638 break; 4639 } 4640 4641 if (subr != DIF_SUBR_STRSTR) { 4642 if (subr == DIF_SUBR_RINDEX) { 4643 limit = orig - 1; 4644 addr += len; 4645 inc = -1; 4646 } 4647 4648 /* 4649 * Both index() and rindex() take an optional position 4650 * argument that denotes the starting position. 4651 */ 4652 if (nargs == 3) { 4653 int64_t pos = (int64_t)tupregs[2].dttk_value; 4654 4655 /* 4656 * If the position argument to index() is 4657 * negative, Perl implicitly clamps it at 4658 * zero. This semantic is a little surprising 4659 * given the special meaning of negative 4660 * positions to similar Perl functions like 4661 * substr(), but it appears to reflect a 4662 * notion that index() can start from a 4663 * negative index and increment its way up to 4664 * the string. Given this notion, Perl's 4665 * rindex() is at least self-consistent in 4666 * that it implicitly clamps positions greater 4667 * than the string length to be the string 4668 * length. Where Perl completely loses 4669 * coherence, however, is when the specified 4670 * substring is the empty string (""). In 4671 * this case, even if the position is 4672 * negative, rindex() returns 0 -- and even if 4673 * the position is greater than the length, 4674 * index() returns the string length. These 4675 * semantics violate the notion that index() 4676 * should never return a value less than the 4677 * specified position and that rindex() should 4678 * never return a value greater than the 4679 * specified position. (One assumes that 4680 * these semantics are artifacts of Perl's 4681 * implementation and not the results of 4682 * deliberate design -- it beggars belief that 4683 * even Larry Wall could desire such oddness.) 4684 * While in the abstract one would wish for 4685 * consistent position semantics across 4686 * substr(), index() and rindex() -- or at the 4687 * very least self-consistent position 4688 * semantics for index() and rindex() -- we 4689 * instead opt to keep with the extant Perl 4690 * semantics, in all their broken glory. (Do 4691 * we have more desire to maintain Perl's 4692 * semantics than Perl does? Probably.) 4693 */ 4694 if (subr == DIF_SUBR_RINDEX) { 4695 if (pos < 0) { 4696 if (sublen == 0) 4697 regs[rd] = 0; 4698 break; 4699 } 4700 4701 if (pos > len) 4702 pos = len; 4703 } else { 4704 if (pos < 0) 4705 pos = 0; 4706 4707 if (pos >= len) { 4708 if (sublen == 0) 4709 regs[rd] = len; 4710 break; 4711 } 4712 } 4713 4714 addr = orig + pos; 4715 } 4716 } 4717 4718 for (regs[rd] = notfound; addr != limit; addr += inc) { 4719 if (dtrace_strncmp(addr, substr, sublen) == 0) { 4720 if (subr != DIF_SUBR_STRSTR) { 4721 /* 4722 * As D index() and rindex() are 4723 * modeled on Perl (and not on awk), 4724 * we return a zero-based (and not a 4725 * one-based) index. (For you Perl 4726 * weenies: no, we're not going to add 4727 * $[ -- and shouldn't you be at a con 4728 * or something?) 4729 */ 4730 regs[rd] = (uintptr_t)(addr - orig); 4731 break; 4732 } 4733 4734 ASSERT(subr == DIF_SUBR_STRSTR); 4735 regs[rd] = (uintptr_t)addr; 4736 break; 4737 } 4738 } 4739 4740 break; 4741 } 4742 4743 case DIF_SUBR_STRTOK: { 4744 uintptr_t addr = tupregs[0].dttk_value; 4745 uintptr_t tokaddr = tupregs[1].dttk_value; 4746 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4747 uintptr_t limit, toklimit = tokaddr + size; 4748 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 4749 char *dest = (char *)mstate->dtms_scratch_ptr; 4750 int i; 4751 4752 /* 4753 * Check both the token buffer and (later) the input buffer, 4754 * since both could be non-scratch addresses. 4755 */ 4756 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 4757 regs[rd] = 0; 4758 break; 4759 } 4760 4761 if (!DTRACE_INSCRATCH(mstate, size)) { 4762 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4763 regs[rd] = 0; 4764 break; 4765 } 4766 4767 if (addr == 0) { 4768 /* 4769 * If the address specified is NULL, we use our saved 4770 * strtok pointer from the mstate. Note that this 4771 * means that the saved strtok pointer is _only_ 4772 * valid within multiple enablings of the same probe -- 4773 * it behaves like an implicit clause-local variable. 4774 */ 4775 addr = mstate->dtms_strtok; 4776 } else { 4777 /* 4778 * If the user-specified address is non-NULL we must 4779 * access check it. This is the only time we have 4780 * a chance to do so, since this address may reside 4781 * in the string table of this clause-- future calls 4782 * (when we fetch addr from mstate->dtms_strtok) 4783 * would fail this access check. 4784 */ 4785 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 4786 regs[rd] = 0; 4787 break; 4788 } 4789 } 4790 4791 /* 4792 * First, zero the token map, and then process the token 4793 * string -- setting a bit in the map for every character 4794 * found in the token string. 4795 */ 4796 for (i = 0; i < sizeof (tokmap); i++) 4797 tokmap[i] = 0; 4798 4799 for (; tokaddr < toklimit; tokaddr++) { 4800 if ((c = dtrace_load8(tokaddr)) == '\0') 4801 break; 4802 4803 ASSERT((c >> 3) < sizeof (tokmap)); 4804 tokmap[c >> 3] |= (1 << (c & 0x7)); 4805 } 4806 4807 for (limit = addr + size; addr < limit; addr++) { 4808 /* 4809 * We're looking for a character that is _not_ contained 4810 * in the token string. 4811 */ 4812 if ((c = dtrace_load8(addr)) == '\0') 4813 break; 4814 4815 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 4816 break; 4817 } 4818 4819 if (c == '\0') { 4820 /* 4821 * We reached the end of the string without finding 4822 * any character that was not in the token string. 4823 * We return NULL in this case, and we set the saved 4824 * address to NULL as well. 4825 */ 4826 regs[rd] = 0; 4827 mstate->dtms_strtok = 0; 4828 break; 4829 } 4830 4831 /* 4832 * From here on, we're copying into the destination string. 4833 */ 4834 for (i = 0; addr < limit && i < size - 1; addr++) { 4835 if ((c = dtrace_load8(addr)) == '\0') 4836 break; 4837 4838 if (tokmap[c >> 3] & (1 << (c & 0x7))) 4839 break; 4840 4841 ASSERT(i < size); 4842 dest[i++] = c; 4843 } 4844 4845 ASSERT(i < size); 4846 dest[i] = '\0'; 4847 regs[rd] = (uintptr_t)dest; 4848 mstate->dtms_scratch_ptr += size; 4849 mstate->dtms_strtok = addr; 4850 break; 4851 } 4852 4853 case DIF_SUBR_SUBSTR: { 4854 uintptr_t s = tupregs[0].dttk_value; 4855 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4856 char *d = (char *)mstate->dtms_scratch_ptr; 4857 int64_t index = (int64_t)tupregs[1].dttk_value; 4858 int64_t remaining = (int64_t)tupregs[2].dttk_value; 4859 size_t len = dtrace_strlen((char *)s, size); 4860 int64_t i; 4861 4862 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4863 regs[rd] = 0; 4864 break; 4865 } 4866 4867 if (!DTRACE_INSCRATCH(mstate, size)) { 4868 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4869 regs[rd] = 0; 4870 break; 4871 } 4872 4873 if (nargs <= 2) 4874 remaining = (int64_t)size; 4875 4876 if (index < 0) { 4877 index += len; 4878 4879 if (index < 0 && index + remaining > 0) { 4880 remaining += index; 4881 index = 0; 4882 } 4883 } 4884 4885 if (index >= len || index < 0) { 4886 remaining = 0; 4887 } else if (remaining < 0) { 4888 remaining += len - index; 4889 } else if (index + remaining > size) { 4890 remaining = size - index; 4891 } 4892 4893 for (i = 0; i < remaining; i++) { 4894 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 4895 break; 4896 } 4897 4898 d[i] = '\0'; 4899 4900 mstate->dtms_scratch_ptr += size; 4901 regs[rd] = (uintptr_t)d; 4902 break; 4903 } 4904 4905 case DIF_SUBR_JSON: { 4906 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4907 uintptr_t json = tupregs[0].dttk_value; 4908 size_t jsonlen = dtrace_strlen((char *)json, size); 4909 uintptr_t elem = tupregs[1].dttk_value; 4910 size_t elemlen = dtrace_strlen((char *)elem, size); 4911 4912 char *dest = (char *)mstate->dtms_scratch_ptr; 4913 char *elemlist = (char *)mstate->dtms_scratch_ptr + jsonlen + 1; 4914 char *ee = elemlist; 4915 int nelems = 1; 4916 uintptr_t cur; 4917 4918 if (!dtrace_canload(json, jsonlen + 1, mstate, vstate) || 4919 !dtrace_canload(elem, elemlen + 1, mstate, vstate)) { 4920 regs[rd] = 0; 4921 break; 4922 } 4923 4924 if (!DTRACE_INSCRATCH(mstate, jsonlen + 1 + elemlen + 1)) { 4925 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4926 regs[rd] = 0; 4927 break; 4928 } 4929 4930 /* 4931 * Read the element selector and split it up into a packed list 4932 * of strings. 4933 */ 4934 for (cur = elem; cur < elem + elemlen; cur++) { 4935 char cc = dtrace_load8(cur); 4936 4937 if (cur == elem && cc == '[') { 4938 /* 4939 * If the first element selector key is 4940 * actually an array index then ignore the 4941 * bracket. 4942 */ 4943 continue; 4944 } 4945 4946 if (cc == ']') 4947 continue; 4948 4949 if (cc == '.' || cc == '[') { 4950 nelems++; 4951 cc = '\0'; 4952 } 4953 4954 *ee++ = cc; 4955 } 4956 *ee++ = '\0'; 4957 4958 if ((regs[rd] = (uintptr_t)dtrace_json(size, json, elemlist, 4959 nelems, dest)) != 0) 4960 mstate->dtms_scratch_ptr += jsonlen + 1; 4961 break; 4962 } 4963 4964 case DIF_SUBR_TOUPPER: 4965 case DIF_SUBR_TOLOWER: { 4966 uintptr_t s = tupregs[0].dttk_value; 4967 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4968 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4969 size_t len = dtrace_strlen((char *)s, size); 4970 char lower, upper, convert; 4971 int64_t i; 4972 4973 if (subr == DIF_SUBR_TOUPPER) { 4974 lower = 'a'; 4975 upper = 'z'; 4976 convert = 'A'; 4977 } else { 4978 lower = 'A'; 4979 upper = 'Z'; 4980 convert = 'a'; 4981 } 4982 4983 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 4984 regs[rd] = 0; 4985 break; 4986 } 4987 4988 if (!DTRACE_INSCRATCH(mstate, size)) { 4989 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4990 regs[rd] = 0; 4991 break; 4992 } 4993 4994 for (i = 0; i < size - 1; i++) { 4995 if ((c = dtrace_load8(s + i)) == '\0') 4996 break; 4997 4998 if (c >= lower && c <= upper) 4999 c = convert + (c - lower); 5000 5001 dest[i] = c; 5002 } 5003 5004 ASSERT(i < size); 5005 dest[i] = '\0'; 5006 regs[rd] = (uintptr_t)dest; 5007 mstate->dtms_scratch_ptr += size; 5008 break; 5009 } 5010 5011#ifdef illumos 5012 case DIF_SUBR_GETMAJOR: 5013#ifdef _LP64 5014 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 5015#else 5016 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 5017#endif 5018 break; 5019 5020 case DIF_SUBR_GETMINOR: 5021#ifdef _LP64 5022 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 5023#else 5024 regs[rd] = tupregs[0].dttk_value & MAXMIN; 5025#endif 5026 break; 5027 5028 case DIF_SUBR_DDI_PATHNAME: { 5029 /* 5030 * This one is a galactic mess. We are going to roughly 5031 * emulate ddi_pathname(), but it's made more complicated 5032 * by the fact that we (a) want to include the minor name and 5033 * (b) must proceed iteratively instead of recursively. 5034 */ 5035 uintptr_t dest = mstate->dtms_scratch_ptr; 5036 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5037 char *start = (char *)dest, *end = start + size - 1; 5038 uintptr_t daddr = tupregs[0].dttk_value; 5039 int64_t minor = (int64_t)tupregs[1].dttk_value; 5040 char *s; 5041 int i, len, depth = 0; 5042 5043 /* 5044 * Due to all the pointer jumping we do and context we must 5045 * rely upon, we just mandate that the user must have kernel 5046 * read privileges to use this routine. 5047 */ 5048 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 5049 *flags |= CPU_DTRACE_KPRIV; 5050 *illval = daddr; 5051 regs[rd] = 0; 5052 } 5053 5054 if (!DTRACE_INSCRATCH(mstate, size)) { 5055 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5056 regs[rd] = 0; 5057 break; 5058 } 5059 5060 *end = '\0'; 5061 5062 /* 5063 * We want to have a name for the minor. In order to do this, 5064 * we need to walk the minor list from the devinfo. We want 5065 * to be sure that we don't infinitely walk a circular list, 5066 * so we check for circularity by sending a scout pointer 5067 * ahead two elements for every element that we iterate over; 5068 * if the list is circular, these will ultimately point to the 5069 * same element. You may recognize this little trick as the 5070 * answer to a stupid interview question -- one that always 5071 * seems to be asked by those who had to have it laboriously 5072 * explained to them, and who can't even concisely describe 5073 * the conditions under which one would be forced to resort to 5074 * this technique. Needless to say, those conditions are 5075 * found here -- and probably only here. Is this the only use 5076 * of this infamous trick in shipping, production code? If it 5077 * isn't, it probably should be... 5078 */ 5079 if (minor != -1) { 5080 uintptr_t maddr = dtrace_loadptr(daddr + 5081 offsetof(struct dev_info, devi_minor)); 5082 5083 uintptr_t next = offsetof(struct ddi_minor_data, next); 5084 uintptr_t name = offsetof(struct ddi_minor_data, 5085 d_minor) + offsetof(struct ddi_minor, name); 5086 uintptr_t dev = offsetof(struct ddi_minor_data, 5087 d_minor) + offsetof(struct ddi_minor, dev); 5088 uintptr_t scout; 5089 5090 if (maddr != NULL) 5091 scout = dtrace_loadptr(maddr + next); 5092 5093 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5094 uint64_t m; 5095#ifdef _LP64 5096 m = dtrace_load64(maddr + dev) & MAXMIN64; 5097#else 5098 m = dtrace_load32(maddr + dev) & MAXMIN; 5099#endif 5100 if (m != minor) { 5101 maddr = dtrace_loadptr(maddr + next); 5102 5103 if (scout == NULL) 5104 continue; 5105 5106 scout = dtrace_loadptr(scout + next); 5107 5108 if (scout == NULL) 5109 continue; 5110 5111 scout = dtrace_loadptr(scout + next); 5112 5113 if (scout == NULL) 5114 continue; 5115 5116 if (scout == maddr) { 5117 *flags |= CPU_DTRACE_ILLOP; 5118 break; 5119 } 5120 5121 continue; 5122 } 5123 5124 /* 5125 * We have the minor data. Now we need to 5126 * copy the minor's name into the end of the 5127 * pathname. 5128 */ 5129 s = (char *)dtrace_loadptr(maddr + name); 5130 len = dtrace_strlen(s, size); 5131 5132 if (*flags & CPU_DTRACE_FAULT) 5133 break; 5134 5135 if (len != 0) { 5136 if ((end -= (len + 1)) < start) 5137 break; 5138 5139 *end = ':'; 5140 } 5141 5142 for (i = 1; i <= len; i++) 5143 end[i] = dtrace_load8((uintptr_t)s++); 5144 break; 5145 } 5146 } 5147 5148 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 5149 ddi_node_state_t devi_state; 5150 5151 devi_state = dtrace_load32(daddr + 5152 offsetof(struct dev_info, devi_node_state)); 5153 5154 if (*flags & CPU_DTRACE_FAULT) 5155 break; 5156 5157 if (devi_state >= DS_INITIALIZED) { 5158 s = (char *)dtrace_loadptr(daddr + 5159 offsetof(struct dev_info, devi_addr)); 5160 len = dtrace_strlen(s, size); 5161 5162 if (*flags & CPU_DTRACE_FAULT) 5163 break; 5164 5165 if (len != 0) { 5166 if ((end -= (len + 1)) < start) 5167 break; 5168 5169 *end = '@'; 5170 } 5171 5172 for (i = 1; i <= len; i++) 5173 end[i] = dtrace_load8((uintptr_t)s++); 5174 } 5175 5176 /* 5177 * Now for the node name... 5178 */ 5179 s = (char *)dtrace_loadptr(daddr + 5180 offsetof(struct dev_info, devi_node_name)); 5181 5182 daddr = dtrace_loadptr(daddr + 5183 offsetof(struct dev_info, devi_parent)); 5184 5185 /* 5186 * If our parent is NULL (that is, if we're the root 5187 * node), we're going to use the special path 5188 * "devices". 5189 */ 5190 if (daddr == 0) 5191 s = "devices"; 5192 5193 len = dtrace_strlen(s, size); 5194 if (*flags & CPU_DTRACE_FAULT) 5195 break; 5196 5197 if ((end -= (len + 1)) < start) 5198 break; 5199 5200 for (i = 1; i <= len; i++) 5201 end[i] = dtrace_load8((uintptr_t)s++); 5202 *end = '/'; 5203 5204 if (depth++ > dtrace_devdepth_max) { 5205 *flags |= CPU_DTRACE_ILLOP; 5206 break; 5207 } 5208 } 5209 5210 if (end < start) 5211 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5212 5213 if (daddr == 0) { 5214 regs[rd] = (uintptr_t)end; 5215 mstate->dtms_scratch_ptr += size; 5216 } 5217 5218 break; 5219 } 5220#endif 5221 5222 case DIF_SUBR_STRJOIN: { 5223 char *d = (char *)mstate->dtms_scratch_ptr; 5224 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5225 uintptr_t s1 = tupregs[0].dttk_value; 5226 uintptr_t s2 = tupregs[1].dttk_value; 5227 int i = 0; 5228 5229 if (!dtrace_strcanload(s1, size, mstate, vstate) || 5230 !dtrace_strcanload(s2, size, mstate, vstate)) { 5231 regs[rd] = 0; 5232 break; 5233 } 5234 5235 if (!DTRACE_INSCRATCH(mstate, size)) { 5236 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5237 regs[rd] = 0; 5238 break; 5239 } 5240 5241 for (;;) { 5242 if (i >= size) { 5243 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5244 regs[rd] = 0; 5245 break; 5246 } 5247 5248 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 5249 i--; 5250 break; 5251 } 5252 } 5253 5254 for (;;) { 5255 if (i >= size) { 5256 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5257 regs[rd] = 0; 5258 break; 5259 } 5260 5261 if ((d[i++] = dtrace_load8(s2++)) == '\0') 5262 break; 5263 } 5264 5265 if (i < size) { 5266 mstate->dtms_scratch_ptr += i; 5267 regs[rd] = (uintptr_t)d; 5268 } 5269 5270 break; 5271 } 5272 5273 case DIF_SUBR_STRTOLL: { 5274 uintptr_t s = tupregs[0].dttk_value; 5275 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5276 int base = 10; 5277 5278 if (nargs > 1) { 5279 if ((base = tupregs[1].dttk_value) <= 1 || 5280 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5281 *flags |= CPU_DTRACE_ILLOP; 5282 break; 5283 } 5284 } 5285 5286 if (!dtrace_strcanload(s, size, mstate, vstate)) { 5287 regs[rd] = INT64_MIN; 5288 break; 5289 } 5290 5291 regs[rd] = dtrace_strtoll((char *)s, base, size); 5292 break; 5293 } 5294 5295 case DIF_SUBR_LLTOSTR: { 5296 int64_t i = (int64_t)tupregs[0].dttk_value; 5297 uint64_t val, digit; 5298 uint64_t size = 65; /* enough room for 2^64 in binary */ 5299 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 5300 int base = 10; 5301 5302 if (nargs > 1) { 5303 if ((base = tupregs[1].dttk_value) <= 1 || 5304 base > ('z' - 'a' + 1) + ('9' - '0' + 1)) { 5305 *flags |= CPU_DTRACE_ILLOP; 5306 break; 5307 } 5308 } 5309 5310 val = (base == 10 && i < 0) ? i * -1 : i; 5311 5312 if (!DTRACE_INSCRATCH(mstate, size)) { 5313 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5314 regs[rd] = 0; 5315 break; 5316 } 5317 5318 for (*end-- = '\0'; val; val /= base) { 5319 if ((digit = val % base) <= '9' - '0') { 5320 *end-- = '0' + digit; 5321 } else { 5322 *end-- = 'a' + (digit - ('9' - '0') - 1); 5323 } 5324 } 5325 5326 if (i == 0 && base == 16) 5327 *end-- = '0'; 5328 5329 if (base == 16) 5330 *end-- = 'x'; 5331 5332 if (i == 0 || base == 8 || base == 16) 5333 *end-- = '0'; 5334 5335 if (i < 0 && base == 10) 5336 *end-- = '-'; 5337 5338 regs[rd] = (uintptr_t)end + 1; 5339 mstate->dtms_scratch_ptr += size; 5340 break; 5341 } 5342 5343 case DIF_SUBR_HTONS: 5344 case DIF_SUBR_NTOHS: 5345#if BYTE_ORDER == BIG_ENDIAN 5346 regs[rd] = (uint16_t)tupregs[0].dttk_value; 5347#else 5348 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 5349#endif 5350 break; 5351 5352 5353 case DIF_SUBR_HTONL: 5354 case DIF_SUBR_NTOHL: 5355#if BYTE_ORDER == BIG_ENDIAN 5356 regs[rd] = (uint32_t)tupregs[0].dttk_value; 5357#else 5358 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 5359#endif 5360 break; 5361 5362 5363 case DIF_SUBR_HTONLL: 5364 case DIF_SUBR_NTOHLL: 5365#if BYTE_ORDER == BIG_ENDIAN 5366 regs[rd] = (uint64_t)tupregs[0].dttk_value; 5367#else 5368 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 5369#endif 5370 break; 5371 5372 5373 case DIF_SUBR_DIRNAME: 5374 case DIF_SUBR_BASENAME: { 5375 char *dest = (char *)mstate->dtms_scratch_ptr; 5376 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5377 uintptr_t src = tupregs[0].dttk_value; 5378 int i, j, len = dtrace_strlen((char *)src, size); 5379 int lastbase = -1, firstbase = -1, lastdir = -1; 5380 int start, end; 5381 5382 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 5383 regs[rd] = 0; 5384 break; 5385 } 5386 5387 if (!DTRACE_INSCRATCH(mstate, size)) { 5388 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5389 regs[rd] = 0; 5390 break; 5391 } 5392 5393 /* 5394 * The basename and dirname for a zero-length string is 5395 * defined to be "." 5396 */ 5397 if (len == 0) { 5398 len = 1; 5399 src = (uintptr_t)"."; 5400 } 5401 5402 /* 5403 * Start from the back of the string, moving back toward the 5404 * front until we see a character that isn't a slash. That 5405 * character is the last character in the basename. 5406 */ 5407 for (i = len - 1; i >= 0; i--) { 5408 if (dtrace_load8(src + i) != '/') 5409 break; 5410 } 5411 5412 if (i >= 0) 5413 lastbase = i; 5414 5415 /* 5416 * Starting from the last character in the basename, move 5417 * towards the front until we find a slash. The character 5418 * that we processed immediately before that is the first 5419 * character in the basename. 5420 */ 5421 for (; i >= 0; i--) { 5422 if (dtrace_load8(src + i) == '/') 5423 break; 5424 } 5425 5426 if (i >= 0) 5427 firstbase = i + 1; 5428 5429 /* 5430 * Now keep going until we find a non-slash character. That 5431 * character is the last character in the dirname. 5432 */ 5433 for (; i >= 0; i--) { 5434 if (dtrace_load8(src + i) != '/') 5435 break; 5436 } 5437 5438 if (i >= 0) 5439 lastdir = i; 5440 5441 ASSERT(!(lastbase == -1 && firstbase != -1)); 5442 ASSERT(!(firstbase == -1 && lastdir != -1)); 5443 5444 if (lastbase == -1) { 5445 /* 5446 * We didn't find a non-slash character. We know that 5447 * the length is non-zero, so the whole string must be 5448 * slashes. In either the dirname or the basename 5449 * case, we return '/'. 5450 */ 5451 ASSERT(firstbase == -1); 5452 firstbase = lastbase = lastdir = 0; 5453 } 5454 5455 if (firstbase == -1) { 5456 /* 5457 * The entire string consists only of a basename 5458 * component. If we're looking for dirname, we need 5459 * to change our string to be just "."; if we're 5460 * looking for a basename, we'll just set the first 5461 * character of the basename to be 0. 5462 */ 5463 if (subr == DIF_SUBR_DIRNAME) { 5464 ASSERT(lastdir == -1); 5465 src = (uintptr_t)"."; 5466 lastdir = 0; 5467 } else { 5468 firstbase = 0; 5469 } 5470 } 5471 5472 if (subr == DIF_SUBR_DIRNAME) { 5473 if (lastdir == -1) { 5474 /* 5475 * We know that we have a slash in the name -- 5476 * or lastdir would be set to 0, above. And 5477 * because lastdir is -1, we know that this 5478 * slash must be the first character. (That 5479 * is, the full string must be of the form 5480 * "/basename".) In this case, the last 5481 * character of the directory name is 0. 5482 */ 5483 lastdir = 0; 5484 } 5485 5486 start = 0; 5487 end = lastdir; 5488 } else { 5489 ASSERT(subr == DIF_SUBR_BASENAME); 5490 ASSERT(firstbase != -1 && lastbase != -1); 5491 start = firstbase; 5492 end = lastbase; 5493 } 5494 5495 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 5496 dest[j] = dtrace_load8(src + i); 5497 5498 dest[j] = '\0'; 5499 regs[rd] = (uintptr_t)dest; 5500 mstate->dtms_scratch_ptr += size; 5501 break; 5502 } 5503 5504 case DIF_SUBR_GETF: { 5505 uintptr_t fd = tupregs[0].dttk_value; 5506 struct filedesc *fdp; 5507 file_t *fp; 5508 5509 if (!dtrace_priv_proc(state)) { 5510 regs[rd] = 0; 5511 break; 5512 } 5513 fdp = curproc->p_fd; 5514 FILEDESC_SLOCK(fdp); 5515 fp = fget_locked(fdp, fd); 5516 mstate->dtms_getf = fp; 5517 regs[rd] = (uintptr_t)fp; 5518 FILEDESC_SUNLOCK(fdp); 5519 break; 5520 } 5521 5522 case DIF_SUBR_CLEANPATH: { 5523 char *dest = (char *)mstate->dtms_scratch_ptr, c; 5524 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 5525 uintptr_t src = tupregs[0].dttk_value; 5526 int i = 0, j = 0; 5527#ifdef illumos 5528 zone_t *z; 5529#endif 5530 5531 if (!dtrace_strcanload(src, size, mstate, vstate)) { 5532 regs[rd] = 0; 5533 break; 5534 } 5535 5536 if (!DTRACE_INSCRATCH(mstate, size)) { 5537 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5538 regs[rd] = 0; 5539 break; 5540 } 5541 5542 /* 5543 * Move forward, loading each character. 5544 */ 5545 do { 5546 c = dtrace_load8(src + i++); 5547next: 5548 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 5549 break; 5550 5551 if (c != '/') { 5552 dest[j++] = c; 5553 continue; 5554 } 5555 5556 c = dtrace_load8(src + i++); 5557 5558 if (c == '/') { 5559 /* 5560 * We have two slashes -- we can just advance 5561 * to the next character. 5562 */ 5563 goto next; 5564 } 5565 5566 if (c != '.') { 5567 /* 5568 * This is not "." and it's not ".." -- we can 5569 * just store the "/" and this character and 5570 * drive on. 5571 */ 5572 dest[j++] = '/'; 5573 dest[j++] = c; 5574 continue; 5575 } 5576 5577 c = dtrace_load8(src + i++); 5578 5579 if (c == '/') { 5580 /* 5581 * This is a "/./" component. We're not going 5582 * to store anything in the destination buffer; 5583 * we're just going to go to the next component. 5584 */ 5585 goto next; 5586 } 5587 5588 if (c != '.') { 5589 /* 5590 * This is not ".." -- we can just store the 5591 * "/." and this character and continue 5592 * processing. 5593 */ 5594 dest[j++] = '/'; 5595 dest[j++] = '.'; 5596 dest[j++] = c; 5597 continue; 5598 } 5599 5600 c = dtrace_load8(src + i++); 5601 5602 if (c != '/' && c != '\0') { 5603 /* 5604 * This is not ".." -- it's "..[mumble]". 5605 * We'll store the "/.." and this character 5606 * and continue processing. 5607 */ 5608 dest[j++] = '/'; 5609 dest[j++] = '.'; 5610 dest[j++] = '.'; 5611 dest[j++] = c; 5612 continue; 5613 } 5614 5615 /* 5616 * This is "/../" or "/..\0". We need to back up 5617 * our destination pointer until we find a "/". 5618 */ 5619 i--; 5620 while (j != 0 && dest[--j] != '/') 5621 continue; 5622 5623 if (c == '\0') 5624 dest[++j] = '/'; 5625 } while (c != '\0'); 5626 5627 dest[j] = '\0'; 5628 5629#ifdef illumos 5630 if (mstate->dtms_getf != NULL && 5631 !(mstate->dtms_access & DTRACE_ACCESS_KERNEL) && 5632 (z = state->dts_cred.dcr_cred->cr_zone) != kcred->cr_zone) { 5633 /* 5634 * If we've done a getf() as a part of this ECB and we 5635 * don't have kernel access (and we're not in the global 5636 * zone), check if the path we cleaned up begins with 5637 * the zone's root path, and trim it off if so. Note 5638 * that this is an output cleanliness issue, not a 5639 * security issue: knowing one's zone root path does 5640 * not enable privilege escalation. 5641 */ 5642 if (strstr(dest, z->zone_rootpath) == dest) 5643 dest += strlen(z->zone_rootpath) - 1; 5644 } 5645#endif 5646 5647 regs[rd] = (uintptr_t)dest; 5648 mstate->dtms_scratch_ptr += size; 5649 break; 5650 } 5651 5652 case DIF_SUBR_INET_NTOA: 5653 case DIF_SUBR_INET_NTOA6: 5654 case DIF_SUBR_INET_NTOP: { 5655 size_t size; 5656 int af, argi, i; 5657 char *base, *end; 5658 5659 if (subr == DIF_SUBR_INET_NTOP) { 5660 af = (int)tupregs[0].dttk_value; 5661 argi = 1; 5662 } else { 5663 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 5664 argi = 0; 5665 } 5666 5667 if (af == AF_INET) { 5668 ipaddr_t ip4; 5669 uint8_t *ptr8, val; 5670 5671 if (!dtrace_canload(tupregs[argi].dttk_value, 5672 sizeof (ipaddr_t), mstate, vstate)) { 5673 regs[rd] = 0; 5674 break; 5675 } 5676 5677 /* 5678 * Safely load the IPv4 address. 5679 */ 5680 ip4 = dtrace_load32(tupregs[argi].dttk_value); 5681 5682 /* 5683 * Check an IPv4 string will fit in scratch. 5684 */ 5685 size = INET_ADDRSTRLEN; 5686 if (!DTRACE_INSCRATCH(mstate, size)) { 5687 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5688 regs[rd] = 0; 5689 break; 5690 } 5691 base = (char *)mstate->dtms_scratch_ptr; 5692 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5693 5694 /* 5695 * Stringify as a dotted decimal quad. 5696 */ 5697 *end-- = '\0'; 5698 ptr8 = (uint8_t *)&ip4; 5699 for (i = 3; i >= 0; i--) { 5700 val = ptr8[i]; 5701 5702 if (val == 0) { 5703 *end-- = '0'; 5704 } else { 5705 for (; val; val /= 10) { 5706 *end-- = '0' + (val % 10); 5707 } 5708 } 5709 5710 if (i > 0) 5711 *end-- = '.'; 5712 } 5713 ASSERT(end + 1 >= base); 5714 5715 } else if (af == AF_INET6) { 5716 struct in6_addr ip6; 5717 int firstzero, tryzero, numzero, v6end; 5718 uint16_t val; 5719 const char digits[] = "0123456789abcdef"; 5720 5721 /* 5722 * Stringify using RFC 1884 convention 2 - 16 bit 5723 * hexadecimal values with a zero-run compression. 5724 * Lower case hexadecimal digits are used. 5725 * eg, fe80::214:4fff:fe0b:76c8. 5726 * The IPv4 embedded form is returned for inet_ntop, 5727 * just the IPv4 string is returned for inet_ntoa6. 5728 */ 5729 5730 if (!dtrace_canload(tupregs[argi].dttk_value, 5731 sizeof (struct in6_addr), mstate, vstate)) { 5732 regs[rd] = 0; 5733 break; 5734 } 5735 5736 /* 5737 * Safely load the IPv6 address. 5738 */ 5739 dtrace_bcopy( 5740 (void *)(uintptr_t)tupregs[argi].dttk_value, 5741 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 5742 5743 /* 5744 * Check an IPv6 string will fit in scratch. 5745 */ 5746 size = INET6_ADDRSTRLEN; 5747 if (!DTRACE_INSCRATCH(mstate, size)) { 5748 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5749 regs[rd] = 0; 5750 break; 5751 } 5752 base = (char *)mstate->dtms_scratch_ptr; 5753 end = (char *)mstate->dtms_scratch_ptr + size - 1; 5754 *end-- = '\0'; 5755 5756 /* 5757 * Find the longest run of 16 bit zero values 5758 * for the single allowed zero compression - "::". 5759 */ 5760 firstzero = -1; 5761 tryzero = -1; 5762 numzero = 1; 5763 for (i = 0; i < sizeof (struct in6_addr); i++) { 5764#ifdef illumos 5765 if (ip6._S6_un._S6_u8[i] == 0 && 5766#else 5767 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5768#endif 5769 tryzero == -1 && i % 2 == 0) { 5770 tryzero = i; 5771 continue; 5772 } 5773 5774 if (tryzero != -1 && 5775#ifdef illumos 5776 (ip6._S6_un._S6_u8[i] != 0 || 5777#else 5778 (ip6.__u6_addr.__u6_addr8[i] != 0 || 5779#endif 5780 i == sizeof (struct in6_addr) - 1)) { 5781 5782 if (i - tryzero <= numzero) { 5783 tryzero = -1; 5784 continue; 5785 } 5786 5787 firstzero = tryzero; 5788 numzero = i - i % 2 - tryzero; 5789 tryzero = -1; 5790 5791#ifdef illumos 5792 if (ip6._S6_un._S6_u8[i] == 0 && 5793#else 5794 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 5795#endif 5796 i == sizeof (struct in6_addr) - 1) 5797 numzero += 2; 5798 } 5799 } 5800 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 5801 5802 /* 5803 * Check for an IPv4 embedded address. 5804 */ 5805 v6end = sizeof (struct in6_addr) - 2; 5806 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 5807 IN6_IS_ADDR_V4COMPAT(&ip6)) { 5808 for (i = sizeof (struct in6_addr) - 1; 5809 i >= DTRACE_V4MAPPED_OFFSET; i--) { 5810 ASSERT(end >= base); 5811 5812#ifdef illumos 5813 val = ip6._S6_un._S6_u8[i]; 5814#else 5815 val = ip6.__u6_addr.__u6_addr8[i]; 5816#endif 5817 5818 if (val == 0) { 5819 *end-- = '0'; 5820 } else { 5821 for (; val; val /= 10) { 5822 *end-- = '0' + val % 10; 5823 } 5824 } 5825 5826 if (i > DTRACE_V4MAPPED_OFFSET) 5827 *end-- = '.'; 5828 } 5829 5830 if (subr == DIF_SUBR_INET_NTOA6) 5831 goto inetout; 5832 5833 /* 5834 * Set v6end to skip the IPv4 address that 5835 * we have already stringified. 5836 */ 5837 v6end = 10; 5838 } 5839 5840 /* 5841 * Build the IPv6 string by working through the 5842 * address in reverse. 5843 */ 5844 for (i = v6end; i >= 0; i -= 2) { 5845 ASSERT(end >= base); 5846 5847 if (i == firstzero + numzero - 2) { 5848 *end-- = ':'; 5849 *end-- = ':'; 5850 i -= numzero - 2; 5851 continue; 5852 } 5853 5854 if (i < 14 && i != firstzero - 2) 5855 *end-- = ':'; 5856 5857#ifdef illumos 5858 val = (ip6._S6_un._S6_u8[i] << 8) + 5859 ip6._S6_un._S6_u8[i + 1]; 5860#else 5861 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 5862 ip6.__u6_addr.__u6_addr8[i + 1]; 5863#endif 5864 5865 if (val == 0) { 5866 *end-- = '0'; 5867 } else { 5868 for (; val; val /= 16) { 5869 *end-- = digits[val % 16]; 5870 } 5871 } 5872 } 5873 ASSERT(end + 1 >= base); 5874 5875 } else { 5876 /* 5877 * The user didn't use AH_INET or AH_INET6. 5878 */ 5879 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5880 regs[rd] = 0; 5881 break; 5882 } 5883 5884inetout: regs[rd] = (uintptr_t)end + 1; 5885 mstate->dtms_scratch_ptr += size; 5886 break; 5887 } 5888 5889 case DIF_SUBR_MEMREF: { 5890 uintptr_t size = 2 * sizeof(uintptr_t); 5891 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5892 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 5893 5894 /* address and length */ 5895 memref[0] = tupregs[0].dttk_value; 5896 memref[1] = tupregs[1].dttk_value; 5897 5898 regs[rd] = (uintptr_t) memref; 5899 mstate->dtms_scratch_ptr += scratch_size; 5900 break; 5901 } 5902 5903#ifndef illumos 5904 case DIF_SUBR_MEMSTR: { 5905 char *str = (char *)mstate->dtms_scratch_ptr; 5906 uintptr_t mem = tupregs[0].dttk_value; 5907 char c = tupregs[1].dttk_value; 5908 size_t size = tupregs[2].dttk_value; 5909 uint8_t n; 5910 int i; 5911 5912 regs[rd] = 0; 5913 5914 if (size == 0) 5915 break; 5916 5917 if (!dtrace_canload(mem, size - 1, mstate, vstate)) 5918 break; 5919 5920 if (!DTRACE_INSCRATCH(mstate, size)) { 5921 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5922 break; 5923 } 5924 5925 if (dtrace_memstr_max != 0 && size > dtrace_memstr_max) { 5926 *flags |= CPU_DTRACE_ILLOP; 5927 break; 5928 } 5929 5930 for (i = 0; i < size - 1; i++) { 5931 n = dtrace_load8(mem++); 5932 str[i] = (n == 0) ? c : n; 5933 } 5934 str[size - 1] = 0; 5935 5936 regs[rd] = (uintptr_t)str; 5937 mstate->dtms_scratch_ptr += size; 5938 break; 5939 } 5940#endif 5941 5942 case DIF_SUBR_TYPEREF: { 5943 uintptr_t size = 4 * sizeof(uintptr_t); 5944 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 5945 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 5946 5947 /* address, num_elements, type_str, type_len */ 5948 typeref[0] = tupregs[0].dttk_value; 5949 typeref[1] = tupregs[1].dttk_value; 5950 typeref[2] = tupregs[2].dttk_value; 5951 typeref[3] = tupregs[3].dttk_value; 5952 5953 regs[rd] = (uintptr_t) typeref; 5954 mstate->dtms_scratch_ptr += scratch_size; 5955 break; 5956 } 5957 } 5958} 5959 5960/* 5961 * Emulate the execution of DTrace IR instructions specified by the given 5962 * DIF object. This function is deliberately void of assertions as all of 5963 * the necessary checks are handled by a call to dtrace_difo_validate(). 5964 */ 5965static uint64_t 5966dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 5967 dtrace_vstate_t *vstate, dtrace_state_t *state) 5968{ 5969 const dif_instr_t *text = difo->dtdo_buf; 5970 const uint_t textlen = difo->dtdo_len; 5971 const char *strtab = difo->dtdo_strtab; 5972 const uint64_t *inttab = difo->dtdo_inttab; 5973 5974 uint64_t rval = 0; 5975 dtrace_statvar_t *svar; 5976 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 5977 dtrace_difv_t *v; 5978 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5979 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 5980 5981 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 5982 uint64_t regs[DIF_DIR_NREGS]; 5983 uint64_t *tmp; 5984 5985 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 5986 int64_t cc_r; 5987 uint_t pc = 0, id, opc = 0; 5988 uint8_t ttop = 0; 5989 dif_instr_t instr; 5990 uint_t r1, r2, rd; 5991 5992 /* 5993 * We stash the current DIF object into the machine state: we need it 5994 * for subsequent access checking. 5995 */ 5996 mstate->dtms_difo = difo; 5997 5998 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 5999 6000 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 6001 opc = pc; 6002 6003 instr = text[pc++]; 6004 r1 = DIF_INSTR_R1(instr); 6005 r2 = DIF_INSTR_R2(instr); 6006 rd = DIF_INSTR_RD(instr); 6007 6008 switch (DIF_INSTR_OP(instr)) { 6009 case DIF_OP_OR: 6010 regs[rd] = regs[r1] | regs[r2]; 6011 break; 6012 case DIF_OP_XOR: 6013 regs[rd] = regs[r1] ^ regs[r2]; 6014 break; 6015 case DIF_OP_AND: 6016 regs[rd] = regs[r1] & regs[r2]; 6017 break; 6018 case DIF_OP_SLL: 6019 regs[rd] = regs[r1] << regs[r2]; 6020 break; 6021 case DIF_OP_SRL: 6022 regs[rd] = regs[r1] >> regs[r2]; 6023 break; 6024 case DIF_OP_SUB: 6025 regs[rd] = regs[r1] - regs[r2]; 6026 break; 6027 case DIF_OP_ADD: 6028 regs[rd] = regs[r1] + regs[r2]; 6029 break; 6030 case DIF_OP_MUL: 6031 regs[rd] = regs[r1] * regs[r2]; 6032 break; 6033 case DIF_OP_SDIV: 6034 if (regs[r2] == 0) { 6035 regs[rd] = 0; 6036 *flags |= CPU_DTRACE_DIVZERO; 6037 } else { 6038 regs[rd] = (int64_t)regs[r1] / 6039 (int64_t)regs[r2]; 6040 } 6041 break; 6042 6043 case DIF_OP_UDIV: 6044 if (regs[r2] == 0) { 6045 regs[rd] = 0; 6046 *flags |= CPU_DTRACE_DIVZERO; 6047 } else { 6048 regs[rd] = regs[r1] / regs[r2]; 6049 } 6050 break; 6051 6052 case DIF_OP_SREM: 6053 if (regs[r2] == 0) { 6054 regs[rd] = 0; 6055 *flags |= CPU_DTRACE_DIVZERO; 6056 } else { 6057 regs[rd] = (int64_t)regs[r1] % 6058 (int64_t)regs[r2]; 6059 } 6060 break; 6061 6062 case DIF_OP_UREM: 6063 if (regs[r2] == 0) { 6064 regs[rd] = 0; 6065 *flags |= CPU_DTRACE_DIVZERO; 6066 } else { 6067 regs[rd] = regs[r1] % regs[r2]; 6068 } 6069 break; 6070 6071 case DIF_OP_NOT: 6072 regs[rd] = ~regs[r1]; 6073 break; 6074 case DIF_OP_MOV: 6075 regs[rd] = regs[r1]; 6076 break; 6077 case DIF_OP_CMP: 6078 cc_r = regs[r1] - regs[r2]; 6079 cc_n = cc_r < 0; 6080 cc_z = cc_r == 0; 6081 cc_v = 0; 6082 cc_c = regs[r1] < regs[r2]; 6083 break; 6084 case DIF_OP_TST: 6085 cc_n = cc_v = cc_c = 0; 6086 cc_z = regs[r1] == 0; 6087 break; 6088 case DIF_OP_BA: 6089 pc = DIF_INSTR_LABEL(instr); 6090 break; 6091 case DIF_OP_BE: 6092 if (cc_z) 6093 pc = DIF_INSTR_LABEL(instr); 6094 break; 6095 case DIF_OP_BNE: 6096 if (cc_z == 0) 6097 pc = DIF_INSTR_LABEL(instr); 6098 break; 6099 case DIF_OP_BG: 6100 if ((cc_z | (cc_n ^ cc_v)) == 0) 6101 pc = DIF_INSTR_LABEL(instr); 6102 break; 6103 case DIF_OP_BGU: 6104 if ((cc_c | cc_z) == 0) 6105 pc = DIF_INSTR_LABEL(instr); 6106 break; 6107 case DIF_OP_BGE: 6108 if ((cc_n ^ cc_v) == 0) 6109 pc = DIF_INSTR_LABEL(instr); 6110 break; 6111 case DIF_OP_BGEU: 6112 if (cc_c == 0) 6113 pc = DIF_INSTR_LABEL(instr); 6114 break; 6115 case DIF_OP_BL: 6116 if (cc_n ^ cc_v) 6117 pc = DIF_INSTR_LABEL(instr); 6118 break; 6119 case DIF_OP_BLU: 6120 if (cc_c) 6121 pc = DIF_INSTR_LABEL(instr); 6122 break; 6123 case DIF_OP_BLE: 6124 if (cc_z | (cc_n ^ cc_v)) 6125 pc = DIF_INSTR_LABEL(instr); 6126 break; 6127 case DIF_OP_BLEU: 6128 if (cc_c | cc_z) 6129 pc = DIF_INSTR_LABEL(instr); 6130 break; 6131 case DIF_OP_RLDSB: 6132 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6133 break; 6134 /*FALLTHROUGH*/ 6135 case DIF_OP_LDSB: 6136 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 6137 break; 6138 case DIF_OP_RLDSH: 6139 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6140 break; 6141 /*FALLTHROUGH*/ 6142 case DIF_OP_LDSH: 6143 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 6144 break; 6145 case DIF_OP_RLDSW: 6146 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6147 break; 6148 /*FALLTHROUGH*/ 6149 case DIF_OP_LDSW: 6150 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 6151 break; 6152 case DIF_OP_RLDUB: 6153 if (!dtrace_canload(regs[r1], 1, mstate, vstate)) 6154 break; 6155 /*FALLTHROUGH*/ 6156 case DIF_OP_LDUB: 6157 regs[rd] = dtrace_load8(regs[r1]); 6158 break; 6159 case DIF_OP_RLDUH: 6160 if (!dtrace_canload(regs[r1], 2, mstate, vstate)) 6161 break; 6162 /*FALLTHROUGH*/ 6163 case DIF_OP_LDUH: 6164 regs[rd] = dtrace_load16(regs[r1]); 6165 break; 6166 case DIF_OP_RLDUW: 6167 if (!dtrace_canload(regs[r1], 4, mstate, vstate)) 6168 break; 6169 /*FALLTHROUGH*/ 6170 case DIF_OP_LDUW: 6171 regs[rd] = dtrace_load32(regs[r1]); 6172 break; 6173 case DIF_OP_RLDX: 6174 if (!dtrace_canload(regs[r1], 8, mstate, vstate)) 6175 break; 6176 /*FALLTHROUGH*/ 6177 case DIF_OP_LDX: 6178 regs[rd] = dtrace_load64(regs[r1]); 6179 break; 6180 case DIF_OP_ULDSB: 6181 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6182 regs[rd] = (int8_t) 6183 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6184 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6185 break; 6186 case DIF_OP_ULDSH: 6187 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6188 regs[rd] = (int16_t) 6189 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6190 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6191 break; 6192 case DIF_OP_ULDSW: 6193 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6194 regs[rd] = (int32_t) 6195 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6196 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6197 break; 6198 case DIF_OP_ULDUB: 6199 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6200 regs[rd] = 6201 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 6202 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6203 break; 6204 case DIF_OP_ULDUH: 6205 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6206 regs[rd] = 6207 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 6208 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6209 break; 6210 case DIF_OP_ULDUW: 6211 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6212 regs[rd] = 6213 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 6214 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6215 break; 6216 case DIF_OP_ULDX: 6217 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6218 regs[rd] = 6219 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 6220 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6221 break; 6222 case DIF_OP_RET: 6223 rval = regs[rd]; 6224 pc = textlen; 6225 break; 6226 case DIF_OP_NOP: 6227 break; 6228 case DIF_OP_SETX: 6229 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 6230 break; 6231 case DIF_OP_SETS: 6232 regs[rd] = (uint64_t)(uintptr_t) 6233 (strtab + DIF_INSTR_STRING(instr)); 6234 break; 6235 case DIF_OP_SCMP: { 6236 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 6237 uintptr_t s1 = regs[r1]; 6238 uintptr_t s2 = regs[r2]; 6239 6240 if (s1 != 0 && 6241 !dtrace_strcanload(s1, sz, mstate, vstate)) 6242 break; 6243 if (s2 != 0 && 6244 !dtrace_strcanload(s2, sz, mstate, vstate)) 6245 break; 6246 6247 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 6248 6249 cc_n = cc_r < 0; 6250 cc_z = cc_r == 0; 6251 cc_v = cc_c = 0; 6252 break; 6253 } 6254 case DIF_OP_LDGA: 6255 regs[rd] = dtrace_dif_variable(mstate, state, 6256 r1, regs[r2]); 6257 break; 6258 case DIF_OP_LDGS: 6259 id = DIF_INSTR_VAR(instr); 6260 6261 if (id >= DIF_VAR_OTHER_UBASE) { 6262 uintptr_t a; 6263 6264 id -= DIF_VAR_OTHER_UBASE; 6265 svar = vstate->dtvs_globals[id]; 6266 ASSERT(svar != NULL); 6267 v = &svar->dtsv_var; 6268 6269 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 6270 regs[rd] = svar->dtsv_data; 6271 break; 6272 } 6273 6274 a = (uintptr_t)svar->dtsv_data; 6275 6276 if (*(uint8_t *)a == UINT8_MAX) { 6277 /* 6278 * If the 0th byte is set to UINT8_MAX 6279 * then this is to be treated as a 6280 * reference to a NULL variable. 6281 */ 6282 regs[rd] = 0; 6283 } else { 6284 regs[rd] = a + sizeof (uint64_t); 6285 } 6286 6287 break; 6288 } 6289 6290 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 6291 break; 6292 6293 case DIF_OP_STGS: 6294 id = DIF_INSTR_VAR(instr); 6295 6296 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6297 id -= DIF_VAR_OTHER_UBASE; 6298 6299 VERIFY(id < vstate->dtvs_nglobals); 6300 svar = vstate->dtvs_globals[id]; 6301 ASSERT(svar != NULL); 6302 v = &svar->dtsv_var; 6303 6304 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6305 uintptr_t a = (uintptr_t)svar->dtsv_data; 6306 6307 ASSERT(a != 0); 6308 ASSERT(svar->dtsv_size != 0); 6309 6310 if (regs[rd] == 0) { 6311 *(uint8_t *)a = UINT8_MAX; 6312 break; 6313 } else { 6314 *(uint8_t *)a = 0; 6315 a += sizeof (uint64_t); 6316 } 6317 if (!dtrace_vcanload( 6318 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6319 mstate, vstate)) 6320 break; 6321 6322 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6323 (void *)a, &v->dtdv_type); 6324 break; 6325 } 6326 6327 svar->dtsv_data = regs[rd]; 6328 break; 6329 6330 case DIF_OP_LDTA: 6331 /* 6332 * There are no DTrace built-in thread-local arrays at 6333 * present. This opcode is saved for future work. 6334 */ 6335 *flags |= CPU_DTRACE_ILLOP; 6336 regs[rd] = 0; 6337 break; 6338 6339 case DIF_OP_LDLS: 6340 id = DIF_INSTR_VAR(instr); 6341 6342 if (id < DIF_VAR_OTHER_UBASE) { 6343 /* 6344 * For now, this has no meaning. 6345 */ 6346 regs[rd] = 0; 6347 break; 6348 } 6349 6350 id -= DIF_VAR_OTHER_UBASE; 6351 6352 ASSERT(id < vstate->dtvs_nlocals); 6353 ASSERT(vstate->dtvs_locals != NULL); 6354 6355 svar = vstate->dtvs_locals[id]; 6356 ASSERT(svar != NULL); 6357 v = &svar->dtsv_var; 6358 6359 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6360 uintptr_t a = (uintptr_t)svar->dtsv_data; 6361 size_t sz = v->dtdv_type.dtdt_size; 6362 6363 sz += sizeof (uint64_t); 6364 ASSERT(svar->dtsv_size == NCPU * sz); 6365 a += curcpu * sz; 6366 6367 if (*(uint8_t *)a == UINT8_MAX) { 6368 /* 6369 * If the 0th byte is set to UINT8_MAX 6370 * then this is to be treated as a 6371 * reference to a NULL variable. 6372 */ 6373 regs[rd] = 0; 6374 } else { 6375 regs[rd] = a + sizeof (uint64_t); 6376 } 6377 6378 break; 6379 } 6380 6381 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6382 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6383 regs[rd] = tmp[curcpu]; 6384 break; 6385 6386 case DIF_OP_STLS: 6387 id = DIF_INSTR_VAR(instr); 6388 6389 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6390 id -= DIF_VAR_OTHER_UBASE; 6391 VERIFY(id < vstate->dtvs_nlocals); 6392 6393 ASSERT(vstate->dtvs_locals != NULL); 6394 svar = vstate->dtvs_locals[id]; 6395 ASSERT(svar != NULL); 6396 v = &svar->dtsv_var; 6397 6398 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6399 uintptr_t a = (uintptr_t)svar->dtsv_data; 6400 size_t sz = v->dtdv_type.dtdt_size; 6401 6402 sz += sizeof (uint64_t); 6403 ASSERT(svar->dtsv_size == NCPU * sz); 6404 a += curcpu * sz; 6405 6406 if (regs[rd] == 0) { 6407 *(uint8_t *)a = UINT8_MAX; 6408 break; 6409 } else { 6410 *(uint8_t *)a = 0; 6411 a += sizeof (uint64_t); 6412 } 6413 6414 if (!dtrace_vcanload( 6415 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6416 mstate, vstate)) 6417 break; 6418 6419 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6420 (void *)a, &v->dtdv_type); 6421 break; 6422 } 6423 6424 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 6425 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 6426 tmp[curcpu] = regs[rd]; 6427 break; 6428 6429 case DIF_OP_LDTS: { 6430 dtrace_dynvar_t *dvar; 6431 dtrace_key_t *key; 6432 6433 id = DIF_INSTR_VAR(instr); 6434 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6435 id -= DIF_VAR_OTHER_UBASE; 6436 v = &vstate->dtvs_tlocals[id]; 6437 6438 key = &tupregs[DIF_DTR_NREGS]; 6439 key[0].dttk_value = (uint64_t)id; 6440 key[0].dttk_size = 0; 6441 DTRACE_TLS_THRKEY(key[1].dttk_value); 6442 key[1].dttk_size = 0; 6443 6444 dvar = dtrace_dynvar(dstate, 2, key, 6445 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 6446 mstate, vstate); 6447 6448 if (dvar == NULL) { 6449 regs[rd] = 0; 6450 break; 6451 } 6452 6453 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6454 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6455 } else { 6456 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6457 } 6458 6459 break; 6460 } 6461 6462 case DIF_OP_STTS: { 6463 dtrace_dynvar_t *dvar; 6464 dtrace_key_t *key; 6465 6466 id = DIF_INSTR_VAR(instr); 6467 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6468 id -= DIF_VAR_OTHER_UBASE; 6469 VERIFY(id < vstate->dtvs_ntlocals); 6470 6471 key = &tupregs[DIF_DTR_NREGS]; 6472 key[0].dttk_value = (uint64_t)id; 6473 key[0].dttk_size = 0; 6474 DTRACE_TLS_THRKEY(key[1].dttk_value); 6475 key[1].dttk_size = 0; 6476 v = &vstate->dtvs_tlocals[id]; 6477 6478 dvar = dtrace_dynvar(dstate, 2, key, 6479 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6480 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6481 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6482 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6483 6484 /* 6485 * Given that we're storing to thread-local data, 6486 * we need to flush our predicate cache. 6487 */ 6488 curthread->t_predcache = 0; 6489 6490 if (dvar == NULL) 6491 break; 6492 6493 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6494 if (!dtrace_vcanload( 6495 (void *)(uintptr_t)regs[rd], 6496 &v->dtdv_type, mstate, vstate)) 6497 break; 6498 6499 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6500 dvar->dtdv_data, &v->dtdv_type); 6501 } else { 6502 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6503 } 6504 6505 break; 6506 } 6507 6508 case DIF_OP_SRA: 6509 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 6510 break; 6511 6512 case DIF_OP_CALL: 6513 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 6514 regs, tupregs, ttop, mstate, state); 6515 break; 6516 6517 case DIF_OP_PUSHTR: 6518 if (ttop == DIF_DTR_NREGS) { 6519 *flags |= CPU_DTRACE_TUPOFLOW; 6520 break; 6521 } 6522 6523 if (r1 == DIF_TYPE_STRING) { 6524 /* 6525 * If this is a string type and the size is 0, 6526 * we'll use the system-wide default string 6527 * size. Note that we are _not_ looking at 6528 * the value of the DTRACEOPT_STRSIZE option; 6529 * had this been set, we would expect to have 6530 * a non-zero size value in the "pushtr". 6531 */ 6532 tupregs[ttop].dttk_size = 6533 dtrace_strlen((char *)(uintptr_t)regs[rd], 6534 regs[r2] ? regs[r2] : 6535 dtrace_strsize_default) + 1; 6536 } else { 6537 if (regs[r2] > LONG_MAX) { 6538 *flags |= CPU_DTRACE_ILLOP; 6539 break; 6540 } 6541 6542 tupregs[ttop].dttk_size = regs[r2]; 6543 } 6544 6545 tupregs[ttop++].dttk_value = regs[rd]; 6546 break; 6547 6548 case DIF_OP_PUSHTV: 6549 if (ttop == DIF_DTR_NREGS) { 6550 *flags |= CPU_DTRACE_TUPOFLOW; 6551 break; 6552 } 6553 6554 tupregs[ttop].dttk_value = regs[rd]; 6555 tupregs[ttop++].dttk_size = 0; 6556 break; 6557 6558 case DIF_OP_POPTS: 6559 if (ttop != 0) 6560 ttop--; 6561 break; 6562 6563 case DIF_OP_FLUSHTS: 6564 ttop = 0; 6565 break; 6566 6567 case DIF_OP_LDGAA: 6568 case DIF_OP_LDTAA: { 6569 dtrace_dynvar_t *dvar; 6570 dtrace_key_t *key = tupregs; 6571 uint_t nkeys = ttop; 6572 6573 id = DIF_INSTR_VAR(instr); 6574 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6575 id -= DIF_VAR_OTHER_UBASE; 6576 6577 key[nkeys].dttk_value = (uint64_t)id; 6578 key[nkeys++].dttk_size = 0; 6579 6580 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 6581 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6582 key[nkeys++].dttk_size = 0; 6583 VERIFY(id < vstate->dtvs_ntlocals); 6584 v = &vstate->dtvs_tlocals[id]; 6585 } else { 6586 VERIFY(id < vstate->dtvs_nglobals); 6587 v = &vstate->dtvs_globals[id]->dtsv_var; 6588 } 6589 6590 dvar = dtrace_dynvar(dstate, nkeys, key, 6591 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6592 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6593 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 6594 6595 if (dvar == NULL) { 6596 regs[rd] = 0; 6597 break; 6598 } 6599 6600 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6601 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 6602 } else { 6603 regs[rd] = *((uint64_t *)dvar->dtdv_data); 6604 } 6605 6606 break; 6607 } 6608 6609 case DIF_OP_STGAA: 6610 case DIF_OP_STTAA: { 6611 dtrace_dynvar_t *dvar; 6612 dtrace_key_t *key = tupregs; 6613 uint_t nkeys = ttop; 6614 6615 id = DIF_INSTR_VAR(instr); 6616 ASSERT(id >= DIF_VAR_OTHER_UBASE); 6617 id -= DIF_VAR_OTHER_UBASE; 6618 6619 key[nkeys].dttk_value = (uint64_t)id; 6620 key[nkeys++].dttk_size = 0; 6621 6622 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 6623 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 6624 key[nkeys++].dttk_size = 0; 6625 VERIFY(id < vstate->dtvs_ntlocals); 6626 v = &vstate->dtvs_tlocals[id]; 6627 } else { 6628 VERIFY(id < vstate->dtvs_nglobals); 6629 v = &vstate->dtvs_globals[id]->dtsv_var; 6630 } 6631 6632 dvar = dtrace_dynvar(dstate, nkeys, key, 6633 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 6634 v->dtdv_type.dtdt_size : sizeof (uint64_t), 6635 regs[rd] ? DTRACE_DYNVAR_ALLOC : 6636 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 6637 6638 if (dvar == NULL) 6639 break; 6640 6641 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 6642 if (!dtrace_vcanload( 6643 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 6644 mstate, vstate)) 6645 break; 6646 6647 dtrace_vcopy((void *)(uintptr_t)regs[rd], 6648 dvar->dtdv_data, &v->dtdv_type); 6649 } else { 6650 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 6651 } 6652 6653 break; 6654 } 6655 6656 case DIF_OP_ALLOCS: { 6657 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6658 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 6659 6660 /* 6661 * Rounding up the user allocation size could have 6662 * overflowed large, bogus allocations (like -1ULL) to 6663 * 0. 6664 */ 6665 if (size < regs[r1] || 6666 !DTRACE_INSCRATCH(mstate, size)) { 6667 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6668 regs[rd] = 0; 6669 break; 6670 } 6671 6672 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 6673 mstate->dtms_scratch_ptr += size; 6674 regs[rd] = ptr; 6675 break; 6676 } 6677 6678 case DIF_OP_COPYS: 6679 if (!dtrace_canstore(regs[rd], regs[r2], 6680 mstate, vstate)) { 6681 *flags |= CPU_DTRACE_BADADDR; 6682 *illval = regs[rd]; 6683 break; 6684 } 6685 6686 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 6687 break; 6688 6689 dtrace_bcopy((void *)(uintptr_t)regs[r1], 6690 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 6691 break; 6692 6693 case DIF_OP_STB: 6694 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 6695 *flags |= CPU_DTRACE_BADADDR; 6696 *illval = regs[rd]; 6697 break; 6698 } 6699 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 6700 break; 6701 6702 case DIF_OP_STH: 6703 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 6704 *flags |= CPU_DTRACE_BADADDR; 6705 *illval = regs[rd]; 6706 break; 6707 } 6708 if (regs[rd] & 1) { 6709 *flags |= CPU_DTRACE_BADALIGN; 6710 *illval = regs[rd]; 6711 break; 6712 } 6713 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 6714 break; 6715 6716 case DIF_OP_STW: 6717 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 6718 *flags |= CPU_DTRACE_BADADDR; 6719 *illval = regs[rd]; 6720 break; 6721 } 6722 if (regs[rd] & 3) { 6723 *flags |= CPU_DTRACE_BADALIGN; 6724 *illval = regs[rd]; 6725 break; 6726 } 6727 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 6728 break; 6729 6730 case DIF_OP_STX: 6731 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 6732 *flags |= CPU_DTRACE_BADADDR; 6733 *illval = regs[rd]; 6734 break; 6735 } 6736 if (regs[rd] & 7) { 6737 *flags |= CPU_DTRACE_BADALIGN; 6738 *illval = regs[rd]; 6739 break; 6740 } 6741 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 6742 break; 6743 } 6744 } 6745 6746 if (!(*flags & CPU_DTRACE_FAULT)) 6747 return (rval); 6748 6749 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 6750 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 6751 6752 return (0); 6753} 6754 6755static void 6756dtrace_action_breakpoint(dtrace_ecb_t *ecb) 6757{ 6758 dtrace_probe_t *probe = ecb->dte_probe; 6759 dtrace_provider_t *prov = probe->dtpr_provider; 6760 char c[DTRACE_FULLNAMELEN + 80], *str; 6761 char *msg = "dtrace: breakpoint action at probe "; 6762 char *ecbmsg = " (ecb "; 6763 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 6764 uintptr_t val = (uintptr_t)ecb; 6765 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 6766 6767 if (dtrace_destructive_disallow) 6768 return; 6769 6770 /* 6771 * It's impossible to be taking action on the NULL probe. 6772 */ 6773 ASSERT(probe != NULL); 6774 6775 /* 6776 * This is a poor man's (destitute man's?) sprintf(): we want to 6777 * print the provider name, module name, function name and name of 6778 * the probe, along with the hex address of the ECB with the breakpoint 6779 * action -- all of which we must place in the character buffer by 6780 * hand. 6781 */ 6782 while (*msg != '\0') 6783 c[i++] = *msg++; 6784 6785 for (str = prov->dtpv_name; *str != '\0'; str++) 6786 c[i++] = *str; 6787 c[i++] = ':'; 6788 6789 for (str = probe->dtpr_mod; *str != '\0'; str++) 6790 c[i++] = *str; 6791 c[i++] = ':'; 6792 6793 for (str = probe->dtpr_func; *str != '\0'; str++) 6794 c[i++] = *str; 6795 c[i++] = ':'; 6796 6797 for (str = probe->dtpr_name; *str != '\0'; str++) 6798 c[i++] = *str; 6799 6800 while (*ecbmsg != '\0') 6801 c[i++] = *ecbmsg++; 6802 6803 while (shift >= 0) { 6804 mask = (uintptr_t)0xf << shift; 6805 6806 if (val >= ((uintptr_t)1 << shift)) 6807 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 6808 shift -= 4; 6809 } 6810 6811 c[i++] = ')'; 6812 c[i] = '\0'; 6813 6814#ifdef illumos 6815 debug_enter(c); 6816#else 6817 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 6818#endif 6819} 6820 6821static void 6822dtrace_action_panic(dtrace_ecb_t *ecb) 6823{ 6824 dtrace_probe_t *probe = ecb->dte_probe; 6825 6826 /* 6827 * It's impossible to be taking action on the NULL probe. 6828 */ 6829 ASSERT(probe != NULL); 6830 6831 if (dtrace_destructive_disallow) 6832 return; 6833 6834 if (dtrace_panicked != NULL) 6835 return; 6836 6837 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 6838 return; 6839 6840 /* 6841 * We won the right to panic. (We want to be sure that only one 6842 * thread calls panic() from dtrace_probe(), and that panic() is 6843 * called exactly once.) 6844 */ 6845 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 6846 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 6847 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 6848} 6849 6850static void 6851dtrace_action_raise(uint64_t sig) 6852{ 6853 if (dtrace_destructive_disallow) 6854 return; 6855 6856 if (sig >= NSIG) { 6857 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 6858 return; 6859 } 6860 6861#ifdef illumos 6862 /* 6863 * raise() has a queue depth of 1 -- we ignore all subsequent 6864 * invocations of the raise() action. 6865 */ 6866 if (curthread->t_dtrace_sig == 0) 6867 curthread->t_dtrace_sig = (uint8_t)sig; 6868 6869 curthread->t_sig_check = 1; 6870 aston(curthread); 6871#else 6872 struct proc *p = curproc; 6873 PROC_LOCK(p); 6874 kern_psignal(p, sig); 6875 PROC_UNLOCK(p); 6876#endif 6877} 6878 6879static void 6880dtrace_action_stop(void) 6881{ 6882 if (dtrace_destructive_disallow) 6883 return; 6884 6885#ifdef illumos 6886 if (!curthread->t_dtrace_stop) { 6887 curthread->t_dtrace_stop = 1; 6888 curthread->t_sig_check = 1; 6889 aston(curthread); 6890 } 6891#else 6892 struct proc *p = curproc; 6893 PROC_LOCK(p); 6894 kern_psignal(p, SIGSTOP); 6895 PROC_UNLOCK(p); 6896#endif 6897} 6898 6899static void 6900dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 6901{ 6902 hrtime_t now; 6903 volatile uint16_t *flags; 6904#ifdef illumos 6905 cpu_t *cpu = CPU; 6906#else 6907 cpu_t *cpu = &solaris_cpu[curcpu]; 6908#endif 6909 6910 if (dtrace_destructive_disallow) 6911 return; 6912 6913 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 6914 6915 now = dtrace_gethrtime(); 6916 6917 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 6918 /* 6919 * We need to advance the mark to the current time. 6920 */ 6921 cpu->cpu_dtrace_chillmark = now; 6922 cpu->cpu_dtrace_chilled = 0; 6923 } 6924 6925 /* 6926 * Now check to see if the requested chill time would take us over 6927 * the maximum amount of time allowed in the chill interval. (Or 6928 * worse, if the calculation itself induces overflow.) 6929 */ 6930 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 6931 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 6932 *flags |= CPU_DTRACE_ILLOP; 6933 return; 6934 } 6935 6936 while (dtrace_gethrtime() - now < val) 6937 continue; 6938 6939 /* 6940 * Normally, we assure that the value of the variable "timestamp" does 6941 * not change within an ECB. The presence of chill() represents an 6942 * exception to this rule, however. 6943 */ 6944 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 6945 cpu->cpu_dtrace_chilled += val; 6946} 6947 6948static void 6949dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 6950 uint64_t *buf, uint64_t arg) 6951{ 6952 int nframes = DTRACE_USTACK_NFRAMES(arg); 6953 int strsize = DTRACE_USTACK_STRSIZE(arg); 6954 uint64_t *pcs = &buf[1], *fps; 6955 char *str = (char *)&pcs[nframes]; 6956 int size, offs = 0, i, j; 6957 uintptr_t old = mstate->dtms_scratch_ptr, saved; 6958 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 6959 char *sym; 6960 6961 /* 6962 * Should be taking a faster path if string space has not been 6963 * allocated. 6964 */ 6965 ASSERT(strsize != 0); 6966 6967 /* 6968 * We will first allocate some temporary space for the frame pointers. 6969 */ 6970 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 6971 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 6972 (nframes * sizeof (uint64_t)); 6973 6974 if (!DTRACE_INSCRATCH(mstate, size)) { 6975 /* 6976 * Not enough room for our frame pointers -- need to indicate 6977 * that we ran out of scratch space. 6978 */ 6979 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 6980 return; 6981 } 6982 6983 mstate->dtms_scratch_ptr += size; 6984 saved = mstate->dtms_scratch_ptr; 6985 6986 /* 6987 * Now get a stack with both program counters and frame pointers. 6988 */ 6989 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6990 dtrace_getufpstack(buf, fps, nframes + 1); 6991 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6992 6993 /* 6994 * If that faulted, we're cooked. 6995 */ 6996 if (*flags & CPU_DTRACE_FAULT) 6997 goto out; 6998 6999 /* 7000 * Now we want to walk up the stack, calling the USTACK helper. For 7001 * each iteration, we restore the scratch pointer. 7002 */ 7003 for (i = 0; i < nframes; i++) { 7004 mstate->dtms_scratch_ptr = saved; 7005 7006 if (offs >= strsize) 7007 break; 7008 7009 sym = (char *)(uintptr_t)dtrace_helper( 7010 DTRACE_HELPER_ACTION_USTACK, 7011 mstate, state, pcs[i], fps[i]); 7012 7013 /* 7014 * If we faulted while running the helper, we're going to 7015 * clear the fault and null out the corresponding string. 7016 */ 7017 if (*flags & CPU_DTRACE_FAULT) { 7018 *flags &= ~CPU_DTRACE_FAULT; 7019 str[offs++] = '\0'; 7020 continue; 7021 } 7022 7023 if (sym == NULL) { 7024 str[offs++] = '\0'; 7025 continue; 7026 } 7027 7028 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7029 7030 /* 7031 * Now copy in the string that the helper returned to us. 7032 */ 7033 for (j = 0; offs + j < strsize; j++) { 7034 if ((str[offs + j] = sym[j]) == '\0') 7035 break; 7036 } 7037 7038 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7039 7040 offs += j + 1; 7041 } 7042 7043 if (offs >= strsize) { 7044 /* 7045 * If we didn't have room for all of the strings, we don't 7046 * abort processing -- this needn't be a fatal error -- but we 7047 * still want to increment a counter (dts_stkstroverflows) to 7048 * allow this condition to be warned about. (If this is from 7049 * a jstack() action, it is easily tuned via jstackstrsize.) 7050 */ 7051 dtrace_error(&state->dts_stkstroverflows); 7052 } 7053 7054 while (offs < strsize) 7055 str[offs++] = '\0'; 7056 7057out: 7058 mstate->dtms_scratch_ptr = old; 7059} 7060 7061static void 7062dtrace_store_by_ref(dtrace_difo_t *dp, caddr_t tomax, size_t size, 7063 size_t *valoffsp, uint64_t *valp, uint64_t end, int intuple, int dtkind) 7064{ 7065 volatile uint16_t *flags; 7066 uint64_t val = *valp; 7067 size_t valoffs = *valoffsp; 7068 7069 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 7070 ASSERT(dtkind == DIF_TF_BYREF || dtkind == DIF_TF_BYUREF); 7071 7072 /* 7073 * If this is a string, we're going to only load until we find the zero 7074 * byte -- after which we'll store zero bytes. 7075 */ 7076 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 7077 char c = '\0' + 1; 7078 size_t s; 7079 7080 for (s = 0; s < size; s++) { 7081 if (c != '\0' && dtkind == DIF_TF_BYREF) { 7082 c = dtrace_load8(val++); 7083 } else if (c != '\0' && dtkind == DIF_TF_BYUREF) { 7084 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7085 c = dtrace_fuword8((void *)(uintptr_t)val++); 7086 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7087 if (*flags & CPU_DTRACE_FAULT) 7088 break; 7089 } 7090 7091 DTRACE_STORE(uint8_t, tomax, valoffs++, c); 7092 7093 if (c == '\0' && intuple) 7094 break; 7095 } 7096 } else { 7097 uint8_t c; 7098 while (valoffs < end) { 7099 if (dtkind == DIF_TF_BYREF) { 7100 c = dtrace_load8(val++); 7101 } else if (dtkind == DIF_TF_BYUREF) { 7102 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7103 c = dtrace_fuword8((void *)(uintptr_t)val++); 7104 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7105 if (*flags & CPU_DTRACE_FAULT) 7106 break; 7107 } 7108 7109 DTRACE_STORE(uint8_t, tomax, 7110 valoffs++, c); 7111 } 7112 } 7113 7114 *valp = val; 7115 *valoffsp = valoffs; 7116} 7117 7118/* 7119 * Disables interrupts and sets the per-thread inprobe flag. When DEBUG is 7120 * defined, we also assert that we are not recursing unless the probe ID is an 7121 * error probe. 7122 */ 7123static dtrace_icookie_t 7124dtrace_probe_enter(dtrace_id_t id) 7125{ 7126 dtrace_icookie_t cookie; 7127 7128 cookie = dtrace_interrupt_disable(); 7129 7130 /* 7131 * Unless this is an ERROR probe, we are not allowed to recurse in 7132 * dtrace_probe(). Recursing into DTrace probe usually means that a 7133 * function is instrumented that should not have been instrumented or 7134 * that the ordering guarantee of the records will be violated, 7135 * resulting in unexpected output. If there is an exception to this 7136 * assertion, a new case should be added. 7137 */ 7138 ASSERT(curthread->t_dtrace_inprobe == 0 || 7139 id == dtrace_probeid_error); 7140 curthread->t_dtrace_inprobe = 1; 7141 7142 return (cookie); 7143} 7144 7145/* 7146 * Disables interrupts and clears the per-thread inprobe flag. 7147 */ 7148static void 7149dtrace_probe_exit(dtrace_icookie_t cookie) 7150{ 7151 7152 curthread->t_dtrace_inprobe = 0; 7153 dtrace_interrupt_enable(cookie); 7154} 7155 7156/* 7157 * If you're looking for the epicenter of DTrace, you just found it. This 7158 * is the function called by the provider to fire a probe -- from which all 7159 * subsequent probe-context DTrace activity emanates. 7160 */ 7161void 7162dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 7163 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 7164{ 7165 processorid_t cpuid; 7166 dtrace_icookie_t cookie; 7167 dtrace_probe_t *probe; 7168 dtrace_mstate_t mstate; 7169 dtrace_ecb_t *ecb; 7170 dtrace_action_t *act; 7171 intptr_t offs; 7172 size_t size; 7173 int vtime, onintr; 7174 volatile uint16_t *flags; 7175 hrtime_t now; 7176 7177 if (panicstr != NULL) 7178 return; 7179 7180#ifdef illumos 7181 /* 7182 * Kick out immediately if this CPU is still being born (in which case 7183 * curthread will be set to -1) or the current thread can't allow 7184 * probes in its current context. 7185 */ 7186 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 7187 return; 7188#endif 7189 7190 cookie = dtrace_probe_enter(id); 7191 probe = dtrace_probes[id - 1]; 7192 cpuid = curcpu; 7193 onintr = CPU_ON_INTR(CPU); 7194 7195 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 7196 probe->dtpr_predcache == curthread->t_predcache) { 7197 /* 7198 * We have hit in the predicate cache; we know that 7199 * this predicate would evaluate to be false. 7200 */ 7201 dtrace_probe_exit(cookie); 7202 return; 7203 } 7204 7205#ifdef illumos 7206 if (panic_quiesce) { 7207#else 7208 if (panicstr != NULL) { 7209#endif 7210 /* 7211 * We don't trace anything if we're panicking. 7212 */ 7213 dtrace_probe_exit(cookie); 7214 return; 7215 } 7216 7217 now = mstate.dtms_timestamp = dtrace_gethrtime(); 7218 mstate.dtms_present = DTRACE_MSTATE_TIMESTAMP; 7219 vtime = dtrace_vtime_references != 0; 7220 7221 if (vtime && curthread->t_dtrace_start) 7222 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 7223 7224 mstate.dtms_difo = NULL; 7225 mstate.dtms_probe = probe; 7226 mstate.dtms_strtok = 0; 7227 mstate.dtms_arg[0] = arg0; 7228 mstate.dtms_arg[1] = arg1; 7229 mstate.dtms_arg[2] = arg2; 7230 mstate.dtms_arg[3] = arg3; 7231 mstate.dtms_arg[4] = arg4; 7232 7233 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 7234 7235 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 7236 dtrace_predicate_t *pred = ecb->dte_predicate; 7237 dtrace_state_t *state = ecb->dte_state; 7238 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 7239 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 7240 dtrace_vstate_t *vstate = &state->dts_vstate; 7241 dtrace_provider_t *prov = probe->dtpr_provider; 7242 uint64_t tracememsize = 0; 7243 int committed = 0; 7244 caddr_t tomax; 7245 7246 /* 7247 * A little subtlety with the following (seemingly innocuous) 7248 * declaration of the automatic 'val': by looking at the 7249 * code, you might think that it could be declared in the 7250 * action processing loop, below. (That is, it's only used in 7251 * the action processing loop.) However, it must be declared 7252 * out of that scope because in the case of DIF expression 7253 * arguments to aggregating actions, one iteration of the 7254 * action loop will use the last iteration's value. 7255 */ 7256 uint64_t val = 0; 7257 7258 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 7259 mstate.dtms_getf = NULL; 7260 7261 *flags &= ~CPU_DTRACE_ERROR; 7262 7263 if (prov == dtrace_provider) { 7264 /* 7265 * If dtrace itself is the provider of this probe, 7266 * we're only going to continue processing the ECB if 7267 * arg0 (the dtrace_state_t) is equal to the ECB's 7268 * creating state. (This prevents disjoint consumers 7269 * from seeing one another's metaprobes.) 7270 */ 7271 if (arg0 != (uint64_t)(uintptr_t)state) 7272 continue; 7273 } 7274 7275 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 7276 /* 7277 * We're not currently active. If our provider isn't 7278 * the dtrace pseudo provider, we're not interested. 7279 */ 7280 if (prov != dtrace_provider) 7281 continue; 7282 7283 /* 7284 * Now we must further check if we are in the BEGIN 7285 * probe. If we are, we will only continue processing 7286 * if we're still in WARMUP -- if one BEGIN enabling 7287 * has invoked the exit() action, we don't want to 7288 * evaluate subsequent BEGIN enablings. 7289 */ 7290 if (probe->dtpr_id == dtrace_probeid_begin && 7291 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 7292 ASSERT(state->dts_activity == 7293 DTRACE_ACTIVITY_DRAINING); 7294 continue; 7295 } 7296 } 7297 7298 if (ecb->dte_cond) { 7299 /* 7300 * If the dte_cond bits indicate that this 7301 * consumer is only allowed to see user-mode firings 7302 * of this probe, call the provider's dtps_usermode() 7303 * entry point to check that the probe was fired 7304 * while in a user context. Skip this ECB if that's 7305 * not the case. 7306 */ 7307 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 7308 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 7309 probe->dtpr_id, probe->dtpr_arg) == 0) 7310 continue; 7311 7312#ifdef illumos 7313 /* 7314 * This is more subtle than it looks. We have to be 7315 * absolutely certain that CRED() isn't going to 7316 * change out from under us so it's only legit to 7317 * examine that structure if we're in constrained 7318 * situations. Currently, the only times we'll this 7319 * check is if a non-super-user has enabled the 7320 * profile or syscall providers -- providers that 7321 * allow visibility of all processes. For the 7322 * profile case, the check above will ensure that 7323 * we're examining a user context. 7324 */ 7325 if (ecb->dte_cond & DTRACE_COND_OWNER) { 7326 cred_t *cr; 7327 cred_t *s_cr = 7328 ecb->dte_state->dts_cred.dcr_cred; 7329 proc_t *proc; 7330 7331 ASSERT(s_cr != NULL); 7332 7333 if ((cr = CRED()) == NULL || 7334 s_cr->cr_uid != cr->cr_uid || 7335 s_cr->cr_uid != cr->cr_ruid || 7336 s_cr->cr_uid != cr->cr_suid || 7337 s_cr->cr_gid != cr->cr_gid || 7338 s_cr->cr_gid != cr->cr_rgid || 7339 s_cr->cr_gid != cr->cr_sgid || 7340 (proc = ttoproc(curthread)) == NULL || 7341 (proc->p_flag & SNOCD)) 7342 continue; 7343 } 7344 7345 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 7346 cred_t *cr; 7347 cred_t *s_cr = 7348 ecb->dte_state->dts_cred.dcr_cred; 7349 7350 ASSERT(s_cr != NULL); 7351 7352 if ((cr = CRED()) == NULL || 7353 s_cr->cr_zone->zone_id != 7354 cr->cr_zone->zone_id) 7355 continue; 7356 } 7357#endif 7358 } 7359 7360 if (now - state->dts_alive > dtrace_deadman_timeout) { 7361 /* 7362 * We seem to be dead. Unless we (a) have kernel 7363 * destructive permissions (b) have explicitly enabled 7364 * destructive actions and (c) destructive actions have 7365 * not been disabled, we're going to transition into 7366 * the KILLED state, from which no further processing 7367 * on this state will be performed. 7368 */ 7369 if (!dtrace_priv_kernel_destructive(state) || 7370 !state->dts_cred.dcr_destructive || 7371 dtrace_destructive_disallow) { 7372 void *activity = &state->dts_activity; 7373 dtrace_activity_t current; 7374 7375 do { 7376 current = state->dts_activity; 7377 } while (dtrace_cas32(activity, current, 7378 DTRACE_ACTIVITY_KILLED) != current); 7379 7380 continue; 7381 } 7382 } 7383 7384 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 7385 ecb->dte_alignment, state, &mstate)) < 0) 7386 continue; 7387 7388 tomax = buf->dtb_tomax; 7389 ASSERT(tomax != NULL); 7390 7391 if (ecb->dte_size != 0) { 7392 dtrace_rechdr_t dtrh; 7393 if (!(mstate.dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 7394 mstate.dtms_timestamp = dtrace_gethrtime(); 7395 mstate.dtms_present |= DTRACE_MSTATE_TIMESTAMP; 7396 } 7397 ASSERT3U(ecb->dte_size, >=, sizeof (dtrace_rechdr_t)); 7398 dtrh.dtrh_epid = ecb->dte_epid; 7399 DTRACE_RECORD_STORE_TIMESTAMP(&dtrh, 7400 mstate.dtms_timestamp); 7401 *((dtrace_rechdr_t *)(tomax + offs)) = dtrh; 7402 } 7403 7404 mstate.dtms_epid = ecb->dte_epid; 7405 mstate.dtms_present |= DTRACE_MSTATE_EPID; 7406 7407 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 7408 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 7409 else 7410 mstate.dtms_access = 0; 7411 7412 if (pred != NULL) { 7413 dtrace_difo_t *dp = pred->dtp_difo; 7414 uint64_t rval; 7415 7416 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 7417 7418 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 7419 dtrace_cacheid_t cid = probe->dtpr_predcache; 7420 7421 if (cid != DTRACE_CACHEIDNONE && !onintr) { 7422 /* 7423 * Update the predicate cache... 7424 */ 7425 ASSERT(cid == pred->dtp_cacheid); 7426 curthread->t_predcache = cid; 7427 } 7428 7429 continue; 7430 } 7431 } 7432 7433 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 7434 act != NULL; act = act->dta_next) { 7435 size_t valoffs; 7436 dtrace_difo_t *dp; 7437 dtrace_recdesc_t *rec = &act->dta_rec; 7438 7439 size = rec->dtrd_size; 7440 valoffs = offs + rec->dtrd_offset; 7441 7442 if (DTRACEACT_ISAGG(act->dta_kind)) { 7443 uint64_t v = 0xbad; 7444 dtrace_aggregation_t *agg; 7445 7446 agg = (dtrace_aggregation_t *)act; 7447 7448 if ((dp = act->dta_difo) != NULL) 7449 v = dtrace_dif_emulate(dp, 7450 &mstate, vstate, state); 7451 7452 if (*flags & CPU_DTRACE_ERROR) 7453 continue; 7454 7455 /* 7456 * Note that we always pass the expression 7457 * value from the previous iteration of the 7458 * action loop. This value will only be used 7459 * if there is an expression argument to the 7460 * aggregating action, denoted by the 7461 * dtag_hasarg field. 7462 */ 7463 dtrace_aggregate(agg, buf, 7464 offs, aggbuf, v, val); 7465 continue; 7466 } 7467 7468 switch (act->dta_kind) { 7469 case DTRACEACT_STOP: 7470 if (dtrace_priv_proc_destructive(state)) 7471 dtrace_action_stop(); 7472 continue; 7473 7474 case DTRACEACT_BREAKPOINT: 7475 if (dtrace_priv_kernel_destructive(state)) 7476 dtrace_action_breakpoint(ecb); 7477 continue; 7478 7479 case DTRACEACT_PANIC: 7480 if (dtrace_priv_kernel_destructive(state)) 7481 dtrace_action_panic(ecb); 7482 continue; 7483 7484 case DTRACEACT_STACK: 7485 if (!dtrace_priv_kernel(state)) 7486 continue; 7487 7488 dtrace_getpcstack((pc_t *)(tomax + valoffs), 7489 size / sizeof (pc_t), probe->dtpr_aframes, 7490 DTRACE_ANCHORED(probe) ? NULL : 7491 (uint32_t *)arg0); 7492 continue; 7493 7494 case DTRACEACT_JSTACK: 7495 case DTRACEACT_USTACK: 7496 if (!dtrace_priv_proc(state)) 7497 continue; 7498 7499 /* 7500 * See comment in DIF_VAR_PID. 7501 */ 7502 if (DTRACE_ANCHORED(mstate.dtms_probe) && 7503 CPU_ON_INTR(CPU)) { 7504 int depth = DTRACE_USTACK_NFRAMES( 7505 rec->dtrd_arg) + 1; 7506 7507 dtrace_bzero((void *)(tomax + valoffs), 7508 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 7509 + depth * sizeof (uint64_t)); 7510 7511 continue; 7512 } 7513 7514 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 7515 curproc->p_dtrace_helpers != NULL) { 7516 /* 7517 * This is the slow path -- we have 7518 * allocated string space, and we're 7519 * getting the stack of a process that 7520 * has helpers. Call into a separate 7521 * routine to perform this processing. 7522 */ 7523 dtrace_action_ustack(&mstate, state, 7524 (uint64_t *)(tomax + valoffs), 7525 rec->dtrd_arg); 7526 continue; 7527 } 7528 7529 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 7530 dtrace_getupcstack((uint64_t *) 7531 (tomax + valoffs), 7532 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 7533 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 7534 continue; 7535 7536 default: 7537 break; 7538 } 7539 7540 dp = act->dta_difo; 7541 ASSERT(dp != NULL); 7542 7543 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 7544 7545 if (*flags & CPU_DTRACE_ERROR) 7546 continue; 7547 7548 switch (act->dta_kind) { 7549 case DTRACEACT_SPECULATE: { 7550 dtrace_rechdr_t *dtrh; 7551 7552 ASSERT(buf == &state->dts_buffer[cpuid]); 7553 buf = dtrace_speculation_buffer(state, 7554 cpuid, val); 7555 7556 if (buf == NULL) { 7557 *flags |= CPU_DTRACE_DROP; 7558 continue; 7559 } 7560 7561 offs = dtrace_buffer_reserve(buf, 7562 ecb->dte_needed, ecb->dte_alignment, 7563 state, NULL); 7564 7565 if (offs < 0) { 7566 *flags |= CPU_DTRACE_DROP; 7567 continue; 7568 } 7569 7570 tomax = buf->dtb_tomax; 7571 ASSERT(tomax != NULL); 7572 7573 if (ecb->dte_size == 0) 7574 continue; 7575 7576 ASSERT3U(ecb->dte_size, >=, 7577 sizeof (dtrace_rechdr_t)); 7578 dtrh = ((void *)(tomax + offs)); 7579 dtrh->dtrh_epid = ecb->dte_epid; 7580 /* 7581 * When the speculation is committed, all of 7582 * the records in the speculative buffer will 7583 * have their timestamps set to the commit 7584 * time. Until then, it is set to a sentinel 7585 * value, for debugability. 7586 */ 7587 DTRACE_RECORD_STORE_TIMESTAMP(dtrh, UINT64_MAX); 7588 continue; 7589 } 7590 7591 case DTRACEACT_PRINTM: { 7592 /* The DIF returns a 'memref'. */ 7593 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 7594 7595 /* Get the size from the memref. */ 7596 size = memref[1]; 7597 7598 /* 7599 * Check if the size exceeds the allocated 7600 * buffer size. 7601 */ 7602 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7603 /* Flag a drop! */ 7604 *flags |= CPU_DTRACE_DROP; 7605 continue; 7606 } 7607 7608 /* Store the size in the buffer first. */ 7609 DTRACE_STORE(uintptr_t, tomax, 7610 valoffs, size); 7611 7612 /* 7613 * Offset the buffer address to the start 7614 * of the data. 7615 */ 7616 valoffs += sizeof(uintptr_t); 7617 7618 /* 7619 * Reset to the memory address rather than 7620 * the memref array, then let the BYREF 7621 * code below do the work to store the 7622 * memory data in the buffer. 7623 */ 7624 val = memref[0]; 7625 break; 7626 } 7627 7628 case DTRACEACT_PRINTT: { 7629 /* The DIF returns a 'typeref'. */ 7630 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 7631 char c = '\0' + 1; 7632 size_t s; 7633 7634 /* 7635 * Get the type string length and round it 7636 * up so that the data that follows is 7637 * aligned for easy access. 7638 */ 7639 size_t typs = strlen((char *) typeref[2]) + 1; 7640 typs = roundup(typs, sizeof(uintptr_t)); 7641 7642 /* 7643 *Get the size from the typeref using the 7644 * number of elements and the type size. 7645 */ 7646 size = typeref[1] * typeref[3]; 7647 7648 /* 7649 * Check if the size exceeds the allocated 7650 * buffer size. 7651 */ 7652 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 7653 /* Flag a drop! */ 7654 *flags |= CPU_DTRACE_DROP; 7655 7656 } 7657 7658 /* Store the size in the buffer first. */ 7659 DTRACE_STORE(uintptr_t, tomax, 7660 valoffs, size); 7661 valoffs += sizeof(uintptr_t); 7662 7663 /* Store the type size in the buffer. */ 7664 DTRACE_STORE(uintptr_t, tomax, 7665 valoffs, typeref[3]); 7666 valoffs += sizeof(uintptr_t); 7667 7668 val = typeref[2]; 7669 7670 for (s = 0; s < typs; s++) { 7671 if (c != '\0') 7672 c = dtrace_load8(val++); 7673 7674 DTRACE_STORE(uint8_t, tomax, 7675 valoffs++, c); 7676 } 7677 7678 /* 7679 * Reset to the memory address rather than 7680 * the typeref array, then let the BYREF 7681 * code below do the work to store the 7682 * memory data in the buffer. 7683 */ 7684 val = typeref[0]; 7685 break; 7686 } 7687 7688 case DTRACEACT_CHILL: 7689 if (dtrace_priv_kernel_destructive(state)) 7690 dtrace_action_chill(&mstate, val); 7691 continue; 7692 7693 case DTRACEACT_RAISE: 7694 if (dtrace_priv_proc_destructive(state)) 7695 dtrace_action_raise(val); 7696 continue; 7697 7698 case DTRACEACT_COMMIT: 7699 ASSERT(!committed); 7700 7701 /* 7702 * We need to commit our buffer state. 7703 */ 7704 if (ecb->dte_size) 7705 buf->dtb_offset = offs + ecb->dte_size; 7706 buf = &state->dts_buffer[cpuid]; 7707 dtrace_speculation_commit(state, cpuid, val); 7708 committed = 1; 7709 continue; 7710 7711 case DTRACEACT_DISCARD: 7712 dtrace_speculation_discard(state, cpuid, val); 7713 continue; 7714 7715 case DTRACEACT_DIFEXPR: 7716 case DTRACEACT_LIBACT: 7717 case DTRACEACT_PRINTF: 7718 case DTRACEACT_PRINTA: 7719 case DTRACEACT_SYSTEM: 7720 case DTRACEACT_FREOPEN: 7721 case DTRACEACT_TRACEMEM: 7722 break; 7723 7724 case DTRACEACT_TRACEMEM_DYNSIZE: 7725 tracememsize = val; 7726 break; 7727 7728 case DTRACEACT_SYM: 7729 case DTRACEACT_MOD: 7730 if (!dtrace_priv_kernel(state)) 7731 continue; 7732 break; 7733 7734 case DTRACEACT_USYM: 7735 case DTRACEACT_UMOD: 7736 case DTRACEACT_UADDR: { 7737#ifdef illumos 7738 struct pid *pid = curthread->t_procp->p_pidp; 7739#endif 7740 7741 if (!dtrace_priv_proc(state)) 7742 continue; 7743 7744 DTRACE_STORE(uint64_t, tomax, 7745#ifdef illumos 7746 valoffs, (uint64_t)pid->pid_id); 7747#else 7748 valoffs, (uint64_t) curproc->p_pid); 7749#endif 7750 DTRACE_STORE(uint64_t, tomax, 7751 valoffs + sizeof (uint64_t), val); 7752 7753 continue; 7754 } 7755 7756 case DTRACEACT_EXIT: { 7757 /* 7758 * For the exit action, we are going to attempt 7759 * to atomically set our activity to be 7760 * draining. If this fails (either because 7761 * another CPU has beat us to the exit action, 7762 * or because our current activity is something 7763 * other than ACTIVE or WARMUP), we will 7764 * continue. This assures that the exit action 7765 * can be successfully recorded at most once 7766 * when we're in the ACTIVE state. If we're 7767 * encountering the exit() action while in 7768 * COOLDOWN, however, we want to honor the new 7769 * status code. (We know that we're the only 7770 * thread in COOLDOWN, so there is no race.) 7771 */ 7772 void *activity = &state->dts_activity; 7773 dtrace_activity_t current = state->dts_activity; 7774 7775 if (current == DTRACE_ACTIVITY_COOLDOWN) 7776 break; 7777 7778 if (current != DTRACE_ACTIVITY_WARMUP) 7779 current = DTRACE_ACTIVITY_ACTIVE; 7780 7781 if (dtrace_cas32(activity, current, 7782 DTRACE_ACTIVITY_DRAINING) != current) { 7783 *flags |= CPU_DTRACE_DROP; 7784 continue; 7785 } 7786 7787 break; 7788 } 7789 7790 default: 7791 ASSERT(0); 7792 } 7793 7794 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF || 7795 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYUREF) { 7796 uintptr_t end = valoffs + size; 7797 7798 if (tracememsize != 0 && 7799 valoffs + tracememsize < end) { 7800 end = valoffs + tracememsize; 7801 tracememsize = 0; 7802 } 7803 7804 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF && 7805 !dtrace_vcanload((void *)(uintptr_t)val, 7806 &dp->dtdo_rtype, &mstate, vstate)) 7807 continue; 7808 7809 dtrace_store_by_ref(dp, tomax, size, &valoffs, 7810 &val, end, act->dta_intuple, 7811 dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF ? 7812 DIF_TF_BYREF: DIF_TF_BYUREF); 7813 continue; 7814 } 7815 7816 switch (size) { 7817 case 0: 7818 break; 7819 7820 case sizeof (uint8_t): 7821 DTRACE_STORE(uint8_t, tomax, valoffs, val); 7822 break; 7823 case sizeof (uint16_t): 7824 DTRACE_STORE(uint16_t, tomax, valoffs, val); 7825 break; 7826 case sizeof (uint32_t): 7827 DTRACE_STORE(uint32_t, tomax, valoffs, val); 7828 break; 7829 case sizeof (uint64_t): 7830 DTRACE_STORE(uint64_t, tomax, valoffs, val); 7831 break; 7832 default: 7833 /* 7834 * Any other size should have been returned by 7835 * reference, not by value. 7836 */ 7837 ASSERT(0); 7838 break; 7839 } 7840 } 7841 7842 if (*flags & CPU_DTRACE_DROP) 7843 continue; 7844 7845 if (*flags & CPU_DTRACE_FAULT) { 7846 int ndx; 7847 dtrace_action_t *err; 7848 7849 buf->dtb_errors++; 7850 7851 if (probe->dtpr_id == dtrace_probeid_error) { 7852 /* 7853 * There's nothing we can do -- we had an 7854 * error on the error probe. We bump an 7855 * error counter to at least indicate that 7856 * this condition happened. 7857 */ 7858 dtrace_error(&state->dts_dblerrors); 7859 continue; 7860 } 7861 7862 if (vtime) { 7863 /* 7864 * Before recursing on dtrace_probe(), we 7865 * need to explicitly clear out our start 7866 * time to prevent it from being accumulated 7867 * into t_dtrace_vtime. 7868 */ 7869 curthread->t_dtrace_start = 0; 7870 } 7871 7872 /* 7873 * Iterate over the actions to figure out which action 7874 * we were processing when we experienced the error. 7875 * Note that act points _past_ the faulting action; if 7876 * act is ecb->dte_action, the fault was in the 7877 * predicate, if it's ecb->dte_action->dta_next it's 7878 * in action #1, and so on. 7879 */ 7880 for (err = ecb->dte_action, ndx = 0; 7881 err != act; err = err->dta_next, ndx++) 7882 continue; 7883 7884 dtrace_probe_error(state, ecb->dte_epid, ndx, 7885 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 7886 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 7887 cpu_core[cpuid].cpuc_dtrace_illval); 7888 7889 continue; 7890 } 7891 7892 if (!committed) 7893 buf->dtb_offset = offs + ecb->dte_size; 7894 } 7895 7896 if (vtime) 7897 curthread->t_dtrace_start = dtrace_gethrtime(); 7898 7899 dtrace_probe_exit(cookie); 7900} 7901 7902/* 7903 * DTrace Probe Hashing Functions 7904 * 7905 * The functions in this section (and indeed, the functions in remaining 7906 * sections) are not _called_ from probe context. (Any exceptions to this are 7907 * marked with a "Note:".) Rather, they are called from elsewhere in the 7908 * DTrace framework to look-up probes in, add probes to and remove probes from 7909 * the DTrace probe hashes. (Each probe is hashed by each element of the 7910 * probe tuple -- allowing for fast lookups, regardless of what was 7911 * specified.) 7912 */ 7913static uint_t 7914dtrace_hash_str(const char *p) 7915{ 7916 unsigned int g; 7917 uint_t hval = 0; 7918 7919 while (*p) { 7920 hval = (hval << 4) + *p++; 7921 if ((g = (hval & 0xf0000000)) != 0) 7922 hval ^= g >> 24; 7923 hval &= ~g; 7924 } 7925 return (hval); 7926} 7927 7928static dtrace_hash_t * 7929dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 7930{ 7931 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 7932 7933 hash->dth_stroffs = stroffs; 7934 hash->dth_nextoffs = nextoffs; 7935 hash->dth_prevoffs = prevoffs; 7936 7937 hash->dth_size = 1; 7938 hash->dth_mask = hash->dth_size - 1; 7939 7940 hash->dth_tab = kmem_zalloc(hash->dth_size * 7941 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 7942 7943 return (hash); 7944} 7945 7946static void 7947dtrace_hash_destroy(dtrace_hash_t *hash) 7948{ 7949#ifdef DEBUG 7950 int i; 7951 7952 for (i = 0; i < hash->dth_size; i++) 7953 ASSERT(hash->dth_tab[i] == NULL); 7954#endif 7955 7956 kmem_free(hash->dth_tab, 7957 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 7958 kmem_free(hash, sizeof (dtrace_hash_t)); 7959} 7960 7961static void 7962dtrace_hash_resize(dtrace_hash_t *hash) 7963{ 7964 int size = hash->dth_size, i, ndx; 7965 int new_size = hash->dth_size << 1; 7966 int new_mask = new_size - 1; 7967 dtrace_hashbucket_t **new_tab, *bucket, *next; 7968 7969 ASSERT((new_size & new_mask) == 0); 7970 7971 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 7972 7973 for (i = 0; i < size; i++) { 7974 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 7975 dtrace_probe_t *probe = bucket->dthb_chain; 7976 7977 ASSERT(probe != NULL); 7978 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 7979 7980 next = bucket->dthb_next; 7981 bucket->dthb_next = new_tab[ndx]; 7982 new_tab[ndx] = bucket; 7983 } 7984 } 7985 7986 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 7987 hash->dth_tab = new_tab; 7988 hash->dth_size = new_size; 7989 hash->dth_mask = new_mask; 7990} 7991 7992static void 7993dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 7994{ 7995 int hashval = DTRACE_HASHSTR(hash, new); 7996 int ndx = hashval & hash->dth_mask; 7997 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 7998 dtrace_probe_t **nextp, **prevp; 7999 8000 for (; bucket != NULL; bucket = bucket->dthb_next) { 8001 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 8002 goto add; 8003 } 8004 8005 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 8006 dtrace_hash_resize(hash); 8007 dtrace_hash_add(hash, new); 8008 return; 8009 } 8010 8011 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 8012 bucket->dthb_next = hash->dth_tab[ndx]; 8013 hash->dth_tab[ndx] = bucket; 8014 hash->dth_nbuckets++; 8015 8016add: 8017 nextp = DTRACE_HASHNEXT(hash, new); 8018 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 8019 *nextp = bucket->dthb_chain; 8020 8021 if (bucket->dthb_chain != NULL) { 8022 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 8023 ASSERT(*prevp == NULL); 8024 *prevp = new; 8025 } 8026 8027 bucket->dthb_chain = new; 8028 bucket->dthb_len++; 8029} 8030 8031static dtrace_probe_t * 8032dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 8033{ 8034 int hashval = DTRACE_HASHSTR(hash, template); 8035 int ndx = hashval & hash->dth_mask; 8036 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8037 8038 for (; bucket != NULL; bucket = bucket->dthb_next) { 8039 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8040 return (bucket->dthb_chain); 8041 } 8042 8043 return (NULL); 8044} 8045 8046static int 8047dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 8048{ 8049 int hashval = DTRACE_HASHSTR(hash, template); 8050 int ndx = hashval & hash->dth_mask; 8051 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8052 8053 for (; bucket != NULL; bucket = bucket->dthb_next) { 8054 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 8055 return (bucket->dthb_len); 8056 } 8057 8058 return (0); 8059} 8060 8061static void 8062dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 8063{ 8064 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 8065 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 8066 8067 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 8068 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 8069 8070 /* 8071 * Find the bucket that we're removing this probe from. 8072 */ 8073 for (; bucket != NULL; bucket = bucket->dthb_next) { 8074 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 8075 break; 8076 } 8077 8078 ASSERT(bucket != NULL); 8079 8080 if (*prevp == NULL) { 8081 if (*nextp == NULL) { 8082 /* 8083 * The removed probe was the only probe on this 8084 * bucket; we need to remove the bucket. 8085 */ 8086 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 8087 8088 ASSERT(bucket->dthb_chain == probe); 8089 ASSERT(b != NULL); 8090 8091 if (b == bucket) { 8092 hash->dth_tab[ndx] = bucket->dthb_next; 8093 } else { 8094 while (b->dthb_next != bucket) 8095 b = b->dthb_next; 8096 b->dthb_next = bucket->dthb_next; 8097 } 8098 8099 ASSERT(hash->dth_nbuckets > 0); 8100 hash->dth_nbuckets--; 8101 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 8102 return; 8103 } 8104 8105 bucket->dthb_chain = *nextp; 8106 } else { 8107 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 8108 } 8109 8110 if (*nextp != NULL) 8111 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 8112} 8113 8114/* 8115 * DTrace Utility Functions 8116 * 8117 * These are random utility functions that are _not_ called from probe context. 8118 */ 8119static int 8120dtrace_badattr(const dtrace_attribute_t *a) 8121{ 8122 return (a->dtat_name > DTRACE_STABILITY_MAX || 8123 a->dtat_data > DTRACE_STABILITY_MAX || 8124 a->dtat_class > DTRACE_CLASS_MAX); 8125} 8126 8127/* 8128 * Return a duplicate copy of a string. If the specified string is NULL, 8129 * this function returns a zero-length string. 8130 */ 8131static char * 8132dtrace_strdup(const char *str) 8133{ 8134 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 8135 8136 if (str != NULL) 8137 (void) strcpy(new, str); 8138 8139 return (new); 8140} 8141 8142#define DTRACE_ISALPHA(c) \ 8143 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 8144 8145static int 8146dtrace_badname(const char *s) 8147{ 8148 char c; 8149 8150 if (s == NULL || (c = *s++) == '\0') 8151 return (0); 8152 8153 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 8154 return (1); 8155 8156 while ((c = *s++) != '\0') { 8157 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 8158 c != '-' && c != '_' && c != '.' && c != '`') 8159 return (1); 8160 } 8161 8162 return (0); 8163} 8164 8165static void 8166dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 8167{ 8168 uint32_t priv; 8169 8170#ifdef illumos 8171 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 8172 /* 8173 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 8174 */ 8175 priv = DTRACE_PRIV_ALL; 8176 } else { 8177 *uidp = crgetuid(cr); 8178 *zoneidp = crgetzoneid(cr); 8179 8180 priv = 0; 8181 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 8182 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 8183 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 8184 priv |= DTRACE_PRIV_USER; 8185 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 8186 priv |= DTRACE_PRIV_PROC; 8187 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 8188 priv |= DTRACE_PRIV_OWNER; 8189 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 8190 priv |= DTRACE_PRIV_ZONEOWNER; 8191 } 8192#else 8193 priv = DTRACE_PRIV_ALL; 8194#endif 8195 8196 *privp = priv; 8197} 8198 8199#ifdef DTRACE_ERRDEBUG 8200static void 8201dtrace_errdebug(const char *str) 8202{ 8203 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 8204 int occupied = 0; 8205 8206 mutex_enter(&dtrace_errlock); 8207 dtrace_errlast = str; 8208 dtrace_errthread = curthread; 8209 8210 while (occupied++ < DTRACE_ERRHASHSZ) { 8211 if (dtrace_errhash[hval].dter_msg == str) { 8212 dtrace_errhash[hval].dter_count++; 8213 goto out; 8214 } 8215 8216 if (dtrace_errhash[hval].dter_msg != NULL) { 8217 hval = (hval + 1) % DTRACE_ERRHASHSZ; 8218 continue; 8219 } 8220 8221 dtrace_errhash[hval].dter_msg = str; 8222 dtrace_errhash[hval].dter_count = 1; 8223 goto out; 8224 } 8225 8226 panic("dtrace: undersized error hash"); 8227out: 8228 mutex_exit(&dtrace_errlock); 8229} 8230#endif 8231 8232/* 8233 * DTrace Matching Functions 8234 * 8235 * These functions are used to match groups of probes, given some elements of 8236 * a probe tuple, or some globbed expressions for elements of a probe tuple. 8237 */ 8238static int 8239dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 8240 zoneid_t zoneid) 8241{ 8242 if (priv != DTRACE_PRIV_ALL) { 8243 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 8244 uint32_t match = priv & ppriv; 8245 8246 /* 8247 * No PRIV_DTRACE_* privileges... 8248 */ 8249 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 8250 DTRACE_PRIV_KERNEL)) == 0) 8251 return (0); 8252 8253 /* 8254 * No matching bits, but there were bits to match... 8255 */ 8256 if (match == 0 && ppriv != 0) 8257 return (0); 8258 8259 /* 8260 * Need to have permissions to the process, but don't... 8261 */ 8262 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 8263 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 8264 return (0); 8265 } 8266 8267 /* 8268 * Need to be in the same zone unless we possess the 8269 * privilege to examine all zones. 8270 */ 8271 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 8272 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 8273 return (0); 8274 } 8275 } 8276 8277 return (1); 8278} 8279 8280/* 8281 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 8282 * consists of input pattern strings and an ops-vector to evaluate them. 8283 * This function returns >0 for match, 0 for no match, and <0 for error. 8284 */ 8285static int 8286dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 8287 uint32_t priv, uid_t uid, zoneid_t zoneid) 8288{ 8289 dtrace_provider_t *pvp = prp->dtpr_provider; 8290 int rv; 8291 8292 if (pvp->dtpv_defunct) 8293 return (0); 8294 8295 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 8296 return (rv); 8297 8298 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 8299 return (rv); 8300 8301 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 8302 return (rv); 8303 8304 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 8305 return (rv); 8306 8307 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 8308 return (0); 8309 8310 return (rv); 8311} 8312 8313/* 8314 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 8315 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 8316 * libc's version, the kernel version only applies to 8-bit ASCII strings. 8317 * In addition, all of the recursion cases except for '*' matching have been 8318 * unwound. For '*', we still implement recursive evaluation, but a depth 8319 * counter is maintained and matching is aborted if we recurse too deep. 8320 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 8321 */ 8322static int 8323dtrace_match_glob(const char *s, const char *p, int depth) 8324{ 8325 const char *olds; 8326 char s1, c; 8327 int gs; 8328 8329 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 8330 return (-1); 8331 8332 if (s == NULL) 8333 s = ""; /* treat NULL as empty string */ 8334 8335top: 8336 olds = s; 8337 s1 = *s++; 8338 8339 if (p == NULL) 8340 return (0); 8341 8342 if ((c = *p++) == '\0') 8343 return (s1 == '\0'); 8344 8345 switch (c) { 8346 case '[': { 8347 int ok = 0, notflag = 0; 8348 char lc = '\0'; 8349 8350 if (s1 == '\0') 8351 return (0); 8352 8353 if (*p == '!') { 8354 notflag = 1; 8355 p++; 8356 } 8357 8358 if ((c = *p++) == '\0') 8359 return (0); 8360 8361 do { 8362 if (c == '-' && lc != '\0' && *p != ']') { 8363 if ((c = *p++) == '\0') 8364 return (0); 8365 if (c == '\\' && (c = *p++) == '\0') 8366 return (0); 8367 8368 if (notflag) { 8369 if (s1 < lc || s1 > c) 8370 ok++; 8371 else 8372 return (0); 8373 } else if (lc <= s1 && s1 <= c) 8374 ok++; 8375 8376 } else if (c == '\\' && (c = *p++) == '\0') 8377 return (0); 8378 8379 lc = c; /* save left-hand 'c' for next iteration */ 8380 8381 if (notflag) { 8382 if (s1 != c) 8383 ok++; 8384 else 8385 return (0); 8386 } else if (s1 == c) 8387 ok++; 8388 8389 if ((c = *p++) == '\0') 8390 return (0); 8391 8392 } while (c != ']'); 8393 8394 if (ok) 8395 goto top; 8396 8397 return (0); 8398 } 8399 8400 case '\\': 8401 if ((c = *p++) == '\0') 8402 return (0); 8403 /*FALLTHRU*/ 8404 8405 default: 8406 if (c != s1) 8407 return (0); 8408 /*FALLTHRU*/ 8409 8410 case '?': 8411 if (s1 != '\0') 8412 goto top; 8413 return (0); 8414 8415 case '*': 8416 while (*p == '*') 8417 p++; /* consecutive *'s are identical to a single one */ 8418 8419 if (*p == '\0') 8420 return (1); 8421 8422 for (s = olds; *s != '\0'; s++) { 8423 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 8424 return (gs); 8425 } 8426 8427 return (0); 8428 } 8429} 8430 8431/*ARGSUSED*/ 8432static int 8433dtrace_match_string(const char *s, const char *p, int depth) 8434{ 8435 return (s != NULL && strcmp(s, p) == 0); 8436} 8437 8438/*ARGSUSED*/ 8439static int 8440dtrace_match_nul(const char *s, const char *p, int depth) 8441{ 8442 return (1); /* always match the empty pattern */ 8443} 8444 8445/*ARGSUSED*/ 8446static int 8447dtrace_match_nonzero(const char *s, const char *p, int depth) 8448{ 8449 return (s != NULL && s[0] != '\0'); 8450} 8451 8452static int 8453dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 8454 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 8455{ 8456 dtrace_probe_t template, *probe; 8457 dtrace_hash_t *hash = NULL; 8458 int len, best = INT_MAX, nmatched = 0; 8459 dtrace_id_t i; 8460 8461 ASSERT(MUTEX_HELD(&dtrace_lock)); 8462 8463 /* 8464 * If the probe ID is specified in the key, just lookup by ID and 8465 * invoke the match callback once if a matching probe is found. 8466 */ 8467 if (pkp->dtpk_id != DTRACE_IDNONE) { 8468 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 8469 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 8470 (void) (*matched)(probe, arg); 8471 nmatched++; 8472 } 8473 return (nmatched); 8474 } 8475 8476 template.dtpr_mod = (char *)pkp->dtpk_mod; 8477 template.dtpr_func = (char *)pkp->dtpk_func; 8478 template.dtpr_name = (char *)pkp->dtpk_name; 8479 8480 /* 8481 * We want to find the most distinct of the module name, function 8482 * name, and name. So for each one that is not a glob pattern or 8483 * empty string, we perform a lookup in the corresponding hash and 8484 * use the hash table with the fewest collisions to do our search. 8485 */ 8486 if (pkp->dtpk_mmatch == &dtrace_match_string && 8487 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 8488 best = len; 8489 hash = dtrace_bymod; 8490 } 8491 8492 if (pkp->dtpk_fmatch == &dtrace_match_string && 8493 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 8494 best = len; 8495 hash = dtrace_byfunc; 8496 } 8497 8498 if (pkp->dtpk_nmatch == &dtrace_match_string && 8499 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 8500 best = len; 8501 hash = dtrace_byname; 8502 } 8503 8504 /* 8505 * If we did not select a hash table, iterate over every probe and 8506 * invoke our callback for each one that matches our input probe key. 8507 */ 8508 if (hash == NULL) { 8509 for (i = 0; i < dtrace_nprobes; i++) { 8510 if ((probe = dtrace_probes[i]) == NULL || 8511 dtrace_match_probe(probe, pkp, priv, uid, 8512 zoneid) <= 0) 8513 continue; 8514 8515 nmatched++; 8516 8517 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8518 break; 8519 } 8520 8521 return (nmatched); 8522 } 8523 8524 /* 8525 * If we selected a hash table, iterate over each probe of the same key 8526 * name and invoke the callback for every probe that matches the other 8527 * attributes of our input probe key. 8528 */ 8529 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 8530 probe = *(DTRACE_HASHNEXT(hash, probe))) { 8531 8532 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 8533 continue; 8534 8535 nmatched++; 8536 8537 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 8538 break; 8539 } 8540 8541 return (nmatched); 8542} 8543 8544/* 8545 * Return the function pointer dtrace_probecmp() should use to compare the 8546 * specified pattern with a string. For NULL or empty patterns, we select 8547 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 8548 * For non-empty non-glob strings, we use dtrace_match_string(). 8549 */ 8550static dtrace_probekey_f * 8551dtrace_probekey_func(const char *p) 8552{ 8553 char c; 8554 8555 if (p == NULL || *p == '\0') 8556 return (&dtrace_match_nul); 8557 8558 while ((c = *p++) != '\0') { 8559 if (c == '[' || c == '?' || c == '*' || c == '\\') 8560 return (&dtrace_match_glob); 8561 } 8562 8563 return (&dtrace_match_string); 8564} 8565 8566/* 8567 * Build a probe comparison key for use with dtrace_match_probe() from the 8568 * given probe description. By convention, a null key only matches anchored 8569 * probes: if each field is the empty string, reset dtpk_fmatch to 8570 * dtrace_match_nonzero(). 8571 */ 8572static void 8573dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 8574{ 8575 pkp->dtpk_prov = pdp->dtpd_provider; 8576 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 8577 8578 pkp->dtpk_mod = pdp->dtpd_mod; 8579 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 8580 8581 pkp->dtpk_func = pdp->dtpd_func; 8582 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 8583 8584 pkp->dtpk_name = pdp->dtpd_name; 8585 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 8586 8587 pkp->dtpk_id = pdp->dtpd_id; 8588 8589 if (pkp->dtpk_id == DTRACE_IDNONE && 8590 pkp->dtpk_pmatch == &dtrace_match_nul && 8591 pkp->dtpk_mmatch == &dtrace_match_nul && 8592 pkp->dtpk_fmatch == &dtrace_match_nul && 8593 pkp->dtpk_nmatch == &dtrace_match_nul) 8594 pkp->dtpk_fmatch = &dtrace_match_nonzero; 8595} 8596 8597/* 8598 * DTrace Provider-to-Framework API Functions 8599 * 8600 * These functions implement much of the Provider-to-Framework API, as 8601 * described in <sys/dtrace.h>. The parts of the API not in this section are 8602 * the functions in the API for probe management (found below), and 8603 * dtrace_probe() itself (found above). 8604 */ 8605 8606/* 8607 * Register the calling provider with the DTrace framework. This should 8608 * generally be called by DTrace providers in their attach(9E) entry point. 8609 */ 8610int 8611dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 8612 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 8613{ 8614 dtrace_provider_t *provider; 8615 8616 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 8617 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8618 "arguments", name ? name : "<NULL>"); 8619 return (EINVAL); 8620 } 8621 8622 if (name[0] == '\0' || dtrace_badname(name)) { 8623 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8624 "provider name", name); 8625 return (EINVAL); 8626 } 8627 8628 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 8629 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 8630 pops->dtps_destroy == NULL || 8631 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 8632 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8633 "provider ops", name); 8634 return (EINVAL); 8635 } 8636 8637 if (dtrace_badattr(&pap->dtpa_provider) || 8638 dtrace_badattr(&pap->dtpa_mod) || 8639 dtrace_badattr(&pap->dtpa_func) || 8640 dtrace_badattr(&pap->dtpa_name) || 8641 dtrace_badattr(&pap->dtpa_args)) { 8642 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8643 "provider attributes", name); 8644 return (EINVAL); 8645 } 8646 8647 if (priv & ~DTRACE_PRIV_ALL) { 8648 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 8649 "privilege attributes", name); 8650 return (EINVAL); 8651 } 8652 8653 if ((priv & DTRACE_PRIV_KERNEL) && 8654 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 8655 pops->dtps_usermode == NULL) { 8656 cmn_err(CE_WARN, "failed to register provider '%s': need " 8657 "dtps_usermode() op for given privilege attributes", name); 8658 return (EINVAL); 8659 } 8660 8661 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 8662 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8663 (void) strcpy(provider->dtpv_name, name); 8664 8665 provider->dtpv_attr = *pap; 8666 provider->dtpv_priv.dtpp_flags = priv; 8667 if (cr != NULL) { 8668 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 8669 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 8670 } 8671 provider->dtpv_pops = *pops; 8672 8673 if (pops->dtps_provide == NULL) { 8674 ASSERT(pops->dtps_provide_module != NULL); 8675 provider->dtpv_pops.dtps_provide = 8676 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 8677 } 8678 8679 if (pops->dtps_provide_module == NULL) { 8680 ASSERT(pops->dtps_provide != NULL); 8681 provider->dtpv_pops.dtps_provide_module = 8682 (void (*)(void *, modctl_t *))dtrace_nullop; 8683 } 8684 8685 if (pops->dtps_suspend == NULL) { 8686 ASSERT(pops->dtps_resume == NULL); 8687 provider->dtpv_pops.dtps_suspend = 8688 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8689 provider->dtpv_pops.dtps_resume = 8690 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 8691 } 8692 8693 provider->dtpv_arg = arg; 8694 *idp = (dtrace_provider_id_t)provider; 8695 8696 if (pops == &dtrace_provider_ops) { 8697 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8698 ASSERT(MUTEX_HELD(&dtrace_lock)); 8699 ASSERT(dtrace_anon.dta_enabling == NULL); 8700 8701 /* 8702 * We make sure that the DTrace provider is at the head of 8703 * the provider chain. 8704 */ 8705 provider->dtpv_next = dtrace_provider; 8706 dtrace_provider = provider; 8707 return (0); 8708 } 8709 8710 mutex_enter(&dtrace_provider_lock); 8711 mutex_enter(&dtrace_lock); 8712 8713 /* 8714 * If there is at least one provider registered, we'll add this 8715 * provider after the first provider. 8716 */ 8717 if (dtrace_provider != NULL) { 8718 provider->dtpv_next = dtrace_provider->dtpv_next; 8719 dtrace_provider->dtpv_next = provider; 8720 } else { 8721 dtrace_provider = provider; 8722 } 8723 8724 if (dtrace_retained != NULL) { 8725 dtrace_enabling_provide(provider); 8726 8727 /* 8728 * Now we need to call dtrace_enabling_matchall() -- which 8729 * will acquire cpu_lock and dtrace_lock. We therefore need 8730 * to drop all of our locks before calling into it... 8731 */ 8732 mutex_exit(&dtrace_lock); 8733 mutex_exit(&dtrace_provider_lock); 8734 dtrace_enabling_matchall(); 8735 8736 return (0); 8737 } 8738 8739 mutex_exit(&dtrace_lock); 8740 mutex_exit(&dtrace_provider_lock); 8741 8742 return (0); 8743} 8744 8745/* 8746 * Unregister the specified provider from the DTrace framework. This should 8747 * generally be called by DTrace providers in their detach(9E) entry point. 8748 */ 8749int 8750dtrace_unregister(dtrace_provider_id_t id) 8751{ 8752 dtrace_provider_t *old = (dtrace_provider_t *)id; 8753 dtrace_provider_t *prev = NULL; 8754 int i, self = 0, noreap = 0; 8755 dtrace_probe_t *probe, *first = NULL; 8756 8757 if (old->dtpv_pops.dtps_enable == 8758 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 8759 /* 8760 * If DTrace itself is the provider, we're called with locks 8761 * already held. 8762 */ 8763 ASSERT(old == dtrace_provider); 8764#ifdef illumos 8765 ASSERT(dtrace_devi != NULL); 8766#endif 8767 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 8768 ASSERT(MUTEX_HELD(&dtrace_lock)); 8769 self = 1; 8770 8771 if (dtrace_provider->dtpv_next != NULL) { 8772 /* 8773 * There's another provider here; return failure. 8774 */ 8775 return (EBUSY); 8776 } 8777 } else { 8778 mutex_enter(&dtrace_provider_lock); 8779#ifdef illumos 8780 mutex_enter(&mod_lock); 8781#endif 8782 mutex_enter(&dtrace_lock); 8783 } 8784 8785 /* 8786 * If anyone has /dev/dtrace open, or if there are anonymous enabled 8787 * probes, we refuse to let providers slither away, unless this 8788 * provider has already been explicitly invalidated. 8789 */ 8790 if (!old->dtpv_defunct && 8791 (dtrace_opens || (dtrace_anon.dta_state != NULL && 8792 dtrace_anon.dta_state->dts_necbs > 0))) { 8793 if (!self) { 8794 mutex_exit(&dtrace_lock); 8795#ifdef illumos 8796 mutex_exit(&mod_lock); 8797#endif 8798 mutex_exit(&dtrace_provider_lock); 8799 } 8800 return (EBUSY); 8801 } 8802 8803 /* 8804 * Attempt to destroy the probes associated with this provider. 8805 */ 8806 for (i = 0; i < dtrace_nprobes; i++) { 8807 if ((probe = dtrace_probes[i]) == NULL) 8808 continue; 8809 8810 if (probe->dtpr_provider != old) 8811 continue; 8812 8813 if (probe->dtpr_ecb == NULL) 8814 continue; 8815 8816 /* 8817 * If we are trying to unregister a defunct provider, and the 8818 * provider was made defunct within the interval dictated by 8819 * dtrace_unregister_defunct_reap, we'll (asynchronously) 8820 * attempt to reap our enablings. To denote that the provider 8821 * should reattempt to unregister itself at some point in the 8822 * future, we will return a differentiable error code (EAGAIN 8823 * instead of EBUSY) in this case. 8824 */ 8825 if (dtrace_gethrtime() - old->dtpv_defunct > 8826 dtrace_unregister_defunct_reap) 8827 noreap = 1; 8828 8829 if (!self) { 8830 mutex_exit(&dtrace_lock); 8831#ifdef illumos 8832 mutex_exit(&mod_lock); 8833#endif 8834 mutex_exit(&dtrace_provider_lock); 8835 } 8836 8837 if (noreap) 8838 return (EBUSY); 8839 8840 (void) taskq_dispatch(dtrace_taskq, 8841 (task_func_t *)dtrace_enabling_reap, NULL, TQ_SLEEP); 8842 8843 return (EAGAIN); 8844 } 8845 8846 /* 8847 * All of the probes for this provider are disabled; we can safely 8848 * remove all of them from their hash chains and from the probe array. 8849 */ 8850 for (i = 0; i < dtrace_nprobes; i++) { 8851 if ((probe = dtrace_probes[i]) == NULL) 8852 continue; 8853 8854 if (probe->dtpr_provider != old) 8855 continue; 8856 8857 dtrace_probes[i] = NULL; 8858 8859 dtrace_hash_remove(dtrace_bymod, probe); 8860 dtrace_hash_remove(dtrace_byfunc, probe); 8861 dtrace_hash_remove(dtrace_byname, probe); 8862 8863 if (first == NULL) { 8864 first = probe; 8865 probe->dtpr_nextmod = NULL; 8866 } else { 8867 probe->dtpr_nextmod = first; 8868 first = probe; 8869 } 8870 } 8871 8872 /* 8873 * The provider's probes have been removed from the hash chains and 8874 * from the probe array. Now issue a dtrace_sync() to be sure that 8875 * everyone has cleared out from any probe array processing. 8876 */ 8877 dtrace_sync(); 8878 8879 for (probe = first; probe != NULL; probe = first) { 8880 first = probe->dtpr_nextmod; 8881 8882 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 8883 probe->dtpr_arg); 8884 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 8885 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 8886 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 8887#ifdef illumos 8888 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 8889#else 8890 free_unr(dtrace_arena, probe->dtpr_id); 8891#endif 8892 kmem_free(probe, sizeof (dtrace_probe_t)); 8893 } 8894 8895 if ((prev = dtrace_provider) == old) { 8896#ifdef illumos 8897 ASSERT(self || dtrace_devi == NULL); 8898 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 8899#endif 8900 dtrace_provider = old->dtpv_next; 8901 } else { 8902 while (prev != NULL && prev->dtpv_next != old) 8903 prev = prev->dtpv_next; 8904 8905 if (prev == NULL) { 8906 panic("attempt to unregister non-existent " 8907 "dtrace provider %p\n", (void *)id); 8908 } 8909 8910 prev->dtpv_next = old->dtpv_next; 8911 } 8912 8913 if (!self) { 8914 mutex_exit(&dtrace_lock); 8915#ifdef illumos 8916 mutex_exit(&mod_lock); 8917#endif 8918 mutex_exit(&dtrace_provider_lock); 8919 } 8920 8921 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 8922 kmem_free(old, sizeof (dtrace_provider_t)); 8923 8924 return (0); 8925} 8926 8927/* 8928 * Invalidate the specified provider. All subsequent probe lookups for the 8929 * specified provider will fail, but its probes will not be removed. 8930 */ 8931void 8932dtrace_invalidate(dtrace_provider_id_t id) 8933{ 8934 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 8935 8936 ASSERT(pvp->dtpv_pops.dtps_enable != 8937 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8938 8939 mutex_enter(&dtrace_provider_lock); 8940 mutex_enter(&dtrace_lock); 8941 8942 pvp->dtpv_defunct = dtrace_gethrtime(); 8943 8944 mutex_exit(&dtrace_lock); 8945 mutex_exit(&dtrace_provider_lock); 8946} 8947 8948/* 8949 * Indicate whether or not DTrace has attached. 8950 */ 8951int 8952dtrace_attached(void) 8953{ 8954 /* 8955 * dtrace_provider will be non-NULL iff the DTrace driver has 8956 * attached. (It's non-NULL because DTrace is always itself a 8957 * provider.) 8958 */ 8959 return (dtrace_provider != NULL); 8960} 8961 8962/* 8963 * Remove all the unenabled probes for the given provider. This function is 8964 * not unlike dtrace_unregister(), except that it doesn't remove the provider 8965 * -- just as many of its associated probes as it can. 8966 */ 8967int 8968dtrace_condense(dtrace_provider_id_t id) 8969{ 8970 dtrace_provider_t *prov = (dtrace_provider_t *)id; 8971 int i; 8972 dtrace_probe_t *probe; 8973 8974 /* 8975 * Make sure this isn't the dtrace provider itself. 8976 */ 8977 ASSERT(prov->dtpv_pops.dtps_enable != 8978 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 8979 8980 mutex_enter(&dtrace_provider_lock); 8981 mutex_enter(&dtrace_lock); 8982 8983 /* 8984 * Attempt to destroy the probes associated with this provider. 8985 */ 8986 for (i = 0; i < dtrace_nprobes; i++) { 8987 if ((probe = dtrace_probes[i]) == NULL) 8988 continue; 8989 8990 if (probe->dtpr_provider != prov) 8991 continue; 8992 8993 if (probe->dtpr_ecb != NULL) 8994 continue; 8995 8996 dtrace_probes[i] = NULL; 8997 8998 dtrace_hash_remove(dtrace_bymod, probe); 8999 dtrace_hash_remove(dtrace_byfunc, probe); 9000 dtrace_hash_remove(dtrace_byname, probe); 9001 9002 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 9003 probe->dtpr_arg); 9004 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 9005 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 9006 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 9007 kmem_free(probe, sizeof (dtrace_probe_t)); 9008#ifdef illumos 9009 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 9010#else 9011 free_unr(dtrace_arena, i + 1); 9012#endif 9013 } 9014 9015 mutex_exit(&dtrace_lock); 9016 mutex_exit(&dtrace_provider_lock); 9017 9018 return (0); 9019} 9020 9021/* 9022 * DTrace Probe Management Functions 9023 * 9024 * The functions in this section perform the DTrace probe management, 9025 * including functions to create probes, look-up probes, and call into the 9026 * providers to request that probes be provided. Some of these functions are 9027 * in the Provider-to-Framework API; these functions can be identified by the 9028 * fact that they are not declared "static". 9029 */ 9030 9031/* 9032 * Create a probe with the specified module name, function name, and name. 9033 */ 9034dtrace_id_t 9035dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 9036 const char *func, const char *name, int aframes, void *arg) 9037{ 9038 dtrace_probe_t *probe, **probes; 9039 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 9040 dtrace_id_t id; 9041 9042 if (provider == dtrace_provider) { 9043 ASSERT(MUTEX_HELD(&dtrace_lock)); 9044 } else { 9045 mutex_enter(&dtrace_lock); 9046 } 9047 9048#ifdef illumos 9049 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 9050 VM_BESTFIT | VM_SLEEP); 9051#else 9052 id = alloc_unr(dtrace_arena); 9053#endif 9054 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 9055 9056 probe->dtpr_id = id; 9057 probe->dtpr_gen = dtrace_probegen++; 9058 probe->dtpr_mod = dtrace_strdup(mod); 9059 probe->dtpr_func = dtrace_strdup(func); 9060 probe->dtpr_name = dtrace_strdup(name); 9061 probe->dtpr_arg = arg; 9062 probe->dtpr_aframes = aframes; 9063 probe->dtpr_provider = provider; 9064 9065 dtrace_hash_add(dtrace_bymod, probe); 9066 dtrace_hash_add(dtrace_byfunc, probe); 9067 dtrace_hash_add(dtrace_byname, probe); 9068 9069 if (id - 1 >= dtrace_nprobes) { 9070 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 9071 size_t nsize = osize << 1; 9072 9073 if (nsize == 0) { 9074 ASSERT(osize == 0); 9075 ASSERT(dtrace_probes == NULL); 9076 nsize = sizeof (dtrace_probe_t *); 9077 } 9078 9079 probes = kmem_zalloc(nsize, KM_SLEEP); 9080 9081 if (dtrace_probes == NULL) { 9082 ASSERT(osize == 0); 9083 dtrace_probes = probes; 9084 dtrace_nprobes = 1; 9085 } else { 9086 dtrace_probe_t **oprobes = dtrace_probes; 9087 9088 bcopy(oprobes, probes, osize); 9089 dtrace_membar_producer(); 9090 dtrace_probes = probes; 9091 9092 dtrace_sync(); 9093 9094 /* 9095 * All CPUs are now seeing the new probes array; we can 9096 * safely free the old array. 9097 */ 9098 kmem_free(oprobes, osize); 9099 dtrace_nprobes <<= 1; 9100 } 9101 9102 ASSERT(id - 1 < dtrace_nprobes); 9103 } 9104 9105 ASSERT(dtrace_probes[id - 1] == NULL); 9106 dtrace_probes[id - 1] = probe; 9107 9108 if (provider != dtrace_provider) 9109 mutex_exit(&dtrace_lock); 9110 9111 return (id); 9112} 9113 9114static dtrace_probe_t * 9115dtrace_probe_lookup_id(dtrace_id_t id) 9116{ 9117 ASSERT(MUTEX_HELD(&dtrace_lock)); 9118 9119 if (id == 0 || id > dtrace_nprobes) 9120 return (NULL); 9121 9122 return (dtrace_probes[id - 1]); 9123} 9124 9125static int 9126dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 9127{ 9128 *((dtrace_id_t *)arg) = probe->dtpr_id; 9129 9130 return (DTRACE_MATCH_DONE); 9131} 9132 9133/* 9134 * Look up a probe based on provider and one or more of module name, function 9135 * name and probe name. 9136 */ 9137dtrace_id_t 9138dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 9139 char *func, char *name) 9140{ 9141 dtrace_probekey_t pkey; 9142 dtrace_id_t id; 9143 int match; 9144 9145 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 9146 pkey.dtpk_pmatch = &dtrace_match_string; 9147 pkey.dtpk_mod = mod; 9148 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 9149 pkey.dtpk_func = func; 9150 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 9151 pkey.dtpk_name = name; 9152 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 9153 pkey.dtpk_id = DTRACE_IDNONE; 9154 9155 mutex_enter(&dtrace_lock); 9156 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 9157 dtrace_probe_lookup_match, &id); 9158 mutex_exit(&dtrace_lock); 9159 9160 ASSERT(match == 1 || match == 0); 9161 return (match ? id : 0); 9162} 9163 9164/* 9165 * Returns the probe argument associated with the specified probe. 9166 */ 9167void * 9168dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 9169{ 9170 dtrace_probe_t *probe; 9171 void *rval = NULL; 9172 9173 mutex_enter(&dtrace_lock); 9174 9175 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 9176 probe->dtpr_provider == (dtrace_provider_t *)id) 9177 rval = probe->dtpr_arg; 9178 9179 mutex_exit(&dtrace_lock); 9180 9181 return (rval); 9182} 9183 9184/* 9185 * Copy a probe into a probe description. 9186 */ 9187static void 9188dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 9189{ 9190 bzero(pdp, sizeof (dtrace_probedesc_t)); 9191 pdp->dtpd_id = prp->dtpr_id; 9192 9193 (void) strncpy(pdp->dtpd_provider, 9194 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 9195 9196 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 9197 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 9198 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 9199} 9200 9201/* 9202 * Called to indicate that a probe -- or probes -- should be provided by a 9203 * specfied provider. If the specified description is NULL, the provider will 9204 * be told to provide all of its probes. (This is done whenever a new 9205 * consumer comes along, or whenever a retained enabling is to be matched.) If 9206 * the specified description is non-NULL, the provider is given the 9207 * opportunity to dynamically provide the specified probe, allowing providers 9208 * to support the creation of probes on-the-fly. (So-called _autocreated_ 9209 * probes.) If the provider is NULL, the operations will be applied to all 9210 * providers; if the provider is non-NULL the operations will only be applied 9211 * to the specified provider. The dtrace_provider_lock must be held, and the 9212 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 9213 * will need to grab the dtrace_lock when it reenters the framework through 9214 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 9215 */ 9216static void 9217dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 9218{ 9219#ifdef illumos 9220 modctl_t *ctl; 9221#endif 9222 int all = 0; 9223 9224 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 9225 9226 if (prv == NULL) { 9227 all = 1; 9228 prv = dtrace_provider; 9229 } 9230 9231 do { 9232 /* 9233 * First, call the blanket provide operation. 9234 */ 9235 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 9236 9237#ifdef illumos 9238 /* 9239 * Now call the per-module provide operation. We will grab 9240 * mod_lock to prevent the list from being modified. Note 9241 * that this also prevents the mod_busy bits from changing. 9242 * (mod_busy can only be changed with mod_lock held.) 9243 */ 9244 mutex_enter(&mod_lock); 9245 9246 ctl = &modules; 9247 do { 9248 if (ctl->mod_busy || ctl->mod_mp == NULL) 9249 continue; 9250 9251 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 9252 9253 } while ((ctl = ctl->mod_next) != &modules); 9254 9255 mutex_exit(&mod_lock); 9256#endif 9257 } while (all && (prv = prv->dtpv_next) != NULL); 9258} 9259 9260#ifdef illumos 9261/* 9262 * Iterate over each probe, and call the Framework-to-Provider API function 9263 * denoted by offs. 9264 */ 9265static void 9266dtrace_probe_foreach(uintptr_t offs) 9267{ 9268 dtrace_provider_t *prov; 9269 void (*func)(void *, dtrace_id_t, void *); 9270 dtrace_probe_t *probe; 9271 dtrace_icookie_t cookie; 9272 int i; 9273 9274 /* 9275 * We disable interrupts to walk through the probe array. This is 9276 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 9277 * won't see stale data. 9278 */ 9279 cookie = dtrace_interrupt_disable(); 9280 9281 for (i = 0; i < dtrace_nprobes; i++) { 9282 if ((probe = dtrace_probes[i]) == NULL) 9283 continue; 9284 9285 if (probe->dtpr_ecb == NULL) { 9286 /* 9287 * This probe isn't enabled -- don't call the function. 9288 */ 9289 continue; 9290 } 9291 9292 prov = probe->dtpr_provider; 9293 func = *((void(**)(void *, dtrace_id_t, void *)) 9294 ((uintptr_t)&prov->dtpv_pops + offs)); 9295 9296 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 9297 } 9298 9299 dtrace_interrupt_enable(cookie); 9300} 9301#endif 9302 9303static int 9304dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 9305{ 9306 dtrace_probekey_t pkey; 9307 uint32_t priv; 9308 uid_t uid; 9309 zoneid_t zoneid; 9310 9311 ASSERT(MUTEX_HELD(&dtrace_lock)); 9312 dtrace_ecb_create_cache = NULL; 9313 9314 if (desc == NULL) { 9315 /* 9316 * If we're passed a NULL description, we're being asked to 9317 * create an ECB with a NULL probe. 9318 */ 9319 (void) dtrace_ecb_create_enable(NULL, enab); 9320 return (0); 9321 } 9322 9323 dtrace_probekey(desc, &pkey); 9324 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 9325 &priv, &uid, &zoneid); 9326 9327 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 9328 enab)); 9329} 9330 9331/* 9332 * DTrace Helper Provider Functions 9333 */ 9334static void 9335dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 9336{ 9337 attr->dtat_name = DOF_ATTR_NAME(dofattr); 9338 attr->dtat_data = DOF_ATTR_DATA(dofattr); 9339 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 9340} 9341 9342static void 9343dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 9344 const dof_provider_t *dofprov, char *strtab) 9345{ 9346 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 9347 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 9348 dofprov->dofpv_provattr); 9349 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 9350 dofprov->dofpv_modattr); 9351 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 9352 dofprov->dofpv_funcattr); 9353 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 9354 dofprov->dofpv_nameattr); 9355 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 9356 dofprov->dofpv_argsattr); 9357} 9358 9359static void 9360dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9361{ 9362 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9363 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9364 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 9365 dof_provider_t *provider; 9366 dof_probe_t *probe; 9367 uint32_t *off, *enoff; 9368 uint8_t *arg; 9369 char *strtab; 9370 uint_t i, nprobes; 9371 dtrace_helper_provdesc_t dhpv; 9372 dtrace_helper_probedesc_t dhpb; 9373 dtrace_meta_t *meta = dtrace_meta_pid; 9374 dtrace_mops_t *mops = &meta->dtm_mops; 9375 void *parg; 9376 9377 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9378 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9379 provider->dofpv_strtab * dof->dofh_secsize); 9380 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9381 provider->dofpv_probes * dof->dofh_secsize); 9382 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9383 provider->dofpv_prargs * dof->dofh_secsize); 9384 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9385 provider->dofpv_proffs * dof->dofh_secsize); 9386 9387 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9388 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 9389 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 9390 enoff = NULL; 9391 9392 /* 9393 * See dtrace_helper_provider_validate(). 9394 */ 9395 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 9396 provider->dofpv_prenoffs != DOF_SECT_NONE) { 9397 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9398 provider->dofpv_prenoffs * dof->dofh_secsize); 9399 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 9400 } 9401 9402 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 9403 9404 /* 9405 * Create the provider. 9406 */ 9407 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9408 9409 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 9410 return; 9411 9412 meta->dtm_count++; 9413 9414 /* 9415 * Create the probes. 9416 */ 9417 for (i = 0; i < nprobes; i++) { 9418 probe = (dof_probe_t *)(uintptr_t)(daddr + 9419 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 9420 9421 /* See the check in dtrace_helper_provider_validate(). */ 9422 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) 9423 continue; 9424 9425 dhpb.dthpb_mod = dhp->dofhp_mod; 9426 dhpb.dthpb_func = strtab + probe->dofpr_func; 9427 dhpb.dthpb_name = strtab + probe->dofpr_name; 9428 dhpb.dthpb_base = probe->dofpr_addr; 9429 dhpb.dthpb_offs = off + probe->dofpr_offidx; 9430 dhpb.dthpb_noffs = probe->dofpr_noffs; 9431 if (enoff != NULL) { 9432 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 9433 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 9434 } else { 9435 dhpb.dthpb_enoffs = NULL; 9436 dhpb.dthpb_nenoffs = 0; 9437 } 9438 dhpb.dthpb_args = arg + probe->dofpr_argidx; 9439 dhpb.dthpb_nargc = probe->dofpr_nargc; 9440 dhpb.dthpb_xargc = probe->dofpr_xargc; 9441 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 9442 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 9443 9444 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 9445 } 9446} 9447 9448static void 9449dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 9450{ 9451 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9452 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9453 int i; 9454 9455 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9456 9457 for (i = 0; i < dof->dofh_secnum; i++) { 9458 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9459 dof->dofh_secoff + i * dof->dofh_secsize); 9460 9461 if (sec->dofs_type != DOF_SECT_PROVIDER) 9462 continue; 9463 9464 dtrace_helper_provide_one(dhp, sec, pid); 9465 } 9466 9467 /* 9468 * We may have just created probes, so we must now rematch against 9469 * any retained enablings. Note that this call will acquire both 9470 * cpu_lock and dtrace_lock; the fact that we are holding 9471 * dtrace_meta_lock now is what defines the ordering with respect to 9472 * these three locks. 9473 */ 9474 dtrace_enabling_matchall(); 9475} 9476 9477static void 9478dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 9479{ 9480 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9481 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9482 dof_sec_t *str_sec; 9483 dof_provider_t *provider; 9484 char *strtab; 9485 dtrace_helper_provdesc_t dhpv; 9486 dtrace_meta_t *meta = dtrace_meta_pid; 9487 dtrace_mops_t *mops = &meta->dtm_mops; 9488 9489 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 9490 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 9491 provider->dofpv_strtab * dof->dofh_secsize); 9492 9493 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 9494 9495 /* 9496 * Create the provider. 9497 */ 9498 dtrace_dofprov2hprov(&dhpv, provider, strtab); 9499 9500 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 9501 9502 meta->dtm_count--; 9503} 9504 9505static void 9506dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 9507{ 9508 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 9509 dof_hdr_t *dof = (dof_hdr_t *)daddr; 9510 int i; 9511 9512 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 9513 9514 for (i = 0; i < dof->dofh_secnum; i++) { 9515 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 9516 dof->dofh_secoff + i * dof->dofh_secsize); 9517 9518 if (sec->dofs_type != DOF_SECT_PROVIDER) 9519 continue; 9520 9521 dtrace_helper_provider_remove_one(dhp, sec, pid); 9522 } 9523} 9524 9525/* 9526 * DTrace Meta Provider-to-Framework API Functions 9527 * 9528 * These functions implement the Meta Provider-to-Framework API, as described 9529 * in <sys/dtrace.h>. 9530 */ 9531int 9532dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 9533 dtrace_meta_provider_id_t *idp) 9534{ 9535 dtrace_meta_t *meta; 9536 dtrace_helpers_t *help, *next; 9537 int i; 9538 9539 *idp = DTRACE_METAPROVNONE; 9540 9541 /* 9542 * We strictly don't need the name, but we hold onto it for 9543 * debuggability. All hail error queues! 9544 */ 9545 if (name == NULL) { 9546 cmn_err(CE_WARN, "failed to register meta-provider: " 9547 "invalid name"); 9548 return (EINVAL); 9549 } 9550 9551 if (mops == NULL || 9552 mops->dtms_create_probe == NULL || 9553 mops->dtms_provide_pid == NULL || 9554 mops->dtms_remove_pid == NULL) { 9555 cmn_err(CE_WARN, "failed to register meta-register %s: " 9556 "invalid ops", name); 9557 return (EINVAL); 9558 } 9559 9560 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 9561 meta->dtm_mops = *mops; 9562 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 9563 (void) strcpy(meta->dtm_name, name); 9564 meta->dtm_arg = arg; 9565 9566 mutex_enter(&dtrace_meta_lock); 9567 mutex_enter(&dtrace_lock); 9568 9569 if (dtrace_meta_pid != NULL) { 9570 mutex_exit(&dtrace_lock); 9571 mutex_exit(&dtrace_meta_lock); 9572 cmn_err(CE_WARN, "failed to register meta-register %s: " 9573 "user-land meta-provider exists", name); 9574 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 9575 kmem_free(meta, sizeof (dtrace_meta_t)); 9576 return (EINVAL); 9577 } 9578 9579 dtrace_meta_pid = meta; 9580 *idp = (dtrace_meta_provider_id_t)meta; 9581 9582 /* 9583 * If there are providers and probes ready to go, pass them 9584 * off to the new meta provider now. 9585 */ 9586 9587 help = dtrace_deferred_pid; 9588 dtrace_deferred_pid = NULL; 9589 9590 mutex_exit(&dtrace_lock); 9591 9592 while (help != NULL) { 9593 for (i = 0; i < help->dthps_nprovs; i++) { 9594 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 9595 help->dthps_pid); 9596 } 9597 9598 next = help->dthps_next; 9599 help->dthps_next = NULL; 9600 help->dthps_prev = NULL; 9601 help->dthps_deferred = 0; 9602 help = next; 9603 } 9604 9605 mutex_exit(&dtrace_meta_lock); 9606 9607 return (0); 9608} 9609 9610int 9611dtrace_meta_unregister(dtrace_meta_provider_id_t id) 9612{ 9613 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 9614 9615 mutex_enter(&dtrace_meta_lock); 9616 mutex_enter(&dtrace_lock); 9617 9618 if (old == dtrace_meta_pid) { 9619 pp = &dtrace_meta_pid; 9620 } else { 9621 panic("attempt to unregister non-existent " 9622 "dtrace meta-provider %p\n", (void *)old); 9623 } 9624 9625 if (old->dtm_count != 0) { 9626 mutex_exit(&dtrace_lock); 9627 mutex_exit(&dtrace_meta_lock); 9628 return (EBUSY); 9629 } 9630 9631 *pp = NULL; 9632 9633 mutex_exit(&dtrace_lock); 9634 mutex_exit(&dtrace_meta_lock); 9635 9636 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 9637 kmem_free(old, sizeof (dtrace_meta_t)); 9638 9639 return (0); 9640} 9641 9642 9643/* 9644 * DTrace DIF Object Functions 9645 */ 9646static int 9647dtrace_difo_err(uint_t pc, const char *format, ...) 9648{ 9649 if (dtrace_err_verbose) { 9650 va_list alist; 9651 9652 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 9653 va_start(alist, format); 9654 (void) vuprintf(format, alist); 9655 va_end(alist); 9656 } 9657 9658#ifdef DTRACE_ERRDEBUG 9659 dtrace_errdebug(format); 9660#endif 9661 return (1); 9662} 9663 9664/* 9665 * Validate a DTrace DIF object by checking the IR instructions. The following 9666 * rules are currently enforced by dtrace_difo_validate(): 9667 * 9668 * 1. Each instruction must have a valid opcode 9669 * 2. Each register, string, variable, or subroutine reference must be valid 9670 * 3. No instruction can modify register %r0 (must be zero) 9671 * 4. All instruction reserved bits must be set to zero 9672 * 5. The last instruction must be a "ret" instruction 9673 * 6. All branch targets must reference a valid instruction _after_ the branch 9674 */ 9675static int 9676dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 9677 cred_t *cr) 9678{ 9679 int err = 0, i; 9680 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 9681 int kcheckload; 9682 uint_t pc; 9683 int maxglobal = -1, maxlocal = -1, maxtlocal = -1; 9684 9685 kcheckload = cr == NULL || 9686 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 9687 9688 dp->dtdo_destructive = 0; 9689 9690 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 9691 dif_instr_t instr = dp->dtdo_buf[pc]; 9692 9693 uint_t r1 = DIF_INSTR_R1(instr); 9694 uint_t r2 = DIF_INSTR_R2(instr); 9695 uint_t rd = DIF_INSTR_RD(instr); 9696 uint_t rs = DIF_INSTR_RS(instr); 9697 uint_t label = DIF_INSTR_LABEL(instr); 9698 uint_t v = DIF_INSTR_VAR(instr); 9699 uint_t subr = DIF_INSTR_SUBR(instr); 9700 uint_t type = DIF_INSTR_TYPE(instr); 9701 uint_t op = DIF_INSTR_OP(instr); 9702 9703 switch (op) { 9704 case DIF_OP_OR: 9705 case DIF_OP_XOR: 9706 case DIF_OP_AND: 9707 case DIF_OP_SLL: 9708 case DIF_OP_SRL: 9709 case DIF_OP_SRA: 9710 case DIF_OP_SUB: 9711 case DIF_OP_ADD: 9712 case DIF_OP_MUL: 9713 case DIF_OP_SDIV: 9714 case DIF_OP_UDIV: 9715 case DIF_OP_SREM: 9716 case DIF_OP_UREM: 9717 case DIF_OP_COPYS: 9718 if (r1 >= nregs) 9719 err += efunc(pc, "invalid register %u\n", r1); 9720 if (r2 >= nregs) 9721 err += efunc(pc, "invalid register %u\n", r2); 9722 if (rd >= nregs) 9723 err += efunc(pc, "invalid register %u\n", rd); 9724 if (rd == 0) 9725 err += efunc(pc, "cannot write to %r0\n"); 9726 break; 9727 case DIF_OP_NOT: 9728 case DIF_OP_MOV: 9729 case DIF_OP_ALLOCS: 9730 if (r1 >= nregs) 9731 err += efunc(pc, "invalid register %u\n", r1); 9732 if (r2 != 0) 9733 err += efunc(pc, "non-zero reserved bits\n"); 9734 if (rd >= nregs) 9735 err += efunc(pc, "invalid register %u\n", rd); 9736 if (rd == 0) 9737 err += efunc(pc, "cannot write to %r0\n"); 9738 break; 9739 case DIF_OP_LDSB: 9740 case DIF_OP_LDSH: 9741 case DIF_OP_LDSW: 9742 case DIF_OP_LDUB: 9743 case DIF_OP_LDUH: 9744 case DIF_OP_LDUW: 9745 case DIF_OP_LDX: 9746 if (r1 >= nregs) 9747 err += efunc(pc, "invalid register %u\n", r1); 9748 if (r2 != 0) 9749 err += efunc(pc, "non-zero reserved bits\n"); 9750 if (rd >= nregs) 9751 err += efunc(pc, "invalid register %u\n", rd); 9752 if (rd == 0) 9753 err += efunc(pc, "cannot write to %r0\n"); 9754 if (kcheckload) 9755 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 9756 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 9757 break; 9758 case DIF_OP_RLDSB: 9759 case DIF_OP_RLDSH: 9760 case DIF_OP_RLDSW: 9761 case DIF_OP_RLDUB: 9762 case DIF_OP_RLDUH: 9763 case DIF_OP_RLDUW: 9764 case DIF_OP_RLDX: 9765 if (r1 >= nregs) 9766 err += efunc(pc, "invalid register %u\n", r1); 9767 if (r2 != 0) 9768 err += efunc(pc, "non-zero reserved bits\n"); 9769 if (rd >= nregs) 9770 err += efunc(pc, "invalid register %u\n", rd); 9771 if (rd == 0) 9772 err += efunc(pc, "cannot write to %r0\n"); 9773 break; 9774 case DIF_OP_ULDSB: 9775 case DIF_OP_ULDSH: 9776 case DIF_OP_ULDSW: 9777 case DIF_OP_ULDUB: 9778 case DIF_OP_ULDUH: 9779 case DIF_OP_ULDUW: 9780 case DIF_OP_ULDX: 9781 if (r1 >= nregs) 9782 err += efunc(pc, "invalid register %u\n", r1); 9783 if (r2 != 0) 9784 err += efunc(pc, "non-zero reserved bits\n"); 9785 if (rd >= nregs) 9786 err += efunc(pc, "invalid register %u\n", rd); 9787 if (rd == 0) 9788 err += efunc(pc, "cannot write to %r0\n"); 9789 break; 9790 case DIF_OP_STB: 9791 case DIF_OP_STH: 9792 case DIF_OP_STW: 9793 case DIF_OP_STX: 9794 if (r1 >= nregs) 9795 err += efunc(pc, "invalid register %u\n", r1); 9796 if (r2 != 0) 9797 err += efunc(pc, "non-zero reserved bits\n"); 9798 if (rd >= nregs) 9799 err += efunc(pc, "invalid register %u\n", rd); 9800 if (rd == 0) 9801 err += efunc(pc, "cannot write to 0 address\n"); 9802 break; 9803 case DIF_OP_CMP: 9804 case DIF_OP_SCMP: 9805 if (r1 >= nregs) 9806 err += efunc(pc, "invalid register %u\n", r1); 9807 if (r2 >= nregs) 9808 err += efunc(pc, "invalid register %u\n", r2); 9809 if (rd != 0) 9810 err += efunc(pc, "non-zero reserved bits\n"); 9811 break; 9812 case DIF_OP_TST: 9813 if (r1 >= nregs) 9814 err += efunc(pc, "invalid register %u\n", r1); 9815 if (r2 != 0 || rd != 0) 9816 err += efunc(pc, "non-zero reserved bits\n"); 9817 break; 9818 case DIF_OP_BA: 9819 case DIF_OP_BE: 9820 case DIF_OP_BNE: 9821 case DIF_OP_BG: 9822 case DIF_OP_BGU: 9823 case DIF_OP_BGE: 9824 case DIF_OP_BGEU: 9825 case DIF_OP_BL: 9826 case DIF_OP_BLU: 9827 case DIF_OP_BLE: 9828 case DIF_OP_BLEU: 9829 if (label >= dp->dtdo_len) { 9830 err += efunc(pc, "invalid branch target %u\n", 9831 label); 9832 } 9833 if (label <= pc) { 9834 err += efunc(pc, "backward branch to %u\n", 9835 label); 9836 } 9837 break; 9838 case DIF_OP_RET: 9839 if (r1 != 0 || r2 != 0) 9840 err += efunc(pc, "non-zero reserved bits\n"); 9841 if (rd >= nregs) 9842 err += efunc(pc, "invalid register %u\n", rd); 9843 break; 9844 case DIF_OP_NOP: 9845 case DIF_OP_POPTS: 9846 case DIF_OP_FLUSHTS: 9847 if (r1 != 0 || r2 != 0 || rd != 0) 9848 err += efunc(pc, "non-zero reserved bits\n"); 9849 break; 9850 case DIF_OP_SETX: 9851 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 9852 err += efunc(pc, "invalid integer ref %u\n", 9853 DIF_INSTR_INTEGER(instr)); 9854 } 9855 if (rd >= nregs) 9856 err += efunc(pc, "invalid register %u\n", rd); 9857 if (rd == 0) 9858 err += efunc(pc, "cannot write to %r0\n"); 9859 break; 9860 case DIF_OP_SETS: 9861 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 9862 err += efunc(pc, "invalid string ref %u\n", 9863 DIF_INSTR_STRING(instr)); 9864 } 9865 if (rd >= nregs) 9866 err += efunc(pc, "invalid register %u\n", rd); 9867 if (rd == 0) 9868 err += efunc(pc, "cannot write to %r0\n"); 9869 break; 9870 case DIF_OP_LDGA: 9871 case DIF_OP_LDTA: 9872 if (r1 > DIF_VAR_ARRAY_MAX) 9873 err += efunc(pc, "invalid array %u\n", r1); 9874 if (r2 >= nregs) 9875 err += efunc(pc, "invalid register %u\n", r2); 9876 if (rd >= nregs) 9877 err += efunc(pc, "invalid register %u\n", rd); 9878 if (rd == 0) 9879 err += efunc(pc, "cannot write to %r0\n"); 9880 break; 9881 case DIF_OP_LDGS: 9882 case DIF_OP_LDTS: 9883 case DIF_OP_LDLS: 9884 case DIF_OP_LDGAA: 9885 case DIF_OP_LDTAA: 9886 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 9887 err += efunc(pc, "invalid variable %u\n", v); 9888 if (rd >= nregs) 9889 err += efunc(pc, "invalid register %u\n", rd); 9890 if (rd == 0) 9891 err += efunc(pc, "cannot write to %r0\n"); 9892 break; 9893 case DIF_OP_STGS: 9894 case DIF_OP_STTS: 9895 case DIF_OP_STLS: 9896 case DIF_OP_STGAA: 9897 case DIF_OP_STTAA: 9898 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 9899 err += efunc(pc, "invalid variable %u\n", v); 9900 if (rs >= nregs) 9901 err += efunc(pc, "invalid register %u\n", rd); 9902 break; 9903 case DIF_OP_CALL: 9904 if (subr > DIF_SUBR_MAX) 9905 err += efunc(pc, "invalid subr %u\n", subr); 9906 if (rd >= nregs) 9907 err += efunc(pc, "invalid register %u\n", rd); 9908 if (rd == 0) 9909 err += efunc(pc, "cannot write to %r0\n"); 9910 9911 if (subr == DIF_SUBR_COPYOUT || 9912 subr == DIF_SUBR_COPYOUTSTR) { 9913 dp->dtdo_destructive = 1; 9914 } 9915 9916 if (subr == DIF_SUBR_GETF) { 9917 /* 9918 * If we have a getf() we need to record that 9919 * in our state. Note that our state can be 9920 * NULL if this is a helper -- but in that 9921 * case, the call to getf() is itself illegal, 9922 * and will be caught (slightly later) when 9923 * the helper is validated. 9924 */ 9925 if (vstate->dtvs_state != NULL) 9926 vstate->dtvs_state->dts_getf++; 9927 } 9928 9929 break; 9930 case DIF_OP_PUSHTR: 9931 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 9932 err += efunc(pc, "invalid ref type %u\n", type); 9933 if (r2 >= nregs) 9934 err += efunc(pc, "invalid register %u\n", r2); 9935 if (rs >= nregs) 9936 err += efunc(pc, "invalid register %u\n", rs); 9937 break; 9938 case DIF_OP_PUSHTV: 9939 if (type != DIF_TYPE_CTF) 9940 err += efunc(pc, "invalid val type %u\n", type); 9941 if (r2 >= nregs) 9942 err += efunc(pc, "invalid register %u\n", r2); 9943 if (rs >= nregs) 9944 err += efunc(pc, "invalid register %u\n", rs); 9945 break; 9946 default: 9947 err += efunc(pc, "invalid opcode %u\n", 9948 DIF_INSTR_OP(instr)); 9949 } 9950 } 9951 9952 if (dp->dtdo_len != 0 && 9953 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 9954 err += efunc(dp->dtdo_len - 1, 9955 "expected 'ret' as last DIF instruction\n"); 9956 } 9957 9958 if (!(dp->dtdo_rtype.dtdt_flags & (DIF_TF_BYREF | DIF_TF_BYUREF))) { 9959 /* 9960 * If we're not returning by reference, the size must be either 9961 * 0 or the size of one of the base types. 9962 */ 9963 switch (dp->dtdo_rtype.dtdt_size) { 9964 case 0: 9965 case sizeof (uint8_t): 9966 case sizeof (uint16_t): 9967 case sizeof (uint32_t): 9968 case sizeof (uint64_t): 9969 break; 9970 9971 default: 9972 err += efunc(dp->dtdo_len - 1, "bad return size\n"); 9973 } 9974 } 9975 9976 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 9977 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 9978 dtrace_diftype_t *vt, *et; 9979 uint_t id, ndx; 9980 9981 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 9982 v->dtdv_scope != DIFV_SCOPE_THREAD && 9983 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 9984 err += efunc(i, "unrecognized variable scope %d\n", 9985 v->dtdv_scope); 9986 break; 9987 } 9988 9989 if (v->dtdv_kind != DIFV_KIND_ARRAY && 9990 v->dtdv_kind != DIFV_KIND_SCALAR) { 9991 err += efunc(i, "unrecognized variable type %d\n", 9992 v->dtdv_kind); 9993 break; 9994 } 9995 9996 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 9997 err += efunc(i, "%d exceeds variable id limit\n", id); 9998 break; 9999 } 10000 10001 if (id < DIF_VAR_OTHER_UBASE) 10002 continue; 10003 10004 /* 10005 * For user-defined variables, we need to check that this 10006 * definition is identical to any previous definition that we 10007 * encountered. 10008 */ 10009 ndx = id - DIF_VAR_OTHER_UBASE; 10010 10011 switch (v->dtdv_scope) { 10012 case DIFV_SCOPE_GLOBAL: 10013 if (maxglobal == -1 || ndx > maxglobal) 10014 maxglobal = ndx; 10015 10016 if (ndx < vstate->dtvs_nglobals) { 10017 dtrace_statvar_t *svar; 10018 10019 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 10020 existing = &svar->dtsv_var; 10021 } 10022 10023 break; 10024 10025 case DIFV_SCOPE_THREAD: 10026 if (maxtlocal == -1 || ndx > maxtlocal) 10027 maxtlocal = ndx; 10028 10029 if (ndx < vstate->dtvs_ntlocals) 10030 existing = &vstate->dtvs_tlocals[ndx]; 10031 break; 10032 10033 case DIFV_SCOPE_LOCAL: 10034 if (maxlocal == -1 || ndx > maxlocal) 10035 maxlocal = ndx; 10036 10037 if (ndx < vstate->dtvs_nlocals) { 10038 dtrace_statvar_t *svar; 10039 10040 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 10041 existing = &svar->dtsv_var; 10042 } 10043 10044 break; 10045 } 10046 10047 vt = &v->dtdv_type; 10048 10049 if (vt->dtdt_flags & DIF_TF_BYREF) { 10050 if (vt->dtdt_size == 0) { 10051 err += efunc(i, "zero-sized variable\n"); 10052 break; 10053 } 10054 10055 if ((v->dtdv_scope == DIFV_SCOPE_GLOBAL || 10056 v->dtdv_scope == DIFV_SCOPE_LOCAL) && 10057 vt->dtdt_size > dtrace_statvar_maxsize) { 10058 err += efunc(i, "oversized by-ref static\n"); 10059 break; 10060 } 10061 } 10062 10063 if (existing == NULL || existing->dtdv_id == 0) 10064 continue; 10065 10066 ASSERT(existing->dtdv_id == v->dtdv_id); 10067 ASSERT(existing->dtdv_scope == v->dtdv_scope); 10068 10069 if (existing->dtdv_kind != v->dtdv_kind) 10070 err += efunc(i, "%d changed variable kind\n", id); 10071 10072 et = &existing->dtdv_type; 10073 10074 if (vt->dtdt_flags != et->dtdt_flags) { 10075 err += efunc(i, "%d changed variable type flags\n", id); 10076 break; 10077 } 10078 10079 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 10080 err += efunc(i, "%d changed variable type size\n", id); 10081 break; 10082 } 10083 } 10084 10085 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 10086 dif_instr_t instr = dp->dtdo_buf[pc]; 10087 10088 uint_t v = DIF_INSTR_VAR(instr); 10089 uint_t op = DIF_INSTR_OP(instr); 10090 10091 switch (op) { 10092 case DIF_OP_LDGS: 10093 case DIF_OP_LDGAA: 10094 case DIF_OP_STGS: 10095 case DIF_OP_STGAA: 10096 if (v > DIF_VAR_OTHER_UBASE + maxglobal) 10097 err += efunc(pc, "invalid variable %u\n", v); 10098 break; 10099 case DIF_OP_LDTS: 10100 case DIF_OP_LDTAA: 10101 case DIF_OP_STTS: 10102 case DIF_OP_STTAA: 10103 if (v > DIF_VAR_OTHER_UBASE + maxtlocal) 10104 err += efunc(pc, "invalid variable %u\n", v); 10105 break; 10106 case DIF_OP_LDLS: 10107 case DIF_OP_STLS: 10108 if (v > DIF_VAR_OTHER_UBASE + maxlocal) 10109 err += efunc(pc, "invalid variable %u\n", v); 10110 break; 10111 default: 10112 break; 10113 } 10114 } 10115 10116 return (err); 10117} 10118 10119/* 10120 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 10121 * are much more constrained than normal DIFOs. Specifically, they may 10122 * not: 10123 * 10124 * 1. Make calls to subroutines other than copyin(), copyinstr() or 10125 * miscellaneous string routines 10126 * 2. Access DTrace variables other than the args[] array, and the 10127 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 10128 * 3. Have thread-local variables. 10129 * 4. Have dynamic variables. 10130 */ 10131static int 10132dtrace_difo_validate_helper(dtrace_difo_t *dp) 10133{ 10134 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 10135 int err = 0; 10136 uint_t pc; 10137 10138 for (pc = 0; pc < dp->dtdo_len; pc++) { 10139 dif_instr_t instr = dp->dtdo_buf[pc]; 10140 10141 uint_t v = DIF_INSTR_VAR(instr); 10142 uint_t subr = DIF_INSTR_SUBR(instr); 10143 uint_t op = DIF_INSTR_OP(instr); 10144 10145 switch (op) { 10146 case DIF_OP_OR: 10147 case DIF_OP_XOR: 10148 case DIF_OP_AND: 10149 case DIF_OP_SLL: 10150 case DIF_OP_SRL: 10151 case DIF_OP_SRA: 10152 case DIF_OP_SUB: 10153 case DIF_OP_ADD: 10154 case DIF_OP_MUL: 10155 case DIF_OP_SDIV: 10156 case DIF_OP_UDIV: 10157 case DIF_OP_SREM: 10158 case DIF_OP_UREM: 10159 case DIF_OP_COPYS: 10160 case DIF_OP_NOT: 10161 case DIF_OP_MOV: 10162 case DIF_OP_RLDSB: 10163 case DIF_OP_RLDSH: 10164 case DIF_OP_RLDSW: 10165 case DIF_OP_RLDUB: 10166 case DIF_OP_RLDUH: 10167 case DIF_OP_RLDUW: 10168 case DIF_OP_RLDX: 10169 case DIF_OP_ULDSB: 10170 case DIF_OP_ULDSH: 10171 case DIF_OP_ULDSW: 10172 case DIF_OP_ULDUB: 10173 case DIF_OP_ULDUH: 10174 case DIF_OP_ULDUW: 10175 case DIF_OP_ULDX: 10176 case DIF_OP_STB: 10177 case DIF_OP_STH: 10178 case DIF_OP_STW: 10179 case DIF_OP_STX: 10180 case DIF_OP_ALLOCS: 10181 case DIF_OP_CMP: 10182 case DIF_OP_SCMP: 10183 case DIF_OP_TST: 10184 case DIF_OP_BA: 10185 case DIF_OP_BE: 10186 case DIF_OP_BNE: 10187 case DIF_OP_BG: 10188 case DIF_OP_BGU: 10189 case DIF_OP_BGE: 10190 case DIF_OP_BGEU: 10191 case DIF_OP_BL: 10192 case DIF_OP_BLU: 10193 case DIF_OP_BLE: 10194 case DIF_OP_BLEU: 10195 case DIF_OP_RET: 10196 case DIF_OP_NOP: 10197 case DIF_OP_POPTS: 10198 case DIF_OP_FLUSHTS: 10199 case DIF_OP_SETX: 10200 case DIF_OP_SETS: 10201 case DIF_OP_LDGA: 10202 case DIF_OP_LDLS: 10203 case DIF_OP_STGS: 10204 case DIF_OP_STLS: 10205 case DIF_OP_PUSHTR: 10206 case DIF_OP_PUSHTV: 10207 break; 10208 10209 case DIF_OP_LDGS: 10210 if (v >= DIF_VAR_OTHER_UBASE) 10211 break; 10212 10213 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 10214 break; 10215 10216 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 10217 v == DIF_VAR_PPID || v == DIF_VAR_TID || 10218 v == DIF_VAR_EXECARGS || 10219 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 10220 v == DIF_VAR_UID || v == DIF_VAR_GID) 10221 break; 10222 10223 err += efunc(pc, "illegal variable %u\n", v); 10224 break; 10225 10226 case DIF_OP_LDTA: 10227 case DIF_OP_LDTS: 10228 case DIF_OP_LDGAA: 10229 case DIF_OP_LDTAA: 10230 err += efunc(pc, "illegal dynamic variable load\n"); 10231 break; 10232 10233 case DIF_OP_STTS: 10234 case DIF_OP_STGAA: 10235 case DIF_OP_STTAA: 10236 err += efunc(pc, "illegal dynamic variable store\n"); 10237 break; 10238 10239 case DIF_OP_CALL: 10240 if (subr == DIF_SUBR_ALLOCA || 10241 subr == DIF_SUBR_BCOPY || 10242 subr == DIF_SUBR_COPYIN || 10243 subr == DIF_SUBR_COPYINTO || 10244 subr == DIF_SUBR_COPYINSTR || 10245 subr == DIF_SUBR_INDEX || 10246 subr == DIF_SUBR_INET_NTOA || 10247 subr == DIF_SUBR_INET_NTOA6 || 10248 subr == DIF_SUBR_INET_NTOP || 10249 subr == DIF_SUBR_JSON || 10250 subr == DIF_SUBR_LLTOSTR || 10251 subr == DIF_SUBR_STRTOLL || 10252 subr == DIF_SUBR_RINDEX || 10253 subr == DIF_SUBR_STRCHR || 10254 subr == DIF_SUBR_STRJOIN || 10255 subr == DIF_SUBR_STRRCHR || 10256 subr == DIF_SUBR_STRSTR || 10257 subr == DIF_SUBR_HTONS || 10258 subr == DIF_SUBR_HTONL || 10259 subr == DIF_SUBR_HTONLL || 10260 subr == DIF_SUBR_NTOHS || 10261 subr == DIF_SUBR_NTOHL || 10262 subr == DIF_SUBR_NTOHLL || 10263 subr == DIF_SUBR_MEMREF || 10264#ifndef illumos 10265 subr == DIF_SUBR_MEMSTR || 10266#endif 10267 subr == DIF_SUBR_TYPEREF) 10268 break; 10269 10270 err += efunc(pc, "invalid subr %u\n", subr); 10271 break; 10272 10273 default: 10274 err += efunc(pc, "invalid opcode %u\n", 10275 DIF_INSTR_OP(instr)); 10276 } 10277 } 10278 10279 return (err); 10280} 10281 10282/* 10283 * Returns 1 if the expression in the DIF object can be cached on a per-thread 10284 * basis; 0 if not. 10285 */ 10286static int 10287dtrace_difo_cacheable(dtrace_difo_t *dp) 10288{ 10289 int i; 10290 10291 if (dp == NULL) 10292 return (0); 10293 10294 for (i = 0; i < dp->dtdo_varlen; i++) { 10295 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10296 10297 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 10298 continue; 10299 10300 switch (v->dtdv_id) { 10301 case DIF_VAR_CURTHREAD: 10302 case DIF_VAR_PID: 10303 case DIF_VAR_TID: 10304 case DIF_VAR_EXECARGS: 10305 case DIF_VAR_EXECNAME: 10306 case DIF_VAR_ZONENAME: 10307 break; 10308 10309 default: 10310 return (0); 10311 } 10312 } 10313 10314 /* 10315 * This DIF object may be cacheable. Now we need to look for any 10316 * array loading instructions, any memory loading instructions, or 10317 * any stores to thread-local variables. 10318 */ 10319 for (i = 0; i < dp->dtdo_len; i++) { 10320 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 10321 10322 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 10323 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 10324 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 10325 op == DIF_OP_LDGA || op == DIF_OP_STTS) 10326 return (0); 10327 } 10328 10329 return (1); 10330} 10331 10332static void 10333dtrace_difo_hold(dtrace_difo_t *dp) 10334{ 10335 int i; 10336 10337 ASSERT(MUTEX_HELD(&dtrace_lock)); 10338 10339 dp->dtdo_refcnt++; 10340 ASSERT(dp->dtdo_refcnt != 0); 10341 10342 /* 10343 * We need to check this DIF object for references to the variable 10344 * DIF_VAR_VTIMESTAMP. 10345 */ 10346 for (i = 0; i < dp->dtdo_varlen; i++) { 10347 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10348 10349 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10350 continue; 10351 10352 if (dtrace_vtime_references++ == 0) 10353 dtrace_vtime_enable(); 10354 } 10355} 10356 10357/* 10358 * This routine calculates the dynamic variable chunksize for a given DIF 10359 * object. The calculation is not fool-proof, and can probably be tricked by 10360 * malicious DIF -- but it works for all compiler-generated DIF. Because this 10361 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 10362 * if a dynamic variable size exceeds the chunksize. 10363 */ 10364static void 10365dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10366{ 10367 uint64_t sval = 0; 10368 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 10369 const dif_instr_t *text = dp->dtdo_buf; 10370 uint_t pc, srd = 0; 10371 uint_t ttop = 0; 10372 size_t size, ksize; 10373 uint_t id, i; 10374 10375 for (pc = 0; pc < dp->dtdo_len; pc++) { 10376 dif_instr_t instr = text[pc]; 10377 uint_t op = DIF_INSTR_OP(instr); 10378 uint_t rd = DIF_INSTR_RD(instr); 10379 uint_t r1 = DIF_INSTR_R1(instr); 10380 uint_t nkeys = 0; 10381 uchar_t scope = 0; 10382 10383 dtrace_key_t *key = tupregs; 10384 10385 switch (op) { 10386 case DIF_OP_SETX: 10387 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 10388 srd = rd; 10389 continue; 10390 10391 case DIF_OP_STTS: 10392 key = &tupregs[DIF_DTR_NREGS]; 10393 key[0].dttk_size = 0; 10394 key[1].dttk_size = 0; 10395 nkeys = 2; 10396 scope = DIFV_SCOPE_THREAD; 10397 break; 10398 10399 case DIF_OP_STGAA: 10400 case DIF_OP_STTAA: 10401 nkeys = ttop; 10402 10403 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 10404 key[nkeys++].dttk_size = 0; 10405 10406 key[nkeys++].dttk_size = 0; 10407 10408 if (op == DIF_OP_STTAA) { 10409 scope = DIFV_SCOPE_THREAD; 10410 } else { 10411 scope = DIFV_SCOPE_GLOBAL; 10412 } 10413 10414 break; 10415 10416 case DIF_OP_PUSHTR: 10417 if (ttop == DIF_DTR_NREGS) 10418 return; 10419 10420 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 10421 /* 10422 * If the register for the size of the "pushtr" 10423 * is %r0 (or the value is 0) and the type is 10424 * a string, we'll use the system-wide default 10425 * string size. 10426 */ 10427 tupregs[ttop++].dttk_size = 10428 dtrace_strsize_default; 10429 } else { 10430 if (srd == 0) 10431 return; 10432 10433 if (sval > LONG_MAX) 10434 return; 10435 10436 tupregs[ttop++].dttk_size = sval; 10437 } 10438 10439 break; 10440 10441 case DIF_OP_PUSHTV: 10442 if (ttop == DIF_DTR_NREGS) 10443 return; 10444 10445 tupregs[ttop++].dttk_size = 0; 10446 break; 10447 10448 case DIF_OP_FLUSHTS: 10449 ttop = 0; 10450 break; 10451 10452 case DIF_OP_POPTS: 10453 if (ttop != 0) 10454 ttop--; 10455 break; 10456 } 10457 10458 sval = 0; 10459 srd = 0; 10460 10461 if (nkeys == 0) 10462 continue; 10463 10464 /* 10465 * We have a dynamic variable allocation; calculate its size. 10466 */ 10467 for (ksize = 0, i = 0; i < nkeys; i++) 10468 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 10469 10470 size = sizeof (dtrace_dynvar_t); 10471 size += sizeof (dtrace_key_t) * (nkeys - 1); 10472 size += ksize; 10473 10474 /* 10475 * Now we need to determine the size of the stored data. 10476 */ 10477 id = DIF_INSTR_VAR(instr); 10478 10479 for (i = 0; i < dp->dtdo_varlen; i++) { 10480 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10481 10482 if (v->dtdv_id == id && v->dtdv_scope == scope) { 10483 size += v->dtdv_type.dtdt_size; 10484 break; 10485 } 10486 } 10487 10488 if (i == dp->dtdo_varlen) 10489 return; 10490 10491 /* 10492 * We have the size. If this is larger than the chunk size 10493 * for our dynamic variable state, reset the chunk size. 10494 */ 10495 size = P2ROUNDUP(size, sizeof (uint64_t)); 10496 10497 /* 10498 * Before setting the chunk size, check that we're not going 10499 * to set it to a negative value... 10500 */ 10501 if (size > LONG_MAX) 10502 return; 10503 10504 /* 10505 * ...and make certain that we didn't badly overflow. 10506 */ 10507 if (size < ksize || size < sizeof (dtrace_dynvar_t)) 10508 return; 10509 10510 if (size > vstate->dtvs_dynvars.dtds_chunksize) 10511 vstate->dtvs_dynvars.dtds_chunksize = size; 10512 } 10513} 10514 10515static void 10516dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10517{ 10518 int i, oldsvars, osz, nsz, otlocals, ntlocals; 10519 uint_t id; 10520 10521 ASSERT(MUTEX_HELD(&dtrace_lock)); 10522 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 10523 10524 for (i = 0; i < dp->dtdo_varlen; i++) { 10525 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10526 dtrace_statvar_t *svar, ***svarp = NULL; 10527 size_t dsize = 0; 10528 uint8_t scope = v->dtdv_scope; 10529 int *np = NULL; 10530 10531 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10532 continue; 10533 10534 id -= DIF_VAR_OTHER_UBASE; 10535 10536 switch (scope) { 10537 case DIFV_SCOPE_THREAD: 10538 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 10539 dtrace_difv_t *tlocals; 10540 10541 if ((ntlocals = (otlocals << 1)) == 0) 10542 ntlocals = 1; 10543 10544 osz = otlocals * sizeof (dtrace_difv_t); 10545 nsz = ntlocals * sizeof (dtrace_difv_t); 10546 10547 tlocals = kmem_zalloc(nsz, KM_SLEEP); 10548 10549 if (osz != 0) { 10550 bcopy(vstate->dtvs_tlocals, 10551 tlocals, osz); 10552 kmem_free(vstate->dtvs_tlocals, osz); 10553 } 10554 10555 vstate->dtvs_tlocals = tlocals; 10556 vstate->dtvs_ntlocals = ntlocals; 10557 } 10558 10559 vstate->dtvs_tlocals[id] = *v; 10560 continue; 10561 10562 case DIFV_SCOPE_LOCAL: 10563 np = &vstate->dtvs_nlocals; 10564 svarp = &vstate->dtvs_locals; 10565 10566 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10567 dsize = NCPU * (v->dtdv_type.dtdt_size + 10568 sizeof (uint64_t)); 10569 else 10570 dsize = NCPU * sizeof (uint64_t); 10571 10572 break; 10573 10574 case DIFV_SCOPE_GLOBAL: 10575 np = &vstate->dtvs_nglobals; 10576 svarp = &vstate->dtvs_globals; 10577 10578 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 10579 dsize = v->dtdv_type.dtdt_size + 10580 sizeof (uint64_t); 10581 10582 break; 10583 10584 default: 10585 ASSERT(0); 10586 } 10587 10588 while (id >= (oldsvars = *np)) { 10589 dtrace_statvar_t **statics; 10590 int newsvars, oldsize, newsize; 10591 10592 if ((newsvars = (oldsvars << 1)) == 0) 10593 newsvars = 1; 10594 10595 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 10596 newsize = newsvars * sizeof (dtrace_statvar_t *); 10597 10598 statics = kmem_zalloc(newsize, KM_SLEEP); 10599 10600 if (oldsize != 0) { 10601 bcopy(*svarp, statics, oldsize); 10602 kmem_free(*svarp, oldsize); 10603 } 10604 10605 *svarp = statics; 10606 *np = newsvars; 10607 } 10608 10609 if ((svar = (*svarp)[id]) == NULL) { 10610 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 10611 svar->dtsv_var = *v; 10612 10613 if ((svar->dtsv_size = dsize) != 0) { 10614 svar->dtsv_data = (uint64_t)(uintptr_t) 10615 kmem_zalloc(dsize, KM_SLEEP); 10616 } 10617 10618 (*svarp)[id] = svar; 10619 } 10620 10621 svar->dtsv_refcnt++; 10622 } 10623 10624 dtrace_difo_chunksize(dp, vstate); 10625 dtrace_difo_hold(dp); 10626} 10627 10628static dtrace_difo_t * 10629dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10630{ 10631 dtrace_difo_t *new; 10632 size_t sz; 10633 10634 ASSERT(dp->dtdo_buf != NULL); 10635 ASSERT(dp->dtdo_refcnt != 0); 10636 10637 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 10638 10639 ASSERT(dp->dtdo_buf != NULL); 10640 sz = dp->dtdo_len * sizeof (dif_instr_t); 10641 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 10642 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 10643 new->dtdo_len = dp->dtdo_len; 10644 10645 if (dp->dtdo_strtab != NULL) { 10646 ASSERT(dp->dtdo_strlen != 0); 10647 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 10648 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 10649 new->dtdo_strlen = dp->dtdo_strlen; 10650 } 10651 10652 if (dp->dtdo_inttab != NULL) { 10653 ASSERT(dp->dtdo_intlen != 0); 10654 sz = dp->dtdo_intlen * sizeof (uint64_t); 10655 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 10656 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 10657 new->dtdo_intlen = dp->dtdo_intlen; 10658 } 10659 10660 if (dp->dtdo_vartab != NULL) { 10661 ASSERT(dp->dtdo_varlen != 0); 10662 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 10663 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 10664 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 10665 new->dtdo_varlen = dp->dtdo_varlen; 10666 } 10667 10668 dtrace_difo_init(new, vstate); 10669 return (new); 10670} 10671 10672static void 10673dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10674{ 10675 int i; 10676 10677 ASSERT(dp->dtdo_refcnt == 0); 10678 10679 for (i = 0; i < dp->dtdo_varlen; i++) { 10680 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10681 dtrace_statvar_t *svar, **svarp = NULL; 10682 uint_t id; 10683 uint8_t scope = v->dtdv_scope; 10684 int *np = NULL; 10685 10686 switch (scope) { 10687 case DIFV_SCOPE_THREAD: 10688 continue; 10689 10690 case DIFV_SCOPE_LOCAL: 10691 np = &vstate->dtvs_nlocals; 10692 svarp = vstate->dtvs_locals; 10693 break; 10694 10695 case DIFV_SCOPE_GLOBAL: 10696 np = &vstate->dtvs_nglobals; 10697 svarp = vstate->dtvs_globals; 10698 break; 10699 10700 default: 10701 ASSERT(0); 10702 } 10703 10704 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 10705 continue; 10706 10707 id -= DIF_VAR_OTHER_UBASE; 10708 ASSERT(id < *np); 10709 10710 svar = svarp[id]; 10711 ASSERT(svar != NULL); 10712 ASSERT(svar->dtsv_refcnt > 0); 10713 10714 if (--svar->dtsv_refcnt > 0) 10715 continue; 10716 10717 if (svar->dtsv_size != 0) { 10718 ASSERT(svar->dtsv_data != 0); 10719 kmem_free((void *)(uintptr_t)svar->dtsv_data, 10720 svar->dtsv_size); 10721 } 10722 10723 kmem_free(svar, sizeof (dtrace_statvar_t)); 10724 svarp[id] = NULL; 10725 } 10726 10727 if (dp->dtdo_buf != NULL) 10728 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 10729 if (dp->dtdo_inttab != NULL) 10730 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 10731 if (dp->dtdo_strtab != NULL) 10732 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 10733 if (dp->dtdo_vartab != NULL) 10734 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 10735 10736 kmem_free(dp, sizeof (dtrace_difo_t)); 10737} 10738 10739static void 10740dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 10741{ 10742 int i; 10743 10744 ASSERT(MUTEX_HELD(&dtrace_lock)); 10745 ASSERT(dp->dtdo_refcnt != 0); 10746 10747 for (i = 0; i < dp->dtdo_varlen; i++) { 10748 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 10749 10750 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 10751 continue; 10752 10753 ASSERT(dtrace_vtime_references > 0); 10754 if (--dtrace_vtime_references == 0) 10755 dtrace_vtime_disable(); 10756 } 10757 10758 if (--dp->dtdo_refcnt == 0) 10759 dtrace_difo_destroy(dp, vstate); 10760} 10761 10762/* 10763 * DTrace Format Functions 10764 */ 10765static uint16_t 10766dtrace_format_add(dtrace_state_t *state, char *str) 10767{ 10768 char *fmt, **new; 10769 uint16_t ndx, len = strlen(str) + 1; 10770 10771 fmt = kmem_zalloc(len, KM_SLEEP); 10772 bcopy(str, fmt, len); 10773 10774 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 10775 if (state->dts_formats[ndx] == NULL) { 10776 state->dts_formats[ndx] = fmt; 10777 return (ndx + 1); 10778 } 10779 } 10780 10781 if (state->dts_nformats == USHRT_MAX) { 10782 /* 10783 * This is only likely if a denial-of-service attack is being 10784 * attempted. As such, it's okay to fail silently here. 10785 */ 10786 kmem_free(fmt, len); 10787 return (0); 10788 } 10789 10790 /* 10791 * For simplicity, we always resize the formats array to be exactly the 10792 * number of formats. 10793 */ 10794 ndx = state->dts_nformats++; 10795 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 10796 10797 if (state->dts_formats != NULL) { 10798 ASSERT(ndx != 0); 10799 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 10800 kmem_free(state->dts_formats, ndx * sizeof (char *)); 10801 } 10802 10803 state->dts_formats = new; 10804 state->dts_formats[ndx] = fmt; 10805 10806 return (ndx + 1); 10807} 10808 10809static void 10810dtrace_format_remove(dtrace_state_t *state, uint16_t format) 10811{ 10812 char *fmt; 10813 10814 ASSERT(state->dts_formats != NULL); 10815 ASSERT(format <= state->dts_nformats); 10816 ASSERT(state->dts_formats[format - 1] != NULL); 10817 10818 fmt = state->dts_formats[format - 1]; 10819 kmem_free(fmt, strlen(fmt) + 1); 10820 state->dts_formats[format - 1] = NULL; 10821} 10822 10823static void 10824dtrace_format_destroy(dtrace_state_t *state) 10825{ 10826 int i; 10827 10828 if (state->dts_nformats == 0) { 10829 ASSERT(state->dts_formats == NULL); 10830 return; 10831 } 10832 10833 ASSERT(state->dts_formats != NULL); 10834 10835 for (i = 0; i < state->dts_nformats; i++) { 10836 char *fmt = state->dts_formats[i]; 10837 10838 if (fmt == NULL) 10839 continue; 10840 10841 kmem_free(fmt, strlen(fmt) + 1); 10842 } 10843 10844 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 10845 state->dts_nformats = 0; 10846 state->dts_formats = NULL; 10847} 10848 10849/* 10850 * DTrace Predicate Functions 10851 */ 10852static dtrace_predicate_t * 10853dtrace_predicate_create(dtrace_difo_t *dp) 10854{ 10855 dtrace_predicate_t *pred; 10856 10857 ASSERT(MUTEX_HELD(&dtrace_lock)); 10858 ASSERT(dp->dtdo_refcnt != 0); 10859 10860 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 10861 pred->dtp_difo = dp; 10862 pred->dtp_refcnt = 1; 10863 10864 if (!dtrace_difo_cacheable(dp)) 10865 return (pred); 10866 10867 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 10868 /* 10869 * This is only theoretically possible -- we have had 2^32 10870 * cacheable predicates on this machine. We cannot allow any 10871 * more predicates to become cacheable: as unlikely as it is, 10872 * there may be a thread caching a (now stale) predicate cache 10873 * ID. (N.B.: the temptation is being successfully resisted to 10874 * have this cmn_err() "Holy shit -- we executed this code!") 10875 */ 10876 return (pred); 10877 } 10878 10879 pred->dtp_cacheid = dtrace_predcache_id++; 10880 10881 return (pred); 10882} 10883 10884static void 10885dtrace_predicate_hold(dtrace_predicate_t *pred) 10886{ 10887 ASSERT(MUTEX_HELD(&dtrace_lock)); 10888 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 10889 ASSERT(pred->dtp_refcnt > 0); 10890 10891 pred->dtp_refcnt++; 10892} 10893 10894static void 10895dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 10896{ 10897 dtrace_difo_t *dp = pred->dtp_difo; 10898 10899 ASSERT(MUTEX_HELD(&dtrace_lock)); 10900 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 10901 ASSERT(pred->dtp_refcnt > 0); 10902 10903 if (--pred->dtp_refcnt == 0) { 10904 dtrace_difo_release(pred->dtp_difo, vstate); 10905 kmem_free(pred, sizeof (dtrace_predicate_t)); 10906 } 10907} 10908 10909/* 10910 * DTrace Action Description Functions 10911 */ 10912static dtrace_actdesc_t * 10913dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 10914 uint64_t uarg, uint64_t arg) 10915{ 10916 dtrace_actdesc_t *act; 10917 10918#ifdef illumos 10919 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 10920 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 10921#endif 10922 10923 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 10924 act->dtad_kind = kind; 10925 act->dtad_ntuple = ntuple; 10926 act->dtad_uarg = uarg; 10927 act->dtad_arg = arg; 10928 act->dtad_refcnt = 1; 10929 10930 return (act); 10931} 10932 10933static void 10934dtrace_actdesc_hold(dtrace_actdesc_t *act) 10935{ 10936 ASSERT(act->dtad_refcnt >= 1); 10937 act->dtad_refcnt++; 10938} 10939 10940static void 10941dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 10942{ 10943 dtrace_actkind_t kind = act->dtad_kind; 10944 dtrace_difo_t *dp; 10945 10946 ASSERT(act->dtad_refcnt >= 1); 10947 10948 if (--act->dtad_refcnt != 0) 10949 return; 10950 10951 if ((dp = act->dtad_difo) != NULL) 10952 dtrace_difo_release(dp, vstate); 10953 10954 if (DTRACEACT_ISPRINTFLIKE(kind)) { 10955 char *str = (char *)(uintptr_t)act->dtad_arg; 10956 10957#ifdef illumos 10958 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 10959 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 10960#endif 10961 10962 if (str != NULL) 10963 kmem_free(str, strlen(str) + 1); 10964 } 10965 10966 kmem_free(act, sizeof (dtrace_actdesc_t)); 10967} 10968 10969/* 10970 * DTrace ECB Functions 10971 */ 10972static dtrace_ecb_t * 10973dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 10974{ 10975 dtrace_ecb_t *ecb; 10976 dtrace_epid_t epid; 10977 10978 ASSERT(MUTEX_HELD(&dtrace_lock)); 10979 10980 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 10981 ecb->dte_predicate = NULL; 10982 ecb->dte_probe = probe; 10983 10984 /* 10985 * The default size is the size of the default action: recording 10986 * the header. 10987 */ 10988 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_rechdr_t); 10989 ecb->dte_alignment = sizeof (dtrace_epid_t); 10990 10991 epid = state->dts_epid++; 10992 10993 if (epid - 1 >= state->dts_necbs) { 10994 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 10995 int necbs = state->dts_necbs << 1; 10996 10997 ASSERT(epid == state->dts_necbs + 1); 10998 10999 if (necbs == 0) { 11000 ASSERT(oecbs == NULL); 11001 necbs = 1; 11002 } 11003 11004 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 11005 11006 if (oecbs != NULL) 11007 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 11008 11009 dtrace_membar_producer(); 11010 state->dts_ecbs = ecbs; 11011 11012 if (oecbs != NULL) { 11013 /* 11014 * If this state is active, we must dtrace_sync() 11015 * before we can free the old dts_ecbs array: we're 11016 * coming in hot, and there may be active ring 11017 * buffer processing (which indexes into the dts_ecbs 11018 * array) on another CPU. 11019 */ 11020 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 11021 dtrace_sync(); 11022 11023 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 11024 } 11025 11026 dtrace_membar_producer(); 11027 state->dts_necbs = necbs; 11028 } 11029 11030 ecb->dte_state = state; 11031 11032 ASSERT(state->dts_ecbs[epid - 1] == NULL); 11033 dtrace_membar_producer(); 11034 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 11035 11036 return (ecb); 11037} 11038 11039static void 11040dtrace_ecb_enable(dtrace_ecb_t *ecb) 11041{ 11042 dtrace_probe_t *probe = ecb->dte_probe; 11043 11044 ASSERT(MUTEX_HELD(&cpu_lock)); 11045 ASSERT(MUTEX_HELD(&dtrace_lock)); 11046 ASSERT(ecb->dte_next == NULL); 11047 11048 if (probe == NULL) { 11049 /* 11050 * This is the NULL probe -- there's nothing to do. 11051 */ 11052 return; 11053 } 11054 11055 if (probe->dtpr_ecb == NULL) { 11056 dtrace_provider_t *prov = probe->dtpr_provider; 11057 11058 /* 11059 * We're the first ECB on this probe. 11060 */ 11061 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 11062 11063 if (ecb->dte_predicate != NULL) 11064 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 11065 11066 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 11067 probe->dtpr_id, probe->dtpr_arg); 11068 } else { 11069 /* 11070 * This probe is already active. Swing the last pointer to 11071 * point to the new ECB, and issue a dtrace_sync() to assure 11072 * that all CPUs have seen the change. 11073 */ 11074 ASSERT(probe->dtpr_ecb_last != NULL); 11075 probe->dtpr_ecb_last->dte_next = ecb; 11076 probe->dtpr_ecb_last = ecb; 11077 probe->dtpr_predcache = 0; 11078 11079 dtrace_sync(); 11080 } 11081} 11082 11083static void 11084dtrace_ecb_resize(dtrace_ecb_t *ecb) 11085{ 11086 dtrace_action_t *act; 11087 uint32_t curneeded = UINT32_MAX; 11088 uint32_t aggbase = UINT32_MAX; 11089 11090 /* 11091 * If we record anything, we always record the dtrace_rechdr_t. (And 11092 * we always record it first.) 11093 */ 11094 ecb->dte_size = sizeof (dtrace_rechdr_t); 11095 ecb->dte_alignment = sizeof (dtrace_epid_t); 11096 11097 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11098 dtrace_recdesc_t *rec = &act->dta_rec; 11099 ASSERT(rec->dtrd_size > 0 || rec->dtrd_alignment == 1); 11100 11101 ecb->dte_alignment = MAX(ecb->dte_alignment, 11102 rec->dtrd_alignment); 11103 11104 if (DTRACEACT_ISAGG(act->dta_kind)) { 11105 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11106 11107 ASSERT(rec->dtrd_size != 0); 11108 ASSERT(agg->dtag_first != NULL); 11109 ASSERT(act->dta_prev->dta_intuple); 11110 ASSERT(aggbase != UINT32_MAX); 11111 ASSERT(curneeded != UINT32_MAX); 11112 11113 agg->dtag_base = aggbase; 11114 11115 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11116 rec->dtrd_offset = curneeded; 11117 curneeded += rec->dtrd_size; 11118 ecb->dte_needed = MAX(ecb->dte_needed, curneeded); 11119 11120 aggbase = UINT32_MAX; 11121 curneeded = UINT32_MAX; 11122 } else if (act->dta_intuple) { 11123 if (curneeded == UINT32_MAX) { 11124 /* 11125 * This is the first record in a tuple. Align 11126 * curneeded to be at offset 4 in an 8-byte 11127 * aligned block. 11128 */ 11129 ASSERT(act->dta_prev == NULL || 11130 !act->dta_prev->dta_intuple); 11131 ASSERT3U(aggbase, ==, UINT32_MAX); 11132 curneeded = P2PHASEUP(ecb->dte_size, 11133 sizeof (uint64_t), sizeof (dtrace_aggid_t)); 11134 11135 aggbase = curneeded - sizeof (dtrace_aggid_t); 11136 ASSERT(IS_P2ALIGNED(aggbase, 11137 sizeof (uint64_t))); 11138 } 11139 curneeded = P2ROUNDUP(curneeded, rec->dtrd_alignment); 11140 rec->dtrd_offset = curneeded; 11141 curneeded += rec->dtrd_size; 11142 } else { 11143 /* tuples must be followed by an aggregation */ 11144 ASSERT(act->dta_prev == NULL || 11145 !act->dta_prev->dta_intuple); 11146 11147 ecb->dte_size = P2ROUNDUP(ecb->dte_size, 11148 rec->dtrd_alignment); 11149 rec->dtrd_offset = ecb->dte_size; 11150 ecb->dte_size += rec->dtrd_size; 11151 ecb->dte_needed = MAX(ecb->dte_needed, ecb->dte_size); 11152 } 11153 } 11154 11155 if ((act = ecb->dte_action) != NULL && 11156 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 11157 ecb->dte_size == sizeof (dtrace_rechdr_t)) { 11158 /* 11159 * If the size is still sizeof (dtrace_rechdr_t), then all 11160 * actions store no data; set the size to 0. 11161 */ 11162 ecb->dte_size = 0; 11163 } 11164 11165 ecb->dte_size = P2ROUNDUP(ecb->dte_size, sizeof (dtrace_epid_t)); 11166 ecb->dte_needed = P2ROUNDUP(ecb->dte_needed, (sizeof (dtrace_epid_t))); 11167 ecb->dte_state->dts_needed = MAX(ecb->dte_state->dts_needed, 11168 ecb->dte_needed); 11169} 11170 11171static dtrace_action_t * 11172dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11173{ 11174 dtrace_aggregation_t *agg; 11175 size_t size = sizeof (uint64_t); 11176 int ntuple = desc->dtad_ntuple; 11177 dtrace_action_t *act; 11178 dtrace_recdesc_t *frec; 11179 dtrace_aggid_t aggid; 11180 dtrace_state_t *state = ecb->dte_state; 11181 11182 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 11183 agg->dtag_ecb = ecb; 11184 11185 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 11186 11187 switch (desc->dtad_kind) { 11188 case DTRACEAGG_MIN: 11189 agg->dtag_initial = INT64_MAX; 11190 agg->dtag_aggregate = dtrace_aggregate_min; 11191 break; 11192 11193 case DTRACEAGG_MAX: 11194 agg->dtag_initial = INT64_MIN; 11195 agg->dtag_aggregate = dtrace_aggregate_max; 11196 break; 11197 11198 case DTRACEAGG_COUNT: 11199 agg->dtag_aggregate = dtrace_aggregate_count; 11200 break; 11201 11202 case DTRACEAGG_QUANTIZE: 11203 agg->dtag_aggregate = dtrace_aggregate_quantize; 11204 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 11205 sizeof (uint64_t); 11206 break; 11207 11208 case DTRACEAGG_LQUANTIZE: { 11209 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 11210 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 11211 11212 agg->dtag_initial = desc->dtad_arg; 11213 agg->dtag_aggregate = dtrace_aggregate_lquantize; 11214 11215 if (step == 0 || levels == 0) 11216 goto err; 11217 11218 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 11219 break; 11220 } 11221 11222 case DTRACEAGG_LLQUANTIZE: { 11223 uint16_t factor = DTRACE_LLQUANTIZE_FACTOR(desc->dtad_arg); 11224 uint16_t low = DTRACE_LLQUANTIZE_LOW(desc->dtad_arg); 11225 uint16_t high = DTRACE_LLQUANTIZE_HIGH(desc->dtad_arg); 11226 uint16_t nsteps = DTRACE_LLQUANTIZE_NSTEP(desc->dtad_arg); 11227 int64_t v; 11228 11229 agg->dtag_initial = desc->dtad_arg; 11230 agg->dtag_aggregate = dtrace_aggregate_llquantize; 11231 11232 if (factor < 2 || low >= high || nsteps < factor) 11233 goto err; 11234 11235 /* 11236 * Now check that the number of steps evenly divides a power 11237 * of the factor. (This assures both integer bucket size and 11238 * linearity within each magnitude.) 11239 */ 11240 for (v = factor; v < nsteps; v *= factor) 11241 continue; 11242 11243 if ((v % nsteps) || (nsteps % factor)) 11244 goto err; 11245 11246 size = (dtrace_aggregate_llquantize_bucket(factor, 11247 low, high, nsteps, INT64_MAX) + 2) * sizeof (uint64_t); 11248 break; 11249 } 11250 11251 case DTRACEAGG_AVG: 11252 agg->dtag_aggregate = dtrace_aggregate_avg; 11253 size = sizeof (uint64_t) * 2; 11254 break; 11255 11256 case DTRACEAGG_STDDEV: 11257 agg->dtag_aggregate = dtrace_aggregate_stddev; 11258 size = sizeof (uint64_t) * 4; 11259 break; 11260 11261 case DTRACEAGG_SUM: 11262 agg->dtag_aggregate = dtrace_aggregate_sum; 11263 break; 11264 11265 default: 11266 goto err; 11267 } 11268 11269 agg->dtag_action.dta_rec.dtrd_size = size; 11270 11271 if (ntuple == 0) 11272 goto err; 11273 11274 /* 11275 * We must make sure that we have enough actions for the n-tuple. 11276 */ 11277 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 11278 if (DTRACEACT_ISAGG(act->dta_kind)) 11279 break; 11280 11281 if (--ntuple == 0) { 11282 /* 11283 * This is the action with which our n-tuple begins. 11284 */ 11285 agg->dtag_first = act; 11286 goto success; 11287 } 11288 } 11289 11290 /* 11291 * This n-tuple is short by ntuple elements. Return failure. 11292 */ 11293 ASSERT(ntuple != 0); 11294err: 11295 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11296 return (NULL); 11297 11298success: 11299 /* 11300 * If the last action in the tuple has a size of zero, it's actually 11301 * an expression argument for the aggregating action. 11302 */ 11303 ASSERT(ecb->dte_action_last != NULL); 11304 act = ecb->dte_action_last; 11305 11306 if (act->dta_kind == DTRACEACT_DIFEXPR) { 11307 ASSERT(act->dta_difo != NULL); 11308 11309 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 11310 agg->dtag_hasarg = 1; 11311 } 11312 11313 /* 11314 * We need to allocate an id for this aggregation. 11315 */ 11316#ifdef illumos 11317 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 11318 VM_BESTFIT | VM_SLEEP); 11319#else 11320 aggid = alloc_unr(state->dts_aggid_arena); 11321#endif 11322 11323 if (aggid - 1 >= state->dts_naggregations) { 11324 dtrace_aggregation_t **oaggs = state->dts_aggregations; 11325 dtrace_aggregation_t **aggs; 11326 int naggs = state->dts_naggregations << 1; 11327 int onaggs = state->dts_naggregations; 11328 11329 ASSERT(aggid == state->dts_naggregations + 1); 11330 11331 if (naggs == 0) { 11332 ASSERT(oaggs == NULL); 11333 naggs = 1; 11334 } 11335 11336 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 11337 11338 if (oaggs != NULL) { 11339 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 11340 kmem_free(oaggs, onaggs * sizeof (*aggs)); 11341 } 11342 11343 state->dts_aggregations = aggs; 11344 state->dts_naggregations = naggs; 11345 } 11346 11347 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 11348 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 11349 11350 frec = &agg->dtag_first->dta_rec; 11351 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 11352 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 11353 11354 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 11355 ASSERT(!act->dta_intuple); 11356 act->dta_intuple = 1; 11357 } 11358 11359 return (&agg->dtag_action); 11360} 11361 11362static void 11363dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 11364{ 11365 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 11366 dtrace_state_t *state = ecb->dte_state; 11367 dtrace_aggid_t aggid = agg->dtag_id; 11368 11369 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 11370#ifdef illumos 11371 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 11372#else 11373 free_unr(state->dts_aggid_arena, aggid); 11374#endif 11375 11376 ASSERT(state->dts_aggregations[aggid - 1] == agg); 11377 state->dts_aggregations[aggid - 1] = NULL; 11378 11379 kmem_free(agg, sizeof (dtrace_aggregation_t)); 11380} 11381 11382static int 11383dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 11384{ 11385 dtrace_action_t *action, *last; 11386 dtrace_difo_t *dp = desc->dtad_difo; 11387 uint32_t size = 0, align = sizeof (uint8_t), mask; 11388 uint16_t format = 0; 11389 dtrace_recdesc_t *rec; 11390 dtrace_state_t *state = ecb->dte_state; 11391 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 11392 uint64_t arg = desc->dtad_arg; 11393 11394 ASSERT(MUTEX_HELD(&dtrace_lock)); 11395 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 11396 11397 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 11398 /* 11399 * If this is an aggregating action, there must be neither 11400 * a speculate nor a commit on the action chain. 11401 */ 11402 dtrace_action_t *act; 11403 11404 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 11405 if (act->dta_kind == DTRACEACT_COMMIT) 11406 return (EINVAL); 11407 11408 if (act->dta_kind == DTRACEACT_SPECULATE) 11409 return (EINVAL); 11410 } 11411 11412 action = dtrace_ecb_aggregation_create(ecb, desc); 11413 11414 if (action == NULL) 11415 return (EINVAL); 11416 } else { 11417 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 11418 (desc->dtad_kind == DTRACEACT_DIFEXPR && 11419 dp != NULL && dp->dtdo_destructive)) { 11420 state->dts_destructive = 1; 11421 } 11422 11423 switch (desc->dtad_kind) { 11424 case DTRACEACT_PRINTF: 11425 case DTRACEACT_PRINTA: 11426 case DTRACEACT_SYSTEM: 11427 case DTRACEACT_FREOPEN: 11428 case DTRACEACT_DIFEXPR: 11429 /* 11430 * We know that our arg is a string -- turn it into a 11431 * format. 11432 */ 11433 if (arg == 0) { 11434 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA || 11435 desc->dtad_kind == DTRACEACT_DIFEXPR); 11436 format = 0; 11437 } else { 11438 ASSERT(arg != 0); 11439#ifdef illumos 11440 ASSERT(arg > KERNELBASE); 11441#endif 11442 format = dtrace_format_add(state, 11443 (char *)(uintptr_t)arg); 11444 } 11445 11446 /*FALLTHROUGH*/ 11447 case DTRACEACT_LIBACT: 11448 case DTRACEACT_TRACEMEM: 11449 case DTRACEACT_TRACEMEM_DYNSIZE: 11450 if (dp == NULL) 11451 return (EINVAL); 11452 11453 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 11454 break; 11455 11456 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 11457 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11458 return (EINVAL); 11459 11460 size = opt[DTRACEOPT_STRSIZE]; 11461 } 11462 11463 break; 11464 11465 case DTRACEACT_STACK: 11466 if ((nframes = arg) == 0) { 11467 nframes = opt[DTRACEOPT_STACKFRAMES]; 11468 ASSERT(nframes > 0); 11469 arg = nframes; 11470 } 11471 11472 size = nframes * sizeof (pc_t); 11473 break; 11474 11475 case DTRACEACT_JSTACK: 11476 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 11477 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 11478 11479 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 11480 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 11481 11482 arg = DTRACE_USTACK_ARG(nframes, strsize); 11483 11484 /*FALLTHROUGH*/ 11485 case DTRACEACT_USTACK: 11486 if (desc->dtad_kind != DTRACEACT_JSTACK && 11487 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 11488 strsize = DTRACE_USTACK_STRSIZE(arg); 11489 nframes = opt[DTRACEOPT_USTACKFRAMES]; 11490 ASSERT(nframes > 0); 11491 arg = DTRACE_USTACK_ARG(nframes, strsize); 11492 } 11493 11494 /* 11495 * Save a slot for the pid. 11496 */ 11497 size = (nframes + 1) * sizeof (uint64_t); 11498 size += DTRACE_USTACK_STRSIZE(arg); 11499 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 11500 11501 break; 11502 11503 case DTRACEACT_SYM: 11504 case DTRACEACT_MOD: 11505 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 11506 sizeof (uint64_t)) || 11507 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11508 return (EINVAL); 11509 break; 11510 11511 case DTRACEACT_USYM: 11512 case DTRACEACT_UMOD: 11513 case DTRACEACT_UADDR: 11514 if (dp == NULL || 11515 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 11516 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11517 return (EINVAL); 11518 11519 /* 11520 * We have a slot for the pid, plus a slot for the 11521 * argument. To keep things simple (aligned with 11522 * bitness-neutral sizing), we store each as a 64-bit 11523 * quantity. 11524 */ 11525 size = 2 * sizeof (uint64_t); 11526 break; 11527 11528 case DTRACEACT_STOP: 11529 case DTRACEACT_BREAKPOINT: 11530 case DTRACEACT_PANIC: 11531 break; 11532 11533 case DTRACEACT_CHILL: 11534 case DTRACEACT_DISCARD: 11535 case DTRACEACT_RAISE: 11536 if (dp == NULL) 11537 return (EINVAL); 11538 break; 11539 11540 case DTRACEACT_EXIT: 11541 if (dp == NULL || 11542 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 11543 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 11544 return (EINVAL); 11545 break; 11546 11547 case DTRACEACT_SPECULATE: 11548 if (ecb->dte_size > sizeof (dtrace_rechdr_t)) 11549 return (EINVAL); 11550 11551 if (dp == NULL) 11552 return (EINVAL); 11553 11554 state->dts_speculates = 1; 11555 break; 11556 11557 case DTRACEACT_PRINTM: 11558 size = dp->dtdo_rtype.dtdt_size; 11559 break; 11560 11561 case DTRACEACT_PRINTT: 11562 size = dp->dtdo_rtype.dtdt_size; 11563 break; 11564 11565 case DTRACEACT_COMMIT: { 11566 dtrace_action_t *act = ecb->dte_action; 11567 11568 for (; act != NULL; act = act->dta_next) { 11569 if (act->dta_kind == DTRACEACT_COMMIT) 11570 return (EINVAL); 11571 } 11572 11573 if (dp == NULL) 11574 return (EINVAL); 11575 break; 11576 } 11577 11578 default: 11579 return (EINVAL); 11580 } 11581 11582 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 11583 /* 11584 * If this is a data-storing action or a speculate, 11585 * we must be sure that there isn't a commit on the 11586 * action chain. 11587 */ 11588 dtrace_action_t *act = ecb->dte_action; 11589 11590 for (; act != NULL; act = act->dta_next) { 11591 if (act->dta_kind == DTRACEACT_COMMIT) 11592 return (EINVAL); 11593 } 11594 } 11595 11596 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 11597 action->dta_rec.dtrd_size = size; 11598 } 11599 11600 action->dta_refcnt = 1; 11601 rec = &action->dta_rec; 11602 size = rec->dtrd_size; 11603 11604 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 11605 if (!(size & mask)) { 11606 align = mask + 1; 11607 break; 11608 } 11609 } 11610 11611 action->dta_kind = desc->dtad_kind; 11612 11613 if ((action->dta_difo = dp) != NULL) 11614 dtrace_difo_hold(dp); 11615 11616 rec->dtrd_action = action->dta_kind; 11617 rec->dtrd_arg = arg; 11618 rec->dtrd_uarg = desc->dtad_uarg; 11619 rec->dtrd_alignment = (uint16_t)align; 11620 rec->dtrd_format = format; 11621 11622 if ((last = ecb->dte_action_last) != NULL) { 11623 ASSERT(ecb->dte_action != NULL); 11624 action->dta_prev = last; 11625 last->dta_next = action; 11626 } else { 11627 ASSERT(ecb->dte_action == NULL); 11628 ecb->dte_action = action; 11629 } 11630 11631 ecb->dte_action_last = action; 11632 11633 return (0); 11634} 11635 11636static void 11637dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 11638{ 11639 dtrace_action_t *act = ecb->dte_action, *next; 11640 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 11641 dtrace_difo_t *dp; 11642 uint16_t format; 11643 11644 if (act != NULL && act->dta_refcnt > 1) { 11645 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 11646 act->dta_refcnt--; 11647 } else { 11648 for (; act != NULL; act = next) { 11649 next = act->dta_next; 11650 ASSERT(next != NULL || act == ecb->dte_action_last); 11651 ASSERT(act->dta_refcnt == 1); 11652 11653 if ((format = act->dta_rec.dtrd_format) != 0) 11654 dtrace_format_remove(ecb->dte_state, format); 11655 11656 if ((dp = act->dta_difo) != NULL) 11657 dtrace_difo_release(dp, vstate); 11658 11659 if (DTRACEACT_ISAGG(act->dta_kind)) { 11660 dtrace_ecb_aggregation_destroy(ecb, act); 11661 } else { 11662 kmem_free(act, sizeof (dtrace_action_t)); 11663 } 11664 } 11665 } 11666 11667 ecb->dte_action = NULL; 11668 ecb->dte_action_last = NULL; 11669 ecb->dte_size = 0; 11670} 11671 11672static void 11673dtrace_ecb_disable(dtrace_ecb_t *ecb) 11674{ 11675 /* 11676 * We disable the ECB by removing it from its probe. 11677 */ 11678 dtrace_ecb_t *pecb, *prev = NULL; 11679 dtrace_probe_t *probe = ecb->dte_probe; 11680 11681 ASSERT(MUTEX_HELD(&dtrace_lock)); 11682 11683 if (probe == NULL) { 11684 /* 11685 * This is the NULL probe; there is nothing to disable. 11686 */ 11687 return; 11688 } 11689 11690 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 11691 if (pecb == ecb) 11692 break; 11693 prev = pecb; 11694 } 11695 11696 ASSERT(pecb != NULL); 11697 11698 if (prev == NULL) { 11699 probe->dtpr_ecb = ecb->dte_next; 11700 } else { 11701 prev->dte_next = ecb->dte_next; 11702 } 11703 11704 if (ecb == probe->dtpr_ecb_last) { 11705 ASSERT(ecb->dte_next == NULL); 11706 probe->dtpr_ecb_last = prev; 11707 } 11708 11709 /* 11710 * The ECB has been disconnected from the probe; now sync to assure 11711 * that all CPUs have seen the change before returning. 11712 */ 11713 dtrace_sync(); 11714 11715 if (probe->dtpr_ecb == NULL) { 11716 /* 11717 * That was the last ECB on the probe; clear the predicate 11718 * cache ID for the probe, disable it and sync one more time 11719 * to assure that we'll never hit it again. 11720 */ 11721 dtrace_provider_t *prov = probe->dtpr_provider; 11722 11723 ASSERT(ecb->dte_next == NULL); 11724 ASSERT(probe->dtpr_ecb_last == NULL); 11725 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 11726 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 11727 probe->dtpr_id, probe->dtpr_arg); 11728 dtrace_sync(); 11729 } else { 11730 /* 11731 * There is at least one ECB remaining on the probe. If there 11732 * is _exactly_ one, set the probe's predicate cache ID to be 11733 * the predicate cache ID of the remaining ECB. 11734 */ 11735 ASSERT(probe->dtpr_ecb_last != NULL); 11736 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 11737 11738 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 11739 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 11740 11741 ASSERT(probe->dtpr_ecb->dte_next == NULL); 11742 11743 if (p != NULL) 11744 probe->dtpr_predcache = p->dtp_cacheid; 11745 } 11746 11747 ecb->dte_next = NULL; 11748 } 11749} 11750 11751static void 11752dtrace_ecb_destroy(dtrace_ecb_t *ecb) 11753{ 11754 dtrace_state_t *state = ecb->dte_state; 11755 dtrace_vstate_t *vstate = &state->dts_vstate; 11756 dtrace_predicate_t *pred; 11757 dtrace_epid_t epid = ecb->dte_epid; 11758 11759 ASSERT(MUTEX_HELD(&dtrace_lock)); 11760 ASSERT(ecb->dte_next == NULL); 11761 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 11762 11763 if ((pred = ecb->dte_predicate) != NULL) 11764 dtrace_predicate_release(pred, vstate); 11765 11766 dtrace_ecb_action_remove(ecb); 11767 11768 ASSERT(state->dts_ecbs[epid - 1] == ecb); 11769 state->dts_ecbs[epid - 1] = NULL; 11770 11771 kmem_free(ecb, sizeof (dtrace_ecb_t)); 11772} 11773 11774static dtrace_ecb_t * 11775dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 11776 dtrace_enabling_t *enab) 11777{ 11778 dtrace_ecb_t *ecb; 11779 dtrace_predicate_t *pred; 11780 dtrace_actdesc_t *act; 11781 dtrace_provider_t *prov; 11782 dtrace_ecbdesc_t *desc = enab->dten_current; 11783 11784 ASSERT(MUTEX_HELD(&dtrace_lock)); 11785 ASSERT(state != NULL); 11786 11787 ecb = dtrace_ecb_add(state, probe); 11788 ecb->dte_uarg = desc->dted_uarg; 11789 11790 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 11791 dtrace_predicate_hold(pred); 11792 ecb->dte_predicate = pred; 11793 } 11794 11795 if (probe != NULL) { 11796 /* 11797 * If the provider shows more leg than the consumer is old 11798 * enough to see, we need to enable the appropriate implicit 11799 * predicate bits to prevent the ecb from activating at 11800 * revealing times. 11801 * 11802 * Providers specifying DTRACE_PRIV_USER at register time 11803 * are stating that they need the /proc-style privilege 11804 * model to be enforced, and this is what DTRACE_COND_OWNER 11805 * and DTRACE_COND_ZONEOWNER will then do at probe time. 11806 */ 11807 prov = probe->dtpr_provider; 11808 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 11809 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11810 ecb->dte_cond |= DTRACE_COND_OWNER; 11811 11812 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 11813 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 11814 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 11815 11816 /* 11817 * If the provider shows us kernel innards and the user 11818 * is lacking sufficient privilege, enable the 11819 * DTRACE_COND_USERMODE implicit predicate. 11820 */ 11821 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 11822 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 11823 ecb->dte_cond |= DTRACE_COND_USERMODE; 11824 } 11825 11826 if (dtrace_ecb_create_cache != NULL) { 11827 /* 11828 * If we have a cached ecb, we'll use its action list instead 11829 * of creating our own (saving both time and space). 11830 */ 11831 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 11832 dtrace_action_t *act = cached->dte_action; 11833 11834 if (act != NULL) { 11835 ASSERT(act->dta_refcnt > 0); 11836 act->dta_refcnt++; 11837 ecb->dte_action = act; 11838 ecb->dte_action_last = cached->dte_action_last; 11839 ecb->dte_needed = cached->dte_needed; 11840 ecb->dte_size = cached->dte_size; 11841 ecb->dte_alignment = cached->dte_alignment; 11842 } 11843 11844 return (ecb); 11845 } 11846 11847 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 11848 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 11849 dtrace_ecb_destroy(ecb); 11850 return (NULL); 11851 } 11852 } 11853 11854 dtrace_ecb_resize(ecb); 11855 11856 return (dtrace_ecb_create_cache = ecb); 11857} 11858 11859static int 11860dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 11861{ 11862 dtrace_ecb_t *ecb; 11863 dtrace_enabling_t *enab = arg; 11864 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 11865 11866 ASSERT(state != NULL); 11867 11868 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 11869 /* 11870 * This probe was created in a generation for which this 11871 * enabling has previously created ECBs; we don't want to 11872 * enable it again, so just kick out. 11873 */ 11874 return (DTRACE_MATCH_NEXT); 11875 } 11876 11877 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 11878 return (DTRACE_MATCH_DONE); 11879 11880 dtrace_ecb_enable(ecb); 11881 return (DTRACE_MATCH_NEXT); 11882} 11883 11884static dtrace_ecb_t * 11885dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 11886{ 11887 dtrace_ecb_t *ecb; 11888 11889 ASSERT(MUTEX_HELD(&dtrace_lock)); 11890 11891 if (id == 0 || id > state->dts_necbs) 11892 return (NULL); 11893 11894 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 11895 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 11896 11897 return (state->dts_ecbs[id - 1]); 11898} 11899 11900static dtrace_aggregation_t * 11901dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 11902{ 11903 dtrace_aggregation_t *agg; 11904 11905 ASSERT(MUTEX_HELD(&dtrace_lock)); 11906 11907 if (id == 0 || id > state->dts_naggregations) 11908 return (NULL); 11909 11910 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 11911 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 11912 agg->dtag_id == id); 11913 11914 return (state->dts_aggregations[id - 1]); 11915} 11916 11917/* 11918 * DTrace Buffer Functions 11919 * 11920 * The following functions manipulate DTrace buffers. Most of these functions 11921 * are called in the context of establishing or processing consumer state; 11922 * exceptions are explicitly noted. 11923 */ 11924 11925/* 11926 * Note: called from cross call context. This function switches the two 11927 * buffers on a given CPU. The atomicity of this operation is assured by 11928 * disabling interrupts while the actual switch takes place; the disabling of 11929 * interrupts serializes the execution with any execution of dtrace_probe() on 11930 * the same CPU. 11931 */ 11932static void 11933dtrace_buffer_switch(dtrace_buffer_t *buf) 11934{ 11935 caddr_t tomax = buf->dtb_tomax; 11936 caddr_t xamot = buf->dtb_xamot; 11937 dtrace_icookie_t cookie; 11938 hrtime_t now; 11939 11940 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11941 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 11942 11943 cookie = dtrace_interrupt_disable(); 11944 now = dtrace_gethrtime(); 11945 buf->dtb_tomax = xamot; 11946 buf->dtb_xamot = tomax; 11947 buf->dtb_xamot_drops = buf->dtb_drops; 11948 buf->dtb_xamot_offset = buf->dtb_offset; 11949 buf->dtb_xamot_errors = buf->dtb_errors; 11950 buf->dtb_xamot_flags = buf->dtb_flags; 11951 buf->dtb_offset = 0; 11952 buf->dtb_drops = 0; 11953 buf->dtb_errors = 0; 11954 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 11955 buf->dtb_interval = now - buf->dtb_switched; 11956 buf->dtb_switched = now; 11957 dtrace_interrupt_enable(cookie); 11958} 11959 11960/* 11961 * Note: called from cross call context. This function activates a buffer 11962 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 11963 * is guaranteed by the disabling of interrupts. 11964 */ 11965static void 11966dtrace_buffer_activate(dtrace_state_t *state) 11967{ 11968 dtrace_buffer_t *buf; 11969 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 11970 11971 buf = &state->dts_buffer[curcpu]; 11972 11973 if (buf->dtb_tomax != NULL) { 11974 /* 11975 * We might like to assert that the buffer is marked inactive, 11976 * but this isn't necessarily true: the buffer for the CPU 11977 * that processes the BEGIN probe has its buffer activated 11978 * manually. In this case, we take the (harmless) action 11979 * re-clearing the bit INACTIVE bit. 11980 */ 11981 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 11982 } 11983 11984 dtrace_interrupt_enable(cookie); 11985} 11986 11987#ifdef __FreeBSD__ 11988/* 11989 * Activate the specified per-CPU buffer. This is used instead of 11990 * dtrace_buffer_activate() when APs have not yet started, i.e. when 11991 * activating anonymous state. 11992 */ 11993static void 11994dtrace_buffer_activate_cpu(dtrace_state_t *state, int cpu) 11995{ 11996 11997 if (state->dts_buffer[cpu].dtb_tomax != NULL) 11998 state->dts_buffer[cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 11999} 12000#endif 12001 12002static int 12003dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 12004 processorid_t cpu, int *factor) 12005{ 12006#ifdef illumos 12007 cpu_t *cp; 12008#endif 12009 dtrace_buffer_t *buf; 12010 int allocated = 0, desired = 0; 12011 12012#ifdef illumos 12013 ASSERT(MUTEX_HELD(&cpu_lock)); 12014 ASSERT(MUTEX_HELD(&dtrace_lock)); 12015 12016 *factor = 1; 12017 12018 if (size > dtrace_nonroot_maxsize && 12019 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 12020 return (EFBIG); 12021 12022 cp = cpu_list; 12023 12024 do { 12025 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12026 continue; 12027 12028 buf = &bufs[cp->cpu_id]; 12029 12030 /* 12031 * If there is already a buffer allocated for this CPU, it 12032 * is only possible that this is a DR event. In this case, 12033 */ 12034 if (buf->dtb_tomax != NULL) { 12035 ASSERT(buf->dtb_size == size); 12036 continue; 12037 } 12038 12039 ASSERT(buf->dtb_xamot == NULL); 12040 12041 if ((buf->dtb_tomax = kmem_zalloc(size, 12042 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12043 goto err; 12044 12045 buf->dtb_size = size; 12046 buf->dtb_flags = flags; 12047 buf->dtb_offset = 0; 12048 buf->dtb_drops = 0; 12049 12050 if (flags & DTRACEBUF_NOSWITCH) 12051 continue; 12052 12053 if ((buf->dtb_xamot = kmem_zalloc(size, 12054 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12055 goto err; 12056 } while ((cp = cp->cpu_next) != cpu_list); 12057 12058 return (0); 12059 12060err: 12061 cp = cpu_list; 12062 12063 do { 12064 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 12065 continue; 12066 12067 buf = &bufs[cp->cpu_id]; 12068 desired += 2; 12069 12070 if (buf->dtb_xamot != NULL) { 12071 ASSERT(buf->dtb_tomax != NULL); 12072 ASSERT(buf->dtb_size == size); 12073 kmem_free(buf->dtb_xamot, size); 12074 allocated++; 12075 } 12076 12077 if (buf->dtb_tomax != NULL) { 12078 ASSERT(buf->dtb_size == size); 12079 kmem_free(buf->dtb_tomax, size); 12080 allocated++; 12081 } 12082 12083 buf->dtb_tomax = NULL; 12084 buf->dtb_xamot = NULL; 12085 buf->dtb_size = 0; 12086 } while ((cp = cp->cpu_next) != cpu_list); 12087#else 12088 int i; 12089 12090 *factor = 1; 12091#if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \ 12092 defined(__mips__) || defined(__powerpc__) || defined(__riscv__) 12093 /* 12094 * FreeBSD isn't good at limiting the amount of memory we 12095 * ask to malloc, so let's place a limit here before trying 12096 * to do something that might well end in tears at bedtime. 12097 */ 12098 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 12099 return (ENOMEM); 12100#endif 12101 12102 ASSERT(MUTEX_HELD(&dtrace_lock)); 12103 CPU_FOREACH(i) { 12104 if (cpu != DTRACE_CPUALL && cpu != i) 12105 continue; 12106 12107 buf = &bufs[i]; 12108 12109 /* 12110 * If there is already a buffer allocated for this CPU, it 12111 * is only possible that this is a DR event. In this case, 12112 * the buffer size must match our specified size. 12113 */ 12114 if (buf->dtb_tomax != NULL) { 12115 ASSERT(buf->dtb_size == size); 12116 continue; 12117 } 12118 12119 ASSERT(buf->dtb_xamot == NULL); 12120 12121 if ((buf->dtb_tomax = kmem_zalloc(size, 12122 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12123 goto err; 12124 12125 buf->dtb_size = size; 12126 buf->dtb_flags = flags; 12127 buf->dtb_offset = 0; 12128 buf->dtb_drops = 0; 12129 12130 if (flags & DTRACEBUF_NOSWITCH) 12131 continue; 12132 12133 if ((buf->dtb_xamot = kmem_zalloc(size, 12134 KM_NOSLEEP | KM_NORMALPRI)) == NULL) 12135 goto err; 12136 } 12137 12138 return (0); 12139 12140err: 12141 /* 12142 * Error allocating memory, so free the buffers that were 12143 * allocated before the failed allocation. 12144 */ 12145 CPU_FOREACH(i) { 12146 if (cpu != DTRACE_CPUALL && cpu != i) 12147 continue; 12148 12149 buf = &bufs[i]; 12150 desired += 2; 12151 12152 if (buf->dtb_xamot != NULL) { 12153 ASSERT(buf->dtb_tomax != NULL); 12154 ASSERT(buf->dtb_size == size); 12155 kmem_free(buf->dtb_xamot, size); 12156 allocated++; 12157 } 12158 12159 if (buf->dtb_tomax != NULL) { 12160 ASSERT(buf->dtb_size == size); 12161 kmem_free(buf->dtb_tomax, size); 12162 allocated++; 12163 } 12164 12165 buf->dtb_tomax = NULL; 12166 buf->dtb_xamot = NULL; 12167 buf->dtb_size = 0; 12168 12169 } 12170#endif 12171 *factor = desired / (allocated > 0 ? allocated : 1); 12172 12173 return (ENOMEM); 12174} 12175 12176/* 12177 * Note: called from probe context. This function just increments the drop 12178 * count on a buffer. It has been made a function to allow for the 12179 * possibility of understanding the source of mysterious drop counts. (A 12180 * problem for which one may be particularly disappointed that DTrace cannot 12181 * be used to understand DTrace.) 12182 */ 12183static void 12184dtrace_buffer_drop(dtrace_buffer_t *buf) 12185{ 12186 buf->dtb_drops++; 12187} 12188 12189/* 12190 * Note: called from probe context. This function is called to reserve space 12191 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 12192 * mstate. Returns the new offset in the buffer, or a negative value if an 12193 * error has occurred. 12194 */ 12195static intptr_t 12196dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 12197 dtrace_state_t *state, dtrace_mstate_t *mstate) 12198{ 12199 intptr_t offs = buf->dtb_offset, soffs; 12200 intptr_t woffs; 12201 caddr_t tomax; 12202 size_t total; 12203 12204 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 12205 return (-1); 12206 12207 if ((tomax = buf->dtb_tomax) == NULL) { 12208 dtrace_buffer_drop(buf); 12209 return (-1); 12210 } 12211 12212 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 12213 while (offs & (align - 1)) { 12214 /* 12215 * Assert that our alignment is off by a number which 12216 * is itself sizeof (uint32_t) aligned. 12217 */ 12218 ASSERT(!((align - (offs & (align - 1))) & 12219 (sizeof (uint32_t) - 1))); 12220 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12221 offs += sizeof (uint32_t); 12222 } 12223 12224 if ((soffs = offs + needed) > buf->dtb_size) { 12225 dtrace_buffer_drop(buf); 12226 return (-1); 12227 } 12228 12229 if (mstate == NULL) 12230 return (offs); 12231 12232 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 12233 mstate->dtms_scratch_size = buf->dtb_size - soffs; 12234 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12235 12236 return (offs); 12237 } 12238 12239 if (buf->dtb_flags & DTRACEBUF_FILL) { 12240 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 12241 (buf->dtb_flags & DTRACEBUF_FULL)) 12242 return (-1); 12243 goto out; 12244 } 12245 12246 total = needed + (offs & (align - 1)); 12247 12248 /* 12249 * For a ring buffer, life is quite a bit more complicated. Before 12250 * we can store any padding, we need to adjust our wrapping offset. 12251 * (If we've never before wrapped or we're not about to, no adjustment 12252 * is required.) 12253 */ 12254 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 12255 offs + total > buf->dtb_size) { 12256 woffs = buf->dtb_xamot_offset; 12257 12258 if (offs + total > buf->dtb_size) { 12259 /* 12260 * We can't fit in the end of the buffer. First, a 12261 * sanity check that we can fit in the buffer at all. 12262 */ 12263 if (total > buf->dtb_size) { 12264 dtrace_buffer_drop(buf); 12265 return (-1); 12266 } 12267 12268 /* 12269 * We're going to be storing at the top of the buffer, 12270 * so now we need to deal with the wrapped offset. We 12271 * only reset our wrapped offset to 0 if it is 12272 * currently greater than the current offset. If it 12273 * is less than the current offset, it is because a 12274 * previous allocation induced a wrap -- but the 12275 * allocation didn't subsequently take the space due 12276 * to an error or false predicate evaluation. In this 12277 * case, we'll just leave the wrapped offset alone: if 12278 * the wrapped offset hasn't been advanced far enough 12279 * for this allocation, it will be adjusted in the 12280 * lower loop. 12281 */ 12282 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 12283 if (woffs >= offs) 12284 woffs = 0; 12285 } else { 12286 woffs = 0; 12287 } 12288 12289 /* 12290 * Now we know that we're going to be storing to the 12291 * top of the buffer and that there is room for us 12292 * there. We need to clear the buffer from the current 12293 * offset to the end (there may be old gunk there). 12294 */ 12295 while (offs < buf->dtb_size) 12296 tomax[offs++] = 0; 12297 12298 /* 12299 * We need to set our offset to zero. And because we 12300 * are wrapping, we need to set the bit indicating as 12301 * much. We can also adjust our needed space back 12302 * down to the space required by the ECB -- we know 12303 * that the top of the buffer is aligned. 12304 */ 12305 offs = 0; 12306 total = needed; 12307 buf->dtb_flags |= DTRACEBUF_WRAPPED; 12308 } else { 12309 /* 12310 * There is room for us in the buffer, so we simply 12311 * need to check the wrapped offset. 12312 */ 12313 if (woffs < offs) { 12314 /* 12315 * The wrapped offset is less than the offset. 12316 * This can happen if we allocated buffer space 12317 * that induced a wrap, but then we didn't 12318 * subsequently take the space due to an error 12319 * or false predicate evaluation. This is 12320 * okay; we know that _this_ allocation isn't 12321 * going to induce a wrap. We still can't 12322 * reset the wrapped offset to be zero, 12323 * however: the space may have been trashed in 12324 * the previous failed probe attempt. But at 12325 * least the wrapped offset doesn't need to 12326 * be adjusted at all... 12327 */ 12328 goto out; 12329 } 12330 } 12331 12332 while (offs + total > woffs) { 12333 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 12334 size_t size; 12335 12336 if (epid == DTRACE_EPIDNONE) { 12337 size = sizeof (uint32_t); 12338 } else { 12339 ASSERT3U(epid, <=, state->dts_necbs); 12340 ASSERT(state->dts_ecbs[epid - 1] != NULL); 12341 12342 size = state->dts_ecbs[epid - 1]->dte_size; 12343 } 12344 12345 ASSERT(woffs + size <= buf->dtb_size); 12346 ASSERT(size != 0); 12347 12348 if (woffs + size == buf->dtb_size) { 12349 /* 12350 * We've reached the end of the buffer; we want 12351 * to set the wrapped offset to 0 and break 12352 * out. However, if the offs is 0, then we're 12353 * in a strange edge-condition: the amount of 12354 * space that we want to reserve plus the size 12355 * of the record that we're overwriting is 12356 * greater than the size of the buffer. This 12357 * is problematic because if we reserve the 12358 * space but subsequently don't consume it (due 12359 * to a failed predicate or error) the wrapped 12360 * offset will be 0 -- yet the EPID at offset 0 12361 * will not be committed. This situation is 12362 * relatively easy to deal with: if we're in 12363 * this case, the buffer is indistinguishable 12364 * from one that hasn't wrapped; we need only 12365 * finish the job by clearing the wrapped bit, 12366 * explicitly setting the offset to be 0, and 12367 * zero'ing out the old data in the buffer. 12368 */ 12369 if (offs == 0) { 12370 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 12371 buf->dtb_offset = 0; 12372 woffs = total; 12373 12374 while (woffs < buf->dtb_size) 12375 tomax[woffs++] = 0; 12376 } 12377 12378 woffs = 0; 12379 break; 12380 } 12381 12382 woffs += size; 12383 } 12384 12385 /* 12386 * We have a wrapped offset. It may be that the wrapped offset 12387 * has become zero -- that's okay. 12388 */ 12389 buf->dtb_xamot_offset = woffs; 12390 } 12391 12392out: 12393 /* 12394 * Now we can plow the buffer with any necessary padding. 12395 */ 12396 while (offs & (align - 1)) { 12397 /* 12398 * Assert that our alignment is off by a number which 12399 * is itself sizeof (uint32_t) aligned. 12400 */ 12401 ASSERT(!((align - (offs & (align - 1))) & 12402 (sizeof (uint32_t) - 1))); 12403 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 12404 offs += sizeof (uint32_t); 12405 } 12406 12407 if (buf->dtb_flags & DTRACEBUF_FILL) { 12408 if (offs + needed > buf->dtb_size - state->dts_reserve) { 12409 buf->dtb_flags |= DTRACEBUF_FULL; 12410 return (-1); 12411 } 12412 } 12413 12414 if (mstate == NULL) 12415 return (offs); 12416 12417 /* 12418 * For ring buffers and fill buffers, the scratch space is always 12419 * the inactive buffer. 12420 */ 12421 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 12422 mstate->dtms_scratch_size = buf->dtb_size; 12423 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 12424 12425 return (offs); 12426} 12427 12428static void 12429dtrace_buffer_polish(dtrace_buffer_t *buf) 12430{ 12431 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 12432 ASSERT(MUTEX_HELD(&dtrace_lock)); 12433 12434 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 12435 return; 12436 12437 /* 12438 * We need to polish the ring buffer. There are three cases: 12439 * 12440 * - The first (and presumably most common) is that there is no gap 12441 * between the buffer offset and the wrapped offset. In this case, 12442 * there is nothing in the buffer that isn't valid data; we can 12443 * mark the buffer as polished and return. 12444 * 12445 * - The second (less common than the first but still more common 12446 * than the third) is that there is a gap between the buffer offset 12447 * and the wrapped offset, and the wrapped offset is larger than the 12448 * buffer offset. This can happen because of an alignment issue, or 12449 * can happen because of a call to dtrace_buffer_reserve() that 12450 * didn't subsequently consume the buffer space. In this case, 12451 * we need to zero the data from the buffer offset to the wrapped 12452 * offset. 12453 * 12454 * - The third (and least common) is that there is a gap between the 12455 * buffer offset and the wrapped offset, but the wrapped offset is 12456 * _less_ than the buffer offset. This can only happen because a 12457 * call to dtrace_buffer_reserve() induced a wrap, but the space 12458 * was not subsequently consumed. In this case, we need to zero the 12459 * space from the offset to the end of the buffer _and_ from the 12460 * top of the buffer to the wrapped offset. 12461 */ 12462 if (buf->dtb_offset < buf->dtb_xamot_offset) { 12463 bzero(buf->dtb_tomax + buf->dtb_offset, 12464 buf->dtb_xamot_offset - buf->dtb_offset); 12465 } 12466 12467 if (buf->dtb_offset > buf->dtb_xamot_offset) { 12468 bzero(buf->dtb_tomax + buf->dtb_offset, 12469 buf->dtb_size - buf->dtb_offset); 12470 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 12471 } 12472} 12473 12474/* 12475 * This routine determines if data generated at the specified time has likely 12476 * been entirely consumed at user-level. This routine is called to determine 12477 * if an ECB on a defunct probe (but for an active enabling) can be safely 12478 * disabled and destroyed. 12479 */ 12480static int 12481dtrace_buffer_consumed(dtrace_buffer_t *bufs, hrtime_t when) 12482{ 12483 int i; 12484 12485 for (i = 0; i < NCPU; i++) { 12486 dtrace_buffer_t *buf = &bufs[i]; 12487 12488 if (buf->dtb_size == 0) 12489 continue; 12490 12491 if (buf->dtb_flags & DTRACEBUF_RING) 12492 return (0); 12493 12494 if (!buf->dtb_switched && buf->dtb_offset != 0) 12495 return (0); 12496 12497 if (buf->dtb_switched - buf->dtb_interval < when) 12498 return (0); 12499 } 12500 12501 return (1); 12502} 12503 12504static void 12505dtrace_buffer_free(dtrace_buffer_t *bufs) 12506{ 12507 int i; 12508 12509 for (i = 0; i < NCPU; i++) { 12510 dtrace_buffer_t *buf = &bufs[i]; 12511 12512 if (buf->dtb_tomax == NULL) { 12513 ASSERT(buf->dtb_xamot == NULL); 12514 ASSERT(buf->dtb_size == 0); 12515 continue; 12516 } 12517 12518 if (buf->dtb_xamot != NULL) { 12519 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 12520 kmem_free(buf->dtb_xamot, buf->dtb_size); 12521 } 12522 12523 kmem_free(buf->dtb_tomax, buf->dtb_size); 12524 buf->dtb_size = 0; 12525 buf->dtb_tomax = NULL; 12526 buf->dtb_xamot = NULL; 12527 } 12528} 12529 12530/* 12531 * DTrace Enabling Functions 12532 */ 12533static dtrace_enabling_t * 12534dtrace_enabling_create(dtrace_vstate_t *vstate) 12535{ 12536 dtrace_enabling_t *enab; 12537 12538 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 12539 enab->dten_vstate = vstate; 12540 12541 return (enab); 12542} 12543 12544static void 12545dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 12546{ 12547 dtrace_ecbdesc_t **ndesc; 12548 size_t osize, nsize; 12549 12550 /* 12551 * We can't add to enablings after we've enabled them, or after we've 12552 * retained them. 12553 */ 12554 ASSERT(enab->dten_probegen == 0); 12555 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12556 12557 if (enab->dten_ndesc < enab->dten_maxdesc) { 12558 enab->dten_desc[enab->dten_ndesc++] = ecb; 12559 return; 12560 } 12561 12562 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12563 12564 if (enab->dten_maxdesc == 0) { 12565 enab->dten_maxdesc = 1; 12566 } else { 12567 enab->dten_maxdesc <<= 1; 12568 } 12569 12570 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 12571 12572 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 12573 ndesc = kmem_zalloc(nsize, KM_SLEEP); 12574 bcopy(enab->dten_desc, ndesc, osize); 12575 if (enab->dten_desc != NULL) 12576 kmem_free(enab->dten_desc, osize); 12577 12578 enab->dten_desc = ndesc; 12579 enab->dten_desc[enab->dten_ndesc++] = ecb; 12580} 12581 12582static void 12583dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 12584 dtrace_probedesc_t *pd) 12585{ 12586 dtrace_ecbdesc_t *new; 12587 dtrace_predicate_t *pred; 12588 dtrace_actdesc_t *act; 12589 12590 /* 12591 * We're going to create a new ECB description that matches the 12592 * specified ECB in every way, but has the specified probe description. 12593 */ 12594 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12595 12596 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 12597 dtrace_predicate_hold(pred); 12598 12599 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 12600 dtrace_actdesc_hold(act); 12601 12602 new->dted_action = ecb->dted_action; 12603 new->dted_pred = ecb->dted_pred; 12604 new->dted_probe = *pd; 12605 new->dted_uarg = ecb->dted_uarg; 12606 12607 dtrace_enabling_add(enab, new); 12608} 12609 12610static void 12611dtrace_enabling_dump(dtrace_enabling_t *enab) 12612{ 12613 int i; 12614 12615 for (i = 0; i < enab->dten_ndesc; i++) { 12616 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 12617 12618#ifdef __FreeBSD__ 12619 printf("dtrace: enabling probe %d (%s:%s:%s:%s)\n", i, 12620 desc->dtpd_provider, desc->dtpd_mod, 12621 desc->dtpd_func, desc->dtpd_name); 12622#else 12623 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 12624 desc->dtpd_provider, desc->dtpd_mod, 12625 desc->dtpd_func, desc->dtpd_name); 12626#endif 12627 } 12628} 12629 12630static void 12631dtrace_enabling_destroy(dtrace_enabling_t *enab) 12632{ 12633 int i; 12634 dtrace_ecbdesc_t *ep; 12635 dtrace_vstate_t *vstate = enab->dten_vstate; 12636 12637 ASSERT(MUTEX_HELD(&dtrace_lock)); 12638 12639 for (i = 0; i < enab->dten_ndesc; i++) { 12640 dtrace_actdesc_t *act, *next; 12641 dtrace_predicate_t *pred; 12642 12643 ep = enab->dten_desc[i]; 12644 12645 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 12646 dtrace_predicate_release(pred, vstate); 12647 12648 for (act = ep->dted_action; act != NULL; act = next) { 12649 next = act->dtad_next; 12650 dtrace_actdesc_release(act, vstate); 12651 } 12652 12653 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12654 } 12655 12656 if (enab->dten_desc != NULL) 12657 kmem_free(enab->dten_desc, 12658 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 12659 12660 /* 12661 * If this was a retained enabling, decrement the dts_nretained count 12662 * and take it off of the dtrace_retained list. 12663 */ 12664 if (enab->dten_prev != NULL || enab->dten_next != NULL || 12665 dtrace_retained == enab) { 12666 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12667 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 12668 enab->dten_vstate->dtvs_state->dts_nretained--; 12669 dtrace_retained_gen++; 12670 } 12671 12672 if (enab->dten_prev == NULL) { 12673 if (dtrace_retained == enab) { 12674 dtrace_retained = enab->dten_next; 12675 12676 if (dtrace_retained != NULL) 12677 dtrace_retained->dten_prev = NULL; 12678 } 12679 } else { 12680 ASSERT(enab != dtrace_retained); 12681 ASSERT(dtrace_retained != NULL); 12682 enab->dten_prev->dten_next = enab->dten_next; 12683 } 12684 12685 if (enab->dten_next != NULL) { 12686 ASSERT(dtrace_retained != NULL); 12687 enab->dten_next->dten_prev = enab->dten_prev; 12688 } 12689 12690 kmem_free(enab, sizeof (dtrace_enabling_t)); 12691} 12692 12693static int 12694dtrace_enabling_retain(dtrace_enabling_t *enab) 12695{ 12696 dtrace_state_t *state; 12697 12698 ASSERT(MUTEX_HELD(&dtrace_lock)); 12699 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 12700 ASSERT(enab->dten_vstate != NULL); 12701 12702 state = enab->dten_vstate->dtvs_state; 12703 ASSERT(state != NULL); 12704 12705 /* 12706 * We only allow each state to retain dtrace_retain_max enablings. 12707 */ 12708 if (state->dts_nretained >= dtrace_retain_max) 12709 return (ENOSPC); 12710 12711 state->dts_nretained++; 12712 dtrace_retained_gen++; 12713 12714 if (dtrace_retained == NULL) { 12715 dtrace_retained = enab; 12716 return (0); 12717 } 12718 12719 enab->dten_next = dtrace_retained; 12720 dtrace_retained->dten_prev = enab; 12721 dtrace_retained = enab; 12722 12723 return (0); 12724} 12725 12726static int 12727dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 12728 dtrace_probedesc_t *create) 12729{ 12730 dtrace_enabling_t *new, *enab; 12731 int found = 0, err = ENOENT; 12732 12733 ASSERT(MUTEX_HELD(&dtrace_lock)); 12734 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 12735 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 12736 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 12737 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 12738 12739 new = dtrace_enabling_create(&state->dts_vstate); 12740 12741 /* 12742 * Iterate over all retained enablings, looking for enablings that 12743 * match the specified state. 12744 */ 12745 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12746 int i; 12747 12748 /* 12749 * dtvs_state can only be NULL for helper enablings -- and 12750 * helper enablings can't be retained. 12751 */ 12752 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12753 12754 if (enab->dten_vstate->dtvs_state != state) 12755 continue; 12756 12757 /* 12758 * Now iterate over each probe description; we're looking for 12759 * an exact match to the specified probe description. 12760 */ 12761 for (i = 0; i < enab->dten_ndesc; i++) { 12762 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12763 dtrace_probedesc_t *pd = &ep->dted_probe; 12764 12765 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 12766 continue; 12767 12768 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 12769 continue; 12770 12771 if (strcmp(pd->dtpd_func, match->dtpd_func)) 12772 continue; 12773 12774 if (strcmp(pd->dtpd_name, match->dtpd_name)) 12775 continue; 12776 12777 /* 12778 * We have a winning probe! Add it to our growing 12779 * enabling. 12780 */ 12781 found = 1; 12782 dtrace_enabling_addlike(new, ep, create); 12783 } 12784 } 12785 12786 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 12787 dtrace_enabling_destroy(new); 12788 return (err); 12789 } 12790 12791 return (0); 12792} 12793 12794static void 12795dtrace_enabling_retract(dtrace_state_t *state) 12796{ 12797 dtrace_enabling_t *enab, *next; 12798 12799 ASSERT(MUTEX_HELD(&dtrace_lock)); 12800 12801 /* 12802 * Iterate over all retained enablings, destroy the enablings retained 12803 * for the specified state. 12804 */ 12805 for (enab = dtrace_retained; enab != NULL; enab = next) { 12806 next = enab->dten_next; 12807 12808 /* 12809 * dtvs_state can only be NULL for helper enablings -- and 12810 * helper enablings can't be retained. 12811 */ 12812 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12813 12814 if (enab->dten_vstate->dtvs_state == state) { 12815 ASSERT(state->dts_nretained > 0); 12816 dtrace_enabling_destroy(enab); 12817 } 12818 } 12819 12820 ASSERT(state->dts_nretained == 0); 12821} 12822 12823static int 12824dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 12825{ 12826 int i = 0; 12827 int matched = 0; 12828 12829 ASSERT(MUTEX_HELD(&cpu_lock)); 12830 ASSERT(MUTEX_HELD(&dtrace_lock)); 12831 12832 for (i = 0; i < enab->dten_ndesc; i++) { 12833 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 12834 12835 enab->dten_current = ep; 12836 enab->dten_error = 0; 12837 12838 matched += dtrace_probe_enable(&ep->dted_probe, enab); 12839 12840 if (enab->dten_error != 0) { 12841 /* 12842 * If we get an error half-way through enabling the 12843 * probes, we kick out -- perhaps with some number of 12844 * them enabled. Leaving enabled probes enabled may 12845 * be slightly confusing for user-level, but we expect 12846 * that no one will attempt to actually drive on in 12847 * the face of such errors. If this is an anonymous 12848 * enabling (indicated with a NULL nmatched pointer), 12849 * we cmn_err() a message. We aren't expecting to 12850 * get such an error -- such as it can exist at all, 12851 * it would be a result of corrupted DOF in the driver 12852 * properties. 12853 */ 12854 if (nmatched == NULL) { 12855 cmn_err(CE_WARN, "dtrace_enabling_match() " 12856 "error on %p: %d", (void *)ep, 12857 enab->dten_error); 12858 } 12859 12860 return (enab->dten_error); 12861 } 12862 } 12863 12864 enab->dten_probegen = dtrace_probegen; 12865 if (nmatched != NULL) 12866 *nmatched = matched; 12867 12868 return (0); 12869} 12870 12871static void 12872dtrace_enabling_matchall(void) 12873{ 12874 dtrace_enabling_t *enab; 12875 12876 mutex_enter(&cpu_lock); 12877 mutex_enter(&dtrace_lock); 12878 12879 /* 12880 * Iterate over all retained enablings to see if any probes match 12881 * against them. We only perform this operation on enablings for which 12882 * we have sufficient permissions by virtue of being in the global zone 12883 * or in the same zone as the DTrace client. Because we can be called 12884 * after dtrace_detach() has been called, we cannot assert that there 12885 * are retained enablings. We can safely load from dtrace_retained, 12886 * however: the taskq_destroy() at the end of dtrace_detach() will 12887 * block pending our completion. 12888 */ 12889 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12890#ifdef illumos 12891 cred_t *cr = enab->dten_vstate->dtvs_state->dts_cred.dcr_cred; 12892 12893 if (INGLOBALZONE(curproc) || 12894 cr != NULL && getzoneid() == crgetzoneid(cr)) 12895#endif 12896 (void) dtrace_enabling_match(enab, NULL); 12897 } 12898 12899 mutex_exit(&dtrace_lock); 12900 mutex_exit(&cpu_lock); 12901} 12902 12903/* 12904 * If an enabling is to be enabled without having matched probes (that is, if 12905 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 12906 * enabling must be _primed_ by creating an ECB for every ECB description. 12907 * This must be done to assure that we know the number of speculations, the 12908 * number of aggregations, the minimum buffer size needed, etc. before we 12909 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 12910 * enabling any probes, we create ECBs for every ECB decription, but with a 12911 * NULL probe -- which is exactly what this function does. 12912 */ 12913static void 12914dtrace_enabling_prime(dtrace_state_t *state) 12915{ 12916 dtrace_enabling_t *enab; 12917 int i; 12918 12919 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 12920 ASSERT(enab->dten_vstate->dtvs_state != NULL); 12921 12922 if (enab->dten_vstate->dtvs_state != state) 12923 continue; 12924 12925 /* 12926 * We don't want to prime an enabling more than once, lest 12927 * we allow a malicious user to induce resource exhaustion. 12928 * (The ECBs that result from priming an enabling aren't 12929 * leaked -- but they also aren't deallocated until the 12930 * consumer state is destroyed.) 12931 */ 12932 if (enab->dten_primed) 12933 continue; 12934 12935 for (i = 0; i < enab->dten_ndesc; i++) { 12936 enab->dten_current = enab->dten_desc[i]; 12937 (void) dtrace_probe_enable(NULL, enab); 12938 } 12939 12940 enab->dten_primed = 1; 12941 } 12942} 12943 12944/* 12945 * Called to indicate that probes should be provided due to retained 12946 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 12947 * must take an initial lap through the enabling calling the dtps_provide() 12948 * entry point explicitly to allow for autocreated probes. 12949 */ 12950static void 12951dtrace_enabling_provide(dtrace_provider_t *prv) 12952{ 12953 int i, all = 0; 12954 dtrace_probedesc_t desc; 12955 dtrace_genid_t gen; 12956 12957 ASSERT(MUTEX_HELD(&dtrace_lock)); 12958 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 12959 12960 if (prv == NULL) { 12961 all = 1; 12962 prv = dtrace_provider; 12963 } 12964 12965 do { 12966 dtrace_enabling_t *enab; 12967 void *parg = prv->dtpv_arg; 12968 12969retry: 12970 gen = dtrace_retained_gen; 12971 for (enab = dtrace_retained; enab != NULL; 12972 enab = enab->dten_next) { 12973 for (i = 0; i < enab->dten_ndesc; i++) { 12974 desc = enab->dten_desc[i]->dted_probe; 12975 mutex_exit(&dtrace_lock); 12976 prv->dtpv_pops.dtps_provide(parg, &desc); 12977 mutex_enter(&dtrace_lock); 12978 /* 12979 * Process the retained enablings again if 12980 * they have changed while we weren't holding 12981 * dtrace_lock. 12982 */ 12983 if (gen != dtrace_retained_gen) 12984 goto retry; 12985 } 12986 } 12987 } while (all && (prv = prv->dtpv_next) != NULL); 12988 12989 mutex_exit(&dtrace_lock); 12990 dtrace_probe_provide(NULL, all ? NULL : prv); 12991 mutex_enter(&dtrace_lock); 12992} 12993 12994/* 12995 * Called to reap ECBs that are attached to probes from defunct providers. 12996 */ 12997static void 12998dtrace_enabling_reap(void) 12999{ 13000 dtrace_provider_t *prov; 13001 dtrace_probe_t *probe; 13002 dtrace_ecb_t *ecb; 13003 hrtime_t when; 13004 int i; 13005 13006 mutex_enter(&cpu_lock); 13007 mutex_enter(&dtrace_lock); 13008 13009 for (i = 0; i < dtrace_nprobes; i++) { 13010 if ((probe = dtrace_probes[i]) == NULL) 13011 continue; 13012 13013 if (probe->dtpr_ecb == NULL) 13014 continue; 13015 13016 prov = probe->dtpr_provider; 13017 13018 if ((when = prov->dtpv_defunct) == 0) 13019 continue; 13020 13021 /* 13022 * We have ECBs on a defunct provider: we want to reap these 13023 * ECBs to allow the provider to unregister. The destruction 13024 * of these ECBs must be done carefully: if we destroy the ECB 13025 * and the consumer later wishes to consume an EPID that 13026 * corresponds to the destroyed ECB (and if the EPID metadata 13027 * has not been previously consumed), the consumer will abort 13028 * processing on the unknown EPID. To reduce (but not, sadly, 13029 * eliminate) the possibility of this, we will only destroy an 13030 * ECB for a defunct provider if, for the state that 13031 * corresponds to the ECB: 13032 * 13033 * (a) There is no speculative tracing (which can effectively 13034 * cache an EPID for an arbitrary amount of time). 13035 * 13036 * (b) The principal buffers have been switched twice since the 13037 * provider became defunct. 13038 * 13039 * (c) The aggregation buffers are of zero size or have been 13040 * switched twice since the provider became defunct. 13041 * 13042 * We use dts_speculates to determine (a) and call a function 13043 * (dtrace_buffer_consumed()) to determine (b) and (c). Note 13044 * that as soon as we've been unable to destroy one of the ECBs 13045 * associated with the probe, we quit trying -- reaping is only 13046 * fruitful in as much as we can destroy all ECBs associated 13047 * with the defunct provider's probes. 13048 */ 13049 while ((ecb = probe->dtpr_ecb) != NULL) { 13050 dtrace_state_t *state = ecb->dte_state; 13051 dtrace_buffer_t *buf = state->dts_buffer; 13052 dtrace_buffer_t *aggbuf = state->dts_aggbuffer; 13053 13054 if (state->dts_speculates) 13055 break; 13056 13057 if (!dtrace_buffer_consumed(buf, when)) 13058 break; 13059 13060 if (!dtrace_buffer_consumed(aggbuf, when)) 13061 break; 13062 13063 dtrace_ecb_disable(ecb); 13064 ASSERT(probe->dtpr_ecb != ecb); 13065 dtrace_ecb_destroy(ecb); 13066 } 13067 } 13068 13069 mutex_exit(&dtrace_lock); 13070 mutex_exit(&cpu_lock); 13071} 13072 13073/* 13074 * DTrace DOF Functions 13075 */ 13076/*ARGSUSED*/ 13077static void 13078dtrace_dof_error(dof_hdr_t *dof, const char *str) 13079{ 13080 if (dtrace_err_verbose) 13081 cmn_err(CE_WARN, "failed to process DOF: %s", str); 13082 13083#ifdef DTRACE_ERRDEBUG 13084 dtrace_errdebug(str); 13085#endif 13086} 13087 13088/* 13089 * Create DOF out of a currently enabled state. Right now, we only create 13090 * DOF containing the run-time options -- but this could be expanded to create 13091 * complete DOF representing the enabled state. 13092 */ 13093static dof_hdr_t * 13094dtrace_dof_create(dtrace_state_t *state) 13095{ 13096 dof_hdr_t *dof; 13097 dof_sec_t *sec; 13098 dof_optdesc_t *opt; 13099 int i, len = sizeof (dof_hdr_t) + 13100 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 13101 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13102 13103 ASSERT(MUTEX_HELD(&dtrace_lock)); 13104 13105 dof = kmem_zalloc(len, KM_SLEEP); 13106 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 13107 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 13108 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 13109 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 13110 13111 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 13112 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 13113 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 13114 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 13115 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 13116 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 13117 13118 dof->dofh_flags = 0; 13119 dof->dofh_hdrsize = sizeof (dof_hdr_t); 13120 dof->dofh_secsize = sizeof (dof_sec_t); 13121 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 13122 dof->dofh_secoff = sizeof (dof_hdr_t); 13123 dof->dofh_loadsz = len; 13124 dof->dofh_filesz = len; 13125 dof->dofh_pad = 0; 13126 13127 /* 13128 * Fill in the option section header... 13129 */ 13130 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 13131 sec->dofs_type = DOF_SECT_OPTDESC; 13132 sec->dofs_align = sizeof (uint64_t); 13133 sec->dofs_flags = DOF_SECF_LOAD; 13134 sec->dofs_entsize = sizeof (dof_optdesc_t); 13135 13136 opt = (dof_optdesc_t *)((uintptr_t)sec + 13137 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 13138 13139 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 13140 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 13141 13142 for (i = 0; i < DTRACEOPT_MAX; i++) { 13143 opt[i].dofo_option = i; 13144 opt[i].dofo_strtab = DOF_SECIDX_NONE; 13145 opt[i].dofo_value = state->dts_options[i]; 13146 } 13147 13148 return (dof); 13149} 13150 13151static dof_hdr_t * 13152dtrace_dof_copyin(uintptr_t uarg, int *errp) 13153{ 13154 dof_hdr_t hdr, *dof; 13155 13156 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13157 13158 /* 13159 * First, we're going to copyin() the sizeof (dof_hdr_t). 13160 */ 13161 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 13162 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13163 *errp = EFAULT; 13164 return (NULL); 13165 } 13166 13167 /* 13168 * Now we'll allocate the entire DOF and copy it in -- provided 13169 * that the length isn't outrageous. 13170 */ 13171 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13172 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13173 *errp = E2BIG; 13174 return (NULL); 13175 } 13176 13177 if (hdr.dofh_loadsz < sizeof (hdr)) { 13178 dtrace_dof_error(&hdr, "invalid load size"); 13179 *errp = EINVAL; 13180 return (NULL); 13181 } 13182 13183 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 13184 13185 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0 || 13186 dof->dofh_loadsz != hdr.dofh_loadsz) { 13187 kmem_free(dof, hdr.dofh_loadsz); 13188 *errp = EFAULT; 13189 return (NULL); 13190 } 13191 13192 return (dof); 13193} 13194 13195#ifdef __FreeBSD__ 13196static dof_hdr_t * 13197dtrace_dof_copyin_proc(struct proc *p, uintptr_t uarg, int *errp) 13198{ 13199 dof_hdr_t hdr, *dof; 13200 struct thread *td; 13201 size_t loadsz; 13202 13203 ASSERT(!MUTEX_HELD(&dtrace_lock)); 13204 13205 td = curthread; 13206 13207 /* 13208 * First, we're going to copyin() the sizeof (dof_hdr_t). 13209 */ 13210 if (proc_readmem(td, p, uarg, &hdr, sizeof(hdr)) != sizeof(hdr)) { 13211 dtrace_dof_error(NULL, "failed to copyin DOF header"); 13212 *errp = EFAULT; 13213 return (NULL); 13214 } 13215 13216 /* 13217 * Now we'll allocate the entire DOF and copy it in -- provided 13218 * that the length isn't outrageous. 13219 */ 13220 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 13221 dtrace_dof_error(&hdr, "load size exceeds maximum"); 13222 *errp = E2BIG; 13223 return (NULL); 13224 } 13225 loadsz = (size_t)hdr.dofh_loadsz; 13226 13227 if (loadsz < sizeof (hdr)) { 13228 dtrace_dof_error(&hdr, "invalid load size"); 13229 *errp = EINVAL; 13230 return (NULL); 13231 } 13232 13233 dof = kmem_alloc(loadsz, KM_SLEEP); 13234 13235 if (proc_readmem(td, p, uarg, dof, loadsz) != loadsz || 13236 dof->dofh_loadsz != loadsz) { 13237 kmem_free(dof, hdr.dofh_loadsz); 13238 *errp = EFAULT; 13239 return (NULL); 13240 } 13241 13242 return (dof); 13243} 13244 13245static __inline uchar_t 13246dtrace_dof_char(char c) 13247{ 13248 13249 switch (c) { 13250 case '0': 13251 case '1': 13252 case '2': 13253 case '3': 13254 case '4': 13255 case '5': 13256 case '6': 13257 case '7': 13258 case '8': 13259 case '9': 13260 return (c - '0'); 13261 case 'A': 13262 case 'B': 13263 case 'C': 13264 case 'D': 13265 case 'E': 13266 case 'F': 13267 return (c - 'A' + 10); 13268 case 'a': 13269 case 'b': 13270 case 'c': 13271 case 'd': 13272 case 'e': 13273 case 'f': 13274 return (c - 'a' + 10); 13275 } 13276 /* Should not reach here. */ 13277 return (UCHAR_MAX); 13278} 13279#endif /* __FreeBSD__ */ 13280 13281static dof_hdr_t * 13282dtrace_dof_property(const char *name) 13283{ 13284#ifdef __FreeBSD__ 13285 uint8_t *dofbuf; 13286 u_char *data, *eol; 13287 caddr_t doffile; 13288 size_t bytes, len, i; 13289 dof_hdr_t *dof; 13290 u_char c1, c2; 13291 13292 dof = NULL; 13293 13294 doffile = preload_search_by_type("dtrace_dof"); 13295 if (doffile == NULL) 13296 return (NULL); 13297 13298 data = preload_fetch_addr(doffile); 13299 len = preload_fetch_size(doffile); 13300 for (;;) { 13301 /* Look for the end of the line. All lines end in a newline. */ 13302 eol = memchr(data, '\n', len); 13303 if (eol == NULL) 13304 return (NULL); 13305 13306 if (strncmp(name, data, strlen(name)) == 0) 13307 break; 13308 13309 eol++; /* skip past the newline */ 13310 len -= eol - data; 13311 data = eol; 13312 } 13313 13314 /* We've found the data corresponding to the specified key. */ 13315 13316 data += strlen(name) + 1; /* skip past the '=' */ 13317 len = eol - data; 13318 bytes = len / 2; 13319 13320 if (bytes < sizeof(dof_hdr_t)) { 13321 dtrace_dof_error(NULL, "truncated header"); 13322 goto doferr; 13323 } 13324 13325 /* 13326 * Each byte is represented by the two ASCII characters in its hex 13327 * representation. 13328 */ 13329 dofbuf = malloc(bytes, M_SOLARIS, M_WAITOK); 13330 for (i = 0; i < bytes; i++) { 13331 c1 = dtrace_dof_char(data[i * 2]); 13332 c2 = dtrace_dof_char(data[i * 2 + 1]); 13333 if (c1 == UCHAR_MAX || c2 == UCHAR_MAX) { 13334 dtrace_dof_error(NULL, "invalid hex char in DOF"); 13335 goto doferr; 13336 } 13337 dofbuf[i] = c1 * 16 + c2; 13338 } 13339 13340 dof = (dof_hdr_t *)dofbuf; 13341 if (bytes < dof->dofh_loadsz) { 13342 dtrace_dof_error(NULL, "truncated DOF"); 13343 goto doferr; 13344 } 13345 13346 if (dof->dofh_loadsz >= dtrace_dof_maxsize) { 13347 dtrace_dof_error(NULL, "oversized DOF"); 13348 goto doferr; 13349 } 13350 13351 return (dof); 13352 13353doferr: 13354 free(dof, M_SOLARIS); 13355 return (NULL); 13356#else /* __FreeBSD__ */ 13357 uchar_t *buf; 13358 uint64_t loadsz; 13359 unsigned int len, i; 13360 dof_hdr_t *dof; 13361 13362 /* 13363 * Unfortunately, array of values in .conf files are always (and 13364 * only) interpreted to be integer arrays. We must read our DOF 13365 * as an integer array, and then squeeze it into a byte array. 13366 */ 13367 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 13368 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 13369 return (NULL); 13370 13371 for (i = 0; i < len; i++) 13372 buf[i] = (uchar_t)(((int *)buf)[i]); 13373 13374 if (len < sizeof (dof_hdr_t)) { 13375 ddi_prop_free(buf); 13376 dtrace_dof_error(NULL, "truncated header"); 13377 return (NULL); 13378 } 13379 13380 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 13381 ddi_prop_free(buf); 13382 dtrace_dof_error(NULL, "truncated DOF"); 13383 return (NULL); 13384 } 13385 13386 if (loadsz >= dtrace_dof_maxsize) { 13387 ddi_prop_free(buf); 13388 dtrace_dof_error(NULL, "oversized DOF"); 13389 return (NULL); 13390 } 13391 13392 dof = kmem_alloc(loadsz, KM_SLEEP); 13393 bcopy(buf, dof, loadsz); 13394 ddi_prop_free(buf); 13395 13396 return (dof); 13397#endif /* !__FreeBSD__ */ 13398} 13399 13400static void 13401dtrace_dof_destroy(dof_hdr_t *dof) 13402{ 13403 kmem_free(dof, dof->dofh_loadsz); 13404} 13405 13406/* 13407 * Return the dof_sec_t pointer corresponding to a given section index. If the 13408 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 13409 * a type other than DOF_SECT_NONE is specified, the header is checked against 13410 * this type and NULL is returned if the types do not match. 13411 */ 13412static dof_sec_t * 13413dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 13414{ 13415 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 13416 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 13417 13418 if (i >= dof->dofh_secnum) { 13419 dtrace_dof_error(dof, "referenced section index is invalid"); 13420 return (NULL); 13421 } 13422 13423 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 13424 dtrace_dof_error(dof, "referenced section is not loadable"); 13425 return (NULL); 13426 } 13427 13428 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 13429 dtrace_dof_error(dof, "referenced section is the wrong type"); 13430 return (NULL); 13431 } 13432 13433 return (sec); 13434} 13435 13436static dtrace_probedesc_t * 13437dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 13438{ 13439 dof_probedesc_t *probe; 13440 dof_sec_t *strtab; 13441 uintptr_t daddr = (uintptr_t)dof; 13442 uintptr_t str; 13443 size_t size; 13444 13445 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 13446 dtrace_dof_error(dof, "invalid probe section"); 13447 return (NULL); 13448 } 13449 13450 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13451 dtrace_dof_error(dof, "bad alignment in probe description"); 13452 return (NULL); 13453 } 13454 13455 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 13456 dtrace_dof_error(dof, "truncated probe description"); 13457 return (NULL); 13458 } 13459 13460 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 13461 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 13462 13463 if (strtab == NULL) 13464 return (NULL); 13465 13466 str = daddr + strtab->dofs_offset; 13467 size = strtab->dofs_size; 13468 13469 if (probe->dofp_provider >= strtab->dofs_size) { 13470 dtrace_dof_error(dof, "corrupt probe provider"); 13471 return (NULL); 13472 } 13473 13474 (void) strncpy(desc->dtpd_provider, 13475 (char *)(str + probe->dofp_provider), 13476 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 13477 13478 if (probe->dofp_mod >= strtab->dofs_size) { 13479 dtrace_dof_error(dof, "corrupt probe module"); 13480 return (NULL); 13481 } 13482 13483 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 13484 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 13485 13486 if (probe->dofp_func >= strtab->dofs_size) { 13487 dtrace_dof_error(dof, "corrupt probe function"); 13488 return (NULL); 13489 } 13490 13491 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 13492 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 13493 13494 if (probe->dofp_name >= strtab->dofs_size) { 13495 dtrace_dof_error(dof, "corrupt probe name"); 13496 return (NULL); 13497 } 13498 13499 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 13500 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 13501 13502 return (desc); 13503} 13504 13505static dtrace_difo_t * 13506dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13507 cred_t *cr) 13508{ 13509 dtrace_difo_t *dp; 13510 size_t ttl = 0; 13511 dof_difohdr_t *dofd; 13512 uintptr_t daddr = (uintptr_t)dof; 13513 size_t max = dtrace_difo_maxsize; 13514 int i, l, n; 13515 13516 static const struct { 13517 int section; 13518 int bufoffs; 13519 int lenoffs; 13520 int entsize; 13521 int align; 13522 const char *msg; 13523 } difo[] = { 13524 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 13525 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 13526 sizeof (dif_instr_t), "multiple DIF sections" }, 13527 13528 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 13529 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 13530 sizeof (uint64_t), "multiple integer tables" }, 13531 13532 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 13533 offsetof(dtrace_difo_t, dtdo_strlen), 0, 13534 sizeof (char), "multiple string tables" }, 13535 13536 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 13537 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 13538 sizeof (uint_t), "multiple variable tables" }, 13539 13540 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 13541 }; 13542 13543 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 13544 dtrace_dof_error(dof, "invalid DIFO header section"); 13545 return (NULL); 13546 } 13547 13548 if (sec->dofs_align != sizeof (dof_secidx_t)) { 13549 dtrace_dof_error(dof, "bad alignment in DIFO header"); 13550 return (NULL); 13551 } 13552 13553 if (sec->dofs_size < sizeof (dof_difohdr_t) || 13554 sec->dofs_size % sizeof (dof_secidx_t)) { 13555 dtrace_dof_error(dof, "bad size in DIFO header"); 13556 return (NULL); 13557 } 13558 13559 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13560 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 13561 13562 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 13563 dp->dtdo_rtype = dofd->dofd_rtype; 13564 13565 for (l = 0; l < n; l++) { 13566 dof_sec_t *subsec; 13567 void **bufp; 13568 uint32_t *lenp; 13569 13570 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 13571 dofd->dofd_links[l])) == NULL) 13572 goto err; /* invalid section link */ 13573 13574 if (ttl + subsec->dofs_size > max) { 13575 dtrace_dof_error(dof, "exceeds maximum size"); 13576 goto err; 13577 } 13578 13579 ttl += subsec->dofs_size; 13580 13581 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 13582 if (subsec->dofs_type != difo[i].section) 13583 continue; 13584 13585 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 13586 dtrace_dof_error(dof, "section not loaded"); 13587 goto err; 13588 } 13589 13590 if (subsec->dofs_align != difo[i].align) { 13591 dtrace_dof_error(dof, "bad alignment"); 13592 goto err; 13593 } 13594 13595 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 13596 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 13597 13598 if (*bufp != NULL) { 13599 dtrace_dof_error(dof, difo[i].msg); 13600 goto err; 13601 } 13602 13603 if (difo[i].entsize != subsec->dofs_entsize) { 13604 dtrace_dof_error(dof, "entry size mismatch"); 13605 goto err; 13606 } 13607 13608 if (subsec->dofs_entsize != 0 && 13609 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 13610 dtrace_dof_error(dof, "corrupt entry size"); 13611 goto err; 13612 } 13613 13614 *lenp = subsec->dofs_size; 13615 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 13616 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 13617 *bufp, subsec->dofs_size); 13618 13619 if (subsec->dofs_entsize != 0) 13620 *lenp /= subsec->dofs_entsize; 13621 13622 break; 13623 } 13624 13625 /* 13626 * If we encounter a loadable DIFO sub-section that is not 13627 * known to us, assume this is a broken program and fail. 13628 */ 13629 if (difo[i].section == DOF_SECT_NONE && 13630 (subsec->dofs_flags & DOF_SECF_LOAD)) { 13631 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 13632 goto err; 13633 } 13634 } 13635 13636 if (dp->dtdo_buf == NULL) { 13637 /* 13638 * We can't have a DIF object without DIF text. 13639 */ 13640 dtrace_dof_error(dof, "missing DIF text"); 13641 goto err; 13642 } 13643 13644 /* 13645 * Before we validate the DIF object, run through the variable table 13646 * looking for the strings -- if any of their size are under, we'll set 13647 * their size to be the system-wide default string size. Note that 13648 * this should _not_ happen if the "strsize" option has been set -- 13649 * in this case, the compiler should have set the size to reflect the 13650 * setting of the option. 13651 */ 13652 for (i = 0; i < dp->dtdo_varlen; i++) { 13653 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 13654 dtrace_diftype_t *t = &v->dtdv_type; 13655 13656 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 13657 continue; 13658 13659 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 13660 t->dtdt_size = dtrace_strsize_default; 13661 } 13662 13663 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 13664 goto err; 13665 13666 dtrace_difo_init(dp, vstate); 13667 return (dp); 13668 13669err: 13670 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 13671 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 13672 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 13673 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 13674 13675 kmem_free(dp, sizeof (dtrace_difo_t)); 13676 return (NULL); 13677} 13678 13679static dtrace_predicate_t * 13680dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13681 cred_t *cr) 13682{ 13683 dtrace_difo_t *dp; 13684 13685 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 13686 return (NULL); 13687 13688 return (dtrace_predicate_create(dp)); 13689} 13690 13691static dtrace_actdesc_t * 13692dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13693 cred_t *cr) 13694{ 13695 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 13696 dof_actdesc_t *desc; 13697 dof_sec_t *difosec; 13698 size_t offs; 13699 uintptr_t daddr = (uintptr_t)dof; 13700 uint64_t arg; 13701 dtrace_actkind_t kind; 13702 13703 if (sec->dofs_type != DOF_SECT_ACTDESC) { 13704 dtrace_dof_error(dof, "invalid action section"); 13705 return (NULL); 13706 } 13707 13708 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 13709 dtrace_dof_error(dof, "truncated action description"); 13710 return (NULL); 13711 } 13712 13713 if (sec->dofs_align != sizeof (uint64_t)) { 13714 dtrace_dof_error(dof, "bad alignment in action description"); 13715 return (NULL); 13716 } 13717 13718 if (sec->dofs_size < sec->dofs_entsize) { 13719 dtrace_dof_error(dof, "section entry size exceeds total size"); 13720 return (NULL); 13721 } 13722 13723 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 13724 dtrace_dof_error(dof, "bad entry size in action description"); 13725 return (NULL); 13726 } 13727 13728 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 13729 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 13730 return (NULL); 13731 } 13732 13733 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 13734 desc = (dof_actdesc_t *)(daddr + 13735 (uintptr_t)sec->dofs_offset + offs); 13736 kind = (dtrace_actkind_t)desc->dofa_kind; 13737 13738 if ((DTRACEACT_ISPRINTFLIKE(kind) && 13739 (kind != DTRACEACT_PRINTA || 13740 desc->dofa_strtab != DOF_SECIDX_NONE)) || 13741 (kind == DTRACEACT_DIFEXPR && 13742 desc->dofa_strtab != DOF_SECIDX_NONE)) { 13743 dof_sec_t *strtab; 13744 char *str, *fmt; 13745 uint64_t i; 13746 13747 /* 13748 * The argument to these actions is an index into the 13749 * DOF string table. For printf()-like actions, this 13750 * is the format string. For print(), this is the 13751 * CTF type of the expression result. 13752 */ 13753 if ((strtab = dtrace_dof_sect(dof, 13754 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 13755 goto err; 13756 13757 str = (char *)((uintptr_t)dof + 13758 (uintptr_t)strtab->dofs_offset); 13759 13760 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 13761 if (str[i] == '\0') 13762 break; 13763 } 13764 13765 if (i >= strtab->dofs_size) { 13766 dtrace_dof_error(dof, "bogus format string"); 13767 goto err; 13768 } 13769 13770 if (i == desc->dofa_arg) { 13771 dtrace_dof_error(dof, "empty format string"); 13772 goto err; 13773 } 13774 13775 i -= desc->dofa_arg; 13776 fmt = kmem_alloc(i + 1, KM_SLEEP); 13777 bcopy(&str[desc->dofa_arg], fmt, i + 1); 13778 arg = (uint64_t)(uintptr_t)fmt; 13779 } else { 13780 if (kind == DTRACEACT_PRINTA) { 13781 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 13782 arg = 0; 13783 } else { 13784 arg = desc->dofa_arg; 13785 } 13786 } 13787 13788 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 13789 desc->dofa_uarg, arg); 13790 13791 if (last != NULL) { 13792 last->dtad_next = act; 13793 } else { 13794 first = act; 13795 } 13796 13797 last = act; 13798 13799 if (desc->dofa_difo == DOF_SECIDX_NONE) 13800 continue; 13801 13802 if ((difosec = dtrace_dof_sect(dof, 13803 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 13804 goto err; 13805 13806 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 13807 13808 if (act->dtad_difo == NULL) 13809 goto err; 13810 } 13811 13812 ASSERT(first != NULL); 13813 return (first); 13814 13815err: 13816 for (act = first; act != NULL; act = next) { 13817 next = act->dtad_next; 13818 dtrace_actdesc_release(act, vstate); 13819 } 13820 13821 return (NULL); 13822} 13823 13824static dtrace_ecbdesc_t * 13825dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 13826 cred_t *cr) 13827{ 13828 dtrace_ecbdesc_t *ep; 13829 dof_ecbdesc_t *ecb; 13830 dtrace_probedesc_t *desc; 13831 dtrace_predicate_t *pred = NULL; 13832 13833 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 13834 dtrace_dof_error(dof, "truncated ECB description"); 13835 return (NULL); 13836 } 13837 13838 if (sec->dofs_align != sizeof (uint64_t)) { 13839 dtrace_dof_error(dof, "bad alignment in ECB description"); 13840 return (NULL); 13841 } 13842 13843 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 13844 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 13845 13846 if (sec == NULL) 13847 return (NULL); 13848 13849 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 13850 ep->dted_uarg = ecb->dofe_uarg; 13851 desc = &ep->dted_probe; 13852 13853 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 13854 goto err; 13855 13856 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 13857 if ((sec = dtrace_dof_sect(dof, 13858 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 13859 goto err; 13860 13861 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 13862 goto err; 13863 13864 ep->dted_pred.dtpdd_predicate = pred; 13865 } 13866 13867 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 13868 if ((sec = dtrace_dof_sect(dof, 13869 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 13870 goto err; 13871 13872 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 13873 13874 if (ep->dted_action == NULL) 13875 goto err; 13876 } 13877 13878 return (ep); 13879 13880err: 13881 if (pred != NULL) 13882 dtrace_predicate_release(pred, vstate); 13883 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 13884 return (NULL); 13885} 13886 13887/* 13888 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 13889 * specified DOF. SETX relocations are computed using 'ubase', the base load 13890 * address of the object containing the DOF, and DOFREL relocations are relative 13891 * to the relocation offset within the DOF. 13892 */ 13893static int 13894dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase, 13895 uint64_t udaddr) 13896{ 13897 uintptr_t daddr = (uintptr_t)dof; 13898 uintptr_t ts_end; 13899 dof_relohdr_t *dofr = 13900 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 13901 dof_sec_t *ss, *rs, *ts; 13902 dof_relodesc_t *r; 13903 uint_t i, n; 13904 13905 if (sec->dofs_size < sizeof (dof_relohdr_t) || 13906 sec->dofs_align != sizeof (dof_secidx_t)) { 13907 dtrace_dof_error(dof, "invalid relocation header"); 13908 return (-1); 13909 } 13910 13911 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 13912 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 13913 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 13914 ts_end = (uintptr_t)ts + sizeof (dof_sec_t); 13915 13916 if (ss == NULL || rs == NULL || ts == NULL) 13917 return (-1); /* dtrace_dof_error() has been called already */ 13918 13919 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 13920 rs->dofs_align != sizeof (uint64_t)) { 13921 dtrace_dof_error(dof, "invalid relocation section"); 13922 return (-1); 13923 } 13924 13925 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 13926 n = rs->dofs_size / rs->dofs_entsize; 13927 13928 for (i = 0; i < n; i++) { 13929 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 13930 13931 switch (r->dofr_type) { 13932 case DOF_RELO_NONE: 13933 break; 13934 case DOF_RELO_SETX: 13935 case DOF_RELO_DOFREL: 13936 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 13937 sizeof (uint64_t) > ts->dofs_size) { 13938 dtrace_dof_error(dof, "bad relocation offset"); 13939 return (-1); 13940 } 13941 13942 if (taddr >= (uintptr_t)ts && taddr < ts_end) { 13943 dtrace_dof_error(dof, "bad relocation offset"); 13944 return (-1); 13945 } 13946 13947 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 13948 dtrace_dof_error(dof, "misaligned setx relo"); 13949 return (-1); 13950 } 13951 13952 if (r->dofr_type == DOF_RELO_SETX) 13953 *(uint64_t *)taddr += ubase; 13954 else 13955 *(uint64_t *)taddr += 13956 udaddr + ts->dofs_offset + r->dofr_offset; 13957 break; 13958 default: 13959 dtrace_dof_error(dof, "invalid relocation type"); 13960 return (-1); 13961 } 13962 13963 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 13964 } 13965 13966 return (0); 13967} 13968 13969/* 13970 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 13971 * header: it should be at the front of a memory region that is at least 13972 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 13973 * size. It need not be validated in any other way. 13974 */ 13975static int 13976dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 13977 dtrace_enabling_t **enabp, uint64_t ubase, uint64_t udaddr, int noprobes) 13978{ 13979 uint64_t len = dof->dofh_loadsz, seclen; 13980 uintptr_t daddr = (uintptr_t)dof; 13981 dtrace_ecbdesc_t *ep; 13982 dtrace_enabling_t *enab; 13983 uint_t i; 13984 13985 ASSERT(MUTEX_HELD(&dtrace_lock)); 13986 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 13987 13988 /* 13989 * Check the DOF header identification bytes. In addition to checking 13990 * valid settings, we also verify that unused bits/bytes are zeroed so 13991 * we can use them later without fear of regressing existing binaries. 13992 */ 13993 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 13994 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 13995 dtrace_dof_error(dof, "DOF magic string mismatch"); 13996 return (-1); 13997 } 13998 13999 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 14000 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 14001 dtrace_dof_error(dof, "DOF has invalid data model"); 14002 return (-1); 14003 } 14004 14005 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 14006 dtrace_dof_error(dof, "DOF encoding mismatch"); 14007 return (-1); 14008 } 14009 14010 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14011 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 14012 dtrace_dof_error(dof, "DOF version mismatch"); 14013 return (-1); 14014 } 14015 14016 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 14017 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 14018 return (-1); 14019 } 14020 14021 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 14022 dtrace_dof_error(dof, "DOF uses too many integer registers"); 14023 return (-1); 14024 } 14025 14026 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 14027 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 14028 return (-1); 14029 } 14030 14031 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 14032 if (dof->dofh_ident[i] != 0) { 14033 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 14034 return (-1); 14035 } 14036 } 14037 14038 if (dof->dofh_flags & ~DOF_FL_VALID) { 14039 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 14040 return (-1); 14041 } 14042 14043 if (dof->dofh_secsize == 0) { 14044 dtrace_dof_error(dof, "zero section header size"); 14045 return (-1); 14046 } 14047 14048 /* 14049 * Check that the section headers don't exceed the amount of DOF 14050 * data. Note that we cast the section size and number of sections 14051 * to uint64_t's to prevent possible overflow in the multiplication. 14052 */ 14053 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 14054 14055 if (dof->dofh_secoff > len || seclen > len || 14056 dof->dofh_secoff + seclen > len) { 14057 dtrace_dof_error(dof, "truncated section headers"); 14058 return (-1); 14059 } 14060 14061 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 14062 dtrace_dof_error(dof, "misaligned section headers"); 14063 return (-1); 14064 } 14065 14066 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 14067 dtrace_dof_error(dof, "misaligned section size"); 14068 return (-1); 14069 } 14070 14071 /* 14072 * Take an initial pass through the section headers to be sure that 14073 * the headers don't have stray offsets. If the 'noprobes' flag is 14074 * set, do not permit sections relating to providers, probes, or args. 14075 */ 14076 for (i = 0; i < dof->dofh_secnum; i++) { 14077 dof_sec_t *sec = (dof_sec_t *)(daddr + 14078 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14079 14080 if (noprobes) { 14081 switch (sec->dofs_type) { 14082 case DOF_SECT_PROVIDER: 14083 case DOF_SECT_PROBES: 14084 case DOF_SECT_PRARGS: 14085 case DOF_SECT_PROFFS: 14086 dtrace_dof_error(dof, "illegal sections " 14087 "for enabling"); 14088 return (-1); 14089 } 14090 } 14091 14092 if (DOF_SEC_ISLOADABLE(sec->dofs_type) && 14093 !(sec->dofs_flags & DOF_SECF_LOAD)) { 14094 dtrace_dof_error(dof, "loadable section with load " 14095 "flag unset"); 14096 return (-1); 14097 } 14098 14099 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14100 continue; /* just ignore non-loadable sections */ 14101 14102 if (!ISP2(sec->dofs_align)) { 14103 dtrace_dof_error(dof, "bad section alignment"); 14104 return (-1); 14105 } 14106 14107 if (sec->dofs_offset & (sec->dofs_align - 1)) { 14108 dtrace_dof_error(dof, "misaligned section"); 14109 return (-1); 14110 } 14111 14112 if (sec->dofs_offset > len || sec->dofs_size > len || 14113 sec->dofs_offset + sec->dofs_size > len) { 14114 dtrace_dof_error(dof, "corrupt section header"); 14115 return (-1); 14116 } 14117 14118 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 14119 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 14120 dtrace_dof_error(dof, "non-terminating string table"); 14121 return (-1); 14122 } 14123 } 14124 14125 /* 14126 * Take a second pass through the sections and locate and perform any 14127 * relocations that are present. We do this after the first pass to 14128 * be sure that all sections have had their headers validated. 14129 */ 14130 for (i = 0; i < dof->dofh_secnum; i++) { 14131 dof_sec_t *sec = (dof_sec_t *)(daddr + 14132 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14133 14134 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 14135 continue; /* skip sections that are not loadable */ 14136 14137 switch (sec->dofs_type) { 14138 case DOF_SECT_URELHDR: 14139 if (dtrace_dof_relocate(dof, sec, ubase, udaddr) != 0) 14140 return (-1); 14141 break; 14142 } 14143 } 14144 14145 if ((enab = *enabp) == NULL) 14146 enab = *enabp = dtrace_enabling_create(vstate); 14147 14148 for (i = 0; i < dof->dofh_secnum; i++) { 14149 dof_sec_t *sec = (dof_sec_t *)(daddr + 14150 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14151 14152 if (sec->dofs_type != DOF_SECT_ECBDESC) 14153 continue; 14154 14155 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 14156 dtrace_enabling_destroy(enab); 14157 *enabp = NULL; 14158 return (-1); 14159 } 14160 14161 dtrace_enabling_add(enab, ep); 14162 } 14163 14164 return (0); 14165} 14166 14167/* 14168 * Process DOF for any options. This routine assumes that the DOF has been 14169 * at least processed by dtrace_dof_slurp(). 14170 */ 14171static int 14172dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 14173{ 14174 int i, rval; 14175 uint32_t entsize; 14176 size_t offs; 14177 dof_optdesc_t *desc; 14178 14179 for (i = 0; i < dof->dofh_secnum; i++) { 14180 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 14181 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 14182 14183 if (sec->dofs_type != DOF_SECT_OPTDESC) 14184 continue; 14185 14186 if (sec->dofs_align != sizeof (uint64_t)) { 14187 dtrace_dof_error(dof, "bad alignment in " 14188 "option description"); 14189 return (EINVAL); 14190 } 14191 14192 if ((entsize = sec->dofs_entsize) == 0) { 14193 dtrace_dof_error(dof, "zeroed option entry size"); 14194 return (EINVAL); 14195 } 14196 14197 if (entsize < sizeof (dof_optdesc_t)) { 14198 dtrace_dof_error(dof, "bad option entry size"); 14199 return (EINVAL); 14200 } 14201 14202 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 14203 desc = (dof_optdesc_t *)((uintptr_t)dof + 14204 (uintptr_t)sec->dofs_offset + offs); 14205 14206 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 14207 dtrace_dof_error(dof, "non-zero option string"); 14208 return (EINVAL); 14209 } 14210 14211 if (desc->dofo_value == DTRACEOPT_UNSET) { 14212 dtrace_dof_error(dof, "unset option"); 14213 return (EINVAL); 14214 } 14215 14216 if ((rval = dtrace_state_option(state, 14217 desc->dofo_option, desc->dofo_value)) != 0) { 14218 dtrace_dof_error(dof, "rejected option"); 14219 return (rval); 14220 } 14221 } 14222 } 14223 14224 return (0); 14225} 14226 14227/* 14228 * DTrace Consumer State Functions 14229 */ 14230static int 14231dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 14232{ 14233 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 14234 void *base; 14235 uintptr_t limit; 14236 dtrace_dynvar_t *dvar, *next, *start; 14237 int i; 14238 14239 ASSERT(MUTEX_HELD(&dtrace_lock)); 14240 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 14241 14242 bzero(dstate, sizeof (dtrace_dstate_t)); 14243 14244 if ((dstate->dtds_chunksize = chunksize) == 0) 14245 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 14246 14247 VERIFY(dstate->dtds_chunksize < LONG_MAX); 14248 14249 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 14250 size = min; 14251 14252 if ((base = kmem_zalloc(size, KM_NOSLEEP | KM_NORMALPRI)) == NULL) 14253 return (ENOMEM); 14254 14255 dstate->dtds_size = size; 14256 dstate->dtds_base = base; 14257 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 14258 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 14259 14260 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 14261 14262 if (hashsize != 1 && (hashsize & 1)) 14263 hashsize--; 14264 14265 dstate->dtds_hashsize = hashsize; 14266 dstate->dtds_hash = dstate->dtds_base; 14267 14268 /* 14269 * Set all of our hash buckets to point to the single sink, and (if 14270 * it hasn't already been set), set the sink's hash value to be the 14271 * sink sentinel value. The sink is needed for dynamic variable 14272 * lookups to know that they have iterated over an entire, valid hash 14273 * chain. 14274 */ 14275 for (i = 0; i < hashsize; i++) 14276 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 14277 14278 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 14279 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 14280 14281 /* 14282 * Determine number of active CPUs. Divide free list evenly among 14283 * active CPUs. 14284 */ 14285 start = (dtrace_dynvar_t *) 14286 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 14287 limit = (uintptr_t)base + size; 14288 14289 VERIFY((uintptr_t)start < limit); 14290 VERIFY((uintptr_t)start >= (uintptr_t)base); 14291 14292 maxper = (limit - (uintptr_t)start) / NCPU; 14293 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 14294 14295#ifndef illumos 14296 CPU_FOREACH(i) { 14297#else 14298 for (i = 0; i < NCPU; i++) { 14299#endif 14300 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 14301 14302 /* 14303 * If we don't even have enough chunks to make it once through 14304 * NCPUs, we're just going to allocate everything to the first 14305 * CPU. And if we're on the last CPU, we're going to allocate 14306 * whatever is left over. In either case, we set the limit to 14307 * be the limit of the dynamic variable space. 14308 */ 14309 if (maxper == 0 || i == NCPU - 1) { 14310 limit = (uintptr_t)base + size; 14311 start = NULL; 14312 } else { 14313 limit = (uintptr_t)start + maxper; 14314 start = (dtrace_dynvar_t *)limit; 14315 } 14316 14317 VERIFY(limit <= (uintptr_t)base + size); 14318 14319 for (;;) { 14320 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 14321 dstate->dtds_chunksize); 14322 14323 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 14324 break; 14325 14326 VERIFY((uintptr_t)dvar >= (uintptr_t)base && 14327 (uintptr_t)dvar <= (uintptr_t)base + size); 14328 dvar->dtdv_next = next; 14329 dvar = next; 14330 } 14331 14332 if (maxper == 0) 14333 break; 14334 } 14335 14336 return (0); 14337} 14338 14339static void 14340dtrace_dstate_fini(dtrace_dstate_t *dstate) 14341{ 14342 ASSERT(MUTEX_HELD(&cpu_lock)); 14343 14344 if (dstate->dtds_base == NULL) 14345 return; 14346 14347 kmem_free(dstate->dtds_base, dstate->dtds_size); 14348 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 14349} 14350 14351static void 14352dtrace_vstate_fini(dtrace_vstate_t *vstate) 14353{ 14354 /* 14355 * Logical XOR, where are you? 14356 */ 14357 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 14358 14359 if (vstate->dtvs_nglobals > 0) { 14360 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 14361 sizeof (dtrace_statvar_t *)); 14362 } 14363 14364 if (vstate->dtvs_ntlocals > 0) { 14365 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 14366 sizeof (dtrace_difv_t)); 14367 } 14368 14369 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 14370 14371 if (vstate->dtvs_nlocals > 0) { 14372 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 14373 sizeof (dtrace_statvar_t *)); 14374 } 14375} 14376 14377#ifdef illumos 14378static void 14379dtrace_state_clean(dtrace_state_t *state) 14380{ 14381 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14382 return; 14383 14384 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14385 dtrace_speculation_clean(state); 14386} 14387 14388static void 14389dtrace_state_deadman(dtrace_state_t *state) 14390{ 14391 hrtime_t now; 14392 14393 dtrace_sync(); 14394 14395 now = dtrace_gethrtime(); 14396 14397 if (state != dtrace_anon.dta_state && 14398 now - state->dts_laststatus >= dtrace_deadman_user) 14399 return; 14400 14401 /* 14402 * We must be sure that dts_alive never appears to be less than the 14403 * value upon entry to dtrace_state_deadman(), and because we lack a 14404 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14405 * store INT64_MAX to it, followed by a memory barrier, followed by 14406 * the new value. This assures that dts_alive never appears to be 14407 * less than its true value, regardless of the order in which the 14408 * stores to the underlying storage are issued. 14409 */ 14410 state->dts_alive = INT64_MAX; 14411 dtrace_membar_producer(); 14412 state->dts_alive = now; 14413} 14414#else /* !illumos */ 14415static void 14416dtrace_state_clean(void *arg) 14417{ 14418 dtrace_state_t *state = arg; 14419 dtrace_optval_t *opt = state->dts_options; 14420 14421 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 14422 return; 14423 14424 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 14425 dtrace_speculation_clean(state); 14426 14427 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 14428 dtrace_state_clean, state); 14429} 14430 14431static void 14432dtrace_state_deadman(void *arg) 14433{ 14434 dtrace_state_t *state = arg; 14435 hrtime_t now; 14436 14437 dtrace_sync(); 14438 14439 dtrace_debug_output(); 14440 14441 now = dtrace_gethrtime(); 14442 14443 if (state != dtrace_anon.dta_state && 14444 now - state->dts_laststatus >= dtrace_deadman_user) 14445 return; 14446 14447 /* 14448 * We must be sure that dts_alive never appears to be less than the 14449 * value upon entry to dtrace_state_deadman(), and because we lack a 14450 * dtrace_cas64(), we cannot store to it atomically. We thus instead 14451 * store INT64_MAX to it, followed by a memory barrier, followed by 14452 * the new value. This assures that dts_alive never appears to be 14453 * less than its true value, regardless of the order in which the 14454 * stores to the underlying storage are issued. 14455 */ 14456 state->dts_alive = INT64_MAX; 14457 dtrace_membar_producer(); 14458 state->dts_alive = now; 14459 14460 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 14461 dtrace_state_deadman, state); 14462} 14463#endif /* illumos */ 14464 14465static dtrace_state_t * 14466#ifdef illumos 14467dtrace_state_create(dev_t *devp, cred_t *cr) 14468#else 14469dtrace_state_create(struct cdev *dev, struct ucred *cred __unused) 14470#endif 14471{ 14472#ifdef illumos 14473 minor_t minor; 14474 major_t major; 14475#else 14476 cred_t *cr = NULL; 14477 int m = 0; 14478#endif 14479 char c[30]; 14480 dtrace_state_t *state; 14481 dtrace_optval_t *opt; 14482 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 14483 int cpu_it; 14484 14485 ASSERT(MUTEX_HELD(&dtrace_lock)); 14486 ASSERT(MUTEX_HELD(&cpu_lock)); 14487 14488#ifdef illumos 14489 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 14490 VM_BESTFIT | VM_SLEEP); 14491 14492 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 14493 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 14494 return (NULL); 14495 } 14496 14497 state = ddi_get_soft_state(dtrace_softstate, minor); 14498#else 14499 if (dev != NULL) { 14500 cr = dev->si_cred; 14501 m = dev2unit(dev); 14502 } 14503 14504 /* Allocate memory for the state. */ 14505 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 14506#endif 14507 14508 state->dts_epid = DTRACE_EPIDNONE + 1; 14509 14510 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 14511#ifdef illumos 14512 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 14513 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 14514 14515 if (devp != NULL) { 14516 major = getemajor(*devp); 14517 } else { 14518 major = ddi_driver_major(dtrace_devi); 14519 } 14520 14521 state->dts_dev = makedevice(major, minor); 14522 14523 if (devp != NULL) 14524 *devp = state->dts_dev; 14525#else 14526 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 14527 state->dts_dev = dev; 14528#endif 14529 14530 /* 14531 * We allocate NCPU buffers. On the one hand, this can be quite 14532 * a bit of memory per instance (nearly 36K on a Starcat). On the 14533 * other hand, it saves an additional memory reference in the probe 14534 * path. 14535 */ 14536 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 14537 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 14538 14539 /* 14540 * Allocate and initialise the per-process per-CPU random state. 14541 * SI_SUB_RANDOM < SI_SUB_DTRACE_ANON therefore entropy device is 14542 * assumed to be seeded at this point (if from Fortuna seed file). 14543 */ 14544 (void) read_random(&state->dts_rstate[0], 2 * sizeof(uint64_t)); 14545 for (cpu_it = 1; cpu_it < NCPU; cpu_it++) { 14546 /* 14547 * Each CPU is assigned a 2^64 period, non-overlapping 14548 * subsequence. 14549 */ 14550 dtrace_xoroshiro128_plus_jump(state->dts_rstate[cpu_it-1], 14551 state->dts_rstate[cpu_it]); 14552 } 14553 14554#ifdef illumos 14555 state->dts_cleaner = CYCLIC_NONE; 14556 state->dts_deadman = CYCLIC_NONE; 14557#else 14558 callout_init(&state->dts_cleaner, 1); 14559 callout_init(&state->dts_deadman, 1); 14560#endif 14561 state->dts_vstate.dtvs_state = state; 14562 14563 for (i = 0; i < DTRACEOPT_MAX; i++) 14564 state->dts_options[i] = DTRACEOPT_UNSET; 14565 14566 /* 14567 * Set the default options. 14568 */ 14569 opt = state->dts_options; 14570 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 14571 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 14572 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 14573 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 14574 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 14575 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 14576 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 14577 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 14578 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 14579 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 14580 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 14581 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 14582 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 14583 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 14584 14585 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 14586 14587 /* 14588 * Depending on the user credentials, we set flag bits which alter probe 14589 * visibility or the amount of destructiveness allowed. In the case of 14590 * actual anonymous tracing, or the possession of all privileges, all of 14591 * the normal checks are bypassed. 14592 */ 14593 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 14594 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 14595 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 14596 } else { 14597 /* 14598 * Set up the credentials for this instantiation. We take a 14599 * hold on the credential to prevent it from disappearing on 14600 * us; this in turn prevents the zone_t referenced by this 14601 * credential from disappearing. This means that we can 14602 * examine the credential and the zone from probe context. 14603 */ 14604 crhold(cr); 14605 state->dts_cred.dcr_cred = cr; 14606 14607 /* 14608 * CRA_PROC means "we have *some* privilege for dtrace" and 14609 * unlocks the use of variables like pid, zonename, etc. 14610 */ 14611 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 14612 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14613 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 14614 } 14615 14616 /* 14617 * dtrace_user allows use of syscall and profile providers. 14618 * If the user also has proc_owner and/or proc_zone, we 14619 * extend the scope to include additional visibility and 14620 * destructive power. 14621 */ 14622 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 14623 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 14624 state->dts_cred.dcr_visible |= 14625 DTRACE_CRV_ALLPROC; 14626 14627 state->dts_cred.dcr_action |= 14628 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14629 } 14630 14631 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 14632 state->dts_cred.dcr_visible |= 14633 DTRACE_CRV_ALLZONE; 14634 14635 state->dts_cred.dcr_action |= 14636 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14637 } 14638 14639 /* 14640 * If we have all privs in whatever zone this is, 14641 * we can do destructive things to processes which 14642 * have altered credentials. 14643 */ 14644#ifdef illumos 14645 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14646 cr->cr_zone->zone_privset)) { 14647 state->dts_cred.dcr_action |= 14648 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14649 } 14650#endif 14651 } 14652 14653 /* 14654 * Holding the dtrace_kernel privilege also implies that 14655 * the user has the dtrace_user privilege from a visibility 14656 * perspective. But without further privileges, some 14657 * destructive actions are not available. 14658 */ 14659 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 14660 /* 14661 * Make all probes in all zones visible. However, 14662 * this doesn't mean that all actions become available 14663 * to all zones. 14664 */ 14665 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 14666 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 14667 14668 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 14669 DTRACE_CRA_PROC; 14670 /* 14671 * Holding proc_owner means that destructive actions 14672 * for *this* zone are allowed. 14673 */ 14674 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14675 state->dts_cred.dcr_action |= 14676 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14677 14678 /* 14679 * Holding proc_zone means that destructive actions 14680 * for this user/group ID in all zones is allowed. 14681 */ 14682 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14683 state->dts_cred.dcr_action |= 14684 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14685 14686#ifdef illumos 14687 /* 14688 * If we have all privs in whatever zone this is, 14689 * we can do destructive things to processes which 14690 * have altered credentials. 14691 */ 14692 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 14693 cr->cr_zone->zone_privset)) { 14694 state->dts_cred.dcr_action |= 14695 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 14696 } 14697#endif 14698 } 14699 14700 /* 14701 * Holding the dtrace_proc privilege gives control over fasttrap 14702 * and pid providers. We need to grant wider destructive 14703 * privileges in the event that the user has proc_owner and/or 14704 * proc_zone. 14705 */ 14706 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 14707 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 14708 state->dts_cred.dcr_action |= 14709 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 14710 14711 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 14712 state->dts_cred.dcr_action |= 14713 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 14714 } 14715 } 14716 14717 return (state); 14718} 14719 14720static int 14721dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 14722{ 14723 dtrace_optval_t *opt = state->dts_options, size; 14724 processorid_t cpu = 0;; 14725 int flags = 0, rval, factor, divisor = 1; 14726 14727 ASSERT(MUTEX_HELD(&dtrace_lock)); 14728 ASSERT(MUTEX_HELD(&cpu_lock)); 14729 ASSERT(which < DTRACEOPT_MAX); 14730 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 14731 (state == dtrace_anon.dta_state && 14732 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 14733 14734 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 14735 return (0); 14736 14737 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 14738 cpu = opt[DTRACEOPT_CPU]; 14739 14740 if (which == DTRACEOPT_SPECSIZE) 14741 flags |= DTRACEBUF_NOSWITCH; 14742 14743 if (which == DTRACEOPT_BUFSIZE) { 14744 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 14745 flags |= DTRACEBUF_RING; 14746 14747 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 14748 flags |= DTRACEBUF_FILL; 14749 14750 if (state != dtrace_anon.dta_state || 14751 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14752 flags |= DTRACEBUF_INACTIVE; 14753 } 14754 14755 for (size = opt[which]; size >= sizeof (uint64_t); size /= divisor) { 14756 /* 14757 * The size must be 8-byte aligned. If the size is not 8-byte 14758 * aligned, drop it down by the difference. 14759 */ 14760 if (size & (sizeof (uint64_t) - 1)) 14761 size -= size & (sizeof (uint64_t) - 1); 14762 14763 if (size < state->dts_reserve) { 14764 /* 14765 * Buffers always must be large enough to accommodate 14766 * their prereserved space. We return E2BIG instead 14767 * of ENOMEM in this case to allow for user-level 14768 * software to differentiate the cases. 14769 */ 14770 return (E2BIG); 14771 } 14772 14773 rval = dtrace_buffer_alloc(buf, size, flags, cpu, &factor); 14774 14775 if (rval != ENOMEM) { 14776 opt[which] = size; 14777 return (rval); 14778 } 14779 14780 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 14781 return (rval); 14782 14783 for (divisor = 2; divisor < factor; divisor <<= 1) 14784 continue; 14785 } 14786 14787 return (ENOMEM); 14788} 14789 14790static int 14791dtrace_state_buffers(dtrace_state_t *state) 14792{ 14793 dtrace_speculation_t *spec = state->dts_speculations; 14794 int rval, i; 14795 14796 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 14797 DTRACEOPT_BUFSIZE)) != 0) 14798 return (rval); 14799 14800 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 14801 DTRACEOPT_AGGSIZE)) != 0) 14802 return (rval); 14803 14804 for (i = 0; i < state->dts_nspeculations; i++) { 14805 if ((rval = dtrace_state_buffer(state, 14806 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 14807 return (rval); 14808 } 14809 14810 return (0); 14811} 14812 14813static void 14814dtrace_state_prereserve(dtrace_state_t *state) 14815{ 14816 dtrace_ecb_t *ecb; 14817 dtrace_probe_t *probe; 14818 14819 state->dts_reserve = 0; 14820 14821 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 14822 return; 14823 14824 /* 14825 * If our buffer policy is a "fill" buffer policy, we need to set the 14826 * prereserved space to be the space required by the END probes. 14827 */ 14828 probe = dtrace_probes[dtrace_probeid_end - 1]; 14829 ASSERT(probe != NULL); 14830 14831 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 14832 if (ecb->dte_state != state) 14833 continue; 14834 14835 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 14836 } 14837} 14838 14839static int 14840dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 14841{ 14842 dtrace_optval_t *opt = state->dts_options, sz, nspec; 14843 dtrace_speculation_t *spec; 14844 dtrace_buffer_t *buf; 14845#ifdef illumos 14846 cyc_handler_t hdlr; 14847 cyc_time_t when; 14848#endif 14849 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 14850 dtrace_icookie_t cookie; 14851 14852 mutex_enter(&cpu_lock); 14853 mutex_enter(&dtrace_lock); 14854 14855 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 14856 rval = EBUSY; 14857 goto out; 14858 } 14859 14860 /* 14861 * Before we can perform any checks, we must prime all of the 14862 * retained enablings that correspond to this state. 14863 */ 14864 dtrace_enabling_prime(state); 14865 14866 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 14867 rval = EACCES; 14868 goto out; 14869 } 14870 14871 dtrace_state_prereserve(state); 14872 14873 /* 14874 * Now we want to do is try to allocate our speculations. 14875 * We do not automatically resize the number of speculations; if 14876 * this fails, we will fail the operation. 14877 */ 14878 nspec = opt[DTRACEOPT_NSPEC]; 14879 ASSERT(nspec != DTRACEOPT_UNSET); 14880 14881 if (nspec > INT_MAX) { 14882 rval = ENOMEM; 14883 goto out; 14884 } 14885 14886 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), 14887 KM_NOSLEEP | KM_NORMALPRI); 14888 14889 if (spec == NULL) { 14890 rval = ENOMEM; 14891 goto out; 14892 } 14893 14894 state->dts_speculations = spec; 14895 state->dts_nspeculations = (int)nspec; 14896 14897 for (i = 0; i < nspec; i++) { 14898 if ((buf = kmem_zalloc(bufsize, 14899 KM_NOSLEEP | KM_NORMALPRI)) == NULL) { 14900 rval = ENOMEM; 14901 goto err; 14902 } 14903 14904 spec[i].dtsp_buffer = buf; 14905 } 14906 14907 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 14908 if (dtrace_anon.dta_state == NULL) { 14909 rval = ENOENT; 14910 goto out; 14911 } 14912 14913 if (state->dts_necbs != 0) { 14914 rval = EALREADY; 14915 goto out; 14916 } 14917 14918 state->dts_anon = dtrace_anon_grab(); 14919 ASSERT(state->dts_anon != NULL); 14920 state = state->dts_anon; 14921 14922 /* 14923 * We want "grabanon" to be set in the grabbed state, so we'll 14924 * copy that option value from the grabbing state into the 14925 * grabbed state. 14926 */ 14927 state->dts_options[DTRACEOPT_GRABANON] = 14928 opt[DTRACEOPT_GRABANON]; 14929 14930 *cpu = dtrace_anon.dta_beganon; 14931 14932 /* 14933 * If the anonymous state is active (as it almost certainly 14934 * is if the anonymous enabling ultimately matched anything), 14935 * we don't allow any further option processing -- but we 14936 * don't return failure. 14937 */ 14938 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 14939 goto out; 14940 } 14941 14942 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 14943 opt[DTRACEOPT_AGGSIZE] != 0) { 14944 if (state->dts_aggregations == NULL) { 14945 /* 14946 * We're not going to create an aggregation buffer 14947 * because we don't have any ECBs that contain 14948 * aggregations -- set this option to 0. 14949 */ 14950 opt[DTRACEOPT_AGGSIZE] = 0; 14951 } else { 14952 /* 14953 * If we have an aggregation buffer, we must also have 14954 * a buffer to use as scratch. 14955 */ 14956 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 14957 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 14958 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 14959 } 14960 } 14961 } 14962 14963 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 14964 opt[DTRACEOPT_SPECSIZE] != 0) { 14965 if (!state->dts_speculates) { 14966 /* 14967 * We're not going to create speculation buffers 14968 * because we don't have any ECBs that actually 14969 * speculate -- set the speculation size to 0. 14970 */ 14971 opt[DTRACEOPT_SPECSIZE] = 0; 14972 } 14973 } 14974 14975 /* 14976 * The bare minimum size for any buffer that we're actually going to 14977 * do anything to is sizeof (uint64_t). 14978 */ 14979 sz = sizeof (uint64_t); 14980 14981 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 14982 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 14983 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 14984 /* 14985 * A buffer size has been explicitly set to 0 (or to a size 14986 * that will be adjusted to 0) and we need the space -- we 14987 * need to return failure. We return ENOSPC to differentiate 14988 * it from failing to allocate a buffer due to failure to meet 14989 * the reserve (for which we return E2BIG). 14990 */ 14991 rval = ENOSPC; 14992 goto out; 14993 } 14994 14995 if ((rval = dtrace_state_buffers(state)) != 0) 14996 goto err; 14997 14998 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 14999 sz = dtrace_dstate_defsize; 15000 15001 do { 15002 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 15003 15004 if (rval == 0) 15005 break; 15006 15007 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 15008 goto err; 15009 } while (sz >>= 1); 15010 15011 opt[DTRACEOPT_DYNVARSIZE] = sz; 15012 15013 if (rval != 0) 15014 goto err; 15015 15016 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 15017 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 15018 15019 if (opt[DTRACEOPT_CLEANRATE] == 0) 15020 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15021 15022 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 15023 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 15024 15025 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 15026 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 15027 15028 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 15029#ifdef illumos 15030 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 15031 hdlr.cyh_arg = state; 15032 hdlr.cyh_level = CY_LOW_LEVEL; 15033 15034 when.cyt_when = 0; 15035 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 15036 15037 state->dts_cleaner = cyclic_add(&hdlr, &when); 15038 15039 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 15040 hdlr.cyh_arg = state; 15041 hdlr.cyh_level = CY_LOW_LEVEL; 15042 15043 when.cyt_when = 0; 15044 when.cyt_interval = dtrace_deadman_interval; 15045 15046 state->dts_deadman = cyclic_add(&hdlr, &when); 15047#else 15048 callout_reset(&state->dts_cleaner, hz * opt[DTRACEOPT_CLEANRATE] / NANOSEC, 15049 dtrace_state_clean, state); 15050 callout_reset(&state->dts_deadman, hz * dtrace_deadman_interval / NANOSEC, 15051 dtrace_state_deadman, state); 15052#endif 15053 15054 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 15055 15056#ifdef illumos 15057 if (state->dts_getf != 0 && 15058 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15059 /* 15060 * We don't have kernel privs but we have at least one call 15061 * to getf(); we need to bump our zone's count, and (if 15062 * this is the first enabling to have an unprivileged call 15063 * to getf()) we need to hook into closef(). 15064 */ 15065 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf++; 15066 15067 if (dtrace_getf++ == 0) { 15068 ASSERT(dtrace_closef == NULL); 15069 dtrace_closef = dtrace_getf_barrier; 15070 } 15071 } 15072#endif 15073 15074 /* 15075 * Now it's time to actually fire the BEGIN probe. We need to disable 15076 * interrupts here both to record the CPU on which we fired the BEGIN 15077 * probe (the data from this CPU will be processed first at user 15078 * level) and to manually activate the buffer for this CPU. 15079 */ 15080 cookie = dtrace_interrupt_disable(); 15081 *cpu = curcpu; 15082 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 15083 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 15084 15085 dtrace_probe(dtrace_probeid_begin, 15086 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15087 dtrace_interrupt_enable(cookie); 15088 /* 15089 * We may have had an exit action from a BEGIN probe; only change our 15090 * state to ACTIVE if we're still in WARMUP. 15091 */ 15092 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 15093 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 15094 15095 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 15096 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 15097 15098#ifdef __FreeBSD__ 15099 /* 15100 * We enable anonymous tracing before APs are started, so we must 15101 * activate buffers using the current CPU. 15102 */ 15103 if (state == dtrace_anon.dta_state) 15104 for (int i = 0; i < NCPU; i++) 15105 dtrace_buffer_activate_cpu(state, i); 15106 else 15107 dtrace_xcall(DTRACE_CPUALL, 15108 (dtrace_xcall_t)dtrace_buffer_activate, state); 15109#else 15110 /* 15111 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 15112 * want each CPU to transition its principal buffer out of the 15113 * INACTIVE state. Doing this assures that no CPU will suddenly begin 15114 * processing an ECB halfway down a probe's ECB chain; all CPUs will 15115 * atomically transition from processing none of a state's ECBs to 15116 * processing all of them. 15117 */ 15118 dtrace_xcall(DTRACE_CPUALL, 15119 (dtrace_xcall_t)dtrace_buffer_activate, state); 15120#endif 15121 goto out; 15122 15123err: 15124 dtrace_buffer_free(state->dts_buffer); 15125 dtrace_buffer_free(state->dts_aggbuffer); 15126 15127 if ((nspec = state->dts_nspeculations) == 0) { 15128 ASSERT(state->dts_speculations == NULL); 15129 goto out; 15130 } 15131 15132 spec = state->dts_speculations; 15133 ASSERT(spec != NULL); 15134 15135 for (i = 0; i < state->dts_nspeculations; i++) { 15136 if ((buf = spec[i].dtsp_buffer) == NULL) 15137 break; 15138 15139 dtrace_buffer_free(buf); 15140 kmem_free(buf, bufsize); 15141 } 15142 15143 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15144 state->dts_nspeculations = 0; 15145 state->dts_speculations = NULL; 15146 15147out: 15148 mutex_exit(&dtrace_lock); 15149 mutex_exit(&cpu_lock); 15150 15151 return (rval); 15152} 15153 15154static int 15155dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 15156{ 15157 dtrace_icookie_t cookie; 15158 15159 ASSERT(MUTEX_HELD(&dtrace_lock)); 15160 15161 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 15162 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 15163 return (EINVAL); 15164 15165 /* 15166 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 15167 * to be sure that every CPU has seen it. See below for the details 15168 * on why this is done. 15169 */ 15170 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 15171 dtrace_sync(); 15172 15173 /* 15174 * By this point, it is impossible for any CPU to be still processing 15175 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 15176 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 15177 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 15178 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 15179 * iff we're in the END probe. 15180 */ 15181 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 15182 dtrace_sync(); 15183 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 15184 15185 /* 15186 * Finally, we can release the reserve and call the END probe. We 15187 * disable interrupts across calling the END probe to allow us to 15188 * return the CPU on which we actually called the END probe. This 15189 * allows user-land to be sure that this CPU's principal buffer is 15190 * processed last. 15191 */ 15192 state->dts_reserve = 0; 15193 15194 cookie = dtrace_interrupt_disable(); 15195 *cpu = curcpu; 15196 dtrace_probe(dtrace_probeid_end, 15197 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 15198 dtrace_interrupt_enable(cookie); 15199 15200 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 15201 dtrace_sync(); 15202 15203#ifdef illumos 15204 if (state->dts_getf != 0 && 15205 !(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL)) { 15206 /* 15207 * We don't have kernel privs but we have at least one call 15208 * to getf(); we need to lower our zone's count, and (if 15209 * this is the last enabling to have an unprivileged call 15210 * to getf()) we need to clear the closef() hook. 15211 */ 15212 ASSERT(state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf > 0); 15213 ASSERT(dtrace_closef == dtrace_getf_barrier); 15214 ASSERT(dtrace_getf > 0); 15215 15216 state->dts_cred.dcr_cred->cr_zone->zone_dtrace_getf--; 15217 15218 if (--dtrace_getf == 0) 15219 dtrace_closef = NULL; 15220 } 15221#endif 15222 15223 return (0); 15224} 15225 15226static int 15227dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 15228 dtrace_optval_t val) 15229{ 15230 ASSERT(MUTEX_HELD(&dtrace_lock)); 15231 15232 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 15233 return (EBUSY); 15234 15235 if (option >= DTRACEOPT_MAX) 15236 return (EINVAL); 15237 15238 if (option != DTRACEOPT_CPU && val < 0) 15239 return (EINVAL); 15240 15241 switch (option) { 15242 case DTRACEOPT_DESTRUCTIVE: 15243 if (dtrace_destructive_disallow) 15244 return (EACCES); 15245 15246 state->dts_cred.dcr_destructive = 1; 15247 break; 15248 15249 case DTRACEOPT_BUFSIZE: 15250 case DTRACEOPT_DYNVARSIZE: 15251 case DTRACEOPT_AGGSIZE: 15252 case DTRACEOPT_SPECSIZE: 15253 case DTRACEOPT_STRSIZE: 15254 if (val < 0) 15255 return (EINVAL); 15256 15257 if (val >= LONG_MAX) { 15258 /* 15259 * If this is an otherwise negative value, set it to 15260 * the highest multiple of 128m less than LONG_MAX. 15261 * Technically, we're adjusting the size without 15262 * regard to the buffer resizing policy, but in fact, 15263 * this has no effect -- if we set the buffer size to 15264 * ~LONG_MAX and the buffer policy is ultimately set to 15265 * be "manual", the buffer allocation is guaranteed to 15266 * fail, if only because the allocation requires two 15267 * buffers. (We set the the size to the highest 15268 * multiple of 128m because it ensures that the size 15269 * will remain a multiple of a megabyte when 15270 * repeatedly halved -- all the way down to 15m.) 15271 */ 15272 val = LONG_MAX - (1 << 27) + 1; 15273 } 15274 } 15275 15276 state->dts_options[option] = val; 15277 15278 return (0); 15279} 15280 15281static void 15282dtrace_state_destroy(dtrace_state_t *state) 15283{ 15284 dtrace_ecb_t *ecb; 15285 dtrace_vstate_t *vstate = &state->dts_vstate; 15286#ifdef illumos 15287 minor_t minor = getminor(state->dts_dev); 15288#endif 15289 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 15290 dtrace_speculation_t *spec = state->dts_speculations; 15291 int nspec = state->dts_nspeculations; 15292 uint32_t match; 15293 15294 ASSERT(MUTEX_HELD(&dtrace_lock)); 15295 ASSERT(MUTEX_HELD(&cpu_lock)); 15296 15297 /* 15298 * First, retract any retained enablings for this state. 15299 */ 15300 dtrace_enabling_retract(state); 15301 ASSERT(state->dts_nretained == 0); 15302 15303 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 15304 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 15305 /* 15306 * We have managed to come into dtrace_state_destroy() on a 15307 * hot enabling -- almost certainly because of a disorderly 15308 * shutdown of a consumer. (That is, a consumer that is 15309 * exiting without having called dtrace_stop().) In this case, 15310 * we're going to set our activity to be KILLED, and then 15311 * issue a sync to be sure that everyone is out of probe 15312 * context before we start blowing away ECBs. 15313 */ 15314 state->dts_activity = DTRACE_ACTIVITY_KILLED; 15315 dtrace_sync(); 15316 } 15317 15318 /* 15319 * Release the credential hold we took in dtrace_state_create(). 15320 */ 15321 if (state->dts_cred.dcr_cred != NULL) 15322 crfree(state->dts_cred.dcr_cred); 15323 15324 /* 15325 * Now we can safely disable and destroy any enabled probes. Because 15326 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 15327 * (especially if they're all enabled), we take two passes through the 15328 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 15329 * in the second we disable whatever is left over. 15330 */ 15331 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 15332 for (i = 0; i < state->dts_necbs; i++) { 15333 if ((ecb = state->dts_ecbs[i]) == NULL) 15334 continue; 15335 15336 if (match && ecb->dte_probe != NULL) { 15337 dtrace_probe_t *probe = ecb->dte_probe; 15338 dtrace_provider_t *prov = probe->dtpr_provider; 15339 15340 if (!(prov->dtpv_priv.dtpp_flags & match)) 15341 continue; 15342 } 15343 15344 dtrace_ecb_disable(ecb); 15345 dtrace_ecb_destroy(ecb); 15346 } 15347 15348 if (!match) 15349 break; 15350 } 15351 15352 /* 15353 * Before we free the buffers, perform one more sync to assure that 15354 * every CPU is out of probe context. 15355 */ 15356 dtrace_sync(); 15357 15358 dtrace_buffer_free(state->dts_buffer); 15359 dtrace_buffer_free(state->dts_aggbuffer); 15360 15361 for (i = 0; i < nspec; i++) 15362 dtrace_buffer_free(spec[i].dtsp_buffer); 15363 15364#ifdef illumos 15365 if (state->dts_cleaner != CYCLIC_NONE) 15366 cyclic_remove(state->dts_cleaner); 15367 15368 if (state->dts_deadman != CYCLIC_NONE) 15369 cyclic_remove(state->dts_deadman); 15370#else 15371 callout_stop(&state->dts_cleaner); 15372 callout_drain(&state->dts_cleaner); 15373 callout_stop(&state->dts_deadman); 15374 callout_drain(&state->dts_deadman); 15375#endif 15376 15377 dtrace_dstate_fini(&vstate->dtvs_dynvars); 15378 dtrace_vstate_fini(vstate); 15379 if (state->dts_ecbs != NULL) 15380 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 15381 15382 if (state->dts_aggregations != NULL) { 15383#ifdef DEBUG 15384 for (i = 0; i < state->dts_naggregations; i++) 15385 ASSERT(state->dts_aggregations[i] == NULL); 15386#endif 15387 ASSERT(state->dts_naggregations > 0); 15388 kmem_free(state->dts_aggregations, 15389 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 15390 } 15391 15392 kmem_free(state->dts_buffer, bufsize); 15393 kmem_free(state->dts_aggbuffer, bufsize); 15394 15395 for (i = 0; i < nspec; i++) 15396 kmem_free(spec[i].dtsp_buffer, bufsize); 15397 15398 if (spec != NULL) 15399 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 15400 15401 dtrace_format_destroy(state); 15402 15403 if (state->dts_aggid_arena != NULL) { 15404#ifdef illumos 15405 vmem_destroy(state->dts_aggid_arena); 15406#else 15407 delete_unrhdr(state->dts_aggid_arena); 15408#endif 15409 state->dts_aggid_arena = NULL; 15410 } 15411#ifdef illumos 15412 ddi_soft_state_free(dtrace_softstate, minor); 15413 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 15414#endif 15415} 15416 15417/* 15418 * DTrace Anonymous Enabling Functions 15419 */ 15420static dtrace_state_t * 15421dtrace_anon_grab(void) 15422{ 15423 dtrace_state_t *state; 15424 15425 ASSERT(MUTEX_HELD(&dtrace_lock)); 15426 15427 if ((state = dtrace_anon.dta_state) == NULL) { 15428 ASSERT(dtrace_anon.dta_enabling == NULL); 15429 return (NULL); 15430 } 15431 15432 ASSERT(dtrace_anon.dta_enabling != NULL); 15433 ASSERT(dtrace_retained != NULL); 15434 15435 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 15436 dtrace_anon.dta_enabling = NULL; 15437 dtrace_anon.dta_state = NULL; 15438 15439 return (state); 15440} 15441 15442static void 15443dtrace_anon_property(void) 15444{ 15445 int i, rv; 15446 dtrace_state_t *state; 15447 dof_hdr_t *dof; 15448 char c[32]; /* enough for "dof-data-" + digits */ 15449 15450 ASSERT(MUTEX_HELD(&dtrace_lock)); 15451 ASSERT(MUTEX_HELD(&cpu_lock)); 15452 15453 for (i = 0; ; i++) { 15454 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 15455 15456 dtrace_err_verbose = 1; 15457 15458 if ((dof = dtrace_dof_property(c)) == NULL) { 15459 dtrace_err_verbose = 0; 15460 break; 15461 } 15462 15463#ifdef illumos 15464 /* 15465 * We want to create anonymous state, so we need to transition 15466 * the kernel debugger to indicate that DTrace is active. If 15467 * this fails (e.g. because the debugger has modified text in 15468 * some way), we won't continue with the processing. 15469 */ 15470 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15471 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 15472 "enabling ignored."); 15473 dtrace_dof_destroy(dof); 15474 break; 15475 } 15476#endif 15477 15478 /* 15479 * If we haven't allocated an anonymous state, we'll do so now. 15480 */ 15481 if ((state = dtrace_anon.dta_state) == NULL) { 15482 state = dtrace_state_create(NULL, NULL); 15483 dtrace_anon.dta_state = state; 15484 15485 if (state == NULL) { 15486 /* 15487 * This basically shouldn't happen: the only 15488 * failure mode from dtrace_state_create() is a 15489 * failure of ddi_soft_state_zalloc() that 15490 * itself should never happen. Still, the 15491 * interface allows for a failure mode, and 15492 * we want to fail as gracefully as possible: 15493 * we'll emit an error message and cease 15494 * processing anonymous state in this case. 15495 */ 15496 cmn_err(CE_WARN, "failed to create " 15497 "anonymous state"); 15498 dtrace_dof_destroy(dof); 15499 break; 15500 } 15501 } 15502 15503 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 15504 &dtrace_anon.dta_enabling, 0, 0, B_TRUE); 15505 15506 if (rv == 0) 15507 rv = dtrace_dof_options(dof, state); 15508 15509 dtrace_err_verbose = 0; 15510 dtrace_dof_destroy(dof); 15511 15512 if (rv != 0) { 15513 /* 15514 * This is malformed DOF; chuck any anonymous state 15515 * that we created. 15516 */ 15517 ASSERT(dtrace_anon.dta_enabling == NULL); 15518 dtrace_state_destroy(state); 15519 dtrace_anon.dta_state = NULL; 15520 break; 15521 } 15522 15523 ASSERT(dtrace_anon.dta_enabling != NULL); 15524 } 15525 15526 if (dtrace_anon.dta_enabling != NULL) { 15527 int rval; 15528 15529 /* 15530 * dtrace_enabling_retain() can only fail because we are 15531 * trying to retain more enablings than are allowed -- but 15532 * we only have one anonymous enabling, and we are guaranteed 15533 * to be allowed at least one retained enabling; we assert 15534 * that dtrace_enabling_retain() returns success. 15535 */ 15536 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 15537 ASSERT(rval == 0); 15538 15539 dtrace_enabling_dump(dtrace_anon.dta_enabling); 15540 } 15541} 15542 15543/* 15544 * DTrace Helper Functions 15545 */ 15546static void 15547dtrace_helper_trace(dtrace_helper_action_t *helper, 15548 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 15549{ 15550 uint32_t size, next, nnext, i; 15551 dtrace_helptrace_t *ent, *buffer; 15552 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 15553 15554 if ((buffer = dtrace_helptrace_buffer) == NULL) 15555 return; 15556 15557 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 15558 15559 /* 15560 * What would a tracing framework be without its own tracing 15561 * framework? (Well, a hell of a lot simpler, for starters...) 15562 */ 15563 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 15564 sizeof (uint64_t) - sizeof (uint64_t); 15565 15566 /* 15567 * Iterate until we can allocate a slot in the trace buffer. 15568 */ 15569 do { 15570 next = dtrace_helptrace_next; 15571 15572 if (next + size < dtrace_helptrace_bufsize) { 15573 nnext = next + size; 15574 } else { 15575 nnext = size; 15576 } 15577 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 15578 15579 /* 15580 * We have our slot; fill it in. 15581 */ 15582 if (nnext == size) { 15583 dtrace_helptrace_wrapped++; 15584 next = 0; 15585 } 15586 15587 ent = (dtrace_helptrace_t *)((uintptr_t)buffer + next); 15588 ent->dtht_helper = helper; 15589 ent->dtht_where = where; 15590 ent->dtht_nlocals = vstate->dtvs_nlocals; 15591 15592 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 15593 mstate->dtms_fltoffs : -1; 15594 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 15595 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 15596 15597 for (i = 0; i < vstate->dtvs_nlocals; i++) { 15598 dtrace_statvar_t *svar; 15599 15600 if ((svar = vstate->dtvs_locals[i]) == NULL) 15601 continue; 15602 15603 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 15604 ent->dtht_locals[i] = 15605 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 15606 } 15607} 15608 15609static uint64_t 15610dtrace_helper(int which, dtrace_mstate_t *mstate, 15611 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 15612{ 15613 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 15614 uint64_t sarg0 = mstate->dtms_arg[0]; 15615 uint64_t sarg1 = mstate->dtms_arg[1]; 15616 uint64_t rval = 0; 15617 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 15618 dtrace_helper_action_t *helper; 15619 dtrace_vstate_t *vstate; 15620 dtrace_difo_t *pred; 15621 int i, trace = dtrace_helptrace_buffer != NULL; 15622 15623 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 15624 15625 if (helpers == NULL) 15626 return (0); 15627 15628 if ((helper = helpers->dthps_actions[which]) == NULL) 15629 return (0); 15630 15631 vstate = &helpers->dthps_vstate; 15632 mstate->dtms_arg[0] = arg0; 15633 mstate->dtms_arg[1] = arg1; 15634 15635 /* 15636 * Now iterate over each helper. If its predicate evaluates to 'true', 15637 * we'll call the corresponding actions. Note that the below calls 15638 * to dtrace_dif_emulate() may set faults in machine state. This is 15639 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 15640 * the stored DIF offset with its own (which is the desired behavior). 15641 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 15642 * from machine state; this is okay, too. 15643 */ 15644 for (; helper != NULL; helper = helper->dtha_next) { 15645 if ((pred = helper->dtha_predicate) != NULL) { 15646 if (trace) 15647 dtrace_helper_trace(helper, mstate, vstate, 0); 15648 15649 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 15650 goto next; 15651 15652 if (*flags & CPU_DTRACE_FAULT) 15653 goto err; 15654 } 15655 15656 for (i = 0; i < helper->dtha_nactions; i++) { 15657 if (trace) 15658 dtrace_helper_trace(helper, 15659 mstate, vstate, i + 1); 15660 15661 rval = dtrace_dif_emulate(helper->dtha_actions[i], 15662 mstate, vstate, state); 15663 15664 if (*flags & CPU_DTRACE_FAULT) 15665 goto err; 15666 } 15667 15668next: 15669 if (trace) 15670 dtrace_helper_trace(helper, mstate, vstate, 15671 DTRACE_HELPTRACE_NEXT); 15672 } 15673 15674 if (trace) 15675 dtrace_helper_trace(helper, mstate, vstate, 15676 DTRACE_HELPTRACE_DONE); 15677 15678 /* 15679 * Restore the arg0 that we saved upon entry. 15680 */ 15681 mstate->dtms_arg[0] = sarg0; 15682 mstate->dtms_arg[1] = sarg1; 15683 15684 return (rval); 15685 15686err: 15687 if (trace) 15688 dtrace_helper_trace(helper, mstate, vstate, 15689 DTRACE_HELPTRACE_ERR); 15690 15691 /* 15692 * Restore the arg0 that we saved upon entry. 15693 */ 15694 mstate->dtms_arg[0] = sarg0; 15695 mstate->dtms_arg[1] = sarg1; 15696 15697 return (0); 15698} 15699 15700static void 15701dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 15702 dtrace_vstate_t *vstate) 15703{ 15704 int i; 15705 15706 if (helper->dtha_predicate != NULL) 15707 dtrace_difo_release(helper->dtha_predicate, vstate); 15708 15709 for (i = 0; i < helper->dtha_nactions; i++) { 15710 ASSERT(helper->dtha_actions[i] != NULL); 15711 dtrace_difo_release(helper->dtha_actions[i], vstate); 15712 } 15713 15714 kmem_free(helper->dtha_actions, 15715 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 15716 kmem_free(helper, sizeof (dtrace_helper_action_t)); 15717} 15718 15719static int 15720dtrace_helper_destroygen(dtrace_helpers_t *help, int gen) 15721{ 15722 proc_t *p = curproc; 15723 dtrace_vstate_t *vstate; 15724 int i; 15725 15726 if (help == NULL) 15727 help = p->p_dtrace_helpers; 15728 15729 ASSERT(MUTEX_HELD(&dtrace_lock)); 15730 15731 if (help == NULL || gen > help->dthps_generation) 15732 return (EINVAL); 15733 15734 vstate = &help->dthps_vstate; 15735 15736 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 15737 dtrace_helper_action_t *last = NULL, *h, *next; 15738 15739 for (h = help->dthps_actions[i]; h != NULL; h = next) { 15740 next = h->dtha_next; 15741 15742 if (h->dtha_generation == gen) { 15743 if (last != NULL) { 15744 last->dtha_next = next; 15745 } else { 15746 help->dthps_actions[i] = next; 15747 } 15748 15749 dtrace_helper_action_destroy(h, vstate); 15750 } else { 15751 last = h; 15752 } 15753 } 15754 } 15755 15756 /* 15757 * Interate until we've cleared out all helper providers with the 15758 * given generation number. 15759 */ 15760 for (;;) { 15761 dtrace_helper_provider_t *prov; 15762 15763 /* 15764 * Look for a helper provider with the right generation. We 15765 * have to start back at the beginning of the list each time 15766 * because we drop dtrace_lock. It's unlikely that we'll make 15767 * more than two passes. 15768 */ 15769 for (i = 0; i < help->dthps_nprovs; i++) { 15770 prov = help->dthps_provs[i]; 15771 15772 if (prov->dthp_generation == gen) 15773 break; 15774 } 15775 15776 /* 15777 * If there were no matches, we're done. 15778 */ 15779 if (i == help->dthps_nprovs) 15780 break; 15781 15782 /* 15783 * Move the last helper provider into this slot. 15784 */ 15785 help->dthps_nprovs--; 15786 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 15787 help->dthps_provs[help->dthps_nprovs] = NULL; 15788 15789 mutex_exit(&dtrace_lock); 15790 15791 /* 15792 * If we have a meta provider, remove this helper provider. 15793 */ 15794 mutex_enter(&dtrace_meta_lock); 15795 if (dtrace_meta_pid != NULL) { 15796 ASSERT(dtrace_deferred_pid == NULL); 15797 dtrace_helper_provider_remove(&prov->dthp_prov, 15798 p->p_pid); 15799 } 15800 mutex_exit(&dtrace_meta_lock); 15801 15802 dtrace_helper_provider_destroy(prov); 15803 15804 mutex_enter(&dtrace_lock); 15805 } 15806 15807 return (0); 15808} 15809 15810static int 15811dtrace_helper_validate(dtrace_helper_action_t *helper) 15812{ 15813 int err = 0, i; 15814 dtrace_difo_t *dp; 15815 15816 if ((dp = helper->dtha_predicate) != NULL) 15817 err += dtrace_difo_validate_helper(dp); 15818 15819 for (i = 0; i < helper->dtha_nactions; i++) 15820 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 15821 15822 return (err == 0); 15823} 15824 15825static int 15826dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep, 15827 dtrace_helpers_t *help) 15828{ 15829 dtrace_helper_action_t *helper, *last; 15830 dtrace_actdesc_t *act; 15831 dtrace_vstate_t *vstate; 15832 dtrace_predicate_t *pred; 15833 int count = 0, nactions = 0, i; 15834 15835 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 15836 return (EINVAL); 15837 15838 last = help->dthps_actions[which]; 15839 vstate = &help->dthps_vstate; 15840 15841 for (count = 0; last != NULL; last = last->dtha_next) { 15842 count++; 15843 if (last->dtha_next == NULL) 15844 break; 15845 } 15846 15847 /* 15848 * If we already have dtrace_helper_actions_max helper actions for this 15849 * helper action type, we'll refuse to add a new one. 15850 */ 15851 if (count >= dtrace_helper_actions_max) 15852 return (ENOSPC); 15853 15854 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 15855 helper->dtha_generation = help->dthps_generation; 15856 15857 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 15858 ASSERT(pred->dtp_difo != NULL); 15859 dtrace_difo_hold(pred->dtp_difo); 15860 helper->dtha_predicate = pred->dtp_difo; 15861 } 15862 15863 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 15864 if (act->dtad_kind != DTRACEACT_DIFEXPR) 15865 goto err; 15866 15867 if (act->dtad_difo == NULL) 15868 goto err; 15869 15870 nactions++; 15871 } 15872 15873 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 15874 (helper->dtha_nactions = nactions), KM_SLEEP); 15875 15876 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 15877 dtrace_difo_hold(act->dtad_difo); 15878 helper->dtha_actions[i++] = act->dtad_difo; 15879 } 15880 15881 if (!dtrace_helper_validate(helper)) 15882 goto err; 15883 15884 if (last == NULL) { 15885 help->dthps_actions[which] = helper; 15886 } else { 15887 last->dtha_next = helper; 15888 } 15889 15890 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 15891 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 15892 dtrace_helptrace_next = 0; 15893 } 15894 15895 return (0); 15896err: 15897 dtrace_helper_action_destroy(helper, vstate); 15898 return (EINVAL); 15899} 15900 15901static void 15902dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 15903 dof_helper_t *dofhp) 15904{ 15905 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 15906 15907 mutex_enter(&dtrace_meta_lock); 15908 mutex_enter(&dtrace_lock); 15909 15910 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 15911 /* 15912 * If the dtrace module is loaded but not attached, or if 15913 * there aren't isn't a meta provider registered to deal with 15914 * these provider descriptions, we need to postpone creating 15915 * the actual providers until later. 15916 */ 15917 15918 if (help->dthps_next == NULL && help->dthps_prev == NULL && 15919 dtrace_deferred_pid != help) { 15920 help->dthps_deferred = 1; 15921 help->dthps_pid = p->p_pid; 15922 help->dthps_next = dtrace_deferred_pid; 15923 help->dthps_prev = NULL; 15924 if (dtrace_deferred_pid != NULL) 15925 dtrace_deferred_pid->dthps_prev = help; 15926 dtrace_deferred_pid = help; 15927 } 15928 15929 mutex_exit(&dtrace_lock); 15930 15931 } else if (dofhp != NULL) { 15932 /* 15933 * If the dtrace module is loaded and we have a particular 15934 * helper provider description, pass that off to the 15935 * meta provider. 15936 */ 15937 15938 mutex_exit(&dtrace_lock); 15939 15940 dtrace_helper_provide(dofhp, p->p_pid); 15941 15942 } else { 15943 /* 15944 * Otherwise, just pass all the helper provider descriptions 15945 * off to the meta provider. 15946 */ 15947 15948 int i; 15949 mutex_exit(&dtrace_lock); 15950 15951 for (i = 0; i < help->dthps_nprovs; i++) { 15952 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 15953 p->p_pid); 15954 } 15955 } 15956 15957 mutex_exit(&dtrace_meta_lock); 15958} 15959 15960static int 15961dtrace_helper_provider_add(dof_helper_t *dofhp, dtrace_helpers_t *help, int gen) 15962{ 15963 dtrace_helper_provider_t *hprov, **tmp_provs; 15964 uint_t tmp_maxprovs, i; 15965 15966 ASSERT(MUTEX_HELD(&dtrace_lock)); 15967 ASSERT(help != NULL); 15968 15969 /* 15970 * If we already have dtrace_helper_providers_max helper providers, 15971 * we're refuse to add a new one. 15972 */ 15973 if (help->dthps_nprovs >= dtrace_helper_providers_max) 15974 return (ENOSPC); 15975 15976 /* 15977 * Check to make sure this isn't a duplicate. 15978 */ 15979 for (i = 0; i < help->dthps_nprovs; i++) { 15980 if (dofhp->dofhp_addr == 15981 help->dthps_provs[i]->dthp_prov.dofhp_addr) 15982 return (EALREADY); 15983 } 15984 15985 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 15986 hprov->dthp_prov = *dofhp; 15987 hprov->dthp_ref = 1; 15988 hprov->dthp_generation = gen; 15989 15990 /* 15991 * Allocate a bigger table for helper providers if it's already full. 15992 */ 15993 if (help->dthps_maxprovs == help->dthps_nprovs) { 15994 tmp_maxprovs = help->dthps_maxprovs; 15995 tmp_provs = help->dthps_provs; 15996 15997 if (help->dthps_maxprovs == 0) 15998 help->dthps_maxprovs = 2; 15999 else 16000 help->dthps_maxprovs *= 2; 16001 if (help->dthps_maxprovs > dtrace_helper_providers_max) 16002 help->dthps_maxprovs = dtrace_helper_providers_max; 16003 16004 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 16005 16006 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 16007 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16008 16009 if (tmp_provs != NULL) { 16010 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 16011 sizeof (dtrace_helper_provider_t *)); 16012 kmem_free(tmp_provs, tmp_maxprovs * 16013 sizeof (dtrace_helper_provider_t *)); 16014 } 16015 } 16016 16017 help->dthps_provs[help->dthps_nprovs] = hprov; 16018 help->dthps_nprovs++; 16019 16020 return (0); 16021} 16022 16023static void 16024dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 16025{ 16026 mutex_enter(&dtrace_lock); 16027 16028 if (--hprov->dthp_ref == 0) { 16029 dof_hdr_t *dof; 16030 mutex_exit(&dtrace_lock); 16031 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 16032 dtrace_dof_destroy(dof); 16033 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 16034 } else { 16035 mutex_exit(&dtrace_lock); 16036 } 16037} 16038 16039static int 16040dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 16041{ 16042 uintptr_t daddr = (uintptr_t)dof; 16043 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 16044 dof_provider_t *provider; 16045 dof_probe_t *probe; 16046 uint8_t *arg; 16047 char *strtab, *typestr; 16048 dof_stridx_t typeidx; 16049 size_t typesz; 16050 uint_t nprobes, j, k; 16051 16052 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 16053 16054 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 16055 dtrace_dof_error(dof, "misaligned section offset"); 16056 return (-1); 16057 } 16058 16059 /* 16060 * The section needs to be large enough to contain the DOF provider 16061 * structure appropriate for the given version. 16062 */ 16063 if (sec->dofs_size < 16064 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 16065 offsetof(dof_provider_t, dofpv_prenoffs) : 16066 sizeof (dof_provider_t))) { 16067 dtrace_dof_error(dof, "provider section too small"); 16068 return (-1); 16069 } 16070 16071 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 16072 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 16073 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 16074 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 16075 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 16076 16077 if (str_sec == NULL || prb_sec == NULL || 16078 arg_sec == NULL || off_sec == NULL) 16079 return (-1); 16080 16081 enoff_sec = NULL; 16082 16083 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 16084 provider->dofpv_prenoffs != DOF_SECT_NONE && 16085 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 16086 provider->dofpv_prenoffs)) == NULL) 16087 return (-1); 16088 16089 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 16090 16091 if (provider->dofpv_name >= str_sec->dofs_size || 16092 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 16093 dtrace_dof_error(dof, "invalid provider name"); 16094 return (-1); 16095 } 16096 16097 if (prb_sec->dofs_entsize == 0 || 16098 prb_sec->dofs_entsize > prb_sec->dofs_size) { 16099 dtrace_dof_error(dof, "invalid entry size"); 16100 return (-1); 16101 } 16102 16103 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 16104 dtrace_dof_error(dof, "misaligned entry size"); 16105 return (-1); 16106 } 16107 16108 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 16109 dtrace_dof_error(dof, "invalid entry size"); 16110 return (-1); 16111 } 16112 16113 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 16114 dtrace_dof_error(dof, "misaligned section offset"); 16115 return (-1); 16116 } 16117 16118 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 16119 dtrace_dof_error(dof, "invalid entry size"); 16120 return (-1); 16121 } 16122 16123 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 16124 16125 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 16126 16127 /* 16128 * Take a pass through the probes to check for errors. 16129 */ 16130 for (j = 0; j < nprobes; j++) { 16131 probe = (dof_probe_t *)(uintptr_t)(daddr + 16132 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 16133 16134 if (probe->dofpr_func >= str_sec->dofs_size) { 16135 dtrace_dof_error(dof, "invalid function name"); 16136 return (-1); 16137 } 16138 16139 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 16140 dtrace_dof_error(dof, "function name too long"); 16141 /* 16142 * Keep going if the function name is too long. 16143 * Unlike provider and probe names, we cannot reasonably 16144 * impose restrictions on function names, since they're 16145 * a property of the code being instrumented. We will 16146 * skip this probe in dtrace_helper_provide_one(). 16147 */ 16148 } 16149 16150 if (probe->dofpr_name >= str_sec->dofs_size || 16151 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 16152 dtrace_dof_error(dof, "invalid probe name"); 16153 return (-1); 16154 } 16155 16156 /* 16157 * The offset count must not wrap the index, and the offsets 16158 * must also not overflow the section's data. 16159 */ 16160 if (probe->dofpr_offidx + probe->dofpr_noffs < 16161 probe->dofpr_offidx || 16162 (probe->dofpr_offidx + probe->dofpr_noffs) * 16163 off_sec->dofs_entsize > off_sec->dofs_size) { 16164 dtrace_dof_error(dof, "invalid probe offset"); 16165 return (-1); 16166 } 16167 16168 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 16169 /* 16170 * If there's no is-enabled offset section, make sure 16171 * there aren't any is-enabled offsets. Otherwise 16172 * perform the same checks as for probe offsets 16173 * (immediately above). 16174 */ 16175 if (enoff_sec == NULL) { 16176 if (probe->dofpr_enoffidx != 0 || 16177 probe->dofpr_nenoffs != 0) { 16178 dtrace_dof_error(dof, "is-enabled " 16179 "offsets with null section"); 16180 return (-1); 16181 } 16182 } else if (probe->dofpr_enoffidx + 16183 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 16184 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 16185 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 16186 dtrace_dof_error(dof, "invalid is-enabled " 16187 "offset"); 16188 return (-1); 16189 } 16190 16191 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 16192 dtrace_dof_error(dof, "zero probe and " 16193 "is-enabled offsets"); 16194 return (-1); 16195 } 16196 } else if (probe->dofpr_noffs == 0) { 16197 dtrace_dof_error(dof, "zero probe offsets"); 16198 return (-1); 16199 } 16200 16201 if (probe->dofpr_argidx + probe->dofpr_xargc < 16202 probe->dofpr_argidx || 16203 (probe->dofpr_argidx + probe->dofpr_xargc) * 16204 arg_sec->dofs_entsize > arg_sec->dofs_size) { 16205 dtrace_dof_error(dof, "invalid args"); 16206 return (-1); 16207 } 16208 16209 typeidx = probe->dofpr_nargv; 16210 typestr = strtab + probe->dofpr_nargv; 16211 for (k = 0; k < probe->dofpr_nargc; k++) { 16212 if (typeidx >= str_sec->dofs_size) { 16213 dtrace_dof_error(dof, "bad " 16214 "native argument type"); 16215 return (-1); 16216 } 16217 16218 typesz = strlen(typestr) + 1; 16219 if (typesz > DTRACE_ARGTYPELEN) { 16220 dtrace_dof_error(dof, "native " 16221 "argument type too long"); 16222 return (-1); 16223 } 16224 typeidx += typesz; 16225 typestr += typesz; 16226 } 16227 16228 typeidx = probe->dofpr_xargv; 16229 typestr = strtab + probe->dofpr_xargv; 16230 for (k = 0; k < probe->dofpr_xargc; k++) { 16231 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 16232 dtrace_dof_error(dof, "bad " 16233 "native argument index"); 16234 return (-1); 16235 } 16236 16237 if (typeidx >= str_sec->dofs_size) { 16238 dtrace_dof_error(dof, "bad " 16239 "translated argument type"); 16240 return (-1); 16241 } 16242 16243 typesz = strlen(typestr) + 1; 16244 if (typesz > DTRACE_ARGTYPELEN) { 16245 dtrace_dof_error(dof, "translated argument " 16246 "type too long"); 16247 return (-1); 16248 } 16249 16250 typeidx += typesz; 16251 typestr += typesz; 16252 } 16253 } 16254 16255 return (0); 16256} 16257 16258static int 16259dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp, struct proc *p) 16260{ 16261 dtrace_helpers_t *help; 16262 dtrace_vstate_t *vstate; 16263 dtrace_enabling_t *enab = NULL; 16264 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 16265 uintptr_t daddr = (uintptr_t)dof; 16266 16267 ASSERT(MUTEX_HELD(&dtrace_lock)); 16268 16269 if ((help = p->p_dtrace_helpers) == NULL) 16270 help = dtrace_helpers_create(p); 16271 16272 vstate = &help->dthps_vstate; 16273 16274 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, dhp->dofhp_addr, 16275 dhp->dofhp_dof, B_FALSE)) != 0) { 16276 dtrace_dof_destroy(dof); 16277 return (rv); 16278 } 16279 16280 /* 16281 * Look for helper providers and validate their descriptions. 16282 */ 16283 for (i = 0; i < dof->dofh_secnum; i++) { 16284 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 16285 dof->dofh_secoff + i * dof->dofh_secsize); 16286 16287 if (sec->dofs_type != DOF_SECT_PROVIDER) 16288 continue; 16289 16290 if (dtrace_helper_provider_validate(dof, sec) != 0) { 16291 dtrace_enabling_destroy(enab); 16292 dtrace_dof_destroy(dof); 16293 return (-1); 16294 } 16295 16296 nprovs++; 16297 } 16298 16299 /* 16300 * Now we need to walk through the ECB descriptions in the enabling. 16301 */ 16302 for (i = 0; i < enab->dten_ndesc; i++) { 16303 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 16304 dtrace_probedesc_t *desc = &ep->dted_probe; 16305 16306 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 16307 continue; 16308 16309 if (strcmp(desc->dtpd_mod, "helper") != 0) 16310 continue; 16311 16312 if (strcmp(desc->dtpd_func, "ustack") != 0) 16313 continue; 16314 16315 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 16316 ep, help)) != 0) { 16317 /* 16318 * Adding this helper action failed -- we are now going 16319 * to rip out the entire generation and return failure. 16320 */ 16321 (void) dtrace_helper_destroygen(help, 16322 help->dthps_generation); 16323 dtrace_enabling_destroy(enab); 16324 dtrace_dof_destroy(dof); 16325 return (-1); 16326 } 16327 16328 nhelpers++; 16329 } 16330 16331 if (nhelpers < enab->dten_ndesc) 16332 dtrace_dof_error(dof, "unmatched helpers"); 16333 16334 gen = help->dthps_generation++; 16335 dtrace_enabling_destroy(enab); 16336 16337 if (nprovs > 0) { 16338 /* 16339 * Now that this is in-kernel, we change the sense of the 16340 * members: dofhp_dof denotes the in-kernel copy of the DOF 16341 * and dofhp_addr denotes the address at user-level. 16342 */ 16343 dhp->dofhp_addr = dhp->dofhp_dof; 16344 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 16345 16346 if (dtrace_helper_provider_add(dhp, help, gen) == 0) { 16347 mutex_exit(&dtrace_lock); 16348 dtrace_helper_provider_register(p, help, dhp); 16349 mutex_enter(&dtrace_lock); 16350 16351 destroy = 0; 16352 } 16353 } 16354 16355 if (destroy) 16356 dtrace_dof_destroy(dof); 16357 16358 return (gen); 16359} 16360 16361static dtrace_helpers_t * 16362dtrace_helpers_create(proc_t *p) 16363{ 16364 dtrace_helpers_t *help; 16365 16366 ASSERT(MUTEX_HELD(&dtrace_lock)); 16367 ASSERT(p->p_dtrace_helpers == NULL); 16368 16369 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 16370 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 16371 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 16372 16373 p->p_dtrace_helpers = help; 16374 dtrace_helpers++; 16375 16376 return (help); 16377} 16378 16379#ifdef illumos 16380static 16381#endif 16382void 16383dtrace_helpers_destroy(proc_t *p) 16384{ 16385 dtrace_helpers_t *help; 16386 dtrace_vstate_t *vstate; 16387#ifdef illumos 16388 proc_t *p = curproc; 16389#endif 16390 int i; 16391 16392 mutex_enter(&dtrace_lock); 16393 16394 ASSERT(p->p_dtrace_helpers != NULL); 16395 ASSERT(dtrace_helpers > 0); 16396 16397 help = p->p_dtrace_helpers; 16398 vstate = &help->dthps_vstate; 16399 16400 /* 16401 * We're now going to lose the help from this process. 16402 */ 16403 p->p_dtrace_helpers = NULL; 16404 dtrace_sync(); 16405 16406 /* 16407 * Destory the helper actions. 16408 */ 16409 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16410 dtrace_helper_action_t *h, *next; 16411 16412 for (h = help->dthps_actions[i]; h != NULL; h = next) { 16413 next = h->dtha_next; 16414 dtrace_helper_action_destroy(h, vstate); 16415 h = next; 16416 } 16417 } 16418 16419 mutex_exit(&dtrace_lock); 16420 16421 /* 16422 * Destroy the helper providers. 16423 */ 16424 if (help->dthps_maxprovs > 0) { 16425 mutex_enter(&dtrace_meta_lock); 16426 if (dtrace_meta_pid != NULL) { 16427 ASSERT(dtrace_deferred_pid == NULL); 16428 16429 for (i = 0; i < help->dthps_nprovs; i++) { 16430 dtrace_helper_provider_remove( 16431 &help->dthps_provs[i]->dthp_prov, p->p_pid); 16432 } 16433 } else { 16434 mutex_enter(&dtrace_lock); 16435 ASSERT(help->dthps_deferred == 0 || 16436 help->dthps_next != NULL || 16437 help->dthps_prev != NULL || 16438 help == dtrace_deferred_pid); 16439 16440 /* 16441 * Remove the helper from the deferred list. 16442 */ 16443 if (help->dthps_next != NULL) 16444 help->dthps_next->dthps_prev = help->dthps_prev; 16445 if (help->dthps_prev != NULL) 16446 help->dthps_prev->dthps_next = help->dthps_next; 16447 if (dtrace_deferred_pid == help) { 16448 dtrace_deferred_pid = help->dthps_next; 16449 ASSERT(help->dthps_prev == NULL); 16450 } 16451 16452 mutex_exit(&dtrace_lock); 16453 } 16454 16455 mutex_exit(&dtrace_meta_lock); 16456 16457 for (i = 0; i < help->dthps_nprovs; i++) { 16458 dtrace_helper_provider_destroy(help->dthps_provs[i]); 16459 } 16460 16461 kmem_free(help->dthps_provs, help->dthps_maxprovs * 16462 sizeof (dtrace_helper_provider_t *)); 16463 } 16464 16465 mutex_enter(&dtrace_lock); 16466 16467 dtrace_vstate_fini(&help->dthps_vstate); 16468 kmem_free(help->dthps_actions, 16469 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 16470 kmem_free(help, sizeof (dtrace_helpers_t)); 16471 16472 --dtrace_helpers; 16473 mutex_exit(&dtrace_lock); 16474} 16475 16476#ifdef illumos 16477static 16478#endif 16479void 16480dtrace_helpers_duplicate(proc_t *from, proc_t *to) 16481{ 16482 dtrace_helpers_t *help, *newhelp; 16483 dtrace_helper_action_t *helper, *new, *last; 16484 dtrace_difo_t *dp; 16485 dtrace_vstate_t *vstate; 16486 int i, j, sz, hasprovs = 0; 16487 16488 mutex_enter(&dtrace_lock); 16489 ASSERT(from->p_dtrace_helpers != NULL); 16490 ASSERT(dtrace_helpers > 0); 16491 16492 help = from->p_dtrace_helpers; 16493 newhelp = dtrace_helpers_create(to); 16494 ASSERT(to->p_dtrace_helpers != NULL); 16495 16496 newhelp->dthps_generation = help->dthps_generation; 16497 vstate = &newhelp->dthps_vstate; 16498 16499 /* 16500 * Duplicate the helper actions. 16501 */ 16502 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 16503 if ((helper = help->dthps_actions[i]) == NULL) 16504 continue; 16505 16506 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 16507 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 16508 KM_SLEEP); 16509 new->dtha_generation = helper->dtha_generation; 16510 16511 if ((dp = helper->dtha_predicate) != NULL) { 16512 dp = dtrace_difo_duplicate(dp, vstate); 16513 new->dtha_predicate = dp; 16514 } 16515 16516 new->dtha_nactions = helper->dtha_nactions; 16517 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 16518 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 16519 16520 for (j = 0; j < new->dtha_nactions; j++) { 16521 dtrace_difo_t *dp = helper->dtha_actions[j]; 16522 16523 ASSERT(dp != NULL); 16524 dp = dtrace_difo_duplicate(dp, vstate); 16525 new->dtha_actions[j] = dp; 16526 } 16527 16528 if (last != NULL) { 16529 last->dtha_next = new; 16530 } else { 16531 newhelp->dthps_actions[i] = new; 16532 } 16533 16534 last = new; 16535 } 16536 } 16537 16538 /* 16539 * Duplicate the helper providers and register them with the 16540 * DTrace framework. 16541 */ 16542 if (help->dthps_nprovs > 0) { 16543 newhelp->dthps_nprovs = help->dthps_nprovs; 16544 newhelp->dthps_maxprovs = help->dthps_nprovs; 16545 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 16546 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 16547 for (i = 0; i < newhelp->dthps_nprovs; i++) { 16548 newhelp->dthps_provs[i] = help->dthps_provs[i]; 16549 newhelp->dthps_provs[i]->dthp_ref++; 16550 } 16551 16552 hasprovs = 1; 16553 } 16554 16555 mutex_exit(&dtrace_lock); 16556 16557 if (hasprovs) 16558 dtrace_helper_provider_register(to, newhelp, NULL); 16559} 16560 16561/* 16562 * DTrace Hook Functions 16563 */ 16564static void 16565dtrace_module_loaded(modctl_t *ctl) 16566{ 16567 dtrace_provider_t *prv; 16568 16569 mutex_enter(&dtrace_provider_lock); 16570#ifdef illumos 16571 mutex_enter(&mod_lock); 16572#endif 16573 16574#ifdef illumos 16575 ASSERT(ctl->mod_busy); 16576#endif 16577 16578 /* 16579 * We're going to call each providers per-module provide operation 16580 * specifying only this module. 16581 */ 16582 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 16583 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 16584 16585#ifdef illumos 16586 mutex_exit(&mod_lock); 16587#endif 16588 mutex_exit(&dtrace_provider_lock); 16589 16590 /* 16591 * If we have any retained enablings, we need to match against them. 16592 * Enabling probes requires that cpu_lock be held, and we cannot hold 16593 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 16594 * module. (In particular, this happens when loading scheduling 16595 * classes.) So if we have any retained enablings, we need to dispatch 16596 * our task queue to do the match for us. 16597 */ 16598 mutex_enter(&dtrace_lock); 16599 16600 if (dtrace_retained == NULL) { 16601 mutex_exit(&dtrace_lock); 16602 return; 16603 } 16604 16605 (void) taskq_dispatch(dtrace_taskq, 16606 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 16607 16608 mutex_exit(&dtrace_lock); 16609 16610 /* 16611 * And now, for a little heuristic sleaze: in general, we want to 16612 * match modules as soon as they load. However, we cannot guarantee 16613 * this, because it would lead us to the lock ordering violation 16614 * outlined above. The common case, of course, is that cpu_lock is 16615 * _not_ held -- so we delay here for a clock tick, hoping that that's 16616 * long enough for the task queue to do its work. If it's not, it's 16617 * not a serious problem -- it just means that the module that we 16618 * just loaded may not be immediately instrumentable. 16619 */ 16620 delay(1); 16621} 16622 16623static void 16624#ifdef illumos 16625dtrace_module_unloaded(modctl_t *ctl) 16626#else 16627dtrace_module_unloaded(modctl_t *ctl, int *error) 16628#endif 16629{ 16630 dtrace_probe_t template, *probe, *first, *next; 16631 dtrace_provider_t *prov; 16632#ifndef illumos 16633 char modname[DTRACE_MODNAMELEN]; 16634 size_t len; 16635#endif 16636 16637#ifdef illumos 16638 template.dtpr_mod = ctl->mod_modname; 16639#else 16640 /* Handle the fact that ctl->filename may end in ".ko". */ 16641 strlcpy(modname, ctl->filename, sizeof(modname)); 16642 len = strlen(ctl->filename); 16643 if (len > 3 && strcmp(modname + len - 3, ".ko") == 0) 16644 modname[len - 3] = '\0'; 16645 template.dtpr_mod = modname; 16646#endif 16647 16648 mutex_enter(&dtrace_provider_lock); 16649#ifdef illumos 16650 mutex_enter(&mod_lock); 16651#endif 16652 mutex_enter(&dtrace_lock); 16653 16654#ifndef illumos 16655 if (ctl->nenabled > 0) { 16656 /* Don't allow unloads if a probe is enabled. */ 16657 mutex_exit(&dtrace_provider_lock); 16658 mutex_exit(&dtrace_lock); 16659 *error = -1; 16660 printf( 16661 "kldunload: attempt to unload module that has DTrace probes enabled\n"); 16662 return; 16663 } 16664#endif 16665 16666 if (dtrace_bymod == NULL) { 16667 /* 16668 * The DTrace module is loaded (obviously) but not attached; 16669 * we don't have any work to do. 16670 */ 16671 mutex_exit(&dtrace_provider_lock); 16672#ifdef illumos 16673 mutex_exit(&mod_lock); 16674#endif 16675 mutex_exit(&dtrace_lock); 16676 return; 16677 } 16678 16679 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 16680 probe != NULL; probe = probe->dtpr_nextmod) { 16681 if (probe->dtpr_ecb != NULL) { 16682 mutex_exit(&dtrace_provider_lock); 16683#ifdef illumos 16684 mutex_exit(&mod_lock); 16685#endif 16686 mutex_exit(&dtrace_lock); 16687 16688 /* 16689 * This shouldn't _actually_ be possible -- we're 16690 * unloading a module that has an enabled probe in it. 16691 * (It's normally up to the provider to make sure that 16692 * this can't happen.) However, because dtps_enable() 16693 * doesn't have a failure mode, there can be an 16694 * enable/unload race. Upshot: we don't want to 16695 * assert, but we're not going to disable the 16696 * probe, either. 16697 */ 16698 if (dtrace_err_verbose) { 16699#ifdef illumos 16700 cmn_err(CE_WARN, "unloaded module '%s' had " 16701 "enabled probes", ctl->mod_modname); 16702#else 16703 cmn_err(CE_WARN, "unloaded module '%s' had " 16704 "enabled probes", modname); 16705#endif 16706 } 16707 16708 return; 16709 } 16710 } 16711 16712 probe = first; 16713 16714 for (first = NULL; probe != NULL; probe = next) { 16715 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 16716 16717 dtrace_probes[probe->dtpr_id - 1] = NULL; 16718 16719 next = probe->dtpr_nextmod; 16720 dtrace_hash_remove(dtrace_bymod, probe); 16721 dtrace_hash_remove(dtrace_byfunc, probe); 16722 dtrace_hash_remove(dtrace_byname, probe); 16723 16724 if (first == NULL) { 16725 first = probe; 16726 probe->dtpr_nextmod = NULL; 16727 } else { 16728 probe->dtpr_nextmod = first; 16729 first = probe; 16730 } 16731 } 16732 16733 /* 16734 * We've removed all of the module's probes from the hash chains and 16735 * from the probe array. Now issue a dtrace_sync() to be sure that 16736 * everyone has cleared out from any probe array processing. 16737 */ 16738 dtrace_sync(); 16739 16740 for (probe = first; probe != NULL; probe = first) { 16741 first = probe->dtpr_nextmod; 16742 prov = probe->dtpr_provider; 16743 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 16744 probe->dtpr_arg); 16745 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 16746 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 16747 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 16748#ifdef illumos 16749 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 16750#else 16751 free_unr(dtrace_arena, probe->dtpr_id); 16752#endif 16753 kmem_free(probe, sizeof (dtrace_probe_t)); 16754 } 16755 16756 mutex_exit(&dtrace_lock); 16757#ifdef illumos 16758 mutex_exit(&mod_lock); 16759#endif 16760 mutex_exit(&dtrace_provider_lock); 16761} 16762 16763#ifndef illumos 16764static void 16765dtrace_kld_load(void *arg __unused, linker_file_t lf) 16766{ 16767 16768 dtrace_module_loaded(lf); 16769} 16770 16771static void 16772dtrace_kld_unload_try(void *arg __unused, linker_file_t lf, int *error) 16773{ 16774 16775 if (*error != 0) 16776 /* We already have an error, so don't do anything. */ 16777 return; 16778 dtrace_module_unloaded(lf, error); 16779} 16780#endif 16781 16782#ifdef illumos 16783static void 16784dtrace_suspend(void) 16785{ 16786 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 16787} 16788 16789static void 16790dtrace_resume(void) 16791{ 16792 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 16793} 16794#endif 16795 16796static int 16797dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 16798{ 16799 ASSERT(MUTEX_HELD(&cpu_lock)); 16800 mutex_enter(&dtrace_lock); 16801 16802 switch (what) { 16803 case CPU_CONFIG: { 16804 dtrace_state_t *state; 16805 dtrace_optval_t *opt, rs, c; 16806 16807 /* 16808 * For now, we only allocate a new buffer for anonymous state. 16809 */ 16810 if ((state = dtrace_anon.dta_state) == NULL) 16811 break; 16812 16813 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 16814 break; 16815 16816 opt = state->dts_options; 16817 c = opt[DTRACEOPT_CPU]; 16818 16819 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 16820 break; 16821 16822 /* 16823 * Regardless of what the actual policy is, we're going to 16824 * temporarily set our resize policy to be manual. We're 16825 * also going to temporarily set our CPU option to denote 16826 * the newly configured CPU. 16827 */ 16828 rs = opt[DTRACEOPT_BUFRESIZE]; 16829 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 16830 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 16831 16832 (void) dtrace_state_buffers(state); 16833 16834 opt[DTRACEOPT_BUFRESIZE] = rs; 16835 opt[DTRACEOPT_CPU] = c; 16836 16837 break; 16838 } 16839 16840 case CPU_UNCONFIG: 16841 /* 16842 * We don't free the buffer in the CPU_UNCONFIG case. (The 16843 * buffer will be freed when the consumer exits.) 16844 */ 16845 break; 16846 16847 default: 16848 break; 16849 } 16850 16851 mutex_exit(&dtrace_lock); 16852 return (0); 16853} 16854 16855#ifdef illumos 16856static void 16857dtrace_cpu_setup_initial(processorid_t cpu) 16858{ 16859 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 16860} 16861#endif 16862 16863static void 16864dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 16865{ 16866 if (dtrace_toxranges >= dtrace_toxranges_max) { 16867 int osize, nsize; 16868 dtrace_toxrange_t *range; 16869 16870 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16871 16872 if (osize == 0) { 16873 ASSERT(dtrace_toxrange == NULL); 16874 ASSERT(dtrace_toxranges_max == 0); 16875 dtrace_toxranges_max = 1; 16876 } else { 16877 dtrace_toxranges_max <<= 1; 16878 } 16879 16880 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 16881 range = kmem_zalloc(nsize, KM_SLEEP); 16882 16883 if (dtrace_toxrange != NULL) { 16884 ASSERT(osize != 0); 16885 bcopy(dtrace_toxrange, range, osize); 16886 kmem_free(dtrace_toxrange, osize); 16887 } 16888 16889 dtrace_toxrange = range; 16890 } 16891 16892 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 16893 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 16894 16895 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 16896 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 16897 dtrace_toxranges++; 16898} 16899 16900static void 16901dtrace_getf_barrier() 16902{ 16903#ifdef illumos 16904 /* 16905 * When we have unprivileged (that is, non-DTRACE_CRV_KERNEL) enablings 16906 * that contain calls to getf(), this routine will be called on every 16907 * closef() before either the underlying vnode is released or the 16908 * file_t itself is freed. By the time we are here, it is essential 16909 * that the file_t can no longer be accessed from a call to getf() 16910 * in probe context -- that assures that a dtrace_sync() can be used 16911 * to clear out any enablings referring to the old structures. 16912 */ 16913 if (curthread->t_procp->p_zone->zone_dtrace_getf != 0 || 16914 kcred->cr_zone->zone_dtrace_getf != 0) 16915 dtrace_sync(); 16916#endif 16917} 16918 16919/* 16920 * DTrace Driver Cookbook Functions 16921 */ 16922#ifdef illumos 16923/*ARGSUSED*/ 16924static int 16925dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 16926{ 16927 dtrace_provider_id_t id; 16928 dtrace_state_t *state = NULL; 16929 dtrace_enabling_t *enab; 16930 16931 mutex_enter(&cpu_lock); 16932 mutex_enter(&dtrace_provider_lock); 16933 mutex_enter(&dtrace_lock); 16934 16935 if (ddi_soft_state_init(&dtrace_softstate, 16936 sizeof (dtrace_state_t), 0) != 0) { 16937 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 16938 mutex_exit(&cpu_lock); 16939 mutex_exit(&dtrace_provider_lock); 16940 mutex_exit(&dtrace_lock); 16941 return (DDI_FAILURE); 16942 } 16943 16944 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 16945 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 16946 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 16947 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 16948 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 16949 ddi_remove_minor_node(devi, NULL); 16950 ddi_soft_state_fini(&dtrace_softstate); 16951 mutex_exit(&cpu_lock); 16952 mutex_exit(&dtrace_provider_lock); 16953 mutex_exit(&dtrace_lock); 16954 return (DDI_FAILURE); 16955 } 16956 16957 ddi_report_dev(devi); 16958 dtrace_devi = devi; 16959 16960 dtrace_modload = dtrace_module_loaded; 16961 dtrace_modunload = dtrace_module_unloaded; 16962 dtrace_cpu_init = dtrace_cpu_setup_initial; 16963 dtrace_helpers_cleanup = dtrace_helpers_destroy; 16964 dtrace_helpers_fork = dtrace_helpers_duplicate; 16965 dtrace_cpustart_init = dtrace_suspend; 16966 dtrace_cpustart_fini = dtrace_resume; 16967 dtrace_debugger_init = dtrace_suspend; 16968 dtrace_debugger_fini = dtrace_resume; 16969 16970 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16971 16972 ASSERT(MUTEX_HELD(&cpu_lock)); 16973 16974 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 16975 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 16976 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 16977 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 16978 VM_SLEEP | VMC_IDENTIFIER); 16979 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 16980 1, INT_MAX, 0); 16981 16982 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 16983 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 16984 NULL, NULL, NULL, NULL, NULL, 0); 16985 16986 ASSERT(MUTEX_HELD(&cpu_lock)); 16987 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 16988 offsetof(dtrace_probe_t, dtpr_nextmod), 16989 offsetof(dtrace_probe_t, dtpr_prevmod)); 16990 16991 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 16992 offsetof(dtrace_probe_t, dtpr_nextfunc), 16993 offsetof(dtrace_probe_t, dtpr_prevfunc)); 16994 16995 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 16996 offsetof(dtrace_probe_t, dtpr_nextname), 16997 offsetof(dtrace_probe_t, dtpr_prevname)); 16998 16999 if (dtrace_retain_max < 1) { 17000 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 17001 "setting to 1", dtrace_retain_max); 17002 dtrace_retain_max = 1; 17003 } 17004 17005 /* 17006 * Now discover our toxic ranges. 17007 */ 17008 dtrace_toxic_ranges(dtrace_toxrange_add); 17009 17010 /* 17011 * Before we register ourselves as a provider to our own framework, 17012 * we would like to assert that dtrace_provider is NULL -- but that's 17013 * not true if we were loaded as a dependency of a DTrace provider. 17014 * Once we've registered, we can assert that dtrace_provider is our 17015 * pseudo provider. 17016 */ 17017 (void) dtrace_register("dtrace", &dtrace_provider_attr, 17018 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 17019 17020 ASSERT(dtrace_provider != NULL); 17021 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 17022 17023 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 17024 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 17025 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 17026 dtrace_provider, NULL, NULL, "END", 0, NULL); 17027 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 17028 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 17029 17030 dtrace_anon_property(); 17031 mutex_exit(&cpu_lock); 17032 17033 /* 17034 * If there are already providers, we must ask them to provide their 17035 * probes, and then match any anonymous enabling against them. Note 17036 * that there should be no other retained enablings at this time: 17037 * the only retained enablings at this time should be the anonymous 17038 * enabling. 17039 */ 17040 if (dtrace_anon.dta_enabling != NULL) { 17041 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 17042 17043 dtrace_enabling_provide(NULL); 17044 state = dtrace_anon.dta_state; 17045 17046 /* 17047 * We couldn't hold cpu_lock across the above call to 17048 * dtrace_enabling_provide(), but we must hold it to actually 17049 * enable the probes. We have to drop all of our locks, pick 17050 * up cpu_lock, and regain our locks before matching the 17051 * retained anonymous enabling. 17052 */ 17053 mutex_exit(&dtrace_lock); 17054 mutex_exit(&dtrace_provider_lock); 17055 17056 mutex_enter(&cpu_lock); 17057 mutex_enter(&dtrace_provider_lock); 17058 mutex_enter(&dtrace_lock); 17059 17060 if ((enab = dtrace_anon.dta_enabling) != NULL) 17061 (void) dtrace_enabling_match(enab, NULL); 17062 17063 mutex_exit(&cpu_lock); 17064 } 17065 17066 mutex_exit(&dtrace_lock); 17067 mutex_exit(&dtrace_provider_lock); 17068 17069 if (state != NULL) { 17070 /* 17071 * If we created any anonymous state, set it going now. 17072 */ 17073 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 17074 } 17075 17076 return (DDI_SUCCESS); 17077} 17078#endif /* illumos */ 17079 17080#ifndef illumos 17081static void dtrace_dtr(void *); 17082#endif 17083 17084/*ARGSUSED*/ 17085static int 17086#ifdef illumos 17087dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 17088#else 17089dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 17090#endif 17091{ 17092 dtrace_state_t *state; 17093 uint32_t priv; 17094 uid_t uid; 17095 zoneid_t zoneid; 17096 17097#ifdef illumos 17098 if (getminor(*devp) == DTRACEMNRN_HELPER) 17099 return (0); 17100 17101 /* 17102 * If this wasn't an open with the "helper" minor, then it must be 17103 * the "dtrace" minor. 17104 */ 17105 if (getminor(*devp) == DTRACEMNRN_DTRACE) 17106 return (ENXIO); 17107#else 17108 cred_t *cred_p = NULL; 17109 cred_p = dev->si_cred; 17110 17111 /* 17112 * If no DTRACE_PRIV_* bits are set in the credential, then the 17113 * caller lacks sufficient permission to do anything with DTrace. 17114 */ 17115 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 17116 if (priv == DTRACE_PRIV_NONE) { 17117#endif 17118 17119 return (EACCES); 17120 } 17121 17122 /* 17123 * Ask all providers to provide all their probes. 17124 */ 17125 mutex_enter(&dtrace_provider_lock); 17126 dtrace_probe_provide(NULL, NULL); 17127 mutex_exit(&dtrace_provider_lock); 17128 17129 mutex_enter(&cpu_lock); 17130 mutex_enter(&dtrace_lock); 17131 dtrace_opens++; 17132 dtrace_membar_producer(); 17133 17134#ifdef illumos 17135 /* 17136 * If the kernel debugger is active (that is, if the kernel debugger 17137 * modified text in some way), we won't allow the open. 17138 */ 17139 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 17140 dtrace_opens--; 17141 mutex_exit(&cpu_lock); 17142 mutex_exit(&dtrace_lock); 17143 return (EBUSY); 17144 } 17145 17146 if (dtrace_helptrace_enable && dtrace_helptrace_buffer == NULL) { 17147 /* 17148 * If DTrace helper tracing is enabled, we need to allocate the 17149 * trace buffer and initialize the values. 17150 */ 17151 dtrace_helptrace_buffer = 17152 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 17153 dtrace_helptrace_next = 0; 17154 dtrace_helptrace_wrapped = 0; 17155 dtrace_helptrace_enable = 0; 17156 } 17157 17158 state = dtrace_state_create(devp, cred_p); 17159#else 17160 state = dtrace_state_create(dev, NULL); 17161 devfs_set_cdevpriv(state, dtrace_dtr); 17162#endif 17163 17164 mutex_exit(&cpu_lock); 17165 17166 if (state == NULL) { 17167#ifdef illumos 17168 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17169 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17170#else 17171 --dtrace_opens; 17172#endif 17173 mutex_exit(&dtrace_lock); 17174 return (EAGAIN); 17175 } 17176 17177 mutex_exit(&dtrace_lock); 17178 17179 return (0); 17180} 17181 17182/*ARGSUSED*/ 17183#ifdef illumos 17184static int 17185dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 17186#else 17187static void 17188dtrace_dtr(void *data) 17189#endif 17190{ 17191#ifdef illumos 17192 minor_t minor = getminor(dev); 17193 dtrace_state_t *state; 17194#endif 17195 dtrace_helptrace_t *buf = NULL; 17196 17197#ifdef illumos 17198 if (minor == DTRACEMNRN_HELPER) 17199 return (0); 17200 17201 state = ddi_get_soft_state(dtrace_softstate, minor); 17202#else 17203 dtrace_state_t *state = data; 17204#endif 17205 17206 mutex_enter(&cpu_lock); 17207 mutex_enter(&dtrace_lock); 17208 17209#ifdef illumos 17210 if (state->dts_anon) 17211#else 17212 if (state != NULL && state->dts_anon) 17213#endif 17214 { 17215 /* 17216 * There is anonymous state. Destroy that first. 17217 */ 17218 ASSERT(dtrace_anon.dta_state == NULL); 17219 dtrace_state_destroy(state->dts_anon); 17220 } 17221 17222 if (dtrace_helptrace_disable) { 17223 /* 17224 * If we have been told to disable helper tracing, set the 17225 * buffer to NULL before calling into dtrace_state_destroy(); 17226 * we take advantage of its dtrace_sync() to know that no 17227 * CPU is in probe context with enabled helper tracing 17228 * after it returns. 17229 */ 17230 buf = dtrace_helptrace_buffer; 17231 dtrace_helptrace_buffer = NULL; 17232 } 17233 17234#ifdef illumos 17235 dtrace_state_destroy(state); 17236#else 17237 if (state != NULL) { 17238 dtrace_state_destroy(state); 17239 kmem_free(state, 0); 17240 } 17241#endif 17242 ASSERT(dtrace_opens > 0); 17243 17244#ifdef illumos 17245 /* 17246 * Only relinquish control of the kernel debugger interface when there 17247 * are no consumers and no anonymous enablings. 17248 */ 17249 if (--dtrace_opens == 0 && dtrace_anon.dta_enabling == NULL) 17250 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 17251#else 17252 --dtrace_opens; 17253#endif 17254 17255 if (buf != NULL) { 17256 kmem_free(buf, dtrace_helptrace_bufsize); 17257 dtrace_helptrace_disable = 0; 17258 } 17259 17260 mutex_exit(&dtrace_lock); 17261 mutex_exit(&cpu_lock); 17262 17263#ifdef illumos 17264 return (0); 17265#endif 17266} 17267 17268#ifdef illumos 17269/*ARGSUSED*/ 17270static int 17271dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 17272{ 17273 int rval; 17274 dof_helper_t help, *dhp = NULL; 17275 17276 switch (cmd) { 17277 case DTRACEHIOC_ADDDOF: 17278 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 17279 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 17280 return (EFAULT); 17281 } 17282 17283 dhp = &help; 17284 arg = (intptr_t)help.dofhp_dof; 17285 /*FALLTHROUGH*/ 17286 17287 case DTRACEHIOC_ADD: { 17288 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 17289 17290 if (dof == NULL) 17291 return (rval); 17292 17293 mutex_enter(&dtrace_lock); 17294 17295 /* 17296 * dtrace_helper_slurp() takes responsibility for the dof -- 17297 * it may free it now or it may save it and free it later. 17298 */ 17299 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 17300 *rv = rval; 17301 rval = 0; 17302 } else { 17303 rval = EINVAL; 17304 } 17305 17306 mutex_exit(&dtrace_lock); 17307 return (rval); 17308 } 17309 17310 case DTRACEHIOC_REMOVE: { 17311 mutex_enter(&dtrace_lock); 17312 rval = dtrace_helper_destroygen(NULL, arg); 17313 mutex_exit(&dtrace_lock); 17314 17315 return (rval); 17316 } 17317 17318 default: 17319 break; 17320 } 17321 17322 return (ENOTTY); 17323} 17324 17325/*ARGSUSED*/ 17326static int 17327dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 17328{ 17329 minor_t minor = getminor(dev); 17330 dtrace_state_t *state; 17331 int rval; 17332 17333 if (minor == DTRACEMNRN_HELPER) 17334 return (dtrace_ioctl_helper(cmd, arg, rv)); 17335 17336 state = ddi_get_soft_state(dtrace_softstate, minor); 17337 17338 if (state->dts_anon) { 17339 ASSERT(dtrace_anon.dta_state == NULL); 17340 state = state->dts_anon; 17341 } 17342 17343 switch (cmd) { 17344 case DTRACEIOC_PROVIDER: { 17345 dtrace_providerdesc_t pvd; 17346 dtrace_provider_t *pvp; 17347 17348 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 17349 return (EFAULT); 17350 17351 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 17352 mutex_enter(&dtrace_provider_lock); 17353 17354 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 17355 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 17356 break; 17357 } 17358 17359 mutex_exit(&dtrace_provider_lock); 17360 17361 if (pvp == NULL) 17362 return (ESRCH); 17363 17364 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 17365 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 17366 17367 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 17368 return (EFAULT); 17369 17370 return (0); 17371 } 17372 17373 case DTRACEIOC_EPROBE: { 17374 dtrace_eprobedesc_t epdesc; 17375 dtrace_ecb_t *ecb; 17376 dtrace_action_t *act; 17377 void *buf; 17378 size_t size; 17379 uintptr_t dest; 17380 int nrecs; 17381 17382 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 17383 return (EFAULT); 17384 17385 mutex_enter(&dtrace_lock); 17386 17387 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 17388 mutex_exit(&dtrace_lock); 17389 return (EINVAL); 17390 } 17391 17392 if (ecb->dte_probe == NULL) { 17393 mutex_exit(&dtrace_lock); 17394 return (EINVAL); 17395 } 17396 17397 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 17398 epdesc.dtepd_uarg = ecb->dte_uarg; 17399 epdesc.dtepd_size = ecb->dte_size; 17400 17401 nrecs = epdesc.dtepd_nrecs; 17402 epdesc.dtepd_nrecs = 0; 17403 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17404 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17405 continue; 17406 17407 epdesc.dtepd_nrecs++; 17408 } 17409 17410 /* 17411 * Now that we have the size, we need to allocate a temporary 17412 * buffer in which to store the complete description. We need 17413 * the temporary buffer to be able to drop dtrace_lock() 17414 * across the copyout(), below. 17415 */ 17416 size = sizeof (dtrace_eprobedesc_t) + 17417 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 17418 17419 buf = kmem_alloc(size, KM_SLEEP); 17420 dest = (uintptr_t)buf; 17421 17422 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 17423 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 17424 17425 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 17426 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 17427 continue; 17428 17429 if (nrecs-- == 0) 17430 break; 17431 17432 bcopy(&act->dta_rec, (void *)dest, 17433 sizeof (dtrace_recdesc_t)); 17434 dest += sizeof (dtrace_recdesc_t); 17435 } 17436 17437 mutex_exit(&dtrace_lock); 17438 17439 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17440 kmem_free(buf, size); 17441 return (EFAULT); 17442 } 17443 17444 kmem_free(buf, size); 17445 return (0); 17446 } 17447 17448 case DTRACEIOC_AGGDESC: { 17449 dtrace_aggdesc_t aggdesc; 17450 dtrace_action_t *act; 17451 dtrace_aggregation_t *agg; 17452 int nrecs; 17453 uint32_t offs; 17454 dtrace_recdesc_t *lrec; 17455 void *buf; 17456 size_t size; 17457 uintptr_t dest; 17458 17459 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 17460 return (EFAULT); 17461 17462 mutex_enter(&dtrace_lock); 17463 17464 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 17465 mutex_exit(&dtrace_lock); 17466 return (EINVAL); 17467 } 17468 17469 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 17470 17471 nrecs = aggdesc.dtagd_nrecs; 17472 aggdesc.dtagd_nrecs = 0; 17473 17474 offs = agg->dtag_base; 17475 lrec = &agg->dtag_action.dta_rec; 17476 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 17477 17478 for (act = agg->dtag_first; ; act = act->dta_next) { 17479 ASSERT(act->dta_intuple || 17480 DTRACEACT_ISAGG(act->dta_kind)); 17481 17482 /* 17483 * If this action has a record size of zero, it 17484 * denotes an argument to the aggregating action. 17485 * Because the presence of this record doesn't (or 17486 * shouldn't) affect the way the data is interpreted, 17487 * we don't copy it out to save user-level the 17488 * confusion of dealing with a zero-length record. 17489 */ 17490 if (act->dta_rec.dtrd_size == 0) { 17491 ASSERT(agg->dtag_hasarg); 17492 continue; 17493 } 17494 17495 aggdesc.dtagd_nrecs++; 17496 17497 if (act == &agg->dtag_action) 17498 break; 17499 } 17500 17501 /* 17502 * Now that we have the size, we need to allocate a temporary 17503 * buffer in which to store the complete description. We need 17504 * the temporary buffer to be able to drop dtrace_lock() 17505 * across the copyout(), below. 17506 */ 17507 size = sizeof (dtrace_aggdesc_t) + 17508 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 17509 17510 buf = kmem_alloc(size, KM_SLEEP); 17511 dest = (uintptr_t)buf; 17512 17513 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 17514 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 17515 17516 for (act = agg->dtag_first; ; act = act->dta_next) { 17517 dtrace_recdesc_t rec = act->dta_rec; 17518 17519 /* 17520 * See the comment in the above loop for why we pass 17521 * over zero-length records. 17522 */ 17523 if (rec.dtrd_size == 0) { 17524 ASSERT(agg->dtag_hasarg); 17525 continue; 17526 } 17527 17528 if (nrecs-- == 0) 17529 break; 17530 17531 rec.dtrd_offset -= offs; 17532 bcopy(&rec, (void *)dest, sizeof (rec)); 17533 dest += sizeof (dtrace_recdesc_t); 17534 17535 if (act == &agg->dtag_action) 17536 break; 17537 } 17538 17539 mutex_exit(&dtrace_lock); 17540 17541 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 17542 kmem_free(buf, size); 17543 return (EFAULT); 17544 } 17545 17546 kmem_free(buf, size); 17547 return (0); 17548 } 17549 17550 case DTRACEIOC_ENABLE: { 17551 dof_hdr_t *dof; 17552 dtrace_enabling_t *enab = NULL; 17553 dtrace_vstate_t *vstate; 17554 int err = 0; 17555 17556 *rv = 0; 17557 17558 /* 17559 * If a NULL argument has been passed, we take this as our 17560 * cue to reevaluate our enablings. 17561 */ 17562 if (arg == NULL) { 17563 dtrace_enabling_matchall(); 17564 17565 return (0); 17566 } 17567 17568 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 17569 return (rval); 17570 17571 mutex_enter(&cpu_lock); 17572 mutex_enter(&dtrace_lock); 17573 vstate = &state->dts_vstate; 17574 17575 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 17576 mutex_exit(&dtrace_lock); 17577 mutex_exit(&cpu_lock); 17578 dtrace_dof_destroy(dof); 17579 return (EBUSY); 17580 } 17581 17582 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 17583 mutex_exit(&dtrace_lock); 17584 mutex_exit(&cpu_lock); 17585 dtrace_dof_destroy(dof); 17586 return (EINVAL); 17587 } 17588 17589 if ((rval = dtrace_dof_options(dof, state)) != 0) { 17590 dtrace_enabling_destroy(enab); 17591 mutex_exit(&dtrace_lock); 17592 mutex_exit(&cpu_lock); 17593 dtrace_dof_destroy(dof); 17594 return (rval); 17595 } 17596 17597 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 17598 err = dtrace_enabling_retain(enab); 17599 } else { 17600 dtrace_enabling_destroy(enab); 17601 } 17602 17603 mutex_exit(&cpu_lock); 17604 mutex_exit(&dtrace_lock); 17605 dtrace_dof_destroy(dof); 17606 17607 return (err); 17608 } 17609 17610 case DTRACEIOC_REPLICATE: { 17611 dtrace_repldesc_t desc; 17612 dtrace_probedesc_t *match = &desc.dtrpd_match; 17613 dtrace_probedesc_t *create = &desc.dtrpd_create; 17614 int err; 17615 17616 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17617 return (EFAULT); 17618 17619 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17620 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17621 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17622 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17623 17624 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17625 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17626 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17627 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17628 17629 mutex_enter(&dtrace_lock); 17630 err = dtrace_enabling_replicate(state, match, create); 17631 mutex_exit(&dtrace_lock); 17632 17633 return (err); 17634 } 17635 17636 case DTRACEIOC_PROBEMATCH: 17637 case DTRACEIOC_PROBES: { 17638 dtrace_probe_t *probe = NULL; 17639 dtrace_probedesc_t desc; 17640 dtrace_probekey_t pkey; 17641 dtrace_id_t i; 17642 int m = 0; 17643 uint32_t priv; 17644 uid_t uid; 17645 zoneid_t zoneid; 17646 17647 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17648 return (EFAULT); 17649 17650 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 17651 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 17652 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 17653 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 17654 17655 /* 17656 * Before we attempt to match this probe, we want to give 17657 * all providers the opportunity to provide it. 17658 */ 17659 if (desc.dtpd_id == DTRACE_IDNONE) { 17660 mutex_enter(&dtrace_provider_lock); 17661 dtrace_probe_provide(&desc, NULL); 17662 mutex_exit(&dtrace_provider_lock); 17663 desc.dtpd_id++; 17664 } 17665 17666 if (cmd == DTRACEIOC_PROBEMATCH) { 17667 dtrace_probekey(&desc, &pkey); 17668 pkey.dtpk_id = DTRACE_IDNONE; 17669 } 17670 17671 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 17672 17673 mutex_enter(&dtrace_lock); 17674 17675 if (cmd == DTRACEIOC_PROBEMATCH) { 17676 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17677 if ((probe = dtrace_probes[i - 1]) != NULL && 17678 (m = dtrace_match_probe(probe, &pkey, 17679 priv, uid, zoneid)) != 0) 17680 break; 17681 } 17682 17683 if (m < 0) { 17684 mutex_exit(&dtrace_lock); 17685 return (EINVAL); 17686 } 17687 17688 } else { 17689 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 17690 if ((probe = dtrace_probes[i - 1]) != NULL && 17691 dtrace_match_priv(probe, priv, uid, zoneid)) 17692 break; 17693 } 17694 } 17695 17696 if (probe == NULL) { 17697 mutex_exit(&dtrace_lock); 17698 return (ESRCH); 17699 } 17700 17701 dtrace_probe_description(probe, &desc); 17702 mutex_exit(&dtrace_lock); 17703 17704 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17705 return (EFAULT); 17706 17707 return (0); 17708 } 17709 17710 case DTRACEIOC_PROBEARG: { 17711 dtrace_argdesc_t desc; 17712 dtrace_probe_t *probe; 17713 dtrace_provider_t *prov; 17714 17715 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17716 return (EFAULT); 17717 17718 if (desc.dtargd_id == DTRACE_IDNONE) 17719 return (EINVAL); 17720 17721 if (desc.dtargd_ndx == DTRACE_ARGNONE) 17722 return (EINVAL); 17723 17724 mutex_enter(&dtrace_provider_lock); 17725 mutex_enter(&mod_lock); 17726 mutex_enter(&dtrace_lock); 17727 17728 if (desc.dtargd_id > dtrace_nprobes) { 17729 mutex_exit(&dtrace_lock); 17730 mutex_exit(&mod_lock); 17731 mutex_exit(&dtrace_provider_lock); 17732 return (EINVAL); 17733 } 17734 17735 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 17736 mutex_exit(&dtrace_lock); 17737 mutex_exit(&mod_lock); 17738 mutex_exit(&dtrace_provider_lock); 17739 return (EINVAL); 17740 } 17741 17742 mutex_exit(&dtrace_lock); 17743 17744 prov = probe->dtpr_provider; 17745 17746 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 17747 /* 17748 * There isn't any typed information for this probe. 17749 * Set the argument number to DTRACE_ARGNONE. 17750 */ 17751 desc.dtargd_ndx = DTRACE_ARGNONE; 17752 } else { 17753 desc.dtargd_native[0] = '\0'; 17754 desc.dtargd_xlate[0] = '\0'; 17755 desc.dtargd_mapping = desc.dtargd_ndx; 17756 17757 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 17758 probe->dtpr_id, probe->dtpr_arg, &desc); 17759 } 17760 17761 mutex_exit(&mod_lock); 17762 mutex_exit(&dtrace_provider_lock); 17763 17764 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17765 return (EFAULT); 17766 17767 return (0); 17768 } 17769 17770 case DTRACEIOC_GO: { 17771 processorid_t cpuid; 17772 rval = dtrace_state_go(state, &cpuid); 17773 17774 if (rval != 0) 17775 return (rval); 17776 17777 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17778 return (EFAULT); 17779 17780 return (0); 17781 } 17782 17783 case DTRACEIOC_STOP: { 17784 processorid_t cpuid; 17785 17786 mutex_enter(&dtrace_lock); 17787 rval = dtrace_state_stop(state, &cpuid); 17788 mutex_exit(&dtrace_lock); 17789 17790 if (rval != 0) 17791 return (rval); 17792 17793 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 17794 return (EFAULT); 17795 17796 return (0); 17797 } 17798 17799 case DTRACEIOC_DOFGET: { 17800 dof_hdr_t hdr, *dof; 17801 uint64_t len; 17802 17803 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 17804 return (EFAULT); 17805 17806 mutex_enter(&dtrace_lock); 17807 dof = dtrace_dof_create(state); 17808 mutex_exit(&dtrace_lock); 17809 17810 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 17811 rval = copyout(dof, (void *)arg, len); 17812 dtrace_dof_destroy(dof); 17813 17814 return (rval == 0 ? 0 : EFAULT); 17815 } 17816 17817 case DTRACEIOC_AGGSNAP: 17818 case DTRACEIOC_BUFSNAP: { 17819 dtrace_bufdesc_t desc; 17820 caddr_t cached; 17821 dtrace_buffer_t *buf; 17822 17823 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 17824 return (EFAULT); 17825 17826 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 17827 return (EINVAL); 17828 17829 mutex_enter(&dtrace_lock); 17830 17831 if (cmd == DTRACEIOC_BUFSNAP) { 17832 buf = &state->dts_buffer[desc.dtbd_cpu]; 17833 } else { 17834 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 17835 } 17836 17837 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 17838 size_t sz = buf->dtb_offset; 17839 17840 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 17841 mutex_exit(&dtrace_lock); 17842 return (EBUSY); 17843 } 17844 17845 /* 17846 * If this buffer has already been consumed, we're 17847 * going to indicate that there's nothing left here 17848 * to consume. 17849 */ 17850 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 17851 mutex_exit(&dtrace_lock); 17852 17853 desc.dtbd_size = 0; 17854 desc.dtbd_drops = 0; 17855 desc.dtbd_errors = 0; 17856 desc.dtbd_oldest = 0; 17857 sz = sizeof (desc); 17858 17859 if (copyout(&desc, (void *)arg, sz) != 0) 17860 return (EFAULT); 17861 17862 return (0); 17863 } 17864 17865 /* 17866 * If this is a ring buffer that has wrapped, we want 17867 * to copy the whole thing out. 17868 */ 17869 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 17870 dtrace_buffer_polish(buf); 17871 sz = buf->dtb_size; 17872 } 17873 17874 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 17875 mutex_exit(&dtrace_lock); 17876 return (EFAULT); 17877 } 17878 17879 desc.dtbd_size = sz; 17880 desc.dtbd_drops = buf->dtb_drops; 17881 desc.dtbd_errors = buf->dtb_errors; 17882 desc.dtbd_oldest = buf->dtb_xamot_offset; 17883 desc.dtbd_timestamp = dtrace_gethrtime(); 17884 17885 mutex_exit(&dtrace_lock); 17886 17887 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17888 return (EFAULT); 17889 17890 buf->dtb_flags |= DTRACEBUF_CONSUMED; 17891 17892 return (0); 17893 } 17894 17895 if (buf->dtb_tomax == NULL) { 17896 ASSERT(buf->dtb_xamot == NULL); 17897 mutex_exit(&dtrace_lock); 17898 return (ENOENT); 17899 } 17900 17901 cached = buf->dtb_tomax; 17902 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 17903 17904 dtrace_xcall(desc.dtbd_cpu, 17905 (dtrace_xcall_t)dtrace_buffer_switch, buf); 17906 17907 state->dts_errors += buf->dtb_xamot_errors; 17908 17909 /* 17910 * If the buffers did not actually switch, then the cross call 17911 * did not take place -- presumably because the given CPU is 17912 * not in the ready set. If this is the case, we'll return 17913 * ENOENT. 17914 */ 17915 if (buf->dtb_tomax == cached) { 17916 ASSERT(buf->dtb_xamot != cached); 17917 mutex_exit(&dtrace_lock); 17918 return (ENOENT); 17919 } 17920 17921 ASSERT(cached == buf->dtb_xamot); 17922 17923 /* 17924 * We have our snapshot; now copy it out. 17925 */ 17926 if (copyout(buf->dtb_xamot, desc.dtbd_data, 17927 buf->dtb_xamot_offset) != 0) { 17928 mutex_exit(&dtrace_lock); 17929 return (EFAULT); 17930 } 17931 17932 desc.dtbd_size = buf->dtb_xamot_offset; 17933 desc.dtbd_drops = buf->dtb_xamot_drops; 17934 desc.dtbd_errors = buf->dtb_xamot_errors; 17935 desc.dtbd_oldest = 0; 17936 desc.dtbd_timestamp = buf->dtb_switched; 17937 17938 mutex_exit(&dtrace_lock); 17939 17940 /* 17941 * Finally, copy out the buffer description. 17942 */ 17943 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 17944 return (EFAULT); 17945 17946 return (0); 17947 } 17948 17949 case DTRACEIOC_CONF: { 17950 dtrace_conf_t conf; 17951 17952 bzero(&conf, sizeof (conf)); 17953 conf.dtc_difversion = DIF_VERSION; 17954 conf.dtc_difintregs = DIF_DIR_NREGS; 17955 conf.dtc_diftupregs = DIF_DTR_NREGS; 17956 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 17957 17958 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 17959 return (EFAULT); 17960 17961 return (0); 17962 } 17963 17964 case DTRACEIOC_STATUS: { 17965 dtrace_status_t stat; 17966 dtrace_dstate_t *dstate; 17967 int i, j; 17968 uint64_t nerrs; 17969 17970 /* 17971 * See the comment in dtrace_state_deadman() for the reason 17972 * for setting dts_laststatus to INT64_MAX before setting 17973 * it to the correct value. 17974 */ 17975 state->dts_laststatus = INT64_MAX; 17976 dtrace_membar_producer(); 17977 state->dts_laststatus = dtrace_gethrtime(); 17978 17979 bzero(&stat, sizeof (stat)); 17980 17981 mutex_enter(&dtrace_lock); 17982 17983 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 17984 mutex_exit(&dtrace_lock); 17985 return (ENOENT); 17986 } 17987 17988 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 17989 stat.dtst_exiting = 1; 17990 17991 nerrs = state->dts_errors; 17992 dstate = &state->dts_vstate.dtvs_dynvars; 17993 17994 for (i = 0; i < NCPU; i++) { 17995 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 17996 17997 stat.dtst_dyndrops += dcpu->dtdsc_drops; 17998 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 17999 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 18000 18001 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 18002 stat.dtst_filled++; 18003 18004 nerrs += state->dts_buffer[i].dtb_errors; 18005 18006 for (j = 0; j < state->dts_nspeculations; j++) { 18007 dtrace_speculation_t *spec; 18008 dtrace_buffer_t *buf; 18009 18010 spec = &state->dts_speculations[j]; 18011 buf = &spec->dtsp_buffer[i]; 18012 stat.dtst_specdrops += buf->dtb_xamot_drops; 18013 } 18014 } 18015 18016 stat.dtst_specdrops_busy = state->dts_speculations_busy; 18017 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 18018 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 18019 stat.dtst_dblerrors = state->dts_dblerrors; 18020 stat.dtst_killed = 18021 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 18022 stat.dtst_errors = nerrs; 18023 18024 mutex_exit(&dtrace_lock); 18025 18026 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 18027 return (EFAULT); 18028 18029 return (0); 18030 } 18031 18032 case DTRACEIOC_FORMAT: { 18033 dtrace_fmtdesc_t fmt; 18034 char *str; 18035 int len; 18036 18037 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 18038 return (EFAULT); 18039 18040 mutex_enter(&dtrace_lock); 18041 18042 if (fmt.dtfd_format == 0 || 18043 fmt.dtfd_format > state->dts_nformats) { 18044 mutex_exit(&dtrace_lock); 18045 return (EINVAL); 18046 } 18047 18048 /* 18049 * Format strings are allocated contiguously and they are 18050 * never freed; if a format index is less than the number 18051 * of formats, we can assert that the format map is non-NULL 18052 * and that the format for the specified index is non-NULL. 18053 */ 18054 ASSERT(state->dts_formats != NULL); 18055 str = state->dts_formats[fmt.dtfd_format - 1]; 18056 ASSERT(str != NULL); 18057 18058 len = strlen(str) + 1; 18059 18060 if (len > fmt.dtfd_length) { 18061 fmt.dtfd_length = len; 18062 18063 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 18064 mutex_exit(&dtrace_lock); 18065 return (EINVAL); 18066 } 18067 } else { 18068 if (copyout(str, fmt.dtfd_string, len) != 0) { 18069 mutex_exit(&dtrace_lock); 18070 return (EINVAL); 18071 } 18072 } 18073 18074 mutex_exit(&dtrace_lock); 18075 return (0); 18076 } 18077 18078 default: 18079 break; 18080 } 18081 18082 return (ENOTTY); 18083} 18084 18085/*ARGSUSED*/ 18086static int 18087dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 18088{ 18089 dtrace_state_t *state; 18090 18091 switch (cmd) { 18092 case DDI_DETACH: 18093 break; 18094 18095 case DDI_SUSPEND: 18096 return (DDI_SUCCESS); 18097 18098 default: 18099 return (DDI_FAILURE); 18100 } 18101 18102 mutex_enter(&cpu_lock); 18103 mutex_enter(&dtrace_provider_lock); 18104 mutex_enter(&dtrace_lock); 18105 18106 ASSERT(dtrace_opens == 0); 18107 18108 if (dtrace_helpers > 0) { 18109 mutex_exit(&dtrace_provider_lock); 18110 mutex_exit(&dtrace_lock); 18111 mutex_exit(&cpu_lock); 18112 return (DDI_FAILURE); 18113 } 18114 18115 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 18116 mutex_exit(&dtrace_provider_lock); 18117 mutex_exit(&dtrace_lock); 18118 mutex_exit(&cpu_lock); 18119 return (DDI_FAILURE); 18120 } 18121 18122 dtrace_provider = NULL; 18123 18124 if ((state = dtrace_anon_grab()) != NULL) { 18125 /* 18126 * If there were ECBs on this state, the provider should 18127 * have not been allowed to detach; assert that there is 18128 * none. 18129 */ 18130 ASSERT(state->dts_necbs == 0); 18131 dtrace_state_destroy(state); 18132 18133 /* 18134 * If we're being detached with anonymous state, we need to 18135 * indicate to the kernel debugger that DTrace is now inactive. 18136 */ 18137 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 18138 } 18139 18140 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 18141 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 18142 dtrace_cpu_init = NULL; 18143 dtrace_helpers_cleanup = NULL; 18144 dtrace_helpers_fork = NULL; 18145 dtrace_cpustart_init = NULL; 18146 dtrace_cpustart_fini = NULL; 18147 dtrace_debugger_init = NULL; 18148 dtrace_debugger_fini = NULL; 18149 dtrace_modload = NULL; 18150 dtrace_modunload = NULL; 18151 18152 ASSERT(dtrace_getf == 0); 18153 ASSERT(dtrace_closef == NULL); 18154 18155 mutex_exit(&cpu_lock); 18156 18157 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 18158 dtrace_probes = NULL; 18159 dtrace_nprobes = 0; 18160 18161 dtrace_hash_destroy(dtrace_bymod); 18162 dtrace_hash_destroy(dtrace_byfunc); 18163 dtrace_hash_destroy(dtrace_byname); 18164 dtrace_bymod = NULL; 18165 dtrace_byfunc = NULL; 18166 dtrace_byname = NULL; 18167 18168 kmem_cache_destroy(dtrace_state_cache); 18169 vmem_destroy(dtrace_minor); 18170 vmem_destroy(dtrace_arena); 18171 18172 if (dtrace_toxrange != NULL) { 18173 kmem_free(dtrace_toxrange, 18174 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 18175 dtrace_toxrange = NULL; 18176 dtrace_toxranges = 0; 18177 dtrace_toxranges_max = 0; 18178 } 18179 18180 ddi_remove_minor_node(dtrace_devi, NULL); 18181 dtrace_devi = NULL; 18182 18183 ddi_soft_state_fini(&dtrace_softstate); 18184 18185 ASSERT(dtrace_vtime_references == 0); 18186 ASSERT(dtrace_opens == 0); 18187 ASSERT(dtrace_retained == NULL); 18188 18189 mutex_exit(&dtrace_lock); 18190 mutex_exit(&dtrace_provider_lock); 18191 18192 /* 18193 * We don't destroy the task queue until after we have dropped our 18194 * locks (taskq_destroy() may block on running tasks). To prevent 18195 * attempting to do work after we have effectively detached but before 18196 * the task queue has been destroyed, all tasks dispatched via the 18197 * task queue must check that DTrace is still attached before 18198 * performing any operation. 18199 */ 18200 taskq_destroy(dtrace_taskq); 18201 dtrace_taskq = NULL; 18202 18203 return (DDI_SUCCESS); 18204} 18205#endif 18206 18207#ifdef illumos 18208/*ARGSUSED*/ 18209static int 18210dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 18211{ 18212 int error; 18213 18214 switch (infocmd) { 18215 case DDI_INFO_DEVT2DEVINFO: 18216 *result = (void *)dtrace_devi; 18217 error = DDI_SUCCESS; 18218 break; 18219 case DDI_INFO_DEVT2INSTANCE: 18220 *result = (void *)0; 18221 error = DDI_SUCCESS; 18222 break; 18223 default: 18224 error = DDI_FAILURE; 18225 } 18226 return (error); 18227} 18228#endif 18229 18230#ifdef illumos 18231static struct cb_ops dtrace_cb_ops = { 18232 dtrace_open, /* open */ 18233 dtrace_close, /* close */ 18234 nulldev, /* strategy */ 18235 nulldev, /* print */ 18236 nodev, /* dump */ 18237 nodev, /* read */ 18238 nodev, /* write */ 18239 dtrace_ioctl, /* ioctl */ 18240 nodev, /* devmap */ 18241 nodev, /* mmap */ 18242 nodev, /* segmap */ 18243 nochpoll, /* poll */ 18244 ddi_prop_op, /* cb_prop_op */ 18245 0, /* streamtab */ 18246 D_NEW | D_MP /* Driver compatibility flag */ 18247}; 18248 18249static struct dev_ops dtrace_ops = { 18250 DEVO_REV, /* devo_rev */ 18251 0, /* refcnt */ 18252 dtrace_info, /* get_dev_info */ 18253 nulldev, /* identify */ 18254 nulldev, /* probe */ 18255 dtrace_attach, /* attach */ 18256 dtrace_detach, /* detach */ 18257 nodev, /* reset */ 18258 &dtrace_cb_ops, /* driver operations */ 18259 NULL, /* bus operations */ 18260 nodev /* dev power */ 18261}; 18262 18263static struct modldrv modldrv = { 18264 &mod_driverops, /* module type (this is a pseudo driver) */ 18265 "Dynamic Tracing", /* name of module */ 18266 &dtrace_ops, /* driver ops */ 18267}; 18268 18269static struct modlinkage modlinkage = { 18270 MODREV_1, 18271 (void *)&modldrv, 18272 NULL 18273}; 18274 18275int 18276_init(void) 18277{ 18278 return (mod_install(&modlinkage)); 18279} 18280 18281int 18282_info(struct modinfo *modinfop) 18283{ 18284 return (mod_info(&modlinkage, modinfop)); 18285} 18286 18287int 18288_fini(void) 18289{ 18290 return (mod_remove(&modlinkage)); 18291} 18292#else 18293 18294static d_ioctl_t dtrace_ioctl; 18295static d_ioctl_t dtrace_ioctl_helper; 18296static void dtrace_load(void *); 18297static int dtrace_unload(void); 18298static struct cdev *dtrace_dev; 18299static struct cdev *helper_dev; 18300 18301void dtrace_invop_init(void); 18302void dtrace_invop_uninit(void); 18303 18304static struct cdevsw dtrace_cdevsw = { 18305 .d_version = D_VERSION, 18306 .d_ioctl = dtrace_ioctl, 18307 .d_open = dtrace_open, 18308 .d_name = "dtrace", 18309}; 18310 18311static struct cdevsw helper_cdevsw = { 18312 .d_version = D_VERSION, 18313 .d_ioctl = dtrace_ioctl_helper, 18314 .d_name = "helper", 18315}; 18316 18317#include <dtrace_anon.c> 18318#include <dtrace_ioctl.c> 18319#include <dtrace_load.c> 18320#include <dtrace_modevent.c> 18321#include <dtrace_sysctl.c> 18322#include <dtrace_unload.c> 18323#include <dtrace_vtime.c> 18324#include <dtrace_hacks.c> 18325#include <dtrace_isa.c> 18326 18327SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 18328SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 18329SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 18330 18331DEV_MODULE(dtrace, dtrace_modevent, NULL); 18332MODULE_VERSION(dtrace, 1); 18333MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 18334#endif 18335