dtrace.c revision 179307
1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22/* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27#pragma ident "%Z%%M% %I% %E% SMI" 28 29/* 30 * DTrace - Dynamic Tracing for Solaris 31 * 32 * This is the implementation of the Solaris Dynamic Tracing framework 33 * (DTrace). The user-visible interface to DTrace is described at length in 34 * the "Solaris Dynamic Tracing Guide". The interfaces between the libdtrace 35 * library, the in-kernel DTrace framework, and the DTrace providers are 36 * described in the block comments in the <sys/dtrace.h> header file. The 37 * internal architecture of DTrace is described in the block comments in the 38 * <sys/dtrace_impl.h> header file. The comments contained within the DTrace 39 * implementation very much assume mastery of all of these sources; if one has 40 * an unanswered question about the implementation, one should consult them 41 * first. 42 * 43 * The functions here are ordered roughly as follows: 44 * 45 * - Probe context functions 46 * - Probe hashing functions 47 * - Non-probe context utility functions 48 * - Matching functions 49 * - Provider-to-Framework API functions 50 * - Probe management functions 51 * - DIF object functions 52 * - Format functions 53 * - Predicate functions 54 * - ECB functions 55 * - Buffer functions 56 * - Enabling functions 57 * - DOF functions 58 * - Anonymous enabling functions 59 * - Consumer state functions 60 * - Helper functions 61 * - Hook functions 62 * - Driver cookbook functions 63 * 64 * Each group of functions begins with a block comment labelled the "DTrace 65 * [Group] Functions", allowing one to find each block by searching forward 66 * on capital-f functions. 67 */ 68#include <sys/errno.h> 69#if !defined(sun) 70#include <sys/time.h> 71#endif 72#include <sys/stat.h> 73#include <sys/modctl.h> 74#include <sys/conf.h> 75#include <sys/systm.h> 76#if defined(sun) 77#include <sys/ddi.h> 78#include <sys/sunddi.h> 79#endif 80#include <sys/cpuvar.h> 81#include <sys/kmem.h> 82#if defined(sun) 83#include <sys/strsubr.h> 84#endif 85#include <sys/sysmacros.h> 86#include <sys/dtrace_impl.h> 87#include <sys/atomic.h> 88#include <sys/cmn_err.h> 89#if defined(sun) 90#include <sys/mutex_impl.h> 91#include <sys/rwlock_impl.h> 92#endif 93#include <sys/ctf_api.h> 94#if defined(sun) 95#include <sys/panic.h> 96#include <sys/priv_impl.h> 97#endif 98#include <sys/policy.h> 99#if defined(sun) 100#include <sys/cred_impl.h> 101#include <sys/procfs_isa.h> 102#endif 103#include <sys/taskq.h> 104#if defined(sun) 105#include <sys/mkdev.h> 106#include <sys/kdi.h> 107#endif 108#include <sys/zone.h> 109#include <sys/socket.h> 110#include <netinet/in.h> 111 112/* FreeBSD includes: */ 113#if !defined(sun) 114#include <sys/ctype.h> 115#include <sys/limits.h> 116#include <sys/kdb.h> 117#include <sys/kernel.h> 118#include <sys/malloc.h> 119#include <sys/sysctl.h> 120#include <sys/lock.h> 121#include <sys/mutex.h> 122#include <sys/sx.h> 123#include <sys/dtrace_bsd.h> 124#include <netinet/in.h> 125#include "dtrace_cddl.h" 126#include "dtrace_debug.c" 127#endif 128 129/* 130 * DTrace Tunable Variables 131 * 132 * The following variables may be tuned by adding a line to /etc/system that 133 * includes both the name of the DTrace module ("dtrace") and the name of the 134 * variable. For example: 135 * 136 * set dtrace:dtrace_destructive_disallow = 1 137 * 138 * In general, the only variables that one should be tuning this way are those 139 * that affect system-wide DTrace behavior, and for which the default behavior 140 * is undesirable. Most of these variables are tunable on a per-consumer 141 * basis using DTrace options, and need not be tuned on a system-wide basis. 142 * When tuning these variables, avoid pathological values; while some attempt 143 * is made to verify the integrity of these variables, they are not considered 144 * part of the supported interface to DTrace, and they are therefore not 145 * checked comprehensively. Further, these variables should not be tuned 146 * dynamically via "mdb -kw" or other means; they should only be tuned via 147 * /etc/system. 148 */ 149int dtrace_destructive_disallow = 0; 150dtrace_optval_t dtrace_nonroot_maxsize = (16 * 1024 * 1024); 151size_t dtrace_difo_maxsize = (256 * 1024); 152dtrace_optval_t dtrace_dof_maxsize = (256 * 1024); 153size_t dtrace_global_maxsize = (16 * 1024); 154size_t dtrace_actions_max = (16 * 1024); 155size_t dtrace_retain_max = 1024; 156dtrace_optval_t dtrace_helper_actions_max = 32; 157dtrace_optval_t dtrace_helper_providers_max = 32; 158dtrace_optval_t dtrace_dstate_defsize = (1 * 1024 * 1024); 159size_t dtrace_strsize_default = 256; 160dtrace_optval_t dtrace_cleanrate_default = 9900990; /* 101 hz */ 161dtrace_optval_t dtrace_cleanrate_min = 200000; /* 5000 hz */ 162dtrace_optval_t dtrace_cleanrate_max = (uint64_t)60 * NANOSEC; /* 1/minute */ 163dtrace_optval_t dtrace_aggrate_default = NANOSEC; /* 1 hz */ 164dtrace_optval_t dtrace_statusrate_default = NANOSEC; /* 1 hz */ 165dtrace_optval_t dtrace_statusrate_max = (hrtime_t)10 * NANOSEC; /* 6/minute */ 166dtrace_optval_t dtrace_switchrate_default = NANOSEC; /* 1 hz */ 167dtrace_optval_t dtrace_nspec_default = 1; 168dtrace_optval_t dtrace_specsize_default = 32 * 1024; 169dtrace_optval_t dtrace_stackframes_default = 20; 170dtrace_optval_t dtrace_ustackframes_default = 20; 171dtrace_optval_t dtrace_jstackframes_default = 50; 172dtrace_optval_t dtrace_jstackstrsize_default = 512; 173int dtrace_msgdsize_max = 128; 174hrtime_t dtrace_chill_max = 500 * (NANOSEC / MILLISEC); /* 500 ms */ 175hrtime_t dtrace_chill_interval = NANOSEC; /* 1000 ms */ 176int dtrace_devdepth_max = 32; 177int dtrace_err_verbose; 178hrtime_t dtrace_deadman_interval = NANOSEC; 179hrtime_t dtrace_deadman_timeout = (hrtime_t)10 * NANOSEC; 180hrtime_t dtrace_deadman_user = (hrtime_t)30 * NANOSEC; 181 182/* 183 * DTrace External Variables 184 * 185 * As dtrace(7D) is a kernel module, any DTrace variables are obviously 186 * available to DTrace consumers via the backtick (`) syntax. One of these, 187 * dtrace_zero, is made deliberately so: it is provided as a source of 188 * well-known, zero-filled memory. While this variable is not documented, 189 * it is used by some translators as an implementation detail. 190 */ 191const char dtrace_zero[256] = { 0 }; /* zero-filled memory */ 192 193/* 194 * DTrace Internal Variables 195 */ 196#if defined(sun) 197static dev_info_t *dtrace_devi; /* device info */ 198#endif 199#if defined(sun) 200static vmem_t *dtrace_arena; /* probe ID arena */ 201static vmem_t *dtrace_minor; /* minor number arena */ 202static taskq_t *dtrace_taskq; /* task queue */ 203#else 204static struct unrhdr *dtrace_arena; /* Probe ID number. */ 205#endif 206static dtrace_probe_t **dtrace_probes; /* array of all probes */ 207static int dtrace_nprobes; /* number of probes */ 208static dtrace_provider_t *dtrace_provider; /* provider list */ 209static dtrace_meta_t *dtrace_meta_pid; /* user-land meta provider */ 210static int dtrace_opens; /* number of opens */ 211static int dtrace_helpers; /* number of helpers */ 212#if defined(sun) 213static void *dtrace_softstate; /* softstate pointer */ 214#endif 215static dtrace_hash_t *dtrace_bymod; /* probes hashed by module */ 216static dtrace_hash_t *dtrace_byfunc; /* probes hashed by function */ 217static dtrace_hash_t *dtrace_byname; /* probes hashed by name */ 218static dtrace_toxrange_t *dtrace_toxrange; /* toxic range array */ 219static int dtrace_toxranges; /* number of toxic ranges */ 220static int dtrace_toxranges_max; /* size of toxic range array */ 221static dtrace_anon_t dtrace_anon; /* anonymous enabling */ 222static kmem_cache_t *dtrace_state_cache; /* cache for dynamic state */ 223static uint64_t dtrace_vtime_references; /* number of vtimestamp refs */ 224static kthread_t *dtrace_panicked; /* panicking thread */ 225static dtrace_ecb_t *dtrace_ecb_create_cache; /* cached created ECB */ 226static dtrace_genid_t dtrace_probegen; /* current probe generation */ 227static dtrace_helpers_t *dtrace_deferred_pid; /* deferred helper list */ 228static dtrace_enabling_t *dtrace_retained; /* list of retained enablings */ 229static dtrace_dynvar_t dtrace_dynhash_sink; /* end of dynamic hash chains */ 230#if !defined(sun) 231static struct mtx dtrace_unr_mtx; 232MTX_SYSINIT(dtrace_unr_mtx, &dtrace_unr_mtx, "Unique resource identifier", MTX_DEF); 233int dtrace_in_probe; /* non-zero if executing a probe */ 234#if defined(__i386__) || defined(__amd64__) 235uintptr_t dtrace_in_probe_addr; /* Address of invop when already in probe */ 236#endif 237#endif 238 239/* 240 * DTrace Locking 241 * DTrace is protected by three (relatively coarse-grained) locks: 242 * 243 * (1) dtrace_lock is required to manipulate essentially any DTrace state, 244 * including enabling state, probes, ECBs, consumer state, helper state, 245 * etc. Importantly, dtrace_lock is _not_ required when in probe context; 246 * probe context is lock-free -- synchronization is handled via the 247 * dtrace_sync() cross call mechanism. 248 * 249 * (2) dtrace_provider_lock is required when manipulating provider state, or 250 * when provider state must be held constant. 251 * 252 * (3) dtrace_meta_lock is required when manipulating meta provider state, or 253 * when meta provider state must be held constant. 254 * 255 * The lock ordering between these three locks is dtrace_meta_lock before 256 * dtrace_provider_lock before dtrace_lock. (In particular, there are 257 * several places where dtrace_provider_lock is held by the framework as it 258 * calls into the providers -- which then call back into the framework, 259 * grabbing dtrace_lock.) 260 * 261 * There are two other locks in the mix: mod_lock and cpu_lock. With respect 262 * to dtrace_provider_lock and dtrace_lock, cpu_lock continues its historical 263 * role as a coarse-grained lock; it is acquired before both of these locks. 264 * With respect to dtrace_meta_lock, its behavior is stranger: cpu_lock must 265 * be acquired _between_ dtrace_meta_lock and any other DTrace locks. 266 * mod_lock is similar with respect to dtrace_provider_lock in that it must be 267 * acquired _between_ dtrace_provider_lock and dtrace_lock. 268 */ 269static kmutex_t dtrace_lock; /* probe state lock */ 270static kmutex_t dtrace_provider_lock; /* provider state lock */ 271static kmutex_t dtrace_meta_lock; /* meta-provider state lock */ 272 273#if !defined(sun) 274/* XXX FreeBSD hacks. */ 275static kmutex_t mod_lock; 276 277#define cr_suid cr_svuid 278#define cr_sgid cr_svgid 279#define ipaddr_t in_addr_t 280#define mod_modname pathname 281#define vuprintf vprintf 282#define ttoproc(_a) ((_a)->td_proc) 283#define crgetzoneid(_a) 0 284#define NCPU MAXCPU 285#define SNOCD 0 286#define CPU_ON_INTR(_a) 0 287 288#define PRIV_EFFECTIVE (1 << 0) 289#define PRIV_DTRACE_KERNEL (1 << 1) 290#define PRIV_DTRACE_PROC (1 << 2) 291#define PRIV_DTRACE_USER (1 << 3) 292#define PRIV_PROC_OWNER (1 << 4) 293#define PRIV_PROC_ZONE (1 << 5) 294#define PRIV_ALL ~0 295 296SYSCTL_NODE(_debug, OID_AUTO, dtrace, CTLFLAG_RD, 0, "DTrace Information"); 297#endif 298 299#if defined(sun) 300#define curcpu CPU->cpu_id 301#endif 302 303 304/* 305 * DTrace Provider Variables 306 * 307 * These are the variables relating to DTrace as a provider (that is, the 308 * provider of the BEGIN, END, and ERROR probes). 309 */ 310static dtrace_pattr_t dtrace_provider_attr = { 311{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 312{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 313{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN }, 314{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 315{ DTRACE_STABILITY_STABLE, DTRACE_STABILITY_STABLE, DTRACE_CLASS_COMMON }, 316}; 317 318static void 319dtrace_nullop(void) 320{} 321 322static dtrace_pops_t dtrace_provider_ops = { 323 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop, 324 (void (*)(void *, modctl_t *))dtrace_nullop, 325 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 326 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 327 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 328 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop, 329 NULL, 330 NULL, 331 NULL, 332 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop 333}; 334 335static dtrace_id_t dtrace_probeid_begin; /* special BEGIN probe */ 336static dtrace_id_t dtrace_probeid_end; /* special END probe */ 337dtrace_id_t dtrace_probeid_error; /* special ERROR probe */ 338 339/* 340 * DTrace Helper Tracing Variables 341 */ 342uint32_t dtrace_helptrace_next = 0; 343uint32_t dtrace_helptrace_nlocals; 344char *dtrace_helptrace_buffer; 345int dtrace_helptrace_bufsize = 512 * 1024; 346 347#ifdef DEBUG 348int dtrace_helptrace_enabled = 1; 349#else 350int dtrace_helptrace_enabled = 0; 351#endif 352 353/* 354 * DTrace Error Hashing 355 * 356 * On DEBUG kernels, DTrace will track the errors that has seen in a hash 357 * table. This is very useful for checking coverage of tests that are 358 * expected to induce DIF or DOF processing errors, and may be useful for 359 * debugging problems in the DIF code generator or in DOF generation . The 360 * error hash may be examined with the ::dtrace_errhash MDB dcmd. 361 */ 362#ifdef DEBUG 363static dtrace_errhash_t dtrace_errhash[DTRACE_ERRHASHSZ]; 364static const char *dtrace_errlast; 365static kthread_t *dtrace_errthread; 366static kmutex_t dtrace_errlock; 367#endif 368 369/* 370 * DTrace Macros and Constants 371 * 372 * These are various macros that are useful in various spots in the 373 * implementation, along with a few random constants that have no meaning 374 * outside of the implementation. There is no real structure to this cpp 375 * mishmash -- but is there ever? 376 */ 377#define DTRACE_HASHSTR(hash, probe) \ 378 dtrace_hash_str(*((char **)((uintptr_t)(probe) + (hash)->dth_stroffs))) 379 380#define DTRACE_HASHNEXT(hash, probe) \ 381 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_nextoffs) 382 383#define DTRACE_HASHPREV(hash, probe) \ 384 (dtrace_probe_t **)((uintptr_t)(probe) + (hash)->dth_prevoffs) 385 386#define DTRACE_HASHEQ(hash, lhs, rhs) \ 387 (strcmp(*((char **)((uintptr_t)(lhs) + (hash)->dth_stroffs)), \ 388 *((char **)((uintptr_t)(rhs) + (hash)->dth_stroffs))) == 0) 389 390#define DTRACE_AGGHASHSIZE_SLEW 17 391 392#define DTRACE_V4MAPPED_OFFSET (sizeof (uint32_t) * 3) 393 394/* 395 * The key for a thread-local variable consists of the lower 61 bits of the 396 * t_did, plus the 3 bits of the highest active interrupt above LOCK_LEVEL. 397 * We add DIF_VARIABLE_MAX to t_did to assure that the thread key is never 398 * equal to a variable identifier. This is necessary (but not sufficient) to 399 * assure that global associative arrays never collide with thread-local 400 * variables. To guarantee that they cannot collide, we must also define the 401 * order for keying dynamic variables. That order is: 402 * 403 * [ key0 ] ... [ keyn ] [ variable-key ] [ tls-key ] 404 * 405 * Because the variable-key and the tls-key are in orthogonal spaces, there is 406 * no way for a global variable key signature to match a thread-local key 407 * signature. 408 */ 409#if defined(sun) 410#define DTRACE_TLS_THRKEY(where) { \ 411 uint_t intr = 0; \ 412 uint_t actv = CPU->cpu_intr_actv >> (LOCK_LEVEL + 1); \ 413 for (; actv; actv >>= 1) \ 414 intr++; \ 415 ASSERT(intr < (1 << 3)); \ 416 (where) = ((curthread->t_did + DIF_VARIABLE_MAX) & \ 417 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 418} 419#else 420#define DTRACE_TLS_THRKEY(where) { \ 421 solaris_cpu_t *_c = &solaris_cpu[curcpu]; \ 422 uint_t intr = 0; \ 423 uint_t actv = _c->cpu_intr_actv; \ 424 for (; actv; actv >>= 1) \ 425 intr++; \ 426 ASSERT(intr < (1 << 3)); \ 427 (where) = ((curthread->td_tid + DIF_VARIABLE_MAX) & \ 428 (((uint64_t)1 << 61) - 1)) | ((uint64_t)intr << 61); \ 429} 430#endif 431 432#define DT_BSWAP_8(x) ((x) & 0xff) 433#define DT_BSWAP_16(x) ((DT_BSWAP_8(x) << 8) | DT_BSWAP_8((x) >> 8)) 434#define DT_BSWAP_32(x) ((DT_BSWAP_16(x) << 16) | DT_BSWAP_16((x) >> 16)) 435#define DT_BSWAP_64(x) ((DT_BSWAP_32(x) << 32) | DT_BSWAP_32((x) >> 32)) 436 437#define DT_MASK_LO 0x00000000FFFFFFFFULL 438 439#define DTRACE_STORE(type, tomax, offset, what) \ 440 *((type *)((uintptr_t)(tomax) + (uintptr_t)offset)) = (type)(what); 441 442#ifndef __i386 443#define DTRACE_ALIGNCHECK(addr, size, flags) \ 444 if (addr & (size - 1)) { \ 445 *flags |= CPU_DTRACE_BADALIGN; \ 446 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 447 return (0); \ 448 } 449#else 450#define DTRACE_ALIGNCHECK(addr, size, flags) 451#endif 452 453/* 454 * Test whether a range of memory starting at testaddr of size testsz falls 455 * within the range of memory described by addr, sz. We take care to avoid 456 * problems with overflow and underflow of the unsigned quantities, and 457 * disallow all negative sizes. Ranges of size 0 are allowed. 458 */ 459#define DTRACE_INRANGE(testaddr, testsz, baseaddr, basesz) \ 460 ((testaddr) - (baseaddr) < (basesz) && \ 461 (testaddr) + (testsz) - (baseaddr) <= (basesz) && \ 462 (testaddr) + (testsz) >= (testaddr)) 463 464/* 465 * Test whether alloc_sz bytes will fit in the scratch region. We isolate 466 * alloc_sz on the righthand side of the comparison in order to avoid overflow 467 * or underflow in the comparison with it. This is simpler than the INRANGE 468 * check above, because we know that the dtms_scratch_ptr is valid in the 469 * range. Allocations of size zero are allowed. 470 */ 471#define DTRACE_INSCRATCH(mstate, alloc_sz) \ 472 ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ 473 (mstate)->dtms_scratch_ptr >= (alloc_sz)) 474 475#define DTRACE_LOADFUNC(bits) \ 476/*CSTYLED*/ \ 477uint##bits##_t \ 478dtrace_load##bits(uintptr_t addr) \ 479{ \ 480 size_t size = bits / NBBY; \ 481 /*CSTYLED*/ \ 482 uint##bits##_t rval; \ 483 int i; \ 484 volatile uint16_t *flags = (volatile uint16_t *) \ 485 &cpu_core[curcpu].cpuc_dtrace_flags; \ 486 \ 487 DTRACE_ALIGNCHECK(addr, size, flags); \ 488 \ 489 for (i = 0; i < dtrace_toxranges; i++) { \ 490 if (addr >= dtrace_toxrange[i].dtt_limit) \ 491 continue; \ 492 \ 493 if (addr + size <= dtrace_toxrange[i].dtt_base) \ 494 continue; \ 495 \ 496 /* \ 497 * This address falls within a toxic region; return 0. \ 498 */ \ 499 *flags |= CPU_DTRACE_BADADDR; \ 500 cpu_core[curcpu].cpuc_dtrace_illval = addr; \ 501 return (0); \ 502 } \ 503 \ 504 *flags |= CPU_DTRACE_NOFAULT; \ 505 /*CSTYLED*/ \ 506 rval = *((volatile uint##bits##_t *)addr); \ 507 *flags &= ~CPU_DTRACE_NOFAULT; \ 508 \ 509 return (!(*flags & CPU_DTRACE_FAULT) ? rval : 0); \ 510} 511 512#ifdef _LP64 513#define dtrace_loadptr dtrace_load64 514#else 515#define dtrace_loadptr dtrace_load32 516#endif 517 518#define DTRACE_DYNHASH_FREE 0 519#define DTRACE_DYNHASH_SINK 1 520#define DTRACE_DYNHASH_VALID 2 521 522#define DTRACE_MATCH_NEXT 0 523#define DTRACE_MATCH_DONE 1 524#define DTRACE_ANCHORED(probe) ((probe)->dtpr_func[0] != '\0') 525#define DTRACE_STATE_ALIGN 64 526 527#define DTRACE_FLAGS2FLT(flags) \ 528 (((flags) & CPU_DTRACE_BADADDR) ? DTRACEFLT_BADADDR : \ 529 ((flags) & CPU_DTRACE_ILLOP) ? DTRACEFLT_ILLOP : \ 530 ((flags) & CPU_DTRACE_DIVZERO) ? DTRACEFLT_DIVZERO : \ 531 ((flags) & CPU_DTRACE_KPRIV) ? DTRACEFLT_KPRIV : \ 532 ((flags) & CPU_DTRACE_UPRIV) ? DTRACEFLT_UPRIV : \ 533 ((flags) & CPU_DTRACE_TUPOFLOW) ? DTRACEFLT_TUPOFLOW : \ 534 ((flags) & CPU_DTRACE_BADALIGN) ? DTRACEFLT_BADALIGN : \ 535 ((flags) & CPU_DTRACE_NOSCRATCH) ? DTRACEFLT_NOSCRATCH : \ 536 ((flags) & CPU_DTRACE_BADSTACK) ? DTRACEFLT_BADSTACK : \ 537 DTRACEFLT_UNKNOWN) 538 539#define DTRACEACT_ISSTRING(act) \ 540 ((act)->dta_kind == DTRACEACT_DIFEXPR && \ 541 (act)->dta_difo->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) 542 543/* Function prototype definitions: */ 544static size_t dtrace_strlen(const char *, size_t); 545static dtrace_probe_t *dtrace_probe_lookup_id(dtrace_id_t id); 546static void dtrace_enabling_provide(dtrace_provider_t *); 547static int dtrace_enabling_match(dtrace_enabling_t *, int *); 548static void dtrace_enabling_matchall(void); 549static dtrace_state_t *dtrace_anon_grab(void); 550#if defined(sun) 551static uint64_t dtrace_helper(int, dtrace_mstate_t *, 552 dtrace_state_t *, uint64_t, uint64_t); 553static dtrace_helpers_t *dtrace_helpers_create(proc_t *); 554#endif 555static void dtrace_buffer_drop(dtrace_buffer_t *); 556static intptr_t dtrace_buffer_reserve(dtrace_buffer_t *, size_t, size_t, 557 dtrace_state_t *, dtrace_mstate_t *); 558static int dtrace_state_option(dtrace_state_t *, dtrace_optid_t, 559 dtrace_optval_t); 560static int dtrace_ecb_create_enable(dtrace_probe_t *, void *); 561#if defined(sun) 562static void dtrace_helper_provider_destroy(dtrace_helper_provider_t *); 563#endif 564uint16_t dtrace_load16(uintptr_t); 565uint32_t dtrace_load32(uintptr_t); 566uint64_t dtrace_load64(uintptr_t); 567uint8_t dtrace_load8(uintptr_t); 568void dtrace_dynvar_clean(dtrace_dstate_t *); 569dtrace_dynvar_t *dtrace_dynvar(dtrace_dstate_t *, uint_t, dtrace_key_t *, 570 size_t, dtrace_dynvar_op_t, dtrace_mstate_t *, dtrace_vstate_t *); 571uintptr_t dtrace_dif_varstr(uintptr_t, dtrace_state_t *, dtrace_mstate_t *); 572 573/* 574 * DTrace Probe Context Functions 575 * 576 * These functions are called from probe context. Because probe context is 577 * any context in which C may be called, arbitrarily locks may be held, 578 * interrupts may be disabled, we may be in arbitrary dispatched state, etc. 579 * As a result, functions called from probe context may only call other DTrace 580 * support functions -- they may not interact at all with the system at large. 581 * (Note that the ASSERT macro is made probe-context safe by redefining it in 582 * terms of dtrace_assfail(), a probe-context safe function.) If arbitrary 583 * loads are to be performed from probe context, they _must_ be in terms of 584 * the safe dtrace_load*() variants. 585 * 586 * Some functions in this block are not actually called from probe context; 587 * for these functions, there will be a comment above the function reading 588 * "Note: not called from probe context." 589 */ 590void 591dtrace_panic(const char *format, ...) 592{ 593 va_list alist; 594 595 va_start(alist, format); 596 dtrace_vpanic(format, alist); 597 va_end(alist); 598} 599 600int 601dtrace_assfail(const char *a, const char *f, int l) 602{ 603 dtrace_panic("assertion failed: %s, file: %s, line: %d", a, f, l); 604 605 /* 606 * We just need something here that even the most clever compiler 607 * cannot optimize away. 608 */ 609 return (a[(uintptr_t)f]); 610} 611 612/* 613 * Atomically increment a specified error counter from probe context. 614 */ 615static void 616dtrace_error(uint32_t *counter) 617{ 618 /* 619 * Most counters stored to in probe context are per-CPU counters. 620 * However, there are some error conditions that are sufficiently 621 * arcane that they don't merit per-CPU storage. If these counters 622 * are incremented concurrently on different CPUs, scalability will be 623 * adversely affected -- but we don't expect them to be white-hot in a 624 * correctly constructed enabling... 625 */ 626 uint32_t oval, nval; 627 628 do { 629 oval = *counter; 630 631 if ((nval = oval + 1) == 0) { 632 /* 633 * If the counter would wrap, set it to 1 -- assuring 634 * that the counter is never zero when we have seen 635 * errors. (The counter must be 32-bits because we 636 * aren't guaranteed a 64-bit compare&swap operation.) 637 * To save this code both the infamy of being fingered 638 * by a priggish news story and the indignity of being 639 * the target of a neo-puritan witch trial, we're 640 * carefully avoiding any colorful description of the 641 * likelihood of this condition -- but suffice it to 642 * say that it is only slightly more likely than the 643 * overflow of predicate cache IDs, as discussed in 644 * dtrace_predicate_create(). 645 */ 646 nval = 1; 647 } 648 } while (dtrace_cas32(counter, oval, nval) != oval); 649} 650 651/* 652 * Use the DTRACE_LOADFUNC macro to define functions for each of loading a 653 * uint8_t, a uint16_t, a uint32_t and a uint64_t. 654 */ 655DTRACE_LOADFUNC(8) 656DTRACE_LOADFUNC(16) 657DTRACE_LOADFUNC(32) 658DTRACE_LOADFUNC(64) 659 660static int 661dtrace_inscratch(uintptr_t dest, size_t size, dtrace_mstate_t *mstate) 662{ 663 if (dest < mstate->dtms_scratch_base) 664 return (0); 665 666 if (dest + size < dest) 667 return (0); 668 669 if (dest + size > mstate->dtms_scratch_ptr) 670 return (0); 671 672 return (1); 673} 674 675static int 676dtrace_canstore_statvar(uint64_t addr, size_t sz, 677 dtrace_statvar_t **svars, int nsvars) 678{ 679 int i; 680 681 for (i = 0; i < nsvars; i++) { 682 dtrace_statvar_t *svar = svars[i]; 683 684 if (svar == NULL || svar->dtsv_size == 0) 685 continue; 686 687 if (DTRACE_INRANGE(addr, sz, svar->dtsv_data, svar->dtsv_size)) 688 return (1); 689 } 690 691 return (0); 692} 693 694/* 695 * Check to see if the address is within a memory region to which a store may 696 * be issued. This includes the DTrace scratch areas, and any DTrace variable 697 * region. The caller of dtrace_canstore() is responsible for performing any 698 * alignment checks that are needed before stores are actually executed. 699 */ 700static int 701dtrace_canstore(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 702 dtrace_vstate_t *vstate) 703{ 704 /* 705 * First, check to see if the address is in scratch space... 706 */ 707 if (DTRACE_INRANGE(addr, sz, mstate->dtms_scratch_base, 708 mstate->dtms_scratch_size)) 709 return (1); 710 711 /* 712 * Now check to see if it's a dynamic variable. This check will pick 713 * up both thread-local variables and any global dynamically-allocated 714 * variables. 715 */ 716 if (DTRACE_INRANGE(addr, sz, (uintptr_t)vstate->dtvs_dynvars.dtds_base, 717 vstate->dtvs_dynvars.dtds_size)) { 718 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 719 uintptr_t base = (uintptr_t)dstate->dtds_base + 720 (dstate->dtds_hashsize * sizeof (dtrace_dynhash_t)); 721 uintptr_t chunkoffs; 722 723 /* 724 * Before we assume that we can store here, we need to make 725 * sure that it isn't in our metadata -- storing to our 726 * dynamic variable metadata would corrupt our state. For 727 * the range to not include any dynamic variable metadata, 728 * it must: 729 * 730 * (1) Start above the hash table that is at the base of 731 * the dynamic variable space 732 * 733 * (2) Have a starting chunk offset that is beyond the 734 * dtrace_dynvar_t that is at the base of every chunk 735 * 736 * (3) Not span a chunk boundary 737 * 738 */ 739 if (addr < base) 740 return (0); 741 742 chunkoffs = (addr - base) % dstate->dtds_chunksize; 743 744 if (chunkoffs < sizeof (dtrace_dynvar_t)) 745 return (0); 746 747 if (chunkoffs + sz > dstate->dtds_chunksize) 748 return (0); 749 750 return (1); 751 } 752 753 /* 754 * Finally, check the static local and global variables. These checks 755 * take the longest, so we perform them last. 756 */ 757 if (dtrace_canstore_statvar(addr, sz, 758 vstate->dtvs_locals, vstate->dtvs_nlocals)) 759 return (1); 760 761 if (dtrace_canstore_statvar(addr, sz, 762 vstate->dtvs_globals, vstate->dtvs_nglobals)) 763 return (1); 764 765 return (0); 766} 767 768 769/* 770 * Convenience routine to check to see if the address is within a memory 771 * region in which a load may be issued given the user's privilege level; 772 * if not, it sets the appropriate error flags and loads 'addr' into the 773 * illegal value slot. 774 * 775 * DTrace subroutines (DIF_SUBR_*) should use this helper to implement 776 * appropriate memory access protection. 777 */ 778static int 779dtrace_canload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 780 dtrace_vstate_t *vstate) 781{ 782 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 783 784 /* 785 * If we hold the privilege to read from kernel memory, then 786 * everything is readable. 787 */ 788 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 789 return (1); 790 791 /* 792 * You can obviously read that which you can store. 793 */ 794 if (dtrace_canstore(addr, sz, mstate, vstate)) 795 return (1); 796 797 /* 798 * We're allowed to read from our own string table. 799 */ 800 if (DTRACE_INRANGE(addr, sz, (uintptr_t)mstate->dtms_difo->dtdo_strtab, 801 mstate->dtms_difo->dtdo_strlen)) 802 return (1); 803 804 DTRACE_CPUFLAG_SET(CPU_DTRACE_KPRIV); 805 *illval = addr; 806 return (0); 807} 808 809/* 810 * Convenience routine to check to see if a given string is within a memory 811 * region in which a load may be issued given the user's privilege level; 812 * this exists so that we don't need to issue unnecessary dtrace_strlen() 813 * calls in the event that the user has all privileges. 814 */ 815static int 816dtrace_strcanload(uint64_t addr, size_t sz, dtrace_mstate_t *mstate, 817 dtrace_vstate_t *vstate) 818{ 819 size_t strsz; 820 821 /* 822 * If we hold the privilege to read from kernel memory, then 823 * everything is readable. 824 */ 825 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 826 return (1); 827 828 strsz = 1 + dtrace_strlen((char *)(uintptr_t)addr, sz); 829 if (dtrace_canload(addr, strsz, mstate, vstate)) 830 return (1); 831 832 return (0); 833} 834 835/* 836 * Convenience routine to check to see if a given variable is within a memory 837 * region in which a load may be issued given the user's privilege level. 838 */ 839static int 840dtrace_vcanload(void *src, dtrace_diftype_t *type, dtrace_mstate_t *mstate, 841 dtrace_vstate_t *vstate) 842{ 843 size_t sz; 844 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 845 846 /* 847 * If we hold the privilege to read from kernel memory, then 848 * everything is readable. 849 */ 850 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 851 return (1); 852 853 if (type->dtdt_kind == DIF_TYPE_STRING) 854 sz = dtrace_strlen(src, 855 vstate->dtvs_state->dts_options[DTRACEOPT_STRSIZE]) + 1; 856 else 857 sz = type->dtdt_size; 858 859 return (dtrace_canload((uintptr_t)src, sz, mstate, vstate)); 860} 861 862/* 863 * Compare two strings using safe loads. 864 */ 865static int 866dtrace_strncmp(char *s1, char *s2, size_t limit) 867{ 868 uint8_t c1, c2; 869 volatile uint16_t *flags; 870 871 if (s1 == s2 || limit == 0) 872 return (0); 873 874 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 875 876 do { 877 if (s1 == NULL) { 878 c1 = '\0'; 879 } else { 880 c1 = dtrace_load8((uintptr_t)s1++); 881 } 882 883 if (s2 == NULL) { 884 c2 = '\0'; 885 } else { 886 c2 = dtrace_load8((uintptr_t)s2++); 887 } 888 889 if (c1 != c2) 890 return (c1 - c2); 891 } while (--limit && c1 != '\0' && !(*flags & CPU_DTRACE_FAULT)); 892 893 return (0); 894} 895 896/* 897 * Compute strlen(s) for a string using safe memory accesses. The additional 898 * len parameter is used to specify a maximum length to ensure completion. 899 */ 900static size_t 901dtrace_strlen(const char *s, size_t lim) 902{ 903 uint_t len; 904 905 for (len = 0; len != lim; len++) { 906 if (dtrace_load8((uintptr_t)s++) == '\0') 907 break; 908 } 909 910 return (len); 911} 912 913/* 914 * Check if an address falls within a toxic region. 915 */ 916static int 917dtrace_istoxic(uintptr_t kaddr, size_t size) 918{ 919 uintptr_t taddr, tsize; 920 int i; 921 922 for (i = 0; i < dtrace_toxranges; i++) { 923 taddr = dtrace_toxrange[i].dtt_base; 924 tsize = dtrace_toxrange[i].dtt_limit - taddr; 925 926 if (kaddr - taddr < tsize) { 927 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 928 cpu_core[curcpu].cpuc_dtrace_illval = kaddr; 929 return (1); 930 } 931 932 if (taddr - kaddr < size) { 933 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 934 cpu_core[curcpu].cpuc_dtrace_illval = taddr; 935 return (1); 936 } 937 } 938 939 return (0); 940} 941 942/* 943 * Copy src to dst using safe memory accesses. The src is assumed to be unsafe 944 * memory specified by the DIF program. The dst is assumed to be safe memory 945 * that we can store to directly because it is managed by DTrace. As with 946 * standard bcopy, overlapping copies are handled properly. 947 */ 948static void 949dtrace_bcopy(const void *src, void *dst, size_t len) 950{ 951 if (len != 0) { 952 uint8_t *s1 = dst; 953 const uint8_t *s2 = src; 954 955 if (s1 <= s2) { 956 do { 957 *s1++ = dtrace_load8((uintptr_t)s2++); 958 } while (--len != 0); 959 } else { 960 s2 += len; 961 s1 += len; 962 963 do { 964 *--s1 = dtrace_load8((uintptr_t)--s2); 965 } while (--len != 0); 966 } 967 } 968} 969 970/* 971 * Copy src to dst using safe memory accesses, up to either the specified 972 * length, or the point that a nul byte is encountered. The src is assumed to 973 * be unsafe memory specified by the DIF program. The dst is assumed to be 974 * safe memory that we can store to directly because it is managed by DTrace. 975 * Unlike dtrace_bcopy(), overlapping regions are not handled. 976 */ 977static void 978dtrace_strcpy(const void *src, void *dst, size_t len) 979{ 980 if (len != 0) { 981 uint8_t *s1 = dst, c; 982 const uint8_t *s2 = src; 983 984 do { 985 *s1++ = c = dtrace_load8((uintptr_t)s2++); 986 } while (--len != 0 && c != '\0'); 987 } 988} 989 990/* 991 * Copy src to dst, deriving the size and type from the specified (BYREF) 992 * variable type. The src is assumed to be unsafe memory specified by the DIF 993 * program. The dst is assumed to be DTrace variable memory that is of the 994 * specified type; we assume that we can store to directly. 995 */ 996static void 997dtrace_vcopy(void *src, void *dst, dtrace_diftype_t *type) 998{ 999 ASSERT(type->dtdt_flags & DIF_TF_BYREF); 1000 1001 if (type->dtdt_kind == DIF_TYPE_STRING) { 1002 dtrace_strcpy(src, dst, type->dtdt_size); 1003 } else { 1004 dtrace_bcopy(src, dst, type->dtdt_size); 1005 } 1006} 1007 1008/* 1009 * Compare s1 to s2 using safe memory accesses. The s1 data is assumed to be 1010 * unsafe memory specified by the DIF program. The s2 data is assumed to be 1011 * safe memory that we can access directly because it is managed by DTrace. 1012 */ 1013static int 1014dtrace_bcmp(const void *s1, const void *s2, size_t len) 1015{ 1016 volatile uint16_t *flags; 1017 1018 flags = (volatile uint16_t *)&cpu_core[curcpu].cpuc_dtrace_flags; 1019 1020 if (s1 == s2) 1021 return (0); 1022 1023 if (s1 == NULL || s2 == NULL) 1024 return (1); 1025 1026 if (s1 != s2 && len != 0) { 1027 const uint8_t *ps1 = s1; 1028 const uint8_t *ps2 = s2; 1029 1030 do { 1031 if (dtrace_load8((uintptr_t)ps1++) != *ps2++) 1032 return (1); 1033 } while (--len != 0 && !(*flags & CPU_DTRACE_FAULT)); 1034 } 1035 return (0); 1036} 1037 1038/* 1039 * Zero the specified region using a simple byte-by-byte loop. Note that this 1040 * is for safe DTrace-managed memory only. 1041 */ 1042static void 1043dtrace_bzero(void *dst, size_t len) 1044{ 1045 uchar_t *cp; 1046 1047 for (cp = dst; len != 0; len--) 1048 *cp++ = 0; 1049} 1050 1051static void 1052dtrace_add_128(uint64_t *addend1, uint64_t *addend2, uint64_t *sum) 1053{ 1054 uint64_t result[2]; 1055 1056 result[0] = addend1[0] + addend2[0]; 1057 result[1] = addend1[1] + addend2[1] + 1058 (result[0] < addend1[0] || result[0] < addend2[0] ? 1 : 0); 1059 1060 sum[0] = result[0]; 1061 sum[1] = result[1]; 1062} 1063 1064/* 1065 * Shift the 128-bit value in a by b. If b is positive, shift left. 1066 * If b is negative, shift right. 1067 */ 1068static void 1069dtrace_shift_128(uint64_t *a, int b) 1070{ 1071 uint64_t mask; 1072 1073 if (b == 0) 1074 return; 1075 1076 if (b < 0) { 1077 b = -b; 1078 if (b >= 64) { 1079 a[0] = a[1] >> (b - 64); 1080 a[1] = 0; 1081 } else { 1082 a[0] >>= b; 1083 mask = 1LL << (64 - b); 1084 mask -= 1; 1085 a[0] |= ((a[1] & mask) << (64 - b)); 1086 a[1] >>= b; 1087 } 1088 } else { 1089 if (b >= 64) { 1090 a[1] = a[0] << (b - 64); 1091 a[0] = 0; 1092 } else { 1093 a[1] <<= b; 1094 mask = a[0] >> (64 - b); 1095 a[1] |= mask; 1096 a[0] <<= b; 1097 } 1098 } 1099} 1100 1101/* 1102 * The basic idea is to break the 2 64-bit values into 4 32-bit values, 1103 * use native multiplication on those, and then re-combine into the 1104 * resulting 128-bit value. 1105 * 1106 * (hi1 << 32 + lo1) * (hi2 << 32 + lo2) = 1107 * hi1 * hi2 << 64 + 1108 * hi1 * lo2 << 32 + 1109 * hi2 * lo1 << 32 + 1110 * lo1 * lo2 1111 */ 1112static void 1113dtrace_multiply_128(uint64_t factor1, uint64_t factor2, uint64_t *product) 1114{ 1115 uint64_t hi1, hi2, lo1, lo2; 1116 uint64_t tmp[2]; 1117 1118 hi1 = factor1 >> 32; 1119 hi2 = factor2 >> 32; 1120 1121 lo1 = factor1 & DT_MASK_LO; 1122 lo2 = factor2 & DT_MASK_LO; 1123 1124 product[0] = lo1 * lo2; 1125 product[1] = hi1 * hi2; 1126 1127 tmp[0] = hi1 * lo2; 1128 tmp[1] = 0; 1129 dtrace_shift_128(tmp, 32); 1130 dtrace_add_128(product, tmp, product); 1131 1132 tmp[0] = hi2 * lo1; 1133 tmp[1] = 0; 1134 dtrace_shift_128(tmp, 32); 1135 dtrace_add_128(product, tmp, product); 1136} 1137 1138/* 1139 * This privilege check should be used by actions and subroutines to 1140 * verify that the user credentials of the process that enabled the 1141 * invoking ECB match the target credentials 1142 */ 1143static int 1144dtrace_priv_proc_common_user(dtrace_state_t *state) 1145{ 1146 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1147 1148 /* 1149 * We should always have a non-NULL state cred here, since if cred 1150 * is null (anonymous tracing), we fast-path bypass this routine. 1151 */ 1152 ASSERT(s_cr != NULL); 1153 1154 if ((cr = CRED()) != NULL && 1155 s_cr->cr_uid == cr->cr_uid && 1156 s_cr->cr_uid == cr->cr_ruid && 1157 s_cr->cr_uid == cr->cr_suid && 1158 s_cr->cr_gid == cr->cr_gid && 1159 s_cr->cr_gid == cr->cr_rgid && 1160 s_cr->cr_gid == cr->cr_sgid) 1161 return (1); 1162 1163 return (0); 1164} 1165 1166/* 1167 * This privilege check should be used by actions and subroutines to 1168 * verify that the zone of the process that enabled the invoking ECB 1169 * matches the target credentials 1170 */ 1171static int 1172dtrace_priv_proc_common_zone(dtrace_state_t *state) 1173{ 1174#if defined(sun) 1175 cred_t *cr, *s_cr = state->dts_cred.dcr_cred; 1176 1177 /* 1178 * We should always have a non-NULL state cred here, since if cred 1179 * is null (anonymous tracing), we fast-path bypass this routine. 1180 */ 1181 ASSERT(s_cr != NULL); 1182 1183 if ((cr = CRED()) != NULL && 1184 s_cr->cr_zone == cr->cr_zone) 1185 return (1); 1186 1187 return (0); 1188#else 1189 return (1); 1190#endif 1191} 1192 1193/* 1194 * This privilege check should be used by actions and subroutines to 1195 * verify that the process has not setuid or changed credentials. 1196 */ 1197static int 1198dtrace_priv_proc_common_nocd(void) 1199{ 1200 proc_t *proc; 1201 1202 if ((proc = ttoproc(curthread)) != NULL && 1203 !(proc->p_flag & SNOCD)) 1204 return (1); 1205 1206 return (0); 1207} 1208 1209static int 1210dtrace_priv_proc_destructive(dtrace_state_t *state) 1211{ 1212 int action = state->dts_cred.dcr_action; 1213 1214 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE) == 0) && 1215 dtrace_priv_proc_common_zone(state) == 0) 1216 goto bad; 1217 1218 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER) == 0) && 1219 dtrace_priv_proc_common_user(state) == 0) 1220 goto bad; 1221 1222 if (((action & DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG) == 0) && 1223 dtrace_priv_proc_common_nocd() == 0) 1224 goto bad; 1225 1226 return (1); 1227 1228bad: 1229 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1230 1231 return (0); 1232} 1233 1234static int 1235dtrace_priv_proc_control(dtrace_state_t *state) 1236{ 1237 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC_CONTROL) 1238 return (1); 1239 1240 if (dtrace_priv_proc_common_zone(state) && 1241 dtrace_priv_proc_common_user(state) && 1242 dtrace_priv_proc_common_nocd()) 1243 return (1); 1244 1245 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1246 1247 return (0); 1248} 1249 1250static int 1251dtrace_priv_proc(dtrace_state_t *state) 1252{ 1253 if (state->dts_cred.dcr_action & DTRACE_CRA_PROC) 1254 return (1); 1255 1256 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_UPRIV; 1257 1258 return (0); 1259} 1260 1261static int 1262dtrace_priv_kernel(dtrace_state_t *state) 1263{ 1264 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL) 1265 return (1); 1266 1267 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1268 1269 return (0); 1270} 1271 1272static int 1273dtrace_priv_kernel_destructive(dtrace_state_t *state) 1274{ 1275 if (state->dts_cred.dcr_action & DTRACE_CRA_KERNEL_DESTRUCTIVE) 1276 return (1); 1277 1278 cpu_core[curcpu].cpuc_dtrace_flags |= CPU_DTRACE_KPRIV; 1279 1280 return (0); 1281} 1282 1283/* 1284 * Note: not called from probe context. This function is called 1285 * asynchronously (and at a regular interval) from outside of probe context to 1286 * clean the dirty dynamic variable lists on all CPUs. Dynamic variable 1287 * cleaning is explained in detail in <sys/dtrace_impl.h>. 1288 */ 1289void 1290dtrace_dynvar_clean(dtrace_dstate_t *dstate) 1291{ 1292 dtrace_dynvar_t *dirty; 1293 dtrace_dstate_percpu_t *dcpu; 1294 int i, work = 0; 1295 1296 for (i = 0; i < NCPU; i++) { 1297 dcpu = &dstate->dtds_percpu[i]; 1298 1299 ASSERT(dcpu->dtdsc_rinsing == NULL); 1300 1301 /* 1302 * If the dirty list is NULL, there is no dirty work to do. 1303 */ 1304 if (dcpu->dtdsc_dirty == NULL) 1305 continue; 1306 1307 /* 1308 * If the clean list is non-NULL, then we're not going to do 1309 * any work for this CPU -- it means that there has not been 1310 * a dtrace_dynvar() allocation on this CPU (or from this CPU) 1311 * since the last time we cleaned house. 1312 */ 1313 if (dcpu->dtdsc_clean != NULL) 1314 continue; 1315 1316 work = 1; 1317 1318 /* 1319 * Atomically move the dirty list aside. 1320 */ 1321 do { 1322 dirty = dcpu->dtdsc_dirty; 1323 1324 /* 1325 * Before we zap the dirty list, set the rinsing list. 1326 * (This allows for a potential assertion in 1327 * dtrace_dynvar(): if a free dynamic variable appears 1328 * on a hash chain, either the dirty list or the 1329 * rinsing list for some CPU must be non-NULL.) 1330 */ 1331 dcpu->dtdsc_rinsing = dirty; 1332 dtrace_membar_producer(); 1333 } while (dtrace_casptr(&dcpu->dtdsc_dirty, 1334 dirty, NULL) != dirty); 1335 } 1336 1337 if (!work) { 1338 /* 1339 * We have no work to do; we can simply return. 1340 */ 1341 return; 1342 } 1343 1344 dtrace_sync(); 1345 1346 for (i = 0; i < NCPU; i++) { 1347 dcpu = &dstate->dtds_percpu[i]; 1348 1349 if (dcpu->dtdsc_rinsing == NULL) 1350 continue; 1351 1352 /* 1353 * We are now guaranteed that no hash chain contains a pointer 1354 * into this dirty list; we can make it clean. 1355 */ 1356 ASSERT(dcpu->dtdsc_clean == NULL); 1357 dcpu->dtdsc_clean = dcpu->dtdsc_rinsing; 1358 dcpu->dtdsc_rinsing = NULL; 1359 } 1360 1361 /* 1362 * Before we actually set the state to be DTRACE_DSTATE_CLEAN, make 1363 * sure that all CPUs have seen all of the dtdsc_clean pointers. 1364 * This prevents a race whereby a CPU incorrectly decides that 1365 * the state should be something other than DTRACE_DSTATE_CLEAN 1366 * after dtrace_dynvar_clean() has completed. 1367 */ 1368 dtrace_sync(); 1369 1370 dstate->dtds_state = DTRACE_DSTATE_CLEAN; 1371} 1372 1373/* 1374 * Depending on the value of the op parameter, this function looks-up, 1375 * allocates or deallocates an arbitrarily-keyed dynamic variable. If an 1376 * allocation is requested, this function will return a pointer to a 1377 * dtrace_dynvar_t corresponding to the allocated variable -- or NULL if no 1378 * variable can be allocated. If NULL is returned, the appropriate counter 1379 * will be incremented. 1380 */ 1381dtrace_dynvar_t * 1382dtrace_dynvar(dtrace_dstate_t *dstate, uint_t nkeys, 1383 dtrace_key_t *key, size_t dsize, dtrace_dynvar_op_t op, 1384 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate) 1385{ 1386 uint64_t hashval = DTRACE_DYNHASH_VALID; 1387 dtrace_dynhash_t *hash = dstate->dtds_hash; 1388 dtrace_dynvar_t *free, *new_free, *next, *dvar, *start, *prev = NULL; 1389 processorid_t me = curcpu, cpu = me; 1390 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[me]; 1391 size_t bucket, ksize; 1392 size_t chunksize = dstate->dtds_chunksize; 1393 uintptr_t kdata, lock, nstate; 1394 uint_t i; 1395 1396 ASSERT(nkeys != 0); 1397 1398 /* 1399 * Hash the key. As with aggregations, we use Jenkins' "One-at-a-time" 1400 * algorithm. For the by-value portions, we perform the algorithm in 1401 * 16-bit chunks (as opposed to 8-bit chunks). This speeds things up a 1402 * bit, and seems to have only a minute effect on distribution. For 1403 * the by-reference data, we perform "One-at-a-time" iterating (safely) 1404 * over each referenced byte. It's painful to do this, but it's much 1405 * better than pathological hash distribution. The efficacy of the 1406 * hashing algorithm (and a comparison with other algorithms) may be 1407 * found by running the ::dtrace_dynstat MDB dcmd. 1408 */ 1409 for (i = 0; i < nkeys; i++) { 1410 if (key[i].dttk_size == 0) { 1411 uint64_t val = key[i].dttk_value; 1412 1413 hashval += (val >> 48) & 0xffff; 1414 hashval += (hashval << 10); 1415 hashval ^= (hashval >> 6); 1416 1417 hashval += (val >> 32) & 0xffff; 1418 hashval += (hashval << 10); 1419 hashval ^= (hashval >> 6); 1420 1421 hashval += (val >> 16) & 0xffff; 1422 hashval += (hashval << 10); 1423 hashval ^= (hashval >> 6); 1424 1425 hashval += val & 0xffff; 1426 hashval += (hashval << 10); 1427 hashval ^= (hashval >> 6); 1428 } else { 1429 /* 1430 * This is incredibly painful, but it beats the hell 1431 * out of the alternative. 1432 */ 1433 uint64_t j, size = key[i].dttk_size; 1434 uintptr_t base = (uintptr_t)key[i].dttk_value; 1435 1436 if (!dtrace_canload(base, size, mstate, vstate)) 1437 break; 1438 1439 for (j = 0; j < size; j++) { 1440 hashval += dtrace_load8(base + j); 1441 hashval += (hashval << 10); 1442 hashval ^= (hashval >> 6); 1443 } 1444 } 1445 } 1446 1447 if (DTRACE_CPUFLAG_ISSET(CPU_DTRACE_FAULT)) 1448 return (NULL); 1449 1450 hashval += (hashval << 3); 1451 hashval ^= (hashval >> 11); 1452 hashval += (hashval << 15); 1453 1454 /* 1455 * There is a remote chance (ideally, 1 in 2^31) that our hashval 1456 * comes out to be one of our two sentinel hash values. If this 1457 * actually happens, we set the hashval to be a value known to be a 1458 * non-sentinel value. 1459 */ 1460 if (hashval == DTRACE_DYNHASH_FREE || hashval == DTRACE_DYNHASH_SINK) 1461 hashval = DTRACE_DYNHASH_VALID; 1462 1463 /* 1464 * Yes, it's painful to do a divide here. If the cycle count becomes 1465 * important here, tricks can be pulled to reduce it. (However, it's 1466 * critical that hash collisions be kept to an absolute minimum; 1467 * they're much more painful than a divide.) It's better to have a 1468 * solution that generates few collisions and still keeps things 1469 * relatively simple. 1470 */ 1471 bucket = hashval % dstate->dtds_hashsize; 1472 1473 if (op == DTRACE_DYNVAR_DEALLOC) { 1474 volatile uintptr_t *lockp = &hash[bucket].dtdh_lock; 1475 1476 for (;;) { 1477 while ((lock = *lockp) & 1) 1478 continue; 1479 1480 if (dtrace_casptr((volatile void *)lockp, 1481 (volatile void *)lock, (volatile void *)(lock + 1)) == (void *)lock) 1482 break; 1483 } 1484 1485 dtrace_membar_producer(); 1486 } 1487 1488top: 1489 prev = NULL; 1490 lock = hash[bucket].dtdh_lock; 1491 1492 dtrace_membar_consumer(); 1493 1494 start = hash[bucket].dtdh_chain; 1495 ASSERT(start != NULL && (start->dtdv_hashval == DTRACE_DYNHASH_SINK || 1496 start->dtdv_hashval != DTRACE_DYNHASH_FREE || 1497 op != DTRACE_DYNVAR_DEALLOC)); 1498 1499 for (dvar = start; dvar != NULL; dvar = dvar->dtdv_next) { 1500 dtrace_tuple_t *dtuple = &dvar->dtdv_tuple; 1501 dtrace_key_t *dkey = &dtuple->dtt_key[0]; 1502 1503 if (dvar->dtdv_hashval != hashval) { 1504 if (dvar->dtdv_hashval == DTRACE_DYNHASH_SINK) { 1505 /* 1506 * We've reached the sink, and therefore the 1507 * end of the hash chain; we can kick out of 1508 * the loop knowing that we have seen a valid 1509 * snapshot of state. 1510 */ 1511 ASSERT(dvar->dtdv_next == NULL); 1512 ASSERT(dvar == &dtrace_dynhash_sink); 1513 break; 1514 } 1515 1516 if (dvar->dtdv_hashval == DTRACE_DYNHASH_FREE) { 1517 /* 1518 * We've gone off the rails: somewhere along 1519 * the line, one of the members of this hash 1520 * chain was deleted. Note that we could also 1521 * detect this by simply letting this loop run 1522 * to completion, as we would eventually hit 1523 * the end of the dirty list. However, we 1524 * want to avoid running the length of the 1525 * dirty list unnecessarily (it might be quite 1526 * long), so we catch this as early as 1527 * possible by detecting the hash marker. In 1528 * this case, we simply set dvar to NULL and 1529 * break; the conditional after the loop will 1530 * send us back to top. 1531 */ 1532 dvar = NULL; 1533 break; 1534 } 1535 1536 goto next; 1537 } 1538 1539 if (dtuple->dtt_nkeys != nkeys) 1540 goto next; 1541 1542 for (i = 0; i < nkeys; i++, dkey++) { 1543 if (dkey->dttk_size != key[i].dttk_size) 1544 goto next; /* size or type mismatch */ 1545 1546 if (dkey->dttk_size != 0) { 1547 if (dtrace_bcmp( 1548 (void *)(uintptr_t)key[i].dttk_value, 1549 (void *)(uintptr_t)dkey->dttk_value, 1550 dkey->dttk_size)) 1551 goto next; 1552 } else { 1553 if (dkey->dttk_value != key[i].dttk_value) 1554 goto next; 1555 } 1556 } 1557 1558 if (op != DTRACE_DYNVAR_DEALLOC) 1559 return (dvar); 1560 1561 ASSERT(dvar->dtdv_next == NULL || 1562 dvar->dtdv_next->dtdv_hashval != DTRACE_DYNHASH_FREE); 1563 1564 if (prev != NULL) { 1565 ASSERT(hash[bucket].dtdh_chain != dvar); 1566 ASSERT(start != dvar); 1567 ASSERT(prev->dtdv_next == dvar); 1568 prev->dtdv_next = dvar->dtdv_next; 1569 } else { 1570 if (dtrace_casptr(&hash[bucket].dtdh_chain, 1571 start, dvar->dtdv_next) != start) { 1572 /* 1573 * We have failed to atomically swing the 1574 * hash table head pointer, presumably because 1575 * of a conflicting allocation on another CPU. 1576 * We need to reread the hash chain and try 1577 * again. 1578 */ 1579 goto top; 1580 } 1581 } 1582 1583 dtrace_membar_producer(); 1584 1585 /* 1586 * Now set the hash value to indicate that it's free. 1587 */ 1588 ASSERT(hash[bucket].dtdh_chain != dvar); 1589 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1590 1591 dtrace_membar_producer(); 1592 1593 /* 1594 * Set the next pointer to point at the dirty list, and 1595 * atomically swing the dirty pointer to the newly freed dvar. 1596 */ 1597 do { 1598 next = dcpu->dtdsc_dirty; 1599 dvar->dtdv_next = next; 1600 } while (dtrace_casptr(&dcpu->dtdsc_dirty, next, dvar) != next); 1601 1602 /* 1603 * Finally, unlock this hash bucket. 1604 */ 1605 ASSERT(hash[bucket].dtdh_lock == lock); 1606 ASSERT(lock & 1); 1607 hash[bucket].dtdh_lock++; 1608 1609 return (NULL); 1610next: 1611 prev = dvar; 1612 continue; 1613 } 1614 1615 if (dvar == NULL) { 1616 /* 1617 * If dvar is NULL, it is because we went off the rails: 1618 * one of the elements that we traversed in the hash chain 1619 * was deleted while we were traversing it. In this case, 1620 * we assert that we aren't doing a dealloc (deallocs lock 1621 * the hash bucket to prevent themselves from racing with 1622 * one another), and retry the hash chain traversal. 1623 */ 1624 ASSERT(op != DTRACE_DYNVAR_DEALLOC); 1625 goto top; 1626 } 1627 1628 if (op != DTRACE_DYNVAR_ALLOC) { 1629 /* 1630 * If we are not to allocate a new variable, we want to 1631 * return NULL now. Before we return, check that the value 1632 * of the lock word hasn't changed. If it has, we may have 1633 * seen an inconsistent snapshot. 1634 */ 1635 if (op == DTRACE_DYNVAR_NOALLOC) { 1636 if (hash[bucket].dtdh_lock != lock) 1637 goto top; 1638 } else { 1639 ASSERT(op == DTRACE_DYNVAR_DEALLOC); 1640 ASSERT(hash[bucket].dtdh_lock == lock); 1641 ASSERT(lock & 1); 1642 hash[bucket].dtdh_lock++; 1643 } 1644 1645 return (NULL); 1646 } 1647 1648 /* 1649 * We need to allocate a new dynamic variable. The size we need is the 1650 * size of dtrace_dynvar plus the size of nkeys dtrace_key_t's plus the 1651 * size of any auxiliary key data (rounded up to 8-byte alignment) plus 1652 * the size of any referred-to data (dsize). We then round the final 1653 * size up to the chunksize for allocation. 1654 */ 1655 for (ksize = 0, i = 0; i < nkeys; i++) 1656 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 1657 1658 /* 1659 * This should be pretty much impossible, but could happen if, say, 1660 * strange DIF specified the tuple. Ideally, this should be an 1661 * assertion and not an error condition -- but that requires that the 1662 * chunksize calculation in dtrace_difo_chunksize() be absolutely 1663 * bullet-proof. (That is, it must not be able to be fooled by 1664 * malicious DIF.) Given the lack of backwards branches in DIF, 1665 * solving this would presumably not amount to solving the Halting 1666 * Problem -- but it still seems awfully hard. 1667 */ 1668 if (sizeof (dtrace_dynvar_t) + sizeof (dtrace_key_t) * (nkeys - 1) + 1669 ksize + dsize > chunksize) { 1670 dcpu->dtdsc_drops++; 1671 return (NULL); 1672 } 1673 1674 nstate = DTRACE_DSTATE_EMPTY; 1675 1676 do { 1677retry: 1678 free = dcpu->dtdsc_free; 1679 1680 if (free == NULL) { 1681 dtrace_dynvar_t *clean = dcpu->dtdsc_clean; 1682 void *rval; 1683 1684 if (clean == NULL) { 1685 /* 1686 * We're out of dynamic variable space on 1687 * this CPU. Unless we have tried all CPUs, 1688 * we'll try to allocate from a different 1689 * CPU. 1690 */ 1691 switch (dstate->dtds_state) { 1692 case DTRACE_DSTATE_CLEAN: { 1693 void *sp = &dstate->dtds_state; 1694 1695 if (++cpu >= NCPU) 1696 cpu = 0; 1697 1698 if (dcpu->dtdsc_dirty != NULL && 1699 nstate == DTRACE_DSTATE_EMPTY) 1700 nstate = DTRACE_DSTATE_DIRTY; 1701 1702 if (dcpu->dtdsc_rinsing != NULL) 1703 nstate = DTRACE_DSTATE_RINSING; 1704 1705 dcpu = &dstate->dtds_percpu[cpu]; 1706 1707 if (cpu != me) 1708 goto retry; 1709 1710 (void) dtrace_cas32(sp, 1711 DTRACE_DSTATE_CLEAN, nstate); 1712 1713 /* 1714 * To increment the correct bean 1715 * counter, take another lap. 1716 */ 1717 goto retry; 1718 } 1719 1720 case DTRACE_DSTATE_DIRTY: 1721 dcpu->dtdsc_dirty_drops++; 1722 break; 1723 1724 case DTRACE_DSTATE_RINSING: 1725 dcpu->dtdsc_rinsing_drops++; 1726 break; 1727 1728 case DTRACE_DSTATE_EMPTY: 1729 dcpu->dtdsc_drops++; 1730 break; 1731 } 1732 1733 DTRACE_CPUFLAG_SET(CPU_DTRACE_DROP); 1734 return (NULL); 1735 } 1736 1737 /* 1738 * The clean list appears to be non-empty. We want to 1739 * move the clean list to the free list; we start by 1740 * moving the clean pointer aside. 1741 */ 1742 if (dtrace_casptr(&dcpu->dtdsc_clean, 1743 clean, NULL) != clean) { 1744 /* 1745 * We are in one of two situations: 1746 * 1747 * (a) The clean list was switched to the 1748 * free list by another CPU. 1749 * 1750 * (b) The clean list was added to by the 1751 * cleansing cyclic. 1752 * 1753 * In either of these situations, we can 1754 * just reattempt the free list allocation. 1755 */ 1756 goto retry; 1757 } 1758 1759 ASSERT(clean->dtdv_hashval == DTRACE_DYNHASH_FREE); 1760 1761 /* 1762 * Now we'll move the clean list to the free list. 1763 * It's impossible for this to fail: the only way 1764 * the free list can be updated is through this 1765 * code path, and only one CPU can own the clean list. 1766 * Thus, it would only be possible for this to fail if 1767 * this code were racing with dtrace_dynvar_clean(). 1768 * (That is, if dtrace_dynvar_clean() updated the clean 1769 * list, and we ended up racing to update the free 1770 * list.) This race is prevented by the dtrace_sync() 1771 * in dtrace_dynvar_clean() -- which flushes the 1772 * owners of the clean lists out before resetting 1773 * the clean lists. 1774 */ 1775 rval = dtrace_casptr(&dcpu->dtdsc_free, NULL, clean); 1776 ASSERT(rval == NULL); 1777 goto retry; 1778 } 1779 1780 dvar = free; 1781 new_free = dvar->dtdv_next; 1782 } while (dtrace_casptr(&dcpu->dtdsc_free, free, new_free) != free); 1783 1784 /* 1785 * We have now allocated a new chunk. We copy the tuple keys into the 1786 * tuple array and copy any referenced key data into the data space 1787 * following the tuple array. As we do this, we relocate dttk_value 1788 * in the final tuple to point to the key data address in the chunk. 1789 */ 1790 kdata = (uintptr_t)&dvar->dtdv_tuple.dtt_key[nkeys]; 1791 dvar->dtdv_data = (void *)(kdata + ksize); 1792 dvar->dtdv_tuple.dtt_nkeys = nkeys; 1793 1794 for (i = 0; i < nkeys; i++) { 1795 dtrace_key_t *dkey = &dvar->dtdv_tuple.dtt_key[i]; 1796 size_t kesize = key[i].dttk_size; 1797 1798 if (kesize != 0) { 1799 dtrace_bcopy( 1800 (const void *)(uintptr_t)key[i].dttk_value, 1801 (void *)kdata, kesize); 1802 dkey->dttk_value = kdata; 1803 kdata += P2ROUNDUP(kesize, sizeof (uint64_t)); 1804 } else { 1805 dkey->dttk_value = key[i].dttk_value; 1806 } 1807 1808 dkey->dttk_size = kesize; 1809 } 1810 1811 ASSERT(dvar->dtdv_hashval == DTRACE_DYNHASH_FREE); 1812 dvar->dtdv_hashval = hashval; 1813 dvar->dtdv_next = start; 1814 1815 if (dtrace_casptr(&hash[bucket].dtdh_chain, start, dvar) == start) 1816 return (dvar); 1817 1818 /* 1819 * The cas has failed. Either another CPU is adding an element to 1820 * this hash chain, or another CPU is deleting an element from this 1821 * hash chain. The simplest way to deal with both of these cases 1822 * (though not necessarily the most efficient) is to free our 1823 * allocated block and tail-call ourselves. Note that the free is 1824 * to the dirty list and _not_ to the free list. This is to prevent 1825 * races with allocators, above. 1826 */ 1827 dvar->dtdv_hashval = DTRACE_DYNHASH_FREE; 1828 1829 dtrace_membar_producer(); 1830 1831 do { 1832 free = dcpu->dtdsc_dirty; 1833 dvar->dtdv_next = free; 1834 } while (dtrace_casptr(&dcpu->dtdsc_dirty, free, dvar) != free); 1835 1836 return (dtrace_dynvar(dstate, nkeys, key, dsize, op, mstate, vstate)); 1837} 1838 1839/*ARGSUSED*/ 1840static void 1841dtrace_aggregate_min(uint64_t *oval, uint64_t nval, uint64_t arg) 1842{ 1843 if ((int64_t)nval < (int64_t)*oval) 1844 *oval = nval; 1845} 1846 1847/*ARGSUSED*/ 1848static void 1849dtrace_aggregate_max(uint64_t *oval, uint64_t nval, uint64_t arg) 1850{ 1851 if ((int64_t)nval > (int64_t)*oval) 1852 *oval = nval; 1853} 1854 1855static void 1856dtrace_aggregate_quantize(uint64_t *quanta, uint64_t nval, uint64_t incr) 1857{ 1858 int i, zero = DTRACE_QUANTIZE_ZEROBUCKET; 1859 int64_t val = (int64_t)nval; 1860 1861 if (val < 0) { 1862 for (i = 0; i < zero; i++) { 1863 if (val <= DTRACE_QUANTIZE_BUCKETVAL(i)) { 1864 quanta[i] += incr; 1865 return; 1866 } 1867 } 1868 } else { 1869 for (i = zero + 1; i < DTRACE_QUANTIZE_NBUCKETS; i++) { 1870 if (val < DTRACE_QUANTIZE_BUCKETVAL(i)) { 1871 quanta[i - 1] += incr; 1872 return; 1873 } 1874 } 1875 1876 quanta[DTRACE_QUANTIZE_NBUCKETS - 1] += incr; 1877 return; 1878 } 1879 1880 ASSERT(0); 1881} 1882 1883static void 1884dtrace_aggregate_lquantize(uint64_t *lquanta, uint64_t nval, uint64_t incr) 1885{ 1886 uint64_t arg = *lquanta++; 1887 int32_t base = DTRACE_LQUANTIZE_BASE(arg); 1888 uint16_t step = DTRACE_LQUANTIZE_STEP(arg); 1889 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(arg); 1890 int32_t val = (int32_t)nval, level; 1891 1892 ASSERT(step != 0); 1893 ASSERT(levels != 0); 1894 1895 if (val < base) { 1896 /* 1897 * This is an underflow. 1898 */ 1899 lquanta[0] += incr; 1900 return; 1901 } 1902 1903 level = (val - base) / step; 1904 1905 if (level < levels) { 1906 lquanta[level + 1] += incr; 1907 return; 1908 } 1909 1910 /* 1911 * This is an overflow. 1912 */ 1913 lquanta[levels + 1] += incr; 1914} 1915 1916/*ARGSUSED*/ 1917static void 1918dtrace_aggregate_avg(uint64_t *data, uint64_t nval, uint64_t arg) 1919{ 1920 data[0]++; 1921 data[1] += nval; 1922} 1923 1924/*ARGSUSED*/ 1925static void 1926dtrace_aggregate_stddev(uint64_t *data, uint64_t nval, uint64_t arg) 1927{ 1928 int64_t snval = (int64_t)nval; 1929 uint64_t tmp[2]; 1930 1931 data[0]++; 1932 data[1] += nval; 1933 1934 /* 1935 * What we want to say here is: 1936 * 1937 * data[2] += nval * nval; 1938 * 1939 * But given that nval is 64-bit, we could easily overflow, so 1940 * we do this as 128-bit arithmetic. 1941 */ 1942 if (snval < 0) 1943 snval = -snval; 1944 1945 dtrace_multiply_128((uint64_t)snval, (uint64_t)snval, tmp); 1946 dtrace_add_128(data + 2, tmp, data + 2); 1947} 1948 1949/*ARGSUSED*/ 1950static void 1951dtrace_aggregate_count(uint64_t *oval, uint64_t nval, uint64_t arg) 1952{ 1953 *oval = *oval + 1; 1954} 1955 1956/*ARGSUSED*/ 1957static void 1958dtrace_aggregate_sum(uint64_t *oval, uint64_t nval, uint64_t arg) 1959{ 1960 *oval += nval; 1961} 1962 1963/* 1964 * Aggregate given the tuple in the principal data buffer, and the aggregating 1965 * action denoted by the specified dtrace_aggregation_t. The aggregation 1966 * buffer is specified as the buf parameter. This routine does not return 1967 * failure; if there is no space in the aggregation buffer, the data will be 1968 * dropped, and a corresponding counter incremented. 1969 */ 1970static void 1971dtrace_aggregate(dtrace_aggregation_t *agg, dtrace_buffer_t *dbuf, 1972 intptr_t offset, dtrace_buffer_t *buf, uint64_t expr, uint64_t arg) 1973{ 1974 dtrace_recdesc_t *rec = &agg->dtag_action.dta_rec; 1975 uint32_t i, ndx, size, fsize; 1976 uint32_t align = sizeof (uint64_t) - 1; 1977 dtrace_aggbuffer_t *agb; 1978 dtrace_aggkey_t *key; 1979 uint32_t hashval = 0, limit, isstr; 1980 caddr_t tomax, data, kdata; 1981 dtrace_actkind_t action; 1982 dtrace_action_t *act; 1983 uintptr_t offs; 1984 1985 if (buf == NULL) 1986 return; 1987 1988 if (!agg->dtag_hasarg) { 1989 /* 1990 * Currently, only quantize() and lquantize() take additional 1991 * arguments, and they have the same semantics: an increment 1992 * value that defaults to 1 when not present. If additional 1993 * aggregating actions take arguments, the setting of the 1994 * default argument value will presumably have to become more 1995 * sophisticated... 1996 */ 1997 arg = 1; 1998 } 1999 2000 action = agg->dtag_action.dta_kind - DTRACEACT_AGGREGATION; 2001 size = rec->dtrd_offset - agg->dtag_base; 2002 fsize = size + rec->dtrd_size; 2003 2004 ASSERT(dbuf->dtb_tomax != NULL); 2005 data = dbuf->dtb_tomax + offset + agg->dtag_base; 2006 2007 if ((tomax = buf->dtb_tomax) == NULL) { 2008 dtrace_buffer_drop(buf); 2009 return; 2010 } 2011 2012 /* 2013 * The metastructure is always at the bottom of the buffer. 2014 */ 2015 agb = (dtrace_aggbuffer_t *)(tomax + buf->dtb_size - 2016 sizeof (dtrace_aggbuffer_t)); 2017 2018 if (buf->dtb_offset == 0) { 2019 /* 2020 * We just kludge up approximately 1/8th of the size to be 2021 * buckets. If this guess ends up being routinely 2022 * off-the-mark, we may need to dynamically readjust this 2023 * based on past performance. 2024 */ 2025 uintptr_t hashsize = (buf->dtb_size >> 3) / sizeof (uintptr_t); 2026 2027 if ((uintptr_t)agb - hashsize * sizeof (dtrace_aggkey_t *) < 2028 (uintptr_t)tomax || hashsize == 0) { 2029 /* 2030 * We've been given a ludicrously small buffer; 2031 * increment our drop count and leave. 2032 */ 2033 dtrace_buffer_drop(buf); 2034 return; 2035 } 2036 2037 /* 2038 * And now, a pathetic attempt to try to get a an odd (or 2039 * perchance, a prime) hash size for better hash distribution. 2040 */ 2041 if (hashsize > (DTRACE_AGGHASHSIZE_SLEW << 3)) 2042 hashsize -= DTRACE_AGGHASHSIZE_SLEW; 2043 2044 agb->dtagb_hashsize = hashsize; 2045 agb->dtagb_hash = (dtrace_aggkey_t **)((uintptr_t)agb - 2046 agb->dtagb_hashsize * sizeof (dtrace_aggkey_t *)); 2047 agb->dtagb_free = (uintptr_t)agb->dtagb_hash; 2048 2049 for (i = 0; i < agb->dtagb_hashsize; i++) 2050 agb->dtagb_hash[i] = NULL; 2051 } 2052 2053 ASSERT(agg->dtag_first != NULL); 2054 ASSERT(agg->dtag_first->dta_intuple); 2055 2056 /* 2057 * Calculate the hash value based on the key. Note that we _don't_ 2058 * include the aggid in the hashing (but we will store it as part of 2059 * the key). The hashing algorithm is Bob Jenkins' "One-at-a-time" 2060 * algorithm: a simple, quick algorithm that has no known funnels, and 2061 * gets good distribution in practice. The efficacy of the hashing 2062 * algorithm (and a comparison with other algorithms) may be found by 2063 * running the ::dtrace_aggstat MDB dcmd. 2064 */ 2065 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2066 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2067 limit = i + act->dta_rec.dtrd_size; 2068 ASSERT(limit <= size); 2069 isstr = DTRACEACT_ISSTRING(act); 2070 2071 for (; i < limit; i++) { 2072 hashval += data[i]; 2073 hashval += (hashval << 10); 2074 hashval ^= (hashval >> 6); 2075 2076 if (isstr && data[i] == '\0') 2077 break; 2078 } 2079 } 2080 2081 hashval += (hashval << 3); 2082 hashval ^= (hashval >> 11); 2083 hashval += (hashval << 15); 2084 2085 /* 2086 * Yes, the divide here is expensive -- but it's generally the least 2087 * of the performance issues given the amount of data that we iterate 2088 * over to compute hash values, compare data, etc. 2089 */ 2090 ndx = hashval % agb->dtagb_hashsize; 2091 2092 for (key = agb->dtagb_hash[ndx]; key != NULL; key = key->dtak_next) { 2093 ASSERT((caddr_t)key >= tomax); 2094 ASSERT((caddr_t)key < tomax + buf->dtb_size); 2095 2096 if (hashval != key->dtak_hashval || key->dtak_size != size) 2097 continue; 2098 2099 kdata = key->dtak_data; 2100 ASSERT(kdata >= tomax && kdata < tomax + buf->dtb_size); 2101 2102 for (act = agg->dtag_first; act->dta_intuple; 2103 act = act->dta_next) { 2104 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2105 limit = i + act->dta_rec.dtrd_size; 2106 ASSERT(limit <= size); 2107 isstr = DTRACEACT_ISSTRING(act); 2108 2109 for (; i < limit; i++) { 2110 if (kdata[i] != data[i]) 2111 goto next; 2112 2113 if (isstr && data[i] == '\0') 2114 break; 2115 } 2116 } 2117 2118 if (action != key->dtak_action) { 2119 /* 2120 * We are aggregating on the same value in the same 2121 * aggregation with two different aggregating actions. 2122 * (This should have been picked up in the compiler, 2123 * so we may be dealing with errant or devious DIF.) 2124 * This is an error condition; we indicate as much, 2125 * and return. 2126 */ 2127 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 2128 return; 2129 } 2130 2131 /* 2132 * This is a hit: we need to apply the aggregator to 2133 * the value at this key. 2134 */ 2135 agg->dtag_aggregate((uint64_t *)(kdata + size), expr, arg); 2136 return; 2137next: 2138 continue; 2139 } 2140 2141 /* 2142 * We didn't find it. We need to allocate some zero-filled space, 2143 * link it into the hash table appropriately, and apply the aggregator 2144 * to the (zero-filled) value. 2145 */ 2146 offs = buf->dtb_offset; 2147 while (offs & (align - 1)) 2148 offs += sizeof (uint32_t); 2149 2150 /* 2151 * If we don't have enough room to both allocate a new key _and_ 2152 * its associated data, increment the drop count and return. 2153 */ 2154 if ((uintptr_t)tomax + offs + fsize > 2155 agb->dtagb_free - sizeof (dtrace_aggkey_t)) { 2156 dtrace_buffer_drop(buf); 2157 return; 2158 } 2159 2160 /*CONSTCOND*/ 2161 ASSERT(!(sizeof (dtrace_aggkey_t) & (sizeof (uintptr_t) - 1))); 2162 key = (dtrace_aggkey_t *)(agb->dtagb_free - sizeof (dtrace_aggkey_t)); 2163 agb->dtagb_free -= sizeof (dtrace_aggkey_t); 2164 2165 key->dtak_data = kdata = tomax + offs; 2166 buf->dtb_offset = offs + fsize; 2167 2168 /* 2169 * Now copy the data across. 2170 */ 2171 *((dtrace_aggid_t *)kdata) = agg->dtag_id; 2172 2173 for (i = sizeof (dtrace_aggid_t); i < size; i++) 2174 kdata[i] = data[i]; 2175 2176 /* 2177 * Because strings are not zeroed out by default, we need to iterate 2178 * looking for actions that store strings, and we need to explicitly 2179 * pad these strings out with zeroes. 2180 */ 2181 for (act = agg->dtag_first; act->dta_intuple; act = act->dta_next) { 2182 int nul; 2183 2184 if (!DTRACEACT_ISSTRING(act)) 2185 continue; 2186 2187 i = act->dta_rec.dtrd_offset - agg->dtag_base; 2188 limit = i + act->dta_rec.dtrd_size; 2189 ASSERT(limit <= size); 2190 2191 for (nul = 0; i < limit; i++) { 2192 if (nul) { 2193 kdata[i] = '\0'; 2194 continue; 2195 } 2196 2197 if (data[i] != '\0') 2198 continue; 2199 2200 nul = 1; 2201 } 2202 } 2203 2204 for (i = size; i < fsize; i++) 2205 kdata[i] = 0; 2206 2207 key->dtak_hashval = hashval; 2208 key->dtak_size = size; 2209 key->dtak_action = action; 2210 key->dtak_next = agb->dtagb_hash[ndx]; 2211 agb->dtagb_hash[ndx] = key; 2212 2213 /* 2214 * Finally, apply the aggregator. 2215 */ 2216 *((uint64_t *)(key->dtak_data + size)) = agg->dtag_initial; 2217 agg->dtag_aggregate((uint64_t *)(key->dtak_data + size), expr, arg); 2218} 2219 2220/* 2221 * Given consumer state, this routine finds a speculation in the INACTIVE 2222 * state and transitions it into the ACTIVE state. If there is no speculation 2223 * in the INACTIVE state, 0 is returned. In this case, no error counter is 2224 * incremented -- it is up to the caller to take appropriate action. 2225 */ 2226static int 2227dtrace_speculation(dtrace_state_t *state) 2228{ 2229 int i = 0; 2230 dtrace_speculation_state_t current; 2231 uint32_t *stat = &state->dts_speculations_unavail, count; 2232 2233 while (i < state->dts_nspeculations) { 2234 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2235 2236 current = spec->dtsp_state; 2237 2238 if (current != DTRACESPEC_INACTIVE) { 2239 if (current == DTRACESPEC_COMMITTINGMANY || 2240 current == DTRACESPEC_COMMITTING || 2241 current == DTRACESPEC_DISCARDING) 2242 stat = &state->dts_speculations_busy; 2243 i++; 2244 continue; 2245 } 2246 2247 if (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2248 current, DTRACESPEC_ACTIVE) == current) 2249 return (i + 1); 2250 } 2251 2252 /* 2253 * We couldn't find a speculation. If we found as much as a single 2254 * busy speculation buffer, we'll attribute this failure as "busy" 2255 * instead of "unavail". 2256 */ 2257 do { 2258 count = *stat; 2259 } while (dtrace_cas32(stat, count, count + 1) != count); 2260 2261 return (0); 2262} 2263 2264/* 2265 * This routine commits an active speculation. If the specified speculation 2266 * is not in a valid state to perform a commit(), this routine will silently do 2267 * nothing. The state of the specified speculation is transitioned according 2268 * to the state transition diagram outlined in <sys/dtrace_impl.h> 2269 */ 2270static void 2271dtrace_speculation_commit(dtrace_state_t *state, processorid_t cpu, 2272 dtrace_specid_t which) 2273{ 2274 dtrace_speculation_t *spec; 2275 dtrace_buffer_t *src, *dest; 2276 uintptr_t daddr, saddr, dlimit; 2277 dtrace_speculation_state_t current, new = 0; 2278 intptr_t offs; 2279 2280 if (which == 0) 2281 return; 2282 2283 if (which > state->dts_nspeculations) { 2284 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2285 return; 2286 } 2287 2288 spec = &state->dts_speculations[which - 1]; 2289 src = &spec->dtsp_buffer[cpu]; 2290 dest = &state->dts_buffer[cpu]; 2291 2292 do { 2293 current = spec->dtsp_state; 2294 2295 if (current == DTRACESPEC_COMMITTINGMANY) 2296 break; 2297 2298 switch (current) { 2299 case DTRACESPEC_INACTIVE: 2300 case DTRACESPEC_DISCARDING: 2301 return; 2302 2303 case DTRACESPEC_COMMITTING: 2304 /* 2305 * This is only possible if we are (a) commit()'ing 2306 * without having done a prior speculate() on this CPU 2307 * and (b) racing with another commit() on a different 2308 * CPU. There's nothing to do -- we just assert that 2309 * our offset is 0. 2310 */ 2311 ASSERT(src->dtb_offset == 0); 2312 return; 2313 2314 case DTRACESPEC_ACTIVE: 2315 new = DTRACESPEC_COMMITTING; 2316 break; 2317 2318 case DTRACESPEC_ACTIVEONE: 2319 /* 2320 * This speculation is active on one CPU. If our 2321 * buffer offset is non-zero, we know that the one CPU 2322 * must be us. Otherwise, we are committing on a 2323 * different CPU from the speculate(), and we must 2324 * rely on being asynchronously cleaned. 2325 */ 2326 if (src->dtb_offset != 0) { 2327 new = DTRACESPEC_COMMITTING; 2328 break; 2329 } 2330 /*FALLTHROUGH*/ 2331 2332 case DTRACESPEC_ACTIVEMANY: 2333 new = DTRACESPEC_COMMITTINGMANY; 2334 break; 2335 2336 default: 2337 ASSERT(0); 2338 } 2339 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2340 current, new) != current); 2341 2342 /* 2343 * We have set the state to indicate that we are committing this 2344 * speculation. Now reserve the necessary space in the destination 2345 * buffer. 2346 */ 2347 if ((offs = dtrace_buffer_reserve(dest, src->dtb_offset, 2348 sizeof (uint64_t), state, NULL)) < 0) { 2349 dtrace_buffer_drop(dest); 2350 goto out; 2351 } 2352 2353 /* 2354 * We have the space; copy the buffer across. (Note that this is a 2355 * highly subobtimal bcopy(); in the unlikely event that this becomes 2356 * a serious performance issue, a high-performance DTrace-specific 2357 * bcopy() should obviously be invented.) 2358 */ 2359 daddr = (uintptr_t)dest->dtb_tomax + offs; 2360 dlimit = daddr + src->dtb_offset; 2361 saddr = (uintptr_t)src->dtb_tomax; 2362 2363 /* 2364 * First, the aligned portion. 2365 */ 2366 while (dlimit - daddr >= sizeof (uint64_t)) { 2367 *((uint64_t *)daddr) = *((uint64_t *)saddr); 2368 2369 daddr += sizeof (uint64_t); 2370 saddr += sizeof (uint64_t); 2371 } 2372 2373 /* 2374 * Now any left-over bit... 2375 */ 2376 while (dlimit - daddr) 2377 *((uint8_t *)daddr++) = *((uint8_t *)saddr++); 2378 2379 /* 2380 * Finally, commit the reserved space in the destination buffer. 2381 */ 2382 dest->dtb_offset = offs + src->dtb_offset; 2383 2384out: 2385 /* 2386 * If we're lucky enough to be the only active CPU on this speculation 2387 * buffer, we can just set the state back to DTRACESPEC_INACTIVE. 2388 */ 2389 if (current == DTRACESPEC_ACTIVE || 2390 (current == DTRACESPEC_ACTIVEONE && new == DTRACESPEC_COMMITTING)) { 2391 uint32_t rval = dtrace_cas32((uint32_t *)&spec->dtsp_state, 2392 DTRACESPEC_COMMITTING, DTRACESPEC_INACTIVE); 2393 2394 ASSERT(rval == DTRACESPEC_COMMITTING); 2395 } 2396 2397 src->dtb_offset = 0; 2398 src->dtb_xamot_drops += src->dtb_drops; 2399 src->dtb_drops = 0; 2400} 2401 2402/* 2403 * This routine discards an active speculation. If the specified speculation 2404 * is not in a valid state to perform a discard(), this routine will silently 2405 * do nothing. The state of the specified speculation is transitioned 2406 * according to the state transition diagram outlined in <sys/dtrace_impl.h> 2407 */ 2408static void 2409dtrace_speculation_discard(dtrace_state_t *state, processorid_t cpu, 2410 dtrace_specid_t which) 2411{ 2412 dtrace_speculation_t *spec; 2413 dtrace_speculation_state_t current, new = 0; 2414 dtrace_buffer_t *buf; 2415 2416 if (which == 0) 2417 return; 2418 2419 if (which > state->dts_nspeculations) { 2420 cpu_core[cpu].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2421 return; 2422 } 2423 2424 spec = &state->dts_speculations[which - 1]; 2425 buf = &spec->dtsp_buffer[cpu]; 2426 2427 do { 2428 current = spec->dtsp_state; 2429 2430 switch (current) { 2431 case DTRACESPEC_INACTIVE: 2432 case DTRACESPEC_COMMITTINGMANY: 2433 case DTRACESPEC_COMMITTING: 2434 case DTRACESPEC_DISCARDING: 2435 return; 2436 2437 case DTRACESPEC_ACTIVE: 2438 case DTRACESPEC_ACTIVEMANY: 2439 new = DTRACESPEC_DISCARDING; 2440 break; 2441 2442 case DTRACESPEC_ACTIVEONE: 2443 if (buf->dtb_offset != 0) { 2444 new = DTRACESPEC_INACTIVE; 2445 } else { 2446 new = DTRACESPEC_DISCARDING; 2447 } 2448 break; 2449 2450 default: 2451 ASSERT(0); 2452 } 2453 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2454 current, new) != current); 2455 2456 buf->dtb_offset = 0; 2457 buf->dtb_drops = 0; 2458} 2459 2460/* 2461 * Note: not called from probe context. This function is called 2462 * asynchronously from cross call context to clean any speculations that are 2463 * in the COMMITTINGMANY or DISCARDING states. These speculations may not be 2464 * transitioned back to the INACTIVE state until all CPUs have cleaned the 2465 * speculation. 2466 */ 2467static void 2468dtrace_speculation_clean_here(dtrace_state_t *state) 2469{ 2470 dtrace_icookie_t cookie; 2471 processorid_t cpu = curcpu; 2472 dtrace_buffer_t *dest = &state->dts_buffer[cpu]; 2473 dtrace_specid_t i; 2474 2475 cookie = dtrace_interrupt_disable(); 2476 2477 if (dest->dtb_tomax == NULL) { 2478 dtrace_interrupt_enable(cookie); 2479 return; 2480 } 2481 2482 for (i = 0; i < state->dts_nspeculations; i++) { 2483 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2484 dtrace_buffer_t *src = &spec->dtsp_buffer[cpu]; 2485 2486 if (src->dtb_tomax == NULL) 2487 continue; 2488 2489 if (spec->dtsp_state == DTRACESPEC_DISCARDING) { 2490 src->dtb_offset = 0; 2491 continue; 2492 } 2493 2494 if (spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2495 continue; 2496 2497 if (src->dtb_offset == 0) 2498 continue; 2499 2500 dtrace_speculation_commit(state, cpu, i + 1); 2501 } 2502 2503 dtrace_interrupt_enable(cookie); 2504} 2505 2506/* 2507 * Note: not called from probe context. This function is called 2508 * asynchronously (and at a regular interval) to clean any speculations that 2509 * are in the COMMITTINGMANY or DISCARDING states. If it discovers that there 2510 * is work to be done, it cross calls all CPUs to perform that work; 2511 * COMMITMANY and DISCARDING speculations may not be transitioned back to the 2512 * INACTIVE state until they have been cleaned by all CPUs. 2513 */ 2514static void 2515dtrace_speculation_clean(dtrace_state_t *state) 2516{ 2517 int work = 0, rv; 2518 dtrace_specid_t i; 2519 2520 for (i = 0; i < state->dts_nspeculations; i++) { 2521 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2522 2523 ASSERT(!spec->dtsp_cleaning); 2524 2525 if (spec->dtsp_state != DTRACESPEC_DISCARDING && 2526 spec->dtsp_state != DTRACESPEC_COMMITTINGMANY) 2527 continue; 2528 2529 work++; 2530 spec->dtsp_cleaning = 1; 2531 } 2532 2533 if (!work) 2534 return; 2535 2536 dtrace_xcall(DTRACE_CPUALL, 2537 (dtrace_xcall_t)dtrace_speculation_clean_here, state); 2538 2539 /* 2540 * We now know that all CPUs have committed or discarded their 2541 * speculation buffers, as appropriate. We can now set the state 2542 * to inactive. 2543 */ 2544 for (i = 0; i < state->dts_nspeculations; i++) { 2545 dtrace_speculation_t *spec = &state->dts_speculations[i]; 2546 dtrace_speculation_state_t current, new; 2547 2548 if (!spec->dtsp_cleaning) 2549 continue; 2550 2551 current = spec->dtsp_state; 2552 ASSERT(current == DTRACESPEC_DISCARDING || 2553 current == DTRACESPEC_COMMITTINGMANY); 2554 2555 new = DTRACESPEC_INACTIVE; 2556 2557 rv = dtrace_cas32((uint32_t *)&spec->dtsp_state, current, new); 2558 ASSERT(rv == current); 2559 spec->dtsp_cleaning = 0; 2560 } 2561} 2562 2563/* 2564 * Called as part of a speculate() to get the speculative buffer associated 2565 * with a given speculation. Returns NULL if the specified speculation is not 2566 * in an ACTIVE state. If the speculation is in the ACTIVEONE state -- and 2567 * the active CPU is not the specified CPU -- the speculation will be 2568 * atomically transitioned into the ACTIVEMANY state. 2569 */ 2570static dtrace_buffer_t * 2571dtrace_speculation_buffer(dtrace_state_t *state, processorid_t cpuid, 2572 dtrace_specid_t which) 2573{ 2574 dtrace_speculation_t *spec; 2575 dtrace_speculation_state_t current, new = 0; 2576 dtrace_buffer_t *buf; 2577 2578 if (which == 0) 2579 return (NULL); 2580 2581 if (which > state->dts_nspeculations) { 2582 cpu_core[cpuid].cpuc_dtrace_flags |= CPU_DTRACE_ILLOP; 2583 return (NULL); 2584 } 2585 2586 spec = &state->dts_speculations[which - 1]; 2587 buf = &spec->dtsp_buffer[cpuid]; 2588 2589 do { 2590 current = spec->dtsp_state; 2591 2592 switch (current) { 2593 case DTRACESPEC_INACTIVE: 2594 case DTRACESPEC_COMMITTINGMANY: 2595 case DTRACESPEC_DISCARDING: 2596 return (NULL); 2597 2598 case DTRACESPEC_COMMITTING: 2599 ASSERT(buf->dtb_offset == 0); 2600 return (NULL); 2601 2602 case DTRACESPEC_ACTIVEONE: 2603 /* 2604 * This speculation is currently active on one CPU. 2605 * Check the offset in the buffer; if it's non-zero, 2606 * that CPU must be us (and we leave the state alone). 2607 * If it's zero, assume that we're starting on a new 2608 * CPU -- and change the state to indicate that the 2609 * speculation is active on more than one CPU. 2610 */ 2611 if (buf->dtb_offset != 0) 2612 return (buf); 2613 2614 new = DTRACESPEC_ACTIVEMANY; 2615 break; 2616 2617 case DTRACESPEC_ACTIVEMANY: 2618 return (buf); 2619 2620 case DTRACESPEC_ACTIVE: 2621 new = DTRACESPEC_ACTIVEONE; 2622 break; 2623 2624 default: 2625 ASSERT(0); 2626 } 2627 } while (dtrace_cas32((uint32_t *)&spec->dtsp_state, 2628 current, new) != current); 2629 2630 ASSERT(new == DTRACESPEC_ACTIVEONE || new == DTRACESPEC_ACTIVEMANY); 2631 return (buf); 2632} 2633 2634/* 2635 * Return a string. In the event that the user lacks the privilege to access 2636 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2637 * don't fail access checking. 2638 * 2639 * dtrace_dif_variable() uses this routine as a helper for various 2640 * builtin values such as 'execname' and 'probefunc.' 2641 */ 2642uintptr_t 2643dtrace_dif_varstr(uintptr_t addr, dtrace_state_t *state, 2644 dtrace_mstate_t *mstate) 2645{ 2646 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 2647 uintptr_t ret; 2648 size_t strsz; 2649 2650 /* 2651 * The easy case: this probe is allowed to read all of memory, so 2652 * we can just return this as a vanilla pointer. 2653 */ 2654 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) != 0) 2655 return (addr); 2656 2657 /* 2658 * This is the tougher case: we copy the string in question from 2659 * kernel memory into scratch memory and return it that way: this 2660 * ensures that we won't trip up when access checking tests the 2661 * BYREF return value. 2662 */ 2663 strsz = dtrace_strlen((char *)addr, size) + 1; 2664 2665 if (mstate->dtms_scratch_ptr + strsz > 2666 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2667 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2668 return (0); 2669 } 2670 2671 dtrace_strcpy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2672 strsz); 2673 ret = mstate->dtms_scratch_ptr; 2674 mstate->dtms_scratch_ptr += strsz; 2675 return (ret); 2676} 2677 2678/* 2679 * Return a string from a memoy address which is known to have one or 2680 * more concatenated, individually zero terminated, sub-strings. 2681 * In the event that the user lacks the privilege to access 2682 * arbitrary kernel memory, we copy the string out to scratch memory so that we 2683 * don't fail access checking. 2684 * 2685 * dtrace_dif_variable() uses this routine as a helper for various 2686 * builtin values such as 'execargs'. 2687 */ 2688static uintptr_t 2689dtrace_dif_varstrz(uintptr_t addr, size_t strsz, dtrace_state_t *state, 2690 dtrace_mstate_t *mstate) 2691{ 2692 char *p; 2693 size_t i; 2694 uintptr_t ret; 2695 2696 if (mstate->dtms_scratch_ptr + strsz > 2697 mstate->dtms_scratch_base + mstate->dtms_scratch_size) { 2698 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 2699 return (0); 2700 } 2701 2702 dtrace_bcopy((const void *)addr, (void *)mstate->dtms_scratch_ptr, 2703 strsz); 2704 2705 /* Replace sub-string termination characters with a space. */ 2706 for (p = (char *) mstate->dtms_scratch_ptr, i = 0; i < strsz - 1; 2707 p++, i++) 2708 if (*p == '\0') 2709 *p = ' '; 2710 2711 ret = mstate->dtms_scratch_ptr; 2712 mstate->dtms_scratch_ptr += strsz; 2713 return (ret); 2714} 2715 2716/* 2717 * This function implements the DIF emulator's variable lookups. The emulator 2718 * passes a reserved variable identifier and optional built-in array index. 2719 */ 2720static uint64_t 2721dtrace_dif_variable(dtrace_mstate_t *mstate, dtrace_state_t *state, uint64_t v, 2722 uint64_t ndx) 2723{ 2724 /* 2725 * If we're accessing one of the uncached arguments, we'll turn this 2726 * into a reference in the args array. 2727 */ 2728 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) { 2729 ndx = v - DIF_VAR_ARG0; 2730 v = DIF_VAR_ARGS; 2731 } 2732 2733 switch (v) { 2734 case DIF_VAR_ARGS: 2735 ASSERT(mstate->dtms_present & DTRACE_MSTATE_ARGS); 2736 if (ndx >= sizeof (mstate->dtms_arg) / 2737 sizeof (mstate->dtms_arg[0])) { 2738 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2739 dtrace_provider_t *pv; 2740 uint64_t val; 2741 2742 pv = mstate->dtms_probe->dtpr_provider; 2743 if (pv->dtpv_pops.dtps_getargval != NULL) 2744 val = pv->dtpv_pops.dtps_getargval(pv->dtpv_arg, 2745 mstate->dtms_probe->dtpr_id, 2746 mstate->dtms_probe->dtpr_arg, ndx, aframes); 2747 else 2748 val = dtrace_getarg(ndx, aframes); 2749 2750 /* 2751 * This is regrettably required to keep the compiler 2752 * from tail-optimizing the call to dtrace_getarg(). 2753 * The condition always evaluates to true, but the 2754 * compiler has no way of figuring that out a priori. 2755 * (None of this would be necessary if the compiler 2756 * could be relied upon to _always_ tail-optimize 2757 * the call to dtrace_getarg() -- but it can't.) 2758 */ 2759 if (mstate->dtms_probe != NULL) 2760 return (val); 2761 2762 ASSERT(0); 2763 } 2764 2765 return (mstate->dtms_arg[ndx]); 2766 2767#if defined(sun) 2768 case DIF_VAR_UREGS: { 2769 klwp_t *lwp; 2770 2771 if (!dtrace_priv_proc(state)) 2772 return (0); 2773 2774 if ((lwp = curthread->t_lwp) == NULL) { 2775 DTRACE_CPUFLAG_SET(CPU_DTRACE_BADADDR); 2776 cpu_core[curcpu].cpuc_dtrace_illval = NULL; 2777 return (0); 2778 } 2779 2780 return (dtrace_getreg(lwp->lwp_regs, ndx)); 2781 return (0); 2782 } 2783#endif 2784 2785 case DIF_VAR_CURTHREAD: 2786 if (!dtrace_priv_kernel(state)) 2787 return (0); 2788 return ((uint64_t)(uintptr_t)curthread); 2789 2790 case DIF_VAR_TIMESTAMP: 2791 if (!(mstate->dtms_present & DTRACE_MSTATE_TIMESTAMP)) { 2792 mstate->dtms_timestamp = dtrace_gethrtime(); 2793 mstate->dtms_present |= DTRACE_MSTATE_TIMESTAMP; 2794 } 2795 return (mstate->dtms_timestamp); 2796 2797 case DIF_VAR_VTIMESTAMP: 2798 ASSERT(dtrace_vtime_references != 0); 2799 return (curthread->t_dtrace_vtime); 2800 2801 case DIF_VAR_WALLTIMESTAMP: 2802 if (!(mstate->dtms_present & DTRACE_MSTATE_WALLTIMESTAMP)) { 2803 mstate->dtms_walltimestamp = dtrace_gethrestime(); 2804 mstate->dtms_present |= DTRACE_MSTATE_WALLTIMESTAMP; 2805 } 2806 return (mstate->dtms_walltimestamp); 2807 2808#if defined(sun) 2809 case DIF_VAR_IPL: 2810 if (!dtrace_priv_kernel(state)) 2811 return (0); 2812 if (!(mstate->dtms_present & DTRACE_MSTATE_IPL)) { 2813 mstate->dtms_ipl = dtrace_getipl(); 2814 mstate->dtms_present |= DTRACE_MSTATE_IPL; 2815 } 2816 return (mstate->dtms_ipl); 2817#endif 2818 2819 case DIF_VAR_EPID: 2820 ASSERT(mstate->dtms_present & DTRACE_MSTATE_EPID); 2821 return (mstate->dtms_epid); 2822 2823 case DIF_VAR_ID: 2824 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2825 return (mstate->dtms_probe->dtpr_id); 2826 2827 case DIF_VAR_STACKDEPTH: 2828 if (!dtrace_priv_kernel(state)) 2829 return (0); 2830 if (!(mstate->dtms_present & DTRACE_MSTATE_STACKDEPTH)) { 2831 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2832 2833 mstate->dtms_stackdepth = dtrace_getstackdepth(aframes); 2834 mstate->dtms_present |= DTRACE_MSTATE_STACKDEPTH; 2835 } 2836 return (mstate->dtms_stackdepth); 2837 2838#if defined(sun) 2839 case DIF_VAR_USTACKDEPTH: 2840 if (!dtrace_priv_proc(state)) 2841 return (0); 2842 if (!(mstate->dtms_present & DTRACE_MSTATE_USTACKDEPTH)) { 2843 /* 2844 * See comment in DIF_VAR_PID. 2845 */ 2846 if (DTRACE_ANCHORED(mstate->dtms_probe) && 2847 CPU_ON_INTR(CPU)) { 2848 mstate->dtms_ustackdepth = 0; 2849 } else { 2850 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2851 mstate->dtms_ustackdepth = 2852 dtrace_getustackdepth(); 2853 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2854 } 2855 mstate->dtms_present |= DTRACE_MSTATE_USTACKDEPTH; 2856 } 2857 return (mstate->dtms_ustackdepth); 2858#endif 2859 2860 case DIF_VAR_CALLER: 2861 if (!dtrace_priv_kernel(state)) 2862 return (0); 2863 if (!(mstate->dtms_present & DTRACE_MSTATE_CALLER)) { 2864 int aframes = mstate->dtms_probe->dtpr_aframes + 2; 2865 2866 if (!DTRACE_ANCHORED(mstate->dtms_probe)) { 2867 /* 2868 * If this is an unanchored probe, we are 2869 * required to go through the slow path: 2870 * dtrace_caller() only guarantees correct 2871 * results for anchored probes. 2872 */ 2873 pc_t caller[2] = {0, 0}; 2874 2875 dtrace_getpcstack(caller, 2, aframes, 2876 (uint32_t *)(uintptr_t)mstate->dtms_arg[0]); 2877 mstate->dtms_caller = caller[1]; 2878 } else if ((mstate->dtms_caller = 2879 dtrace_caller(aframes)) == -1) { 2880 /* 2881 * We have failed to do this the quick way; 2882 * we must resort to the slower approach of 2883 * calling dtrace_getpcstack(). 2884 */ 2885 pc_t caller = 0; 2886 2887 dtrace_getpcstack(&caller, 1, aframes, NULL); 2888 mstate->dtms_caller = caller; 2889 } 2890 2891 mstate->dtms_present |= DTRACE_MSTATE_CALLER; 2892 } 2893 return (mstate->dtms_caller); 2894 2895#if defined(sun) 2896 case DIF_VAR_UCALLER: 2897 if (!dtrace_priv_proc(state)) 2898 return (0); 2899 2900 if (!(mstate->dtms_present & DTRACE_MSTATE_UCALLER)) { 2901 uint64_t ustack[3]; 2902 2903 /* 2904 * dtrace_getupcstack() fills in the first uint64_t 2905 * with the current PID. The second uint64_t will 2906 * be the program counter at user-level. The third 2907 * uint64_t will contain the caller, which is what 2908 * we're after. 2909 */ 2910 ustack[2] = 0; 2911 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 2912 dtrace_getupcstack(ustack, 3); 2913 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 2914 mstate->dtms_ucaller = ustack[2]; 2915 mstate->dtms_present |= DTRACE_MSTATE_UCALLER; 2916 } 2917 2918 return (mstate->dtms_ucaller); 2919#endif 2920 2921 case DIF_VAR_PROBEPROV: 2922 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2923 return (dtrace_dif_varstr( 2924 (uintptr_t)mstate->dtms_probe->dtpr_provider->dtpv_name, 2925 state, mstate)); 2926 2927 case DIF_VAR_PROBEMOD: 2928 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2929 return (dtrace_dif_varstr( 2930 (uintptr_t)mstate->dtms_probe->dtpr_mod, 2931 state, mstate)); 2932 2933 case DIF_VAR_PROBEFUNC: 2934 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2935 return (dtrace_dif_varstr( 2936 (uintptr_t)mstate->dtms_probe->dtpr_func, 2937 state, mstate)); 2938 2939 case DIF_VAR_PROBENAME: 2940 ASSERT(mstate->dtms_present & DTRACE_MSTATE_PROBE); 2941 return (dtrace_dif_varstr( 2942 (uintptr_t)mstate->dtms_probe->dtpr_name, 2943 state, mstate)); 2944 2945 case DIF_VAR_PID: 2946 if (!dtrace_priv_proc(state)) 2947 return (0); 2948 2949#if defined(sun) 2950 /* 2951 * Note that we are assuming that an unanchored probe is 2952 * always due to a high-level interrupt. (And we're assuming 2953 * that there is only a single high level interrupt.) 2954 */ 2955 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2956 return (pid0.pid_id); 2957 2958 /* 2959 * It is always safe to dereference one's own t_procp pointer: 2960 * it always points to a valid, allocated proc structure. 2961 * Further, it is always safe to dereference the p_pidp member 2962 * of one's own proc structure. (These are truisms becuase 2963 * threads and processes don't clean up their own state -- 2964 * they leave that task to whomever reaps them.) 2965 */ 2966 return ((uint64_t)curthread->t_procp->p_pidp->pid_id); 2967#else 2968 return ((uint64_t)curproc->p_pid); 2969#endif 2970 2971 case DIF_VAR_PPID: 2972 if (!dtrace_priv_proc(state)) 2973 return (0); 2974 2975#if defined(sun) 2976 /* 2977 * See comment in DIF_VAR_PID. 2978 */ 2979 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2980 return (pid0.pid_id); 2981 2982 /* 2983 * It is always safe to dereference one's own t_procp pointer: 2984 * it always points to a valid, allocated proc structure. 2985 * (This is true because threads don't clean up their own 2986 * state -- they leave that task to whomever reaps them.) 2987 */ 2988 return ((uint64_t)curthread->t_procp->p_ppid); 2989#else 2990 return ((uint64_t)curproc->p_pptr->p_pid); 2991#endif 2992 2993 case DIF_VAR_TID: 2994#if defined(sun) 2995 /* 2996 * See comment in DIF_VAR_PID. 2997 */ 2998 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 2999 return (0); 3000#endif 3001 3002 return ((uint64_t)curthread->t_tid); 3003 3004 case DIF_VAR_EXECARGS: { 3005 struct pargs *p_args = curthread->td_proc->p_args; 3006 3007 return (dtrace_dif_varstrz( 3008 (uintptr_t) p_args->ar_args, p_args->ar_length, state, mstate)); 3009 } 3010 3011 case DIF_VAR_EXECNAME: 3012#if defined(sun) 3013 if (!dtrace_priv_proc(state)) 3014 return (0); 3015 3016 /* 3017 * See comment in DIF_VAR_PID. 3018 */ 3019 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3020 return ((uint64_t)(uintptr_t)p0.p_user.u_comm); 3021 3022 /* 3023 * It is always safe to dereference one's own t_procp pointer: 3024 * it always points to a valid, allocated proc structure. 3025 * (This is true because threads don't clean up their own 3026 * state -- they leave that task to whomever reaps them.) 3027 */ 3028 return (dtrace_dif_varstr( 3029 (uintptr_t)curthread->t_procp->p_user.u_comm, 3030 state, mstate)); 3031#else 3032 return (dtrace_dif_varstr( 3033 (uintptr_t) curthread->td_proc->p_comm, state, mstate)); 3034#endif 3035 3036 case DIF_VAR_ZONENAME: 3037#if defined(sun) 3038 if (!dtrace_priv_proc(state)) 3039 return (0); 3040 3041 /* 3042 * See comment in DIF_VAR_PID. 3043 */ 3044 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3045 return ((uint64_t)(uintptr_t)p0.p_zone->zone_name); 3046 3047 /* 3048 * It is always safe to dereference one's own t_procp pointer: 3049 * it always points to a valid, allocated proc structure. 3050 * (This is true because threads don't clean up their own 3051 * state -- they leave that task to whomever reaps them.) 3052 */ 3053 return (dtrace_dif_varstr( 3054 (uintptr_t)curthread->t_procp->p_zone->zone_name, 3055 state, mstate)); 3056#else 3057 return (0); 3058#endif 3059 3060 case DIF_VAR_UID: 3061 if (!dtrace_priv_proc(state)) 3062 return (0); 3063 3064#if defined(sun) 3065 /* 3066 * See comment in DIF_VAR_PID. 3067 */ 3068 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3069 return ((uint64_t)p0.p_cred->cr_uid); 3070#endif 3071 3072 /* 3073 * It is always safe to dereference one's own t_procp pointer: 3074 * it always points to a valid, allocated proc structure. 3075 * (This is true because threads don't clean up their own 3076 * state -- they leave that task to whomever reaps them.) 3077 * 3078 * Additionally, it is safe to dereference one's own process 3079 * credential, since this is never NULL after process birth. 3080 */ 3081 return ((uint64_t)curthread->t_procp->p_cred->cr_uid); 3082 3083 case DIF_VAR_GID: 3084 if (!dtrace_priv_proc(state)) 3085 return (0); 3086 3087#if defined(sun) 3088 /* 3089 * See comment in DIF_VAR_PID. 3090 */ 3091 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3092 return ((uint64_t)p0.p_cred->cr_gid); 3093#endif 3094 3095 /* 3096 * It is always safe to dereference one's own t_procp pointer: 3097 * it always points to a valid, allocated proc structure. 3098 * (This is true because threads don't clean up their own 3099 * state -- they leave that task to whomever reaps them.) 3100 * 3101 * Additionally, it is safe to dereference one's own process 3102 * credential, since this is never NULL after process birth. 3103 */ 3104 return ((uint64_t)curthread->t_procp->p_cred->cr_gid); 3105 3106 case DIF_VAR_ERRNO: { 3107#if defined(sun) 3108 klwp_t *lwp; 3109 if (!dtrace_priv_proc(state)) 3110 return (0); 3111 3112 /* 3113 * See comment in DIF_VAR_PID. 3114 */ 3115 if (DTRACE_ANCHORED(mstate->dtms_probe) && CPU_ON_INTR(CPU)) 3116 return (0); 3117 3118 /* 3119 * It is always safe to dereference one's own t_lwp pointer in 3120 * the event that this pointer is non-NULL. (This is true 3121 * because threads and lwps don't clean up their own state -- 3122 * they leave that task to whomever reaps them.) 3123 */ 3124 if ((lwp = curthread->t_lwp) == NULL) 3125 return (0); 3126 3127 return ((uint64_t)lwp->lwp_errno); 3128#else 3129 return (curthread->td_errno); 3130#endif 3131 } 3132 default: 3133 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 3134 return (0); 3135 } 3136} 3137 3138/* 3139 * Emulate the execution of DTrace ID subroutines invoked by the call opcode. 3140 * Notice that we don't bother validating the proper number of arguments or 3141 * their types in the tuple stack. This isn't needed because all argument 3142 * interpretation is safe because of our load safety -- the worst that can 3143 * happen is that a bogus program can obtain bogus results. 3144 */ 3145static void 3146dtrace_dif_subr(uint_t subr, uint_t rd, uint64_t *regs, 3147 dtrace_key_t *tupregs, int nargs, 3148 dtrace_mstate_t *mstate, dtrace_state_t *state) 3149{ 3150 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 3151 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 3152 dtrace_vstate_t *vstate = &state->dts_vstate; 3153 3154#if defined(sun) 3155 union { 3156 mutex_impl_t mi; 3157 uint64_t mx; 3158 } m; 3159 3160 union { 3161 krwlock_t ri; 3162 uintptr_t rw; 3163 } r; 3164#else 3165 union { 3166 struct mtx *mi; 3167 uintptr_t mx; 3168 } m; 3169 union { 3170 struct sx *si; 3171 uintptr_t sx; 3172 } s; 3173#endif 3174 3175 switch (subr) { 3176 case DIF_SUBR_RAND: 3177 regs[rd] = (dtrace_gethrtime() * 2416 + 374441) % 1771875; 3178 break; 3179 3180#if defined(sun) 3181 case DIF_SUBR_MUTEX_OWNED: 3182 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3183 mstate, vstate)) { 3184 regs[rd] = 0; 3185 break; 3186 } 3187 3188 m.mx = dtrace_load64(tupregs[0].dttk_value); 3189 if (MUTEX_TYPE_ADAPTIVE(&m.mi)) 3190 regs[rd] = MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER; 3191 else 3192 regs[rd] = LOCK_HELD(&m.mi.m_spin.m_spinlock); 3193 break; 3194 3195 case DIF_SUBR_MUTEX_OWNER: 3196 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3197 mstate, vstate)) { 3198 regs[rd] = 0; 3199 break; 3200 } 3201 3202 m.mx = dtrace_load64(tupregs[0].dttk_value); 3203 if (MUTEX_TYPE_ADAPTIVE(&m.mi) && 3204 MUTEX_OWNER(&m.mi) != MUTEX_NO_OWNER) 3205 regs[rd] = (uintptr_t)MUTEX_OWNER(&m.mi); 3206 else 3207 regs[rd] = 0; 3208 break; 3209 3210 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3211 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3212 mstate, vstate)) { 3213 regs[rd] = 0; 3214 break; 3215 } 3216 3217 m.mx = dtrace_load64(tupregs[0].dttk_value); 3218 regs[rd] = MUTEX_TYPE_ADAPTIVE(&m.mi); 3219 break; 3220 3221 case DIF_SUBR_MUTEX_TYPE_SPIN: 3222 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (kmutex_t), 3223 mstate, vstate)) { 3224 regs[rd] = 0; 3225 break; 3226 } 3227 3228 m.mx = dtrace_load64(tupregs[0].dttk_value); 3229 regs[rd] = MUTEX_TYPE_SPIN(&m.mi); 3230 break; 3231 3232 case DIF_SUBR_RW_READ_HELD: { 3233 uintptr_t tmp; 3234 3235 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (uintptr_t), 3236 mstate, vstate)) { 3237 regs[rd] = 0; 3238 break; 3239 } 3240 3241 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3242 regs[rd] = _RW_READ_HELD(&r.ri, tmp); 3243 break; 3244 } 3245 3246 case DIF_SUBR_RW_WRITE_HELD: 3247 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3248 mstate, vstate)) { 3249 regs[rd] = 0; 3250 break; 3251 } 3252 3253 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3254 regs[rd] = _RW_WRITE_HELD(&r.ri); 3255 break; 3256 3257 case DIF_SUBR_RW_ISWRITER: 3258 if (!dtrace_canload(tupregs[0].dttk_value, sizeof (krwlock_t), 3259 mstate, vstate)) { 3260 regs[rd] = 0; 3261 break; 3262 } 3263 3264 r.rw = dtrace_loadptr(tupregs[0].dttk_value); 3265 regs[rd] = _RW_ISWRITER(&r.ri); 3266 break; 3267 3268#else 3269 /* 3270 * XXX - The following code works because mutex, rwlocks, & sxlocks 3271 * all have similar data structures in FreeBSD. This may not be 3272 * good if someone changes one of the lock data structures. 3273 * Ideally, it would be nice if all these shared a common lock 3274 * object. 3275 */ 3276 case DIF_SUBR_MUTEX_OWNED: 3277 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3278 m.mx = tupregs[0].dttk_value; 3279 3280#ifdef DOODAD 3281 if (LO_CLASSINDEX(&(m.mi->lock_object)) < 2) { 3282 regs[rd] = !(m.mi->mtx_lock & MTX_UNOWNED); 3283 } else { 3284 regs[rd] = !(m.mi->mtx_lock & SX_UNLOCKED); 3285 } 3286#endif 3287 break; 3288 3289 case DIF_SUBR_MUTEX_OWNER: 3290 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3291 m.mx = tupregs[0].dttk_value; 3292 3293 if (LO_CLASSINDEX(&(m.mi->lock_object)) < 2) { 3294 regs[rd] = m.mi->mtx_lock & ~MTX_FLAGMASK; 3295 } else { 3296 if (!(m.mi->mtx_lock & SX_LOCK_SHARED)) 3297 regs[rd] = SX_OWNER(m.mi->mtx_lock); 3298 else 3299 regs[rd] = 0; 3300 } 3301 break; 3302 3303 case DIF_SUBR_MUTEX_TYPE_ADAPTIVE: 3304 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3305 m.mx = tupregs[0].dttk_value; 3306 3307 regs[rd] = (LO_CLASSINDEX(&(m.mi->lock_object)) != 0); 3308 break; 3309 3310 case DIF_SUBR_MUTEX_TYPE_SPIN: 3311 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3312 m.mx = tupregs[0].dttk_value; 3313 3314 regs[rd] = (LO_CLASSINDEX(&(m.mi->lock_object)) == 0); 3315 break; 3316 3317 case DIF_SUBR_RW_READ_HELD: 3318 case DIF_SUBR_SX_SHARED_HELD: 3319 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3320 s.sx = tupregs[0].dttk_value; 3321 regs[rd] = ((s.si->sx_lock & SX_LOCK_SHARED) && 3322 (SX_OWNER(s.si->sx_lock) >> SX_SHARERS_SHIFT) != 0); 3323 break; 3324 3325 case DIF_SUBR_RW_WRITE_HELD: 3326 case DIF_SUBR_SX_EXCLUSIVE_HELD: 3327 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3328 s.sx = tupregs[0].dttk_value; 3329 regs[rd] = (SX_OWNER(s.si->sx_lock) == (uintptr_t) curthread); 3330 break; 3331 3332 case DIF_SUBR_RW_ISWRITER: 3333 case DIF_SUBR_SX_ISEXCLUSIVE: 3334 /* XXX - need to use dtrace_canload() and dtrace_loadptr() */ 3335 s.sx = tupregs[0].dttk_value; 3336 regs[rd] = ((s.si->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS) || 3337 !(s.si->sx_lock & SX_LOCK_SHARED)); 3338 break; 3339#endif /* ! defined(sun) */ 3340 3341 case DIF_SUBR_BCOPY: { 3342 /* 3343 * We need to be sure that the destination is in the scratch 3344 * region -- no other region is allowed. 3345 */ 3346 uintptr_t src = tupregs[0].dttk_value; 3347 uintptr_t dest = tupregs[1].dttk_value; 3348 size_t size = tupregs[2].dttk_value; 3349 3350 if (!dtrace_inscratch(dest, size, mstate)) { 3351 *flags |= CPU_DTRACE_BADADDR; 3352 *illval = regs[rd]; 3353 break; 3354 } 3355 3356 if (!dtrace_canload(src, size, mstate, vstate)) { 3357 regs[rd] = 0; 3358 break; 3359 } 3360 3361 dtrace_bcopy((void *)src, (void *)dest, size); 3362 break; 3363 } 3364 3365 case DIF_SUBR_ALLOCA: 3366 case DIF_SUBR_COPYIN: { 3367 uintptr_t dest = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 3368 uint64_t size = 3369 tupregs[subr == DIF_SUBR_ALLOCA ? 0 : 1].dttk_value; 3370 size_t scratch_size = (dest - mstate->dtms_scratch_ptr) + size; 3371 3372 /* 3373 * This action doesn't require any credential checks since 3374 * probes will not activate in user contexts to which the 3375 * enabling user does not have permissions. 3376 */ 3377 3378 /* 3379 * Rounding up the user allocation size could have overflowed 3380 * a large, bogus allocation (like -1ULL) to 0. 3381 */ 3382 if (scratch_size < size || 3383 !DTRACE_INSCRATCH(mstate, scratch_size)) { 3384 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3385 regs[rd] = 0; 3386 break; 3387 } 3388 3389 if (subr == DIF_SUBR_COPYIN) { 3390 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3391 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3392 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3393 } 3394 3395 mstate->dtms_scratch_ptr += scratch_size; 3396 regs[rd] = dest; 3397 break; 3398 } 3399 3400 case DIF_SUBR_COPYINTO: { 3401 uint64_t size = tupregs[1].dttk_value; 3402 uintptr_t dest = tupregs[2].dttk_value; 3403 3404 /* 3405 * This action doesn't require any credential checks since 3406 * probes will not activate in user contexts to which the 3407 * enabling user does not have permissions. 3408 */ 3409 if (!dtrace_inscratch(dest, size, mstate)) { 3410 *flags |= CPU_DTRACE_BADADDR; 3411 *illval = regs[rd]; 3412 break; 3413 } 3414 3415 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3416 dtrace_copyin(tupregs[0].dttk_value, dest, size, flags); 3417 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3418 break; 3419 } 3420 3421 case DIF_SUBR_COPYINSTR: { 3422 uintptr_t dest = mstate->dtms_scratch_ptr; 3423 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3424 3425 if (nargs > 1 && tupregs[1].dttk_value < size) 3426 size = tupregs[1].dttk_value + 1; 3427 3428 /* 3429 * This action doesn't require any credential checks since 3430 * probes will not activate in user contexts to which the 3431 * enabling user does not have permissions. 3432 */ 3433 if (!DTRACE_INSCRATCH(mstate, size)) { 3434 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3435 regs[rd] = 0; 3436 break; 3437 } 3438 3439 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3440 dtrace_copyinstr(tupregs[0].dttk_value, dest, size, flags); 3441 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3442 3443 ((char *)dest)[size - 1] = '\0'; 3444 mstate->dtms_scratch_ptr += size; 3445 regs[rd] = dest; 3446 break; 3447 } 3448 3449#if defined(sun) 3450 case DIF_SUBR_MSGSIZE: 3451 case DIF_SUBR_MSGDSIZE: { 3452 uintptr_t baddr = tupregs[0].dttk_value, daddr; 3453 uintptr_t wptr, rptr; 3454 size_t count = 0; 3455 int cont = 0; 3456 3457 while (baddr != 0 && !(*flags & CPU_DTRACE_FAULT)) { 3458 3459 if (!dtrace_canload(baddr, sizeof (mblk_t), mstate, 3460 vstate)) { 3461 regs[rd] = 0; 3462 break; 3463 } 3464 3465 wptr = dtrace_loadptr(baddr + 3466 offsetof(mblk_t, b_wptr)); 3467 3468 rptr = dtrace_loadptr(baddr + 3469 offsetof(mblk_t, b_rptr)); 3470 3471 if (wptr < rptr) { 3472 *flags |= CPU_DTRACE_BADADDR; 3473 *illval = tupregs[0].dttk_value; 3474 break; 3475 } 3476 3477 daddr = dtrace_loadptr(baddr + 3478 offsetof(mblk_t, b_datap)); 3479 3480 baddr = dtrace_loadptr(baddr + 3481 offsetof(mblk_t, b_cont)); 3482 3483 /* 3484 * We want to prevent against denial-of-service here, 3485 * so we're only going to search the list for 3486 * dtrace_msgdsize_max mblks. 3487 */ 3488 if (cont++ > dtrace_msgdsize_max) { 3489 *flags |= CPU_DTRACE_ILLOP; 3490 break; 3491 } 3492 3493 if (subr == DIF_SUBR_MSGDSIZE) { 3494 if (dtrace_load8(daddr + 3495 offsetof(dblk_t, db_type)) != M_DATA) 3496 continue; 3497 } 3498 3499 count += wptr - rptr; 3500 } 3501 3502 if (!(*flags & CPU_DTRACE_FAULT)) 3503 regs[rd] = count; 3504 3505 break; 3506 } 3507#endif 3508 3509 case DIF_SUBR_PROGENYOF: { 3510 pid_t pid = tupregs[0].dttk_value; 3511 proc_t *p; 3512 int rval = 0; 3513 3514 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3515 3516 for (p = curthread->t_procp; p != NULL; p = p->p_parent) { 3517#if defined(sun) 3518 if (p->p_pidp->pid_id == pid) { 3519#else 3520 if (p->p_pid == pid) { 3521#endif 3522 rval = 1; 3523 break; 3524 } 3525 } 3526 3527 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3528 3529 regs[rd] = rval; 3530 break; 3531 } 3532 3533 case DIF_SUBR_SPECULATION: 3534 regs[rd] = dtrace_speculation(state); 3535 break; 3536 3537 case DIF_SUBR_COPYOUT: { 3538 uintptr_t kaddr = tupregs[0].dttk_value; 3539 uintptr_t uaddr = tupregs[1].dttk_value; 3540 uint64_t size = tupregs[2].dttk_value; 3541 3542 if (!dtrace_destructive_disallow && 3543 dtrace_priv_proc_control(state) && 3544 !dtrace_istoxic(kaddr, size)) { 3545 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3546 dtrace_copyout(kaddr, uaddr, size, flags); 3547 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3548 } 3549 break; 3550 } 3551 3552 case DIF_SUBR_COPYOUTSTR: { 3553 uintptr_t kaddr = tupregs[0].dttk_value; 3554 uintptr_t uaddr = tupregs[1].dttk_value; 3555 uint64_t size = tupregs[2].dttk_value; 3556 3557 if (!dtrace_destructive_disallow && 3558 dtrace_priv_proc_control(state) && 3559 !dtrace_istoxic(kaddr, size)) { 3560 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 3561 dtrace_copyoutstr(kaddr, uaddr, size, flags); 3562 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 3563 } 3564 break; 3565 } 3566 3567 case DIF_SUBR_STRLEN: { 3568 size_t sz; 3569 uintptr_t addr = (uintptr_t)tupregs[0].dttk_value; 3570 sz = dtrace_strlen((char *)addr, 3571 state->dts_options[DTRACEOPT_STRSIZE]); 3572 3573 if (!dtrace_canload(addr, sz + 1, mstate, vstate)) { 3574 regs[rd] = 0; 3575 break; 3576 } 3577 3578 regs[rd] = sz; 3579 3580 break; 3581 } 3582 3583 case DIF_SUBR_STRCHR: 3584 case DIF_SUBR_STRRCHR: { 3585 /* 3586 * We're going to iterate over the string looking for the 3587 * specified character. We will iterate until we have reached 3588 * the string length or we have found the character. If this 3589 * is DIF_SUBR_STRRCHR, we will look for the last occurrence 3590 * of the specified character instead of the first. 3591 */ 3592 uintptr_t saddr = tupregs[0].dttk_value; 3593 uintptr_t addr = tupregs[0].dttk_value; 3594 uintptr_t limit = addr + state->dts_options[DTRACEOPT_STRSIZE]; 3595 char c, target = (char)tupregs[1].dttk_value; 3596 3597 for (regs[rd] = 0; addr < limit; addr++) { 3598 if ((c = dtrace_load8(addr)) == target) { 3599 regs[rd] = addr; 3600 3601 if (subr == DIF_SUBR_STRCHR) 3602 break; 3603 } 3604 3605 if (c == '\0') 3606 break; 3607 } 3608 3609 if (!dtrace_canload(saddr, addr - saddr, mstate, vstate)) { 3610 regs[rd] = 0; 3611 break; 3612 } 3613 3614 break; 3615 } 3616 3617 case DIF_SUBR_STRSTR: 3618 case DIF_SUBR_INDEX: 3619 case DIF_SUBR_RINDEX: { 3620 /* 3621 * We're going to iterate over the string looking for the 3622 * specified string. We will iterate until we have reached 3623 * the string length or we have found the string. (Yes, this 3624 * is done in the most naive way possible -- but considering 3625 * that the string we're searching for is likely to be 3626 * relatively short, the complexity of Rabin-Karp or similar 3627 * hardly seems merited.) 3628 */ 3629 char *addr = (char *)(uintptr_t)tupregs[0].dttk_value; 3630 char *substr = (char *)(uintptr_t)tupregs[1].dttk_value; 3631 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3632 size_t len = dtrace_strlen(addr, size); 3633 size_t sublen = dtrace_strlen(substr, size); 3634 char *limit = addr + len, *orig = addr; 3635 int notfound = subr == DIF_SUBR_STRSTR ? 0 : -1; 3636 int inc = 1; 3637 3638 regs[rd] = notfound; 3639 3640 if (!dtrace_canload((uintptr_t)addr, len + 1, mstate, vstate)) { 3641 regs[rd] = 0; 3642 break; 3643 } 3644 3645 if (!dtrace_canload((uintptr_t)substr, sublen + 1, mstate, 3646 vstate)) { 3647 regs[rd] = 0; 3648 break; 3649 } 3650 3651 /* 3652 * strstr() and index()/rindex() have similar semantics if 3653 * both strings are the empty string: strstr() returns a 3654 * pointer to the (empty) string, and index() and rindex() 3655 * both return index 0 (regardless of any position argument). 3656 */ 3657 if (sublen == 0 && len == 0) { 3658 if (subr == DIF_SUBR_STRSTR) 3659 regs[rd] = (uintptr_t)addr; 3660 else 3661 regs[rd] = 0; 3662 break; 3663 } 3664 3665 if (subr != DIF_SUBR_STRSTR) { 3666 if (subr == DIF_SUBR_RINDEX) { 3667 limit = orig - 1; 3668 addr += len; 3669 inc = -1; 3670 } 3671 3672 /* 3673 * Both index() and rindex() take an optional position 3674 * argument that denotes the starting position. 3675 */ 3676 if (nargs == 3) { 3677 int64_t pos = (int64_t)tupregs[2].dttk_value; 3678 3679 /* 3680 * If the position argument to index() is 3681 * negative, Perl implicitly clamps it at 3682 * zero. This semantic is a little surprising 3683 * given the special meaning of negative 3684 * positions to similar Perl functions like 3685 * substr(), but it appears to reflect a 3686 * notion that index() can start from a 3687 * negative index and increment its way up to 3688 * the string. Given this notion, Perl's 3689 * rindex() is at least self-consistent in 3690 * that it implicitly clamps positions greater 3691 * than the string length to be the string 3692 * length. Where Perl completely loses 3693 * coherence, however, is when the specified 3694 * substring is the empty string (""). In 3695 * this case, even if the position is 3696 * negative, rindex() returns 0 -- and even if 3697 * the position is greater than the length, 3698 * index() returns the string length. These 3699 * semantics violate the notion that index() 3700 * should never return a value less than the 3701 * specified position and that rindex() should 3702 * never return a value greater than the 3703 * specified position. (One assumes that 3704 * these semantics are artifacts of Perl's 3705 * implementation and not the results of 3706 * deliberate design -- it beggars belief that 3707 * even Larry Wall could desire such oddness.) 3708 * While in the abstract one would wish for 3709 * consistent position semantics across 3710 * substr(), index() and rindex() -- or at the 3711 * very least self-consistent position 3712 * semantics for index() and rindex() -- we 3713 * instead opt to keep with the extant Perl 3714 * semantics, in all their broken glory. (Do 3715 * we have more desire to maintain Perl's 3716 * semantics than Perl does? Probably.) 3717 */ 3718 if (subr == DIF_SUBR_RINDEX) { 3719 if (pos < 0) { 3720 if (sublen == 0) 3721 regs[rd] = 0; 3722 break; 3723 } 3724 3725 if (pos > len) 3726 pos = len; 3727 } else { 3728 if (pos < 0) 3729 pos = 0; 3730 3731 if (pos >= len) { 3732 if (sublen == 0) 3733 regs[rd] = len; 3734 break; 3735 } 3736 } 3737 3738 addr = orig + pos; 3739 } 3740 } 3741 3742 for (regs[rd] = notfound; addr != limit; addr += inc) { 3743 if (dtrace_strncmp(addr, substr, sublen) == 0) { 3744 if (subr != DIF_SUBR_STRSTR) { 3745 /* 3746 * As D index() and rindex() are 3747 * modeled on Perl (and not on awk), 3748 * we return a zero-based (and not a 3749 * one-based) index. (For you Perl 3750 * weenies: no, we're not going to add 3751 * $[ -- and shouldn't you be at a con 3752 * or something?) 3753 */ 3754 regs[rd] = (uintptr_t)(addr - orig); 3755 break; 3756 } 3757 3758 ASSERT(subr == DIF_SUBR_STRSTR); 3759 regs[rd] = (uintptr_t)addr; 3760 break; 3761 } 3762 } 3763 3764 break; 3765 } 3766 3767 case DIF_SUBR_STRTOK: { 3768 uintptr_t addr = tupregs[0].dttk_value; 3769 uintptr_t tokaddr = tupregs[1].dttk_value; 3770 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3771 uintptr_t limit, toklimit = tokaddr + size; 3772 uint8_t c = 0, tokmap[32]; /* 256 / 8 */ 3773 char *dest = (char *)mstate->dtms_scratch_ptr; 3774 int i; 3775 3776 /* 3777 * Check both the token buffer and (later) the input buffer, 3778 * since both could be non-scratch addresses. 3779 */ 3780 if (!dtrace_strcanload(tokaddr, size, mstate, vstate)) { 3781 regs[rd] = 0; 3782 break; 3783 } 3784 3785 if (!DTRACE_INSCRATCH(mstate, size)) { 3786 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3787 regs[rd] = 0; 3788 break; 3789 } 3790 3791 if (addr == 0) { 3792 /* 3793 * If the address specified is NULL, we use our saved 3794 * strtok pointer from the mstate. Note that this 3795 * means that the saved strtok pointer is _only_ 3796 * valid within multiple enablings of the same probe -- 3797 * it behaves like an implicit clause-local variable. 3798 */ 3799 addr = mstate->dtms_strtok; 3800 } else { 3801 /* 3802 * If the user-specified address is non-NULL we must 3803 * access check it. This is the only time we have 3804 * a chance to do so, since this address may reside 3805 * in the string table of this clause-- future calls 3806 * (when we fetch addr from mstate->dtms_strtok) 3807 * would fail this access check. 3808 */ 3809 if (!dtrace_strcanload(addr, size, mstate, vstate)) { 3810 regs[rd] = 0; 3811 break; 3812 } 3813 } 3814 3815 /* 3816 * First, zero the token map, and then process the token 3817 * string -- setting a bit in the map for every character 3818 * found in the token string. 3819 */ 3820 for (i = 0; i < sizeof (tokmap); i++) 3821 tokmap[i] = 0; 3822 3823 for (; tokaddr < toklimit; tokaddr++) { 3824 if ((c = dtrace_load8(tokaddr)) == '\0') 3825 break; 3826 3827 ASSERT((c >> 3) < sizeof (tokmap)); 3828 tokmap[c >> 3] |= (1 << (c & 0x7)); 3829 } 3830 3831 for (limit = addr + size; addr < limit; addr++) { 3832 /* 3833 * We're looking for a character that is _not_ contained 3834 * in the token string. 3835 */ 3836 if ((c = dtrace_load8(addr)) == '\0') 3837 break; 3838 3839 if (!(tokmap[c >> 3] & (1 << (c & 0x7)))) 3840 break; 3841 } 3842 3843 if (c == '\0') { 3844 /* 3845 * We reached the end of the string without finding 3846 * any character that was not in the token string. 3847 * We return NULL in this case, and we set the saved 3848 * address to NULL as well. 3849 */ 3850 regs[rd] = 0; 3851 mstate->dtms_strtok = 0; 3852 break; 3853 } 3854 3855 /* 3856 * From here on, we're copying into the destination string. 3857 */ 3858 for (i = 0; addr < limit && i < size - 1; addr++) { 3859 if ((c = dtrace_load8(addr)) == '\0') 3860 break; 3861 3862 if (tokmap[c >> 3] & (1 << (c & 0x7))) 3863 break; 3864 3865 ASSERT(i < size); 3866 dest[i++] = c; 3867 } 3868 3869 ASSERT(i < size); 3870 dest[i] = '\0'; 3871 regs[rd] = (uintptr_t)dest; 3872 mstate->dtms_scratch_ptr += size; 3873 mstate->dtms_strtok = addr; 3874 break; 3875 } 3876 3877 case DIF_SUBR_SUBSTR: { 3878 uintptr_t s = tupregs[0].dttk_value; 3879 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3880 char *d = (char *)mstate->dtms_scratch_ptr; 3881 int64_t index = (int64_t)tupregs[1].dttk_value; 3882 int64_t remaining = (int64_t)tupregs[2].dttk_value; 3883 size_t len = dtrace_strlen((char *)s, size); 3884 int64_t i = 0; 3885 3886 if (!dtrace_canload(s, len + 1, mstate, vstate)) { 3887 regs[rd] = 0; 3888 break; 3889 } 3890 3891 if (!DTRACE_INSCRATCH(mstate, size)) { 3892 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3893 regs[rd] = 0; 3894 break; 3895 } 3896 3897 if (nargs <= 2) 3898 remaining = (int64_t)size; 3899 3900 if (index < 0) { 3901 index += len; 3902 3903 if (index < 0 && index + remaining > 0) { 3904 remaining += index; 3905 index = 0; 3906 } 3907 } 3908 3909 if (index >= len || index < 0) { 3910 remaining = 0; 3911 } else if (remaining < 0) { 3912 remaining += len - index; 3913 } else if (index + remaining > size) { 3914 remaining = size - index; 3915 } 3916 3917 for (i = 0; i < remaining; i++) { 3918 if ((d[i] = dtrace_load8(s + index + i)) == '\0') 3919 break; 3920 } 3921 3922 d[i] = '\0'; 3923 3924 mstate->dtms_scratch_ptr += size; 3925 regs[rd] = (uintptr_t)d; 3926 break; 3927 } 3928 3929#if defined(sun) 3930 case DIF_SUBR_GETMAJOR: 3931#ifdef _LP64 3932 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR64) & MAXMAJ64; 3933#else 3934 regs[rd] = (tupregs[0].dttk_value >> NBITSMINOR) & MAXMAJ; 3935#endif 3936 break; 3937 3938 case DIF_SUBR_GETMINOR: 3939#ifdef _LP64 3940 regs[rd] = tupregs[0].dttk_value & MAXMIN64; 3941#else 3942 regs[rd] = tupregs[0].dttk_value & MAXMIN; 3943#endif 3944 break; 3945 3946 case DIF_SUBR_DDI_PATHNAME: { 3947 /* 3948 * This one is a galactic mess. We are going to roughly 3949 * emulate ddi_pathname(), but it's made more complicated 3950 * by the fact that we (a) want to include the minor name and 3951 * (b) must proceed iteratively instead of recursively. 3952 */ 3953 uintptr_t dest = mstate->dtms_scratch_ptr; 3954 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 3955 char *start = (char *)dest, *end = start + size - 1; 3956 uintptr_t daddr = tupregs[0].dttk_value; 3957 int64_t minor = (int64_t)tupregs[1].dttk_value; 3958 char *s; 3959 int i, len, depth = 0; 3960 3961 /* 3962 * Due to all the pointer jumping we do and context we must 3963 * rely upon, we just mandate that the user must have kernel 3964 * read privileges to use this routine. 3965 */ 3966 if ((mstate->dtms_access & DTRACE_ACCESS_KERNEL) == 0) { 3967 *flags |= CPU_DTRACE_KPRIV; 3968 *illval = daddr; 3969 regs[rd] = 0; 3970 } 3971 3972 if (!DTRACE_INSCRATCH(mstate, size)) { 3973 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 3974 regs[rd] = 0; 3975 break; 3976 } 3977 3978 *end = '\0'; 3979 3980 /* 3981 * We want to have a name for the minor. In order to do this, 3982 * we need to walk the minor list from the devinfo. We want 3983 * to be sure that we don't infinitely walk a circular list, 3984 * so we check for circularity by sending a scout pointer 3985 * ahead two elements for every element that we iterate over; 3986 * if the list is circular, these will ultimately point to the 3987 * same element. You may recognize this little trick as the 3988 * answer to a stupid interview question -- one that always 3989 * seems to be asked by those who had to have it laboriously 3990 * explained to them, and who can't even concisely describe 3991 * the conditions under which one would be forced to resort to 3992 * this technique. Needless to say, those conditions are 3993 * found here -- and probably only here. Is this the only use 3994 * of this infamous trick in shipping, production code? If it 3995 * isn't, it probably should be... 3996 */ 3997 if (minor != -1) { 3998 uintptr_t maddr = dtrace_loadptr(daddr + 3999 offsetof(struct dev_info, devi_minor)); 4000 4001 uintptr_t next = offsetof(struct ddi_minor_data, next); 4002 uintptr_t name = offsetof(struct ddi_minor_data, 4003 d_minor) + offsetof(struct ddi_minor, name); 4004 uintptr_t dev = offsetof(struct ddi_minor_data, 4005 d_minor) + offsetof(struct ddi_minor, dev); 4006 uintptr_t scout; 4007 4008 if (maddr != NULL) 4009 scout = dtrace_loadptr(maddr + next); 4010 4011 while (maddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4012 uint64_t m; 4013#ifdef _LP64 4014 m = dtrace_load64(maddr + dev) & MAXMIN64; 4015#else 4016 m = dtrace_load32(maddr + dev) & MAXMIN; 4017#endif 4018 if (m != minor) { 4019 maddr = dtrace_loadptr(maddr + next); 4020 4021 if (scout == NULL) 4022 continue; 4023 4024 scout = dtrace_loadptr(scout + next); 4025 4026 if (scout == NULL) 4027 continue; 4028 4029 scout = dtrace_loadptr(scout + next); 4030 4031 if (scout == NULL) 4032 continue; 4033 4034 if (scout == maddr) { 4035 *flags |= CPU_DTRACE_ILLOP; 4036 break; 4037 } 4038 4039 continue; 4040 } 4041 4042 /* 4043 * We have the minor data. Now we need to 4044 * copy the minor's name into the end of the 4045 * pathname. 4046 */ 4047 s = (char *)dtrace_loadptr(maddr + name); 4048 len = dtrace_strlen(s, size); 4049 4050 if (*flags & CPU_DTRACE_FAULT) 4051 break; 4052 4053 if (len != 0) { 4054 if ((end -= (len + 1)) < start) 4055 break; 4056 4057 *end = ':'; 4058 } 4059 4060 for (i = 1; i <= len; i++) 4061 end[i] = dtrace_load8((uintptr_t)s++); 4062 break; 4063 } 4064 } 4065 4066 while (daddr != NULL && !(*flags & CPU_DTRACE_FAULT)) { 4067 ddi_node_state_t devi_state; 4068 4069 devi_state = dtrace_load32(daddr + 4070 offsetof(struct dev_info, devi_node_state)); 4071 4072 if (*flags & CPU_DTRACE_FAULT) 4073 break; 4074 4075 if (devi_state >= DS_INITIALIZED) { 4076 s = (char *)dtrace_loadptr(daddr + 4077 offsetof(struct dev_info, devi_addr)); 4078 len = dtrace_strlen(s, size); 4079 4080 if (*flags & CPU_DTRACE_FAULT) 4081 break; 4082 4083 if (len != 0) { 4084 if ((end -= (len + 1)) < start) 4085 break; 4086 4087 *end = '@'; 4088 } 4089 4090 for (i = 1; i <= len; i++) 4091 end[i] = dtrace_load8((uintptr_t)s++); 4092 } 4093 4094 /* 4095 * Now for the node name... 4096 */ 4097 s = (char *)dtrace_loadptr(daddr + 4098 offsetof(struct dev_info, devi_node_name)); 4099 4100 daddr = dtrace_loadptr(daddr + 4101 offsetof(struct dev_info, devi_parent)); 4102 4103 /* 4104 * If our parent is NULL (that is, if we're the root 4105 * node), we're going to use the special path 4106 * "devices". 4107 */ 4108 if (daddr == 0) 4109 s = "devices"; 4110 4111 len = dtrace_strlen(s, size); 4112 if (*flags & CPU_DTRACE_FAULT) 4113 break; 4114 4115 if ((end -= (len + 1)) < start) 4116 break; 4117 4118 for (i = 1; i <= len; i++) 4119 end[i] = dtrace_load8((uintptr_t)s++); 4120 *end = '/'; 4121 4122 if (depth++ > dtrace_devdepth_max) { 4123 *flags |= CPU_DTRACE_ILLOP; 4124 break; 4125 } 4126 } 4127 4128 if (end < start) 4129 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4130 4131 if (daddr == 0) { 4132 regs[rd] = (uintptr_t)end; 4133 mstate->dtms_scratch_ptr += size; 4134 } 4135 4136 break; 4137 } 4138#endif 4139 4140 case DIF_SUBR_STRJOIN: { 4141 char *d = (char *)mstate->dtms_scratch_ptr; 4142 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4143 uintptr_t s1 = tupregs[0].dttk_value; 4144 uintptr_t s2 = tupregs[1].dttk_value; 4145 int i = 0; 4146 4147 if (!dtrace_strcanload(s1, size, mstate, vstate) || 4148 !dtrace_strcanload(s2, size, mstate, vstate)) { 4149 regs[rd] = 0; 4150 break; 4151 } 4152 4153 if (!DTRACE_INSCRATCH(mstate, size)) { 4154 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4155 regs[rd] = 0; 4156 break; 4157 } 4158 4159 for (;;) { 4160 if (i >= size) { 4161 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4162 regs[rd] = 0; 4163 break; 4164 } 4165 4166 if ((d[i++] = dtrace_load8(s1++)) == '\0') { 4167 i--; 4168 break; 4169 } 4170 } 4171 4172 for (;;) { 4173 if (i >= size) { 4174 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4175 regs[rd] = 0; 4176 break; 4177 } 4178 4179 if ((d[i++] = dtrace_load8(s2++)) == '\0') 4180 break; 4181 } 4182 4183 if (i < size) { 4184 mstate->dtms_scratch_ptr += i; 4185 regs[rd] = (uintptr_t)d; 4186 } 4187 4188 break; 4189 } 4190 4191 case DIF_SUBR_LLTOSTR: { 4192 int64_t i = (int64_t)tupregs[0].dttk_value; 4193 int64_t val = i < 0 ? i * -1 : i; 4194 uint64_t size = 22; /* enough room for 2^64 in decimal */ 4195 char *end = (char *)mstate->dtms_scratch_ptr + size - 1; 4196 4197 if (!DTRACE_INSCRATCH(mstate, size)) { 4198 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4199 regs[rd] = 0; 4200 break; 4201 } 4202 4203 for (*end-- = '\0'; val; val /= 10) 4204 *end-- = '0' + (val % 10); 4205 4206 if (i == 0) 4207 *end-- = '0'; 4208 4209 if (i < 0) 4210 *end-- = '-'; 4211 4212 regs[rd] = (uintptr_t)end + 1; 4213 mstate->dtms_scratch_ptr += size; 4214 break; 4215 } 4216 4217 case DIF_SUBR_HTONS: 4218 case DIF_SUBR_NTOHS: 4219#if BYTE_ORDER == BIG_ENDIAN 4220 regs[rd] = (uint16_t)tupregs[0].dttk_value; 4221#else 4222 regs[rd] = DT_BSWAP_16((uint16_t)tupregs[0].dttk_value); 4223#endif 4224 break; 4225 4226 4227 case DIF_SUBR_HTONL: 4228 case DIF_SUBR_NTOHL: 4229#if BYTE_ORDER == BIG_ENDIAN 4230 regs[rd] = (uint32_t)tupregs[0].dttk_value; 4231#else 4232 regs[rd] = DT_BSWAP_32((uint32_t)tupregs[0].dttk_value); 4233#endif 4234 break; 4235 4236 4237 case DIF_SUBR_HTONLL: 4238 case DIF_SUBR_NTOHLL: 4239#if BYTE_ORDER == BIG_ENDIAN 4240 regs[rd] = (uint64_t)tupregs[0].dttk_value; 4241#else 4242 regs[rd] = DT_BSWAP_64((uint64_t)tupregs[0].dttk_value); 4243#endif 4244 break; 4245 4246 4247 case DIF_SUBR_DIRNAME: 4248 case DIF_SUBR_BASENAME: { 4249 char *dest = (char *)mstate->dtms_scratch_ptr; 4250 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4251 uintptr_t src = tupregs[0].dttk_value; 4252 int i, j, len = dtrace_strlen((char *)src, size); 4253 int lastbase = -1, firstbase = -1, lastdir = -1; 4254 int start, end; 4255 4256 if (!dtrace_canload(src, len + 1, mstate, vstate)) { 4257 regs[rd] = 0; 4258 break; 4259 } 4260 4261 if (!DTRACE_INSCRATCH(mstate, size)) { 4262 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4263 regs[rd] = 0; 4264 break; 4265 } 4266 4267 /* 4268 * The basename and dirname for a zero-length string is 4269 * defined to be "." 4270 */ 4271 if (len == 0) { 4272 len = 1; 4273 src = (uintptr_t)"."; 4274 } 4275 4276 /* 4277 * Start from the back of the string, moving back toward the 4278 * front until we see a character that isn't a slash. That 4279 * character is the last character in the basename. 4280 */ 4281 for (i = len - 1; i >= 0; i--) { 4282 if (dtrace_load8(src + i) != '/') 4283 break; 4284 } 4285 4286 if (i >= 0) 4287 lastbase = i; 4288 4289 /* 4290 * Starting from the last character in the basename, move 4291 * towards the front until we find a slash. The character 4292 * that we processed immediately before that is the first 4293 * character in the basename. 4294 */ 4295 for (; i >= 0; i--) { 4296 if (dtrace_load8(src + i) == '/') 4297 break; 4298 } 4299 4300 if (i >= 0) 4301 firstbase = i + 1; 4302 4303 /* 4304 * Now keep going until we find a non-slash character. That 4305 * character is the last character in the dirname. 4306 */ 4307 for (; i >= 0; i--) { 4308 if (dtrace_load8(src + i) != '/') 4309 break; 4310 } 4311 4312 if (i >= 0) 4313 lastdir = i; 4314 4315 ASSERT(!(lastbase == -1 && firstbase != -1)); 4316 ASSERT(!(firstbase == -1 && lastdir != -1)); 4317 4318 if (lastbase == -1) { 4319 /* 4320 * We didn't find a non-slash character. We know that 4321 * the length is non-zero, so the whole string must be 4322 * slashes. In either the dirname or the basename 4323 * case, we return '/'. 4324 */ 4325 ASSERT(firstbase == -1); 4326 firstbase = lastbase = lastdir = 0; 4327 } 4328 4329 if (firstbase == -1) { 4330 /* 4331 * The entire string consists only of a basename 4332 * component. If we're looking for dirname, we need 4333 * to change our string to be just "."; if we're 4334 * looking for a basename, we'll just set the first 4335 * character of the basename to be 0. 4336 */ 4337 if (subr == DIF_SUBR_DIRNAME) { 4338 ASSERT(lastdir == -1); 4339 src = (uintptr_t)"."; 4340 lastdir = 0; 4341 } else { 4342 firstbase = 0; 4343 } 4344 } 4345 4346 if (subr == DIF_SUBR_DIRNAME) { 4347 if (lastdir == -1) { 4348 /* 4349 * We know that we have a slash in the name -- 4350 * or lastdir would be set to 0, above. And 4351 * because lastdir is -1, we know that this 4352 * slash must be the first character. (That 4353 * is, the full string must be of the form 4354 * "/basename".) In this case, the last 4355 * character of the directory name is 0. 4356 */ 4357 lastdir = 0; 4358 } 4359 4360 start = 0; 4361 end = lastdir; 4362 } else { 4363 ASSERT(subr == DIF_SUBR_BASENAME); 4364 ASSERT(firstbase != -1 && lastbase != -1); 4365 start = firstbase; 4366 end = lastbase; 4367 } 4368 4369 for (i = start, j = 0; i <= end && j < size - 1; i++, j++) 4370 dest[j] = dtrace_load8(src + i); 4371 4372 dest[j] = '\0'; 4373 regs[rd] = (uintptr_t)dest; 4374 mstate->dtms_scratch_ptr += size; 4375 break; 4376 } 4377 4378 case DIF_SUBR_CLEANPATH: { 4379 char *dest = (char *)mstate->dtms_scratch_ptr, c; 4380 uint64_t size = state->dts_options[DTRACEOPT_STRSIZE]; 4381 uintptr_t src = tupregs[0].dttk_value; 4382 int i = 0, j = 0; 4383 4384 if (!dtrace_strcanload(src, size, mstate, vstate)) { 4385 regs[rd] = 0; 4386 break; 4387 } 4388 4389 if (!DTRACE_INSCRATCH(mstate, size)) { 4390 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4391 regs[rd] = 0; 4392 break; 4393 } 4394 4395 /* 4396 * Move forward, loading each character. 4397 */ 4398 do { 4399 c = dtrace_load8(src + i++); 4400next: 4401 if (j + 5 >= size) /* 5 = strlen("/..c\0") */ 4402 break; 4403 4404 if (c != '/') { 4405 dest[j++] = c; 4406 continue; 4407 } 4408 4409 c = dtrace_load8(src + i++); 4410 4411 if (c == '/') { 4412 /* 4413 * We have two slashes -- we can just advance 4414 * to the next character. 4415 */ 4416 goto next; 4417 } 4418 4419 if (c != '.') { 4420 /* 4421 * This is not "." and it's not ".." -- we can 4422 * just store the "/" and this character and 4423 * drive on. 4424 */ 4425 dest[j++] = '/'; 4426 dest[j++] = c; 4427 continue; 4428 } 4429 4430 c = dtrace_load8(src + i++); 4431 4432 if (c == '/') { 4433 /* 4434 * This is a "/./" component. We're not going 4435 * to store anything in the destination buffer; 4436 * we're just going to go to the next component. 4437 */ 4438 goto next; 4439 } 4440 4441 if (c != '.') { 4442 /* 4443 * This is not ".." -- we can just store the 4444 * "/." and this character and continue 4445 * processing. 4446 */ 4447 dest[j++] = '/'; 4448 dest[j++] = '.'; 4449 dest[j++] = c; 4450 continue; 4451 } 4452 4453 c = dtrace_load8(src + i++); 4454 4455 if (c != '/' && c != '\0') { 4456 /* 4457 * This is not ".." -- it's "..[mumble]". 4458 * We'll store the "/.." and this character 4459 * and continue processing. 4460 */ 4461 dest[j++] = '/'; 4462 dest[j++] = '.'; 4463 dest[j++] = '.'; 4464 dest[j++] = c; 4465 continue; 4466 } 4467 4468 /* 4469 * This is "/../" or "/..\0". We need to back up 4470 * our destination pointer until we find a "/". 4471 */ 4472 i--; 4473 while (j != 0 && dest[--j] != '/') 4474 continue; 4475 4476 if (c == '\0') 4477 dest[++j] = '/'; 4478 } while (c != '\0'); 4479 4480 dest[j] = '\0'; 4481 regs[rd] = (uintptr_t)dest; 4482 mstate->dtms_scratch_ptr += size; 4483 break; 4484 } 4485 4486 case DIF_SUBR_INET_NTOA: 4487 case DIF_SUBR_INET_NTOA6: 4488 case DIF_SUBR_INET_NTOP: { 4489 size_t size; 4490 int af, argi, i; 4491 char *base, *end; 4492 4493 if (subr == DIF_SUBR_INET_NTOP) { 4494 af = (int)tupregs[0].dttk_value; 4495 argi = 1; 4496 } else { 4497 af = subr == DIF_SUBR_INET_NTOA ? AF_INET: AF_INET6; 4498 argi = 0; 4499 } 4500 4501 if (af == AF_INET) { 4502 ipaddr_t ip4; 4503 uint8_t *ptr8, val; 4504 4505 /* 4506 * Safely load the IPv4 address. 4507 */ 4508 ip4 = dtrace_load32(tupregs[argi].dttk_value); 4509 4510 /* 4511 * Check an IPv4 string will fit in scratch. 4512 */ 4513 size = INET_ADDRSTRLEN; 4514 if (!DTRACE_INSCRATCH(mstate, size)) { 4515 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4516 regs[rd] = 0; 4517 break; 4518 } 4519 base = (char *)mstate->dtms_scratch_ptr; 4520 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4521 4522 /* 4523 * Stringify as a dotted decimal quad. 4524 */ 4525 *end-- = '\0'; 4526 ptr8 = (uint8_t *)&ip4; 4527 for (i = 3; i >= 0; i--) { 4528 val = ptr8[i]; 4529 4530 if (val == 0) { 4531 *end-- = '0'; 4532 } else { 4533 for (; val; val /= 10) { 4534 *end-- = '0' + (val % 10); 4535 } 4536 } 4537 4538 if (i > 0) 4539 *end-- = '.'; 4540 } 4541 ASSERT(end + 1 >= base); 4542 4543 } else if (af == AF_INET6) { 4544 struct in6_addr ip6; 4545 int firstzero, tryzero, numzero, v6end; 4546 uint16_t val; 4547 const char digits[] = "0123456789abcdef"; 4548 4549 /* 4550 * Stringify using RFC 1884 convention 2 - 16 bit 4551 * hexadecimal values with a zero-run compression. 4552 * Lower case hexadecimal digits are used. 4553 * eg, fe80::214:4fff:fe0b:76c8. 4554 * The IPv4 embedded form is returned for inet_ntop, 4555 * just the IPv4 string is returned for inet_ntoa6. 4556 */ 4557 4558 /* 4559 * Safely load the IPv6 address. 4560 */ 4561 dtrace_bcopy( 4562 (void *)(uintptr_t)tupregs[argi].dttk_value, 4563 (void *)(uintptr_t)&ip6, sizeof (struct in6_addr)); 4564 4565 /* 4566 * Check an IPv6 string will fit in scratch. 4567 */ 4568 size = INET6_ADDRSTRLEN; 4569 if (!DTRACE_INSCRATCH(mstate, size)) { 4570 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 4571 regs[rd] = 0; 4572 break; 4573 } 4574 base = (char *)mstate->dtms_scratch_ptr; 4575 end = (char *)mstate->dtms_scratch_ptr + size - 1; 4576 *end-- = '\0'; 4577 4578 /* 4579 * Find the longest run of 16 bit zero values 4580 * for the single allowed zero compression - "::". 4581 */ 4582 firstzero = -1; 4583 tryzero = -1; 4584 numzero = 1; 4585 for (i = 0; i < sizeof (struct in6_addr); i++) { 4586#if defined(sun) 4587 if (ip6._S6_un._S6_u8[i] == 0 && 4588#else 4589 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4590#endif 4591 tryzero == -1 && i % 2 == 0) { 4592 tryzero = i; 4593 continue; 4594 } 4595 4596 if (tryzero != -1 && 4597#if defined(sun) 4598 (ip6._S6_un._S6_u8[i] != 0 || 4599#else 4600 (ip6.__u6_addr.__u6_addr8[i] != 0 || 4601#endif 4602 i == sizeof (struct in6_addr) - 1)) { 4603 4604 if (i - tryzero <= numzero) { 4605 tryzero = -1; 4606 continue; 4607 } 4608 4609 firstzero = tryzero; 4610 numzero = i - i % 2 - tryzero; 4611 tryzero = -1; 4612 4613#if defined(sun) 4614 if (ip6._S6_un._S6_u8[i] == 0 && 4615#else 4616 if (ip6.__u6_addr.__u6_addr8[i] == 0 && 4617#endif 4618 i == sizeof (struct in6_addr) - 1) 4619 numzero += 2; 4620 } 4621 } 4622 ASSERT(firstzero + numzero <= sizeof (struct in6_addr)); 4623 4624 /* 4625 * Check for an IPv4 embedded address. 4626 */ 4627 v6end = sizeof (struct in6_addr) - 2; 4628 if (IN6_IS_ADDR_V4MAPPED(&ip6) || 4629 IN6_IS_ADDR_V4COMPAT(&ip6)) { 4630 for (i = sizeof (struct in6_addr) - 1; 4631 i >= DTRACE_V4MAPPED_OFFSET; i--) { 4632 ASSERT(end >= base); 4633 4634#if defined(sun) 4635 val = ip6._S6_un._S6_u8[i]; 4636#else 4637 val = ip6.__u6_addr.__u6_addr8[i]; 4638#endif 4639 4640 if (val == 0) { 4641 *end-- = '0'; 4642 } else { 4643 for (; val; val /= 10) { 4644 *end-- = '0' + val % 10; 4645 } 4646 } 4647 4648 if (i > DTRACE_V4MAPPED_OFFSET) 4649 *end-- = '.'; 4650 } 4651 4652 if (subr == DIF_SUBR_INET_NTOA6) 4653 goto inetout; 4654 4655 /* 4656 * Set v6end to skip the IPv4 address that 4657 * we have already stringified. 4658 */ 4659 v6end = 10; 4660 } 4661 4662 /* 4663 * Build the IPv6 string by working through the 4664 * address in reverse. 4665 */ 4666 for (i = v6end; i >= 0; i -= 2) { 4667 ASSERT(end >= base); 4668 4669 if (i == firstzero + numzero - 2) { 4670 *end-- = ':'; 4671 *end-- = ':'; 4672 i -= numzero - 2; 4673 continue; 4674 } 4675 4676 if (i < 14 && i != firstzero - 2) 4677 *end-- = ':'; 4678 4679#if defined(sun) 4680 val = (ip6._S6_un._S6_u8[i] << 8) + 4681 ip6._S6_un._S6_u8[i + 1]; 4682#else 4683 val = (ip6.__u6_addr.__u6_addr8[i] << 8) + 4684 ip6.__u6_addr.__u6_addr8[i + 1]; 4685#endif 4686 4687 if (val == 0) { 4688 *end-- = '0'; 4689 } else { 4690 for (; val; val /= 16) { 4691 *end-- = digits[val % 16]; 4692 } 4693 } 4694 } 4695 ASSERT(end + 1 >= base); 4696 4697 } else { 4698 /* 4699 * The user didn't use AH_INET or AH_INET6. 4700 */ 4701 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 4702 regs[rd] = 0; 4703 break; 4704 } 4705 4706inetout: regs[rd] = (uintptr_t)end + 1; 4707 mstate->dtms_scratch_ptr += size; 4708 break; 4709 } 4710 4711 case DIF_SUBR_MEMREF: { 4712 uintptr_t size = 2 * sizeof(uintptr_t); 4713 uintptr_t *memref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4714 size_t scratch_size = ((uintptr_t) memref - mstate->dtms_scratch_ptr) + size; 4715 4716 /* address and length */ 4717 memref[0] = tupregs[0].dttk_value; 4718 memref[1] = tupregs[1].dttk_value; 4719 4720 regs[rd] = (uintptr_t) memref; 4721 mstate->dtms_scratch_ptr += scratch_size; 4722 break; 4723 } 4724 4725 case DIF_SUBR_TYPEREF: { 4726 uintptr_t size = 4 * sizeof(uintptr_t); 4727 uintptr_t *typeref = (uintptr_t *) P2ROUNDUP(mstate->dtms_scratch_ptr, sizeof(uintptr_t)); 4728 size_t scratch_size = ((uintptr_t) typeref - mstate->dtms_scratch_ptr) + size; 4729 4730 /* address, num_elements, type_str, type_len */ 4731 typeref[0] = tupregs[0].dttk_value; 4732 typeref[1] = tupregs[1].dttk_value; 4733 typeref[2] = tupregs[2].dttk_value; 4734 typeref[3] = tupregs[3].dttk_value; 4735 4736 regs[rd] = (uintptr_t) typeref; 4737 mstate->dtms_scratch_ptr += scratch_size; 4738 break; 4739 } 4740 } 4741} 4742 4743/* 4744 * Emulate the execution of DTrace IR instructions specified by the given 4745 * DIF object. This function is deliberately void of assertions as all of 4746 * the necessary checks are handled by a call to dtrace_difo_validate(). 4747 */ 4748static uint64_t 4749dtrace_dif_emulate(dtrace_difo_t *difo, dtrace_mstate_t *mstate, 4750 dtrace_vstate_t *vstate, dtrace_state_t *state) 4751{ 4752 const dif_instr_t *text = difo->dtdo_buf; 4753 const uint_t textlen = difo->dtdo_len; 4754 const char *strtab = difo->dtdo_strtab; 4755 const uint64_t *inttab = difo->dtdo_inttab; 4756 4757 uint64_t rval = 0; 4758 dtrace_statvar_t *svar; 4759 dtrace_dstate_t *dstate = &vstate->dtvs_dynvars; 4760 dtrace_difv_t *v; 4761 volatile uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 4762 volatile uintptr_t *illval = &cpu_core[curcpu].cpuc_dtrace_illval; 4763 4764 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 4765 uint64_t regs[DIF_DIR_NREGS]; 4766 uint64_t *tmp; 4767 4768 uint8_t cc_n = 0, cc_z = 0, cc_v = 0, cc_c = 0; 4769 int64_t cc_r; 4770 uint_t pc = 0, id, opc = 0; 4771 uint8_t ttop = 0; 4772 dif_instr_t instr; 4773 uint_t r1, r2, rd; 4774 4775 /* 4776 * We stash the current DIF object into the machine state: we need it 4777 * for subsequent access checking. 4778 */ 4779 mstate->dtms_difo = difo; 4780 4781 regs[DIF_REG_R0] = 0; /* %r0 is fixed at zero */ 4782 4783 while (pc < textlen && !(*flags & CPU_DTRACE_FAULT)) { 4784 opc = pc; 4785 4786 instr = text[pc++]; 4787 r1 = DIF_INSTR_R1(instr); 4788 r2 = DIF_INSTR_R2(instr); 4789 rd = DIF_INSTR_RD(instr); 4790 4791 switch (DIF_INSTR_OP(instr)) { 4792 case DIF_OP_OR: 4793 regs[rd] = regs[r1] | regs[r2]; 4794 break; 4795 case DIF_OP_XOR: 4796 regs[rd] = regs[r1] ^ regs[r2]; 4797 break; 4798 case DIF_OP_AND: 4799 regs[rd] = regs[r1] & regs[r2]; 4800 break; 4801 case DIF_OP_SLL: 4802 regs[rd] = regs[r1] << regs[r2]; 4803 break; 4804 case DIF_OP_SRL: 4805 regs[rd] = regs[r1] >> regs[r2]; 4806 break; 4807 case DIF_OP_SUB: 4808 regs[rd] = regs[r1] - regs[r2]; 4809 break; 4810 case DIF_OP_ADD: 4811 regs[rd] = regs[r1] + regs[r2]; 4812 break; 4813 case DIF_OP_MUL: 4814 regs[rd] = regs[r1] * regs[r2]; 4815 break; 4816 case DIF_OP_SDIV: 4817 if (regs[r2] == 0) { 4818 regs[rd] = 0; 4819 *flags |= CPU_DTRACE_DIVZERO; 4820 } else { 4821 regs[rd] = (int64_t)regs[r1] / 4822 (int64_t)regs[r2]; 4823 } 4824 break; 4825 4826 case DIF_OP_UDIV: 4827 if (regs[r2] == 0) { 4828 regs[rd] = 0; 4829 *flags |= CPU_DTRACE_DIVZERO; 4830 } else { 4831 regs[rd] = regs[r1] / regs[r2]; 4832 } 4833 break; 4834 4835 case DIF_OP_SREM: 4836 if (regs[r2] == 0) { 4837 regs[rd] = 0; 4838 *flags |= CPU_DTRACE_DIVZERO; 4839 } else { 4840 regs[rd] = (int64_t)regs[r1] % 4841 (int64_t)regs[r2]; 4842 } 4843 break; 4844 4845 case DIF_OP_UREM: 4846 if (regs[r2] == 0) { 4847 regs[rd] = 0; 4848 *flags |= CPU_DTRACE_DIVZERO; 4849 } else { 4850 regs[rd] = regs[r1] % regs[r2]; 4851 } 4852 break; 4853 4854 case DIF_OP_NOT: 4855 regs[rd] = ~regs[r1]; 4856 break; 4857 case DIF_OP_MOV: 4858 regs[rd] = regs[r1]; 4859 break; 4860 case DIF_OP_CMP: 4861 cc_r = regs[r1] - regs[r2]; 4862 cc_n = cc_r < 0; 4863 cc_z = cc_r == 0; 4864 cc_v = 0; 4865 cc_c = regs[r1] < regs[r2]; 4866 break; 4867 case DIF_OP_TST: 4868 cc_n = cc_v = cc_c = 0; 4869 cc_z = regs[r1] == 0; 4870 break; 4871 case DIF_OP_BA: 4872 pc = DIF_INSTR_LABEL(instr); 4873 break; 4874 case DIF_OP_BE: 4875 if (cc_z) 4876 pc = DIF_INSTR_LABEL(instr); 4877 break; 4878 case DIF_OP_BNE: 4879 if (cc_z == 0) 4880 pc = DIF_INSTR_LABEL(instr); 4881 break; 4882 case DIF_OP_BG: 4883 if ((cc_z | (cc_n ^ cc_v)) == 0) 4884 pc = DIF_INSTR_LABEL(instr); 4885 break; 4886 case DIF_OP_BGU: 4887 if ((cc_c | cc_z) == 0) 4888 pc = DIF_INSTR_LABEL(instr); 4889 break; 4890 case DIF_OP_BGE: 4891 if ((cc_n ^ cc_v) == 0) 4892 pc = DIF_INSTR_LABEL(instr); 4893 break; 4894 case DIF_OP_BGEU: 4895 if (cc_c == 0) 4896 pc = DIF_INSTR_LABEL(instr); 4897 break; 4898 case DIF_OP_BL: 4899 if (cc_n ^ cc_v) 4900 pc = DIF_INSTR_LABEL(instr); 4901 break; 4902 case DIF_OP_BLU: 4903 if (cc_c) 4904 pc = DIF_INSTR_LABEL(instr); 4905 break; 4906 case DIF_OP_BLE: 4907 if (cc_z | (cc_n ^ cc_v)) 4908 pc = DIF_INSTR_LABEL(instr); 4909 break; 4910 case DIF_OP_BLEU: 4911 if (cc_c | cc_z) 4912 pc = DIF_INSTR_LABEL(instr); 4913 break; 4914 case DIF_OP_RLDSB: 4915 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4916 *flags |= CPU_DTRACE_KPRIV; 4917 *illval = regs[r1]; 4918 break; 4919 } 4920 /*FALLTHROUGH*/ 4921 case DIF_OP_LDSB: 4922 regs[rd] = (int8_t)dtrace_load8(regs[r1]); 4923 break; 4924 case DIF_OP_RLDSH: 4925 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4926 *flags |= CPU_DTRACE_KPRIV; 4927 *illval = regs[r1]; 4928 break; 4929 } 4930 /*FALLTHROUGH*/ 4931 case DIF_OP_LDSH: 4932 regs[rd] = (int16_t)dtrace_load16(regs[r1]); 4933 break; 4934 case DIF_OP_RLDSW: 4935 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4936 *flags |= CPU_DTRACE_KPRIV; 4937 *illval = regs[r1]; 4938 break; 4939 } 4940 /*FALLTHROUGH*/ 4941 case DIF_OP_LDSW: 4942 regs[rd] = (int32_t)dtrace_load32(regs[r1]); 4943 break; 4944 case DIF_OP_RLDUB: 4945 if (!dtrace_canstore(regs[r1], 1, mstate, vstate)) { 4946 *flags |= CPU_DTRACE_KPRIV; 4947 *illval = regs[r1]; 4948 break; 4949 } 4950 /*FALLTHROUGH*/ 4951 case DIF_OP_LDUB: 4952 regs[rd] = dtrace_load8(regs[r1]); 4953 break; 4954 case DIF_OP_RLDUH: 4955 if (!dtrace_canstore(regs[r1], 2, mstate, vstate)) { 4956 *flags |= CPU_DTRACE_KPRIV; 4957 *illval = regs[r1]; 4958 break; 4959 } 4960 /*FALLTHROUGH*/ 4961 case DIF_OP_LDUH: 4962 regs[rd] = dtrace_load16(regs[r1]); 4963 break; 4964 case DIF_OP_RLDUW: 4965 if (!dtrace_canstore(regs[r1], 4, mstate, vstate)) { 4966 *flags |= CPU_DTRACE_KPRIV; 4967 *illval = regs[r1]; 4968 break; 4969 } 4970 /*FALLTHROUGH*/ 4971 case DIF_OP_LDUW: 4972 regs[rd] = dtrace_load32(regs[r1]); 4973 break; 4974 case DIF_OP_RLDX: 4975 if (!dtrace_canstore(regs[r1], 8, mstate, vstate)) { 4976 *flags |= CPU_DTRACE_KPRIV; 4977 *illval = regs[r1]; 4978 break; 4979 } 4980 /*FALLTHROUGH*/ 4981 case DIF_OP_LDX: 4982 regs[rd] = dtrace_load64(regs[r1]); 4983 break; 4984 case DIF_OP_ULDSB: 4985 regs[rd] = (int8_t) 4986 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4987 break; 4988 case DIF_OP_ULDSH: 4989 regs[rd] = (int16_t) 4990 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 4991 break; 4992 case DIF_OP_ULDSW: 4993 regs[rd] = (int32_t) 4994 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 4995 break; 4996 case DIF_OP_ULDUB: 4997 regs[rd] = 4998 dtrace_fuword8((void *)(uintptr_t)regs[r1]); 4999 break; 5000 case DIF_OP_ULDUH: 5001 regs[rd] = 5002 dtrace_fuword16((void *)(uintptr_t)regs[r1]); 5003 break; 5004 case DIF_OP_ULDUW: 5005 regs[rd] = 5006 dtrace_fuword32((void *)(uintptr_t)regs[r1]); 5007 break; 5008 case DIF_OP_ULDX: 5009 regs[rd] = 5010 dtrace_fuword64((void *)(uintptr_t)regs[r1]); 5011 break; 5012 case DIF_OP_RET: 5013 rval = regs[rd]; 5014 pc = textlen; 5015 break; 5016 case DIF_OP_NOP: 5017 break; 5018 case DIF_OP_SETX: 5019 regs[rd] = inttab[DIF_INSTR_INTEGER(instr)]; 5020 break; 5021 case DIF_OP_SETS: 5022 regs[rd] = (uint64_t)(uintptr_t) 5023 (strtab + DIF_INSTR_STRING(instr)); 5024 break; 5025 case DIF_OP_SCMP: { 5026 size_t sz = state->dts_options[DTRACEOPT_STRSIZE]; 5027 uintptr_t s1 = regs[r1]; 5028 uintptr_t s2 = regs[r2]; 5029 5030 if (s1 != 0 && 5031 !dtrace_strcanload(s1, sz, mstate, vstate)) 5032 break; 5033 if (s2 != 0 && 5034 !dtrace_strcanload(s2, sz, mstate, vstate)) 5035 break; 5036 5037 cc_r = dtrace_strncmp((char *)s1, (char *)s2, sz); 5038 5039 cc_n = cc_r < 0; 5040 cc_z = cc_r == 0; 5041 cc_v = cc_c = 0; 5042 break; 5043 } 5044 case DIF_OP_LDGA: 5045 regs[rd] = dtrace_dif_variable(mstate, state, 5046 r1, regs[r2]); 5047 break; 5048 case DIF_OP_LDGS: 5049 id = DIF_INSTR_VAR(instr); 5050 5051 if (id >= DIF_VAR_OTHER_UBASE) { 5052 uintptr_t a; 5053 5054 id -= DIF_VAR_OTHER_UBASE; 5055 svar = vstate->dtvs_globals[id]; 5056 ASSERT(svar != NULL); 5057 v = &svar->dtsv_var; 5058 5059 if (!(v->dtdv_type.dtdt_flags & DIF_TF_BYREF)) { 5060 regs[rd] = svar->dtsv_data; 5061 break; 5062 } 5063 5064 a = (uintptr_t)svar->dtsv_data; 5065 5066 if (*(uint8_t *)a == UINT8_MAX) { 5067 /* 5068 * If the 0th byte is set to UINT8_MAX 5069 * then this is to be treated as a 5070 * reference to a NULL variable. 5071 */ 5072 regs[rd] = 0; 5073 } else { 5074 regs[rd] = a + sizeof (uint64_t); 5075 } 5076 5077 break; 5078 } 5079 5080 regs[rd] = dtrace_dif_variable(mstate, state, id, 0); 5081 break; 5082 5083 case DIF_OP_STGS: 5084 id = DIF_INSTR_VAR(instr); 5085 5086 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5087 id -= DIF_VAR_OTHER_UBASE; 5088 5089 svar = vstate->dtvs_globals[id]; 5090 ASSERT(svar != NULL); 5091 v = &svar->dtsv_var; 5092 5093 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5094 uintptr_t a = (uintptr_t)svar->dtsv_data; 5095 5096 ASSERT(a != 0); 5097 ASSERT(svar->dtsv_size != 0); 5098 5099 if (regs[rd] == 0) { 5100 *(uint8_t *)a = UINT8_MAX; 5101 break; 5102 } else { 5103 *(uint8_t *)a = 0; 5104 a += sizeof (uint64_t); 5105 } 5106 if (!dtrace_vcanload( 5107 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5108 mstate, vstate)) 5109 break; 5110 5111 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5112 (void *)a, &v->dtdv_type); 5113 break; 5114 } 5115 5116 svar->dtsv_data = regs[rd]; 5117 break; 5118 5119 case DIF_OP_LDTA: 5120 /* 5121 * There are no DTrace built-in thread-local arrays at 5122 * present. This opcode is saved for future work. 5123 */ 5124 *flags |= CPU_DTRACE_ILLOP; 5125 regs[rd] = 0; 5126 break; 5127 5128 case DIF_OP_LDLS: 5129 id = DIF_INSTR_VAR(instr); 5130 5131 if (id < DIF_VAR_OTHER_UBASE) { 5132 /* 5133 * For now, this has no meaning. 5134 */ 5135 regs[rd] = 0; 5136 break; 5137 } 5138 5139 id -= DIF_VAR_OTHER_UBASE; 5140 5141 ASSERT(id < vstate->dtvs_nlocals); 5142 ASSERT(vstate->dtvs_locals != NULL); 5143 5144 svar = vstate->dtvs_locals[id]; 5145 ASSERT(svar != NULL); 5146 v = &svar->dtsv_var; 5147 5148 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5149 uintptr_t a = (uintptr_t)svar->dtsv_data; 5150 size_t sz = v->dtdv_type.dtdt_size; 5151 5152 sz += sizeof (uint64_t); 5153 ASSERT(svar->dtsv_size == NCPU * sz); 5154 a += curcpu * sz; 5155 5156 if (*(uint8_t *)a == UINT8_MAX) { 5157 /* 5158 * If the 0th byte is set to UINT8_MAX 5159 * then this is to be treated as a 5160 * reference to a NULL variable. 5161 */ 5162 regs[rd] = 0; 5163 } else { 5164 regs[rd] = a + sizeof (uint64_t); 5165 } 5166 5167 break; 5168 } 5169 5170 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5171 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5172 regs[rd] = tmp[curcpu]; 5173 break; 5174 5175 case DIF_OP_STLS: 5176 id = DIF_INSTR_VAR(instr); 5177 5178 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5179 id -= DIF_VAR_OTHER_UBASE; 5180 ASSERT(id < vstate->dtvs_nlocals); 5181 5182 ASSERT(vstate->dtvs_locals != NULL); 5183 svar = vstate->dtvs_locals[id]; 5184 ASSERT(svar != NULL); 5185 v = &svar->dtsv_var; 5186 5187 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5188 uintptr_t a = (uintptr_t)svar->dtsv_data; 5189 size_t sz = v->dtdv_type.dtdt_size; 5190 5191 sz += sizeof (uint64_t); 5192 ASSERT(svar->dtsv_size == NCPU * sz); 5193 a += curcpu * sz; 5194 5195 if (regs[rd] == 0) { 5196 *(uint8_t *)a = UINT8_MAX; 5197 break; 5198 } else { 5199 *(uint8_t *)a = 0; 5200 a += sizeof (uint64_t); 5201 } 5202 5203 if (!dtrace_vcanload( 5204 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5205 mstate, vstate)) 5206 break; 5207 5208 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5209 (void *)a, &v->dtdv_type); 5210 break; 5211 } 5212 5213 ASSERT(svar->dtsv_size == NCPU * sizeof (uint64_t)); 5214 tmp = (uint64_t *)(uintptr_t)svar->dtsv_data; 5215 tmp[curcpu] = regs[rd]; 5216 break; 5217 5218 case DIF_OP_LDTS: { 5219 dtrace_dynvar_t *dvar; 5220 dtrace_key_t *key; 5221 5222 id = DIF_INSTR_VAR(instr); 5223 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5224 id -= DIF_VAR_OTHER_UBASE; 5225 v = &vstate->dtvs_tlocals[id]; 5226 5227 key = &tupregs[DIF_DTR_NREGS]; 5228 key[0].dttk_value = (uint64_t)id; 5229 key[0].dttk_size = 0; 5230 DTRACE_TLS_THRKEY(key[1].dttk_value); 5231 key[1].dttk_size = 0; 5232 5233 dvar = dtrace_dynvar(dstate, 2, key, 5234 sizeof (uint64_t), DTRACE_DYNVAR_NOALLOC, 5235 mstate, vstate); 5236 5237 if (dvar == NULL) { 5238 regs[rd] = 0; 5239 break; 5240 } 5241 5242 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5243 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5244 } else { 5245 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5246 } 5247 5248 break; 5249 } 5250 5251 case DIF_OP_STTS: { 5252 dtrace_dynvar_t *dvar; 5253 dtrace_key_t *key; 5254 5255 id = DIF_INSTR_VAR(instr); 5256 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5257 id -= DIF_VAR_OTHER_UBASE; 5258 5259 key = &tupregs[DIF_DTR_NREGS]; 5260 key[0].dttk_value = (uint64_t)id; 5261 key[0].dttk_size = 0; 5262 DTRACE_TLS_THRKEY(key[1].dttk_value); 5263 key[1].dttk_size = 0; 5264 v = &vstate->dtvs_tlocals[id]; 5265 5266 dvar = dtrace_dynvar(dstate, 2, key, 5267 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5268 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5269 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5270 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5271 5272 /* 5273 * Given that we're storing to thread-local data, 5274 * we need to flush our predicate cache. 5275 */ 5276 curthread->t_predcache = 0; 5277 5278 if (dvar == NULL) 5279 break; 5280 5281 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5282 if (!dtrace_vcanload( 5283 (void *)(uintptr_t)regs[rd], 5284 &v->dtdv_type, mstate, vstate)) 5285 break; 5286 5287 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5288 dvar->dtdv_data, &v->dtdv_type); 5289 } else { 5290 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5291 } 5292 5293 break; 5294 } 5295 5296 case DIF_OP_SRA: 5297 regs[rd] = (int64_t)regs[r1] >> regs[r2]; 5298 break; 5299 5300 case DIF_OP_CALL: 5301 dtrace_dif_subr(DIF_INSTR_SUBR(instr), rd, 5302 regs, tupregs, ttop, mstate, state); 5303 break; 5304 5305 case DIF_OP_PUSHTR: 5306 if (ttop == DIF_DTR_NREGS) { 5307 *flags |= CPU_DTRACE_TUPOFLOW; 5308 break; 5309 } 5310 5311 if (r1 == DIF_TYPE_STRING) { 5312 /* 5313 * If this is a string type and the size is 0, 5314 * we'll use the system-wide default string 5315 * size. Note that we are _not_ looking at 5316 * the value of the DTRACEOPT_STRSIZE option; 5317 * had this been set, we would expect to have 5318 * a non-zero size value in the "pushtr". 5319 */ 5320 tupregs[ttop].dttk_size = 5321 dtrace_strlen((char *)(uintptr_t)regs[rd], 5322 regs[r2] ? regs[r2] : 5323 dtrace_strsize_default) + 1; 5324 } else { 5325 tupregs[ttop].dttk_size = regs[r2]; 5326 } 5327 5328 tupregs[ttop++].dttk_value = regs[rd]; 5329 break; 5330 5331 case DIF_OP_PUSHTV: 5332 if (ttop == DIF_DTR_NREGS) { 5333 *flags |= CPU_DTRACE_TUPOFLOW; 5334 break; 5335 } 5336 5337 tupregs[ttop].dttk_value = regs[rd]; 5338 tupregs[ttop++].dttk_size = 0; 5339 break; 5340 5341 case DIF_OP_POPTS: 5342 if (ttop != 0) 5343 ttop--; 5344 break; 5345 5346 case DIF_OP_FLUSHTS: 5347 ttop = 0; 5348 break; 5349 5350 case DIF_OP_LDGAA: 5351 case DIF_OP_LDTAA: { 5352 dtrace_dynvar_t *dvar; 5353 dtrace_key_t *key = tupregs; 5354 uint_t nkeys = ttop; 5355 5356 id = DIF_INSTR_VAR(instr); 5357 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5358 id -= DIF_VAR_OTHER_UBASE; 5359 5360 key[nkeys].dttk_value = (uint64_t)id; 5361 key[nkeys++].dttk_size = 0; 5362 5363 if (DIF_INSTR_OP(instr) == DIF_OP_LDTAA) { 5364 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5365 key[nkeys++].dttk_size = 0; 5366 v = &vstate->dtvs_tlocals[id]; 5367 } else { 5368 v = &vstate->dtvs_globals[id]->dtsv_var; 5369 } 5370 5371 dvar = dtrace_dynvar(dstate, nkeys, key, 5372 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5373 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5374 DTRACE_DYNVAR_NOALLOC, mstate, vstate); 5375 5376 if (dvar == NULL) { 5377 regs[rd] = 0; 5378 break; 5379 } 5380 5381 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5382 regs[rd] = (uint64_t)(uintptr_t)dvar->dtdv_data; 5383 } else { 5384 regs[rd] = *((uint64_t *)dvar->dtdv_data); 5385 } 5386 5387 break; 5388 } 5389 5390 case DIF_OP_STGAA: 5391 case DIF_OP_STTAA: { 5392 dtrace_dynvar_t *dvar; 5393 dtrace_key_t *key = tupregs; 5394 uint_t nkeys = ttop; 5395 5396 id = DIF_INSTR_VAR(instr); 5397 ASSERT(id >= DIF_VAR_OTHER_UBASE); 5398 id -= DIF_VAR_OTHER_UBASE; 5399 5400 key[nkeys].dttk_value = (uint64_t)id; 5401 key[nkeys++].dttk_size = 0; 5402 5403 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) { 5404 DTRACE_TLS_THRKEY(key[nkeys].dttk_value); 5405 key[nkeys++].dttk_size = 0; 5406 v = &vstate->dtvs_tlocals[id]; 5407 } else { 5408 v = &vstate->dtvs_globals[id]->dtsv_var; 5409 } 5410 5411 dvar = dtrace_dynvar(dstate, nkeys, key, 5412 v->dtdv_type.dtdt_size > sizeof (uint64_t) ? 5413 v->dtdv_type.dtdt_size : sizeof (uint64_t), 5414 regs[rd] ? DTRACE_DYNVAR_ALLOC : 5415 DTRACE_DYNVAR_DEALLOC, mstate, vstate); 5416 5417 if (dvar == NULL) 5418 break; 5419 5420 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) { 5421 if (!dtrace_vcanload( 5422 (void *)(uintptr_t)regs[rd], &v->dtdv_type, 5423 mstate, vstate)) 5424 break; 5425 5426 dtrace_vcopy((void *)(uintptr_t)regs[rd], 5427 dvar->dtdv_data, &v->dtdv_type); 5428 } else { 5429 *((uint64_t *)dvar->dtdv_data) = regs[rd]; 5430 } 5431 5432 break; 5433 } 5434 5435 case DIF_OP_ALLOCS: { 5436 uintptr_t ptr = P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5437 size_t size = ptr - mstate->dtms_scratch_ptr + regs[r1]; 5438 5439 /* 5440 * Rounding up the user allocation size could have 5441 * overflowed large, bogus allocations (like -1ULL) to 5442 * 0. 5443 */ 5444 if (size < regs[r1] || 5445 !DTRACE_INSCRATCH(mstate, size)) { 5446 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5447 regs[rd] = 0; 5448 break; 5449 } 5450 5451 dtrace_bzero((void *) mstate->dtms_scratch_ptr, size); 5452 mstate->dtms_scratch_ptr += size; 5453 regs[rd] = ptr; 5454 break; 5455 } 5456 5457 case DIF_OP_COPYS: 5458 if (!dtrace_canstore(regs[rd], regs[r2], 5459 mstate, vstate)) { 5460 *flags |= CPU_DTRACE_BADADDR; 5461 *illval = regs[rd]; 5462 break; 5463 } 5464 5465 if (!dtrace_canload(regs[r1], regs[r2], mstate, vstate)) 5466 break; 5467 5468 dtrace_bcopy((void *)(uintptr_t)regs[r1], 5469 (void *)(uintptr_t)regs[rd], (size_t)regs[r2]); 5470 break; 5471 5472 case DIF_OP_STB: 5473 if (!dtrace_canstore(regs[rd], 1, mstate, vstate)) { 5474 *flags |= CPU_DTRACE_BADADDR; 5475 *illval = regs[rd]; 5476 break; 5477 } 5478 *((uint8_t *)(uintptr_t)regs[rd]) = (uint8_t)regs[r1]; 5479 break; 5480 5481 case DIF_OP_STH: 5482 if (!dtrace_canstore(regs[rd], 2, mstate, vstate)) { 5483 *flags |= CPU_DTRACE_BADADDR; 5484 *illval = regs[rd]; 5485 break; 5486 } 5487 if (regs[rd] & 1) { 5488 *flags |= CPU_DTRACE_BADALIGN; 5489 *illval = regs[rd]; 5490 break; 5491 } 5492 *((uint16_t *)(uintptr_t)regs[rd]) = (uint16_t)regs[r1]; 5493 break; 5494 5495 case DIF_OP_STW: 5496 if (!dtrace_canstore(regs[rd], 4, mstate, vstate)) { 5497 *flags |= CPU_DTRACE_BADADDR; 5498 *illval = regs[rd]; 5499 break; 5500 } 5501 if (regs[rd] & 3) { 5502 *flags |= CPU_DTRACE_BADALIGN; 5503 *illval = regs[rd]; 5504 break; 5505 } 5506 *((uint32_t *)(uintptr_t)regs[rd]) = (uint32_t)regs[r1]; 5507 break; 5508 5509 case DIF_OP_STX: 5510 if (!dtrace_canstore(regs[rd], 8, mstate, vstate)) { 5511 *flags |= CPU_DTRACE_BADADDR; 5512 *illval = regs[rd]; 5513 break; 5514 } 5515 if (regs[rd] & 7) { 5516 *flags |= CPU_DTRACE_BADALIGN; 5517 *illval = regs[rd]; 5518 break; 5519 } 5520 *((uint64_t *)(uintptr_t)regs[rd]) = regs[r1]; 5521 break; 5522 } 5523 } 5524 5525 if (!(*flags & CPU_DTRACE_FAULT)) 5526 return (rval); 5527 5528 mstate->dtms_fltoffs = opc * sizeof (dif_instr_t); 5529 mstate->dtms_present |= DTRACE_MSTATE_FLTOFFS; 5530 5531 return (0); 5532} 5533 5534static void 5535dtrace_action_breakpoint(dtrace_ecb_t *ecb) 5536{ 5537 dtrace_probe_t *probe = ecb->dte_probe; 5538 dtrace_provider_t *prov = probe->dtpr_provider; 5539 char c[DTRACE_FULLNAMELEN + 80], *str; 5540 char *msg = "dtrace: breakpoint action at probe "; 5541 char *ecbmsg = " (ecb "; 5542 uintptr_t mask = (0xf << (sizeof (uintptr_t) * NBBY / 4)); 5543 uintptr_t val = (uintptr_t)ecb; 5544 int shift = (sizeof (uintptr_t) * NBBY) - 4, i = 0; 5545 5546 if (dtrace_destructive_disallow) 5547 return; 5548 5549 /* 5550 * It's impossible to be taking action on the NULL probe. 5551 */ 5552 ASSERT(probe != NULL); 5553 5554 /* 5555 * This is a poor man's (destitute man's?) sprintf(): we want to 5556 * print the provider name, module name, function name and name of 5557 * the probe, along with the hex address of the ECB with the breakpoint 5558 * action -- all of which we must place in the character buffer by 5559 * hand. 5560 */ 5561 while (*msg != '\0') 5562 c[i++] = *msg++; 5563 5564 for (str = prov->dtpv_name; *str != '\0'; str++) 5565 c[i++] = *str; 5566 c[i++] = ':'; 5567 5568 for (str = probe->dtpr_mod; *str != '\0'; str++) 5569 c[i++] = *str; 5570 c[i++] = ':'; 5571 5572 for (str = probe->dtpr_func; *str != '\0'; str++) 5573 c[i++] = *str; 5574 c[i++] = ':'; 5575 5576 for (str = probe->dtpr_name; *str != '\0'; str++) 5577 c[i++] = *str; 5578 5579 while (*ecbmsg != '\0') 5580 c[i++] = *ecbmsg++; 5581 5582 while (shift >= 0) { 5583 mask = (uintptr_t)0xf << shift; 5584 5585 if (val >= ((uintptr_t)1 << shift)) 5586 c[i++] = "0123456789abcdef"[(val & mask) >> shift]; 5587 shift -= 4; 5588 } 5589 5590 c[i++] = ')'; 5591 c[i] = '\0'; 5592 5593#if defined(sun) 5594 debug_enter(c); 5595#else 5596 kdb_enter(KDB_WHY_DTRACE, "breakpoint action"); 5597#endif 5598} 5599 5600static void 5601dtrace_action_panic(dtrace_ecb_t *ecb) 5602{ 5603 dtrace_probe_t *probe = ecb->dte_probe; 5604 5605 /* 5606 * It's impossible to be taking action on the NULL probe. 5607 */ 5608 ASSERT(probe != NULL); 5609 5610 if (dtrace_destructive_disallow) 5611 return; 5612 5613 if (dtrace_panicked != NULL) 5614 return; 5615 5616 if (dtrace_casptr(&dtrace_panicked, NULL, curthread) != NULL) 5617 return; 5618 5619 /* 5620 * We won the right to panic. (We want to be sure that only one 5621 * thread calls panic() from dtrace_probe(), and that panic() is 5622 * called exactly once.) 5623 */ 5624 dtrace_panic("dtrace: panic action at probe %s:%s:%s:%s (ecb %p)", 5625 probe->dtpr_provider->dtpv_name, probe->dtpr_mod, 5626 probe->dtpr_func, probe->dtpr_name, (void *)ecb); 5627} 5628 5629static void 5630dtrace_action_raise(uint64_t sig) 5631{ 5632 if (dtrace_destructive_disallow) 5633 return; 5634 5635 if (sig >= NSIG) { 5636 DTRACE_CPUFLAG_SET(CPU_DTRACE_ILLOP); 5637 return; 5638 } 5639 5640#if defined(sun) 5641 /* 5642 * raise() has a queue depth of 1 -- we ignore all subsequent 5643 * invocations of the raise() action. 5644 */ 5645 if (curthread->t_dtrace_sig == 0) 5646 curthread->t_dtrace_sig = (uint8_t)sig; 5647 5648 curthread->t_sig_check = 1; 5649 aston(curthread); 5650#else 5651 struct proc *p = curproc; 5652 PROC_LOCK(p); 5653 psignal(p, sig); 5654 PROC_UNLOCK(p); 5655#endif 5656} 5657 5658static void 5659dtrace_action_stop(void) 5660{ 5661 if (dtrace_destructive_disallow) 5662 return; 5663 5664#if defined(sun) 5665 if (!curthread->t_dtrace_stop) { 5666 curthread->t_dtrace_stop = 1; 5667 curthread->t_sig_check = 1; 5668 aston(curthread); 5669 } 5670#else 5671 struct proc *p = curproc; 5672 PROC_LOCK(p); 5673 psignal(p, SIGSTOP); 5674 PROC_UNLOCK(p); 5675#endif 5676} 5677 5678static void 5679dtrace_action_chill(dtrace_mstate_t *mstate, hrtime_t val) 5680{ 5681 hrtime_t now; 5682 volatile uint16_t *flags; 5683#if defined(sun) 5684 cpu_t *cpu = CPU; 5685#else 5686 cpu_t *cpu = &solaris_cpu[curcpu]; 5687#endif 5688 5689 if (dtrace_destructive_disallow) 5690 return; 5691 5692 flags = (volatile uint16_t *)&cpu_core[cpu->cpu_id].cpuc_dtrace_flags; 5693 5694 now = dtrace_gethrtime(); 5695 5696 if (now - cpu->cpu_dtrace_chillmark > dtrace_chill_interval) { 5697 /* 5698 * We need to advance the mark to the current time. 5699 */ 5700 cpu->cpu_dtrace_chillmark = now; 5701 cpu->cpu_dtrace_chilled = 0; 5702 } 5703 5704 /* 5705 * Now check to see if the requested chill time would take us over 5706 * the maximum amount of time allowed in the chill interval. (Or 5707 * worse, if the calculation itself induces overflow.) 5708 */ 5709 if (cpu->cpu_dtrace_chilled + val > dtrace_chill_max || 5710 cpu->cpu_dtrace_chilled + val < cpu->cpu_dtrace_chilled) { 5711 *flags |= CPU_DTRACE_ILLOP; 5712 return; 5713 } 5714 5715 while (dtrace_gethrtime() - now < val) 5716 continue; 5717 5718 /* 5719 * Normally, we assure that the value of the variable "timestamp" does 5720 * not change within an ECB. The presence of chill() represents an 5721 * exception to this rule, however. 5722 */ 5723 mstate->dtms_present &= ~DTRACE_MSTATE_TIMESTAMP; 5724 cpu->cpu_dtrace_chilled += val; 5725} 5726 5727#if defined(sun) 5728static void 5729dtrace_action_ustack(dtrace_mstate_t *mstate, dtrace_state_t *state, 5730 uint64_t *buf, uint64_t arg) 5731{ 5732 int nframes = DTRACE_USTACK_NFRAMES(arg); 5733 int strsize = DTRACE_USTACK_STRSIZE(arg); 5734 uint64_t *pcs = &buf[1], *fps; 5735 char *str = (char *)&pcs[nframes]; 5736 int size, offs = 0, i, j; 5737 uintptr_t old = mstate->dtms_scratch_ptr, saved; 5738 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 5739 char *sym; 5740 5741 /* 5742 * Should be taking a faster path if string space has not been 5743 * allocated. 5744 */ 5745 ASSERT(strsize != 0); 5746 5747 /* 5748 * We will first allocate some temporary space for the frame pointers. 5749 */ 5750 fps = (uint64_t *)P2ROUNDUP(mstate->dtms_scratch_ptr, 8); 5751 size = (uintptr_t)fps - mstate->dtms_scratch_ptr + 5752 (nframes * sizeof (uint64_t)); 5753 5754 if (!DTRACE_INSCRATCH(mstate, size)) { 5755 /* 5756 * Not enough room for our frame pointers -- need to indicate 5757 * that we ran out of scratch space. 5758 */ 5759 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOSCRATCH); 5760 return; 5761 } 5762 5763 mstate->dtms_scratch_ptr += size; 5764 saved = mstate->dtms_scratch_ptr; 5765 5766 /* 5767 * Now get a stack with both program counters and frame pointers. 5768 */ 5769 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5770 dtrace_getufpstack(buf, fps, nframes + 1); 5771 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5772 5773 /* 5774 * If that faulted, we're cooked. 5775 */ 5776 if (*flags & CPU_DTRACE_FAULT) 5777 goto out; 5778 5779 /* 5780 * Now we want to walk up the stack, calling the USTACK helper. For 5781 * each iteration, we restore the scratch pointer. 5782 */ 5783 for (i = 0; i < nframes; i++) { 5784 mstate->dtms_scratch_ptr = saved; 5785 5786 if (offs >= strsize) 5787 break; 5788 5789 sym = (char *)(uintptr_t)dtrace_helper( 5790 DTRACE_HELPER_ACTION_USTACK, 5791 mstate, state, pcs[i], fps[i]); 5792 5793 /* 5794 * If we faulted while running the helper, we're going to 5795 * clear the fault and null out the corresponding string. 5796 */ 5797 if (*flags & CPU_DTRACE_FAULT) { 5798 *flags &= ~CPU_DTRACE_FAULT; 5799 str[offs++] = '\0'; 5800 continue; 5801 } 5802 5803 if (sym == NULL) { 5804 str[offs++] = '\0'; 5805 continue; 5806 } 5807 5808 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 5809 5810 /* 5811 * Now copy in the string that the helper returned to us. 5812 */ 5813 for (j = 0; offs + j < strsize; j++) { 5814 if ((str[offs + j] = sym[j]) == '\0') 5815 break; 5816 } 5817 5818 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 5819 5820 offs += j + 1; 5821 } 5822 5823 if (offs >= strsize) { 5824 /* 5825 * If we didn't have room for all of the strings, we don't 5826 * abort processing -- this needn't be a fatal error -- but we 5827 * still want to increment a counter (dts_stkstroverflows) to 5828 * allow this condition to be warned about. (If this is from 5829 * a jstack() action, it is easily tuned via jstackstrsize.) 5830 */ 5831 dtrace_error(&state->dts_stkstroverflows); 5832 } 5833 5834 while (offs < strsize) 5835 str[offs++] = '\0'; 5836 5837out: 5838 mstate->dtms_scratch_ptr = old; 5839} 5840#endif 5841 5842/* 5843 * If you're looking for the epicenter of DTrace, you just found it. This 5844 * is the function called by the provider to fire a probe -- from which all 5845 * subsequent probe-context DTrace activity emanates. 5846 */ 5847void 5848dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, 5849 uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) 5850{ 5851 processorid_t cpuid; 5852 dtrace_icookie_t cookie; 5853 dtrace_probe_t *probe; 5854 dtrace_mstate_t mstate; 5855 dtrace_ecb_t *ecb; 5856 dtrace_action_t *act; 5857 intptr_t offs; 5858 size_t size; 5859 int vtime, onintr; 5860 volatile uint16_t *flags; 5861 hrtime_t now; 5862 5863#if defined(sun) 5864 /* 5865 * Kick out immediately if this CPU is still being born (in which case 5866 * curthread will be set to -1) or the current thread can't allow 5867 * probes in its current context. 5868 */ 5869 if (((uintptr_t)curthread & 1) || (curthread->t_flag & T_DONTDTRACE)) 5870 return; 5871#endif 5872 5873 cookie = dtrace_interrupt_disable(); 5874 probe = dtrace_probes[id - 1]; 5875 cpuid = curcpu; 5876 onintr = CPU_ON_INTR(CPU); 5877 5878 if (!onintr && probe->dtpr_predcache != DTRACE_CACHEIDNONE && 5879 probe->dtpr_predcache == curthread->t_predcache) { 5880 /* 5881 * We have hit in the predicate cache; we know that 5882 * this predicate would evaluate to be false. 5883 */ 5884 dtrace_interrupt_enable(cookie); 5885 return; 5886 } 5887 5888#if defined(sun) 5889 if (panic_quiesce) { 5890#else 5891 if (panicstr != NULL) { 5892#endif 5893 /* 5894 * We don't trace anything if we're panicking. 5895 */ 5896 dtrace_interrupt_enable(cookie); 5897 return; 5898 } 5899 5900 now = dtrace_gethrtime(); 5901 vtime = dtrace_vtime_references != 0; 5902 5903 if (vtime && curthread->t_dtrace_start) 5904 curthread->t_dtrace_vtime += now - curthread->t_dtrace_start; 5905 5906 mstate.dtms_difo = NULL; 5907 mstate.dtms_probe = probe; 5908 mstate.dtms_strtok = 0; 5909 mstate.dtms_arg[0] = arg0; 5910 mstate.dtms_arg[1] = arg1; 5911 mstate.dtms_arg[2] = arg2; 5912 mstate.dtms_arg[3] = arg3; 5913 mstate.dtms_arg[4] = arg4; 5914 5915 flags = (volatile uint16_t *)&cpu_core[cpuid].cpuc_dtrace_flags; 5916 5917 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 5918 dtrace_predicate_t *pred = ecb->dte_predicate; 5919 dtrace_state_t *state = ecb->dte_state; 5920 dtrace_buffer_t *buf = &state->dts_buffer[cpuid]; 5921 dtrace_buffer_t *aggbuf = &state->dts_aggbuffer[cpuid]; 5922 dtrace_vstate_t *vstate = &state->dts_vstate; 5923 dtrace_provider_t *prov = probe->dtpr_provider; 5924 int committed = 0; 5925 caddr_t tomax; 5926 5927 /* 5928 * A little subtlety with the following (seemingly innocuous) 5929 * declaration of the automatic 'val': by looking at the 5930 * code, you might think that it could be declared in the 5931 * action processing loop, below. (That is, it's only used in 5932 * the action processing loop.) However, it must be declared 5933 * out of that scope because in the case of DIF expression 5934 * arguments to aggregating actions, one iteration of the 5935 * action loop will use the last iteration's value. 5936 */ 5937 uint64_t val = 0; 5938 5939 mstate.dtms_present = DTRACE_MSTATE_ARGS | DTRACE_MSTATE_PROBE; 5940 *flags &= ~CPU_DTRACE_ERROR; 5941 5942 if (prov == dtrace_provider) { 5943 /* 5944 * If dtrace itself is the provider of this probe, 5945 * we're only going to continue processing the ECB if 5946 * arg0 (the dtrace_state_t) is equal to the ECB's 5947 * creating state. (This prevents disjoint consumers 5948 * from seeing one another's metaprobes.) 5949 */ 5950 if (arg0 != (uint64_t)(uintptr_t)state) 5951 continue; 5952 } 5953 5954 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) { 5955 /* 5956 * We're not currently active. If our provider isn't 5957 * the dtrace pseudo provider, we're not interested. 5958 */ 5959 if (prov != dtrace_provider) 5960 continue; 5961 5962 /* 5963 * Now we must further check if we are in the BEGIN 5964 * probe. If we are, we will only continue processing 5965 * if we're still in WARMUP -- if one BEGIN enabling 5966 * has invoked the exit() action, we don't want to 5967 * evaluate subsequent BEGIN enablings. 5968 */ 5969 if (probe->dtpr_id == dtrace_probeid_begin && 5970 state->dts_activity != DTRACE_ACTIVITY_WARMUP) { 5971 ASSERT(state->dts_activity == 5972 DTRACE_ACTIVITY_DRAINING); 5973 continue; 5974 } 5975 } 5976 5977 if (ecb->dte_cond) { 5978 /* 5979 * If the dte_cond bits indicate that this 5980 * consumer is only allowed to see user-mode firings 5981 * of this probe, call the provider's dtps_usermode() 5982 * entry point to check that the probe was fired 5983 * while in a user context. Skip this ECB if that's 5984 * not the case. 5985 */ 5986 if ((ecb->dte_cond & DTRACE_COND_USERMODE) && 5987 prov->dtpv_pops.dtps_usermode(prov->dtpv_arg, 5988 probe->dtpr_id, probe->dtpr_arg) == 0) 5989 continue; 5990 5991#if defined(sun) 5992 /* 5993 * This is more subtle than it looks. We have to be 5994 * absolutely certain that CRED() isn't going to 5995 * change out from under us so it's only legit to 5996 * examine that structure if we're in constrained 5997 * situations. Currently, the only times we'll this 5998 * check is if a non-super-user has enabled the 5999 * profile or syscall providers -- providers that 6000 * allow visibility of all processes. For the 6001 * profile case, the check above will ensure that 6002 * we're examining a user context. 6003 */ 6004 if (ecb->dte_cond & DTRACE_COND_OWNER) { 6005 cred_t *cr; 6006 cred_t *s_cr = 6007 ecb->dte_state->dts_cred.dcr_cred; 6008 proc_t *proc; 6009 6010 ASSERT(s_cr != NULL); 6011 6012 if ((cr = CRED()) == NULL || 6013 s_cr->cr_uid != cr->cr_uid || 6014 s_cr->cr_uid != cr->cr_ruid || 6015 s_cr->cr_uid != cr->cr_suid || 6016 s_cr->cr_gid != cr->cr_gid || 6017 s_cr->cr_gid != cr->cr_rgid || 6018 s_cr->cr_gid != cr->cr_sgid || 6019 (proc = ttoproc(curthread)) == NULL || 6020 (proc->p_flag & SNOCD)) 6021 continue; 6022 } 6023 6024 if (ecb->dte_cond & DTRACE_COND_ZONEOWNER) { 6025 cred_t *cr; 6026 cred_t *s_cr = 6027 ecb->dte_state->dts_cred.dcr_cred; 6028 6029 ASSERT(s_cr != NULL); 6030 6031 if ((cr = CRED()) == NULL || 6032 s_cr->cr_zone->zone_id != 6033 cr->cr_zone->zone_id) 6034 continue; 6035 } 6036#endif 6037 } 6038 6039 if (now - state->dts_alive > dtrace_deadman_timeout) { 6040 /* 6041 * We seem to be dead. Unless we (a) have kernel 6042 * destructive permissions (b) have expicitly enabled 6043 * destructive actions and (c) destructive actions have 6044 * not been disabled, we're going to transition into 6045 * the KILLED state, from which no further processing 6046 * on this state will be performed. 6047 */ 6048 if (!dtrace_priv_kernel_destructive(state) || 6049 !state->dts_cred.dcr_destructive || 6050 dtrace_destructive_disallow) { 6051 void *activity = &state->dts_activity; 6052 dtrace_activity_t current; 6053 6054 do { 6055 current = state->dts_activity; 6056 } while (dtrace_cas32(activity, current, 6057 DTRACE_ACTIVITY_KILLED) != current); 6058 6059 continue; 6060 } 6061 } 6062 6063 if ((offs = dtrace_buffer_reserve(buf, ecb->dte_needed, 6064 ecb->dte_alignment, state, &mstate)) < 0) 6065 continue; 6066 6067 tomax = buf->dtb_tomax; 6068 ASSERT(tomax != NULL); 6069 6070 if (ecb->dte_size != 0) 6071 DTRACE_STORE(uint32_t, tomax, offs, ecb->dte_epid); 6072 6073 mstate.dtms_epid = ecb->dte_epid; 6074 mstate.dtms_present |= DTRACE_MSTATE_EPID; 6075 6076 if (state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) 6077 mstate.dtms_access = DTRACE_ACCESS_KERNEL; 6078 else 6079 mstate.dtms_access = 0; 6080 6081 if (pred != NULL) { 6082 dtrace_difo_t *dp = pred->dtp_difo; 6083 int rval; 6084 6085 rval = dtrace_dif_emulate(dp, &mstate, vstate, state); 6086 6087 if (!(*flags & CPU_DTRACE_ERROR) && !rval) { 6088 dtrace_cacheid_t cid = probe->dtpr_predcache; 6089 6090 if (cid != DTRACE_CACHEIDNONE && !onintr) { 6091 /* 6092 * Update the predicate cache... 6093 */ 6094 ASSERT(cid == pred->dtp_cacheid); 6095 curthread->t_predcache = cid; 6096 } 6097 6098 continue; 6099 } 6100 } 6101 6102 for (act = ecb->dte_action; !(*flags & CPU_DTRACE_ERROR) && 6103 act != NULL; act = act->dta_next) { 6104 size_t valoffs; 6105 dtrace_difo_t *dp; 6106 dtrace_recdesc_t *rec = &act->dta_rec; 6107 6108 size = rec->dtrd_size; 6109 valoffs = offs + rec->dtrd_offset; 6110 6111 if (DTRACEACT_ISAGG(act->dta_kind)) { 6112 uint64_t v = 0xbad; 6113 dtrace_aggregation_t *agg; 6114 6115 agg = (dtrace_aggregation_t *)act; 6116 6117 if ((dp = act->dta_difo) != NULL) 6118 v = dtrace_dif_emulate(dp, 6119 &mstate, vstate, state); 6120 6121 if (*flags & CPU_DTRACE_ERROR) 6122 continue; 6123 6124 /* 6125 * Note that we always pass the expression 6126 * value from the previous iteration of the 6127 * action loop. This value will only be used 6128 * if there is an expression argument to the 6129 * aggregating action, denoted by the 6130 * dtag_hasarg field. 6131 */ 6132 dtrace_aggregate(agg, buf, 6133 offs, aggbuf, v, val); 6134 continue; 6135 } 6136 6137 switch (act->dta_kind) { 6138 case DTRACEACT_STOP: 6139 if (dtrace_priv_proc_destructive(state)) 6140 dtrace_action_stop(); 6141 continue; 6142 6143 case DTRACEACT_BREAKPOINT: 6144 if (dtrace_priv_kernel_destructive(state)) 6145 dtrace_action_breakpoint(ecb); 6146 continue; 6147 6148 case DTRACEACT_PANIC: 6149 if (dtrace_priv_kernel_destructive(state)) 6150 dtrace_action_panic(ecb); 6151 continue; 6152 6153 case DTRACEACT_STACK: 6154 if (!dtrace_priv_kernel(state)) 6155 continue; 6156 6157 dtrace_getpcstack((pc_t *)(tomax + valoffs), 6158 size / sizeof (pc_t), probe->dtpr_aframes, 6159 DTRACE_ANCHORED(probe) ? NULL : 6160 (uint32_t *)arg0); 6161 continue; 6162 6163#if defined(sun) 6164 case DTRACEACT_JSTACK: 6165 case DTRACEACT_USTACK: 6166 if (!dtrace_priv_proc(state)) 6167 continue; 6168 6169 /* 6170 * See comment in DIF_VAR_PID. 6171 */ 6172 if (DTRACE_ANCHORED(mstate.dtms_probe) && 6173 CPU_ON_INTR(CPU)) { 6174 int depth = DTRACE_USTACK_NFRAMES( 6175 rec->dtrd_arg) + 1; 6176 6177 dtrace_bzero((void *)(tomax + valoffs), 6178 DTRACE_USTACK_STRSIZE(rec->dtrd_arg) 6179 + depth * sizeof (uint64_t)); 6180 6181 continue; 6182 } 6183 6184 if (DTRACE_USTACK_STRSIZE(rec->dtrd_arg) != 0 && 6185 curproc->p_dtrace_helpers != NULL) { 6186 /* 6187 * This is the slow path -- we have 6188 * allocated string space, and we're 6189 * getting the stack of a process that 6190 * has helpers. Call into a separate 6191 * routine to perform this processing. 6192 */ 6193 dtrace_action_ustack(&mstate, state, 6194 (uint64_t *)(tomax + valoffs), 6195 rec->dtrd_arg); 6196 continue; 6197 } 6198 6199 DTRACE_CPUFLAG_SET(CPU_DTRACE_NOFAULT); 6200 dtrace_getupcstack((uint64_t *) 6201 (tomax + valoffs), 6202 DTRACE_USTACK_NFRAMES(rec->dtrd_arg) + 1); 6203 DTRACE_CPUFLAG_CLEAR(CPU_DTRACE_NOFAULT); 6204 continue; 6205#endif 6206 6207 default: 6208 break; 6209 } 6210 6211 dp = act->dta_difo; 6212 ASSERT(dp != NULL); 6213 6214 val = dtrace_dif_emulate(dp, &mstate, vstate, state); 6215 6216 if (*flags & CPU_DTRACE_ERROR) 6217 continue; 6218 6219 switch (act->dta_kind) { 6220 case DTRACEACT_SPECULATE: 6221 ASSERT(buf == &state->dts_buffer[cpuid]); 6222 buf = dtrace_speculation_buffer(state, 6223 cpuid, val); 6224 6225 if (buf == NULL) { 6226 *flags |= CPU_DTRACE_DROP; 6227 continue; 6228 } 6229 6230 offs = dtrace_buffer_reserve(buf, 6231 ecb->dte_needed, ecb->dte_alignment, 6232 state, NULL); 6233 6234 if (offs < 0) { 6235 *flags |= CPU_DTRACE_DROP; 6236 continue; 6237 } 6238 6239 tomax = buf->dtb_tomax; 6240 ASSERT(tomax != NULL); 6241 6242 if (ecb->dte_size != 0) 6243 DTRACE_STORE(uint32_t, tomax, offs, 6244 ecb->dte_epid); 6245 continue; 6246 6247 case DTRACEACT_PRINTM: { 6248 /* The DIF returns a 'memref'. */ 6249 uintptr_t *memref = (uintptr_t *)(uintptr_t) val; 6250 6251 /* Get the size from the memref. */ 6252 size = memref[1]; 6253 6254 /* 6255 * Check if the size exceeds the allocated 6256 * buffer size. 6257 */ 6258 if (size + sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6259 /* Flag a drop! */ 6260 *flags |= CPU_DTRACE_DROP; 6261 continue; 6262 } 6263 6264 /* Store the size in the buffer first. */ 6265 DTRACE_STORE(uintptr_t, tomax, 6266 valoffs, size); 6267 6268 /* 6269 * Offset the buffer address to the start 6270 * of the data. 6271 */ 6272 valoffs += sizeof(uintptr_t); 6273 6274 /* 6275 * Reset to the memory address rather than 6276 * the memref array, then let the BYREF 6277 * code below do the work to store the 6278 * memory data in the buffer. 6279 */ 6280 val = memref[0]; 6281 break; 6282 } 6283 6284 case DTRACEACT_PRINTT: { 6285 /* The DIF returns a 'typeref'. */ 6286 uintptr_t *typeref = (uintptr_t *)(uintptr_t) val; 6287 char c = '\0' + 1; 6288 size_t s; 6289 6290 /* 6291 * Get the type string length and round it 6292 * up so that the data that follows is 6293 * aligned for easy access. 6294 */ 6295 size_t typs = strlen((char *) typeref[2]) + 1; 6296 typs = roundup(typs, sizeof(uintptr_t)); 6297 6298 /* 6299 *Get the size from the typeref using the 6300 * number of elements and the type size. 6301 */ 6302 size = typeref[1] * typeref[3]; 6303 6304 /* 6305 * Check if the size exceeds the allocated 6306 * buffer size. 6307 */ 6308 if (size + typs + 2 * sizeof(uintptr_t) > dp->dtdo_rtype.dtdt_size) { 6309 /* Flag a drop! */ 6310 *flags |= CPU_DTRACE_DROP; 6311 6312 } 6313 6314 /* Store the size in the buffer first. */ 6315 DTRACE_STORE(uintptr_t, tomax, 6316 valoffs, size); 6317 valoffs += sizeof(uintptr_t); 6318 6319 /* Store the type size in the buffer. */ 6320 DTRACE_STORE(uintptr_t, tomax, 6321 valoffs, typeref[3]); 6322 valoffs += sizeof(uintptr_t); 6323 6324 val = typeref[2]; 6325 6326 for (s = 0; s < typs; s++) { 6327 if (c != '\0') 6328 c = dtrace_load8(val++); 6329 6330 DTRACE_STORE(uint8_t, tomax, 6331 valoffs++, c); 6332 } 6333 6334 /* 6335 * Reset to the memory address rather than 6336 * the typeref array, then let the BYREF 6337 * code below do the work to store the 6338 * memory data in the buffer. 6339 */ 6340 val = typeref[0]; 6341 break; 6342 } 6343 6344 case DTRACEACT_CHILL: 6345 if (dtrace_priv_kernel_destructive(state)) 6346 dtrace_action_chill(&mstate, val); 6347 continue; 6348 6349 case DTRACEACT_RAISE: 6350 if (dtrace_priv_proc_destructive(state)) 6351 dtrace_action_raise(val); 6352 continue; 6353 6354 case DTRACEACT_COMMIT: 6355 ASSERT(!committed); 6356 6357 /* 6358 * We need to commit our buffer state. 6359 */ 6360 if (ecb->dte_size) 6361 buf->dtb_offset = offs + ecb->dte_size; 6362 buf = &state->dts_buffer[cpuid]; 6363 dtrace_speculation_commit(state, cpuid, val); 6364 committed = 1; 6365 continue; 6366 6367 case DTRACEACT_DISCARD: 6368 dtrace_speculation_discard(state, cpuid, val); 6369 continue; 6370 6371 case DTRACEACT_DIFEXPR: 6372 case DTRACEACT_LIBACT: 6373 case DTRACEACT_PRINTF: 6374 case DTRACEACT_PRINTA: 6375 case DTRACEACT_SYSTEM: 6376 case DTRACEACT_FREOPEN: 6377 break; 6378 6379 case DTRACEACT_SYM: 6380 case DTRACEACT_MOD: 6381 if (!dtrace_priv_kernel(state)) 6382 continue; 6383 break; 6384 6385 case DTRACEACT_USYM: 6386 case DTRACEACT_UMOD: 6387 case DTRACEACT_UADDR: { 6388#if defined(sun) 6389 struct pid *pid = curthread->t_procp->p_pidp; 6390#endif 6391 6392 if (!dtrace_priv_proc(state)) 6393 continue; 6394 6395 DTRACE_STORE(uint64_t, tomax, 6396#if defined(sun) 6397 valoffs, (uint64_t)pid->pid_id); 6398#else 6399 valoffs, (uint64_t) curproc->p_pid); 6400#endif 6401 DTRACE_STORE(uint64_t, tomax, 6402 valoffs + sizeof (uint64_t), val); 6403 6404 continue; 6405 } 6406 6407 case DTRACEACT_EXIT: { 6408 /* 6409 * For the exit action, we are going to attempt 6410 * to atomically set our activity to be 6411 * draining. If this fails (either because 6412 * another CPU has beat us to the exit action, 6413 * or because our current activity is something 6414 * other than ACTIVE or WARMUP), we will 6415 * continue. This assures that the exit action 6416 * can be successfully recorded at most once 6417 * when we're in the ACTIVE state. If we're 6418 * encountering the exit() action while in 6419 * COOLDOWN, however, we want to honor the new 6420 * status code. (We know that we're the only 6421 * thread in COOLDOWN, so there is no race.) 6422 */ 6423 void *activity = &state->dts_activity; 6424 dtrace_activity_t current = state->dts_activity; 6425 6426 if (current == DTRACE_ACTIVITY_COOLDOWN) 6427 break; 6428 6429 if (current != DTRACE_ACTIVITY_WARMUP) 6430 current = DTRACE_ACTIVITY_ACTIVE; 6431 6432 if (dtrace_cas32(activity, current, 6433 DTRACE_ACTIVITY_DRAINING) != current) { 6434 *flags |= CPU_DTRACE_DROP; 6435 continue; 6436 } 6437 6438 break; 6439 } 6440 6441 default: 6442 ASSERT(0); 6443 } 6444 6445 if (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF) { 6446 uintptr_t end = valoffs + size; 6447 6448 if (!dtrace_vcanload((void *)(uintptr_t)val, 6449 &dp->dtdo_rtype, &mstate, vstate)) 6450 continue; 6451 6452 /* 6453 * If this is a string, we're going to only 6454 * load until we find the zero byte -- after 6455 * which we'll store zero bytes. 6456 */ 6457 if (dp->dtdo_rtype.dtdt_kind == 6458 DIF_TYPE_STRING) { 6459 char c = '\0' + 1; 6460 int intuple = act->dta_intuple; 6461 size_t s; 6462 6463 for (s = 0; s < size; s++) { 6464 if (c != '\0') 6465 c = dtrace_load8(val++); 6466 6467 DTRACE_STORE(uint8_t, tomax, 6468 valoffs++, c); 6469 6470 if (c == '\0' && intuple) 6471 break; 6472 } 6473 6474 continue; 6475 } 6476 6477 while (valoffs < end) { 6478 DTRACE_STORE(uint8_t, tomax, valoffs++, 6479 dtrace_load8(val++)); 6480 } 6481 6482 continue; 6483 } 6484 6485 switch (size) { 6486 case 0: 6487 break; 6488 6489 case sizeof (uint8_t): 6490 DTRACE_STORE(uint8_t, tomax, valoffs, val); 6491 break; 6492 case sizeof (uint16_t): 6493 DTRACE_STORE(uint16_t, tomax, valoffs, val); 6494 break; 6495 case sizeof (uint32_t): 6496 DTRACE_STORE(uint32_t, tomax, valoffs, val); 6497 break; 6498 case sizeof (uint64_t): 6499 DTRACE_STORE(uint64_t, tomax, valoffs, val); 6500 break; 6501 default: 6502 /* 6503 * Any other size should have been returned by 6504 * reference, not by value. 6505 */ 6506 ASSERT(0); 6507 break; 6508 } 6509 } 6510 6511 if (*flags & CPU_DTRACE_DROP) 6512 continue; 6513 6514 if (*flags & CPU_DTRACE_FAULT) { 6515 int ndx; 6516 dtrace_action_t *err; 6517 6518 buf->dtb_errors++; 6519 6520 if (probe->dtpr_id == dtrace_probeid_error) { 6521 /* 6522 * There's nothing we can do -- we had an 6523 * error on the error probe. We bump an 6524 * error counter to at least indicate that 6525 * this condition happened. 6526 */ 6527 dtrace_error(&state->dts_dblerrors); 6528 continue; 6529 } 6530 6531 if (vtime) { 6532 /* 6533 * Before recursing on dtrace_probe(), we 6534 * need to explicitly clear out our start 6535 * time to prevent it from being accumulated 6536 * into t_dtrace_vtime. 6537 */ 6538 curthread->t_dtrace_start = 0; 6539 } 6540 6541 /* 6542 * Iterate over the actions to figure out which action 6543 * we were processing when we experienced the error. 6544 * Note that act points _past_ the faulting action; if 6545 * act is ecb->dte_action, the fault was in the 6546 * predicate, if it's ecb->dte_action->dta_next it's 6547 * in action #1, and so on. 6548 */ 6549 for (err = ecb->dte_action, ndx = 0; 6550 err != act; err = err->dta_next, ndx++) 6551 continue; 6552 6553 dtrace_probe_error(state, ecb->dte_epid, ndx, 6554 (mstate.dtms_present & DTRACE_MSTATE_FLTOFFS) ? 6555 mstate.dtms_fltoffs : -1, DTRACE_FLAGS2FLT(*flags), 6556 cpu_core[cpuid].cpuc_dtrace_illval); 6557 6558 continue; 6559 } 6560 6561 if (!committed) 6562 buf->dtb_offset = offs + ecb->dte_size; 6563 } 6564 6565 if (vtime) 6566 curthread->t_dtrace_start = dtrace_gethrtime(); 6567 6568 dtrace_interrupt_enable(cookie); 6569} 6570 6571/* 6572 * DTrace Probe Hashing Functions 6573 * 6574 * The functions in this section (and indeed, the functions in remaining 6575 * sections) are not _called_ from probe context. (Any exceptions to this are 6576 * marked with a "Note:".) Rather, they are called from elsewhere in the 6577 * DTrace framework to look-up probes in, add probes to and remove probes from 6578 * the DTrace probe hashes. (Each probe is hashed by each element of the 6579 * probe tuple -- allowing for fast lookups, regardless of what was 6580 * specified.) 6581 */ 6582static uint_t 6583dtrace_hash_str(const char *p) 6584{ 6585 unsigned int g; 6586 uint_t hval = 0; 6587 6588 while (*p) { 6589 hval = (hval << 4) + *p++; 6590 if ((g = (hval & 0xf0000000)) != 0) 6591 hval ^= g >> 24; 6592 hval &= ~g; 6593 } 6594 return (hval); 6595} 6596 6597static dtrace_hash_t * 6598dtrace_hash_create(uintptr_t stroffs, uintptr_t nextoffs, uintptr_t prevoffs) 6599{ 6600 dtrace_hash_t *hash = kmem_zalloc(sizeof (dtrace_hash_t), KM_SLEEP); 6601 6602 hash->dth_stroffs = stroffs; 6603 hash->dth_nextoffs = nextoffs; 6604 hash->dth_prevoffs = prevoffs; 6605 6606 hash->dth_size = 1; 6607 hash->dth_mask = hash->dth_size - 1; 6608 6609 hash->dth_tab = kmem_zalloc(hash->dth_size * 6610 sizeof (dtrace_hashbucket_t *), KM_SLEEP); 6611 6612 return (hash); 6613} 6614 6615static void 6616dtrace_hash_destroy(dtrace_hash_t *hash) 6617{ 6618#ifdef DEBUG 6619 int i; 6620 6621 for (i = 0; i < hash->dth_size; i++) 6622 ASSERT(hash->dth_tab[i] == NULL); 6623#endif 6624 6625 kmem_free(hash->dth_tab, 6626 hash->dth_size * sizeof (dtrace_hashbucket_t *)); 6627 kmem_free(hash, sizeof (dtrace_hash_t)); 6628} 6629 6630static void 6631dtrace_hash_resize(dtrace_hash_t *hash) 6632{ 6633 int size = hash->dth_size, i, ndx; 6634 int new_size = hash->dth_size << 1; 6635 int new_mask = new_size - 1; 6636 dtrace_hashbucket_t **new_tab, *bucket, *next; 6637 6638 ASSERT((new_size & new_mask) == 0); 6639 6640 new_tab = kmem_zalloc(new_size * sizeof (void *), KM_SLEEP); 6641 6642 for (i = 0; i < size; i++) { 6643 for (bucket = hash->dth_tab[i]; bucket != NULL; bucket = next) { 6644 dtrace_probe_t *probe = bucket->dthb_chain; 6645 6646 ASSERT(probe != NULL); 6647 ndx = DTRACE_HASHSTR(hash, probe) & new_mask; 6648 6649 next = bucket->dthb_next; 6650 bucket->dthb_next = new_tab[ndx]; 6651 new_tab[ndx] = bucket; 6652 } 6653 } 6654 6655 kmem_free(hash->dth_tab, hash->dth_size * sizeof (void *)); 6656 hash->dth_tab = new_tab; 6657 hash->dth_size = new_size; 6658 hash->dth_mask = new_mask; 6659} 6660 6661static void 6662dtrace_hash_add(dtrace_hash_t *hash, dtrace_probe_t *new) 6663{ 6664 int hashval = DTRACE_HASHSTR(hash, new); 6665 int ndx = hashval & hash->dth_mask; 6666 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6667 dtrace_probe_t **nextp, **prevp; 6668 6669 for (; bucket != NULL; bucket = bucket->dthb_next) { 6670 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, new)) 6671 goto add; 6672 } 6673 6674 if ((hash->dth_nbuckets >> 1) > hash->dth_size) { 6675 dtrace_hash_resize(hash); 6676 dtrace_hash_add(hash, new); 6677 return; 6678 } 6679 6680 bucket = kmem_zalloc(sizeof (dtrace_hashbucket_t), KM_SLEEP); 6681 bucket->dthb_next = hash->dth_tab[ndx]; 6682 hash->dth_tab[ndx] = bucket; 6683 hash->dth_nbuckets++; 6684 6685add: 6686 nextp = DTRACE_HASHNEXT(hash, new); 6687 ASSERT(*nextp == NULL && *(DTRACE_HASHPREV(hash, new)) == NULL); 6688 *nextp = bucket->dthb_chain; 6689 6690 if (bucket->dthb_chain != NULL) { 6691 prevp = DTRACE_HASHPREV(hash, bucket->dthb_chain); 6692 ASSERT(*prevp == NULL); 6693 *prevp = new; 6694 } 6695 6696 bucket->dthb_chain = new; 6697 bucket->dthb_len++; 6698} 6699 6700static dtrace_probe_t * 6701dtrace_hash_lookup(dtrace_hash_t *hash, dtrace_probe_t *template) 6702{ 6703 int hashval = DTRACE_HASHSTR(hash, template); 6704 int ndx = hashval & hash->dth_mask; 6705 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6706 6707 for (; bucket != NULL; bucket = bucket->dthb_next) { 6708 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6709 return (bucket->dthb_chain); 6710 } 6711 6712 return (NULL); 6713} 6714 6715static int 6716dtrace_hash_collisions(dtrace_hash_t *hash, dtrace_probe_t *template) 6717{ 6718 int hashval = DTRACE_HASHSTR(hash, template); 6719 int ndx = hashval & hash->dth_mask; 6720 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6721 6722 for (; bucket != NULL; bucket = bucket->dthb_next) { 6723 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, template)) 6724 return (bucket->dthb_len); 6725 } 6726 6727 return (0); 6728} 6729 6730static void 6731dtrace_hash_remove(dtrace_hash_t *hash, dtrace_probe_t *probe) 6732{ 6733 int ndx = DTRACE_HASHSTR(hash, probe) & hash->dth_mask; 6734 dtrace_hashbucket_t *bucket = hash->dth_tab[ndx]; 6735 6736 dtrace_probe_t **prevp = DTRACE_HASHPREV(hash, probe); 6737 dtrace_probe_t **nextp = DTRACE_HASHNEXT(hash, probe); 6738 6739 /* 6740 * Find the bucket that we're removing this probe from. 6741 */ 6742 for (; bucket != NULL; bucket = bucket->dthb_next) { 6743 if (DTRACE_HASHEQ(hash, bucket->dthb_chain, probe)) 6744 break; 6745 } 6746 6747 ASSERT(bucket != NULL); 6748 6749 if (*prevp == NULL) { 6750 if (*nextp == NULL) { 6751 /* 6752 * The removed probe was the only probe on this 6753 * bucket; we need to remove the bucket. 6754 */ 6755 dtrace_hashbucket_t *b = hash->dth_tab[ndx]; 6756 6757 ASSERT(bucket->dthb_chain == probe); 6758 ASSERT(b != NULL); 6759 6760 if (b == bucket) { 6761 hash->dth_tab[ndx] = bucket->dthb_next; 6762 } else { 6763 while (b->dthb_next != bucket) 6764 b = b->dthb_next; 6765 b->dthb_next = bucket->dthb_next; 6766 } 6767 6768 ASSERT(hash->dth_nbuckets > 0); 6769 hash->dth_nbuckets--; 6770 kmem_free(bucket, sizeof (dtrace_hashbucket_t)); 6771 return; 6772 } 6773 6774 bucket->dthb_chain = *nextp; 6775 } else { 6776 *(DTRACE_HASHNEXT(hash, *prevp)) = *nextp; 6777 } 6778 6779 if (*nextp != NULL) 6780 *(DTRACE_HASHPREV(hash, *nextp)) = *prevp; 6781} 6782 6783/* 6784 * DTrace Utility Functions 6785 * 6786 * These are random utility functions that are _not_ called from probe context. 6787 */ 6788static int 6789dtrace_badattr(const dtrace_attribute_t *a) 6790{ 6791 return (a->dtat_name > DTRACE_STABILITY_MAX || 6792 a->dtat_data > DTRACE_STABILITY_MAX || 6793 a->dtat_class > DTRACE_CLASS_MAX); 6794} 6795 6796/* 6797 * Return a duplicate copy of a string. If the specified string is NULL, 6798 * this function returns a zero-length string. 6799 */ 6800static char * 6801dtrace_strdup(const char *str) 6802{ 6803 char *new = kmem_zalloc((str != NULL ? strlen(str) : 0) + 1, KM_SLEEP); 6804 6805 if (str != NULL) 6806 (void) strcpy(new, str); 6807 6808 return (new); 6809} 6810 6811#define DTRACE_ISALPHA(c) \ 6812 (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 6813 6814static int 6815dtrace_badname(const char *s) 6816{ 6817 char c; 6818 6819 if (s == NULL || (c = *s++) == '\0') 6820 return (0); 6821 6822 if (!DTRACE_ISALPHA(c) && c != '-' && c != '_' && c != '.') 6823 return (1); 6824 6825 while ((c = *s++) != '\0') { 6826 if (!DTRACE_ISALPHA(c) && (c < '0' || c > '9') && 6827 c != '-' && c != '_' && c != '.' && c != '`') 6828 return (1); 6829 } 6830 6831 return (0); 6832} 6833 6834static void 6835dtrace_cred2priv(cred_t *cr, uint32_t *privp, uid_t *uidp, zoneid_t *zoneidp) 6836{ 6837 uint32_t priv; 6838 6839#if defined(sun) 6840 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 6841 /* 6842 * For DTRACE_PRIV_ALL, the uid and zoneid don't matter. 6843 */ 6844 priv = DTRACE_PRIV_ALL; 6845 } else { 6846 *uidp = crgetuid(cr); 6847 *zoneidp = crgetzoneid(cr); 6848 6849 priv = 0; 6850 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) 6851 priv |= DTRACE_PRIV_KERNEL | DTRACE_PRIV_USER; 6852 else if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) 6853 priv |= DTRACE_PRIV_USER; 6854 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) 6855 priv |= DTRACE_PRIV_PROC; 6856 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 6857 priv |= DTRACE_PRIV_OWNER; 6858 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 6859 priv |= DTRACE_PRIV_ZONEOWNER; 6860 } 6861#else 6862 priv = DTRACE_PRIV_ALL; 6863#endif 6864 6865 *privp = priv; 6866} 6867 6868#ifdef DTRACE_ERRDEBUG 6869static void 6870dtrace_errdebug(const char *str) 6871{ 6872 int hval = dtrace_hash_str(str) % DTRACE_ERRHASHSZ; 6873 int occupied = 0; 6874 6875 mutex_enter(&dtrace_errlock); 6876 dtrace_errlast = str; 6877 dtrace_errthread = curthread; 6878 6879 while (occupied++ < DTRACE_ERRHASHSZ) { 6880 if (dtrace_errhash[hval].dter_msg == str) { 6881 dtrace_errhash[hval].dter_count++; 6882 goto out; 6883 } 6884 6885 if (dtrace_errhash[hval].dter_msg != NULL) { 6886 hval = (hval + 1) % DTRACE_ERRHASHSZ; 6887 continue; 6888 } 6889 6890 dtrace_errhash[hval].dter_msg = str; 6891 dtrace_errhash[hval].dter_count = 1; 6892 goto out; 6893 } 6894 6895 panic("dtrace: undersized error hash"); 6896out: 6897 mutex_exit(&dtrace_errlock); 6898} 6899#endif 6900 6901/* 6902 * DTrace Matching Functions 6903 * 6904 * These functions are used to match groups of probes, given some elements of 6905 * a probe tuple, or some globbed expressions for elements of a probe tuple. 6906 */ 6907static int 6908dtrace_match_priv(const dtrace_probe_t *prp, uint32_t priv, uid_t uid, 6909 zoneid_t zoneid) 6910{ 6911 if (priv != DTRACE_PRIV_ALL) { 6912 uint32_t ppriv = prp->dtpr_provider->dtpv_priv.dtpp_flags; 6913 uint32_t match = priv & ppriv; 6914 6915 /* 6916 * No PRIV_DTRACE_* privileges... 6917 */ 6918 if ((priv & (DTRACE_PRIV_PROC | DTRACE_PRIV_USER | 6919 DTRACE_PRIV_KERNEL)) == 0) 6920 return (0); 6921 6922 /* 6923 * No matching bits, but there were bits to match... 6924 */ 6925 if (match == 0 && ppriv != 0) 6926 return (0); 6927 6928 /* 6929 * Need to have permissions to the process, but don't... 6930 */ 6931 if (((ppriv & ~match) & DTRACE_PRIV_OWNER) != 0 && 6932 uid != prp->dtpr_provider->dtpv_priv.dtpp_uid) { 6933 return (0); 6934 } 6935 6936 /* 6937 * Need to be in the same zone unless we possess the 6938 * privilege to examine all zones. 6939 */ 6940 if (((ppriv & ~match) & DTRACE_PRIV_ZONEOWNER) != 0 && 6941 zoneid != prp->dtpr_provider->dtpv_priv.dtpp_zoneid) { 6942 return (0); 6943 } 6944 } 6945 6946 return (1); 6947} 6948 6949/* 6950 * dtrace_match_probe compares a dtrace_probe_t to a pre-compiled key, which 6951 * consists of input pattern strings and an ops-vector to evaluate them. 6952 * This function returns >0 for match, 0 for no match, and <0 for error. 6953 */ 6954static int 6955dtrace_match_probe(const dtrace_probe_t *prp, const dtrace_probekey_t *pkp, 6956 uint32_t priv, uid_t uid, zoneid_t zoneid) 6957{ 6958 dtrace_provider_t *pvp = prp->dtpr_provider; 6959 int rv; 6960 6961 if (pvp->dtpv_defunct) 6962 return (0); 6963 6964 if ((rv = pkp->dtpk_pmatch(pvp->dtpv_name, pkp->dtpk_prov, 0)) <= 0) 6965 return (rv); 6966 6967 if ((rv = pkp->dtpk_mmatch(prp->dtpr_mod, pkp->dtpk_mod, 0)) <= 0) 6968 return (rv); 6969 6970 if ((rv = pkp->dtpk_fmatch(prp->dtpr_func, pkp->dtpk_func, 0)) <= 0) 6971 return (rv); 6972 6973 if ((rv = pkp->dtpk_nmatch(prp->dtpr_name, pkp->dtpk_name, 0)) <= 0) 6974 return (rv); 6975 6976 if (dtrace_match_priv(prp, priv, uid, zoneid) == 0) 6977 return (0); 6978 6979 return (rv); 6980} 6981 6982/* 6983 * dtrace_match_glob() is a safe kernel implementation of the gmatch(3GEN) 6984 * interface for matching a glob pattern 'p' to an input string 's'. Unlike 6985 * libc's version, the kernel version only applies to 8-bit ASCII strings. 6986 * In addition, all of the recursion cases except for '*' matching have been 6987 * unwound. For '*', we still implement recursive evaluation, but a depth 6988 * counter is maintained and matching is aborted if we recurse too deep. 6989 * The function returns 0 if no match, >0 if match, and <0 if recursion error. 6990 */ 6991static int 6992dtrace_match_glob(const char *s, const char *p, int depth) 6993{ 6994 const char *olds; 6995 char s1, c; 6996 int gs; 6997 6998 if (depth > DTRACE_PROBEKEY_MAXDEPTH) 6999 return (-1); 7000 7001 if (s == NULL) 7002 s = ""; /* treat NULL as empty string */ 7003 7004top: 7005 olds = s; 7006 s1 = *s++; 7007 7008 if (p == NULL) 7009 return (0); 7010 7011 if ((c = *p++) == '\0') 7012 return (s1 == '\0'); 7013 7014 switch (c) { 7015 case '[': { 7016 int ok = 0, notflag = 0; 7017 char lc = '\0'; 7018 7019 if (s1 == '\0') 7020 return (0); 7021 7022 if (*p == '!') { 7023 notflag = 1; 7024 p++; 7025 } 7026 7027 if ((c = *p++) == '\0') 7028 return (0); 7029 7030 do { 7031 if (c == '-' && lc != '\0' && *p != ']') { 7032 if ((c = *p++) == '\0') 7033 return (0); 7034 if (c == '\\' && (c = *p++) == '\0') 7035 return (0); 7036 7037 if (notflag) { 7038 if (s1 < lc || s1 > c) 7039 ok++; 7040 else 7041 return (0); 7042 } else if (lc <= s1 && s1 <= c) 7043 ok++; 7044 7045 } else if (c == '\\' && (c = *p++) == '\0') 7046 return (0); 7047 7048 lc = c; /* save left-hand 'c' for next iteration */ 7049 7050 if (notflag) { 7051 if (s1 != c) 7052 ok++; 7053 else 7054 return (0); 7055 } else if (s1 == c) 7056 ok++; 7057 7058 if ((c = *p++) == '\0') 7059 return (0); 7060 7061 } while (c != ']'); 7062 7063 if (ok) 7064 goto top; 7065 7066 return (0); 7067 } 7068 7069 case '\\': 7070 if ((c = *p++) == '\0') 7071 return (0); 7072 /*FALLTHRU*/ 7073 7074 default: 7075 if (c != s1) 7076 return (0); 7077 /*FALLTHRU*/ 7078 7079 case '?': 7080 if (s1 != '\0') 7081 goto top; 7082 return (0); 7083 7084 case '*': 7085 while (*p == '*') 7086 p++; /* consecutive *'s are identical to a single one */ 7087 7088 if (*p == '\0') 7089 return (1); 7090 7091 for (s = olds; *s != '\0'; s++) { 7092 if ((gs = dtrace_match_glob(s, p, depth + 1)) != 0) 7093 return (gs); 7094 } 7095 7096 return (0); 7097 } 7098} 7099 7100/*ARGSUSED*/ 7101static int 7102dtrace_match_string(const char *s, const char *p, int depth) 7103{ 7104 return (s != NULL && strcmp(s, p) == 0); 7105} 7106 7107/*ARGSUSED*/ 7108static int 7109dtrace_match_nul(const char *s, const char *p, int depth) 7110{ 7111 return (1); /* always match the empty pattern */ 7112} 7113 7114/*ARGSUSED*/ 7115static int 7116dtrace_match_nonzero(const char *s, const char *p, int depth) 7117{ 7118 return (s != NULL && s[0] != '\0'); 7119} 7120 7121static int 7122dtrace_match(const dtrace_probekey_t *pkp, uint32_t priv, uid_t uid, 7123 zoneid_t zoneid, int (*matched)(dtrace_probe_t *, void *), void *arg) 7124{ 7125 dtrace_probe_t template, *probe; 7126 dtrace_hash_t *hash = NULL; 7127 int len, best = INT_MAX, nmatched = 0; 7128 dtrace_id_t i; 7129 7130 ASSERT(MUTEX_HELD(&dtrace_lock)); 7131 7132 /* 7133 * If the probe ID is specified in the key, just lookup by ID and 7134 * invoke the match callback once if a matching probe is found. 7135 */ 7136 if (pkp->dtpk_id != DTRACE_IDNONE) { 7137 if ((probe = dtrace_probe_lookup_id(pkp->dtpk_id)) != NULL && 7138 dtrace_match_probe(probe, pkp, priv, uid, zoneid) > 0) { 7139 (void) (*matched)(probe, arg); 7140 nmatched++; 7141 } 7142 return (nmatched); 7143 } 7144 7145 template.dtpr_mod = (char *)pkp->dtpk_mod; 7146 template.dtpr_func = (char *)pkp->dtpk_func; 7147 template.dtpr_name = (char *)pkp->dtpk_name; 7148 7149 /* 7150 * We want to find the most distinct of the module name, function 7151 * name, and name. So for each one that is not a glob pattern or 7152 * empty string, we perform a lookup in the corresponding hash and 7153 * use the hash table with the fewest collisions to do our search. 7154 */ 7155 if (pkp->dtpk_mmatch == &dtrace_match_string && 7156 (len = dtrace_hash_collisions(dtrace_bymod, &template)) < best) { 7157 best = len; 7158 hash = dtrace_bymod; 7159 } 7160 7161 if (pkp->dtpk_fmatch == &dtrace_match_string && 7162 (len = dtrace_hash_collisions(dtrace_byfunc, &template)) < best) { 7163 best = len; 7164 hash = dtrace_byfunc; 7165 } 7166 7167 if (pkp->dtpk_nmatch == &dtrace_match_string && 7168 (len = dtrace_hash_collisions(dtrace_byname, &template)) < best) { 7169 best = len; 7170 hash = dtrace_byname; 7171 } 7172 7173 /* 7174 * If we did not select a hash table, iterate over every probe and 7175 * invoke our callback for each one that matches our input probe key. 7176 */ 7177 if (hash == NULL) { 7178 for (i = 0; i < dtrace_nprobes; i++) { 7179 if ((probe = dtrace_probes[i]) == NULL || 7180 dtrace_match_probe(probe, pkp, priv, uid, 7181 zoneid) <= 0) 7182 continue; 7183 7184 nmatched++; 7185 7186 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7187 break; 7188 } 7189 7190 return (nmatched); 7191 } 7192 7193 /* 7194 * If we selected a hash table, iterate over each probe of the same key 7195 * name and invoke the callback for every probe that matches the other 7196 * attributes of our input probe key. 7197 */ 7198 for (probe = dtrace_hash_lookup(hash, &template); probe != NULL; 7199 probe = *(DTRACE_HASHNEXT(hash, probe))) { 7200 7201 if (dtrace_match_probe(probe, pkp, priv, uid, zoneid) <= 0) 7202 continue; 7203 7204 nmatched++; 7205 7206 if ((*matched)(probe, arg) != DTRACE_MATCH_NEXT) 7207 break; 7208 } 7209 7210 return (nmatched); 7211} 7212 7213/* 7214 * Return the function pointer dtrace_probecmp() should use to compare the 7215 * specified pattern with a string. For NULL or empty patterns, we select 7216 * dtrace_match_nul(). For glob pattern strings, we use dtrace_match_glob(). 7217 * For non-empty non-glob strings, we use dtrace_match_string(). 7218 */ 7219static dtrace_probekey_f * 7220dtrace_probekey_func(const char *p) 7221{ 7222 char c; 7223 7224 if (p == NULL || *p == '\0') 7225 return (&dtrace_match_nul); 7226 7227 while ((c = *p++) != '\0') { 7228 if (c == '[' || c == '?' || c == '*' || c == '\\') 7229 return (&dtrace_match_glob); 7230 } 7231 7232 return (&dtrace_match_string); 7233} 7234 7235/* 7236 * Build a probe comparison key for use with dtrace_match_probe() from the 7237 * given probe description. By convention, a null key only matches anchored 7238 * probes: if each field is the empty string, reset dtpk_fmatch to 7239 * dtrace_match_nonzero(). 7240 */ 7241static void 7242dtrace_probekey(dtrace_probedesc_t *pdp, dtrace_probekey_t *pkp) 7243{ 7244 pkp->dtpk_prov = pdp->dtpd_provider; 7245 pkp->dtpk_pmatch = dtrace_probekey_func(pdp->dtpd_provider); 7246 7247 pkp->dtpk_mod = pdp->dtpd_mod; 7248 pkp->dtpk_mmatch = dtrace_probekey_func(pdp->dtpd_mod); 7249 7250 pkp->dtpk_func = pdp->dtpd_func; 7251 pkp->dtpk_fmatch = dtrace_probekey_func(pdp->dtpd_func); 7252 7253 pkp->dtpk_name = pdp->dtpd_name; 7254 pkp->dtpk_nmatch = dtrace_probekey_func(pdp->dtpd_name); 7255 7256 pkp->dtpk_id = pdp->dtpd_id; 7257 7258 if (pkp->dtpk_id == DTRACE_IDNONE && 7259 pkp->dtpk_pmatch == &dtrace_match_nul && 7260 pkp->dtpk_mmatch == &dtrace_match_nul && 7261 pkp->dtpk_fmatch == &dtrace_match_nul && 7262 pkp->dtpk_nmatch == &dtrace_match_nul) 7263 pkp->dtpk_fmatch = &dtrace_match_nonzero; 7264} 7265 7266/* 7267 * DTrace Provider-to-Framework API Functions 7268 * 7269 * These functions implement much of the Provider-to-Framework API, as 7270 * described in <sys/dtrace.h>. The parts of the API not in this section are 7271 * the functions in the API for probe management (found below), and 7272 * dtrace_probe() itself (found above). 7273 */ 7274 7275/* 7276 * Register the calling provider with the DTrace framework. This should 7277 * generally be called by DTrace providers in their attach(9E) entry point. 7278 */ 7279int 7280dtrace_register(const char *name, const dtrace_pattr_t *pap, uint32_t priv, 7281 cred_t *cr, const dtrace_pops_t *pops, void *arg, dtrace_provider_id_t *idp) 7282{ 7283 dtrace_provider_t *provider; 7284 7285 if (name == NULL || pap == NULL || pops == NULL || idp == NULL) { 7286 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7287 "arguments", name ? name : "<NULL>"); 7288 return (EINVAL); 7289 } 7290 7291 if (name[0] == '\0' || dtrace_badname(name)) { 7292 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7293 "provider name", name); 7294 return (EINVAL); 7295 } 7296 7297 if ((pops->dtps_provide == NULL && pops->dtps_provide_module == NULL) || 7298 pops->dtps_enable == NULL || pops->dtps_disable == NULL || 7299 pops->dtps_destroy == NULL || 7300 ((pops->dtps_resume == NULL) != (pops->dtps_suspend == NULL))) { 7301 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7302 "provider ops", name); 7303 return (EINVAL); 7304 } 7305 7306 if (dtrace_badattr(&pap->dtpa_provider) || 7307 dtrace_badattr(&pap->dtpa_mod) || 7308 dtrace_badattr(&pap->dtpa_func) || 7309 dtrace_badattr(&pap->dtpa_name) || 7310 dtrace_badattr(&pap->dtpa_args)) { 7311 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7312 "provider attributes", name); 7313 return (EINVAL); 7314 } 7315 7316 if (priv & ~DTRACE_PRIV_ALL) { 7317 cmn_err(CE_WARN, "failed to register provider '%s': invalid " 7318 "privilege attributes", name); 7319 return (EINVAL); 7320 } 7321 7322 if ((priv & DTRACE_PRIV_KERNEL) && 7323 (priv & (DTRACE_PRIV_USER | DTRACE_PRIV_OWNER)) && 7324 pops->dtps_usermode == NULL) { 7325 cmn_err(CE_WARN, "failed to register provider '%s': need " 7326 "dtps_usermode() op for given privilege attributes", name); 7327 return (EINVAL); 7328 } 7329 7330 provider = kmem_zalloc(sizeof (dtrace_provider_t), KM_SLEEP); 7331 provider->dtpv_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 7332 (void) strcpy(provider->dtpv_name, name); 7333 7334 provider->dtpv_attr = *pap; 7335 provider->dtpv_priv.dtpp_flags = priv; 7336 if (cr != NULL) { 7337 provider->dtpv_priv.dtpp_uid = crgetuid(cr); 7338 provider->dtpv_priv.dtpp_zoneid = crgetzoneid(cr); 7339 } 7340 provider->dtpv_pops = *pops; 7341 7342 if (pops->dtps_provide == NULL) { 7343 ASSERT(pops->dtps_provide_module != NULL); 7344 provider->dtpv_pops.dtps_provide = 7345 (void (*)(void *, dtrace_probedesc_t *))dtrace_nullop; 7346 } 7347 7348 if (pops->dtps_provide_module == NULL) { 7349 ASSERT(pops->dtps_provide != NULL); 7350 provider->dtpv_pops.dtps_provide_module = 7351 (void (*)(void *, modctl_t *))dtrace_nullop; 7352 } 7353 7354 if (pops->dtps_suspend == NULL) { 7355 ASSERT(pops->dtps_resume == NULL); 7356 provider->dtpv_pops.dtps_suspend = 7357 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7358 provider->dtpv_pops.dtps_resume = 7359 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop; 7360 } 7361 7362 provider->dtpv_arg = arg; 7363 *idp = (dtrace_provider_id_t)provider; 7364 7365 if (pops == &dtrace_provider_ops) { 7366 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7367 ASSERT(MUTEX_HELD(&dtrace_lock)); 7368 ASSERT(dtrace_anon.dta_enabling == NULL); 7369 7370 /* 7371 * We make sure that the DTrace provider is at the head of 7372 * the provider chain. 7373 */ 7374 provider->dtpv_next = dtrace_provider; 7375 dtrace_provider = provider; 7376 return (0); 7377 } 7378 7379 mutex_enter(&dtrace_provider_lock); 7380 mutex_enter(&dtrace_lock); 7381 7382 /* 7383 * If there is at least one provider registered, we'll add this 7384 * provider after the first provider. 7385 */ 7386 if (dtrace_provider != NULL) { 7387 provider->dtpv_next = dtrace_provider->dtpv_next; 7388 dtrace_provider->dtpv_next = provider; 7389 } else { 7390 dtrace_provider = provider; 7391 } 7392 7393 if (dtrace_retained != NULL) { 7394 dtrace_enabling_provide(provider); 7395 7396 /* 7397 * Now we need to call dtrace_enabling_matchall() -- which 7398 * will acquire cpu_lock and dtrace_lock. We therefore need 7399 * to drop all of our locks before calling into it... 7400 */ 7401 mutex_exit(&dtrace_lock); 7402 mutex_exit(&dtrace_provider_lock); 7403 dtrace_enabling_matchall(); 7404 7405 return (0); 7406 } 7407 7408 mutex_exit(&dtrace_lock); 7409 mutex_exit(&dtrace_provider_lock); 7410 7411 return (0); 7412} 7413 7414/* 7415 * Unregister the specified provider from the DTrace framework. This should 7416 * generally be called by DTrace providers in their detach(9E) entry point. 7417 */ 7418int 7419dtrace_unregister(dtrace_provider_id_t id) 7420{ 7421 dtrace_provider_t *old = (dtrace_provider_t *)id; 7422 dtrace_provider_t *prev = NULL; 7423 int i, self = 0; 7424 dtrace_probe_t *probe, *first = NULL; 7425 7426 if (old->dtpv_pops.dtps_enable == 7427 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop) { 7428 /* 7429 * If DTrace itself is the provider, we're called with locks 7430 * already held. 7431 */ 7432 ASSERT(old == dtrace_provider); 7433#if defined(sun) 7434 ASSERT(dtrace_devi != NULL); 7435#endif 7436 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7437 ASSERT(MUTEX_HELD(&dtrace_lock)); 7438 self = 1; 7439 7440 if (dtrace_provider->dtpv_next != NULL) { 7441 /* 7442 * There's another provider here; return failure. 7443 */ 7444 return (EBUSY); 7445 } 7446 } else { 7447 mutex_enter(&dtrace_provider_lock); 7448 mutex_enter(&mod_lock); 7449 mutex_enter(&dtrace_lock); 7450 } 7451 7452 /* 7453 * If anyone has /dev/dtrace open, or if there are anonymous enabled 7454 * probes, we refuse to let providers slither away, unless this 7455 * provider has already been explicitly invalidated. 7456 */ 7457 if (!old->dtpv_defunct && 7458 (dtrace_opens || (dtrace_anon.dta_state != NULL && 7459 dtrace_anon.dta_state->dts_necbs > 0))) { 7460 if (!self) { 7461 mutex_exit(&dtrace_lock); 7462 mutex_exit(&mod_lock); 7463 mutex_exit(&dtrace_provider_lock); 7464 } 7465 return (EBUSY); 7466 } 7467 7468 /* 7469 * Attempt to destroy the probes associated with this provider. 7470 */ 7471 for (i = 0; i < dtrace_nprobes; i++) { 7472 if ((probe = dtrace_probes[i]) == NULL) 7473 continue; 7474 7475 if (probe->dtpr_provider != old) 7476 continue; 7477 7478 if (probe->dtpr_ecb == NULL) 7479 continue; 7480 7481 /* 7482 * We have at least one ECB; we can't remove this provider. 7483 */ 7484 if (!self) { 7485 mutex_exit(&dtrace_lock); 7486 mutex_exit(&mod_lock); 7487 mutex_exit(&dtrace_provider_lock); 7488 } 7489 return (EBUSY); 7490 } 7491 7492 /* 7493 * All of the probes for this provider are disabled; we can safely 7494 * remove all of them from their hash chains and from the probe array. 7495 */ 7496 for (i = 0; i < dtrace_nprobes; i++) { 7497 if ((probe = dtrace_probes[i]) == NULL) 7498 continue; 7499 7500 if (probe->dtpr_provider != old) 7501 continue; 7502 7503 dtrace_probes[i] = NULL; 7504 7505 dtrace_hash_remove(dtrace_bymod, probe); 7506 dtrace_hash_remove(dtrace_byfunc, probe); 7507 dtrace_hash_remove(dtrace_byname, probe); 7508 7509 if (first == NULL) { 7510 first = probe; 7511 probe->dtpr_nextmod = NULL; 7512 } else { 7513 probe->dtpr_nextmod = first; 7514 first = probe; 7515 } 7516 } 7517 7518 /* 7519 * The provider's probes have been removed from the hash chains and 7520 * from the probe array. Now issue a dtrace_sync() to be sure that 7521 * everyone has cleared out from any probe array processing. 7522 */ 7523 dtrace_sync(); 7524 7525 for (probe = first; probe != NULL; probe = first) { 7526 first = probe->dtpr_nextmod; 7527 7528 old->dtpv_pops.dtps_destroy(old->dtpv_arg, probe->dtpr_id, 7529 probe->dtpr_arg); 7530 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7531 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7532 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7533#if defined(sun) 7534 vmem_free(dtrace_arena, (void *)(uintptr_t)(probe->dtpr_id), 1); 7535#else 7536 free_unr(dtrace_arena, probe->dtpr_id); 7537#endif 7538 kmem_free(probe, sizeof (dtrace_probe_t)); 7539 } 7540 7541 if ((prev = dtrace_provider) == old) { 7542#if defined(sun) 7543 ASSERT(self || dtrace_devi == NULL); 7544 ASSERT(old->dtpv_next == NULL || dtrace_devi == NULL); 7545#endif 7546 dtrace_provider = old->dtpv_next; 7547 } else { 7548 while (prev != NULL && prev->dtpv_next != old) 7549 prev = prev->dtpv_next; 7550 7551 if (prev == NULL) { 7552 panic("attempt to unregister non-existent " 7553 "dtrace provider %p\n", (void *)id); 7554 } 7555 7556 prev->dtpv_next = old->dtpv_next; 7557 } 7558 7559 if (!self) { 7560 mutex_exit(&dtrace_lock); 7561 mutex_exit(&mod_lock); 7562 mutex_exit(&dtrace_provider_lock); 7563 } 7564 7565 kmem_free(old->dtpv_name, strlen(old->dtpv_name) + 1); 7566 kmem_free(old, sizeof (dtrace_provider_t)); 7567 7568 return (0); 7569} 7570 7571/* 7572 * Invalidate the specified provider. All subsequent probe lookups for the 7573 * specified provider will fail, but its probes will not be removed. 7574 */ 7575void 7576dtrace_invalidate(dtrace_provider_id_t id) 7577{ 7578 dtrace_provider_t *pvp = (dtrace_provider_t *)id; 7579 7580 ASSERT(pvp->dtpv_pops.dtps_enable != 7581 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7582 7583 mutex_enter(&dtrace_provider_lock); 7584 mutex_enter(&dtrace_lock); 7585 7586 pvp->dtpv_defunct = 1; 7587 7588 mutex_exit(&dtrace_lock); 7589 mutex_exit(&dtrace_provider_lock); 7590} 7591 7592/* 7593 * Indicate whether or not DTrace has attached. 7594 */ 7595int 7596dtrace_attached(void) 7597{ 7598 /* 7599 * dtrace_provider will be non-NULL iff the DTrace driver has 7600 * attached. (It's non-NULL because DTrace is always itself a 7601 * provider.) 7602 */ 7603 return (dtrace_provider != NULL); 7604} 7605 7606/* 7607 * Remove all the unenabled probes for the given provider. This function is 7608 * not unlike dtrace_unregister(), except that it doesn't remove the provider 7609 * -- just as many of its associated probes as it can. 7610 */ 7611int 7612dtrace_condense(dtrace_provider_id_t id) 7613{ 7614 dtrace_provider_t *prov = (dtrace_provider_t *)id; 7615 int i; 7616 dtrace_probe_t *probe; 7617 7618 /* 7619 * Make sure this isn't the dtrace provider itself. 7620 */ 7621 ASSERT(prov->dtpv_pops.dtps_enable != 7622 (void (*)(void *, dtrace_id_t, void *))dtrace_nullop); 7623 7624 mutex_enter(&dtrace_provider_lock); 7625 mutex_enter(&dtrace_lock); 7626 7627 /* 7628 * Attempt to destroy the probes associated with this provider. 7629 */ 7630 for (i = 0; i < dtrace_nprobes; i++) { 7631 if ((probe = dtrace_probes[i]) == NULL) 7632 continue; 7633 7634 if (probe->dtpr_provider != prov) 7635 continue; 7636 7637 if (probe->dtpr_ecb != NULL) 7638 continue; 7639 7640 dtrace_probes[i] = NULL; 7641 7642 dtrace_hash_remove(dtrace_bymod, probe); 7643 dtrace_hash_remove(dtrace_byfunc, probe); 7644 dtrace_hash_remove(dtrace_byname, probe); 7645 7646 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, i + 1, 7647 probe->dtpr_arg); 7648 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 7649 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 7650 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 7651 kmem_free(probe, sizeof (dtrace_probe_t)); 7652#if defined(sun) 7653 vmem_free(dtrace_arena, (void *)((uintptr_t)i + 1), 1); 7654#else 7655 free_unr(dtrace_arena, i + 1); 7656#endif 7657 } 7658 7659 mutex_exit(&dtrace_lock); 7660 mutex_exit(&dtrace_provider_lock); 7661 7662 return (0); 7663} 7664 7665/* 7666 * DTrace Probe Management Functions 7667 * 7668 * The functions in this section perform the DTrace probe management, 7669 * including functions to create probes, look-up probes, and call into the 7670 * providers to request that probes be provided. Some of these functions are 7671 * in the Provider-to-Framework API; these functions can be identified by the 7672 * fact that they are not declared "static". 7673 */ 7674 7675/* 7676 * Create a probe with the specified module name, function name, and name. 7677 */ 7678dtrace_id_t 7679dtrace_probe_create(dtrace_provider_id_t prov, const char *mod, 7680 const char *func, const char *name, int aframes, void *arg) 7681{ 7682 dtrace_probe_t *probe, **probes; 7683 dtrace_provider_t *provider = (dtrace_provider_t *)prov; 7684 dtrace_id_t id; 7685 7686 if (provider == dtrace_provider) { 7687 ASSERT(MUTEX_HELD(&dtrace_lock)); 7688 } else { 7689 mutex_enter(&dtrace_lock); 7690 } 7691 7692#if defined(sun) 7693 id = (dtrace_id_t)(uintptr_t)vmem_alloc(dtrace_arena, 1, 7694 VM_BESTFIT | VM_SLEEP); 7695#else 7696 id = alloc_unr(dtrace_arena); 7697#endif 7698 probe = kmem_zalloc(sizeof (dtrace_probe_t), KM_SLEEP); 7699 7700 probe->dtpr_id = id; 7701 probe->dtpr_gen = dtrace_probegen++; 7702 probe->dtpr_mod = dtrace_strdup(mod); 7703 probe->dtpr_func = dtrace_strdup(func); 7704 probe->dtpr_name = dtrace_strdup(name); 7705 probe->dtpr_arg = arg; 7706 probe->dtpr_aframes = aframes; 7707 probe->dtpr_provider = provider; 7708 7709 dtrace_hash_add(dtrace_bymod, probe); 7710 dtrace_hash_add(dtrace_byfunc, probe); 7711 dtrace_hash_add(dtrace_byname, probe); 7712 7713 if (id - 1 >= dtrace_nprobes) { 7714 size_t osize = dtrace_nprobes * sizeof (dtrace_probe_t *); 7715 size_t nsize = osize << 1; 7716 7717 if (nsize == 0) { 7718 ASSERT(osize == 0); 7719 ASSERT(dtrace_probes == NULL); 7720 nsize = sizeof (dtrace_probe_t *); 7721 } 7722 7723 probes = kmem_zalloc(nsize, KM_SLEEP); 7724 7725 if (dtrace_probes == NULL) { 7726 ASSERT(osize == 0); 7727 dtrace_probes = probes; 7728 dtrace_nprobes = 1; 7729 } else { 7730 dtrace_probe_t **oprobes = dtrace_probes; 7731 7732 bcopy(oprobes, probes, osize); 7733 dtrace_membar_producer(); 7734 dtrace_probes = probes; 7735 7736 dtrace_sync(); 7737 7738 /* 7739 * All CPUs are now seeing the new probes array; we can 7740 * safely free the old array. 7741 */ 7742 kmem_free(oprobes, osize); 7743 dtrace_nprobes <<= 1; 7744 } 7745 7746 ASSERT(id - 1 < dtrace_nprobes); 7747 } 7748 7749 ASSERT(dtrace_probes[id - 1] == NULL); 7750 dtrace_probes[id - 1] = probe; 7751 7752 if (provider != dtrace_provider) 7753 mutex_exit(&dtrace_lock); 7754 7755 return (id); 7756} 7757 7758static dtrace_probe_t * 7759dtrace_probe_lookup_id(dtrace_id_t id) 7760{ 7761 ASSERT(MUTEX_HELD(&dtrace_lock)); 7762 7763 if (id == 0 || id > dtrace_nprobes) 7764 return (NULL); 7765 7766 return (dtrace_probes[id - 1]); 7767} 7768 7769static int 7770dtrace_probe_lookup_match(dtrace_probe_t *probe, void *arg) 7771{ 7772 *((dtrace_id_t *)arg) = probe->dtpr_id; 7773 7774 return (DTRACE_MATCH_DONE); 7775} 7776 7777/* 7778 * Look up a probe based on provider and one or more of module name, function 7779 * name and probe name. 7780 */ 7781dtrace_id_t 7782dtrace_probe_lookup(dtrace_provider_id_t prid, char *mod, 7783 char *func, char *name) 7784{ 7785 dtrace_probekey_t pkey; 7786 dtrace_id_t id; 7787 int match; 7788 7789 pkey.dtpk_prov = ((dtrace_provider_t *)prid)->dtpv_name; 7790 pkey.dtpk_pmatch = &dtrace_match_string; 7791 pkey.dtpk_mod = mod; 7792 pkey.dtpk_mmatch = mod ? &dtrace_match_string : &dtrace_match_nul; 7793 pkey.dtpk_func = func; 7794 pkey.dtpk_fmatch = func ? &dtrace_match_string : &dtrace_match_nul; 7795 pkey.dtpk_name = name; 7796 pkey.dtpk_nmatch = name ? &dtrace_match_string : &dtrace_match_nul; 7797 pkey.dtpk_id = DTRACE_IDNONE; 7798 7799 mutex_enter(&dtrace_lock); 7800 match = dtrace_match(&pkey, DTRACE_PRIV_ALL, 0, 0, 7801 dtrace_probe_lookup_match, &id); 7802 mutex_exit(&dtrace_lock); 7803 7804 ASSERT(match == 1 || match == 0); 7805 return (match ? id : 0); 7806} 7807 7808/* 7809 * Returns the probe argument associated with the specified probe. 7810 */ 7811void * 7812dtrace_probe_arg(dtrace_provider_id_t id, dtrace_id_t pid) 7813{ 7814 dtrace_probe_t *probe; 7815 void *rval = NULL; 7816 7817 mutex_enter(&dtrace_lock); 7818 7819 if ((probe = dtrace_probe_lookup_id(pid)) != NULL && 7820 probe->dtpr_provider == (dtrace_provider_t *)id) 7821 rval = probe->dtpr_arg; 7822 7823 mutex_exit(&dtrace_lock); 7824 7825 return (rval); 7826} 7827 7828/* 7829 * Copy a probe into a probe description. 7830 */ 7831static void 7832dtrace_probe_description(const dtrace_probe_t *prp, dtrace_probedesc_t *pdp) 7833{ 7834 bzero(pdp, sizeof (dtrace_probedesc_t)); 7835 pdp->dtpd_id = prp->dtpr_id; 7836 7837 (void) strncpy(pdp->dtpd_provider, 7838 prp->dtpr_provider->dtpv_name, DTRACE_PROVNAMELEN - 1); 7839 7840 (void) strncpy(pdp->dtpd_mod, prp->dtpr_mod, DTRACE_MODNAMELEN - 1); 7841 (void) strncpy(pdp->dtpd_func, prp->dtpr_func, DTRACE_FUNCNAMELEN - 1); 7842 (void) strncpy(pdp->dtpd_name, prp->dtpr_name, DTRACE_NAMELEN - 1); 7843} 7844 7845#if !defined(sun) 7846static int 7847dtrace_probe_provide_cb(linker_file_t lf, void *arg) 7848{ 7849 dtrace_provider_t *prv = (dtrace_provider_t *) arg; 7850 7851 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, lf); 7852 7853 return(0); 7854} 7855#endif 7856 7857 7858/* 7859 * Called to indicate that a probe -- or probes -- should be provided by a 7860 * specfied provider. If the specified description is NULL, the provider will 7861 * be told to provide all of its probes. (This is done whenever a new 7862 * consumer comes along, or whenever a retained enabling is to be matched.) If 7863 * the specified description is non-NULL, the provider is given the 7864 * opportunity to dynamically provide the specified probe, allowing providers 7865 * to support the creation of probes on-the-fly. (So-called _autocreated_ 7866 * probes.) If the provider is NULL, the operations will be applied to all 7867 * providers; if the provider is non-NULL the operations will only be applied 7868 * to the specified provider. The dtrace_provider_lock must be held, and the 7869 * dtrace_lock must _not_ be held -- the provider's dtps_provide() operation 7870 * will need to grab the dtrace_lock when it reenters the framework through 7871 * dtrace_probe_lookup(), dtrace_probe_create(), etc. 7872 */ 7873static void 7874dtrace_probe_provide(dtrace_probedesc_t *desc, dtrace_provider_t *prv) 7875{ 7876#if defined(sun) 7877 modctl_t *ctl; 7878#endif 7879 int all = 0; 7880 7881 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 7882 7883 if (prv == NULL) { 7884 all = 1; 7885 prv = dtrace_provider; 7886 } 7887 7888 do { 7889 /* 7890 * First, call the blanket provide operation. 7891 */ 7892 prv->dtpv_pops.dtps_provide(prv->dtpv_arg, desc); 7893 7894 /* 7895 * Now call the per-module provide operation. We will grab 7896 * mod_lock to prevent the list from being modified. Note 7897 * that this also prevents the mod_busy bits from changing. 7898 * (mod_busy can only be changed with mod_lock held.) 7899 */ 7900 mutex_enter(&mod_lock); 7901 7902#if defined(sun) 7903 ctl = &modules; 7904 do { 7905 if (ctl->mod_busy || ctl->mod_mp == NULL) 7906 continue; 7907 7908 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 7909 7910 } while ((ctl = ctl->mod_next) != &modules); 7911#else 7912 (void) linker_file_foreach(dtrace_probe_provide_cb, prv); 7913#endif 7914 7915 mutex_exit(&mod_lock); 7916 } while (all && (prv = prv->dtpv_next) != NULL); 7917} 7918 7919#if defined(sun) 7920/* 7921 * Iterate over each probe, and call the Framework-to-Provider API function 7922 * denoted by offs. 7923 */ 7924static void 7925dtrace_probe_foreach(uintptr_t offs) 7926{ 7927 dtrace_provider_t *prov; 7928 void (*func)(void *, dtrace_id_t, void *); 7929 dtrace_probe_t *probe; 7930 dtrace_icookie_t cookie; 7931 int i; 7932 7933 /* 7934 * We disable interrupts to walk through the probe array. This is 7935 * safe -- the dtrace_sync() in dtrace_unregister() assures that we 7936 * won't see stale data. 7937 */ 7938 cookie = dtrace_interrupt_disable(); 7939 7940 for (i = 0; i < dtrace_nprobes; i++) { 7941 if ((probe = dtrace_probes[i]) == NULL) 7942 continue; 7943 7944 if (probe->dtpr_ecb == NULL) { 7945 /* 7946 * This probe isn't enabled -- don't call the function. 7947 */ 7948 continue; 7949 } 7950 7951 prov = probe->dtpr_provider; 7952 func = *((void(**)(void *, dtrace_id_t, void *)) 7953 ((uintptr_t)&prov->dtpv_pops + offs)); 7954 7955 func(prov->dtpv_arg, i + 1, probe->dtpr_arg); 7956 } 7957 7958 dtrace_interrupt_enable(cookie); 7959} 7960#endif 7961 7962static int 7963dtrace_probe_enable(dtrace_probedesc_t *desc, dtrace_enabling_t *enab) 7964{ 7965 dtrace_probekey_t pkey; 7966 uint32_t priv; 7967 uid_t uid; 7968 zoneid_t zoneid; 7969 7970 ASSERT(MUTEX_HELD(&dtrace_lock)); 7971 dtrace_ecb_create_cache = NULL; 7972 7973 if (desc == NULL) { 7974 /* 7975 * If we're passed a NULL description, we're being asked to 7976 * create an ECB with a NULL probe. 7977 */ 7978 (void) dtrace_ecb_create_enable(NULL, enab); 7979 return (0); 7980 } 7981 7982 dtrace_probekey(desc, &pkey); 7983 dtrace_cred2priv(enab->dten_vstate->dtvs_state->dts_cred.dcr_cred, 7984 &priv, &uid, &zoneid); 7985 7986 return (dtrace_match(&pkey, priv, uid, zoneid, dtrace_ecb_create_enable, 7987 enab)); 7988} 7989 7990/* 7991 * DTrace Helper Provider Functions 7992 */ 7993static void 7994dtrace_dofattr2attr(dtrace_attribute_t *attr, const dof_attr_t dofattr) 7995{ 7996 attr->dtat_name = DOF_ATTR_NAME(dofattr); 7997 attr->dtat_data = DOF_ATTR_DATA(dofattr); 7998 attr->dtat_class = DOF_ATTR_CLASS(dofattr); 7999} 8000 8001static void 8002dtrace_dofprov2hprov(dtrace_helper_provdesc_t *hprov, 8003 const dof_provider_t *dofprov, char *strtab) 8004{ 8005 hprov->dthpv_provname = strtab + dofprov->dofpv_name; 8006 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_provider, 8007 dofprov->dofpv_provattr); 8008 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_mod, 8009 dofprov->dofpv_modattr); 8010 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_func, 8011 dofprov->dofpv_funcattr); 8012 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_name, 8013 dofprov->dofpv_nameattr); 8014 dtrace_dofattr2attr(&hprov->dthpv_pattr.dtpa_args, 8015 dofprov->dofpv_argsattr); 8016} 8017 8018static void 8019dtrace_helper_provide_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8020{ 8021 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8022 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8023 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 8024 dof_provider_t *provider; 8025 dof_probe_t *probe; 8026 uint32_t *off, *enoff; 8027 uint8_t *arg; 8028 char *strtab; 8029 uint_t i, nprobes; 8030 dtrace_helper_provdesc_t dhpv; 8031 dtrace_helper_probedesc_t dhpb; 8032 dtrace_meta_t *meta = dtrace_meta_pid; 8033 dtrace_mops_t *mops = &meta->dtm_mops; 8034 void *parg; 8035 8036 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8037 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8038 provider->dofpv_strtab * dof->dofh_secsize); 8039 prb_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8040 provider->dofpv_probes * dof->dofh_secsize); 8041 arg_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8042 provider->dofpv_prargs * dof->dofh_secsize); 8043 off_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8044 provider->dofpv_proffs * dof->dofh_secsize); 8045 8046 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8047 off = (uint32_t *)(uintptr_t)(daddr + off_sec->dofs_offset); 8048 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 8049 enoff = NULL; 8050 8051 /* 8052 * See dtrace_helper_provider_validate(). 8053 */ 8054 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 8055 provider->dofpv_prenoffs != DOF_SECT_NONE) { 8056 enoff_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8057 provider->dofpv_prenoffs * dof->dofh_secsize); 8058 enoff = (uint32_t *)(uintptr_t)(daddr + enoff_sec->dofs_offset); 8059 } 8060 8061 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 8062 8063 /* 8064 * Create the provider. 8065 */ 8066 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8067 8068 if ((parg = mops->dtms_provide_pid(meta->dtm_arg, &dhpv, pid)) == NULL) 8069 return; 8070 8071 meta->dtm_count++; 8072 8073 /* 8074 * Create the probes. 8075 */ 8076 for (i = 0; i < nprobes; i++) { 8077 probe = (dof_probe_t *)(uintptr_t)(daddr + 8078 prb_sec->dofs_offset + i * prb_sec->dofs_entsize); 8079 8080 dhpb.dthpb_mod = dhp->dofhp_mod; 8081 dhpb.dthpb_func = strtab + probe->dofpr_func; 8082 dhpb.dthpb_name = strtab + probe->dofpr_name; 8083 dhpb.dthpb_base = probe->dofpr_addr; 8084 dhpb.dthpb_offs = off + probe->dofpr_offidx; 8085 dhpb.dthpb_noffs = probe->dofpr_noffs; 8086 if (enoff != NULL) { 8087 dhpb.dthpb_enoffs = enoff + probe->dofpr_enoffidx; 8088 dhpb.dthpb_nenoffs = probe->dofpr_nenoffs; 8089 } else { 8090 dhpb.dthpb_enoffs = NULL; 8091 dhpb.dthpb_nenoffs = 0; 8092 } 8093 dhpb.dthpb_args = arg + probe->dofpr_argidx; 8094 dhpb.dthpb_nargc = probe->dofpr_nargc; 8095 dhpb.dthpb_xargc = probe->dofpr_xargc; 8096 dhpb.dthpb_ntypes = strtab + probe->dofpr_nargv; 8097 dhpb.dthpb_xtypes = strtab + probe->dofpr_xargv; 8098 8099 mops->dtms_create_probe(meta->dtm_arg, parg, &dhpb); 8100 } 8101} 8102 8103static void 8104dtrace_helper_provide(dof_helper_t *dhp, pid_t pid) 8105{ 8106 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8107 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8108 int i; 8109 8110 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8111 8112 for (i = 0; i < dof->dofh_secnum; i++) { 8113 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8114 dof->dofh_secoff + i * dof->dofh_secsize); 8115 8116 if (sec->dofs_type != DOF_SECT_PROVIDER) 8117 continue; 8118 8119 dtrace_helper_provide_one(dhp, sec, pid); 8120 } 8121 8122 /* 8123 * We may have just created probes, so we must now rematch against 8124 * any retained enablings. Note that this call will acquire both 8125 * cpu_lock and dtrace_lock; the fact that we are holding 8126 * dtrace_meta_lock now is what defines the ordering with respect to 8127 * these three locks. 8128 */ 8129 dtrace_enabling_matchall(); 8130} 8131 8132#if defined(sun) 8133static void 8134dtrace_helper_provider_remove_one(dof_helper_t *dhp, dof_sec_t *sec, pid_t pid) 8135{ 8136 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8137 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8138 dof_sec_t *str_sec; 8139 dof_provider_t *provider; 8140 char *strtab; 8141 dtrace_helper_provdesc_t dhpv; 8142 dtrace_meta_t *meta = dtrace_meta_pid; 8143 dtrace_mops_t *mops = &meta->dtm_mops; 8144 8145 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 8146 str_sec = (dof_sec_t *)(uintptr_t)(daddr + dof->dofh_secoff + 8147 provider->dofpv_strtab * dof->dofh_secsize); 8148 8149 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 8150 8151 /* 8152 * Create the provider. 8153 */ 8154 dtrace_dofprov2hprov(&dhpv, provider, strtab); 8155 8156 mops->dtms_remove_pid(meta->dtm_arg, &dhpv, pid); 8157 8158 meta->dtm_count--; 8159} 8160 8161static void 8162dtrace_helper_provider_remove(dof_helper_t *dhp, pid_t pid) 8163{ 8164 uintptr_t daddr = (uintptr_t)dhp->dofhp_dof; 8165 dof_hdr_t *dof = (dof_hdr_t *)daddr; 8166 int i; 8167 8168 ASSERT(MUTEX_HELD(&dtrace_meta_lock)); 8169 8170 for (i = 0; i < dof->dofh_secnum; i++) { 8171 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 8172 dof->dofh_secoff + i * dof->dofh_secsize); 8173 8174 if (sec->dofs_type != DOF_SECT_PROVIDER) 8175 continue; 8176 8177 dtrace_helper_provider_remove_one(dhp, sec, pid); 8178 } 8179} 8180#endif 8181 8182/* 8183 * DTrace Meta Provider-to-Framework API Functions 8184 * 8185 * These functions implement the Meta Provider-to-Framework API, as described 8186 * in <sys/dtrace.h>. 8187 */ 8188int 8189dtrace_meta_register(const char *name, const dtrace_mops_t *mops, void *arg, 8190 dtrace_meta_provider_id_t *idp) 8191{ 8192 dtrace_meta_t *meta; 8193 dtrace_helpers_t *help, *next; 8194 int i; 8195 8196 *idp = DTRACE_METAPROVNONE; 8197 8198 /* 8199 * We strictly don't need the name, but we hold onto it for 8200 * debuggability. All hail error queues! 8201 */ 8202 if (name == NULL) { 8203 cmn_err(CE_WARN, "failed to register meta-provider: " 8204 "invalid name"); 8205 return (EINVAL); 8206 } 8207 8208 if (mops == NULL || 8209 mops->dtms_create_probe == NULL || 8210 mops->dtms_provide_pid == NULL || 8211 mops->dtms_remove_pid == NULL) { 8212 cmn_err(CE_WARN, "failed to register meta-register %s: " 8213 "invalid ops", name); 8214 return (EINVAL); 8215 } 8216 8217 meta = kmem_zalloc(sizeof (dtrace_meta_t), KM_SLEEP); 8218 meta->dtm_mops = *mops; 8219 meta->dtm_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 8220 (void) strcpy(meta->dtm_name, name); 8221 meta->dtm_arg = arg; 8222 8223 mutex_enter(&dtrace_meta_lock); 8224 mutex_enter(&dtrace_lock); 8225 8226 if (dtrace_meta_pid != NULL) { 8227 mutex_exit(&dtrace_lock); 8228 mutex_exit(&dtrace_meta_lock); 8229 cmn_err(CE_WARN, "failed to register meta-register %s: " 8230 "user-land meta-provider exists", name); 8231 kmem_free(meta->dtm_name, strlen(meta->dtm_name) + 1); 8232 kmem_free(meta, sizeof (dtrace_meta_t)); 8233 return (EINVAL); 8234 } 8235 8236 dtrace_meta_pid = meta; 8237 *idp = (dtrace_meta_provider_id_t)meta; 8238 8239 /* 8240 * If there are providers and probes ready to go, pass them 8241 * off to the new meta provider now. 8242 */ 8243 8244 help = dtrace_deferred_pid; 8245 dtrace_deferred_pid = NULL; 8246 8247 mutex_exit(&dtrace_lock); 8248 8249 while (help != NULL) { 8250 for (i = 0; i < help->dthps_nprovs; i++) { 8251 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 8252 help->dthps_pid); 8253 } 8254 8255 next = help->dthps_next; 8256 help->dthps_next = NULL; 8257 help->dthps_prev = NULL; 8258 help->dthps_deferred = 0; 8259 help = next; 8260 } 8261 8262 mutex_exit(&dtrace_meta_lock); 8263 8264 return (0); 8265} 8266 8267int 8268dtrace_meta_unregister(dtrace_meta_provider_id_t id) 8269{ 8270 dtrace_meta_t **pp, *old = (dtrace_meta_t *)id; 8271 8272 mutex_enter(&dtrace_meta_lock); 8273 mutex_enter(&dtrace_lock); 8274 8275 if (old == dtrace_meta_pid) { 8276 pp = &dtrace_meta_pid; 8277 } else { 8278 panic("attempt to unregister non-existent " 8279 "dtrace meta-provider %p\n", (void *)old); 8280 } 8281 8282 if (old->dtm_count != 0) { 8283 mutex_exit(&dtrace_lock); 8284 mutex_exit(&dtrace_meta_lock); 8285 return (EBUSY); 8286 } 8287 8288 *pp = NULL; 8289 8290 mutex_exit(&dtrace_lock); 8291 mutex_exit(&dtrace_meta_lock); 8292 8293 kmem_free(old->dtm_name, strlen(old->dtm_name) + 1); 8294 kmem_free(old, sizeof (dtrace_meta_t)); 8295 8296 return (0); 8297} 8298 8299 8300/* 8301 * DTrace DIF Object Functions 8302 */ 8303static int 8304dtrace_difo_err(uint_t pc, const char *format, ...) 8305{ 8306 if (dtrace_err_verbose) { 8307 va_list alist; 8308 8309 (void) uprintf("dtrace DIF object error: [%u]: ", pc); 8310 va_start(alist, format); 8311 (void) vuprintf(format, alist); 8312 va_end(alist); 8313 } 8314 8315#ifdef DTRACE_ERRDEBUG 8316 dtrace_errdebug(format); 8317#endif 8318 return (1); 8319} 8320 8321/* 8322 * Validate a DTrace DIF object by checking the IR instructions. The following 8323 * rules are currently enforced by dtrace_difo_validate(): 8324 * 8325 * 1. Each instruction must have a valid opcode 8326 * 2. Each register, string, variable, or subroutine reference must be valid 8327 * 3. No instruction can modify register %r0 (must be zero) 8328 * 4. All instruction reserved bits must be set to zero 8329 * 5. The last instruction must be a "ret" instruction 8330 * 6. All branch targets must reference a valid instruction _after_ the branch 8331 */ 8332static int 8333dtrace_difo_validate(dtrace_difo_t *dp, dtrace_vstate_t *vstate, uint_t nregs, 8334 cred_t *cr) 8335{ 8336 int err = 0, i; 8337 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8338 int kcheckload; 8339 uint_t pc; 8340 8341 kcheckload = cr == NULL || 8342 (vstate->dtvs_state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) == 0; 8343 8344 dp->dtdo_destructive = 0; 8345 8346 for (pc = 0; pc < dp->dtdo_len && err == 0; pc++) { 8347 dif_instr_t instr = dp->dtdo_buf[pc]; 8348 8349 uint_t r1 = DIF_INSTR_R1(instr); 8350 uint_t r2 = DIF_INSTR_R2(instr); 8351 uint_t rd = DIF_INSTR_RD(instr); 8352 uint_t rs = DIF_INSTR_RS(instr); 8353 uint_t label = DIF_INSTR_LABEL(instr); 8354 uint_t v = DIF_INSTR_VAR(instr); 8355 uint_t subr = DIF_INSTR_SUBR(instr); 8356 uint_t type = DIF_INSTR_TYPE(instr); 8357 uint_t op = DIF_INSTR_OP(instr); 8358 8359 switch (op) { 8360 case DIF_OP_OR: 8361 case DIF_OP_XOR: 8362 case DIF_OP_AND: 8363 case DIF_OP_SLL: 8364 case DIF_OP_SRL: 8365 case DIF_OP_SRA: 8366 case DIF_OP_SUB: 8367 case DIF_OP_ADD: 8368 case DIF_OP_MUL: 8369 case DIF_OP_SDIV: 8370 case DIF_OP_UDIV: 8371 case DIF_OP_SREM: 8372 case DIF_OP_UREM: 8373 case DIF_OP_COPYS: 8374 if (r1 >= nregs) 8375 err += efunc(pc, "invalid register %u\n", r1); 8376 if (r2 >= nregs) 8377 err += efunc(pc, "invalid register %u\n", r2); 8378 if (rd >= nregs) 8379 err += efunc(pc, "invalid register %u\n", rd); 8380 if (rd == 0) 8381 err += efunc(pc, "cannot write to %r0\n"); 8382 break; 8383 case DIF_OP_NOT: 8384 case DIF_OP_MOV: 8385 case DIF_OP_ALLOCS: 8386 if (r1 >= nregs) 8387 err += efunc(pc, "invalid register %u\n", r1); 8388 if (r2 != 0) 8389 err += efunc(pc, "non-zero reserved bits\n"); 8390 if (rd >= nregs) 8391 err += efunc(pc, "invalid register %u\n", rd); 8392 if (rd == 0) 8393 err += efunc(pc, "cannot write to %r0\n"); 8394 break; 8395 case DIF_OP_LDSB: 8396 case DIF_OP_LDSH: 8397 case DIF_OP_LDSW: 8398 case DIF_OP_LDUB: 8399 case DIF_OP_LDUH: 8400 case DIF_OP_LDUW: 8401 case DIF_OP_LDX: 8402 if (r1 >= nregs) 8403 err += efunc(pc, "invalid register %u\n", r1); 8404 if (r2 != 0) 8405 err += efunc(pc, "non-zero reserved bits\n"); 8406 if (rd >= nregs) 8407 err += efunc(pc, "invalid register %u\n", rd); 8408 if (rd == 0) 8409 err += efunc(pc, "cannot write to %r0\n"); 8410 if (kcheckload) 8411 dp->dtdo_buf[pc] = DIF_INSTR_LOAD(op + 8412 DIF_OP_RLDSB - DIF_OP_LDSB, r1, rd); 8413 break; 8414 case DIF_OP_RLDSB: 8415 case DIF_OP_RLDSH: 8416 case DIF_OP_RLDSW: 8417 case DIF_OP_RLDUB: 8418 case DIF_OP_RLDUH: 8419 case DIF_OP_RLDUW: 8420 case DIF_OP_RLDX: 8421 if (r1 >= nregs) 8422 err += efunc(pc, "invalid register %u\n", r1); 8423 if (r2 != 0) 8424 err += efunc(pc, "non-zero reserved bits\n"); 8425 if (rd >= nregs) 8426 err += efunc(pc, "invalid register %u\n", rd); 8427 if (rd == 0) 8428 err += efunc(pc, "cannot write to %r0\n"); 8429 break; 8430 case DIF_OP_ULDSB: 8431 case DIF_OP_ULDSH: 8432 case DIF_OP_ULDSW: 8433 case DIF_OP_ULDUB: 8434 case DIF_OP_ULDUH: 8435 case DIF_OP_ULDUW: 8436 case DIF_OP_ULDX: 8437 if (r1 >= nregs) 8438 err += efunc(pc, "invalid register %u\n", r1); 8439 if (r2 != 0) 8440 err += efunc(pc, "non-zero reserved bits\n"); 8441 if (rd >= nregs) 8442 err += efunc(pc, "invalid register %u\n", rd); 8443 if (rd == 0) 8444 err += efunc(pc, "cannot write to %r0\n"); 8445 break; 8446 case DIF_OP_STB: 8447 case DIF_OP_STH: 8448 case DIF_OP_STW: 8449 case DIF_OP_STX: 8450 if (r1 >= nregs) 8451 err += efunc(pc, "invalid register %u\n", r1); 8452 if (r2 != 0) 8453 err += efunc(pc, "non-zero reserved bits\n"); 8454 if (rd >= nregs) 8455 err += efunc(pc, "invalid register %u\n", rd); 8456 if (rd == 0) 8457 err += efunc(pc, "cannot write to 0 address\n"); 8458 break; 8459 case DIF_OP_CMP: 8460 case DIF_OP_SCMP: 8461 if (r1 >= nregs) 8462 err += efunc(pc, "invalid register %u\n", r1); 8463 if (r2 >= nregs) 8464 err += efunc(pc, "invalid register %u\n", r2); 8465 if (rd != 0) 8466 err += efunc(pc, "non-zero reserved bits\n"); 8467 break; 8468 case DIF_OP_TST: 8469 if (r1 >= nregs) 8470 err += efunc(pc, "invalid register %u\n", r1); 8471 if (r2 != 0 || rd != 0) 8472 err += efunc(pc, "non-zero reserved bits\n"); 8473 break; 8474 case DIF_OP_BA: 8475 case DIF_OP_BE: 8476 case DIF_OP_BNE: 8477 case DIF_OP_BG: 8478 case DIF_OP_BGU: 8479 case DIF_OP_BGE: 8480 case DIF_OP_BGEU: 8481 case DIF_OP_BL: 8482 case DIF_OP_BLU: 8483 case DIF_OP_BLE: 8484 case DIF_OP_BLEU: 8485 if (label >= dp->dtdo_len) { 8486 err += efunc(pc, "invalid branch target %u\n", 8487 label); 8488 } 8489 if (label <= pc) { 8490 err += efunc(pc, "backward branch to %u\n", 8491 label); 8492 } 8493 break; 8494 case DIF_OP_RET: 8495 if (r1 != 0 || r2 != 0) 8496 err += efunc(pc, "non-zero reserved bits\n"); 8497 if (rd >= nregs) 8498 err += efunc(pc, "invalid register %u\n", rd); 8499 break; 8500 case DIF_OP_NOP: 8501 case DIF_OP_POPTS: 8502 case DIF_OP_FLUSHTS: 8503 if (r1 != 0 || r2 != 0 || rd != 0) 8504 err += efunc(pc, "non-zero reserved bits\n"); 8505 break; 8506 case DIF_OP_SETX: 8507 if (DIF_INSTR_INTEGER(instr) >= dp->dtdo_intlen) { 8508 err += efunc(pc, "invalid integer ref %u\n", 8509 DIF_INSTR_INTEGER(instr)); 8510 } 8511 if (rd >= nregs) 8512 err += efunc(pc, "invalid register %u\n", rd); 8513 if (rd == 0) 8514 err += efunc(pc, "cannot write to %r0\n"); 8515 break; 8516 case DIF_OP_SETS: 8517 if (DIF_INSTR_STRING(instr) >= dp->dtdo_strlen) { 8518 err += efunc(pc, "invalid string ref %u\n", 8519 DIF_INSTR_STRING(instr)); 8520 } 8521 if (rd >= nregs) 8522 err += efunc(pc, "invalid register %u\n", rd); 8523 if (rd == 0) 8524 err += efunc(pc, "cannot write to %r0\n"); 8525 break; 8526 case DIF_OP_LDGA: 8527 case DIF_OP_LDTA: 8528 if (r1 > DIF_VAR_ARRAY_MAX) 8529 err += efunc(pc, "invalid array %u\n", r1); 8530 if (r2 >= nregs) 8531 err += efunc(pc, "invalid register %u\n", r2); 8532 if (rd >= nregs) 8533 err += efunc(pc, "invalid register %u\n", rd); 8534 if (rd == 0) 8535 err += efunc(pc, "cannot write to %r0\n"); 8536 break; 8537 case DIF_OP_LDGS: 8538 case DIF_OP_LDTS: 8539 case DIF_OP_LDLS: 8540 case DIF_OP_LDGAA: 8541 case DIF_OP_LDTAA: 8542 if (v < DIF_VAR_OTHER_MIN || v > DIF_VAR_OTHER_MAX) 8543 err += efunc(pc, "invalid variable %u\n", v); 8544 if (rd >= nregs) 8545 err += efunc(pc, "invalid register %u\n", rd); 8546 if (rd == 0) 8547 err += efunc(pc, "cannot write to %r0\n"); 8548 break; 8549 case DIF_OP_STGS: 8550 case DIF_OP_STTS: 8551 case DIF_OP_STLS: 8552 case DIF_OP_STGAA: 8553 case DIF_OP_STTAA: 8554 if (v < DIF_VAR_OTHER_UBASE || v > DIF_VAR_OTHER_MAX) 8555 err += efunc(pc, "invalid variable %u\n", v); 8556 if (rs >= nregs) 8557 err += efunc(pc, "invalid register %u\n", rd); 8558 break; 8559 case DIF_OP_CALL: 8560 if (subr > DIF_SUBR_MAX) 8561 err += efunc(pc, "invalid subr %u\n", subr); 8562 if (rd >= nregs) 8563 err += efunc(pc, "invalid register %u\n", rd); 8564 if (rd == 0) 8565 err += efunc(pc, "cannot write to %r0\n"); 8566 8567 if (subr == DIF_SUBR_COPYOUT || 8568 subr == DIF_SUBR_COPYOUTSTR) { 8569 dp->dtdo_destructive = 1; 8570 } 8571 break; 8572 case DIF_OP_PUSHTR: 8573 if (type != DIF_TYPE_STRING && type != DIF_TYPE_CTF) 8574 err += efunc(pc, "invalid ref type %u\n", type); 8575 if (r2 >= nregs) 8576 err += efunc(pc, "invalid register %u\n", r2); 8577 if (rs >= nregs) 8578 err += efunc(pc, "invalid register %u\n", rs); 8579 break; 8580 case DIF_OP_PUSHTV: 8581 if (type != DIF_TYPE_CTF) 8582 err += efunc(pc, "invalid val type %u\n", type); 8583 if (r2 >= nregs) 8584 err += efunc(pc, "invalid register %u\n", r2); 8585 if (rs >= nregs) 8586 err += efunc(pc, "invalid register %u\n", rs); 8587 break; 8588 default: 8589 err += efunc(pc, "invalid opcode %u\n", 8590 DIF_INSTR_OP(instr)); 8591 } 8592 } 8593 8594 if (dp->dtdo_len != 0 && 8595 DIF_INSTR_OP(dp->dtdo_buf[dp->dtdo_len - 1]) != DIF_OP_RET) { 8596 err += efunc(dp->dtdo_len - 1, 8597 "expected 'ret' as last DIF instruction\n"); 8598 } 8599 8600 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) { 8601 /* 8602 * If we're not returning by reference, the size must be either 8603 * 0 or the size of one of the base types. 8604 */ 8605 switch (dp->dtdo_rtype.dtdt_size) { 8606 case 0: 8607 case sizeof (uint8_t): 8608 case sizeof (uint16_t): 8609 case sizeof (uint32_t): 8610 case sizeof (uint64_t): 8611 break; 8612 8613 default: 8614 err += efunc(dp->dtdo_len - 1, "bad return size"); 8615 } 8616 } 8617 8618 for (i = 0; i < dp->dtdo_varlen && err == 0; i++) { 8619 dtrace_difv_t *v = &dp->dtdo_vartab[i], *existing = NULL; 8620 dtrace_diftype_t *vt, *et; 8621 uint_t id, ndx; 8622 8623 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL && 8624 v->dtdv_scope != DIFV_SCOPE_THREAD && 8625 v->dtdv_scope != DIFV_SCOPE_LOCAL) { 8626 err += efunc(i, "unrecognized variable scope %d\n", 8627 v->dtdv_scope); 8628 break; 8629 } 8630 8631 if (v->dtdv_kind != DIFV_KIND_ARRAY && 8632 v->dtdv_kind != DIFV_KIND_SCALAR) { 8633 err += efunc(i, "unrecognized variable type %d\n", 8634 v->dtdv_kind); 8635 break; 8636 } 8637 8638 if ((id = v->dtdv_id) > DIF_VARIABLE_MAX) { 8639 err += efunc(i, "%d exceeds variable id limit\n", id); 8640 break; 8641 } 8642 8643 if (id < DIF_VAR_OTHER_UBASE) 8644 continue; 8645 8646 /* 8647 * For user-defined variables, we need to check that this 8648 * definition is identical to any previous definition that we 8649 * encountered. 8650 */ 8651 ndx = id - DIF_VAR_OTHER_UBASE; 8652 8653 switch (v->dtdv_scope) { 8654 case DIFV_SCOPE_GLOBAL: 8655 if (ndx < vstate->dtvs_nglobals) { 8656 dtrace_statvar_t *svar; 8657 8658 if ((svar = vstate->dtvs_globals[ndx]) != NULL) 8659 existing = &svar->dtsv_var; 8660 } 8661 8662 break; 8663 8664 case DIFV_SCOPE_THREAD: 8665 if (ndx < vstate->dtvs_ntlocals) 8666 existing = &vstate->dtvs_tlocals[ndx]; 8667 break; 8668 8669 case DIFV_SCOPE_LOCAL: 8670 if (ndx < vstate->dtvs_nlocals) { 8671 dtrace_statvar_t *svar; 8672 8673 if ((svar = vstate->dtvs_locals[ndx]) != NULL) 8674 existing = &svar->dtsv_var; 8675 } 8676 8677 break; 8678 } 8679 8680 vt = &v->dtdv_type; 8681 8682 if (vt->dtdt_flags & DIF_TF_BYREF) { 8683 if (vt->dtdt_size == 0) { 8684 err += efunc(i, "zero-sized variable\n"); 8685 break; 8686 } 8687 8688 if (v->dtdv_scope == DIFV_SCOPE_GLOBAL && 8689 vt->dtdt_size > dtrace_global_maxsize) { 8690 err += efunc(i, "oversized by-ref global\n"); 8691 break; 8692 } 8693 } 8694 8695 if (existing == NULL || existing->dtdv_id == 0) 8696 continue; 8697 8698 ASSERT(existing->dtdv_id == v->dtdv_id); 8699 ASSERT(existing->dtdv_scope == v->dtdv_scope); 8700 8701 if (existing->dtdv_kind != v->dtdv_kind) 8702 err += efunc(i, "%d changed variable kind\n", id); 8703 8704 et = &existing->dtdv_type; 8705 8706 if (vt->dtdt_flags != et->dtdt_flags) { 8707 err += efunc(i, "%d changed variable type flags\n", id); 8708 break; 8709 } 8710 8711 if (vt->dtdt_size != 0 && vt->dtdt_size != et->dtdt_size) { 8712 err += efunc(i, "%d changed variable type size\n", id); 8713 break; 8714 } 8715 } 8716 8717 return (err); 8718} 8719 8720#if defined(sun) 8721/* 8722 * Validate a DTrace DIF object that it is to be used as a helper. Helpers 8723 * are much more constrained than normal DIFOs. Specifically, they may 8724 * not: 8725 * 8726 * 1. Make calls to subroutines other than copyin(), copyinstr() or 8727 * miscellaneous string routines 8728 * 2. Access DTrace variables other than the args[] array, and the 8729 * curthread, pid, ppid, tid, execname, zonename, uid and gid variables. 8730 * 3. Have thread-local variables. 8731 * 4. Have dynamic variables. 8732 */ 8733static int 8734dtrace_difo_validate_helper(dtrace_difo_t *dp) 8735{ 8736 int (*efunc)(uint_t pc, const char *, ...) = dtrace_difo_err; 8737 int err = 0; 8738 uint_t pc; 8739 8740 for (pc = 0; pc < dp->dtdo_len; pc++) { 8741 dif_instr_t instr = dp->dtdo_buf[pc]; 8742 8743 uint_t v = DIF_INSTR_VAR(instr); 8744 uint_t subr = DIF_INSTR_SUBR(instr); 8745 uint_t op = DIF_INSTR_OP(instr); 8746 8747 switch (op) { 8748 case DIF_OP_OR: 8749 case DIF_OP_XOR: 8750 case DIF_OP_AND: 8751 case DIF_OP_SLL: 8752 case DIF_OP_SRL: 8753 case DIF_OP_SRA: 8754 case DIF_OP_SUB: 8755 case DIF_OP_ADD: 8756 case DIF_OP_MUL: 8757 case DIF_OP_SDIV: 8758 case DIF_OP_UDIV: 8759 case DIF_OP_SREM: 8760 case DIF_OP_UREM: 8761 case DIF_OP_COPYS: 8762 case DIF_OP_NOT: 8763 case DIF_OP_MOV: 8764 case DIF_OP_RLDSB: 8765 case DIF_OP_RLDSH: 8766 case DIF_OP_RLDSW: 8767 case DIF_OP_RLDUB: 8768 case DIF_OP_RLDUH: 8769 case DIF_OP_RLDUW: 8770 case DIF_OP_RLDX: 8771 case DIF_OP_ULDSB: 8772 case DIF_OP_ULDSH: 8773 case DIF_OP_ULDSW: 8774 case DIF_OP_ULDUB: 8775 case DIF_OP_ULDUH: 8776 case DIF_OP_ULDUW: 8777 case DIF_OP_ULDX: 8778 case DIF_OP_STB: 8779 case DIF_OP_STH: 8780 case DIF_OP_STW: 8781 case DIF_OP_STX: 8782 case DIF_OP_ALLOCS: 8783 case DIF_OP_CMP: 8784 case DIF_OP_SCMP: 8785 case DIF_OP_TST: 8786 case DIF_OP_BA: 8787 case DIF_OP_BE: 8788 case DIF_OP_BNE: 8789 case DIF_OP_BG: 8790 case DIF_OP_BGU: 8791 case DIF_OP_BGE: 8792 case DIF_OP_BGEU: 8793 case DIF_OP_BL: 8794 case DIF_OP_BLU: 8795 case DIF_OP_BLE: 8796 case DIF_OP_BLEU: 8797 case DIF_OP_RET: 8798 case DIF_OP_NOP: 8799 case DIF_OP_POPTS: 8800 case DIF_OP_FLUSHTS: 8801 case DIF_OP_SETX: 8802 case DIF_OP_SETS: 8803 case DIF_OP_LDGA: 8804 case DIF_OP_LDLS: 8805 case DIF_OP_STGS: 8806 case DIF_OP_STLS: 8807 case DIF_OP_PUSHTR: 8808 case DIF_OP_PUSHTV: 8809 break; 8810 8811 case DIF_OP_LDGS: 8812 if (v >= DIF_VAR_OTHER_UBASE) 8813 break; 8814 8815 if (v >= DIF_VAR_ARG0 && v <= DIF_VAR_ARG9) 8816 break; 8817 8818 if (v == DIF_VAR_CURTHREAD || v == DIF_VAR_PID || 8819 v == DIF_VAR_PPID || v == DIF_VAR_TID || 8820 v == DIF_VAR_EXECARGS || 8821 v == DIF_VAR_EXECNAME || v == DIF_VAR_ZONENAME || 8822 v == DIF_VAR_UID || v == DIF_VAR_GID) 8823 break; 8824 8825 err += efunc(pc, "illegal variable %u\n", v); 8826 break; 8827 8828 case DIF_OP_LDTA: 8829 case DIF_OP_LDTS: 8830 case DIF_OP_LDGAA: 8831 case DIF_OP_LDTAA: 8832 err += efunc(pc, "illegal dynamic variable load\n"); 8833 break; 8834 8835 case DIF_OP_STTS: 8836 case DIF_OP_STGAA: 8837 case DIF_OP_STTAA: 8838 err += efunc(pc, "illegal dynamic variable store\n"); 8839 break; 8840 8841 case DIF_OP_CALL: 8842 if (subr == DIF_SUBR_ALLOCA || 8843 subr == DIF_SUBR_BCOPY || 8844 subr == DIF_SUBR_COPYIN || 8845 subr == DIF_SUBR_COPYINTO || 8846 subr == DIF_SUBR_COPYINSTR || 8847 subr == DIF_SUBR_INDEX || 8848 subr == DIF_SUBR_INET_NTOA || 8849 subr == DIF_SUBR_INET_NTOA6 || 8850 subr == DIF_SUBR_INET_NTOP || 8851 subr == DIF_SUBR_LLTOSTR || 8852 subr == DIF_SUBR_RINDEX || 8853 subr == DIF_SUBR_STRCHR || 8854 subr == DIF_SUBR_STRJOIN || 8855 subr == DIF_SUBR_STRRCHR || 8856 subr == DIF_SUBR_STRSTR || 8857 subr == DIF_SUBR_HTONS || 8858 subr == DIF_SUBR_HTONL || 8859 subr == DIF_SUBR_HTONLL || 8860 subr == DIF_SUBR_NTOHS || 8861 subr == DIF_SUBR_NTOHL || 8862 subr == DIF_SUBR_NTOHLL || 8863 subr == DIF_SUBR_MEMREF || 8864 subr == DIF_SUBR_TYPEREF) 8865 break; 8866 8867 err += efunc(pc, "invalid subr %u\n", subr); 8868 break; 8869 8870 default: 8871 err += efunc(pc, "invalid opcode %u\n", 8872 DIF_INSTR_OP(instr)); 8873 } 8874 } 8875 8876 return (err); 8877} 8878#endif 8879 8880/* 8881 * Returns 1 if the expression in the DIF object can be cached on a per-thread 8882 * basis; 0 if not. 8883 */ 8884static int 8885dtrace_difo_cacheable(dtrace_difo_t *dp) 8886{ 8887 int i; 8888 8889 if (dp == NULL) 8890 return (0); 8891 8892 for (i = 0; i < dp->dtdo_varlen; i++) { 8893 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8894 8895 if (v->dtdv_scope != DIFV_SCOPE_GLOBAL) 8896 continue; 8897 8898 switch (v->dtdv_id) { 8899 case DIF_VAR_CURTHREAD: 8900 case DIF_VAR_PID: 8901 case DIF_VAR_TID: 8902 case DIF_VAR_EXECARGS: 8903 case DIF_VAR_EXECNAME: 8904 case DIF_VAR_ZONENAME: 8905 break; 8906 8907 default: 8908 return (0); 8909 } 8910 } 8911 8912 /* 8913 * This DIF object may be cacheable. Now we need to look for any 8914 * array loading instructions, any memory loading instructions, or 8915 * any stores to thread-local variables. 8916 */ 8917 for (i = 0; i < dp->dtdo_len; i++) { 8918 uint_t op = DIF_INSTR_OP(dp->dtdo_buf[i]); 8919 8920 if ((op >= DIF_OP_LDSB && op <= DIF_OP_LDX) || 8921 (op >= DIF_OP_ULDSB && op <= DIF_OP_ULDX) || 8922 (op >= DIF_OP_RLDSB && op <= DIF_OP_RLDX) || 8923 op == DIF_OP_LDGA || op == DIF_OP_STTS) 8924 return (0); 8925 } 8926 8927 return (1); 8928} 8929 8930static void 8931dtrace_difo_hold(dtrace_difo_t *dp) 8932{ 8933 int i; 8934 8935 ASSERT(MUTEX_HELD(&dtrace_lock)); 8936 8937 dp->dtdo_refcnt++; 8938 ASSERT(dp->dtdo_refcnt != 0); 8939 8940 /* 8941 * We need to check this DIF object for references to the variable 8942 * DIF_VAR_VTIMESTAMP. 8943 */ 8944 for (i = 0; i < dp->dtdo_varlen; i++) { 8945 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 8946 8947 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 8948 continue; 8949 8950 if (dtrace_vtime_references++ == 0) 8951 dtrace_vtime_enable(); 8952 } 8953} 8954 8955/* 8956 * This routine calculates the dynamic variable chunksize for a given DIF 8957 * object. The calculation is not fool-proof, and can probably be tricked by 8958 * malicious DIF -- but it works for all compiler-generated DIF. Because this 8959 * calculation is likely imperfect, dtrace_dynvar() is able to gracefully fail 8960 * if a dynamic variable size exceeds the chunksize. 8961 */ 8962static void 8963dtrace_difo_chunksize(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 8964{ 8965 uint64_t sval = 0; 8966 dtrace_key_t tupregs[DIF_DTR_NREGS + 2]; /* +2 for thread and id */ 8967 const dif_instr_t *text = dp->dtdo_buf; 8968 uint_t pc, srd = 0; 8969 uint_t ttop = 0; 8970 size_t size, ksize; 8971 uint_t id, i; 8972 8973 for (pc = 0; pc < dp->dtdo_len; pc++) { 8974 dif_instr_t instr = text[pc]; 8975 uint_t op = DIF_INSTR_OP(instr); 8976 uint_t rd = DIF_INSTR_RD(instr); 8977 uint_t r1 = DIF_INSTR_R1(instr); 8978 uint_t nkeys = 0; 8979 uchar_t scope = 0; 8980 8981 dtrace_key_t *key = tupregs; 8982 8983 switch (op) { 8984 case DIF_OP_SETX: 8985 sval = dp->dtdo_inttab[DIF_INSTR_INTEGER(instr)]; 8986 srd = rd; 8987 continue; 8988 8989 case DIF_OP_STTS: 8990 key = &tupregs[DIF_DTR_NREGS]; 8991 key[0].dttk_size = 0; 8992 key[1].dttk_size = 0; 8993 nkeys = 2; 8994 scope = DIFV_SCOPE_THREAD; 8995 break; 8996 8997 case DIF_OP_STGAA: 8998 case DIF_OP_STTAA: 8999 nkeys = ttop; 9000 9001 if (DIF_INSTR_OP(instr) == DIF_OP_STTAA) 9002 key[nkeys++].dttk_size = 0; 9003 9004 key[nkeys++].dttk_size = 0; 9005 9006 if (op == DIF_OP_STTAA) { 9007 scope = DIFV_SCOPE_THREAD; 9008 } else { 9009 scope = DIFV_SCOPE_GLOBAL; 9010 } 9011 9012 break; 9013 9014 case DIF_OP_PUSHTR: 9015 if (ttop == DIF_DTR_NREGS) 9016 return; 9017 9018 if ((srd == 0 || sval == 0) && r1 == DIF_TYPE_STRING) { 9019 /* 9020 * If the register for the size of the "pushtr" 9021 * is %r0 (or the value is 0) and the type is 9022 * a string, we'll use the system-wide default 9023 * string size. 9024 */ 9025 tupregs[ttop++].dttk_size = 9026 dtrace_strsize_default; 9027 } else { 9028 if (srd == 0) 9029 return; 9030 9031 tupregs[ttop++].dttk_size = sval; 9032 } 9033 9034 break; 9035 9036 case DIF_OP_PUSHTV: 9037 if (ttop == DIF_DTR_NREGS) 9038 return; 9039 9040 tupregs[ttop++].dttk_size = 0; 9041 break; 9042 9043 case DIF_OP_FLUSHTS: 9044 ttop = 0; 9045 break; 9046 9047 case DIF_OP_POPTS: 9048 if (ttop != 0) 9049 ttop--; 9050 break; 9051 } 9052 9053 sval = 0; 9054 srd = 0; 9055 9056 if (nkeys == 0) 9057 continue; 9058 9059 /* 9060 * We have a dynamic variable allocation; calculate its size. 9061 */ 9062 for (ksize = 0, i = 0; i < nkeys; i++) 9063 ksize += P2ROUNDUP(key[i].dttk_size, sizeof (uint64_t)); 9064 9065 size = sizeof (dtrace_dynvar_t); 9066 size += sizeof (dtrace_key_t) * (nkeys - 1); 9067 size += ksize; 9068 9069 /* 9070 * Now we need to determine the size of the stored data. 9071 */ 9072 id = DIF_INSTR_VAR(instr); 9073 9074 for (i = 0; i < dp->dtdo_varlen; i++) { 9075 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9076 9077 if (v->dtdv_id == id && v->dtdv_scope == scope) { 9078 size += v->dtdv_type.dtdt_size; 9079 break; 9080 } 9081 } 9082 9083 if (i == dp->dtdo_varlen) 9084 return; 9085 9086 /* 9087 * We have the size. If this is larger than the chunk size 9088 * for our dynamic variable state, reset the chunk size. 9089 */ 9090 size = P2ROUNDUP(size, sizeof (uint64_t)); 9091 9092 if (size > vstate->dtvs_dynvars.dtds_chunksize) 9093 vstate->dtvs_dynvars.dtds_chunksize = size; 9094 } 9095} 9096 9097static void 9098dtrace_difo_init(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9099{ 9100 int i, oldsvars, osz, nsz, otlocals, ntlocals; 9101 uint_t id; 9102 9103 ASSERT(MUTEX_HELD(&dtrace_lock)); 9104 ASSERT(dp->dtdo_buf != NULL && dp->dtdo_len != 0); 9105 9106 for (i = 0; i < dp->dtdo_varlen; i++) { 9107 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9108 dtrace_statvar_t *svar, ***svarp = NULL; 9109 size_t dsize = 0; 9110 uint8_t scope = v->dtdv_scope; 9111 int *np = NULL; 9112 9113 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9114 continue; 9115 9116 id -= DIF_VAR_OTHER_UBASE; 9117 9118 switch (scope) { 9119 case DIFV_SCOPE_THREAD: 9120 while (id >= (otlocals = vstate->dtvs_ntlocals)) { 9121 dtrace_difv_t *tlocals; 9122 9123 if ((ntlocals = (otlocals << 1)) == 0) 9124 ntlocals = 1; 9125 9126 osz = otlocals * sizeof (dtrace_difv_t); 9127 nsz = ntlocals * sizeof (dtrace_difv_t); 9128 9129 tlocals = kmem_zalloc(nsz, KM_SLEEP); 9130 9131 if (osz != 0) { 9132 bcopy(vstate->dtvs_tlocals, 9133 tlocals, osz); 9134 kmem_free(vstate->dtvs_tlocals, osz); 9135 } 9136 9137 vstate->dtvs_tlocals = tlocals; 9138 vstate->dtvs_ntlocals = ntlocals; 9139 } 9140 9141 vstate->dtvs_tlocals[id] = *v; 9142 continue; 9143 9144 case DIFV_SCOPE_LOCAL: 9145 np = &vstate->dtvs_nlocals; 9146 svarp = &vstate->dtvs_locals; 9147 9148 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9149 dsize = NCPU * (v->dtdv_type.dtdt_size + 9150 sizeof (uint64_t)); 9151 else 9152 dsize = NCPU * sizeof (uint64_t); 9153 9154 break; 9155 9156 case DIFV_SCOPE_GLOBAL: 9157 np = &vstate->dtvs_nglobals; 9158 svarp = &vstate->dtvs_globals; 9159 9160 if (v->dtdv_type.dtdt_flags & DIF_TF_BYREF) 9161 dsize = v->dtdv_type.dtdt_size + 9162 sizeof (uint64_t); 9163 9164 break; 9165 9166 default: 9167 ASSERT(0); 9168 } 9169 9170 while (id >= (oldsvars = *np)) { 9171 dtrace_statvar_t **statics; 9172 int newsvars, oldsize, newsize; 9173 9174 if ((newsvars = (oldsvars << 1)) == 0) 9175 newsvars = 1; 9176 9177 oldsize = oldsvars * sizeof (dtrace_statvar_t *); 9178 newsize = newsvars * sizeof (dtrace_statvar_t *); 9179 9180 statics = kmem_zalloc(newsize, KM_SLEEP); 9181 9182 if (oldsize != 0) { 9183 bcopy(*svarp, statics, oldsize); 9184 kmem_free(*svarp, oldsize); 9185 } 9186 9187 *svarp = statics; 9188 *np = newsvars; 9189 } 9190 9191 if ((svar = (*svarp)[id]) == NULL) { 9192 svar = kmem_zalloc(sizeof (dtrace_statvar_t), KM_SLEEP); 9193 svar->dtsv_var = *v; 9194 9195 if ((svar->dtsv_size = dsize) != 0) { 9196 svar->dtsv_data = (uint64_t)(uintptr_t) 9197 kmem_zalloc(dsize, KM_SLEEP); 9198 } 9199 9200 (*svarp)[id] = svar; 9201 } 9202 9203 svar->dtsv_refcnt++; 9204 } 9205 9206 dtrace_difo_chunksize(dp, vstate); 9207 dtrace_difo_hold(dp); 9208} 9209 9210#if defined(sun) 9211static dtrace_difo_t * 9212dtrace_difo_duplicate(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9213{ 9214 dtrace_difo_t *new; 9215 size_t sz; 9216 9217 ASSERT(dp->dtdo_buf != NULL); 9218 ASSERT(dp->dtdo_refcnt != 0); 9219 9220 new = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 9221 9222 ASSERT(dp->dtdo_buf != NULL); 9223 sz = dp->dtdo_len * sizeof (dif_instr_t); 9224 new->dtdo_buf = kmem_alloc(sz, KM_SLEEP); 9225 bcopy(dp->dtdo_buf, new->dtdo_buf, sz); 9226 new->dtdo_len = dp->dtdo_len; 9227 9228 if (dp->dtdo_strtab != NULL) { 9229 ASSERT(dp->dtdo_strlen != 0); 9230 new->dtdo_strtab = kmem_alloc(dp->dtdo_strlen, KM_SLEEP); 9231 bcopy(dp->dtdo_strtab, new->dtdo_strtab, dp->dtdo_strlen); 9232 new->dtdo_strlen = dp->dtdo_strlen; 9233 } 9234 9235 if (dp->dtdo_inttab != NULL) { 9236 ASSERT(dp->dtdo_intlen != 0); 9237 sz = dp->dtdo_intlen * sizeof (uint64_t); 9238 new->dtdo_inttab = kmem_alloc(sz, KM_SLEEP); 9239 bcopy(dp->dtdo_inttab, new->dtdo_inttab, sz); 9240 new->dtdo_intlen = dp->dtdo_intlen; 9241 } 9242 9243 if (dp->dtdo_vartab != NULL) { 9244 ASSERT(dp->dtdo_varlen != 0); 9245 sz = dp->dtdo_varlen * sizeof (dtrace_difv_t); 9246 new->dtdo_vartab = kmem_alloc(sz, KM_SLEEP); 9247 bcopy(dp->dtdo_vartab, new->dtdo_vartab, sz); 9248 new->dtdo_varlen = dp->dtdo_varlen; 9249 } 9250 9251 dtrace_difo_init(new, vstate); 9252 return (new); 9253} 9254#endif 9255 9256static void 9257dtrace_difo_destroy(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9258{ 9259 int i; 9260 9261 ASSERT(dp->dtdo_refcnt == 0); 9262 9263 for (i = 0; i < dp->dtdo_varlen; i++) { 9264 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9265 dtrace_statvar_t *svar, **svarp = NULL; 9266 uint_t id; 9267 uint8_t scope = v->dtdv_scope; 9268 int *np = NULL; 9269 9270 switch (scope) { 9271 case DIFV_SCOPE_THREAD: 9272 continue; 9273 9274 case DIFV_SCOPE_LOCAL: 9275 np = &vstate->dtvs_nlocals; 9276 svarp = vstate->dtvs_locals; 9277 break; 9278 9279 case DIFV_SCOPE_GLOBAL: 9280 np = &vstate->dtvs_nglobals; 9281 svarp = vstate->dtvs_globals; 9282 break; 9283 9284 default: 9285 ASSERT(0); 9286 } 9287 9288 if ((id = v->dtdv_id) < DIF_VAR_OTHER_UBASE) 9289 continue; 9290 9291 id -= DIF_VAR_OTHER_UBASE; 9292 ASSERT(id < *np); 9293 9294 svar = svarp[id]; 9295 ASSERT(svar != NULL); 9296 ASSERT(svar->dtsv_refcnt > 0); 9297 9298 if (--svar->dtsv_refcnt > 0) 9299 continue; 9300 9301 if (svar->dtsv_size != 0) { 9302 ASSERT(svar->dtsv_data != 0); 9303 kmem_free((void *)(uintptr_t)svar->dtsv_data, 9304 svar->dtsv_size); 9305 } 9306 9307 kmem_free(svar, sizeof (dtrace_statvar_t)); 9308 svarp[id] = NULL; 9309 } 9310 9311 if (dp->dtdo_buf != NULL) 9312 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 9313 if (dp->dtdo_inttab != NULL) 9314 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 9315 if (dp->dtdo_strtab != NULL) 9316 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 9317 if (dp->dtdo_vartab != NULL) 9318 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 9319 9320 kmem_free(dp, sizeof (dtrace_difo_t)); 9321} 9322 9323static void 9324dtrace_difo_release(dtrace_difo_t *dp, dtrace_vstate_t *vstate) 9325{ 9326 int i; 9327 9328 ASSERT(MUTEX_HELD(&dtrace_lock)); 9329 ASSERT(dp->dtdo_refcnt != 0); 9330 9331 for (i = 0; i < dp->dtdo_varlen; i++) { 9332 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 9333 9334 if (v->dtdv_id != DIF_VAR_VTIMESTAMP) 9335 continue; 9336 9337 ASSERT(dtrace_vtime_references > 0); 9338 if (--dtrace_vtime_references == 0) 9339 dtrace_vtime_disable(); 9340 } 9341 9342 if (--dp->dtdo_refcnt == 0) 9343 dtrace_difo_destroy(dp, vstate); 9344} 9345 9346/* 9347 * DTrace Format Functions 9348 */ 9349static uint16_t 9350dtrace_format_add(dtrace_state_t *state, char *str) 9351{ 9352 char *fmt, **new; 9353 uint16_t ndx, len = strlen(str) + 1; 9354 9355 fmt = kmem_zalloc(len, KM_SLEEP); 9356 bcopy(str, fmt, len); 9357 9358 for (ndx = 0; ndx < state->dts_nformats; ndx++) { 9359 if (state->dts_formats[ndx] == NULL) { 9360 state->dts_formats[ndx] = fmt; 9361 return (ndx + 1); 9362 } 9363 } 9364 9365 if (state->dts_nformats == USHRT_MAX) { 9366 /* 9367 * This is only likely if a denial-of-service attack is being 9368 * attempted. As such, it's okay to fail silently here. 9369 */ 9370 kmem_free(fmt, len); 9371 return (0); 9372 } 9373 9374 /* 9375 * For simplicity, we always resize the formats array to be exactly the 9376 * number of formats. 9377 */ 9378 ndx = state->dts_nformats++; 9379 new = kmem_alloc((ndx + 1) * sizeof (char *), KM_SLEEP); 9380 9381 if (state->dts_formats != NULL) { 9382 ASSERT(ndx != 0); 9383 bcopy(state->dts_formats, new, ndx * sizeof (char *)); 9384 kmem_free(state->dts_formats, ndx * sizeof (char *)); 9385 } 9386 9387 state->dts_formats = new; 9388 state->dts_formats[ndx] = fmt; 9389 9390 return (ndx + 1); 9391} 9392 9393static void 9394dtrace_format_remove(dtrace_state_t *state, uint16_t format) 9395{ 9396 char *fmt; 9397 9398 ASSERT(state->dts_formats != NULL); 9399 ASSERT(format <= state->dts_nformats); 9400 ASSERT(state->dts_formats[format - 1] != NULL); 9401 9402 fmt = state->dts_formats[format - 1]; 9403 kmem_free(fmt, strlen(fmt) + 1); 9404 state->dts_formats[format - 1] = NULL; 9405} 9406 9407static void 9408dtrace_format_destroy(dtrace_state_t *state) 9409{ 9410 int i; 9411 9412 if (state->dts_nformats == 0) { 9413 ASSERT(state->dts_formats == NULL); 9414 return; 9415 } 9416 9417 ASSERT(state->dts_formats != NULL); 9418 9419 for (i = 0; i < state->dts_nformats; i++) { 9420 char *fmt = state->dts_formats[i]; 9421 9422 if (fmt == NULL) 9423 continue; 9424 9425 kmem_free(fmt, strlen(fmt) + 1); 9426 } 9427 9428 kmem_free(state->dts_formats, state->dts_nformats * sizeof (char *)); 9429 state->dts_nformats = 0; 9430 state->dts_formats = NULL; 9431} 9432 9433/* 9434 * DTrace Predicate Functions 9435 */ 9436static dtrace_predicate_t * 9437dtrace_predicate_create(dtrace_difo_t *dp) 9438{ 9439 dtrace_predicate_t *pred; 9440 9441 ASSERT(MUTEX_HELD(&dtrace_lock)); 9442 ASSERT(dp->dtdo_refcnt != 0); 9443 9444 pred = kmem_zalloc(sizeof (dtrace_predicate_t), KM_SLEEP); 9445 pred->dtp_difo = dp; 9446 pred->dtp_refcnt = 1; 9447 9448 if (!dtrace_difo_cacheable(dp)) 9449 return (pred); 9450 9451 if (dtrace_predcache_id == DTRACE_CACHEIDNONE) { 9452 /* 9453 * This is only theoretically possible -- we have had 2^32 9454 * cacheable predicates on this machine. We cannot allow any 9455 * more predicates to become cacheable: as unlikely as it is, 9456 * there may be a thread caching a (now stale) predicate cache 9457 * ID. (N.B.: the temptation is being successfully resisted to 9458 * have this cmn_err() "Holy shit -- we executed this code!") 9459 */ 9460 return (pred); 9461 } 9462 9463 pred->dtp_cacheid = dtrace_predcache_id++; 9464 9465 return (pred); 9466} 9467 9468static void 9469dtrace_predicate_hold(dtrace_predicate_t *pred) 9470{ 9471 ASSERT(MUTEX_HELD(&dtrace_lock)); 9472 ASSERT(pred->dtp_difo != NULL && pred->dtp_difo->dtdo_refcnt != 0); 9473 ASSERT(pred->dtp_refcnt > 0); 9474 9475 pred->dtp_refcnt++; 9476} 9477 9478static void 9479dtrace_predicate_release(dtrace_predicate_t *pred, dtrace_vstate_t *vstate) 9480{ 9481 dtrace_difo_t *dp = pred->dtp_difo; 9482 9483 ASSERT(MUTEX_HELD(&dtrace_lock)); 9484 ASSERT(dp != NULL && dp->dtdo_refcnt != 0); 9485 ASSERT(pred->dtp_refcnt > 0); 9486 9487 if (--pred->dtp_refcnt == 0) { 9488 dtrace_difo_release(pred->dtp_difo, vstate); 9489 kmem_free(pred, sizeof (dtrace_predicate_t)); 9490 } 9491} 9492 9493/* 9494 * DTrace Action Description Functions 9495 */ 9496static dtrace_actdesc_t * 9497dtrace_actdesc_create(dtrace_actkind_t kind, uint32_t ntuple, 9498 uint64_t uarg, uint64_t arg) 9499{ 9500 dtrace_actdesc_t *act; 9501 9502#if defined(sun) 9503 ASSERT(!DTRACEACT_ISPRINTFLIKE(kind) || (arg != NULL && 9504 arg >= KERNELBASE) || (arg == NULL && kind == DTRACEACT_PRINTA)); 9505#endif 9506 9507 act = kmem_zalloc(sizeof (dtrace_actdesc_t), KM_SLEEP); 9508 act->dtad_kind = kind; 9509 act->dtad_ntuple = ntuple; 9510 act->dtad_uarg = uarg; 9511 act->dtad_arg = arg; 9512 act->dtad_refcnt = 1; 9513 9514 return (act); 9515} 9516 9517static void 9518dtrace_actdesc_hold(dtrace_actdesc_t *act) 9519{ 9520 ASSERT(act->dtad_refcnt >= 1); 9521 act->dtad_refcnt++; 9522} 9523 9524static void 9525dtrace_actdesc_release(dtrace_actdesc_t *act, dtrace_vstate_t *vstate) 9526{ 9527 dtrace_actkind_t kind = act->dtad_kind; 9528 dtrace_difo_t *dp; 9529 9530 ASSERT(act->dtad_refcnt >= 1); 9531 9532 if (--act->dtad_refcnt != 0) 9533 return; 9534 9535 if ((dp = act->dtad_difo) != NULL) 9536 dtrace_difo_release(dp, vstate); 9537 9538 if (DTRACEACT_ISPRINTFLIKE(kind)) { 9539 char *str = (char *)(uintptr_t)act->dtad_arg; 9540 9541#if defined(sun) 9542 ASSERT((str != NULL && (uintptr_t)str >= KERNELBASE) || 9543 (str == NULL && act->dtad_kind == DTRACEACT_PRINTA)); 9544#endif 9545 9546 if (str != NULL) 9547 kmem_free(str, strlen(str) + 1); 9548 } 9549 9550 kmem_free(act, sizeof (dtrace_actdesc_t)); 9551} 9552 9553/* 9554 * DTrace ECB Functions 9555 */ 9556static dtrace_ecb_t * 9557dtrace_ecb_add(dtrace_state_t *state, dtrace_probe_t *probe) 9558{ 9559 dtrace_ecb_t *ecb; 9560 dtrace_epid_t epid; 9561 9562 ASSERT(MUTEX_HELD(&dtrace_lock)); 9563 9564 ecb = kmem_zalloc(sizeof (dtrace_ecb_t), KM_SLEEP); 9565 ecb->dte_predicate = NULL; 9566 ecb->dte_probe = probe; 9567 9568 /* 9569 * The default size is the size of the default action: recording 9570 * the epid. 9571 */ 9572 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9573 ecb->dte_alignment = sizeof (dtrace_epid_t); 9574 9575 epid = state->dts_epid++; 9576 9577 if (epid - 1 >= state->dts_necbs) { 9578 dtrace_ecb_t **oecbs = state->dts_ecbs, **ecbs; 9579 int necbs = state->dts_necbs << 1; 9580 9581 ASSERT(epid == state->dts_necbs + 1); 9582 9583 if (necbs == 0) { 9584 ASSERT(oecbs == NULL); 9585 necbs = 1; 9586 } 9587 9588 ecbs = kmem_zalloc(necbs * sizeof (*ecbs), KM_SLEEP); 9589 9590 if (oecbs != NULL) 9591 bcopy(oecbs, ecbs, state->dts_necbs * sizeof (*ecbs)); 9592 9593 dtrace_membar_producer(); 9594 state->dts_ecbs = ecbs; 9595 9596 if (oecbs != NULL) { 9597 /* 9598 * If this state is active, we must dtrace_sync() 9599 * before we can free the old dts_ecbs array: we're 9600 * coming in hot, and there may be active ring 9601 * buffer processing (which indexes into the dts_ecbs 9602 * array) on another CPU. 9603 */ 9604 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 9605 dtrace_sync(); 9606 9607 kmem_free(oecbs, state->dts_necbs * sizeof (*ecbs)); 9608 } 9609 9610 dtrace_membar_producer(); 9611 state->dts_necbs = necbs; 9612 } 9613 9614 ecb->dte_state = state; 9615 9616 ASSERT(state->dts_ecbs[epid - 1] == NULL); 9617 dtrace_membar_producer(); 9618 state->dts_ecbs[(ecb->dte_epid = epid) - 1] = ecb; 9619 9620 return (ecb); 9621} 9622 9623static void 9624dtrace_ecb_enable(dtrace_ecb_t *ecb) 9625{ 9626 dtrace_probe_t *probe = ecb->dte_probe; 9627 9628 ASSERT(MUTEX_HELD(&cpu_lock)); 9629 ASSERT(MUTEX_HELD(&dtrace_lock)); 9630 ASSERT(ecb->dte_next == NULL); 9631 9632 if (probe == NULL) { 9633 /* 9634 * This is the NULL probe -- there's nothing to do. 9635 */ 9636 return; 9637 } 9638 9639 if (probe->dtpr_ecb == NULL) { 9640 dtrace_provider_t *prov = probe->dtpr_provider; 9641 9642 /* 9643 * We're the first ECB on this probe. 9644 */ 9645 probe->dtpr_ecb = probe->dtpr_ecb_last = ecb; 9646 9647 if (ecb->dte_predicate != NULL) 9648 probe->dtpr_predcache = ecb->dte_predicate->dtp_cacheid; 9649 9650 prov->dtpv_pops.dtps_enable(prov->dtpv_arg, 9651 probe->dtpr_id, probe->dtpr_arg); 9652 } else { 9653 /* 9654 * This probe is already active. Swing the last pointer to 9655 * point to the new ECB, and issue a dtrace_sync() to assure 9656 * that all CPUs have seen the change. 9657 */ 9658 ASSERT(probe->dtpr_ecb_last != NULL); 9659 probe->dtpr_ecb_last->dte_next = ecb; 9660 probe->dtpr_ecb_last = ecb; 9661 probe->dtpr_predcache = 0; 9662 9663 dtrace_sync(); 9664 } 9665} 9666 9667static void 9668dtrace_ecb_resize(dtrace_ecb_t *ecb) 9669{ 9670 uint32_t maxalign = sizeof (dtrace_epid_t); 9671 uint32_t align = sizeof (uint8_t), offs, diff; 9672 dtrace_action_t *act; 9673 int wastuple = 0; 9674 uint32_t aggbase = UINT32_MAX; 9675 dtrace_state_t *state = ecb->dte_state; 9676 9677 /* 9678 * If we record anything, we always record the epid. (And we always 9679 * record it first.) 9680 */ 9681 offs = sizeof (dtrace_epid_t); 9682 ecb->dte_size = ecb->dte_needed = sizeof (dtrace_epid_t); 9683 9684 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9685 dtrace_recdesc_t *rec = &act->dta_rec; 9686 9687 if ((align = rec->dtrd_alignment) > maxalign) 9688 maxalign = align; 9689 9690 if (!wastuple && act->dta_intuple) { 9691 /* 9692 * This is the first record in a tuple. Align the 9693 * offset to be at offset 4 in an 8-byte aligned 9694 * block. 9695 */ 9696 diff = offs + sizeof (dtrace_aggid_t); 9697 9698 if ((diff = (diff & (sizeof (uint64_t) - 1)))) 9699 offs += sizeof (uint64_t) - diff; 9700 9701 aggbase = offs - sizeof (dtrace_aggid_t); 9702 ASSERT(!(aggbase & (sizeof (uint64_t) - 1))); 9703 } 9704 9705 /*LINTED*/ 9706 if (rec->dtrd_size != 0 && (diff = (offs & (align - 1)))) { 9707 /* 9708 * The current offset is not properly aligned; align it. 9709 */ 9710 offs += align - diff; 9711 } 9712 9713 rec->dtrd_offset = offs; 9714 9715 if (offs + rec->dtrd_size > ecb->dte_needed) { 9716 ecb->dte_needed = offs + rec->dtrd_size; 9717 9718 if (ecb->dte_needed > state->dts_needed) 9719 state->dts_needed = ecb->dte_needed; 9720 } 9721 9722 if (DTRACEACT_ISAGG(act->dta_kind)) { 9723 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9724 dtrace_action_t *first = agg->dtag_first, *prev; 9725 9726 ASSERT(rec->dtrd_size != 0 && first != NULL); 9727 ASSERT(wastuple); 9728 ASSERT(aggbase != UINT32_MAX); 9729 9730 agg->dtag_base = aggbase; 9731 9732 while ((prev = first->dta_prev) != NULL && 9733 DTRACEACT_ISAGG(prev->dta_kind)) { 9734 agg = (dtrace_aggregation_t *)prev; 9735 first = agg->dtag_first; 9736 } 9737 9738 if (prev != NULL) { 9739 offs = prev->dta_rec.dtrd_offset + 9740 prev->dta_rec.dtrd_size; 9741 } else { 9742 offs = sizeof (dtrace_epid_t); 9743 } 9744 wastuple = 0; 9745 } else { 9746 if (!act->dta_intuple) 9747 ecb->dte_size = offs + rec->dtrd_size; 9748 9749 offs += rec->dtrd_size; 9750 } 9751 9752 wastuple = act->dta_intuple; 9753 } 9754 9755 if ((act = ecb->dte_action) != NULL && 9756 !(act->dta_kind == DTRACEACT_SPECULATE && act->dta_next == NULL) && 9757 ecb->dte_size == sizeof (dtrace_epid_t)) { 9758 /* 9759 * If the size is still sizeof (dtrace_epid_t), then all 9760 * actions store no data; set the size to 0. 9761 */ 9762 ecb->dte_alignment = maxalign; 9763 ecb->dte_size = 0; 9764 9765 /* 9766 * If the needed space is still sizeof (dtrace_epid_t), then 9767 * all actions need no additional space; set the needed 9768 * size to 0. 9769 */ 9770 if (ecb->dte_needed == sizeof (dtrace_epid_t)) 9771 ecb->dte_needed = 0; 9772 9773 return; 9774 } 9775 9776 /* 9777 * Set our alignment, and make sure that the dte_size and dte_needed 9778 * are aligned to the size of an EPID. 9779 */ 9780 ecb->dte_alignment = maxalign; 9781 ecb->dte_size = (ecb->dte_size + (sizeof (dtrace_epid_t) - 1)) & 9782 ~(sizeof (dtrace_epid_t) - 1); 9783 ecb->dte_needed = (ecb->dte_needed + (sizeof (dtrace_epid_t) - 1)) & 9784 ~(sizeof (dtrace_epid_t) - 1); 9785 ASSERT(ecb->dte_size <= ecb->dte_needed); 9786} 9787 9788static dtrace_action_t * 9789dtrace_ecb_aggregation_create(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9790{ 9791 dtrace_aggregation_t *agg; 9792 size_t size = sizeof (uint64_t); 9793 int ntuple = desc->dtad_ntuple; 9794 dtrace_action_t *act; 9795 dtrace_recdesc_t *frec; 9796 dtrace_aggid_t aggid; 9797 dtrace_state_t *state = ecb->dte_state; 9798 9799 agg = kmem_zalloc(sizeof (dtrace_aggregation_t), KM_SLEEP); 9800 agg->dtag_ecb = ecb; 9801 9802 ASSERT(DTRACEACT_ISAGG(desc->dtad_kind)); 9803 9804 switch (desc->dtad_kind) { 9805 case DTRACEAGG_MIN: 9806 agg->dtag_initial = INT64_MAX; 9807 agg->dtag_aggregate = dtrace_aggregate_min; 9808 break; 9809 9810 case DTRACEAGG_MAX: 9811 agg->dtag_initial = INT64_MIN; 9812 agg->dtag_aggregate = dtrace_aggregate_max; 9813 break; 9814 9815 case DTRACEAGG_COUNT: 9816 agg->dtag_aggregate = dtrace_aggregate_count; 9817 break; 9818 9819 case DTRACEAGG_QUANTIZE: 9820 agg->dtag_aggregate = dtrace_aggregate_quantize; 9821 size = (((sizeof (uint64_t) * NBBY) - 1) * 2 + 1) * 9822 sizeof (uint64_t); 9823 break; 9824 9825 case DTRACEAGG_LQUANTIZE: { 9826 uint16_t step = DTRACE_LQUANTIZE_STEP(desc->dtad_arg); 9827 uint16_t levels = DTRACE_LQUANTIZE_LEVELS(desc->dtad_arg); 9828 9829 agg->dtag_initial = desc->dtad_arg; 9830 agg->dtag_aggregate = dtrace_aggregate_lquantize; 9831 9832 if (step == 0 || levels == 0) 9833 goto err; 9834 9835 size = levels * sizeof (uint64_t) + 3 * sizeof (uint64_t); 9836 break; 9837 } 9838 9839 case DTRACEAGG_AVG: 9840 agg->dtag_aggregate = dtrace_aggregate_avg; 9841 size = sizeof (uint64_t) * 2; 9842 break; 9843 9844 case DTRACEAGG_STDDEV: 9845 agg->dtag_aggregate = dtrace_aggregate_stddev; 9846 size = sizeof (uint64_t) * 4; 9847 break; 9848 9849 case DTRACEAGG_SUM: 9850 agg->dtag_aggregate = dtrace_aggregate_sum; 9851 break; 9852 9853 default: 9854 goto err; 9855 } 9856 9857 agg->dtag_action.dta_rec.dtrd_size = size; 9858 9859 if (ntuple == 0) 9860 goto err; 9861 9862 /* 9863 * We must make sure that we have enough actions for the n-tuple. 9864 */ 9865 for (act = ecb->dte_action_last; act != NULL; act = act->dta_prev) { 9866 if (DTRACEACT_ISAGG(act->dta_kind)) 9867 break; 9868 9869 if (--ntuple == 0) { 9870 /* 9871 * This is the action with which our n-tuple begins. 9872 */ 9873 agg->dtag_first = act; 9874 goto success; 9875 } 9876 } 9877 9878 /* 9879 * This n-tuple is short by ntuple elements. Return failure. 9880 */ 9881 ASSERT(ntuple != 0); 9882err: 9883 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9884 return (NULL); 9885 9886success: 9887 /* 9888 * If the last action in the tuple has a size of zero, it's actually 9889 * an expression argument for the aggregating action. 9890 */ 9891 ASSERT(ecb->dte_action_last != NULL); 9892 act = ecb->dte_action_last; 9893 9894 if (act->dta_kind == DTRACEACT_DIFEXPR) { 9895 ASSERT(act->dta_difo != NULL); 9896 9897 if (act->dta_difo->dtdo_rtype.dtdt_size == 0) 9898 agg->dtag_hasarg = 1; 9899 } 9900 9901 /* 9902 * We need to allocate an id for this aggregation. 9903 */ 9904#if defined(sun) 9905 aggid = (dtrace_aggid_t)(uintptr_t)vmem_alloc(state->dts_aggid_arena, 1, 9906 VM_BESTFIT | VM_SLEEP); 9907#else 9908 aggid = alloc_unr(state->dts_aggid_arena); 9909#endif 9910 9911 if (aggid - 1 >= state->dts_naggregations) { 9912 dtrace_aggregation_t **oaggs = state->dts_aggregations; 9913 dtrace_aggregation_t **aggs; 9914 int naggs = state->dts_naggregations << 1; 9915 int onaggs = state->dts_naggregations; 9916 9917 ASSERT(aggid == state->dts_naggregations + 1); 9918 9919 if (naggs == 0) { 9920 ASSERT(oaggs == NULL); 9921 naggs = 1; 9922 } 9923 9924 aggs = kmem_zalloc(naggs * sizeof (*aggs), KM_SLEEP); 9925 9926 if (oaggs != NULL) { 9927 bcopy(oaggs, aggs, onaggs * sizeof (*aggs)); 9928 kmem_free(oaggs, onaggs * sizeof (*aggs)); 9929 } 9930 9931 state->dts_aggregations = aggs; 9932 state->dts_naggregations = naggs; 9933 } 9934 9935 ASSERT(state->dts_aggregations[aggid - 1] == NULL); 9936 state->dts_aggregations[(agg->dtag_id = aggid) - 1] = agg; 9937 9938 frec = &agg->dtag_first->dta_rec; 9939 if (frec->dtrd_alignment < sizeof (dtrace_aggid_t)) 9940 frec->dtrd_alignment = sizeof (dtrace_aggid_t); 9941 9942 for (act = agg->dtag_first; act != NULL; act = act->dta_next) { 9943 ASSERT(!act->dta_intuple); 9944 act->dta_intuple = 1; 9945 } 9946 9947 return (&agg->dtag_action); 9948} 9949 9950static void 9951dtrace_ecb_aggregation_destroy(dtrace_ecb_t *ecb, dtrace_action_t *act) 9952{ 9953 dtrace_aggregation_t *agg = (dtrace_aggregation_t *)act; 9954 dtrace_state_t *state = ecb->dte_state; 9955 dtrace_aggid_t aggid = agg->dtag_id; 9956 9957 ASSERT(DTRACEACT_ISAGG(act->dta_kind)); 9958#if defined(sun) 9959 vmem_free(state->dts_aggid_arena, (void *)(uintptr_t)aggid, 1); 9960#else 9961 free_unr(state->dts_aggid_arena, aggid); 9962#endif 9963 9964 ASSERT(state->dts_aggregations[aggid - 1] == agg); 9965 state->dts_aggregations[aggid - 1] = NULL; 9966 9967 kmem_free(agg, sizeof (dtrace_aggregation_t)); 9968} 9969 9970static int 9971dtrace_ecb_action_add(dtrace_ecb_t *ecb, dtrace_actdesc_t *desc) 9972{ 9973 dtrace_action_t *action, *last; 9974 dtrace_difo_t *dp = desc->dtad_difo; 9975 uint32_t size = 0, align = sizeof (uint8_t), mask; 9976 uint16_t format = 0; 9977 dtrace_recdesc_t *rec; 9978 dtrace_state_t *state = ecb->dte_state; 9979 dtrace_optval_t *opt = state->dts_options, nframes = 0, strsize; 9980 uint64_t arg = desc->dtad_arg; 9981 9982 ASSERT(MUTEX_HELD(&dtrace_lock)); 9983 ASSERT(ecb->dte_action == NULL || ecb->dte_action->dta_refcnt == 1); 9984 9985 if (DTRACEACT_ISAGG(desc->dtad_kind)) { 9986 /* 9987 * If this is an aggregating action, there must be neither 9988 * a speculate nor a commit on the action chain. 9989 */ 9990 dtrace_action_t *act; 9991 9992 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 9993 if (act->dta_kind == DTRACEACT_COMMIT) 9994 return (EINVAL); 9995 9996 if (act->dta_kind == DTRACEACT_SPECULATE) 9997 return (EINVAL); 9998 } 9999 10000 action = dtrace_ecb_aggregation_create(ecb, desc); 10001 10002 if (action == NULL) 10003 return (EINVAL); 10004 } else { 10005 if (DTRACEACT_ISDESTRUCTIVE(desc->dtad_kind) || 10006 (desc->dtad_kind == DTRACEACT_DIFEXPR && 10007 dp != NULL && dp->dtdo_destructive)) { 10008 state->dts_destructive = 1; 10009 } 10010 10011 switch (desc->dtad_kind) { 10012 case DTRACEACT_PRINTF: 10013 case DTRACEACT_PRINTA: 10014 case DTRACEACT_SYSTEM: 10015 case DTRACEACT_FREOPEN: 10016 /* 10017 * We know that our arg is a string -- turn it into a 10018 * format. 10019 */ 10020 if (arg == 0) { 10021 ASSERT(desc->dtad_kind == DTRACEACT_PRINTA); 10022 format = 0; 10023 } else { 10024 ASSERT(arg != 0); 10025#if defined(sun) 10026 ASSERT(arg > KERNELBASE); 10027#endif 10028 format = dtrace_format_add(state, 10029 (char *)(uintptr_t)arg); 10030 } 10031 10032 /*FALLTHROUGH*/ 10033 case DTRACEACT_LIBACT: 10034 case DTRACEACT_DIFEXPR: 10035 if (dp == NULL) 10036 return (EINVAL); 10037 10038 if ((size = dp->dtdo_rtype.dtdt_size) != 0) 10039 break; 10040 10041 if (dp->dtdo_rtype.dtdt_kind == DIF_TYPE_STRING) { 10042 if (!(dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10043 return (EINVAL); 10044 10045 size = opt[DTRACEOPT_STRSIZE]; 10046 } 10047 10048 break; 10049 10050 case DTRACEACT_STACK: 10051 if ((nframes = arg) == 0) { 10052 nframes = opt[DTRACEOPT_STACKFRAMES]; 10053 ASSERT(nframes > 0); 10054 arg = nframes; 10055 } 10056 10057 size = nframes * sizeof (pc_t); 10058 break; 10059 10060 case DTRACEACT_JSTACK: 10061 if ((strsize = DTRACE_USTACK_STRSIZE(arg)) == 0) 10062 strsize = opt[DTRACEOPT_JSTACKSTRSIZE]; 10063 10064 if ((nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) 10065 nframes = opt[DTRACEOPT_JSTACKFRAMES]; 10066 10067 arg = DTRACE_USTACK_ARG(nframes, strsize); 10068 10069 /*FALLTHROUGH*/ 10070 case DTRACEACT_USTACK: 10071 if (desc->dtad_kind != DTRACEACT_JSTACK && 10072 (nframes = DTRACE_USTACK_NFRAMES(arg)) == 0) { 10073 strsize = DTRACE_USTACK_STRSIZE(arg); 10074 nframes = opt[DTRACEOPT_USTACKFRAMES]; 10075 ASSERT(nframes > 0); 10076 arg = DTRACE_USTACK_ARG(nframes, strsize); 10077 } 10078 10079 /* 10080 * Save a slot for the pid. 10081 */ 10082 size = (nframes + 1) * sizeof (uint64_t); 10083 size += DTRACE_USTACK_STRSIZE(arg); 10084 size = P2ROUNDUP(size, (uint32_t)(sizeof (uintptr_t))); 10085 10086 break; 10087 10088 case DTRACEACT_SYM: 10089 case DTRACEACT_MOD: 10090 if (dp == NULL || ((size = dp->dtdo_rtype.dtdt_size) != 10091 sizeof (uint64_t)) || 10092 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10093 return (EINVAL); 10094 break; 10095 10096 case DTRACEACT_USYM: 10097 case DTRACEACT_UMOD: 10098 case DTRACEACT_UADDR: 10099 if (dp == NULL || 10100 (dp->dtdo_rtype.dtdt_size != sizeof (uint64_t)) || 10101 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10102 return (EINVAL); 10103 10104 /* 10105 * We have a slot for the pid, plus a slot for the 10106 * argument. To keep things simple (aligned with 10107 * bitness-neutral sizing), we store each as a 64-bit 10108 * quantity. 10109 */ 10110 size = 2 * sizeof (uint64_t); 10111 break; 10112 10113 case DTRACEACT_STOP: 10114 case DTRACEACT_BREAKPOINT: 10115 case DTRACEACT_PANIC: 10116 break; 10117 10118 case DTRACEACT_CHILL: 10119 case DTRACEACT_DISCARD: 10120 case DTRACEACT_RAISE: 10121 if (dp == NULL) 10122 return (EINVAL); 10123 break; 10124 10125 case DTRACEACT_EXIT: 10126 if (dp == NULL || 10127 (size = dp->dtdo_rtype.dtdt_size) != sizeof (int) || 10128 (dp->dtdo_rtype.dtdt_flags & DIF_TF_BYREF)) 10129 return (EINVAL); 10130 break; 10131 10132 case DTRACEACT_SPECULATE: 10133 if (ecb->dte_size > sizeof (dtrace_epid_t)) 10134 return (EINVAL); 10135 10136 if (dp == NULL) 10137 return (EINVAL); 10138 10139 state->dts_speculates = 1; 10140 break; 10141 10142 case DTRACEACT_PRINTM: 10143 size = dp->dtdo_rtype.dtdt_size; 10144 break; 10145 10146 case DTRACEACT_PRINTT: 10147 size = dp->dtdo_rtype.dtdt_size; 10148 break; 10149 10150 case DTRACEACT_COMMIT: { 10151 dtrace_action_t *act = ecb->dte_action; 10152 10153 for (; act != NULL; act = act->dta_next) { 10154 if (act->dta_kind == DTRACEACT_COMMIT) 10155 return (EINVAL); 10156 } 10157 10158 if (dp == NULL) 10159 return (EINVAL); 10160 break; 10161 } 10162 10163 default: 10164 return (EINVAL); 10165 } 10166 10167 if (size != 0 || desc->dtad_kind == DTRACEACT_SPECULATE) { 10168 /* 10169 * If this is a data-storing action or a speculate, 10170 * we must be sure that there isn't a commit on the 10171 * action chain. 10172 */ 10173 dtrace_action_t *act = ecb->dte_action; 10174 10175 for (; act != NULL; act = act->dta_next) { 10176 if (act->dta_kind == DTRACEACT_COMMIT) 10177 return (EINVAL); 10178 } 10179 } 10180 10181 action = kmem_zalloc(sizeof (dtrace_action_t), KM_SLEEP); 10182 action->dta_rec.dtrd_size = size; 10183 } 10184 10185 action->dta_refcnt = 1; 10186 rec = &action->dta_rec; 10187 size = rec->dtrd_size; 10188 10189 for (mask = sizeof (uint64_t) - 1; size != 0 && mask > 0; mask >>= 1) { 10190 if (!(size & mask)) { 10191 align = mask + 1; 10192 break; 10193 } 10194 } 10195 10196 action->dta_kind = desc->dtad_kind; 10197 10198 if ((action->dta_difo = dp) != NULL) 10199 dtrace_difo_hold(dp); 10200 10201 rec->dtrd_action = action->dta_kind; 10202 rec->dtrd_arg = arg; 10203 rec->dtrd_uarg = desc->dtad_uarg; 10204 rec->dtrd_alignment = (uint16_t)align; 10205 rec->dtrd_format = format; 10206 10207 if ((last = ecb->dte_action_last) != NULL) { 10208 ASSERT(ecb->dte_action != NULL); 10209 action->dta_prev = last; 10210 last->dta_next = action; 10211 } else { 10212 ASSERT(ecb->dte_action == NULL); 10213 ecb->dte_action = action; 10214 } 10215 10216 ecb->dte_action_last = action; 10217 10218 return (0); 10219} 10220 10221static void 10222dtrace_ecb_action_remove(dtrace_ecb_t *ecb) 10223{ 10224 dtrace_action_t *act = ecb->dte_action, *next; 10225 dtrace_vstate_t *vstate = &ecb->dte_state->dts_vstate; 10226 dtrace_difo_t *dp; 10227 uint16_t format; 10228 10229 if (act != NULL && act->dta_refcnt > 1) { 10230 ASSERT(act->dta_next == NULL || act->dta_next->dta_refcnt == 1); 10231 act->dta_refcnt--; 10232 } else { 10233 for (; act != NULL; act = next) { 10234 next = act->dta_next; 10235 ASSERT(next != NULL || act == ecb->dte_action_last); 10236 ASSERT(act->dta_refcnt == 1); 10237 10238 if ((format = act->dta_rec.dtrd_format) != 0) 10239 dtrace_format_remove(ecb->dte_state, format); 10240 10241 if ((dp = act->dta_difo) != NULL) 10242 dtrace_difo_release(dp, vstate); 10243 10244 if (DTRACEACT_ISAGG(act->dta_kind)) { 10245 dtrace_ecb_aggregation_destroy(ecb, act); 10246 } else { 10247 kmem_free(act, sizeof (dtrace_action_t)); 10248 } 10249 } 10250 } 10251 10252 ecb->dte_action = NULL; 10253 ecb->dte_action_last = NULL; 10254 ecb->dte_size = sizeof (dtrace_epid_t); 10255} 10256 10257static void 10258dtrace_ecb_disable(dtrace_ecb_t *ecb) 10259{ 10260 /* 10261 * We disable the ECB by removing it from its probe. 10262 */ 10263 dtrace_ecb_t *pecb, *prev = NULL; 10264 dtrace_probe_t *probe = ecb->dte_probe; 10265 10266 ASSERT(MUTEX_HELD(&dtrace_lock)); 10267 10268 if (probe == NULL) { 10269 /* 10270 * This is the NULL probe; there is nothing to disable. 10271 */ 10272 return; 10273 } 10274 10275 for (pecb = probe->dtpr_ecb; pecb != NULL; pecb = pecb->dte_next) { 10276 if (pecb == ecb) 10277 break; 10278 prev = pecb; 10279 } 10280 10281 ASSERT(pecb != NULL); 10282 10283 if (prev == NULL) { 10284 probe->dtpr_ecb = ecb->dte_next; 10285 } else { 10286 prev->dte_next = ecb->dte_next; 10287 } 10288 10289 if (ecb == probe->dtpr_ecb_last) { 10290 ASSERT(ecb->dte_next == NULL); 10291 probe->dtpr_ecb_last = prev; 10292 } 10293 10294 /* 10295 * The ECB has been disconnected from the probe; now sync to assure 10296 * that all CPUs have seen the change before returning. 10297 */ 10298 dtrace_sync(); 10299 10300 if (probe->dtpr_ecb == NULL) { 10301 /* 10302 * That was the last ECB on the probe; clear the predicate 10303 * cache ID for the probe, disable it and sync one more time 10304 * to assure that we'll never hit it again. 10305 */ 10306 dtrace_provider_t *prov = probe->dtpr_provider; 10307 10308 ASSERT(ecb->dte_next == NULL); 10309 ASSERT(probe->dtpr_ecb_last == NULL); 10310 probe->dtpr_predcache = DTRACE_CACHEIDNONE; 10311 prov->dtpv_pops.dtps_disable(prov->dtpv_arg, 10312 probe->dtpr_id, probe->dtpr_arg); 10313 dtrace_sync(); 10314 } else { 10315 /* 10316 * There is at least one ECB remaining on the probe. If there 10317 * is _exactly_ one, set the probe's predicate cache ID to be 10318 * the predicate cache ID of the remaining ECB. 10319 */ 10320 ASSERT(probe->dtpr_ecb_last != NULL); 10321 ASSERT(probe->dtpr_predcache == DTRACE_CACHEIDNONE); 10322 10323 if (probe->dtpr_ecb == probe->dtpr_ecb_last) { 10324 dtrace_predicate_t *p = probe->dtpr_ecb->dte_predicate; 10325 10326 ASSERT(probe->dtpr_ecb->dte_next == NULL); 10327 10328 if (p != NULL) 10329 probe->dtpr_predcache = p->dtp_cacheid; 10330 } 10331 10332 ecb->dte_next = NULL; 10333 } 10334} 10335 10336static void 10337dtrace_ecb_destroy(dtrace_ecb_t *ecb) 10338{ 10339 dtrace_state_t *state = ecb->dte_state; 10340 dtrace_vstate_t *vstate = &state->dts_vstate; 10341 dtrace_predicate_t *pred; 10342 dtrace_epid_t epid = ecb->dte_epid; 10343 10344 ASSERT(MUTEX_HELD(&dtrace_lock)); 10345 ASSERT(ecb->dte_next == NULL); 10346 ASSERT(ecb->dte_probe == NULL || ecb->dte_probe->dtpr_ecb != ecb); 10347 10348 if ((pred = ecb->dte_predicate) != NULL) 10349 dtrace_predicate_release(pred, vstate); 10350 10351 dtrace_ecb_action_remove(ecb); 10352 10353 ASSERT(state->dts_ecbs[epid - 1] == ecb); 10354 state->dts_ecbs[epid - 1] = NULL; 10355 10356 kmem_free(ecb, sizeof (dtrace_ecb_t)); 10357} 10358 10359static dtrace_ecb_t * 10360dtrace_ecb_create(dtrace_state_t *state, dtrace_probe_t *probe, 10361 dtrace_enabling_t *enab) 10362{ 10363 dtrace_ecb_t *ecb; 10364 dtrace_predicate_t *pred; 10365 dtrace_actdesc_t *act; 10366 dtrace_provider_t *prov; 10367 dtrace_ecbdesc_t *desc = enab->dten_current; 10368 10369 ASSERT(MUTEX_HELD(&dtrace_lock)); 10370 ASSERT(state != NULL); 10371 10372 ecb = dtrace_ecb_add(state, probe); 10373 ecb->dte_uarg = desc->dted_uarg; 10374 10375 if ((pred = desc->dted_pred.dtpdd_predicate) != NULL) { 10376 dtrace_predicate_hold(pred); 10377 ecb->dte_predicate = pred; 10378 } 10379 10380 if (probe != NULL) { 10381 /* 10382 * If the provider shows more leg than the consumer is old 10383 * enough to see, we need to enable the appropriate implicit 10384 * predicate bits to prevent the ecb from activating at 10385 * revealing times. 10386 * 10387 * Providers specifying DTRACE_PRIV_USER at register time 10388 * are stating that they need the /proc-style privilege 10389 * model to be enforced, and this is what DTRACE_COND_OWNER 10390 * and DTRACE_COND_ZONEOWNER will then do at probe time. 10391 */ 10392 prov = probe->dtpr_provider; 10393 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLPROC) && 10394 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10395 ecb->dte_cond |= DTRACE_COND_OWNER; 10396 10397 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_ALLZONE) && 10398 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_USER)) 10399 ecb->dte_cond |= DTRACE_COND_ZONEOWNER; 10400 10401 /* 10402 * If the provider shows us kernel innards and the user 10403 * is lacking sufficient privilege, enable the 10404 * DTRACE_COND_USERMODE implicit predicate. 10405 */ 10406 if (!(state->dts_cred.dcr_visible & DTRACE_CRV_KERNEL) && 10407 (prov->dtpv_priv.dtpp_flags & DTRACE_PRIV_KERNEL)) 10408 ecb->dte_cond |= DTRACE_COND_USERMODE; 10409 } 10410 10411 if (dtrace_ecb_create_cache != NULL) { 10412 /* 10413 * If we have a cached ecb, we'll use its action list instead 10414 * of creating our own (saving both time and space). 10415 */ 10416 dtrace_ecb_t *cached = dtrace_ecb_create_cache; 10417 dtrace_action_t *act = cached->dte_action; 10418 10419 if (act != NULL) { 10420 ASSERT(act->dta_refcnt > 0); 10421 act->dta_refcnt++; 10422 ecb->dte_action = act; 10423 ecb->dte_action_last = cached->dte_action_last; 10424 ecb->dte_needed = cached->dte_needed; 10425 ecb->dte_size = cached->dte_size; 10426 ecb->dte_alignment = cached->dte_alignment; 10427 } 10428 10429 return (ecb); 10430 } 10431 10432 for (act = desc->dted_action; act != NULL; act = act->dtad_next) { 10433 if ((enab->dten_error = dtrace_ecb_action_add(ecb, act)) != 0) { 10434 dtrace_ecb_destroy(ecb); 10435 return (NULL); 10436 } 10437 } 10438 10439 dtrace_ecb_resize(ecb); 10440 10441 return (dtrace_ecb_create_cache = ecb); 10442} 10443 10444static int 10445dtrace_ecb_create_enable(dtrace_probe_t *probe, void *arg) 10446{ 10447 dtrace_ecb_t *ecb; 10448 dtrace_enabling_t *enab = arg; 10449 dtrace_state_t *state = enab->dten_vstate->dtvs_state; 10450 10451 ASSERT(state != NULL); 10452 10453 if (probe != NULL && probe->dtpr_gen < enab->dten_probegen) { 10454 /* 10455 * This probe was created in a generation for which this 10456 * enabling has previously created ECBs; we don't want to 10457 * enable it again, so just kick out. 10458 */ 10459 return (DTRACE_MATCH_NEXT); 10460 } 10461 10462 if ((ecb = dtrace_ecb_create(state, probe, enab)) == NULL) 10463 return (DTRACE_MATCH_DONE); 10464 10465 dtrace_ecb_enable(ecb); 10466 return (DTRACE_MATCH_NEXT); 10467} 10468 10469static dtrace_ecb_t * 10470dtrace_epid2ecb(dtrace_state_t *state, dtrace_epid_t id) 10471{ 10472 dtrace_ecb_t *ecb; 10473 10474 ASSERT(MUTEX_HELD(&dtrace_lock)); 10475 10476 if (id == 0 || id > state->dts_necbs) 10477 return (NULL); 10478 10479 ASSERT(state->dts_necbs > 0 && state->dts_ecbs != NULL); 10480 ASSERT((ecb = state->dts_ecbs[id - 1]) == NULL || ecb->dte_epid == id); 10481 10482 return (state->dts_ecbs[id - 1]); 10483} 10484 10485static dtrace_aggregation_t * 10486dtrace_aggid2agg(dtrace_state_t *state, dtrace_aggid_t id) 10487{ 10488 dtrace_aggregation_t *agg; 10489 10490 ASSERT(MUTEX_HELD(&dtrace_lock)); 10491 10492 if (id == 0 || id > state->dts_naggregations) 10493 return (NULL); 10494 10495 ASSERT(state->dts_naggregations > 0 && state->dts_aggregations != NULL); 10496 ASSERT((agg = state->dts_aggregations[id - 1]) == NULL || 10497 agg->dtag_id == id); 10498 10499 return (state->dts_aggregations[id - 1]); 10500} 10501 10502/* 10503 * DTrace Buffer Functions 10504 * 10505 * The following functions manipulate DTrace buffers. Most of these functions 10506 * are called in the context of establishing or processing consumer state; 10507 * exceptions are explicitly noted. 10508 */ 10509 10510/* 10511 * Note: called from cross call context. This function switches the two 10512 * buffers on a given CPU. The atomicity of this operation is assured by 10513 * disabling interrupts while the actual switch takes place; the disabling of 10514 * interrupts serializes the execution with any execution of dtrace_probe() on 10515 * the same CPU. 10516 */ 10517static void 10518dtrace_buffer_switch(dtrace_buffer_t *buf) 10519{ 10520 caddr_t tomax = buf->dtb_tomax; 10521 caddr_t xamot = buf->dtb_xamot; 10522 dtrace_icookie_t cookie; 10523 10524 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 10525 ASSERT(!(buf->dtb_flags & DTRACEBUF_RING)); 10526 10527 cookie = dtrace_interrupt_disable(); 10528 buf->dtb_tomax = xamot; 10529 buf->dtb_xamot = tomax; 10530 buf->dtb_xamot_drops = buf->dtb_drops; 10531 buf->dtb_xamot_offset = buf->dtb_offset; 10532 buf->dtb_xamot_errors = buf->dtb_errors; 10533 buf->dtb_xamot_flags = buf->dtb_flags; 10534 buf->dtb_offset = 0; 10535 buf->dtb_drops = 0; 10536 buf->dtb_errors = 0; 10537 buf->dtb_flags &= ~(DTRACEBUF_ERROR | DTRACEBUF_DROPPED); 10538 dtrace_interrupt_enable(cookie); 10539} 10540 10541/* 10542 * Note: called from cross call context. This function activates a buffer 10543 * on a CPU. As with dtrace_buffer_switch(), the atomicity of the operation 10544 * is guaranteed by the disabling of interrupts. 10545 */ 10546static void 10547dtrace_buffer_activate(dtrace_state_t *state) 10548{ 10549 dtrace_buffer_t *buf; 10550 dtrace_icookie_t cookie = dtrace_interrupt_disable(); 10551 10552 buf = &state->dts_buffer[curcpu]; 10553 10554 if (buf->dtb_tomax != NULL) { 10555 /* 10556 * We might like to assert that the buffer is marked inactive, 10557 * but this isn't necessarily true: the buffer for the CPU 10558 * that processes the BEGIN probe has its buffer activated 10559 * manually. In this case, we take the (harmless) action 10560 * re-clearing the bit INACTIVE bit. 10561 */ 10562 buf->dtb_flags &= ~DTRACEBUF_INACTIVE; 10563 } 10564 10565 dtrace_interrupt_enable(cookie); 10566} 10567 10568static int 10569dtrace_buffer_alloc(dtrace_buffer_t *bufs, size_t size, int flags, 10570 processorid_t cpu) 10571{ 10572#if defined(sun) 10573 cpu_t *cp; 10574#else 10575 struct pcpu *cp; 10576#endif 10577 dtrace_buffer_t *buf; 10578 10579#if defined(sun) 10580 ASSERT(MUTEX_HELD(&cpu_lock)); 10581 ASSERT(MUTEX_HELD(&dtrace_lock)); 10582 10583 if (size > dtrace_nonroot_maxsize && 10584 !PRIV_POLICY_CHOICE(CRED(), PRIV_ALL, B_FALSE)) 10585 return (EFBIG); 10586 10587 cp = cpu_list; 10588 10589 do { 10590 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10591 continue; 10592 10593 buf = &bufs[cp->cpu_id]; 10594 10595 /* 10596 * If there is already a buffer allocated for this CPU, it 10597 * is only possible that this is a DR event. In this case, 10598 * the buffer size must match our specified size. 10599 */ 10600 if (buf->dtb_tomax != NULL) { 10601 ASSERT(buf->dtb_size == size); 10602 continue; 10603 } 10604 10605 ASSERT(buf->dtb_xamot == NULL); 10606 10607 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10608 goto err; 10609 10610 buf->dtb_size = size; 10611 buf->dtb_flags = flags; 10612 buf->dtb_offset = 0; 10613 buf->dtb_drops = 0; 10614 10615 if (flags & DTRACEBUF_NOSWITCH) 10616 continue; 10617 10618 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10619 goto err; 10620 } while ((cp = cp->cpu_next) != cpu_list); 10621 10622 return (0); 10623 10624err: 10625 cp = cpu_list; 10626 10627 do { 10628 if (cpu != DTRACE_CPUALL && cpu != cp->cpu_id) 10629 continue; 10630 10631 buf = &bufs[cp->cpu_id]; 10632 10633 if (buf->dtb_xamot != NULL) { 10634 ASSERT(buf->dtb_tomax != NULL); 10635 ASSERT(buf->dtb_size == size); 10636 kmem_free(buf->dtb_xamot, size); 10637 } 10638 10639 if (buf->dtb_tomax != NULL) { 10640 ASSERT(buf->dtb_size == size); 10641 kmem_free(buf->dtb_tomax, size); 10642 } 10643 10644 buf->dtb_tomax = NULL; 10645 buf->dtb_xamot = NULL; 10646 buf->dtb_size = 0; 10647 } while ((cp = cp->cpu_next) != cpu_list); 10648 10649 return (ENOMEM); 10650#else 10651 int i; 10652 10653#if defined(__amd64__) 10654 /* 10655 * FreeBSD isn't good at limiting the amount of memory we 10656 * ask to malloc, so let's place a limit here before trying 10657 * to do something that might well end in tears at bedtime. 10658 */ 10659 if (size > physmem * PAGE_SIZE / (128 * (mp_maxid + 1))) 10660 return(ENOMEM); 10661#endif 10662 10663 ASSERT(MUTEX_HELD(&dtrace_lock)); 10664 for (i = 0; i <= mp_maxid; i++) { 10665 if ((cp = pcpu_find(i)) == NULL) 10666 continue; 10667 10668 if (cpu != DTRACE_CPUALL && cpu != i) 10669 continue; 10670 10671 buf = &bufs[i]; 10672 10673 /* 10674 * If there is already a buffer allocated for this CPU, it 10675 * is only possible that this is a DR event. In this case, 10676 * the buffer size must match our specified size. 10677 */ 10678 if (buf->dtb_tomax != NULL) { 10679 ASSERT(buf->dtb_size == size); 10680 continue; 10681 } 10682 10683 ASSERT(buf->dtb_xamot == NULL); 10684 10685 if ((buf->dtb_tomax = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10686 goto err; 10687 10688 buf->dtb_size = size; 10689 buf->dtb_flags = flags; 10690 buf->dtb_offset = 0; 10691 buf->dtb_drops = 0; 10692 10693 if (flags & DTRACEBUF_NOSWITCH) 10694 continue; 10695 10696 if ((buf->dtb_xamot = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 10697 goto err; 10698 } 10699 10700 return (0); 10701 10702err: 10703 /* 10704 * Error allocating memory, so free the buffers that were 10705 * allocated before the failed allocation. 10706 */ 10707 for (i = 0; i <= mp_maxid; i++) { 10708 if ((cp = pcpu_find(i)) == NULL) 10709 continue; 10710 10711 if (cpu != DTRACE_CPUALL && cpu != i) 10712 continue; 10713 10714 buf = &bufs[i]; 10715 10716 if (buf->dtb_xamot != NULL) { 10717 ASSERT(buf->dtb_tomax != NULL); 10718 ASSERT(buf->dtb_size == size); 10719 kmem_free(buf->dtb_xamot, size); 10720 } 10721 10722 if (buf->dtb_tomax != NULL) { 10723 ASSERT(buf->dtb_size == size); 10724 kmem_free(buf->dtb_tomax, size); 10725 } 10726 10727 buf->dtb_tomax = NULL; 10728 buf->dtb_xamot = NULL; 10729 buf->dtb_size = 0; 10730 10731 } 10732 10733 return (ENOMEM); 10734#endif 10735} 10736 10737/* 10738 * Note: called from probe context. This function just increments the drop 10739 * count on a buffer. It has been made a function to allow for the 10740 * possibility of understanding the source of mysterious drop counts. (A 10741 * problem for which one may be particularly disappointed that DTrace cannot 10742 * be used to understand DTrace.) 10743 */ 10744static void 10745dtrace_buffer_drop(dtrace_buffer_t *buf) 10746{ 10747 buf->dtb_drops++; 10748} 10749 10750/* 10751 * Note: called from probe context. This function is called to reserve space 10752 * in a buffer. If mstate is non-NULL, sets the scratch base and size in the 10753 * mstate. Returns the new offset in the buffer, or a negative value if an 10754 * error has occurred. 10755 */ 10756static intptr_t 10757dtrace_buffer_reserve(dtrace_buffer_t *buf, size_t needed, size_t align, 10758 dtrace_state_t *state, dtrace_mstate_t *mstate) 10759{ 10760 intptr_t offs = buf->dtb_offset, soffs; 10761 intptr_t woffs; 10762 caddr_t tomax; 10763 size_t total; 10764 10765 if (buf->dtb_flags & DTRACEBUF_INACTIVE) 10766 return (-1); 10767 10768 if ((tomax = buf->dtb_tomax) == NULL) { 10769 dtrace_buffer_drop(buf); 10770 return (-1); 10771 } 10772 10773 if (!(buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL))) { 10774 while (offs & (align - 1)) { 10775 /* 10776 * Assert that our alignment is off by a number which 10777 * is itself sizeof (uint32_t) aligned. 10778 */ 10779 ASSERT(!((align - (offs & (align - 1))) & 10780 (sizeof (uint32_t) - 1))); 10781 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10782 offs += sizeof (uint32_t); 10783 } 10784 10785 if ((soffs = offs + needed) > buf->dtb_size) { 10786 dtrace_buffer_drop(buf); 10787 return (-1); 10788 } 10789 10790 if (mstate == NULL) 10791 return (offs); 10792 10793 mstate->dtms_scratch_base = (uintptr_t)tomax + soffs; 10794 mstate->dtms_scratch_size = buf->dtb_size - soffs; 10795 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10796 10797 return (offs); 10798 } 10799 10800 if (buf->dtb_flags & DTRACEBUF_FILL) { 10801 if (state->dts_activity != DTRACE_ACTIVITY_COOLDOWN && 10802 (buf->dtb_flags & DTRACEBUF_FULL)) 10803 return (-1); 10804 goto out; 10805 } 10806 10807 total = needed + (offs & (align - 1)); 10808 10809 /* 10810 * For a ring buffer, life is quite a bit more complicated. Before 10811 * we can store any padding, we need to adjust our wrapping offset. 10812 * (If we've never before wrapped or we're not about to, no adjustment 10813 * is required.) 10814 */ 10815 if ((buf->dtb_flags & DTRACEBUF_WRAPPED) || 10816 offs + total > buf->dtb_size) { 10817 woffs = buf->dtb_xamot_offset; 10818 10819 if (offs + total > buf->dtb_size) { 10820 /* 10821 * We can't fit in the end of the buffer. First, a 10822 * sanity check that we can fit in the buffer at all. 10823 */ 10824 if (total > buf->dtb_size) { 10825 dtrace_buffer_drop(buf); 10826 return (-1); 10827 } 10828 10829 /* 10830 * We're going to be storing at the top of the buffer, 10831 * so now we need to deal with the wrapped offset. We 10832 * only reset our wrapped offset to 0 if it is 10833 * currently greater than the current offset. If it 10834 * is less than the current offset, it is because a 10835 * previous allocation induced a wrap -- but the 10836 * allocation didn't subsequently take the space due 10837 * to an error or false predicate evaluation. In this 10838 * case, we'll just leave the wrapped offset alone: if 10839 * the wrapped offset hasn't been advanced far enough 10840 * for this allocation, it will be adjusted in the 10841 * lower loop. 10842 */ 10843 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 10844 if (woffs >= offs) 10845 woffs = 0; 10846 } else { 10847 woffs = 0; 10848 } 10849 10850 /* 10851 * Now we know that we're going to be storing to the 10852 * top of the buffer and that there is room for us 10853 * there. We need to clear the buffer from the current 10854 * offset to the end (there may be old gunk there). 10855 */ 10856 while (offs < buf->dtb_size) 10857 tomax[offs++] = 0; 10858 10859 /* 10860 * We need to set our offset to zero. And because we 10861 * are wrapping, we need to set the bit indicating as 10862 * much. We can also adjust our needed space back 10863 * down to the space required by the ECB -- we know 10864 * that the top of the buffer is aligned. 10865 */ 10866 offs = 0; 10867 total = needed; 10868 buf->dtb_flags |= DTRACEBUF_WRAPPED; 10869 } else { 10870 /* 10871 * There is room for us in the buffer, so we simply 10872 * need to check the wrapped offset. 10873 */ 10874 if (woffs < offs) { 10875 /* 10876 * The wrapped offset is less than the offset. 10877 * This can happen if we allocated buffer space 10878 * that induced a wrap, but then we didn't 10879 * subsequently take the space due to an error 10880 * or false predicate evaluation. This is 10881 * okay; we know that _this_ allocation isn't 10882 * going to induce a wrap. We still can't 10883 * reset the wrapped offset to be zero, 10884 * however: the space may have been trashed in 10885 * the previous failed probe attempt. But at 10886 * least the wrapped offset doesn't need to 10887 * be adjusted at all... 10888 */ 10889 goto out; 10890 } 10891 } 10892 10893 while (offs + total > woffs) { 10894 dtrace_epid_t epid = *(uint32_t *)(tomax + woffs); 10895 size_t size; 10896 10897 if (epid == DTRACE_EPIDNONE) { 10898 size = sizeof (uint32_t); 10899 } else { 10900 ASSERT(epid <= state->dts_necbs); 10901 ASSERT(state->dts_ecbs[epid - 1] != NULL); 10902 10903 size = state->dts_ecbs[epid - 1]->dte_size; 10904 } 10905 10906 ASSERT(woffs + size <= buf->dtb_size); 10907 ASSERT(size != 0); 10908 10909 if (woffs + size == buf->dtb_size) { 10910 /* 10911 * We've reached the end of the buffer; we want 10912 * to set the wrapped offset to 0 and break 10913 * out. However, if the offs is 0, then we're 10914 * in a strange edge-condition: the amount of 10915 * space that we want to reserve plus the size 10916 * of the record that we're overwriting is 10917 * greater than the size of the buffer. This 10918 * is problematic because if we reserve the 10919 * space but subsequently don't consume it (due 10920 * to a failed predicate or error) the wrapped 10921 * offset will be 0 -- yet the EPID at offset 0 10922 * will not be committed. This situation is 10923 * relatively easy to deal with: if we're in 10924 * this case, the buffer is indistinguishable 10925 * from one that hasn't wrapped; we need only 10926 * finish the job by clearing the wrapped bit, 10927 * explicitly setting the offset to be 0, and 10928 * zero'ing out the old data in the buffer. 10929 */ 10930 if (offs == 0) { 10931 buf->dtb_flags &= ~DTRACEBUF_WRAPPED; 10932 buf->dtb_offset = 0; 10933 woffs = total; 10934 10935 while (woffs < buf->dtb_size) 10936 tomax[woffs++] = 0; 10937 } 10938 10939 woffs = 0; 10940 break; 10941 } 10942 10943 woffs += size; 10944 } 10945 10946 /* 10947 * We have a wrapped offset. It may be that the wrapped offset 10948 * has become zero -- that's okay. 10949 */ 10950 buf->dtb_xamot_offset = woffs; 10951 } 10952 10953out: 10954 /* 10955 * Now we can plow the buffer with any necessary padding. 10956 */ 10957 while (offs & (align - 1)) { 10958 /* 10959 * Assert that our alignment is off by a number which 10960 * is itself sizeof (uint32_t) aligned. 10961 */ 10962 ASSERT(!((align - (offs & (align - 1))) & 10963 (sizeof (uint32_t) - 1))); 10964 DTRACE_STORE(uint32_t, tomax, offs, DTRACE_EPIDNONE); 10965 offs += sizeof (uint32_t); 10966 } 10967 10968 if (buf->dtb_flags & DTRACEBUF_FILL) { 10969 if (offs + needed > buf->dtb_size - state->dts_reserve) { 10970 buf->dtb_flags |= DTRACEBUF_FULL; 10971 return (-1); 10972 } 10973 } 10974 10975 if (mstate == NULL) 10976 return (offs); 10977 10978 /* 10979 * For ring buffers and fill buffers, the scratch space is always 10980 * the inactive buffer. 10981 */ 10982 mstate->dtms_scratch_base = (uintptr_t)buf->dtb_xamot; 10983 mstate->dtms_scratch_size = buf->dtb_size; 10984 mstate->dtms_scratch_ptr = mstate->dtms_scratch_base; 10985 10986 return (offs); 10987} 10988 10989static void 10990dtrace_buffer_polish(dtrace_buffer_t *buf) 10991{ 10992 ASSERT(buf->dtb_flags & DTRACEBUF_RING); 10993 ASSERT(MUTEX_HELD(&dtrace_lock)); 10994 10995 if (!(buf->dtb_flags & DTRACEBUF_WRAPPED)) 10996 return; 10997 10998 /* 10999 * We need to polish the ring buffer. There are three cases: 11000 * 11001 * - The first (and presumably most common) is that there is no gap 11002 * between the buffer offset and the wrapped offset. In this case, 11003 * there is nothing in the buffer that isn't valid data; we can 11004 * mark the buffer as polished and return. 11005 * 11006 * - The second (less common than the first but still more common 11007 * than the third) is that there is a gap between the buffer offset 11008 * and the wrapped offset, and the wrapped offset is larger than the 11009 * buffer offset. This can happen because of an alignment issue, or 11010 * can happen because of a call to dtrace_buffer_reserve() that 11011 * didn't subsequently consume the buffer space. In this case, 11012 * we need to zero the data from the buffer offset to the wrapped 11013 * offset. 11014 * 11015 * - The third (and least common) is that there is a gap between the 11016 * buffer offset and the wrapped offset, but the wrapped offset is 11017 * _less_ than the buffer offset. This can only happen because a 11018 * call to dtrace_buffer_reserve() induced a wrap, but the space 11019 * was not subsequently consumed. In this case, we need to zero the 11020 * space from the offset to the end of the buffer _and_ from the 11021 * top of the buffer to the wrapped offset. 11022 */ 11023 if (buf->dtb_offset < buf->dtb_xamot_offset) { 11024 bzero(buf->dtb_tomax + buf->dtb_offset, 11025 buf->dtb_xamot_offset - buf->dtb_offset); 11026 } 11027 11028 if (buf->dtb_offset > buf->dtb_xamot_offset) { 11029 bzero(buf->dtb_tomax + buf->dtb_offset, 11030 buf->dtb_size - buf->dtb_offset); 11031 bzero(buf->dtb_tomax, buf->dtb_xamot_offset); 11032 } 11033} 11034 11035static void 11036dtrace_buffer_free(dtrace_buffer_t *bufs) 11037{ 11038 int i; 11039 11040 for (i = 0; i < NCPU; i++) { 11041 dtrace_buffer_t *buf = &bufs[i]; 11042 11043 if (buf->dtb_tomax == NULL) { 11044 ASSERT(buf->dtb_xamot == NULL); 11045 ASSERT(buf->dtb_size == 0); 11046 continue; 11047 } 11048 11049 if (buf->dtb_xamot != NULL) { 11050 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 11051 kmem_free(buf->dtb_xamot, buf->dtb_size); 11052 } 11053 11054 kmem_free(buf->dtb_tomax, buf->dtb_size); 11055 buf->dtb_size = 0; 11056 buf->dtb_tomax = NULL; 11057 buf->dtb_xamot = NULL; 11058 } 11059} 11060 11061/* 11062 * DTrace Enabling Functions 11063 */ 11064static dtrace_enabling_t * 11065dtrace_enabling_create(dtrace_vstate_t *vstate) 11066{ 11067 dtrace_enabling_t *enab; 11068 11069 enab = kmem_zalloc(sizeof (dtrace_enabling_t), KM_SLEEP); 11070 enab->dten_vstate = vstate; 11071 11072 return (enab); 11073} 11074 11075static void 11076dtrace_enabling_add(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb) 11077{ 11078 dtrace_ecbdesc_t **ndesc; 11079 size_t osize, nsize; 11080 11081 /* 11082 * We can't add to enablings after we've enabled them, or after we've 11083 * retained them. 11084 */ 11085 ASSERT(enab->dten_probegen == 0); 11086 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11087 11088 if (enab->dten_ndesc < enab->dten_maxdesc) { 11089 enab->dten_desc[enab->dten_ndesc++] = ecb; 11090 return; 11091 } 11092 11093 osize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11094 11095 if (enab->dten_maxdesc == 0) { 11096 enab->dten_maxdesc = 1; 11097 } else { 11098 enab->dten_maxdesc <<= 1; 11099 } 11100 11101 ASSERT(enab->dten_ndesc < enab->dten_maxdesc); 11102 11103 nsize = enab->dten_maxdesc * sizeof (dtrace_enabling_t *); 11104 ndesc = kmem_zalloc(nsize, KM_SLEEP); 11105 bcopy(enab->dten_desc, ndesc, osize); 11106 if (enab->dten_desc != NULL) 11107 kmem_free(enab->dten_desc, osize); 11108 11109 enab->dten_desc = ndesc; 11110 enab->dten_desc[enab->dten_ndesc++] = ecb; 11111} 11112 11113static void 11114dtrace_enabling_addlike(dtrace_enabling_t *enab, dtrace_ecbdesc_t *ecb, 11115 dtrace_probedesc_t *pd) 11116{ 11117 dtrace_ecbdesc_t *new; 11118 dtrace_predicate_t *pred; 11119 dtrace_actdesc_t *act; 11120 11121 /* 11122 * We're going to create a new ECB description that matches the 11123 * specified ECB in every way, but has the specified probe description. 11124 */ 11125 new = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 11126 11127 if ((pred = ecb->dted_pred.dtpdd_predicate) != NULL) 11128 dtrace_predicate_hold(pred); 11129 11130 for (act = ecb->dted_action; act != NULL; act = act->dtad_next) 11131 dtrace_actdesc_hold(act); 11132 11133 new->dted_action = ecb->dted_action; 11134 new->dted_pred = ecb->dted_pred; 11135 new->dted_probe = *pd; 11136 new->dted_uarg = ecb->dted_uarg; 11137 11138 dtrace_enabling_add(enab, new); 11139} 11140 11141static void 11142dtrace_enabling_dump(dtrace_enabling_t *enab) 11143{ 11144 int i; 11145 11146 for (i = 0; i < enab->dten_ndesc; i++) { 11147 dtrace_probedesc_t *desc = &enab->dten_desc[i]->dted_probe; 11148 11149 cmn_err(CE_NOTE, "enabling probe %d (%s:%s:%s:%s)", i, 11150 desc->dtpd_provider, desc->dtpd_mod, 11151 desc->dtpd_func, desc->dtpd_name); 11152 } 11153} 11154 11155static void 11156dtrace_enabling_destroy(dtrace_enabling_t *enab) 11157{ 11158 int i; 11159 dtrace_ecbdesc_t *ep; 11160 dtrace_vstate_t *vstate = enab->dten_vstate; 11161 11162 ASSERT(MUTEX_HELD(&dtrace_lock)); 11163 11164 for (i = 0; i < enab->dten_ndesc; i++) { 11165 dtrace_actdesc_t *act, *next; 11166 dtrace_predicate_t *pred; 11167 11168 ep = enab->dten_desc[i]; 11169 11170 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) 11171 dtrace_predicate_release(pred, vstate); 11172 11173 for (act = ep->dted_action; act != NULL; act = next) { 11174 next = act->dtad_next; 11175 dtrace_actdesc_release(act, vstate); 11176 } 11177 11178 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 11179 } 11180 11181 if (enab->dten_desc != NULL) 11182 kmem_free(enab->dten_desc, 11183 enab->dten_maxdesc * sizeof (dtrace_enabling_t *)); 11184 11185 /* 11186 * If this was a retained enabling, decrement the dts_nretained count 11187 * and take it off of the dtrace_retained list. 11188 */ 11189 if (enab->dten_prev != NULL || enab->dten_next != NULL || 11190 dtrace_retained == enab) { 11191 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11192 ASSERT(enab->dten_vstate->dtvs_state->dts_nretained > 0); 11193 enab->dten_vstate->dtvs_state->dts_nretained--; 11194 } 11195 11196 if (enab->dten_prev == NULL) { 11197 if (dtrace_retained == enab) { 11198 dtrace_retained = enab->dten_next; 11199 11200 if (dtrace_retained != NULL) 11201 dtrace_retained->dten_prev = NULL; 11202 } 11203 } else { 11204 ASSERT(enab != dtrace_retained); 11205 ASSERT(dtrace_retained != NULL); 11206 enab->dten_prev->dten_next = enab->dten_next; 11207 } 11208 11209 if (enab->dten_next != NULL) { 11210 ASSERT(dtrace_retained != NULL); 11211 enab->dten_next->dten_prev = enab->dten_prev; 11212 } 11213 11214 kmem_free(enab, sizeof (dtrace_enabling_t)); 11215} 11216 11217static int 11218dtrace_enabling_retain(dtrace_enabling_t *enab) 11219{ 11220 dtrace_state_t *state; 11221 11222 ASSERT(MUTEX_HELD(&dtrace_lock)); 11223 ASSERT(enab->dten_next == NULL && enab->dten_prev == NULL); 11224 ASSERT(enab->dten_vstate != NULL); 11225 11226 state = enab->dten_vstate->dtvs_state; 11227 ASSERT(state != NULL); 11228 11229 /* 11230 * We only allow each state to retain dtrace_retain_max enablings. 11231 */ 11232 if (state->dts_nretained >= dtrace_retain_max) 11233 return (ENOSPC); 11234 11235 state->dts_nretained++; 11236 11237 if (dtrace_retained == NULL) { 11238 dtrace_retained = enab; 11239 return (0); 11240 } 11241 11242 enab->dten_next = dtrace_retained; 11243 dtrace_retained->dten_prev = enab; 11244 dtrace_retained = enab; 11245 11246 return (0); 11247} 11248 11249static int 11250dtrace_enabling_replicate(dtrace_state_t *state, dtrace_probedesc_t *match, 11251 dtrace_probedesc_t *create) 11252{ 11253 dtrace_enabling_t *new, *enab; 11254 int found = 0, err = ENOENT; 11255 11256 ASSERT(MUTEX_HELD(&dtrace_lock)); 11257 ASSERT(strlen(match->dtpd_provider) < DTRACE_PROVNAMELEN); 11258 ASSERT(strlen(match->dtpd_mod) < DTRACE_MODNAMELEN); 11259 ASSERT(strlen(match->dtpd_func) < DTRACE_FUNCNAMELEN); 11260 ASSERT(strlen(match->dtpd_name) < DTRACE_NAMELEN); 11261 11262 new = dtrace_enabling_create(&state->dts_vstate); 11263 11264 /* 11265 * Iterate over all retained enablings, looking for enablings that 11266 * match the specified state. 11267 */ 11268 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11269 int i; 11270 11271 /* 11272 * dtvs_state can only be NULL for helper enablings -- and 11273 * helper enablings can't be retained. 11274 */ 11275 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11276 11277 if (enab->dten_vstate->dtvs_state != state) 11278 continue; 11279 11280 /* 11281 * Now iterate over each probe description; we're looking for 11282 * an exact match to the specified probe description. 11283 */ 11284 for (i = 0; i < enab->dten_ndesc; i++) { 11285 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11286 dtrace_probedesc_t *pd = &ep->dted_probe; 11287 11288 if (strcmp(pd->dtpd_provider, match->dtpd_provider)) 11289 continue; 11290 11291 if (strcmp(pd->dtpd_mod, match->dtpd_mod)) 11292 continue; 11293 11294 if (strcmp(pd->dtpd_func, match->dtpd_func)) 11295 continue; 11296 11297 if (strcmp(pd->dtpd_name, match->dtpd_name)) 11298 continue; 11299 11300 /* 11301 * We have a winning probe! Add it to our growing 11302 * enabling. 11303 */ 11304 found = 1; 11305 dtrace_enabling_addlike(new, ep, create); 11306 } 11307 } 11308 11309 if (!found || (err = dtrace_enabling_retain(new)) != 0) { 11310 dtrace_enabling_destroy(new); 11311 return (err); 11312 } 11313 11314 return (0); 11315} 11316 11317static void 11318dtrace_enabling_retract(dtrace_state_t *state) 11319{ 11320 dtrace_enabling_t *enab, *next; 11321 11322 ASSERT(MUTEX_HELD(&dtrace_lock)); 11323 11324 /* 11325 * Iterate over all retained enablings, destroy the enablings retained 11326 * for the specified state. 11327 */ 11328 for (enab = dtrace_retained; enab != NULL; enab = next) { 11329 next = enab->dten_next; 11330 11331 /* 11332 * dtvs_state can only be NULL for helper enablings -- and 11333 * helper enablings can't be retained. 11334 */ 11335 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11336 11337 if (enab->dten_vstate->dtvs_state == state) { 11338 ASSERT(state->dts_nretained > 0); 11339 dtrace_enabling_destroy(enab); 11340 } 11341 } 11342 11343 ASSERT(state->dts_nretained == 0); 11344} 11345 11346static int 11347dtrace_enabling_match(dtrace_enabling_t *enab, int *nmatched) 11348{ 11349 int i = 0; 11350 int matched = 0; 11351 11352 ASSERT(MUTEX_HELD(&cpu_lock)); 11353 ASSERT(MUTEX_HELD(&dtrace_lock)); 11354 11355 for (i = 0; i < enab->dten_ndesc; i++) { 11356 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 11357 11358 enab->dten_current = ep; 11359 enab->dten_error = 0; 11360 11361 matched += dtrace_probe_enable(&ep->dted_probe, enab); 11362 11363 if (enab->dten_error != 0) { 11364 /* 11365 * If we get an error half-way through enabling the 11366 * probes, we kick out -- perhaps with some number of 11367 * them enabled. Leaving enabled probes enabled may 11368 * be slightly confusing for user-level, but we expect 11369 * that no one will attempt to actually drive on in 11370 * the face of such errors. If this is an anonymous 11371 * enabling (indicated with a NULL nmatched pointer), 11372 * we cmn_err() a message. We aren't expecting to 11373 * get such an error -- such as it can exist at all, 11374 * it would be a result of corrupted DOF in the driver 11375 * properties. 11376 */ 11377 if (nmatched == NULL) { 11378 cmn_err(CE_WARN, "dtrace_enabling_match() " 11379 "error on %p: %d", (void *)ep, 11380 enab->dten_error); 11381 } 11382 11383 return (enab->dten_error); 11384 } 11385 } 11386 11387 enab->dten_probegen = dtrace_probegen; 11388 if (nmatched != NULL) 11389 *nmatched = matched; 11390 11391 return (0); 11392} 11393 11394static void 11395dtrace_enabling_matchall(void) 11396{ 11397 dtrace_enabling_t *enab; 11398 11399 mutex_enter(&cpu_lock); 11400 mutex_enter(&dtrace_lock); 11401 11402 /* 11403 * Because we can be called after dtrace_detach() has been called, we 11404 * cannot assert that there are retained enablings. We can safely 11405 * load from dtrace_retained, however: the taskq_destroy() at the 11406 * end of dtrace_detach() will block pending our completion. 11407 */ 11408 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) 11409 (void) dtrace_enabling_match(enab, NULL); 11410 11411 mutex_exit(&dtrace_lock); 11412 mutex_exit(&cpu_lock); 11413} 11414 11415/* 11416 * If an enabling is to be enabled without having matched probes (that is, if 11417 * dtrace_state_go() is to be called on the underlying dtrace_state_t), the 11418 * enabling must be _primed_ by creating an ECB for every ECB description. 11419 * This must be done to assure that we know the number of speculations, the 11420 * number of aggregations, the minimum buffer size needed, etc. before we 11421 * transition out of DTRACE_ACTIVITY_INACTIVE. To do this without actually 11422 * enabling any probes, we create ECBs for every ECB decription, but with a 11423 * NULL probe -- which is exactly what this function does. 11424 */ 11425static void 11426dtrace_enabling_prime(dtrace_state_t *state) 11427{ 11428 dtrace_enabling_t *enab; 11429 int i; 11430 11431 for (enab = dtrace_retained; enab != NULL; enab = enab->dten_next) { 11432 ASSERT(enab->dten_vstate->dtvs_state != NULL); 11433 11434 if (enab->dten_vstate->dtvs_state != state) 11435 continue; 11436 11437 /* 11438 * We don't want to prime an enabling more than once, lest 11439 * we allow a malicious user to induce resource exhaustion. 11440 * (The ECBs that result from priming an enabling aren't 11441 * leaked -- but they also aren't deallocated until the 11442 * consumer state is destroyed.) 11443 */ 11444 if (enab->dten_primed) 11445 continue; 11446 11447 for (i = 0; i < enab->dten_ndesc; i++) { 11448 enab->dten_current = enab->dten_desc[i]; 11449 (void) dtrace_probe_enable(NULL, enab); 11450 } 11451 11452 enab->dten_primed = 1; 11453 } 11454} 11455 11456/* 11457 * Called to indicate that probes should be provided due to retained 11458 * enablings. This is implemented in terms of dtrace_probe_provide(), but it 11459 * must take an initial lap through the enabling calling the dtps_provide() 11460 * entry point explicitly to allow for autocreated probes. 11461 */ 11462static void 11463dtrace_enabling_provide(dtrace_provider_t *prv) 11464{ 11465 int i, all = 0; 11466 dtrace_probedesc_t desc; 11467 11468 ASSERT(MUTEX_HELD(&dtrace_lock)); 11469 ASSERT(MUTEX_HELD(&dtrace_provider_lock)); 11470 11471 if (prv == NULL) { 11472 all = 1; 11473 prv = dtrace_provider; 11474 } 11475 11476 do { 11477 dtrace_enabling_t *enab = dtrace_retained; 11478 void *parg = prv->dtpv_arg; 11479 11480 for (; enab != NULL; enab = enab->dten_next) { 11481 for (i = 0; i < enab->dten_ndesc; i++) { 11482 desc = enab->dten_desc[i]->dted_probe; 11483 mutex_exit(&dtrace_lock); 11484 prv->dtpv_pops.dtps_provide(parg, &desc); 11485 mutex_enter(&dtrace_lock); 11486 } 11487 } 11488 } while (all && (prv = prv->dtpv_next) != NULL); 11489 11490 mutex_exit(&dtrace_lock); 11491 dtrace_probe_provide(NULL, all ? NULL : prv); 11492 mutex_enter(&dtrace_lock); 11493} 11494 11495/* 11496 * DTrace DOF Functions 11497 */ 11498/*ARGSUSED*/ 11499static void 11500dtrace_dof_error(dof_hdr_t *dof, const char *str) 11501{ 11502 if (dtrace_err_verbose) 11503 cmn_err(CE_WARN, "failed to process DOF: %s", str); 11504 11505#ifdef DTRACE_ERRDEBUG 11506 dtrace_errdebug(str); 11507#endif 11508} 11509 11510/* 11511 * Create DOF out of a currently enabled state. Right now, we only create 11512 * DOF containing the run-time options -- but this could be expanded to create 11513 * complete DOF representing the enabled state. 11514 */ 11515static dof_hdr_t * 11516dtrace_dof_create(dtrace_state_t *state) 11517{ 11518 dof_hdr_t *dof; 11519 dof_sec_t *sec; 11520 dof_optdesc_t *opt; 11521 int i, len = sizeof (dof_hdr_t) + 11522 roundup(sizeof (dof_sec_t), sizeof (uint64_t)) + 11523 sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11524 11525 ASSERT(MUTEX_HELD(&dtrace_lock)); 11526 11527 dof = kmem_zalloc(len, KM_SLEEP); 11528 dof->dofh_ident[DOF_ID_MAG0] = DOF_MAG_MAG0; 11529 dof->dofh_ident[DOF_ID_MAG1] = DOF_MAG_MAG1; 11530 dof->dofh_ident[DOF_ID_MAG2] = DOF_MAG_MAG2; 11531 dof->dofh_ident[DOF_ID_MAG3] = DOF_MAG_MAG3; 11532 11533 dof->dofh_ident[DOF_ID_MODEL] = DOF_MODEL_NATIVE; 11534 dof->dofh_ident[DOF_ID_ENCODING] = DOF_ENCODE_NATIVE; 11535 dof->dofh_ident[DOF_ID_VERSION] = DOF_VERSION; 11536 dof->dofh_ident[DOF_ID_DIFVERS] = DIF_VERSION; 11537 dof->dofh_ident[DOF_ID_DIFIREG] = DIF_DIR_NREGS; 11538 dof->dofh_ident[DOF_ID_DIFTREG] = DIF_DTR_NREGS; 11539 11540 dof->dofh_flags = 0; 11541 dof->dofh_hdrsize = sizeof (dof_hdr_t); 11542 dof->dofh_secsize = sizeof (dof_sec_t); 11543 dof->dofh_secnum = 1; /* only DOF_SECT_OPTDESC */ 11544 dof->dofh_secoff = sizeof (dof_hdr_t); 11545 dof->dofh_loadsz = len; 11546 dof->dofh_filesz = len; 11547 dof->dofh_pad = 0; 11548 11549 /* 11550 * Fill in the option section header... 11551 */ 11552 sec = (dof_sec_t *)((uintptr_t)dof + sizeof (dof_hdr_t)); 11553 sec->dofs_type = DOF_SECT_OPTDESC; 11554 sec->dofs_align = sizeof (uint64_t); 11555 sec->dofs_flags = DOF_SECF_LOAD; 11556 sec->dofs_entsize = sizeof (dof_optdesc_t); 11557 11558 opt = (dof_optdesc_t *)((uintptr_t)sec + 11559 roundup(sizeof (dof_sec_t), sizeof (uint64_t))); 11560 11561 sec->dofs_offset = (uintptr_t)opt - (uintptr_t)dof; 11562 sec->dofs_size = sizeof (dof_optdesc_t) * DTRACEOPT_MAX; 11563 11564 for (i = 0; i < DTRACEOPT_MAX; i++) { 11565 opt[i].dofo_option = i; 11566 opt[i].dofo_strtab = DOF_SECIDX_NONE; 11567 opt[i].dofo_value = state->dts_options[i]; 11568 } 11569 11570 return (dof); 11571} 11572 11573static dof_hdr_t * 11574dtrace_dof_copyin(uintptr_t uarg, int *errp) 11575{ 11576 dof_hdr_t hdr, *dof; 11577 11578 ASSERT(!MUTEX_HELD(&dtrace_lock)); 11579 11580 /* 11581 * First, we're going to copyin() the sizeof (dof_hdr_t). 11582 */ 11583 if (copyin((void *)uarg, &hdr, sizeof (hdr)) != 0) { 11584 dtrace_dof_error(NULL, "failed to copyin DOF header"); 11585 *errp = EFAULT; 11586 return (NULL); 11587 } 11588 11589 /* 11590 * Now we'll allocate the entire DOF and copy it in -- provided 11591 * that the length isn't outrageous. 11592 */ 11593 if (hdr.dofh_loadsz >= dtrace_dof_maxsize) { 11594 dtrace_dof_error(&hdr, "load size exceeds maximum"); 11595 *errp = E2BIG; 11596 return (NULL); 11597 } 11598 11599 if (hdr.dofh_loadsz < sizeof (hdr)) { 11600 dtrace_dof_error(&hdr, "invalid load size"); 11601 *errp = EINVAL; 11602 return (NULL); 11603 } 11604 11605 dof = kmem_alloc(hdr.dofh_loadsz, KM_SLEEP); 11606 11607 if (copyin((void *)uarg, dof, hdr.dofh_loadsz) != 0) { 11608 kmem_free(dof, hdr.dofh_loadsz); 11609 *errp = EFAULT; 11610 return (NULL); 11611 } 11612 11613 return (dof); 11614} 11615 11616#if !defined(sun) 11617static __inline uchar_t 11618dtrace_dof_char(char c) { 11619 switch (c) { 11620 case '0': 11621 case '1': 11622 case '2': 11623 case '3': 11624 case '4': 11625 case '5': 11626 case '6': 11627 case '7': 11628 case '8': 11629 case '9': 11630 return (c - '0'); 11631 case 'A': 11632 case 'B': 11633 case 'C': 11634 case 'D': 11635 case 'E': 11636 case 'F': 11637 return (c - 'A' + 10); 11638 case 'a': 11639 case 'b': 11640 case 'c': 11641 case 'd': 11642 case 'e': 11643 case 'f': 11644 return (c - 'a' + 10); 11645 } 11646 /* Should not reach here. */ 11647 return (0); 11648} 11649#endif 11650 11651static dof_hdr_t * 11652dtrace_dof_property(const char *name) 11653{ 11654 uchar_t *buf; 11655 uint64_t loadsz; 11656 unsigned int len, i; 11657 dof_hdr_t *dof; 11658 11659#if defined(sun) 11660 /* 11661 * Unfortunately, array of values in .conf files are always (and 11662 * only) interpreted to be integer arrays. We must read our DOF 11663 * as an integer array, and then squeeze it into a byte array. 11664 */ 11665 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dtrace_devi, 0, 11666 (char *)name, (int **)&buf, &len) != DDI_PROP_SUCCESS) 11667 return (NULL); 11668 11669 for (i = 0; i < len; i++) 11670 buf[i] = (uchar_t)(((int *)buf)[i]); 11671 11672 if (len < sizeof (dof_hdr_t)) { 11673 ddi_prop_free(buf); 11674 dtrace_dof_error(NULL, "truncated header"); 11675 return (NULL); 11676 } 11677 11678 if (len < (loadsz = ((dof_hdr_t *)buf)->dofh_loadsz)) { 11679 ddi_prop_free(buf); 11680 dtrace_dof_error(NULL, "truncated DOF"); 11681 return (NULL); 11682 } 11683 11684 if (loadsz >= dtrace_dof_maxsize) { 11685 ddi_prop_free(buf); 11686 dtrace_dof_error(NULL, "oversized DOF"); 11687 return (NULL); 11688 } 11689 11690 dof = kmem_alloc(loadsz, KM_SLEEP); 11691 bcopy(buf, dof, loadsz); 11692 ddi_prop_free(buf); 11693#else 11694 char *p; 11695 char *p_env; 11696 11697 if ((p_env = getenv(name)) == NULL) 11698 return (NULL); 11699 11700 len = strlen(p_env) / 2; 11701 11702 buf = kmem_alloc(len, KM_SLEEP); 11703 11704 dof = (dof_hdr_t *) buf; 11705 11706 p = p_env; 11707 11708 for (i = 0; i < len; i++) { 11709 buf[i] = (dtrace_dof_char(p[0]) << 4) | 11710 dtrace_dof_char(p[1]); 11711 p += 2; 11712 } 11713 11714 freeenv(p_env); 11715 11716 if (len < sizeof (dof_hdr_t)) { 11717 kmem_free(buf, 0); 11718 dtrace_dof_error(NULL, "truncated header"); 11719 return (NULL); 11720 } 11721 11722 if (len < (loadsz = dof->dofh_loadsz)) { 11723 kmem_free(buf, 0); 11724 dtrace_dof_error(NULL, "truncated DOF"); 11725 return (NULL); 11726 } 11727 11728 if (loadsz >= dtrace_dof_maxsize) { 11729 kmem_free(buf, 0); 11730 dtrace_dof_error(NULL, "oversized DOF"); 11731 return (NULL); 11732 } 11733#endif 11734 11735 return (dof); 11736} 11737 11738static void 11739dtrace_dof_destroy(dof_hdr_t *dof) 11740{ 11741 kmem_free(dof, dof->dofh_loadsz); 11742} 11743 11744/* 11745 * Return the dof_sec_t pointer corresponding to a given section index. If the 11746 * index is not valid, dtrace_dof_error() is called and NULL is returned. If 11747 * a type other than DOF_SECT_NONE is specified, the header is checked against 11748 * this type and NULL is returned if the types do not match. 11749 */ 11750static dof_sec_t * 11751dtrace_dof_sect(dof_hdr_t *dof, uint32_t type, dof_secidx_t i) 11752{ 11753 dof_sec_t *sec = (dof_sec_t *)(uintptr_t) 11754 ((uintptr_t)dof + dof->dofh_secoff + i * dof->dofh_secsize); 11755 11756 if (i >= dof->dofh_secnum) { 11757 dtrace_dof_error(dof, "referenced section index is invalid"); 11758 return (NULL); 11759 } 11760 11761 if (!(sec->dofs_flags & DOF_SECF_LOAD)) { 11762 dtrace_dof_error(dof, "referenced section is not loadable"); 11763 return (NULL); 11764 } 11765 11766 if (type != DOF_SECT_NONE && type != sec->dofs_type) { 11767 dtrace_dof_error(dof, "referenced section is the wrong type"); 11768 return (NULL); 11769 } 11770 11771 return (sec); 11772} 11773 11774static dtrace_probedesc_t * 11775dtrace_dof_probedesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_probedesc_t *desc) 11776{ 11777 dof_probedesc_t *probe; 11778 dof_sec_t *strtab; 11779 uintptr_t daddr = (uintptr_t)dof; 11780 uintptr_t str; 11781 size_t size; 11782 11783 if (sec->dofs_type != DOF_SECT_PROBEDESC) { 11784 dtrace_dof_error(dof, "invalid probe section"); 11785 return (NULL); 11786 } 11787 11788 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11789 dtrace_dof_error(dof, "bad alignment in probe description"); 11790 return (NULL); 11791 } 11792 11793 if (sec->dofs_offset + sizeof (dof_probedesc_t) > dof->dofh_loadsz) { 11794 dtrace_dof_error(dof, "truncated probe description"); 11795 return (NULL); 11796 } 11797 11798 probe = (dof_probedesc_t *)(uintptr_t)(daddr + sec->dofs_offset); 11799 strtab = dtrace_dof_sect(dof, DOF_SECT_STRTAB, probe->dofp_strtab); 11800 11801 if (strtab == NULL) 11802 return (NULL); 11803 11804 str = daddr + strtab->dofs_offset; 11805 size = strtab->dofs_size; 11806 11807 if (probe->dofp_provider >= strtab->dofs_size) { 11808 dtrace_dof_error(dof, "corrupt probe provider"); 11809 return (NULL); 11810 } 11811 11812 (void) strncpy(desc->dtpd_provider, 11813 (char *)(str + probe->dofp_provider), 11814 MIN(DTRACE_PROVNAMELEN - 1, size - probe->dofp_provider)); 11815 11816 if (probe->dofp_mod >= strtab->dofs_size) { 11817 dtrace_dof_error(dof, "corrupt probe module"); 11818 return (NULL); 11819 } 11820 11821 (void) strncpy(desc->dtpd_mod, (char *)(str + probe->dofp_mod), 11822 MIN(DTRACE_MODNAMELEN - 1, size - probe->dofp_mod)); 11823 11824 if (probe->dofp_func >= strtab->dofs_size) { 11825 dtrace_dof_error(dof, "corrupt probe function"); 11826 return (NULL); 11827 } 11828 11829 (void) strncpy(desc->dtpd_func, (char *)(str + probe->dofp_func), 11830 MIN(DTRACE_FUNCNAMELEN - 1, size - probe->dofp_func)); 11831 11832 if (probe->dofp_name >= strtab->dofs_size) { 11833 dtrace_dof_error(dof, "corrupt probe name"); 11834 return (NULL); 11835 } 11836 11837 (void) strncpy(desc->dtpd_name, (char *)(str + probe->dofp_name), 11838 MIN(DTRACE_NAMELEN - 1, size - probe->dofp_name)); 11839 11840 return (desc); 11841} 11842 11843static dtrace_difo_t * 11844dtrace_dof_difo(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 11845 cred_t *cr) 11846{ 11847 dtrace_difo_t *dp; 11848 size_t ttl = 0; 11849 dof_difohdr_t *dofd; 11850 uintptr_t daddr = (uintptr_t)dof; 11851 size_t max = dtrace_difo_maxsize; 11852 int i, l, n; 11853 11854 static const struct { 11855 int section; 11856 int bufoffs; 11857 int lenoffs; 11858 int entsize; 11859 int align; 11860 const char *msg; 11861 } difo[] = { 11862 { DOF_SECT_DIF, offsetof(dtrace_difo_t, dtdo_buf), 11863 offsetof(dtrace_difo_t, dtdo_len), sizeof (dif_instr_t), 11864 sizeof (dif_instr_t), "multiple DIF sections" }, 11865 11866 { DOF_SECT_INTTAB, offsetof(dtrace_difo_t, dtdo_inttab), 11867 offsetof(dtrace_difo_t, dtdo_intlen), sizeof (uint64_t), 11868 sizeof (uint64_t), "multiple integer tables" }, 11869 11870 { DOF_SECT_STRTAB, offsetof(dtrace_difo_t, dtdo_strtab), 11871 offsetof(dtrace_difo_t, dtdo_strlen), 0, 11872 sizeof (char), "multiple string tables" }, 11873 11874 { DOF_SECT_VARTAB, offsetof(dtrace_difo_t, dtdo_vartab), 11875 offsetof(dtrace_difo_t, dtdo_varlen), sizeof (dtrace_difv_t), 11876 sizeof (uint_t), "multiple variable tables" }, 11877 11878 { DOF_SECT_NONE, 0, 0, 0, 0, NULL } 11879 }; 11880 11881 if (sec->dofs_type != DOF_SECT_DIFOHDR) { 11882 dtrace_dof_error(dof, "invalid DIFO header section"); 11883 return (NULL); 11884 } 11885 11886 if (sec->dofs_align != sizeof (dof_secidx_t)) { 11887 dtrace_dof_error(dof, "bad alignment in DIFO header"); 11888 return (NULL); 11889 } 11890 11891 if (sec->dofs_size < sizeof (dof_difohdr_t) || 11892 sec->dofs_size % sizeof (dof_secidx_t)) { 11893 dtrace_dof_error(dof, "bad size in DIFO header"); 11894 return (NULL); 11895 } 11896 11897 dofd = (dof_difohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 11898 n = (sec->dofs_size - sizeof (*dofd)) / sizeof (dof_secidx_t) + 1; 11899 11900 dp = kmem_zalloc(sizeof (dtrace_difo_t), KM_SLEEP); 11901 dp->dtdo_rtype = dofd->dofd_rtype; 11902 11903 for (l = 0; l < n; l++) { 11904 dof_sec_t *subsec; 11905 void **bufp; 11906 uint32_t *lenp; 11907 11908 if ((subsec = dtrace_dof_sect(dof, DOF_SECT_NONE, 11909 dofd->dofd_links[l])) == NULL) 11910 goto err; /* invalid section link */ 11911 11912 if (ttl + subsec->dofs_size > max) { 11913 dtrace_dof_error(dof, "exceeds maximum size"); 11914 goto err; 11915 } 11916 11917 ttl += subsec->dofs_size; 11918 11919 for (i = 0; difo[i].section != DOF_SECT_NONE; i++) { 11920 if (subsec->dofs_type != difo[i].section) 11921 continue; 11922 11923 if (!(subsec->dofs_flags & DOF_SECF_LOAD)) { 11924 dtrace_dof_error(dof, "section not loaded"); 11925 goto err; 11926 } 11927 11928 if (subsec->dofs_align != difo[i].align) { 11929 dtrace_dof_error(dof, "bad alignment"); 11930 goto err; 11931 } 11932 11933 bufp = (void **)((uintptr_t)dp + difo[i].bufoffs); 11934 lenp = (uint32_t *)((uintptr_t)dp + difo[i].lenoffs); 11935 11936 if (*bufp != NULL) { 11937 dtrace_dof_error(dof, difo[i].msg); 11938 goto err; 11939 } 11940 11941 if (difo[i].entsize != subsec->dofs_entsize) { 11942 dtrace_dof_error(dof, "entry size mismatch"); 11943 goto err; 11944 } 11945 11946 if (subsec->dofs_entsize != 0 && 11947 (subsec->dofs_size % subsec->dofs_entsize) != 0) { 11948 dtrace_dof_error(dof, "corrupt entry size"); 11949 goto err; 11950 } 11951 11952 *lenp = subsec->dofs_size; 11953 *bufp = kmem_alloc(subsec->dofs_size, KM_SLEEP); 11954 bcopy((char *)(uintptr_t)(daddr + subsec->dofs_offset), 11955 *bufp, subsec->dofs_size); 11956 11957 if (subsec->dofs_entsize != 0) 11958 *lenp /= subsec->dofs_entsize; 11959 11960 break; 11961 } 11962 11963 /* 11964 * If we encounter a loadable DIFO sub-section that is not 11965 * known to us, assume this is a broken program and fail. 11966 */ 11967 if (difo[i].section == DOF_SECT_NONE && 11968 (subsec->dofs_flags & DOF_SECF_LOAD)) { 11969 dtrace_dof_error(dof, "unrecognized DIFO subsection"); 11970 goto err; 11971 } 11972 } 11973 11974 if (dp->dtdo_buf == NULL) { 11975 /* 11976 * We can't have a DIF object without DIF text. 11977 */ 11978 dtrace_dof_error(dof, "missing DIF text"); 11979 goto err; 11980 } 11981 11982 /* 11983 * Before we validate the DIF object, run through the variable table 11984 * looking for the strings -- if any of their size are under, we'll set 11985 * their size to be the system-wide default string size. Note that 11986 * this should _not_ happen if the "strsize" option has been set -- 11987 * in this case, the compiler should have set the size to reflect the 11988 * setting of the option. 11989 */ 11990 for (i = 0; i < dp->dtdo_varlen; i++) { 11991 dtrace_difv_t *v = &dp->dtdo_vartab[i]; 11992 dtrace_diftype_t *t = &v->dtdv_type; 11993 11994 if (v->dtdv_id < DIF_VAR_OTHER_UBASE) 11995 continue; 11996 11997 if (t->dtdt_kind == DIF_TYPE_STRING && t->dtdt_size == 0) 11998 t->dtdt_size = dtrace_strsize_default; 11999 } 12000 12001 if (dtrace_difo_validate(dp, vstate, DIF_DIR_NREGS, cr) != 0) 12002 goto err; 12003 12004 dtrace_difo_init(dp, vstate); 12005 return (dp); 12006 12007err: 12008 kmem_free(dp->dtdo_buf, dp->dtdo_len * sizeof (dif_instr_t)); 12009 kmem_free(dp->dtdo_inttab, dp->dtdo_intlen * sizeof (uint64_t)); 12010 kmem_free(dp->dtdo_strtab, dp->dtdo_strlen); 12011 kmem_free(dp->dtdo_vartab, dp->dtdo_varlen * sizeof (dtrace_difv_t)); 12012 12013 kmem_free(dp, sizeof (dtrace_difo_t)); 12014 return (NULL); 12015} 12016 12017static dtrace_predicate_t * 12018dtrace_dof_predicate(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12019 cred_t *cr) 12020{ 12021 dtrace_difo_t *dp; 12022 12023 if ((dp = dtrace_dof_difo(dof, sec, vstate, cr)) == NULL) 12024 return (NULL); 12025 12026 return (dtrace_predicate_create(dp)); 12027} 12028 12029static dtrace_actdesc_t * 12030dtrace_dof_actdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12031 cred_t *cr) 12032{ 12033 dtrace_actdesc_t *act, *first = NULL, *last = NULL, *next; 12034 dof_actdesc_t *desc; 12035 dof_sec_t *difosec; 12036 size_t offs; 12037 uintptr_t daddr = (uintptr_t)dof; 12038 uint64_t arg; 12039 dtrace_actkind_t kind; 12040 12041 if (sec->dofs_type != DOF_SECT_ACTDESC) { 12042 dtrace_dof_error(dof, "invalid action section"); 12043 return (NULL); 12044 } 12045 12046 if (sec->dofs_offset + sizeof (dof_actdesc_t) > dof->dofh_loadsz) { 12047 dtrace_dof_error(dof, "truncated action description"); 12048 return (NULL); 12049 } 12050 12051 if (sec->dofs_align != sizeof (uint64_t)) { 12052 dtrace_dof_error(dof, "bad alignment in action description"); 12053 return (NULL); 12054 } 12055 12056 if (sec->dofs_size < sec->dofs_entsize) { 12057 dtrace_dof_error(dof, "section entry size exceeds total size"); 12058 return (NULL); 12059 } 12060 12061 if (sec->dofs_entsize != sizeof (dof_actdesc_t)) { 12062 dtrace_dof_error(dof, "bad entry size in action description"); 12063 return (NULL); 12064 } 12065 12066 if (sec->dofs_size / sec->dofs_entsize > dtrace_actions_max) { 12067 dtrace_dof_error(dof, "actions exceed dtrace_actions_max"); 12068 return (NULL); 12069 } 12070 12071 for (offs = 0; offs < sec->dofs_size; offs += sec->dofs_entsize) { 12072 desc = (dof_actdesc_t *)(daddr + 12073 (uintptr_t)sec->dofs_offset + offs); 12074 kind = (dtrace_actkind_t)desc->dofa_kind; 12075 12076 if (DTRACEACT_ISPRINTFLIKE(kind) && 12077 (kind != DTRACEACT_PRINTA || 12078 desc->dofa_strtab != DOF_SECIDX_NONE)) { 12079 dof_sec_t *strtab; 12080 char *str, *fmt; 12081 uint64_t i; 12082 12083 /* 12084 * printf()-like actions must have a format string. 12085 */ 12086 if ((strtab = dtrace_dof_sect(dof, 12087 DOF_SECT_STRTAB, desc->dofa_strtab)) == NULL) 12088 goto err; 12089 12090 str = (char *)((uintptr_t)dof + 12091 (uintptr_t)strtab->dofs_offset); 12092 12093 for (i = desc->dofa_arg; i < strtab->dofs_size; i++) { 12094 if (str[i] == '\0') 12095 break; 12096 } 12097 12098 if (i >= strtab->dofs_size) { 12099 dtrace_dof_error(dof, "bogus format string"); 12100 goto err; 12101 } 12102 12103 if (i == desc->dofa_arg) { 12104 dtrace_dof_error(dof, "empty format string"); 12105 goto err; 12106 } 12107 12108 i -= desc->dofa_arg; 12109 fmt = kmem_alloc(i + 1, KM_SLEEP); 12110 bcopy(&str[desc->dofa_arg], fmt, i + 1); 12111 arg = (uint64_t)(uintptr_t)fmt; 12112 } else { 12113 if (kind == DTRACEACT_PRINTA) { 12114 ASSERT(desc->dofa_strtab == DOF_SECIDX_NONE); 12115 arg = 0; 12116 } else { 12117 arg = desc->dofa_arg; 12118 } 12119 } 12120 12121 act = dtrace_actdesc_create(kind, desc->dofa_ntuple, 12122 desc->dofa_uarg, arg); 12123 12124 if (last != NULL) { 12125 last->dtad_next = act; 12126 } else { 12127 first = act; 12128 } 12129 12130 last = act; 12131 12132 if (desc->dofa_difo == DOF_SECIDX_NONE) 12133 continue; 12134 12135 if ((difosec = dtrace_dof_sect(dof, 12136 DOF_SECT_DIFOHDR, desc->dofa_difo)) == NULL) 12137 goto err; 12138 12139 act->dtad_difo = dtrace_dof_difo(dof, difosec, vstate, cr); 12140 12141 if (act->dtad_difo == NULL) 12142 goto err; 12143 } 12144 12145 ASSERT(first != NULL); 12146 return (first); 12147 12148err: 12149 for (act = first; act != NULL; act = next) { 12150 next = act->dtad_next; 12151 dtrace_actdesc_release(act, vstate); 12152 } 12153 12154 return (NULL); 12155} 12156 12157static dtrace_ecbdesc_t * 12158dtrace_dof_ecbdesc(dof_hdr_t *dof, dof_sec_t *sec, dtrace_vstate_t *vstate, 12159 cred_t *cr) 12160{ 12161 dtrace_ecbdesc_t *ep; 12162 dof_ecbdesc_t *ecb; 12163 dtrace_probedesc_t *desc; 12164 dtrace_predicate_t *pred = NULL; 12165 12166 if (sec->dofs_size < sizeof (dof_ecbdesc_t)) { 12167 dtrace_dof_error(dof, "truncated ECB description"); 12168 return (NULL); 12169 } 12170 12171 if (sec->dofs_align != sizeof (uint64_t)) { 12172 dtrace_dof_error(dof, "bad alignment in ECB description"); 12173 return (NULL); 12174 } 12175 12176 ecb = (dof_ecbdesc_t *)((uintptr_t)dof + (uintptr_t)sec->dofs_offset); 12177 sec = dtrace_dof_sect(dof, DOF_SECT_PROBEDESC, ecb->dofe_probes); 12178 12179 if (sec == NULL) 12180 return (NULL); 12181 12182 ep = kmem_zalloc(sizeof (dtrace_ecbdesc_t), KM_SLEEP); 12183 ep->dted_uarg = ecb->dofe_uarg; 12184 desc = &ep->dted_probe; 12185 12186 if (dtrace_dof_probedesc(dof, sec, desc) == NULL) 12187 goto err; 12188 12189 if (ecb->dofe_pred != DOF_SECIDX_NONE) { 12190 if ((sec = dtrace_dof_sect(dof, 12191 DOF_SECT_DIFOHDR, ecb->dofe_pred)) == NULL) 12192 goto err; 12193 12194 if ((pred = dtrace_dof_predicate(dof, sec, vstate, cr)) == NULL) 12195 goto err; 12196 12197 ep->dted_pred.dtpdd_predicate = pred; 12198 } 12199 12200 if (ecb->dofe_actions != DOF_SECIDX_NONE) { 12201 if ((sec = dtrace_dof_sect(dof, 12202 DOF_SECT_ACTDESC, ecb->dofe_actions)) == NULL) 12203 goto err; 12204 12205 ep->dted_action = dtrace_dof_actdesc(dof, sec, vstate, cr); 12206 12207 if (ep->dted_action == NULL) 12208 goto err; 12209 } 12210 12211 return (ep); 12212 12213err: 12214 if (pred != NULL) 12215 dtrace_predicate_release(pred, vstate); 12216 kmem_free(ep, sizeof (dtrace_ecbdesc_t)); 12217 return (NULL); 12218} 12219 12220/* 12221 * Apply the relocations from the specified 'sec' (a DOF_SECT_URELHDR) to the 12222 * specified DOF. At present, this amounts to simply adding 'ubase' to the 12223 * site of any user SETX relocations to account for load object base address. 12224 * In the future, if we need other relocations, this function can be extended. 12225 */ 12226static int 12227dtrace_dof_relocate(dof_hdr_t *dof, dof_sec_t *sec, uint64_t ubase) 12228{ 12229 uintptr_t daddr = (uintptr_t)dof; 12230 dof_relohdr_t *dofr = 12231 (dof_relohdr_t *)(uintptr_t)(daddr + sec->dofs_offset); 12232 dof_sec_t *ss, *rs, *ts; 12233 dof_relodesc_t *r; 12234 uint_t i, n; 12235 12236 if (sec->dofs_size < sizeof (dof_relohdr_t) || 12237 sec->dofs_align != sizeof (dof_secidx_t)) { 12238 dtrace_dof_error(dof, "invalid relocation header"); 12239 return (-1); 12240 } 12241 12242 ss = dtrace_dof_sect(dof, DOF_SECT_STRTAB, dofr->dofr_strtab); 12243 rs = dtrace_dof_sect(dof, DOF_SECT_RELTAB, dofr->dofr_relsec); 12244 ts = dtrace_dof_sect(dof, DOF_SECT_NONE, dofr->dofr_tgtsec); 12245 12246 if (ss == NULL || rs == NULL || ts == NULL) 12247 return (-1); /* dtrace_dof_error() has been called already */ 12248 12249 if (rs->dofs_entsize < sizeof (dof_relodesc_t) || 12250 rs->dofs_align != sizeof (uint64_t)) { 12251 dtrace_dof_error(dof, "invalid relocation section"); 12252 return (-1); 12253 } 12254 12255 r = (dof_relodesc_t *)(uintptr_t)(daddr + rs->dofs_offset); 12256 n = rs->dofs_size / rs->dofs_entsize; 12257 12258 for (i = 0; i < n; i++) { 12259 uintptr_t taddr = daddr + ts->dofs_offset + r->dofr_offset; 12260 12261 switch (r->dofr_type) { 12262 case DOF_RELO_NONE: 12263 break; 12264 case DOF_RELO_SETX: 12265 if (r->dofr_offset >= ts->dofs_size || r->dofr_offset + 12266 sizeof (uint64_t) > ts->dofs_size) { 12267 dtrace_dof_error(dof, "bad relocation offset"); 12268 return (-1); 12269 } 12270 12271 if (!IS_P2ALIGNED(taddr, sizeof (uint64_t))) { 12272 dtrace_dof_error(dof, "misaligned setx relo"); 12273 return (-1); 12274 } 12275 12276 *(uint64_t *)taddr += ubase; 12277 break; 12278 default: 12279 dtrace_dof_error(dof, "invalid relocation type"); 12280 return (-1); 12281 } 12282 12283 r = (dof_relodesc_t *)((uintptr_t)r + rs->dofs_entsize); 12284 } 12285 12286 return (0); 12287} 12288 12289/* 12290 * The dof_hdr_t passed to dtrace_dof_slurp() should be a partially validated 12291 * header: it should be at the front of a memory region that is at least 12292 * sizeof (dof_hdr_t) in size -- and then at least dof_hdr.dofh_loadsz in 12293 * size. It need not be validated in any other way. 12294 */ 12295static int 12296dtrace_dof_slurp(dof_hdr_t *dof, dtrace_vstate_t *vstate, cred_t *cr, 12297 dtrace_enabling_t **enabp, uint64_t ubase, int noprobes) 12298{ 12299 uint64_t len = dof->dofh_loadsz, seclen; 12300 uintptr_t daddr = (uintptr_t)dof; 12301 dtrace_ecbdesc_t *ep; 12302 dtrace_enabling_t *enab; 12303 uint_t i; 12304 12305 ASSERT(MUTEX_HELD(&dtrace_lock)); 12306 ASSERT(dof->dofh_loadsz >= sizeof (dof_hdr_t)); 12307 12308 /* 12309 * Check the DOF header identification bytes. In addition to checking 12310 * valid settings, we also verify that unused bits/bytes are zeroed so 12311 * we can use them later without fear of regressing existing binaries. 12312 */ 12313 if (bcmp(&dof->dofh_ident[DOF_ID_MAG0], 12314 DOF_MAG_STRING, DOF_MAG_STRLEN) != 0) { 12315 dtrace_dof_error(dof, "DOF magic string mismatch"); 12316 return (-1); 12317 } 12318 12319 if (dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_ILP32 && 12320 dof->dofh_ident[DOF_ID_MODEL] != DOF_MODEL_LP64) { 12321 dtrace_dof_error(dof, "DOF has invalid data model"); 12322 return (-1); 12323 } 12324 12325 if (dof->dofh_ident[DOF_ID_ENCODING] != DOF_ENCODE_NATIVE) { 12326 dtrace_dof_error(dof, "DOF encoding mismatch"); 12327 return (-1); 12328 } 12329 12330 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 12331 dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_2) { 12332 dtrace_dof_error(dof, "DOF version mismatch"); 12333 return (-1); 12334 } 12335 12336 if (dof->dofh_ident[DOF_ID_DIFVERS] != DIF_VERSION_2) { 12337 dtrace_dof_error(dof, "DOF uses unsupported instruction set"); 12338 return (-1); 12339 } 12340 12341 if (dof->dofh_ident[DOF_ID_DIFIREG] > DIF_DIR_NREGS) { 12342 dtrace_dof_error(dof, "DOF uses too many integer registers"); 12343 return (-1); 12344 } 12345 12346 if (dof->dofh_ident[DOF_ID_DIFTREG] > DIF_DTR_NREGS) { 12347 dtrace_dof_error(dof, "DOF uses too many tuple registers"); 12348 return (-1); 12349 } 12350 12351 for (i = DOF_ID_PAD; i < DOF_ID_SIZE; i++) { 12352 if (dof->dofh_ident[i] != 0) { 12353 dtrace_dof_error(dof, "DOF has invalid ident byte set"); 12354 return (-1); 12355 } 12356 } 12357 12358 if (dof->dofh_flags & ~DOF_FL_VALID) { 12359 dtrace_dof_error(dof, "DOF has invalid flag bits set"); 12360 return (-1); 12361 } 12362 12363 if (dof->dofh_secsize == 0) { 12364 dtrace_dof_error(dof, "zero section header size"); 12365 return (-1); 12366 } 12367 12368 /* 12369 * Check that the section headers don't exceed the amount of DOF 12370 * data. Note that we cast the section size and number of sections 12371 * to uint64_t's to prevent possible overflow in the multiplication. 12372 */ 12373 seclen = (uint64_t)dof->dofh_secnum * (uint64_t)dof->dofh_secsize; 12374 12375 if (dof->dofh_secoff > len || seclen > len || 12376 dof->dofh_secoff + seclen > len) { 12377 dtrace_dof_error(dof, "truncated section headers"); 12378 return (-1); 12379 } 12380 12381 if (!IS_P2ALIGNED(dof->dofh_secoff, sizeof (uint64_t))) { 12382 dtrace_dof_error(dof, "misaligned section headers"); 12383 return (-1); 12384 } 12385 12386 if (!IS_P2ALIGNED(dof->dofh_secsize, sizeof (uint64_t))) { 12387 dtrace_dof_error(dof, "misaligned section size"); 12388 return (-1); 12389 } 12390 12391 /* 12392 * Take an initial pass through the section headers to be sure that 12393 * the headers don't have stray offsets. If the 'noprobes' flag is 12394 * set, do not permit sections relating to providers, probes, or args. 12395 */ 12396 for (i = 0; i < dof->dofh_secnum; i++) { 12397 dof_sec_t *sec = (dof_sec_t *)(daddr + 12398 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12399 12400 if (noprobes) { 12401 switch (sec->dofs_type) { 12402 case DOF_SECT_PROVIDER: 12403 case DOF_SECT_PROBES: 12404 case DOF_SECT_PRARGS: 12405 case DOF_SECT_PROFFS: 12406 dtrace_dof_error(dof, "illegal sections " 12407 "for enabling"); 12408 return (-1); 12409 } 12410 } 12411 12412 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12413 continue; /* just ignore non-loadable sections */ 12414 12415 if (sec->dofs_align & (sec->dofs_align - 1)) { 12416 dtrace_dof_error(dof, "bad section alignment"); 12417 return (-1); 12418 } 12419 12420 if (sec->dofs_offset & (sec->dofs_align - 1)) { 12421 dtrace_dof_error(dof, "misaligned section"); 12422 return (-1); 12423 } 12424 12425 if (sec->dofs_offset > len || sec->dofs_size > len || 12426 sec->dofs_offset + sec->dofs_size > len) { 12427 dtrace_dof_error(dof, "corrupt section header"); 12428 return (-1); 12429 } 12430 12431 if (sec->dofs_type == DOF_SECT_STRTAB && *((char *)daddr + 12432 sec->dofs_offset + sec->dofs_size - 1) != '\0') { 12433 dtrace_dof_error(dof, "non-terminating string table"); 12434 return (-1); 12435 } 12436 } 12437 12438 /* 12439 * Take a second pass through the sections and locate and perform any 12440 * relocations that are present. We do this after the first pass to 12441 * be sure that all sections have had their headers validated. 12442 */ 12443 for (i = 0; i < dof->dofh_secnum; i++) { 12444 dof_sec_t *sec = (dof_sec_t *)(daddr + 12445 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12446 12447 if (!(sec->dofs_flags & DOF_SECF_LOAD)) 12448 continue; /* skip sections that are not loadable */ 12449 12450 switch (sec->dofs_type) { 12451 case DOF_SECT_URELHDR: 12452 if (dtrace_dof_relocate(dof, sec, ubase) != 0) 12453 return (-1); 12454 break; 12455 } 12456 } 12457 12458 if ((enab = *enabp) == NULL) 12459 enab = *enabp = dtrace_enabling_create(vstate); 12460 12461 for (i = 0; i < dof->dofh_secnum; i++) { 12462 dof_sec_t *sec = (dof_sec_t *)(daddr + 12463 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12464 12465 if (sec->dofs_type != DOF_SECT_ECBDESC) 12466 continue; 12467 12468 if ((ep = dtrace_dof_ecbdesc(dof, sec, vstate, cr)) == NULL) { 12469 dtrace_enabling_destroy(enab); 12470 *enabp = NULL; 12471 return (-1); 12472 } 12473 12474 dtrace_enabling_add(enab, ep); 12475 } 12476 12477 return (0); 12478} 12479 12480/* 12481 * Process DOF for any options. This routine assumes that the DOF has been 12482 * at least processed by dtrace_dof_slurp(). 12483 */ 12484static int 12485dtrace_dof_options(dof_hdr_t *dof, dtrace_state_t *state) 12486{ 12487 int i, rval; 12488 uint32_t entsize; 12489 size_t offs; 12490 dof_optdesc_t *desc; 12491 12492 for (i = 0; i < dof->dofh_secnum; i++) { 12493 dof_sec_t *sec = (dof_sec_t *)((uintptr_t)dof + 12494 (uintptr_t)dof->dofh_secoff + i * dof->dofh_secsize); 12495 12496 if (sec->dofs_type != DOF_SECT_OPTDESC) 12497 continue; 12498 12499 if (sec->dofs_align != sizeof (uint64_t)) { 12500 dtrace_dof_error(dof, "bad alignment in " 12501 "option description"); 12502 return (EINVAL); 12503 } 12504 12505 if ((entsize = sec->dofs_entsize) == 0) { 12506 dtrace_dof_error(dof, "zeroed option entry size"); 12507 return (EINVAL); 12508 } 12509 12510 if (entsize < sizeof (dof_optdesc_t)) { 12511 dtrace_dof_error(dof, "bad option entry size"); 12512 return (EINVAL); 12513 } 12514 12515 for (offs = 0; offs < sec->dofs_size; offs += entsize) { 12516 desc = (dof_optdesc_t *)((uintptr_t)dof + 12517 (uintptr_t)sec->dofs_offset + offs); 12518 12519 if (desc->dofo_strtab != DOF_SECIDX_NONE) { 12520 dtrace_dof_error(dof, "non-zero option string"); 12521 return (EINVAL); 12522 } 12523 12524 if (desc->dofo_value == DTRACEOPT_UNSET) { 12525 dtrace_dof_error(dof, "unset option"); 12526 return (EINVAL); 12527 } 12528 12529 if ((rval = dtrace_state_option(state, 12530 desc->dofo_option, desc->dofo_value)) != 0) { 12531 dtrace_dof_error(dof, "rejected option"); 12532 return (rval); 12533 } 12534 } 12535 } 12536 12537 return (0); 12538} 12539 12540/* 12541 * DTrace Consumer State Functions 12542 */ 12543static int 12544dtrace_dstate_init(dtrace_dstate_t *dstate, size_t size) 12545{ 12546 size_t hashsize, maxper, min, chunksize = dstate->dtds_chunksize; 12547 void *base; 12548 uintptr_t limit; 12549 dtrace_dynvar_t *dvar, *next, *start; 12550 int i; 12551 12552 ASSERT(MUTEX_HELD(&dtrace_lock)); 12553 ASSERT(dstate->dtds_base == NULL && dstate->dtds_percpu == NULL); 12554 12555 bzero(dstate, sizeof (dtrace_dstate_t)); 12556 12557 if ((dstate->dtds_chunksize = chunksize) == 0) 12558 dstate->dtds_chunksize = DTRACE_DYNVAR_CHUNKSIZE; 12559 12560 if (size < (min = dstate->dtds_chunksize + sizeof (dtrace_dynhash_t))) 12561 size = min; 12562 12563 if ((base = kmem_zalloc(size, KM_NOSLEEP)) == NULL) 12564 return (ENOMEM); 12565 12566 dstate->dtds_size = size; 12567 dstate->dtds_base = base; 12568 dstate->dtds_percpu = kmem_cache_alloc(dtrace_state_cache, KM_SLEEP); 12569 bzero(dstate->dtds_percpu, NCPU * sizeof (dtrace_dstate_percpu_t)); 12570 12571 hashsize = size / (dstate->dtds_chunksize + sizeof (dtrace_dynhash_t)); 12572 12573 if (hashsize != 1 && (hashsize & 1)) 12574 hashsize--; 12575 12576 dstate->dtds_hashsize = hashsize; 12577 dstate->dtds_hash = dstate->dtds_base; 12578 12579 /* 12580 * Set all of our hash buckets to point to the single sink, and (if 12581 * it hasn't already been set), set the sink's hash value to be the 12582 * sink sentinel value. The sink is needed for dynamic variable 12583 * lookups to know that they have iterated over an entire, valid hash 12584 * chain. 12585 */ 12586 for (i = 0; i < hashsize; i++) 12587 dstate->dtds_hash[i].dtdh_chain = &dtrace_dynhash_sink; 12588 12589 if (dtrace_dynhash_sink.dtdv_hashval != DTRACE_DYNHASH_SINK) 12590 dtrace_dynhash_sink.dtdv_hashval = DTRACE_DYNHASH_SINK; 12591 12592 /* 12593 * Determine number of active CPUs. Divide free list evenly among 12594 * active CPUs. 12595 */ 12596 start = (dtrace_dynvar_t *) 12597 ((uintptr_t)base + hashsize * sizeof (dtrace_dynhash_t)); 12598 limit = (uintptr_t)base + size; 12599 12600 maxper = (limit - (uintptr_t)start) / NCPU; 12601 maxper = (maxper / dstate->dtds_chunksize) * dstate->dtds_chunksize; 12602 12603 for (i = 0; i < NCPU; i++) { 12604#if !defined(sun) 12605 if (CPU_ABSENT(i)) 12606 continue; 12607#endif 12608 dstate->dtds_percpu[i].dtdsc_free = dvar = start; 12609 12610 /* 12611 * If we don't even have enough chunks to make it once through 12612 * NCPUs, we're just going to allocate everything to the first 12613 * CPU. And if we're on the last CPU, we're going to allocate 12614 * whatever is left over. In either case, we set the limit to 12615 * be the limit of the dynamic variable space. 12616 */ 12617 if (maxper == 0 || i == NCPU - 1) { 12618 limit = (uintptr_t)base + size; 12619 start = NULL; 12620 } else { 12621 limit = (uintptr_t)start + maxper; 12622 start = (dtrace_dynvar_t *)limit; 12623 } 12624 12625 ASSERT(limit <= (uintptr_t)base + size); 12626 12627 for (;;) { 12628 next = (dtrace_dynvar_t *)((uintptr_t)dvar + 12629 dstate->dtds_chunksize); 12630 12631 if ((uintptr_t)next + dstate->dtds_chunksize >= limit) 12632 break; 12633 12634 dvar->dtdv_next = next; 12635 dvar = next; 12636 } 12637 12638 if (maxper == 0) 12639 break; 12640 } 12641 12642 return (0); 12643} 12644 12645static void 12646dtrace_dstate_fini(dtrace_dstate_t *dstate) 12647{ 12648 ASSERT(MUTEX_HELD(&cpu_lock)); 12649 12650 if (dstate->dtds_base == NULL) 12651 return; 12652 12653 kmem_free(dstate->dtds_base, dstate->dtds_size); 12654 kmem_cache_free(dtrace_state_cache, dstate->dtds_percpu); 12655} 12656 12657static void 12658dtrace_vstate_fini(dtrace_vstate_t *vstate) 12659{ 12660 /* 12661 * Logical XOR, where are you? 12662 */ 12663 ASSERT((vstate->dtvs_nglobals == 0) ^ (vstate->dtvs_globals != NULL)); 12664 12665 if (vstate->dtvs_nglobals > 0) { 12666 kmem_free(vstate->dtvs_globals, vstate->dtvs_nglobals * 12667 sizeof (dtrace_statvar_t *)); 12668 } 12669 12670 if (vstate->dtvs_ntlocals > 0) { 12671 kmem_free(vstate->dtvs_tlocals, vstate->dtvs_ntlocals * 12672 sizeof (dtrace_difv_t)); 12673 } 12674 12675 ASSERT((vstate->dtvs_nlocals == 0) ^ (vstate->dtvs_locals != NULL)); 12676 12677 if (vstate->dtvs_nlocals > 0) { 12678 kmem_free(vstate->dtvs_locals, vstate->dtvs_nlocals * 12679 sizeof (dtrace_statvar_t *)); 12680 } 12681} 12682 12683static void 12684dtrace_state_clean(dtrace_state_t *state) 12685{ 12686 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) 12687 return; 12688 12689 dtrace_dynvar_clean(&state->dts_vstate.dtvs_dynvars); 12690 dtrace_speculation_clean(state); 12691} 12692 12693static void 12694dtrace_state_deadman(dtrace_state_t *state) 12695{ 12696 hrtime_t now; 12697 12698 dtrace_sync(); 12699 12700#if !defined(sun) 12701 dtrace_debug_output(); 12702#endif 12703 12704 now = dtrace_gethrtime(); 12705 12706 if (state != dtrace_anon.dta_state && 12707 now - state->dts_laststatus >= dtrace_deadman_user) 12708 return; 12709 12710 /* 12711 * We must be sure that dts_alive never appears to be less than the 12712 * value upon entry to dtrace_state_deadman(), and because we lack a 12713 * dtrace_cas64(), we cannot store to it atomically. We thus instead 12714 * store INT64_MAX to it, followed by a memory barrier, followed by 12715 * the new value. This assures that dts_alive never appears to be 12716 * less than its true value, regardless of the order in which the 12717 * stores to the underlying storage are issued. 12718 */ 12719 state->dts_alive = INT64_MAX; 12720 dtrace_membar_producer(); 12721 state->dts_alive = now; 12722} 12723 12724static dtrace_state_t * 12725#if defined(sun) 12726dtrace_state_create(dev_t *devp, cred_t *cr) 12727#else 12728dtrace_state_create(struct cdev *dev) 12729#endif 12730{ 12731#if defined(sun) 12732 minor_t minor; 12733 major_t major; 12734#else 12735 cred_t *cr = NULL; 12736 int m = 0; 12737#endif 12738 char c[30]; 12739 dtrace_state_t *state; 12740 dtrace_optval_t *opt; 12741 int bufsize = NCPU * sizeof (dtrace_buffer_t), i; 12742 12743 ASSERT(MUTEX_HELD(&dtrace_lock)); 12744 ASSERT(MUTEX_HELD(&cpu_lock)); 12745 12746#if defined(sun) 12747 minor = (minor_t)(uintptr_t)vmem_alloc(dtrace_minor, 1, 12748 VM_BESTFIT | VM_SLEEP); 12749 12750 if (ddi_soft_state_zalloc(dtrace_softstate, minor) != DDI_SUCCESS) { 12751 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 12752 return (NULL); 12753 } 12754 12755 state = ddi_get_soft_state(dtrace_softstate, minor); 12756#else 12757 if (dev != NULL) { 12758 cr = dev->si_cred; 12759 m = minor(dev); 12760 } 12761 12762 /* Allocate memory for the state. */ 12763 state = kmem_zalloc(sizeof(dtrace_state_t), KM_SLEEP); 12764#endif 12765 12766 state->dts_epid = DTRACE_EPIDNONE + 1; 12767 12768 (void) snprintf(c, sizeof (c), "dtrace_aggid_%d", m); 12769#if defined(sun) 12770 state->dts_aggid_arena = vmem_create(c, (void *)1, UINT32_MAX, 1, 12771 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 12772 12773 if (devp != NULL) { 12774 major = getemajor(*devp); 12775 } else { 12776 major = ddi_driver_major(dtrace_devi); 12777 } 12778 12779 state->dts_dev = makedevice(major, minor); 12780 12781 if (devp != NULL) 12782 *devp = state->dts_dev; 12783#else 12784 state->dts_aggid_arena = new_unrhdr(1, INT_MAX, &dtrace_unr_mtx); 12785 state->dts_dev = dev; 12786#endif 12787 12788 /* 12789 * We allocate NCPU buffers. On the one hand, this can be quite 12790 * a bit of memory per instance (nearly 36K on a Starcat). On the 12791 * other hand, it saves an additional memory reference in the probe 12792 * path. 12793 */ 12794 state->dts_buffer = kmem_zalloc(bufsize, KM_SLEEP); 12795 state->dts_aggbuffer = kmem_zalloc(bufsize, KM_SLEEP); 12796 state->dts_cleaner = CYCLIC_NONE; 12797 state->dts_deadman = CYCLIC_NONE; 12798 state->dts_vstate.dtvs_state = state; 12799 12800 for (i = 0; i < DTRACEOPT_MAX; i++) 12801 state->dts_options[i] = DTRACEOPT_UNSET; 12802 12803 /* 12804 * Set the default options. 12805 */ 12806 opt = state->dts_options; 12807 opt[DTRACEOPT_BUFPOLICY] = DTRACEOPT_BUFPOLICY_SWITCH; 12808 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_AUTO; 12809 opt[DTRACEOPT_NSPEC] = dtrace_nspec_default; 12810 opt[DTRACEOPT_SPECSIZE] = dtrace_specsize_default; 12811 opt[DTRACEOPT_CPU] = (dtrace_optval_t)DTRACE_CPUALL; 12812 opt[DTRACEOPT_STRSIZE] = dtrace_strsize_default; 12813 opt[DTRACEOPT_STACKFRAMES] = dtrace_stackframes_default; 12814 opt[DTRACEOPT_USTACKFRAMES] = dtrace_ustackframes_default; 12815 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_default; 12816 opt[DTRACEOPT_AGGRATE] = dtrace_aggrate_default; 12817 opt[DTRACEOPT_SWITCHRATE] = dtrace_switchrate_default; 12818 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_default; 12819 opt[DTRACEOPT_JSTACKFRAMES] = dtrace_jstackframes_default; 12820 opt[DTRACEOPT_JSTACKSTRSIZE] = dtrace_jstackstrsize_default; 12821 12822 state->dts_activity = DTRACE_ACTIVITY_INACTIVE; 12823 12824 /* 12825 * Depending on the user credentials, we set flag bits which alter probe 12826 * visibility or the amount of destructiveness allowed. In the case of 12827 * actual anonymous tracing, or the possession of all privileges, all of 12828 * the normal checks are bypassed. 12829 */ 12830 if (cr == NULL || PRIV_POLICY_ONLY(cr, PRIV_ALL, B_FALSE)) { 12831 state->dts_cred.dcr_visible = DTRACE_CRV_ALL; 12832 state->dts_cred.dcr_action = DTRACE_CRA_ALL; 12833 } else { 12834 /* 12835 * Set up the credentials for this instantiation. We take a 12836 * hold on the credential to prevent it from disappearing on 12837 * us; this in turn prevents the zone_t referenced by this 12838 * credential from disappearing. This means that we can 12839 * examine the credential and the zone from probe context. 12840 */ 12841 crhold(cr); 12842 state->dts_cred.dcr_cred = cr; 12843 12844 /* 12845 * CRA_PROC means "we have *some* privilege for dtrace" and 12846 * unlocks the use of variables like pid, zonename, etc. 12847 */ 12848 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE) || 12849 PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12850 state->dts_cred.dcr_action |= DTRACE_CRA_PROC; 12851 } 12852 12853 /* 12854 * dtrace_user allows use of syscall and profile providers. 12855 * If the user also has proc_owner and/or proc_zone, we 12856 * extend the scope to include additional visibility and 12857 * destructive power. 12858 */ 12859 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_USER, B_FALSE)) { 12860 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) { 12861 state->dts_cred.dcr_visible |= 12862 DTRACE_CRV_ALLPROC; 12863 12864 state->dts_cred.dcr_action |= 12865 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12866 } 12867 12868 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) { 12869 state->dts_cred.dcr_visible |= 12870 DTRACE_CRV_ALLZONE; 12871 12872 state->dts_cred.dcr_action |= 12873 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12874 } 12875 12876 /* 12877 * If we have all privs in whatever zone this is, 12878 * we can do destructive things to processes which 12879 * have altered credentials. 12880 */ 12881#if defined(sun) 12882 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12883 cr->cr_zone->zone_privset)) { 12884 state->dts_cred.dcr_action |= 12885 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12886 } 12887#endif 12888 } 12889 12890 /* 12891 * Holding the dtrace_kernel privilege also implies that 12892 * the user has the dtrace_user privilege from a visibility 12893 * perspective. But without further privileges, some 12894 * destructive actions are not available. 12895 */ 12896 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_KERNEL, B_FALSE)) { 12897 /* 12898 * Make all probes in all zones visible. However, 12899 * this doesn't mean that all actions become available 12900 * to all zones. 12901 */ 12902 state->dts_cred.dcr_visible |= DTRACE_CRV_KERNEL | 12903 DTRACE_CRV_ALLPROC | DTRACE_CRV_ALLZONE; 12904 12905 state->dts_cred.dcr_action |= DTRACE_CRA_KERNEL | 12906 DTRACE_CRA_PROC; 12907 /* 12908 * Holding proc_owner means that destructive actions 12909 * for *this* zone are allowed. 12910 */ 12911 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12912 state->dts_cred.dcr_action |= 12913 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12914 12915 /* 12916 * Holding proc_zone means that destructive actions 12917 * for this user/group ID in all zones is allowed. 12918 */ 12919 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12920 state->dts_cred.dcr_action |= 12921 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12922 12923#if defined(sun) 12924 /* 12925 * If we have all privs in whatever zone this is, 12926 * we can do destructive things to processes which 12927 * have altered credentials. 12928 */ 12929 if (priv_isequalset(priv_getset(cr, PRIV_EFFECTIVE), 12930 cr->cr_zone->zone_privset)) { 12931 state->dts_cred.dcr_action |= 12932 DTRACE_CRA_PROC_DESTRUCTIVE_CREDCHG; 12933 } 12934#endif 12935 } 12936 12937 /* 12938 * Holding the dtrace_proc privilege gives control over fasttrap 12939 * and pid providers. We need to grant wider destructive 12940 * privileges in the event that the user has proc_owner and/or 12941 * proc_zone. 12942 */ 12943 if (PRIV_POLICY_ONLY(cr, PRIV_DTRACE_PROC, B_FALSE)) { 12944 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_OWNER, B_FALSE)) 12945 state->dts_cred.dcr_action |= 12946 DTRACE_CRA_PROC_DESTRUCTIVE_ALLUSER; 12947 12948 if (PRIV_POLICY_ONLY(cr, PRIV_PROC_ZONE, B_FALSE)) 12949 state->dts_cred.dcr_action |= 12950 DTRACE_CRA_PROC_DESTRUCTIVE_ALLZONE; 12951 } 12952 } 12953 12954 return (state); 12955} 12956 12957static int 12958dtrace_state_buffer(dtrace_state_t *state, dtrace_buffer_t *buf, int which) 12959{ 12960 dtrace_optval_t *opt = state->dts_options, size; 12961 processorid_t cpu = 0;; 12962 int flags = 0, rval; 12963 12964 ASSERT(MUTEX_HELD(&dtrace_lock)); 12965 ASSERT(MUTEX_HELD(&cpu_lock)); 12966 ASSERT(which < DTRACEOPT_MAX); 12967 ASSERT(state->dts_activity == DTRACE_ACTIVITY_INACTIVE || 12968 (state == dtrace_anon.dta_state && 12969 state->dts_activity == DTRACE_ACTIVITY_ACTIVE)); 12970 12971 if (opt[which] == DTRACEOPT_UNSET || opt[which] == 0) 12972 return (0); 12973 12974 if (opt[DTRACEOPT_CPU] != DTRACEOPT_UNSET) 12975 cpu = opt[DTRACEOPT_CPU]; 12976 12977 if (which == DTRACEOPT_SPECSIZE) 12978 flags |= DTRACEBUF_NOSWITCH; 12979 12980 if (which == DTRACEOPT_BUFSIZE) { 12981 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_RING) 12982 flags |= DTRACEBUF_RING; 12983 12984 if (opt[DTRACEOPT_BUFPOLICY] == DTRACEOPT_BUFPOLICY_FILL) 12985 flags |= DTRACEBUF_FILL; 12986 12987 if (state != dtrace_anon.dta_state || 12988 state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 12989 flags |= DTRACEBUF_INACTIVE; 12990 } 12991 12992 for (size = opt[which]; size >= sizeof (uint64_t); size >>= 1) { 12993 /* 12994 * The size must be 8-byte aligned. If the size is not 8-byte 12995 * aligned, drop it down by the difference. 12996 */ 12997 if (size & (sizeof (uint64_t) - 1)) 12998 size -= size & (sizeof (uint64_t) - 1); 12999 13000 if (size < state->dts_reserve) { 13001 /* 13002 * Buffers always must be large enough to accommodate 13003 * their prereserved space. We return E2BIG instead 13004 * of ENOMEM in this case to allow for user-level 13005 * software to differentiate the cases. 13006 */ 13007 return (E2BIG); 13008 } 13009 13010 rval = dtrace_buffer_alloc(buf, size, flags, cpu); 13011 13012 if (rval != ENOMEM) { 13013 opt[which] = size; 13014 return (rval); 13015 } 13016 13017 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13018 return (rval); 13019 } 13020 13021 return (ENOMEM); 13022} 13023 13024static int 13025dtrace_state_buffers(dtrace_state_t *state) 13026{ 13027 dtrace_speculation_t *spec = state->dts_speculations; 13028 int rval, i; 13029 13030 if ((rval = dtrace_state_buffer(state, state->dts_buffer, 13031 DTRACEOPT_BUFSIZE)) != 0) 13032 return (rval); 13033 13034 if ((rval = dtrace_state_buffer(state, state->dts_aggbuffer, 13035 DTRACEOPT_AGGSIZE)) != 0) 13036 return (rval); 13037 13038 for (i = 0; i < state->dts_nspeculations; i++) { 13039 if ((rval = dtrace_state_buffer(state, 13040 spec[i].dtsp_buffer, DTRACEOPT_SPECSIZE)) != 0) 13041 return (rval); 13042 } 13043 13044 return (0); 13045} 13046 13047static void 13048dtrace_state_prereserve(dtrace_state_t *state) 13049{ 13050 dtrace_ecb_t *ecb; 13051 dtrace_probe_t *probe; 13052 13053 state->dts_reserve = 0; 13054 13055 if (state->dts_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) 13056 return; 13057 13058 /* 13059 * If our buffer policy is a "fill" buffer policy, we need to set the 13060 * prereserved space to be the space required by the END probes. 13061 */ 13062 probe = dtrace_probes[dtrace_probeid_end - 1]; 13063 ASSERT(probe != NULL); 13064 13065 for (ecb = probe->dtpr_ecb; ecb != NULL; ecb = ecb->dte_next) { 13066 if (ecb->dte_state != state) 13067 continue; 13068 13069 state->dts_reserve += ecb->dte_needed + ecb->dte_alignment; 13070 } 13071} 13072 13073static int 13074dtrace_state_go(dtrace_state_t *state, processorid_t *cpu) 13075{ 13076 dtrace_optval_t *opt = state->dts_options, sz, nspec; 13077 dtrace_speculation_t *spec; 13078 dtrace_buffer_t *buf; 13079 cyc_handler_t hdlr; 13080 cyc_time_t when; 13081 int rval = 0, i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13082 dtrace_icookie_t cookie; 13083 13084 mutex_enter(&cpu_lock); 13085 mutex_enter(&dtrace_lock); 13086 13087 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 13088 rval = EBUSY; 13089 goto out; 13090 } 13091 13092 /* 13093 * Before we can perform any checks, we must prime all of the 13094 * retained enablings that correspond to this state. 13095 */ 13096 dtrace_enabling_prime(state); 13097 13098 if (state->dts_destructive && !state->dts_cred.dcr_destructive) { 13099 rval = EACCES; 13100 goto out; 13101 } 13102 13103 dtrace_state_prereserve(state); 13104 13105 /* 13106 * Now we want to do is try to allocate our speculations. 13107 * We do not automatically resize the number of speculations; if 13108 * this fails, we will fail the operation. 13109 */ 13110 nspec = opt[DTRACEOPT_NSPEC]; 13111 ASSERT(nspec != DTRACEOPT_UNSET); 13112 13113 if (nspec > INT_MAX) { 13114 rval = ENOMEM; 13115 goto out; 13116 } 13117 13118 spec = kmem_zalloc(nspec * sizeof (dtrace_speculation_t), KM_NOSLEEP); 13119 13120 if (spec == NULL) { 13121 rval = ENOMEM; 13122 goto out; 13123 } 13124 13125 state->dts_speculations = spec; 13126 state->dts_nspeculations = (int)nspec; 13127 13128 for (i = 0; i < nspec; i++) { 13129 if ((buf = kmem_zalloc(bufsize, KM_NOSLEEP)) == NULL) { 13130 rval = ENOMEM; 13131 goto err; 13132 } 13133 13134 spec[i].dtsp_buffer = buf; 13135 } 13136 13137 if (opt[DTRACEOPT_GRABANON] != DTRACEOPT_UNSET) { 13138 if (dtrace_anon.dta_state == NULL) { 13139 rval = ENOENT; 13140 goto out; 13141 } 13142 13143 if (state->dts_necbs != 0) { 13144 rval = EALREADY; 13145 goto out; 13146 } 13147 13148 state->dts_anon = dtrace_anon_grab(); 13149 ASSERT(state->dts_anon != NULL); 13150 state = state->dts_anon; 13151 13152 /* 13153 * We want "grabanon" to be set in the grabbed state, so we'll 13154 * copy that option value from the grabbing state into the 13155 * grabbed state. 13156 */ 13157 state->dts_options[DTRACEOPT_GRABANON] = 13158 opt[DTRACEOPT_GRABANON]; 13159 13160 *cpu = dtrace_anon.dta_beganon; 13161 13162 /* 13163 * If the anonymous state is active (as it almost certainly 13164 * is if the anonymous enabling ultimately matched anything), 13165 * we don't allow any further option processing -- but we 13166 * don't return failure. 13167 */ 13168 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13169 goto out; 13170 } 13171 13172 if (opt[DTRACEOPT_AGGSIZE] != DTRACEOPT_UNSET && 13173 opt[DTRACEOPT_AGGSIZE] != 0) { 13174 if (state->dts_aggregations == NULL) { 13175 /* 13176 * We're not going to create an aggregation buffer 13177 * because we don't have any ECBs that contain 13178 * aggregations -- set this option to 0. 13179 */ 13180 opt[DTRACEOPT_AGGSIZE] = 0; 13181 } else { 13182 /* 13183 * If we have an aggregation buffer, we must also have 13184 * a buffer to use as scratch. 13185 */ 13186 if (opt[DTRACEOPT_BUFSIZE] == DTRACEOPT_UNSET || 13187 opt[DTRACEOPT_BUFSIZE] < state->dts_needed) { 13188 opt[DTRACEOPT_BUFSIZE] = state->dts_needed; 13189 } 13190 } 13191 } 13192 13193 if (opt[DTRACEOPT_SPECSIZE] != DTRACEOPT_UNSET && 13194 opt[DTRACEOPT_SPECSIZE] != 0) { 13195 if (!state->dts_speculates) { 13196 /* 13197 * We're not going to create speculation buffers 13198 * because we don't have any ECBs that actually 13199 * speculate -- set the speculation size to 0. 13200 */ 13201 opt[DTRACEOPT_SPECSIZE] = 0; 13202 } 13203 } 13204 13205 /* 13206 * The bare minimum size for any buffer that we're actually going to 13207 * do anything to is sizeof (uint64_t). 13208 */ 13209 sz = sizeof (uint64_t); 13210 13211 if ((state->dts_needed != 0 && opt[DTRACEOPT_BUFSIZE] < sz) || 13212 (state->dts_speculates && opt[DTRACEOPT_SPECSIZE] < sz) || 13213 (state->dts_aggregations != NULL && opt[DTRACEOPT_AGGSIZE] < sz)) { 13214 /* 13215 * A buffer size has been explicitly set to 0 (or to a size 13216 * that will be adjusted to 0) and we need the space -- we 13217 * need to return failure. We return ENOSPC to differentiate 13218 * it from failing to allocate a buffer due to failure to meet 13219 * the reserve (for which we return E2BIG). 13220 */ 13221 rval = ENOSPC; 13222 goto out; 13223 } 13224 13225 if ((rval = dtrace_state_buffers(state)) != 0) 13226 goto err; 13227 13228 if ((sz = opt[DTRACEOPT_DYNVARSIZE]) == DTRACEOPT_UNSET) 13229 sz = dtrace_dstate_defsize; 13230 13231 do { 13232 rval = dtrace_dstate_init(&state->dts_vstate.dtvs_dynvars, sz); 13233 13234 if (rval == 0) 13235 break; 13236 13237 if (opt[DTRACEOPT_BUFRESIZE] == DTRACEOPT_BUFRESIZE_MANUAL) 13238 goto err; 13239 } while (sz >>= 1); 13240 13241 opt[DTRACEOPT_DYNVARSIZE] = sz; 13242 13243 if (rval != 0) 13244 goto err; 13245 13246 if (opt[DTRACEOPT_STATUSRATE] > dtrace_statusrate_max) 13247 opt[DTRACEOPT_STATUSRATE] = dtrace_statusrate_max; 13248 13249 if (opt[DTRACEOPT_CLEANRATE] == 0) 13250 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13251 13252 if (opt[DTRACEOPT_CLEANRATE] < dtrace_cleanrate_min) 13253 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_min; 13254 13255 if (opt[DTRACEOPT_CLEANRATE] > dtrace_cleanrate_max) 13256 opt[DTRACEOPT_CLEANRATE] = dtrace_cleanrate_max; 13257 13258 hdlr.cyh_func = (cyc_func_t)dtrace_state_clean; 13259 hdlr.cyh_arg = state; 13260#if defined(sun) 13261 hdlr.cyh_level = CY_LOW_LEVEL; 13262#endif 13263 13264 when.cyt_when = 0; 13265 when.cyt_interval = opt[DTRACEOPT_CLEANRATE]; 13266 13267 state->dts_cleaner = cyclic_add(&hdlr, &when); 13268 13269 hdlr.cyh_func = (cyc_func_t)dtrace_state_deadman; 13270 hdlr.cyh_arg = state; 13271#if defined(sun) 13272 hdlr.cyh_level = CY_LOW_LEVEL; 13273#endif 13274 13275 when.cyt_when = 0; 13276 when.cyt_interval = dtrace_deadman_interval; 13277 13278 state->dts_alive = state->dts_laststatus = dtrace_gethrtime(); 13279 state->dts_deadman = cyclic_add(&hdlr, &when); 13280 13281 state->dts_activity = DTRACE_ACTIVITY_WARMUP; 13282 13283 /* 13284 * Now it's time to actually fire the BEGIN probe. We need to disable 13285 * interrupts here both to record the CPU on which we fired the BEGIN 13286 * probe (the data from this CPU will be processed first at user 13287 * level) and to manually activate the buffer for this CPU. 13288 */ 13289 cookie = dtrace_interrupt_disable(); 13290 *cpu = curcpu; 13291 ASSERT(state->dts_buffer[*cpu].dtb_flags & DTRACEBUF_INACTIVE); 13292 state->dts_buffer[*cpu].dtb_flags &= ~DTRACEBUF_INACTIVE; 13293 13294 dtrace_probe(dtrace_probeid_begin, 13295 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13296 dtrace_interrupt_enable(cookie); 13297 /* 13298 * We may have had an exit action from a BEGIN probe; only change our 13299 * state to ACTIVE if we're still in WARMUP. 13300 */ 13301 ASSERT(state->dts_activity == DTRACE_ACTIVITY_WARMUP || 13302 state->dts_activity == DTRACE_ACTIVITY_DRAINING); 13303 13304 if (state->dts_activity == DTRACE_ACTIVITY_WARMUP) 13305 state->dts_activity = DTRACE_ACTIVITY_ACTIVE; 13306 13307 /* 13308 * Regardless of whether or not now we're in ACTIVE or DRAINING, we 13309 * want each CPU to transition its principal buffer out of the 13310 * INACTIVE state. Doing this assures that no CPU will suddenly begin 13311 * processing an ECB halfway down a probe's ECB chain; all CPUs will 13312 * atomically transition from processing none of a state's ECBs to 13313 * processing all of them. 13314 */ 13315 dtrace_xcall(DTRACE_CPUALL, 13316 (dtrace_xcall_t)dtrace_buffer_activate, state); 13317 goto out; 13318 13319err: 13320 dtrace_buffer_free(state->dts_buffer); 13321 dtrace_buffer_free(state->dts_aggbuffer); 13322 13323 if ((nspec = state->dts_nspeculations) == 0) { 13324 ASSERT(state->dts_speculations == NULL); 13325 goto out; 13326 } 13327 13328 spec = state->dts_speculations; 13329 ASSERT(spec != NULL); 13330 13331 for (i = 0; i < state->dts_nspeculations; i++) { 13332 if ((buf = spec[i].dtsp_buffer) == NULL) 13333 break; 13334 13335 dtrace_buffer_free(buf); 13336 kmem_free(buf, bufsize); 13337 } 13338 13339 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13340 state->dts_nspeculations = 0; 13341 state->dts_speculations = NULL; 13342 13343out: 13344 mutex_exit(&dtrace_lock); 13345 mutex_exit(&cpu_lock); 13346 13347 return (rval); 13348} 13349 13350static int 13351dtrace_state_stop(dtrace_state_t *state, processorid_t *cpu) 13352{ 13353 dtrace_icookie_t cookie; 13354 13355 ASSERT(MUTEX_HELD(&dtrace_lock)); 13356 13357 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE && 13358 state->dts_activity != DTRACE_ACTIVITY_DRAINING) 13359 return (EINVAL); 13360 13361 /* 13362 * We'll set the activity to DTRACE_ACTIVITY_DRAINING, and issue a sync 13363 * to be sure that every CPU has seen it. See below for the details 13364 * on why this is done. 13365 */ 13366 state->dts_activity = DTRACE_ACTIVITY_DRAINING; 13367 dtrace_sync(); 13368 13369 /* 13370 * By this point, it is impossible for any CPU to be still processing 13371 * with DTRACE_ACTIVITY_ACTIVE. We can thus set our activity to 13372 * DTRACE_ACTIVITY_COOLDOWN and know that we're not racing with any 13373 * other CPU in dtrace_buffer_reserve(). This allows dtrace_probe() 13374 * and callees to know that the activity is DTRACE_ACTIVITY_COOLDOWN 13375 * iff we're in the END probe. 13376 */ 13377 state->dts_activity = DTRACE_ACTIVITY_COOLDOWN; 13378 dtrace_sync(); 13379 ASSERT(state->dts_activity == DTRACE_ACTIVITY_COOLDOWN); 13380 13381 /* 13382 * Finally, we can release the reserve and call the END probe. We 13383 * disable interrupts across calling the END probe to allow us to 13384 * return the CPU on which we actually called the END probe. This 13385 * allows user-land to be sure that this CPU's principal buffer is 13386 * processed last. 13387 */ 13388 state->dts_reserve = 0; 13389 13390 cookie = dtrace_interrupt_disable(); 13391 *cpu = curcpu; 13392 dtrace_probe(dtrace_probeid_end, 13393 (uint64_t)(uintptr_t)state, 0, 0, 0, 0); 13394 dtrace_interrupt_enable(cookie); 13395 13396 state->dts_activity = DTRACE_ACTIVITY_STOPPED; 13397 dtrace_sync(); 13398 13399 return (0); 13400} 13401 13402static int 13403dtrace_state_option(dtrace_state_t *state, dtrace_optid_t option, 13404 dtrace_optval_t val) 13405{ 13406 ASSERT(MUTEX_HELD(&dtrace_lock)); 13407 13408 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) 13409 return (EBUSY); 13410 13411 if (option >= DTRACEOPT_MAX) 13412 return (EINVAL); 13413 13414 if (option != DTRACEOPT_CPU && val < 0) 13415 return (EINVAL); 13416 13417 switch (option) { 13418 case DTRACEOPT_DESTRUCTIVE: 13419 if (dtrace_destructive_disallow) 13420 return (EACCES); 13421 13422 state->dts_cred.dcr_destructive = 1; 13423 break; 13424 13425 case DTRACEOPT_BUFSIZE: 13426 case DTRACEOPT_DYNVARSIZE: 13427 case DTRACEOPT_AGGSIZE: 13428 case DTRACEOPT_SPECSIZE: 13429 case DTRACEOPT_STRSIZE: 13430 if (val < 0) 13431 return (EINVAL); 13432 13433 if (val >= LONG_MAX) { 13434 /* 13435 * If this is an otherwise negative value, set it to 13436 * the highest multiple of 128m less than LONG_MAX. 13437 * Technically, we're adjusting the size without 13438 * regard to the buffer resizing policy, but in fact, 13439 * this has no effect -- if we set the buffer size to 13440 * ~LONG_MAX and the buffer policy is ultimately set to 13441 * be "manual", the buffer allocation is guaranteed to 13442 * fail, if only because the allocation requires two 13443 * buffers. (We set the the size to the highest 13444 * multiple of 128m because it ensures that the size 13445 * will remain a multiple of a megabyte when 13446 * repeatedly halved -- all the way down to 15m.) 13447 */ 13448 val = LONG_MAX - (1 << 27) + 1; 13449 } 13450 } 13451 13452 state->dts_options[option] = val; 13453 13454 return (0); 13455} 13456 13457static void 13458dtrace_state_destroy(dtrace_state_t *state) 13459{ 13460 dtrace_ecb_t *ecb; 13461 dtrace_vstate_t *vstate = &state->dts_vstate; 13462#if defined(sun) 13463 minor_t minor = getminor(state->dts_dev); 13464#endif 13465 int i, bufsize = NCPU * sizeof (dtrace_buffer_t); 13466 dtrace_speculation_t *spec = state->dts_speculations; 13467 int nspec = state->dts_nspeculations; 13468 uint32_t match; 13469 13470 ASSERT(MUTEX_HELD(&dtrace_lock)); 13471 ASSERT(MUTEX_HELD(&cpu_lock)); 13472 13473 /* 13474 * First, retract any retained enablings for this state. 13475 */ 13476 dtrace_enabling_retract(state); 13477 ASSERT(state->dts_nretained == 0); 13478 13479 if (state->dts_activity == DTRACE_ACTIVITY_ACTIVE || 13480 state->dts_activity == DTRACE_ACTIVITY_DRAINING) { 13481 /* 13482 * We have managed to come into dtrace_state_destroy() on a 13483 * hot enabling -- almost certainly because of a disorderly 13484 * shutdown of a consumer. (That is, a consumer that is 13485 * exiting without having called dtrace_stop().) In this case, 13486 * we're going to set our activity to be KILLED, and then 13487 * issue a sync to be sure that everyone is out of probe 13488 * context before we start blowing away ECBs. 13489 */ 13490 state->dts_activity = DTRACE_ACTIVITY_KILLED; 13491 dtrace_sync(); 13492 } 13493 13494 /* 13495 * Release the credential hold we took in dtrace_state_create(). 13496 */ 13497 if (state->dts_cred.dcr_cred != NULL) 13498 crfree(state->dts_cred.dcr_cred); 13499 13500 /* 13501 * Now we can safely disable and destroy any enabled probes. Because 13502 * any DTRACE_PRIV_KERNEL probes may actually be slowing our progress 13503 * (especially if they're all enabled), we take two passes through the 13504 * ECBs: in the first, we disable just DTRACE_PRIV_KERNEL probes, and 13505 * in the second we disable whatever is left over. 13506 */ 13507 for (match = DTRACE_PRIV_KERNEL; ; match = 0) { 13508 for (i = 0; i < state->dts_necbs; i++) { 13509 if ((ecb = state->dts_ecbs[i]) == NULL) 13510 continue; 13511 13512 if (match && ecb->dte_probe != NULL) { 13513 dtrace_probe_t *probe = ecb->dte_probe; 13514 dtrace_provider_t *prov = probe->dtpr_provider; 13515 13516 if (!(prov->dtpv_priv.dtpp_flags & match)) 13517 continue; 13518 } 13519 13520 dtrace_ecb_disable(ecb); 13521 dtrace_ecb_destroy(ecb); 13522 } 13523 13524 if (!match) 13525 break; 13526 } 13527 13528 /* 13529 * Before we free the buffers, perform one more sync to assure that 13530 * every CPU is out of probe context. 13531 */ 13532 dtrace_sync(); 13533 13534 dtrace_buffer_free(state->dts_buffer); 13535 dtrace_buffer_free(state->dts_aggbuffer); 13536 13537 for (i = 0; i < nspec; i++) 13538 dtrace_buffer_free(spec[i].dtsp_buffer); 13539 13540 if (state->dts_cleaner != CYCLIC_NONE) 13541 cyclic_remove(state->dts_cleaner); 13542 13543 if (state->dts_deadman != CYCLIC_NONE) 13544 cyclic_remove(state->dts_deadman); 13545 13546 dtrace_dstate_fini(&vstate->dtvs_dynvars); 13547 dtrace_vstate_fini(vstate); 13548 if (state->dts_ecbs != NULL) 13549 kmem_free(state->dts_ecbs, state->dts_necbs * sizeof (dtrace_ecb_t *)); 13550 13551 if (state->dts_aggregations != NULL) { 13552#ifdef DEBUG 13553 for (i = 0; i < state->dts_naggregations; i++) 13554 ASSERT(state->dts_aggregations[i] == NULL); 13555#endif 13556 ASSERT(state->dts_naggregations > 0); 13557 kmem_free(state->dts_aggregations, 13558 state->dts_naggregations * sizeof (dtrace_aggregation_t *)); 13559 } 13560 13561 kmem_free(state->dts_buffer, bufsize); 13562 kmem_free(state->dts_aggbuffer, bufsize); 13563 13564 for (i = 0; i < nspec; i++) 13565 kmem_free(spec[i].dtsp_buffer, bufsize); 13566 13567 if (spec != NULL) 13568 kmem_free(spec, nspec * sizeof (dtrace_speculation_t)); 13569 13570 dtrace_format_destroy(state); 13571 13572 if (state->dts_aggid_arena != NULL) { 13573#if defined(sun) 13574 vmem_destroy(state->dts_aggid_arena); 13575#else 13576 delete_unrhdr(state->dts_aggid_arena); 13577#endif 13578 state->dts_aggid_arena = NULL; 13579 } 13580#if defined(sun) 13581 ddi_soft_state_free(dtrace_softstate, minor); 13582 vmem_free(dtrace_minor, (void *)(uintptr_t)minor, 1); 13583#endif 13584} 13585 13586/* 13587 * DTrace Anonymous Enabling Functions 13588 */ 13589static dtrace_state_t * 13590dtrace_anon_grab(void) 13591{ 13592 dtrace_state_t *state; 13593 13594 ASSERT(MUTEX_HELD(&dtrace_lock)); 13595 13596 if ((state = dtrace_anon.dta_state) == NULL) { 13597 ASSERT(dtrace_anon.dta_enabling == NULL); 13598 return (NULL); 13599 } 13600 13601 ASSERT(dtrace_anon.dta_enabling != NULL); 13602 ASSERT(dtrace_retained != NULL); 13603 13604 dtrace_enabling_destroy(dtrace_anon.dta_enabling); 13605 dtrace_anon.dta_enabling = NULL; 13606 dtrace_anon.dta_state = NULL; 13607 13608 return (state); 13609} 13610 13611static void 13612dtrace_anon_property(void) 13613{ 13614 int i, rv; 13615 dtrace_state_t *state; 13616 dof_hdr_t *dof; 13617 char c[32]; /* enough for "dof-data-" + digits */ 13618 13619 ASSERT(MUTEX_HELD(&dtrace_lock)); 13620 ASSERT(MUTEX_HELD(&cpu_lock)); 13621 13622 for (i = 0; ; i++) { 13623 (void) snprintf(c, sizeof (c), "dof-data-%d", i); 13624 13625 dtrace_err_verbose = 1; 13626 13627 if ((dof = dtrace_dof_property(c)) == NULL) { 13628 dtrace_err_verbose = 0; 13629 break; 13630 } 13631 13632#if defined(sun) 13633 /* 13634 * We want to create anonymous state, so we need to transition 13635 * the kernel debugger to indicate that DTrace is active. If 13636 * this fails (e.g. because the debugger has modified text in 13637 * some way), we won't continue with the processing. 13638 */ 13639 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 13640 cmn_err(CE_NOTE, "kernel debugger active; anonymous " 13641 "enabling ignored."); 13642 dtrace_dof_destroy(dof); 13643 break; 13644 } 13645#endif 13646 13647 /* 13648 * If we haven't allocated an anonymous state, we'll do so now. 13649 */ 13650 if ((state = dtrace_anon.dta_state) == NULL) { 13651#if defined(sun) 13652 state = dtrace_state_create(NULL, NULL); 13653#else 13654 state = dtrace_state_create(NULL); 13655#endif 13656 dtrace_anon.dta_state = state; 13657 13658 if (state == NULL) { 13659 /* 13660 * This basically shouldn't happen: the only 13661 * failure mode from dtrace_state_create() is a 13662 * failure of ddi_soft_state_zalloc() that 13663 * itself should never happen. Still, the 13664 * interface allows for a failure mode, and 13665 * we want to fail as gracefully as possible: 13666 * we'll emit an error message and cease 13667 * processing anonymous state in this case. 13668 */ 13669 cmn_err(CE_WARN, "failed to create " 13670 "anonymous state"); 13671 dtrace_dof_destroy(dof); 13672 break; 13673 } 13674 } 13675 13676 rv = dtrace_dof_slurp(dof, &state->dts_vstate, CRED(), 13677 &dtrace_anon.dta_enabling, 0, B_TRUE); 13678 13679 if (rv == 0) 13680 rv = dtrace_dof_options(dof, state); 13681 13682 dtrace_err_verbose = 0; 13683 dtrace_dof_destroy(dof); 13684 13685 if (rv != 0) { 13686 /* 13687 * This is malformed DOF; chuck any anonymous state 13688 * that we created. 13689 */ 13690 ASSERT(dtrace_anon.dta_enabling == NULL); 13691 dtrace_state_destroy(state); 13692 dtrace_anon.dta_state = NULL; 13693 break; 13694 } 13695 13696 ASSERT(dtrace_anon.dta_enabling != NULL); 13697 } 13698 13699 if (dtrace_anon.dta_enabling != NULL) { 13700 int rval; 13701 13702 /* 13703 * dtrace_enabling_retain() can only fail because we are 13704 * trying to retain more enablings than are allowed -- but 13705 * we only have one anonymous enabling, and we are guaranteed 13706 * to be allowed at least one retained enabling; we assert 13707 * that dtrace_enabling_retain() returns success. 13708 */ 13709 rval = dtrace_enabling_retain(dtrace_anon.dta_enabling); 13710 ASSERT(rval == 0); 13711 13712 dtrace_enabling_dump(dtrace_anon.dta_enabling); 13713 } 13714} 13715 13716#if defined(sun) 13717/* 13718 * DTrace Helper Functions 13719 */ 13720static void 13721dtrace_helper_trace(dtrace_helper_action_t *helper, 13722 dtrace_mstate_t *mstate, dtrace_vstate_t *vstate, int where) 13723{ 13724 uint32_t size, next, nnext, i; 13725 dtrace_helptrace_t *ent; 13726 uint16_t flags = cpu_core[curcpu].cpuc_dtrace_flags; 13727 13728 if (!dtrace_helptrace_enabled) 13729 return; 13730 13731 ASSERT(vstate->dtvs_nlocals <= dtrace_helptrace_nlocals); 13732 13733 /* 13734 * What would a tracing framework be without its own tracing 13735 * framework? (Well, a hell of a lot simpler, for starters...) 13736 */ 13737 size = sizeof (dtrace_helptrace_t) + dtrace_helptrace_nlocals * 13738 sizeof (uint64_t) - sizeof (uint64_t); 13739 13740 /* 13741 * Iterate until we can allocate a slot in the trace buffer. 13742 */ 13743 do { 13744 next = dtrace_helptrace_next; 13745 13746 if (next + size < dtrace_helptrace_bufsize) { 13747 nnext = next + size; 13748 } else { 13749 nnext = size; 13750 } 13751 } while (dtrace_cas32(&dtrace_helptrace_next, next, nnext) != next); 13752 13753 /* 13754 * We have our slot; fill it in. 13755 */ 13756 if (nnext == size) 13757 next = 0; 13758 13759 ent = (dtrace_helptrace_t *)&dtrace_helptrace_buffer[next]; 13760 ent->dtht_helper = helper; 13761 ent->dtht_where = where; 13762 ent->dtht_nlocals = vstate->dtvs_nlocals; 13763 13764 ent->dtht_fltoffs = (mstate->dtms_present & DTRACE_MSTATE_FLTOFFS) ? 13765 mstate->dtms_fltoffs : -1; 13766 ent->dtht_fault = DTRACE_FLAGS2FLT(flags); 13767 ent->dtht_illval = cpu_core[curcpu].cpuc_dtrace_illval; 13768 13769 for (i = 0; i < vstate->dtvs_nlocals; i++) { 13770 dtrace_statvar_t *svar; 13771 13772 if ((svar = vstate->dtvs_locals[i]) == NULL) 13773 continue; 13774 13775 ASSERT(svar->dtsv_size >= NCPU * sizeof (uint64_t)); 13776 ent->dtht_locals[i] = 13777 ((uint64_t *)(uintptr_t)svar->dtsv_data)[curcpu]; 13778 } 13779} 13780#endif 13781 13782#if defined(sun) 13783static uint64_t 13784dtrace_helper(int which, dtrace_mstate_t *mstate, 13785 dtrace_state_t *state, uint64_t arg0, uint64_t arg1) 13786{ 13787 uint16_t *flags = &cpu_core[curcpu].cpuc_dtrace_flags; 13788 uint64_t sarg0 = mstate->dtms_arg[0]; 13789 uint64_t sarg1 = mstate->dtms_arg[1]; 13790 uint64_t rval; 13791 dtrace_helpers_t *helpers = curproc->p_dtrace_helpers; 13792 dtrace_helper_action_t *helper; 13793 dtrace_vstate_t *vstate; 13794 dtrace_difo_t *pred; 13795 int i, trace = dtrace_helptrace_enabled; 13796 13797 ASSERT(which >= 0 && which < DTRACE_NHELPER_ACTIONS); 13798 13799 if (helpers == NULL) 13800 return (0); 13801 13802 if ((helper = helpers->dthps_actions[which]) == NULL) 13803 return (0); 13804 13805 vstate = &helpers->dthps_vstate; 13806 mstate->dtms_arg[0] = arg0; 13807 mstate->dtms_arg[1] = arg1; 13808 13809 /* 13810 * Now iterate over each helper. If its predicate evaluates to 'true', 13811 * we'll call the corresponding actions. Note that the below calls 13812 * to dtrace_dif_emulate() may set faults in machine state. This is 13813 * okay: our caller (the outer dtrace_dif_emulate()) will simply plow 13814 * the stored DIF offset with its own (which is the desired behavior). 13815 * Also, note the calls to dtrace_dif_emulate() may allocate scratch 13816 * from machine state; this is okay, too. 13817 */ 13818 for (; helper != NULL; helper = helper->dtha_next) { 13819 if ((pred = helper->dtha_predicate) != NULL) { 13820 if (trace) 13821 dtrace_helper_trace(helper, mstate, vstate, 0); 13822 13823 if (!dtrace_dif_emulate(pred, mstate, vstate, state)) 13824 goto next; 13825 13826 if (*flags & CPU_DTRACE_FAULT) 13827 goto err; 13828 } 13829 13830 for (i = 0; i < helper->dtha_nactions; i++) { 13831 if (trace) 13832 dtrace_helper_trace(helper, 13833 mstate, vstate, i + 1); 13834 13835 rval = dtrace_dif_emulate(helper->dtha_actions[i], 13836 mstate, vstate, state); 13837 13838 if (*flags & CPU_DTRACE_FAULT) 13839 goto err; 13840 } 13841 13842next: 13843 if (trace) 13844 dtrace_helper_trace(helper, mstate, vstate, 13845 DTRACE_HELPTRACE_NEXT); 13846 } 13847 13848 if (trace) 13849 dtrace_helper_trace(helper, mstate, vstate, 13850 DTRACE_HELPTRACE_DONE); 13851 13852 /* 13853 * Restore the arg0 that we saved upon entry. 13854 */ 13855 mstate->dtms_arg[0] = sarg0; 13856 mstate->dtms_arg[1] = sarg1; 13857 13858 return (rval); 13859 13860err: 13861 if (trace) 13862 dtrace_helper_trace(helper, mstate, vstate, 13863 DTRACE_HELPTRACE_ERR); 13864 13865 /* 13866 * Restore the arg0 that we saved upon entry. 13867 */ 13868 mstate->dtms_arg[0] = sarg0; 13869 mstate->dtms_arg[1] = sarg1; 13870 13871 return (0); 13872} 13873 13874static void 13875dtrace_helper_action_destroy(dtrace_helper_action_t *helper, 13876 dtrace_vstate_t *vstate) 13877{ 13878 int i; 13879 13880 if (helper->dtha_predicate != NULL) 13881 dtrace_difo_release(helper->dtha_predicate, vstate); 13882 13883 for (i = 0; i < helper->dtha_nactions; i++) { 13884 ASSERT(helper->dtha_actions[i] != NULL); 13885 dtrace_difo_release(helper->dtha_actions[i], vstate); 13886 } 13887 13888 kmem_free(helper->dtha_actions, 13889 helper->dtha_nactions * sizeof (dtrace_difo_t *)); 13890 kmem_free(helper, sizeof (dtrace_helper_action_t)); 13891} 13892 13893static int 13894dtrace_helper_destroygen(int gen) 13895{ 13896 proc_t *p = curproc; 13897 dtrace_helpers_t *help = p->p_dtrace_helpers; 13898 dtrace_vstate_t *vstate; 13899 int i; 13900 13901 ASSERT(MUTEX_HELD(&dtrace_lock)); 13902 13903 if (help == NULL || gen > help->dthps_generation) 13904 return (EINVAL); 13905 13906 vstate = &help->dthps_vstate; 13907 13908 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 13909 dtrace_helper_action_t *last = NULL, *h, *next; 13910 13911 for (h = help->dthps_actions[i]; h != NULL; h = next) { 13912 next = h->dtha_next; 13913 13914 if (h->dtha_generation == gen) { 13915 if (last != NULL) { 13916 last->dtha_next = next; 13917 } else { 13918 help->dthps_actions[i] = next; 13919 } 13920 13921 dtrace_helper_action_destroy(h, vstate); 13922 } else { 13923 last = h; 13924 } 13925 } 13926 } 13927 13928 /* 13929 * Interate until we've cleared out all helper providers with the 13930 * given generation number. 13931 */ 13932 for (;;) { 13933 dtrace_helper_provider_t *prov; 13934 13935 /* 13936 * Look for a helper provider with the right generation. We 13937 * have to start back at the beginning of the list each time 13938 * because we drop dtrace_lock. It's unlikely that we'll make 13939 * more than two passes. 13940 */ 13941 for (i = 0; i < help->dthps_nprovs; i++) { 13942 prov = help->dthps_provs[i]; 13943 13944 if (prov->dthp_generation == gen) 13945 break; 13946 } 13947 13948 /* 13949 * If there were no matches, we're done. 13950 */ 13951 if (i == help->dthps_nprovs) 13952 break; 13953 13954 /* 13955 * Move the last helper provider into this slot. 13956 */ 13957 help->dthps_nprovs--; 13958 help->dthps_provs[i] = help->dthps_provs[help->dthps_nprovs]; 13959 help->dthps_provs[help->dthps_nprovs] = NULL; 13960 13961 mutex_exit(&dtrace_lock); 13962 13963 /* 13964 * If we have a meta provider, remove this helper provider. 13965 */ 13966 mutex_enter(&dtrace_meta_lock); 13967 if (dtrace_meta_pid != NULL) { 13968 ASSERT(dtrace_deferred_pid == NULL); 13969 dtrace_helper_provider_remove(&prov->dthp_prov, 13970 p->p_pid); 13971 } 13972 mutex_exit(&dtrace_meta_lock); 13973 13974 dtrace_helper_provider_destroy(prov); 13975 13976 mutex_enter(&dtrace_lock); 13977 } 13978 13979 return (0); 13980} 13981#endif 13982 13983#if defined(sun) 13984static int 13985dtrace_helper_validate(dtrace_helper_action_t *helper) 13986{ 13987 int err = 0, i; 13988 dtrace_difo_t *dp; 13989 13990 if ((dp = helper->dtha_predicate) != NULL) 13991 err += dtrace_difo_validate_helper(dp); 13992 13993 for (i = 0; i < helper->dtha_nactions; i++) 13994 err += dtrace_difo_validate_helper(helper->dtha_actions[i]); 13995 13996 return (err == 0); 13997} 13998#endif 13999 14000#if defined(sun) 14001static int 14002dtrace_helper_action_add(int which, dtrace_ecbdesc_t *ep) 14003{ 14004 dtrace_helpers_t *help; 14005 dtrace_helper_action_t *helper, *last; 14006 dtrace_actdesc_t *act; 14007 dtrace_vstate_t *vstate; 14008 dtrace_predicate_t *pred; 14009 int count = 0, nactions = 0, i; 14010 14011 if (which < 0 || which >= DTRACE_NHELPER_ACTIONS) 14012 return (EINVAL); 14013 14014 help = curproc->p_dtrace_helpers; 14015 last = help->dthps_actions[which]; 14016 vstate = &help->dthps_vstate; 14017 14018 for (count = 0; last != NULL; last = last->dtha_next) { 14019 count++; 14020 if (last->dtha_next == NULL) 14021 break; 14022 } 14023 14024 /* 14025 * If we already have dtrace_helper_actions_max helper actions for this 14026 * helper action type, we'll refuse to add a new one. 14027 */ 14028 if (count >= dtrace_helper_actions_max) 14029 return (ENOSPC); 14030 14031 helper = kmem_zalloc(sizeof (dtrace_helper_action_t), KM_SLEEP); 14032 helper->dtha_generation = help->dthps_generation; 14033 14034 if ((pred = ep->dted_pred.dtpdd_predicate) != NULL) { 14035 ASSERT(pred->dtp_difo != NULL); 14036 dtrace_difo_hold(pred->dtp_difo); 14037 helper->dtha_predicate = pred->dtp_difo; 14038 } 14039 14040 for (act = ep->dted_action; act != NULL; act = act->dtad_next) { 14041 if (act->dtad_kind != DTRACEACT_DIFEXPR) 14042 goto err; 14043 14044 if (act->dtad_difo == NULL) 14045 goto err; 14046 14047 nactions++; 14048 } 14049 14050 helper->dtha_actions = kmem_zalloc(sizeof (dtrace_difo_t *) * 14051 (helper->dtha_nactions = nactions), KM_SLEEP); 14052 14053 for (act = ep->dted_action, i = 0; act != NULL; act = act->dtad_next) { 14054 dtrace_difo_hold(act->dtad_difo); 14055 helper->dtha_actions[i++] = act->dtad_difo; 14056 } 14057 14058 if (!dtrace_helper_validate(helper)) 14059 goto err; 14060 14061 if (last == NULL) { 14062 help->dthps_actions[which] = helper; 14063 } else { 14064 last->dtha_next = helper; 14065 } 14066 14067 if (vstate->dtvs_nlocals > dtrace_helptrace_nlocals) { 14068 dtrace_helptrace_nlocals = vstate->dtvs_nlocals; 14069 dtrace_helptrace_next = 0; 14070 } 14071 14072 return (0); 14073err: 14074 dtrace_helper_action_destroy(helper, vstate); 14075 return (EINVAL); 14076} 14077 14078static void 14079dtrace_helper_provider_register(proc_t *p, dtrace_helpers_t *help, 14080 dof_helper_t *dofhp) 14081{ 14082 ASSERT(MUTEX_NOT_HELD(&dtrace_lock)); 14083 14084 mutex_enter(&dtrace_meta_lock); 14085 mutex_enter(&dtrace_lock); 14086 14087 if (!dtrace_attached() || dtrace_meta_pid == NULL) { 14088 /* 14089 * If the dtrace module is loaded but not attached, or if 14090 * there aren't isn't a meta provider registered to deal with 14091 * these provider descriptions, we need to postpone creating 14092 * the actual providers until later. 14093 */ 14094 14095 if (help->dthps_next == NULL && help->dthps_prev == NULL && 14096 dtrace_deferred_pid != help) { 14097 help->dthps_deferred = 1; 14098 help->dthps_pid = p->p_pid; 14099 help->dthps_next = dtrace_deferred_pid; 14100 help->dthps_prev = NULL; 14101 if (dtrace_deferred_pid != NULL) 14102 dtrace_deferred_pid->dthps_prev = help; 14103 dtrace_deferred_pid = help; 14104 } 14105 14106 mutex_exit(&dtrace_lock); 14107 14108 } else if (dofhp != NULL) { 14109 /* 14110 * If the dtrace module is loaded and we have a particular 14111 * helper provider description, pass that off to the 14112 * meta provider. 14113 */ 14114 14115 mutex_exit(&dtrace_lock); 14116 14117 dtrace_helper_provide(dofhp, p->p_pid); 14118 14119 } else { 14120 /* 14121 * Otherwise, just pass all the helper provider descriptions 14122 * off to the meta provider. 14123 */ 14124 14125 int i; 14126 mutex_exit(&dtrace_lock); 14127 14128 for (i = 0; i < help->dthps_nprovs; i++) { 14129 dtrace_helper_provide(&help->dthps_provs[i]->dthp_prov, 14130 p->p_pid); 14131 } 14132 } 14133 14134 mutex_exit(&dtrace_meta_lock); 14135} 14136 14137static int 14138dtrace_helper_provider_add(dof_helper_t *dofhp, int gen) 14139{ 14140 dtrace_helpers_t *help; 14141 dtrace_helper_provider_t *hprov, **tmp_provs; 14142 uint_t tmp_maxprovs, i; 14143 14144 ASSERT(MUTEX_HELD(&dtrace_lock)); 14145 14146 help = curproc->p_dtrace_helpers; 14147 ASSERT(help != NULL); 14148 14149 /* 14150 * If we already have dtrace_helper_providers_max helper providers, 14151 * we're refuse to add a new one. 14152 */ 14153 if (help->dthps_nprovs >= dtrace_helper_providers_max) 14154 return (ENOSPC); 14155 14156 /* 14157 * Check to make sure this isn't a duplicate. 14158 */ 14159 for (i = 0; i < help->dthps_nprovs; i++) { 14160 if (dofhp->dofhp_addr == 14161 help->dthps_provs[i]->dthp_prov.dofhp_addr) 14162 return (EALREADY); 14163 } 14164 14165 hprov = kmem_zalloc(sizeof (dtrace_helper_provider_t), KM_SLEEP); 14166 hprov->dthp_prov = *dofhp; 14167 hprov->dthp_ref = 1; 14168 hprov->dthp_generation = gen; 14169 14170 /* 14171 * Allocate a bigger table for helper providers if it's already full. 14172 */ 14173 if (help->dthps_maxprovs == help->dthps_nprovs) { 14174 tmp_maxprovs = help->dthps_maxprovs; 14175 tmp_provs = help->dthps_provs; 14176 14177 if (help->dthps_maxprovs == 0) 14178 help->dthps_maxprovs = 2; 14179 else 14180 help->dthps_maxprovs *= 2; 14181 if (help->dthps_maxprovs > dtrace_helper_providers_max) 14182 help->dthps_maxprovs = dtrace_helper_providers_max; 14183 14184 ASSERT(tmp_maxprovs < help->dthps_maxprovs); 14185 14186 help->dthps_provs = kmem_zalloc(help->dthps_maxprovs * 14187 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14188 14189 if (tmp_provs != NULL) { 14190 bcopy(tmp_provs, help->dthps_provs, tmp_maxprovs * 14191 sizeof (dtrace_helper_provider_t *)); 14192 kmem_free(tmp_provs, tmp_maxprovs * 14193 sizeof (dtrace_helper_provider_t *)); 14194 } 14195 } 14196 14197 help->dthps_provs[help->dthps_nprovs] = hprov; 14198 help->dthps_nprovs++; 14199 14200 return (0); 14201} 14202 14203static void 14204dtrace_helper_provider_destroy(dtrace_helper_provider_t *hprov) 14205{ 14206 mutex_enter(&dtrace_lock); 14207 14208 if (--hprov->dthp_ref == 0) { 14209 dof_hdr_t *dof; 14210 mutex_exit(&dtrace_lock); 14211 dof = (dof_hdr_t *)(uintptr_t)hprov->dthp_prov.dofhp_dof; 14212 dtrace_dof_destroy(dof); 14213 kmem_free(hprov, sizeof (dtrace_helper_provider_t)); 14214 } else { 14215 mutex_exit(&dtrace_lock); 14216 } 14217} 14218 14219static int 14220dtrace_helper_provider_validate(dof_hdr_t *dof, dof_sec_t *sec) 14221{ 14222 uintptr_t daddr = (uintptr_t)dof; 14223 dof_sec_t *str_sec, *prb_sec, *arg_sec, *off_sec, *enoff_sec; 14224 dof_provider_t *provider; 14225 dof_probe_t *probe; 14226 uint8_t *arg; 14227 char *strtab, *typestr; 14228 dof_stridx_t typeidx; 14229 size_t typesz; 14230 uint_t nprobes, j, k; 14231 14232 ASSERT(sec->dofs_type == DOF_SECT_PROVIDER); 14233 14234 if (sec->dofs_offset & (sizeof (uint_t) - 1)) { 14235 dtrace_dof_error(dof, "misaligned section offset"); 14236 return (-1); 14237 } 14238 14239 /* 14240 * The section needs to be large enough to contain the DOF provider 14241 * structure appropriate for the given version. 14242 */ 14243 if (sec->dofs_size < 14244 ((dof->dofh_ident[DOF_ID_VERSION] == DOF_VERSION_1) ? 14245 offsetof(dof_provider_t, dofpv_prenoffs) : 14246 sizeof (dof_provider_t))) { 14247 dtrace_dof_error(dof, "provider section too small"); 14248 return (-1); 14249 } 14250 14251 provider = (dof_provider_t *)(uintptr_t)(daddr + sec->dofs_offset); 14252 str_sec = dtrace_dof_sect(dof, DOF_SECT_STRTAB, provider->dofpv_strtab); 14253 prb_sec = dtrace_dof_sect(dof, DOF_SECT_PROBES, provider->dofpv_probes); 14254 arg_sec = dtrace_dof_sect(dof, DOF_SECT_PRARGS, provider->dofpv_prargs); 14255 off_sec = dtrace_dof_sect(dof, DOF_SECT_PROFFS, provider->dofpv_proffs); 14256 14257 if (str_sec == NULL || prb_sec == NULL || 14258 arg_sec == NULL || off_sec == NULL) 14259 return (-1); 14260 14261 enoff_sec = NULL; 14262 14263 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1 && 14264 provider->dofpv_prenoffs != DOF_SECT_NONE && 14265 (enoff_sec = dtrace_dof_sect(dof, DOF_SECT_PRENOFFS, 14266 provider->dofpv_prenoffs)) == NULL) 14267 return (-1); 14268 14269 strtab = (char *)(uintptr_t)(daddr + str_sec->dofs_offset); 14270 14271 if (provider->dofpv_name >= str_sec->dofs_size || 14272 strlen(strtab + provider->dofpv_name) >= DTRACE_PROVNAMELEN) { 14273 dtrace_dof_error(dof, "invalid provider name"); 14274 return (-1); 14275 } 14276 14277 if (prb_sec->dofs_entsize == 0 || 14278 prb_sec->dofs_entsize > prb_sec->dofs_size) { 14279 dtrace_dof_error(dof, "invalid entry size"); 14280 return (-1); 14281 } 14282 14283 if (prb_sec->dofs_entsize & (sizeof (uintptr_t) - 1)) { 14284 dtrace_dof_error(dof, "misaligned entry size"); 14285 return (-1); 14286 } 14287 14288 if (off_sec->dofs_entsize != sizeof (uint32_t)) { 14289 dtrace_dof_error(dof, "invalid entry size"); 14290 return (-1); 14291 } 14292 14293 if (off_sec->dofs_offset & (sizeof (uint32_t) - 1)) { 14294 dtrace_dof_error(dof, "misaligned section offset"); 14295 return (-1); 14296 } 14297 14298 if (arg_sec->dofs_entsize != sizeof (uint8_t)) { 14299 dtrace_dof_error(dof, "invalid entry size"); 14300 return (-1); 14301 } 14302 14303 arg = (uint8_t *)(uintptr_t)(daddr + arg_sec->dofs_offset); 14304 14305 nprobes = prb_sec->dofs_size / prb_sec->dofs_entsize; 14306 14307 /* 14308 * Take a pass through the probes to check for errors. 14309 */ 14310 for (j = 0; j < nprobes; j++) { 14311 probe = (dof_probe_t *)(uintptr_t)(daddr + 14312 prb_sec->dofs_offset + j * prb_sec->dofs_entsize); 14313 14314 if (probe->dofpr_func >= str_sec->dofs_size) { 14315 dtrace_dof_error(dof, "invalid function name"); 14316 return (-1); 14317 } 14318 14319 if (strlen(strtab + probe->dofpr_func) >= DTRACE_FUNCNAMELEN) { 14320 dtrace_dof_error(dof, "function name too long"); 14321 return (-1); 14322 } 14323 14324 if (probe->dofpr_name >= str_sec->dofs_size || 14325 strlen(strtab + probe->dofpr_name) >= DTRACE_NAMELEN) { 14326 dtrace_dof_error(dof, "invalid probe name"); 14327 return (-1); 14328 } 14329 14330 /* 14331 * The offset count must not wrap the index, and the offsets 14332 * must also not overflow the section's data. 14333 */ 14334 if (probe->dofpr_offidx + probe->dofpr_noffs < 14335 probe->dofpr_offidx || 14336 (probe->dofpr_offidx + probe->dofpr_noffs) * 14337 off_sec->dofs_entsize > off_sec->dofs_size) { 14338 dtrace_dof_error(dof, "invalid probe offset"); 14339 return (-1); 14340 } 14341 14342 if (dof->dofh_ident[DOF_ID_VERSION] != DOF_VERSION_1) { 14343 /* 14344 * If there's no is-enabled offset section, make sure 14345 * there aren't any is-enabled offsets. Otherwise 14346 * perform the same checks as for probe offsets 14347 * (immediately above). 14348 */ 14349 if (enoff_sec == NULL) { 14350 if (probe->dofpr_enoffidx != 0 || 14351 probe->dofpr_nenoffs != 0) { 14352 dtrace_dof_error(dof, "is-enabled " 14353 "offsets with null section"); 14354 return (-1); 14355 } 14356 } else if (probe->dofpr_enoffidx + 14357 probe->dofpr_nenoffs < probe->dofpr_enoffidx || 14358 (probe->dofpr_enoffidx + probe->dofpr_nenoffs) * 14359 enoff_sec->dofs_entsize > enoff_sec->dofs_size) { 14360 dtrace_dof_error(dof, "invalid is-enabled " 14361 "offset"); 14362 return (-1); 14363 } 14364 14365 if (probe->dofpr_noffs + probe->dofpr_nenoffs == 0) { 14366 dtrace_dof_error(dof, "zero probe and " 14367 "is-enabled offsets"); 14368 return (-1); 14369 } 14370 } else if (probe->dofpr_noffs == 0) { 14371 dtrace_dof_error(dof, "zero probe offsets"); 14372 return (-1); 14373 } 14374 14375 if (probe->dofpr_argidx + probe->dofpr_xargc < 14376 probe->dofpr_argidx || 14377 (probe->dofpr_argidx + probe->dofpr_xargc) * 14378 arg_sec->dofs_entsize > arg_sec->dofs_size) { 14379 dtrace_dof_error(dof, "invalid args"); 14380 return (-1); 14381 } 14382 14383 typeidx = probe->dofpr_nargv; 14384 typestr = strtab + probe->dofpr_nargv; 14385 for (k = 0; k < probe->dofpr_nargc; k++) { 14386 if (typeidx >= str_sec->dofs_size) { 14387 dtrace_dof_error(dof, "bad " 14388 "native argument type"); 14389 return (-1); 14390 } 14391 14392 typesz = strlen(typestr) + 1; 14393 if (typesz > DTRACE_ARGTYPELEN) { 14394 dtrace_dof_error(dof, "native " 14395 "argument type too long"); 14396 return (-1); 14397 } 14398 typeidx += typesz; 14399 typestr += typesz; 14400 } 14401 14402 typeidx = probe->dofpr_xargv; 14403 typestr = strtab + probe->dofpr_xargv; 14404 for (k = 0; k < probe->dofpr_xargc; k++) { 14405 if (arg[probe->dofpr_argidx + k] > probe->dofpr_nargc) { 14406 dtrace_dof_error(dof, "bad " 14407 "native argument index"); 14408 return (-1); 14409 } 14410 14411 if (typeidx >= str_sec->dofs_size) { 14412 dtrace_dof_error(dof, "bad " 14413 "translated argument type"); 14414 return (-1); 14415 } 14416 14417 typesz = strlen(typestr) + 1; 14418 if (typesz > DTRACE_ARGTYPELEN) { 14419 dtrace_dof_error(dof, "translated argument " 14420 "type too long"); 14421 return (-1); 14422 } 14423 14424 typeidx += typesz; 14425 typestr += typesz; 14426 } 14427 } 14428 14429 return (0); 14430} 14431 14432static int 14433dtrace_helper_slurp(dof_hdr_t *dof, dof_helper_t *dhp) 14434{ 14435 dtrace_helpers_t *help; 14436 dtrace_vstate_t *vstate; 14437 dtrace_enabling_t *enab = NULL; 14438 int i, gen, rv, nhelpers = 0, nprovs = 0, destroy = 1; 14439 uintptr_t daddr = (uintptr_t)dof; 14440 14441 ASSERT(MUTEX_HELD(&dtrace_lock)); 14442 14443 if ((help = curproc->p_dtrace_helpers) == NULL) 14444 help = dtrace_helpers_create(curproc); 14445 14446 vstate = &help->dthps_vstate; 14447 14448 if ((rv = dtrace_dof_slurp(dof, vstate, NULL, &enab, 14449 dhp != NULL ? dhp->dofhp_addr : 0, B_FALSE)) != 0) { 14450 dtrace_dof_destroy(dof); 14451 return (rv); 14452 } 14453 14454 /* 14455 * Look for helper providers and validate their descriptions. 14456 */ 14457 if (dhp != NULL) { 14458 for (i = 0; i < dof->dofh_secnum; i++) { 14459 dof_sec_t *sec = (dof_sec_t *)(uintptr_t)(daddr + 14460 dof->dofh_secoff + i * dof->dofh_secsize); 14461 14462 if (sec->dofs_type != DOF_SECT_PROVIDER) 14463 continue; 14464 14465 if (dtrace_helper_provider_validate(dof, sec) != 0) { 14466 dtrace_enabling_destroy(enab); 14467 dtrace_dof_destroy(dof); 14468 return (-1); 14469 } 14470 14471 nprovs++; 14472 } 14473 } 14474 14475 /* 14476 * Now we need to walk through the ECB descriptions in the enabling. 14477 */ 14478 for (i = 0; i < enab->dten_ndesc; i++) { 14479 dtrace_ecbdesc_t *ep = enab->dten_desc[i]; 14480 dtrace_probedesc_t *desc = &ep->dted_probe; 14481 14482 if (strcmp(desc->dtpd_provider, "dtrace") != 0) 14483 continue; 14484 14485 if (strcmp(desc->dtpd_mod, "helper") != 0) 14486 continue; 14487 14488 if (strcmp(desc->dtpd_func, "ustack") != 0) 14489 continue; 14490 14491 if ((rv = dtrace_helper_action_add(DTRACE_HELPER_ACTION_USTACK, 14492 ep)) != 0) { 14493 /* 14494 * Adding this helper action failed -- we are now going 14495 * to rip out the entire generation and return failure. 14496 */ 14497 (void) dtrace_helper_destroygen(help->dthps_generation); 14498 dtrace_enabling_destroy(enab); 14499 dtrace_dof_destroy(dof); 14500 return (-1); 14501 } 14502 14503 nhelpers++; 14504 } 14505 14506 if (nhelpers < enab->dten_ndesc) 14507 dtrace_dof_error(dof, "unmatched helpers"); 14508 14509 gen = help->dthps_generation++; 14510 dtrace_enabling_destroy(enab); 14511 14512 if (dhp != NULL && nprovs > 0) { 14513 dhp->dofhp_dof = (uint64_t)(uintptr_t)dof; 14514 if (dtrace_helper_provider_add(dhp, gen) == 0) { 14515 mutex_exit(&dtrace_lock); 14516 dtrace_helper_provider_register(curproc, help, dhp); 14517 mutex_enter(&dtrace_lock); 14518 14519 destroy = 0; 14520 } 14521 } 14522 14523 if (destroy) 14524 dtrace_dof_destroy(dof); 14525 14526 return (gen); 14527} 14528 14529static dtrace_helpers_t * 14530dtrace_helpers_create(proc_t *p) 14531{ 14532 dtrace_helpers_t *help; 14533 14534 ASSERT(MUTEX_HELD(&dtrace_lock)); 14535 ASSERT(p->p_dtrace_helpers == NULL); 14536 14537 help = kmem_zalloc(sizeof (dtrace_helpers_t), KM_SLEEP); 14538 help->dthps_actions = kmem_zalloc(sizeof (dtrace_helper_action_t *) * 14539 DTRACE_NHELPER_ACTIONS, KM_SLEEP); 14540 14541 p->p_dtrace_helpers = help; 14542 dtrace_helpers++; 14543 14544 return (help); 14545} 14546 14547static void 14548dtrace_helpers_destroy(void) 14549{ 14550 dtrace_helpers_t *help; 14551 dtrace_vstate_t *vstate; 14552 proc_t *p = curproc; 14553 int i; 14554 14555 mutex_enter(&dtrace_lock); 14556 14557 ASSERT(p->p_dtrace_helpers != NULL); 14558 ASSERT(dtrace_helpers > 0); 14559 14560 help = p->p_dtrace_helpers; 14561 vstate = &help->dthps_vstate; 14562 14563 /* 14564 * We're now going to lose the help from this process. 14565 */ 14566 p->p_dtrace_helpers = NULL; 14567 dtrace_sync(); 14568 14569 /* 14570 * Destory the helper actions. 14571 */ 14572 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14573 dtrace_helper_action_t *h, *next; 14574 14575 for (h = help->dthps_actions[i]; h != NULL; h = next) { 14576 next = h->dtha_next; 14577 dtrace_helper_action_destroy(h, vstate); 14578 h = next; 14579 } 14580 } 14581 14582 mutex_exit(&dtrace_lock); 14583 14584 /* 14585 * Destroy the helper providers. 14586 */ 14587 if (help->dthps_maxprovs > 0) { 14588 mutex_enter(&dtrace_meta_lock); 14589 if (dtrace_meta_pid != NULL) { 14590 ASSERT(dtrace_deferred_pid == NULL); 14591 14592 for (i = 0; i < help->dthps_nprovs; i++) { 14593 dtrace_helper_provider_remove( 14594 &help->dthps_provs[i]->dthp_prov, p->p_pid); 14595 } 14596 } else { 14597 mutex_enter(&dtrace_lock); 14598 ASSERT(help->dthps_deferred == 0 || 14599 help->dthps_next != NULL || 14600 help->dthps_prev != NULL || 14601 help == dtrace_deferred_pid); 14602 14603 /* 14604 * Remove the helper from the deferred list. 14605 */ 14606 if (help->dthps_next != NULL) 14607 help->dthps_next->dthps_prev = help->dthps_prev; 14608 if (help->dthps_prev != NULL) 14609 help->dthps_prev->dthps_next = help->dthps_next; 14610 if (dtrace_deferred_pid == help) { 14611 dtrace_deferred_pid = help->dthps_next; 14612 ASSERT(help->dthps_prev == NULL); 14613 } 14614 14615 mutex_exit(&dtrace_lock); 14616 } 14617 14618 mutex_exit(&dtrace_meta_lock); 14619 14620 for (i = 0; i < help->dthps_nprovs; i++) { 14621 dtrace_helper_provider_destroy(help->dthps_provs[i]); 14622 } 14623 14624 kmem_free(help->dthps_provs, help->dthps_maxprovs * 14625 sizeof (dtrace_helper_provider_t *)); 14626 } 14627 14628 mutex_enter(&dtrace_lock); 14629 14630 dtrace_vstate_fini(&help->dthps_vstate); 14631 kmem_free(help->dthps_actions, 14632 sizeof (dtrace_helper_action_t *) * DTRACE_NHELPER_ACTIONS); 14633 kmem_free(help, sizeof (dtrace_helpers_t)); 14634 14635 --dtrace_helpers; 14636 mutex_exit(&dtrace_lock); 14637} 14638 14639static void 14640dtrace_helpers_duplicate(proc_t *from, proc_t *to) 14641{ 14642 dtrace_helpers_t *help, *newhelp; 14643 dtrace_helper_action_t *helper, *new, *last; 14644 dtrace_difo_t *dp; 14645 dtrace_vstate_t *vstate; 14646 int i, j, sz, hasprovs = 0; 14647 14648 mutex_enter(&dtrace_lock); 14649 ASSERT(from->p_dtrace_helpers != NULL); 14650 ASSERT(dtrace_helpers > 0); 14651 14652 help = from->p_dtrace_helpers; 14653 newhelp = dtrace_helpers_create(to); 14654 ASSERT(to->p_dtrace_helpers != NULL); 14655 14656 newhelp->dthps_generation = help->dthps_generation; 14657 vstate = &newhelp->dthps_vstate; 14658 14659 /* 14660 * Duplicate the helper actions. 14661 */ 14662 for (i = 0; i < DTRACE_NHELPER_ACTIONS; i++) { 14663 if ((helper = help->dthps_actions[i]) == NULL) 14664 continue; 14665 14666 for (last = NULL; helper != NULL; helper = helper->dtha_next) { 14667 new = kmem_zalloc(sizeof (dtrace_helper_action_t), 14668 KM_SLEEP); 14669 new->dtha_generation = helper->dtha_generation; 14670 14671 if ((dp = helper->dtha_predicate) != NULL) { 14672 dp = dtrace_difo_duplicate(dp, vstate); 14673 new->dtha_predicate = dp; 14674 } 14675 14676 new->dtha_nactions = helper->dtha_nactions; 14677 sz = sizeof (dtrace_difo_t *) * new->dtha_nactions; 14678 new->dtha_actions = kmem_alloc(sz, KM_SLEEP); 14679 14680 for (j = 0; j < new->dtha_nactions; j++) { 14681 dtrace_difo_t *dp = helper->dtha_actions[j]; 14682 14683 ASSERT(dp != NULL); 14684 dp = dtrace_difo_duplicate(dp, vstate); 14685 new->dtha_actions[j] = dp; 14686 } 14687 14688 if (last != NULL) { 14689 last->dtha_next = new; 14690 } else { 14691 newhelp->dthps_actions[i] = new; 14692 } 14693 14694 last = new; 14695 } 14696 } 14697 14698 /* 14699 * Duplicate the helper providers and register them with the 14700 * DTrace framework. 14701 */ 14702 if (help->dthps_nprovs > 0) { 14703 newhelp->dthps_nprovs = help->dthps_nprovs; 14704 newhelp->dthps_maxprovs = help->dthps_nprovs; 14705 newhelp->dthps_provs = kmem_alloc(newhelp->dthps_nprovs * 14706 sizeof (dtrace_helper_provider_t *), KM_SLEEP); 14707 for (i = 0; i < newhelp->dthps_nprovs; i++) { 14708 newhelp->dthps_provs[i] = help->dthps_provs[i]; 14709 newhelp->dthps_provs[i]->dthp_ref++; 14710 } 14711 14712 hasprovs = 1; 14713 } 14714 14715 mutex_exit(&dtrace_lock); 14716 14717 if (hasprovs) 14718 dtrace_helper_provider_register(to, newhelp, NULL); 14719} 14720#endif 14721 14722#if defined(sun) 14723/* 14724 * DTrace Hook Functions 14725 */ 14726static void 14727dtrace_module_loaded(modctl_t *ctl) 14728{ 14729 dtrace_provider_t *prv; 14730 14731 mutex_enter(&dtrace_provider_lock); 14732 mutex_enter(&mod_lock); 14733 14734 ASSERT(ctl->mod_busy); 14735 14736 /* 14737 * We're going to call each providers per-module provide operation 14738 * specifying only this module. 14739 */ 14740 for (prv = dtrace_provider; prv != NULL; prv = prv->dtpv_next) 14741 prv->dtpv_pops.dtps_provide_module(prv->dtpv_arg, ctl); 14742 14743 mutex_exit(&mod_lock); 14744 mutex_exit(&dtrace_provider_lock); 14745 14746 /* 14747 * If we have any retained enablings, we need to match against them. 14748 * Enabling probes requires that cpu_lock be held, and we cannot hold 14749 * cpu_lock here -- it is legal for cpu_lock to be held when loading a 14750 * module. (In particular, this happens when loading scheduling 14751 * classes.) So if we have any retained enablings, we need to dispatch 14752 * our task queue to do the match for us. 14753 */ 14754 mutex_enter(&dtrace_lock); 14755 14756 if (dtrace_retained == NULL) { 14757 mutex_exit(&dtrace_lock); 14758 return; 14759 } 14760 14761 (void) taskq_dispatch(dtrace_taskq, 14762 (task_func_t *)dtrace_enabling_matchall, NULL, TQ_SLEEP); 14763 14764 mutex_exit(&dtrace_lock); 14765 14766 /* 14767 * And now, for a little heuristic sleaze: in general, we want to 14768 * match modules as soon as they load. However, we cannot guarantee 14769 * this, because it would lead us to the lock ordering violation 14770 * outlined above. The common case, of course, is that cpu_lock is 14771 * _not_ held -- so we delay here for a clock tick, hoping that that's 14772 * long enough for the task queue to do its work. If it's not, it's 14773 * not a serious problem -- it just means that the module that we 14774 * just loaded may not be immediately instrumentable. 14775 */ 14776 delay(1); 14777} 14778 14779static void 14780dtrace_module_unloaded(modctl_t *ctl) 14781{ 14782 dtrace_probe_t template, *probe, *first, *next; 14783 dtrace_provider_t *prov; 14784 14785 template.dtpr_mod = ctl->mod_modname; 14786 14787 mutex_enter(&dtrace_provider_lock); 14788 mutex_enter(&mod_lock); 14789 mutex_enter(&dtrace_lock); 14790 14791 if (dtrace_bymod == NULL) { 14792 /* 14793 * The DTrace module is loaded (obviously) but not attached; 14794 * we don't have any work to do. 14795 */ 14796 mutex_exit(&dtrace_provider_lock); 14797 mutex_exit(&mod_lock); 14798 mutex_exit(&dtrace_lock); 14799 return; 14800 } 14801 14802 for (probe = first = dtrace_hash_lookup(dtrace_bymod, &template); 14803 probe != NULL; probe = probe->dtpr_nextmod) { 14804 if (probe->dtpr_ecb != NULL) { 14805 mutex_exit(&dtrace_provider_lock); 14806 mutex_exit(&mod_lock); 14807 mutex_exit(&dtrace_lock); 14808 14809 /* 14810 * This shouldn't _actually_ be possible -- we're 14811 * unloading a module that has an enabled probe in it. 14812 * (It's normally up to the provider to make sure that 14813 * this can't happen.) However, because dtps_enable() 14814 * doesn't have a failure mode, there can be an 14815 * enable/unload race. Upshot: we don't want to 14816 * assert, but we're not going to disable the 14817 * probe, either. 14818 */ 14819 if (dtrace_err_verbose) { 14820 cmn_err(CE_WARN, "unloaded module '%s' had " 14821 "enabled probes", ctl->mod_modname); 14822 } 14823 14824 return; 14825 } 14826 } 14827 14828 probe = first; 14829 14830 for (first = NULL; probe != NULL; probe = next) { 14831 ASSERT(dtrace_probes[probe->dtpr_id - 1] == probe); 14832 14833 dtrace_probes[probe->dtpr_id - 1] = NULL; 14834 14835 next = probe->dtpr_nextmod; 14836 dtrace_hash_remove(dtrace_bymod, probe); 14837 dtrace_hash_remove(dtrace_byfunc, probe); 14838 dtrace_hash_remove(dtrace_byname, probe); 14839 14840 if (first == NULL) { 14841 first = probe; 14842 probe->dtpr_nextmod = NULL; 14843 } else { 14844 probe->dtpr_nextmod = first; 14845 first = probe; 14846 } 14847 } 14848 14849 /* 14850 * We've removed all of the module's probes from the hash chains and 14851 * from the probe array. Now issue a dtrace_sync() to be sure that 14852 * everyone has cleared out from any probe array processing. 14853 */ 14854 dtrace_sync(); 14855 14856 for (probe = first; probe != NULL; probe = first) { 14857 first = probe->dtpr_nextmod; 14858 prov = probe->dtpr_provider; 14859 prov->dtpv_pops.dtps_destroy(prov->dtpv_arg, probe->dtpr_id, 14860 probe->dtpr_arg); 14861 kmem_free(probe->dtpr_mod, strlen(probe->dtpr_mod) + 1); 14862 kmem_free(probe->dtpr_func, strlen(probe->dtpr_func) + 1); 14863 kmem_free(probe->dtpr_name, strlen(probe->dtpr_name) + 1); 14864 vmem_free(dtrace_arena, (void *)(uintptr_t)probe->dtpr_id, 1); 14865 kmem_free(probe, sizeof (dtrace_probe_t)); 14866 } 14867 14868 mutex_exit(&dtrace_lock); 14869 mutex_exit(&mod_lock); 14870 mutex_exit(&dtrace_provider_lock); 14871} 14872 14873static void 14874dtrace_suspend(void) 14875{ 14876 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_suspend)); 14877} 14878 14879static void 14880dtrace_resume(void) 14881{ 14882 dtrace_probe_foreach(offsetof(dtrace_pops_t, dtps_resume)); 14883} 14884#endif 14885 14886static int 14887dtrace_cpu_setup(cpu_setup_t what, processorid_t cpu) 14888{ 14889 ASSERT(MUTEX_HELD(&cpu_lock)); 14890 mutex_enter(&dtrace_lock); 14891 14892 switch (what) { 14893 case CPU_CONFIG: { 14894 dtrace_state_t *state; 14895 dtrace_optval_t *opt, rs, c; 14896 14897 /* 14898 * For now, we only allocate a new buffer for anonymous state. 14899 */ 14900 if ((state = dtrace_anon.dta_state) == NULL) 14901 break; 14902 14903 if (state->dts_activity != DTRACE_ACTIVITY_ACTIVE) 14904 break; 14905 14906 opt = state->dts_options; 14907 c = opt[DTRACEOPT_CPU]; 14908 14909 if (c != DTRACE_CPUALL && c != DTRACEOPT_UNSET && c != cpu) 14910 break; 14911 14912 /* 14913 * Regardless of what the actual policy is, we're going to 14914 * temporarily set our resize policy to be manual. We're 14915 * also going to temporarily set our CPU option to denote 14916 * the newly configured CPU. 14917 */ 14918 rs = opt[DTRACEOPT_BUFRESIZE]; 14919 opt[DTRACEOPT_BUFRESIZE] = DTRACEOPT_BUFRESIZE_MANUAL; 14920 opt[DTRACEOPT_CPU] = (dtrace_optval_t)cpu; 14921 14922 (void) dtrace_state_buffers(state); 14923 14924 opt[DTRACEOPT_BUFRESIZE] = rs; 14925 opt[DTRACEOPT_CPU] = c; 14926 14927 break; 14928 } 14929 14930 case CPU_UNCONFIG: 14931 /* 14932 * We don't free the buffer in the CPU_UNCONFIG case. (The 14933 * buffer will be freed when the consumer exits.) 14934 */ 14935 break; 14936 14937 default: 14938 break; 14939 } 14940 14941 mutex_exit(&dtrace_lock); 14942 return (0); 14943} 14944 14945#if defined(sun) 14946static void 14947dtrace_cpu_setup_initial(processorid_t cpu) 14948{ 14949 (void) dtrace_cpu_setup(CPU_CONFIG, cpu); 14950} 14951#endif 14952 14953static void 14954dtrace_toxrange_add(uintptr_t base, uintptr_t limit) 14955{ 14956 if (dtrace_toxranges >= dtrace_toxranges_max) { 14957 int osize, nsize; 14958 dtrace_toxrange_t *range; 14959 14960 osize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14961 14962 if (osize == 0) { 14963 ASSERT(dtrace_toxrange == NULL); 14964 ASSERT(dtrace_toxranges_max == 0); 14965 dtrace_toxranges_max = 1; 14966 } else { 14967 dtrace_toxranges_max <<= 1; 14968 } 14969 14970 nsize = dtrace_toxranges_max * sizeof (dtrace_toxrange_t); 14971 range = kmem_zalloc(nsize, KM_SLEEP); 14972 14973 if (dtrace_toxrange != NULL) { 14974 ASSERT(osize != 0); 14975 bcopy(dtrace_toxrange, range, osize); 14976 kmem_free(dtrace_toxrange, osize); 14977 } 14978 14979 dtrace_toxrange = range; 14980 } 14981 14982 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_base == 0); 14983 ASSERT(dtrace_toxrange[dtrace_toxranges].dtt_limit == 0); 14984 14985 dtrace_toxrange[dtrace_toxranges].dtt_base = base; 14986 dtrace_toxrange[dtrace_toxranges].dtt_limit = limit; 14987 dtrace_toxranges++; 14988} 14989 14990/* 14991 * DTrace Driver Cookbook Functions 14992 */ 14993#if defined(sun) 14994/*ARGSUSED*/ 14995static int 14996dtrace_attach(dev_info_t *devi, ddi_attach_cmd_t cmd) 14997{ 14998 dtrace_provider_id_t id; 14999 dtrace_state_t *state = NULL; 15000 dtrace_enabling_t *enab; 15001 15002 mutex_enter(&cpu_lock); 15003 mutex_enter(&dtrace_provider_lock); 15004 mutex_enter(&dtrace_lock); 15005 15006 if (ddi_soft_state_init(&dtrace_softstate, 15007 sizeof (dtrace_state_t), 0) != 0) { 15008 cmn_err(CE_NOTE, "/dev/dtrace failed to initialize soft state"); 15009 mutex_exit(&cpu_lock); 15010 mutex_exit(&dtrace_provider_lock); 15011 mutex_exit(&dtrace_lock); 15012 return (DDI_FAILURE); 15013 } 15014 15015 if (ddi_create_minor_node(devi, DTRACEMNR_DTRACE, S_IFCHR, 15016 DTRACEMNRN_DTRACE, DDI_PSEUDO, NULL) == DDI_FAILURE || 15017 ddi_create_minor_node(devi, DTRACEMNR_HELPER, S_IFCHR, 15018 DTRACEMNRN_HELPER, DDI_PSEUDO, NULL) == DDI_FAILURE) { 15019 cmn_err(CE_NOTE, "/dev/dtrace couldn't create minor nodes"); 15020 ddi_remove_minor_node(devi, NULL); 15021 ddi_soft_state_fini(&dtrace_softstate); 15022 mutex_exit(&cpu_lock); 15023 mutex_exit(&dtrace_provider_lock); 15024 mutex_exit(&dtrace_lock); 15025 return (DDI_FAILURE); 15026 } 15027 15028 ddi_report_dev(devi); 15029 dtrace_devi = devi; 15030 15031 dtrace_modload = dtrace_module_loaded; 15032 dtrace_modunload = dtrace_module_unloaded; 15033 dtrace_cpu_init = dtrace_cpu_setup_initial; 15034 dtrace_helpers_cleanup = dtrace_helpers_destroy; 15035 dtrace_helpers_fork = dtrace_helpers_duplicate; 15036 dtrace_cpustart_init = dtrace_suspend; 15037 dtrace_cpustart_fini = dtrace_resume; 15038 dtrace_debugger_init = dtrace_suspend; 15039 dtrace_debugger_fini = dtrace_resume; 15040 15041 register_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 15042 15043 ASSERT(MUTEX_HELD(&cpu_lock)); 15044 15045 dtrace_arena = vmem_create("dtrace", (void *)1, UINT32_MAX, 1, 15046 NULL, NULL, NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 15047 dtrace_minor = vmem_create("dtrace_minor", (void *)DTRACEMNRN_CLONE, 15048 UINT32_MAX - DTRACEMNRN_CLONE, 1, NULL, NULL, NULL, 0, 15049 VM_SLEEP | VMC_IDENTIFIER); 15050 dtrace_taskq = taskq_create("dtrace_taskq", 1, maxclsyspri, 15051 1, INT_MAX, 0); 15052 15053 dtrace_state_cache = kmem_cache_create("dtrace_state_cache", 15054 sizeof (dtrace_dstate_percpu_t) * NCPU, DTRACE_STATE_ALIGN, 15055 NULL, NULL, NULL, NULL, NULL, 0); 15056 15057 ASSERT(MUTEX_HELD(&cpu_lock)); 15058 dtrace_bymod = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_mod), 15059 offsetof(dtrace_probe_t, dtpr_nextmod), 15060 offsetof(dtrace_probe_t, dtpr_prevmod)); 15061 15062 dtrace_byfunc = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_func), 15063 offsetof(dtrace_probe_t, dtpr_nextfunc), 15064 offsetof(dtrace_probe_t, dtpr_prevfunc)); 15065 15066 dtrace_byname = dtrace_hash_create(offsetof(dtrace_probe_t, dtpr_name), 15067 offsetof(dtrace_probe_t, dtpr_nextname), 15068 offsetof(dtrace_probe_t, dtpr_prevname)); 15069 15070 if (dtrace_retain_max < 1) { 15071 cmn_err(CE_WARN, "illegal value (%lu) for dtrace_retain_max; " 15072 "setting to 1", dtrace_retain_max); 15073 dtrace_retain_max = 1; 15074 } 15075 15076 /* 15077 * Now discover our toxic ranges. 15078 */ 15079 dtrace_toxic_ranges(dtrace_toxrange_add); 15080 15081 /* 15082 * Before we register ourselves as a provider to our own framework, 15083 * we would like to assert that dtrace_provider is NULL -- but that's 15084 * not true if we were loaded as a dependency of a DTrace provider. 15085 * Once we've registered, we can assert that dtrace_provider is our 15086 * pseudo provider. 15087 */ 15088 (void) dtrace_register("dtrace", &dtrace_provider_attr, 15089 DTRACE_PRIV_NONE, 0, &dtrace_provider_ops, NULL, &id); 15090 15091 ASSERT(dtrace_provider != NULL); 15092 ASSERT((dtrace_provider_id_t)dtrace_provider == id); 15093 15094 dtrace_probeid_begin = dtrace_probe_create((dtrace_provider_id_t) 15095 dtrace_provider, NULL, NULL, "BEGIN", 0, NULL); 15096 dtrace_probeid_end = dtrace_probe_create((dtrace_provider_id_t) 15097 dtrace_provider, NULL, NULL, "END", 0, NULL); 15098 dtrace_probeid_error = dtrace_probe_create((dtrace_provider_id_t) 15099 dtrace_provider, NULL, NULL, "ERROR", 1, NULL); 15100 15101 dtrace_anon_property(); 15102 mutex_exit(&cpu_lock); 15103 15104 /* 15105 * If DTrace helper tracing is enabled, we need to allocate the 15106 * trace buffer and initialize the values. 15107 */ 15108 if (dtrace_helptrace_enabled) { 15109 ASSERT(dtrace_helptrace_buffer == NULL); 15110 dtrace_helptrace_buffer = 15111 kmem_zalloc(dtrace_helptrace_bufsize, KM_SLEEP); 15112 dtrace_helptrace_next = 0; 15113 } 15114 15115 /* 15116 * If there are already providers, we must ask them to provide their 15117 * probes, and then match any anonymous enabling against them. Note 15118 * that there should be no other retained enablings at this time: 15119 * the only retained enablings at this time should be the anonymous 15120 * enabling. 15121 */ 15122 if (dtrace_anon.dta_enabling != NULL) { 15123 ASSERT(dtrace_retained == dtrace_anon.dta_enabling); 15124 15125 dtrace_enabling_provide(NULL); 15126 state = dtrace_anon.dta_state; 15127 15128 /* 15129 * We couldn't hold cpu_lock across the above call to 15130 * dtrace_enabling_provide(), but we must hold it to actually 15131 * enable the probes. We have to drop all of our locks, pick 15132 * up cpu_lock, and regain our locks before matching the 15133 * retained anonymous enabling. 15134 */ 15135 mutex_exit(&dtrace_lock); 15136 mutex_exit(&dtrace_provider_lock); 15137 15138 mutex_enter(&cpu_lock); 15139 mutex_enter(&dtrace_provider_lock); 15140 mutex_enter(&dtrace_lock); 15141 15142 if ((enab = dtrace_anon.dta_enabling) != NULL) 15143 (void) dtrace_enabling_match(enab, NULL); 15144 15145 mutex_exit(&cpu_lock); 15146 } 15147 15148 mutex_exit(&dtrace_lock); 15149 mutex_exit(&dtrace_provider_lock); 15150 15151 if (state != NULL) { 15152 /* 15153 * If we created any anonymous state, set it going now. 15154 */ 15155 (void) dtrace_state_go(state, &dtrace_anon.dta_beganon); 15156 } 15157 15158 return (DDI_SUCCESS); 15159} 15160#endif 15161 15162/*ARGSUSED*/ 15163static int 15164#if defined(sun) 15165dtrace_open(dev_t *devp, int flag, int otyp, cred_t *cred_p) 15166#else 15167dtrace_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 15168#endif 15169{ 15170 dtrace_state_t *state; 15171 uint32_t priv; 15172 uid_t uid; 15173 zoneid_t zoneid; 15174 15175#if defined(sun) 15176 if (getminor(*devp) == DTRACEMNRN_HELPER) 15177 return (0); 15178 15179 /* 15180 * If this wasn't an open with the "helper" minor, then it must be 15181 * the "dtrace" minor. 15182 */ 15183 ASSERT(getminor(*devp) == DTRACEMNRN_DTRACE); 15184#else 15185 cred_t *cred_p = NULL; 15186 15187 /* 15188 * The first minor device is the one that is cloned so there is 15189 * nothing more to do here. 15190 */ 15191 if (minor(dev) == 0) 15192 return 0; 15193 15194 /* 15195 * Devices are cloned, so if the DTrace state has already 15196 * been allocated, that means this device belongs to a 15197 * different client. Each client should open '/dev/dtrace' 15198 * to get a cloned device. 15199 */ 15200 if (dev->si_drv1 != NULL) 15201 return (EBUSY); 15202 15203 cred_p = dev->si_cred; 15204#endif 15205 15206 /* 15207 * If no DTRACE_PRIV_* bits are set in the credential, then the 15208 * caller lacks sufficient permission to do anything with DTrace. 15209 */ 15210 dtrace_cred2priv(cred_p, &priv, &uid, &zoneid); 15211 if (priv == DTRACE_PRIV_NONE) { 15212#if !defined(sun) 15213 /* Destroy the cloned device. */ 15214 destroy_dev(dev); 15215#endif 15216 15217 return (EACCES); 15218 } 15219 15220 /* 15221 * Ask all providers to provide all their probes. 15222 */ 15223 mutex_enter(&dtrace_provider_lock); 15224 dtrace_probe_provide(NULL, NULL); 15225 mutex_exit(&dtrace_provider_lock); 15226 15227 mutex_enter(&cpu_lock); 15228 mutex_enter(&dtrace_lock); 15229 dtrace_opens++; 15230 dtrace_membar_producer(); 15231 15232#if defined(sun) 15233 /* 15234 * If the kernel debugger is active (that is, if the kernel debugger 15235 * modified text in some way), we won't allow the open. 15236 */ 15237 if (kdi_dtrace_set(KDI_DTSET_DTRACE_ACTIVATE) != 0) { 15238 dtrace_opens--; 15239 mutex_exit(&cpu_lock); 15240 mutex_exit(&dtrace_lock); 15241 return (EBUSY); 15242 } 15243 15244 state = dtrace_state_create(devp, cred_p); 15245#else 15246 state = dtrace_state_create(dev); 15247 dev->si_drv1 = state; 15248#endif 15249 15250 mutex_exit(&cpu_lock); 15251 15252 if (state == NULL) { 15253#if defined(sun) 15254 if (--dtrace_opens == 0) 15255 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15256#else 15257 --dtrace_opens; 15258#endif 15259 mutex_exit(&dtrace_lock); 15260#if !defined(sun) 15261 /* Destroy the cloned device. */ 15262 destroy_dev(dev); 15263#endif 15264 return (EAGAIN); 15265 } 15266 15267 mutex_exit(&dtrace_lock); 15268 15269 return (0); 15270} 15271 15272/*ARGSUSED*/ 15273static int 15274#if defined(sun) 15275dtrace_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 15276#else 15277dtrace_close(struct cdev *dev, int flags, int fmt __unused, struct thread *td) 15278#endif 15279{ 15280#if defined(sun) 15281 minor_t minor = getminor(dev); 15282 dtrace_state_t *state; 15283 15284 if (minor == DTRACEMNRN_HELPER) 15285 return (0); 15286 15287 state = ddi_get_soft_state(dtrace_softstate, minor); 15288#else 15289 dtrace_state_t *state = dev->si_drv1; 15290 15291 /* Check if this is not a cloned device. */ 15292 if (minor(dev) == 0) 15293 return (0); 15294 15295#endif 15296 15297 mutex_enter(&cpu_lock); 15298 mutex_enter(&dtrace_lock); 15299 15300 if (state != NULL) { 15301 if (state->dts_anon) { 15302 /* 15303 * There is anonymous state. Destroy that first. 15304 */ 15305 ASSERT(dtrace_anon.dta_state == NULL); 15306 dtrace_state_destroy(state->dts_anon); 15307 } 15308 15309 dtrace_state_destroy(state); 15310 15311#if !defined(sun) 15312 kmem_free(state, 0); 15313 dev->si_drv1 = NULL; 15314#endif 15315 } 15316 15317 ASSERT(dtrace_opens > 0); 15318#if defined(sun) 15319 if (--dtrace_opens == 0) 15320 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 15321#else 15322 --dtrace_opens; 15323#endif 15324 15325 mutex_exit(&dtrace_lock); 15326 mutex_exit(&cpu_lock); 15327 15328 /* Schedule this cloned device to be destroyed. */ 15329 destroy_dev_sched(dev); 15330 15331 return (0); 15332} 15333 15334#if defined(sun) 15335/*ARGSUSED*/ 15336static int 15337dtrace_ioctl_helper(int cmd, intptr_t arg, int *rv) 15338{ 15339 int rval; 15340 dof_helper_t help, *dhp = NULL; 15341 15342 switch (cmd) { 15343 case DTRACEHIOC_ADDDOF: 15344 if (copyin((void *)arg, &help, sizeof (help)) != 0) { 15345 dtrace_dof_error(NULL, "failed to copyin DOF helper"); 15346 return (EFAULT); 15347 } 15348 15349 dhp = &help; 15350 arg = (intptr_t)help.dofhp_dof; 15351 /*FALLTHROUGH*/ 15352 15353 case DTRACEHIOC_ADD: { 15354 dof_hdr_t *dof = dtrace_dof_copyin(arg, &rval); 15355 15356 if (dof == NULL) 15357 return (rval); 15358 15359 mutex_enter(&dtrace_lock); 15360 15361 /* 15362 * dtrace_helper_slurp() takes responsibility for the dof -- 15363 * it may free it now or it may save it and free it later. 15364 */ 15365 if ((rval = dtrace_helper_slurp(dof, dhp)) != -1) { 15366 *rv = rval; 15367 rval = 0; 15368 } else { 15369 rval = EINVAL; 15370 } 15371 15372 mutex_exit(&dtrace_lock); 15373 return (rval); 15374 } 15375 15376 case DTRACEHIOC_REMOVE: { 15377 mutex_enter(&dtrace_lock); 15378 rval = dtrace_helper_destroygen(arg); 15379 mutex_exit(&dtrace_lock); 15380 15381 return (rval); 15382 } 15383 15384 default: 15385 break; 15386 } 15387 15388 return (ENOTTY); 15389} 15390 15391/*ARGSUSED*/ 15392static int 15393dtrace_ioctl(dev_t dev, int cmd, intptr_t arg, int md, cred_t *cr, int *rv) 15394{ 15395 minor_t minor = getminor(dev); 15396 dtrace_state_t *state; 15397 int rval; 15398 15399 if (minor == DTRACEMNRN_HELPER) 15400 return (dtrace_ioctl_helper(cmd, arg, rv)); 15401 15402 state = ddi_get_soft_state(dtrace_softstate, minor); 15403 15404 if (state->dts_anon) { 15405 ASSERT(dtrace_anon.dta_state == NULL); 15406 state = state->dts_anon; 15407 } 15408 15409 switch (cmd) { 15410 case DTRACEIOC_PROVIDER: { 15411 dtrace_providerdesc_t pvd; 15412 dtrace_provider_t *pvp; 15413 15414 if (copyin((void *)arg, &pvd, sizeof (pvd)) != 0) 15415 return (EFAULT); 15416 15417 pvd.dtvd_name[DTRACE_PROVNAMELEN - 1] = '\0'; 15418 mutex_enter(&dtrace_provider_lock); 15419 15420 for (pvp = dtrace_provider; pvp != NULL; pvp = pvp->dtpv_next) { 15421 if (strcmp(pvp->dtpv_name, pvd.dtvd_name) == 0) 15422 break; 15423 } 15424 15425 mutex_exit(&dtrace_provider_lock); 15426 15427 if (pvp == NULL) 15428 return (ESRCH); 15429 15430 bcopy(&pvp->dtpv_priv, &pvd.dtvd_priv, sizeof (dtrace_ppriv_t)); 15431 bcopy(&pvp->dtpv_attr, &pvd.dtvd_attr, sizeof (dtrace_pattr_t)); 15432 15433 if (copyout(&pvd, (void *)arg, sizeof (pvd)) != 0) 15434 return (EFAULT); 15435 15436 return (0); 15437 } 15438 15439 case DTRACEIOC_EPROBE: { 15440 dtrace_eprobedesc_t epdesc; 15441 dtrace_ecb_t *ecb; 15442 dtrace_action_t *act; 15443 void *buf; 15444 size_t size; 15445 uintptr_t dest; 15446 int nrecs; 15447 15448 if (copyin((void *)arg, &epdesc, sizeof (epdesc)) != 0) 15449 return (EFAULT); 15450 15451 mutex_enter(&dtrace_lock); 15452 15453 if ((ecb = dtrace_epid2ecb(state, epdesc.dtepd_epid)) == NULL) { 15454 mutex_exit(&dtrace_lock); 15455 return (EINVAL); 15456 } 15457 15458 if (ecb->dte_probe == NULL) { 15459 mutex_exit(&dtrace_lock); 15460 return (EINVAL); 15461 } 15462 15463 epdesc.dtepd_probeid = ecb->dte_probe->dtpr_id; 15464 epdesc.dtepd_uarg = ecb->dte_uarg; 15465 epdesc.dtepd_size = ecb->dte_size; 15466 15467 nrecs = epdesc.dtepd_nrecs; 15468 epdesc.dtepd_nrecs = 0; 15469 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15470 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15471 continue; 15472 15473 epdesc.dtepd_nrecs++; 15474 } 15475 15476 /* 15477 * Now that we have the size, we need to allocate a temporary 15478 * buffer in which to store the complete description. We need 15479 * the temporary buffer to be able to drop dtrace_lock() 15480 * across the copyout(), below. 15481 */ 15482 size = sizeof (dtrace_eprobedesc_t) + 15483 (epdesc.dtepd_nrecs * sizeof (dtrace_recdesc_t)); 15484 15485 buf = kmem_alloc(size, KM_SLEEP); 15486 dest = (uintptr_t)buf; 15487 15488 bcopy(&epdesc, (void *)dest, sizeof (epdesc)); 15489 dest += offsetof(dtrace_eprobedesc_t, dtepd_rec[0]); 15490 15491 for (act = ecb->dte_action; act != NULL; act = act->dta_next) { 15492 if (DTRACEACT_ISAGG(act->dta_kind) || act->dta_intuple) 15493 continue; 15494 15495 if (nrecs-- == 0) 15496 break; 15497 15498 bcopy(&act->dta_rec, (void *)dest, 15499 sizeof (dtrace_recdesc_t)); 15500 dest += sizeof (dtrace_recdesc_t); 15501 } 15502 15503 mutex_exit(&dtrace_lock); 15504 15505 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15506 kmem_free(buf, size); 15507 return (EFAULT); 15508 } 15509 15510 kmem_free(buf, size); 15511 return (0); 15512 } 15513 15514 case DTRACEIOC_AGGDESC: { 15515 dtrace_aggdesc_t aggdesc; 15516 dtrace_action_t *act; 15517 dtrace_aggregation_t *agg; 15518 int nrecs; 15519 uint32_t offs; 15520 dtrace_recdesc_t *lrec; 15521 void *buf; 15522 size_t size; 15523 uintptr_t dest; 15524 15525 if (copyin((void *)arg, &aggdesc, sizeof (aggdesc)) != 0) 15526 return (EFAULT); 15527 15528 mutex_enter(&dtrace_lock); 15529 15530 if ((agg = dtrace_aggid2agg(state, aggdesc.dtagd_id)) == NULL) { 15531 mutex_exit(&dtrace_lock); 15532 return (EINVAL); 15533 } 15534 15535 aggdesc.dtagd_epid = agg->dtag_ecb->dte_epid; 15536 15537 nrecs = aggdesc.dtagd_nrecs; 15538 aggdesc.dtagd_nrecs = 0; 15539 15540 offs = agg->dtag_base; 15541 lrec = &agg->dtag_action.dta_rec; 15542 aggdesc.dtagd_size = lrec->dtrd_offset + lrec->dtrd_size - offs; 15543 15544 for (act = agg->dtag_first; ; act = act->dta_next) { 15545 ASSERT(act->dta_intuple || 15546 DTRACEACT_ISAGG(act->dta_kind)); 15547 15548 /* 15549 * If this action has a record size of zero, it 15550 * denotes an argument to the aggregating action. 15551 * Because the presence of this record doesn't (or 15552 * shouldn't) affect the way the data is interpreted, 15553 * we don't copy it out to save user-level the 15554 * confusion of dealing with a zero-length record. 15555 */ 15556 if (act->dta_rec.dtrd_size == 0) { 15557 ASSERT(agg->dtag_hasarg); 15558 continue; 15559 } 15560 15561 aggdesc.dtagd_nrecs++; 15562 15563 if (act == &agg->dtag_action) 15564 break; 15565 } 15566 15567 /* 15568 * Now that we have the size, we need to allocate a temporary 15569 * buffer in which to store the complete description. We need 15570 * the temporary buffer to be able to drop dtrace_lock() 15571 * across the copyout(), below. 15572 */ 15573 size = sizeof (dtrace_aggdesc_t) + 15574 (aggdesc.dtagd_nrecs * sizeof (dtrace_recdesc_t)); 15575 15576 buf = kmem_alloc(size, KM_SLEEP); 15577 dest = (uintptr_t)buf; 15578 15579 bcopy(&aggdesc, (void *)dest, sizeof (aggdesc)); 15580 dest += offsetof(dtrace_aggdesc_t, dtagd_rec[0]); 15581 15582 for (act = agg->dtag_first; ; act = act->dta_next) { 15583 dtrace_recdesc_t rec = act->dta_rec; 15584 15585 /* 15586 * See the comment in the above loop for why we pass 15587 * over zero-length records. 15588 */ 15589 if (rec.dtrd_size == 0) { 15590 ASSERT(agg->dtag_hasarg); 15591 continue; 15592 } 15593 15594 if (nrecs-- == 0) 15595 break; 15596 15597 rec.dtrd_offset -= offs; 15598 bcopy(&rec, (void *)dest, sizeof (rec)); 15599 dest += sizeof (dtrace_recdesc_t); 15600 15601 if (act == &agg->dtag_action) 15602 break; 15603 } 15604 15605 mutex_exit(&dtrace_lock); 15606 15607 if (copyout(buf, (void *)arg, dest - (uintptr_t)buf) != 0) { 15608 kmem_free(buf, size); 15609 return (EFAULT); 15610 } 15611 15612 kmem_free(buf, size); 15613 return (0); 15614 } 15615 15616 case DTRACEIOC_ENABLE: { 15617 dof_hdr_t *dof; 15618 dtrace_enabling_t *enab = NULL; 15619 dtrace_vstate_t *vstate; 15620 int err = 0; 15621 15622 *rv = 0; 15623 15624 /* 15625 * If a NULL argument has been passed, we take this as our 15626 * cue to reevaluate our enablings. 15627 */ 15628 if (arg == NULL) { 15629 dtrace_enabling_matchall(); 15630 15631 return (0); 15632 } 15633 15634 if ((dof = dtrace_dof_copyin(arg, &rval)) == NULL) 15635 return (rval); 15636 15637 mutex_enter(&cpu_lock); 15638 mutex_enter(&dtrace_lock); 15639 vstate = &state->dts_vstate; 15640 15641 if (state->dts_activity != DTRACE_ACTIVITY_INACTIVE) { 15642 mutex_exit(&dtrace_lock); 15643 mutex_exit(&cpu_lock); 15644 dtrace_dof_destroy(dof); 15645 return (EBUSY); 15646 } 15647 15648 if (dtrace_dof_slurp(dof, vstate, cr, &enab, 0, B_TRUE) != 0) { 15649 mutex_exit(&dtrace_lock); 15650 mutex_exit(&cpu_lock); 15651 dtrace_dof_destroy(dof); 15652 return (EINVAL); 15653 } 15654 15655 if ((rval = dtrace_dof_options(dof, state)) != 0) { 15656 dtrace_enabling_destroy(enab); 15657 mutex_exit(&dtrace_lock); 15658 mutex_exit(&cpu_lock); 15659 dtrace_dof_destroy(dof); 15660 return (rval); 15661 } 15662 15663 if ((err = dtrace_enabling_match(enab, rv)) == 0) { 15664 err = dtrace_enabling_retain(enab); 15665 } else { 15666 dtrace_enabling_destroy(enab); 15667 } 15668 15669 mutex_exit(&cpu_lock); 15670 mutex_exit(&dtrace_lock); 15671 dtrace_dof_destroy(dof); 15672 15673 return (err); 15674 } 15675 15676 case DTRACEIOC_REPLICATE: { 15677 dtrace_repldesc_t desc; 15678 dtrace_probedesc_t *match = &desc.dtrpd_match; 15679 dtrace_probedesc_t *create = &desc.dtrpd_create; 15680 int err; 15681 15682 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15683 return (EFAULT); 15684 15685 match->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15686 match->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15687 match->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15688 match->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15689 15690 create->dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15691 create->dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15692 create->dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15693 create->dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15694 15695 mutex_enter(&dtrace_lock); 15696 err = dtrace_enabling_replicate(state, match, create); 15697 mutex_exit(&dtrace_lock); 15698 15699 return (err); 15700 } 15701 15702 case DTRACEIOC_PROBEMATCH: 15703 case DTRACEIOC_PROBES: { 15704 dtrace_probe_t *probe = NULL; 15705 dtrace_probedesc_t desc; 15706 dtrace_probekey_t pkey; 15707 dtrace_id_t i; 15708 int m = 0; 15709 uint32_t priv; 15710 uid_t uid; 15711 zoneid_t zoneid; 15712 15713 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15714 return (EFAULT); 15715 15716 desc.dtpd_provider[DTRACE_PROVNAMELEN - 1] = '\0'; 15717 desc.dtpd_mod[DTRACE_MODNAMELEN - 1] = '\0'; 15718 desc.dtpd_func[DTRACE_FUNCNAMELEN - 1] = '\0'; 15719 desc.dtpd_name[DTRACE_NAMELEN - 1] = '\0'; 15720 15721 /* 15722 * Before we attempt to match this probe, we want to give 15723 * all providers the opportunity to provide it. 15724 */ 15725 if (desc.dtpd_id == DTRACE_IDNONE) { 15726 mutex_enter(&dtrace_provider_lock); 15727 dtrace_probe_provide(&desc, NULL); 15728 mutex_exit(&dtrace_provider_lock); 15729 desc.dtpd_id++; 15730 } 15731 15732 if (cmd == DTRACEIOC_PROBEMATCH) { 15733 dtrace_probekey(&desc, &pkey); 15734 pkey.dtpk_id = DTRACE_IDNONE; 15735 } 15736 15737 dtrace_cred2priv(cr, &priv, &uid, &zoneid); 15738 15739 mutex_enter(&dtrace_lock); 15740 15741 if (cmd == DTRACEIOC_PROBEMATCH) { 15742 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15743 if ((probe = dtrace_probes[i - 1]) != NULL && 15744 (m = dtrace_match_probe(probe, &pkey, 15745 priv, uid, zoneid)) != 0) 15746 break; 15747 } 15748 15749 if (m < 0) { 15750 mutex_exit(&dtrace_lock); 15751 return (EINVAL); 15752 } 15753 15754 } else { 15755 for (i = desc.dtpd_id; i <= dtrace_nprobes; i++) { 15756 if ((probe = dtrace_probes[i - 1]) != NULL && 15757 dtrace_match_priv(probe, priv, uid, zoneid)) 15758 break; 15759 } 15760 } 15761 15762 if (probe == NULL) { 15763 mutex_exit(&dtrace_lock); 15764 return (ESRCH); 15765 } 15766 15767 dtrace_probe_description(probe, &desc); 15768 mutex_exit(&dtrace_lock); 15769 15770 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15771 return (EFAULT); 15772 15773 return (0); 15774 } 15775 15776 case DTRACEIOC_PROBEARG: { 15777 dtrace_argdesc_t desc; 15778 dtrace_probe_t *probe; 15779 dtrace_provider_t *prov; 15780 15781 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15782 return (EFAULT); 15783 15784 if (desc.dtargd_id == DTRACE_IDNONE) 15785 return (EINVAL); 15786 15787 if (desc.dtargd_ndx == DTRACE_ARGNONE) 15788 return (EINVAL); 15789 15790 mutex_enter(&dtrace_provider_lock); 15791 mutex_enter(&mod_lock); 15792 mutex_enter(&dtrace_lock); 15793 15794 if (desc.dtargd_id > dtrace_nprobes) { 15795 mutex_exit(&dtrace_lock); 15796 mutex_exit(&mod_lock); 15797 mutex_exit(&dtrace_provider_lock); 15798 return (EINVAL); 15799 } 15800 15801 if ((probe = dtrace_probes[desc.dtargd_id - 1]) == NULL) { 15802 mutex_exit(&dtrace_lock); 15803 mutex_exit(&mod_lock); 15804 mutex_exit(&dtrace_provider_lock); 15805 return (EINVAL); 15806 } 15807 15808 mutex_exit(&dtrace_lock); 15809 15810 prov = probe->dtpr_provider; 15811 15812 if (prov->dtpv_pops.dtps_getargdesc == NULL) { 15813 /* 15814 * There isn't any typed information for this probe. 15815 * Set the argument number to DTRACE_ARGNONE. 15816 */ 15817 desc.dtargd_ndx = DTRACE_ARGNONE; 15818 } else { 15819 desc.dtargd_native[0] = '\0'; 15820 desc.dtargd_xlate[0] = '\0'; 15821 desc.dtargd_mapping = desc.dtargd_ndx; 15822 15823 prov->dtpv_pops.dtps_getargdesc(prov->dtpv_arg, 15824 probe->dtpr_id, probe->dtpr_arg, &desc); 15825 } 15826 15827 mutex_exit(&mod_lock); 15828 mutex_exit(&dtrace_provider_lock); 15829 15830 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15831 return (EFAULT); 15832 15833 return (0); 15834 } 15835 15836 case DTRACEIOC_GO: { 15837 processorid_t cpuid; 15838 rval = dtrace_state_go(state, &cpuid); 15839 15840 if (rval != 0) 15841 return (rval); 15842 15843 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15844 return (EFAULT); 15845 15846 return (0); 15847 } 15848 15849 case DTRACEIOC_STOP: { 15850 processorid_t cpuid; 15851 15852 mutex_enter(&dtrace_lock); 15853 rval = dtrace_state_stop(state, &cpuid); 15854 mutex_exit(&dtrace_lock); 15855 15856 if (rval != 0) 15857 return (rval); 15858 15859 if (copyout(&cpuid, (void *)arg, sizeof (cpuid)) != 0) 15860 return (EFAULT); 15861 15862 return (0); 15863 } 15864 15865 case DTRACEIOC_DOFGET: { 15866 dof_hdr_t hdr, *dof; 15867 uint64_t len; 15868 15869 if (copyin((void *)arg, &hdr, sizeof (hdr)) != 0) 15870 return (EFAULT); 15871 15872 mutex_enter(&dtrace_lock); 15873 dof = dtrace_dof_create(state); 15874 mutex_exit(&dtrace_lock); 15875 15876 len = MIN(hdr.dofh_loadsz, dof->dofh_loadsz); 15877 rval = copyout(dof, (void *)arg, len); 15878 dtrace_dof_destroy(dof); 15879 15880 return (rval == 0 ? 0 : EFAULT); 15881 } 15882 15883 case DTRACEIOC_AGGSNAP: 15884 case DTRACEIOC_BUFSNAP: { 15885 dtrace_bufdesc_t desc; 15886 caddr_t cached; 15887 dtrace_buffer_t *buf; 15888 15889 if (copyin((void *)arg, &desc, sizeof (desc)) != 0) 15890 return (EFAULT); 15891 15892 if (desc.dtbd_cpu < 0 || desc.dtbd_cpu >= NCPU) 15893 return (EINVAL); 15894 15895 mutex_enter(&dtrace_lock); 15896 15897 if (cmd == DTRACEIOC_BUFSNAP) { 15898 buf = &state->dts_buffer[desc.dtbd_cpu]; 15899 } else { 15900 buf = &state->dts_aggbuffer[desc.dtbd_cpu]; 15901 } 15902 15903 if (buf->dtb_flags & (DTRACEBUF_RING | DTRACEBUF_FILL)) { 15904 size_t sz = buf->dtb_offset; 15905 15906 if (state->dts_activity != DTRACE_ACTIVITY_STOPPED) { 15907 mutex_exit(&dtrace_lock); 15908 return (EBUSY); 15909 } 15910 15911 /* 15912 * If this buffer has already been consumed, we're 15913 * going to indicate that there's nothing left here 15914 * to consume. 15915 */ 15916 if (buf->dtb_flags & DTRACEBUF_CONSUMED) { 15917 mutex_exit(&dtrace_lock); 15918 15919 desc.dtbd_size = 0; 15920 desc.dtbd_drops = 0; 15921 desc.dtbd_errors = 0; 15922 desc.dtbd_oldest = 0; 15923 sz = sizeof (desc); 15924 15925 if (copyout(&desc, (void *)arg, sz) != 0) 15926 return (EFAULT); 15927 15928 return (0); 15929 } 15930 15931 /* 15932 * If this is a ring buffer that has wrapped, we want 15933 * to copy the whole thing out. 15934 */ 15935 if (buf->dtb_flags & DTRACEBUF_WRAPPED) { 15936 dtrace_buffer_polish(buf); 15937 sz = buf->dtb_size; 15938 } 15939 15940 if (copyout(buf->dtb_tomax, desc.dtbd_data, sz) != 0) { 15941 mutex_exit(&dtrace_lock); 15942 return (EFAULT); 15943 } 15944 15945 desc.dtbd_size = sz; 15946 desc.dtbd_drops = buf->dtb_drops; 15947 desc.dtbd_errors = buf->dtb_errors; 15948 desc.dtbd_oldest = buf->dtb_xamot_offset; 15949 15950 mutex_exit(&dtrace_lock); 15951 15952 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 15953 return (EFAULT); 15954 15955 buf->dtb_flags |= DTRACEBUF_CONSUMED; 15956 15957 return (0); 15958 } 15959 15960 if (buf->dtb_tomax == NULL) { 15961 ASSERT(buf->dtb_xamot == NULL); 15962 mutex_exit(&dtrace_lock); 15963 return (ENOENT); 15964 } 15965 15966 cached = buf->dtb_tomax; 15967 ASSERT(!(buf->dtb_flags & DTRACEBUF_NOSWITCH)); 15968 15969 dtrace_xcall(desc.dtbd_cpu, 15970 (dtrace_xcall_t)dtrace_buffer_switch, buf); 15971 15972 state->dts_errors += buf->dtb_xamot_errors; 15973 15974 /* 15975 * If the buffers did not actually switch, then the cross call 15976 * did not take place -- presumably because the given CPU is 15977 * not in the ready set. If this is the case, we'll return 15978 * ENOENT. 15979 */ 15980 if (buf->dtb_tomax == cached) { 15981 ASSERT(buf->dtb_xamot != cached); 15982 mutex_exit(&dtrace_lock); 15983 return (ENOENT); 15984 } 15985 15986 ASSERT(cached == buf->dtb_xamot); 15987 15988 /* 15989 * We have our snapshot; now copy it out. 15990 */ 15991 if (copyout(buf->dtb_xamot, desc.dtbd_data, 15992 buf->dtb_xamot_offset) != 0) { 15993 mutex_exit(&dtrace_lock); 15994 return (EFAULT); 15995 } 15996 15997 desc.dtbd_size = buf->dtb_xamot_offset; 15998 desc.dtbd_drops = buf->dtb_xamot_drops; 15999 desc.dtbd_errors = buf->dtb_xamot_errors; 16000 desc.dtbd_oldest = 0; 16001 16002 mutex_exit(&dtrace_lock); 16003 16004 /* 16005 * Finally, copy out the buffer description. 16006 */ 16007 if (copyout(&desc, (void *)arg, sizeof (desc)) != 0) 16008 return (EFAULT); 16009 16010 return (0); 16011 } 16012 16013 case DTRACEIOC_CONF: { 16014 dtrace_conf_t conf; 16015 16016 bzero(&conf, sizeof (conf)); 16017 conf.dtc_difversion = DIF_VERSION; 16018 conf.dtc_difintregs = DIF_DIR_NREGS; 16019 conf.dtc_diftupregs = DIF_DTR_NREGS; 16020 conf.dtc_ctfmodel = CTF_MODEL_NATIVE; 16021 16022 if (copyout(&conf, (void *)arg, sizeof (conf)) != 0) 16023 return (EFAULT); 16024 16025 return (0); 16026 } 16027 16028 case DTRACEIOC_STATUS: { 16029 dtrace_status_t stat; 16030 dtrace_dstate_t *dstate; 16031 int i, j; 16032 uint64_t nerrs; 16033 16034 /* 16035 * See the comment in dtrace_state_deadman() for the reason 16036 * for setting dts_laststatus to INT64_MAX before setting 16037 * it to the correct value. 16038 */ 16039 state->dts_laststatus = INT64_MAX; 16040 dtrace_membar_producer(); 16041 state->dts_laststatus = dtrace_gethrtime(); 16042 16043 bzero(&stat, sizeof (stat)); 16044 16045 mutex_enter(&dtrace_lock); 16046 16047 if (state->dts_activity == DTRACE_ACTIVITY_INACTIVE) { 16048 mutex_exit(&dtrace_lock); 16049 return (ENOENT); 16050 } 16051 16052 if (state->dts_activity == DTRACE_ACTIVITY_DRAINING) 16053 stat.dtst_exiting = 1; 16054 16055 nerrs = state->dts_errors; 16056 dstate = &state->dts_vstate.dtvs_dynvars; 16057 16058 for (i = 0; i < NCPU; i++) { 16059 dtrace_dstate_percpu_t *dcpu = &dstate->dtds_percpu[i]; 16060 16061 stat.dtst_dyndrops += dcpu->dtdsc_drops; 16062 stat.dtst_dyndrops_dirty += dcpu->dtdsc_dirty_drops; 16063 stat.dtst_dyndrops_rinsing += dcpu->dtdsc_rinsing_drops; 16064 16065 if (state->dts_buffer[i].dtb_flags & DTRACEBUF_FULL) 16066 stat.dtst_filled++; 16067 16068 nerrs += state->dts_buffer[i].dtb_errors; 16069 16070 for (j = 0; j < state->dts_nspeculations; j++) { 16071 dtrace_speculation_t *spec; 16072 dtrace_buffer_t *buf; 16073 16074 spec = &state->dts_speculations[j]; 16075 buf = &spec->dtsp_buffer[i]; 16076 stat.dtst_specdrops += buf->dtb_xamot_drops; 16077 } 16078 } 16079 16080 stat.dtst_specdrops_busy = state->dts_speculations_busy; 16081 stat.dtst_specdrops_unavail = state->dts_speculations_unavail; 16082 stat.dtst_stkstroverflows = state->dts_stkstroverflows; 16083 stat.dtst_dblerrors = state->dts_dblerrors; 16084 stat.dtst_killed = 16085 (state->dts_activity == DTRACE_ACTIVITY_KILLED); 16086 stat.dtst_errors = nerrs; 16087 16088 mutex_exit(&dtrace_lock); 16089 16090 if (copyout(&stat, (void *)arg, sizeof (stat)) != 0) 16091 return (EFAULT); 16092 16093 return (0); 16094 } 16095 16096 case DTRACEIOC_FORMAT: { 16097 dtrace_fmtdesc_t fmt; 16098 char *str; 16099 int len; 16100 16101 if (copyin((void *)arg, &fmt, sizeof (fmt)) != 0) 16102 return (EFAULT); 16103 16104 mutex_enter(&dtrace_lock); 16105 16106 if (fmt.dtfd_format == 0 || 16107 fmt.dtfd_format > state->dts_nformats) { 16108 mutex_exit(&dtrace_lock); 16109 return (EINVAL); 16110 } 16111 16112 /* 16113 * Format strings are allocated contiguously and they are 16114 * never freed; if a format index is less than the number 16115 * of formats, we can assert that the format map is non-NULL 16116 * and that the format for the specified index is non-NULL. 16117 */ 16118 ASSERT(state->dts_formats != NULL); 16119 str = state->dts_formats[fmt.dtfd_format - 1]; 16120 ASSERT(str != NULL); 16121 16122 len = strlen(str) + 1; 16123 16124 if (len > fmt.dtfd_length) { 16125 fmt.dtfd_length = len; 16126 16127 if (copyout(&fmt, (void *)arg, sizeof (fmt)) != 0) { 16128 mutex_exit(&dtrace_lock); 16129 return (EINVAL); 16130 } 16131 } else { 16132 if (copyout(str, fmt.dtfd_string, len) != 0) { 16133 mutex_exit(&dtrace_lock); 16134 return (EINVAL); 16135 } 16136 } 16137 16138 mutex_exit(&dtrace_lock); 16139 return (0); 16140 } 16141 16142 default: 16143 break; 16144 } 16145 16146 return (ENOTTY); 16147} 16148 16149/*ARGSUSED*/ 16150static int 16151dtrace_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 16152{ 16153 dtrace_state_t *state; 16154 16155 switch (cmd) { 16156 case DDI_DETACH: 16157 break; 16158 16159 case DDI_SUSPEND: 16160 return (DDI_SUCCESS); 16161 16162 default: 16163 return (DDI_FAILURE); 16164 } 16165 16166 mutex_enter(&cpu_lock); 16167 mutex_enter(&dtrace_provider_lock); 16168 mutex_enter(&dtrace_lock); 16169 16170 ASSERT(dtrace_opens == 0); 16171 16172 if (dtrace_helpers > 0) { 16173 mutex_exit(&dtrace_provider_lock); 16174 mutex_exit(&dtrace_lock); 16175 mutex_exit(&cpu_lock); 16176 return (DDI_FAILURE); 16177 } 16178 16179 if (dtrace_unregister((dtrace_provider_id_t)dtrace_provider) != 0) { 16180 mutex_exit(&dtrace_provider_lock); 16181 mutex_exit(&dtrace_lock); 16182 mutex_exit(&cpu_lock); 16183 return (DDI_FAILURE); 16184 } 16185 16186 dtrace_provider = NULL; 16187 16188 if ((state = dtrace_anon_grab()) != NULL) { 16189 /* 16190 * If there were ECBs on this state, the provider should 16191 * have not been allowed to detach; assert that there is 16192 * none. 16193 */ 16194 ASSERT(state->dts_necbs == 0); 16195 dtrace_state_destroy(state); 16196 16197 /* 16198 * If we're being detached with anonymous state, we need to 16199 * indicate to the kernel debugger that DTrace is now inactive. 16200 */ 16201 (void) kdi_dtrace_set(KDI_DTSET_DTRACE_DEACTIVATE); 16202 } 16203 16204 bzero(&dtrace_anon, sizeof (dtrace_anon_t)); 16205 unregister_cpu_setup_func((cpu_setup_func_t *)dtrace_cpu_setup, NULL); 16206 dtrace_cpu_init = NULL; 16207 dtrace_helpers_cleanup = NULL; 16208 dtrace_helpers_fork = NULL; 16209 dtrace_cpustart_init = NULL; 16210 dtrace_cpustart_fini = NULL; 16211 dtrace_debugger_init = NULL; 16212 dtrace_debugger_fini = NULL; 16213 dtrace_modload = NULL; 16214 dtrace_modunload = NULL; 16215 16216 mutex_exit(&cpu_lock); 16217 16218 if (dtrace_helptrace_enabled) { 16219 kmem_free(dtrace_helptrace_buffer, dtrace_helptrace_bufsize); 16220 dtrace_helptrace_buffer = NULL; 16221 } 16222 16223 kmem_free(dtrace_probes, dtrace_nprobes * sizeof (dtrace_probe_t *)); 16224 dtrace_probes = NULL; 16225 dtrace_nprobes = 0; 16226 16227 dtrace_hash_destroy(dtrace_bymod); 16228 dtrace_hash_destroy(dtrace_byfunc); 16229 dtrace_hash_destroy(dtrace_byname); 16230 dtrace_bymod = NULL; 16231 dtrace_byfunc = NULL; 16232 dtrace_byname = NULL; 16233 16234 kmem_cache_destroy(dtrace_state_cache); 16235 vmem_destroy(dtrace_minor); 16236 vmem_destroy(dtrace_arena); 16237 16238 if (dtrace_toxrange != NULL) { 16239 kmem_free(dtrace_toxrange, 16240 dtrace_toxranges_max * sizeof (dtrace_toxrange_t)); 16241 dtrace_toxrange = NULL; 16242 dtrace_toxranges = 0; 16243 dtrace_toxranges_max = 0; 16244 } 16245 16246 ddi_remove_minor_node(dtrace_devi, NULL); 16247 dtrace_devi = NULL; 16248 16249 ddi_soft_state_fini(&dtrace_softstate); 16250 16251 ASSERT(dtrace_vtime_references == 0); 16252 ASSERT(dtrace_opens == 0); 16253 ASSERT(dtrace_retained == NULL); 16254 16255 mutex_exit(&dtrace_lock); 16256 mutex_exit(&dtrace_provider_lock); 16257 16258 /* 16259 * We don't destroy the task queue until after we have dropped our 16260 * locks (taskq_destroy() may block on running tasks). To prevent 16261 * attempting to do work after we have effectively detached but before 16262 * the task queue has been destroyed, all tasks dispatched via the 16263 * task queue must check that DTrace is still attached before 16264 * performing any operation. 16265 */ 16266 taskq_destroy(dtrace_taskq); 16267 dtrace_taskq = NULL; 16268 16269 return (DDI_SUCCESS); 16270} 16271#endif 16272 16273#if defined(sun) 16274/*ARGSUSED*/ 16275static int 16276dtrace_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 16277{ 16278 int error; 16279 16280 switch (infocmd) { 16281 case DDI_INFO_DEVT2DEVINFO: 16282 *result = (void *)dtrace_devi; 16283 error = DDI_SUCCESS; 16284 break; 16285 case DDI_INFO_DEVT2INSTANCE: 16286 *result = (void *)0; 16287 error = DDI_SUCCESS; 16288 break; 16289 default: 16290 error = DDI_FAILURE; 16291 } 16292 return (error); 16293} 16294#endif 16295 16296#if defined(sun) 16297static struct cb_ops dtrace_cb_ops = { 16298 dtrace_open, /* open */ 16299 dtrace_close, /* close */ 16300 nulldev, /* strategy */ 16301 nulldev, /* print */ 16302 nodev, /* dump */ 16303 nodev, /* read */ 16304 nodev, /* write */ 16305 dtrace_ioctl, /* ioctl */ 16306 nodev, /* devmap */ 16307 nodev, /* mmap */ 16308 nodev, /* segmap */ 16309 nochpoll, /* poll */ 16310 ddi_prop_op, /* cb_prop_op */ 16311 0, /* streamtab */ 16312 D_NEW | D_MP /* Driver compatibility flag */ 16313}; 16314 16315static struct dev_ops dtrace_ops = { 16316 DEVO_REV, /* devo_rev */ 16317 0, /* refcnt */ 16318 dtrace_info, /* get_dev_info */ 16319 nulldev, /* identify */ 16320 nulldev, /* probe */ 16321 dtrace_attach, /* attach */ 16322 dtrace_detach, /* detach */ 16323 nodev, /* reset */ 16324 &dtrace_cb_ops, /* driver operations */ 16325 NULL, /* bus operations */ 16326 nodev /* dev power */ 16327}; 16328 16329static struct modldrv modldrv = { 16330 &mod_driverops, /* module type (this is a pseudo driver) */ 16331 "Dynamic Tracing", /* name of module */ 16332 &dtrace_ops, /* driver ops */ 16333}; 16334 16335static struct modlinkage modlinkage = { 16336 MODREV_1, 16337 (void *)&modldrv, 16338 NULL 16339}; 16340 16341int 16342_init(void) 16343{ 16344 return (mod_install(&modlinkage)); 16345} 16346 16347int 16348_info(struct modinfo *modinfop) 16349{ 16350 return (mod_info(&modlinkage, modinfop)); 16351} 16352 16353int 16354_fini(void) 16355{ 16356 return (mod_remove(&modlinkage)); 16357} 16358#else 16359 16360static d_ioctl_t dtrace_ioctl; 16361static void dtrace_load(void *); 16362static int dtrace_unload(void); 16363static void dtrace_clone(void *, struct ucred *, char *, int , struct cdev **); 16364static struct clonedevs *dtrace_clones; /* Ptr to the array of cloned devices. */ 16365static eventhandler_tag eh_tag; /* Event handler tag. */ 16366 16367void dtrace_invop_init(void); 16368void dtrace_invop_uninit(void); 16369 16370static struct cdevsw dtrace_cdevsw = { 16371 .d_version = D_VERSION, 16372 .d_close = dtrace_close, 16373 .d_ioctl = dtrace_ioctl, 16374 .d_open = dtrace_open, 16375 .d_name = "dtrace", 16376}; 16377 16378#include <dtrace_anon.c> 16379#include <dtrace_clone.c> 16380#include <dtrace_ioctl.c> 16381#include <dtrace_load.c> 16382#include <dtrace_modevent.c> 16383#include <dtrace_sysctl.c> 16384#include <dtrace_unload.c> 16385#include <dtrace_vtime.c> 16386#include <dtrace_hacks.c> 16387#include <dtrace_isa.c> 16388 16389SYSINIT(dtrace_load, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_load, NULL); 16390SYSUNINIT(dtrace_unload, SI_SUB_DTRACE, SI_ORDER_FIRST, dtrace_unload, NULL); 16391SYSINIT(dtrace_anon_init, SI_SUB_DTRACE_ANON, SI_ORDER_FIRST, dtrace_anon_init, NULL); 16392 16393DEV_MODULE(dtrace, dtrace_modevent, NULL); 16394MODULE_VERSION(dtrace, 1); 16395MODULE_DEPEND(dtrace, cyclic, 1, 1, 1); 16396MODULE_DEPEND(dtrace, opensolaris, 1, 1, 1); 16397#endif 16398